1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * elf.c - ELF access library
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>
10 #include <sys/types.h>
23 #define MAX_NAME_LEN 128
25 struct section *find_section_by_name(struct elf *elf, const char *name)
29 list_for_each_entry(sec, &elf->sections, list)
30 if (!strcmp(sec->name, name))
36 static struct section *find_section_by_index(struct elf *elf,
41 hash_for_each_possible(elf->section_hash, sec, hash, idx)
48 static struct symbol *find_symbol_by_index(struct elf *elf, unsigned int idx)
52 hash_for_each_possible(elf->symbol_hash, sym, hash, idx)
59 struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset)
63 list_for_each_entry(sym, &sec->symbol_list, list)
64 if (sym->type != STT_SECTION && sym->offset == offset)
70 struct symbol *find_func_by_offset(struct section *sec, unsigned long offset)
74 list_for_each_entry(sym, &sec->symbol_list, list)
75 if (sym->type == STT_FUNC && sym->offset == offset)
81 struct symbol *find_symbol_by_name(struct elf *elf, const char *name)
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))
94 struct symbol *find_symbol_containing(struct section *sec, unsigned long offset)
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)
106 struct rela *find_rela_by_dest_range(struct section *sec, unsigned long offset,
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)
123 struct rela *find_rela_by_dest(struct section *sec, unsigned long offset)
125 return find_rela_by_dest_range(sec, offset, 1);
128 struct symbol *find_containing_func(struct section *sec, unsigned long offset)
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)
140 static int read_sections(struct elf *elf)
144 size_t shstrndx, sections_nr;
147 if (elf_getshdrnum(elf->elf, §ions_nr)) {
148 WARN_ELF("elf_getshdrnum");
152 if (elf_getshdrstrndx(elf->elf, &shstrndx)) {
153 WARN_ELF("elf_getshdrstrndx");
157 for (i = 0; i < sections_nr; i++) {
158 sec = malloc(sizeof(*sec));
163 memset(sec, 0, sizeof(*sec));
165 INIT_LIST_HEAD(&sec->symbol_list);
166 INIT_LIST_HEAD(&sec->rela_list);
167 hash_init(sec->rela_hash);
169 s = elf_getscn(elf->elf, i);
171 WARN_ELF("elf_getscn");
175 sec->idx = elf_ndxscn(s);
177 if (!gelf_getshdr(s, &sec->sh)) {
178 WARN_ELF("gelf_getshdr");
182 sec->name = elf_strptr(elf->elf, shstrndx, sec->sh.sh_name);
184 WARN_ELF("elf_strptr");
188 if (sec->sh.sh_size != 0) {
189 sec->data = elf_getdata(s, NULL);
191 WARN_ELF("elf_getdata");
194 if (sec->data->d_off != 0 ||
195 sec->data->d_size != sec->sh.sh_size) {
196 WARN("unexpected data attributes for %s",
201 sec->len = sec->sh.sh_size;
203 list_add_tail(&sec->list, &elf->sections);
204 hash_add(elf->section_hash, &sec->hash, sec->idx);
208 printf("nr_sections: %lu\n", (unsigned long)sections_nr);
210 /* sanity check, one more call to elf_nextscn() should return NULL */
211 if (elf_nextscn(elf->elf, s)) {
212 WARN("section entry mismatch");
219 static int read_symbols(struct elf *elf)
221 struct section *symtab, *sec;
222 struct symbol *sym, *pfunc, *alias;
223 struct list_head *entry, *tmp;
227 symtab = find_section_by_name(elf, ".symtab");
229 WARN("missing symbol table");
233 symbols_nr = symtab->sh.sh_size / symtab->sh.sh_entsize;
235 for (i = 0; i < symbols_nr; i++) {
236 sym = malloc(sizeof(*sym));
241 memset(sym, 0, sizeof(*sym));
246 if (!gelf_getsym(symtab->data, i, &sym->sym)) {
247 WARN_ELF("gelf_getsym");
251 sym->name = elf_strptr(elf->elf, symtab->sh.sh_link,
254 WARN_ELF("elf_strptr");
258 sym->type = GELF_ST_TYPE(sym->sym.st_info);
259 sym->bind = GELF_ST_BIND(sym->sym.st_info);
261 if (sym->sym.st_shndx > SHN_UNDEF &&
262 sym->sym.st_shndx < SHN_LORESERVE) {
263 sym->sec = find_section_by_index(elf,
266 WARN("couldn't find section for symbol %s",
270 if (sym->type == STT_SECTION) {
271 sym->name = sym->sec->name;
275 sym->sec = find_section_by_index(elf, 0);
277 sym->offset = sym->sym.st_value;
278 sym->len = sym->sym.st_size;
280 /* sorted insert into a per-section list */
281 entry = &sym->sec->symbol_list;
282 list_for_each_prev(tmp, &sym->sec->symbol_list) {
285 s = list_entry(tmp, struct symbol, list);
287 if (sym->offset > s->offset) {
292 if (sym->offset == s->offset) {
293 if (sym->len && sym->len == s->len && alias == sym)
296 if (sym->len >= s->len) {
303 list_add(&sym->list, entry);
304 hash_add(elf->symbol_hash, &sym->hash, sym->idx);
308 printf("nr_symbols: %lu\n", (unsigned long)symbols_nr);
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];
315 if (sym->type != STT_FUNC)
317 sym->pfunc = sym->cfunc = sym;
318 coldstr = strstr(sym->name, ".cold");
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);
329 strncpy(pname, sym->name, pnamelen);
330 pname[pnamelen] = '\0';
331 pfunc = find_symbol_by_name(elf, pname);
334 WARN("%s(): can't find parent function",
343 * Unfortunately, -fnoreorder-functions puts the child
344 * inside the parent. Remove the overlap so we can
345 * have sane assumptions.
347 * Note that pfunc->len now no longer matches
348 * pfunc->sym.st_size.
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;
365 static int read_relas(struct elf *elf)
371 unsigned long nr_rela, max_rela = 0, tot_rela = 0;
373 list_for_each_entry(sec, &elf->sections, list) {
374 if (sec->sh.sh_type != SHT_RELA)
377 sec->base = find_section_by_name(elf, sec->name + 5);
379 WARN("can't find base section for rela section %s",
384 sec->base->rela = sec;
387 for (i = 0; i < sec->sh.sh_size / sec->sh.sh_entsize; i++) {
388 rela = malloc(sizeof(*rela));
393 memset(rela, 0, sizeof(*rela));
395 if (!gelf_getrela(sec->data, i, &rela->rela)) {
396 WARN_ELF("gelf_getrela");
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);
407 WARN("can't find rela entry symbol %d for %s",
412 list_add_tail(&rela->list, &sec->rela_list);
413 hash_add(sec->rela_hash, &rela->hash, rela->offset);
416 max_rela = max(max_rela, nr_rela);
421 printf("max_rela: %lu\n", max_rela);
422 printf("tot_rela: %lu\n", tot_rela);
428 struct elf *elf_read(const char *name, int flags)
433 elf_version(EV_CURRENT);
435 elf = malloc(sizeof(*elf));
440 memset(elf, 0, sizeof(*elf));
442 hash_init(elf->symbol_hash);
443 hash_init(elf->section_hash);
444 INIT_LIST_HEAD(&elf->sections);
446 elf->fd = open(name, flags);
448 fprintf(stderr, "objtool: Can't open '%s': %s\n",
449 name, strerror(errno));
453 if ((flags & O_ACCMODE) == O_RDONLY)
454 cmd = ELF_C_READ_MMAP;
455 else if ((flags & O_ACCMODE) == O_RDWR)
460 elf->elf = elf_begin(elf->fd, cmd, NULL);
462 WARN_ELF("elf_begin");
466 if (!gelf_getehdr(elf->elf, &elf->ehdr)) {
467 WARN_ELF("gelf_getehdr");
471 if (read_sections(elf))
474 if (read_symbols(elf))
487 struct section *elf_create_section(struct elf *elf, const char *name,
488 size_t entsize, int nr)
490 struct section *sec, *shstrtab;
491 size_t size = entsize * nr;
495 sec = malloc(sizeof(*sec));
500 memset(sec, 0, sizeof(*sec));
502 INIT_LIST_HEAD(&sec->symbol_list);
503 INIT_LIST_HEAD(&sec->rela_list);
504 hash_init(sec->rela_hash);
506 s = elf_newscn(elf->elf);
508 WARN_ELF("elf_newscn");
512 sec->name = strdup(name);
518 sec->idx = elf_ndxscn(s);
522 sec->data = elf_newdata(s);
524 WARN_ELF("elf_newdata");
528 sec->data->d_size = size;
529 sec->data->d_align = 1;
532 sec->data->d_buf = malloc(size);
533 if (!sec->data->d_buf) {
537 memset(sec->data->d_buf, 0, size);
540 if (!gelf_getshdr(s, &sec->sh)) {
541 WARN_ELF("gelf_getshdr");
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;
552 /* Add section name to .shstrtab (or .strtab for Clang) */
553 shstrtab = find_section_by_name(elf, ".shstrtab");
555 shstrtab = find_section_by_name(elf, ".strtab");
557 WARN("can't find .shstrtab or .strtab section");
561 s = elf_getscn(elf->elf, shstrtab->idx);
563 WARN_ELF("elf_getscn");
567 data = elf_newdata(s);
569 WARN_ELF("elf_newdata");
573 data->d_buf = sec->name;
574 data->d_size = strlen(name) + 1;
577 sec->sh.sh_name = shstrtab->len;
579 shstrtab->len += strlen(name) + 1;
580 shstrtab->changed = true;
582 list_add_tail(&sec->list, &elf->sections);
583 hash_add(elf->section_hash, &sec->hash, sec->idx);
588 struct section *elf_create_rela_section(struct elf *elf, struct section *base)
593 relaname = malloc(strlen(base->name) + strlen(".rela") + 1);
598 strcpy(relaname, ".rela");
599 strcat(relaname, base->name);
601 sec = elf_create_section(elf, relaname, sizeof(GElf_Rela), 0);
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;
618 int elf_rebuild_rela_section(struct section *sec)
621 int nr, idx = 0, size;
625 list_for_each_entry(rela, &sec->rela_list, list)
628 size = nr * sizeof(*relas);
629 relas = malloc(size);
635 sec->data->d_buf = relas;
636 sec->data->d_size = size;
638 sec->sh.sh_size = size;
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);
651 int elf_write(struct elf *elf)
656 /* Update section headers for changed sections: */
657 list_for_each_entry(sec, &elf->sections, list) {
659 s = elf_getscn(elf->elf, sec->idx);
661 WARN_ELF("elf_getscn");
664 if (!gelf_update_shdr(s, &sec->sh)) {
665 WARN_ELF("gelf_update_shdr");
671 /* Make sure the new section header entries get updated properly. */
672 elf_flagelf(elf->elf, ELF_C_SET, ELF_F_DIRTY);
674 /* Write all changes to the file. */
675 if (elf_update(elf->elf, ELF_C_WRITE) < 0) {
676 WARN_ELF("elf_update");
683 void elf_close(struct elf *elf)
685 struct section *sec, *tmpsec;
686 struct symbol *sym, *tmpsym;
687 struct rela *rela, *tmprela;
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);
701 list_for_each_entry_safe(rela, tmprela, &sec->rela_list, list) {
702 list_del(&rela->list);
703 hash_del(&rela->hash);
706 list_del(&sec->list);