Merge cloneScn() into reladd.c
[centaur.git] / src / libelfu / modelops / reladd.c
1 #include <assert.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <sys/types.h>
5 #include <libelfu/libelfu.h>
6
7
8 static ElfuScn* cloneScn(ElfuScn *ms)
9 {
10   ElfuScn *newscn;
11
12   assert(ms);
13
14   newscn = elfu_mScnAlloc();
15   if (!newscn) {
16     return NULL;
17   }
18
19   newscn->shdr = ms->shdr;
20
21   if (ms->databuf) {
22     void *newbuf = malloc(ms->shdr.sh_size);
23     if (!newbuf) {
24       ELFU_WARN("cloneScn: Could not allocate memory for new data buffer.\n");
25       free(newscn);
26       return NULL;
27     }
28
29     memcpy(newbuf, ms->databuf, ms->shdr.sh_size);
30     newscn->databuf = newbuf;
31   }
32
33   newscn->oldptr = ms;
34
35   return newscn;
36 }
37
38
39 static int appendData(ElfuScn *ms, void *buf, size_t len)
40 {
41   char *newbuf;
42
43   assert(ms);
44   assert(ms->shdr.sh_type != SHT_NOBITS);
45   assert(ms->databuf);
46
47   newbuf = realloc(ms->databuf, ms->shdr.sh_size + len);
48   if (!newbuf) {
49     ELFU_WARN("appendData: malloc() failed for newbuf.\n");
50     return 1;
51   }
52
53   ms->databuf = newbuf;
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);
57
58   return 0;
59 }
60
61
62 static ElfuScn* insertSection(ElfuElf *me, ElfuElf *mrel, ElfuScn *oldscn)
63 {
64   ElfuScn *newscn = NULL;
65   GElf_Addr injAddr;
66   GElf_Off injOffset;
67   ElfuPhdr *injPhdr;
68
69   if (oldscn->shdr.sh_flags & SHF_ALLOC) {
70     newscn = cloneScn(oldscn);
71     if (!newscn) {
72       return NULL;
73     }
74
75     if (newscn->shdr.sh_type == SHT_NOBITS) {
76       /* Expand this to SHT_PROGBITS, then insert as such. */
77
78       assert(!newscn->databuf);
79
80       newscn->databuf = malloc(newscn->shdr.sh_size);
81       if (!newscn->databuf) {
82         goto ERROR;
83       }
84       newscn->shdr.sh_type = SHT_PROGBITS;
85     }
86
87     injAddr = elfu_mLayoutGetSpaceInPhdr(me,
88                                          newscn->shdr.sh_size,
89                                          newscn->shdr.sh_addralign,
90                                          newscn->shdr.sh_flags & SHF_WRITE,
91                                          newscn->shdr.sh_flags & SHF_EXECINSTR,
92                                          &injPhdr);
93
94     if (!injPhdr) {
95       ELFU_WARN("insertSection: Could not find a place to insert section.\n");
96       goto ERROR;
97     }
98
99     ELFU_INFO("Inserting %s at address 0x%x...\n",
100               elfu_mScnName(mrel, oldscn),
101               (unsigned)injAddr);
102
103     injOffset = injAddr - injPhdr->phdr.p_vaddr + injPhdr->phdr.p_offset;
104
105     newscn->shdr.sh_addr = injAddr;
106     newscn->shdr.sh_offset = injOffset;
107
108     if (CIRCLEQ_EMPTY(&injPhdr->childScnList)
109         || CIRCLEQ_LAST(&injPhdr->childScnList)->shdr.sh_offset < injOffset) {
110       CIRCLEQ_INSERT_TAIL(&injPhdr->childScnList, newscn, elemChildScn);
111     } else {
112       ElfuScn *ms;
113       CIRCLEQ_FOREACH(ms, &injPhdr->childScnList, elemChildScn) {
114         if (injOffset < ms->shdr.sh_offset) {
115           CIRCLEQ_INSERT_BEFORE(&injPhdr->childScnList, ms, newscn, elemChildScn);
116           break;
117         }
118       }
119     }
120
121
122     /* Inject name */
123     if (me->shstrtab) {
124       char *newname;
125       size_t newnamelen;
126
127       newnamelen = strlen("reladd") + 1;
128       if (elfu_mScnName(mrel, oldscn)) {
129         newnamelen += strlen(elfu_mScnName(mrel, oldscn));
130       }
131
132       newname = malloc(newnamelen);
133       strcpy(newname, "reladd");
134       strcat(newname, elfu_mScnName(mrel, oldscn));
135
136       if (!newname) {
137         ELFU_WARN("insertSection: malloc() failed for newname. Leaving section name empty.\n");
138         newscn->shdr.sh_name = 0;
139       } else {
140         size_t offset = me->shstrtab->shdr.sh_size;
141
142         if (!appendData(me->shstrtab, newname, newnamelen)) {
143           newscn->shdr.sh_name = offset;
144         }
145
146         free(newname);
147       }
148     }
149
150     return newscn;
151   } else {
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);
156       goto ERROR;
157   }
158
159   ERROR:
160   if (newscn) {
161     // TODO: Destroy newscn
162   }
163   return NULL;
164 }
165
166
167 static void* subScnAdd1(ElfuElf *mrel, ElfuScn *ms, void *aux1, void *aux2)
168 {
169   ElfuScn *newscn;
170   ElfuElf *me = (ElfuElf*)aux1;
171   (void)aux2;
172
173
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) {
179         break;
180       }
181
182       /* Find a place where it belongs and shove it in. */
183       newscn = insertSection(me, mrel, ms);
184       if (!newscn) {
185         ELFU_WARN("mReladd: Could not insert section %s (type %d), skipping.\n",
186                   elfu_mScnName(mrel, ms),
187                   ms->shdr.sh_type);
188       }
189       break;
190   }
191
192   return NULL;
193 }
194
195
196 static void* subScnAdd2(ElfuElf *mrel, ElfuScn *ms, void *aux1, void *aux2)
197 {
198   ElfuElf *me = (ElfuElf*)aux1;
199   (void)aux2;
200
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 */
206       break;
207
208
209     case SHT_REL: /* 9 */
210       /* Relocate. */
211       elfu_mRelocate(me, elfu_mScnByOldscn(me, ms->infoptr), ms);
212       break;
213
214     case SHT_RELA: /* 4 */
215       // TODO: Needs a parser
216       //elfu_mRelocate(elfu_mScnByOldscn(me, ms->infoptr), ms);
217
218     case SHT_SYMTAB: /* 2 */
219       /* Merge with the existing table. Take care of string tables also. */
220
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 */
234     default:
235       ELFU_WARN("mReladd: Skipping section %s (type %d).\n",
236                 elfu_mScnName(mrel, ms),
237                 ms->shdr.sh_type);
238   }
239
240   return NULL;
241 }
242
243
244
245 static void insertSymClone(ElfuElf *me, const ElfuScn *oldmsst, const ElfuSym *oldsym)
246 {
247   GElf_Xword newsize;
248   char *newbuf;
249   ElfuScn *newscn = NULL;
250   ElfuSym *newsym;
251   char *oldsymname;
252
253   assert(me);
254   assert(oldmsst);
255   assert(oldsym);
256
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);
260
261     /* If we didn't copy the section referenced, we won't
262      * copy this symbol either */
263     if (!newscn) {
264       return;
265     }
266   }
267
268   // TODO: Allocate symtab if none present
269   assert(me->symtab);
270
271   /* Allocate memory for the cloned symbol */
272   newsym = malloc(sizeof(*newsym));
273   if (!newsym) {
274     ELFU_WARN("insertSymClone: malloc() failed for newsym.\n");
275     goto ERROR;
276   }
277
278   oldsymname = ELFU_SYMSTR(oldmsst, oldsym->name);
279
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);
283   if (!newbuf) {
284     ELFU_WARN("insertSymClone: realloc() failed for strtab.\n");
285     goto ERROR;
286   }
287
288   me->symtab->linkptr->databuf = newbuf;
289
290   newsym->name = me->symtab->linkptr->shdr.sh_size;
291
292   strcpy(newbuf + newsym->name, oldsymname);
293
294   me->symtab->linkptr->shdr.sh_size = newsize;
295
296
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;
305
306   /* In executables, symbol addresses need to be in memory */
307   if (newscn) {
308     newsym->value += newscn->shdr.sh_addr;
309   }
310
311   /* Insert symbol */
312   CIRCLEQ_INSERT_TAIL(&me->symtab->symtab.syms, newsym, elem);
313
314   return;
315
316   ERROR:
317   if (newsym) {
318     free(newsym);
319   }
320 }
321
322 static void mergeSymtab(ElfuElf *me, const ElfuElf *mrel)
323 {
324   ElfuSym *sym;
325
326   assert(me);
327   assert(mrel);
328
329   CIRCLEQ_FOREACH(sym, &mrel->symtab->symtab.syms, elem) {
330     insertSymClone(me, mrel->symtab, sym);
331   }
332 }
333
334
335
336 void elfu_mReladd(ElfuElf *me, const ElfuElf *mrel)
337 {
338   assert(me);
339   assert(mrel);
340
341   /* For each section in object file, guess how to insert it */
342   elfu_mScnForall((ElfuElf*)mrel, subScnAdd1, me, NULL);
343
344   mergeSymtab(me, mrel);
345
346   /* Do relocations and other stuff */
347   elfu_mScnForall((ElfuElf*)mrel, subScnAdd2, me, NULL);
348
349   /* Re-layout to accommodate new contents */
350   elfu_mLayoutAuto(me);
351 }