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