OberonLoader.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. G.F. 31.01.2017
  3. Loader for statically linked oberon binaries.
  4. Compile command:
  5. gcc -m32 -s -O OberonLoader.c -ldl -o OberonLoader
  6. gcc -m64 -s -O OberonLoader.c -ldl -o OberonLoader
  7. The statically linked oberon binary 'oberon.bin' has to be
  8. appended to this loader by the following A2-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 16*1024 /* startpos of the appended oberon 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.*.Glue.Mod */
  27. /* Oberon --> loader: */
  28. char id[24]; /* must match coreID */
  29. int codesize;
  30. int relocations;
  31. OberonProc entry; /* Glue.Init0 */
  32. /* loader --> Oberon: */
  33. addr *dlopenaddr;
  34. addr *dlcloseaddr;
  35. addr *dlsymaddr;
  36. int *argc;
  37. addr *argv;
  38. addr *env;
  39. addr *cout;
  40. } *Header;
  41. #if defined(__LP64__) || defined(_LP64)
  42. char *coreID = "Oberon64G.binary"; /* cf. Generic.Unix.AMD64.Glue.Mod */
  43. #else
  44. char *coreID = "Oberon32G.binary"; /* cf. Generic.Unix.I386.Glue.Mod */
  45. #endif
  46. addr buf;
  47. int fd;
  48. uint bufsize;
  49. void cout( char c ) {
  50. char buf[8];
  51. buf[0] = c;
  52. write( 1, &buf, 1 );
  53. }
  54. uint ReadInteger( ) {
  55. union {
  56. char buf[4];
  57. uint i;
  58. } u;
  59. read( fd, &u.buf, 4 );
  60. return (u.i);
  61. }
  62. void Relocate( uint relocations ) {
  63. addr *a;
  64. uint i;
  65. for (i=0; i<relocations; i++) {
  66. a = buf + ReadInteger();
  67. #if defined(__LP64__) || defined(_LP64)
  68. *a = buf+(ulong)(*a);
  69. #else
  70. *a = buf+(uint)(*a);
  71. #endif
  72. }
  73. }
  74. char *which_path(const char *name) {
  75. char *tname, *tok, *path;
  76. path = strdup(getenv("PATH"));
  77. if (NULL == path) return NULL;
  78. tok = strtok(path, ":");
  79. while (tok) {
  80. tname = malloc(strlen(tok) + 2 + strlen(name));
  81. sprintf(tname, "%s/%s", tok, name);
  82. if (0 == access(tname, X_OK)) { free(path); return tname; }
  83. free(tname);
  84. tok = strtok(NULL, ":");
  85. }
  86. free(path);
  87. return NULL;
  88. }
  89. int main( int argc, char *argv[], char *env[] ) {
  90. int r, n, binsize, relocations;
  91. size_t fsize;
  92. struct stat sb;
  93. Header header;
  94. char *path;
  95. addr a;
  96. fd = open( *argv, O_RDONLY );
  97. if (fd < 0) {
  98. /* find myself in PATH */
  99. path = which_path(*argv);
  100. fd = open(path, O_RDONLY);
  101. }
  102. r = fstat( fd, &sb );
  103. if ( sb.st_size < Offset+2048 ) {
  104. fprintf( stderr, "%s: missing appended Oberon binary\n", *argv );
  105. exit( 2 );
  106. }
  107. r = lseek( fd, Offset, SEEK_SET );
  108. buf = malloc( 512 );
  109. n = read( fd, buf, 256 );
  110. header = (Header)buf;
  111. if (strcmp(header->id, coreID) != 0) {
  112. fprintf( stderr, "wrong Oberon headerId: got '%s', expected '%s'\n", header->id, coreID );
  113. exit( 2 );
  114. }
  115. binsize = header->codesize;
  116. relocations = header->relocations;
  117. bufsize = BlkSize;
  118. while (bufsize < binsize) bufsize += BlkSize;
  119. r = lseek( fd, Offset, SEEK_SET );
  120. free( buf );
  121. r = posix_memalign( &buf, BlkSize, bufsize );
  122. if (mprotect( buf, bufsize, PROT_READ|PROT_WRITE|PROT_EXEC) != 0)
  123. perror("mprotect");
  124. n = read( fd, buf, binsize );
  125. Relocate( relocations );
  126. header = (Header)buf;
  127. *(header->dlopenaddr) = dlopen;
  128. *(header->dlcloseaddr) = dlclose;
  129. *(header->dlsymaddr) = dlsym;
  130. *(header->argc) = argc;
  131. *(header->argv) = argv;
  132. *(header->env) = env;
  133. *(header->cout) = cout;
  134. header->entry();
  135. return (0);
  136. }