int findelfsecidx(Elf *elf, const char *file, const char *tofind) { Elf_Scn *scn = NULL; GElf_Ehdr ehdr; GElf_Shdr shdr; if (gelf_getehdr(elf, &ehdr) == NULL) elfterminate(file, "Couldn't read ehdr"); while ((scn = elf_nextscn(elf, scn)) != NULL) { char *name; if (gelf_getshdr(scn, &shdr) == NULL) { elfterminate(file, "Couldn't read header for section %d", elf_ndxscn(scn)); } if ((name = elf_strptr(elf, ehdr.e_shstrndx, (size_t)shdr.sh_name)) == NULL) { elfterminate(file, "Couldn't get name for section %d", elf_ndxscn(scn)); } if (strcmp(name, tofind) == 0) return (elf_ndxscn(scn)); } return (-1); }
int hash_lookup(Elf *elf, section_info_t *hash_info, section_info_t *symtab_info, const char *symname, GElf_Sym *sym_mem) { Elf32_Word *hash_data = (Elf32_Word *)hash_info->data->d_buf; Elf32_Word index; Elf32_Word nbuckets = *hash_data++; Elf32_Word *buckets = ++hash_data; Elf32_Word *chains = hash_data + nbuckets; GElf_Sym *sym; index = buckets[elf_hash(symname) % nbuckets]; while(index != STN_UNDEF) { sym = gelf_getsymshndx (symtab_info->data, NULL, index, sym_mem, NULL); FAILIF_LIBELF(NULL == sym, gelf_getsymshndx); if (!strcmp(symname, elf_strptr(elf, symtab_info->hdr->sh_link, sym->st_name))) break; index = chains[index]; } return index; }
static elf_info_s *elf_symbols(int fd) { /* Open Elf */ elf_version(EV_CURRENT); elf_info_s *elf = calloc(1, sizeof (elf_info_s)); elf->elf = elf_begin(fd, ELF_C_READ, NULL); gelf_getehdr(elf->elf, &elf->hdr); /* Iterate through the sections */ Elf_Scn *scn = NULL; while ((scn = elf_nextscn(elf->elf, scn)) != 0) { GElf_Shdr shdr; const char *name; /* get section header */ gelf_getshdr(scn, &shdr); /* get section name */ name = elf_strptr(elf->elf, elf->hdr.e_shstrndx, shdr.sh_name); //LOG(INFO, "Iter on %s", name); if (shdr.sh_type == SHT_DYNSYM) handle_dynsym_section(elf, scn, shdr); else if (shdr.sh_type == SHT_DYNAMIC) handle_dynamic_section(elf, scn, shdr); else if ((shdr.sh_type == SHT_PROGBITS || shdr.sh_type == SHT_NOBITS) && !strcmp(name, ".plt")) handle_plt_section(elf, shdr); else if (!strcmp(name, ".got.plt")) handle_gotplt_section(elf, shdr); } return elf; }
static void resolve_globals( void ) { OBJECT_FILE *file; global_t *global_sym; Elf32_Sym *symbol; int i; int compare; char *sym_name; file = objects; while (file) { symbol = file->symbols + file->first_global; for ( i = file->first_global ; i < file->symbol_count ; ++i ) { sym_name = elf_strptr( file->elf, file->string_index, symbol->st_name); if (symbol->st_shndx == SHN_UNDEF) { global_sym = (global_t *)bbtree_preinsert( &globals, sym_name, &compare ); if (compare == 0) { symbol->st_value = global_sym->symbol->st_value; /* set to any section other than SHN_UNDEF */ symbol->st_shndx = 1; } /* not an error until a symbol is referenced */ } ++symbol; } file = file->next_object; } }
static SYM64 * read_64_syms(Elf64_Sym *data, size_t num, Elf *elf, Elf64_Word link) { #ifdef HAVE_ELF64_GETEHDR SYM64 *s, *buf; size_t i; if( (buf = (SYM64 *)calloc(num, sizeof(SYM64)) ) == NULL) { return NULL; } s = buf; /* save pointer to head of array */ for(i=1; i<num; i++,data++,buf++) { buf->indx = i; buf->name = (char *)elf_strptr(elf, link, data->st_name); buf->value = data->st_value; buf->size = data->st_size; buf->type = ELF64_ST_TYPE(data->st_info); buf->bind = ELF64_ST_BIND(data->st_info); buf->other = data->st_other; buf->shndx = data->st_shndx; } /* end for loop */ return(s); #else return 0; #endif /* HAVE_ELF64_GETEHDR */ }
std::string get_embedded_repo() { GElf_Shdr shdr; size_t shstrndx; char *name; Elf_Scn *scn; if (elf_version(EV_CURRENT) == EV_NONE) return ""; int fd = open("/proc/self/exe", O_RDONLY, 0); if (fd < 0) return ""; Elf* e = elf_begin(fd, ELF_C_READ, nullptr); if (!e || elf_kind(e) != ELF_K_ELF || !elf_getshstrndx(e, &shstrndx)) { return ""; } scn = nullptr; while ((scn = elf_nextscn(e, scn)) != nullptr) { if (gelf_getshdr(scn, &shdr) != &shdr || !(name = elf_strptr(e, shstrndx , shdr.sh_name))) { return ""; } if (!strcmp("repo", name)) { GElf_Shdr ghdr; if (gelf_getshdr(scn, &ghdr) != &ghdr) return ""; char buf[512]; sprintf(buf, "/proc/self/exe:%lu:%lu", ghdr.sh_offset, ghdr.sh_size); sqlite3_embedded_initialize(nullptr, true); return buf; } } return ""; }
int find_symbol(char *fn, struct lsym *lsym, unsigned long baseaddr) { struct lsym *ls; int num = 0; for (ls = lsym; ls->name; ls++) num++; elf_version(EV_CURRENT); int fd = open(fn, O_RDONLY); if (fd < 0) return -1; long ret = -1; Elf *elf = elf_begin(fd, ELF_C_READ, NULL); if (elf == NULL) goto out; GElf_Ehdr header; if (!gelf_getehdr(elf, &header)) goto out_elf; Elf_Scn *section = NULL; int found = 0; while (found < num && (section = elf_nextscn(elf, section)) != 0) { GElf_Shdr shdr, *sh; sh = gelf_getshdr(section, &shdr); if (sh->sh_type == SHT_SYMTAB || sh->sh_type == SHT_DYNSYM) { Elf_Data *data = elf_getdata(section, NULL); GElf_Sym *sym, symbol; int j; unsigned numsym = sh->sh_size / sh->sh_entsize; for (j = 0; j < numsym; j++) { sym = gelf_getsymshndx(data, NULL, j, &symbol, NULL); char *symname = elf_strptr(elf, shdr.sh_link, sym->st_name); for (ls = lsym; ls->name; ls++) { if (!strcmp(symname, ls->name)) { Elf_Scn *oscn = elf_getscn(elf, sym->st_shndx); GElf_Shdr oshdr, *osh; osh = gelf_getshdr(oscn, &oshdr); ls->addr = (sym->st_value - osh->sh_addr) + osh->sh_offset + baseaddr; found++; if (found == num) break; } } } } } if (found == num) ret = 0; out_elf: elf_end(elf); out: close(fd); return ret; }
scn_hdr_t* get_scn_hdr(int idx) { Elf_Scn* scn = g.scns[idx]; GElf_Shdr shdr = g.shdrs[idx]; Elf_Data* data; size_t n; // if ( gelf_getshdr(scn , &shdr) != &shdr ) { // errx ( EXIT_FAILURE, // " getshdr( shstrndx ) failed : %s.", // elf_errmsg ( -1)); // return NULL; // } // data = NULL; size_t buf_sz = 0; // while ((data=elf_getdata(scn, data)) != NULL) { // printf("+%d", data->d_size); // buf_sz += data->d_size; // } uint8_t* buf = (uint8_t*) malloc(shdr.sh_size); if (buf == NULL) { errx (EXIT_FAILURE, "malloc failed"); return NULL; } data = NULL; n = 0; while (n < shdr.sh_size && (data = elf_getdata(scn, data)) != NULL) { uint8_t* p = (uint8_t*)data->d_buf; size_t dsize = data->d_size; if(p != NULL) memcpy(buf+n, p, dsize); else //FIXME: seems some sections are empty in file memset(buf+n, 0, dsize); n += dsize; } // get the name string char* name; if ((name = elf_strptr(g.e, g.shstrndx, shdr.sh_name)) == NULL) errx(EXIT_FAILURE , " elf_strptr() failed : %s.", elf_errmsg (-1)); scn_hdr_t* scn_hdr_p = (scn_hdr_t*)malloc(sizeof(scn_hdr_t)); if (scn_hdr_p == NULL) { errx (EXIT_FAILURE , "scn_hdr_t malloc failed"); return NULL; } scn_hdr_p->sh_idx = idx; scn_hdr_p->name = name; scn_hdr_p->sh_addr = shdr.sh_addr; scn_hdr_p->sh_size = n; scn_hdr_p->data = buf; return scn_hdr_p; }
int find_injected_secaddr(elf_data_t *elf, inject_data_t *inject) { Elf_Scn *scn; GElf_Shdr shdr; uint64_t max_inject_addr = 0; char* s; size_t shstrndx; if(elf_getshdrstrndx(elf->e, &shstrndx) < 0) { return -1; } scn = NULL; while((scn = elf_nextscn(elf->e, scn))) { if(!gelf_getshdr(scn, &shdr)) { return -1; } s = elf_strptr(elf->e, shstrndx, shdr.sh_name); if(!s) { return -1; } if(!strcmp(s, DYNINST_NAME)) fprintf(finfo,"dyninst end=%lx\n",shdr.sh_addr + shdr.sh_size); if (shdr.sh_addr + shdr.sh_offset > max_inject_addr) max_inject_addr = shdr.sh_addr + shdr.sh_size; } inject->secaddr = max_inject_addr + inject->len + 0x4000; return 0; }
/** * @brief Search a single, particular ELF symbol table for a named symbol * * @param elf The open ELF object * @param symtab The ELF symbol table section * @param name The symbol name to find * * @returns The address to which the symbol points, 0 otherwise. */ static GElf_Addr elf_scn_getsymaddr_byname(Elf *elf, Elf_Scn *symtab, const char * name) { GElf_Shdr shdr; Elf_Data *data; uint32_t syms, i; GElf_Sym sym; if (gelf_getshdr(symtab, &shdr) == NULL || shdr.sh_type != SHT_SYMTAB) { return 0; } data = elf_getdata(symtab, NULL); if (data == NULL) { return 0; } syms = shdr.sh_size / shdr.sh_entsize; for(i=0; i<syms; i++) { gelf_getsym(data, i, &sym); if (strcmp(elf_strptr(elf, shdr.sh_link, sym.st_name), name) == 0) { return sym.st_value; } } return 0; }
static void readSymbols(Elf *e, Elf_Scn *scn, const GElf_Shdr &shdr, unsigned low, unsigned high, std::auto_ptr<CoreSymbolInfo> &SI) { Elf_Data *data = elf_getdata(scn, NULL); if (data == NULL) { return; } unsigned count = shdr.sh_size / shdr.sh_entsize; CoreSymbolInfoBuilder builder; for (unsigned i = 0; i < count; i++) { GElf_Sym sym; if (gelf_getsym(data, i, &sym) == NULL) { continue; } if (sym.st_shndx == SHN_ABS) continue; if (sym.st_value < low || sym.st_value >= high) continue; builder.addSymbol(elf_strptr(e, shdr.sh_link, sym.st_name), sym.st_value, sym.st_info); } SI = builder.getSymbolInfo(); }
/** * @brief Search an elf for a named section * * @param elf The open elf object * @param name The section name to find * * @returns A pointer to the section if successful, NULL otherwise. */ static Elf_Scn * elf_getscn_byname(Elf *elf, const char * name) { Elf_Scn *scn = NULL; size_t shstrndx; GElf_Shdr shdr; char *section_name; if (elf_getshdrstrndx(elf, &shstrndx) != 0) { fprintf(stderr, "elf_getshdrstrndx() failed: %s\n", elf_errmsg(-1)); } else { /** * Iterate over the sections in the ELF descriptor to get the indices * of the .text and .data descriptors */ while ((scn = elf_nextscn(elf, scn)) != NULL) { if (gelf_getshdr(scn, &shdr) != &shdr) { fprintf(stderr, "getshdr() failed: %s\n", elf_errmsg(-1)); } else { section_name = elf_strptr(elf, shstrndx, shdr.sh_name); if (section_name == NULL) { fprintf(stderr, "elf_strptr() failed: %s\n", elf_errmsg(-1)); } else if (strcmp(section_name, name) == 0) { break; } } } } return scn; }
static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep, GElf_Shdr *shp, const char *name, size_t *idx) { Elf_Scn *sec = NULL; size_t cnt = 1; /* Elf is corrupted/truncated, avoid calling elf_strptr. */ if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL)) return NULL; while ((sec = elf_nextscn(elf, sec)) != NULL) { char *str; gelf_getshdr(sec, shp); str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name); if (!strcmp(name, str)) { if (idx) *idx = cnt; break; } ++cnt; } return sec; }
//--------------------------------------------------------------------- Elf_Scn* getELFSectionByName(Elf* elf, char* secName) { Elf32_Ehdr* ehdr; Elf_Scn *scn; Elf32_Shdr *shdr; // Get the ELF Header ehdr = elf32_getehdr(elf); if (NULL == ehdr){ fprintf(stderr, "getELFSectionByName: Error reading ELF header\n"); return NULL; } scn = NULL; while ((scn = elf_nextscn(elf, scn)) != NULL){ if ((shdr = elf32_getshdr(scn)) != NULL){ char* CurrSecName = NULL; CurrSecName = elf_strptr(elf, ehdr->e_shstrndx, shdr->sh_name); if (strcmp(CurrSecName, secName) == 0) return scn; } } return NULL; }
//--------------------------------------------------------------------- int getELFSymbolTableNdx(Elf* elf, char* symName) { Elf_Scn *scn; Elf32_Shdr *shdr; Elf_Data *edata; Elf32_Sym *esym; scn = NULL; while ((scn = elf_nextscn(elf, scn)) != NULL){ if ((shdr = elf32_getshdr(scn)) != NULL){ if (SHT_SYMTAB == shdr->sh_type){ edata = NULL; while ((edata = elf_getdata(scn, edata)) != NULL){ if (ELF_T_SYM == edata->d_type){ int numSymbols, i; char* symNameRead; esym = (Elf32_Sym*)(edata->d_buf); numSymbols = (int)(edata->d_size/shdr->sh_entsize); for (i = 0; i < numSymbols; i++){ symNameRead = elf_strptr(elf, shdr->sh_link, esym[i].st_name); if (strcmp(symNameRead, symName) == 0){ return i; } } } } } } } fprintf(stderr, "getElfSymbolTableNdx: Could not locate symbol in the symbol table.\n"); return -1; }
int rewrite_section_name(elf_data_t *elf, inject_data_t *inject, char **err) { Elf_Scn *scn; GElf_Shdr shdr; char *s; size_t shstrndx, stroff, strbase; if(strlen(inject->secname) > strlen(ABITAG_NAME)) { (*err) = "section name too long"; return -1; } if(elf_getshdrstrndx(elf->e, &shstrndx) < 0) { (*err) = "failed to get string table section index"; return -1; } stroff = 0; strbase = 0; scn = NULL; while((scn = elf_nextscn(elf->e, scn))) { if(!gelf_getshdr(scn, &shdr)) { (*err) = "failed to get section header"; return -1; } s = elf_strptr(elf->e, shstrndx, shdr.sh_name); if(!s) { (*err) = "failed to get section name"; return -1; } if(!strcmp(s, ABITAG_NAME)) { stroff = shdr.sh_name; /* offset into shstrtab */ } else if(!strcmp(s, SHSTRTAB_NAME)) { strbase = shdr.sh_offset; /* offset to start of shstrtab */ } } if(stroff == 0) { (*err) = "cannot find shstrtab entry for injected section"; return -1; } else if(strbase == 0) { (*err) = "cannot find shstrtab"; return -1; } inject->shstroff = strbase + stroff; verbose("renaming rewritten section to \"%s\"", inject->secname); verbose("writing section string table to file"); if(write_secname(elf, inject, err) < 0) { return -1; } return 0; }
const char * drsym_obj_symbol_name(void *mod_in, uint idx) { elf_info_t *mod = (elf_info_t *) mod_in; if (mod == NULL || idx >= mod->num_syms || mod->syms == NULL) return NULL; return elf_strptr(mod->elf, mod->strtab_idx, mod->syms[idx].st_name); }
/** * Finds a section by its name in an ELF file. * @param elf * A libelf handle representing the file. * @param section_name * Name of section to be found. * @param data_dest * Save the resulting elf data pointer here. Cannot be NULL. * @param shdr_dest * Save the section header here. Cannot be NULL. * @param error_message * Will be filled by an error message if the function fails (returns * zero). Caller is responsible for calling free() on the string * pointer. If function succeeds, the pointer is not touched by the * function. * @returns * Zero on error, index of the section on success. */ static unsigned find_elf_section_by_name(Elf *elf, const char *section_name, Elf_Data **data_dest, GElf_Shdr *shdr_dest, char **error_message) { /* Find the string table index */ size_t shdr_string_index; if (0 != elf_getshdrstrndx(elf, &shdr_string_index)) { *error_message = sr_asprintf("elf_getshdrstrndx failed"); return 0; } unsigned section_index = 0; Elf_Scn *section = NULL; while ((section = elf_nextscn(elf, section)) != NULL) { /* Starting index is 1. */ ++section_index; GElf_Shdr shdr; if (gelf_getshdr(section, &shdr) != &shdr) { *error_message = sr_asprintf("gelf_getshdr failed"); return 0; } const char *current_section_name = elf_strptr(elf, shdr_string_index, shdr.sh_name); if (!current_section_name) { *error_message = sr_asprintf("elf_strptr failed"); return 0; } if (0 == strcmp(current_section_name, section_name)) { /* We found the right section! Save the data. */ *data_dest = elf_getdata(section, NULL); if (!*data_dest) { *error_message = sr_asprintf("elf_getdata failed"); return 0; } /* Save the section header. */ *shdr_dest = shdr; return section_index; } } *error_message = sr_asprintf("Section %s not found", section_name); return 0; }
static void find_section_base(const char *exe, Elf *e, const char *section) { Dwarf_Addr off; Elf_Scn *scn; GElf_Ehdr eh; GElf_Shdr sh; size_t shstrndx; int elferr; const char *name; if (gelf_getehdr(e, &eh) != &eh) { warnx("gelf_getehdr failed: %s", elf_errmsg(-1)); return; } if (!elf_getshstrndx(e, &shstrndx)) { warnx("elf_getshstrndx failed: %s", elf_errmsg(-1)); return; } (void) elf_errno(); off = 0; scn = NULL; while ((scn = elf_nextscn(e, scn)) != NULL) { if (gelf_getshdr(scn, &sh) == NULL) { warnx("gelf_getshdr failed: %s", elf_errmsg(-1)); continue; } if ((name = elf_strptr(e, shstrndx, sh.sh_name)) == NULL) goto next; if (!strcmp(section, name)) { if (eh.e_type == ET_EXEC || eh.e_type == ET_DYN) { /* * For executables, section base is the virtual * address of the specified section. */ section_base = sh.sh_addr; } else if (eh.e_type == ET_REL) { /* * For relocatables, section base is the * relative offset of the specified section * to the start of the first section. */ section_base = off; } else warnx("unknown e_type %u", eh.e_type); return; } next: off += sh.sh_size; } elferr = elf_errno(); if (elferr != 0) warnx("elf_nextscn failed: %s", elf_errmsg(elferr)); errx(EXIT_FAILURE, "%s: cannot find section %s", exe, section); }
const char *get_scn_name(vita_elf_t *ve, Elf_Scn *scn) { size_t shstrndx; GElf_Shdr shdr; elf_getshdrstrndx(ve->elf, &shstrndx); gelf_getshdr(scn, &shdr); return elf_strptr(ve->elf, shstrndx, shdr.sh_name); }
/* * @param e elf handle * @param name name of section to be found * @param filename filename for logging messages * @param dest save the resulting elf data pointer here (can be NULL) * @param shdr_dest save the section header here (can be NULL) * @returns zero on error, index of the section on success */ static unsigned xelf_section_by_name(Elf *e, const char *name, const char *filename, Elf_Data **dest, GElf_Shdr *shdr_dest) { Elf_Scn *scn = NULL; GElf_Shdr shdr; unsigned section_index = 0; size_t shstrndx; /* Find the string table index */ if (elf_getshdrstrndx(e, &shstrndx) != 0) { VERB1 log_elf_error("elf_getshdrstrndx", filename); return 0; } while ((scn = elf_nextscn(e, scn)) != NULL) { section_index++; /* starting index is 1 */ if (gelf_getshdr(scn, &shdr) != &shdr) { VERB1 log_elf_error("gelf_getshdr", filename); continue; } const char *scnname = elf_strptr(e, shstrndx, shdr.sh_name); if (scnname == NULL) { VERB1 log_elf_error("elf_strptr", filename); continue; } if (strcmp(scnname, name) == 0) { /* Found, save data */ if (dest) { *dest = elf_getdata(scn, NULL); if (*dest == NULL) { VERB1 log_elf_error("elf_getdata", filename); break; } } /* save shdr */ if (shdr_dest) { *shdr_dest = shdr; } return section_index; } } VERB1 log("Section %s not found in %s\n", name, filename); return 0; }
static void check_section (Dwarf *result, GElf_Ehdr *ehdr, Elf_Scn *scn, bool inscngrp) { GElf_Shdr shdr_mem; GElf_Shdr *shdr; /* Get the section header data. */ shdr = gelf_getshdr (scn, &shdr_mem); if (shdr == NULL) /* This should never happen. If it does something is wrong in the libelf library. */ abort (); /* Make sure the section is part of a section group only iff we really need it. If we are looking for the global (= non-section group debug info) we have to ignore all the info in section groups. If we are looking into a section group we cannot look at a section which isn't part of the section group. */ if (! inscngrp && (shdr->sh_flags & SHF_GROUP) != 0) /* Ignore the section. */ return; /* We recognize the DWARF section by their names. This is not very safe and stable but the best we can do. */ const char *scnname = elf_strptr (result->elf, ehdr->e_shstrndx, shdr->sh_name); if (scnname == NULL) { /* The section name must be valid. Otherwise is the ELF file invalid. */ __libdw_seterrno (DWARF_E_INVALID_ELF); free (result); return; } /* Recognize the various sections. Most names start with .debug_. */ size_t cnt; for (cnt = 0; cnt < ndwarf_scnnames; ++cnt) if (strcmp (scnname, dwarf_scnnames[cnt]) == 0) { /* Found it. Remember where the data is. */ if (unlikely (result->sectiondata[cnt] != NULL)) /* A section appears twice. That's bad. We ignore the section. */ break; /* Get the section data. */ Elf_Data *data = elf_getdata (scn, NULL); if (data != NULL && data->d_size != 0) /* Yep, there is actually data available. */ result->sectiondata[cnt] = data; break; } }
uint8_t *dump_section_data(Elf *elf_object, int *size) { uint8_t *buffer = NULL; Elf_Data *data = NULL; size_t shdr_num; size_t max_saddr = 0; GElf_Shdr shdr; size_t shstrndx; char *name = NULL; *size = 0; int ret = elf_getshdrnum(elf_object, &shdr_num); if (ret) { printf("Problem during ELF parsing\n"); return NULL; } if (shdr_num == 0) return NULL; Elf_Scn *cur_section = NULL; ret = elf_getshdrstrndx(elf_object, &shstrndx); if (ret) printf("No string table found\n"); while ((cur_section = elf_nextscn(elf_object, cur_section)) != NULL ) { if (gelf_getshdr(cur_section, &shdr) != &shdr) { printf("Problem during ELF parsing\n"); return NULL; } if ((shdr.sh_type == SHT_PROGBITS) && (shdr.sh_flags & SHF_ALLOC) && shdr.sh_size != 0) { name = elf_strptr(elf_object, shstrndx , shdr.sh_name); printf("Loading section %s, size 0x%08X lma 0x%08X\n", name ? name : "??", (unsigned int)shdr.sh_size, (unsigned int)shdr.sh_addr); if (shdr.sh_addr + shdr.sh_size >= max_saddr) { max_saddr = shdr.sh_addr + shdr.sh_size; buffer = realloc(buffer, max_saddr); } data = elf_getdata(cur_section, data); if (data != NULL) memcpy(buffer + shdr.sh_addr, data->d_buf, data->d_size); else { printf("Couldn't load section data chunk\n"); return NULL; } } } *size = max_saddr; return buffer; }
static const char * elf_section_name (Elf *elf, GElf_Word shndx) { GElf_Ehdr ehdr; GElf_Shdr shdr; Elf_Scn *scn = elf_getscn (elf, shndx); gelf_getshdr (scn, &shdr); gelf_getehdr (elf, &ehdr); return elf_strptr (elf, ehdr.e_shstrndx, shdr.sh_name); }
void radeon_elf_read(const char *elf_data, unsigned elf_size, struct radeon_shader_binary *binary, unsigned debug) { char *elf_buffer; Elf *elf; Elf_Scn *section = NULL; size_t section_str_index; /* One of the libelf implementations * (http://www.mr511.de/software/english.htm) requires calling * elf_version() before elf_memory(). */ elf_version(EV_CURRENT); elf_buffer = MALLOC(elf_size); memcpy(elf_buffer, elf_data, elf_size); elf = elf_memory(elf_buffer, elf_size); elf_getshdrstrndx(elf, §ion_str_index); binary->disassembled = 0; while ((section = elf_nextscn(elf, section))) { const char *name; Elf_Data *section_data = NULL; GElf_Shdr section_header; if (gelf_getshdr(section, §ion_header) != §ion_header) { fprintf(stderr, "Failed to read ELF section header\n"); return; } name = elf_strptr(elf, section_str_index, section_header.sh_name); if (!strcmp(name, ".text")) { section_data = elf_getdata(section, section_data); binary->code_size = section_data->d_size; binary->code = MALLOC(binary->code_size * sizeof(unsigned char)); memcpy(binary->code, section_data->d_buf, binary->code_size); } else if (!strcmp(name, ".AMDGPU.config")) { section_data = elf_getdata(section, section_data); binary->config_size = section_data->d_size; binary->config = MALLOC(binary->config_size * sizeof(unsigned char)); memcpy(binary->config, section_data->d_buf, binary->config_size); } else if (debug && !strcmp(name, ".AMDGPU.disasm")) { binary->disassembled = 1; section_data = elf_getdata(section, section_data); fprintf(stderr, "\nShader Disassembly:\n\n"); fprintf(stderr, "%.*s\n", (int)section_data->d_size, (char *)section_data->d_buf); } } if (elf){ elf_end(elf); } FREE(elf_buffer); }
static void check_strings (Elf *elf, int ndx, const char *msg) { check_orig_strings (elf, ndx, msg); const char *str = elf_strptr (elf, ndx, str1_off); printf ("\t'%s'\n", str); if (str == NULL || strcmp (str1, str) != 0) exit (1); str = elf_strptr (elf, ndx, str2_off); printf ("\t'%s'\n", str); if (str == NULL || strcmp (str2, str) != 0) exit (1); str = elf_strptr (elf, ndx, str3_off); printf ("\t'%s'\n", str); if (str == NULL || strcmp (str3, str) != 0) exit (1); }
//get elf main function entry address uint32_t ELF_main_entry(){ uint32_t result = 0; uint32_t sym_num; Elf32_Sym * sym; int strtab_index = 0; scn = NULL; data = NULL; while((scn = elf_nextscn(e, scn)) != NULL){ if(gelf_getshdr(scn, &shdr) != &shdr){ fprintf(stderr, "getshdr() failed.\n"); exit(1); } strtab_index ++; char* name = elf_strptr(e, ehdr.e_shstrndx, shdr.sh_name); if(strcmp(name, ".strtab")==0) break; } scn = NULL; while((scn = elf_nextscn(e, scn)) != NULL){ if(gelf_getshdr(scn, &shdr) != &shdr){ fprintf(stderr, "getshdr() failed.\n"); exit(1); } char* name = elf_strptr(e, ehdr.e_shstrndx, shdr.sh_name); if(strcmp(name, ".symtab")==0){ data = elf_getdata(scn, data); sym_num = data->d_size/sizeof(Elf32_Sym); sym = (Elf32_Sym*)data->d_buf; int i; for(i=0; i<sym_num; i++){ name = elf_strptr(e, strtab_index, sym[i].st_name); if(strcmp(name, "main") == 0){ result = sym[i].st_value; return result; } } } } return result; }
static void processVerNeed(Elf_Scn *scn, GElf_Shdr *shdr, elfInfo *ei) { Elf_Data *data = NULL; char *soname = NULL; while ((data = elf_getdata(scn, data)) != NULL) { unsigned int offset = 0, auxoffset; for (int i = shdr->sh_info; --i >= 0; ) { const char *s = NULL; GElf_Verneed need_mem, *need; need = gelf_getverneed (data, offset, &need_mem); if (need == NULL) break; s = elf_strptr(ei->elf, shdr->sh_link, need->vn_file); if (s == NULL) break; rfree(soname); soname = rstrdup(s); auxoffset = offset + need->vn_aux; for (int j = need->vn_cnt; --j >= 0; ) { GElf_Vernaux aux_mem, * aux; aux = gelf_getvernaux (data, auxoffset, &aux_mem); if (aux == NULL) break; s = elf_strptr(ei->elf, shdr->sh_link, aux->vna_name); if (s == NULL) break; if (ei->isExec && soname && !soname_only && !skipPrivate(s)) { addDep(&ei->requires, soname, s, ei->marker); } auxoffset += aux->vna_next; } offset += need->vn_next; } } rfree(soname); }
static void processDynamic(Elf_Scn *scn, GElf_Shdr *shdr, elfInfo *ei) { Elf_Data *data = NULL; while ((data = elf_getdata(scn, data)) != NULL) { for (int i = 0; i < (shdr->sh_size / shdr->sh_entsize); i++) { const char *s = NULL; GElf_Dyn dyn_mem, *dyn; dyn = gelf_getdyn (data, i, &dyn_mem); if (dyn == NULL) break; switch (dyn->d_tag) { case DT_HASH: ei->gotHASH = 1; break; case DT_GNU_HASH: ei->gotGNUHASH = 1; break; case DT_DEBUG: ei->gotDEBUG = 1; break; case DT_SONAME: s = elf_strptr(ei->elf, shdr->sh_link, dyn->d_un.d_val); if (s) ei->soname = rstrdup(s); break; case DT_NEEDED: if (ei->isExec) { s = elf_strptr(ei->elf, shdr->sh_link, dyn->d_un.d_val); if (s) addDep(&ei->requires, s, NULL, ei->marker); } break; } } } }
static GElf_Sym FindSymbol(Elf *e, const char *name) { auto sym_scn = FindSectionByType(e, SHT_SYMTAB); auto sym_data = elf_getdata(sym_scn.first, nullptr); auto num_syms = sym_scn.second.sh_size / sym_scn.second.sh_entsize; for (size_t i = 0; i < num_syms; i++) { GElf_Sym sym; gelf_getsym(sym_data, i, &sym); auto sym_name = elf_strptr(e, sym_scn.second.sh_link, sym.st_name); if (strcmp(sym_name, name) == 0) return sym; } errx(EX_SOFTWARE, "Binary does not contain symbol:%s", name); return GElf_Sym(); }