Lines Matching refs:elf
3 * elf.c - ELF access library
22 #include <objtool/elf.h>
32 #define __elf_table(name) (elf->name##_hash)
33 #define __elf_bits(name) (elf->name##_bits)
132 struct section *find_section_by_name(const struct elf *elf, const char *name)
144 static struct section *find_section_by_index(struct elf *elf,
157 static struct symbol *find_symbol_by_index(struct elf *elf, unsigned int idx)
256 struct symbol *find_symbol_by_name(const struct elf *elf, const char *name)
268 struct reloc *find_reloc_by_dest_range(const struct elf *elf, struct section *sec,
298 struct reloc *find_reloc_by_dest(const struct elf *elf, struct section *sec, unsigned long offset)
300 return find_reloc_by_dest_range(elf, sec, offset, 1);
308 static int read_sections(struct elf *elf)
315 if (elf_getshdrnum(elf->elf, §ions_nr)) {
320 if (elf_getshdrstrndx(elf->elf, &shstrndx)) {
329 elf->section_data = calloc(sections_nr, sizeof(*sec));
330 if (!elf->section_data) {
335 sec = &elf->section_data[i];
339 s = elf_getscn(elf->elf, i);
352 sec->name = elf_strptr(elf->elf, shstrndx, sec->sh.sh_name);
372 list_add_tail(&sec->list, &elf->sections);
377 elf->num_relocs += sec_num_entries(sec);
382 printf("section_bits: %d\n", elf->section_bits);
386 if (elf_nextscn(elf->elf, s)) {
394 static void elf_add_symbol(struct elf *elf, struct symbol *sym)
407 elf->num_files++;
435 static int read_symbols(struct elf *elf)
444 symtab = find_section_by_name(elf, ".symtab");
446 symtab_shndx = find_section_by_name(elf, ".symtab_shndx");
465 elf->symbol_data = calloc(symbols_nr, sizeof(*sym));
466 if (!elf->symbol_data) {
471 sym = &elf->symbol_data[i];
481 sym->name = elf_strptr(elf->elf, symtab->sh.sh_link,
494 sym->sec = find_section_by_index(elf, shndx);
505 sym->sec = find_section_by_index(elf, 0);
507 elf_add_symbol(elf, sym);
512 printf("symbol_bits: %d\n", elf->symbol_bits);
516 list_for_each_entry(sec, &elf->sections, list) {
542 pfunc = find_symbol_by_name(elf, pname);
579 static int elf_update_sym_relocs(struct elf *elf, struct symbol *sym)
584 set_reloc_sym(elf, reloc, reloc->sym->idx);
597 static int elf_update_symbol(struct elf *elf, struct section *symtab,
611 s = elf_getscn(elf->elf, symtab->idx);
618 t = elf_getscn(elf->elf, symtab_shndx->idx);
663 mark_sec_changed(elf, symtab, true);
678 mark_sec_changed(elf, symtab_shndx, true);
728 __elf_create_symbol(struct elf *elf, struct symbol *sym)
734 symtab = find_section_by_name(elf, ".symtab");
736 symtab_shndx = find_section_by_name(elf, ".symtab_shndx");
752 old = find_symbol_by_index(elf, first_non_local);
759 if (elf_update_symbol(elf, symtab, symtab_shndx, old)) {
764 if (elf_update_sym_relocs(elf, old))
777 if (elf_update_symbol(elf, symtab, symtab_shndx, sym)) {
783 mark_sec_changed(elf, symtab, true);
787 mark_sec_changed(elf, symtab_shndx, true);
794 elf_create_section_symbol(struct elf *elf, struct section *sec)
812 sym = __elf_create_symbol(elf, sym);
814 elf_add_symbol(elf, sym);
819 static int elf_add_string(struct elf *elf, struct section *strtab, char *str);
822 elf_create_prefix_symbol(struct elf *elf, struct symbol *orig, long size)
838 sym->sym.st_name = elf_add_string(elf, NULL, name);
843 sym = __elf_create_symbol(elf, sym);
845 elf_add_symbol(elf, sym);
850 static struct reloc *elf_init_reloc(struct elf *elf, struct section *rsec,
874 set_reloc_offset(elf, reloc, offset);
875 set_reloc_sym(elf, reloc, sym->idx);
876 set_reloc_type(elf, reloc, type);
877 set_reloc_addend(elf, reloc, addend);
886 struct reloc *elf_init_reloc_text_sym(struct elf *elf, struct section *sec,
908 sym = elf_create_section_symbol(elf, insn_sec);
915 return elf_init_reloc(elf, sec->rsec, reloc_idx, offset, sym, addend,
916 elf_text_rela_type(elf));
919 struct reloc *elf_init_reloc_data_sym(struct elf *elf, struct section *sec,
931 return elf_init_reloc(elf, sec->rsec, reloc_idx, offset, sym, addend,
932 elf_data_rela_type(elf));
935 static int read_relocs(struct elf *elf)
944 if (!elf_alloc_hash(reloc, elf->num_relocs))
947 list_for_each_entry(rsec, &elf->sections, list) {
951 rsec->base = find_section_by_index(elf, rsec->sh.sh_info);
971 reloc->sym = sym = find_symbol_by_index(elf, symndx);
989 printf("num_relocs: %lu\n", elf->num_relocs);
990 printf("reloc_bits: %d\n", elf->reloc_bits);
996 struct elf *elf_open_read(const char *name, int flags)
998 struct elf *elf;
1003 elf = malloc(sizeof(*elf));
1004 if (!elf) {
1008 memset(elf, 0, sizeof(*elf));
1010 INIT_LIST_HEAD(&elf->sections);
1012 elf->fd = open(name, flags);
1013 if (elf->fd == -1) {
1026 elf->elf = elf_begin(elf->fd, cmd, NULL);
1027 if (!elf->elf) {
1032 if (!gelf_getehdr(elf->elf, &elf->ehdr)) {
1037 if (read_sections(elf))
1040 if (read_symbols(elf))
1043 if (read_relocs(elf))
1046 return elf;
1049 elf_close(elf);
1053 static int elf_add_string(struct elf *elf, struct section *strtab, char *str)
1060 strtab = find_section_by_name(elf, ".strtab");
1066 s = elf_getscn(elf->elf, strtab->idx);
1085 mark_sec_changed(elf, strtab, true);
1090 struct section *elf_create_section(struct elf *elf, const char *name,
1106 s = elf_newscn(elf->elf);
1150 shstrtab = find_section_by_name(elf, ".shstrtab");
1152 shstrtab = find_section_by_name(elf, ".strtab");
1157 sec->sh.sh_name = elf_add_string(elf, shstrtab, sec->name);
1161 list_add_tail(&sec->list, &elf->sections);
1165 mark_sec_changed(elf, sec, true);
1170 static struct section *elf_create_rela_section(struct elf *elf,
1185 rsec = elf_create_section(elf, rsec_name, elf_rela_size(elf), reloc_nr);
1192 rsec->sh.sh_addralign = elf_addr_size(elf);
1193 rsec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
1209 struct section *elf_create_section_pair(struct elf *elf, const char *name,
1215 sec = elf_create_section(elf, name, entsize, nr);
1219 if (!elf_create_rela_section(elf, sec, reloc_nr))
1225 int elf_write_insn(struct elf *elf, struct section *sec,
1238 mark_sec_changed(elf, sec, true);
1252 static int elf_truncate_section(struct elf *elf, struct section *sec)
1259 s = elf_getscn(elf->elf, sec->idx);
1297 int elf_write(struct elf *elf)
1306 list_for_each_entry(sec, &elf->sections, list) {
1308 elf_truncate_section(elf, sec);
1311 s = elf_getscn(elf->elf, sec->idx);
1323 mark_sec_changed(elf, sec, false);
1328 elf_flagelf(elf->elf, ELF_C_SET, ELF_F_DIRTY);
1331 if (elf_update(elf->elf, ELF_C_WRITE) < 0) {
1336 elf->changed = false;
1341 void elf_close(struct elf *elf)
1343 if (elf->elf)
1344 elf_end(elf->elf);
1346 if (elf->fd > 0)
1347 close(elf->fd);