+++ /dev/null
-#include <libelf.h>
-#include <gelf.h>
-
-#include <libelfu/libelfu.h>
-
-
-int elfu_segmentContainsSection(GElf_Phdr *phdr, GElf_Shdr *shdr)
-{
- size_t secStart = shdr->sh_offset;
- size_t secEnd = shdr->sh_offset + shdr->sh_size;
- size_t segStart = phdr->p_offset;
- size_t segEnd = phdr->p_offset + phdr->p_memsz;
-
- if (secStart < segStart || secEnd > segEnd) {
- return 0;
- }
-
- return 1;
-}
--- /dev/null
+#include <stdlib.h>
+
+#include <libelf.h>
+#include <gelf.h>
+
+#include <libelfu/libelfu.h>
+
+
+/*
+ * Returns the section that starts at the same point in the file as
+ * the segment AND is wholly contained in the memory image.
+ *
+ * If no section fits, NULL is returned.
+ */
+Elf_Scn* elfu_firstSectionInSegment(Elf *e, GElf_Phdr *phdr)
+{
+ Elf_Scn *scn;
+
+ scn = elf_getscn(e, 1);
+ while (scn) {
+ GElf_Shdr shdr;
+
+ if (gelf_getshdr(scn, &shdr) != &shdr) {
+ return NULL;
+ }
+
+ if (shdr.sh_offset == phdr->p_offset
+ && elfu_segmentContainsSection(phdr, &shdr)) {
+ return scn;
+ }
+
+ scn = elf_nextscn(e, scn);
+ }
+
+ return NULL;
+}
--- /dev/null
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <libelf.h>
+#include <gelf.h>
+
+#include <libelfu/libelfu.h>
+
+
+/*
+ * Returns the first section that is contained in the segment and
+ * ends as close to its memory image of as possible (the "last"
+ * section in the segment).
+ *
+ * If no section fits, NULL is returned.
+ */
+Elf_Scn* elfu_lastSectionInSegment(Elf *e, GElf_Phdr *phdr)
+{
+ Elf_Scn *last = NULL;
+ Elf_Scn *scn;
+
+
+ scn = elf_getscn(e, 1);
+ while (scn) {
+ GElf_Shdr shdr;
+
+ if (gelf_getshdr(scn, &shdr) != &shdr) {
+ fprintf(stderr, "gelf_getshdr() failed: %s\n", elf_errmsg(-1));
+ continue;
+ }
+
+ if (elfu_segmentContainsSection(phdr, &shdr)) {
+ if (!last) {
+ last = scn;
+ } else {
+ GElf_Shdr shdrOld;
+
+ if (gelf_getshdr(last, &shdrOld) != &shdrOld) {
+ continue;
+ }
+
+ if (shdr.sh_offset + shdr.sh_size
+ > shdrOld.sh_offset + shdrOld.sh_size) {
+ // TODO: Check (leftover space in memory image) < (p_align)
+ last = scn;
+ }
+ }
+ }
+
+ scn = elf_nextscn(e, scn);
+ }
+
+ return last;
+}
--- /dev/null
+#include <stdio.h>
+
+#include <libelf.h>
+#include <gelf.h>
+
+void elfu_fixupPhdrSelfRef(Elf *e)
+{
+ GElf_Ehdr ehdr;
+ size_t i, n;
+
+ if (!gelf_getehdr(e, &ehdr)) {
+ fprintf(stderr, "gelf_getehdr() failed: %s.", elf_errmsg(-1));
+ return;
+ }
+
+ if (elf_getphdrnum(e, &n)) {
+ fprintf(stderr, "elf_getphdrnum() failed: %s\n", elf_errmsg(-1));
+ }
+
+ for (i = 0; i < n; i++) {
+ GElf_Phdr phdr;
+
+ if (gelf_getphdr(e, i, &phdr) != &phdr) {
+ fprintf(stderr, "gelf_getphdr() failed for #%d: %s\n", i, elf_errmsg(-1));
+ continue;
+ }
+
+ if (phdr.p_type == PT_PHDR) {
+ phdr.p_offset = ehdr.e_phoff;
+ phdr.p_filesz = elf32_fsize(ELF_T_PHDR, n, EV_CURRENT);
+ phdr.p_memsz = phdr.p_filesz;
+
+ if (!gelf_update_phdr (e, i, &phdr)) {
+ fprintf(stderr, "gelf_update_ehdr() failed: %s\n", elf_errmsg(-1));
+ }
+ }
+ }
+
+ /* Tell libelf that phdrs have changed */
+ elf_flagphdr(e, ELF_C_SET, ELF_F_DIRTY);
+}
--- /dev/null
+#include <assert.h>
+#include <sys/types.h>
+#include <gelf.h>
+#include <libelfu/libelfu.h>
+
+
+
+size_t elfu_scnSizeFile(const GElf_Shdr *shdr)
+{
+ assert(shdr);
+
+ return shdr->sh_type == SHT_NOBITS ? 0 : shdr->sh_size;
+}
--- /dev/null
+#include <string.h>
+
+#include <libelf.h>
+#include <gelf.h>
+
+#include <libelfu/libelfu.h>
+
+
+Elf_Scn* elfu_sectionByName(Elf *e, char *name)
+{
+ size_t shstrndx;
+ Elf_Scn *scn;
+
+ if (elf_getshdrstrndx(e, &shstrndx) != 0) {
+ return NULL;
+ }
+
+ scn = elf_getscn(e, 1);
+ while (scn) {
+ GElf_Shdr shdr;
+ char *curname;
+
+ if (gelf_getshdr(scn, &shdr) != &shdr) {
+ return NULL;
+ }
+
+ /* elf_strptr returns NULL if there was an error */
+ curname = elf_strptr(e, shstrndx, shdr.sh_name);
+
+ /* strcmp... but we really have no bounds on the lengths here */
+ if (!strcmp(curname, name)) {
+ return scn;
+ }
+
+ scn = elf_nextscn(e, scn);
+ }
+
+ return NULL;
+}
--- /dev/null
+#include <stdlib.h>
+
+#include <libelf.h>
+#include <gelf.h>
+
+#include <libelfu/libelfu.h>
+
+
+char* elfu_sectionName(Elf *e, Elf_Scn *scn)
+{
+ size_t shstrndx;
+ GElf_Shdr shdr;
+
+ if (elf_getshdrstrndx(e, &shstrndx) != 0) {
+ return NULL;
+ }
+
+ if (gelf_getshdr(scn, &shdr) != &shdr) {
+ return NULL;
+ }
+
+ /* elf_strptr returns NULL if there was an error */
+ return elf_strptr(e, shstrndx, shdr.sh_name);
+}
--- /dev/null
+#include <libelf.h>
+#include <gelf.h>
+
+#include <libelfu/libelfu.h>
+
+
+int elfu_segmentContainsSection(GElf_Phdr *phdr, GElf_Shdr *shdr)
+{
+ size_t secStart = shdr->sh_offset;
+ size_t secEnd = shdr->sh_offset + shdr->sh_size;
+ size_t segStart = phdr->p_offset;
+ size_t segEnd = phdr->p_offset + phdr->p_memsz;
+
+ if (secStart < segStart || secEnd > segEnd) {
+ return 0;
+ }
+
+ return 1;
+}
+++ /dev/null
-#include <stdio.h>
-
-#include <libelf.h>
-#include <gelf.h>
-
-void elfu_fixupPhdrSelfRef(Elf *e)
-{
- GElf_Ehdr ehdr;
- size_t i, n;
-
- if (!gelf_getehdr(e, &ehdr)) {
- fprintf(stderr, "gelf_getehdr() failed: %s.", elf_errmsg(-1));
- return;
- }
-
- if (elf_getphdrnum(e, &n)) {
- fprintf(stderr, "elf_getphdrnum() failed: %s\n", elf_errmsg(-1));
- }
-
- for (i = 0; i < n; i++) {
- GElf_Phdr phdr;
-
- if (gelf_getphdr(e, i, &phdr) != &phdr) {
- fprintf(stderr, "gelf_getphdr() failed for #%d: %s\n", i, elf_errmsg(-1));
- continue;
- }
-
- if (phdr.p_type == PT_PHDR) {
- phdr.p_offset = ehdr.e_phoff;
- phdr.p_filesz = elf32_fsize(ELF_T_PHDR, n, EV_CURRENT);
- phdr.p_memsz = phdr.p_filesz;
-
- if (!gelf_update_phdr (e, i, &phdr)) {
- fprintf(stderr, "gelf_update_ehdr() failed: %s\n", elf_errmsg(-1));
- }
- }
- }
-
- /* Tell libelf that phdrs have changed */
- elf_flagphdr(e, ELF_C_SET, ELF_F_DIRTY);
-}
+++ /dev/null
-#include <stdlib.h>
-
-#include <libelf.h>
-#include <gelf.h>
-
-#include <libelfu/libelfu.h>
-
-
-/*
- * Returns the section that starts at the same point in the file as
- * the segment AND is wholly contained in the memory image.
- *
- * If no section fits, NULL is returned.
- */
-Elf_Scn* elfu_firstSectionInSegment(Elf *e, GElf_Phdr *phdr)
-{
- Elf_Scn *scn;
-
- scn = elf_getscn(e, 1);
- while (scn) {
- GElf_Shdr shdr;
-
- if (gelf_getshdr(scn, &shdr) != &shdr) {
- return NULL;
- }
-
- if (shdr.sh_offset == phdr->p_offset
- && elfu_segmentContainsSection(phdr, &shdr)) {
- return scn;
- }
-
- scn = elf_nextscn(e, scn);
- }
-
- return NULL;
-}
+++ /dev/null
-#include <stdio.h>
-#include <stdlib.h>
-
-#include <libelf.h>
-#include <gelf.h>
-
-#include <libelfu/libelfu.h>
-
-
-/*
- * Returns the first section that is contained in the segment and
- * ends as close to its memory image of as possible (the "last"
- * section in the segment).
- *
- * If no section fits, NULL is returned.
- */
-Elf_Scn* elfu_lastSectionInSegment(Elf *e, GElf_Phdr *phdr)
-{
- Elf_Scn *last = NULL;
- Elf_Scn *scn;
-
-
- scn = elf_getscn(e, 1);
- while (scn) {
- GElf_Shdr shdr;
-
- if (gelf_getshdr(scn, &shdr) != &shdr) {
- fprintf(stderr, "gelf_getshdr() failed: %s\n", elf_errmsg(-1));
- continue;
- }
-
- if (elfu_segmentContainsSection(phdr, &shdr)) {
- if (!last) {
- last = scn;
- } else {
- GElf_Shdr shdrOld;
-
- if (gelf_getshdr(last, &shdrOld) != &shdrOld) {
- continue;
- }
-
- if (shdr.sh_offset + shdr.sh_size
- > shdrOld.sh_offset + shdrOld.sh_size) {
- // TODO: Check (leftover space in memory image) < (p_align)
- last = scn;
- }
- }
- }
-
- scn = elf_nextscn(e, scn);
- }
-
- return last;
-}
+++ /dev/null
-#include <assert.h>
-#include <sys/types.h>
-#include <gelf.h>
-#include <libelfu/libelfu.h>
-
-
-
-size_t elfu_scnSizeFile(const GElf_Shdr *shdr)
-{
- assert(shdr);
-
- return shdr->sh_type == SHT_NOBITS ? 0 : shdr->sh_size;
-}
+++ /dev/null
-#include <string.h>
-
-#include <libelf.h>
-#include <gelf.h>
-
-#include <libelfu/libelfu.h>
-
-
-Elf_Scn* elfu_sectionByName(Elf *e, char *name)
-{
- size_t shstrndx;
- Elf_Scn *scn;
-
- if (elf_getshdrstrndx(e, &shstrndx) != 0) {
- return NULL;
- }
-
- scn = elf_getscn(e, 1);
- while (scn) {
- GElf_Shdr shdr;
- char *curname;
-
- if (gelf_getshdr(scn, &shdr) != &shdr) {
- return NULL;
- }
-
- /* elf_strptr returns NULL if there was an error */
- curname = elf_strptr(e, shstrndx, shdr.sh_name);
-
- /* strcmp... but we really have no bounds on the lengths here */
- if (!strcmp(curname, name)) {
- return scn;
- }
-
- scn = elf_nextscn(e, scn);
- }
-
- return NULL;
-}
+++ /dev/null
-#include <stdlib.h>
-
-#include <libelf.h>
-#include <gelf.h>
-
-#include <libelfu/libelfu.h>
-
-
-char* elfu_sectionName(Elf *e, Elf_Scn *scn)
-{
- size_t shstrndx;
- GElf_Shdr shdr;
-
- if (elf_getshdrstrndx(e, &shstrndx) != 0) {
- return NULL;
- }
-
- if (gelf_getshdr(scn, &shdr) != &shdr) {
- return NULL;
- }
-
- /* elf_strptr returns NULL if there was an error */
- return elf_strptr(e, shstrndx, shdr.sh_name);
-}