Lines Matching defs:elf

3  * elf.c - ELF access library
22 #include <objtool/elf.h>
30 #define __elf_table(name) (elf->name##_hash)
31 #define __elf_bits(name) (elf->name##_bits)
130 struct section *find_section_by_name(const struct elf *elf, const char *name)
142 static struct section *find_section_by_index(struct elf *elf,
155 static struct symbol *find_symbol_by_index(struct elf *elf, unsigned int idx)
254 struct symbol *find_symbol_by_name(const struct elf *elf, const char *name)
266 struct reloc *find_reloc_by_dest_range(const struct elf *elf, struct section *sec,
296 struct reloc *find_reloc_by_dest(const struct elf *elf, struct section *sec, unsigned long offset)
298 return find_reloc_by_dest_range(elf, sec, offset, 1);
306 static int read_sections(struct elf *elf)
313 if (elf_getshdrnum(elf->elf, &sections_nr)) {
318 if (elf_getshdrstrndx(elf->elf, &shstrndx)) {
327 elf->section_data = calloc(sections_nr, sizeof(*sec));
328 if (!elf->section_data) {
333 sec = &elf->section_data[i];
337 s = elf_getscn(elf->elf, i);
350 sec->name = elf_strptr(elf->elf, shstrndx, sec->sh.sh_name);
370 list_add_tail(&sec->list, &elf->sections);
375 elf->num_relocs += sec_num_entries(sec);
380 printf("section_bits: %d\n", elf->section_bits);
384 if (elf_nextscn(elf->elf, s)) {
392 static void elf_add_symbol(struct elf *elf, struct symbol *sym)
405 elf->num_files++;
433 static int read_symbols(struct elf *elf)
442 symtab = find_section_by_name(elf, ".symtab");
444 symtab_shndx = find_section_by_name(elf, ".symtab_shndx");
463 elf->symbol_data = calloc(symbols_nr, sizeof(*sym));
464 if (!elf->symbol_data) {
469 sym = &elf->symbol_data[i];
479 sym->name = elf_strptr(elf->elf, symtab->sh.sh_link,
492 sym->sec = find_section_by_index(elf, shndx);
503 sym->sec = find_section_by_index(elf, 0);
505 elf_add_symbol(elf, sym);
510 printf("symbol_bits: %d\n", elf->symbol_bits);
514 list_for_each_entry(sec, &elf->sections, list) {
539 pfunc = find_symbol_by_name(elf, pname);
577 static int elf_update_sym_relocs(struct elf *elf, struct symbol *sym)
582 set_reloc_sym(elf, reloc, reloc->sym->idx);
595 static int elf_update_symbol(struct elf *elf, struct section *symtab,
609 s = elf_getscn(elf->elf, symtab->idx);
616 t = elf_getscn(elf->elf, symtab_shndx->idx);
661 mark_sec_changed(elf, symtab, true);
676 mark_sec_changed(elf, symtab_shndx, true);
726 __elf_create_symbol(struct elf *elf, struct symbol *sym)
732 symtab = find_section_by_name(elf, ".symtab");
734 symtab_shndx = find_section_by_name(elf, ".symtab_shndx");
750 old = find_symbol_by_index(elf, first_non_local);
757 if (elf_update_symbol(elf, symtab, symtab_shndx, old)) {
762 if (elf_update_sym_relocs(elf, old))
775 if (elf_update_symbol(elf, symtab, symtab_shndx, sym)) {
781 mark_sec_changed(elf, symtab, true);
785 mark_sec_changed(elf, symtab_shndx, true);
792 elf_create_section_symbol(struct elf *elf, struct section *sec)
810 sym = __elf_create_symbol(elf, sym);
812 elf_add_symbol(elf, sym);
817 static int elf_add_string(struct elf *elf, struct section *strtab, char *str);
820 elf_create_prefix_symbol(struct elf *elf, struct symbol *orig, long size)
836 sym->sym.st_name = elf_add_string(elf, NULL, name);
841 sym = __elf_create_symbol(elf, sym);
843 elf_add_symbol(elf, sym);
848 static struct reloc *elf_init_reloc(struct elf *elf, struct section *rsec,
872 set_reloc_offset(elf, reloc, offset);
873 set_reloc_sym(elf, reloc, sym->idx);
874 set_reloc_type(elf, reloc, type);
875 set_reloc_addend(elf, reloc, addend);
884 struct reloc *elf_init_reloc_text_sym(struct elf *elf, struct section *sec,
906 sym = elf_create_section_symbol(elf, insn_sec);
913 return elf_init_reloc(elf, sec->rsec, reloc_idx, offset, sym, addend,
914 elf_text_rela_type(elf));
917 struct reloc *elf_init_reloc_data_sym(struct elf *elf, struct section *sec,
929 return elf_init_reloc(elf, sec->rsec, reloc_idx, offset, sym, addend,
930 elf_data_rela_type(elf));
933 static int read_relocs(struct elf *elf)
942 if (!elf_alloc_hash(reloc, elf->num_relocs))
945 list_for_each_entry(rsec, &elf->sections, list) {
949 rsec->base = find_section_by_index(elf, rsec->sh.sh_info);
969 reloc->sym = sym = find_symbol_by_index(elf, symndx);
987 printf("num_relocs: %lu\n", elf->num_relocs);
988 printf("reloc_bits: %d\n", elf->reloc_bits);
994 struct elf *elf_open_read(const char *name, int flags)
996 struct elf *elf;
1001 elf = malloc(sizeof(*elf));
1002 if (!elf) {
1006 memset(elf, 0, sizeof(*elf));
1008 INIT_LIST_HEAD(&elf->sections);
1010 elf->fd = open(name, flags);
1011 if (elf->fd == -1) {
1024 elf->elf = elf_begin(elf->fd, cmd, NULL);
1025 if (!elf->elf) {
1030 if (!gelf_getehdr(elf->elf, &elf->ehdr)) {
1035 if (read_sections(elf))
1038 if (read_symbols(elf))
1041 if (read_relocs(elf))
1044 return elf;
1047 elf_close(elf);
1051 static int elf_add_string(struct elf *elf, struct section *strtab, char *str)
1058 strtab = find_section_by_name(elf, ".strtab");
1064 s = elf_getscn(elf->elf, strtab->idx);
1083 mark_sec_changed(elf, strtab, true);
1088 struct section *elf_create_section(struct elf *elf, const char *name,
1104 s = elf_newscn(elf->elf);
1148 shstrtab = find_section_by_name(elf, ".shstrtab");
1150 shstrtab = find_section_by_name(elf, ".strtab");
1155 sec->sh.sh_name = elf_add_string(elf, shstrtab, sec->name);
1159 list_add_tail(&sec->list, &elf->sections);
1163 mark_sec_changed(elf, sec, true);
1168 static struct section *elf_create_rela_section(struct elf *elf,
1183 rsec = elf_create_section(elf, rsec_name, elf_rela_size(elf), reloc_nr);
1190 rsec->sh.sh_addralign = elf_addr_size(elf);
1191 rsec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
1207 struct section *elf_create_section_pair(struct elf *elf, const char *name,
1213 sec = elf_create_section(elf, name, entsize, nr);
1217 if (!elf_create_rela_section(elf, sec, reloc_nr))
1223 int elf_write_insn(struct elf *elf, struct section *sec,
1236 mark_sec_changed(elf, sec, true);
1250 static int elf_truncate_section(struct elf *elf, struct section *sec)
1257 s = elf_getscn(elf->elf, sec->idx);
1295 int elf_write(struct elf *elf)
1304 list_for_each_entry(sec, &elf->sections, list) {
1306 elf_truncate_section(elf, sec);
1309 s = elf_getscn(elf->elf, sec->idx);
1321 mark_sec_changed(elf, sec, false);
1326 elf_flagelf(elf->elf, ELF_C_SET, ELF_F_DIRTY);
1329 if (elf_update(elf->elf, ELF_C_WRITE) < 0) {
1334 elf->changed = false;
1339 void elf_close(struct elf *elf)
1341 if (elf->elf)
1342 elf_end(elf->elf);
1344 if (elf->fd > 0)
1345 close(elf->fd);