fc0adc360d05d5e94915cc41e68dcf52d2edfb3e
[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     ELFU_WARN("insertSection: Skipping non-memory section %s (type %d flags %u).\n",
71               elfu_mScnName(mrel, oldscn),
72               oldscn->shdr.sh_type,
73               (unsigned)oldscn->shdr.sh_flags);
74     goto ERROR;
75   } else {
76     newscn = cloneScn(oldscn);
77     if (!newscn) {
78       return NULL;
79     }
80
81     if (newscn->shdr.sh_type == SHT_NOBITS) {
82       /* Expand this to SHT_PROGBITS, then insert as such. */
83
84       assert(!newscn->databuf);
85
86       newscn->databuf = malloc(newscn->shdr.sh_size);
87       if (!newscn->databuf) {
88         goto ERROR;
89       }
90       newscn->shdr.sh_type = SHT_PROGBITS;
91     }
92
93     injAddr = elfu_mLayoutGetSpaceInPhdr(me,
94                                          newscn->shdr.sh_size,
95                                          newscn->shdr.sh_addralign,
96                                          newscn->shdr.sh_flags & SHF_WRITE,
97                                          newscn->shdr.sh_flags & SHF_EXECINSTR,
98                                          &injPhdr);
99
100     if (!injPhdr) {
101       ELFU_WARN("insertSection: Could not find a place to insert section.\n");
102       goto ERROR;
103     }
104
105     ELFU_INFO("Inserting %s at address 0x%x...\n",
106               elfu_mScnName(mrel, oldscn),
107               (unsigned)injAddr);
108
109     injOffset = injAddr - injPhdr->phdr.p_vaddr + injPhdr->phdr.p_offset;
110
111     newscn->shdr.sh_addr = injAddr;
112     newscn->shdr.sh_offset = injOffset;
113
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);
118     } else {
119       ElfuScn *ms;
120       CIRCLEQ_FOREACH(ms, &injPhdr->childScnList, elemChildScn) {
121         if (injAddr < ms->shdr.sh_addr) {
122           CIRCLEQ_INSERT_BEFORE(&injPhdr->childScnList, ms, newscn, elemChildScn);
123           break;
124         }
125       }
126     }
127
128
129     /* Inject name */
130     if (me->shstrtab) {
131       char *newname;
132       size_t newnamelen;
133
134       newnamelen = strlen("reladd") + 1;
135       if (elfu_mScnName(mrel, oldscn)) {
136         newnamelen += strlen(elfu_mScnName(mrel, oldscn));
137       }
138
139       newname = malloc(newnamelen);
140       strcpy(newname, "reladd");
141       strcat(newname, elfu_mScnName(mrel, oldscn));
142
143       if (!newname) {
144         ELFU_WARN("insertSection: malloc() failed for newname. Leaving section name empty.\n");
145         newscn->shdr.sh_name = 0;
146       } else {
147         size_t offset = me->shstrtab->shdr.sh_size;
148
149         if (!appendData(me->shstrtab, newname, newnamelen)) {
150           newscn->shdr.sh_name = offset;
151         }
152
153         free(newname);
154       }
155     }
156
157     return newscn;
158   }
159
160   ERROR:
161   if (newscn) {
162     // TODO: Destroy newscn
163   }
164   return NULL;
165 }
166
167
168 static void* subScnAdd1(ElfuElf *mrel, ElfuScn *ms, void *aux1, void *aux2)
169 {
170   ElfuScn *newscn;
171   ElfuElf *me = (ElfuElf*)aux1;
172   (void)aux2;
173
174
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) {
180         break;
181       }
182
183       /* Find a place where it belongs and shove it in. */
184       newscn = insertSection(me, mrel, ms);
185       if (!newscn) {
186         ELFU_WARN("mReladd: Could not insert section %s (type %d), skipping.\n",
187                   elfu_mScnName(mrel, ms),
188                   ms->shdr.sh_type);
189       }
190       break;
191   }
192
193   return NULL;
194 }
195
196
197 static void* subScnAdd2(ElfuElf *mrel, ElfuScn *ms, void *aux1, void *aux2)
198 {
199   ElfuElf *me = (ElfuElf*)aux1;
200   (void)aux2;
201
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 */
208       break;
209
210     case SHT_RELA: /* 4 */
211     case SHT_REL: /* 9 */
212       /* Relocate. */
213       if (elfu_mRelocate(me, elfu_mScnByOldscn(me, ms->infoptr), ms)) {
214         return (void*)-1;
215       }
216       break;
217
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 */
231     default:
232       ELFU_WARN("mReladd: Skipping section %s (type %d).\n",
233                 elfu_mScnName(mrel, ms),
234                 ms->shdr.sh_type);
235   }
236
237   return NULL;
238 }
239
240
241
242 static void insertSymClone(ElfuElf *me, const ElfuScn *oldmsst, const ElfuSym *oldsym)
243 {
244   GElf_Xword newsize;
245   char *newbuf;
246   ElfuScn *newscn = NULL;
247   ElfuSym *newsym;
248   char *oldsymname;
249
250   assert(me);
251   assert(oldmsst);
252   assert(oldsym);
253
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);
257
258     /* If we didn't copy the section referenced, we won't
259      * copy this symbol either */
260     if (!newscn) {
261       return;
262     }
263   }
264
265   // TODO: Allocate symtab if none present
266   assert(me->symtab);
267
268   /* Allocate memory for the cloned symbol */
269   newsym = malloc(sizeof(*newsym));
270   if (!newsym) {
271     ELFU_WARN("insertSymClone: malloc() failed for newsym.\n");
272     goto ERROR;
273   }
274
275   oldsymname = ELFU_SYMSTR(oldmsst, oldsym->name);
276
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);
280   if (!newbuf) {
281     ELFU_WARN("insertSymClone: realloc() failed for strtab.\n");
282     goto ERROR;
283   }
284
285   me->symtab->linkptr->databuf = newbuf;
286
287   newsym->name = me->symtab->linkptr->shdr.sh_size;
288
289   strcpy(newbuf + newsym->name, oldsymname);
290
291   me->symtab->linkptr->shdr.sh_size = newsize;
292
293
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;
302
303   /* In executables, symbol addresses need to be in memory */
304   if (newscn) {
305     newsym->value += newscn->shdr.sh_addr;
306   }
307
308   /* Insert symbol */
309   CIRCLEQ_INSERT_TAIL(&me->symtab->symtab.syms, newsym, elem);
310
311   return;
312
313   ERROR:
314   if (newsym) {
315     free(newsym);
316   }
317 }
318
319 static void mergeSymtab(ElfuElf *me, const ElfuElf *mrel)
320 {
321   ElfuSym *sym;
322
323   assert(me);
324   assert(mrel);
325
326   CIRCLEQ_FOREACH(sym, &mrel->symtab->symtab.syms, elem) {
327     insertSymClone(me, mrel->symtab, sym);
328   }
329
330   elfu_mSymtabFlatten(me);
331 }
332
333
334
335 int elfu_mReladd(ElfuElf *me, const ElfuElf *mrel)
336 {
337   assert(me);
338   assert(mrel);
339
340   /* For each section in object file, guess how to insert it */
341   elfu_mScnForall((ElfuElf*)mrel, subScnAdd1, me, NULL);
342
343   mergeSymtab(me, mrel);
344
345   /* Do relocations and other stuff */
346   if (elfu_mScnForall((ElfuElf*)mrel, subScnAdd2, me, NULL)) {
347     ELFU_WARN("elfu_mReladd: Reladd aborted. Target model is unclean.\n");
348     return -1;
349   }
350
351   /* Re-layout to accommodate new contents */
352   elfu_mLayoutAuto(me);
353
354   return 0;
355 }