18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci#include <stdio.h>
38c2ecf20Sopenharmony_ci#include <stdlib.h>
48c2ecf20Sopenharmony_ci#include <string.h>
58c2ecf20Sopenharmony_ci#include <elf.h>
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ciint
88c2ecf20Sopenharmony_cimain(int argc, char **argv)
98c2ecf20Sopenharmony_ci{
108c2ecf20Sopenharmony_ci	unsigned char ei[EI_NIDENT];
118c2ecf20Sopenharmony_ci	union { short s; char c[2]; } endian_test;
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci	if (fread(ei, 1, EI_NIDENT, stdin) != EI_NIDENT) {
148c2ecf20Sopenharmony_ci		fprintf(stderr, "Error: input truncated\n");
158c2ecf20Sopenharmony_ci		return 1;
168c2ecf20Sopenharmony_ci	}
178c2ecf20Sopenharmony_ci	if (memcmp(ei, ELFMAG, SELFMAG) != 0) {
188c2ecf20Sopenharmony_ci		fprintf(stderr, "Error: not ELF\n");
198c2ecf20Sopenharmony_ci		return 1;
208c2ecf20Sopenharmony_ci	}
218c2ecf20Sopenharmony_ci	switch (ei[EI_CLASS]) {
228c2ecf20Sopenharmony_ci	case ELFCLASS32:
238c2ecf20Sopenharmony_ci		printf("#define KERNEL_ELFCLASS ELFCLASS32\n");
248c2ecf20Sopenharmony_ci		break;
258c2ecf20Sopenharmony_ci	case ELFCLASS64:
268c2ecf20Sopenharmony_ci		printf("#define KERNEL_ELFCLASS ELFCLASS64\n");
278c2ecf20Sopenharmony_ci		break;
288c2ecf20Sopenharmony_ci	default:
298c2ecf20Sopenharmony_ci		exit(1);
308c2ecf20Sopenharmony_ci	}
318c2ecf20Sopenharmony_ci	switch (ei[EI_DATA]) {
328c2ecf20Sopenharmony_ci	case ELFDATA2LSB:
338c2ecf20Sopenharmony_ci		printf("#define KERNEL_ELFDATA ELFDATA2LSB\n");
348c2ecf20Sopenharmony_ci		break;
358c2ecf20Sopenharmony_ci	case ELFDATA2MSB:
368c2ecf20Sopenharmony_ci		printf("#define KERNEL_ELFDATA ELFDATA2MSB\n");
378c2ecf20Sopenharmony_ci		break;
388c2ecf20Sopenharmony_ci	default:
398c2ecf20Sopenharmony_ci		exit(1);
408c2ecf20Sopenharmony_ci	}
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci	if (sizeof(unsigned long) == 4) {
438c2ecf20Sopenharmony_ci		printf("#define HOST_ELFCLASS ELFCLASS32\n");
448c2ecf20Sopenharmony_ci	} else if (sizeof(unsigned long) == 8) {
458c2ecf20Sopenharmony_ci		printf("#define HOST_ELFCLASS ELFCLASS64\n");
468c2ecf20Sopenharmony_ci	}
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	endian_test.s = 0x0102;
498c2ecf20Sopenharmony_ci	if (memcmp(endian_test.c, "\x01\x02", 2) == 0)
508c2ecf20Sopenharmony_ci		printf("#define HOST_ELFDATA ELFDATA2MSB\n");
518c2ecf20Sopenharmony_ci	else if (memcmp(endian_test.c, "\x02\x01", 2) == 0)
528c2ecf20Sopenharmony_ci		printf("#define HOST_ELFDATA ELFDATA2LSB\n");
538c2ecf20Sopenharmony_ci	else
548c2ecf20Sopenharmony_ci		exit(1);
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci	return 0;
578c2ecf20Sopenharmony_ci}
58