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>
18 #include <objtool/builtin.h>
20 #include <objtool/elf.h>
21 #include <objtool/warn.h>
23 #define MAX_NAME_LEN 128
25 static inline u32 str_hash(const char *str)
27 return jhash(str, strlen(str), 0);
30 static inline int elf_hash_bits(void)
32 return vmlinux ? ELF_HASH_BITS : 16;
35 #define elf_hash_add(hashtable, node, key) \
36 hlist_add_head(node, &hashtable[hash_min(key, elf_hash_bits())])
38 static void elf_hash_init(struct hlist_head *table)
40 __hash_init(table, 1U << elf_hash_bits());
43 #define elf_hash_for_each_possible(name, obj, member, key) \
44 hlist_for_each_entry(obj, &name[hash_min(key, elf_hash_bits())], member)
46 static bool symbol_to_offset(struct rb_node *a, const struct rb_node *b)
48 struct symbol *sa = rb_entry(a, struct symbol, node);
49 struct symbol *sb = rb_entry(b, struct symbol, node);
51 if (sa->offset < sb->offset)
53 if (sa->offset > sb->offset)
56 if (sa->len < sb->len)
58 if (sa->len > sb->len)
66 static int symbol_by_offset(const void *key, const struct rb_node *node)
68 const struct symbol *s = rb_entry(node, struct symbol, node);
69 const unsigned long *o = key;
73 if (*o >= s->offset + s->len)
79 struct section *find_section_by_name(const struct elf *elf, const char *name)
83 elf_hash_for_each_possible(elf->section_name_hash, sec, name_hash, str_hash(name))
84 if (!strcmp(sec->name, name))
90 static struct section *find_section_by_index(struct elf *elf,
95 elf_hash_for_each_possible(elf->section_hash, sec, hash, idx)
102 static struct symbol *find_symbol_by_index(struct elf *elf, unsigned int idx)
106 elf_hash_for_each_possible(elf->symbol_hash, sym, hash, idx)
113 struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset)
115 struct rb_node *node;
117 rb_for_each(node, &offset, &sec->symbol_tree, symbol_by_offset) {
118 struct symbol *s = rb_entry(node, struct symbol, node);
120 if (s->offset == offset && s->type != STT_SECTION)
127 struct symbol *find_func_by_offset(struct section *sec, unsigned long offset)
129 struct rb_node *node;
131 rb_for_each(node, &offset, &sec->symbol_tree, symbol_by_offset) {
132 struct symbol *s = rb_entry(node, struct symbol, node);
134 if (s->offset == offset && s->type == STT_FUNC)
141 struct symbol *find_symbol_containing(const struct section *sec, unsigned long offset)
143 struct rb_node *node;
145 rb_for_each(node, &offset, &sec->symbol_tree, symbol_by_offset) {
146 struct symbol *s = rb_entry(node, struct symbol, node);
148 if (s->type != STT_SECTION)
155 struct symbol *find_func_containing(struct section *sec, unsigned long offset)
157 struct rb_node *node;
159 rb_for_each(node, &offset, &sec->symbol_tree, symbol_by_offset) {
160 struct symbol *s = rb_entry(node, struct symbol, node);
162 if (s->type == STT_FUNC)
169 struct symbol *find_symbol_by_name(const struct elf *elf, const char *name)
173 elf_hash_for_each_possible(elf->symbol_name_hash, sym, name_hash, str_hash(name))
174 if (!strcmp(sym->name, name))
180 struct reloc *find_reloc_by_dest_range(const struct elf *elf, struct section *sec,
181 unsigned long offset, unsigned int len)
183 struct reloc *reloc, *r = NULL;
191 for_offset_range(o, offset, offset + len) {
192 elf_hash_for_each_possible(elf->reloc_hash, reloc, hash,
193 sec_offset_hash(sec, o)) {
194 if (reloc->sec != sec)
197 if (reloc->offset >= offset && reloc->offset < offset + len) {
198 if (!r || reloc->offset < r->offset)
209 struct reloc *find_reloc_by_dest(const struct elf *elf, struct section *sec, unsigned long offset)
211 return find_reloc_by_dest_range(elf, sec, offset, 1);
214 void insn_to_reloc_sym_addend(struct section *sec, unsigned long offset,
218 reloc->sym = sec->sym;
219 reloc->addend = offset;
224 * The Clang assembler strips section symbols, so we have to reference
225 * the function symbol instead:
227 reloc->sym = find_symbol_containing(sec, offset);
230 * Hack alert. This happens when we need to reference the NOP
231 * pad insn immediately after the function.
233 reloc->sym = find_symbol_containing(sec, offset - 1);
237 reloc->addend = offset - reloc->sym->offset;
240 static int read_sections(struct elf *elf)
244 size_t shstrndx, sections_nr;
247 if (elf_getshdrnum(elf->elf, §ions_nr)) {
248 WARN_ELF("elf_getshdrnum");
252 if (elf_getshdrstrndx(elf->elf, &shstrndx)) {
253 WARN_ELF("elf_getshdrstrndx");
257 for (i = 0; i < sections_nr; i++) {
258 sec = malloc(sizeof(*sec));
263 memset(sec, 0, sizeof(*sec));
265 INIT_LIST_HEAD(&sec->symbol_list);
266 INIT_LIST_HEAD(&sec->reloc_list);
268 s = elf_getscn(elf->elf, i);
270 WARN_ELF("elf_getscn");
274 sec->idx = elf_ndxscn(s);
276 if (!gelf_getshdr(s, &sec->sh)) {
277 WARN_ELF("gelf_getshdr");
281 sec->name = elf_strptr(elf->elf, shstrndx, sec->sh.sh_name);
283 WARN_ELF("elf_strptr");
287 if (sec->sh.sh_size != 0) {
288 sec->data = elf_getdata(s, NULL);
290 WARN_ELF("elf_getdata");
293 if (sec->data->d_off != 0 ||
294 sec->data->d_size != sec->sh.sh_size) {
295 WARN("unexpected data attributes for %s",
300 sec->len = sec->sh.sh_size;
302 list_add_tail(&sec->list, &elf->sections);
303 elf_hash_add(elf->section_hash, &sec->hash, sec->idx);
304 elf_hash_add(elf->section_name_hash, &sec->name_hash, str_hash(sec->name));
308 printf("nr_sections: %lu\n", (unsigned long)sections_nr);
310 /* sanity check, one more call to elf_nextscn() should return NULL */
311 if (elf_nextscn(elf->elf, s)) {
312 WARN("section entry mismatch");
319 static int read_symbols(struct elf *elf)
321 struct section *symtab, *symtab_shndx, *sec;
322 struct symbol *sym, *pfunc;
323 struct list_head *entry;
324 struct rb_node *pnode;
327 Elf_Data *shndx_data = NULL;
330 symtab = find_section_by_name(elf, ".symtab");
333 * A missing symbol table is actually possible if it's an empty
334 * .o file. This can happen for thunk_64.o.
339 symtab_shndx = find_section_by_name(elf, ".symtab_shndx");
341 shndx_data = symtab_shndx->data;
343 symbols_nr = symtab->sh.sh_size / symtab->sh.sh_entsize;
345 for (i = 0; i < symbols_nr; i++) {
346 sym = malloc(sizeof(*sym));
351 memset(sym, 0, sizeof(*sym));
356 if (!gelf_getsymshndx(symtab->data, shndx_data, i, &sym->sym,
358 WARN_ELF("gelf_getsymshndx");
362 sym->name = elf_strptr(elf->elf, symtab->sh.sh_link,
365 WARN_ELF("elf_strptr");
369 sym->type = GELF_ST_TYPE(sym->sym.st_info);
370 sym->bind = GELF_ST_BIND(sym->sym.st_info);
372 if ((sym->sym.st_shndx > SHN_UNDEF &&
373 sym->sym.st_shndx < SHN_LORESERVE) ||
374 (shndx_data && sym->sym.st_shndx == SHN_XINDEX)) {
375 if (sym->sym.st_shndx != SHN_XINDEX)
376 shndx = sym->sym.st_shndx;
378 sym->sec = find_section_by_index(elf, shndx);
380 WARN("couldn't find section for symbol %s",
384 if (sym->type == STT_SECTION) {
385 sym->name = sym->sec->name;
389 sym->sec = find_section_by_index(elf, 0);
391 sym->offset = sym->sym.st_value;
392 sym->len = sym->sym.st_size;
394 rb_add(&sym->node, &sym->sec->symbol_tree, symbol_to_offset);
395 pnode = rb_prev(&sym->node);
397 entry = &rb_entry(pnode, struct symbol, node)->list;
399 entry = &sym->sec->symbol_list;
400 list_add(&sym->list, entry);
401 elf_hash_add(elf->symbol_hash, &sym->hash, sym->idx);
402 elf_hash_add(elf->symbol_name_hash, &sym->name_hash, str_hash(sym->name));
405 * Don't store empty STT_NOTYPE symbols in the rbtree. They
406 * can exist within a function, confusing the sorting.
409 rb_erase(&sym->node, &sym->sec->symbol_tree);
413 printf("nr_symbols: %lu\n", (unsigned long)symbols_nr);
415 /* Create parent/child links for any cold subfunctions */
416 list_for_each_entry(sec, &elf->sections, list) {
417 list_for_each_entry(sym, &sec->symbol_list, list) {
418 char pname[MAX_NAME_LEN + 1];
420 if (sym->type != STT_FUNC)
423 if (sym->pfunc == NULL)
426 if (sym->cfunc == NULL)
429 coldstr = strstr(sym->name, ".cold");
433 pnamelen = coldstr - sym->name;
434 if (pnamelen > MAX_NAME_LEN) {
435 WARN("%s(): parent function name exceeds maximum length of %d characters",
436 sym->name, MAX_NAME_LEN);
440 strncpy(pname, sym->name, pnamelen);
441 pname[pnamelen] = '\0';
442 pfunc = find_symbol_by_name(elf, pname);
445 WARN("%s(): can't find parent function",
454 * Unfortunately, -fnoreorder-functions puts the child
455 * inside the parent. Remove the overlap so we can
456 * have sane assumptions.
458 * Note that pfunc->len now no longer matches
459 * pfunc->sym.st_size.
461 if (sym->sec == pfunc->sec &&
462 sym->offset >= pfunc->offset &&
463 sym->offset + sym->len == pfunc->offset + pfunc->len) {
464 pfunc->len -= sym->len;
476 void elf_add_reloc(struct elf *elf, struct reloc *reloc)
478 struct section *sec = reloc->sec;
480 list_add_tail(&reloc->list, &sec->reloc_list);
481 elf_hash_add(elf->reloc_hash, &reloc->hash, reloc_hash(reloc));
486 static int read_rel_reloc(struct section *sec, int i, struct reloc *reloc, unsigned int *symndx)
488 if (!gelf_getrel(sec->data, i, &reloc->rel)) {
489 WARN_ELF("gelf_getrel");
492 reloc->type = GELF_R_TYPE(reloc->rel.r_info);
494 reloc->offset = reloc->rel.r_offset;
495 *symndx = GELF_R_SYM(reloc->rel.r_info);
499 static int read_rela_reloc(struct section *sec, int i, struct reloc *reloc, unsigned int *symndx)
501 if (!gelf_getrela(sec->data, i, &reloc->rela)) {
502 WARN_ELF("gelf_getrela");
505 reloc->type = GELF_R_TYPE(reloc->rela.r_info);
506 reloc->addend = reloc->rela.r_addend;
507 reloc->offset = reloc->rela.r_offset;
508 *symndx = GELF_R_SYM(reloc->rela.r_info);
512 static int read_relocs(struct elf *elf)
518 unsigned long nr_reloc, max_reloc = 0, tot_reloc = 0;
520 list_for_each_entry(sec, &elf->sections, list) {
521 if ((sec->sh.sh_type != SHT_RELA) &&
522 (sec->sh.sh_type != SHT_REL))
525 sec->base = find_section_by_index(elf, sec->sh.sh_info);
527 WARN("can't find base section for reloc section %s",
532 sec->base->reloc = sec;
535 for (i = 0; i < sec->sh.sh_size / sec->sh.sh_entsize; i++) {
536 reloc = malloc(sizeof(*reloc));
541 memset(reloc, 0, sizeof(*reloc));
542 switch (sec->sh.sh_type) {
544 if (read_rel_reloc(sec, i, reloc, &symndx))
548 if (read_rela_reloc(sec, i, reloc, &symndx))
556 reloc->sym = find_symbol_by_index(elf, symndx);
558 WARN("can't find reloc entry symbol %d for %s",
563 list_add_tail(&reloc->list, &sec->reloc_list);
564 elf_hash_add(elf->reloc_hash, &reloc->hash, reloc_hash(reloc));
568 max_reloc = max(max_reloc, nr_reloc);
569 tot_reloc += nr_reloc;
573 printf("max_reloc: %lu\n", max_reloc);
574 printf("tot_reloc: %lu\n", tot_reloc);
580 struct elf *elf_open_read(const char *name, int flags)
585 elf_version(EV_CURRENT);
587 elf = malloc(sizeof(*elf));
592 memset(elf, 0, offsetof(struct elf, sections));
594 INIT_LIST_HEAD(&elf->sections);
596 elf_hash_init(elf->symbol_hash);
597 elf_hash_init(elf->symbol_name_hash);
598 elf_hash_init(elf->section_hash);
599 elf_hash_init(elf->section_name_hash);
600 elf_hash_init(elf->reloc_hash);
602 elf->fd = open(name, flags);
604 fprintf(stderr, "objtool: Can't open '%s': %s\n",
605 name, strerror(errno));
609 if ((flags & O_ACCMODE) == O_RDONLY)
610 cmd = ELF_C_READ_MMAP;
611 else if ((flags & O_ACCMODE) == O_RDWR)
616 elf->elf = elf_begin(elf->fd, cmd, NULL);
618 WARN_ELF("elf_begin");
622 if (!gelf_getehdr(elf->elf, &elf->ehdr)) {
623 WARN_ELF("gelf_getehdr");
627 if (read_sections(elf))
630 if (read_symbols(elf))
633 if (read_relocs(elf))
643 struct section *elf_create_section(struct elf *elf, const char *name,
644 unsigned int sh_flags, size_t entsize, int nr)
646 struct section *sec, *shstrtab;
647 size_t size = entsize * nr;
651 sec = malloc(sizeof(*sec));
656 memset(sec, 0, sizeof(*sec));
658 INIT_LIST_HEAD(&sec->symbol_list);
659 INIT_LIST_HEAD(&sec->reloc_list);
661 s = elf_newscn(elf->elf);
663 WARN_ELF("elf_newscn");
667 sec->name = strdup(name);
673 sec->idx = elf_ndxscn(s);
677 sec->data = elf_newdata(s);
679 WARN_ELF("elf_newdata");
683 sec->data->d_size = size;
684 sec->data->d_align = 1;
687 sec->data->d_buf = malloc(size);
688 if (!sec->data->d_buf) {
692 memset(sec->data->d_buf, 0, size);
695 if (!gelf_getshdr(s, &sec->sh)) {
696 WARN_ELF("gelf_getshdr");
700 sec->sh.sh_size = size;
701 sec->sh.sh_entsize = entsize;
702 sec->sh.sh_type = SHT_PROGBITS;
703 sec->sh.sh_addralign = 1;
704 sec->sh.sh_flags = SHF_ALLOC | sh_flags;
707 /* Add section name to .shstrtab (or .strtab for Clang) */
708 shstrtab = find_section_by_name(elf, ".shstrtab");
710 shstrtab = find_section_by_name(elf, ".strtab");
712 WARN("can't find .shstrtab or .strtab section");
716 s = elf_getscn(elf->elf, shstrtab->idx);
718 WARN_ELF("elf_getscn");
722 data = elf_newdata(s);
724 WARN_ELF("elf_newdata");
728 data->d_buf = sec->name;
729 data->d_size = strlen(name) + 1;
732 sec->sh.sh_name = shstrtab->len;
734 shstrtab->len += strlen(name) + 1;
735 shstrtab->changed = true;
737 list_add_tail(&sec->list, &elf->sections);
738 elf_hash_add(elf->section_hash, &sec->hash, sec->idx);
739 elf_hash_add(elf->section_name_hash, &sec->name_hash, str_hash(sec->name));
746 static struct section *elf_create_rel_reloc_section(struct elf *elf, struct section *base)
751 relocname = malloc(strlen(base->name) + strlen(".rel") + 1);
756 strcpy(relocname, ".rel");
757 strcat(relocname, base->name);
759 sec = elf_create_section(elf, relocname, 0, sizeof(GElf_Rel), 0);
767 sec->sh.sh_type = SHT_REL;
768 sec->sh.sh_addralign = 8;
769 sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
770 sec->sh.sh_info = base->idx;
771 sec->sh.sh_flags = SHF_INFO_LINK;
776 static struct section *elf_create_rela_reloc_section(struct elf *elf, struct section *base)
781 relocname = malloc(strlen(base->name) + strlen(".rela") + 1);
786 strcpy(relocname, ".rela");
787 strcat(relocname, base->name);
789 sec = elf_create_section(elf, relocname, 0, sizeof(GElf_Rela), 0);
797 sec->sh.sh_type = SHT_RELA;
798 sec->sh.sh_addralign = 8;
799 sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
800 sec->sh.sh_info = base->idx;
801 sec->sh.sh_flags = SHF_INFO_LINK;
806 struct section *elf_create_reloc_section(struct elf *elf,
807 struct section *base,
811 case SHT_REL: return elf_create_rel_reloc_section(elf, base);
812 case SHT_RELA: return elf_create_rela_reloc_section(elf, base);
813 default: return NULL;
817 static int elf_rebuild_rel_reloc_section(struct section *sec, int nr)
823 /* Allocate a buffer for relocations */
824 size = nr * sizeof(GElf_Rel);
831 sec->data->d_buf = buf;
832 sec->data->d_size = size;
833 sec->data->d_type = ELF_T_REL;
835 sec->sh.sh_size = size;
838 list_for_each_entry(reloc, &sec->reloc_list, list) {
839 reloc->rel.r_offset = reloc->offset;
840 reloc->rel.r_info = GELF_R_INFO(reloc->sym->idx, reloc->type);
841 gelf_update_rel(sec->data, idx, &reloc->rel);
848 static int elf_rebuild_rela_reloc_section(struct section *sec, int nr)
854 /* Allocate a buffer for relocations with addends */
855 size = nr * sizeof(GElf_Rela);
862 sec->data->d_buf = buf;
863 sec->data->d_size = size;
864 sec->data->d_type = ELF_T_RELA;
866 sec->sh.sh_size = size;
869 list_for_each_entry(reloc, &sec->reloc_list, list) {
870 reloc->rela.r_offset = reloc->offset;
871 reloc->rela.r_addend = reloc->addend;
872 reloc->rela.r_info = GELF_R_INFO(reloc->sym->idx, reloc->type);
873 gelf_update_rela(sec->data, idx, &reloc->rela);
880 static int elf_rebuild_reloc_section(struct elf *elf, struct section *sec)
886 list_for_each_entry(reloc, &sec->reloc_list, list)
889 switch (sec->sh.sh_type) {
890 case SHT_REL: return elf_rebuild_rel_reloc_section(sec, nr);
891 case SHT_RELA: return elf_rebuild_rela_reloc_section(sec, nr);
896 int elf_write_insn(struct elf *elf, struct section *sec,
897 unsigned long offset, unsigned int len,
900 Elf_Data *data = sec->data;
902 if (data->d_type != ELF_T_BYTE || data->d_off) {
903 WARN("write to unexpected data for section: %s", sec->name);
907 memcpy(data->d_buf + offset, insn, len);
908 elf_flagdata(data, ELF_C_SET, ELF_F_DIRTY);
915 int elf_write_reloc(struct elf *elf, struct reloc *reloc)
917 struct section *sec = reloc->sec;
919 if (sec->sh.sh_type == SHT_REL) {
920 reloc->rel.r_info = GELF_R_INFO(reloc->sym->idx, reloc->type);
921 reloc->rel.r_offset = reloc->offset;
923 if (!gelf_update_rel(sec->data, reloc->idx, &reloc->rel)) {
924 WARN_ELF("gelf_update_rel");
928 reloc->rela.r_info = GELF_R_INFO(reloc->sym->idx, reloc->type);
929 reloc->rela.r_addend = reloc->addend;
930 reloc->rela.r_offset = reloc->offset;
932 if (!gelf_update_rela(sec->data, reloc->idx, &reloc->rela)) {
933 WARN_ELF("gelf_update_rela");
943 int elf_write(struct elf *elf)
948 /* Update changed relocation sections and section headers: */
949 list_for_each_entry(sec, &elf->sections, list) {
952 elf_rebuild_reloc_section(elf, sec)) {
953 WARN("elf_rebuild_reloc_section");
957 s = elf_getscn(elf->elf, sec->idx);
959 WARN_ELF("elf_getscn");
962 if (!gelf_update_shdr(s, &sec->sh)) {
963 WARN_ELF("gelf_update_shdr");
967 sec->changed = false;
972 /* Make sure the new section header entries get updated properly. */
973 elf_flagelf(elf->elf, ELF_C_SET, ELF_F_DIRTY);
975 /* Write all changes to the file. */
976 if (elf_update(elf->elf, ELF_C_WRITE) < 0) {
977 WARN_ELF("elf_update");
981 elf->changed = false;
986 void elf_close(struct elf *elf)
988 struct section *sec, *tmpsec;
989 struct symbol *sym, *tmpsym;
990 struct reloc *reloc, *tmpreloc;
998 list_for_each_entry_safe(sec, tmpsec, &elf->sections, list) {
999 list_for_each_entry_safe(sym, tmpsym, &sec->symbol_list, list) {
1000 list_del(&sym->list);
1001 hash_del(&sym->hash);
1004 list_for_each_entry_safe(reloc, tmpreloc, &sec->reloc_list, list) {
1005 list_del(&reloc->list);
1006 hash_del(&reloc->hash);
1009 list_del(&sec->list);