• Home
  • Raw
  • Download

Lines Matching full:elf

3  * elf.c - ELF access library
20 #include "elf.h"
130 struct section *find_section_by_name(const struct elf *elf, const char *name) in find_section_by_name() argument
134 elf_hash_for_each_possible(elf->section_name_hash, sec, name_hash, str_hash(name)) in find_section_by_name()
141 static struct section *find_section_by_index(struct elf *elf, in find_section_by_index() argument
146 elf_hash_for_each_possible(elf->section_hash, sec, hash, idx) in find_section_by_index()
153 static struct symbol *find_symbol_by_index(struct elf *elf, unsigned int idx) in find_symbol_by_index() argument
157 elf_hash_for_each_possible(elf->symbol_hash, sym, hash, idx) in find_symbol_by_index()
220 struct symbol *find_symbol_by_name(const struct elf *elf, const char *name) in find_symbol_by_name() argument
224 elf_hash_for_each_possible(elf->symbol_name_hash, sym, name_hash, str_hash(name)) in find_symbol_by_name()
231 struct reloc *find_reloc_by_dest_range(const struct elf *elf, struct section *sec, in find_reloc_by_dest_range() argument
243 elf_hash_for_each_possible(elf->reloc_hash, reloc, hash, in find_reloc_by_dest_range()
260 struct reloc *find_reloc_by_dest(const struct elf *elf, struct section *sec, unsigned long offset) in find_reloc_by_dest() argument
262 return find_reloc_by_dest_range(elf, sec, offset, 1); in find_reloc_by_dest()
265 static int read_sections(struct elf *elf) in read_sections() argument
272 if (elf_getshdrnum(elf->elf, &sections_nr)) { in read_sections()
277 if (elf_getshdrstrndx(elf->elf, &shstrndx)) { in read_sections()
293 s = elf_getscn(elf->elf, i); in read_sections()
306 sec->name = elf_strptr(elf->elf, shstrndx, sec->sh.sh_name); in read_sections()
327 list_add_tail(&sec->list, &elf->sections); in read_sections()
328 elf_hash_add(elf->section_hash, &sec->hash, sec->idx); in read_sections()
329 elf_hash_add(elf->section_name_hash, &sec->name_hash, str_hash(sec->name)); in read_sections()
336 if (elf_nextscn(elf->elf, s)) { in read_sections()
344 static void elf_add_symbol(struct elf *elf, struct symbol *sym) in elf_add_symbol() argument
364 elf_hash_add(elf->symbol_hash, &sym->hash, sym->idx); in elf_add_symbol()
365 elf_hash_add(elf->symbol_name_hash, &sym->name_hash, str_hash(sym->name)); in elf_add_symbol()
375 static int read_symbols(struct elf *elf) in read_symbols() argument
384 symtab = find_section_by_name(elf, ".symtab"); in read_symbols()
393 symtab_shndx = find_section_by_name(elf, ".symtab_shndx"); in read_symbols()
415 sym->name = elf_strptr(elf->elf, symtab->sh.sh_link, in read_symbols()
428 sym->sec = find_section_by_index(elf, shndx); in read_symbols()
439 sym->sec = find_section_by_index(elf, 0); in read_symbols()
441 elf_add_symbol(elf, sym); in read_symbols()
448 list_for_each_entry(sec, &elf->sections, list) { in read_symbols()
474 pfunc = find_symbol_by_name(elf, pname); in read_symbols()
508 static struct section *elf_create_reloc_section(struct elf *elf,
512 int elf_add_reloc(struct elf *elf, struct section *sec, unsigned long offset, in elf_add_reloc() argument
517 if (!sec->reloc && !elf_create_reloc_section(elf, sec, SHT_RELA)) in elf_add_reloc()
534 elf_hash_add(elf->reloc_hash, &reloc->hash, reloc_hash(reloc)); in elf_add_reloc()
546 static void elf_dirty_reloc_sym(struct elf *elf, struct symbol *sym) in elf_dirty_reloc_sym() argument
550 list_for_each_entry(sec, &elf->sections, list) { in elf_dirty_reloc_sym()
573 static int elf_update_symbol(struct elf *elf, struct section *symtab, in elf_update_symbol() argument
587 s = elf_getscn(elf->elf, symtab->idx); in elf_update_symbol()
594 t = elf_getscn(elf->elf, symtab_shndx->idx); in elf_update_symbol()
692 elf_create_section_symbol(struct elf *elf, struct section *sec) in elf_create_section_symbol() argument
698 symtab = find_section_by_name(elf, ".symtab"); in elf_create_section_symbol()
700 symtab_shndx = find_section_by_name(elf, ".symtab_shndx"); in elf_create_section_symbol()
727 old = find_symbol_by_index(elf, first_non_local); in elf_create_section_symbol()
732 elf_hash_add(elf->symbol_hash, &old->hash, old->idx); in elf_create_section_symbol()
734 elf_dirty_reloc_sym(elf, old); in elf_create_section_symbol()
736 if (elf_update_symbol(elf, symtab, symtab_shndx, old)) { in elf_create_section_symbol()
745 if (elf_update_symbol(elf, symtab, symtab_shndx, sym)) { in elf_create_section_symbol()
755 elf_add_symbol(elf, sym); in elf_create_section_symbol()
760 int elf_add_reloc_to_insn(struct elf *elf, struct section *sec, in elf_add_reloc_to_insn() argument
774 sym = elf_create_section_symbol(elf, insn_sec); in elf_add_reloc_to_insn()
781 return elf_add_reloc(elf, sec, offset, type, sym, addend); in elf_add_reloc_to_insn()
810 static int read_relocs(struct elf *elf) in read_relocs() argument
818 list_for_each_entry(sec, &elf->sections, list) { in read_relocs()
823 sec->base = find_section_by_index(elf, sec->sh.sh_info); in read_relocs()
854 reloc->sym = find_symbol_by_index(elf, symndx); in read_relocs()
862 elf_hash_add(elf->reloc_hash, &reloc->hash, reloc_hash(reloc)); in read_relocs()
878 struct elf *elf_open_read(const char *name, int flags) in elf_open_read()
880 struct elf *elf; in elf_open_read() local
885 elf = malloc(sizeof(*elf)); in elf_open_read()
886 if (!elf) { in elf_open_read()
890 memset(elf, 0, offsetof(struct elf, sections)); in elf_open_read()
892 INIT_LIST_HEAD(&elf->sections); in elf_open_read()
894 elf_hash_init(elf->symbol_hash); in elf_open_read()
895 elf_hash_init(elf->symbol_name_hash); in elf_open_read()
896 elf_hash_init(elf->section_hash); in elf_open_read()
897 elf_hash_init(elf->section_name_hash); in elf_open_read()
898 elf_hash_init(elf->reloc_hash); in elf_open_read()
900 elf->fd = open(name, flags); in elf_open_read()
901 if (elf->fd == -1) { in elf_open_read()
914 elf->elf = elf_begin(elf->fd, cmd, NULL); in elf_open_read()
915 if (!elf->elf) { in elf_open_read()
920 if (!gelf_getehdr(elf->elf, &elf->ehdr)) { in elf_open_read()
925 if (read_sections(elf)) in elf_open_read()
928 if (read_symbols(elf)) in elf_open_read()
931 if (read_relocs(elf)) in elf_open_read()
934 return elf; in elf_open_read()
937 elf_close(elf); in elf_open_read()
941 static int elf_add_string(struct elf *elf, struct section *strtab, char *str) in elf_add_string() argument
948 strtab = find_section_by_name(elf, ".strtab"); in elf_add_string()
954 s = elf_getscn(elf->elf, strtab->idx); in elf_add_string()
978 struct section *elf_create_section(struct elf *elf, const char *name, in elf_create_section() argument
995 s = elf_newscn(elf->elf); in elf_create_section()
1041 shstrtab = find_section_by_name(elf, ".shstrtab"); in elf_create_section()
1043 shstrtab = find_section_by_name(elf, ".strtab"); in elf_create_section()
1048 sec->sh.sh_name = elf_add_string(elf, shstrtab, sec->name); in elf_create_section()
1052 list_add_tail(&sec->list, &elf->sections); in elf_create_section()
1053 elf_hash_add(elf->section_hash, &sec->hash, sec->idx); in elf_create_section()
1054 elf_hash_add(elf->section_name_hash, &sec->name_hash, str_hash(sec->name)); in elf_create_section()
1056 elf->changed = true; in elf_create_section()
1061 static struct section *elf_create_rel_reloc_section(struct elf *elf, struct section *base) in elf_create_rel_reloc_section() argument
1074 sec = elf_create_section(elf, relocname, 0, sizeof(GElf_Rel), 0); in elf_create_rel_reloc_section()
1084 sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx; in elf_create_rel_reloc_section()
1091 static struct section *elf_create_rela_reloc_section(struct elf *elf, struct section *base) in elf_create_rela_reloc_section() argument
1104 sec = elf_create_section(elf, relocname, 0, sizeof(GElf_Rela), 0); in elf_create_rela_reloc_section()
1114 sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx; in elf_create_rela_reloc_section()
1121 static struct section *elf_create_reloc_section(struct elf *elf, in elf_create_reloc_section() argument
1126 case SHT_REL: return elf_create_rel_reloc_section(elf, base); in elf_create_reloc_section()
1127 case SHT_RELA: return elf_create_rela_reloc_section(elf, base); in elf_create_reloc_section()
1191 static int elf_rebuild_reloc_section(struct elf *elf, struct section *sec) in elf_rebuild_reloc_section() argument
1207 int elf_write_insn(struct elf *elf, struct section *sec, in elf_write_insn() argument
1221 elf->changed = true; in elf_write_insn()
1226 int elf_write_reloc(struct elf *elf, struct reloc *reloc) in elf_write_reloc() argument
1249 elf->changed = true; in elf_write_reloc()
1254 int elf_write(struct elf *elf) in elf_write() argument
1260 list_for_each_entry(sec, &elf->sections, list) { in elf_write()
1263 elf_rebuild_reloc_section(elf, sec)) { in elf_write()
1268 s = elf_getscn(elf->elf, sec->idx); in elf_write()
1279 elf->changed = true; in elf_write()
1284 elf_flagelf(elf->elf, ELF_C_SET, ELF_F_DIRTY); in elf_write()
1287 if (elf_update(elf->elf, ELF_C_WRITE) < 0) { in elf_write()
1292 elf->changed = false; in elf_write()
1297 void elf_close(struct elf *elf) in elf_close() argument
1303 if (elf->elf) in elf_close()
1304 elf_end(elf->elf); in elf_close()
1306 if (elf->fd > 0) in elf_close()
1307 close(elf->fd); in elf_close()
1309 list_for_each_entry_safe(sec, tmpsec, &elf->sections, list) { in elf_close()
1324 free(elf); in elf_close()