Lines Matching refs:elf

32 #define __elf_table(name)	(elf->name##_hash)
33 #define __elf_bits(name) (elf->name##_bits)
96 struct section *find_section_by_name(const struct elf *elf, const char *name) in find_section_by_name() argument
108 static struct section *find_section_by_index(struct elf *elf, in find_section_by_index() argument
121 static struct symbol *find_symbol_by_index(struct elf *elf, unsigned int idx) in find_symbol_by_index() argument
220 struct symbol *find_symbol_by_name(const struct elf *elf, const char *name) in find_symbol_by_name() argument
232 struct reloc *find_reloc_by_dest_range(const struct elf *elf, struct section *sec, in find_reloc_by_dest_range() argument
261 struct reloc *find_reloc_by_dest(const struct elf *elf, struct section *sec, unsigned long offset) in find_reloc_by_dest() argument
263 return find_reloc_by_dest_range(elf, sec, offset, 1); in find_reloc_by_dest()
266 static int read_sections(struct elf *elf) in read_sections() argument
273 if (elf_getshdrnum(elf->elf, &sections_nr)) { in read_sections()
278 if (elf_getshdrstrndx(elf->elf, &shstrndx)) { in read_sections()
287 elf->section_data = calloc(sections_nr, sizeof(*sec)); in read_sections()
288 if (!elf->section_data) { in read_sections()
293 sec = &elf->section_data[i]; in read_sections()
298 s = elf_getscn(elf->elf, i); in read_sections()
311 sec->name = elf_strptr(elf->elf, shstrndx, sec->sh.sh_name); in read_sections()
332 elf->text_size += sec->sh.sh_size; in read_sections()
334 list_add_tail(&sec->list, &elf->sections); in read_sections()
341 printf("section_bits: %d\n", elf->section_bits); in read_sections()
345 if (elf_nextscn(elf->elf, s)) { in read_sections()
353 static void elf_add_symbol(struct elf *elf, struct symbol *sym) in elf_add_symbol() argument
367 elf->num_files++; in elf_add_symbol()
395 static int read_symbols(struct elf *elf) in read_symbols() argument
404 symtab = find_section_by_name(elf, ".symtab"); in read_symbols()
406 symtab_shndx = find_section_by_name(elf, ".symtab_shndx"); in read_symbols()
425 elf->symbol_data = calloc(symbols_nr, sizeof(*sym)); in read_symbols()
426 if (!elf->symbol_data) { in read_symbols()
431 sym = &elf->symbol_data[i]; in read_symbols()
441 sym->name = elf_strptr(elf->elf, symtab->sh.sh_link, in read_symbols()
454 sym->sec = find_section_by_index(elf, shndx); in read_symbols()
465 sym->sec = find_section_by_index(elf, 0); in read_symbols()
467 elf_add_symbol(elf, sym); in read_symbols()
472 printf("symbol_bits: %d\n", elf->symbol_bits); in read_symbols()
476 list_for_each_entry(sec, &elf->sections, list) { in read_symbols()
502 pfunc = find_symbol_by_name(elf, pname); in read_symbols()
536 static struct section *elf_create_reloc_section(struct elf *elf,
540 int elf_add_reloc(struct elf *elf, struct section *sec, unsigned long offset, in elf_add_reloc() argument
545 if (!sec->reloc && !elf_create_reloc_section(elf, sec, SHT_RELA)) in elf_add_reloc()
576 static void elf_dirty_reloc_sym(struct elf *elf, struct symbol *sym) in elf_dirty_reloc_sym() argument
592 static int elf_update_symbol(struct elf *elf, struct section *symtab, in elf_update_symbol() argument
606 s = elf_getscn(elf->elf, symtab->idx); in elf_update_symbol()
613 t = elf_getscn(elf->elf, symtab_shndx->idx); in elf_update_symbol()
723 __elf_create_symbol(struct elf *elf, struct symbol *sym) in __elf_create_symbol() argument
729 symtab = find_section_by_name(elf, ".symtab"); in __elf_create_symbol()
731 symtab_shndx = find_section_by_name(elf, ".symtab_shndx"); in __elf_create_symbol()
747 old = find_symbol_by_index(elf, first_non_local); in __elf_create_symbol()
754 elf_dirty_reloc_sym(elf, old); in __elf_create_symbol()
756 if (elf_update_symbol(elf, symtab, symtab_shndx, old)) { in __elf_create_symbol()
771 if (elf_update_symbol(elf, symtab, symtab_shndx, sym)) { in __elf_create_symbol()
788 elf_create_section_symbol(struct elf *elf, struct section *sec) in elf_create_section_symbol() argument
806 sym = __elf_create_symbol(elf, sym); in elf_create_section_symbol()
808 elf_add_symbol(elf, sym); in elf_create_section_symbol()
813 static int elf_add_string(struct elf *elf, struct section *strtab, char *str);
816 elf_create_prefix_symbol(struct elf *elf, struct symbol *orig, long size) in elf_create_prefix_symbol() argument
832 sym->sym.st_name = elf_add_string(elf, NULL, name); in elf_create_prefix_symbol()
837 sym = __elf_create_symbol(elf, sym); in elf_create_prefix_symbol()
839 elf_add_symbol(elf, sym); in elf_create_prefix_symbol()
844 int elf_add_reloc_to_insn(struct elf *elf, struct section *sec, in elf_add_reloc_to_insn() argument
858 sym = elf_create_section_symbol(elf, insn_sec); in elf_add_reloc_to_insn()
865 return elf_add_reloc(elf, sec, offset, type, sym, addend); in elf_add_reloc_to_insn()
894 static int read_relocs(struct elf *elf) in read_relocs() argument
903 if (!elf_alloc_hash(reloc, elf->text_size / 16)) in read_relocs()
906 list_for_each_entry(sec, &elf->sections, list) { in read_relocs()
911 sec->base = find_section_by_index(elf, sec->sh.sh_info); in read_relocs()
942 reloc->sym = sym = find_symbol_by_index(elf, symndx); in read_relocs()
962 printf("reloc_bits: %d\n", elf->reloc_bits); in read_relocs()
968 struct elf *elf_open_read(const char *name, int flags) in elf_open_read()
970 struct elf *elf; in elf_open_read() local
975 elf = malloc(sizeof(*elf)); in elf_open_read()
976 if (!elf) { in elf_open_read()
980 memset(elf, 0, offsetof(struct elf, sections)); in elf_open_read()
982 INIT_LIST_HEAD(&elf->sections); in elf_open_read()
984 elf->fd = open(name, flags); in elf_open_read()
985 if (elf->fd == -1) { in elf_open_read()
998 elf->elf = elf_begin(elf->fd, cmd, NULL); in elf_open_read()
999 if (!elf->elf) { in elf_open_read()
1004 if (!gelf_getehdr(elf->elf, &elf->ehdr)) { in elf_open_read()
1009 if (read_sections(elf)) in elf_open_read()
1012 if (read_symbols(elf)) in elf_open_read()
1015 if (read_relocs(elf)) in elf_open_read()
1018 return elf; in elf_open_read()
1021 elf_close(elf); in elf_open_read()
1025 static int elf_add_string(struct elf *elf, struct section *strtab, char *str) in elf_add_string() argument
1032 strtab = find_section_by_name(elf, ".strtab"); in elf_add_string()
1038 s = elf_getscn(elf->elf, strtab->idx); in elf_add_string()
1061 struct section *elf_create_section(struct elf *elf, const char *name, in elf_create_section() argument
1078 s = elf_newscn(elf->elf); in elf_create_section()
1123 shstrtab = find_section_by_name(elf, ".shstrtab"); in elf_create_section()
1125 shstrtab = find_section_by_name(elf, ".strtab"); in elf_create_section()
1130 sec->sh.sh_name = elf_add_string(elf, shstrtab, sec->name); in elf_create_section()
1134 list_add_tail(&sec->list, &elf->sections); in elf_create_section()
1138 elf->changed = true; in elf_create_section()
1143 static struct section *elf_create_rel_reloc_section(struct elf *elf, struct section *base) in elf_create_rel_reloc_section() argument
1156 sec = elf_create_section(elf, relocname, 0, sizeof(GElf_Rel), 0); in elf_create_rel_reloc_section()
1166 sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx; in elf_create_rel_reloc_section()
1173 static struct section *elf_create_rela_reloc_section(struct elf *elf, struct section *base) in elf_create_rela_reloc_section() argument
1177 int addrsize = elf_class_addrsize(elf); in elf_create_rela_reloc_section()
1188 sec = elf_create_section(elf, relocname, 0, sizeof(Elf32_Rela), 0); in elf_create_rela_reloc_section()
1190 sec = elf_create_section(elf, relocname, 0, sizeof(GElf_Rela), 0); in elf_create_rela_reloc_section()
1200 sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx; in elf_create_rela_reloc_section()
1207 static struct section *elf_create_reloc_section(struct elf *elf, in elf_create_reloc_section() argument
1212 case SHT_REL: return elf_create_rel_reloc_section(elf, base); in elf_create_reloc_section()
1213 case SHT_RELA: return elf_create_rela_reloc_section(elf, base); in elf_create_reloc_section()
1281 static int elf_rebuild_reloc_section(struct elf *elf, struct section *sec) in elf_rebuild_reloc_section() argument
1290 int elf_write_insn(struct elf *elf, struct section *sec, in elf_write_insn() argument
1304 elf->changed = true; in elf_write_insn()
1309 int elf_write_reloc(struct elf *elf, struct reloc *reloc) in elf_write_reloc() argument
1332 elf->changed = true; in elf_write_reloc()
1346 static int elf_truncate_section(struct elf *elf, struct section *sec) in elf_truncate_section() argument
1353 s = elf_getscn(elf->elf, sec->idx); in elf_truncate_section()
1391 int elf_write(struct elf *elf) in elf_write() argument
1400 list_for_each_entry(sec, &elf->sections, list) { in elf_write()
1402 elf_truncate_section(elf, sec); in elf_write()
1405 s = elf_getscn(elf->elf, sec->idx); in elf_write()
1416 elf_rebuild_reloc_section(elf, sec)) { in elf_write()
1422 elf->changed = true; in elf_write()
1427 elf_flagelf(elf->elf, ELF_C_SET, ELF_F_DIRTY); in elf_write()
1430 if (elf_update(elf->elf, ELF_C_WRITE) < 0) { in elf_write()
1435 elf->changed = false; in elf_write()
1440 void elf_close(struct elf *elf) in elf_close() argument
1446 if (elf->elf) in elf_close()
1447 elf_end(elf->elf); in elf_close()
1449 if (elf->fd > 0) in elf_close()
1450 close(elf->fd); in elf_close()
1452 list_for_each_entry_safe(sec, tmpsec, &elf->sections, list) { in elf_close()
1465 free(elf->symbol_data); in elf_close()
1466 free(elf->section_data); in elf_close()
1467 free(elf); in elf_close()