reladd: Work more with sh_addr instead of sh_offset
[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       elfu_mRelocate(me, elfu_mScnByOldscn(me, ms->infoptr), ms);
214       break;
215
216     /* The next section types either do not occur in .o files, or are
217      * not strictly necessary to process here. */
218     case SHT_NOTE: /* 7 */
219     case SHT_HASH: /* 5 */
220     case SHT_DYNAMIC: /* 6 */
221     case SHT_SHLIB: /* 10 */
222     case SHT_DYNSYM: /* 11 */
223     case SHT_INIT_ARRAY: /* 14 */
224     case SHT_FINI_ARRAY: /* 15 */
225     case SHT_PREINIT_ARRAY: /* 16 */
226     case SHT_GROUP: /* 17 */
227     case SHT_SYMTAB_SHNDX: /* 18 */
228     case SHT_NUM: /* 19 */
229     default:
230       ELFU_WARN("mReladd: Skipping section %s (type %d).\n",
231                 elfu_mScnName(mrel, ms),
232                 ms->shdr.sh_type);
233   }
234
235   return NULL;
236 }
237
238
239
240 static void insertSymClone(ElfuElf *me, const ElfuScn *oldmsst, const ElfuSym *oldsym)
241 {
242   GElf_Xword newsize;
243   char *newbuf;
244   ElfuScn *newscn = NULL;
245   ElfuSym *newsym;
246   char *oldsymname;
247
248   assert(me);
249   assert(oldmsst);
250   assert(oldsym);
251
252   /* If the old symbol pointed to a section, find its clone in the target */
253   if (oldsym->scnptr) {
254     newscn = elfu_mScnByOldscn(me, oldsym->scnptr);
255
256     /* If we didn't copy the section referenced, we won't
257      * copy this symbol either */
258     if (!newscn) {
259       return;
260     }
261   }
262
263   // TODO: Allocate symtab if none present
264   assert(me->symtab);
265
266   /* Allocate memory for the cloned symbol */
267   newsym = malloc(sizeof(*newsym));
268   if (!newsym) {
269     ELFU_WARN("insertSymClone: malloc() failed for newsym.\n");
270     goto ERROR;
271   }
272
273   oldsymname = ELFU_SYMSTR(oldmsst, oldsym->name);
274
275   /* Expand .strtab, append symbol name, link newsym to it */
276   newsize = me->symtab->linkptr->shdr.sh_size + strlen(oldsymname) + 1;
277   newbuf = realloc(me->symtab->linkptr->databuf, newsize);
278   if (!newbuf) {
279     ELFU_WARN("insertSymClone: realloc() failed for strtab.\n");
280     goto ERROR;
281   }
282
283   me->symtab->linkptr->databuf = newbuf;
284
285   newsym->name = me->symtab->linkptr->shdr.sh_size;
286
287   strcpy(newbuf + newsym->name, oldsymname);
288
289   me->symtab->linkptr->shdr.sh_size = newsize;
290
291
292   /* Copy all other fields */
293   newsym->scnptr = newscn;
294   newsym->shndx = oldsym->shndx; /* If scnptr == NULL, this becomes relevant */
295   newsym->bind = oldsym->bind;
296   newsym->other = oldsym->other;
297   newsym->size = oldsym->size;
298   newsym->type = oldsym->type;
299   newsym->value = oldsym->value;
300
301   /* In executables, symbol addresses need to be in memory */
302   if (newscn) {
303     newsym->value += newscn->shdr.sh_addr;
304   }
305
306   /* Insert symbol */
307   CIRCLEQ_INSERT_TAIL(&me->symtab->symtab.syms, newsym, elem);
308
309   return;
310
311   ERROR:
312   if (newsym) {
313     free(newsym);
314   }
315 }
316
317 static void mergeSymtab(ElfuElf *me, const ElfuElf *mrel)
318 {
319   ElfuSym *sym;
320
321   assert(me);
322   assert(mrel);
323
324   CIRCLEQ_FOREACH(sym, &mrel->symtab->symtab.syms, elem) {
325     insertSymClone(me, mrel->symtab, sym);
326   }
327 }
328
329
330
331 void elfu_mReladd(ElfuElf *me, const ElfuElf *mrel)
332 {
333   assert(me);
334   assert(mrel);
335
336   /* For each section in object file, guess how to insert it */
337   elfu_mScnForall((ElfuElf*)mrel, subScnAdd1, me, NULL);
338
339   mergeSymtab(me, mrel);
340
341   /* Do relocations and other stuff */
342   elfu_mScnForall((ElfuElf*)mrel, subScnAdd2, me, NULL);
343
344   /* Re-layout to accommodate new contents */
345   elfu_mLayoutAuto(me);
346 }