summaryrefslogtreecommitdiff
path: root/src/elfops
diff options
context:
space:
mode:
authornorly <ny-git@enpas.org>2013-02-23 15:41:35 +0000
committernorly <ny-git@enpas.org>2013-02-23 15:42:42 +0000
commit5b7078265bb864ddec23162b2dfc5c02dacb0e1d (patch)
treef8973076c5f93c0e8e31d432b32a808c72bdb8db /src/elfops
parent6f12fafdc4102a9b8c3ffee060cab48f50f4c37d (diff)
Move ELF-related files together
Diffstat (limited to 'src/elfops')
-rw-r--r--src/elfops/first-section-in-segment.c36
-rw-r--r--src/elfops/last-section-in-segment.c54
-rw-r--r--src/elfops/phdr.c41
-rw-r--r--src/elfops/scnSize.c13
-rw-r--r--src/elfops/section-by-name.c39
-rw-r--r--src/elfops/section-name.c24
-rw-r--r--src/elfops/segment-contains-section.c19
7 files changed, 226 insertions, 0 deletions
diff --git a/src/elfops/first-section-in-segment.c b/src/elfops/first-section-in-segment.c
new file mode 100644
index 0000000..58065a5
--- /dev/null
+++ b/src/elfops/first-section-in-segment.c
@@ -0,0 +1,36 @@
+#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;
+}
diff --git a/src/elfops/last-section-in-segment.c b/src/elfops/last-section-in-segment.c
new file mode 100644
index 0000000..53323f6
--- /dev/null
+++ b/src/elfops/last-section-in-segment.c
@@ -0,0 +1,54 @@
+#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;
+}
diff --git a/src/elfops/phdr.c b/src/elfops/phdr.c
new file mode 100644
index 0000000..ccdc021
--- /dev/null
+++ b/src/elfops/phdr.c
@@ -0,0 +1,41 @@
+#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);
+}
diff --git a/src/elfops/scnSize.c b/src/elfops/scnSize.c
new file mode 100644
index 0000000..2c65ca1
--- /dev/null
+++ b/src/elfops/scnSize.c
@@ -0,0 +1,13 @@
+#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;
+}
diff --git a/src/elfops/section-by-name.c b/src/elfops/section-by-name.c
new file mode 100644
index 0000000..1697140
--- /dev/null
+++ b/src/elfops/section-by-name.c
@@ -0,0 +1,39 @@
+#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;
+}
diff --git a/src/elfops/section-name.c b/src/elfops/section-name.c
new file mode 100644
index 0000000..1365eb6
--- /dev/null
+++ b/src/elfops/section-name.c
@@ -0,0 +1,24 @@
+#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);
+}
diff --git a/src/elfops/segment-contains-section.c b/src/elfops/segment-contains-section.c
new file mode 100644
index 0000000..298cf72
--- /dev/null
+++ b/src/elfops/segment-contains-section.c
@@ -0,0 +1,19 @@
+#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;
+}