4 #include <libelf/gelf.h>
5 #include <libelfu/libelfu.h>
7 typedef enum Destsegment {
14 static Destsegment destsegment(ElfuScn *ms)
16 if (!(ms->shdr.sh_flags & SHF_ALLOC)) {
20 if (!(ms->shdr.sh_flags & SHF_WRITE)
21 && (ms->shdr.sh_flags & SHF_EXECINSTR)) {
23 } else if ((ms->shdr.sh_flags & SHF_WRITE)
24 && !(ms->shdr.sh_flags & SHF_EXECINSTR)) {
33 static ElfuScn* insertSection(ElfuElf *me, ElfuElf *mrel, ElfuScn *ms)
36 ElfuPhdr *first = NULL;
37 ElfuPhdr *last = NULL;
38 ElfuScn *newscn = NULL;
40 int searchForCode = 0;
42 switch (destsegment(ms)) {
46 newscn = elfu_mCloneScn(ms);
51 /* Find first and last LOAD PHDRs. */
52 CIRCLEQ_FOREACH(mp, &me->phdrList, elem) {
53 if (mp->phdr.p_type != PT_LOAD) {
57 if (!first || mp->phdr.p_vaddr < first->phdr.p_vaddr) {
60 if (!last || mp->phdr.p_vaddr > last->phdr.p_vaddr) {
61 /* No need to check p_memsz as segments may not overlap in memory. */
67 if ((first->phdr.p_flags & PF_X) && !(first->phdr.p_flags & PF_W)) {
69 } else if ((last->phdr.p_flags & PF_X) && !(last->phdr.p_flags & PF_W)) {
75 if ((first->phdr.p_flags & PF_W) && !(first->phdr.p_flags & PF_X)) {
77 } else if ((last->phdr.p_flags & PF_W) && !(last->phdr.p_flags & PF_X)) {
85 ELFU_WARN("insertSection: Could not find injection anchor.\n"
86 " It has to be the first or last segment in the memory image.\n");
90 /* If the anchor is first or last, insert before or after */
91 if (injAnchor == first) {
92 /* Find first section and inject before it */
93 ElfuScn *firstScn = elfu_mScnFirstInSegment(me, injAnchor);
95 ELFU_WARN("insertSection: mScnFirstInSegment failed.\n");
97 // TODO: Error handling
99 injOffset = firstScn->shdr.sh_offset;
101 ELFU_INFO("Inserting %s at offset 0x%jx...\n",
102 elfu_mScnName(mrel, ms),
106 elfu_mInsertSpaceBefore(me, injOffset, ms->shdr.sh_size);
108 /* Update memory offset */
109 newscn->shdr.sh_addr = injAnchor->phdr.p_vaddr;
111 /* Insert into chain of sections */
112 elfu_mInsertScnInChainBefore(me, firstScn, newscn);
115 /* Find last section and inject after it */
116 ElfuScn *lastScn = elfu_mScnLastInSegment(me, injAnchor);
118 ELFU_WARN("insertSection: mScnLastInSegment failed.\n");
120 // TODO: Error handling
122 injOffset = lastScn->shdr.sh_offset + SCNFILESIZE(&lastScn->shdr);
124 ELFU_INFO("Expanding at offset 0x%jx...\n",
127 /* Expand NOBITS sections at injection site, if any. */
128 elfu_mExpandNobits(me, injOffset);
130 /* Recalculate injOffset in case we expanded a NOBITS section */
131 lastScn = elfu_mScnLastInSegment(me, injAnchor);
132 injOffset = lastScn->shdr.sh_offset + SCNFILESIZE(&lastScn->shdr);
134 ELFU_INFO("Inserting %s at offset 0x%jx...\n",
135 elfu_mScnName(mrel, ms),
139 elfu_mInsertSpaceAfter(me, injOffset, ms->shdr.sh_size);
141 /* Update memory offset */
142 newscn->shdr.sh_addr = injAnchor->phdr.p_vaddr + (injOffset - injAnchor->phdr.p_offset);
144 /* Insert into chain of sections */
145 elfu_mInsertScnInChainAfter(me, lastScn, newscn);
149 /* Update file offset in new section BEFORE we do anything else */
150 newscn->shdr.sh_offset = injOffset;
154 newscn->shdr.sh_name = 0;
163 ELFU_WARN("insertSection: Don't know where to insert ' %s with flags %jd (type %d).\n",
164 elfu_mScnName(mrel, ms),
168 ELFU_WARN("insertSection: Skipping section %s with flags %jd (type %d).\n",
169 elfu_mScnName(mrel, ms),
176 // TODO: Destroy newscn
183 void elfu_mReladd(ElfuElf *me, ElfuElf *mrel)
191 /* For each section in object file, guess how to insert it */
192 CIRCLEQ_FOREACH(ms, &mrel->scnList, elem) {
195 switch(ms->shdr.sh_type) {
196 case SHT_NULL: /* 0 */
199 case SHT_PROGBITS: /* 1 */
200 /* Find a place where it belongs and shove it in. */
201 newscn = insertSection(me, mrel, ms);
203 ELFU_WARN("mReladd: Could not insert section %s (type %d), skipping.\n",
204 elfu_mScnName(mrel, ms),
209 case SHT_SYMTAB: /* 2 */
210 case SHT_DYNSYM: /* 11 */
211 /* Merge with the existing table. Take care of string tables also. */
213 case SHT_STRTAB: /* 3 */
214 /* May have to be merged with the existing string table for
215 * the symbol table. */
217 case SHT_RELA: /* 4 */
218 case SHT_REL: /* 9 */
219 /* Possibly append this in memory to the section model
220 * that it describes. */
222 case SHT_NOBITS: /* 8 */
223 /* Expand this to SHT_PROGBITS, then insert as such. */
225 case SHT_HASH: /* 5 */
226 case SHT_DYNAMIC: /* 6 */
227 case SHT_SHLIB: /* 10 */
228 case SHT_SYMTAB_SHNDX: /* 18 */
230 /* Don't care about the next ones yet. I've never seen
231 * them and they can be implemented when necessary. */
232 case SHT_NOTE: /* 7 */
233 case SHT_INIT_ARRAY: /* 14 */
234 case SHT_FINI_ARRAY: /* 15 */
235 case SHT_PREINIT_ARRAY: /* 16 */
236 case SHT_GROUP: /* 17 */
237 case SHT_NUM: /* 19 */
239 ELFU_WARN("mReladd: Skipping section %s (type %d).\n",
240 elfu_mScnName(mrel, ms),