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()
389 sym->name = elf_strptr(elf->elf, symtab->sh.sh_link, in read_symbols()
402 sym->sec = find_section_by_index(elf, shndx); in read_symbols()
413 sym->sec = find_section_by_index(elf, 0); in read_symbols()
415 elf_add_symbol(elf, sym); in read_symbols()
420 printf("symbol_bits: %d\n", elf->symbol_bits); in read_symbols()
424 list_for_each_entry(sec, &elf->sections, list) { in read_symbols()
450 pfunc = find_symbol_by_name(elf, pname); in read_symbols()
484 static struct section *elf_create_reloc_section(struct elf *elf,
488 int elf_add_reloc(struct elf *elf, struct section *sec, unsigned long offset, in elf_add_reloc() argument
493 if (!sec->reloc && !elf_create_reloc_section(elf, sec, SHT_RELA)) in elf_add_reloc()
518 int elf_add_reloc_to_insn(struct elf *elf, struct section *sec, in elf_add_reloc_to_insn() argument
551 return elf_add_reloc(elf, sec, offset, type, sym, addend); in elf_add_reloc_to_insn()
580 static int read_relocs(struct elf *elf) in read_relocs() argument
588 if (!elf_alloc_hash(reloc, elf->text_size / 16)) in read_relocs()
591 list_for_each_entry(sec, &elf->sections, list) { in read_relocs()
596 sec->base = find_section_by_index(elf, sec->sh.sh_info); in read_relocs()
627 reloc->sym = find_symbol_by_index(elf, symndx); in read_relocs()
646 printf("reloc_bits: %d\n", elf->reloc_bits); in read_relocs()
652 struct elf *elf_open_read(const char *name, int flags) in elf_open_read()
654 struct elf *elf; in elf_open_read() local
659 elf = malloc(sizeof(*elf)); in elf_open_read()
660 if (!elf) { in elf_open_read()
664 memset(elf, 0, offsetof(struct elf, sections)); in elf_open_read()
666 INIT_LIST_HEAD(&elf->sections); in elf_open_read()
668 elf->fd = open(name, flags); in elf_open_read()
669 if (elf->fd == -1) { in elf_open_read()
682 elf->elf = elf_begin(elf->fd, cmd, NULL); in elf_open_read()
683 if (!elf->elf) { in elf_open_read()
688 if (!gelf_getehdr(elf->elf, &elf->ehdr)) { in elf_open_read()
693 if (read_sections(elf)) in elf_open_read()
696 if (read_symbols(elf)) in elf_open_read()
699 if (read_relocs(elf)) in elf_open_read()
702 return elf; in elf_open_read()
705 elf_close(elf); in elf_open_read()
709 static int elf_add_string(struct elf *elf, struct section *strtab, char *str) in elf_add_string() argument
716 strtab = find_section_by_name(elf, ".strtab"); in elf_add_string()
722 s = elf_getscn(elf->elf, strtab->idx); in elf_add_string()
745 struct section *elf_create_section(struct elf *elf, const char *name, in elf_create_section() argument
762 s = elf_newscn(elf->elf); in elf_create_section()
807 shstrtab = find_section_by_name(elf, ".shstrtab"); in elf_create_section()
809 shstrtab = find_section_by_name(elf, ".strtab"); in elf_create_section()
814 sec->sh.sh_name = elf_add_string(elf, shstrtab, sec->name); in elf_create_section()
818 list_add_tail(&sec->list, &elf->sections); in elf_create_section()
822 elf->changed = true; in elf_create_section()
827 static struct section *elf_create_rel_reloc_section(struct elf *elf, struct section *base) in elf_create_rel_reloc_section() argument
840 sec = elf_create_section(elf, relocname, 0, sizeof(GElf_Rel), 0); in elf_create_rel_reloc_section()
850 sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx; in elf_create_rel_reloc_section()
857 static struct section *elf_create_rela_reloc_section(struct elf *elf, struct section *base) in elf_create_rela_reloc_section() argument
870 sec = elf_create_section(elf, relocname, 0, sizeof(GElf_Rela), 0); in elf_create_rela_reloc_section()
880 sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx; in elf_create_rela_reloc_section()
887 static struct section *elf_create_reloc_section(struct elf *elf, in elf_create_reloc_section() argument
892 case SHT_REL: return elf_create_rel_reloc_section(elf, base); in elf_create_reloc_section()
893 case SHT_RELA: return elf_create_rela_reloc_section(elf, base); in elf_create_reloc_section()
961 static int elf_rebuild_reloc_section(struct elf *elf, struct section *sec) in elf_rebuild_reloc_section() argument
970 int elf_write_insn(struct elf *elf, struct section *sec, in elf_write_insn() argument
984 elf->changed = true; in elf_write_insn()
989 int elf_write_reloc(struct elf *elf, struct reloc *reloc) in elf_write_reloc() argument
1012 elf->changed = true; in elf_write_reloc()
1017 int elf_write(struct elf *elf) in elf_write() argument
1023 list_for_each_entry(sec, &elf->sections, list) { in elf_write()
1025 s = elf_getscn(elf->elf, sec->idx); in elf_write()
1036 elf_rebuild_reloc_section(elf, sec)) { in elf_write()
1042 elf->changed = true; in elf_write()
1047 elf_flagelf(elf->elf, ELF_C_SET, ELF_F_DIRTY); in elf_write()
1050 if (elf_update(elf->elf, ELF_C_WRITE) < 0) { in elf_write()
1055 elf->changed = false; in elf_write()
1060 void elf_close(struct elf *elf) in elf_close() argument
1066 if (elf->elf) in elf_close()
1067 elf_end(elf->elf); in elf_close()
1069 if (elf->fd > 0) in elf_close()
1070 close(elf->fd); in elf_close()
1072 list_for_each_entry_safe(sec, tmpsec, &elf->sections, list) { in elf_close()
1087 free(elf); in elf_close()