diff mbox series

[v3,2/9] selftests/arm: Add elf test

Message ID 20240625122408.1439097-3-dev.jain@arm.com
State New
Headers show
Series A new selftests/ directory for arm compatibility testing | expand

Commit Message

Dev Jain June 25, 2024, 12:24 p.m. UTC
This patch introduces an ELF parsing test. A basic sanity check is
required to ensure that we are actually testing a 32-bit build.

Signed-off-by: Dev Jain <dev.jain@arm.com>
---
v2->v3:
 - Introduce two more testcases

 tools/testing/selftests/arm/elf/parse_elf.c | 77 +++++++++++++++++++++
 1 file changed, 77 insertions(+)
 create mode 100644 tools/testing/selftests/arm/elf/parse_elf.c
diff mbox series

Patch

diff --git a/tools/testing/selftests/arm/elf/parse_elf.c b/tools/testing/selftests/arm/elf/parse_elf.c
new file mode 100644
index 000000000000..6eb8fb91f6a7
--- /dev/null
+++ b/tools/testing/selftests/arm/elf/parse_elf.c
@@ -0,0 +1,77 @@ 
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2024 ARM Limited
+ *
+ * Author : Dev Jain <dev.jain@arm.com>
+ *
+ * Parse elf header to confirm 32-bit process
+ */
+
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <elf.h>
+#include <stdint.h>
+
+#include <kselftest.h>
+
+/* The ELF file header.  This appears at the start of every ELF file. */
+
+struct elf_header {
+	unsigned char	e_ident[16];	/* Magic number and other info */
+	uint16_t	e_type;		/* Object file type */
+	uint16_t	e_machine;	/* Architecture */
+	uint32_t	e_version;	/* Object file version */
+	uint32_t	e_entry;	/* Entry point virtual address */
+	uint32_t	e_phoff;	/* Program header table file offset */
+	uint32_t	e_shoff;	/* Section header table file offset */
+	uint32_t	e_flags;	/* Processor-specific flags */
+	uint16_t	e_ehsize;	/* ELF header size in bytes */
+	uint16_t	e_phentsize;	/* Program header table entry size */
+	uint16_t	e_phnum;	/* Program header table entry count */
+	uint16_t	e_shentsize;	/* Section header table entry size */
+	uint16_t	e_shnum;	/* Section header table entry count */
+	uint16_t	e_shstrndx;	/* Section header string table index */
+};
+
+#define ELFCLASS32	1
+#define EM_ARM		40
+
+void read_elf_header(const char  *elfFile)
+{
+	struct elf_header header;
+	FILE *file;
+	int ret;
+
+	file = fopen(elfFile, "r");
+	if (!file)
+		ksft_exit_fail_perror("/proc/self/exe");
+
+	/* store header in struct */
+	if (fread(&header, 1, sizeof(header), file) != sizeof(header))
+		ksft_exit_fail_perror("fread");
+
+	if (fclose(file))
+		ksft_exit_fail_perror("fclose");
+
+	/* sanity check: does it really follow ELF format */
+	ret = (header.e_ident[0] == 0x7f && header.e_ident[1] == 'E'
+	       && header.e_ident[2] == 'L' && header.e_ident[3] == 'F');
+
+	ksft_test_result(ret == 1, "Follows ELF format\n");
+
+	ksft_test_result(header.e_ident[4] == ELFCLASS32, "ELF is 32 bit\n");
+
+	ksft_test_result(header.e_machine == EM_ARM, "Machine type\n");
+}
+
+int main(int argc, char *argv[])
+{
+	ksft_print_header();
+	ksft_set_plan(3);
+
+	read_elf_header("/proc/self/exe");
+
+	ksft_finished();
+}