1 /* This file is part of centaur.
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.
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.
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/>.
19 #include <sys/types.h>
20 #include <libelfu/libelfu.h>
23 static ElfuScn* cloneScn(ElfuScn *ms)
29 newscn = elfu_mScnAlloc();
34 newscn->shdr = ms->shdr;
37 void *newbuf = malloc(ms->shdr.sh_size);
39 ELFU_WARN("cloneScn: Could not allocate memory for new data buffer.\n");
44 memcpy(newbuf, ms->databuf, ms->shdr.sh_size);
45 newscn->databuf = newbuf;
55 static ElfuScn* insertSection(ElfuElf *me, ElfuElf *mrel, ElfuScn *oldscn)
57 ElfuScn *newscn = NULL;
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),
66 (unsigned)oldscn->shdr.sh_flags);
69 newscn = cloneScn(oldscn);
74 if (newscn->shdr.sh_type == SHT_NOBITS) {
75 /* Expand this to SHT_PROGBITS, then insert as such. */
77 assert(!newscn->databuf);
79 newscn->databuf = malloc(newscn->shdr.sh_size);
80 if (!newscn->databuf) {
83 newscn->shdr.sh_type = SHT_PROGBITS;
86 injAddr = elfu_mLayoutGetSpaceInPhdr(me,
88 newscn->shdr.sh_addralign,
89 newscn->shdr.sh_flags & SHF_WRITE,
90 newscn->shdr.sh_flags & SHF_EXECINSTR,
94 ELFU_WARN("insertSection: Could not find a place to insert section.\n");
98 ELFU_INFO("Inserting %s at address 0x%x...\n",
99 elfu_mScnName(mrel, oldscn),
102 injOffset = injAddr - injPhdr->phdr.p_vaddr + injPhdr->phdr.p_offset;
104 newscn->shdr.sh_addr = injAddr;
105 newscn->shdr.sh_offset = injOffset;
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);
113 CIRCLEQ_FOREACH(ms, &injPhdr->childScnList, elemChildScn) {
114 if (injAddr < ms->shdr.sh_addr) {
115 CIRCLEQ_INSERT_BEFORE(&injPhdr->childScnList, ms, newscn, elemChildScn);
126 newnamelen = strlen("reladd") + 1;
127 if (elfu_mScnName(mrel, oldscn)) {
128 newnamelen += strlen(elfu_mScnName(mrel, oldscn));
131 char newname[newnamelen];
133 strcpy(newname, "reladd");
134 strcat(newname, elfu_mScnName(mrel, oldscn));
136 newscn->shdr.sh_name = me->shstrtab->shdr.sh_size;
138 if (elfu_mScnAppendData(me->shstrtab, newname, newnamelen)) {
139 newscn->shdr.sh_name = 0;
148 elfu_mScnDestroy(newscn);
154 static void* subScnAdd1(ElfuElf *mrel, ElfuScn *ms, void *aux1, void *aux2)
157 ElfuElf *me = (ElfuElf*)aux1;
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) {
169 /* Find a place where it belongs and shove it in. */
170 newscn = insertSection(me, mrel, ms);
172 ELFU_WARN("mReladd: Could not insert section %s (type %d), skipping.\n",
173 elfu_mScnName(mrel, ms),
183 static void* subScnAdd2(ElfuElf *mrel, ElfuScn *ms, void *aux1, void *aux2)
185 ElfuElf *me = (ElfuElf*)aux1;
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 */
196 case SHT_RELA: /* 4 */
197 case SHT_REL: /* 9 */
199 if (elfu_mRelocate(me, elfu_mScnByOldscn(me, ms->infoptr), ms)) {
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 */
218 ELFU_WARN("mReladd: Skipping section %s (type %d).\n",
219 elfu_mScnName(mrel, ms),
228 static void insertSymClone(ElfuElf *me, const ElfuScn *oldmsst, const ElfuSym *oldsym)
232 ElfuScn *newscn = NULL;
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);
244 /* If we didn't copy the section referenced, we won't
245 * copy this symbol either */
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);
255 /* Allocate memory for the cloned symbol */
256 newsym = malloc(sizeof(*newsym));
258 ELFU_WARN("insertSymClone: malloc() failed for newsym.\n");
262 oldsymname = ELFU_SYMSTR(oldmsst, oldsym->name);
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);
268 ELFU_WARN("insertSymClone: realloc() failed for strtab.\n");
272 me->symtab->linkptr->databuf = newbuf;
274 newsym->name = me->symtab->linkptr->shdr.sh_size;
276 strcpy(newbuf + newsym->name, oldsymname);
278 me->symtab->linkptr->shdr.sh_size = newsize;
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;
290 /* In executables, symbol addresses need to be in memory */
292 newsym->value += newscn->shdr.sh_addr;
296 CIRCLEQ_INSERT_TAIL(&me->symtab->symtab.syms, newsym, elem);
306 static void mergeSymtab(ElfuElf *me, const ElfuElf *mrel)
313 CIRCLEQ_FOREACH(sym, &mrel->symtab->symtab.syms, elem) {
314 insertSymClone(me, mrel->symtab, sym);
317 elfu_mSymtabFlatten(me);
322 int elfu_mReladd(ElfuElf *me, const ElfuElf *mrel)
327 /* For each section in object file, guess how to insert it */
328 elfu_mScnForall((ElfuElf*)mrel, subScnAdd1, me, NULL);
330 mergeSymtab(me, mrel);
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");
338 /* Re-layout to accommodate new contents */
339 elfu_mLayoutAuto(me);