2fdfb48bf25fc56c4a59a791046fba42118668be
[centaur.git] / src / 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   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     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%jx...\n",
71               elfu_mScnName(mrel, oldscn),
72               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 %jd).\n",
124                 elfu_mScnName(mrel, oldscn),
125                 oldscn->shdr.sh_type,
126                 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   (void)aux2;
141   ElfuElf *me = (ElfuElf*)aux1;
142
143   ElfuScn *newscn;
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   (void)aux2;
170   ElfuElf *me = (ElfuElf*)aux1;
171   (void)me;
172
173   switch(ms->shdr.sh_type) {
174     case SHT_NULL: /* 0 */
175     case SHT_PROGBITS: /* 1 */
176     case SHT_STRTAB: /* 3 */
177     case SHT_NOBITS: /* 8 */
178       break;
179
180
181     case SHT_REL: /* 9 */
182       /* Relocate. */
183       elfu_mRelocate32(me, elfu_mScnByOldscn(me, ms->infoptr), ms);
184       break;
185
186     case SHT_RELA: /* 4 */
187       // TODO: Needs a parser
188       //elfu_mRelocate(elfu_mScnByOldscn(me, ms->infoptr), ms);
189
190     case SHT_SYMTAB: /* 2 */
191       /* Merge with the existing table. Take care of string tables also. */
192
193     /* The next section types either do not occur in .o files, or are
194      * not strictly necessary to process here. */
195     case SHT_NOTE: /* 7 */
196     case SHT_HASH: /* 5 */
197     case SHT_DYNAMIC: /* 6 */
198     case SHT_SHLIB: /* 10 */
199     case SHT_DYNSYM: /* 11 */
200     case SHT_INIT_ARRAY: /* 14 */
201     case SHT_FINI_ARRAY: /* 15 */
202     case SHT_PREINIT_ARRAY: /* 16 */
203     case SHT_GROUP: /* 17 */
204     case SHT_SYMTAB_SHNDX: /* 18 */
205     case SHT_NUM: /* 19 */
206     default:
207       ELFU_WARN("mReladd: Skipping section %s (type %d).\n",
208                 elfu_mScnName(mrel, ms),
209                 ms->shdr.sh_type);
210   }
211
212   return NULL;
213 }
214
215
216 void elfu_mReladd(ElfuElf *me, ElfuElf *mrel)
217 {
218   assert(me);
219   assert(mrel);
220
221   /* For each section in object file, guess how to insert it */
222   elfu_mScnForall(mrel, subScnAdd1, me, NULL);
223
224   /* Do relocations and other stuff */
225   elfu_mScnForall(mrel, subScnAdd2, me, NULL);
226
227   /* Re-layout to accommodate new contents */
228   elfu_mLayoutAuto(me);
229 }