<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Tue, Feb 14, 2017 at 10:51 AM, Matt Turner <span dir="ltr"><<a href="mailto:mattst88@gmail.com" target="_blank">mattst88@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Provides the ability to read the .note.gnu.build-id section of ELF<br>
binaries, which is inserted by the --build-id=... flag to ld.<br>
---<br>
 <a href="http://configure.ac" rel="noreferrer" target="_blank">configure.ac</a>              |   2 +<br>
 src/util/Makefile.sources |   2 +<br>
 src/util/build_id.c       | 109 ++++++++++++++++++++++++++++++<wbr>++++++++++++++++<br>
 src/util/build_id.h       |  56 ++++++++++++++++++++++++<br>
 4 files changed, 169 insertions(+)<br>
 create mode 100644 src/util/build_id.c<br>
 create mode 100644 src/util/build_id.h<br>
<br>
diff --git a/<a href="http://configure.ac" rel="noreferrer" target="_blank">configure.ac</a> b/<a href="http://configure.ac" rel="noreferrer" target="_blank">configure.ac</a><br>
index f001743..99c74f0 100644<br>
--- a/<a href="http://configure.ac" rel="noreferrer" target="_blank">configure.ac</a><br>
+++ b/<a href="http://configure.ac" rel="noreferrer" target="_blank">configure.ac</a><br>
@@ -768,6 +768,8 @@ LIBS="$LIBS $DLOPEN_LIBS"<br>
 AC_CHECK_FUNCS([dladdr])<br>
 LIBS="$save_LIBS"<br>
<br>
+AC_CHECK_FUNC([dl_iterate_<wbr>phdr], [DEFINES="$DEFINES -DHAVE_DL_ITERATE_PHDR"])<br>
+<br>
 case "$host_os" in<br>
 darwin*)<br>
     ;;<br>
diff --git a/src/util/Makefile.sources b/src/util/Makefile.sources<br>
index a68a5fe..4c12e5f 100644<br>
--- a/src/util/Makefile.sources<br>
+++ b/src/util/Makefile.sources<br>
@@ -2,6 +2,8 @@ MESA_UTIL_FILES :=      \<br>
        bitscan.c \<br>
        bitscan.h \<br>
        bitset.h \<br>
+       build_id.c \<br>
+       build_id.h \<br>
        crc32.c \<br>
        crc32.h \<br>
        debug.c \<br>
diff --git a/src/util/build_id.c b/src/util/build_id.c<br>
new file mode 100644<br>
index 0000000..a2e21b7<br>
--- /dev/null<br>
+++ b/src/util/build_id.c<br>
@@ -0,0 +1,109 @@<br>
+/*<br>
+ * Copyright © 2016 Intel Corporation<br>
+ *<br>
+ * Permission is hereby granted, free of charge, to any person obtaining a<br>
+ * copy of this software and associated documentation files (the "Software"),<br>
+ * to deal in the Software without restriction, including without limitation<br>
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,<br>
+ * and/or sell copies of the Software, and to permit persons to whom the<br>
+ * Software is furnished to do so, subject to the following conditions:<br>
+ *<br>
+ * The above copyright notice and this permission notice (including the next<br>
+ * paragraph) shall be included in all copies or substantial portions of the<br>
+ * Software.<br>
+ *<br>
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR<br>
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,<br>
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL<br>
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER<br>
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING<br>
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS<br>
+ * IN THE SOFTWARE.<br>
+ */<br>
+<br>
+#ifdef HAVE_DL_ITERATE_PHDR<br>
+#include <link.h><br>
+#include <stddef.h><br>
+#include <string.h><br>
+<br>
+#include "build_id.h"<br>
+<br>
+#define ALIGN(val, align)      (((val) + (align) - 1) & ~((align) - 1))<br>
+<br>
+struct note {<br>
+   ElfW(Nhdr) nhdr;<br>
+<br>
+   char name[4];<br>
+   uint8_t build_id[0];<br>
+};<br>
+<br>
+struct callback_data {<br>
+   const char *name;<br>
+   struct note *note;<br>
+};<br>
+<br>
+static int<br>
+build_id_find_nhdr_callback(<wbr>struct dl_phdr_info *info, size_t size, void *data_)<br>
+{<br>
+   struct callback_data *data = data_;<br>
+<br>
+   char *ptr = strstr(info->dlpi_name, data->name);<br>
+   if (ptr == NULL || ptr[strlen(data->name)] != '\0')<br>
+      return 0;<br>
+<br>
+   for (unsigned i = 0; i < info->dlpi_phnum; i++) {<br>
+      if (info->dlpi_phdr[i].p_type != PT_NOTE)<br>
+         continue;<br>
+<br>
+      struct note *note = (void *)(info->dlpi_addr +<br>
+                                   info->dlpi_phdr[i].p_vaddr);<br>
+      ptrdiff_t len = info->dlpi_phdr[i].p_filesz;<br>
+<br>
+      while (len >= sizeof(struct note)) {<br>
+         if (note->nhdr.n_type == NT_GNU_BUILD_ID &&<br>
+            note->nhdr.n_descsz != 0 &&<br>
+            note->nhdr.n_namesz == 4 &&<br>
+            memcmp(note->name, "GNU", 4) == 0) {<br>
+            data->note = note;<br>
+            return 1;<br>
+         }<br>
+<br>
+         size_t offset = sizeof(ElfW(Nhdr)) +<br>
+                         ALIGN(note->nhdr.n_namesz, 4) +<br>
+                         ALIGN(note->nhdr.n_descsz, 4);<br>
+         note = (struct note *)((char *)note + offset);<br>
+         len -= offset;<br>
+      }<br>
+   }<br>
+<br>
+   return 0;<br>
+}<br>
+<br>
+const struct note *<br>
+build_id_find_nhdr(const char *name)<br>
+{<br>
+   struct callback_data data = {<br>
+      .name = name,<br>
+      .note = NULL,<br>
+   };<br>
+<br>
+   if (dl_iterate_phdr(build_id_<wbr>find_nhdr_callback, &data)) {<br>
+      return data.note;<br>
+   } else {<br>
+      return NULL;<br>
+   }<br>
+}<br>
+<br>
+unsigned<br>
+build_id_length(const struct note *note)<br>
+{<br>
+   return note->nhdr.n_descsz;<br>
+}<br>
+<br>
+void<br>
+build_id_read(const struct note *note, unsigned char *build_id)<br>
+{<br>
+   memcpy(build_id, note->build_id, note->nhdr.n_descsz);<br>
+}<br>
+<br>
+#endif<br>
diff --git a/src/util/build_id.h b/src/util/build_id.h<br>
new file mode 100644<br>
index 0000000..0eaecf9<br>
--- /dev/null<br>
+++ b/src/util/build_id.h<br>
@@ -0,0 +1,56 @@<br>
+/*<br>
+ * Copyright © 2016 Intel Corporation<br>
+ *<br>
+ * Permission is hereby granted, free of charge, to any person obtaining a<br>
+ * copy of this software and associated documentation files (the "Software"),<br>
+ * to deal in the Software without restriction, including without limitation<br>
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,<br>
+ * and/or sell copies of the Software, and to permit persons to whom the<br>
+ * Software is furnished to do so, subject to the following conditions:<br>
+ *<br>
+ * The above copyright notice and this permission notice (including the next<br>
+ * paragraph) shall be included in all copies or substantial portions of the<br>
+ * Software.<br>
+ *<br>
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR<br>
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,<br>
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL<br>
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER<br>
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING<br>
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS<br>
+ * IN THE SOFTWARE.<br>
+ */<br>
+<br>
+struct note;<br>
+<br>
+#ifdef HAVE_DL_ITERATE_PHDR<br>
+<br>
+const struct note *<br>
+build_id_find_nhdr(const char *name);<br>
+<br>
+unsigned<br>
+build_id_length(const struct note *note);<br>
+<br>
+void<br>
+build_id_read(const struct note *note, unsigned char *build_id);<br>
+<br>
+#else<br></blockquote><div><br></div><div>I'm not sure how I feel about the silent fall-backs.  At least in the Vulkan driver, we should fail to compile if we can't get build-id.  Otherwise, you'll end up compiling a driver that will always fail device creation.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+<br>
+static inline const struct note *<br>
+build_id_find_nhdr(const char *name)<br>
+{<br>
+   return NULL;<br>
+}<br>
+<br>
+static inline unsigned<br>
+build_id_length(const struct note *note)<br>
+{<br>
+   return 0;<br>
+}<br>
+<br>
+static inline void<br>
+build_id_read(const struct note *note, unsigned char *build_id)<br>
+{<br>
+}<br>
+<br>
+#endif<br>
<span class="HOEnZb"><font color="#888888">--<br>
2.10.2<br>
<br>
______________________________<wbr>_________________<br>
mesa-dev mailing list<br>
<a href="mailto:mesa-dev@lists.freedesktop.org">mesa-dev@lists.freedesktop.org</a><br>
<a href="https://lists.freedesktop.org/mailman/listinfo/mesa-dev" rel="noreferrer" target="_blank">https://lists.freedesktop.org/<wbr>mailman/listinfo/mesa-dev</a><br>
</font></span></blockquote></div><br></div></div>