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 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 void rb_add(struct rb_root *tree, struct rb_node *node,
47 int (*cmp)(struct rb_node *, const struct rb_node *))
49 struct rb_node **link = &tree->rb_node;
50 struct rb_node *parent = NULL;
54 if (cmp(node, parent) < 0)
55 link = &parent->rb_left;
57 link = &parent->rb_right;
60 rb_link_node(node, parent, link);
61 rb_insert_color(node, tree);
64 static struct rb_node *rb_find_first(const struct rb_root *tree, const void *key,
65 int (*cmp)(const void *key, const struct rb_node *))
67 struct rb_node *node = tree->rb_node;
68 struct rb_node *match = NULL;
71 int c = cmp(key, node);
77 node = node->rb_right;
84 static struct rb_node *rb_next_match(struct rb_node *node, const void *key,
85 int (*cmp)(const void *key, const struct rb_node *))
88 if (node && cmp(key, node))
93 #define rb_for_each(tree, node, key, cmp) \
94 for ((node) = rb_find_first((tree), (key), (cmp)); \
95 (node); (node) = rb_next_match((node), (key), (cmp)))
97 static int symbol_to_offset(struct rb_node *a, const struct rb_node *b)
99 struct symbol *sa = rb_entry(a, struct symbol, node);
100 struct symbol *sb = rb_entry(b, struct symbol, node);
102 if (sa->offset < sb->offset)
104 if (sa->offset > sb->offset)
107 if (sa->len < sb->len)
109 if (sa->len > sb->len)
117 static int symbol_by_offset(const void *key, const struct rb_node *node)
119 const struct symbol *s = rb_entry(node, struct symbol, node);
120 const unsigned long *o = key;
124 if (*o >= s->offset + s->len)
130 struct section *find_section_by_name(const struct elf *elf, const char *name)
134 elf_hash_for_each_possible(elf->section_name_hash, sec, name_hash, str_hash(name))
135 if (!strcmp(sec->name, name))
141 static struct section *find_section_by_index(struct elf *elf,
146 elf_hash_for_each_possible(elf->section_hash, sec, hash, idx)
153 static struct symbol *find_symbol_by_index(struct elf *elf, unsigned int idx)
157 elf_hash_for_each_possible(elf->symbol_hash, sym, hash, idx)
164 struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset)
166 struct rb_node *node;
168 rb_for_each(&sec->symbol_tree, node, &offset, symbol_by_offset) {
169 struct symbol *s = rb_entry(node, struct symbol, node);
171 if (s->offset == offset && s->type != STT_SECTION)
178 struct symbol *find_func_by_offset(struct section *sec, unsigned long offset)
180 struct rb_node *node;
182 rb_for_each(&sec->symbol_tree, node, &offset, symbol_by_offset) {
183 struct symbol *s = rb_entry(node, struct symbol, node);
185 if (s->offset == offset && s->type == STT_FUNC)
192 struct symbol *find_symbol_containing(const struct section *sec, unsigned long offset)
194 struct rb_node *node;
196 rb_for_each(&sec->symbol_tree, node, &offset, symbol_by_offset) {
197 struct symbol *s = rb_entry(node, struct symbol, node);
199 if (s->type != STT_SECTION)
206 struct symbol *find_func_containing(struct section *sec, unsigned long offset)
208 struct rb_node *node;
210 rb_for_each(&sec->symbol_tree, node, &offset, symbol_by_offset) {
211 struct symbol *s = rb_entry(node, struct symbol, node);
213 if (s->type == STT_FUNC)
220 struct symbol *find_symbol_by_name(const struct elf *elf, const char *name)
224 elf_hash_for_each_possible(elf->symbol_name_hash, sym, name_hash, str_hash(name))
225 if (!strcmp(sym->name, name))
231 struct reloc *find_reloc_by_dest_range(const struct elf *elf, struct section *sec,
232 unsigned long offset, unsigned int len)
234 struct reloc *reloc, *r = NULL;
242 for_offset_range(o, offset, offset + len) {
243 elf_hash_for_each_possible(elf->reloc_hash, reloc, hash,
244 sec_offset_hash(sec, o)) {
245 if (reloc->sec != sec)
248 if (reloc->offset >= offset && reloc->offset < offset + len) {
249 if (!r || reloc->offset < r->offset)
260 struct reloc *find_reloc_by_dest(const struct elf *elf, struct section *sec, unsigned long offset)
262 return find_reloc_by_dest_range(elf, sec, offset, 1);
265 static int read_sections(struct elf *elf)
269 size_t shstrndx, sections_nr;
272 if (elf_getshdrnum(elf->elf, §ions_nr)) {
273 WARN_ELF("elf_getshdrnum");
277 if (elf_getshdrstrndx(elf->elf, &shstrndx)) {
278 WARN_ELF("elf_getshdrstrndx");
282 for (i = 0; i < sections_nr; i++) {
283 sec = malloc(sizeof(*sec));
288 memset(sec, 0, sizeof(*sec));
290 INIT_LIST_HEAD(&sec->symbol_list);
291 INIT_LIST_HEAD(&sec->reloc_list);
293 s = elf_getscn(elf->elf, i);
295 WARN_ELF("elf_getscn");
299 sec->idx = elf_ndxscn(s);
301 if (!gelf_getshdr(s, &sec->sh)) {
302 WARN_ELF("gelf_getshdr");
306 sec->name = elf_strptr(elf->elf, shstrndx, sec->sh.sh_name);
308 WARN_ELF("elf_strptr");
312 if (sec->sh.sh_size != 0) {
313 sec->data = elf_getdata(s, NULL);
315 WARN_ELF("elf_getdata");
318 if (sec->data->d_off != 0 ||
319 sec->data->d_size != sec->sh.sh_size) {
320 WARN("unexpected data attributes for %s",
325 sec->len = sec->sh.sh_size;
327 list_add_tail(&sec->list, &elf->sections);
328 elf_hash_add(elf->section_hash, &sec->hash, sec->idx);
329 elf_hash_add(elf->section_name_hash, &sec->name_hash, str_hash(sec->name));
333 printf("nr_sections: %lu\n", (unsigned long)sections_nr);
335 /* sanity check, one more call to elf_nextscn() should return NULL */
336 if (elf_nextscn(elf->elf, s)) {
337 WARN("section entry mismatch");
344 static void elf_add_symbol(struct elf *elf, struct symbol *sym)
346 struct list_head *entry;
347 struct rb_node *pnode;
351 sym->type = GELF_ST_TYPE(sym->sym.st_info);
352 sym->bind = GELF_ST_BIND(sym->sym.st_info);
354 sym->offset = sym->sym.st_value;
355 sym->len = sym->sym.st_size;
357 rb_add(&sym->sec->symbol_tree, &sym->node, symbol_to_offset);
358 pnode = rb_prev(&sym->node);
360 entry = &rb_entry(pnode, struct symbol, node)->list;
362 entry = &sym->sec->symbol_list;
363 list_add(&sym->list, entry);
364 elf_hash_add(elf->symbol_hash, &sym->hash, sym->idx);
365 elf_hash_add(elf->symbol_name_hash, &sym->name_hash, str_hash(sym->name));
368 * Don't store empty STT_NOTYPE symbols in the rbtree. They
369 * can exist within a function, confusing the sorting.
372 rb_erase(&sym->node, &sym->sec->symbol_tree);
375 static int read_symbols(struct elf *elf)
377 struct section *symtab, *symtab_shndx, *sec;
378 struct symbol *sym, *pfunc;
381 Elf_Data *shndx_data = NULL;
384 symtab = find_section_by_name(elf, ".symtab");
387 * A missing symbol table is actually possible if it's an empty
388 * .o file. This can happen for thunk_64.o.
393 symtab_shndx = find_section_by_name(elf, ".symtab_shndx");
395 shndx_data = symtab_shndx->data;
397 symbols_nr = symtab->sh.sh_size / symtab->sh.sh_entsize;
399 for (i = 0; i < symbols_nr; i++) {
400 sym = malloc(sizeof(*sym));
405 memset(sym, 0, sizeof(*sym));
409 if (!gelf_getsymshndx(symtab->data, shndx_data, i, &sym->sym,
411 WARN_ELF("gelf_getsymshndx");
415 sym->name = elf_strptr(elf->elf, symtab->sh.sh_link,
418 WARN_ELF("elf_strptr");
422 if ((sym->sym.st_shndx > SHN_UNDEF &&
423 sym->sym.st_shndx < SHN_LORESERVE) ||
424 (shndx_data && sym->sym.st_shndx == SHN_XINDEX)) {
425 if (sym->sym.st_shndx != SHN_XINDEX)
426 shndx = sym->sym.st_shndx;
428 sym->sec = find_section_by_index(elf, shndx);
430 WARN("couldn't find section for symbol %s",
434 if (GELF_ST_TYPE(sym->sym.st_info) == STT_SECTION) {
435 sym->name = sym->sec->name;
439 sym->sec = find_section_by_index(elf, 0);
441 elf_add_symbol(elf, sym);
445 printf("nr_symbols: %lu\n", (unsigned long)symbols_nr);
447 /* Create parent/child links for any cold subfunctions */
448 list_for_each_entry(sec, &elf->sections, list) {
449 list_for_each_entry(sym, &sec->symbol_list, list) {
450 char pname[MAX_NAME_LEN + 1];
452 if (sym->type != STT_FUNC)
455 if (sym->pfunc == NULL)
458 if (sym->cfunc == NULL)
461 coldstr = strstr(sym->name, ".cold");
465 pnamelen = coldstr - sym->name;
466 if (pnamelen > MAX_NAME_LEN) {
467 WARN("%s(): parent function name exceeds maximum length of %d characters",
468 sym->name, MAX_NAME_LEN);
472 strncpy(pname, sym->name, pnamelen);
473 pname[pnamelen] = '\0';
474 pfunc = find_symbol_by_name(elf, pname);
477 WARN("%s(): can't find parent function",
486 * Unfortunately, -fnoreorder-functions puts the child
487 * inside the parent. Remove the overlap so we can
488 * have sane assumptions.
490 * Note that pfunc->len now no longer matches
491 * pfunc->sym.st_size.
493 if (sym->sec == pfunc->sec &&
494 sym->offset >= pfunc->offset &&
495 sym->offset + sym->len == pfunc->offset + pfunc->len) {
496 pfunc->len -= sym->len;
508 static struct section *elf_create_reloc_section(struct elf *elf,
509 struct section *base,
512 int elf_add_reloc(struct elf *elf, struct section *sec, unsigned long offset,
513 unsigned int type, struct symbol *sym, s64 addend)
517 if (!sec->reloc && !elf_create_reloc_section(elf, sec, SHT_RELA))
520 reloc = malloc(sizeof(*reloc));
525 memset(reloc, 0, sizeof(*reloc));
527 reloc->sec = sec->reloc;
528 reloc->offset = offset;
531 reloc->addend = addend;
533 list_add_tail(&reloc->list, &sec->reloc->reloc_list);
534 elf_hash_add(elf->reloc_hash, &reloc->hash, reloc_hash(reloc));
536 sec->reloc->changed = true;
542 * Ensure that any reloc section containing references to @sym is marked
543 * changed such that it will get re-generated in elf_rebuild_reloc_sections()
544 * with the new symbol index.
546 static void elf_dirty_reloc_sym(struct elf *elf, struct symbol *sym)
550 list_for_each_entry(sec, &elf->sections, list) {
556 list_for_each_entry(reloc, &sec->reloc_list, list) {
557 if (reloc->sym == sym) {
566 * The libelf API is terrible; gelf_update_sym*() takes a data block relative
567 * index value, *NOT* the symbol index. As such, iterate the data blocks and
568 * adjust index until it fits.
570 * If no data block is found, allow adding a new data block provided the index
571 * is only one past the end.
573 static int elf_update_symbol(struct elf *elf, struct section *symtab,
574 struct section *symtab_shndx, struct symbol *sym)
576 Elf32_Word shndx = sym->sec ? sym->sec->idx : SHN_UNDEF;
577 Elf_Data *symtab_data = NULL, *shndx_data = NULL;
578 Elf64_Xword entsize = symtab->sh.sh_entsize;
579 int max_idx, idx = sym->idx;
580 Elf_Scn *s, *t = NULL;
581 bool is_special_shndx = sym->sym.st_shndx >= SHN_LORESERVE &&
582 sym->sym.st_shndx != SHN_XINDEX;
584 if (is_special_shndx)
585 shndx = sym->sym.st_shndx;
587 s = elf_getscn(elf->elf, symtab->idx);
589 WARN_ELF("elf_getscn");
594 t = elf_getscn(elf->elf, symtab_shndx->idx);
596 WARN_ELF("elf_getscn");
602 /* get next data descriptor for the relevant sections */
603 symtab_data = elf_getdata(s, symtab_data);
605 shndx_data = elf_getdata(t, shndx_data);
612 /* we don't do holes in symbol tables */
613 WARN("index out of range");
617 /* if @idx == 0, it's the next contiguous entry, create it */
618 symtab_data = elf_newdata(s);
620 shndx_data = elf_newdata(t);
622 buf = calloc(1, entsize);
628 symtab_data->d_buf = buf;
629 symtab_data->d_size = entsize;
630 symtab_data->d_align = 1;
631 symtab_data->d_type = ELF_T_SYM;
633 symtab->sh.sh_size += entsize;
634 symtab->changed = true;
637 shndx_data->d_buf = &sym->sec->idx;
638 shndx_data->d_size = sizeof(Elf32_Word);
639 shndx_data->d_align = sizeof(Elf32_Word);
640 shndx_data->d_type = ELF_T_WORD;
642 symtab_shndx->sh.sh_size += sizeof(Elf32_Word);
643 symtab_shndx->changed = true;
649 /* empty blocks should not happen */
650 if (!symtab_data->d_size) {
651 WARN("zero size data");
655 /* is this the right block? */
656 max_idx = symtab_data->d_size / entsize;
660 /* adjust index and try again */
664 /* something went side-ways */
666 WARN("negative index");
670 /* setup extended section index magic and write the symbol */
671 if ((shndx >= SHN_UNDEF && shndx < SHN_LORESERVE) || is_special_shndx) {
672 sym->sym.st_shndx = shndx;
676 sym->sym.st_shndx = SHN_XINDEX;
678 WARN("no .symtab_shndx");
683 if (!gelf_update_symshndx(symtab_data, shndx_data, idx, &sym->sym, shndx)) {
684 WARN_ELF("gelf_update_symshndx");
691 static struct symbol *
692 elf_create_section_symbol(struct elf *elf, struct section *sec)
694 struct section *symtab, *symtab_shndx;
695 Elf32_Word first_non_local, new_idx;
696 struct symbol *sym, *old;
698 symtab = find_section_by_name(elf, ".symtab");
700 symtab_shndx = find_section_by_name(elf, ".symtab_shndx");
706 sym = calloc(1, sizeof(*sym));
712 sym->name = sec->name;
716 sym->sym.st_info = GELF_ST_INFO(STB_LOCAL, STT_SECTION);
722 * Move the first global symbol, as per sh_info, into a new, higher
723 * symbol index. This fees up a spot for a new local symbol.
725 first_non_local = symtab->sh.sh_info;
726 new_idx = symtab->sh.sh_size / symtab->sh.sh_entsize;
727 old = find_symbol_by_index(elf, first_non_local);
731 hlist_del(&old->hash);
732 elf_hash_add(elf->symbol_hash, &old->hash, old->idx);
734 elf_dirty_reloc_sym(elf, old);
736 if (elf_update_symbol(elf, symtab, symtab_shndx, old)) {
737 WARN("elf_update_symbol move");
741 new_idx = first_non_local;
745 if (elf_update_symbol(elf, symtab, symtab_shndx, sym)) {
746 WARN("elf_update_symbol");
751 * Either way, we added a LOCAL symbol.
753 symtab->sh.sh_info += 1;
755 elf_add_symbol(elf, sym);
760 int elf_add_reloc_to_insn(struct elf *elf, struct section *sec,
761 unsigned long offset, unsigned int type,
762 struct section *insn_sec, unsigned long insn_off)
764 struct symbol *sym = insn_sec->sym;
765 int addend = insn_off;
769 * Due to how weak functions work, we must use section based
770 * relocations. Symbol based relocations would result in the
771 * weak and non-weak function annotations being overlaid on the
772 * non-weak function after linking.
774 sym = elf_create_section_symbol(elf, insn_sec);
781 return elf_add_reloc(elf, sec, offset, type, sym, addend);
784 static int read_rel_reloc(struct section *sec, int i, struct reloc *reloc, unsigned int *symndx)
786 if (!gelf_getrel(sec->data, i, &reloc->rel)) {
787 WARN_ELF("gelf_getrel");
790 reloc->type = GELF_R_TYPE(reloc->rel.r_info);
792 reloc->offset = reloc->rel.r_offset;
793 *symndx = GELF_R_SYM(reloc->rel.r_info);
797 static int read_rela_reloc(struct section *sec, int i, struct reloc *reloc, unsigned int *symndx)
799 if (!gelf_getrela(sec->data, i, &reloc->rela)) {
800 WARN_ELF("gelf_getrela");
803 reloc->type = GELF_R_TYPE(reloc->rela.r_info);
804 reloc->addend = reloc->rela.r_addend;
805 reloc->offset = reloc->rela.r_offset;
806 *symndx = GELF_R_SYM(reloc->rela.r_info);
810 static int read_relocs(struct elf *elf)
816 unsigned long nr_reloc, max_reloc = 0, tot_reloc = 0;
818 list_for_each_entry(sec, &elf->sections, list) {
819 if ((sec->sh.sh_type != SHT_RELA) &&
820 (sec->sh.sh_type != SHT_REL))
823 sec->base = find_section_by_index(elf, sec->sh.sh_info);
825 WARN("can't find base section for reloc section %s",
830 sec->base->reloc = sec;
833 for (i = 0; i < sec->sh.sh_size / sec->sh.sh_entsize; i++) {
834 reloc = malloc(sizeof(*reloc));
839 memset(reloc, 0, sizeof(*reloc));
840 switch (sec->sh.sh_type) {
842 if (read_rel_reloc(sec, i, reloc, &symndx))
846 if (read_rela_reloc(sec, i, reloc, &symndx))
854 reloc->sym = find_symbol_by_index(elf, symndx);
856 WARN("can't find reloc entry symbol %d for %s",
861 list_add_tail(&reloc->list, &sec->reloc_list);
862 elf_hash_add(elf->reloc_hash, &reloc->hash, reloc_hash(reloc));
866 max_reloc = max(max_reloc, nr_reloc);
867 tot_reloc += nr_reloc;
871 printf("max_reloc: %lu\n", max_reloc);
872 printf("tot_reloc: %lu\n", tot_reloc);
878 struct elf *elf_open_read(const char *name, int flags)
883 elf_version(EV_CURRENT);
885 elf = malloc(sizeof(*elf));
890 memset(elf, 0, offsetof(struct elf, sections));
892 INIT_LIST_HEAD(&elf->sections);
894 elf_hash_init(elf->symbol_hash);
895 elf_hash_init(elf->symbol_name_hash);
896 elf_hash_init(elf->section_hash);
897 elf_hash_init(elf->section_name_hash);
898 elf_hash_init(elf->reloc_hash);
900 elf->fd = open(name, flags);
902 fprintf(stderr, "objtool: Can't open '%s': %s\n",
903 name, strerror(errno));
907 if ((flags & O_ACCMODE) == O_RDONLY)
908 cmd = ELF_C_READ_MMAP;
909 else if ((flags & O_ACCMODE) == O_RDWR)
914 elf->elf = elf_begin(elf->fd, cmd, NULL);
916 WARN_ELF("elf_begin");
920 if (!gelf_getehdr(elf->elf, &elf->ehdr)) {
921 WARN_ELF("gelf_getehdr");
925 if (read_sections(elf))
928 if (read_symbols(elf))
931 if (read_relocs(elf))
941 static int elf_add_string(struct elf *elf, struct section *strtab, char *str)
948 strtab = find_section_by_name(elf, ".strtab");
950 WARN("can't find .strtab section");
954 s = elf_getscn(elf->elf, strtab->idx);
956 WARN_ELF("elf_getscn");
960 data = elf_newdata(s);
962 WARN_ELF("elf_newdata");
967 data->d_size = strlen(str) + 1;
969 data->d_type = ELF_T_SYM;
972 strtab->len += data->d_size;
973 strtab->changed = true;
978 struct section *elf_create_section(struct elf *elf, const char *name,
979 unsigned int sh_flags, size_t entsize, int nr)
981 struct section *sec, *shstrtab;
982 size_t size = entsize * nr;
985 sec = malloc(sizeof(*sec));
990 memset(sec, 0, sizeof(*sec));
992 INIT_LIST_HEAD(&sec->symbol_list);
993 INIT_LIST_HEAD(&sec->reloc_list);
995 s = elf_newscn(elf->elf);
997 WARN_ELF("elf_newscn");
1001 sec->name = strdup(name);
1007 sec->idx = elf_ndxscn(s);
1009 sec->changed = true;
1011 sec->data = elf_newdata(s);
1013 WARN_ELF("elf_newdata");
1017 sec->data->d_size = size;
1018 sec->data->d_align = 1;
1021 sec->data->d_buf = malloc(size);
1022 if (!sec->data->d_buf) {
1026 memset(sec->data->d_buf, 0, size);
1029 if (!gelf_getshdr(s, &sec->sh)) {
1030 WARN_ELF("gelf_getshdr");
1034 sec->sh.sh_size = size;
1035 sec->sh.sh_entsize = entsize;
1036 sec->sh.sh_type = SHT_PROGBITS;
1037 sec->sh.sh_addralign = 1;
1038 sec->sh.sh_flags = SHF_ALLOC | sh_flags;
1040 /* Add section name to .shstrtab (or .strtab for Clang) */
1041 shstrtab = find_section_by_name(elf, ".shstrtab");
1043 shstrtab = find_section_by_name(elf, ".strtab");
1045 WARN("can't find .shstrtab or .strtab section");
1048 sec->sh.sh_name = elf_add_string(elf, shstrtab, sec->name);
1049 if (sec->sh.sh_name == -1)
1052 list_add_tail(&sec->list, &elf->sections);
1053 elf_hash_add(elf->section_hash, &sec->hash, sec->idx);
1054 elf_hash_add(elf->section_name_hash, &sec->name_hash, str_hash(sec->name));
1056 elf->changed = true;
1061 static struct section *elf_create_rel_reloc_section(struct elf *elf, struct section *base)
1064 struct section *sec;
1066 relocname = malloc(strlen(base->name) + strlen(".rel") + 1);
1071 strcpy(relocname, ".rel");
1072 strcat(relocname, base->name);
1074 sec = elf_create_section(elf, relocname, 0, sizeof(GElf_Rel), 0);
1082 sec->sh.sh_type = SHT_REL;
1083 sec->sh.sh_addralign = 8;
1084 sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
1085 sec->sh.sh_info = base->idx;
1086 sec->sh.sh_flags = SHF_INFO_LINK;
1091 static struct section *elf_create_rela_reloc_section(struct elf *elf, struct section *base)
1094 struct section *sec;
1096 relocname = malloc(strlen(base->name) + strlen(".rela") + 1);
1101 strcpy(relocname, ".rela");
1102 strcat(relocname, base->name);
1104 sec = elf_create_section(elf, relocname, 0, sizeof(GElf_Rela), 0);
1112 sec->sh.sh_type = SHT_RELA;
1113 sec->sh.sh_addralign = 8;
1114 sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
1115 sec->sh.sh_info = base->idx;
1116 sec->sh.sh_flags = SHF_INFO_LINK;
1121 static struct section *elf_create_reloc_section(struct elf *elf,
1122 struct section *base,
1126 case SHT_REL: return elf_create_rel_reloc_section(elf, base);
1127 case SHT_RELA: return elf_create_rela_reloc_section(elf, base);
1128 default: return NULL;
1132 static int elf_rebuild_rel_reloc_section(struct section *sec, int nr)
1134 struct reloc *reloc;
1138 /* Allocate a buffer for relocations */
1139 size = nr * sizeof(*relocs);
1140 relocs = malloc(size);
1146 sec->data->d_buf = relocs;
1147 sec->data->d_size = size;
1149 sec->sh.sh_size = size;
1152 list_for_each_entry(reloc, &sec->reloc_list, list) {
1153 relocs[idx].r_offset = reloc->offset;
1154 relocs[idx].r_info = GELF_R_INFO(reloc->sym->idx, reloc->type);
1161 static int elf_rebuild_rela_reloc_section(struct section *sec, int nr)
1163 struct reloc *reloc;
1167 /* Allocate a buffer for relocations with addends */
1168 size = nr * sizeof(*relocs);
1169 relocs = malloc(size);
1175 sec->data->d_buf = relocs;
1176 sec->data->d_size = size;
1178 sec->sh.sh_size = size;
1181 list_for_each_entry(reloc, &sec->reloc_list, list) {
1182 relocs[idx].r_offset = reloc->offset;
1183 relocs[idx].r_addend = reloc->addend;
1184 relocs[idx].r_info = GELF_R_INFO(reloc->sym->idx, reloc->type);
1191 static int elf_rebuild_reloc_section(struct elf *elf, struct section *sec)
1193 struct reloc *reloc;
1197 list_for_each_entry(reloc, &sec->reloc_list, list)
1200 switch (sec->sh.sh_type) {
1201 case SHT_REL: return elf_rebuild_rel_reloc_section(sec, nr);
1202 case SHT_RELA: return elf_rebuild_rela_reloc_section(sec, nr);
1207 int elf_write_insn(struct elf *elf, struct section *sec,
1208 unsigned long offset, unsigned int len,
1211 Elf_Data *data = sec->data;
1213 if (data->d_type != ELF_T_BYTE || data->d_off) {
1214 WARN("write to unexpected data for section: %s", sec->name);
1218 memcpy(data->d_buf + offset, insn, len);
1219 elf_flagdata(data, ELF_C_SET, ELF_F_DIRTY);
1221 elf->changed = true;
1226 int elf_write_reloc(struct elf *elf, struct reloc *reloc)
1228 struct section *sec = reloc->sec;
1230 if (sec->sh.sh_type == SHT_REL) {
1231 reloc->rel.r_info = GELF_R_INFO(reloc->sym->idx, reloc->type);
1232 reloc->rel.r_offset = reloc->offset;
1234 if (!gelf_update_rel(sec->data, reloc->idx, &reloc->rel)) {
1235 WARN_ELF("gelf_update_rel");
1239 reloc->rela.r_info = GELF_R_INFO(reloc->sym->idx, reloc->type);
1240 reloc->rela.r_addend = reloc->addend;
1241 reloc->rela.r_offset = reloc->offset;
1243 if (!gelf_update_rela(sec->data, reloc->idx, &reloc->rela)) {
1244 WARN_ELF("gelf_update_rela");
1249 elf->changed = true;
1254 int elf_write(struct elf *elf)
1256 struct section *sec;
1259 /* Update changed relocation sections and section headers: */
1260 list_for_each_entry(sec, &elf->sections, list) {
1263 elf_rebuild_reloc_section(elf, sec)) {
1264 WARN("elf_rebuild_reloc_section");
1268 s = elf_getscn(elf->elf, sec->idx);
1270 WARN_ELF("elf_getscn");
1273 if (!gelf_update_shdr(s, &sec->sh)) {
1274 WARN_ELF("gelf_update_shdr");
1278 sec->changed = false;
1279 elf->changed = true;
1283 /* Make sure the new section header entries get updated properly. */
1284 elf_flagelf(elf->elf, ELF_C_SET, ELF_F_DIRTY);
1286 /* Write all changes to the file. */
1287 if (elf_update(elf->elf, ELF_C_WRITE) < 0) {
1288 WARN_ELF("elf_update");
1292 elf->changed = false;
1297 void elf_close(struct elf *elf)
1299 struct section *sec, *tmpsec;
1300 struct symbol *sym, *tmpsym;
1301 struct reloc *reloc, *tmpreloc;
1309 list_for_each_entry_safe(sec, tmpsec, &elf->sections, list) {
1310 list_for_each_entry_safe(sym, tmpsym, &sec->symbol_list, list) {
1311 list_del(&sym->list);
1312 hash_del(&sym->hash);
1315 list_for_each_entry_safe(reloc, tmpreloc, &sec->reloc_list, list) {
1316 list_del(&reloc->list);
1317 hash_del(&reloc->hash);
1320 list_del(&sec->list);