5 #include <libelf/libelf.h>
6 #include <libelf/gelf.h>
7 #include <libelfu/libelfu.h>
10 static int cmpScnOffs(const void *ms1, const void *ms2)
15 ElfuScn *s1 = *(ElfuScn**)ms1;
16 ElfuScn *s2 = *(ElfuScn**)ms2;
22 if (s1->shdr.sh_offset < s2->shdr.sh_offset) {
24 } else if (s1->shdr.sh_offset == s2->shdr.sh_offset) {
26 } else /* if (s1->shdr.sh_offset > s2->shdr.sh_offset) */ {
33 static ElfuPhdr* parentPhdr(ElfuElf *me, ElfuScn *ms)
40 CIRCLEQ_FOREACH(mp, &me->phdrList, elem) {
41 if (mp->phdr.p_type != PT_LOAD) {
45 if (ms->shdr.sh_addr >= mp->phdr.p_vaddr
46 && OFFS_END(ms->shdr.sh_addr, ms->shdr.sh_size) <= OFFS_END(mp->phdr.p_vaddr, mp->phdr.p_memsz)) {
48 } else if (ms->shdr.sh_offset >= mp->phdr.p_offset
49 && OFFS_END(ms->shdr.sh_offset, SCNFILESIZE(&ms->shdr)) <= OFFS_END(mp->phdr.p_offset, mp->phdr.p_filesz)
50 && OFFS_END(ms->shdr.sh_offset, ms->shdr.sh_size) <= OFFS_END(mp->phdr.p_offset, mp->phdr.p_memsz)) {
59 static ElfuPhdr* modelFromPhdr(GElf_Phdr *phdr)
65 mp = malloc(sizeof(ElfuPhdr));
67 ELFU_WARN("modelFromPhdr: malloc() failed for ElfuPhdr.\n");
73 CIRCLEQ_INIT(&mp->phdrToScnList);
79 static ElfuScn* modelFromSection(Elf_Scn *scn)
85 ms = malloc(sizeof(ElfuScn));
87 ELFU_WARN("modelFromSection: malloc() failed for ElfuScn.\n");
92 assert(gelf_getshdr(scn, &ms->shdr) == &ms->shdr);
95 /* Copy each data part in source segment */
97 ms->data.d_buf = NULL;
99 ms->data.d_type = ELF_T_BYTE;
100 ms->data.d_size = ms->shdr.sh_size;
101 ms->data.d_version = elf_version(EV_NONE);
102 if (ms->shdr.sh_type != SHT_NOBITS
103 && ms->shdr.sh_size > 0) {
106 ms->data.d_buf = malloc(ms->shdr.sh_size);
107 if (!ms->data.d_buf) {
108 ELFU_WARN("modelFromSection: malloc() failed for data buffer (%jx bytes).\n", ms->shdr.sh_size);
112 /* A non-empty section should contain at least one data block. */
113 data = elf_rawdata(scn, NULL);
116 ms->data.d_align = data->d_align;
117 ms->data.d_type = data->d_type;
118 ms->data.d_version = data->d_version;
121 if (data->d_off + data->d_size > ms->shdr.sh_size) {
122 ELFU_WARN("modelFromSection: libelf delivered a bogus data blob. Skipping\n");
124 memcpy(ms->data.d_buf + data->d_off, data->d_buf, data->d_size);
127 data = elf_rawdata(scn, data);
147 ElfuElf* elfu_mFromElf(Elf *e)
151 size_t i, numPhdr, numShdr;
152 ElfuScn **secArray = NULL;
155 if (elfu_eCheck(e)) {
159 me = malloc(sizeof(ElfuElf));
161 ELFU_WARN("elfu_mFromElf: malloc() failed for ElfuElf.\n");
167 CIRCLEQ_INIT(&me->scnList);
168 CIRCLEQ_INIT(&me->phdrList);
171 me->elfclass = gelf_getclass(e);
172 assert(me->elfclass != ELFCLASSNONE);
173 assert(gelf_getehdr(e, &me->ehdr) == &me->ehdr);
176 /* Get the section string table index */
177 if (elf_getshdrstrndx(e, &shstrndx) != 0) {
183 assert(!elf_getphdrnum(e, &numPhdr));
184 for (i = 0; i < numPhdr; i++) {
188 assert(gelf_getphdr(e, i, &phdr) == &phdr);
190 mp = modelFromPhdr(&phdr);
195 CIRCLEQ_INSERT_TAIL(&me->phdrList, mp, elem);
200 assert(!elf_getshdrnum(e, &numShdr));
202 secArray = malloc((numShdr - 1) * sizeof(*secArray));
204 ELFU_WARN("elfu_mFromElf: malloc() failed for secArray.\n");
208 for (i = 1; i < numShdr; i++) {
212 scn = elf_getscn(e, i);
215 ms = modelFromSection(scn);
228 /* Find sh_link dependencies */
229 for (i = 0; i < numShdr - 1; i++) {
230 ElfuScn *ms = secArray[i];
232 switch (ms->shdr.sh_type) {
235 if (ms->shdr.sh_info > 0) {
236 ms->infoptr = secArray[ms->shdr.sh_info - 1];
244 case SHT_GNU_verneed:
245 if (ms->shdr.sh_link > 0) {
246 ms->linkptr = secArray[ms->shdr.sh_link - 1];
252 /* Sort sections by file offset */
253 qsort(secArray, numShdr - 1, sizeof(*secArray), cmpScnOffs);
256 /* Find PHDR -> Section dependencies (needs sorted sections) */
257 for (i = 0; i < numShdr - 1; i++) {
258 ElfuScn *ms = secArray[i];
260 ElfuPhdr *parent = parentPhdr(me, ms);
263 CIRCLEQ_INSERT_TAIL(&parent->phdrToScnList, ms, elemPhdrToScn);
268 /* Put sections into list of all sections */
269 for (i = 0; i < numShdr - 1; i++) {
270 CIRCLEQ_INSERT_TAIL(&me->scnList, secArray[i], elem);
283 // TODO: Free data structures
286 ELFU_WARN("elfu_mFromElf: Failed to load file.\n");