relocate.c 5.7 KB

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