Commit | Line | Data |
---|---|---|
e5a1845f NK |
1 | #include <fcntl.h> |
2 | #include <stdio.h> | |
3 | #include <errno.h> | |
4 | #include <string.h> | |
5 | #include <unistd.h> | |
6 | #include <inttypes.h> | |
7 | ||
8 | #include "symbol.h" | |
c506c96b | 9 | #include <symbol/kallsyms.h> |
e5a1845f NK |
10 | #include "debug.h" |
11 | ||
89fe808a | 12 | #ifndef HAVE_ELF_GETPHDRNUM_SUPPORT |
e955d5c4 AH |
13 | static int elf_getphdrnum(Elf *elf, size_t *dst) |
14 | { | |
15 | GElf_Ehdr gehdr; | |
16 | GElf_Ehdr *ehdr; | |
17 | ||
18 | ehdr = gelf_getehdr(elf, &gehdr); | |
19 | if (!ehdr) | |
20 | return -1; | |
21 | ||
22 | *dst = ehdr->e_phnum; | |
23 | ||
24 | return 0; | |
25 | } | |
26 | #endif | |
27 | ||
e5a1845f NK |
28 | #ifndef NT_GNU_BUILD_ID |
29 | #define NT_GNU_BUILD_ID 3 | |
30 | #endif | |
31 | ||
32 | /** | |
33 | * elf_symtab__for_each_symbol - iterate thru all the symbols | |
34 | * | |
35 | * @syms: struct elf_symtab instance to iterate | |
36 | * @idx: uint32_t idx | |
37 | * @sym: GElf_Sym iterator | |
38 | */ | |
39 | #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \ | |
40 | for (idx = 0, gelf_getsym(syms, idx, &sym);\ | |
41 | idx < nr_syms; \ | |
42 | idx++, gelf_getsym(syms, idx, &sym)) | |
43 | ||
44 | static inline uint8_t elf_sym__type(const GElf_Sym *sym) | |
45 | { | |
46 | return GELF_ST_TYPE(sym->st_info); | |
47 | } | |
48 | ||
49 | static inline int elf_sym__is_function(const GElf_Sym *sym) | |
50 | { | |
51 | return elf_sym__type(sym) == STT_FUNC && | |
52 | sym->st_name != 0 && | |
53 | sym->st_shndx != SHN_UNDEF; | |
54 | } | |
55 | ||
56 | static inline bool elf_sym__is_object(const GElf_Sym *sym) | |
57 | { | |
58 | return elf_sym__type(sym) == STT_OBJECT && | |
59 | sym->st_name != 0 && | |
60 | sym->st_shndx != SHN_UNDEF; | |
61 | } | |
62 | ||
63 | static inline int elf_sym__is_label(const GElf_Sym *sym) | |
64 | { | |
65 | return elf_sym__type(sym) == STT_NOTYPE && | |
66 | sym->st_name != 0 && | |
67 | sym->st_shndx != SHN_UNDEF && | |
68 | sym->st_shndx != SHN_ABS; | |
69 | } | |
70 | ||
71 | static bool elf_sym__is_a(GElf_Sym *sym, enum map_type type) | |
72 | { | |
73 | switch (type) { | |
74 | case MAP__FUNCTION: | |
75 | return elf_sym__is_function(sym); | |
76 | case MAP__VARIABLE: | |
77 | return elf_sym__is_object(sym); | |
78 | default: | |
79 | return false; | |
80 | } | |
81 | } | |
82 | ||
83 | static inline const char *elf_sym__name(const GElf_Sym *sym, | |
84 | const Elf_Data *symstrs) | |
85 | { | |
86 | return symstrs->d_buf + sym->st_name; | |
87 | } | |
88 | ||
89 | static inline const char *elf_sec__name(const GElf_Shdr *shdr, | |
90 | const Elf_Data *secstrs) | |
91 | { | |
92 | return secstrs->d_buf + shdr->sh_name; | |
93 | } | |
94 | ||
95 | static inline int elf_sec__is_text(const GElf_Shdr *shdr, | |
96 | const Elf_Data *secstrs) | |
97 | { | |
98 | return strstr(elf_sec__name(shdr, secstrs), "text") != NULL; | |
99 | } | |
100 | ||
101 | static inline bool elf_sec__is_data(const GElf_Shdr *shdr, | |
102 | const Elf_Data *secstrs) | |
103 | { | |
104 | return strstr(elf_sec__name(shdr, secstrs), "data") != NULL; | |
105 | } | |
106 | ||
107 | static bool elf_sec__is_a(GElf_Shdr *shdr, Elf_Data *secstrs, | |
108 | enum map_type type) | |
109 | { | |
110 | switch (type) { | |
111 | case MAP__FUNCTION: | |
112 | return elf_sec__is_text(shdr, secstrs); | |
113 | case MAP__VARIABLE: | |
114 | return elf_sec__is_data(shdr, secstrs); | |
115 | default: | |
116 | return false; | |
117 | } | |
118 | } | |
119 | ||
120 | static size_t elf_addr_to_index(Elf *elf, GElf_Addr addr) | |
121 | { | |
122 | Elf_Scn *sec = NULL; | |
123 | GElf_Shdr shdr; | |
124 | size_t cnt = 1; | |
125 | ||
126 | while ((sec = elf_nextscn(elf, sec)) != NULL) { | |
127 | gelf_getshdr(sec, &shdr); | |
128 | ||
129 | if ((addr >= shdr.sh_addr) && | |
130 | (addr < (shdr.sh_addr + shdr.sh_size))) | |
131 | return cnt; | |
132 | ||
133 | ++cnt; | |
134 | } | |
135 | ||
136 | return -1; | |
137 | } | |
138 | ||
99ca4233 MH |
139 | Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep, |
140 | GElf_Shdr *shp, const char *name, size_t *idx) | |
e5a1845f NK |
141 | { |
142 | Elf_Scn *sec = NULL; | |
143 | size_t cnt = 1; | |
144 | ||
49274654 CS |
145 | /* Elf is corrupted/truncated, avoid calling elf_strptr. */ |
146 | if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL)) | |
147 | return NULL; | |
148 | ||
e5a1845f NK |
149 | while ((sec = elf_nextscn(elf, sec)) != NULL) { |
150 | char *str; | |
151 | ||
152 | gelf_getshdr(sec, shp); | |
153 | str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name); | |
155b3a13 | 154 | if (str && !strcmp(name, str)) { |
e5a1845f NK |
155 | if (idx) |
156 | *idx = cnt; | |
155b3a13 | 157 | return sec; |
e5a1845f NK |
158 | } |
159 | ++cnt; | |
160 | } | |
161 | ||
155b3a13 | 162 | return NULL; |
e5a1845f NK |
163 | } |
164 | ||
165 | #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \ | |
166 | for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \ | |
167 | idx < nr_entries; \ | |
168 | ++idx, pos = gelf_getrel(reldata, idx, &pos_mem)) | |
169 | ||
170 | #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \ | |
171 | for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \ | |
172 | idx < nr_entries; \ | |
173 | ++idx, pos = gelf_getrela(reldata, idx, &pos_mem)) | |
174 | ||
175 | /* | |
176 | * We need to check if we have a .dynsym, so that we can handle the | |
177 | * .plt, synthesizing its symbols, that aren't on the symtabs (be it | |
178 | * .dynsym or .symtab). | |
179 | * And always look at the original dso, not at debuginfo packages, that | |
180 | * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS). | |
181 | */ | |
a44f605b | 182 | int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss, struct map *map, |
e5a1845f NK |
183 | symbol_filter_t filter) |
184 | { | |
185 | uint32_t nr_rel_entries, idx; | |
186 | GElf_Sym sym; | |
187 | u64 plt_offset; | |
188 | GElf_Shdr shdr_plt; | |
189 | struct symbol *f; | |
190 | GElf_Shdr shdr_rel_plt, shdr_dynsym; | |
191 | Elf_Data *reldata, *syms, *symstrs; | |
192 | Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym; | |
193 | size_t dynsym_idx; | |
194 | GElf_Ehdr ehdr; | |
195 | char sympltname[1024]; | |
196 | Elf *elf; | |
a44f605b | 197 | int nr = 0, symidx, err = 0; |
e5a1845f | 198 | |
f47b58b7 DA |
199 | if (!ss->dynsym) |
200 | return 0; | |
201 | ||
a44f605b CS |
202 | elf = ss->elf; |
203 | ehdr = ss->ehdr; | |
e5a1845f | 204 | |
a44f605b CS |
205 | scn_dynsym = ss->dynsym; |
206 | shdr_dynsym = ss->dynshdr; | |
207 | dynsym_idx = ss->dynsym_idx; | |
e5a1845f | 208 | |
e5a1845f NK |
209 | if (scn_dynsym == NULL) |
210 | goto out_elf_end; | |
211 | ||
212 | scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt, | |
213 | ".rela.plt", NULL); | |
214 | if (scn_plt_rel == NULL) { | |
215 | scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt, | |
216 | ".rel.plt", NULL); | |
217 | if (scn_plt_rel == NULL) | |
218 | goto out_elf_end; | |
219 | } | |
220 | ||
221 | err = -1; | |
222 | ||
223 | if (shdr_rel_plt.sh_link != dynsym_idx) | |
224 | goto out_elf_end; | |
225 | ||
226 | if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL) | |
227 | goto out_elf_end; | |
228 | ||
229 | /* | |
230 | * Fetch the relocation section to find the idxes to the GOT | |
231 | * and the symbols in the .dynsym they refer to. | |
232 | */ | |
233 | reldata = elf_getdata(scn_plt_rel, NULL); | |
234 | if (reldata == NULL) | |
235 | goto out_elf_end; | |
236 | ||
237 | syms = elf_getdata(scn_dynsym, NULL); | |
238 | if (syms == NULL) | |
239 | goto out_elf_end; | |
240 | ||
241 | scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link); | |
242 | if (scn_symstrs == NULL) | |
243 | goto out_elf_end; | |
244 | ||
245 | symstrs = elf_getdata(scn_symstrs, NULL); | |
246 | if (symstrs == NULL) | |
247 | goto out_elf_end; | |
248 | ||
52f9ddba CS |
249 | if (symstrs->d_size == 0) |
250 | goto out_elf_end; | |
251 | ||
e5a1845f NK |
252 | nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize; |
253 | plt_offset = shdr_plt.sh_offset; | |
254 | ||
255 | if (shdr_rel_plt.sh_type == SHT_RELA) { | |
256 | GElf_Rela pos_mem, *pos; | |
257 | ||
258 | elf_section__for_each_rela(reldata, pos, pos_mem, idx, | |
259 | nr_rel_entries) { | |
260 | symidx = GELF_R_SYM(pos->r_info); | |
261 | plt_offset += shdr_plt.sh_entsize; | |
262 | gelf_getsym(syms, symidx, &sym); | |
263 | snprintf(sympltname, sizeof(sympltname), | |
264 | "%s@plt", elf_sym__name(&sym, symstrs)); | |
265 | ||
266 | f = symbol__new(plt_offset, shdr_plt.sh_entsize, | |
267 | STB_GLOBAL, sympltname); | |
268 | if (!f) | |
269 | goto out_elf_end; | |
270 | ||
271 | if (filter && filter(map, f)) | |
272 | symbol__delete(f); | |
273 | else { | |
274 | symbols__insert(&dso->symbols[map->type], f); | |
275 | ++nr; | |
276 | } | |
277 | } | |
278 | } else if (shdr_rel_plt.sh_type == SHT_REL) { | |
279 | GElf_Rel pos_mem, *pos; | |
280 | elf_section__for_each_rel(reldata, pos, pos_mem, idx, | |
281 | nr_rel_entries) { | |
282 | symidx = GELF_R_SYM(pos->r_info); | |
283 | plt_offset += shdr_plt.sh_entsize; | |
284 | gelf_getsym(syms, symidx, &sym); | |
285 | snprintf(sympltname, sizeof(sympltname), | |
286 | "%s@plt", elf_sym__name(&sym, symstrs)); | |
287 | ||
288 | f = symbol__new(plt_offset, shdr_plt.sh_entsize, | |
289 | STB_GLOBAL, sympltname); | |
290 | if (!f) | |
291 | goto out_elf_end; | |
292 | ||
293 | if (filter && filter(map, f)) | |
294 | symbol__delete(f); | |
295 | else { | |
296 | symbols__insert(&dso->symbols[map->type], f); | |
297 | ++nr; | |
298 | } | |
299 | } | |
300 | } | |
301 | ||
302 | err = 0; | |
303 | out_elf_end: | |
e5a1845f NK |
304 | if (err == 0) |
305 | return nr; | |
e5a1845f NK |
306 | pr_debug("%s: problems reading %s PLT info.\n", |
307 | __func__, dso->long_name); | |
308 | return 0; | |
309 | } | |
310 | ||
311 | /* | |
312 | * Align offset to 4 bytes as needed for note name and descriptor data. | |
313 | */ | |
314 | #define NOTE_ALIGN(n) (((n) + 3) & -4U) | |
315 | ||
316 | static int elf_read_build_id(Elf *elf, void *bf, size_t size) | |
317 | { | |
318 | int err = -1; | |
319 | GElf_Ehdr ehdr; | |
320 | GElf_Shdr shdr; | |
321 | Elf_Data *data; | |
322 | Elf_Scn *sec; | |
323 | Elf_Kind ek; | |
324 | void *ptr; | |
325 | ||
326 | if (size < BUILD_ID_SIZE) | |
327 | goto out; | |
328 | ||
329 | ek = elf_kind(elf); | |
330 | if (ek != ELF_K_ELF) | |
331 | goto out; | |
332 | ||
333 | if (gelf_getehdr(elf, &ehdr) == NULL) { | |
334 | pr_err("%s: cannot get elf header.\n", __func__); | |
335 | goto out; | |
336 | } | |
337 | ||
338 | /* | |
339 | * Check following sections for notes: | |
340 | * '.note.gnu.build-id' | |
341 | * '.notes' | |
342 | * '.note' (VDSO specific) | |
343 | */ | |
344 | do { | |
345 | sec = elf_section_by_name(elf, &ehdr, &shdr, | |
346 | ".note.gnu.build-id", NULL); | |
347 | if (sec) | |
348 | break; | |
349 | ||
350 | sec = elf_section_by_name(elf, &ehdr, &shdr, | |
351 | ".notes", NULL); | |
352 | if (sec) | |
353 | break; | |
354 | ||
355 | sec = elf_section_by_name(elf, &ehdr, &shdr, | |
356 | ".note", NULL); | |
357 | if (sec) | |
358 | break; | |
359 | ||
360 | return err; | |
361 | ||
362 | } while (0); | |
363 | ||
364 | data = elf_getdata(sec, NULL); | |
365 | if (data == NULL) | |
366 | goto out; | |
367 | ||
368 | ptr = data->d_buf; | |
369 | while (ptr < (data->d_buf + data->d_size)) { | |
370 | GElf_Nhdr *nhdr = ptr; | |
371 | size_t namesz = NOTE_ALIGN(nhdr->n_namesz), | |
372 | descsz = NOTE_ALIGN(nhdr->n_descsz); | |
373 | const char *name; | |
374 | ||
375 | ptr += sizeof(*nhdr); | |
376 | name = ptr; | |
377 | ptr += namesz; | |
378 | if (nhdr->n_type == NT_GNU_BUILD_ID && | |
379 | nhdr->n_namesz == sizeof("GNU")) { | |
380 | if (memcmp(name, "GNU", sizeof("GNU")) == 0) { | |
381 | size_t sz = min(size, descsz); | |
382 | memcpy(bf, ptr, sz); | |
383 | memset(bf + sz, 0, size - sz); | |
384 | err = descsz; | |
385 | break; | |
386 | } | |
387 | } | |
388 | ptr += descsz; | |
389 | } | |
390 | ||
391 | out: | |
392 | return err; | |
393 | } | |
394 | ||
395 | int filename__read_build_id(const char *filename, void *bf, size_t size) | |
396 | { | |
397 | int fd, err = -1; | |
398 | Elf *elf; | |
399 | ||
400 | if (size < BUILD_ID_SIZE) | |
401 | goto out; | |
402 | ||
403 | fd = open(filename, O_RDONLY); | |
404 | if (fd < 0) | |
405 | goto out; | |
406 | ||
407 | elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); | |
408 | if (elf == NULL) { | |
409 | pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename); | |
410 | goto out_close; | |
411 | } | |
412 | ||
413 | err = elf_read_build_id(elf, bf, size); | |
414 | ||
415 | elf_end(elf); | |
416 | out_close: | |
417 | close(fd); | |
418 | out: | |
419 | return err; | |
420 | } | |
421 | ||
422 | int sysfs__read_build_id(const char *filename, void *build_id, size_t size) | |
423 | { | |
424 | int fd, err = -1; | |
425 | ||
426 | if (size < BUILD_ID_SIZE) | |
427 | goto out; | |
428 | ||
429 | fd = open(filename, O_RDONLY); | |
430 | if (fd < 0) | |
431 | goto out; | |
432 | ||
433 | while (1) { | |
434 | char bf[BUFSIZ]; | |
435 | GElf_Nhdr nhdr; | |
436 | size_t namesz, descsz; | |
437 | ||
438 | if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr)) | |
439 | break; | |
440 | ||
441 | namesz = NOTE_ALIGN(nhdr.n_namesz); | |
442 | descsz = NOTE_ALIGN(nhdr.n_descsz); | |
443 | if (nhdr.n_type == NT_GNU_BUILD_ID && | |
444 | nhdr.n_namesz == sizeof("GNU")) { | |
445 | if (read(fd, bf, namesz) != (ssize_t)namesz) | |
446 | break; | |
447 | if (memcmp(bf, "GNU", sizeof("GNU")) == 0) { | |
448 | size_t sz = min(descsz, size); | |
449 | if (read(fd, build_id, sz) == (ssize_t)sz) { | |
450 | memset(build_id + sz, 0, size - sz); | |
451 | err = 0; | |
452 | break; | |
453 | } | |
454 | } else if (read(fd, bf, descsz) != (ssize_t)descsz) | |
455 | break; | |
456 | } else { | |
457 | int n = namesz + descsz; | |
458 | if (read(fd, bf, n) != n) | |
459 | break; | |
460 | } | |
461 | } | |
462 | close(fd); | |
463 | out: | |
464 | return err; | |
465 | } | |
466 | ||
467 | int filename__read_debuglink(const char *filename, char *debuglink, | |
468 | size_t size) | |
469 | { | |
470 | int fd, err = -1; | |
471 | Elf *elf; | |
472 | GElf_Ehdr ehdr; | |
473 | GElf_Shdr shdr; | |
474 | Elf_Data *data; | |
475 | Elf_Scn *sec; | |
476 | Elf_Kind ek; | |
477 | ||
478 | fd = open(filename, O_RDONLY); | |
479 | if (fd < 0) | |
480 | goto out; | |
481 | ||
482 | elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); | |
483 | if (elf == NULL) { | |
484 | pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename); | |
485 | goto out_close; | |
486 | } | |
487 | ||
488 | ek = elf_kind(elf); | |
489 | if (ek != ELF_K_ELF) | |
784f3390 | 490 | goto out_elf_end; |
e5a1845f NK |
491 | |
492 | if (gelf_getehdr(elf, &ehdr) == NULL) { | |
493 | pr_err("%s: cannot get elf header.\n", __func__); | |
784f3390 | 494 | goto out_elf_end; |
e5a1845f NK |
495 | } |
496 | ||
497 | sec = elf_section_by_name(elf, &ehdr, &shdr, | |
498 | ".gnu_debuglink", NULL); | |
499 | if (sec == NULL) | |
784f3390 | 500 | goto out_elf_end; |
e5a1845f NK |
501 | |
502 | data = elf_getdata(sec, NULL); | |
503 | if (data == NULL) | |
784f3390 | 504 | goto out_elf_end; |
e5a1845f NK |
505 | |
506 | /* the start of this section is a zero-terminated string */ | |
507 | strncpy(debuglink, data->d_buf, size); | |
508 | ||
784f3390 | 509 | out_elf_end: |
e5a1845f | 510 | elf_end(elf); |
e5a1845f NK |
511 | out_close: |
512 | close(fd); | |
513 | out: | |
514 | return err; | |
515 | } | |
516 | ||
517 | static int dso__swap_init(struct dso *dso, unsigned char eidata) | |
518 | { | |
519 | static unsigned int const endian = 1; | |
520 | ||
521 | dso->needs_swap = DSO_SWAP__NO; | |
522 | ||
523 | switch (eidata) { | |
524 | case ELFDATA2LSB: | |
525 | /* We are big endian, DSO is little endian. */ | |
526 | if (*(unsigned char const *)&endian != 1) | |
527 | dso->needs_swap = DSO_SWAP__YES; | |
528 | break; | |
529 | ||
530 | case ELFDATA2MSB: | |
531 | /* We are little endian, DSO is big endian. */ | |
532 | if (*(unsigned char const *)&endian != 0) | |
533 | dso->needs_swap = DSO_SWAP__YES; | |
534 | break; | |
535 | ||
536 | default: | |
537 | pr_err("unrecognized DSO data encoding %d\n", eidata); | |
538 | return -EINVAL; | |
539 | } | |
540 | ||
541 | return 0; | |
542 | } | |
543 | ||
3aafe5ae CS |
544 | bool symsrc__possibly_runtime(struct symsrc *ss) |
545 | { | |
546 | return ss->dynsym || ss->opdsec; | |
547 | } | |
548 | ||
d26cd12b CS |
549 | bool symsrc__has_symtab(struct symsrc *ss) |
550 | { | |
551 | return ss->symtab != NULL; | |
552 | } | |
b68e2f91 CS |
553 | |
554 | void symsrc__destroy(struct symsrc *ss) | |
555 | { | |
74cf249d | 556 | zfree(&ss->name); |
b68e2f91 CS |
557 | elf_end(ss->elf); |
558 | close(ss->fd); | |
559 | } | |
560 | ||
561 | int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name, | |
562 | enum dso_binary_type type) | |
e5a1845f | 563 | { |
e5a1845f | 564 | int err = -1; |
e5a1845f | 565 | GElf_Ehdr ehdr; |
e5a1845f | 566 | Elf *elf; |
b68e2f91 CS |
567 | int fd; |
568 | ||
569 | fd = open(name, O_RDONLY); | |
570 | if (fd < 0) | |
571 | return -1; | |
e5a1845f NK |
572 | |
573 | elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); | |
574 | if (elf == NULL) { | |
575 | pr_debug("%s: cannot read %s ELF file.\n", __func__, name); | |
576 | goto out_close; | |
577 | } | |
578 | ||
579 | if (gelf_getehdr(elf, &ehdr) == NULL) { | |
580 | pr_debug("%s: cannot get elf header.\n", __func__); | |
581 | goto out_elf_end; | |
582 | } | |
583 | ||
584 | if (dso__swap_init(dso, ehdr.e_ident[EI_DATA])) | |
585 | goto out_elf_end; | |
586 | ||
587 | /* Always reject images with a mismatched build-id: */ | |
588 | if (dso->has_build_id) { | |
589 | u8 build_id[BUILD_ID_SIZE]; | |
590 | ||
591 | if (elf_read_build_id(elf, build_id, BUILD_ID_SIZE) < 0) | |
592 | goto out_elf_end; | |
593 | ||
594 | if (!dso__build_id_equal(dso, build_id)) | |
595 | goto out_elf_end; | |
596 | } | |
597 | ||
b68e2f91 CS |
598 | ss->symtab = elf_section_by_name(elf, &ehdr, &ss->symshdr, ".symtab", |
599 | NULL); | |
600 | if (ss->symshdr.sh_type != SHT_SYMTAB) | |
601 | ss->symtab = NULL; | |
602 | ||
603 | ss->dynsym_idx = 0; | |
604 | ss->dynsym = elf_section_by_name(elf, &ehdr, &ss->dynshdr, ".dynsym", | |
605 | &ss->dynsym_idx); | |
606 | if (ss->dynshdr.sh_type != SHT_DYNSYM) | |
607 | ss->dynsym = NULL; | |
608 | ||
609 | ss->opdidx = 0; | |
610 | ss->opdsec = elf_section_by_name(elf, &ehdr, &ss->opdshdr, ".opd", | |
611 | &ss->opdidx); | |
612 | if (ss->opdshdr.sh_type != SHT_PROGBITS) | |
613 | ss->opdsec = NULL; | |
614 | ||
615 | if (dso->kernel == DSO_TYPE_USER) { | |
616 | GElf_Shdr shdr; | |
617 | ss->adjust_symbols = (ehdr.e_type == ET_EXEC || | |
0131c4ec | 618 | ehdr.e_type == ET_REL || |
b68e2f91 CS |
619 | elf_section_by_name(elf, &ehdr, &shdr, |
620 | ".gnu.prelink_undo", | |
621 | NULL) != NULL); | |
622 | } else { | |
0131c4ec AH |
623 | ss->adjust_symbols = ehdr.e_type == ET_EXEC || |
624 | ehdr.e_type == ET_REL; | |
b68e2f91 CS |
625 | } |
626 | ||
627 | ss->name = strdup(name); | |
628 | if (!ss->name) | |
629 | goto out_elf_end; | |
630 | ||
631 | ss->elf = elf; | |
632 | ss->fd = fd; | |
633 | ss->ehdr = ehdr; | |
634 | ss->type = type; | |
635 | ||
636 | return 0; | |
637 | ||
638 | out_elf_end: | |
639 | elf_end(elf); | |
640 | out_close: | |
641 | close(fd); | |
642 | return err; | |
643 | } | |
644 | ||
39b12f78 AH |
645 | /** |
646 | * ref_reloc_sym_not_found - has kernel relocation symbol been found. | |
647 | * @kmap: kernel maps and relocation reference symbol | |
648 | * | |
649 | * This function returns %true if we are dealing with the kernel maps and the | |
650 | * relocation reference symbol has not yet been found. Otherwise %false is | |
651 | * returned. | |
652 | */ | |
653 | static bool ref_reloc_sym_not_found(struct kmap *kmap) | |
654 | { | |
655 | return kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name && | |
656 | !kmap->ref_reloc_sym->unrelocated_addr; | |
657 | } | |
658 | ||
659 | /** | |
660 | * ref_reloc - kernel relocation offset. | |
661 | * @kmap: kernel maps and relocation reference symbol | |
662 | * | |
663 | * This function returns the offset of kernel addresses as determined by using | |
664 | * the relocation reference symbol i.e. if the kernel has not been relocated | |
665 | * then the return value is zero. | |
666 | */ | |
667 | static u64 ref_reloc(struct kmap *kmap) | |
668 | { | |
669 | if (kmap && kmap->ref_reloc_sym && | |
670 | kmap->ref_reloc_sym->unrelocated_addr) | |
671 | return kmap->ref_reloc_sym->addr - | |
672 | kmap->ref_reloc_sym->unrelocated_addr; | |
673 | return 0; | |
674 | } | |
675 | ||
261360b6 CS |
676 | int dso__load_sym(struct dso *dso, struct map *map, |
677 | struct symsrc *syms_ss, struct symsrc *runtime_ss, | |
d26cd12b | 678 | symbol_filter_t filter, int kmodule) |
b68e2f91 CS |
679 | { |
680 | struct kmap *kmap = dso->kernel ? map__kmap(map) : NULL; | |
681 | struct map *curr_map = map; | |
682 | struct dso *curr_dso = dso; | |
683 | Elf_Data *symstrs, *secstrs; | |
684 | uint32_t nr_syms; | |
685 | int err = -1; | |
686 | uint32_t idx; | |
687 | GElf_Ehdr ehdr; | |
261360b6 | 688 | GElf_Shdr shdr; |
b68e2f91 CS |
689 | Elf_Data *syms, *opddata = NULL; |
690 | GElf_Sym sym; | |
261360b6 | 691 | Elf_Scn *sec, *sec_strndx; |
b68e2f91 CS |
692 | Elf *elf; |
693 | int nr = 0; | |
39b12f78 | 694 | bool remap_kernel = false, adjust_kernel_syms = false; |
b68e2f91 | 695 | |
261360b6 | 696 | dso->symtab_type = syms_ss->type; |
0131c4ec AH |
697 | dso->rel = syms_ss->ehdr.e_type == ET_REL; |
698 | ||
699 | /* | |
700 | * Modules may already have symbols from kallsyms, but those symbols | |
701 | * have the wrong values for the dso maps, so remove them. | |
702 | */ | |
703 | if (kmodule && syms_ss->symtab) | |
704 | symbols__delete(&dso->symbols[map->type]); | |
005f9294 | 705 | |
261360b6 CS |
706 | if (!syms_ss->symtab) { |
707 | syms_ss->symtab = syms_ss->dynsym; | |
708 | syms_ss->symshdr = syms_ss->dynshdr; | |
d26cd12b CS |
709 | } |
710 | ||
261360b6 CS |
711 | elf = syms_ss->elf; |
712 | ehdr = syms_ss->ehdr; | |
713 | sec = syms_ss->symtab; | |
714 | shdr = syms_ss->symshdr; | |
b68e2f91 | 715 | |
261360b6 CS |
716 | if (runtime_ss->opdsec) |
717 | opddata = elf_rawdata(runtime_ss->opdsec, NULL); | |
e5a1845f NK |
718 | |
719 | syms = elf_getdata(sec, NULL); | |
720 | if (syms == NULL) | |
721 | goto out_elf_end; | |
722 | ||
723 | sec = elf_getscn(elf, shdr.sh_link); | |
724 | if (sec == NULL) | |
725 | goto out_elf_end; | |
726 | ||
727 | symstrs = elf_getdata(sec, NULL); | |
728 | if (symstrs == NULL) | |
729 | goto out_elf_end; | |
730 | ||
731 | sec_strndx = elf_getscn(elf, ehdr.e_shstrndx); | |
732 | if (sec_strndx == NULL) | |
733 | goto out_elf_end; | |
734 | ||
735 | secstrs = elf_getdata(sec_strndx, NULL); | |
736 | if (secstrs == NULL) | |
737 | goto out_elf_end; | |
738 | ||
739 | nr_syms = shdr.sh_size / shdr.sh_entsize; | |
740 | ||
741 | memset(&sym, 0, sizeof(sym)); | |
39b12f78 AH |
742 | |
743 | /* | |
744 | * The kernel relocation symbol is needed in advance in order to adjust | |
745 | * kernel maps correctly. | |
746 | */ | |
747 | if (ref_reloc_sym_not_found(kmap)) { | |
748 | elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) { | |
749 | const char *elf_name = elf_sym__name(&sym, symstrs); | |
750 | ||
751 | if (strcmp(elf_name, kmap->ref_reloc_sym->name)) | |
752 | continue; | |
753 | kmap->ref_reloc_sym->unrelocated_addr = sym.st_value; | |
9176753d AH |
754 | map->reloc = kmap->ref_reloc_sym->addr - |
755 | kmap->ref_reloc_sym->unrelocated_addr; | |
39b12f78 AH |
756 | break; |
757 | } | |
758 | } | |
759 | ||
760 | dso->adjust_symbols = runtime_ss->adjust_symbols || ref_reloc(kmap); | |
761 | /* | |
762 | * Initial kernel and module mappings do not map to the dso. For | |
763 | * function mappings, flag the fixups. | |
764 | */ | |
765 | if (map->type == MAP__FUNCTION && (dso->kernel || kmodule)) { | |
766 | remap_kernel = true; | |
767 | adjust_kernel_syms = dso->adjust_symbols; | |
768 | } | |
e5a1845f NK |
769 | elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) { |
770 | struct symbol *f; | |
771 | const char *elf_name = elf_sym__name(&sym, symstrs); | |
772 | char *demangled = NULL; | |
773 | int is_label = elf_sym__is_label(&sym); | |
774 | const char *section_name; | |
261360b6 | 775 | bool used_opd = false; |
e5a1845f | 776 | |
e5a1845f NK |
777 | if (!is_label && !elf_sym__is_a(&sym, map->type)) |
778 | continue; | |
779 | ||
780 | /* Reject ARM ELF "mapping symbols": these aren't unique and | |
781 | * don't identify functions, so will confuse the profile | |
782 | * output: */ | |
783 | if (ehdr.e_machine == EM_ARM) { | |
784 | if (!strcmp(elf_name, "$a") || | |
785 | !strcmp(elf_name, "$d") || | |
786 | !strcmp(elf_name, "$t")) | |
787 | continue; | |
788 | } | |
789 | ||
261360b6 CS |
790 | if (runtime_ss->opdsec && sym.st_shndx == runtime_ss->opdidx) { |
791 | u32 offset = sym.st_value - syms_ss->opdshdr.sh_addr; | |
e5a1845f NK |
792 | u64 *opd = opddata->d_buf + offset; |
793 | sym.st_value = DSO__SWAP(dso, u64, *opd); | |
261360b6 CS |
794 | sym.st_shndx = elf_addr_to_index(runtime_ss->elf, |
795 | sym.st_value); | |
796 | used_opd = true; | |
e5a1845f | 797 | } |
3843b05d NK |
798 | /* |
799 | * When loading symbols in a data mapping, ABS symbols (which | |
800 | * has a value of SHN_ABS in its st_shndx) failed at | |
801 | * elf_getscn(). And it marks the loading as a failure so | |
802 | * already loaded symbols cannot be fixed up. | |
803 | * | |
804 | * I'm not sure what should be done. Just ignore them for now. | |
805 | * - Namhyung Kim | |
806 | */ | |
807 | if (sym.st_shndx == SHN_ABS) | |
808 | continue; | |
e5a1845f | 809 | |
261360b6 | 810 | sec = elf_getscn(runtime_ss->elf, sym.st_shndx); |
e5a1845f NK |
811 | if (!sec) |
812 | goto out_elf_end; | |
813 | ||
814 | gelf_getshdr(sec, &shdr); | |
815 | ||
816 | if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type)) | |
817 | continue; | |
818 | ||
819 | section_name = elf_sec__name(&shdr, secstrs); | |
820 | ||
821 | /* On ARM, symbols for thumb functions have 1 added to | |
822 | * the symbol address as a flag - remove it */ | |
823 | if ((ehdr.e_machine == EM_ARM) && | |
824 | (map->type == MAP__FUNCTION) && | |
825 | (sym.st_value & 1)) | |
826 | --sym.st_value; | |
827 | ||
39b12f78 | 828 | if (dso->kernel || kmodule) { |
e5a1845f NK |
829 | char dso_name[PATH_MAX]; |
830 | ||
39b12f78 AH |
831 | /* Adjust symbol to map to file offset */ |
832 | if (adjust_kernel_syms) | |
833 | sym.st_value -= shdr.sh_addr - shdr.sh_offset; | |
834 | ||
e5a1845f NK |
835 | if (strcmp(section_name, |
836 | (curr_dso->short_name + | |
837 | dso->short_name_len)) == 0) | |
838 | goto new_symbol; | |
839 | ||
840 | if (strcmp(section_name, ".text") == 0) { | |
39b12f78 AH |
841 | /* |
842 | * The initial kernel mapping is based on | |
843 | * kallsyms and identity maps. Overwrite it to | |
844 | * map to the kernel dso. | |
845 | */ | |
846 | if (remap_kernel && dso->kernel) { | |
847 | remap_kernel = false; | |
848 | map->start = shdr.sh_addr + | |
849 | ref_reloc(kmap); | |
850 | map->end = map->start + shdr.sh_size; | |
851 | map->pgoff = shdr.sh_offset; | |
852 | map->map_ip = map__map_ip; | |
853 | map->unmap_ip = map__unmap_ip; | |
854 | /* Ensure maps are correctly ordered */ | |
855 | map_groups__remove(kmap->kmaps, map); | |
856 | map_groups__insert(kmap->kmaps, map); | |
857 | } | |
858 | ||
0131c4ec AH |
859 | /* |
860 | * The initial module mapping is based on | |
861 | * /proc/modules mapped to offset zero. | |
862 | * Overwrite it to map to the module dso. | |
863 | */ | |
864 | if (remap_kernel && kmodule) { | |
865 | remap_kernel = false; | |
866 | map->pgoff = shdr.sh_offset; | |
867 | } | |
868 | ||
e5a1845f NK |
869 | curr_map = map; |
870 | curr_dso = dso; | |
871 | goto new_symbol; | |
872 | } | |
873 | ||
0131c4ec AH |
874 | if (!kmap) |
875 | goto new_symbol; | |
876 | ||
e5a1845f NK |
877 | snprintf(dso_name, sizeof(dso_name), |
878 | "%s%s", dso->short_name, section_name); | |
879 | ||
880 | curr_map = map_groups__find_by_name(kmap->kmaps, map->type, dso_name); | |
881 | if (curr_map == NULL) { | |
882 | u64 start = sym.st_value; | |
883 | ||
884 | if (kmodule) | |
885 | start += map->start + shdr.sh_offset; | |
886 | ||
887 | curr_dso = dso__new(dso_name); | |
888 | if (curr_dso == NULL) | |
889 | goto out_elf_end; | |
890 | curr_dso->kernel = dso->kernel; | |
891 | curr_dso->long_name = dso->long_name; | |
892 | curr_dso->long_name_len = dso->long_name_len; | |
893 | curr_map = map__new2(start, curr_dso, | |
894 | map->type); | |
895 | if (curr_map == NULL) { | |
896 | dso__delete(curr_dso); | |
897 | goto out_elf_end; | |
898 | } | |
39b12f78 AH |
899 | if (adjust_kernel_syms) { |
900 | curr_map->start = shdr.sh_addr + | |
901 | ref_reloc(kmap); | |
902 | curr_map->end = curr_map->start + | |
903 | shdr.sh_size; | |
904 | curr_map->pgoff = shdr.sh_offset; | |
905 | } else { | |
906 | curr_map->map_ip = identity__map_ip; | |
907 | curr_map->unmap_ip = identity__map_ip; | |
908 | } | |
e5a1845f NK |
909 | curr_dso->symtab_type = dso->symtab_type; |
910 | map_groups__insert(kmap->kmaps, curr_map); | |
911 | dsos__add(&dso->node, curr_dso); | |
912 | dso__set_loaded(curr_dso, map->type); | |
913 | } else | |
914 | curr_dso = curr_map->dso; | |
915 | ||
916 | goto new_symbol; | |
917 | } | |
918 | ||
261360b6 CS |
919 | if ((used_opd && runtime_ss->adjust_symbols) |
920 | || (!used_opd && syms_ss->adjust_symbols)) { | |
e5a1845f NK |
921 | pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64 " " |
922 | "sh_addr: %#" PRIx64 " sh_offset: %#" PRIx64 "\n", __func__, | |
923 | (u64)sym.st_value, (u64)shdr.sh_addr, | |
924 | (u64)shdr.sh_offset); | |
925 | sym.st_value -= shdr.sh_addr - shdr.sh_offset; | |
926 | } | |
950b8354 | 927 | new_symbol: |
e5a1845f NK |
928 | /* |
929 | * We need to figure out if the object was created from C++ sources | |
930 | * DWARF DW_compile_unit has this, but we don't always have access | |
931 | * to it... | |
932 | */ | |
328ccdac | 933 | if (symbol_conf.demangle) { |
14951f22 | 934 | demangled = bfd_demangle(NULL, elf_name, |
328ccdac NK |
935 | DMGL_PARAMS | DMGL_ANSI); |
936 | if (demangled != NULL) | |
937 | elf_name = demangled; | |
938 | } | |
e5a1845f NK |
939 | f = symbol__new(sym.st_value, sym.st_size, |
940 | GELF_ST_BIND(sym.st_info), elf_name); | |
941 | free(demangled); | |
942 | if (!f) | |
943 | goto out_elf_end; | |
944 | ||
945 | if (filter && filter(curr_map, f)) | |
946 | symbol__delete(f); | |
947 | else { | |
948 | symbols__insert(&curr_dso->symbols[curr_map->type], f); | |
949 | nr++; | |
950 | } | |
951 | } | |
952 | ||
953 | /* | |
954 | * For misannotated, zeroed, ASM function sizes. | |
955 | */ | |
956 | if (nr > 0) { | |
957 | symbols__fixup_duplicate(&dso->symbols[map->type]); | |
958 | symbols__fixup_end(&dso->symbols[map->type]); | |
959 | if (kmap) { | |
960 | /* | |
961 | * We need to fixup this here too because we create new | |
962 | * maps here, for things like vsyscall sections. | |
963 | */ | |
964 | __map_groups__fixup_end(kmap->kmaps, map->type); | |
965 | } | |
966 | } | |
967 | err = nr; | |
968 | out_elf_end: | |
e5a1845f NK |
969 | return err; |
970 | } | |
971 | ||
8e0cf965 AH |
972 | static int elf_read_maps(Elf *elf, bool exe, mapfn_t mapfn, void *data) |
973 | { | |
974 | GElf_Phdr phdr; | |
975 | size_t i, phdrnum; | |
976 | int err; | |
977 | u64 sz; | |
978 | ||
979 | if (elf_getphdrnum(elf, &phdrnum)) | |
980 | return -1; | |
981 | ||
982 | for (i = 0; i < phdrnum; i++) { | |
983 | if (gelf_getphdr(elf, i, &phdr) == NULL) | |
984 | return -1; | |
985 | if (phdr.p_type != PT_LOAD) | |
986 | continue; | |
987 | if (exe) { | |
988 | if (!(phdr.p_flags & PF_X)) | |
989 | continue; | |
990 | } else { | |
991 | if (!(phdr.p_flags & PF_R)) | |
992 | continue; | |
993 | } | |
994 | sz = min(phdr.p_memsz, phdr.p_filesz); | |
995 | if (!sz) | |
996 | continue; | |
997 | err = mapfn(phdr.p_vaddr, sz, phdr.p_offset, data); | |
998 | if (err) | |
999 | return err; | |
1000 | } | |
1001 | return 0; | |
1002 | } | |
1003 | ||
1004 | int file__read_maps(int fd, bool exe, mapfn_t mapfn, void *data, | |
1005 | bool *is_64_bit) | |
1006 | { | |
1007 | int err; | |
1008 | Elf *elf; | |
1009 | ||
1010 | elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); | |
1011 | if (elf == NULL) | |
1012 | return -1; | |
1013 | ||
1014 | if (is_64_bit) | |
1015 | *is_64_bit = (gelf_getclass(elf) == ELFCLASS64); | |
1016 | ||
1017 | err = elf_read_maps(elf, exe, mapfn, data); | |
1018 | ||
1019 | elf_end(elf); | |
1020 | return err; | |
1021 | } | |
1022 | ||
afba19d9 AH |
1023 | static int copy_bytes(int from, off_t from_offs, int to, off_t to_offs, u64 len) |
1024 | { | |
1025 | ssize_t r; | |
1026 | size_t n; | |
1027 | int err = -1; | |
1028 | char *buf = malloc(page_size); | |
1029 | ||
1030 | if (buf == NULL) | |
1031 | return -1; | |
1032 | ||
1033 | if (lseek(to, to_offs, SEEK_SET) != to_offs) | |
1034 | goto out; | |
1035 | ||
1036 | if (lseek(from, from_offs, SEEK_SET) != from_offs) | |
1037 | goto out; | |
1038 | ||
1039 | while (len) { | |
1040 | n = page_size; | |
1041 | if (len < n) | |
1042 | n = len; | |
1043 | /* Use read because mmap won't work on proc files */ | |
1044 | r = read(from, buf, n); | |
1045 | if (r < 0) | |
1046 | goto out; | |
1047 | if (!r) | |
1048 | break; | |
1049 | n = r; | |
1050 | r = write(to, buf, n); | |
1051 | if (r < 0) | |
1052 | goto out; | |
1053 | if ((size_t)r != n) | |
1054 | goto out; | |
1055 | len -= n; | |
1056 | } | |
1057 | ||
1058 | err = 0; | |
1059 | out: | |
1060 | free(buf); | |
1061 | return err; | |
1062 | } | |
1063 | ||
1064 | struct kcore { | |
1065 | int fd; | |
1066 | int elfclass; | |
1067 | Elf *elf; | |
1068 | GElf_Ehdr ehdr; | |
1069 | }; | |
1070 | ||
1071 | static int kcore__open(struct kcore *kcore, const char *filename) | |
1072 | { | |
1073 | GElf_Ehdr *ehdr; | |
1074 | ||
1075 | kcore->fd = open(filename, O_RDONLY); | |
1076 | if (kcore->fd == -1) | |
1077 | return -1; | |
1078 | ||
1079 | kcore->elf = elf_begin(kcore->fd, ELF_C_READ, NULL); | |
1080 | if (!kcore->elf) | |
1081 | goto out_close; | |
1082 | ||
1083 | kcore->elfclass = gelf_getclass(kcore->elf); | |
1084 | if (kcore->elfclass == ELFCLASSNONE) | |
1085 | goto out_end; | |
1086 | ||
1087 | ehdr = gelf_getehdr(kcore->elf, &kcore->ehdr); | |
1088 | if (!ehdr) | |
1089 | goto out_end; | |
1090 | ||
1091 | return 0; | |
1092 | ||
1093 | out_end: | |
1094 | elf_end(kcore->elf); | |
1095 | out_close: | |
1096 | close(kcore->fd); | |
1097 | return -1; | |
1098 | } | |
1099 | ||
1100 | static int kcore__init(struct kcore *kcore, char *filename, int elfclass, | |
1101 | bool temp) | |
1102 | { | |
1103 | GElf_Ehdr *ehdr; | |
1104 | ||
1105 | kcore->elfclass = elfclass; | |
1106 | ||
1107 | if (temp) | |
1108 | kcore->fd = mkstemp(filename); | |
1109 | else | |
1110 | kcore->fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0400); | |
1111 | if (kcore->fd == -1) | |
1112 | return -1; | |
1113 | ||
1114 | kcore->elf = elf_begin(kcore->fd, ELF_C_WRITE, NULL); | |
1115 | if (!kcore->elf) | |
1116 | goto out_close; | |
1117 | ||
1118 | if (!gelf_newehdr(kcore->elf, elfclass)) | |
1119 | goto out_end; | |
1120 | ||
1121 | ehdr = gelf_getehdr(kcore->elf, &kcore->ehdr); | |
1122 | if (!ehdr) | |
1123 | goto out_end; | |
1124 | ||
1125 | return 0; | |
1126 | ||
1127 | out_end: | |
1128 | elf_end(kcore->elf); | |
1129 | out_close: | |
1130 | close(kcore->fd); | |
1131 | unlink(filename); | |
1132 | return -1; | |
1133 | } | |
1134 | ||
1135 | static void kcore__close(struct kcore *kcore) | |
1136 | { | |
1137 | elf_end(kcore->elf); | |
1138 | close(kcore->fd); | |
1139 | } | |
1140 | ||
1141 | static int kcore__copy_hdr(struct kcore *from, struct kcore *to, size_t count) | |
1142 | { | |
1143 | GElf_Ehdr *ehdr = &to->ehdr; | |
1144 | GElf_Ehdr *kehdr = &from->ehdr; | |
1145 | ||
1146 | memcpy(ehdr->e_ident, kehdr->e_ident, EI_NIDENT); | |
1147 | ehdr->e_type = kehdr->e_type; | |
1148 | ehdr->e_machine = kehdr->e_machine; | |
1149 | ehdr->e_version = kehdr->e_version; | |
1150 | ehdr->e_entry = 0; | |
1151 | ehdr->e_shoff = 0; | |
1152 | ehdr->e_flags = kehdr->e_flags; | |
1153 | ehdr->e_phnum = count; | |
1154 | ehdr->e_shentsize = 0; | |
1155 | ehdr->e_shnum = 0; | |
1156 | ehdr->e_shstrndx = 0; | |
1157 | ||
1158 | if (from->elfclass == ELFCLASS32) { | |
1159 | ehdr->e_phoff = sizeof(Elf32_Ehdr); | |
1160 | ehdr->e_ehsize = sizeof(Elf32_Ehdr); | |
1161 | ehdr->e_phentsize = sizeof(Elf32_Phdr); | |
1162 | } else { | |
1163 | ehdr->e_phoff = sizeof(Elf64_Ehdr); | |
1164 | ehdr->e_ehsize = sizeof(Elf64_Ehdr); | |
1165 | ehdr->e_phentsize = sizeof(Elf64_Phdr); | |
1166 | } | |
1167 | ||
1168 | if (!gelf_update_ehdr(to->elf, ehdr)) | |
1169 | return -1; | |
1170 | ||
1171 | if (!gelf_newphdr(to->elf, count)) | |
1172 | return -1; | |
1173 | ||
1174 | return 0; | |
1175 | } | |
1176 | ||
1177 | static int kcore__add_phdr(struct kcore *kcore, int idx, off_t offset, | |
1178 | u64 addr, u64 len) | |
1179 | { | |
1180 | GElf_Phdr gphdr; | |
1181 | GElf_Phdr *phdr; | |
1182 | ||
1183 | phdr = gelf_getphdr(kcore->elf, idx, &gphdr); | |
1184 | if (!phdr) | |
1185 | return -1; | |
1186 | ||
1187 | phdr->p_type = PT_LOAD; | |
1188 | phdr->p_flags = PF_R | PF_W | PF_X; | |
1189 | phdr->p_offset = offset; | |
1190 | phdr->p_vaddr = addr; | |
1191 | phdr->p_paddr = 0; | |
1192 | phdr->p_filesz = len; | |
1193 | phdr->p_memsz = len; | |
1194 | phdr->p_align = page_size; | |
1195 | ||
1196 | if (!gelf_update_phdr(kcore->elf, idx, phdr)) | |
1197 | return -1; | |
1198 | ||
1199 | return 0; | |
1200 | } | |
1201 | ||
1202 | static off_t kcore__write(struct kcore *kcore) | |
1203 | { | |
1204 | return elf_update(kcore->elf, ELF_C_WRITE); | |
1205 | } | |
1206 | ||
fc1b691d AH |
1207 | struct phdr_data { |
1208 | off_t offset; | |
1209 | u64 addr; | |
1210 | u64 len; | |
1211 | }; | |
1212 | ||
1213 | struct kcore_copy_info { | |
1214 | u64 stext; | |
1215 | u64 etext; | |
1216 | u64 first_symbol; | |
1217 | u64 last_symbol; | |
1218 | u64 first_module; | |
1219 | u64 last_module_symbol; | |
1220 | struct phdr_data kernel_map; | |
1221 | struct phdr_data modules_map; | |
1222 | }; | |
1223 | ||
1224 | static int kcore_copy__process_kallsyms(void *arg, const char *name, char type, | |
1225 | u64 start) | |
1226 | { | |
1227 | struct kcore_copy_info *kci = arg; | |
1228 | ||
1229 | if (!symbol_type__is_a(type, MAP__FUNCTION)) | |
1230 | return 0; | |
1231 | ||
1232 | if (strchr(name, '[')) { | |
1233 | if (start > kci->last_module_symbol) | |
1234 | kci->last_module_symbol = start; | |
1235 | return 0; | |
1236 | } | |
1237 | ||
1238 | if (!kci->first_symbol || start < kci->first_symbol) | |
1239 | kci->first_symbol = start; | |
1240 | ||
1241 | if (!kci->last_symbol || start > kci->last_symbol) | |
1242 | kci->last_symbol = start; | |
1243 | ||
1244 | if (!strcmp(name, "_stext")) { | |
1245 | kci->stext = start; | |
1246 | return 0; | |
1247 | } | |
1248 | ||
1249 | if (!strcmp(name, "_etext")) { | |
1250 | kci->etext = start; | |
1251 | return 0; | |
1252 | } | |
1253 | ||
1254 | return 0; | |
1255 | } | |
1256 | ||
1257 | static int kcore_copy__parse_kallsyms(struct kcore_copy_info *kci, | |
1258 | const char *dir) | |
1259 | { | |
1260 | char kallsyms_filename[PATH_MAX]; | |
1261 | ||
1262 | scnprintf(kallsyms_filename, PATH_MAX, "%s/kallsyms", dir); | |
1263 | ||
1264 | if (symbol__restricted_filename(kallsyms_filename, "/proc/kallsyms")) | |
1265 | return -1; | |
1266 | ||
1267 | if (kallsyms__parse(kallsyms_filename, kci, | |
1268 | kcore_copy__process_kallsyms) < 0) | |
1269 | return -1; | |
1270 | ||
1271 | return 0; | |
1272 | } | |
1273 | ||
1274 | static int kcore_copy__process_modules(void *arg, | |
1275 | const char *name __maybe_unused, | |
1276 | u64 start) | |
1277 | { | |
1278 | struct kcore_copy_info *kci = arg; | |
1279 | ||
1280 | if (!kci->first_module || start < kci->first_module) | |
1281 | kci->first_module = start; | |
1282 | ||
1283 | return 0; | |
1284 | } | |
1285 | ||
1286 | static int kcore_copy__parse_modules(struct kcore_copy_info *kci, | |
1287 | const char *dir) | |
1288 | { | |
1289 | char modules_filename[PATH_MAX]; | |
1290 | ||
1291 | scnprintf(modules_filename, PATH_MAX, "%s/modules", dir); | |
1292 | ||
1293 | if (symbol__restricted_filename(modules_filename, "/proc/modules")) | |
1294 | return -1; | |
1295 | ||
1296 | if (modules__parse(modules_filename, kci, | |
1297 | kcore_copy__process_modules) < 0) | |
1298 | return -1; | |
1299 | ||
1300 | return 0; | |
1301 | } | |
1302 | ||
1303 | static void kcore_copy__map(struct phdr_data *p, u64 start, u64 end, u64 pgoff, | |
1304 | u64 s, u64 e) | |
1305 | { | |
1306 | if (p->addr || s < start || s >= end) | |
1307 | return; | |
1308 | ||
1309 | p->addr = s; | |
1310 | p->offset = (s - start) + pgoff; | |
1311 | p->len = e < end ? e - s : end - s; | |
1312 | } | |
1313 | ||
1314 | static int kcore_copy__read_map(u64 start, u64 len, u64 pgoff, void *data) | |
1315 | { | |
1316 | struct kcore_copy_info *kci = data; | |
1317 | u64 end = start + len; | |
1318 | ||
1319 | kcore_copy__map(&kci->kernel_map, start, end, pgoff, kci->stext, | |
1320 | kci->etext); | |
1321 | ||
1322 | kcore_copy__map(&kci->modules_map, start, end, pgoff, kci->first_module, | |
1323 | kci->last_module_symbol); | |
1324 | ||
1325 | return 0; | |
1326 | } | |
1327 | ||
1328 | static int kcore_copy__read_maps(struct kcore_copy_info *kci, Elf *elf) | |
1329 | { | |
1330 | if (elf_read_maps(elf, true, kcore_copy__read_map, kci) < 0) | |
1331 | return -1; | |
1332 | ||
1333 | return 0; | |
1334 | } | |
1335 | ||
1336 | static int kcore_copy__calc_maps(struct kcore_copy_info *kci, const char *dir, | |
1337 | Elf *elf) | |
1338 | { | |
1339 | if (kcore_copy__parse_kallsyms(kci, dir)) | |
1340 | return -1; | |
1341 | ||
1342 | if (kcore_copy__parse_modules(kci, dir)) | |
1343 | return -1; | |
1344 | ||
1345 | if (kci->stext) | |
1346 | kci->stext = round_down(kci->stext, page_size); | |
1347 | else | |
1348 | kci->stext = round_down(kci->first_symbol, page_size); | |
1349 | ||
1350 | if (kci->etext) { | |
1351 | kci->etext = round_up(kci->etext, page_size); | |
1352 | } else if (kci->last_symbol) { | |
1353 | kci->etext = round_up(kci->last_symbol, page_size); | |
1354 | kci->etext += page_size; | |
1355 | } | |
1356 | ||
1357 | kci->first_module = round_down(kci->first_module, page_size); | |
1358 | ||
1359 | if (kci->last_module_symbol) { | |
1360 | kci->last_module_symbol = round_up(kci->last_module_symbol, | |
1361 | page_size); | |
1362 | kci->last_module_symbol += page_size; | |
1363 | } | |
1364 | ||
1365 | if (!kci->stext || !kci->etext) | |
1366 | return -1; | |
1367 | ||
1368 | if (kci->first_module && !kci->last_module_symbol) | |
1369 | return -1; | |
1370 | ||
1371 | return kcore_copy__read_maps(kci, elf); | |
1372 | } | |
1373 | ||
1374 | static int kcore_copy__copy_file(const char *from_dir, const char *to_dir, | |
1375 | const char *name) | |
1376 | { | |
1377 | char from_filename[PATH_MAX]; | |
1378 | char to_filename[PATH_MAX]; | |
1379 | ||
1380 | scnprintf(from_filename, PATH_MAX, "%s/%s", from_dir, name); | |
1381 | scnprintf(to_filename, PATH_MAX, "%s/%s", to_dir, name); | |
1382 | ||
1383 | return copyfile_mode(from_filename, to_filename, 0400); | |
1384 | } | |
1385 | ||
1386 | static int kcore_copy__unlink(const char *dir, const char *name) | |
1387 | { | |
1388 | char filename[PATH_MAX]; | |
1389 | ||
1390 | scnprintf(filename, PATH_MAX, "%s/%s", dir, name); | |
1391 | ||
1392 | return unlink(filename); | |
1393 | } | |
1394 | ||
1395 | static int kcore_copy__compare_fds(int from, int to) | |
1396 | { | |
1397 | char *buf_from; | |
1398 | char *buf_to; | |
1399 | ssize_t ret; | |
1400 | size_t len; | |
1401 | int err = -1; | |
1402 | ||
1403 | buf_from = malloc(page_size); | |
1404 | buf_to = malloc(page_size); | |
1405 | if (!buf_from || !buf_to) | |
1406 | goto out; | |
1407 | ||
1408 | while (1) { | |
1409 | /* Use read because mmap won't work on proc files */ | |
1410 | ret = read(from, buf_from, page_size); | |
1411 | if (ret < 0) | |
1412 | goto out; | |
1413 | ||
1414 | if (!ret) | |
1415 | break; | |
1416 | ||
1417 | len = ret; | |
1418 | ||
1419 | if (readn(to, buf_to, len) != (int)len) | |
1420 | goto out; | |
1421 | ||
1422 | if (memcmp(buf_from, buf_to, len)) | |
1423 | goto out; | |
1424 | } | |
1425 | ||
1426 | err = 0; | |
1427 | out: | |
1428 | free(buf_to); | |
1429 | free(buf_from); | |
1430 | return err; | |
1431 | } | |
1432 | ||
1433 | static int kcore_copy__compare_files(const char *from_filename, | |
1434 | const char *to_filename) | |
1435 | { | |
1436 | int from, to, err = -1; | |
1437 | ||
1438 | from = open(from_filename, O_RDONLY); | |
1439 | if (from < 0) | |
1440 | return -1; | |
1441 | ||
1442 | to = open(to_filename, O_RDONLY); | |
1443 | if (to < 0) | |
1444 | goto out_close_from; | |
1445 | ||
1446 | err = kcore_copy__compare_fds(from, to); | |
1447 | ||
1448 | close(to); | |
1449 | out_close_from: | |
1450 | close(from); | |
1451 | return err; | |
1452 | } | |
1453 | ||
1454 | static int kcore_copy__compare_file(const char *from_dir, const char *to_dir, | |
1455 | const char *name) | |
1456 | { | |
1457 | char from_filename[PATH_MAX]; | |
1458 | char to_filename[PATH_MAX]; | |
1459 | ||
1460 | scnprintf(from_filename, PATH_MAX, "%s/%s", from_dir, name); | |
1461 | scnprintf(to_filename, PATH_MAX, "%s/%s", to_dir, name); | |
1462 | ||
1463 | return kcore_copy__compare_files(from_filename, to_filename); | |
1464 | } | |
1465 | ||
1466 | /** | |
1467 | * kcore_copy - copy kallsyms, modules and kcore from one directory to another. | |
1468 | * @from_dir: from directory | |
1469 | * @to_dir: to directory | |
1470 | * | |
1471 | * This function copies kallsyms, modules and kcore files from one directory to | |
1472 | * another. kallsyms and modules are copied entirely. Only code segments are | |
1473 | * copied from kcore. It is assumed that two segments suffice: one for the | |
1474 | * kernel proper and one for all the modules. The code segments are determined | |
1475 | * from kallsyms and modules files. The kernel map starts at _stext or the | |
1476 | * lowest function symbol, and ends at _etext or the highest function symbol. | |
1477 | * The module map starts at the lowest module address and ends at the highest | |
1478 | * module symbol. Start addresses are rounded down to the nearest page. End | |
1479 | * addresses are rounded up to the nearest page. An extra page is added to the | |
1480 | * highest kernel symbol and highest module symbol to, hopefully, encompass that | |
1481 | * symbol too. Because it contains only code sections, the resulting kcore is | |
1482 | * unusual. One significant peculiarity is that the mapping (start -> pgoff) | |
1483 | * is not the same for the kernel map and the modules map. That happens because | |
1484 | * the data is copied adjacently whereas the original kcore has gaps. Finally, | |
1485 | * kallsyms and modules files are compared with their copies to check that | |
1486 | * modules have not been loaded or unloaded while the copies were taking place. | |
1487 | * | |
1488 | * Return: %0 on success, %-1 on failure. | |
1489 | */ | |
1490 | int kcore_copy(const char *from_dir, const char *to_dir) | |
1491 | { | |
1492 | struct kcore kcore; | |
1493 | struct kcore extract; | |
1494 | size_t count = 2; | |
1495 | int idx = 0, err = -1; | |
1496 | off_t offset = page_size, sz, modules_offset = 0; | |
1497 | struct kcore_copy_info kci = { .stext = 0, }; | |
1498 | char kcore_filename[PATH_MAX]; | |
1499 | char extract_filename[PATH_MAX]; | |
1500 | ||
1501 | if (kcore_copy__copy_file(from_dir, to_dir, "kallsyms")) | |
1502 | return -1; | |
1503 | ||
1504 | if (kcore_copy__copy_file(from_dir, to_dir, "modules")) | |
1505 | goto out_unlink_kallsyms; | |
1506 | ||
1507 | scnprintf(kcore_filename, PATH_MAX, "%s/kcore", from_dir); | |
1508 | scnprintf(extract_filename, PATH_MAX, "%s/kcore", to_dir); | |
1509 | ||
1510 | if (kcore__open(&kcore, kcore_filename)) | |
1511 | goto out_unlink_modules; | |
1512 | ||
1513 | if (kcore_copy__calc_maps(&kci, from_dir, kcore.elf)) | |
1514 | goto out_kcore_close; | |
1515 | ||
1516 | if (kcore__init(&extract, extract_filename, kcore.elfclass, false)) | |
1517 | goto out_kcore_close; | |
1518 | ||
1519 | if (!kci.modules_map.addr) | |
1520 | count -= 1; | |
1521 | ||
1522 | if (kcore__copy_hdr(&kcore, &extract, count)) | |
1523 | goto out_extract_close; | |
1524 | ||
1525 | if (kcore__add_phdr(&extract, idx++, offset, kci.kernel_map.addr, | |
1526 | kci.kernel_map.len)) | |
1527 | goto out_extract_close; | |
1528 | ||
1529 | if (kci.modules_map.addr) { | |
1530 | modules_offset = offset + kci.kernel_map.len; | |
1531 | if (kcore__add_phdr(&extract, idx, modules_offset, | |
1532 | kci.modules_map.addr, kci.modules_map.len)) | |
1533 | goto out_extract_close; | |
1534 | } | |
1535 | ||
1536 | sz = kcore__write(&extract); | |
1537 | if (sz < 0 || sz > offset) | |
1538 | goto out_extract_close; | |
1539 | ||
1540 | if (copy_bytes(kcore.fd, kci.kernel_map.offset, extract.fd, offset, | |
1541 | kci.kernel_map.len)) | |
1542 | goto out_extract_close; | |
1543 | ||
1544 | if (modules_offset && copy_bytes(kcore.fd, kci.modules_map.offset, | |
1545 | extract.fd, modules_offset, | |
1546 | kci.modules_map.len)) | |
1547 | goto out_extract_close; | |
1548 | ||
1549 | if (kcore_copy__compare_file(from_dir, to_dir, "modules")) | |
1550 | goto out_extract_close; | |
1551 | ||
1552 | if (kcore_copy__compare_file(from_dir, to_dir, "kallsyms")) | |
1553 | goto out_extract_close; | |
1554 | ||
1555 | err = 0; | |
1556 | ||
1557 | out_extract_close: | |
1558 | kcore__close(&extract); | |
1559 | if (err) | |
1560 | unlink(extract_filename); | |
1561 | out_kcore_close: | |
1562 | kcore__close(&kcore); | |
1563 | out_unlink_modules: | |
1564 | if (err) | |
1565 | kcore_copy__unlink(to_dir, "modules"); | |
1566 | out_unlink_kallsyms: | |
1567 | if (err) | |
1568 | kcore_copy__unlink(to_dir, "kallsyms"); | |
1569 | ||
1570 | return err; | |
1571 | } | |
1572 | ||
afba19d9 AH |
1573 | int kcore_extract__create(struct kcore_extract *kce) |
1574 | { | |
1575 | struct kcore kcore; | |
1576 | struct kcore extract; | |
1577 | size_t count = 1; | |
1578 | int idx = 0, err = -1; | |
1579 | off_t offset = page_size, sz; | |
1580 | ||
1581 | if (kcore__open(&kcore, kce->kcore_filename)) | |
1582 | return -1; | |
1583 | ||
1584 | strcpy(kce->extract_filename, PERF_KCORE_EXTRACT); | |
1585 | if (kcore__init(&extract, kce->extract_filename, kcore.elfclass, true)) | |
1586 | goto out_kcore_close; | |
1587 | ||
1588 | if (kcore__copy_hdr(&kcore, &extract, count)) | |
1589 | goto out_extract_close; | |
1590 | ||
1591 | if (kcore__add_phdr(&extract, idx, offset, kce->addr, kce->len)) | |
1592 | goto out_extract_close; | |
1593 | ||
1594 | sz = kcore__write(&extract); | |
1595 | if (sz < 0 || sz > offset) | |
1596 | goto out_extract_close; | |
1597 | ||
1598 | if (copy_bytes(kcore.fd, kce->offs, extract.fd, offset, kce->len)) | |
1599 | goto out_extract_close; | |
1600 | ||
1601 | err = 0; | |
1602 | ||
1603 | out_extract_close: | |
1604 | kcore__close(&extract); | |
1605 | if (err) | |
1606 | unlink(kce->extract_filename); | |
1607 | out_kcore_close: | |
1608 | kcore__close(&kcore); | |
1609 | ||
1610 | return err; | |
1611 | } | |
1612 | ||
1613 | void kcore_extract__delete(struct kcore_extract *kce) | |
1614 | { | |
1615 | unlink(kce->extract_filename); | |
1616 | } | |
1617 | ||
e5a1845f NK |
1618 | void symbol__elf_init(void) |
1619 | { | |
1620 | elf_version(EV_CURRENT); | |
1621 | } |