Lines Matching refs:elf

31 #define __elf_table(name)	(elf->name##_hash)
32 #define __elf_bits(name) (elf->name##_bits)
86 struct section *find_section_by_name(const struct elf *elf, const char *name) in find_section_by_name() argument
98 static struct section *find_section_by_index(struct elf *elf, in find_section_by_index() argument
111 static struct symbol *find_symbol_by_index(struct elf *elf, unsigned int idx) in find_symbol_by_index() argument
179 struct symbol *find_symbol_by_name(const struct elf *elf, const char *name) in find_symbol_by_name() argument
191 struct reloc *find_reloc_by_dest_range(const struct elf *elf, struct section *sec, in find_reloc_by_dest_range() argument
220 struct reloc *find_reloc_by_dest(const struct elf *elf, struct section *sec, unsigned long offset) in find_reloc_by_dest() argument
222 return find_reloc_by_dest_range(elf, sec, offset, 1); in find_reloc_by_dest()
225 static int read_sections(struct elf *elf) in read_sections() argument
232 if (elf_getshdrnum(elf->elf, &sections_nr)) { in read_sections()
237 if (elf_getshdrstrndx(elf->elf, &shstrndx)) { in read_sections()
257 s = elf_getscn(elf->elf, i); in read_sections()
270 sec->name = elf_strptr(elf->elf, shstrndx, sec->sh.sh_name); in read_sections()
291 elf->text_size += sec->sh.sh_size; in read_sections()
293 list_add_tail(&sec->list, &elf->sections); in read_sections()
300 printf("section_bits: %d\n", elf->section_bits); in read_sections()
304 if (elf_nextscn(elf->elf, s)) { in read_sections()
312 static void elf_add_symbol(struct elf *elf, struct symbol *sym) in elf_add_symbol() argument
341 static int read_symbols(struct elf *elf) in read_symbols() argument
350 symtab = find_section_by_name(elf, ".symtab"); in read_symbols()
352 symtab_shndx = find_section_by_name(elf, ".symtab_shndx"); in read_symbols()
388 sym->name = elf_strptr(elf->elf, symtab->sh.sh_link, in read_symbols()
401 sym->sec = find_section_by_index(elf, shndx); in read_symbols()
412 sym->sec = find_section_by_index(elf, 0); in read_symbols()
414 elf_add_symbol(elf, sym); in read_symbols()
419 printf("symbol_bits: %d\n", elf->symbol_bits); in read_symbols()
423 list_for_each_entry(sec, &elf->sections, list) { in read_symbols()
449 pfunc = find_symbol_by_name(elf, pname); in read_symbols()
483 static struct section *elf_create_reloc_section(struct elf *elf,
487 int elf_add_reloc(struct elf *elf, struct section *sec, unsigned long offset, in elf_add_reloc() argument
492 if (!sec->reloc && !elf_create_reloc_section(elf, sec, SHT_RELA)) in elf_add_reloc()
517 int elf_add_reloc_to_insn(struct elf *elf, struct section *sec, in elf_add_reloc_to_insn() argument
550 return elf_add_reloc(elf, sec, offset, type, sym, addend); in elf_add_reloc_to_insn()
579 static int read_relocs(struct elf *elf) in read_relocs() argument
587 if (!elf_alloc_hash(reloc, elf->text_size / 16)) in read_relocs()
590 list_for_each_entry(sec, &elf->sections, list) { in read_relocs()
595 sec->base = find_section_by_index(elf, sec->sh.sh_info); in read_relocs()
626 reloc->sym = find_symbol_by_index(elf, symndx); in read_relocs()
645 printf("reloc_bits: %d\n", elf->reloc_bits); in read_relocs()
651 struct elf *elf_open_read(const char *name, int flags) in elf_open_read()
653 struct elf *elf; in elf_open_read() local
658 elf = malloc(sizeof(*elf)); in elf_open_read()
659 if (!elf) { in elf_open_read()
663 memset(elf, 0, offsetof(struct elf, sections)); in elf_open_read()
665 INIT_LIST_HEAD(&elf->sections); in elf_open_read()
667 elf->fd = open(name, flags); in elf_open_read()
668 if (elf->fd == -1) { in elf_open_read()
681 elf->elf = elf_begin(elf->fd, cmd, NULL); in elf_open_read()
682 if (!elf->elf) { in elf_open_read()
687 if (!gelf_getehdr(elf->elf, &elf->ehdr)) { in elf_open_read()
692 if (read_sections(elf)) in elf_open_read()
695 if (read_symbols(elf)) in elf_open_read()
698 if (read_relocs(elf)) in elf_open_read()
701 return elf; in elf_open_read()
704 elf_close(elf); in elf_open_read()
708 static int elf_add_string(struct elf *elf, struct section *strtab, char *str) in elf_add_string() argument
715 strtab = find_section_by_name(elf, ".strtab"); in elf_add_string()
721 s = elf_getscn(elf->elf, strtab->idx); in elf_add_string()
744 struct symbol *elf_create_undef_symbol(struct elf *elf, const char *name) in elf_create_undef_symbol() argument
760 sym->sym.st_name = elf_add_string(elf, NULL, sym->name); in elf_create_undef_symbol()
770 symtab = find_section_by_name(elf, ".symtab"); in elf_create_undef_symbol()
776 s = elf_getscn(elf->elf, symtab->idx); in elf_create_undef_symbol()
798 symtab_shndx = find_section_by_name(elf, ".symtab_shndx"); in elf_create_undef_symbol()
800 s = elf_getscn(elf->elf, symtab_shndx->idx); in elf_create_undef_symbol()
821 sym->sec = find_section_by_index(elf, 0); in elf_create_undef_symbol()
823 elf_add_symbol(elf, sym); in elf_create_undef_symbol()
828 struct section *elf_create_section(struct elf *elf, const char *name, in elf_create_section() argument
845 s = elf_newscn(elf->elf); in elf_create_section()
890 shstrtab = find_section_by_name(elf, ".shstrtab"); in elf_create_section()
892 shstrtab = find_section_by_name(elf, ".strtab"); in elf_create_section()
897 sec->sh.sh_name = elf_add_string(elf, shstrtab, sec->name); in elf_create_section()
901 list_add_tail(&sec->list, &elf->sections); in elf_create_section()
905 elf->changed = true; in elf_create_section()
910 static struct section *elf_create_rel_reloc_section(struct elf *elf, struct section *base) in elf_create_rel_reloc_section() argument
923 sec = elf_create_section(elf, relocname, 0, sizeof(GElf_Rel), 0); in elf_create_rel_reloc_section()
933 sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx; in elf_create_rel_reloc_section()
940 static struct section *elf_create_rela_reloc_section(struct elf *elf, struct section *base) in elf_create_rela_reloc_section() argument
953 sec = elf_create_section(elf, relocname, 0, sizeof(GElf_Rela), 0); in elf_create_rela_reloc_section()
963 sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx; in elf_create_rela_reloc_section()
970 static struct section *elf_create_reloc_section(struct elf *elf, in elf_create_reloc_section() argument
975 case SHT_REL: return elf_create_rel_reloc_section(elf, base); in elf_create_reloc_section()
976 case SHT_RELA: return elf_create_rela_reloc_section(elf, base); in elf_create_reloc_section()
1044 static int elf_rebuild_reloc_section(struct elf *elf, struct section *sec) in elf_rebuild_reloc_section() argument
1053 int elf_write_insn(struct elf *elf, struct section *sec, in elf_write_insn() argument
1067 elf->changed = true; in elf_write_insn()
1072 int elf_write_reloc(struct elf *elf, struct reloc *reloc) in elf_write_reloc() argument
1095 elf->changed = true; in elf_write_reloc()
1100 int elf_write(struct elf *elf) in elf_write() argument
1106 list_for_each_entry(sec, &elf->sections, list) { in elf_write()
1108 s = elf_getscn(elf->elf, sec->idx); in elf_write()
1119 elf_rebuild_reloc_section(elf, sec)) { in elf_write()
1125 elf->changed = true; in elf_write()
1130 elf_flagelf(elf->elf, ELF_C_SET, ELF_F_DIRTY); in elf_write()
1133 if (elf_update(elf->elf, ELF_C_WRITE) < 0) { in elf_write()
1138 elf->changed = false; in elf_write()
1143 void elf_close(struct elf *elf) in elf_close() argument
1149 if (elf->elf) in elf_close()
1150 elf_end(elf->elf); in elf_close()
1152 if (elf->fd > 0) in elf_close()
1153 close(elf->fd); in elf_close()
1155 list_for_each_entry_safe(sec, tmpsec, &elf->sections, list) { in elf_close()
1170 free(elf); in elf_close()