Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright (c) 2013 The Native Client Authors. All rights reserved. | |
| 3 * Use of this source code is governed by a BSD-style license that can be | |
| 4 * found in the LICENSE file. | |
| 5 */ | |
| 6 | |
| 7 #include <assert.h> | |
| 8 #include <string.h> | |
| 9 | |
| 10 #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
| |
| 11 | |
| 12 | |
| 13 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).
| |
| 14 | |
| 15 static uintptr_t note_align(uintptr_t value) { | |
| 16 return (value + 3) & ~3; | |
| 17 } | |
| 18 | |
| 19 int nacl_get_build_id(const char **data, size_t *size) { | |
| 20 if (&__ehdr_start != NULL && | |
| 21 __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
| |
| 22 __ehdr_start.e_phentsize == sizeof(Elf32_Phdr)) { | |
| 23 int phnum = __ehdr_start.e_phnum; | |
| 24 uintptr_t start_addr = (uintptr_t) &__ehdr_start; | |
| 25 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
| |
| 26 for (int i = 0; i < phnum; ++i) { | |
| 27 Elf32_Phdr *phdr = &phdrs[i]; | |
| 28 if (phdr->p_type == PT_NOTE) { | |
| 29 Elf32_Nhdr *note = (Elf32_Nhdr *) (start_addr + phdr->p_offset); | |
| 30 uintptr_t note_end = (uintptr_t) note + phdr->p_memsz; | |
| 31 while ((uintptr_t) note < note_end) { | |
| 32 uintptr_t name_ptr = (uintptr_t) ¬e[1]; | |
| 33 assert(name_ptr <= note_end); | |
| 34 uintptr_t desc_ptr = note_align(name_ptr + note->n_namesz); | |
| 35 uintptr_t next_ptr = note_align(desc_ptr + note->n_descsz); | |
| 36 assert(next_ptr <= note_end); | |
| 37 if (note->n_type == NT_GNU_BUILD_ID && | |
| 38 note->n_namesz == sizeof(ELF_NOTE_GNU) && | |
| 39 memcmp((const char *) name_ptr, ELF_NOTE_GNU, | |
| 40 sizeof(ELF_NOTE_GNU)) == 0) { | |
| 41 *data = (const char *) desc_ptr; | |
| 42 *size = note->n_descsz; | |
| 43 return 1; | |
| 44 } | |
| 45 note = (Elf32_Nhdr *) next_ptr; | |
| 46 } | |
| 47 } | |
| 48 } | |
| 49 } | |
| 50 return 0; | |
| 51 } | |
| OLD | NEW |