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");
57 if (memcmp(hdr
.ehdr32
.e_ident
, ELFMAG
, SELFMAG
))
58 die("Input is not an ELF\n");
60 switch (hdr
.ehdr32
.e_ident
[EI_CLASS
]) {
62 switch (hdr
.ehdr32
.e_ident
[EI_DATA
]) {
64 entry
= le32toh(hdr
.ehdr32
.e_entry
);
67 entry
= be32toh(hdr
.ehdr32
.e_entry
);
70 die("Invalid ELF encoding\n");
73 /* Sign extend to form a canonical address */
74 entry
= (int64_t)(int32_t)entry
;
78 switch (hdr
.ehdr32
.e_ident
[EI_DATA
]) {
80 entry
= le64toh(hdr
.ehdr64
.e_entry
);
83 entry
= be64toh(hdr
.ehdr64
.e_entry
);
86 die("Invalid ELF encoding\n");
91 die("Invalid ELF class\n");
94 printf("0x%016" PRIx64
"\n", entry
);