5 #include <libelf/gelf.h>
6 #include <libelfu/libelfu.h>
9 static int appendData(ElfuScn *ms, void *buf, size_t len)
14 assert(ms->shdr.sh_type != SHT_NOBITS);
15 assert(ms->data.d_buf);
17 newbuf = realloc(ms->data.d_buf, ms->shdr.sh_size + len);
19 ELFU_WARN("appendData: malloc() failed for newbuf.\n");
23 ms->data.d_buf = newbuf;
24 memcpy(newbuf + ms->shdr.sh_size, buf, len);
25 ms->shdr.sh_size += len;
26 ms->data.d_size += len;
27 assert(ms->shdr.sh_size == ms->data.d_size);
33 static ElfuScn* insertSection(ElfuElf *me, ElfuElf *mrel, ElfuScn *oldscn)
35 ElfuScn *newscn = NULL;
40 if (oldscn->shdr.sh_flags & SHF_ALLOC) {
41 newscn = elfu_mCloneScn(oldscn);
47 injAddr = elfu_mLayoutGetSpaceInPhdr(me,
49 newscn->shdr.sh_addralign,
50 newscn->shdr.sh_flags & SHF_WRITE,
51 newscn->shdr.sh_flags & SHF_EXECINSTR,
55 ELFU_WARN("insertSection: Could not find a place to insert section.\n");
59 ELFU_INFO("Inserting %s at address 0x%jx...\n",
60 elfu_mScnName(mrel, oldscn),
63 injOffset = injAddr - injPhdr->phdr.p_vaddr + injPhdr->phdr.p_offset;
65 newscn->shdr.sh_addr = injAddr;
66 newscn->shdr.sh_offset = injOffset;
68 if (CIRCLEQ_EMPTY(&injPhdr->childScnList)
69 || CIRCLEQ_LAST(&injPhdr->childScnList)->shdr.sh_offset < injOffset) {
70 CIRCLEQ_INSERT_TAIL(&injPhdr->childScnList, newscn, elemChildScn);
73 CIRCLEQ_FOREACH(ms, &injPhdr->childScnList, elemChildScn) {
74 if (injOffset < ms->shdr.sh_offset) {
75 CIRCLEQ_INSERT_BEFORE(&injPhdr->childScnList, ms, newscn, elemChildScn);
87 newnamelen = strlen("reladd") + 1;
88 if (elfu_mScnName(mrel, oldscn)) {
89 newnamelen += strlen(elfu_mScnName(mrel, oldscn));
92 newname = malloc(newnamelen);
93 strcpy(newname, "reladd");
94 strcat(newname, elfu_mScnName(mrel, oldscn));
97 ELFU_WARN("insertSection: malloc() failed for newname. Leaving section name empty.\n");
98 newscn->shdr.sh_name = 0;
100 size_t offset = me->shstrtab->shdr.sh_size;
102 if (!appendData(me->shstrtab, newname, newnamelen)) {
103 newscn->shdr.sh_name = offset;
115 ELFU_WARN("insertSection: Skipping section %s with flags %jd (type %d).\n",
116 elfu_mScnName(mrel, oldscn),
117 oldscn->shdr.sh_flags,
118 oldscn->shdr.sh_type);
124 // TODO: Destroy newscn
130 int subScnAdd1(ElfuElf *mrel, ElfuScn *ms, void *aux1, void *aux2)
133 ElfuElf *me = (ElfuElf*)aux1;
137 switch(ms->shdr.sh_type) {
138 case SHT_PROGBITS: /* 1 */
139 /* Find a place where it belongs and shove it in. */
140 newscn = insertSection(me, mrel, ms);
142 ELFU_WARN("mReladd: Could not insert section %s (type %d), skipping.\n",
143 elfu_mScnName(mrel, ms),
153 int subScnAdd2(ElfuElf *mrel, ElfuScn *ms, void *aux1, void *aux2)
156 ElfuElf *me = (ElfuElf*)aux1;
159 switch(ms->shdr.sh_type) {
160 case SHT_NULL: /* 0 */
161 case SHT_PROGBITS: /* 1 */
164 case SHT_SYMTAB: /* 2 */
165 case SHT_DYNSYM: /* 11 */
166 /* Merge with the existing table. Take care of string tables also. */
168 case SHT_STRTAB: /* 3 */
169 /* May have to be merged with the existing string table for
170 * the symbol table. */
172 case SHT_RELA: /* 4 */
173 case SHT_REL: /* 9 */
174 /* Possibly append this in memory to the section model
175 * that it describes. */
177 case SHT_NOBITS: /* 8 */
178 /* Expand this to SHT_PROGBITS, then insert as such. */
180 case SHT_HASH: /* 5 */
181 case SHT_DYNAMIC: /* 6 */
182 case SHT_SHLIB: /* 10 */
183 case SHT_SYMTAB_SHNDX: /* 18 */
185 /* Don't care about the next ones yet. I've never seen
186 * them and they can be implemented when necessary. */
187 case SHT_NOTE: /* 7 */
188 case SHT_INIT_ARRAY: /* 14 */
189 case SHT_FINI_ARRAY: /* 15 */
190 case SHT_PREINIT_ARRAY: /* 16 */
191 case SHT_GROUP: /* 17 */
192 case SHT_NUM: /* 19 */
194 ELFU_WARN("mReladd: Skipping section %s (type %d).\n",
195 elfu_mScnName(mrel, ms),
203 void elfu_mReladd(ElfuElf *me, ElfuElf *mrel)
208 /* For each section in object file, guess how to insert it */
209 elfu_mScnForall(mrel, subScnAdd1, me, NULL);
211 /* Do relocations and other stuff */
212 elfu_mScnForall(mrel, subScnAdd2, me, NULL);
214 /* Re-layout to accommodate new contents */
215 elfu_mLayoutAuto(me);