Cleaner error handling with ELFU_WARN and ELFU_WARNELF
[centaur.git] / src / elfops / phdr-fixup-selfref.c
1 #include <libelf/libelf.h>
2 #include <libelf/gelf.h>
3
4 #include <libelfu/debug.h>
5
6
7 void elfu_ePhdrFixupSelfRef(Elf *e)
8 {
9   GElf_Ehdr ehdr;
10   size_t i, n;
11
12   if (!gelf_getehdr(e, &ehdr)) {
13     ELFU_WARNELF("gelf_getehdr");
14     return;
15   }
16
17   if (elf_getphdrnum(e, &n)) {
18     ELFU_WARNELF("elf_getphdrnum");
19   }
20
21   for (i = 0; i < n; i++) {
22     GElf_Phdr phdr;
23
24     if (gelf_getphdr(e, i, &phdr) != &phdr) {
25       ELFU_WARN("gelf_getphdr() failed for #%d: %s\n", i, elf_errmsg(-1));
26       continue;
27     }
28
29     if (phdr.p_type == PT_PHDR) {
30       phdr.p_offset = ehdr.e_phoff;
31       phdr.p_filesz = elf32_fsize(ELF_T_PHDR, n, EV_CURRENT);
32       phdr.p_memsz = phdr.p_filesz;
33
34       if (!gelf_update_phdr (e, i, &phdr)) {
35         ELFU_WARNELF("gelf_update_ehdr");
36       }
37     }
38   }
39
40   /* Tell libelf that phdrs have changed */
41   elf_flagphdr(e, ELF_C_SET, ELF_F_DIRTY);
42 }