]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - tools/objtool/elf.c
900771365ae3689fdeb4cdf623a6a99812ed021b
[mirror_ubuntu-hirsute-kernel.git] / tools / objtool / elf.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * elf.c - ELF access library
4 *
5 * Adapted from kpatch (https://github.com/dynup/kpatch):
6 * Copyright (C) 2013-2015 Josh Poimboeuf <jpoimboe@redhat.com>
7 * Copyright (C) 2014 Seth Jennings <sjenning@redhat.com>
8 */
9
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <fcntl.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <unistd.h>
17 #include <errno.h>
18 #include "builtin.h"
19
20 #include "elf.h"
21 #include "warn.h"
22
23 #define MAX_NAME_LEN 128
24
25 struct section *find_section_by_name(struct elf *elf, const char *name)
26 {
27 struct section *sec;
28
29 list_for_each_entry(sec, &elf->sections, list)
30 if (!strcmp(sec->name, name))
31 return sec;
32
33 return NULL;
34 }
35
36 static struct section *find_section_by_index(struct elf *elf,
37 unsigned int idx)
38 {
39 struct section *sec;
40
41 hash_for_each_possible(elf->section_hash, sec, hash, idx)
42 if (sec->idx == idx)
43 return sec;
44
45 return NULL;
46 }
47
48 static struct symbol *find_symbol_by_index(struct elf *elf, unsigned int idx)
49 {
50 struct symbol *sym;
51
52 hash_for_each_possible(elf->symbol_hash, sym, hash, idx)
53 if (sym->idx == idx)
54 return sym;
55
56 return NULL;
57 }
58
59 struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset)
60 {
61 struct symbol *sym;
62
63 list_for_each_entry(sym, &sec->symbol_list, list)
64 if (sym->type != STT_SECTION && sym->offset == offset)
65 return sym;
66
67 return NULL;
68 }
69
70 struct symbol *find_func_by_offset(struct section *sec, unsigned long offset)
71 {
72 struct symbol *sym;
73
74 list_for_each_entry(sym, &sec->symbol_list, list)
75 if (sym->type == STT_FUNC && sym->offset == offset)
76 return sym;
77
78 return NULL;
79 }
80
81 struct symbol *find_symbol_by_name(struct elf *elf, const char *name)
82 {
83 struct section *sec;
84 struct symbol *sym;
85
86 list_for_each_entry(sec, &elf->sections, list)
87 list_for_each_entry(sym, &sec->symbol_list, list)
88 if (!strcmp(sym->name, name))
89 return sym;
90
91 return NULL;
92 }
93
94 struct symbol *find_symbol_containing(struct section *sec, unsigned long offset)
95 {
96 struct symbol *sym;
97
98 list_for_each_entry(sym, &sec->symbol_list, list)
99 if (sym->type != STT_SECTION &&
100 offset >= sym->offset && offset < sym->offset + sym->len)
101 return sym;
102
103 return NULL;
104 }
105
106 struct rela *find_rela_by_dest_range(struct section *sec, unsigned long offset,
107 unsigned int len)
108 {
109 struct rela *rela;
110 unsigned long o;
111
112 if (!sec->rela)
113 return NULL;
114
115 for (o = offset; o < offset + len; o++)
116 hash_for_each_possible(sec->rela->rela_hash, rela, hash, o)
117 if (rela->offset == o)
118 return rela;
119
120 return NULL;
121 }
122
123 struct rela *find_rela_by_dest(struct section *sec, unsigned long offset)
124 {
125 return find_rela_by_dest_range(sec, offset, 1);
126 }
127
128 struct symbol *find_containing_func(struct section *sec, unsigned long offset)
129 {
130 struct symbol *func;
131
132 list_for_each_entry(func, &sec->symbol_list, list)
133 if (func->type == STT_FUNC && offset >= func->offset &&
134 offset < func->offset + func->len)
135 return func;
136
137 return NULL;
138 }
139
140 static int read_sections(struct elf *elf)
141 {
142 Elf_Scn *s = NULL;
143 struct section *sec;
144 size_t shstrndx, sections_nr;
145 int i;
146
147 if (elf_getshdrnum(elf->elf, &sections_nr)) {
148 WARN_ELF("elf_getshdrnum");
149 return -1;
150 }
151
152 if (elf_getshdrstrndx(elf->elf, &shstrndx)) {
153 WARN_ELF("elf_getshdrstrndx");
154 return -1;
155 }
156
157 for (i = 0; i < sections_nr; i++) {
158 sec = malloc(sizeof(*sec));
159 if (!sec) {
160 perror("malloc");
161 return -1;
162 }
163 memset(sec, 0, sizeof(*sec));
164
165 INIT_LIST_HEAD(&sec->symbol_list);
166 INIT_LIST_HEAD(&sec->rela_list);
167 hash_init(sec->rela_hash);
168
169 s = elf_getscn(elf->elf, i);
170 if (!s) {
171 WARN_ELF("elf_getscn");
172 return -1;
173 }
174
175 sec->idx = elf_ndxscn(s);
176
177 if (!gelf_getshdr(s, &sec->sh)) {
178 WARN_ELF("gelf_getshdr");
179 return -1;
180 }
181
182 sec->name = elf_strptr(elf->elf, shstrndx, sec->sh.sh_name);
183 if (!sec->name) {
184 WARN_ELF("elf_strptr");
185 return -1;
186 }
187
188 if (sec->sh.sh_size != 0) {
189 sec->data = elf_getdata(s, NULL);
190 if (!sec->data) {
191 WARN_ELF("elf_getdata");
192 return -1;
193 }
194 if (sec->data->d_off != 0 ||
195 sec->data->d_size != sec->sh.sh_size) {
196 WARN("unexpected data attributes for %s",
197 sec->name);
198 return -1;
199 }
200 }
201 sec->len = sec->sh.sh_size;
202
203 list_add_tail(&sec->list, &elf->sections);
204 hash_add(elf->section_hash, &sec->hash, sec->idx);
205 }
206
207 if (stats)
208 printf("nr_sections: %lu\n", (unsigned long)sections_nr);
209
210 /* sanity check, one more call to elf_nextscn() should return NULL */
211 if (elf_nextscn(elf->elf, s)) {
212 WARN("section entry mismatch");
213 return -1;
214 }
215
216 return 0;
217 }
218
219 static int read_symbols(struct elf *elf)
220 {
221 struct section *symtab, *sec;
222 struct symbol *sym, *pfunc, *alias;
223 struct list_head *entry, *tmp;
224 int symbols_nr, i;
225 char *coldstr;
226
227 symtab = find_section_by_name(elf, ".symtab");
228 if (!symtab) {
229 WARN("missing symbol table");
230 return -1;
231 }
232
233 symbols_nr = symtab->sh.sh_size / symtab->sh.sh_entsize;
234
235 for (i = 0; i < symbols_nr; i++) {
236 sym = malloc(sizeof(*sym));
237 if (!sym) {
238 perror("malloc");
239 return -1;
240 }
241 memset(sym, 0, sizeof(*sym));
242 alias = sym;
243
244 sym->idx = i;
245
246 if (!gelf_getsym(symtab->data, i, &sym->sym)) {
247 WARN_ELF("gelf_getsym");
248 goto err;
249 }
250
251 sym->name = elf_strptr(elf->elf, symtab->sh.sh_link,
252 sym->sym.st_name);
253 if (!sym->name) {
254 WARN_ELF("elf_strptr");
255 goto err;
256 }
257
258 sym->type = GELF_ST_TYPE(sym->sym.st_info);
259 sym->bind = GELF_ST_BIND(sym->sym.st_info);
260
261 if (sym->sym.st_shndx > SHN_UNDEF &&
262 sym->sym.st_shndx < SHN_LORESERVE) {
263 sym->sec = find_section_by_index(elf,
264 sym->sym.st_shndx);
265 if (!sym->sec) {
266 WARN("couldn't find section for symbol %s",
267 sym->name);
268 goto err;
269 }
270 if (sym->type == STT_SECTION) {
271 sym->name = sym->sec->name;
272 sym->sec->sym = sym;
273 }
274 } else
275 sym->sec = find_section_by_index(elf, 0);
276
277 sym->offset = sym->sym.st_value;
278 sym->len = sym->sym.st_size;
279
280 /* sorted insert into a per-section list */
281 entry = &sym->sec->symbol_list;
282 list_for_each_prev(tmp, &sym->sec->symbol_list) {
283 struct symbol *s;
284
285 s = list_entry(tmp, struct symbol, list);
286
287 if (sym->offset > s->offset) {
288 entry = tmp;
289 break;
290 }
291
292 if (sym->offset == s->offset) {
293 if (sym->len && sym->len == s->len && alias == sym)
294 alias = s;
295
296 if (sym->len >= s->len) {
297 entry = tmp;
298 break;
299 }
300 }
301 }
302 sym->alias = alias;
303 list_add(&sym->list, entry);
304 hash_add(elf->symbol_hash, &sym->hash, sym->idx);
305 }
306
307 if (stats)
308 printf("nr_symbols: %lu\n", (unsigned long)symbols_nr);
309
310 /* Create parent/child links for any cold subfunctions */
311 list_for_each_entry(sec, &elf->sections, list) {
312 list_for_each_entry(sym, &sec->symbol_list, list) {
313 char pname[MAX_NAME_LEN + 1];
314 size_t pnamelen;
315 if (sym->type != STT_FUNC)
316 continue;
317 sym->pfunc = sym->cfunc = sym;
318 coldstr = strstr(sym->name, ".cold");
319 if (!coldstr)
320 continue;
321
322 pnamelen = coldstr - sym->name;
323 if (pnamelen > MAX_NAME_LEN) {
324 WARN("%s(): parent function name exceeds maximum length of %d characters",
325 sym->name, MAX_NAME_LEN);
326 return -1;
327 }
328
329 strncpy(pname, sym->name, pnamelen);
330 pname[pnamelen] = '\0';
331 pfunc = find_symbol_by_name(elf, pname);
332
333 if (!pfunc) {
334 WARN("%s(): can't find parent function",
335 sym->name);
336 return -1;
337 }
338
339 sym->pfunc = pfunc;
340 pfunc->cfunc = sym;
341
342 /*
343 * Unfortunately, -fnoreorder-functions puts the child
344 * inside the parent. Remove the overlap so we can
345 * have sane assumptions.
346 *
347 * Note that pfunc->len now no longer matches
348 * pfunc->sym.st_size.
349 */
350 if (sym->sec == pfunc->sec &&
351 sym->offset >= pfunc->offset &&
352 sym->offset + sym->len == pfunc->offset + pfunc->len) {
353 pfunc->len -= sym->len;
354 }
355 }
356 }
357
358 return 0;
359
360 err:
361 free(sym);
362 return -1;
363 }
364
365 static int read_relas(struct elf *elf)
366 {
367 struct section *sec;
368 struct rela *rela;
369 int i;
370 unsigned int symndx;
371 unsigned long nr_rela, max_rela = 0, tot_rela = 0;
372
373 list_for_each_entry(sec, &elf->sections, list) {
374 if (sec->sh.sh_type != SHT_RELA)
375 continue;
376
377 sec->base = find_section_by_name(elf, sec->name + 5);
378 if (!sec->base) {
379 WARN("can't find base section for rela section %s",
380 sec->name);
381 return -1;
382 }
383
384 sec->base->rela = sec;
385
386 nr_rela = 0;
387 for (i = 0; i < sec->sh.sh_size / sec->sh.sh_entsize; i++) {
388 rela = malloc(sizeof(*rela));
389 if (!rela) {
390 perror("malloc");
391 return -1;
392 }
393 memset(rela, 0, sizeof(*rela));
394
395 if (!gelf_getrela(sec->data, i, &rela->rela)) {
396 WARN_ELF("gelf_getrela");
397 return -1;
398 }
399
400 rela->type = GELF_R_TYPE(rela->rela.r_info);
401 rela->addend = rela->rela.r_addend;
402 rela->offset = rela->rela.r_offset;
403 symndx = GELF_R_SYM(rela->rela.r_info);
404 rela->sym = find_symbol_by_index(elf, symndx);
405 rela->sec = sec;
406 if (!rela->sym) {
407 WARN("can't find rela entry symbol %d for %s",
408 symndx, sec->name);
409 return -1;
410 }
411
412 list_add_tail(&rela->list, &sec->rela_list);
413 hash_add(sec->rela_hash, &rela->hash, rela->offset);
414 nr_rela++;
415 }
416 max_rela = max(max_rela, nr_rela);
417 tot_rela += nr_rela;
418 }
419
420 if (stats) {
421 printf("max_rela: %lu\n", max_rela);
422 printf("tot_rela: %lu\n", tot_rela);
423 }
424
425 return 0;
426 }
427
428 struct elf *elf_read(const char *name, int flags)
429 {
430 struct elf *elf;
431 Elf_Cmd cmd;
432
433 elf_version(EV_CURRENT);
434
435 elf = malloc(sizeof(*elf));
436 if (!elf) {
437 perror("malloc");
438 return NULL;
439 }
440 memset(elf, 0, sizeof(*elf));
441
442 hash_init(elf->symbol_hash);
443 hash_init(elf->section_hash);
444 INIT_LIST_HEAD(&elf->sections);
445
446 elf->fd = open(name, flags);
447 if (elf->fd == -1) {
448 fprintf(stderr, "objtool: Can't open '%s': %s\n",
449 name, strerror(errno));
450 goto err;
451 }
452
453 if ((flags & O_ACCMODE) == O_RDONLY)
454 cmd = ELF_C_READ_MMAP;
455 else if ((flags & O_ACCMODE) == O_RDWR)
456 cmd = ELF_C_RDWR;
457 else /* O_WRONLY */
458 cmd = ELF_C_WRITE;
459
460 elf->elf = elf_begin(elf->fd, cmd, NULL);
461 if (!elf->elf) {
462 WARN_ELF("elf_begin");
463 goto err;
464 }
465
466 if (!gelf_getehdr(elf->elf, &elf->ehdr)) {
467 WARN_ELF("gelf_getehdr");
468 goto err;
469 }
470
471 if (read_sections(elf))
472 goto err;
473
474 if (read_symbols(elf))
475 goto err;
476
477 if (read_relas(elf))
478 goto err;
479
480 return elf;
481
482 err:
483 elf_close(elf);
484 return NULL;
485 }
486
487 struct section *elf_create_section(struct elf *elf, const char *name,
488 size_t entsize, int nr)
489 {
490 struct section *sec, *shstrtab;
491 size_t size = entsize * nr;
492 Elf_Scn *s;
493 Elf_Data *data;
494
495 sec = malloc(sizeof(*sec));
496 if (!sec) {
497 perror("malloc");
498 return NULL;
499 }
500 memset(sec, 0, sizeof(*sec));
501
502 INIT_LIST_HEAD(&sec->symbol_list);
503 INIT_LIST_HEAD(&sec->rela_list);
504 hash_init(sec->rela_hash);
505
506 s = elf_newscn(elf->elf);
507 if (!s) {
508 WARN_ELF("elf_newscn");
509 return NULL;
510 }
511
512 sec->name = strdup(name);
513 if (!sec->name) {
514 perror("strdup");
515 return NULL;
516 }
517
518 sec->idx = elf_ndxscn(s);
519 sec->len = size;
520 sec->changed = true;
521
522 sec->data = elf_newdata(s);
523 if (!sec->data) {
524 WARN_ELF("elf_newdata");
525 return NULL;
526 }
527
528 sec->data->d_size = size;
529 sec->data->d_align = 1;
530
531 if (size) {
532 sec->data->d_buf = malloc(size);
533 if (!sec->data->d_buf) {
534 perror("malloc");
535 return NULL;
536 }
537 memset(sec->data->d_buf, 0, size);
538 }
539
540 if (!gelf_getshdr(s, &sec->sh)) {
541 WARN_ELF("gelf_getshdr");
542 return NULL;
543 }
544
545 sec->sh.sh_size = size;
546 sec->sh.sh_entsize = entsize;
547 sec->sh.sh_type = SHT_PROGBITS;
548 sec->sh.sh_addralign = 1;
549 sec->sh.sh_flags = SHF_ALLOC;
550
551
552 /* Add section name to .shstrtab (or .strtab for Clang) */
553 shstrtab = find_section_by_name(elf, ".shstrtab");
554 if (!shstrtab)
555 shstrtab = find_section_by_name(elf, ".strtab");
556 if (!shstrtab) {
557 WARN("can't find .shstrtab or .strtab section");
558 return NULL;
559 }
560
561 s = elf_getscn(elf->elf, shstrtab->idx);
562 if (!s) {
563 WARN_ELF("elf_getscn");
564 return NULL;
565 }
566
567 data = elf_newdata(s);
568 if (!data) {
569 WARN_ELF("elf_newdata");
570 return NULL;
571 }
572
573 data->d_buf = sec->name;
574 data->d_size = strlen(name) + 1;
575 data->d_align = 1;
576
577 sec->sh.sh_name = shstrtab->len;
578
579 shstrtab->len += strlen(name) + 1;
580 shstrtab->changed = true;
581
582 list_add_tail(&sec->list, &elf->sections);
583 hash_add(elf->section_hash, &sec->hash, sec->idx);
584
585 return sec;
586 }
587
588 struct section *elf_create_rela_section(struct elf *elf, struct section *base)
589 {
590 char *relaname;
591 struct section *sec;
592
593 relaname = malloc(strlen(base->name) + strlen(".rela") + 1);
594 if (!relaname) {
595 perror("malloc");
596 return NULL;
597 }
598 strcpy(relaname, ".rela");
599 strcat(relaname, base->name);
600
601 sec = elf_create_section(elf, relaname, sizeof(GElf_Rela), 0);
602 free(relaname);
603 if (!sec)
604 return NULL;
605
606 base->rela = sec;
607 sec->base = base;
608
609 sec->sh.sh_type = SHT_RELA;
610 sec->sh.sh_addralign = 8;
611 sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
612 sec->sh.sh_info = base->idx;
613 sec->sh.sh_flags = SHF_INFO_LINK;
614
615 return sec;
616 }
617
618 int elf_rebuild_rela_section(struct section *sec)
619 {
620 struct rela *rela;
621 int nr, idx = 0, size;
622 GElf_Rela *relas;
623
624 nr = 0;
625 list_for_each_entry(rela, &sec->rela_list, list)
626 nr++;
627
628 size = nr * sizeof(*relas);
629 relas = malloc(size);
630 if (!relas) {
631 perror("malloc");
632 return -1;
633 }
634
635 sec->data->d_buf = relas;
636 sec->data->d_size = size;
637
638 sec->sh.sh_size = size;
639
640 idx = 0;
641 list_for_each_entry(rela, &sec->rela_list, list) {
642 relas[idx].r_offset = rela->offset;
643 relas[idx].r_addend = rela->addend;
644 relas[idx].r_info = GELF_R_INFO(rela->sym->idx, rela->type);
645 idx++;
646 }
647
648 return 0;
649 }
650
651 int elf_write(struct elf *elf)
652 {
653 struct section *sec;
654 Elf_Scn *s;
655
656 /* Update section headers for changed sections: */
657 list_for_each_entry(sec, &elf->sections, list) {
658 if (sec->changed) {
659 s = elf_getscn(elf->elf, sec->idx);
660 if (!s) {
661 WARN_ELF("elf_getscn");
662 return -1;
663 }
664 if (!gelf_update_shdr(s, &sec->sh)) {
665 WARN_ELF("gelf_update_shdr");
666 return -1;
667 }
668 }
669 }
670
671 /* Make sure the new section header entries get updated properly. */
672 elf_flagelf(elf->elf, ELF_C_SET, ELF_F_DIRTY);
673
674 /* Write all changes to the file. */
675 if (elf_update(elf->elf, ELF_C_WRITE) < 0) {
676 WARN_ELF("elf_update");
677 return -1;
678 }
679
680 return 0;
681 }
682
683 void elf_close(struct elf *elf)
684 {
685 struct section *sec, *tmpsec;
686 struct symbol *sym, *tmpsym;
687 struct rela *rela, *tmprela;
688
689 if (elf->elf)
690 elf_end(elf->elf);
691
692 if (elf->fd > 0)
693 close(elf->fd);
694
695 list_for_each_entry_safe(sec, tmpsec, &elf->sections, list) {
696 list_for_each_entry_safe(sym, tmpsym, &sec->symbol_list, list) {
697 list_del(&sym->list);
698 hash_del(&sym->hash);
699 free(sym);
700 }
701 list_for_each_entry_safe(rela, tmprela, &sec->rela_list, list) {
702 list_del(&rela->list);
703 hash_del(&rela->hash);
704 free(rela);
705 }
706 list_del(&sec->list);
707 free(sec);
708 }
709
710 free(elf);
711 }