[PATCH 1/x] ELF: Move some ELF functions from util.[ch] to elfops.[ch]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Also move 32/64-bit X-macros (e.g PERBIT) from toplevel
elfops.c into elf_core.c as these are private.

Signed-off-by: Andreas Robinson <andr345@xxxxxxxxx>
---
 Makefile.am |    4 ++--
 depmod.c    |    1 +
 elf_core.c  |   19 +++++++++++++++++++
 elfops.c    |   52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 elfops.h    |   13 +++++++++++++
 modinfo.c   |    1 +
 modprobe.c  |    1 +
 moduleops.c |    1 +
 util.c      |   46 +---------------------------------------------
 util.h      |    8 --------
 10 files changed, 91 insertions(+), 55 deletions(-)
 create mode 100644 elfops.c
 create mode 100644 elfops.h

diff --git a/Makefile.am b/Makefile.am
index e5bb4c7..82ccf28 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -22,8 +22,8 @@ EXTRA_insmod_static_SOURCES =
 EXTRA_depmod_SOURCES = moduleops_core.c
 EXTRA_modinfo_SOURCES =
 
-libmodtools_a_SOURCES = util.c logging.c index.c config_filter.c \
-	util.h depmod.h logging.h index.h list.h config_filter.h
+libmodtools_a_SOURCES = util.c logging.c index.c config_filter.c elfops.c
+	util.h depmod.h logging.h index.h list.h config_filter.h elfops.h
 libmodtools_a_CFLAGS = -ffunction-sections
 
 EXTRA_libmodtools_a_SOURCES = elf_core.c
diff --git a/depmod.c b/depmod.c
index 3544b89..721f471 100644
--- a/depmod.c
+++ b/depmod.c
@@ -26,6 +26,7 @@
 #include "depmod.h"
 #include "logging.h"
 #include "index.h"
+#include "elfops.h"
 #include "moduleops.h"
 #include "tables.h"
 #include "config_filter.h"
diff --git a/elf_core.c b/elf_core.c
index 1525c36..5280b5d 100644
--- a/elf_core.c
+++ b/elf_core.c
@@ -1,3 +1,19 @@
+#if defined(ELF32BIT)
+
+#define PERBIT(x) x##32
+#define ElfPERBIT(x) Elf32_##x
+#define ELFPERBIT(x) ELF32_##x
+
+#elif defined(ELF64BIT)
+
+#define PERBIT(x) x##64
+#define ElfPERBIT(x) Elf64_##x
+#define ELFPERBIT(x) ELF64_##x
+
+#else
+#  error "Undefined ELF word length"
+#endif
+
 void *PERBIT(get_section)(void *file,
 			  unsigned long fsize,
 			  const char *secname,
@@ -41,3 +57,6 @@ void *PERBIT(get_section)(void *file,
 	return NULL;
 }
 
+#undef PERBIT
+#undef ElfPERBIT
+#undef ELFPERBIT
diff --git a/elfops.c b/elfops.c
new file mode 100644
index 0000000..550266c
--- /dev/null
+++ b/elfops.c
@@ -0,0 +1,52 @@
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <elf.h>
+#include <errno.h>
+#include <fcntl.h>
+
+#include "util.h"
+#include "logging.h"
+#include "zlibsupport.h"
+#include "elfops.h"
+
+#define ELF32BIT
+#include "elf_core.c"
+#undef ELF32BIT
+
+#define ELF64BIT
+#include "elf_core.c"
+#undef ELF64BIT
+
+/*
+ * Check ELF file header.
+ */
+int elf_ident(void *file, unsigned long fsize, int *conv)
+{
+	/* "\177ELF" <byte> where byte = 001 for 32-bit, 002 for 64 */
+	unsigned char *ident = file;
+
+	if (fsize < EI_CLASS || memcmp(file, ELFMAG, SELFMAG) != 0)
+		return -ENOEXEC;	/* Not an ELF object */
+	if (ident[EI_DATA] == 0 || ident[EI_DATA] > 2)
+		return -EINVAL;		/* Unknown endianness */
+
+	if (conv != NULL)
+		*conv = native_endianness() != ident[EI_DATA];
+	return ident[EI_CLASS];
+}
+
+void *get_section(void *file, unsigned long filesize,
+		  const char *secname, unsigned long *secsize)
+{
+	int conv;
+
+	switch (elf_ident(file, filesize, &conv)) {
+	case ELFCLASS32:
+		return get_section32(file, filesize, secname, secsize, conv);
+	case ELFCLASS64:
+		return get_section64(file, filesize, secname, secsize, conv);
+	default:
+		return NULL;
+	}
+}
diff --git a/elfops.h b/elfops.h
new file mode 100644
index 0000000..41ea8b4
--- /dev/null
+++ b/elfops.h
@@ -0,0 +1,13 @@
+#ifndef MODINITTOOLS_ELFOPS_H
+#define MODINITTOOLS_ELFOPS_H
+
+int elf_ident(void *file, unsigned long fsize, int *conv);
+void *get_section(void *file, unsigned long filesize,
+	const char *secname, unsigned long *secsize);
+void *get_section32(void *file, unsigned long filesize,
+	const char *secname, unsigned long *secsize, int conv);
+void *get_section64(void *file, unsigned long filesize,
+	const char *secname, unsigned long *secsize, int conv);
+
+#endif /* MODINITTOOLS_ELFOPS_H */
+
diff --git a/modinfo.c b/modinfo.c
index 07199c2..4f16c78 100644
--- a/modinfo.c
+++ b/modinfo.c
@@ -14,6 +14,7 @@
 #include <sys/mman.h>
 
 #include "util.h"
+#include "elfops.h"
 #include "zlibsupport.h"
 #include "testing.h"
 
diff --git a/modprobe.c b/modprobe.c
index c2d0ef6..c0680a7 100644
--- a/modprobe.c
+++ b/modprobe.c
@@ -40,6 +40,7 @@
 #include <syslog.h>
 
 #include "util.h"
+#include "elfops.h"
 #include "zlibsupport.h"
 #include "logging.h"
 #include "index.h"
diff --git a/moduleops.c b/moduleops.c
index 1ee52be..fe84c0d 100644
--- a/moduleops.c
+++ b/moduleops.c
@@ -8,6 +8,7 @@
 #include "depmod.h"
 #include "util.h"
 #include "logging.h"
+#include "elfops.h"
 #include "moduleops.h"
 #include "tables.h"
 
diff --git a/util.c b/util.c
index 482363b..54eb985 100644
--- a/util.c
+++ b/util.c
@@ -6,6 +6,7 @@
 #include <elf.h>
 #include "logging.h"
 #include "util.h"
+#include "elfops.h"
 
 /*
  * Read one logical line from a configuration file.
@@ -157,48 +158,3 @@ int __attribute__ ((pure)) native_endianness()
 	return (char) *((uint32_t*)("\1\0\0\2"));
 }
 
-/*
- * Check ELF file header.
- */
-int elf_ident(void *file, unsigned long fsize, int *conv)
-{
-	/* "\177ELF" <byte> where byte = 001 for 32-bit, 002 for 64 */
-	unsigned char *ident = file;
-
-	if (fsize < EI_CLASS || memcmp(file, ELFMAG, SELFMAG) != 0)
-		return -ENOEXEC;	/* Not an ELF object */
-	if (ident[EI_DATA] == 0 || ident[EI_DATA] > 2)
-		return -EINVAL;		/* Unknown endianness */
-
-	if (conv != NULL)
-		*conv = native_endianness() != ident[EI_DATA];
-	return ident[EI_CLASS];
-}
-
-#define PERBIT(x) x##32
-#define ElfPERBIT(x) Elf32_##x
-#define ELFPERBIT(x) ELF32_##x
-#include "elf_core.c"
-
-#undef PERBIT
-#undef ElfPERBIT
-#undef ELFPERBIT
-#define PERBIT(x) x##64
-#define ElfPERBIT(x) Elf64_##x
-#define ELFPERBIT(x) ELF64_##x
-#include "elf_core.c"
-
-void *get_section(void *file, unsigned long filesize,
-		  const char *secname, unsigned long *secsize)
-{
-	int conv;
-
-	switch (elf_ident(file, filesize, &conv)) {
-	case ELFCLASS32:
-		return get_section32(file, filesize, secname, secsize, conv);
-	case ELFCLASS64:
-		return get_section64(file, filesize, secname, secsize, conv);
-	default:
-		return NULL;
-	}
-}
diff --git a/util.h b/util.h
index fbda299..cbaab72 100644
--- a/util.h
+++ b/util.h
@@ -31,14 +31,6 @@ static inline void __swap_bytes(const void *src, void *dest, unsigned int size)
 
 int native_endianness(void);
 
-int elf_ident(void *file, unsigned long fsize, int *conv);
-void *get_section(void *file, unsigned long filesize,
-	const char *secname, unsigned long *secsize);
-void *get_section32(void *file, unsigned long filesize,
-	const char *secname, unsigned long *secsize, int conv);
-void *get_section64(void *file, unsigned long filesize,
-	const char *secname, unsigned long *secsize, int conv);
-
 #define streq(a,b) (strcmp((a),(b)) == 0)
 #define strstarts(a,start) (strncmp((a),(start), strlen(start)) == 0)
 
-- 
1.6.0.4

--
To unsubscribe from this list: send the line "unsubscribe linux-modules" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [Big List of Linux Books]

  Powered by Linux