5 #include <libelfu/libelfu.h>
8 static int appendData(ElfuScn *ms, void *buf, size_t len)
13 assert(ms->shdr.sh_type != SHT_NOBITS);
16 newbuf = realloc(ms->databuf, ms->shdr.sh_size + len);
18 ELFU_WARN("appendData: malloc() failed for newbuf.\n");
23 memcpy(newbuf + ms->shdr.sh_size, buf, len);
24 ms->shdr.sh_size += len;
25 assert(ms->shdr.sh_size == ms->shdr.sh_size);
31 static ElfuScn* insertSection(ElfuElf *me, ElfuElf *mrel, ElfuScn *oldscn)
33 ElfuScn *newscn = NULL;
38 if (oldscn->shdr.sh_flags & SHF_ALLOC) {
39 newscn = elfu_mCloneScn(oldscn);
44 if (newscn->shdr.sh_type == SHT_NOBITS) {
45 /* Expand this to SHT_PROGBITS, then insert as such. */
47 assert(!newscn->databuf);
49 newscn->databuf = malloc(newscn->shdr.sh_size);
50 if (!newscn->databuf) {
53 newscn->shdr.sh_type = SHT_PROGBITS;
56 injAddr = elfu_mLayoutGetSpaceInPhdr(me,
58 newscn->shdr.sh_addralign,
59 newscn->shdr.sh_flags & SHF_WRITE,
60 newscn->shdr.sh_flags & SHF_EXECINSTR,
64 ELFU_WARN("insertSection: Could not find a place to insert section.\n");
68 ELFU_INFO("Inserting %s at address 0x%x...\n",
69 elfu_mScnName(mrel, oldscn),
72 injOffset = injAddr - injPhdr->phdr.p_vaddr + injPhdr->phdr.p_offset;
74 newscn->shdr.sh_addr = injAddr;
75 newscn->shdr.sh_offset = injOffset;
77 if (CIRCLEQ_EMPTY(&injPhdr->childScnList)
78 || CIRCLEQ_LAST(&injPhdr->childScnList)->shdr.sh_offset < injOffset) {
79 CIRCLEQ_INSERT_TAIL(&injPhdr->childScnList, newscn, elemChildScn);
82 CIRCLEQ_FOREACH(ms, &injPhdr->childScnList, elemChildScn) {
83 if (injOffset < ms->shdr.sh_offset) {
84 CIRCLEQ_INSERT_BEFORE(&injPhdr->childScnList, ms, newscn, elemChildScn);
96 newnamelen = strlen("reladd") + 1;
97 if (elfu_mScnName(mrel, oldscn)) {
98 newnamelen += strlen(elfu_mScnName(mrel, oldscn));
101 newname = malloc(newnamelen);
102 strcpy(newname, "reladd");
103 strcat(newname, elfu_mScnName(mrel, oldscn));
106 ELFU_WARN("insertSection: malloc() failed for newname. Leaving section name empty.\n");
107 newscn->shdr.sh_name = 0;
109 size_t offset = me->shstrtab->shdr.sh_size;
111 if (!appendData(me->shstrtab, newname, newnamelen)) {
112 newscn->shdr.sh_name = offset;
121 ELFU_WARN("insertSection: Skipping non-memory section %s (type %d flags %u).\n",
122 elfu_mScnName(mrel, oldscn),
123 oldscn->shdr.sh_type,
124 (unsigned)oldscn->shdr.sh_flags);
130 // TODO: Destroy newscn
136 static void* subScnAdd1(ElfuElf *mrel, ElfuScn *ms, void *aux1, void *aux2)
139 ElfuElf *me = (ElfuElf*)aux1;
143 switch(ms->shdr.sh_type) {
144 case SHT_PROGBITS: /* 1 */
145 case SHT_NOBITS: /* 8 */
146 /* Ignore empty sections */
147 if (ms->shdr.sh_size == 0) {
151 /* Find a place where it belongs and shove it in. */
152 newscn = insertSection(me, mrel, ms);
154 ELFU_WARN("mReladd: Could not insert section %s (type %d), skipping.\n",
155 elfu_mScnName(mrel, ms),
165 static void* subScnAdd2(ElfuElf *mrel, ElfuScn *ms, void *aux1, void *aux2)
167 ElfuElf *me = (ElfuElf*)aux1;
170 switch(ms->shdr.sh_type) {
171 case SHT_NULL: /* 0 */
172 case SHT_PROGBITS: /* 1 */
173 case SHT_STRTAB: /* 3 */
174 case SHT_NOBITS: /* 8 */
178 case SHT_REL: /* 9 */
180 elfu_mRelocate(me, elfu_mScnByOldscn(me, ms->infoptr), ms);
183 case SHT_RELA: /* 4 */
184 // TODO: Needs a parser
185 //elfu_mRelocate(elfu_mScnByOldscn(me, ms->infoptr), ms);
187 case SHT_SYMTAB: /* 2 */
188 /* Merge with the existing table. Take care of string tables also. */
190 /* The next section types either do not occur in .o files, or are
191 * not strictly necessary to process here. */
192 case SHT_NOTE: /* 7 */
193 case SHT_HASH: /* 5 */
194 case SHT_DYNAMIC: /* 6 */
195 case SHT_SHLIB: /* 10 */
196 case SHT_DYNSYM: /* 11 */
197 case SHT_INIT_ARRAY: /* 14 */
198 case SHT_FINI_ARRAY: /* 15 */
199 case SHT_PREINIT_ARRAY: /* 16 */
200 case SHT_GROUP: /* 17 */
201 case SHT_SYMTAB_SHNDX: /* 18 */
202 case SHT_NUM: /* 19 */
204 ELFU_WARN("mReladd: Skipping section %s (type %d).\n",
205 elfu_mScnName(mrel, ms),
214 static void insertSymClone(ElfuElf *me, const ElfuScn *oldmsst, const ElfuSym *oldsym)
218 ElfuScn *newscn = NULL;
226 /* If the old symbol pointed to a section, find its clone in the target */
227 if (oldsym->scnptr) {
228 newscn = elfu_mScnByOldscn(me, oldsym->scnptr);
230 /* If we didn't copy the section referenced, we won't
231 * copy this symbol either */
237 // TODO: Allocate symtab if none present
240 /* Allocate memory for the cloned symbol */
241 newsym = malloc(sizeof(*newsym));
243 ELFU_WARN("insertSymClone: malloc() failed for newsym.\n");
247 oldsymname = ELFU_SYMSTR(oldmsst, oldsym->name);
249 /* Expand .strtab, append symbol name, link newsym to it */
250 newsize = me->symtab->linkptr->shdr.sh_size + strlen(oldsymname) + 1;
251 newbuf = realloc(me->symtab->linkptr->databuf, newsize);
253 ELFU_WARN("insertSymClone: realloc() failed for strtab.\n");
257 me->symtab->linkptr->databuf = newbuf;
259 newsym->name = me->symtab->linkptr->shdr.sh_size;
261 strcpy(newbuf + newsym->name, oldsymname);
263 me->symtab->linkptr->shdr.sh_size = newsize;
266 /* Copy all other fields */
267 newsym->scnptr = newscn;
268 newsym->shndx = oldsym->shndx; /* If scnptr == NULL, this becomes relevant */
269 newsym->bind = oldsym->bind;
270 newsym->other = oldsym->other;
271 newsym->size = oldsym->size;
272 newsym->type = oldsym->type;
273 newsym->value = oldsym->value;
275 /* In executables, symbol addresses need to be in memory */
277 newsym->value += newscn->shdr.sh_addr;
281 CIRCLEQ_INSERT_TAIL(&me->symtab->symtab.syms, newsym, elem);
291 static void mergeSymtab(ElfuElf *me, const ElfuElf *mrel)
298 CIRCLEQ_FOREACH(sym, &mrel->symtab->symtab.syms, elem) {
299 insertSymClone(me, mrel->symtab, sym);
305 void elfu_mReladd(ElfuElf *me, const ElfuElf *mrel)
310 /* For each section in object file, guess how to insert it */
311 elfu_mScnForall((ElfuElf*)mrel, subScnAdd1, me, NULL);
313 mergeSymtab(me, mrel);
315 /* Do relocations and other stuff */
316 elfu_mScnForall((ElfuElf*)mrel, subScnAdd2, me, NULL);
318 /* Re-layout to accommodate new contents */
319 elfu_mLayoutAuto(me);