2
0

A2Loader.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 { /* cf. Glue.EntryPoint */
  24. char id[32]; /* must match coreID */
  25. void *displacement; /* must match address of buf */
  26. OberonProc entry; /* Glue.Init0 */
  27. addr *dlopenaddr;
  28. addr *dlcloseaddr;
  29. addr *dlsymaddr;
  30. int *argc;
  31. addr *argv;
  32. addr *env;
  33. } *A2Header;
  34. char *coreID = "Oberon32G.core";
  35. int main( int argc, char *argv[], char *env[] ) {
  36. int r, n, fd;
  37. size_t fsize;
  38. struct stat sb;
  39. void *buf;
  40. A2Header header;
  41. char path[64];
  42. addr a;
  43. r = posix_memalign( &buf, 64*1024, Bufsize );
  44. if ((argc == 2) && (strcmp(argv[1], "-h") == 0)) {
  45. printf("Core displacement must be 0x%x\n", buf );
  46. exit( 0 );
  47. }
  48. r = mprotect( buf, Bufsize, PROT_READ+PROT_WRITE+PROT_EXEC );
  49. a = realpath( argv[0], path );
  50. fd = open( path, O_RDONLY );
  51. r = fstat( fd, &sb );
  52. fsize = sb.st_size;
  53. r = lseek( fd, Offset, SEEK_SET );
  54. n = read( fd, buf, fsize - Offset );
  55. header = (A2Header)buf;
  56. if (strcmp(header->id, coreID) != 0) {
  57. printf( "bad headerId: %s, expected: %s\n", header->id, coreID );
  58. exit( 2 );
  59. }
  60. if (header->displacement != buf) {
  61. printf( "bad displacement: %x, expected: %x\n", header->displacement, buf );
  62. exit( 3 );
  63. }
  64. *(header->dlopenaddr) = dlopen;
  65. *(header->dlcloseaddr) = dlclose;
  66. *(header->dlsymaddr) = dlsym;
  67. *(header->argc) = argc;
  68. *(header->argv) = argv;
  69. *(header->env) = env;
  70. header->entry();
  71. }