blob: c6f369b5d893f37adcec240d2b3adb99bfdebb93 [file] [log] [blame]
Arnaldo Carvalho de Melob1d1b092019-08-30 10:26:37 -03001#include "dso.h"
Namhyung Kim393be2e2012-08-06 13:41:21 +09002#include "symbol.h"
Arnaldo Carvalho de Melob1d1b092019-08-30 10:26:37 -03003#include "symsrc.h"
Namhyung Kim393be2e2012-08-06 13:41:21 +09004
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03005#include <errno.h>
Arnaldo Carvalho de Melo68c01882019-01-22 11:14:55 -02006#include <unistd.h>
Namhyung Kimb691f642012-08-06 13:41:22 +09007#include <stdio.h>
8#include <fcntl.h>
9#include <string.h>
Arnaldo Carvalho de Melo215a0d32019-07-04 11:21:24 -030010#include <stdlib.h>
Namhyung Kimb691f642012-08-06 13:41:22 +090011#include <byteswap.h>
12#include <sys/stat.h>
Arnaldo Carvalho de Melo7f7c5362019-07-04 11:32:27 -030013#include <linux/zalloc.h>
Arnaldo Carvalho de Melofb71c86c2019-09-03 10:56:06 -030014#include <internal/lib.h>
Namhyung Kimb691f642012-08-06 13:41:22 +090015
16static bool check_need_swap(int file_endian)
Namhyung Kim393be2e2012-08-06 13:41:21 +090017{
Namhyung Kimb691f642012-08-06 13:41:22 +090018 const int data = 1;
19 u8 *check = (u8 *)&data;
20 int host_endian;
21
22 if (check[0] == 1)
23 host_endian = ELFDATA2LSB;
24 else
25 host_endian = ELFDATA2MSB;
26
27 return host_endian != file_endian;
Namhyung Kim393be2e2012-08-06 13:41:21 +090028}
29
Namhyung Kimb691f642012-08-06 13:41:22 +090030#define NOTE_ALIGN(sz) (((sz) + 3) & ~3)
31
32#define NT_GNU_BUILD_ID 3
33
Jiri Olsaf7668192020-10-13 21:24:34 +020034static int read_build_id(void *note_data, size_t note_len, struct build_id *bid,
35 bool need_swap)
Namhyung Kim393be2e2012-08-06 13:41:21 +090036{
Jiri Olsaf7668192020-10-13 21:24:34 +020037 size_t size = sizeof(bid->data);
Namhyung Kimb691f642012-08-06 13:41:22 +090038 struct {
39 u32 n_namesz;
40 u32 n_descsz;
41 u32 n_type;
42 } *nhdr;
43 void *ptr;
44
45 ptr = note_data;
46 while (ptr < (note_data + note_len)) {
47 const char *name;
48 size_t namesz, descsz;
49
50 nhdr = ptr;
51 if (need_swap) {
52 nhdr->n_namesz = bswap_32(nhdr->n_namesz);
53 nhdr->n_descsz = bswap_32(nhdr->n_descsz);
54 nhdr->n_type = bswap_32(nhdr->n_type);
55 }
56
57 namesz = NOTE_ALIGN(nhdr->n_namesz);
58 descsz = NOTE_ALIGN(nhdr->n_descsz);
59
60 ptr += sizeof(*nhdr);
61 name = ptr;
62 ptr += namesz;
63 if (nhdr->n_type == NT_GNU_BUILD_ID &&
64 nhdr->n_namesz == sizeof("GNU")) {
65 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
66 size_t sz = min(size, descsz);
Jiri Olsaf7668192020-10-13 21:24:34 +020067 memcpy(bid->data, ptr, sz);
68 memset(bid->data + sz, 0, size - sz);
69 bid->size = sz;
Namhyung Kimb691f642012-08-06 13:41:22 +090070 return 0;
71 }
72 }
73 ptr += descsz;
74 }
75
Namhyung Kim393be2e2012-08-06 13:41:21 +090076 return -1;
77}
78
Irina Tirdea1d037ca2012-09-11 01:15:03 +030079int filename__read_debuglink(const char *filename __maybe_unused,
80 char *debuglink __maybe_unused,
81 size_t size __maybe_unused)
Namhyung Kim393be2e2012-08-06 13:41:21 +090082{
83 return -1;
84}
85
Namhyung Kimb691f642012-08-06 13:41:22 +090086/*
87 * Just try PT_NOTE header otherwise fails
88 */
Jiri Olsaf7668192020-10-13 21:24:34 +020089int filename__read_build_id(const char *filename, struct build_id *bid)
Namhyung Kimb691f642012-08-06 13:41:22 +090090{
91 FILE *fp;
92 int ret = -1;
93 bool need_swap = false;
94 u8 e_ident[EI_NIDENT];
95 size_t buf_size;
96 void *buf;
97 int i;
98
99 fp = fopen(filename, "r");
100 if (fp == NULL)
101 return -1;
102
103 if (fread(e_ident, sizeof(e_ident), 1, fp) != 1)
104 goto out;
105
106 if (memcmp(e_ident, ELFMAG, SELFMAG) ||
107 e_ident[EI_VERSION] != EV_CURRENT)
108 goto out;
109
110 need_swap = check_need_swap(e_ident[EI_DATA]);
111
112 /* for simplicity */
113 fseek(fp, 0, SEEK_SET);
114
115 if (e_ident[EI_CLASS] == ELFCLASS32) {
116 Elf32_Ehdr ehdr;
117 Elf32_Phdr *phdr;
118
119 if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1)
120 goto out;
121
122 if (need_swap) {
123 ehdr.e_phoff = bswap_32(ehdr.e_phoff);
124 ehdr.e_phentsize = bswap_16(ehdr.e_phentsize);
125 ehdr.e_phnum = bswap_16(ehdr.e_phnum);
126 }
127
128 buf_size = ehdr.e_phentsize * ehdr.e_phnum;
129 buf = malloc(buf_size);
130 if (buf == NULL)
131 goto out;
132
133 fseek(fp, ehdr.e_phoff, SEEK_SET);
134 if (fread(buf, buf_size, 1, fp) != 1)
135 goto out_free;
136
137 for (i = 0, phdr = buf; i < ehdr.e_phnum; i++, phdr++) {
138 void *tmp;
Mitchell Krome7ad74b42014-12-16 12:16:12 +1000139 long offset;
Namhyung Kimb691f642012-08-06 13:41:22 +0900140
141 if (need_swap) {
142 phdr->p_type = bswap_32(phdr->p_type);
143 phdr->p_offset = bswap_32(phdr->p_offset);
144 phdr->p_filesz = bswap_32(phdr->p_filesz);
145 }
146
147 if (phdr->p_type != PT_NOTE)
148 continue;
149
150 buf_size = phdr->p_filesz;
Mitchell Krome7ad74b42014-12-16 12:16:12 +1000151 offset = phdr->p_offset;
Namhyung Kimb691f642012-08-06 13:41:22 +0900152 tmp = realloc(buf, buf_size);
153 if (tmp == NULL)
154 goto out_free;
155
156 buf = tmp;
Mitchell Krome7ad74b42014-12-16 12:16:12 +1000157 fseek(fp, offset, SEEK_SET);
Namhyung Kimb691f642012-08-06 13:41:22 +0900158 if (fread(buf, buf_size, 1, fp) != 1)
159 goto out_free;
160
Jiri Olsaf7668192020-10-13 21:24:34 +0200161 ret = read_build_id(buf, buf_size, bid, need_swap);
Chengen Dud0acce62023-11-30 21:57:23 +0800162 if (ret == 0) {
Jiri Olsaf7668192020-10-13 21:24:34 +0200163 ret = bid->size;
Chengen Dud0acce62023-11-30 21:57:23 +0800164 break;
165 }
Namhyung Kimb691f642012-08-06 13:41:22 +0900166 }
167 } else {
168 Elf64_Ehdr ehdr;
169 Elf64_Phdr *phdr;
170
171 if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1)
172 goto out;
173
174 if (need_swap) {
175 ehdr.e_phoff = bswap_64(ehdr.e_phoff);
176 ehdr.e_phentsize = bswap_16(ehdr.e_phentsize);
177 ehdr.e_phnum = bswap_16(ehdr.e_phnum);
178 }
179
180 buf_size = ehdr.e_phentsize * ehdr.e_phnum;
181 buf = malloc(buf_size);
182 if (buf == NULL)
183 goto out;
184
185 fseek(fp, ehdr.e_phoff, SEEK_SET);
186 if (fread(buf, buf_size, 1, fp) != 1)
187 goto out_free;
188
189 for (i = 0, phdr = buf; i < ehdr.e_phnum; i++, phdr++) {
190 void *tmp;
Mitchell Krome7ad74b42014-12-16 12:16:12 +1000191 long offset;
Namhyung Kimb691f642012-08-06 13:41:22 +0900192
193 if (need_swap) {
194 phdr->p_type = bswap_32(phdr->p_type);
195 phdr->p_offset = bswap_64(phdr->p_offset);
196 phdr->p_filesz = bswap_64(phdr->p_filesz);
197 }
198
199 if (phdr->p_type != PT_NOTE)
200 continue;
201
202 buf_size = phdr->p_filesz;
Mitchell Krome7ad74b42014-12-16 12:16:12 +1000203 offset = phdr->p_offset;
Namhyung Kimb691f642012-08-06 13:41:22 +0900204 tmp = realloc(buf, buf_size);
205 if (tmp == NULL)
206 goto out_free;
207
208 buf = tmp;
Mitchell Krome7ad74b42014-12-16 12:16:12 +1000209 fseek(fp, offset, SEEK_SET);
Namhyung Kimb691f642012-08-06 13:41:22 +0900210 if (fread(buf, buf_size, 1, fp) != 1)
211 goto out_free;
212
Jiri Olsaf7668192020-10-13 21:24:34 +0200213 ret = read_build_id(buf, buf_size, bid, need_swap);
Chengen Dud0acce62023-11-30 21:57:23 +0800214 if (ret == 0) {
Jiri Olsaf7668192020-10-13 21:24:34 +0200215 ret = bid->size;
Chengen Dud0acce62023-11-30 21:57:23 +0800216 break;
217 }
Namhyung Kimb691f642012-08-06 13:41:22 +0900218 }
219 }
220out_free:
221 free(buf);
222out:
223 fclose(fp);
224 return ret;
225}
226
Jiri Olsa3ff1b8c2020-10-13 21:24:35 +0200227int sysfs__read_build_id(const char *filename, struct build_id *bid)
Namhyung Kimb691f642012-08-06 13:41:22 +0900228{
229 int fd;
230 int ret = -1;
231 struct stat stbuf;
232 size_t buf_size;
233 void *buf;
234
235 fd = open(filename, O_RDONLY);
236 if (fd < 0)
237 return -1;
238
239 if (fstat(fd, &stbuf) < 0)
240 goto out;
241
242 buf_size = stbuf.st_size;
243 buf = malloc(buf_size);
244 if (buf == NULL)
245 goto out;
246
247 if (read(fd, buf, buf_size) != (ssize_t) buf_size)
248 goto out_free;
249
Jiri Olsa3ff1b8c2020-10-13 21:24:35 +0200250 ret = read_build_id(buf, buf_size, bid, false);
Namhyung Kimb691f642012-08-06 13:41:22 +0900251out_free:
252 free(buf);
253out:
254 close(fd);
255 return ret;
256}
257
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -0300258int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700259 enum dso_binary_type type)
260{
261 int fd = open(name, O_RDONLY);
262 if (fd < 0)
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -0300263 goto out_errno;
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700264
265 ss->name = strdup(name);
266 if (!ss->name)
267 goto out_close;
268
Adrian Hunter779e24e2013-12-04 16:23:01 +0200269 ss->fd = fd;
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700270 ss->type = type;
271
272 return 0;
273out_close:
274 close(fd);
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -0300275out_errno:
Ian Rogersee756ef2024-05-04 14:38:01 -0700276 RC_CHK_ACCESS(dso)->load_errno = errno;
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700277 return -1;
278}
279
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300280bool symsrc__possibly_runtime(struct symsrc *ss __maybe_unused)
Cody P Schafer3aafe5a2012-08-10 15:23:02 -0700281{
282 /* Assume all sym sources could be a runtime image. */
283 return true;
284}
285
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300286bool symsrc__has_symtab(struct symsrc *ss __maybe_unused)
Cody P Schaferd26cd122012-08-10 15:23:00 -0700287{
288 return false;
289}
290
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700291void symsrc__destroy(struct symsrc *ss)
292{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300293 zfree(&ss->name);
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700294 close(ss->fd);
295}
296
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300297int dso__synthesize_plt_symbols(struct dso *dso __maybe_unused,
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300298 struct symsrc *ss __maybe_unused)
Namhyung Kim393be2e2012-08-06 13:41:21 +0900299{
300 return 0;
301}
302
Adrian Hunterc6d8f2a2014-07-14 13:02:41 +0300303static int fd__is_64_bit(int fd)
304{
305 u8 e_ident[EI_NIDENT];
306
307 if (lseek(fd, 0, SEEK_SET))
308 return -1;
309
310 if (readn(fd, e_ident, sizeof(e_ident)) != sizeof(e_ident))
311 return -1;
312
313 if (memcmp(e_ident, ELFMAG, SELFMAG) ||
314 e_ident[EI_VERSION] != EV_CURRENT)
315 return -1;
316
317 return e_ident[EI_CLASS] == ELFCLASS64;
318}
319
Adrian Hunter2b5b8bb2014-07-22 16:17:59 +0300320enum dso_type dso__type_fd(int fd)
321{
322 Elf64_Ehdr ehdr;
323 int ret;
324
325 ret = fd__is_64_bit(fd);
326 if (ret < 0)
327 return DSO__TYPE_UNKNOWN;
328
329 if (ret)
330 return DSO__TYPE_64BIT;
331
332 if (readn(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr))
333 return DSO__TYPE_UNKNOWN;
334
335 if (ehdr.e_machine == EM_X86_64)
336 return DSO__TYPE_X32BIT;
337
338 return DSO__TYPE_32BIT;
339}
340
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300341int dso__load_sym(struct dso *dso, struct map *map __maybe_unused,
342 struct symsrc *ss,
343 struct symsrc *runtime_ss __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300344 int kmodule __maybe_unused)
Namhyung Kim393be2e2012-08-06 13:41:21 +0900345{
Jiri Olsaf7668192020-10-13 21:24:34 +0200346 struct build_id bid;
Adrian Hunterc6d8f2a2014-07-14 13:02:41 +0300347 int ret;
348
349 ret = fd__is_64_bit(ss->fd);
350 if (ret >= 0)
Ian Rogersee756ef2024-05-04 14:38:01 -0700351 RC_CHK_ACCESS(dso)->is_64_bit = ret;
Namhyung Kimb691f642012-08-06 13:41:22 +0900352
Jiri Olsaf7668192020-10-13 21:24:34 +0200353 if (filename__read_build_id(ss->name, &bid) > 0)
Jiri Olsa8dfdf442020-10-13 21:24:37 +0200354 dso__set_build_id(dso, &bid);
Namhyung Kim393be2e2012-08-06 13:41:21 +0900355 return 0;
356}
357
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300358int file__read_maps(int fd __maybe_unused, bool exe __maybe_unused,
359 mapfn_t mapfn __maybe_unused, void *data __maybe_unused,
360 bool *is_64_bit __maybe_unused)
361{
362 return -1;
363}
364
Adrian Hunterafba19d2013-10-09 15:01:12 +0300365int kcore_extract__create(struct kcore_extract *kce __maybe_unused)
366{
367 return -1;
368}
369
370void kcore_extract__delete(struct kcore_extract *kce __maybe_unused)
371{
372}
373
Adrian Hunterfc1b6912013-10-14 16:57:29 +0300374int kcore_copy(const char *from_dir __maybe_unused,
375 const char *to_dir __maybe_unused)
376{
377 return -1;
378}
379
Namhyung Kim393be2e2012-08-06 13:41:21 +0900380void symbol__elf_init(void)
381{
382}
Jin Yaoa64489c2017-03-26 04:34:26 +0800383
384char *dso__demangle_sym(struct dso *dso __maybe_unused,
385 int kmodule __maybe_unused,
Milian Wolff80c345b2017-08-06 23:24:34 +0200386 const char *elf_name __maybe_unused)
Jin Yaoa64489c2017-03-26 04:34:26 +0800387{
388 return NULL;
389}
Namhyung Kim06ea72a2022-12-15 11:28:11 -0800390
391bool filename__has_section(const char *filename __maybe_unused, const char *sec __maybe_unused)
392{
393 return false;
394}