summaryrefslogtreecommitdiff
path: root/src/lookup
diff options
context:
space:
mode:
Diffstat (limited to 'src/lookup')
-rw-r--r--src/lookup/first-section-in-segment.c34
-rw-r--r--src/lookup/last-section-in-segment.c49
-rw-r--r--src/lookup/section-by-name.c39
-rw-r--r--src/lookup/section-name.c22
4 files changed, 144 insertions, 0 deletions
diff --git a/src/lookup/first-section-in-segment.c b/src/lookup/first-section-in-segment.c
new file mode 100644
index 0000000..b66c518
--- /dev/null
+++ b/src/lookup/first-section-in-segment.c
@@ -0,0 +1,34 @@
+#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, scn) == ELFU_TRUE) {
+ return scn;
+ }
+
+ scn = elf_nextscn(e, scn);
+ }
+
+ return NULL;
+}
diff --git a/src/lookup/last-section-in-segment.c b/src/lookup/last-section-in-segment.c
new file mode 100644
index 0000000..0e78263
--- /dev/null
+++ b/src/lookup/last-section-in-segment.c
@@ -0,0 +1,49 @@
+#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) {
+ if (elfu_segmentContainsSection(phdr, scn) == ELFU_TRUE) {
+ if (!last) {
+ last = scn;
+ } else {
+ GElf_Shdr shdrOld;
+ GElf_Shdr shdrNew;
+
+ if (gelf_getshdr(last, &shdrOld) != &shdrOld) {
+ continue;
+ }
+
+ if (gelf_getshdr(scn, &shdrNew) != &shdrNew) {
+ continue;
+ }
+
+ if (shdrNew.sh_offset + shdrNew.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/lookup/section-by-name.c b/src/lookup/section-by-name.c
new file mode 100644
index 0000000..1697140
--- /dev/null
+++ b/src/lookup/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/lookup/section-name.c b/src/lookup/section-name.c
new file mode 100644
index 0000000..d3748f9
--- /dev/null
+++ b/src/lookup/section-name.c
@@ -0,0 +1,22 @@
+#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);
+}