relocate.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  3. * See the copyright notice in the ACK home directory, in the file "Copyright".
  4. */
  5. #ifndef lint
  6. static char rcsid[] = "$Id$";
  7. #endif
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include "out.h"
  11. #include "const.h"
  12. #include "debug.h"
  13. #include "defs.h"
  14. #include "orig.h"
  15. #define UBYTE(x) ((x) & BYTEMASK)
  16. /*
  17. * The bits in type indicate how many bytes the value occupies and what
  18. * significance should be attributed to each byte.
  19. */
  20. static long
  21. getvalu(addr, type)
  22. char addr[];
  23. char type;
  24. {
  25. unsigned short word0, word1;
  26. switch (type & RELSZ) {
  27. case RELO1:
  28. return UBYTE(addr[0]);
  29. case RELO2:
  30. if (type & RELBR)
  31. return (UBYTE(addr[0]) << WIDTH) + UBYTE(addr[1]);
  32. else
  33. return (UBYTE(addr[1]) << WIDTH) + UBYTE(addr[0]);
  34. case RELO4:
  35. if (type & RELBR) {
  36. word0 = (UBYTE(addr[0]) << WIDTH) + UBYTE(addr[1]);
  37. word1 = (UBYTE(addr[2]) << WIDTH) + UBYTE(addr[3]);
  38. } else {
  39. word0 = (UBYTE(addr[1]) << WIDTH) + UBYTE(addr[0]);
  40. word1 = (UBYTE(addr[3]) << WIDTH) + UBYTE(addr[2]);
  41. }
  42. if (type & RELWR)
  43. return ((long)word0 << (2 * WIDTH)) + word1;
  44. else
  45. return ((long)word1 << (2 * WIDTH)) + word0;
  46. default:
  47. fatal("bad relocation size");
  48. }
  49. /* NOTREACHED */
  50. }
  51. /*
  52. * The bits in type indicate how many bytes the value occupies and what
  53. * significance should be attributed to each byte.
  54. * We do not check for overflow.
  55. */
  56. static
  57. putvalu(valu, addr, type)
  58. long valu;
  59. char addr[];
  60. char type;
  61. {
  62. unsigned short word0, word1;
  63. switch (type & RELSZ) {
  64. case RELO1:
  65. addr[0] = valu;
  66. break;
  67. case RELO2:
  68. if (type & RELBR) {
  69. addr[0] = valu >> WIDTH;
  70. addr[1] = valu;
  71. } else {
  72. addr[0] = valu;
  73. addr[1] = valu >> WIDTH;
  74. }
  75. break;
  76. case RELO4:
  77. if (type & RELWR) {
  78. word0 = valu >> (2 * WIDTH);
  79. word1 = valu;
  80. } else {
  81. word0 = valu;
  82. word1 = valu >> (2 * WIDTH);
  83. }
  84. if (type & RELBR) {
  85. addr[0] = word0 >> WIDTH;
  86. addr[1] = word0;
  87. addr[2] = word1 >> WIDTH;
  88. addr[3] = word1;
  89. } else {
  90. addr[0] = word0;
  91. addr[1] = word0 >> WIDTH;
  92. addr[2] = word1;
  93. addr[3] = word1 >> WIDTH;
  94. }
  95. break;
  96. default:
  97. fatal("bad relocation size");
  98. }
  99. }
  100. extern unsigned short NLocals, NGlobals;
  101. extern struct outsect outsect[];
  102. extern struct orig relorig[];
  103. /*
  104. * There are two cases: `local' is an undefined external or common name,
  105. * or `local' is a section name.
  106. * First case: if the name has been defined in another module,
  107. * its value is known and can be added. Or_nami will be the
  108. * index of the name of the section in which this name was
  109. * defined. Otherwise we must change or_nami to the index of
  110. * this name in the name table of the output file and leave
  111. * its value unchanged.
  112. * Second case: we must update the value by the change
  113. * in position of the section of local.
  114. */
  115. static unsigned
  116. addrelo(relo, names, valu_out)
  117. struct outrelo *relo;
  118. struct outname *names;
  119. long *valu_out; /* Out variable. */
  120. {
  121. register struct outname *local = &names[relo->or_nami];
  122. register unsigned short index = NLocals;
  123. register long valu = *valu_out;
  124. if ((local->on_type & S_SCT)) {
  125. register int sectindex = (local->on_type & S_TYP) - S_MIN;
  126. valu += relorig[sectindex].org_size;
  127. valu += outsect[sectindex].os_base;
  128. index += NGlobals + sectindex;
  129. } else {
  130. register struct outname *name;
  131. extern int hash();
  132. extern struct outname *searchname();
  133. extern unsigned indexof();
  134. extern struct outhead outhead;
  135. name = searchname(local->on_mptr, hash(local->on_mptr));
  136. if (name == (struct outname *)0)
  137. fatal("name %s not found in pass 2", local->on_mptr);
  138. if (ISCOMMON(name) || ISUNDEFINED(name)) {
  139. debug("can't relocate from %s\n",local->on_mptr,0,0,0);
  140. index += indexof(name);
  141. } else {
  142. valu += name->on_valu;
  143. if ((name->on_type & S_TYP) == S_ABS) {
  144. index += NGlobals + outhead.oh_nsect;
  145. }
  146. else index += NGlobals +
  147. (name->on_type & S_TYP) - S_MIN;
  148. }
  149. }
  150. *valu_out = valu;
  151. return index;
  152. }
  153. /*
  154. * This routine relocates a value in a section pointed to by `emit', of
  155. * which the header is pointed to by `head'. Relocation is relative to the
  156. * names in `names'; `relo' tells how to relocate.
  157. */
  158. relocate(head, emit, names, relo, off)
  159. struct outhead *head;
  160. char *emit;
  161. struct outname names[];
  162. struct outrelo *relo;
  163. long off;
  164. {
  165. long valu;
  166. int sectindex = relo->or_sect - S_MIN;
  167. extern struct outhead outhead;
  168. /*
  169. * Pick up previous value at location to be relocated.
  170. */
  171. valu = getvalu(emit + (relo->or_addr - off), relo->or_type);
  172. /*
  173. * Or_nami is an index in the name table of the considered module.
  174. * The name of which it is an index can be:
  175. * - an undefined external or a common name
  176. * - a section name
  177. * - the first name outside! the name table (argh)
  178. */
  179. if (relo->or_nami < head->oh_nname) {
  180. /* First two cases. */
  181. relo->or_nami = addrelo(relo, names, &valu);
  182. } else {
  183. /*
  184. * Third case: it is absolute. The relocation of absolute
  185. * names is always 0. We only need to change the index.
  186. */
  187. relo->or_nami = NLocals + NGlobals + outhead.oh_nsect;
  188. }
  189. /*
  190. * If relocation is pc-relative, we had to update the value by
  191. * the change in distance between the referencING and referencED
  192. * section. We already added the origin of the referencED section;
  193. * now we subtract the origin of the referencING section.
  194. */
  195. if (relo->or_type & RELPC)
  196. valu -= relorig[sectindex].org_size+outsect[sectindex].os_base;
  197. /*
  198. * Now put the value back.
  199. */
  200. putvalu(valu, emit + (relo->or_addr - off), relo->or_type);
  201. /*
  202. * We must change the offset within the section of the value to be
  203. * relocated to its offset in the new section. `Or_addr' must again be
  204. * in the normal part, of course.
  205. */
  206. relo->or_addr += relorig[sectindex].org_size;
  207. }