OberonLoader.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. G.F. 31.01.2017
  3. Loader for statically linked oberon binaries.
  4. Compile command:
  5. gcc -m32 OberonLoader.c -ldl -o OberonLoader
  6. The filename of the statically linked oberon binary is 'oberon.bin'.
  7. The oberon binary may be joined with the loader to create a Unix program:
  8. This is done in A2 by the following command:
  9. UnixBinary.Build oberon.bin -> <program name> ~
  10. */
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <fcntl.h>
  14. #include <sys/types.h>
  15. #include <sys/mman.h>
  16. #include <sys/uio.h>
  17. #include <unistd.h>
  18. #include <sys/stat.h>
  19. #include <string.h>
  20. #include <dlfcn.h>
  21. #define Offset 10*1024 /* startpos of oberon.bin if it is appended to the loader binary */
  22. #define BlkSize 4*1024
  23. typedef void (*OberonProc)();
  24. typedef void *addr;
  25. typedef unsigned int uint;
  26. typedef struct { /* cf. Generic.Unix.I386.Glue.Mod */
  27. /* Oberon --> loader: */
  28. char id[24]; /* must match coreID */
  29. int codesize;
  30. int relocations; /* if base = 0 */
  31. addr base; /* must be 0 or match the address of buf */
  32. OberonProc entry; /* Glue.Init0 */
  33. /* loader --> Oberon: */
  34. addr *dlopenaddr;
  35. addr *dlcloseaddr;
  36. addr *dlsymaddr;
  37. int *argc;
  38. addr *argv;
  39. addr *env;
  40. addr *cout;
  41. } *Header;
  42. char *coreID = "Oberon32G.binary";
  43. addr buf;
  44. int fd;
  45. uint bufsize;
  46. void cout( char c ) {
  47. char buf[8];
  48. buf[0] = c;
  49. write( 1, &buf, 1 );
  50. }
  51. uint ReadInteger( ) {
  52. union {
  53. char buf[4];
  54. uint i;
  55. } u;
  56. read( fd, &u.buf, 4 );
  57. return (u.i);
  58. }
  59. void Relocate( uint relocations ) {
  60. addr *a;
  61. uint i;
  62. for (i=0; i<relocations; i++) {
  63. a = buf + ReadInteger();
  64. *a = buf+(uint)(*a);
  65. }
  66. }
  67. int main( int argc, char *argv[], char *env[] ) {
  68. int r, n, binsize, relocations;
  69. size_t fsize;
  70. struct stat sb;
  71. Header header;
  72. char path[64];
  73. addr a;
  74. uint binpos;
  75. a = realpath( argv[0], path );
  76. fd = open( path, O_RDONLY );
  77. r = fstat( fd, &sb );
  78. fsize = sb.st_size;
  79. if (fsize > Offset) {
  80. binpos = Offset;
  81. r = lseek( fd, binpos, SEEK_SET );
  82. } else {
  83. binpos = 0;
  84. fd = open( "oberon.bin", O_RDONLY );
  85. }
  86. buf = malloc( 512 );
  87. n = read( fd, buf, 256 );
  88. header = (Header)buf;
  89. if (strcmp(header->id, coreID) != 0) {
  90. printf( "bad headerId: %s, expected: %s\n", header->id, coreID );
  91. exit( 2 );
  92. }
  93. binsize = header->codesize;
  94. relocations = header->relocations;
  95. bufsize = BlkSize;
  96. while (bufsize < binsize) bufsize += BlkSize;
  97. r = lseek( fd, binpos, SEEK_SET );
  98. free( buf );
  99. r = posix_memalign( &buf, BlkSize, bufsize );
  100. if (mprotect( buf, bufsize, PROT_READ|PROT_WRITE|PROT_EXEC) != 0)
  101. perror("mprotect");
  102. n = read( fd, buf, binsize );
  103. Relocate( relocations );
  104. header = (Header)buf;
  105. *(header->dlopenaddr) = dlopen;
  106. *(header->dlcloseaddr) = dlclose;
  107. *(header->dlsymaddr) = dlsym;
  108. *(header->argc) = argc;
  109. *(header->argv) = argv;
  110. *(header->env) = env;
  111. *(header->cout) = cout;
  112. header->entry();
  113. return (0);
  114. }