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.
5 This file is free software; you can redistribute it and/or modify
6 it under the terms of either
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
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
18 or both in parallel, as here.
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.
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/>. */
33 #include "libdwelfP.h"
36 #define NO_VADDR ((GElf_Addr) -1l)
39 check_notes (Elf_Data *data, GElf_Addr data_elfaddr,
40 const void **build_id_bits, GElf_Addr *build_id_elfaddr,
47 while ((pos = gelf_getnote (data, pos, &nhdr, &name_pos, &desc_pos)) > 0)
48 if (nhdr.n_type == NT_GNU_BUILD_ID
49 && nhdr.n_namesz == sizeof "GNU"
50 && !memcmp (data->d_buf + name_pos, "GNU", sizeof "GNU"))
52 *build_id_bits = data->d_buf + desc_pos;
53 *build_id_elfaddr = (data_elfaddr == NO_VADDR
54 ? 0 : data_elfaddr + desc_pos);
55 *build_id_len = nhdr.n_descsz;
61 /* Defined here for reuse. The dwelf interface doesn't care about the
62 address of the note, but libdwfl does. */
64 find_elf_build_id (Dwfl_Module *mod, int e_type, Elf *elf,
65 const void **build_id_bits, GElf_Addr *build_id_elfaddr,
68 size_t shstrndx = SHN_UNDEF;
71 Elf_Scn *scn = elf_nextscn (elf, NULL);
75 /* No sections, have to look for phdrs. */
77 if (unlikely (elf_getphdrnum (elf, &phnum) != 0))
80 __libdwfl_seterrno (DWFL_E_LIBELF);
83 for (size_t i = 0; result == 0 && i < phnum; ++i)
86 GElf_Phdr *phdr = gelf_getphdr (elf, i, &phdr_mem);
87 if (likely (phdr != NULL) && phdr->p_type == PT_NOTE)
88 result = check_notes (elf_getdata_rawchunk (elf,
102 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
103 if (likely (shdr != NULL) && shdr->sh_type == SHT_NOTE)
105 /* Determine the right sh_addr in this module. */
107 if (!(shdr->sh_flags & SHF_ALLOC))
109 else if (mod == NULL || e_type != ET_REL)
110 vaddr = shdr->sh_addr;
111 else if (__libdwfl_relocate_value (mod, elf, &shstrndx,
112 elf_ndxscn (scn), &vaddr))
114 result = check_notes (elf_getdata (scn, NULL), vaddr,
120 while (result == 0 && (scn = elf_nextscn (elf, scn)) != NULL);
127 __libdwfl_find_elf_build_id (Dwfl_Module *mod, Elf *elf,
128 const void **build_id_bits,
129 GElf_Addr *build_id_elfaddr, int *build_id_len)
131 GElf_Ehdr ehdr_mem, *ehdr = gelf_getehdr (elf, &ehdr_mem);
132 if (unlikely (ehdr == NULL))
134 __libdwfl_seterrno (DWFL_E_LIBELF);
137 // MOD->E_TYPE is zero here.
138 assert (ehdr->e_type != ET_REL || mod != NULL);
140 return find_elf_build_id (mod, ehdr->e_type, elf,
141 build_id_bits, build_id_elfaddr, build_id_len);
145 dwelf_elf_gnu_build_id (Elf *elf, const void **build_idp)
147 GElf_Addr build_id_elfaddr;
149 int result = find_elf_build_id (NULL, ET_NONE, elf, build_idp,
150 &build_id_elfaddr, &build_id_len);
156 INTDEF(dwelf_elf_gnu_build_id)