5 #include <libelfu/libelfu.h>
8 static int appendData(ElfuScn *ms, void *buf, size_t len)
13 assert(ms->shdr.sh_type != SHT_NOBITS);
14 assert(ms->data.d_buf);
16 newbuf = realloc(ms->data.d_buf, ms->shdr.sh_size + len);
18 ELFU_WARN("appendData: malloc() failed for newbuf.\n");
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);
32 static ElfuScn* insertSection(ElfuElf *me, ElfuElf *mrel, ElfuScn *oldscn)
34 ElfuScn *newscn = NULL;
39 if (oldscn->shdr.sh_flags & SHF_ALLOC) {
40 newscn = elfu_mCloneScn(oldscn);
46 injAddr = elfu_mLayoutGetSpaceInPhdr(me,
48 newscn->shdr.sh_addralign,
49 newscn->shdr.sh_flags & SHF_WRITE,
50 newscn->shdr.sh_flags & SHF_EXECINSTR,
54 ELFU_WARN("insertSection: Could not find a place to insert section.\n");
58 ELFU_INFO("Inserting %s at address 0x%jx...\n",
59 elfu_mScnName(mrel, oldscn),
62 injOffset = injAddr - injPhdr->phdr.p_vaddr + injPhdr->phdr.p_offset;
64 newscn->shdr.sh_addr = injAddr;
65 newscn->shdr.sh_offset = injOffset;
67 if (CIRCLEQ_EMPTY(&injPhdr->childScnList)
68 || CIRCLEQ_LAST(&injPhdr->childScnList)->shdr.sh_offset < injOffset) {
69 CIRCLEQ_INSERT_TAIL(&injPhdr->childScnList, newscn, elemChildScn);
72 CIRCLEQ_FOREACH(ms, &injPhdr->childScnList, elemChildScn) {
73 if (injOffset < ms->shdr.sh_offset) {
74 CIRCLEQ_INSERT_BEFORE(&injPhdr->childScnList, ms, newscn, elemChildScn);
86 newnamelen = strlen("reladd") + 1;
87 if (elfu_mScnName(mrel, oldscn)) {
88 newnamelen += strlen(elfu_mScnName(mrel, oldscn));
91 newname = malloc(newnamelen);
92 strcpy(newname, "reladd");
93 strcat(newname, elfu_mScnName(mrel, oldscn));
96 ELFU_WARN("insertSection: malloc() failed for newname. Leaving section name empty.\n");
97 newscn->shdr.sh_name = 0;
99 size_t offset = me->shstrtab->shdr.sh_size;
101 if (!appendData(me->shstrtab, newname, newnamelen)) {
102 newscn->shdr.sh_name = offset;
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);
123 // TODO: Destroy newscn
129 static void* subScnAdd1(ElfuElf *mrel, ElfuScn *ms, void *aux1, void *aux2)
132 ElfuElf *me = (ElfuElf*)aux1;
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);
141 ELFU_WARN("mReladd: Could not insert section %s (type %d), skipping.\n",
142 elfu_mScnName(mrel, ms),
146 case SHT_NOBITS: /* 8 */
147 /* Expand this to SHT_PROGBITS, then insert as such. */
158 static void* subScnAdd2(ElfuElf *mrel, ElfuScn *ms, void *aux1, void *aux2)
161 ElfuElf *me = (ElfuElf*)aux1;
164 switch(ms->shdr.sh_type) {
165 case SHT_NULL: /* 0 */
166 case SHT_PROGBITS: /* 1 */
170 case SHT_REL: /* 9 */
172 elfu_mRelocate32(me, elfu_mScnByOldscn(me, ms->infoptr), ms);
175 case SHT_RELA: /* 4 */
176 // TODO: Needs a parser
177 //elfu_mRelocate(elfu_mScnByOldscn(me, ms->infoptr), ms);
179 case SHT_SYMTAB: /* 2 */
180 /* Merge with the existing table. Take care of string tables also. */
182 case SHT_STRTAB: /* 3 */
183 /* May have to be merged with the existing string table for
184 * the symbol table. */
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 */
200 ELFU_WARN("mReladd: Skipping section %s (type %d).\n",
201 elfu_mScnName(mrel, ms),
209 void elfu_mReladd(ElfuElf *me, ElfuElf *mrel)
214 /* For each section in object file, guess how to insert it */
215 elfu_mScnForall(mrel, subScnAdd1, me, NULL);
217 /* Do relocations and other stuff */
218 elfu_mScnForall(mrel, subScnAdd2, me, NULL);
220 /* Re-layout to accommodate new contents */
221 elfu_mLayoutAuto(me);