]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
pe-binary: add pe_is_native() for checking if PE is native
authorLennart Poettering <lennart@poettering.net>
Fri, 5 Jul 2024 14:15:35 +0000 (16:15 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 12 Sep 2024 08:02:15 +0000 (10:02 +0200)
src/fundamental/uki.h
src/shared/pe-binary.c
src/shared/pe-binary.h

index 2fcde690d91e95f65b0756b0ffdfbde2053751eb..e7c59100e1f9ab838a13e9fd58439fe9dcc22590 100644 (file)
@@ -31,3 +31,25 @@ static inline bool unified_section_measure(UnifiedSection section) {
 
 /* Max number of profiles per UKI */
 #define UNIFIED_PROFILES_MAX 256U
+
+/* The native PE machine type, if known, for a full list see:
+ * https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#machine-types */
+#ifndef _IMAGE_FILE_MACHINE_NATIVE
+#  if defined(__x86_64__)
+#    define _IMAGE_FILE_MACHINE_NATIVE UINT16_C(0x8664)
+#  elif defined(__i386__)
+#    define _IMAGE_FILE_MACHINE_NATIVE UINT16_C(0x014c)
+#  elif defined(__ia64__)
+#    define _IMAGE_FILE_MACHINE_NATIVE UINT16_C(0x0200)
+#  elif defined(__aarch64__)
+#    define _IMAGE_FILE_MACHINE_NATIVE UINT16_C(0xaa64)
+#  elif defined(__arm__)
+#    define _IMAGE_FILE_MACHINE_NATIVE UINT16_C(0x01c0)
+#  elif defined(__riscv)
+#    if __SIZEOF_POINTER__ == 4
+#      define _IMAGE_FILE_MACHINE_NATIVE UINT16_C(0x5032)
+#    elif __SIZEOF_POINTER__ == 8
+#      define _IMAGE_FILE_MACHINE_NATIVE UINT16_C(0x5064)
+#    endif
+#  endif
+#endif
index bfeaf3f1e1ded418cd6762fd592210726bb28517..3893ffaf97798b791e559c43eaf609f7d3172353 100644 (file)
@@ -6,6 +6,7 @@
 #include "log.h"
 #include "pe-binary.h"
 #include "string-util.h"
+#include "uki.h"
 
 bool pe_header_is_64bit(const PeHeader *h) {
         assert(h);
@@ -273,3 +274,13 @@ bool pe_is_addon(const PeHeader *pe_header, const IMAGE_SECTION_HEADER *sections
                  pe_header_find_section(pe_header, sections, ".dtb") ||
                  pe_header_find_section(pe_header, sections, ".ucode"));
 }
+
+bool pe_is_native(const PeHeader *pe_header) {
+        assert(pe_header);
+
+#ifdef _IMAGE_FILE_MACHINE_NATIVE
+        return le16toh(pe_header->pe.Machine) == _IMAGE_FILE_MACHINE_NATIVE;
+#else
+        return false;
+#endif
+}
index 20f839e9a00b51118f357716293488ab5dd20274..f5089891853440523ab96610b70f306f007ae77a 100644 (file)
@@ -145,3 +145,5 @@ int pe_read_section_data_by_name(int fd, const PeHeader *pe_header, const IMAGE_
 
 bool pe_is_uki(const PeHeader *pe_header, const IMAGE_SECTION_HEADER *sections);
 bool pe_is_addon(const PeHeader *pe_header, const IMAGE_SECTION_HEADER *sections);
+
+bool pe_is_native(const PeHeader *pe_header);