[PATCH 1/2] PE: Extract get_pehdr_offset() for reuse

Pingfan Liu piliu at redhat.com
Thu Dec 5 18:44:42 PST 2024


The get_pehdr_offset() function helps skip the DOS stub in a PE file to
locate the start of the PE header. Since it can be used in multiple C
files, it is being moved to a header file.

Signed-off-by: Pingfan Liu <piliu at redhat.com>
Cc: Simon Horman <horms at kernel.org>
To: kexec at lists.infradead.org
---
 include/pe.h      | 17 +++++++++++++++++
 kexec/kexec-uki.c | 17 -----------------
 2 files changed, 17 insertions(+), 17 deletions(-)

diff --git a/include/pe.h b/include/pe.h
index 2617074..1e1c59b 100644
--- a/include/pe.h
+++ b/include/pe.h
@@ -101,4 +101,21 @@ struct section_header {
 	uint32_t flags;
 };
 
+/*
+ * Return -1 if not PE, else offset of the PE header
+ */
+static int get_pehdr_offset(const char *buf)
+{
+	int pe_hdr_offset;
+
+	pe_hdr_offset = *((int *)(buf + 0x3c));
+	buf += pe_hdr_offset;
+	if (!!memcmp(buf, "PE\0\0", 4)) {
+		printf("Not a PE file\n");
+		return -1;
+	}
+
+	return pe_hdr_offset;
+}
+
 #endif
diff --git a/kexec/kexec-uki.c b/kexec/kexec-uki.c
index 235f5a1..9888d7e 100644
--- a/kexec/kexec-uki.c
+++ b/kexec/kexec-uki.c
@@ -20,23 +20,6 @@
 static int embeded_linux_format_index = -1;
 static int kernel_fd = -1;
 
-/*
- * Return -1 if not PE, else offset of the PE header
- */
-static int get_pehdr_offset(const char *buf)
-{
-	int pe_hdr_offset;
-
-	pe_hdr_offset = *((int *)(buf + 0x3c));
-	buf += pe_hdr_offset;
-	if (!!memcmp(buf, "PE\0\0", 4)) {
-		printf("Not a PE file\n");
-		return -1;
-	}
-
-	return pe_hdr_offset;
-}
-
 static int create_tmpfd(const char *template, char *buf, int buf_sz, int *tmpfd)
 {
 	char *fname;
-- 
2.41.0




More information about the kexec mailing list