Lines Matching full:elf
3 * elf.c - ELF access library
22 #include <objtool/elf.h>
30 #define __elf_table(name) (elf->name##_hash)
31 #define __elf_bits(name) (elf->name##_bits)
130 struct section *find_section_by_name(const struct elf *elf, const char *name) in find_section_by_name() argument
142 static struct section *find_section_by_index(struct elf *elf, in find_section_by_index() argument
155 static struct symbol *find_symbol_by_index(struct elf *elf, unsigned int idx) in find_symbol_by_index() argument
254 struct symbol *find_symbol_by_name(const struct elf *elf, const char *name) in find_symbol_by_name() argument
266 struct reloc *find_reloc_by_dest_range(const struct elf *elf, struct section *sec, in find_reloc_by_dest_range() argument
296 struct reloc *find_reloc_by_dest(const struct elf *elf, struct section *sec, unsigned long offset) in find_reloc_by_dest() argument
298 return find_reloc_by_dest_range(elf, sec, offset, 1); in find_reloc_by_dest()
306 static int read_sections(struct elf *elf) in read_sections() argument
313 if (elf_getshdrnum(elf->elf, §ions_nr)) { in read_sections()
318 if (elf_getshdrstrndx(elf->elf, &shstrndx)) { in read_sections()
327 elf->section_data = calloc(sections_nr, sizeof(*sec)); in read_sections()
328 if (!elf->section_data) { in read_sections()
333 sec = &elf->section_data[i]; in read_sections()
337 s = elf_getscn(elf->elf, i); in read_sections()
350 sec->name = elf_strptr(elf->elf, shstrndx, sec->sh.sh_name); in read_sections()
370 list_add_tail(&sec->list, &elf->sections); in read_sections()
375 elf->num_relocs += sec_num_entries(sec); in read_sections()
380 printf("section_bits: %d\n", elf->section_bits); in read_sections()
384 if (elf_nextscn(elf->elf, s)) { in read_sections()
392 static void elf_add_symbol(struct elf *elf, struct symbol *sym) in elf_add_symbol() argument
405 elf->num_files++; in elf_add_symbol()
433 static int read_symbols(struct elf *elf) in read_symbols() argument
442 symtab = find_section_by_name(elf, ".symtab"); in read_symbols()
444 symtab_shndx = find_section_by_name(elf, ".symtab_shndx"); in read_symbols()
463 elf->symbol_data = calloc(symbols_nr, sizeof(*sym)); in read_symbols()
464 if (!elf->symbol_data) { in read_symbols()
469 sym = &elf->symbol_data[i]; in read_symbols()
479 sym->name = elf_strptr(elf->elf, symtab->sh.sh_link, in read_symbols()
492 sym->sec = find_section_by_index(elf, shndx); in read_symbols()
503 sym->sec = find_section_by_index(elf, 0); in read_symbols()
505 elf_add_symbol(elf, sym); in read_symbols()
510 printf("symbol_bits: %d\n", elf->symbol_bits); in read_symbols()
514 list_for_each_entry(sec, &elf->sections, list) { in read_symbols()
539 pfunc = find_symbol_by_name(elf, pname); in read_symbols()
577 static int elf_update_sym_relocs(struct elf *elf, struct symbol *sym) in elf_update_sym_relocs() argument
582 set_reloc_sym(elf, reloc, reloc->sym->idx); in elf_update_sym_relocs()
595 static int elf_update_symbol(struct elf *elf, struct section *symtab, in elf_update_symbol() argument
609 s = elf_getscn(elf->elf, symtab->idx); in elf_update_symbol()
616 t = elf_getscn(elf->elf, symtab_shndx->idx); in elf_update_symbol()
661 mark_sec_changed(elf, symtab, true); in elf_update_symbol()
676 mark_sec_changed(elf, symtab_shndx, true); in elf_update_symbol()
726 __elf_create_symbol(struct elf *elf, struct symbol *sym) in __elf_create_symbol() argument
732 symtab = find_section_by_name(elf, ".symtab"); in __elf_create_symbol()
734 symtab_shndx = find_section_by_name(elf, ".symtab_shndx"); in __elf_create_symbol()
750 old = find_symbol_by_index(elf, first_non_local); in __elf_create_symbol()
757 if (elf_update_symbol(elf, symtab, symtab_shndx, old)) { in __elf_create_symbol()
762 if (elf_update_sym_relocs(elf, old)) in __elf_create_symbol()
775 if (elf_update_symbol(elf, symtab, symtab_shndx, sym)) { in __elf_create_symbol()
781 mark_sec_changed(elf, symtab, true); in __elf_create_symbol()
785 mark_sec_changed(elf, symtab_shndx, true); in __elf_create_symbol()
792 elf_create_section_symbol(struct elf *elf, struct section *sec) in elf_create_section_symbol() argument
810 sym = __elf_create_symbol(elf, sym); in elf_create_section_symbol()
812 elf_add_symbol(elf, sym); in elf_create_section_symbol()
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) in elf_create_prefix_symbol() argument
836 sym->sym.st_name = elf_add_string(elf, NULL, name); in elf_create_prefix_symbol()
841 sym = __elf_create_symbol(elf, sym); in elf_create_prefix_symbol()
843 elf_add_symbol(elf, sym); in elf_create_prefix_symbol()
848 static struct reloc *elf_init_reloc(struct elf *elf, struct section *rsec, in elf_init_reloc() argument
872 set_reloc_offset(elf, reloc, offset); in elf_init_reloc()
873 set_reloc_sym(elf, reloc, sym->idx); in elf_init_reloc()
874 set_reloc_type(elf, reloc, type); in elf_init_reloc()
875 set_reloc_addend(elf, reloc, addend); in elf_init_reloc()
884 struct reloc *elf_init_reloc_text_sym(struct elf *elf, struct section *sec, in elf_init_reloc_text_sym() argument
906 sym = elf_create_section_symbol(elf, insn_sec); in elf_init_reloc_text_sym()
913 return elf_init_reloc(elf, sec->rsec, reloc_idx, offset, sym, addend, in elf_init_reloc_text_sym()
914 elf_text_rela_type(elf)); in elf_init_reloc_text_sym()
917 struct reloc *elf_init_reloc_data_sym(struct elf *elf, struct section *sec, in elf_init_reloc_data_sym() argument
929 return elf_init_reloc(elf, sec->rsec, reloc_idx, offset, sym, addend, in elf_init_reloc_data_sym()
930 elf_data_rela_type(elf)); in elf_init_reloc_data_sym()
933 static int read_relocs(struct elf *elf) in read_relocs() argument
942 if (!elf_alloc_hash(reloc, elf->num_relocs)) in read_relocs()
945 list_for_each_entry(rsec, &elf->sections, list) { in read_relocs()
949 rsec->base = find_section_by_index(elf, rsec->sh.sh_info); in read_relocs()
969 reloc->sym = sym = find_symbol_by_index(elf, symndx); in read_relocs()
987 printf("num_relocs: %lu\n", elf->num_relocs); in read_relocs()
988 printf("reloc_bits: %d\n", elf->reloc_bits); in read_relocs()
994 struct elf *elf_open_read(const char *name, int flags) in elf_open_read()
996 struct elf *elf; in elf_open_read() local
1001 elf = malloc(sizeof(*elf)); in elf_open_read()
1002 if (!elf) { in elf_open_read()
1006 memset(elf, 0, sizeof(*elf)); in elf_open_read()
1008 INIT_LIST_HEAD(&elf->sections); in elf_open_read()
1010 elf->fd = open(name, flags); in elf_open_read()
1011 if (elf->fd == -1) { in elf_open_read()
1024 elf->elf = elf_begin(elf->fd, cmd, NULL); in elf_open_read()
1025 if (!elf->elf) { in elf_open_read()
1030 if (!gelf_getehdr(elf->elf, &elf->ehdr)) { in elf_open_read()
1035 if (read_sections(elf)) in elf_open_read()
1038 if (read_symbols(elf)) in elf_open_read()
1041 if (read_relocs(elf)) in elf_open_read()
1044 return elf; in elf_open_read()
1047 elf_close(elf); in elf_open_read()
1051 static int elf_add_string(struct elf *elf, struct section *strtab, char *str) in elf_add_string() argument
1058 strtab = find_section_by_name(elf, ".strtab"); in elf_add_string()
1064 s = elf_getscn(elf->elf, strtab->idx); in elf_add_string()
1083 mark_sec_changed(elf, strtab, true); in elf_add_string()
1088 struct section *elf_create_section(struct elf *elf, const char *name, in elf_create_section() argument
1104 s = elf_newscn(elf->elf); in elf_create_section()
1148 shstrtab = find_section_by_name(elf, ".shstrtab"); in elf_create_section()
1150 shstrtab = find_section_by_name(elf, ".strtab"); in elf_create_section()
1155 sec->sh.sh_name = elf_add_string(elf, shstrtab, sec->name); in elf_create_section()
1159 list_add_tail(&sec->list, &elf->sections); in elf_create_section()
1163 mark_sec_changed(elf, sec, true); in elf_create_section()
1168 static struct section *elf_create_rela_section(struct elf *elf, in elf_create_rela_section() argument
1183 rsec = elf_create_section(elf, rsec_name, elf_rela_size(elf), reloc_nr); in elf_create_rela_section()
1190 rsec->sh.sh_addralign = elf_addr_size(elf); in elf_create_rela_section()
1191 rsec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx; in elf_create_rela_section()
1207 struct section *elf_create_section_pair(struct elf *elf, const char *name, in elf_create_section_pair() argument
1213 sec = elf_create_section(elf, name, entsize, nr); in elf_create_section_pair()
1217 if (!elf_create_rela_section(elf, sec, reloc_nr)) in elf_create_section_pair()
1223 int elf_write_insn(struct elf *elf, struct section *sec, in elf_write_insn() argument
1236 mark_sec_changed(elf, sec, true); in elf_write_insn()
1250 static int elf_truncate_section(struct elf *elf, struct section *sec) in elf_truncate_section() argument
1257 s = elf_getscn(elf->elf, sec->idx); in elf_truncate_section()
1295 int elf_write(struct elf *elf) in elf_write() argument
1304 list_for_each_entry(sec, &elf->sections, list) { in elf_write()
1306 elf_truncate_section(elf, sec); in elf_write()
1309 s = elf_getscn(elf->elf, sec->idx); in elf_write()
1321 mark_sec_changed(elf, sec, false); in elf_write()
1326 elf_flagelf(elf->elf, ELF_C_SET, ELF_F_DIRTY); in elf_write()
1329 if (elf_update(elf->elf, ELF_C_WRITE) < 0) { in elf_write()
1334 elf->changed = false; in elf_write()
1339 void elf_close(struct elf *elf) in elf_close() argument
1341 if (elf->elf) in elf_close()
1342 elf_end(elf->elf); in elf_close()
1344 if (elf->fd > 0) in elf_close()
1345 close(elf->fd); in elf_close()