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