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 int 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),
152 int subScnAdd2(ElfuElf *mrel, ElfuScn *ms, void *aux1, void *aux2)
155 ElfuElf *me = (ElfuElf*)aux1;
158 switch(ms->shdr.sh_type) {
159 case SHT_NULL: /* 0 */
160 case SHT_PROGBITS: /* 1 */
163 case SHT_SYMTAB: /* 2 */
164 case SHT_DYNSYM: /* 11 */
165 /* Merge with the existing table. Take care of string tables also. */
167 case SHT_STRTAB: /* 3 */
168 /* May have to be merged with the existing string table for
169 * the symbol table. */
171 case SHT_RELA: /* 4 */
172 case SHT_REL: /* 9 */
173 /* Possibly append this in memory to the section model
174 * that it describes. */
176 case SHT_NOBITS: /* 8 */
177 /* Expand this to SHT_PROGBITS, then insert as such. */
179 case SHT_HASH: /* 5 */
180 case SHT_DYNAMIC: /* 6 */
181 case SHT_SHLIB: /* 10 */
182 case SHT_SYMTAB_SHNDX: /* 18 */
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 */
193 ELFU_WARN("mReladd: Skipping section %s (type %d).\n",
194 elfu_mScnName(mrel, ms),
202 void elfu_mReladd(ElfuElf *me, ElfuElf *mrel)
207 /* For each section in object file, guess how to insert it */
208 elfu_mScnForall(mrel, subScnAdd1, me, NULL);
210 /* Do relocations and other stuff */
211 elfu_mScnForall(mrel, subScnAdd2, me, NULL);
213 /* Re-layout to accommodate new contents */
214 elfu_mLayoutAuto(me);