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 #define MAX_NAME_LEN 128
27 static inline u32 str_hash(const char *str)
29 return jhash(str, strlen(str), 0);
32 #define __elf_table(name) (elf->name##_hash)
33 #define __elf_bits(name) (elf->name##_bits)
35 #define __elf_table_entry(name, key) \
36 __elf_table(name)[hash_min(key, __elf_bits(name))]
38 #define elf_hash_add(name, node, key) \
40 struct elf_hash_node *__node = node; \
41 __node->next = __elf_table_entry(name, key); \
42 __elf_table_entry(name, key) = __node; \
45 static inline void __elf_hash_del(struct elf_hash_node *node,
46 struct elf_hash_node **head)
48 struct elf_hash_node *cur, *prev;
55 for (prev = NULL, cur = *head; cur; prev = cur, cur = cur->next) {
57 prev->next = cur->next;
63 #define elf_hash_del(name, node, key) \
64 __elf_hash_del(node, &__elf_table_entry(name, key))
66 #define elf_list_entry(ptr, type, member) \
68 typeof(ptr) __ptr = (ptr); \
69 __ptr ? container_of(__ptr, type, member) : NULL; \
72 #define elf_hash_for_each_possible(name, obj, member, key) \
73 for (obj = elf_list_entry(__elf_table_entry(name, key), typeof(*obj), member); \
75 obj = elf_list_entry(obj->member.next, typeof(*(obj)), member))
77 #define elf_alloc_hash(name, size) \
79 __elf_bits(name) = max(10, ilog2(size)); \
80 __elf_table(name) = mmap(NULL, sizeof(struct elf_hash_node *) << __elf_bits(name), \
81 PROT_READ|PROT_WRITE, \
82 MAP_PRIVATE|MAP_ANON, -1, 0); \
83 if (__elf_table(name) == (void *)-1L) { \
84 WARN("mmap fail " #name); \
85 __elf_table(name) = NULL; \
90 static inline unsigned long __sym_start(struct symbol *s)
95 static inline unsigned long __sym_last(struct symbol *s)
97 return s->offset + s->len - 1;
100 INTERVAL_TREE_DEFINE(struct symbol, node, unsigned long, __subtree_last,
101 __sym_start, __sym_last, static, __sym)
103 #define __sym_for_each(_iter, _tree, _start, _end) \
104 for (_iter = __sym_iter_first((_tree), (_start), (_end)); \
105 _iter; _iter = __sym_iter_next(_iter, (_start), (_end)))
109 const struct symbol *sym;
113 * Find !section symbol where @offset is after it.
115 static int symbol_hole_by_offset(const void *key, const struct rb_node *node)
117 const struct symbol *s = rb_entry(node, struct symbol, node);
118 struct symbol_hole *sh = (void *)key;
120 if (sh->key < s->offset)
123 if (sh->key >= s->offset + s->len) {
124 if (s->type != STT_SECTION)
132 struct section *find_section_by_name(const struct elf *elf, const char *name)
136 elf_hash_for_each_possible(section_name, sec, name_hash, str_hash(name)) {
137 if (!strcmp(sec->name, name))
144 static struct section *find_section_by_index(struct elf *elf,
149 elf_hash_for_each_possible(section, sec, hash, idx) {
157 static struct symbol *find_symbol_by_index(struct elf *elf, unsigned int idx)
161 elf_hash_for_each_possible(symbol, sym, hash, idx) {
169 struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset)
171 struct rb_root_cached *tree = (struct rb_root_cached *)&sec->symbol_tree;
174 __sym_for_each(iter, tree, offset, offset) {
175 if (iter->offset == offset && iter->type != STT_SECTION)
182 struct symbol *find_func_by_offset(struct section *sec, unsigned long offset)
184 struct rb_root_cached *tree = (struct rb_root_cached *)&sec->symbol_tree;
187 __sym_for_each(iter, tree, offset, offset) {
188 if (iter->offset == offset && iter->type == STT_FUNC)
195 struct symbol *find_symbol_containing(const struct section *sec, unsigned long offset)
197 struct rb_root_cached *tree = (struct rb_root_cached *)&sec->symbol_tree;
200 __sym_for_each(iter, tree, offset, offset) {
201 if (iter->type != STT_SECTION)
209 * Returns size of hole starting at @offset.
211 int find_symbol_hole_containing(const struct section *sec, unsigned long offset)
213 struct symbol_hole hole = {
221 * Find the rightmost symbol for which @offset is after it.
223 n = rb_find(&hole, &sec->symbol_tree.rb_root, symbol_hole_by_offset);
225 /* found a symbol that contains @offset */
227 return 0; /* not a hole */
229 /* didn't find a symbol for which @offset is after it */
231 return 0; /* not a hole */
233 /* @offset >= sym->offset + sym->len, find symbol after it */
234 n = rb_next(&hole.sym->node);
236 return -1; /* until end of address space */
238 /* hole until start of next symbol */
239 s = rb_entry(n, struct symbol, node);
240 return s->offset - offset;
243 struct symbol *find_func_containing(struct section *sec, unsigned long offset)
245 struct rb_root_cached *tree = (struct rb_root_cached *)&sec->symbol_tree;
248 __sym_for_each(iter, tree, offset, offset) {
249 if (iter->type == STT_FUNC)
256 struct symbol *find_symbol_by_name(const struct elf *elf, const char *name)
260 elf_hash_for_each_possible(symbol_name, sym, name_hash, str_hash(name)) {
261 if (!strcmp(sym->name, name))
268 struct reloc *find_reloc_by_dest_range(const struct elf *elf, struct section *sec,
269 unsigned long offset, unsigned int len)
271 struct reloc *reloc, *r = NULL;
272 struct section *rsec;
279 for_offset_range(o, offset, offset + len) {
280 elf_hash_for_each_possible(reloc, reloc, hash,
281 sec_offset_hash(rsec, o)) {
282 if (reloc->sec != rsec)
285 if (reloc_offset(reloc) >= offset &&
286 reloc_offset(reloc) < offset + len) {
287 if (!r || reloc_offset(reloc) < reloc_offset(r))
298 struct reloc *find_reloc_by_dest(const struct elf *elf, struct section *sec, unsigned long offset)
300 return find_reloc_by_dest_range(elf, sec, offset, 1);
303 static bool is_dwarf_section(struct section *sec)
305 return !strncmp(sec->name, ".debug_", 7);
308 static int read_sections(struct elf *elf)
312 size_t shstrndx, sections_nr;
315 if (elf_getshdrnum(elf->elf, §ions_nr)) {
316 WARN_ELF("elf_getshdrnum");
320 if (elf_getshdrstrndx(elf->elf, &shstrndx)) {
321 WARN_ELF("elf_getshdrstrndx");
325 if (!elf_alloc_hash(section, sections_nr) ||
326 !elf_alloc_hash(section_name, sections_nr))
329 elf->section_data = calloc(sections_nr, sizeof(*sec));
330 if (!elf->section_data) {
334 for (i = 0; i < sections_nr; i++) {
335 sec = &elf->section_data[i];
337 INIT_LIST_HEAD(&sec->symbol_list);
339 s = elf_getscn(elf->elf, i);
341 WARN_ELF("elf_getscn");
345 sec->idx = elf_ndxscn(s);
347 if (!gelf_getshdr(s, &sec->sh)) {
348 WARN_ELF("gelf_getshdr");
352 sec->name = elf_strptr(elf->elf, shstrndx, sec->sh.sh_name);
354 WARN_ELF("elf_strptr");
358 if (sec->sh.sh_size != 0 && !is_dwarf_section(sec)) {
359 sec->data = elf_getdata(s, NULL);
361 WARN_ELF("elf_getdata");
364 if (sec->data->d_off != 0 ||
365 sec->data->d_size != sec->sh.sh_size) {
366 WARN("unexpected data attributes for %s",
372 list_add_tail(&sec->list, &elf->sections);
373 elf_hash_add(section, &sec->hash, sec->idx);
374 elf_hash_add(section_name, &sec->name_hash, str_hash(sec->name));
376 if (is_reloc_sec(sec))
377 elf->num_relocs += sec_num_entries(sec);
381 printf("nr_sections: %lu\n", (unsigned long)sections_nr);
382 printf("section_bits: %d\n", elf->section_bits);
385 /* sanity check, one more call to elf_nextscn() should return NULL */
386 if (elf_nextscn(elf->elf, s)) {
387 WARN("section entry mismatch");
394 static void elf_add_symbol(struct elf *elf, struct symbol *sym)
396 struct list_head *entry;
397 struct rb_node *pnode;
400 INIT_LIST_HEAD(&sym->pv_target);
403 sym->type = GELF_ST_TYPE(sym->sym.st_info);
404 sym->bind = GELF_ST_BIND(sym->sym.st_info);
406 if (sym->type == STT_FILE)
409 sym->offset = sym->sym.st_value;
410 sym->len = sym->sym.st_size;
412 __sym_for_each(iter, &sym->sec->symbol_tree, sym->offset, sym->offset) {
413 if (iter->offset == sym->offset && iter->type == sym->type)
417 __sym_insert(sym, &sym->sec->symbol_tree);
418 pnode = rb_prev(&sym->node);
420 entry = &rb_entry(pnode, struct symbol, node)->list;
422 entry = &sym->sec->symbol_list;
423 list_add(&sym->list, entry);
424 elf_hash_add(symbol, &sym->hash, sym->idx);
425 elf_hash_add(symbol_name, &sym->name_hash, str_hash(sym->name));
428 * Don't store empty STT_NOTYPE symbols in the rbtree. They
429 * can exist within a function, confusing the sorting.
432 __sym_remove(sym, &sym->sec->symbol_tree);
435 static int read_symbols(struct elf *elf)
437 struct section *symtab, *symtab_shndx, *sec;
438 struct symbol *sym, *pfunc;
441 Elf_Data *shndx_data = NULL;
444 symtab = find_section_by_name(elf, ".symtab");
446 symtab_shndx = find_section_by_name(elf, ".symtab_shndx");
448 shndx_data = symtab_shndx->data;
450 symbols_nr = sec_num_entries(symtab);
453 * A missing symbol table is actually possible if it's an empty
454 * .o file. This can happen for thunk_64.o. Make sure to at
455 * least allocate the symbol hash tables so we can do symbol
456 * lookups without crashing.
461 if (!elf_alloc_hash(symbol, symbols_nr) ||
462 !elf_alloc_hash(symbol_name, symbols_nr))
465 elf->symbol_data = calloc(symbols_nr, sizeof(*sym));
466 if (!elf->symbol_data) {
470 for (i = 0; i < symbols_nr; i++) {
471 sym = &elf->symbol_data[i];
475 if (!gelf_getsymshndx(symtab->data, shndx_data, i, &sym->sym,
477 WARN_ELF("gelf_getsymshndx");
481 sym->name = elf_strptr(elf->elf, symtab->sh.sh_link,
484 WARN_ELF("elf_strptr");
488 if ((sym->sym.st_shndx > SHN_UNDEF &&
489 sym->sym.st_shndx < SHN_LORESERVE) ||
490 (shndx_data && sym->sym.st_shndx == SHN_XINDEX)) {
491 if (sym->sym.st_shndx != SHN_XINDEX)
492 shndx = sym->sym.st_shndx;
494 sym->sec = find_section_by_index(elf, shndx);
496 WARN("couldn't find section for symbol %s",
500 if (GELF_ST_TYPE(sym->sym.st_info) == STT_SECTION) {
501 sym->name = sym->sec->name;
505 sym->sec = find_section_by_index(elf, 0);
507 elf_add_symbol(elf, sym);
511 printf("nr_symbols: %lu\n", (unsigned long)symbols_nr);
512 printf("symbol_bits: %d\n", elf->symbol_bits);
515 /* Create parent/child links for any cold subfunctions */
516 list_for_each_entry(sec, &elf->sections, list) {
517 sec_for_each_sym(sec, sym) {
518 char pname[MAX_NAME_LEN + 1];
520 if (sym->type != STT_FUNC)
523 if (sym->pfunc == NULL)
526 if (sym->cfunc == NULL)
529 coldstr = strstr(sym->name, ".cold");
533 pnamelen = coldstr - sym->name;
534 if (pnamelen > MAX_NAME_LEN) {
535 WARN("%s(): parent function name exceeds maximum length of %d characters",
536 sym->name, MAX_NAME_LEN);
540 strncpy(pname, sym->name, pnamelen);
541 pname[pnamelen] = '\0';
542 pfunc = find_symbol_by_name(elf, pname);
545 WARN("%s(): can't find parent function",
554 * Unfortunately, -fnoreorder-functions puts the child
555 * inside the parent. Remove the overlap so we can
556 * have sane assumptions.
558 * Note that pfunc->len now no longer matches
559 * pfunc->sym.st_size.
561 if (sym->sec == pfunc->sec &&
562 sym->offset >= pfunc->offset &&
563 sym->offset + sym->len == pfunc->offset + pfunc->len) {
564 pfunc->len -= sym->len;
577 * @sym's idx has changed. Update the relocs which reference it.
579 static int elf_update_sym_relocs(struct elf *elf, struct symbol *sym)
583 for (reloc = sym->relocs; reloc; reloc = reloc->sym_next_reloc)
584 set_reloc_sym(elf, reloc, reloc->sym->idx);
590 * The libelf API is terrible; gelf_update_sym*() takes a data block relative
591 * index value, *NOT* the symbol index. As such, iterate the data blocks and
592 * adjust index until it fits.
594 * If no data block is found, allow adding a new data block provided the index
595 * is only one past the end.
597 static int elf_update_symbol(struct elf *elf, struct section *symtab,
598 struct section *symtab_shndx, struct symbol *sym)
600 Elf32_Word shndx = sym->sec ? sym->sec->idx : SHN_UNDEF;
601 Elf_Data *symtab_data = NULL, *shndx_data = NULL;
602 Elf64_Xword entsize = symtab->sh.sh_entsize;
603 int max_idx, idx = sym->idx;
604 Elf_Scn *s, *t = NULL;
605 bool is_special_shndx = sym->sym.st_shndx >= SHN_LORESERVE &&
606 sym->sym.st_shndx != SHN_XINDEX;
608 if (is_special_shndx)
609 shndx = sym->sym.st_shndx;
611 s = elf_getscn(elf->elf, symtab->idx);
613 WARN_ELF("elf_getscn");
618 t = elf_getscn(elf->elf, symtab_shndx->idx);
620 WARN_ELF("elf_getscn");
626 /* get next data descriptor for the relevant sections */
627 symtab_data = elf_getdata(s, symtab_data);
629 shndx_data = elf_getdata(t, shndx_data);
634 * Over-allocate to avoid O(n^2) symbol creation
635 * behaviour. The down side is that libelf doesn't
636 * like this; see elf_truncate_section() for the fixup.
638 int num = max(1U, sym->idx/3);
642 /* we don't do holes in symbol tables */
643 WARN("index out of range");
647 /* if @idx == 0, it's the next contiguous entry, create it */
648 symtab_data = elf_newdata(s);
650 shndx_data = elf_newdata(t);
652 buf = calloc(num, entsize);
658 symtab_data->d_buf = buf;
659 symtab_data->d_size = num * entsize;
660 symtab_data->d_align = 1;
661 symtab_data->d_type = ELF_T_SYM;
663 mark_sec_changed(elf, symtab, true);
664 symtab->truncate = true;
667 buf = calloc(num, sizeof(Elf32_Word));
673 shndx_data->d_buf = buf;
674 shndx_data->d_size = num * sizeof(Elf32_Word);
675 shndx_data->d_align = sizeof(Elf32_Word);
676 shndx_data->d_type = ELF_T_WORD;
678 mark_sec_changed(elf, symtab_shndx, true);
679 symtab_shndx->truncate = true;
685 /* empty blocks should not happen */
686 if (!symtab_data->d_size) {
687 WARN("zero size data");
691 /* is this the right block? */
692 max_idx = symtab_data->d_size / entsize;
696 /* adjust index and try again */
700 /* something went side-ways */
702 WARN("negative index");
706 /* setup extended section index magic and write the symbol */
707 if ((shndx >= SHN_UNDEF && shndx < SHN_LORESERVE) || is_special_shndx) {
708 sym->sym.st_shndx = shndx;
712 sym->sym.st_shndx = SHN_XINDEX;
714 WARN("no .symtab_shndx");
719 if (!gelf_update_symshndx(symtab_data, shndx_data, idx, &sym->sym, shndx)) {
720 WARN_ELF("gelf_update_symshndx");
727 static struct symbol *
728 __elf_create_symbol(struct elf *elf, struct symbol *sym)
730 struct section *symtab, *symtab_shndx;
731 Elf32_Word first_non_local, new_idx;
734 symtab = find_section_by_name(elf, ".symtab");
736 symtab_shndx = find_section_by_name(elf, ".symtab_shndx");
742 new_idx = sec_num_entries(symtab);
744 if (GELF_ST_BIND(sym->sym.st_info) != STB_LOCAL)
748 * Move the first global symbol, as per sh_info, into a new, higher
749 * symbol index. This fees up a spot for a new local symbol.
751 first_non_local = symtab->sh.sh_info;
752 old = find_symbol_by_index(elf, first_non_local);
755 elf_hash_del(symbol, &old->hash, old->idx);
756 elf_hash_add(symbol, &old->hash, new_idx);
759 if (elf_update_symbol(elf, symtab, symtab_shndx, old)) {
760 WARN("elf_update_symbol move");
764 if (elf_update_sym_relocs(elf, old))
767 new_idx = first_non_local;
771 * Either way, we will add a LOCAL symbol.
773 symtab->sh.sh_info += 1;
777 if (elf_update_symbol(elf, symtab, symtab_shndx, sym)) {
778 WARN("elf_update_symbol");
782 symtab->sh.sh_size += symtab->sh.sh_entsize;
783 mark_sec_changed(elf, symtab, true);
786 symtab_shndx->sh.sh_size += sizeof(Elf32_Word);
787 mark_sec_changed(elf, symtab_shndx, true);
793 static struct symbol *
794 elf_create_section_symbol(struct elf *elf, struct section *sec)
796 struct symbol *sym = calloc(1, sizeof(*sym));
803 sym->name = sec->name;
807 sym->sym.st_info = GELF_ST_INFO(STB_LOCAL, STT_SECTION);
812 sym = __elf_create_symbol(elf, sym);
814 elf_add_symbol(elf, sym);
819 static int elf_add_string(struct elf *elf, struct section *strtab, char *str);
822 elf_create_prefix_symbol(struct elf *elf, struct symbol *orig, long size)
824 struct symbol *sym = calloc(1, sizeof(*sym));
825 size_t namelen = strlen(orig->name) + sizeof("__pfx_");
826 char *name = malloc(namelen);
833 snprintf(name, namelen, "__pfx_%s", orig->name);
836 sym->sec = orig->sec;
838 sym->sym.st_name = elf_add_string(elf, NULL, name);
839 sym->sym.st_info = orig->sym.st_info;
840 sym->sym.st_value = orig->sym.st_value - size;
841 sym->sym.st_size = size;
843 sym = __elf_create_symbol(elf, sym);
845 elf_add_symbol(elf, sym);
850 static struct reloc *elf_init_reloc(struct elf *elf, struct section *rsec,
851 unsigned int reloc_idx,
852 unsigned long offset, struct symbol *sym,
853 s64 addend, unsigned int type)
855 struct reloc *reloc, empty = { 0 };
857 if (reloc_idx >= sec_num_entries(rsec)) {
858 WARN("%s: bad reloc_idx %u for %s with %d relocs",
859 __func__, reloc_idx, rsec->name, sec_num_entries(rsec));
863 reloc = &rsec->relocs[reloc_idx];
865 if (memcmp(reloc, &empty, sizeof(empty))) {
866 WARN("%s: %s: reloc %d already initialized!",
867 __func__, rsec->name, reloc_idx);
874 set_reloc_offset(elf, reloc, offset);
875 set_reloc_sym(elf, reloc, sym->idx);
876 set_reloc_type(elf, reloc, type);
877 set_reloc_addend(elf, reloc, addend);
879 elf_hash_add(reloc, &reloc->hash, reloc_hash(reloc));
880 reloc->sym_next_reloc = sym->relocs;
886 struct reloc *elf_init_reloc_text_sym(struct elf *elf, struct section *sec,
887 unsigned long offset,
888 unsigned int reloc_idx,
889 struct section *insn_sec,
890 unsigned long insn_off)
892 struct symbol *sym = insn_sec->sym;
893 int addend = insn_off;
895 if (!(insn_sec->sh.sh_flags & SHF_EXECINSTR)) {
896 WARN("bad call to %s() for data symbol %s",
897 __func__, sym->name);
903 * Due to how weak functions work, we must use section based
904 * relocations. Symbol based relocations would result in the
905 * weak and non-weak function annotations being overlaid on the
906 * non-weak function after linking.
908 sym = elf_create_section_symbol(elf, insn_sec);
915 return elf_init_reloc(elf, sec->rsec, reloc_idx, offset, sym, addend,
916 elf_text_rela_type(elf));
919 struct reloc *elf_init_reloc_data_sym(struct elf *elf, struct section *sec,
920 unsigned long offset,
921 unsigned int reloc_idx,
925 if (sym->sec && (sec->sh.sh_flags & SHF_EXECINSTR)) {
926 WARN("bad call to %s() for text symbol %s",
927 __func__, sym->name);
931 return elf_init_reloc(elf, sec->rsec, reloc_idx, offset, sym, addend,
932 elf_data_rela_type(elf));
935 static int read_relocs(struct elf *elf)
937 unsigned long nr_reloc, max_reloc = 0;
938 struct section *rsec;
944 if (!elf_alloc_hash(reloc, elf->num_relocs))
947 list_for_each_entry(rsec, &elf->sections, list) {
948 if (!is_reloc_sec(rsec))
951 rsec->base = find_section_by_index(elf, rsec->sh.sh_info);
953 WARN("can't find base section for reloc section %s",
958 rsec->base->rsec = rsec;
961 rsec->relocs = calloc(sec_num_entries(rsec), sizeof(*reloc));
966 for (i = 0; i < sec_num_entries(rsec); i++) {
967 reloc = &rsec->relocs[i];
970 symndx = reloc_sym(reloc);
971 reloc->sym = sym = find_symbol_by_index(elf, symndx);
973 WARN("can't find reloc entry symbol %d for %s",
978 elf_hash_add(reloc, &reloc->hash, reloc_hash(reloc));
979 reloc->sym_next_reloc = sym->relocs;
984 max_reloc = max(max_reloc, nr_reloc);
988 printf("max_reloc: %lu\n", max_reloc);
989 printf("num_relocs: %lu\n", elf->num_relocs);
990 printf("reloc_bits: %d\n", elf->reloc_bits);
996 struct elf *elf_open_read(const char *name, int flags)
1001 elf_version(EV_CURRENT);
1003 elf = malloc(sizeof(*elf));
1008 memset(elf, 0, offsetof(struct elf, sections));
1010 INIT_LIST_HEAD(&elf->sections);
1012 elf->fd = open(name, flags);
1013 if (elf->fd == -1) {
1014 fprintf(stderr, "objtool: Can't open '%s': %s\n",
1015 name, strerror(errno));
1019 if ((flags & O_ACCMODE) == O_RDONLY)
1020 cmd = ELF_C_READ_MMAP;
1021 else if ((flags & O_ACCMODE) == O_RDWR)
1026 elf->elf = elf_begin(elf->fd, cmd, NULL);
1028 WARN_ELF("elf_begin");
1032 if (!gelf_getehdr(elf->elf, &elf->ehdr)) {
1033 WARN_ELF("gelf_getehdr");
1037 if (read_sections(elf))
1040 if (read_symbols(elf))
1043 if (read_relocs(elf))
1053 static int elf_add_string(struct elf *elf, struct section *strtab, char *str)
1060 strtab = find_section_by_name(elf, ".strtab");
1062 WARN("can't find .strtab section");
1066 s = elf_getscn(elf->elf, strtab->idx);
1068 WARN_ELF("elf_getscn");
1072 data = elf_newdata(s);
1074 WARN_ELF("elf_newdata");
1079 data->d_size = strlen(str) + 1;
1082 len = strtab->sh.sh_size;
1083 strtab->sh.sh_size += data->d_size;
1085 mark_sec_changed(elf, strtab, true);
1090 struct section *elf_create_section(struct elf *elf, const char *name,
1091 size_t entsize, unsigned int nr)
1093 struct section *sec, *shstrtab;
1094 size_t size = entsize * nr;
1097 sec = malloc(sizeof(*sec));
1102 memset(sec, 0, sizeof(*sec));
1104 INIT_LIST_HEAD(&sec->symbol_list);
1106 s = elf_newscn(elf->elf);
1108 WARN_ELF("elf_newscn");
1112 sec->name = strdup(name);
1118 sec->idx = elf_ndxscn(s);
1120 sec->data = elf_newdata(s);
1122 WARN_ELF("elf_newdata");
1126 sec->data->d_size = size;
1127 sec->data->d_align = 1;
1130 sec->data->d_buf = malloc(size);
1131 if (!sec->data->d_buf) {
1135 memset(sec->data->d_buf, 0, size);
1138 if (!gelf_getshdr(s, &sec->sh)) {
1139 WARN_ELF("gelf_getshdr");
1143 sec->sh.sh_size = size;
1144 sec->sh.sh_entsize = entsize;
1145 sec->sh.sh_type = SHT_PROGBITS;
1146 sec->sh.sh_addralign = 1;
1147 sec->sh.sh_flags = SHF_ALLOC;
1149 /* Add section name to .shstrtab (or .strtab for Clang) */
1150 shstrtab = find_section_by_name(elf, ".shstrtab");
1152 shstrtab = find_section_by_name(elf, ".strtab");
1154 WARN("can't find .shstrtab or .strtab section");
1157 sec->sh.sh_name = elf_add_string(elf, shstrtab, sec->name);
1158 if (sec->sh.sh_name == -1)
1161 list_add_tail(&sec->list, &elf->sections);
1162 elf_hash_add(section, &sec->hash, sec->idx);
1163 elf_hash_add(section_name, &sec->name_hash, str_hash(sec->name));
1165 mark_sec_changed(elf, sec, true);
1170 static struct section *elf_create_rela_section(struct elf *elf,
1171 struct section *sec,
1172 unsigned int reloc_nr)
1174 struct section *rsec;
1177 rsec_name = malloc(strlen(sec->name) + strlen(".rela") + 1);
1182 strcpy(rsec_name, ".rela");
1183 strcat(rsec_name, sec->name);
1185 rsec = elf_create_section(elf, rsec_name, elf_rela_size(elf), reloc_nr);
1190 rsec->data->d_type = ELF_T_RELA;
1191 rsec->sh.sh_type = SHT_RELA;
1192 rsec->sh.sh_addralign = elf_addr_size(elf);
1193 rsec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
1194 rsec->sh.sh_info = sec->idx;
1195 rsec->sh.sh_flags = SHF_INFO_LINK;
1197 rsec->relocs = calloc(sec_num_entries(rsec), sizeof(struct reloc));
1198 if (!rsec->relocs) {
1209 struct section *elf_create_section_pair(struct elf *elf, const char *name,
1210 size_t entsize, unsigned int nr,
1211 unsigned int reloc_nr)
1213 struct section *sec;
1215 sec = elf_create_section(elf, name, entsize, nr);
1219 if (!elf_create_rela_section(elf, sec, reloc_nr))
1225 int elf_write_insn(struct elf *elf, struct section *sec,
1226 unsigned long offset, unsigned int len,
1229 Elf_Data *data = sec->data;
1231 if (data->d_type != ELF_T_BYTE || data->d_off) {
1232 WARN("write to unexpected data for section: %s", sec->name);
1236 memcpy(data->d_buf + offset, insn, len);
1238 mark_sec_changed(elf, sec, true);
1244 * When Elf_Scn::sh_size is smaller than the combined Elf_Data::d_size
1247 * A) adhere to the section header and truncate the data, or
1248 * B) ignore the section header and write out all the data you've got?
1250 * Yes, libelf sucks and we need to manually truncate if we over-allocate data.
1252 static int elf_truncate_section(struct elf *elf, struct section *sec)
1254 u64 size = sec->sh.sh_size;
1255 bool truncated = false;
1256 Elf_Data *data = NULL;
1259 s = elf_getscn(elf->elf, sec->idx);
1261 WARN_ELF("elf_getscn");
1266 /* get next data descriptor for the relevant section */
1267 data = elf_getdata(s, data);
1271 WARN("end of section data but non-zero size left\n");
1278 /* when we remove symbols */
1279 WARN("truncated; but more data\n");
1283 if (!data->d_size) {
1284 WARN("zero size data");
1288 if (data->d_size > size) {
1290 data->d_size = size;
1293 size -= data->d_size;
1297 int elf_write(struct elf *elf)
1299 struct section *sec;
1305 /* Update changed relocation sections and section headers: */
1306 list_for_each_entry(sec, &elf->sections, list) {
1308 elf_truncate_section(elf, sec);
1310 if (sec_changed(sec)) {
1311 s = elf_getscn(elf->elf, sec->idx);
1313 WARN_ELF("elf_getscn");
1317 /* Note this also flags the section dirty */
1318 if (!gelf_update_shdr(s, &sec->sh)) {
1319 WARN_ELF("gelf_update_shdr");
1323 mark_sec_changed(elf, sec, false);
1327 /* Make sure the new section header entries get updated properly. */
1328 elf_flagelf(elf->elf, ELF_C_SET, ELF_F_DIRTY);
1330 /* Write all changes to the file. */
1331 if (elf_update(elf->elf, ELF_C_WRITE) < 0) {
1332 WARN_ELF("elf_update");
1336 elf->changed = false;
1341 void elf_close(struct elf *elf)
1350 * NOTE: All remaining allocations are leaked on purpose. Objtool is
1351 * about to exit anyway.