5 #include <libelf/gelf.h>
6 #include <libelfu/libelfu.h>
9 static ElfuScn* insertSection(ElfuElf *me, ElfuElf *mrel, ElfuScn *oldscn)
11 ElfuScn *newscn = NULL;
16 if (oldscn->shdr.sh_flags & SHF_ALLOC) {
17 newscn = elfu_mCloneScn(oldscn);
23 injAddr = elfu_mLayoutGetSpaceInPhdr(me,
25 newscn->shdr.sh_addralign,
26 newscn->shdr.sh_flags & SHF_WRITE,
27 newscn->shdr.sh_flags & SHF_EXECINSTR,
31 ELFU_WARN("insertSection: Could not find a place to insert section.\n");
35 ELFU_INFO("Inserting %s at address 0x%jx...\n",
36 elfu_mScnName(mrel, oldscn),
39 injOffset = injAddr - injPhdr->phdr.p_vaddr + injPhdr->phdr.p_offset;
41 newscn->shdr.sh_addr = injAddr;
42 newscn->shdr.sh_offset = injOffset;
44 if (CIRCLEQ_EMPTY(&injPhdr->childScnList)
45 || CIRCLEQ_LAST(&injPhdr->childScnList)->shdr.sh_offset < injOffset) {
46 CIRCLEQ_INSERT_TAIL(&injPhdr->childScnList, newscn, elemChildScn);
49 CIRCLEQ_FOREACH(ms, &injPhdr->childScnList, elemChildScn) {
50 if (injOffset < ms->shdr.sh_offset) {
51 CIRCLEQ_INSERT_BEFORE(&injPhdr->childScnList, ms, newscn, elemChildScn);
59 newscn->shdr.sh_name = 0;
65 ELFU_WARN("insertSection: Skipping section %s with flags %jd (type %d).\n",
66 elfu_mScnName(mrel, oldscn),
67 oldscn->shdr.sh_flags,
68 oldscn->shdr.sh_type);
74 // TODO: Destroy newscn
80 int subScnAdd1(ElfuElf *mrel, ElfuScn *ms, void *aux1, void *aux2)
83 ElfuElf *me = (ElfuElf*)aux1;
87 switch(ms->shdr.sh_type) {
88 case SHT_PROGBITS: /* 1 */
89 /* Find a place where it belongs and shove it in. */
90 newscn = insertSection(me, mrel, ms);
92 ELFU_WARN("mReladd: Could not insert section %s (type %d), skipping.\n",
93 elfu_mScnName(mrel, ms),
103 int subScnAdd2(ElfuElf *mrel, ElfuScn *ms, void *aux1, void *aux2)
106 ElfuElf *me = (ElfuElf*)aux1;
109 switch(ms->shdr.sh_type) {
110 case SHT_NULL: /* 0 */
111 case SHT_PROGBITS: /* 1 */
114 case SHT_SYMTAB: /* 2 */
115 case SHT_DYNSYM: /* 11 */
116 /* Merge with the existing table. Take care of string tables also. */
118 case SHT_STRTAB: /* 3 */
119 /* May have to be merged with the existing string table for
120 * the symbol table. */
122 case SHT_RELA: /* 4 */
123 case SHT_REL: /* 9 */
124 /* Possibly append this in memory to the section model
125 * that it describes. */
127 case SHT_NOBITS: /* 8 */
128 /* Expand this to SHT_PROGBITS, then insert as such. */
130 case SHT_HASH: /* 5 */
131 case SHT_DYNAMIC: /* 6 */
132 case SHT_SHLIB: /* 10 */
133 case SHT_SYMTAB_SHNDX: /* 18 */
135 /* Don't care about the next ones yet. I've never seen
136 * them and they can be implemented when necessary. */
137 case SHT_NOTE: /* 7 */
138 case SHT_INIT_ARRAY: /* 14 */
139 case SHT_FINI_ARRAY: /* 15 */
140 case SHT_PREINIT_ARRAY: /* 16 */
141 case SHT_GROUP: /* 17 */
142 case SHT_NUM: /* 19 */
144 ELFU_WARN("mReladd: Skipping section %s (type %d).\n",
145 elfu_mScnName(mrel, ms),
153 void elfu_mReladd(ElfuElf *me, ElfuElf *mrel)
158 /* For each section in object file, guess how to insert it */
159 elfu_mScnForall(mrel, subScnAdd1, me, NULL);
161 /* Do relocations and other stuff */
162 elfu_mScnForall(mrel, subScnAdd2, me, NULL);