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