Simplify includes
[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 int 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   }
147
148   return 0;
149 }
150
151
152 int subScnAdd2(ElfuElf *mrel, ElfuScn *ms, void *aux1, void *aux2)
153 {
154   (void)aux2;
155   ElfuElf *me = (ElfuElf*)aux1;
156   (void)me;
157
158   switch(ms->shdr.sh_type) {
159     case SHT_NULL: /* 0 */
160     case SHT_PROGBITS: /* 1 */
161       break;
162
163     case SHT_SYMTAB: /* 2 */
164     case SHT_DYNSYM: /* 11 */
165       /* Merge with the existing table. Take care of string tables also. */
166
167     case SHT_STRTAB: /* 3 */
168       /* May have to be merged with the existing string table for
169        * the symbol table. */
170
171     case SHT_RELA: /* 4 */
172     case SHT_REL: /* 9 */
173       /* Possibly append this in memory to the section model
174        * that it describes. */
175
176     case SHT_NOBITS: /* 8 */
177       /* Expand this to SHT_PROGBITS, then insert as such. */
178
179     case SHT_HASH: /* 5 */
180     case SHT_DYNAMIC: /* 6 */
181     case SHT_SHLIB: /* 10 */
182     case SHT_SYMTAB_SHNDX: /* 18 */
183
184     /* Don't care about the next ones yet. I've never seen
185      * them and they can be implemented when necessary. */
186     case SHT_NOTE: /* 7 */
187     case SHT_INIT_ARRAY: /* 14 */
188     case SHT_FINI_ARRAY: /* 15 */
189     case SHT_PREINIT_ARRAY: /* 16 */
190     case SHT_GROUP: /* 17 */
191     case SHT_NUM: /* 19 */
192     default:
193       ELFU_WARN("mReladd: Skipping section %s (type %d).\n",
194                 elfu_mScnName(mrel, ms),
195                 ms->shdr.sh_type);
196   }
197
198   return 0;
199 }
200
201
202 void elfu_mReladd(ElfuElf *me, ElfuElf *mrel)
203 {
204   assert(me);
205   assert(mrel);
206
207   /* For each section in object file, guess how to insert it */
208   elfu_mScnForall(mrel, subScnAdd1, me, NULL);
209
210   /* Do relocations and other stuff */
211   elfu_mScnForall(mrel, subScnAdd2, me, NULL);
212
213   /* Re-layout to accommodate new contents */
214   elfu_mLayoutAuto(me);
215 }