4 #include <libelfu/libelfu.h>
7 static void* subFindByName(ElfuElf *me, ElfuScn *ms, void *aux1, void *aux2)
9 char *name = (char*)aux1;
12 if (elfu_mScnName(me, ms)) {
13 if (!strcmp(elfu_mScnName(me, ms), name)) {
22 /* Hazard a guess where a function may be found in the PLT */
23 static GElf_Word pltLookupVal(ElfuElf *me, char *name)
30 relplt = elfu_mScnForall(me, subFindByName, ".rel.plt", NULL);
32 ELFU_WARN("dynsymLookupVal: Could not find .rel.plt section in destination ELF.\n");
36 plt = elfu_mScnForall(me, subFindByName, ".plt", NULL);
38 ELFU_WARN("dynsymLookupVal: Could not find .plt section in destination ELF.\n");
43 /* Look up name. If the j-th entry in .rel.plt has the name we are
44 * looking for, we assume that the (j+1)-th entry in .plt is machine
45 * code to jump to the function.
46 * Your mileage may vary, but it works on my GNU binaries. */
47 assert(relplt->linkptr);
49 CIRCLEQ_FOREACH(rel, &relplt->reltab.rels, elem) {
56 /* We only consider runtime relocations for functions.
57 * Technically, these relocations write the functions' addresses
58 * to the GOT, not the PLT, after the dynamic linker has found
60 if ((me->elfclass == ELFCLASS32 && rel->type != R_386_JMP_SLOT)
61 || (me->elfclass == ELFCLASS64 && rel->type != R_X86_64_JUMP_SLOT)) {
65 /* Get the (rel->sym)-th symbol from the symbol table that
66 * .rel.plt points to. */
67 sym = CIRCLEQ_FIRST(&relplt->linkptr->symtab.syms);
68 for (i = 1; i < rel->sym; i++) {
69 sym = CIRCLEQ_NEXT(sym, elem);
72 symname = ELFU_SYMSTR(relplt->linkptr, sym->name);
73 if (!strcmp(symname, name)) {
74 /* If this is the symbol we are looking for, then in an x86 binary
75 * the jump to the dynamic symbol is probably at offset (j * 16)
76 * from the start of the PLT, where j is the PLT entry and 16 is
77 * the number of bytes the machine code in a PLT entry take. */
78 GElf_Addr addr = plt->shdr.sh_addr + (16 * j);
79 ELFU_DEBUG("dynsymLookupVal: Guessing symbol '%s' is in destination memory at %x (PLT entry #%u).\n", name, (unsigned)addr, j);
84 ELFU_WARN("dynsymLookupVal: Could not find symbol '%s' in destination ELF.\n", name);
91 /* Look up a value in the symbol table section *msst which is in *me.
92 * If it is not found there, see if we can find it in *me's PLT. */
93 GElf_Word elfu_mSymtabLookupVal(ElfuElf *me, ElfuScn *msst, GElf_Word entry)
102 assert(!CIRCLEQ_EMPTY(&msst->symtab.syms));
104 sym = CIRCLEQ_FIRST(&msst->symtab.syms);
105 for (i = 1; i < entry; i++) {
106 sym = CIRCLEQ_NEXT(sym, elem);
108 symname = ELFU_SYMSTR(msst, sym->name);
115 ElfuScn *newscn = elfu_mScnByOldscn(me, sym->scnptr);
117 return newscn->shdr.sh_addr + sym->value;
118 } else if (sym->shndx == SHN_UNDEF) {
119 /* Look the symbol up in .rel.plt. If it cannot be found there then
120 * .rel.dyn may need to be expanded with a COPY relocation so the
121 * dynamic linker fixes up the (TODO). */
122 return pltLookupVal(me, symname);
123 } else if (sym->shndx == SHN_ABS) {
126 ELFU_WARN("symtabLookupVal: Symbol binding COMMON is not supported, using 0.\n");
132 assert(elfu_mScnByOldscn(me, sym->scnptr));
133 return elfu_mScnByOldscn(me, sym->scnptr)->shdr.sh_addr;
135 ELFU_WARN("symtabLookupVal: Symbol type FILE is not supported, using 0.\n");
138 ELFU_WARN("symtabLookupVal: Unknown symbol type %d for %s.\n", sym->type, symname);
145 /* Convert symtab from memory model to elfclass specific format */
146 void elfu_mSymtabFlatten(ElfuElf *me)
151 elfu_mLayoutAuto(me);
153 /* Update section indexes and count symbols */
154 CIRCLEQ_FOREACH(sym, &me->symtab->symtab.syms, elem) {
156 sym->shndx = elfu_mScnIndex(me, sym->scnptr);
162 /* Copy symbols to elfclass-specific format */
163 if (me->elfclass == ELFCLASS32) {
164 size_t newsize = (numsyms + 1) * sizeof(Elf32_Sym);
167 if (me->symtab->data.d_buf) {
168 free(me->symtab->data.d_buf);
170 me->symtab->data.d_buf = malloc(newsize);
171 assert(me->symtab->data.d_buf);
173 me->symtab->data.d_size = newsize;
174 me->symtab->shdr.sh_size = newsize;
175 memset(me->symtab->data.d_buf, 0, newsize);
178 CIRCLEQ_FOREACH(sym, &me->symtab->symtab.syms, elem) {
179 Elf32_Sym *es = ((Elf32_Sym*)me->symtab->data.d_buf) + i;
181 es->st_name = sym->name;
182 es->st_value = sym->value;
183 es->st_size = sym->size;
184 es->st_info = ELF32_ST_INFO(sym->bind, sym->type);
185 es->st_other = sym->other;
186 es->st_shndx = sym->shndx;
190 } else if (me->elfclass == ELFCLASS64) {
191 size_t newsize = (numsyms + 1) * sizeof(Elf64_Sym);
194 if (me->symtab->data.d_buf) {
195 free(me->symtab->data.d_buf);
197 me->symtab->data.d_buf = malloc(newsize);
198 assert(me->symtab->data.d_buf);
200 me->symtab->data.d_size = newsize;
201 me->symtab->shdr.sh_size = newsize;
202 memset(me->symtab->data.d_buf, 0, newsize);
205 CIRCLEQ_FOREACH(sym, &me->symtab->symtab.syms, elem) {
206 Elf64_Sym *es = ((Elf64_Sym*)me->symtab->data.d_buf) + i;
208 es->st_name = sym->name;
209 es->st_value = sym->value;
210 es->st_size = sym->size;
211 es->st_info = ELF64_ST_INFO(sym->bind, sym->type);
212 es->st_other = sym->other;
213 es->st_shndx = sym->shndx;
218 /* Unknown elfclass */
222 elfu_mLayoutAuto(me);