rdfload.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /* ----------------------------------------------------------------------- *
  2. *
  3. * Copyright 1996-2017 The NASM Authors - All Rights Reserved
  4. * See the file AUTHORS included with the NASM distribution for
  5. * the specific copyright holders.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following
  9. * conditions are met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * * Redistributions in binary form must reproduce the above
  14. * copyright notice, this list of conditions and the following
  15. * disclaimer in the documentation and/or other materials provided
  16. * with the distribution.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  19. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  20. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  25. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  26. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  29. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  30. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. * ----------------------------------------------------------------------- */
  33. /*
  34. * rdfload.c RDOFF Object File loader library
  35. */
  36. /*
  37. * TODO: this has been modified from previous version only in very
  38. * simplistic ways. Needs to be improved drastically, especially:
  39. * - support for more than the 2 standard segments
  40. * - support for segment relocations (hard to do in ANSI C)
  41. */
  42. #include "compiler.h"
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <string.h>
  46. #include "rdfload.h"
  47. #include "symtab.h"
  48. #include "collectn.h"
  49. extern int rdf_errno;
  50. rdfmodule *rdfload(const char *filename)
  51. {
  52. rdfmodule *f;
  53. int32_t bsslength = 0;
  54. char *hdr;
  55. rdfheaderrec *r;
  56. f = nasm_malloc(sizeof(rdfmodule));
  57. if (f == NULL) {
  58. rdf_errno = RDF_ERR_NOMEM;
  59. return NULL;
  60. }
  61. f->symtab = symtabNew();
  62. if (!f->symtab) {
  63. nasm_free(f);
  64. rdf_errno = RDF_ERR_NOMEM;
  65. return NULL;
  66. }
  67. /* open the file */
  68. if (rdfopen(&(f->f), filename)) {
  69. nasm_free(f);
  70. return NULL;
  71. }
  72. /* read in text and data segments, and header */
  73. f->t = nasm_malloc(f->f.seg[0].length);
  74. f->d = nasm_malloc(f->f.seg[1].length); /* BSS seg allocated later */
  75. hdr = nasm_malloc(f->f.header_len);
  76. if (!f->t || !f->d || !hdr) {
  77. rdf_errno = RDF_ERR_NOMEM;
  78. rdfclose(&f->f);
  79. if (f->t)
  80. nasm_free(f->t);
  81. if (f->d)
  82. nasm_free(f->d);
  83. nasm_free(f);
  84. nasm_free(hdr);
  85. return NULL;
  86. }
  87. if (rdfloadseg(&f->f, RDOFF_HEADER, hdr) ||
  88. rdfloadseg(&f->f, RDOFF_CODE, f->t) ||
  89. rdfloadseg(&f->f, RDOFF_DATA, f->d)) {
  90. rdfclose(&f->f);
  91. nasm_free(f->t);
  92. nasm_free(f->d);
  93. nasm_free(f);
  94. nasm_free(hdr);
  95. return NULL;
  96. }
  97. rdfclose(&f->f);
  98. /* Allocate BSS segment; step through header and count BSS records */
  99. while ((r = rdfgetheaderrec(&f->f))) {
  100. if (r->type == 5)
  101. bsslength += r->b.amount;
  102. }
  103. f->b = nasm_malloc(bsslength);
  104. if (bsslength && (!f->b)) {
  105. nasm_free(f->t);
  106. nasm_free(f->d);
  107. nasm_free(f);
  108. nasm_free(hdr);
  109. rdf_errno = RDF_ERR_NOMEM;
  110. return NULL;
  111. }
  112. rdfheaderrewind(&f->f);
  113. f->textrel = (int32_t)(size_t)f->t;
  114. f->datarel = (int32_t)(size_t)f->d;
  115. f->bssrel = (int32_t)(size_t)f->b;
  116. return f;
  117. }
  118. int rdf_relocate(rdfmodule * m)
  119. {
  120. rdfheaderrec *r;
  121. Collection imports;
  122. symtabEnt e;
  123. int32_t rel;
  124. uint8_t *seg;
  125. rdfheaderrewind(&m->f);
  126. collection_init(&imports);
  127. while ((r = rdfgetheaderrec(&m->f))) {
  128. switch (r->type) {
  129. case 1: /* Relocation record */
  130. /* calculate relocation factor */
  131. if (r->r.refseg == 0)
  132. rel = m->textrel;
  133. else if (r->r.refseg == 1)
  134. rel = m->datarel;
  135. else if (r->r.refseg == 2)
  136. rel = m->bssrel;
  137. else
  138. /* We currently do not support load-time linkage.
  139. This should be added some time soon... */
  140. return 1; /* return error code */
  141. if ((r->r.segment & 63) == 0)
  142. seg = m->t;
  143. else if ((r->r.segment & 63) == 1)
  144. seg = m->d;
  145. else
  146. continue; /* relocation not in a loaded segment */
  147. /* it doesn't matter in this case that the code is non-portable,
  148. as the entire concept of executing a module like this is
  149. non-portable */
  150. switch (r->r.length) {
  151. case 1:
  152. seg[r->r.offset] += (char)rel;
  153. break;
  154. case 2:
  155. *(uint16_t *) (seg + r->r.offset) += (uint16_t) rel;
  156. break;
  157. case 4:
  158. *(int32_t *)(seg + r->r.offset) += rel;
  159. break;
  160. }
  161. break;
  162. case 3: /* export record - add to symtab */
  163. e.segment = r->e.segment;
  164. e.offset = r->e.offset + (e.segment == 0 ? m->textrel : /* 0 -> code */
  165. e.segment == 1 ? m->datarel : /* 1 -> data */
  166. m->bssrel); /* 2 -> bss */
  167. e.flags = 0;
  168. e.name = nasm_malloc(strlen(r->e.label) + 1);
  169. if (!e.name)
  170. return 1;
  171. strcpy(e.name, r->e.label);
  172. symtabInsert(m->symtab, &e);
  173. break;
  174. case 6: /* segment relocation */
  175. fprintf(stderr, "%s: segment relocation not supported by this "
  176. "loader\n", m->f.name);
  177. return 1;
  178. }
  179. }
  180. return 0;
  181. }