5 #include <libelfu/libelfu.h>
8 static int appendData(ElfuScn *ms, void *buf, size_t len)
13 assert(ms->shdr.sh_type != SHT_NOBITS);
14 assert(ms->data.d_buf);
16 newbuf = realloc(ms->data.d_buf, ms->shdr.sh_size + len);
18 ELFU_WARN("appendData: malloc() failed for newbuf.\n");
22 ms->data.d_buf = newbuf;
23 memcpy(newbuf + ms->shdr.sh_size, buf, len);
24 ms->shdr.sh_size += len;
25 ms->data.d_size += len;
26 assert(ms->shdr.sh_size == ms->data.d_size);
32 static ElfuScn* insertSection(ElfuElf *me, ElfuElf *mrel, ElfuScn *oldscn)
34 ElfuScn *newscn = NULL;
39 if (oldscn->shdr.sh_flags & SHF_ALLOC) {
40 newscn = elfu_mCloneScn(oldscn);
45 if (newscn->shdr.sh_type == SHT_NOBITS) {
46 /* Expand this to SHT_PROGBITS, then insert as such. */
48 assert(!newscn->data.d_buf);
50 newscn->data.d_buf = malloc(newscn->shdr.sh_size);
51 if (!newscn->data.d_buf) {
54 newscn->data.d_size = newscn->shdr.sh_size;
55 newscn->shdr.sh_type = SHT_PROGBITS;
58 injAddr = elfu_mLayoutGetSpaceInPhdr(me,
60 newscn->shdr.sh_addralign,
61 newscn->shdr.sh_flags & SHF_WRITE,
62 newscn->shdr.sh_flags & SHF_EXECINSTR,
66 ELFU_WARN("insertSection: Could not find a place to insert section.\n");
70 ELFU_INFO("Inserting %s at address 0x%jx...\n",
71 elfu_mScnName(mrel, oldscn),
74 injOffset = injAddr - injPhdr->phdr.p_vaddr + injPhdr->phdr.p_offset;
76 newscn->shdr.sh_addr = injAddr;
77 newscn->shdr.sh_offset = injOffset;
79 if (CIRCLEQ_EMPTY(&injPhdr->childScnList)
80 || CIRCLEQ_LAST(&injPhdr->childScnList)->shdr.sh_offset < injOffset) {
81 CIRCLEQ_INSERT_TAIL(&injPhdr->childScnList, newscn, elemChildScn);
84 CIRCLEQ_FOREACH(ms, &injPhdr->childScnList, elemChildScn) {
85 if (injOffset < ms->shdr.sh_offset) {
86 CIRCLEQ_INSERT_BEFORE(&injPhdr->childScnList, ms, newscn, elemChildScn);
98 newnamelen = strlen("reladd") + 1;
99 if (elfu_mScnName(mrel, oldscn)) {
100 newnamelen += strlen(elfu_mScnName(mrel, oldscn));
103 newname = malloc(newnamelen);
104 strcpy(newname, "reladd");
105 strcat(newname, elfu_mScnName(mrel, oldscn));
108 ELFU_WARN("insertSection: malloc() failed for newname. Leaving section name empty.\n");
109 newscn->shdr.sh_name = 0;
111 size_t offset = me->shstrtab->shdr.sh_size;
113 if (!appendData(me->shstrtab, newname, newnamelen)) {
114 newscn->shdr.sh_name = offset;
123 ELFU_WARN("insertSection: Skipping non-memory section %s (type %d flags %jd).\n",
124 elfu_mScnName(mrel, oldscn),
125 oldscn->shdr.sh_type,
126 oldscn->shdr.sh_flags);
132 // TODO: Destroy newscn
138 static void* subScnAdd1(ElfuElf *mrel, ElfuScn *ms, void *aux1, void *aux2)
141 ElfuElf *me = (ElfuElf*)aux1;
145 switch(ms->shdr.sh_type) {
146 case SHT_PROGBITS: /* 1 */
147 case SHT_NOBITS: /* 8 */
148 /* Ignore empty sections */
149 if (ms->shdr.sh_size == 0) {
153 /* Find a place where it belongs and shove it in. */
154 newscn = insertSection(me, mrel, ms);
156 ELFU_WARN("mReladd: Could not insert section %s (type %d), skipping.\n",
157 elfu_mScnName(mrel, ms),
167 static void* subScnAdd2(ElfuElf *mrel, ElfuScn *ms, void *aux1, void *aux2)
170 ElfuElf *me = (ElfuElf*)aux1;
173 switch(ms->shdr.sh_type) {
174 case SHT_NULL: /* 0 */
175 case SHT_PROGBITS: /* 1 */
176 case SHT_STRTAB: /* 3 */
177 case SHT_NOBITS: /* 8 */
181 case SHT_REL: /* 9 */
183 elfu_mRelocate(me, elfu_mScnByOldscn(me, ms->infoptr), ms);
186 case SHT_RELA: /* 4 */
187 // TODO: Needs a parser
188 //elfu_mRelocate(elfu_mScnByOldscn(me, ms->infoptr), ms);
190 case SHT_SYMTAB: /* 2 */
191 /* Merge with the existing table. Take care of string tables also. */
193 /* The next section types either do not occur in .o files, or are
194 * not strictly necessary to process here. */
195 case SHT_NOTE: /* 7 */
196 case SHT_HASH: /* 5 */
197 case SHT_DYNAMIC: /* 6 */
198 case SHT_SHLIB: /* 10 */
199 case SHT_DYNSYM: /* 11 */
200 case SHT_INIT_ARRAY: /* 14 */
201 case SHT_FINI_ARRAY: /* 15 */
202 case SHT_PREINIT_ARRAY: /* 16 */
203 case SHT_GROUP: /* 17 */
204 case SHT_SYMTAB_SHNDX: /* 18 */
205 case SHT_NUM: /* 19 */
207 ELFU_WARN("mReladd: Skipping section %s (type %d).\n",
208 elfu_mScnName(mrel, ms),
217 static void insertSymClone(ElfuElf *me, const ElfuScn *oldmsst, const ElfuSym *oldsym)
221 ElfuScn *newscn = NULL;
229 /* If the old symbol pointed to a section, find its clone in the target */
230 if (oldsym->scnptr) {
231 newscn = elfu_mScnByOldscn(me, oldsym->scnptr);
233 /* If we didn't copy the section referenced, we won't
234 * copy this symbol either */
240 // TODO: Allocate symtab if none present
243 /* Allocate memory for the cloned symbol */
244 newsym = malloc(sizeof(*newsym));
246 ELFU_WARN("insertSymClone: malloc() failed for newsym.\n");
250 oldsymname = ELFU_SYMSTR(oldmsst, oldsym->name);
252 /* Expand .strtab, append symbol name, link newsym to it */
253 newsize = me->symtab->linkptr->shdr.sh_size + strlen(oldsymname) + 1;
254 newbuf = realloc(me->symtab->linkptr->data.d_buf, newsize);
256 ELFU_WARN("insertSymClone: realloc() failed for strtab.\n");
260 me->symtab->linkptr->data.d_buf = newbuf;
262 newsym->name = me->symtab->linkptr->shdr.sh_size;
264 strcpy(newbuf + newsym->name, oldsymname);
266 me->symtab->linkptr->data.d_size = newsize;
267 me->symtab->linkptr->shdr.sh_size = newsize;
270 /* Copy all other fields */
271 newsym->scnptr = newscn;
272 newsym->shndx = oldsym->shndx; /* If scnptr == NULL, this becomes relevant */
273 newsym->bind = oldsym->bind;
274 newsym->other = oldsym->other;
275 newsym->size = oldsym->size;
276 newsym->type = oldsym->type;
277 newsym->value = oldsym->value;
279 /* In executables, symbol addresses need to be in memory */
281 newsym->value += newscn->shdr.sh_addr;
285 CIRCLEQ_INSERT_TAIL(&me->symtab->symtab.syms, newsym, elem);
295 static void mergeSymtab(ElfuElf *me, const ElfuElf *mrel)
302 CIRCLEQ_FOREACH(sym, &mrel->symtab->symtab.syms, elem) {
303 insertSymClone(me, mrel->symtab, sym);
309 void elfu_mReladd(ElfuElf *me, const ElfuElf *mrel)
314 /* For each section in object file, guess how to insert it */
315 elfu_mScnForall((ElfuElf*)mrel, subScnAdd1, me, NULL);
317 mergeSymtab(me, mrel);
319 /* Do relocations and other stuff */
320 elfu_mScnForall((ElfuElf*)mrel, subScnAdd2, me, NULL);
322 /* Re-layout to accommodate new contents */
323 elfu_mLayoutAuto(me);