5 #include <libelfu/libelfu.h>
8 static ElfuScn* cloneScn(ElfuScn *ms)
14 newscn = elfu_mScnAlloc();
19 newscn->shdr = ms->shdr;
22 void *newbuf = malloc(ms->shdr.sh_size);
24 ELFU_WARN("cloneScn: Could not allocate memory for new data buffer.\n");
29 memcpy(newbuf, ms->databuf, ms->shdr.sh_size);
30 newscn->databuf = newbuf;
39 static int appendData(ElfuScn *ms, void *buf, size_t len)
44 assert(ms->shdr.sh_type != SHT_NOBITS);
47 newbuf = realloc(ms->databuf, ms->shdr.sh_size + len);
49 ELFU_WARN("appendData: malloc() failed for newbuf.\n");
54 memcpy(newbuf + ms->shdr.sh_size, buf, len);
55 ms->shdr.sh_size += len;
56 assert(ms->shdr.sh_size == ms->shdr.sh_size);
62 static ElfuScn* insertSection(ElfuElf *me, ElfuElf *mrel, ElfuScn *oldscn)
64 ElfuScn *newscn = NULL;
69 if (oldscn->shdr.sh_flags & SHF_ALLOC) {
70 newscn = cloneScn(oldscn);
75 if (newscn->shdr.sh_type == SHT_NOBITS) {
76 /* Expand this to SHT_PROGBITS, then insert as such. */
78 assert(!newscn->databuf);
80 newscn->databuf = malloc(newscn->shdr.sh_size);
81 if (!newscn->databuf) {
84 newscn->shdr.sh_type = SHT_PROGBITS;
87 injAddr = elfu_mLayoutGetSpaceInPhdr(me,
89 newscn->shdr.sh_addralign,
90 newscn->shdr.sh_flags & SHF_WRITE,
91 newscn->shdr.sh_flags & SHF_EXECINSTR,
95 ELFU_WARN("insertSection: Could not find a place to insert section.\n");
99 ELFU_INFO("Inserting %s at address 0x%x...\n",
100 elfu_mScnName(mrel, oldscn),
103 injOffset = injAddr - injPhdr->phdr.p_vaddr + injPhdr->phdr.p_offset;
105 newscn->shdr.sh_addr = injAddr;
106 newscn->shdr.sh_offset = injOffset;
108 if (CIRCLEQ_EMPTY(&injPhdr->childScnList)
109 || CIRCLEQ_LAST(&injPhdr->childScnList)->shdr.sh_offset < injOffset) {
110 CIRCLEQ_INSERT_TAIL(&injPhdr->childScnList, newscn, elemChildScn);
113 CIRCLEQ_FOREACH(ms, &injPhdr->childScnList, elemChildScn) {
114 if (injOffset < ms->shdr.sh_offset) {
115 CIRCLEQ_INSERT_BEFORE(&injPhdr->childScnList, ms, newscn, elemChildScn);
127 newnamelen = strlen("reladd") + 1;
128 if (elfu_mScnName(mrel, oldscn)) {
129 newnamelen += strlen(elfu_mScnName(mrel, oldscn));
132 newname = malloc(newnamelen);
133 strcpy(newname, "reladd");
134 strcat(newname, elfu_mScnName(mrel, oldscn));
137 ELFU_WARN("insertSection: malloc() failed for newname. Leaving section name empty.\n");
138 newscn->shdr.sh_name = 0;
140 size_t offset = me->shstrtab->shdr.sh_size;
142 if (!appendData(me->shstrtab, newname, newnamelen)) {
143 newscn->shdr.sh_name = offset;
152 ELFU_WARN("insertSection: Skipping non-memory section %s (type %d flags %u).\n",
153 elfu_mScnName(mrel, oldscn),
154 oldscn->shdr.sh_type,
155 (unsigned)oldscn->shdr.sh_flags);
161 // TODO: Destroy newscn
167 static void* subScnAdd1(ElfuElf *mrel, ElfuScn *ms, void *aux1, void *aux2)
170 ElfuElf *me = (ElfuElf*)aux1;
174 switch(ms->shdr.sh_type) {
175 case SHT_PROGBITS: /* 1 */
176 case SHT_NOBITS: /* 8 */
177 /* Ignore empty sections */
178 if (ms->shdr.sh_size == 0) {
182 /* Find a place where it belongs and shove it in. */
183 newscn = insertSection(me, mrel, ms);
185 ELFU_WARN("mReladd: Could not insert section %s (type %d), skipping.\n",
186 elfu_mScnName(mrel, ms),
196 static void* subScnAdd2(ElfuElf *mrel, ElfuScn *ms, void *aux1, void *aux2)
198 ElfuElf *me = (ElfuElf*)aux1;
201 switch(ms->shdr.sh_type) {
202 case SHT_NULL: /* 0 */
203 case SHT_PROGBITS: /* 1 */
204 case SHT_STRTAB: /* 3 */
205 case SHT_NOBITS: /* 8 */
209 case SHT_REL: /* 9 */
211 elfu_mRelocate(me, elfu_mScnByOldscn(me, ms->infoptr), ms);
214 case SHT_RELA: /* 4 */
215 // TODO: Needs a parser
216 //elfu_mRelocate(elfu_mScnByOldscn(me, ms->infoptr), ms);
218 case SHT_SYMTAB: /* 2 */
219 /* Merge with the existing table. Take care of string tables also. */
221 /* The next section types either do not occur in .o files, or are
222 * not strictly necessary to process here. */
223 case SHT_NOTE: /* 7 */
224 case SHT_HASH: /* 5 */
225 case SHT_DYNAMIC: /* 6 */
226 case SHT_SHLIB: /* 10 */
227 case SHT_DYNSYM: /* 11 */
228 case SHT_INIT_ARRAY: /* 14 */
229 case SHT_FINI_ARRAY: /* 15 */
230 case SHT_PREINIT_ARRAY: /* 16 */
231 case SHT_GROUP: /* 17 */
232 case SHT_SYMTAB_SHNDX: /* 18 */
233 case SHT_NUM: /* 19 */
235 ELFU_WARN("mReladd: Skipping section %s (type %d).\n",
236 elfu_mScnName(mrel, ms),
245 static void insertSymClone(ElfuElf *me, const ElfuScn *oldmsst, const ElfuSym *oldsym)
249 ElfuScn *newscn = NULL;
257 /* If the old symbol pointed to a section, find its clone in the target */
258 if (oldsym->scnptr) {
259 newscn = elfu_mScnByOldscn(me, oldsym->scnptr);
261 /* If we didn't copy the section referenced, we won't
262 * copy this symbol either */
268 // TODO: Allocate symtab if none present
271 /* Allocate memory for the cloned symbol */
272 newsym = malloc(sizeof(*newsym));
274 ELFU_WARN("insertSymClone: malloc() failed for newsym.\n");
278 oldsymname = ELFU_SYMSTR(oldmsst, oldsym->name);
280 /* Expand .strtab, append symbol name, link newsym to it */
281 newsize = me->symtab->linkptr->shdr.sh_size + strlen(oldsymname) + 1;
282 newbuf = realloc(me->symtab->linkptr->databuf, newsize);
284 ELFU_WARN("insertSymClone: realloc() failed for strtab.\n");
288 me->symtab->linkptr->databuf = newbuf;
290 newsym->name = me->symtab->linkptr->shdr.sh_size;
292 strcpy(newbuf + newsym->name, oldsymname);
294 me->symtab->linkptr->shdr.sh_size = newsize;
297 /* Copy all other fields */
298 newsym->scnptr = newscn;
299 newsym->shndx = oldsym->shndx; /* If scnptr == NULL, this becomes relevant */
300 newsym->bind = oldsym->bind;
301 newsym->other = oldsym->other;
302 newsym->size = oldsym->size;
303 newsym->type = oldsym->type;
304 newsym->value = oldsym->value;
306 /* In executables, symbol addresses need to be in memory */
308 newsym->value += newscn->shdr.sh_addr;
312 CIRCLEQ_INSERT_TAIL(&me->symtab->symtab.syms, newsym, elem);
322 static void mergeSymtab(ElfuElf *me, const ElfuElf *mrel)
329 CIRCLEQ_FOREACH(sym, &mrel->symtab->symtab.syms, elem) {
330 insertSymClone(me, mrel->symtab, sym);
336 void elfu_mReladd(ElfuElf *me, const ElfuElf *mrel)
341 /* For each section in object file, guess how to insert it */
342 elfu_mScnForall((ElfuElf*)mrel, subScnAdd1, me, NULL);
344 mergeSymtab(me, mrel);
346 /* Do relocations and other stuff */
347 elfu_mScnForall((ElfuElf*)mrel, subScnAdd2, me, NULL);
349 /* Re-layout to accommodate new contents */
350 elfu_mLayoutAuto(me);