extract.c 6.4 KB

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