Index: src/untrusted/minidump_generator/build_id.c |
diff --git a/src/untrusted/minidump_generator/build_id.c b/src/untrusted/minidump_generator/build_id.c |
new file mode 100644 |
index 0000000000000000000000000000000000000000..cf7773060154385326c7c0f435cd9a72520f3fc1 |
--- /dev/null |
+++ b/src/untrusted/minidump_generator/build_id.c |
@@ -0,0 +1,51 @@ |
+/* |
+ * Copyright (c) 2013 The Native Client Authors. All rights reserved. |
+ * Use of this source code is governed by a BSD-style license that can be |
+ * found in the LICENSE file. |
+ */ |
+ |
+#include <assert.h> |
+#include <string.h> |
+ |
+#include "native_client/src/include/elf32.h" |
bradn
2013/05/10 18:51:38
link.h ?
Mark Seaborn
2013/05/10 19:29:41
That's a glibc header related to the dynamic linke
|
+ |
+ |
+extern const Elf32_Ehdr __ehdr_start __attribute__((weak)); |
bradn
2013/05/10 18:51:38
Why does this need the weak attribute?
Mark Seaborn
2013/05/10 19:29:41
Comment added (copied from tls.c).
|
+ |
+static uintptr_t note_align(uintptr_t value) { |
+ return (value + 3) & ~3; |
+} |
+ |
+int nacl_get_build_id(const char **data, size_t *size) { |
+ if (&__ehdr_start != NULL && |
+ __ehdr_start.e_ident[EI_CLASS] == ELFCLASS32 && |
bradn
2013/05/10 18:51:38
The deep nesting is kind of undesirable as this is
Mark Seaborn
2013/05/10 19:29:41
It's not really an error, just "not found when sea
|
+ __ehdr_start.e_phentsize == sizeof(Elf32_Phdr)) { |
+ int phnum = __ehdr_start.e_phnum; |
+ uintptr_t start_addr = (uintptr_t) &__ehdr_start; |
+ Elf32_Phdr *phdrs = (Elf32_Phdr *) (start_addr + __ehdr_start.e_phoff); |
bradn
2013/05/10 18:51:38
Wouldn't using using dl_iterate_phdr be preferable
Mark Seaborn
2013/05/10 19:29:41
dl_iterate_phdr() is a glibc interface. We want t
|
+ for (int i = 0; i < phnum; ++i) { |
+ Elf32_Phdr *phdr = &phdrs[i]; |
+ if (phdr->p_type == PT_NOTE) { |
+ Elf32_Nhdr *note = (Elf32_Nhdr *) (start_addr + phdr->p_offset); |
+ uintptr_t note_end = (uintptr_t) note + phdr->p_memsz; |
+ while ((uintptr_t) note < note_end) { |
+ uintptr_t name_ptr = (uintptr_t) ¬e[1]; |
+ assert(name_ptr <= note_end); |
+ uintptr_t desc_ptr = note_align(name_ptr + note->n_namesz); |
+ uintptr_t next_ptr = note_align(desc_ptr + note->n_descsz); |
+ assert(next_ptr <= note_end); |
+ if (note->n_type == NT_GNU_BUILD_ID && |
+ note->n_namesz == sizeof(ELF_NOTE_GNU) && |
+ memcmp((const char *) name_ptr, ELF_NOTE_GNU, |
+ sizeof(ELF_NOTE_GNU)) == 0) { |
+ *data = (const char *) desc_ptr; |
+ *size = note->n_descsz; |
+ return 1; |
+ } |
+ note = (Elf32_Nhdr *) next_ptr; |
+ } |
+ } |
+ } |
+ } |
+ return 0; |
+} |