a3ccc99d5689ec4b20786a0cfa195ed626d8a249
[centaur.git] / src / model / 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   void *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
46     injAddr = elfu_mLayoutGetSpaceInPhdr(me,
47                                          newscn->shdr.sh_size,
48                                          newscn->shdr.sh_addralign,
49                                          newscn->shdr.sh_flags & SHF_WRITE,
50                                          newscn->shdr.sh_flags & SHF_EXECINSTR,
51                                          &injPhdr);
52
53     if (!injPhdr) {
54       ELFU_WARN("insertSection: Could not find a place to insert section.\n");
55       goto ERROR;
56     }
57
58     ELFU_INFO("Inserting %s at address 0x%jx...\n",
59               elfu_mScnName(mrel, oldscn),
60               injAddr);
61
62     injOffset = injAddr - injPhdr->phdr.p_vaddr + injPhdr->phdr.p_offset;
63
64     newscn->shdr.sh_addr = injAddr;
65     newscn->shdr.sh_offset = injOffset;
66
67     if (CIRCLEQ_EMPTY(&injPhdr->childScnList)
68         || CIRCLEQ_LAST(&injPhdr->childScnList)->shdr.sh_offset < injOffset) {
69       CIRCLEQ_INSERT_TAIL(&injPhdr->childScnList, newscn, elemChildScn);
70     } else {
71       ElfuScn *ms;
72       CIRCLEQ_FOREACH(ms, &injPhdr->childScnList, elemChildScn) {
73         if (injOffset < ms->shdr.sh_offset) {
74           CIRCLEQ_INSERT_BEFORE(&injPhdr->childScnList, ms, newscn, elemChildScn);
75           break;
76         }
77       }
78     }
79
80
81     /* Inject name */
82     if (me->shstrtab) {
83       char *newname;
84       size_t newnamelen;
85
86       newnamelen = strlen("reladd") + 1;
87       if (elfu_mScnName(mrel, oldscn)) {
88         newnamelen += strlen(elfu_mScnName(mrel, oldscn));
89       }
90
91       newname = malloc(newnamelen);
92       strcpy(newname, "reladd");
93       strcat(newname, elfu_mScnName(mrel, oldscn));
94
95       if (!newname) {
96         ELFU_WARN("insertSection: malloc() failed for newname. Leaving section name empty.\n");
97         newscn->shdr.sh_name = 0;
98       } else {
99         size_t offset = me->shstrtab->shdr.sh_size;
100
101         if (!appendData(me->shstrtab, newname, newnamelen)) {
102           newscn->shdr.sh_name = offset;
103         }
104
105         free(newname);
106       }
107     }
108
109
110     // TODO: Relocate
111
112     return newscn;
113   } else {
114       ELFU_WARN("insertSection: Skipping section %s with flags %jd (type %d).\n",
115                 elfu_mScnName(mrel, oldscn),
116                 oldscn->shdr.sh_flags,
117                 oldscn->shdr.sh_type);
118       goto ERROR;
119   }
120
121   ERROR:
122   if (newscn) {
123     // TODO: Destroy newscn
124   }
125   return NULL;
126 }
127
128
129 static void* subScnAdd1(ElfuElf *mrel, ElfuScn *ms, void *aux1, void *aux2)
130 {
131   (void)aux2;
132   ElfuElf *me = (ElfuElf*)aux1;
133
134   ElfuScn *newscn;
135
136   switch(ms->shdr.sh_type) {
137     case SHT_PROGBITS: /* 1 */
138       /* Find a place where it belongs and shove it in. */
139       newscn = insertSection(me, mrel, ms);
140       if (!newscn) {
141         ELFU_WARN("mReladd: Could not insert section %s (type %d), skipping.\n",
142                   elfu_mScnName(mrel, ms),
143                   ms->shdr.sh_type);
144       }
145       break;
146     case SHT_NOBITS: /* 8 */
147       /* Expand this to SHT_PROGBITS, then insert as such. */
148
149       // TODO
150
151       break;
152   }
153
154   return NULL;
155 }
156
157
158 static void* subScnAdd2(ElfuElf *mrel, ElfuScn *ms, void *aux1, void *aux2)
159 {
160   (void)aux2;
161   ElfuElf *me = (ElfuElf*)aux1;
162   (void)me;
163
164   switch(ms->shdr.sh_type) {
165     case SHT_NULL: /* 0 */
166     case SHT_PROGBITS: /* 1 */
167       break;
168
169
170     case SHT_REL: /* 9 */
171       /* Relocate. */
172       elfu_mRelocate32(me, elfu_mScnByOldscn(me, ms->infoptr), ms);
173       break;
174
175     case SHT_RELA: /* 4 */
176       // TODO: Needs a parser
177       //elfu_mRelocate(elfu_mScnByOldscn(me, ms->infoptr), ms);
178
179     case SHT_SYMTAB: /* 2 */
180       /* Merge with the existing table. Take care of string tables also. */
181
182     case SHT_STRTAB: /* 3 */
183       /* May have to be merged with the existing string table for
184        * the symbol table. */
185
186     /* The next section types either do not occur in .o files, or are
187      * not strictly necessary to process here. */
188     case SHT_NOTE: /* 7 */
189     case SHT_HASH: /* 5 */
190     case SHT_DYNAMIC: /* 6 */
191     case SHT_SHLIB: /* 10 */
192     case SHT_DYNSYM: /* 11 */
193     case SHT_INIT_ARRAY: /* 14 */
194     case SHT_FINI_ARRAY: /* 15 */
195     case SHT_PREINIT_ARRAY: /* 16 */
196     case SHT_GROUP: /* 17 */
197     case SHT_SYMTAB_SHNDX: /* 18 */
198     case SHT_NUM: /* 19 */
199     default:
200       ELFU_WARN("mReladd: Skipping section %s (type %d).\n",
201                 elfu_mScnName(mrel, ms),
202                 ms->shdr.sh_type);
203   }
204
205   return NULL;
206 }
207
208
209 void elfu_mReladd(ElfuElf *me, ElfuElf *mrel)
210 {
211   assert(me);
212   assert(mrel);
213
214   /* For each section in object file, guess how to insert it */
215   elfu_mScnForall(mrel, subScnAdd1, me, NULL);
216
217   /* Do relocations and other stuff */
218   elfu_mScnForall(mrel, subScnAdd2, me, NULL);
219
220   /* Re-layout to accommodate new contents */
221   elfu_mLayoutAuto(me);
222 }