extract.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 "memory.h"
  15. #include "orig.h"
  16. #include "scan.h"
  17. static get_names();
  18. static process();
  19. static getexternal();
  20. static redefine();
  21. static transfer();
  22. extern ind_t savechar();
  23. /*
  24. * Get section sizes and symboltable information from present module.
  25. */
  26. extract()
  27. {
  28. struct outhead head;
  29. get_modul();
  30. /*
  31. * Copy head because we need it so often but it can change place,
  32. * so we can't trust a pointer to it.
  33. */
  34. head = *(struct outhead *)modulptr(IND_HEAD);
  35. get_names(&head);
  36. process(&head);
  37. skip_modul(&head);
  38. }
  39. unsigned short NLocals = 0; /* Number of local names to be saved. */
  40. unsigned short NGlobals = 0; /* Number of global names. */
  41. /*
  42. * Walk through the nametable of this module, counting the locals that must
  43. * appear in the final output file if this module is linked.
  44. * That number will be returned.
  45. */
  46. static
  47. get_names(head)
  48. register struct outhead *head;
  49. {
  50. register int nnames;
  51. register ind_t nameindex, charindex;
  52. register ind_t charoff;
  53. extern int flagword;
  54. nnames = head->oh_nname;
  55. nameindex = IND_NAME(*head);
  56. charindex = IND_CHAR(*head);
  57. charoff = OFF_CHAR(*head);
  58. while (nnames--) {
  59. struct outname name; /* A local copy. */
  60. /*
  61. * Because savelocal/getexternal might relocate the modules
  62. * we have to compute the core addresses again.
  63. */
  64. name = *(struct outname *)modulptr(nameindex);
  65. /*
  66. * Change the offset in file into an offset in the memory area.
  67. * There will always be at least a header before the string
  68. * area, so we don't have to be afraid to confuse "no name"
  69. * with "the first name".
  70. */
  71. if (name.on_foff) {
  72. if (name.on_foff < charoff ||
  73. name.on_foff >= charoff+head->oh_nchar) {
  74. fatal("illegal offset in name");
  75. }
  76. name.on_foff += charindex - charoff;
  77. }
  78. namerelocate(&name);
  79. if ((name.on_type & S_TYP) == S_CRS) {
  80. name.on_valu += charindex - charoff;
  81. name.on_valu = savechar(ALLOGCHR, (ind_t)name.on_valu);
  82. }
  83. if (name.on_type & S_EXT) {
  84. getexternal(&name);
  85. } else {
  86. /*
  87. * The only thing we want to know about locals is
  88. * whether they must appear in the output file.
  89. */
  90. if (!(flagword & SFLAG) && mustsavelocal(&name)) {
  91. NLocals++;
  92. savelocal(&name);
  93. }
  94. }
  95. nameindex += sizeof(struct outname);
  96. }
  97. }
  98. extern struct orig relorig[];
  99. static
  100. process(head)
  101. register struct outhead *head;
  102. {
  103. register struct outsect *sects;
  104. register struct outsect *outsp;
  105. register int nsect;
  106. register struct orig *orig = relorig;
  107. extern struct outhead outhead;
  108. extern struct outsect outsect[];
  109. outhead.oh_nrelo += head->oh_nrelo;
  110. outhead.oh_nemit += head->oh_nemit;
  111. if (head->oh_nsect > outhead.oh_nsect)
  112. outhead.oh_nsect = head->oh_nsect;
  113. sects = (struct outsect *)modulptr(IND_SECT(*head));
  114. nsect = head->oh_nsect;
  115. outsp = outsect;
  116. while (nsect--) {
  117. if (sects->os_flen) {
  118. /* contains non-zero stuff */
  119. outhead.oh_nemit += outsp->os_size - outsp->os_flen;
  120. outsp->os_flen = outsp->os_size + sects->os_flen;
  121. }
  122. else {
  123. outsp->os_flen += sects->os_flen;
  124. }
  125. outsp->os_size += sects->os_size;
  126. /*
  127. * Add all flen's and all (size - flen == zero)'s of
  128. * preceding sections with the same number.
  129. */
  130. orig->org_size = outsp->os_size;
  131. orig++; outsp++; sects++;
  132. }
  133. }
  134. /*
  135. * Add relocation constant for names in user defined sections.
  136. * The value of a common name indicates a size instead of an offset,
  137. * and hence shouldn't be relocated.
  138. * Otherwise we just add the accumulated size of all normal parts in preceding
  139. * sections with the same size.
  140. */
  141. namerelocate(name)
  142. register struct outname *name;
  143. {
  144. register int type = name->on_type;
  145. register int sct = type & S_TYP;
  146. if (sct == S_UND || sct == S_ABS || sct == S_CRS)
  147. return;
  148. if (type & S_COM) {
  149. if ( ! (type&S_EXT) ) fatal("local commons should be handled by the assembler") ;
  150. return;
  151. }
  152. name->on_valu += relorig[(type & S_TYP) - S_MIN].org_size;
  153. }
  154. /*
  155. * If we see this name for the first time, we must remember it for
  156. * we might need it later on. Otherwise it must confirm to what we already
  157. * know about it, and eventually add to that knowledge.
  158. */
  159. static
  160. getexternal(name)
  161. register struct outname *name;
  162. {
  163. register char *string;
  164. register int h;
  165. register struct outname *old;
  166. extern int hash();
  167. extern struct outname *searchname();
  168. string = modulptr((ind_t)name->on_foff);
  169. h = hash(string);
  170. old = searchname(string, h);
  171. if (old == (struct outname *)0) {
  172. NGlobals++;
  173. entername(name, h);
  174. if (ISUNDEFINED(name)) {
  175. verbose("requires %s", string, 0, 0, 0);
  176. }
  177. } else if (!ISUNDEFINED(name)) {
  178. if (ISUNDEFINED(old)) {
  179. name->on_mptr = string; /* Just for convenience. */
  180. transfer(name, old);
  181. } else {
  182. name->on_mptr = string; /* Just for convenience. */
  183. redefine(name, old);
  184. }
  185. }
  186. }
  187. /*
  188. * Handle the redefinition of `new' in the current module.
  189. * A name can be defined in three ways, in increasing priority:
  190. * undefined,
  191. * common,
  192. * defined in a section.
  193. * A name may become "higher" when defined, but not "lower".
  194. * A redefinition as common is allowed. It is ignored, but a warning is given
  195. * when the desired section of `new' doesn't correspond with the section of
  196. * `old'. If a common definition is given again for a name, we take the
  197. * greatest value so that the common declared name always has enough space.
  198. * If a common is defined as a not-common, the old definition is ignored.
  199. */
  200. static
  201. redefine(new, old)
  202. register struct outname *new, *old;
  203. {
  204. if (!ISCOMMON(old)) {
  205. if (!ISCOMMON(new))
  206. error("%s: multiply defined", new->on_mptr);
  207. /*
  208. else if ((new->on_type & S_TYP) != (old->on_type & S_TYP))
  209. warning("%s: sections differ", new->on_mptr);
  210. */
  211. } else {
  212. /* `Old' is common. */
  213. if (ISCOMMON(new)) {
  214. if ((new->on_type & S_TYP) != (old->on_type & S_TYP))
  215. warning("%s: sections differ", new->on_mptr);
  216. if (new->on_valu > old->on_valu)
  217. old->on_valu = new->on_valu;
  218. } else {
  219. transfer(new, old);
  220. }
  221. }
  222. }
  223. /*
  224. * Transfer things we want to know from `src' to `dst'.
  225. */
  226. static
  227. transfer(src, dst)
  228. register struct outname *src, *dst;
  229. {
  230. debug("%s defined here\n", src->on_mptr, 0, 0, 0);
  231. dst->on_valu = src->on_valu;
  232. dst->on_type = src->on_type;
  233. dst->on_desc = src->on_desc;
  234. }