Debian Codegraph @

libdrgn/elf_notes.c

raw
1 // Copyright (c) Meta Platforms, Inc. and affiliates.
2 // SPDX-License-Identifier: LGPL-2.1-or-later
3
4 #include <byteswap.h>
5 #include <string.h>
6
7 #include "elf_notes.h"
8 #include "util.h"
9
10 bool next_elf_note(const void **p, size_t *size, unsigned int align, bool bswap,
11 GElf_Nhdr *nhdr_ret, const char **name_ret,
12 const void **desc_ret)
13 {
14 uint64_t align_mask = align - 1;
15
16 if (*size < sizeof(*nhdr_ret))
17 return false;
18 memcpy(nhdr_ret, *p, sizeof(*nhdr_ret));
19 if (bswap) {
20 nhdr_ret->n_namesz = bswap_32(nhdr_ret->n_namesz);
21 nhdr_ret->n_descsz = bswap_32(nhdr_ret->n_descsz);
22 nhdr_ret->n_type = bswap_32(nhdr_ret->n_type);
23 }
24
25 if (nhdr_ret->n_namesz > *size - sizeof(*nhdr_ret))
26 return false;
27 uint64_t aligned_namesz = (nhdr_ret->n_namesz + align_mask) & ~align_mask;
28 if (nhdr_ret->n_descsz > 0
29 && (aligned_namesz > *size - sizeof(*nhdr_ret)
30 || nhdr_ret->n_descsz > *size - sizeof(*nhdr_ret) - aligned_namesz))
31 return false;
32
33 *p = (const char *)*p + sizeof(*nhdr_ret);
34 *size -= sizeof(*nhdr_ret);
35
36 *name_ret = *p;
37 if (aligned_namesz > *size) {
38 *p = (const char *)*p + *size;
39 *size = 0;
40 } else {
41 *p = (const char *)*p + aligned_namesz;
42 *size -= aligned_namesz;
43 }
44
45 *desc_ret = *p;
46 uint64_t aligned_descsz = (nhdr_ret->n_descsz + align_mask) & ~align_mask;
47 if (aligned_descsz > *size) {
48 *p = (const char *)*p + *size;
49 *size = 0;
50 } else {
51 *p = (const char *)*p + aligned_descsz;
52 *size -= aligned_descsz;
53 }
54
55 return true;
56 }
57
58 int find_elf_note(Elf *elf, const char *name, uint32_t type, const void **ret,
59 size_t *size_ret)
60 {
61 size_t phnum;
62 if (elf_getphdrnum(elf, &phnum))
63 return -1;
64 size_t name_size = strlen(name) + 1;
65 for (size_t i = 0; i < phnum; i++) {
66 GElf_Phdr phdr_mem, *phdr = gelf_getphdr(elf, i, &phdr_mem);
67 if (!phdr)
68 return -1;
69 if (phdr->p_type != PT_NOTE)
70 continue;
71 Elf_Data *data = elf_getdata_rawchunk(elf, phdr->p_offset,
72 phdr->p_filesz,
73 note_header_type(phdr->p_align));
74 if (!data)
75 return -1;
76 GElf_Nhdr nhdr;
77 size_t offset = 0, name_offset, desc_offset;
78 while (offset < data->d_size &&
79 (offset = gelf_getnote(data, offset, &nhdr,
80 &name_offset,
81 &desc_offset))) {
82 const char *note_name = (char *)data->d_buf + name_offset;
83 if (nhdr.n_namesz == name_size
84 && memcmp(note_name, name, name_size) == 0
85 && nhdr.n_type == type) {
86 *ret = (char *)data->d_buf + desc_offset;
87 *size_ret = nhdr.n_descsz;
88 return 0;
89 }
90 }
91 }
92 *ret = NULL;
93 *size_ret = 0;
94 return 0;
95 }
96
97 size_t parse_gnu_build_id_from_notes(const void *buf, size_t size,
98 unsigned int align, bool bswap,
99 const void **ret)
100 {
101 GElf_Nhdr nhdr;
102 const char *name;
103 const void *desc;
104 while (next_elf_note(&buf, &size, align, bswap, &nhdr, &name, &desc)) {
105 if (nhdr.n_namesz == sizeof("GNU")
106 && memcmp(name, "GNU", sizeof("GNU")) == 0
107 && nhdr.n_type == NT_GNU_BUILD_ID
108 && nhdr.n_descsz > 0) {
109 *ret = desc;
110 return nhdr.n_descsz;
111 }
112 }
113 *ret = NULL;
114 return 0;
115 }
116
117 #if _ELFUTILS_PREREQ(0, 175)
118 ssize_t drgn_elf_gnu_build_id(Elf *elf, const void **ret)
119 {
120 GElf_Ehdr ehdr_mem, *ehdr = gelf_getehdr(elf, &ehdr_mem);
121 if (!ehdr)
122 return -1;
123 bool bswap =
124 (ehdr->e_ident[EI_DATA] == ELFDATA2LSB) != HOST_LITTLE_ENDIAN;
125
126 Elf_Data *nhdr8_data = NULL;
127 size_t num_note_sections = 0;
128
129 Elf_Scn *scn = elf_nextscn(elf, NULL);
130 if (scn) {
131 do {
132 GElf_Shdr shdr_mem, *shdr = gelf_getshdr(scn, &shdr_mem);
133 if (!shdr || shdr->sh_type != SHT_NOTE)
134 continue;
135 num_note_sections++;
136
137 Elf_Data *data = elf_rawdata(scn, NULL);
138 if (!data)
139 continue;
140
141 if (data->d_type == ELF_T_NHDR8)
142 nhdr8_data = data;
143
144 unsigned int align = data->d_type == ELF_T_NHDR8 ? 8 : 4;
145 const void *build_id;
146 size_t size = parse_gnu_build_id_from_notes(data->d_buf,
147 data->d_size,
148 align,
149 bswap,
150 &build_id);
151 if (size) {
152 *ret = build_id;
153 return size;
154 }
155 } while ((scn = elf_nextscn(elf, scn)));
156 } else {
157 size_t phnum;
158 if (elf_getphdrnum(elf, &phnum))
159 return -1;
160 for (size_t i = 0; i < phnum; i++) {
161 GElf_Phdr phdr_mem, *phdr =
162 gelf_getphdr(elf, i, &phdr_mem);
163 if (!phdr || phdr->p_type != PT_NOTE)
164 continue;
165 num_note_sections++;
166
167 Elf_Data *data =
168 elf_getdata_rawchunk(elf, phdr->p_offset,
169 phdr->p_filesz,
170 note_header_type(phdr->p_align));
171 if (!data)
172 continue;
173
174 if (data->d_type == ELF_T_NHDR8)
175 nhdr8_data = data;
176
177 unsigned int align = data->d_type == ELF_T_NHDR8 ? 8 : 4;
178 const void *build_id;
179 size_t size = parse_gnu_build_id_from_notes(data->d_buf,
180 data->d_size,
181 align,
182 bswap,
183 &build_id);
184 if (size) {
185 *ret = build_id;
186 return size;
187 }
188 }
189 }
190
191 // Before Linux kernel commit f7ba52f302fd ("vmlinux.lds.h: Discard
192 // .note.gnu.property section") (in v6.4), the vmlinux .notes section
193 // may specify an 8-byte alignment even though it is actually 4-byte
194 // aligned. This fix was backported to several stable and longterm
195 // kernels, but it never made it to the 4.14 and 4.19 longterm branches.
196 // So, we try to work around this bug here.
197 //
198 // If there is only one note section (or segment) and it specifies
199 // 8-byte alignment, then it might be affected by this bug, so we retry
200 // with 4-byte alignment.
201 //
202 // Whenever we drop Linux 4.14 and 4.19 support, we can probably drop
203 // this workaround and just use dwelf_elf_gnu_build_id().
204 if (nhdr8_data && num_note_sections == 1) {
205 return parse_gnu_build_id_from_notes(nhdr8_data->d_buf,
206 nhdr8_data->d_size, 4,
207 bswap, ret);
208 }
209
210 *ret = NULL;
211 return 0;
212 }
213 #endif