mk_elfconfig.c 1.2 KB

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