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 ELFU_WARN("insertSection: Skipping non-memory section %s (type %d flags %u).\n",
71 elfu_mScnName(mrel, oldscn),
73 (unsigned)oldscn->shdr.sh_flags);
76 newscn = cloneScn(oldscn);
81 if (newscn->shdr.sh_type == SHT_NOBITS) {
82 /* Expand this to SHT_PROGBITS, then insert as such. */
84 assert(!newscn->databuf);
86 newscn->databuf = malloc(newscn->shdr.sh_size);
87 if (!newscn->databuf) {
90 newscn->shdr.sh_type = SHT_PROGBITS;
93 injAddr = elfu_mLayoutGetSpaceInPhdr(me,
95 newscn->shdr.sh_addralign,
96 newscn->shdr.sh_flags & SHF_WRITE,
97 newscn->shdr.sh_flags & SHF_EXECINSTR,
101 ELFU_WARN("insertSection: Could not find a place to insert section.\n");
105 ELFU_INFO("Inserting %s at address 0x%x...\n",
106 elfu_mScnName(mrel, oldscn),
109 injOffset = injAddr - injPhdr->phdr.p_vaddr + injPhdr->phdr.p_offset;
111 newscn->shdr.sh_addr = injAddr;
112 newscn->shdr.sh_offset = injOffset;
114 /* Insert section in child list, ordered by memory address */
115 if (CIRCLEQ_EMPTY(&injPhdr->childScnList)
116 || CIRCLEQ_LAST(&injPhdr->childScnList)->shdr.sh_addr < injAddr) {
117 CIRCLEQ_INSERT_TAIL(&injPhdr->childScnList, newscn, elemChildScn);
120 CIRCLEQ_FOREACH(ms, &injPhdr->childScnList, elemChildScn) {
121 if (injAddr < ms->shdr.sh_addr) {
122 CIRCLEQ_INSERT_BEFORE(&injPhdr->childScnList, ms, newscn, elemChildScn);
134 newnamelen = strlen("reladd") + 1;
135 if (elfu_mScnName(mrel, oldscn)) {
136 newnamelen += strlen(elfu_mScnName(mrel, oldscn));
139 newname = malloc(newnamelen);
140 strcpy(newname, "reladd");
141 strcat(newname, elfu_mScnName(mrel, oldscn));
144 ELFU_WARN("insertSection: malloc() failed for newname. Leaving section name empty.\n");
145 newscn->shdr.sh_name = 0;
147 size_t offset = me->shstrtab->shdr.sh_size;
149 if (!appendData(me->shstrtab, newname, newnamelen)) {
150 newscn->shdr.sh_name = offset;
162 // TODO: Destroy newscn
168 static void* subScnAdd1(ElfuElf *mrel, ElfuScn *ms, void *aux1, void *aux2)
171 ElfuElf *me = (ElfuElf*)aux1;
175 switch(ms->shdr.sh_type) {
176 case SHT_PROGBITS: /* 1 */
177 case SHT_NOBITS: /* 8 */
178 /* Ignore empty sections */
179 if (ms->shdr.sh_size == 0) {
183 /* Find a place where it belongs and shove it in. */
184 newscn = insertSection(me, mrel, ms);
186 ELFU_WARN("mReladd: Could not insert section %s (type %d), skipping.\n",
187 elfu_mScnName(mrel, ms),
197 static void* subScnAdd2(ElfuElf *mrel, ElfuScn *ms, void *aux1, void *aux2)
199 ElfuElf *me = (ElfuElf*)aux1;
202 switch(ms->shdr.sh_type) {
203 case SHT_NULL: /* 0 */
204 case SHT_PROGBITS: /* 1 */
205 case SHT_SYMTAB: /* 2 */
206 case SHT_STRTAB: /* 3 */
207 case SHT_NOBITS: /* 8 */
210 case SHT_RELA: /* 4 */
211 case SHT_REL: /* 9 */
213 if (elfu_mRelocate(me, elfu_mScnByOldscn(me, ms->infoptr), ms)) {
218 /* The next section types either do not occur in .o files, or are
219 * not strictly necessary to process here. */
220 case SHT_NOTE: /* 7 */
221 case SHT_HASH: /* 5 */
222 case SHT_DYNAMIC: /* 6 */
223 case SHT_SHLIB: /* 10 */
224 case SHT_DYNSYM: /* 11 */
225 case SHT_INIT_ARRAY: /* 14 */
226 case SHT_FINI_ARRAY: /* 15 */
227 case SHT_PREINIT_ARRAY: /* 16 */
228 case SHT_GROUP: /* 17 */
229 case SHT_SYMTAB_SHNDX: /* 18 */
230 case SHT_NUM: /* 19 */
232 ELFU_WARN("mReladd: Skipping section %s (type %d).\n",
233 elfu_mScnName(mrel, ms),
242 static void insertSymClone(ElfuElf *me, const ElfuScn *oldmsst, const ElfuSym *oldsym)
246 ElfuScn *newscn = NULL;
254 /* If the old symbol pointed to a section, find its clone in the target */
255 if (oldsym->scnptr) {
256 newscn = elfu_mScnByOldscn(me, oldsym->scnptr);
258 /* If we didn't copy the section referenced, we won't
259 * copy this symbol either */
265 // TODO: Allocate symtab if none present
268 /* Allocate memory for the cloned symbol */
269 newsym = malloc(sizeof(*newsym));
271 ELFU_WARN("insertSymClone: malloc() failed for newsym.\n");
275 oldsymname = ELFU_SYMSTR(oldmsst, oldsym->name);
277 /* Expand .strtab, append symbol name, link newsym to it */
278 newsize = me->symtab->linkptr->shdr.sh_size + strlen(oldsymname) + 1;
279 newbuf = realloc(me->symtab->linkptr->databuf, newsize);
281 ELFU_WARN("insertSymClone: realloc() failed for strtab.\n");
285 me->symtab->linkptr->databuf = newbuf;
287 newsym->name = me->symtab->linkptr->shdr.sh_size;
289 strcpy(newbuf + newsym->name, oldsymname);
291 me->symtab->linkptr->shdr.sh_size = newsize;
294 /* Copy all other fields */
295 newsym->scnptr = newscn;
296 newsym->shndx = oldsym->shndx; /* If scnptr == NULL, this becomes relevant */
297 newsym->bind = oldsym->bind;
298 newsym->other = oldsym->other;
299 newsym->size = oldsym->size;
300 newsym->type = oldsym->type;
301 newsym->value = oldsym->value;
303 /* In executables, symbol addresses need to be in memory */
305 newsym->value += newscn->shdr.sh_addr;
309 CIRCLEQ_INSERT_TAIL(&me->symtab->symtab.syms, newsym, elem);
319 static void mergeSymtab(ElfuElf *me, const ElfuElf *mrel)
326 CIRCLEQ_FOREACH(sym, &mrel->symtab->symtab.syms, elem) {
327 insertSymClone(me, mrel->symtab, sym);
333 int elfu_mReladd(ElfuElf *me, const ElfuElf *mrel)
338 /* For each section in object file, guess how to insert it */
339 elfu_mScnForall((ElfuElf*)mrel, subScnAdd1, me, NULL);
341 mergeSymtab(me, mrel);
343 /* Do relocations and other stuff */
344 if (elfu_mScnForall((ElfuElf*)mrel, subScnAdd2, me, NULL)) {
345 ELFU_WARN("elfu_mReladd: Reladd aborted. Target model is unclean.\n");
349 /* Re-layout to accommodate new contents */
350 elfu_mLayoutAuto(me);