mk_elfconfig.c 1.2 KB

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