4 #include <libelfu/libelfu.h>
7 static void* modelToPhdr(ElfuElf *me, ElfuPhdr *mp, void *aux1, void *aux2)
9 size_t *i = (size_t*)aux1;
12 if (!gelf_update_phdr (e, *i, &mp->phdr)) {
13 ELFU_WARNELF("gelf_update_phdr");
23 static void* modelToSection(ElfuElf *me, ElfuScn *ms, void *aux1, void *aux2)
30 scnOut = elf_newscn(e);
32 ELFU_WARNELF("elf_newscn");
39 ms->shdr.sh_link = elfu_mScnIndex(me, ms->linkptr);
42 ms->shdr.sh_info = elfu_mScnIndex(me, ms->infoptr);
44 if (!gelf_update_shdr(scnOut, &ms->shdr)) {
45 ELFU_WARNELF("gelf_update_shdr");
51 Elf_Data *dataOut = elf_newdata(scnOut);
53 ELFU_WARNELF("elf_newdata");
57 dataOut->d_buf = ms->databuf;
59 dataOut->d_type = ELF_T_BYTE;
60 dataOut->d_size = ms->shdr.sh_size;
61 dataOut->d_version = elf_version(EV_NONE);
71 void elfu_mToElf(ElfuElf *me, Elf *e)
76 elfu_mSymtabFlatten(me);
80 /* We control the ELF file's layout now. */
81 /* tired's libelf also offers ELF_F_LAYOUT_OVERLAP for overlapping sections,
82 * but we don't want that since we filtered it out in the reading stage
83 * already. It would be too mind-blowing to handle the dependencies between
84 * the PHDRs and sections then... */
85 elf_flagelf(e, ELF_C_SET, ELF_F_LAYOUT);
89 if (!gelf_newehdr(e, me->elfclass)) {
90 ELFU_WARNELF("gelf_newehdr");
94 me->ehdr.e_shstrndx = elfu_mScnIndex(me, me->shstrtab);
97 if (!gelf_update_ehdr(e, &me->ehdr)) {
98 ELFU_WARNELF("gelf_update_ehdr");
103 elfu_mScnForall(me, modelToSection, e, NULL);
107 if (!gelf_newphdr(e, elfu_mPhdrCount(me))) {
108 ELFU_WARNELF("gelf_newphdr");
111 elfu_mPhdrForall(me, modelToPhdr, &i, e);
113 elfu_eReorderPhdrs(e);
117 elf_flagelf(e, ELF_C_SET, ELF_F_DIRTY);