A2Loader.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. G.F. 31.01.2017
  3. Loader for the statically linked A2 core.
  4. Compile command:
  5. gcc -m32 A2Loader.c -ldl -o A2Loader.elf
  6. The command 'A2Loader.elf -h' shows the needed
  7. displacement of the A2 core.
  8. The A2 core has to be appended to the binary of this
  9. program by the A2 command:
  10. SolarisELF.Build Solaris32G.elf ~
  11. */
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <fcntl.h>
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. #include <string.h>
  18. #include <dlfcn.h>
  19. #define Offset 10*1024 /* beginning of the A2 core */
  20. #define Bufsize 2*1024*1024
  21. typedef void (*OberonProc)();
  22. typedef void *addr;
  23. typedef struct {
  24. char id[32]; /* must be coreID */
  25. void *displacement; /* must be address of buf */
  26. OberonProc entry; /* Glue.Init0 */
  27. addr *dlopenaddr;
  28. addr *dlsymaddr;
  29. int *argc;
  30. addr *argv;
  31. } *A2Header;
  32. char *coreID = "Solaris32G.core";
  33. int main( int argc, char *argv[] ) {
  34. int r, n, fd;
  35. size_t fsize;
  36. struct stat sb;
  37. void *buf;
  38. A2Header header;
  39. char path[64];
  40. addr a;
  41. r = posix_memalign( &buf, 64*1024, Bufsize );
  42. if ((argc == 2) && (strcmp(argv[1], "-h") == 0)) {
  43. printf("Core displacement must be 0x%x\n", buf );
  44. exit( 0 );
  45. }
  46. r = mprotect( buf, Bufsize, PROT_READ+PROT_WRITE+PROT_EXEC );
  47. a = realpath( argv[0], path );
  48. fd = open( path, O_RDONLY );
  49. r = fstat( fd, &sb );
  50. fsize = sb.st_size;
  51. r = lseek( fd, Offset, SEEK_SET );
  52. n = read( fd, buf, fsize - Offset );
  53. header = (A2Header)buf;
  54. if (strcmp(header->id, coreID) != 0) {
  55. printf( "bad headerId: %s, expected: %s\n", header->id, coreID );
  56. exit( 2 );
  57. }
  58. if (header->displacement != buf) {
  59. printf( "bad displacement: %x, expected: %x\n", header->displacement, buf );
  60. exit( 3 );
  61. }
  62. *(header->dlopenaddr) = dlopen;
  63. *(header->dlsymaddr) = dlsym;
  64. *(header->argc) = argc;
  65. *(header->argv) = argv;
  66. header->entry();
  67. }