1980c765aff0c3c1158439476e363fb4e2900d41
[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
40 static ElfuScn* insertSection(ElfuElf *me, ElfuElf *mrel, ElfuScn *oldscn)
41 {
42   ElfuScn *newscn = NULL;
43   GElf_Addr injAddr;
44   GElf_Off injOffset;
45   ElfuPhdr *injPhdr;
46
47   if (!(oldscn->shdr.sh_flags & SHF_ALLOC)) {
48     ELFU_WARN("insertSection: Skipping non-memory section %s (type %d flags %u).\n",
49               elfu_mScnName(mrel, oldscn),
50               oldscn->shdr.sh_type,
51               (unsigned)oldscn->shdr.sh_flags);
52     goto ERROR;
53   } else {
54     newscn = cloneScn(oldscn);
55     if (!newscn) {
56       return NULL;
57     }
58
59     if (newscn->shdr.sh_type == SHT_NOBITS) {
60       /* Expand this to SHT_PROGBITS, then insert as such. */
61
62       assert(!newscn->databuf);
63
64       newscn->databuf = malloc(newscn->shdr.sh_size);
65       if (!newscn->databuf) {
66         goto ERROR;
67       }
68       newscn->shdr.sh_type = SHT_PROGBITS;
69     }
70
71     injAddr = elfu_mLayoutGetSpaceInPhdr(me,
72                                          newscn->shdr.sh_size,
73                                          newscn->shdr.sh_addralign,
74                                          newscn->shdr.sh_flags & SHF_WRITE,
75                                          newscn->shdr.sh_flags & SHF_EXECINSTR,
76                                          &injPhdr);
77
78     if (!injPhdr) {
79       ELFU_WARN("insertSection: Could not find a place to insert section.\n");
80       goto ERROR;
81     }
82
83     ELFU_INFO("Inserting %s at address 0x%x...\n",
84               elfu_mScnName(mrel, oldscn),
85               (unsigned)injAddr);
86
87     injOffset = injAddr - injPhdr->phdr.p_vaddr + injPhdr->phdr.p_offset;
88
89     newscn->shdr.sh_addr = injAddr;
90     newscn->shdr.sh_offset = injOffset;
91
92     /* Insert section in child list, ordered by memory address */
93     if (CIRCLEQ_EMPTY(&injPhdr->childScnList)
94         || CIRCLEQ_LAST(&injPhdr->childScnList)->shdr.sh_addr < injAddr) {
95       CIRCLEQ_INSERT_TAIL(&injPhdr->childScnList, newscn, elemChildScn);
96     } else {
97       ElfuScn *ms;
98       CIRCLEQ_FOREACH(ms, &injPhdr->childScnList, elemChildScn) {
99         if (injAddr < ms->shdr.sh_addr) {
100           CIRCLEQ_INSERT_BEFORE(&injPhdr->childScnList, ms, newscn, elemChildScn);
101           break;
102         }
103       }
104     }
105
106
107     /* Inject name */
108     if (me->shstrtab) {
109       size_t newnamelen;
110
111       newnamelen = strlen("reladd") + 1;
112       if (elfu_mScnName(mrel, oldscn)) {
113         newnamelen += strlen(elfu_mScnName(mrel, oldscn));
114       }
115
116       char newname[newnamelen];
117
118       strcpy(newname, "reladd");
119       strcat(newname, elfu_mScnName(mrel, oldscn));
120
121       newscn->shdr.sh_name = me->shstrtab->shdr.sh_size;
122
123       if (elfu_mScnAppendData(me->shstrtab, newname, newnamelen)) {
124         newscn->shdr.sh_name = 0;
125       }
126     }
127
128     return newscn;
129   }
130
131   ERROR:
132   if (newscn) {
133     // TODO: Destroy newscn
134   }
135   return NULL;
136 }
137
138
139 static void* subScnAdd1(ElfuElf *mrel, ElfuScn *ms, void *aux1, void *aux2)
140 {
141   ElfuScn *newscn;
142   ElfuElf *me = (ElfuElf*)aux1;
143   (void)aux2;
144
145
146   switch(ms->shdr.sh_type) {
147     case SHT_PROGBITS: /* 1 */
148     case SHT_NOBITS: /* 8 */
149       /* Ignore empty sections */
150       if (ms->shdr.sh_size == 0) {
151         break;
152       }
153
154       /* Find a place where it belongs and shove it in. */
155       newscn = insertSection(me, mrel, ms);
156       if (!newscn) {
157         ELFU_WARN("mReladd: Could not insert section %s (type %d), skipping.\n",
158                   elfu_mScnName(mrel, ms),
159                   ms->shdr.sh_type);
160       }
161       break;
162   }
163
164   return NULL;
165 }
166
167
168 static void* subScnAdd2(ElfuElf *mrel, ElfuScn *ms, void *aux1, void *aux2)
169 {
170   ElfuElf *me = (ElfuElf*)aux1;
171   (void)aux2;
172
173   switch(ms->shdr.sh_type) {
174     case SHT_NULL: /* 0 */
175     case SHT_PROGBITS: /* 1 */
176     case SHT_SYMTAB: /* 2 */
177     case SHT_STRTAB: /* 3 */
178     case SHT_NOBITS: /* 8 */
179       break;
180
181     case SHT_RELA: /* 4 */
182     case SHT_REL: /* 9 */
183       /* Relocate. */
184       if (elfu_mRelocate(me, elfu_mScnByOldscn(me, ms->infoptr), ms)) {
185         return (void*)-1;
186       }
187       break;
188
189     /* The next section types either do not occur in .o files, or are
190      * not strictly necessary to process here. */
191     case SHT_NOTE: /* 7 */
192     case SHT_HASH: /* 5 */
193     case SHT_DYNAMIC: /* 6 */
194     case SHT_SHLIB: /* 10 */
195     case SHT_DYNSYM: /* 11 */
196     case SHT_INIT_ARRAY: /* 14 */
197     case SHT_FINI_ARRAY: /* 15 */
198     case SHT_PREINIT_ARRAY: /* 16 */
199     case SHT_GROUP: /* 17 */
200     case SHT_SYMTAB_SHNDX: /* 18 */
201     case SHT_NUM: /* 19 */
202     default:
203       ELFU_WARN("mReladd: Skipping section %s (type %d).\n",
204                 elfu_mScnName(mrel, ms),
205                 ms->shdr.sh_type);
206   }
207
208   return NULL;
209 }
210
211
212
213 static void insertSymClone(ElfuElf *me, const ElfuScn *oldmsst, const ElfuSym *oldsym)
214 {
215   GElf_Xword newsize;
216   char *newbuf;
217   ElfuScn *newscn = NULL;
218   ElfuSym *newsym;
219   char *oldsymname;
220
221   assert(me);
222   assert(oldmsst);
223   assert(oldsym);
224
225   /* If the old symbol pointed to a section, find its clone in the target */
226   if (oldsym->scnptr) {
227     newscn = elfu_mScnByOldscn(me, oldsym->scnptr);
228
229     /* If we didn't copy the section referenced, we won't
230      * copy this symbol either */
231     if (!newscn) {
232       return;
233     }
234   }
235
236   /* If we don't have a symbol table, create one so we have somewhere to
237    * write our new symbols to. */
238   elfu_mSymtabAddGlobalDymtabIfNotPresent(me);
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   elfu_mSymtabFlatten(me);
303 }
304
305
306
307 int elfu_mReladd(ElfuElf *me, const ElfuElf *mrel)
308 {
309   assert(me);
310   assert(mrel);
311
312   /* For each section in object file, guess how to insert it */
313   elfu_mScnForall((ElfuElf*)mrel, subScnAdd1, me, NULL);
314
315   mergeSymtab(me, mrel);
316
317   /* Do relocations and other stuff */
318   if (elfu_mScnForall((ElfuElf*)mrel, subScnAdd2, me, NULL)) {
319     ELFU_WARN("elfu_mReladd: Reladd aborted. Target model is unclean.\n");
320     return -1;
321   }
322
323   /* Re-layout to accommodate new contents */
324   elfu_mLayoutAuto(me);
325
326   return 0;
327 }