1 /* Returns the build id if found in a NT_GNU_BUILD_ID note. 2 Copyright (C) 2014 Red Hat, Inc. 3 This file is part of elfutils. 4 5 This file is free software; you can redistribute it and/or modify 6 it under the terms of either 7 8 * the GNU Lesser General Public License as published by the Free 9 Software Foundation; either version 3 of the License, or (at 10 your option) any later version 11 12 or 13 14 * the GNU General Public License as published by the Free 15 Software Foundation; either version 2 of the License, or (at 16 your option) any later version 17 18 or both in parallel, as here. 19 20 elfutils is distributed in the hope that it will be useful, but 21 WITHOUT ANY WARRANTY; without even the implied warranty of 22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 23 General Public License for more details. 24 25 You should have received copies of the GNU General Public License and 26 the GNU Lesser General Public License along with this program. If 27 not, see <http://www.gnu.org/licenses/>. */ 28 29 #ifdef HAVE_CONFIG_H 30 # include <config.h> 31 #endif 32 33 #include "libdwelfP.h" 34 #include "libdwflP.h" 35 36 #define NO_VADDR ((GElf_Addr) -1l) 37 38 /* Defined here for reuse. The dwelf interface doesn't care about the 39 address of the note, but libdwfl does. */ 40 static int 41 find_elf_build_id (Dwfl_Module *mod, int e_type, Elf *elf, 42 const void **build_id_bits, GElf_Addr *build_id_elfaddr, 43 int *build_id_len) 44 { 45 int check_notes (Elf_Data *data, GElf_Addr data_elfaddr) 46 { 47 size_t pos = 0; 48 GElf_Nhdr nhdr; 49 size_t name_pos; 50 size_t desc_pos; 51 while ((pos = gelf_getnote (data, pos, &nhdr, &name_pos, &desc_pos)) > 0) 52 if (nhdr.n_type == NT_GNU_BUILD_ID 53 && nhdr.n_namesz == sizeof "GNU" && !memcmp (data->d_buf + name_pos, 54 "GNU", sizeof "GNU")) 55 { 56 *build_id_bits = data->d_buf + desc_pos; 57 *build_id_elfaddr = (data_elfaddr == NO_VADDR 58 ? 0 : data_elfaddr + desc_pos); 59 *build_id_len = nhdr.n_descsz; 60 return 1; 61 } 62 return 0; 63 } 64 65 size_t shstrndx = SHN_UNDEF; 66 int result = 0; 67 68 Elf_Scn *scn = elf_nextscn (elf, NULL); 69 70 if (scn == NULL) 71 { 72 /* No sections, have to look for phdrs. */ 73 size_t phnum; 74 if (unlikely (elf_getphdrnum (elf, &phnum) != 0)) 75 { 76 if (mod != NULL) 77 __libdwfl_seterrno (DWFL_E_LIBELF); 78 return -1; 79 } 80 for (size_t i = 0; result == 0 && i < phnum; ++i) 81 { 82 GElf_Phdr phdr_mem; 83 GElf_Phdr *phdr = gelf_getphdr (elf, i, &phdr_mem); 84 if (likely (phdr != NULL) && phdr->p_type == PT_NOTE) 85 result = check_notes (elf_getdata_rawchunk (elf, 86 phdr->p_offset, 87 phdr->p_filesz, 88 ELF_T_NHDR), 89 phdr->p_vaddr); 90 } 91 } 92 else 93 do 94 { 95 GElf_Shdr shdr_mem; 96 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem); 97 if (likely (shdr != NULL) && shdr->sh_type == SHT_NOTE) 98 { 99 /* Determine the right sh_addr in this module. */ 100 GElf_Addr vaddr = 0; 101 if (!(shdr->sh_flags & SHF_ALLOC)) 102 vaddr = NO_VADDR; 103 else if (mod == NULL || e_type != ET_REL) 104 vaddr = shdr->sh_addr; 105 else if (__libdwfl_relocate_value (mod, elf, &shstrndx, 106 elf_ndxscn (scn), &vaddr)) 107 vaddr = NO_VADDR; 108 result = check_notes (elf_getdata (scn, NULL), vaddr); 109 } 110 } 111 while (result == 0 && (scn = elf_nextscn (elf, scn)) != NULL); 112 113 return result; 114 } 115 116 int 117 internal_function 118 __libdwfl_find_elf_build_id (Dwfl_Module *mod, Elf *elf, 119 const void **build_id_bits, 120 GElf_Addr *build_id_elfaddr, int *build_id_len) 121 { 122 GElf_Ehdr ehdr_mem, *ehdr = gelf_getehdr (elf, &ehdr_mem); 123 if (unlikely (ehdr == NULL)) 124 { 125 __libdwfl_seterrno (DWFL_E_LIBELF); 126 return -1; 127 } 128 // MOD->E_TYPE is zero here. 129 assert (ehdr->e_type != ET_REL || mod != NULL); 130 131 return find_elf_build_id (mod, ehdr->e_type, elf, 132 build_id_bits, build_id_elfaddr, build_id_len); 133 } 134 135 ssize_t 136 dwelf_elf_gnu_build_id (Elf *elf, const void **build_idp) 137 { 138 GElf_Addr build_id_elfaddr; 139 int build_id_len; 140 int result = find_elf_build_id (NULL, ET_NONE, elf, build_idp, 141 &build_id_elfaddr, &build_id_len); 142 if (result > 0) 143 return build_id_len; 144 145 return result; 146 } 147 INTDEF(dwelf_elf_gnu_build_id) 148