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