2 #include <libelf/libelf.h>
3 #include <libelf/gelf.h>
4 #include <libelfu/libelfu.h>
8 static void modelToPhdrs(ElfuElf *me, Elf *e)
15 CIRCLEQ_FOREACH(mp, &me->phdrList, elem) {
19 if (!gelf_newphdr(e, i)) {
20 ELFU_WARNELF("gelf_newphdr");
25 CIRCLEQ_FOREACH(mp, &me->phdrList, elem) {
26 if (!gelf_update_phdr (e, i, &mp->phdr)) {
27 ELFU_WARNELF("gelf_update_phdr");
36 static int modelToSection(ElfuElf *me, ElfuScn *ms, void *aux1, void *aux2)
43 scnOut = elf_newscn(e);
45 ELFU_WARNELF("elf_newscn");
52 ms->shdr.sh_link = elfu_mScnIndex(me, ms->linkptr);
55 ms->shdr.sh_info = elfu_mScnIndex(me, ms->infoptr);
57 if (!gelf_update_shdr(scnOut, &ms->shdr)) {
58 ELFU_WARNELF("gelf_update_shdr");
64 Elf_Data *dataOut = elf_newdata(scnOut);
66 ELFU_WARNELF("elf_newdata");
69 dataOut->d_align = ms->data.d_align;
70 dataOut->d_buf = ms->data.d_buf;
71 dataOut->d_off = ms->data.d_off;
72 dataOut->d_type = ms->data.d_type;
73 dataOut->d_size = ms->data.d_size;
74 dataOut->d_version = ms->data.d_version;
84 void elfu_mToElf(ElfuElf *me, Elf *e)
86 /* We control the ELF file's layout now. */
87 /* tired's libelf also offers ELF_F_LAYOUT_OVERLAP for overlapping sections,
88 * but we don't want that since we filtered it out in the reading stage
89 * already. It would be too mind-blowing to handle the dependencies between
90 * the PHDRs and sections then... */
91 elf_flagelf(e, ELF_C_SET, ELF_F_LAYOUT);
95 if (!gelf_newehdr(e, me->elfclass)) {
96 ELFU_WARNELF("gelf_newehdr");
100 me->ehdr.e_shstrndx = elfu_mScnIndex(me, me->shstrtab);
103 if (!gelf_update_ehdr(e, &me->ehdr)) {
104 ELFU_WARNELF("gelf_update_ehdr");
109 elfu_mScnForall(me, modelToSection, e, NULL);
116 elf_flagelf(e, ELF_C_SET, ELF_F_DIRTY);