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);
45 if (newscn->shdr.sh_type == SHT_NOBITS) {
46 /* Expand this to SHT_PROGBITS, then insert as such. */
48 assert(!newscn->data.d_buf);
50 newscn->data.d_buf = malloc(newscn->shdr.sh_size);
51 if (!newscn->data.d_buf) {
54 newscn->data.d_size = newscn->shdr.sh_size;
55 newscn->shdr.sh_type = SHT_PROGBITS;
58 injAddr = elfu_mLayoutGetSpaceInPhdr(me,
60 newscn->shdr.sh_addralign,
61 newscn->shdr.sh_flags & SHF_WRITE,
62 newscn->shdr.sh_flags & SHF_EXECINSTR,
66 ELFU_WARN("insertSection: Could not find a place to insert section.\n");
70 ELFU_INFO("Inserting %s at address 0x%jx...\n",
71 elfu_mScnName(mrel, oldscn),
74 injOffset = injAddr - injPhdr->phdr.p_vaddr + injPhdr->phdr.p_offset;
76 newscn->shdr.sh_addr = injAddr;
77 newscn->shdr.sh_offset = injOffset;
79 if (CIRCLEQ_EMPTY(&injPhdr->childScnList)
80 || CIRCLEQ_LAST(&injPhdr->childScnList)->shdr.sh_offset < injOffset) {
81 CIRCLEQ_INSERT_TAIL(&injPhdr->childScnList, newscn, elemChildScn);
84 CIRCLEQ_FOREACH(ms, &injPhdr->childScnList, elemChildScn) {
85 if (injOffset < ms->shdr.sh_offset) {
86 CIRCLEQ_INSERT_BEFORE(&injPhdr->childScnList, ms, newscn, elemChildScn);
98 newnamelen = strlen("reladd") + 1;
99 if (elfu_mScnName(mrel, oldscn)) {
100 newnamelen += strlen(elfu_mScnName(mrel, oldscn));
103 newname = malloc(newnamelen);
104 strcpy(newname, "reladd");
105 strcat(newname, elfu_mScnName(mrel, oldscn));
108 ELFU_WARN("insertSection: malloc() failed for newname. Leaving section name empty.\n");
109 newscn->shdr.sh_name = 0;
111 size_t offset = me->shstrtab->shdr.sh_size;
113 if (!appendData(me->shstrtab, newname, newnamelen)) {
114 newscn->shdr.sh_name = offset;
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);
132 // TODO: Destroy newscn
138 static void* subScnAdd1(ElfuElf *mrel, ElfuScn *ms, void *aux1, void *aux2)
141 ElfuElf *me = (ElfuElf*)aux1;
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) {
153 /* Find a place where it belongs and shove it in. */
154 newscn = insertSection(me, mrel, ms);
156 ELFU_WARN("mReladd: Could not insert section %s (type %d), skipping.\n",
157 elfu_mScnName(mrel, ms),
167 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_NOBITS: /* 8 */
180 case SHT_REL: /* 9 */
182 elfu_mRelocate32(me, elfu_mScnByOldscn(me, ms->infoptr), ms);
185 case SHT_RELA: /* 4 */
186 // TODO: Needs a parser
187 //elfu_mRelocate(elfu_mScnByOldscn(me, ms->infoptr), ms);
189 case SHT_SYMTAB: /* 2 */
190 /* Merge with the existing table. Take care of string tables also. */
192 case SHT_STRTAB: /* 3 */
193 /* May have to be merged with the existing string table for
194 * the symbol table. */
196 /* The next section types either do not occur in .o files, or are
197 * not strictly necessary to process here. */
198 case SHT_NOTE: /* 7 */
199 case SHT_HASH: /* 5 */
200 case SHT_DYNAMIC: /* 6 */
201 case SHT_SHLIB: /* 10 */
202 case SHT_DYNSYM: /* 11 */
203 case SHT_INIT_ARRAY: /* 14 */
204 case SHT_FINI_ARRAY: /* 15 */
205 case SHT_PREINIT_ARRAY: /* 16 */
206 case SHT_GROUP: /* 17 */
207 case SHT_SYMTAB_SHNDX: /* 18 */
208 case SHT_NUM: /* 19 */
210 ELFU_WARN("mReladd: Skipping section %s (type %d).\n",
211 elfu_mScnName(mrel, ms),
219 void elfu_mReladd(ElfuElf *me, ElfuElf *mrel)
224 /* For each section in object file, guess how to insert it */
225 elfu_mScnForall(mrel, subScnAdd1, me, NULL);
227 /* Do relocations and other stuff */
228 elfu_mScnForall(mrel, subScnAdd2, me, NULL);
230 /* Re-layout to accommodate new contents */
231 elfu_mLayoutAuto(me);