1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * elf.c - ELF access library
5 * Adapted from kpatch (https://github.com/dynup/kpatch):
10 #include <sys/types.h>
19 #include <linux/interval_tree_generic.h>
20 #include <objtool/builtin.h>
22 #include <objtool/elf.h>
23 #include <objtool/warn.h>
25 static inline u32 str_hash(const char *str)
27 return jhash(str, strlen(str), 0);
30 #define __elf_table(name) (elf->name##_hash)
31 #define __elf_bits(name) (elf->name##_bits)
33 #define __elf_table_entry(name, key) \
34 __elf_table(name)[hash_min(key, __elf_bits(name))]
36 #define elf_hash_add(name, node, key) \
38 struct elf_hash_node *__node = node; \
39 __node->next = __elf_table_entry(name, key); \
40 __elf_table_entry(name, key) = __node; \
43 static inline void __elf_hash_del(struct elf_hash_node *node,
44 struct elf_hash_node **head)
46 struct elf_hash_node *cur, *prev;
53 for (prev = NULL, cur = *head; cur; prev = cur, cur = cur->next) {
55 prev->next = cur->next;
61 #define elf_hash_del(name, node, key) \
62 __elf_hash_del(node, &__elf_table_entry(name, key))
64 #define elf_list_entry(ptr, type, member) \
66 typeof(ptr) __ptr = (ptr); \
67 __ptr ? container_of(__ptr, type, member) : NULL; \
70 #define elf_hash_for_each_possible(name, obj, member, key) \
71 for (obj = elf_list_entry(__elf_table_entry(name, key), typeof(*obj), member); \
73 obj = elf_list_entry(obj->member.next, typeof(*(obj)), member))
75 #define elf_alloc_hash(name, size) \
77 __elf_bits(name) = max(10, ilog2(size)); \
78 __elf_table(name) = mmap(NULL, sizeof(struct elf_hash_node *) << __elf_bits(name), \
79 PROT_READ|PROT_WRITE, \
80 MAP_PRIVATE|MAP_ANON, -1, 0); \
81 if (__elf_table(name) == (void *)-1L) { \
82 WARN("mmap fail " #name); \
83 __elf_table(name) = NULL; \
88 static inline unsigned long __sym_start(struct symbol *s)
93 static inline unsigned long __sym_last(struct symbol *s)
95 return s->offset + s->len - 1;
98 INTERVAL_TREE_DEFINE(struct symbol, node, unsigned long, __subtree_last,
99 __sym_start, __sym_last, static, __sym)
101 #define __sym_for_each(_iter, _tree, _start, _end) \
102 for (_iter = __sym_iter_first((_tree), (_start), (_end)); \
103 _iter; _iter = __sym_iter_next(_iter, (_start), (_end)))
107 const struct symbol *sym;
111 * Find !section symbol where @offset is after it.
113 static int symbol_hole_by_offset(const void *key, const struct rb_node *node)
115 const struct symbol *s = rb_entry(node, struct symbol, node);
116 struct symbol_hole *sh = (void *)key;
118 if (sh->key < s->offset)
121 if (sh->key >= s->offset + s->len) {
122 if (s->type != STT_SECTION)
130 struct section *find_section_by_name(const struct elf *elf, const char *name)
134 elf_hash_for_each_possible(section_name, sec, name_hash, str_hash(name)) {
135 if (!strcmp(sec->name, name))
142 static struct section *find_section_by_index(struct elf *elf,
147 elf_hash_for_each_possible(section, sec, hash, idx) {
155 static struct symbol *find_symbol_by_index(struct elf *elf, unsigned int idx)
159 elf_hash_for_each_possible(symbol, sym, hash, idx) {
167 struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset)
169 struct rb_root_cached *tree = (struct rb_root_cached *)&sec->symbol_tree;
172 __sym_for_each(iter, tree, offset, offset) {
173 if (iter->offset == offset && iter->type != STT_SECTION)
180 struct symbol *find_func_by_offset(struct section *sec, unsigned long offset)
182 struct rb_root_cached *tree = (struct rb_root_cached *)&sec->symbol_tree;
185 __sym_for_each(iter, tree, offset, offset) {
186 if (iter->offset == offset && iter->type == STT_FUNC)
193 struct symbol *find_symbol_containing(const struct section *sec, unsigned long offset)
195 struct rb_root_cached *tree = (struct rb_root_cached *)&sec->symbol_tree;
198 __sym_for_each(iter, tree, offset, offset) {
199 if (iter->type != STT_SECTION)
207 * Returns size of hole starting at @offset.
209 int find_symbol_hole_containing(const struct section *sec, unsigned long offset)
211 struct symbol_hole hole = {
219 * Find the rightmost symbol for which @offset is after it.
221 n = rb_find(&hole, &sec->symbol_tree.rb_root, symbol_hole_by_offset);
223 /* found a symbol that contains @offset */
225 return 0; /* not a hole */
227 /* didn't find a symbol for which @offset is after it */
229 return 0; /* not a hole */
231 /* @offset >= sym->offset + sym->len, find symbol after it */
232 n = rb_next(&hole.sym->node);
234 return -1; /* until end of address space */
236 /* hole until start of next symbol */
237 s = rb_entry(n, struct symbol, node);
238 return s->offset - offset;
241 struct symbol *find_func_containing(struct section *sec, unsigned long offset)
243 struct rb_root_cached *tree = (struct rb_root_cached *)&sec->symbol_tree;
246 __sym_for_each(iter, tree, offset, offset) {
247 if (iter->type == STT_FUNC)
254 struct symbol *find_symbol_by_name(const struct elf *elf, const char *name)
258 elf_hash_for_each_possible(symbol_name, sym, name_hash, str_hash(name)) {
259 if (!strcmp(sym->name, name))
266 struct reloc *find_reloc_by_dest_range(const struct elf *elf, struct section *sec,
267 unsigned long offset, unsigned int len)
269 struct reloc *reloc, *r = NULL;
270 struct section *rsec;
277 for_offset_range(o, offset, offset + len) {
278 elf_hash_for_each_possible(reloc, reloc, hash,
279 sec_offset_hash(rsec, o)) {
280 if (reloc->sec != rsec)
283 if (reloc_offset(reloc) >= offset &&
284 reloc_offset(reloc) < offset + len) {
285 if (!r || reloc_offset(reloc) < reloc_offset(r))
296 struct reloc *find_reloc_by_dest(const struct elf *elf, struct section *sec, unsigned long offset)
298 return find_reloc_by_dest_range(elf, sec, offset, 1);
301 static bool is_dwarf_section(struct section *sec)
303 return !strncmp(sec->name, ".debug_", 7);
306 static int read_sections(struct elf *elf)
310 size_t shstrndx, sections_nr;
313 if (elf_getshdrnum(elf->elf, §ions_nr)) {
314 WARN_ELF("elf_getshdrnum");
318 if (elf_getshdrstrndx(elf->elf, &shstrndx)) {
319 WARN_ELF("elf_getshdrstrndx");
323 if (!elf_alloc_hash(section, sections_nr) ||
324 !elf_alloc_hash(section_name, sections_nr))
327 elf->section_data = calloc(sections_nr, sizeof(*sec));
328 if (!elf->section_data) {
332 for (i = 0; i < sections_nr; i++) {
333 sec = &elf->section_data[i];
335 INIT_LIST_HEAD(&sec->symbol_list);
337 s = elf_getscn(elf->elf, i);
339 WARN_ELF("elf_getscn");
343 sec->idx = elf_ndxscn(s);
345 if (!gelf_getshdr(s, &sec->sh)) {
346 WARN_ELF("gelf_getshdr");
350 sec->name = elf_strptr(elf->elf, shstrndx, sec->sh.sh_name);
352 WARN_ELF("elf_strptr");
356 if (sec->sh.sh_size != 0 && !is_dwarf_section(sec)) {
357 sec->data = elf_getdata(s, NULL);
359 WARN_ELF("elf_getdata");
362 if (sec->data->d_off != 0 ||
363 sec->data->d_size != sec->sh.sh_size) {
364 WARN("unexpected data attributes for %s",
370 list_add_tail(&sec->list, &elf->sections);
371 elf_hash_add(section, &sec->hash, sec->idx);
372 elf_hash_add(section_name, &sec->name_hash, str_hash(sec->name));
374 if (is_reloc_sec(sec))
375 elf->num_relocs += sec_num_entries(sec);
379 printf("nr_sections: %lu\n", (unsigned long)sections_nr);
380 printf("section_bits: %d\n", elf->section_bits);
383 /* sanity check, one more call to elf_nextscn() should return NULL */
384 if (elf_nextscn(elf->elf, s)) {
385 WARN("section entry mismatch");
392 static void elf_add_symbol(struct elf *elf, struct symbol *sym)
394 struct list_head *entry;
395 struct rb_node *pnode;
398 INIT_LIST_HEAD(&sym->pv_target);
401 sym->type = GELF_ST_TYPE(sym->sym.st_info);
402 sym->bind = GELF_ST_BIND(sym->sym.st_info);
404 if (sym->type == STT_FILE)
407 sym->offset = sym->sym.st_value;
408 sym->len = sym->sym.st_size;
410 __sym_for_each(iter, &sym->sec->symbol_tree, sym->offset, sym->offset) {
411 if (iter->offset == sym->offset && iter->type == sym->type)
415 __sym_insert(sym, &sym->sec->symbol_tree);
416 pnode = rb_prev(&sym->node);
418 entry = &rb_entry(pnode, struct symbol, node)->list;
420 entry = &sym->sec->symbol_list;
421 list_add(&sym->list, entry);
422 elf_hash_add(symbol, &sym->hash, sym->idx);
423 elf_hash_add(symbol_name, &sym->name_hash, str_hash(sym->name));
426 * Don't store empty STT_NOTYPE symbols in the rbtree. They
427 * can exist within a function, confusing the sorting.
430 __sym_remove(sym, &sym->sec->symbol_tree);
433 static int read_symbols(struct elf *elf)
435 struct section *symtab, *symtab_shndx, *sec;
436 struct symbol *sym, *pfunc;
439 Elf_Data *shndx_data = NULL;
442 symtab = find_section_by_name(elf, ".symtab");
444 symtab_shndx = find_section_by_name(elf, ".symtab_shndx");
446 shndx_data = symtab_shndx->data;
448 symbols_nr = sec_num_entries(symtab);
451 * A missing symbol table is actually possible if it's an empty
452 * .o file. This can happen for thunk_64.o. Make sure to at
453 * least allocate the symbol hash tables so we can do symbol
454 * lookups without crashing.
459 if (!elf_alloc_hash(symbol, symbols_nr) ||
460 !elf_alloc_hash(symbol_name, symbols_nr))
463 elf->symbol_data = calloc(symbols_nr, sizeof(*sym));
464 if (!elf->symbol_data) {
468 for (i = 0; i < symbols_nr; i++) {
469 sym = &elf->symbol_data[i];
473 if (!gelf_getsymshndx(symtab->data, shndx_data, i, &sym->sym,
475 WARN_ELF("gelf_getsymshndx");
479 sym->name = elf_strptr(elf->elf, symtab->sh.sh_link,
482 WARN_ELF("elf_strptr");
486 if ((sym->sym.st_shndx > SHN_UNDEF &&
487 sym->sym.st_shndx < SHN_LORESERVE) ||
488 (shndx_data && sym->sym.st_shndx == SHN_XINDEX)) {
489 if (sym->sym.st_shndx != SHN_XINDEX)
490 shndx = sym->sym.st_shndx;
492 sym->sec = find_section_by_index(elf, shndx);
494 WARN("couldn't find section for symbol %s",
498 if (GELF_ST_TYPE(sym->sym.st_info) == STT_SECTION) {
499 sym->name = sym->sec->name;
503 sym->sec = find_section_by_index(elf, 0);
505 elf_add_symbol(elf, sym);
509 printf("nr_symbols: %lu\n", (unsigned long)symbols_nr);
510 printf("symbol_bits: %d\n", elf->symbol_bits);
513 /* Create parent/child links for any cold subfunctions */
514 list_for_each_entry(sec, &elf->sections, list) {
515 sec_for_each_sym(sec, sym) {
518 if (sym->type != STT_FUNC)
521 if (sym->pfunc == NULL)
524 if (sym->cfunc == NULL)
527 coldstr = strstr(sym->name, ".cold");
531 pnamelen = coldstr - sym->name;
532 pname = strndup(sym->name, pnamelen);
534 WARN("%s(): failed to allocate memory",
539 pfunc = find_symbol_by_name(elf, pname);
543 WARN("%s(): can't find parent function",
552 * Unfortunately, -fnoreorder-functions puts the child
553 * inside the parent. Remove the overlap so we can
554 * have sane assumptions.
556 * Note that pfunc->len now no longer matches
557 * pfunc->sym.st_size.
559 if (sym->sec == pfunc->sec &&
560 sym->offset >= pfunc->offset &&
561 sym->offset + sym->len == pfunc->offset + pfunc->len) {
562 pfunc->len -= sym->len;
575 * @sym's idx has changed. Update the relocs which reference it.
577 static int elf_update_sym_relocs(struct elf *elf, struct symbol *sym)
581 for (reloc = sym->relocs; reloc; reloc = reloc->sym_next_reloc)
582 set_reloc_sym(elf, reloc, reloc->sym->idx);
588 * The libelf API is terrible; gelf_update_sym*() takes a data block relative
589 * index value, *NOT* the symbol index. As such, iterate the data blocks and
590 * adjust index until it fits.
592 * If no data block is found, allow adding a new data block provided the index
593 * is only one past the end.
595 static int elf_update_symbol(struct elf *elf, struct section *symtab,
596 struct section *symtab_shndx, struct symbol *sym)
598 Elf32_Word shndx = sym->sec ? sym->sec->idx : SHN_UNDEF;
599 Elf_Data *symtab_data = NULL, *shndx_data = NULL;
600 Elf64_Xword entsize = symtab->sh.sh_entsize;
601 int max_idx, idx = sym->idx;
602 Elf_Scn *s, *t = NULL;
603 bool is_special_shndx = sym->sym.st_shndx >= SHN_LORESERVE &&
604 sym->sym.st_shndx != SHN_XINDEX;
606 if (is_special_shndx)
607 shndx = sym->sym.st_shndx;
609 s = elf_getscn(elf->elf, symtab->idx);
611 WARN_ELF("elf_getscn");
616 t = elf_getscn(elf->elf, symtab_shndx->idx);
618 WARN_ELF("elf_getscn");
624 /* get next data descriptor for the relevant sections */
625 symtab_data = elf_getdata(s, symtab_data);
627 shndx_data = elf_getdata(t, shndx_data);
632 * Over-allocate to avoid O(n^2) symbol creation
633 * behaviour. The down side is that libelf doesn't
634 * like this; see elf_truncate_section() for the fixup.
636 int num = max(1U, sym->idx/3);
640 /* we don't do holes in symbol tables */
641 WARN("index out of range");
645 /* if @idx == 0, it's the next contiguous entry, create it */
646 symtab_data = elf_newdata(s);
648 shndx_data = elf_newdata(t);
650 buf = calloc(num, entsize);
656 symtab_data->d_buf = buf;
657 symtab_data->d_size = num * entsize;
658 symtab_data->d_align = 1;
659 symtab_data->d_type = ELF_T_SYM;
661 mark_sec_changed(elf, symtab, true);
662 symtab->truncate = true;
665 buf = calloc(num, sizeof(Elf32_Word));
671 shndx_data->d_buf = buf;
672 shndx_data->d_size = num * sizeof(Elf32_Word);
673 shndx_data->d_align = sizeof(Elf32_Word);
674 shndx_data->d_type = ELF_T_WORD;
676 mark_sec_changed(elf, symtab_shndx, true);
677 symtab_shndx->truncate = true;
683 /* empty blocks should not happen */
684 if (!symtab_data->d_size) {
685 WARN("zero size data");
689 /* is this the right block? */
690 max_idx = symtab_data->d_size / entsize;
694 /* adjust index and try again */
698 /* something went side-ways */
700 WARN("negative index");
704 /* setup extended section index magic and write the symbol */
705 if ((shndx >= SHN_UNDEF && shndx < SHN_LORESERVE) || is_special_shndx) {
706 sym->sym.st_shndx = shndx;
710 sym->sym.st_shndx = SHN_XINDEX;
712 WARN("no .symtab_shndx");
717 if (!gelf_update_symshndx(symtab_data, shndx_data, idx, &sym->sym, shndx)) {
718 WARN_ELF("gelf_update_symshndx");
725 static struct symbol *
726 __elf_create_symbol(struct elf *elf, struct symbol *sym)
728 struct section *symtab, *symtab_shndx;
729 Elf32_Word first_non_local, new_idx;
732 symtab = find_section_by_name(elf, ".symtab");
734 symtab_shndx = find_section_by_name(elf, ".symtab_shndx");
740 new_idx = sec_num_entries(symtab);
742 if (GELF_ST_BIND(sym->sym.st_info) != STB_LOCAL)
746 * Move the first global symbol, as per sh_info, into a new, higher
747 * symbol index. This fees up a spot for a new local symbol.
749 first_non_local = symtab->sh.sh_info;
750 old = find_symbol_by_index(elf, first_non_local);
753 elf_hash_del(symbol, &old->hash, old->idx);
754 elf_hash_add(symbol, &old->hash, new_idx);
757 if (elf_update_symbol(elf, symtab, symtab_shndx, old)) {
758 WARN("elf_update_symbol move");
762 if (elf_update_sym_relocs(elf, old))
765 new_idx = first_non_local;
769 * Either way, we will add a LOCAL symbol.
771 symtab->sh.sh_info += 1;
775 if (elf_update_symbol(elf, symtab, symtab_shndx, sym)) {
776 WARN("elf_update_symbol");
780 symtab->sh.sh_size += symtab->sh.sh_entsize;
781 mark_sec_changed(elf, symtab, true);
784 symtab_shndx->sh.sh_size += sizeof(Elf32_Word);
785 mark_sec_changed(elf, symtab_shndx, true);
791 static struct symbol *
792 elf_create_section_symbol(struct elf *elf, struct section *sec)
794 struct symbol *sym = calloc(1, sizeof(*sym));
801 sym->name = sec->name;
805 sym->sym.st_info = GELF_ST_INFO(STB_LOCAL, STT_SECTION);
810 sym = __elf_create_symbol(elf, sym);
812 elf_add_symbol(elf, sym);
817 static int elf_add_string(struct elf *elf, struct section *strtab, char *str);
820 elf_create_prefix_symbol(struct elf *elf, struct symbol *orig, long size)
822 struct symbol *sym = calloc(1, sizeof(*sym));
823 size_t namelen = strlen(orig->name) + sizeof("__pfx_");
824 char *name = malloc(namelen);
831 snprintf(name, namelen, "__pfx_%s", orig->name);
834 sym->sec = orig->sec;
836 sym->sym.st_name = elf_add_string(elf, NULL, name);
837 sym->sym.st_info = orig->sym.st_info;
838 sym->sym.st_value = orig->sym.st_value - size;
839 sym->sym.st_size = size;
841 sym = __elf_create_symbol(elf, sym);
843 elf_add_symbol(elf, sym);
848 static struct reloc *elf_init_reloc(struct elf *elf, struct section *rsec,
849 unsigned int reloc_idx,
850 unsigned long offset, struct symbol *sym,
851 s64 addend, unsigned int type)
853 struct reloc *reloc, empty = { 0 };
855 if (reloc_idx >= sec_num_entries(rsec)) {
856 WARN("%s: bad reloc_idx %u for %s with %d relocs",
857 __func__, reloc_idx, rsec->name, sec_num_entries(rsec));
861 reloc = &rsec->relocs[reloc_idx];
863 if (memcmp(reloc, &empty, sizeof(empty))) {
864 WARN("%s: %s: reloc %d already initialized!",
865 __func__, rsec->name, reloc_idx);
872 set_reloc_offset(elf, reloc, offset);
873 set_reloc_sym(elf, reloc, sym->idx);
874 set_reloc_type(elf, reloc, type);
875 set_reloc_addend(elf, reloc, addend);
877 elf_hash_add(reloc, &reloc->hash, reloc_hash(reloc));
878 reloc->sym_next_reloc = sym->relocs;
884 struct reloc *elf_init_reloc_text_sym(struct elf *elf, struct section *sec,
885 unsigned long offset,
886 unsigned int reloc_idx,
887 struct section *insn_sec,
888 unsigned long insn_off)
890 struct symbol *sym = insn_sec->sym;
891 int addend = insn_off;
893 if (!(insn_sec->sh.sh_flags & SHF_EXECINSTR)) {
894 WARN("bad call to %s() for data symbol %s",
895 __func__, sym->name);
901 * Due to how weak functions work, we must use section based
902 * relocations. Symbol based relocations would result in the
903 * weak and non-weak function annotations being overlaid on the
904 * non-weak function after linking.
906 sym = elf_create_section_symbol(elf, insn_sec);
913 return elf_init_reloc(elf, sec->rsec, reloc_idx, offset, sym, addend,
914 elf_text_rela_type(elf));
917 struct reloc *elf_init_reloc_data_sym(struct elf *elf, struct section *sec,
918 unsigned long offset,
919 unsigned int reloc_idx,
923 if (sym->sec && (sec->sh.sh_flags & SHF_EXECINSTR)) {
924 WARN("bad call to %s() for text symbol %s",
925 __func__, sym->name);
929 return elf_init_reloc(elf, sec->rsec, reloc_idx, offset, sym, addend,
930 elf_data_rela_type(elf));
933 static int read_relocs(struct elf *elf)
935 unsigned long nr_reloc, max_reloc = 0;
936 struct section *rsec;
942 if (!elf_alloc_hash(reloc, elf->num_relocs))
945 list_for_each_entry(rsec, &elf->sections, list) {
946 if (!is_reloc_sec(rsec))
949 rsec->base = find_section_by_index(elf, rsec->sh.sh_info);
951 WARN("can't find base section for reloc section %s",
956 rsec->base->rsec = rsec;
959 rsec->relocs = calloc(sec_num_entries(rsec), sizeof(*reloc));
964 for (i = 0; i < sec_num_entries(rsec); i++) {
965 reloc = &rsec->relocs[i];
968 symndx = reloc_sym(reloc);
969 reloc->sym = sym = find_symbol_by_index(elf, symndx);
971 WARN("can't find reloc entry symbol %d for %s",
976 elf_hash_add(reloc, &reloc->hash, reloc_hash(reloc));
977 reloc->sym_next_reloc = sym->relocs;
982 max_reloc = max(max_reloc, nr_reloc);
986 printf("max_reloc: %lu\n", max_reloc);
987 printf("num_relocs: %lu\n", elf->num_relocs);
988 printf("reloc_bits: %d\n", elf->reloc_bits);
994 struct elf *elf_open_read(const char *name, int flags)
999 elf_version(EV_CURRENT);
1001 elf = malloc(sizeof(*elf));
1006 memset(elf, 0, sizeof(*elf));
1008 INIT_LIST_HEAD(&elf->sections);
1010 elf->fd = open(name, flags);
1011 if (elf->fd == -1) {
1012 fprintf(stderr, "objtool: Can't open '%s': %s\n",
1013 name, strerror(errno));
1017 if ((flags & O_ACCMODE) == O_RDONLY)
1018 cmd = ELF_C_READ_MMAP;
1019 else if ((flags & O_ACCMODE) == O_RDWR)
1024 elf->elf = elf_begin(elf->fd, cmd, NULL);
1026 WARN_ELF("elf_begin");
1030 if (!gelf_getehdr(elf->elf, &elf->ehdr)) {
1031 WARN_ELF("gelf_getehdr");
1035 if (read_sections(elf))
1038 if (read_symbols(elf))
1041 if (read_relocs(elf))
1051 static int elf_add_string(struct elf *elf, struct section *strtab, char *str)
1058 strtab = find_section_by_name(elf, ".strtab");
1060 WARN("can't find .strtab section");
1064 s = elf_getscn(elf->elf, strtab->idx);
1066 WARN_ELF("elf_getscn");
1070 data = elf_newdata(s);
1072 WARN_ELF("elf_newdata");
1077 data->d_size = strlen(str) + 1;
1080 len = strtab->sh.sh_size;
1081 strtab->sh.sh_size += data->d_size;
1083 mark_sec_changed(elf, strtab, true);
1088 struct section *elf_create_section(struct elf *elf, const char *name,
1089 size_t entsize, unsigned int nr)
1091 struct section *sec, *shstrtab;
1092 size_t size = entsize * nr;
1095 sec = malloc(sizeof(*sec));
1100 memset(sec, 0, sizeof(*sec));
1102 INIT_LIST_HEAD(&sec->symbol_list);
1104 s = elf_newscn(elf->elf);
1106 WARN_ELF("elf_newscn");
1110 sec->name = strdup(name);
1116 sec->idx = elf_ndxscn(s);
1118 sec->data = elf_newdata(s);
1120 WARN_ELF("elf_newdata");
1124 sec->data->d_size = size;
1125 sec->data->d_align = 1;
1128 sec->data->d_buf = malloc(size);
1129 if (!sec->data->d_buf) {
1133 memset(sec->data->d_buf, 0, size);
1136 if (!gelf_getshdr(s, &sec->sh)) {
1137 WARN_ELF("gelf_getshdr");
1141 sec->sh.sh_size = size;
1142 sec->sh.sh_entsize = entsize;
1143 sec->sh.sh_type = SHT_PROGBITS;
1144 sec->sh.sh_addralign = 1;
1145 sec->sh.sh_flags = SHF_ALLOC;
1147 /* Add section name to .shstrtab (or .strtab for Clang) */
1148 shstrtab = find_section_by_name(elf, ".shstrtab");
1150 shstrtab = find_section_by_name(elf, ".strtab");
1152 WARN("can't find .shstrtab or .strtab section");
1155 sec->sh.sh_name = elf_add_string(elf, shstrtab, sec->name);
1156 if (sec->sh.sh_name == -1)
1159 list_add_tail(&sec->list, &elf->sections);
1160 elf_hash_add(section, &sec->hash, sec->idx);
1161 elf_hash_add(section_name, &sec->name_hash, str_hash(sec->name));
1163 mark_sec_changed(elf, sec, true);
1168 static struct section *elf_create_rela_section(struct elf *elf,
1169 struct section *sec,
1170 unsigned int reloc_nr)
1172 struct section *rsec;
1175 rsec_name = malloc(strlen(sec->name) + strlen(".rela") + 1);
1180 strcpy(rsec_name, ".rela");
1181 strcat(rsec_name, sec->name);
1183 rsec = elf_create_section(elf, rsec_name, elf_rela_size(elf), reloc_nr);
1188 rsec->data->d_type = ELF_T_RELA;
1189 rsec->sh.sh_type = SHT_RELA;
1190 rsec->sh.sh_addralign = elf_addr_size(elf);
1191 rsec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
1192 rsec->sh.sh_info = sec->idx;
1193 rsec->sh.sh_flags = SHF_INFO_LINK;
1195 rsec->relocs = calloc(sec_num_entries(rsec), sizeof(struct reloc));
1196 if (!rsec->relocs) {
1207 struct section *elf_create_section_pair(struct elf *elf, const char *name,
1208 size_t entsize, unsigned int nr,
1209 unsigned int reloc_nr)
1211 struct section *sec;
1213 sec = elf_create_section(elf, name, entsize, nr);
1217 if (!elf_create_rela_section(elf, sec, reloc_nr))
1223 int elf_write_insn(struct elf *elf, struct section *sec,
1224 unsigned long offset, unsigned int len,
1227 Elf_Data *data = sec->data;
1229 if (data->d_type != ELF_T_BYTE || data->d_off) {
1230 WARN("write to unexpected data for section: %s", sec->name);
1234 memcpy(data->d_buf + offset, insn, len);
1236 mark_sec_changed(elf, sec, true);
1242 * When Elf_Scn::sh_size is smaller than the combined Elf_Data::d_size
1245 * A) adhere to the section header and truncate the data, or
1246 * B) ignore the section header and write out all the data you've got?
1248 * Yes, libelf sucks and we need to manually truncate if we over-allocate data.
1250 static int elf_truncate_section(struct elf *elf, struct section *sec)
1252 u64 size = sec->sh.sh_size;
1253 bool truncated = false;
1254 Elf_Data *data = NULL;
1257 s = elf_getscn(elf->elf, sec->idx);
1259 WARN_ELF("elf_getscn");
1264 /* get next data descriptor for the relevant section */
1265 data = elf_getdata(s, data);
1269 WARN("end of section data but non-zero size left\n");
1276 /* when we remove symbols */
1277 WARN("truncated; but more data\n");
1281 if (!data->d_size) {
1282 WARN("zero size data");
1286 if (data->d_size > size) {
1288 data->d_size = size;
1291 size -= data->d_size;
1295 int elf_write(struct elf *elf)
1297 struct section *sec;
1303 /* Update changed relocation sections and section headers: */
1304 list_for_each_entry(sec, &elf->sections, list) {
1306 elf_truncate_section(elf, sec);
1308 if (sec_changed(sec)) {
1309 s = elf_getscn(elf->elf, sec->idx);
1311 WARN_ELF("elf_getscn");
1315 /* Note this also flags the section dirty */
1316 if (!gelf_update_shdr(s, &sec->sh)) {
1317 WARN_ELF("gelf_update_shdr");
1321 mark_sec_changed(elf, sec, false);
1325 /* Make sure the new section header entries get updated properly. */
1326 elf_flagelf(elf->elf, ELF_C_SET, ELF_F_DIRTY);
1328 /* Write all changes to the file. */
1329 if (elf_update(elf->elf, ELF_C_WRITE) < 0) {
1330 WARN_ELF("elf_update");
1334 elf->changed = false;
1339 void elf_close(struct elf *elf)
1348 * NOTE: All remaining allocations are leaked on purpose. Objtool is
1349 * about to exit anyway.