xref: /openbmc/linux/arch/mips/tools/elf-entry.c (revision 6d99a79c)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <byteswap.h>
3 #include <elf.h>
4 #include <endian.h>
5 #include <inttypes.h>
6 #include <stdint.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 
11 #ifdef be32toh
12 /* If libc provides [bl]e{32,64}toh() then we'll use them */
13 #elif BYTE_ORDER == LITTLE_ENDIAN
14 # define be32toh(x)	bswap_32(x)
15 # define le32toh(x)	(x)
16 # define be64toh(x)	bswap_64(x)
17 # define le64toh(x)	(x)
18 #elif BYTE_ORDER == BIG_ENDIAN
19 # define be32toh(x)	(x)
20 # define le32toh(x)	bswap_32(x)
21 # define be64toh(x)	(x)
22 # define le64toh(x)	bswap_64(x)
23 #endif
24 
25 __attribute__((noreturn))
26 static void die(const char *msg)
27 {
28 	fputs(msg, stderr);
29 	exit(EXIT_FAILURE);
30 }
31 
32 int main(int argc, const char *argv[])
33 {
34 	uint64_t entry;
35 	size_t nread;
36 	FILE *file;
37 	union {
38 		Elf32_Ehdr ehdr32;
39 		Elf64_Ehdr ehdr64;
40 	} hdr;
41 
42 	if (argc != 2)
43 		die("Usage: elf-entry <elf-file>\n");
44 
45 	file = fopen(argv[1], "r");
46 	if (!file) {
47 		perror("Unable to open input file");
48 		return EXIT_FAILURE;
49 	}
50 
51 	nread = fread(&hdr, 1, sizeof(hdr), file);
52 	if (nread != sizeof(hdr)) {
53 		perror("Unable to read input file");
54 		return EXIT_FAILURE;
55 	}
56 
57 	if (memcmp(hdr.ehdr32.e_ident, ELFMAG, SELFMAG))
58 		die("Input is not an ELF\n");
59 
60 	switch (hdr.ehdr32.e_ident[EI_CLASS]) {
61 	case ELFCLASS32:
62 		switch (hdr.ehdr32.e_ident[EI_DATA]) {
63 		case ELFDATA2LSB:
64 			entry = le32toh(hdr.ehdr32.e_entry);
65 			break;
66 		case ELFDATA2MSB:
67 			entry = be32toh(hdr.ehdr32.e_entry);
68 			break;
69 		default:
70 			die("Invalid ELF encoding\n");
71 		}
72 
73 		/* Sign extend to form a canonical address */
74 		entry = (int64_t)(int32_t)entry;
75 		break;
76 
77 	case ELFCLASS64:
78 		switch (hdr.ehdr32.e_ident[EI_DATA]) {
79 		case ELFDATA2LSB:
80 			entry = le64toh(hdr.ehdr64.e_entry);
81 			break;
82 		case ELFDATA2MSB:
83 			entry = be64toh(hdr.ehdr64.e_entry);
84 			break;
85 		default:
86 			die("Invalid ELF encoding\n");
87 		}
88 		break;
89 
90 	default:
91 		die("Invalid ELF class\n");
92 	}
93 
94 	printf("0x%016" PRIx64 "\n", entry);
95 	return EXIT_SUCCESS;
96 }
97