1 /* This file is part of centaur.
3 * centaur is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License 2 as
5 * published by the Free Software Foundation.
7 * centaur is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with centaur. If not, see <http://www.gnu.org/licenses/>.
19 #include <libelfu/libelfu.h>
22 static void* modelToPhdr(ElfuElf *me, ElfuPhdr *mp, void *aux1, void *aux2)
24 size_t *i = (size_t*)aux1;
27 if (!gelf_update_phdr (e, *i, &mp->phdr)) {
28 ELFU_WARNELF("gelf_update_phdr");
38 static void* modelToSection(ElfuElf *me, ElfuScn *ms, void *aux1, void *aux2)
45 scnOut = elf_newscn(e);
47 ELFU_WARNELF("elf_newscn");
54 ms->shdr.sh_link = elfu_mScnIndex(me, ms->linkptr);
57 ms->shdr.sh_info = elfu_mScnIndex(me, ms->infoptr);
59 if (!gelf_update_shdr(scnOut, &ms->shdr)) {
60 ELFU_WARNELF("gelf_update_shdr");
66 Elf_Data *dataOut = elf_newdata(scnOut);
68 ELFU_WARNELF("elf_newdata");
72 dataOut->d_buf = ms->databuf;
74 dataOut->d_type = ELF_T_BYTE;
75 dataOut->d_size = ms->shdr.sh_size;
76 dataOut->d_version = elf_version(EV_NONE);
86 void elfu_mToElf(ElfuElf *me, Elf *e)
91 elfu_mSymtabFlatten(me);
95 /* We control the ELF file's layout now. */
96 /* tired's libelf also offers ELF_F_LAYOUT_OVERLAP for overlapping sections,
97 * but we don't want that since we filtered it out in the reading stage
98 * already. It would be too mind-blowing to handle the dependencies between
99 * the PHDRs and sections then... */
100 elf_flagelf(e, ELF_C_SET, ELF_F_LAYOUT);
104 if (!gelf_newehdr(e, me->elfclass)) {
105 ELFU_WARNELF("gelf_newehdr");
109 me->ehdr.e_shstrndx = elfu_mScnIndex(me, me->shstrtab);
112 if (!gelf_update_ehdr(e, &me->ehdr)) {
113 ELFU_WARNELF("gelf_update_ehdr");
118 elfu_mScnForall(me, modelToSection, e, NULL);
122 if (!gelf_newphdr(e, elfu_mPhdrCount(me))) {
123 ELFU_WARNELF("gelf_newphdr");
126 elfu_mPhdrForall(me, modelToPhdr, &i, e);
128 elfu_eReorderPhdrs(e);
132 elf_flagelf(e, ELF_C_SET, ELF_F_DIRTY);