Lines Matching refs:elf

32 #define __elf_table(name)	(elf->name##_hash)
33 #define __elf_bits(name) (elf->name##_bits)
132 struct section *find_section_by_name(const struct elf *elf, const char *name) in find_section_by_name() argument
144 static struct section *find_section_by_index(struct elf *elf, in find_section_by_index() argument
157 static struct symbol *find_symbol_by_index(struct elf *elf, unsigned int idx) in find_symbol_by_index() argument
256 struct symbol *find_symbol_by_name(const struct elf *elf, const char *name) in find_symbol_by_name() argument
268 struct reloc *find_reloc_by_dest_range(const struct elf *elf, struct section *sec, in find_reloc_by_dest_range() argument
298 struct reloc *find_reloc_by_dest(const struct elf *elf, struct section *sec, unsigned long offset) in find_reloc_by_dest() argument
300 return find_reloc_by_dest_range(elf, sec, offset, 1); in find_reloc_by_dest()
308 static int read_sections(struct elf *elf) in read_sections() argument
315 if (elf_getshdrnum(elf->elf, &sections_nr)) { in read_sections()
320 if (elf_getshdrstrndx(elf->elf, &shstrndx)) { in read_sections()
329 elf->section_data = calloc(sections_nr, sizeof(*sec)); in read_sections()
330 if (!elf->section_data) { in read_sections()
335 sec = &elf->section_data[i]; in read_sections()
339 s = elf_getscn(elf->elf, i); in read_sections()
352 sec->name = elf_strptr(elf->elf, shstrndx, sec->sh.sh_name); in read_sections()
372 list_add_tail(&sec->list, &elf->sections); in read_sections()
377 elf->num_relocs += sec_num_entries(sec); in read_sections()
382 printf("section_bits: %d\n", elf->section_bits); in read_sections()
386 if (elf_nextscn(elf->elf, s)) { in read_sections()
394 static void elf_add_symbol(struct elf *elf, struct symbol *sym) in elf_add_symbol() argument
407 elf->num_files++; in elf_add_symbol()
435 static int read_symbols(struct elf *elf) in read_symbols() argument
444 symtab = find_section_by_name(elf, ".symtab"); in read_symbols()
446 symtab_shndx = find_section_by_name(elf, ".symtab_shndx"); in read_symbols()
465 elf->symbol_data = calloc(symbols_nr, sizeof(*sym)); in read_symbols()
466 if (!elf->symbol_data) { in read_symbols()
471 sym = &elf->symbol_data[i]; in read_symbols()
481 sym->name = elf_strptr(elf->elf, symtab->sh.sh_link, in read_symbols()
494 sym->sec = find_section_by_index(elf, shndx); in read_symbols()
505 sym->sec = find_section_by_index(elf, 0); in read_symbols()
507 elf_add_symbol(elf, sym); in read_symbols()
512 printf("symbol_bits: %d\n", elf->symbol_bits); in read_symbols()
516 list_for_each_entry(sec, &elf->sections, list) { in read_symbols()
542 pfunc = find_symbol_by_name(elf, pname); in read_symbols()
579 static int elf_update_sym_relocs(struct elf *elf, struct symbol *sym) in elf_update_sym_relocs() argument
584 set_reloc_sym(elf, reloc, reloc->sym->idx); in elf_update_sym_relocs()
597 static int elf_update_symbol(struct elf *elf, struct section *symtab, in elf_update_symbol() argument
611 s = elf_getscn(elf->elf, symtab->idx); in elf_update_symbol()
618 t = elf_getscn(elf->elf, symtab_shndx->idx); in elf_update_symbol()
663 mark_sec_changed(elf, symtab, true); in elf_update_symbol()
678 mark_sec_changed(elf, symtab_shndx, true); in elf_update_symbol()
728 __elf_create_symbol(struct elf *elf, struct symbol *sym) in __elf_create_symbol() argument
734 symtab = find_section_by_name(elf, ".symtab"); in __elf_create_symbol()
736 symtab_shndx = find_section_by_name(elf, ".symtab_shndx"); in __elf_create_symbol()
752 old = find_symbol_by_index(elf, first_non_local); in __elf_create_symbol()
759 if (elf_update_symbol(elf, symtab, symtab_shndx, old)) { in __elf_create_symbol()
764 if (elf_update_sym_relocs(elf, old)) in __elf_create_symbol()
777 if (elf_update_symbol(elf, symtab, symtab_shndx, sym)) { in __elf_create_symbol()
783 mark_sec_changed(elf, symtab, true); in __elf_create_symbol()
787 mark_sec_changed(elf, symtab_shndx, true); in __elf_create_symbol()
794 elf_create_section_symbol(struct elf *elf, struct section *sec) in elf_create_section_symbol() argument
812 sym = __elf_create_symbol(elf, sym); in elf_create_section_symbol()
814 elf_add_symbol(elf, sym); in elf_create_section_symbol()
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) in elf_create_prefix_symbol() argument
838 sym->sym.st_name = elf_add_string(elf, NULL, name); in elf_create_prefix_symbol()
843 sym = __elf_create_symbol(elf, sym); in elf_create_prefix_symbol()
845 elf_add_symbol(elf, sym); in elf_create_prefix_symbol()
850 static struct reloc *elf_init_reloc(struct elf *elf, struct section *rsec, in elf_init_reloc() argument
874 set_reloc_offset(elf, reloc, offset); in elf_init_reloc()
875 set_reloc_sym(elf, reloc, sym->idx); in elf_init_reloc()
876 set_reloc_type(elf, reloc, type); in elf_init_reloc()
877 set_reloc_addend(elf, reloc, addend); in elf_init_reloc()
886 struct reloc *elf_init_reloc_text_sym(struct elf *elf, struct section *sec, in elf_init_reloc_text_sym() argument
908 sym = elf_create_section_symbol(elf, insn_sec); in elf_init_reloc_text_sym()
915 return elf_init_reloc(elf, sec->rsec, reloc_idx, offset, sym, addend, in elf_init_reloc_text_sym()
916 elf_text_rela_type(elf)); in elf_init_reloc_text_sym()
919 struct reloc *elf_init_reloc_data_sym(struct elf *elf, struct section *sec, in elf_init_reloc_data_sym() argument
931 return elf_init_reloc(elf, sec->rsec, reloc_idx, offset, sym, addend, in elf_init_reloc_data_sym()
932 elf_data_rela_type(elf)); in elf_init_reloc_data_sym()
935 static int read_relocs(struct elf *elf) in read_relocs() argument
944 if (!elf_alloc_hash(reloc, elf->num_relocs)) in read_relocs()
947 list_for_each_entry(rsec, &elf->sections, list) { in read_relocs()
951 rsec->base = find_section_by_index(elf, rsec->sh.sh_info); in read_relocs()
971 reloc->sym = sym = find_symbol_by_index(elf, symndx); in read_relocs()
989 printf("num_relocs: %lu\n", elf->num_relocs); in read_relocs()
990 printf("reloc_bits: %d\n", elf->reloc_bits); in read_relocs()
996 struct elf *elf_open_read(const char *name, int flags) in elf_open_read()
998 struct elf *elf; in elf_open_read() local
1003 elf = malloc(sizeof(*elf)); in elf_open_read()
1004 if (!elf) { in elf_open_read()
1008 memset(elf, 0, sizeof(*elf)); in elf_open_read()
1010 INIT_LIST_HEAD(&elf->sections); in elf_open_read()
1012 elf->fd = open(name, flags); in elf_open_read()
1013 if (elf->fd == -1) { in elf_open_read()
1026 elf->elf = elf_begin(elf->fd, cmd, NULL); in elf_open_read()
1027 if (!elf->elf) { in elf_open_read()
1032 if (!gelf_getehdr(elf->elf, &elf->ehdr)) { in elf_open_read()
1037 if (read_sections(elf)) in elf_open_read()
1040 if (read_symbols(elf)) in elf_open_read()
1043 if (read_relocs(elf)) in elf_open_read()
1046 return elf; in elf_open_read()
1049 elf_close(elf); in elf_open_read()
1053 static int elf_add_string(struct elf *elf, struct section *strtab, char *str) in elf_add_string() argument
1060 strtab = find_section_by_name(elf, ".strtab"); in elf_add_string()
1066 s = elf_getscn(elf->elf, strtab->idx); in elf_add_string()
1085 mark_sec_changed(elf, strtab, true); in elf_add_string()
1090 struct section *elf_create_section(struct elf *elf, const char *name, in elf_create_section() argument
1106 s = elf_newscn(elf->elf); in elf_create_section()
1150 shstrtab = find_section_by_name(elf, ".shstrtab"); in elf_create_section()
1152 shstrtab = find_section_by_name(elf, ".strtab"); in elf_create_section()
1157 sec->sh.sh_name = elf_add_string(elf, shstrtab, sec->name); in elf_create_section()
1161 list_add_tail(&sec->list, &elf->sections); in elf_create_section()
1165 mark_sec_changed(elf, sec, true); in elf_create_section()
1170 static struct section *elf_create_rela_section(struct elf *elf, in elf_create_rela_section() argument
1185 rsec = elf_create_section(elf, rsec_name, elf_rela_size(elf), reloc_nr); in elf_create_rela_section()
1192 rsec->sh.sh_addralign = elf_addr_size(elf); in elf_create_rela_section()
1193 rsec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx; in elf_create_rela_section()
1209 struct section *elf_create_section_pair(struct elf *elf, const char *name, in elf_create_section_pair() argument
1215 sec = elf_create_section(elf, name, entsize, nr); in elf_create_section_pair()
1219 if (!elf_create_rela_section(elf, sec, reloc_nr)) in elf_create_section_pair()
1225 int elf_write_insn(struct elf *elf, struct section *sec, in elf_write_insn() argument
1238 mark_sec_changed(elf, sec, true); in elf_write_insn()
1252 static int elf_truncate_section(struct elf *elf, struct section *sec) in elf_truncate_section() argument
1259 s = elf_getscn(elf->elf, sec->idx); in elf_truncate_section()
1297 int elf_write(struct elf *elf) in elf_write() argument
1306 list_for_each_entry(sec, &elf->sections, list) { in elf_write()
1308 elf_truncate_section(elf, sec); in elf_write()
1311 s = elf_getscn(elf->elf, sec->idx); in elf_write()
1323 mark_sec_changed(elf, sec, false); in elf_write()
1328 elf_flagelf(elf->elf, ELF_C_SET, ELF_F_DIRTY); in elf_write()
1331 if (elf_update(elf->elf, ELF_C_WRITE) < 0) { in elf_write()
1336 elf->changed = false; in elf_write()
1341 void elf_close(struct elf *elf) in elf_close() argument
1343 if (elf->elf) in elf_close()
1344 elf_end(elf->elf); in elf_close()
1346 if (elf->fd > 0) in elf_close()
1347 close(elf->fd); in elf_close()