blob: 630988155e062f81d4380e1acea262ce54f1739c [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#ifndef __APPLE__
6#include <elf.h>
7#else
8#include "elf.h"
9#endif
10
11int
12main(int argc, char **argv)
13{
14 unsigned char ei[EI_NIDENT];
15 union { short s; char c[2]; } endian_test;
16
17 if (fread(ei, 1, EI_NIDENT, stdin) != EI_NIDENT) {
18 fprintf(stderr, "Error: input truncated\n");
19 return 1;
20 }
21 if (memcmp(ei, ELFMAG, SELFMAG) != 0) {
22 fprintf(stderr, "Error: not ELF\n");
23 return 1;
24 }
25 switch (ei[EI_CLASS]) {
26 case ELFCLASS32:
27 printf("#define KERNEL_ELFCLASS ELFCLASS32\n");
28 break;
29 case ELFCLASS64:
30 printf("#define KERNEL_ELFCLASS ELFCLASS64\n");
31 break;
32 default:
33 exit(1);
34 }
35 switch (ei[EI_DATA]) {
36 case ELFDATA2LSB:
37 printf("#define KERNEL_ELFDATA ELFDATA2LSB\n");
38 break;
39 case ELFDATA2MSB:
40 printf("#define KERNEL_ELFDATA ELFDATA2MSB\n");
41 break;
42 default:
43 exit(1);
44 }
45
46 if (sizeof(unsigned long) == 4) {
47 printf("#define HOST_ELFCLASS ELFCLASS32\n");
48 } else if (sizeof(unsigned long) == 8) {
49 printf("#define HOST_ELFCLASS ELFCLASS64\n");
50 }
51
52 endian_test.s = 0x0102;
53 if (memcmp(endian_test.c, "\x01\x02", 2) == 0)
54 printf("#define HOST_ELFDATA ELFDATA2MSB\n");
55 else if (memcmp(endian_test.c, "\x02\x01", 2) == 0)
56 printf("#define HOST_ELFDATA ELFDATA2LSB\n");
57 else
58 exit(1);
59
60 return 0;
61}