1 // SPDX-License-Identifier: GPL-2.0
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)
25 __attribute__((noreturn))
26 static void die(const char *msg)
32 int main(int argc, const char *argv[])
43 die("Usage: elf-entry <elf-file>\n");
45 file = fopen(argv[1], "r");
47 perror("Unable to open input file");
51 nread = fread(&hdr, 1, sizeof(hdr), file);
52 if (nread != sizeof(hdr)) {
53 perror("Unable to read input file");
58 if (memcmp(hdr.ehdr32.e_ident, ELFMAG, SELFMAG)) {
60 die("Input is not an ELF\n");
63 switch (hdr.ehdr32.e_ident[EI_CLASS]) {
65 switch (hdr.ehdr32.e_ident[EI_DATA]) {
67 entry = le32toh(hdr.ehdr32.e_entry);
70 entry = be32toh(hdr.ehdr32.e_entry);
74 die("Invalid ELF encoding\n");
77 /* Sign extend to form a canonical address */
78 entry = (int64_t)(int32_t)entry;
82 switch (hdr.ehdr32.e_ident[EI_DATA]) {
84 entry = le64toh(hdr.ehdr64.e_entry);
87 entry = be64toh(hdr.ehdr64.e_entry);
91 die("Invalid ELF encoding\n");
97 die("Invalid ELF class\n");
100 printf("0x%016" PRIx64 "\n", entry);