PHDR addition. Should make x86-64 work in many cases.
[centaur.git] / include / libelfu / generic.h
index f5b0e0f030e7ccba0c8e3746131b15a042f33217..340719cc562c3386d8f5905a7ac9ae63421f988e 100644 (file)
@@ -1,11 +1,38 @@
 #ifndef __LIBELFU_GENERIC_H__
 #define __LIBELFU_GENERIC_H__
 
-#include <libelf/gelf.h>
+#include <gelf.h>
 
 
-size_t elfu_gScnSizeFile(const GElf_Shdr *shdr);
+#define MIN(x, y) ((x) < (y) ? (x) : (y))
+#define ROUNDUP(x, align) ((x) + ((align) - ((x) % (align))) % (align))
+
+
+#define OFFS_END(off, sz) ((off) + (sz))
+
+#define OVERLAPPING(off1, sz1, off2, sz2) \
+  (!((off1) == (off2) && ((sz1 == 0) || (sz2 == 0))) \
+   && (((off1) <= (off2) && (off2) < OFFS_END((off1), (sz1))) \
+       || ((off2) <= (off1) && (off1) < OFFS_END((off2), (sz2)))) \
+  )
+
+#define FULLY_OVERLAPPING(off1, sz1, off2, sz2) \
+  (((off1) <= (off2) && OFFS_END((off2), (sz2)) <= OFFS_END((off1), (sz1))) \
+   || ((off2) <= (off1) && OFFS_END((off1), (sz1)) <= OFFS_END((off2), (sz2))))
+
+
+
+#define SCNFILESIZE(shdr) ((shdr)->sh_type == SHT_NOBITS ? 0 : (shdr)->sh_size)
+
+
+
+#define PHDR_CONTAINS_SCN_IN_MEMORY(phdr, shdr) \
+  (((phdr)->p_vaddr) <= ((shdr)->sh_addr) \
+   && OFFS_END((shdr)->sh_addr, (shdr)->sh_size) <= OFFS_END((phdr)->p_vaddr, (phdr)->p_memsz))
+
+#define PHDR_CONTAINS_SCN_IN_FILE(phdr, shdr) \
+  (((phdr)->p_offset) <= ((shdr)->sh_offset) \
+   && OFFS_END((shdr)->sh_offset, SCNFILESIZE(shdr)) <= OFFS_END((phdr)->p_offset, (phdr)->p_filesz))
 
-int elfu_gPhdrContainsScn(GElf_Phdr *phdr, GElf_Shdr *shdr);
 
 #endif