5 #include <libelfu/libelfu.h>
8 static ElfuScn* cloneScn(ElfuScn *ms)
14 newscn = elfu_mScnAlloc();
19 newscn->shdr = ms->shdr;
22 void *newbuf = malloc(ms->shdr.sh_size);
24 ELFU_WARN("cloneScn: Could not allocate memory for new data buffer.\n");
29 memcpy(newbuf, ms->databuf, ms->shdr.sh_size);
30 newscn->databuf = newbuf;
40 static ElfuScn* insertSection(ElfuElf *me, ElfuElf *mrel, ElfuScn *oldscn)
42 ElfuScn *newscn = NULL;
47 if (!(oldscn->shdr.sh_flags & SHF_ALLOC)) {
48 ELFU_WARN("insertSection: Skipping non-memory section %s (type %d flags %u).\n",
49 elfu_mScnName(mrel, oldscn),
51 (unsigned)oldscn->shdr.sh_flags);
54 newscn = cloneScn(oldscn);
59 if (newscn->shdr.sh_type == SHT_NOBITS) {
60 /* Expand this to SHT_PROGBITS, then insert as such. */
62 assert(!newscn->databuf);
64 newscn->databuf = malloc(newscn->shdr.sh_size);
65 if (!newscn->databuf) {
68 newscn->shdr.sh_type = SHT_PROGBITS;
71 injAddr = elfu_mLayoutGetSpaceInPhdr(me,
73 newscn->shdr.sh_addralign,
74 newscn->shdr.sh_flags & SHF_WRITE,
75 newscn->shdr.sh_flags & SHF_EXECINSTR,
79 ELFU_WARN("insertSection: Could not find a place to insert section.\n");
83 ELFU_INFO("Inserting %s at address 0x%x...\n",
84 elfu_mScnName(mrel, oldscn),
87 injOffset = injAddr - injPhdr->phdr.p_vaddr + injPhdr->phdr.p_offset;
89 newscn->shdr.sh_addr = injAddr;
90 newscn->shdr.sh_offset = injOffset;
92 /* Insert section in child list, ordered by memory address */
93 if (CIRCLEQ_EMPTY(&injPhdr->childScnList)
94 || CIRCLEQ_LAST(&injPhdr->childScnList)->shdr.sh_addr < injAddr) {
95 CIRCLEQ_INSERT_TAIL(&injPhdr->childScnList, newscn, elemChildScn);
98 CIRCLEQ_FOREACH(ms, &injPhdr->childScnList, elemChildScn) {
99 if (injAddr < ms->shdr.sh_addr) {
100 CIRCLEQ_INSERT_BEFORE(&injPhdr->childScnList, ms, newscn, elemChildScn);
111 newnamelen = strlen("reladd") + 1;
112 if (elfu_mScnName(mrel, oldscn)) {
113 newnamelen += strlen(elfu_mScnName(mrel, oldscn));
116 char newname[newnamelen];
118 strcpy(newname, "reladd");
119 strcat(newname, elfu_mScnName(mrel, oldscn));
121 newscn->shdr.sh_name = me->shstrtab->shdr.sh_size;
123 if (elfu_mScnAppendData(me->shstrtab, newname, newnamelen)) {
124 newscn->shdr.sh_name = 0;
133 // TODO: Destroy newscn
139 static void* subScnAdd1(ElfuElf *mrel, ElfuScn *ms, void *aux1, void *aux2)
142 ElfuElf *me = (ElfuElf*)aux1;
146 switch(ms->shdr.sh_type) {
147 case SHT_PROGBITS: /* 1 */
148 case SHT_NOBITS: /* 8 */
149 /* Ignore empty sections */
150 if (ms->shdr.sh_size == 0) {
154 /* Find a place where it belongs and shove it in. */
155 newscn = insertSection(me, mrel, ms);
157 ELFU_WARN("mReladd: Could not insert section %s (type %d), skipping.\n",
158 elfu_mScnName(mrel, ms),
168 static void* subScnAdd2(ElfuElf *mrel, ElfuScn *ms, void *aux1, void *aux2)
170 ElfuElf *me = (ElfuElf*)aux1;
173 switch(ms->shdr.sh_type) {
174 case SHT_NULL: /* 0 */
175 case SHT_PROGBITS: /* 1 */
176 case SHT_SYMTAB: /* 2 */
177 case SHT_STRTAB: /* 3 */
178 case SHT_NOBITS: /* 8 */
181 case SHT_RELA: /* 4 */
182 case SHT_REL: /* 9 */
184 if (elfu_mRelocate(me, elfu_mScnByOldscn(me, ms->infoptr), ms)) {
189 /* The next section types either do not occur in .o files, or are
190 * not strictly necessary to process here. */
191 case SHT_NOTE: /* 7 */
192 case SHT_HASH: /* 5 */
193 case SHT_DYNAMIC: /* 6 */
194 case SHT_SHLIB: /* 10 */
195 case SHT_DYNSYM: /* 11 */
196 case SHT_INIT_ARRAY: /* 14 */
197 case SHT_FINI_ARRAY: /* 15 */
198 case SHT_PREINIT_ARRAY: /* 16 */
199 case SHT_GROUP: /* 17 */
200 case SHT_SYMTAB_SHNDX: /* 18 */
201 case SHT_NUM: /* 19 */
203 ELFU_WARN("mReladd: Skipping section %s (type %d).\n",
204 elfu_mScnName(mrel, ms),
213 static void insertSymClone(ElfuElf *me, const ElfuScn *oldmsst, const ElfuSym *oldsym)
217 ElfuScn *newscn = NULL;
225 /* If the old symbol pointed to a section, find its clone in the target */
226 if (oldsym->scnptr) {
227 newscn = elfu_mScnByOldscn(me, oldsym->scnptr);
229 /* If we didn't copy the section referenced, we won't
230 * copy this symbol either */
236 /* If we don't have a symbol table, create one so we have somewhere to
237 * write our new symbols to. */
238 elfu_mSymtabAddGlobalDymtabIfNotPresent(me);
240 /* Allocate memory for the cloned symbol */
241 newsym = malloc(sizeof(*newsym));
243 ELFU_WARN("insertSymClone: malloc() failed for newsym.\n");
247 oldsymname = ELFU_SYMSTR(oldmsst, oldsym->name);
249 /* Expand .strtab, append symbol name, link newsym to it */
250 newsize = me->symtab->linkptr->shdr.sh_size + strlen(oldsymname) + 1;
251 newbuf = realloc(me->symtab->linkptr->databuf, newsize);
253 ELFU_WARN("insertSymClone: realloc() failed for strtab.\n");
257 me->symtab->linkptr->databuf = newbuf;
259 newsym->name = me->symtab->linkptr->shdr.sh_size;
261 strcpy(newbuf + newsym->name, oldsymname);
263 me->symtab->linkptr->shdr.sh_size = newsize;
266 /* Copy all other fields */
267 newsym->scnptr = newscn;
268 newsym->shndx = oldsym->shndx; /* If scnptr == NULL, this becomes relevant */
269 newsym->bind = oldsym->bind;
270 newsym->other = oldsym->other;
271 newsym->size = oldsym->size;
272 newsym->type = oldsym->type;
273 newsym->value = oldsym->value;
275 /* In executables, symbol addresses need to be in memory */
277 newsym->value += newscn->shdr.sh_addr;
281 CIRCLEQ_INSERT_TAIL(&me->symtab->symtab.syms, newsym, elem);
291 static void mergeSymtab(ElfuElf *me, const ElfuElf *mrel)
298 CIRCLEQ_FOREACH(sym, &mrel->symtab->symtab.syms, elem) {
299 insertSymClone(me, mrel->symtab, sym);
302 elfu_mSymtabFlatten(me);
307 int elfu_mReladd(ElfuElf *me, const ElfuElf *mrel)
312 /* For each section in object file, guess how to insert it */
313 elfu_mScnForall((ElfuElf*)mrel, subScnAdd1, me, NULL);
315 mergeSymtab(me, mrel);
317 /* Do relocations and other stuff */
318 if (elfu_mScnForall((ElfuElf*)mrel, subScnAdd2, me, NULL)) {
319 ELFU_WARN("elfu_mReladd: Reladd aborted. Target model is unclean.\n");
323 /* Re-layout to accommodate new contents */
324 elfu_mLayoutAuto(me);