8c56d4ff7faa2af178b047598c7c74f46ae8e5db
[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 int appendData(ElfuScn *ms, void *buf, size_t len)
9 {
10   char *newbuf;
11
12   assert(ms);
13   assert(ms->shdr.sh_type != SHT_NOBITS);
14   assert(ms->databuf);
15
16   newbuf = realloc(ms->databuf, ms->shdr.sh_size + len);
17   if (!newbuf) {
18     ELFU_WARN("appendData: malloc() failed for newbuf.\n");
19     return 1;
20   }
21
22   ms->databuf = newbuf;
23   memcpy(newbuf + ms->shdr.sh_size, buf, len);
24   ms->shdr.sh_size += len;
25   assert(ms->shdr.sh_size == ms->shdr.sh_size);
26
27   return 0;
28 }
29
30
31 static ElfuScn* insertSection(ElfuElf *me, ElfuElf *mrel, ElfuScn *oldscn)
32 {
33   ElfuScn *newscn = NULL;
34   GElf_Addr injAddr;
35   GElf_Off injOffset;
36   ElfuPhdr *injPhdr;
37
38   if (oldscn->shdr.sh_flags & SHF_ALLOC) {
39     newscn = elfu_mCloneScn(oldscn);
40     if (!newscn) {
41       return NULL;
42     }
43
44     if (newscn->shdr.sh_type == SHT_NOBITS) {
45       /* Expand this to SHT_PROGBITS, then insert as such. */
46
47       assert(!newscn->databuf);
48
49       newscn->databuf = malloc(newscn->shdr.sh_size);
50       if (!newscn->databuf) {
51         goto ERROR;
52       }
53       newscn->shdr.sh_type = SHT_PROGBITS;
54     }
55
56     injAddr = elfu_mLayoutGetSpaceInPhdr(me,
57                                          newscn->shdr.sh_size,
58                                          newscn->shdr.sh_addralign,
59                                          newscn->shdr.sh_flags & SHF_WRITE,
60                                          newscn->shdr.sh_flags & SHF_EXECINSTR,
61                                          &injPhdr);
62
63     if (!injPhdr) {
64       ELFU_WARN("insertSection: Could not find a place to insert section.\n");
65       goto ERROR;
66     }
67
68     ELFU_INFO("Inserting %s at address 0x%x...\n",
69               elfu_mScnName(mrel, oldscn),
70               (unsigned)injAddr);
71
72     injOffset = injAddr - injPhdr->phdr.p_vaddr + injPhdr->phdr.p_offset;
73
74     newscn->shdr.sh_addr = injAddr;
75     newscn->shdr.sh_offset = injOffset;
76
77     if (CIRCLEQ_EMPTY(&injPhdr->childScnList)
78         || CIRCLEQ_LAST(&injPhdr->childScnList)->shdr.sh_offset < injOffset) {
79       CIRCLEQ_INSERT_TAIL(&injPhdr->childScnList, newscn, elemChildScn);
80     } else {
81       ElfuScn *ms;
82       CIRCLEQ_FOREACH(ms, &injPhdr->childScnList, elemChildScn) {
83         if (injOffset < ms->shdr.sh_offset) {
84           CIRCLEQ_INSERT_BEFORE(&injPhdr->childScnList, ms, newscn, elemChildScn);
85           break;
86         }
87       }
88     }
89
90
91     /* Inject name */
92     if (me->shstrtab) {
93       char *newname;
94       size_t newnamelen;
95
96       newnamelen = strlen("reladd") + 1;
97       if (elfu_mScnName(mrel, oldscn)) {
98         newnamelen += strlen(elfu_mScnName(mrel, oldscn));
99       }
100
101       newname = malloc(newnamelen);
102       strcpy(newname, "reladd");
103       strcat(newname, elfu_mScnName(mrel, oldscn));
104
105       if (!newname) {
106         ELFU_WARN("insertSection: malloc() failed for newname. Leaving section name empty.\n");
107         newscn->shdr.sh_name = 0;
108       } else {
109         size_t offset = me->shstrtab->shdr.sh_size;
110
111         if (!appendData(me->shstrtab, newname, newnamelen)) {
112           newscn->shdr.sh_name = offset;
113         }
114
115         free(newname);
116       }
117     }
118
119     return newscn;
120   } else {
121       ELFU_WARN("insertSection: Skipping non-memory section %s (type %d flags %u).\n",
122                 elfu_mScnName(mrel, oldscn),
123                 oldscn->shdr.sh_type,
124                 (unsigned)oldscn->shdr.sh_flags);
125       goto ERROR;
126   }
127
128   ERROR:
129   if (newscn) {
130     // TODO: Destroy newscn
131   }
132   return NULL;
133 }
134
135
136 static void* subScnAdd1(ElfuElf *mrel, ElfuScn *ms, void *aux1, void *aux2)
137 {
138   ElfuScn *newscn;
139   ElfuElf *me = (ElfuElf*)aux1;
140   (void)aux2;
141
142
143   switch(ms->shdr.sh_type) {
144     case SHT_PROGBITS: /* 1 */
145     case SHT_NOBITS: /* 8 */
146       /* Ignore empty sections */
147       if (ms->shdr.sh_size == 0) {
148         break;
149       }
150
151       /* Find a place where it belongs and shove it in. */
152       newscn = insertSection(me, mrel, ms);
153       if (!newscn) {
154         ELFU_WARN("mReladd: Could not insert section %s (type %d), skipping.\n",
155                   elfu_mScnName(mrel, ms),
156                   ms->shdr.sh_type);
157       }
158       break;
159   }
160
161   return NULL;
162 }
163
164
165 static void* subScnAdd2(ElfuElf *mrel, ElfuScn *ms, void *aux1, void *aux2)
166 {
167   ElfuElf *me = (ElfuElf*)aux1;
168   (void)aux2;
169
170   switch(ms->shdr.sh_type) {
171     case SHT_NULL: /* 0 */
172     case SHT_PROGBITS: /* 1 */
173     case SHT_STRTAB: /* 3 */
174     case SHT_NOBITS: /* 8 */
175       break;
176
177
178     case SHT_REL: /* 9 */
179       /* Relocate. */
180       elfu_mRelocate(me, elfu_mScnByOldscn(me, ms->infoptr), ms);
181       break;
182
183     case SHT_RELA: /* 4 */
184       // TODO: Needs a parser
185       //elfu_mRelocate(elfu_mScnByOldscn(me, ms->infoptr), ms);
186
187     case SHT_SYMTAB: /* 2 */
188       /* Merge with the existing table. Take care of string tables also. */
189
190     /* The next section types either do not occur in .o files, or are
191      * not strictly necessary to process here. */
192     case SHT_NOTE: /* 7 */
193     case SHT_HASH: /* 5 */
194     case SHT_DYNAMIC: /* 6 */
195     case SHT_SHLIB: /* 10 */
196     case SHT_DYNSYM: /* 11 */
197     case SHT_INIT_ARRAY: /* 14 */
198     case SHT_FINI_ARRAY: /* 15 */
199     case SHT_PREINIT_ARRAY: /* 16 */
200     case SHT_GROUP: /* 17 */
201     case SHT_SYMTAB_SHNDX: /* 18 */
202     case SHT_NUM: /* 19 */
203     default:
204       ELFU_WARN("mReladd: Skipping section %s (type %d).\n",
205                 elfu_mScnName(mrel, ms),
206                 ms->shdr.sh_type);
207   }
208
209   return NULL;
210 }
211
212
213
214 static void insertSymClone(ElfuElf *me, const ElfuScn *oldmsst, const ElfuSym *oldsym)
215 {
216   GElf_Xword newsize;
217   char *newbuf;
218   ElfuScn *newscn = NULL;
219   ElfuSym *newsym;
220   char *oldsymname;
221
222   assert(me);
223   assert(oldmsst);
224   assert(oldsym);
225
226   /* If the old symbol pointed to a section, find its clone in the target */
227   if (oldsym->scnptr) {
228     newscn = elfu_mScnByOldscn(me, oldsym->scnptr);
229
230     /* If we didn't copy the section referenced, we won't
231      * copy this symbol either */
232     if (!newscn) {
233       return;
234     }
235   }
236
237   // TODO: Allocate symtab if none present
238   assert(me->symtab);
239
240   /* Allocate memory for the cloned symbol */
241   newsym = malloc(sizeof(*newsym));
242   if (!newsym) {
243     ELFU_WARN("insertSymClone: malloc() failed for newsym.\n");
244     goto ERROR;
245   }
246
247   oldsymname = ELFU_SYMSTR(oldmsst, oldsym->name);
248
249   /* Expand .strtab, append symbol name, link newsym to it */
250   newsize = me->symtab->linkptr->shdr.sh_size + strlen(oldsymname) + 1;
251   newbuf = realloc(me->symtab->linkptr->databuf, newsize);
252   if (!newbuf) {
253     ELFU_WARN("insertSymClone: realloc() failed for strtab.\n");
254     goto ERROR;
255   }
256
257   me->symtab->linkptr->databuf = newbuf;
258
259   newsym->name = me->symtab->linkptr->shdr.sh_size;
260
261   strcpy(newbuf + newsym->name, oldsymname);
262
263   me->symtab->linkptr->shdr.sh_size = newsize;
264
265
266   /* Copy all other fields */
267   newsym->scnptr = newscn;
268   newsym->shndx = oldsym->shndx; /* If scnptr == NULL, this becomes relevant */
269   newsym->bind = oldsym->bind;
270   newsym->other = oldsym->other;
271   newsym->size = oldsym->size;
272   newsym->type = oldsym->type;
273   newsym->value = oldsym->value;
274
275   /* In executables, symbol addresses need to be in memory */
276   if (newscn) {
277     newsym->value += newscn->shdr.sh_addr;
278   }
279
280   /* Insert symbol */
281   CIRCLEQ_INSERT_TAIL(&me->symtab->symtab.syms, newsym, elem);
282
283   return;
284
285   ERROR:
286   if (newsym) {
287     free(newsym);
288   }
289 }
290
291 static void mergeSymtab(ElfuElf *me, const ElfuElf *mrel)
292 {
293   ElfuSym *sym;
294
295   assert(me);
296   assert(mrel);
297
298   CIRCLEQ_FOREACH(sym, &mrel->symtab->symtab.syms, elem) {
299     insertSymClone(me, mrel->symtab, sym);
300   }
301 }
302
303
304
305 void elfu_mReladd(ElfuElf *me, const ElfuElf *mrel)
306 {
307   assert(me);
308   assert(mrel);
309
310   /* For each section in object file, guess how to insert it */
311   elfu_mScnForall((ElfuElf*)mrel, subScnAdd1, me, NULL);
312
313   mergeSymtab(me, mrel);
314
315   /* Do relocations and other stuff */
316   elfu_mScnForall((ElfuElf*)mrel, subScnAdd2, me, NULL);
317
318   /* Re-layout to accommodate new contents */
319   elfu_mLayoutAuto(me);
320 }