1 /* ------------------------------------------------------------------------- */
2 /* "linker" : For compiling and linking modules */
4 /* Copyright (c) Graham Nelson 1993 - 2016 */
6 /* This file is part of Inform. */
8 /* Inform is free software: you can redistribute it and/or modify */
9 /* it under the terms of the GNU General Public License as published by */
10 /* the Free Software Foundation, either version 3 of the License, or */
11 /* (at your option) any later version. */
13 /* Inform is distributed in the hope that it will be useful, */
14 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
15 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
16 /* GNU General Public License for more details. */
18 /* You should have received a copy of the GNU General Public License */
19 /* along with Inform. If not, see https://gnu.org/licenses/ */
21 /* ------------------------------------------------------------------------- */
25 memory_block link_data_area;
26 uchar *link_data_holding_area, *link_data_top;
27 /* Start, current top, size of */
28 int32 link_data_size; /* link data table being written */
29 /* (holding import/export names) */
30 extern int32 *action_symbol;
32 /* ------------------------------------------------------------------------- */
34 /* ------------------------------------------------------------------------- */
36 extern char *describe_mv(int mval)
38 { case NULL_MV: return("null");
40 /* Marker values used in ordinary story file backpatching */
42 case DWORD_MV: return("dictionary word");
43 case STRING_MV: return("string literal");
44 case INCON_MV: return("system constant");
45 case IROUTINE_MV: return("routine");
46 case VROUTINE_MV: return("veneer routine");
47 case ARRAY_MV: return("internal array");
48 case NO_OBJS_MV: return("the number of objects");
49 case INHERIT_MV: return("inherited common p value");
50 case INDIVPT_MV: return("indiv prop table address");
51 case INHERIT_INDIV_MV: return("inherited indiv p value");
52 case MAIN_MV: return("ref to Main");
53 case SYMBOL_MV: return("ref to symbol value");
55 /* Additional marker values used in module backpatching */
57 case VARIABLE_MV: return("global variable");
58 case IDENT_MV: return("prop identifier number");
59 case ACTION_MV: return("action");
60 case OBJECT_MV: return("internal object");
62 /* Record types in the import/export table (not really marker
65 case EXPORT_MV: return("Export ");
66 case EXPORTSF_MV: return("Export sf");
67 case EXPORTAC_MV: return("Export ##");
68 case IMPORT_MV: return("Import ");
70 return("** No such MV **");
73 /* ------------------------------------------------------------------------- */
74 /* Import/export records */
75 /* ------------------------------------------------------------------------- */
77 typedef struct importexport_s
86 static void describe_importexport(ImportExport *I)
87 { printf("%8s %20s %04d %04x %s\n",
88 describe_mv(I->module_value), I->symbol_name,
89 I->symbol_number, I->symbol_value, typename(I->symbol_type));
92 /* ========================================================================= */
93 /* Linking in external modules: this code is run when the external */
94 /* program hits a Link directive. */
95 /* ------------------------------------------------------------------------- */
96 /* This map is between global variable numbers in the module and in the */
97 /* external program: variables_map[n] will be the external global variable */
98 /* no for module global variable no n. (The entries [0] to [15] are not */
100 /* ------------------------------------------------------------------------- */
102 static int variables_map[256], actions_map[256];
104 int32 module_map[16];
108 /* ------------------------------------------------------------------------- */
109 /* These are offsets within the module: */
110 /* ------------------------------------------------------------------------- */
112 static int32 m_code_offset, m_strs_offset, m_static_offset, m_dict_offset,
113 m_vars_offset, m_objs_offset, m_props_offset, m_class_numbers,
114 m_individuals_offset, m_individuals_length;
116 static int m_no_objects, m_no_globals, p_no_globals, lowest_imported_global_no;
118 int32 *xref_table; int xref_top;
119 int32 *property_identifier_map;
120 int *accession_numbers_map;
121 int32 routine_replace[64],
122 routine_replace_with[64]; int no_rr;
124 /* ------------------------------------------------------------------------- */
125 /* Reading and writing bytes/words in the module (as loaded in), indexing */
126 /* via "marker addresses". */
127 /* ------------------------------------------------------------------------- */
129 static int32 read_marker_address(uchar *p, int size,
130 int zmachine_area, int32 offset)
132 /* A routine to read the value referred to by the marker address
133 (zmachine_area, offset): size is 1 for byte, 2 for word, and the
134 module itself resides at p. */
138 switch(zmachine_area)
140 case DYNAMIC_ARRAY_ZA:
141 addr = m_vars_offset; break;
143 addr = m_code_offset; break;
144 case STATIC_STRINGS_ZA:
145 addr = m_strs_offset; break;
147 addr = m_dict_offset; break;
149 addr = m_objs_offset; break;
151 addr = m_props_offset; break;
152 case INDIVIDUAL_PROP_ZA:
153 addr = m_individuals_offset; break;
155 if (size == 1) return p[addr+offset];
156 return 256*p[addr+offset] + p[addr+offset+1];
159 static void write_marker_address(uchar *p, int size,
160 int zmachine_area, int32 offset, int32 value)
162 /* Similar, but to write to it. */
166 switch(zmachine_area)
168 case DYNAMIC_ARRAY_ZA:
169 addr = m_vars_offset; break;
171 addr = m_code_offset; break;
172 case STATIC_STRINGS_ZA:
173 addr = m_strs_offset; break;
175 addr = m_dict_offset; break;
177 addr = m_objs_offset; break;
179 addr = m_props_offset; break;
180 case INDIVIDUAL_PROP_ZA:
181 addr = m_individuals_offset; break;
183 if (size == 1) { p[addr+offset] = value%256; return; }
184 p[addr+offset] = value/256;
185 p[addr+offset+1] = value%256;
190 static int get_next_record(uchar *p)
192 int record_type = p[m_read_pos++];
199 IE.module_value = record_type;
200 i=p[m_read_pos++]; IE.symbol_number = 256*i + p[m_read_pos++];
201 IE.symbol_type = p[m_read_pos++];
202 if (record_type != IMPORT_MV) IE.backpatch = p[m_read_pos++];
203 i=p[m_read_pos++]; IE.symbol_value = 256*i + p[m_read_pos++];
204 IE.symbol_name = (char *) (p+m_read_pos);
205 m_read_pos += strlen((char *) (p+m_read_pos))+1;
206 if (linker_trace_level >= 2) describe_importexport(&IE);
209 printf("Marker value of %d\n", record_type);
210 compiler_error("Link: illegal import/export marker value");
216 static char link_errorm[128];
218 static void accept_export(void)
219 { int32 index, map_to = IE.symbol_value % 0x10000;
220 index = symbol_index(IE.symbol_name, -1);
222 xref_table[IE.symbol_number] = index;
224 if (!(sflags[index] & UNKNOWN_SFLAG))
225 { if (IE.module_value == EXPORTAC_MV)
226 { if ((!(sflags[index] & ACTION_SFLAG))
227 && (stypes[index] != FAKE_ACTION_T))
229 "action name clash with", IE.symbol_name);
232 if (stypes[index] == IE.symbol_type)
233 { switch(IE.symbol_type)
235 if ((!(svals[index] == IE.symbol_value))
236 || (IE.backpatch != 0))
238 "program and module give differing values of", IE.symbol_name);
240 case INDIVIDUAL_PROPERTY_T:
241 property_identifier_map[IE.symbol_value] = svals[index];
244 if ((IE.module_value == EXPORTSF_MV)
245 && (sflags[index] & REPLACE_SFLAG))
249 "%s '%s' in both program and module",
250 typename(IE.symbol_type), IE.symbol_name);
251 link_error(link_errorm);
256 { sprintf(link_errorm,
257 "'%s' has type %s in program but type %s in module",
258 IE.symbol_name, typename(stypes[index]),
259 typename(IE.symbol_type));
260 link_error(link_errorm);
264 { if (IE.module_value == EXPORTAC_MV)
265 { IE.symbol_value = no_actions;
266 action_symbol[no_actions++] = index;
267 if (linker_trace_level >= 4)
268 printf("Creating action ##%s\n", (char *) symbs[index]);
271 switch(IE.symbol_type)
273 if ((IE.module_value == EXPORTSF_MV)
274 && (sflags[index] & REPLACE_SFLAG))
275 { routine_replace[no_rr] = IE.symbol_value;
276 routine_replace_with[no_rr++] = index;
279 IE.symbol_value += (zmachine_pc/scale_factor);
283 IE.symbol_value += no_objects;
286 IE.symbol_value += dynamic_array_area_size - (MAX_GLOBAL_VARIABLES*2);
288 case GLOBAL_VARIABLE_T:
291 "failed because too many extra global variables needed");
294 variables_map[16 + m_no_globals++] = 16 + no_globals;
295 set_variable_value(no_globals, IE.symbol_value);
296 IE.symbol_value = 16 + no_globals++;
298 case INDIVIDUAL_PROPERTY_T:
299 property_identifier_map[IE.symbol_value]
300 = no_individual_properties;
301 IE.symbol_value = no_individual_properties++;
303 if (debugfile_switch)
304 { debug_file_printf("<property>");
306 ("<identifier>%s</identifier>", IE.symbol_name);
308 ("<value>%d</value>", IE.symbol_value);
309 debug_file_printf("</property>");
314 assign_symbol(index, IE.backpatch*0x10000 + IE.symbol_value,
316 if (IE.backpatch != 0) sflags[index] |= CHANGE_SFLAG;
317 sflags[index] |= EXPORT_SFLAG;
318 if (IE.module_value == EXPORTSF_MV)
319 sflags[index] |= INSF_SFLAG;
320 if (IE.module_value == EXPORTAC_MV)
321 sflags[index] |= ACTION_SFLAG;
324 if (IE.module_value == EXPORTAC_MV)
325 { if (linker_trace_level >= 4)
326 printf("Map %d '%s' to %d\n",
327 IE.symbol_value, (char *) (symbs[index]), svals[index]);
328 actions_map[map_to] = svals[index];
332 static void accept_import(void)
335 index = symbol_index(IE.symbol_name, -1);
336 sflags[index] |= USED_SFLAG;
337 xref_table[IE.symbol_number] = index;
339 if (!(sflags[index] & UNKNOWN_SFLAG))
340 { switch (IE.symbol_type)
342 case GLOBAL_VARIABLE_T:
343 if (stypes[index] != GLOBAL_VARIABLE_T)
345 "module (wrongly) declared this a variable:", IE.symbol_name);
346 variables_map[IE.symbol_value] = svals[index];
347 if (IE.symbol_value < lowest_imported_global_no)
348 lowest_imported_global_no = IE.symbol_value;
351 switch(stypes[index])
354 "this attribute is undeclared within module:", IE.symbol_name);; break;
357 "this property is undeclared within module:", IE.symbol_name); break;
358 case INDIVIDUAL_PROPERTY_T:
368 "this was referred to as a constant, but isn't:", IE.symbol_name);
375 { switch (IE.symbol_type)
377 case GLOBAL_VARIABLE_T:
378 if (stypes[index] != GLOBAL_VARIABLE_T)
380 "Module tried to import a Global variable not defined here:",
382 variables_map[IE.symbol_value] = 16;
383 if (IE.symbol_value < lowest_imported_global_no)
384 lowest_imported_global_no = IE.symbol_value;
390 static int32 backpatch_backpatch(int32 v)
391 { switch(backpatch_marker)
393 /* Backpatches made now which are final */
397 backpatch_marker = NULL_MV;
401 if ((v<0) || (v>=256) || (actions_map[v] == -1))
402 { link_error("unmapped action number");
403 printf("*** Link: unmapped action number %d ***", v);
408 backpatch_marker = NULL_MV;
412 { int f = v & 0x8000;
413 v = f + property_identifier_map[v-f];
414 backpatch_marker = NULL_MV;
419 backpatch_marker = NULL_MV;
420 if (v < lowest_imported_global_no)
421 { v = v + p_no_globals; break;
423 if (variables_map[v] == -1)
424 { printf("** Unmapped variable %d! **\n", v);
425 variables_map[v] = 16;
426 link_error("unmapped variable error"); break;
428 v = variables_map[v];
431 /* Backpatch values which are themselves being backpatched */
434 v += individuals_length;
439 if ((v<0) || (v>=no_symbols))
440 { printf("** Symbol number %d cannot be crossreferenced **\n", v);
441 link_error("symbol crossreference error"); v=0;
447 v += static_strings_extent/scale_factor;
452 for (i=0;i<no_rr;i++)
453 if (v == routine_replace[i])
454 { v = routine_replace_with[i];
455 backpatch_marker = SYMBOL_MV;
458 v += zmachine_pc/scale_factor;
467 if (v < (MAX_GLOBAL_VARIABLES*2))
468 { v = 2*(variables_map[v/2 + 16] - 16);
471 { v += dynamic_array_area_size - (MAX_GLOBAL_VARIABLES*2);
476 v = accession_numbers_map[v];
480 v += properties_table_size;
483 case INHERIT_INDIV_MV:
484 v += individuals_length;
490 static void backpatch_module_image(uchar *p,
491 int marker_value, int zmachine_area, int32 offset)
492 { int size = (marker_value>=0x80)?1:2; int32 v;
493 marker_value &= 0x7f;
495 backpatch_marker = marker_value;
497 if (zmachine_area == PROP_DEFAULTS_ZA) return;
499 if (linker_trace_level >= 3)
500 printf("Backpatch %s area %d offset %04x size %d: ",
501 describe_mv(marker_value), zmachine_area, offset, size);
503 v = read_marker_address(p, size, zmachine_area, offset);
504 if (linker_trace_level >= 3) printf("%04x ", v);
506 v = backpatch_backpatch(v);
508 write_marker_address(p, size, zmachine_area, offset, v);
509 if (linker_trace_level >= 3) printf("%04x\n", v);
512 /* ------------------------------------------------------------------------- */
513 /* The main routine: linking in a module with the given filename. */
514 /* ------------------------------------------------------------------------- */
516 char current_module_filename[128];
518 void link_module(char *given_filename)
523 int32 last, i, j, k, l, m, vn, len, size, link_offset, module_size, map,
524 max_property_identifier, symbols_base = no_symbols;
526 strcpy(current_module_filename, given_filename);
528 /* (1) Load in the module to link */
532 { i = translate_link_filename(i, filename, given_filename);
533 fin=fopen(filename,"rb");
534 } while ((fin == NULL) && (i != 0));
537 { error_named("Couldn't open module file", filename); return;
540 for (i=0;i<64;i++) p0[i]=fgetc(fin);
543 if ((vn<65) || (vn>75))
544 { error_named("File isn't a module:", filename);
548 if (vn != 64 + version_number)
551 "module compiled as Version %d (so it can't link\
552 into this V%d game):", vn-64, version_number);
553 error_named(ebuff, filename);
557 module_size = (256*p0[26] + p0[27])*scale_factor;
558 p = my_malloc(module_size + 16, "link module storage");
559 /* The + 16 allows for rounding errors */
561 for (k=0;k<64;k++) p[k] = p0[k];
562 for (k=64;k<module_size;k++) p[k] = fgetc(fin);
565 if ((p0[52] != 0) || (p0[53] != 0))
566 { /* Then the module contains a character set table */
567 if (alphabet_modified)
568 { k = FALSE; m = 256*p0[52] + p0[53];
569 for (i=0;i<3;i++) for (j=0;j<26;j++)
570 { l = alphabet[i][j]; if (l == '~') l = '\"';
571 if (l != p[m]) k = TRUE;
574 link_error("module and game both define non-standard character sets, \
581 { if (alphabet_modified) k = TRUE;
585 link_error("module and game use different character sets");
588 if (i > MODULE_VERSION_NUMBER)
589 warning_named("module has a more advanced format than this release \
590 of the Inform 6 compiler knows about: it may not link in correctly", filename);
592 /* (2) Calculate offsets: see the header-writing code in "tables.c" */
594 map = (256*p[6] + p[7]);
595 for (i=0; i<16; i++) module_map[i] = 256*p[map + i*2] + p[map + i*2 + 1];
597 m_vars_offset = (256*p[12] + p[13]);
598 m_static_offset = (256*p[14] + p[15]);
599 m_dict_offset = (256*p[8] + p[9]);
600 m_code_offset = (256*p[4] + p[5]);
602 /* (3) Read the "module map" table */
604 if (linker_trace_level>=4)
605 { printf("[Reading module map:\n");
606 for (i=0; i<16; i++) printf("%04x ", module_map[i]);
610 m_objs_offset = module_map[0];
611 m_props_offset = module_map[1];
612 m_strs_offset = scale_factor*module_map[2];
613 m_class_numbers = module_map[3];
614 m_individuals_offset = module_map[4];
615 m_individuals_length = module_map[5];
617 for (i=16;i<256;i++) variables_map[i] = -1;
618 for (i=0;i<16;i++) variables_map[i] = i;
619 for (i=LOWEST_SYSTEM_VAR_NUMBER;i<256;i++) variables_map[i] = i;
621 for (i=0;i<256;i++) actions_map[i] = -1;
623 xref_table = my_calloc(sizeof(int32), module_map[6],
624 "linker cross-references table");
625 for (i=0;i<module_map[6];i++) xref_table[i] = -1;
627 max_property_identifier = module_map[7];
628 property_identifier_map = my_calloc(sizeof(int32), max_property_identifier,
629 "property identifier map");
630 for (i=0; i<max_property_identifier; i++)
631 property_identifier_map[i] = i;
633 m_no_objects = module_map[8];
634 link_offset = module_map[9];
636 m_no_globals = 0; p_no_globals = no_globals;
637 lowest_imported_global_no=236;
641 if ((linker_trace_level>=1) || transcript_switch)
642 { char link_banner[128];
644 "[Linking release %d.%c%c%c%c%c%c of module '%s' (size %dK)]",
645 p[2]*256 + p[3], p[18], p[19], p[20], p[21], p[22], p[23],
646 filename, module_size/1024);
647 if (linker_trace_level >= 1) printf("%s\n", link_banner);
648 if (transcript_switch)
649 write_to_transcript_file(link_banner);
652 /* (4) Merge in the dictionary */
654 if (linker_trace_level >= 2)
655 printf("Merging module's dictionary at %04x\n", m_dict_offset);
656 k=m_dict_offset; k+=p[k]+1;
658 size = p[k]*256 + p[k+1]; k+=2;
660 accession_numbers_map = my_calloc(sizeof(int), size,
661 "dictionary accession numbers map");
663 for (i=0;i<size;i++, k+=len)
665 word_to_ascii(p+k,word);
666 if (linker_trace_level >= 3)
667 printf("%03d %04x '%s' %02x %02x %02x\n",i,k,
668 word, p[k+len-3], p[k+len-2], p[k+len-1]);
670 accession_numbers_map[i]
671 = dictionary_add(word, p[k+len-3], p[k+len-2], p[k+len-1]);
674 /* (5) Run through import/export table */
676 m_read_pos = module_map[9];
677 if (linker_trace_level>=2)
678 printf("Import/export table is at byte offset %04x\n", m_read_pos);
681 { record_type = get_next_record(p);
682 if (((record_type == EXPORT_MV) || (record_type == EXPORTSF_MV))
683 && (IE.symbol_type == INDIVIDUAL_PROPERTY_T))
684 { int32 si = symbol_index(IE.symbol_name, -1);
685 property_identifier_map[IE.symbol_value] = svals[si];
691 accept_export(); break;
693 accept_import(); break;
695 } while (record_type != 0);
697 if ((linker_trace_level >= 4) && (no_rr != 0))
698 { printf("Replaced routine addresses:\n");
699 for (i=0; i<no_rr; i++)
700 { printf("Replace code offset %04x with %04x\n",
701 routine_replace[i], routine_replace_with[i]);
705 if (linker_trace_level >= 4)
706 { printf("Symbol cross-references table:\n");
707 for (i=0; i<module_map[6]; i++)
708 { if (xref_table[i] != -1)
709 printf("module %4d -> story file '%s'\n", i,
710 (char *) symbs[xref_table[i]]);
714 if (linker_trace_level >= 4)
715 { printf("Action numbers map:\n");
716 for (i=0; i<256; i++)
717 if (actions_map[i] != -1)
718 printf("%3d -> %3d\n", i, actions_map[i]);
721 if ((linker_trace_level >= 4) && (max_property_identifier > 72))
722 { printf("Property identifier number map:\n");
723 for (i=72; i<max_property_identifier; i++)
724 { printf("module %04x -> program %04x\n",
725 i, property_identifier_map[i]);
729 /* (6) Backpatch the backpatch markers attached to exported symbols */
731 for (i=symbols_base; i<no_symbols; i++)
732 { if ((sflags[i] & CHANGE_SFLAG) && (sflags[i] & EXPORT_SFLAG))
733 { backpatch_marker = svals[i]/0x10000;
734 j = svals[i] % 0x10000;
736 j = backpatch_backpatch(j);
738 svals[i] = backpatch_marker*0x10000 + j;
739 if (backpatch_marker == 0) sflags[i] &= (~(CHANGE_SFLAG));
743 /* (7) Run through the Z-code backpatch table */
745 for (i=module_map[11]; i<module_map[11]+module_map[12]; i += 3)
746 { int marker_value = p[i];
747 int32 offset = 256*p[i+1] + p[i+2];
749 switch(marker_value & 0x7f)
754 backpatch_module_image(p, marker_value, ZCODE_ZA, offset);
757 backpatch_module_image(p, marker_value, ZCODE_ZA, offset);
758 write_byte_to_memory_block(&zcode_backpatch_table,
759 zcode_backpatch_size++, backpatch_marker);
760 write_byte_to_memory_block(&zcode_backpatch_table,
761 zcode_backpatch_size++, (offset + zmachine_pc)/256);
762 write_byte_to_memory_block(&zcode_backpatch_table,
763 zcode_backpatch_size++, (offset + zmachine_pc)%256);
768 /* (8) Run through the Z-machine backpatch table */
770 for (i=module_map[13]; i<module_map[13]+module_map[14]; i += 4)
771 { int marker_value = p[i], zmachine_area = p[i+1];
772 int32 offset = 256*p[i+2] + p[i+3];
778 backpatch_module_image(p, marker_value, zmachine_area, offset);
781 backpatch_module_image(p, marker_value, zmachine_area, offset);
782 switch(zmachine_area)
783 { case PROP_DEFAULTS_ZA:
786 offset += properties_table_size; break;
787 case INDIVIDUAL_PROP_ZA:
788 offset += individuals_length; break;
789 case DYNAMIC_ARRAY_ZA:
790 if (offset < (MAX_GLOBAL_VARIABLES*2))
791 { offset = 2*(variables_map[offset/2 + 16] - 16);
794 { offset += dynamic_array_area_size - (MAX_GLOBAL_VARIABLES*2);
798 backpatch_zmachine(backpatch_marker, zmachine_area, offset);
803 /* (9) Adjust initial values of variables */
805 if (linker_trace_level >= 3)
806 printf("\nFinal variables map, Module -> Main:\n");
809 if (variables_map[i]!=-1)
810 { if (linker_trace_level>=2)
811 printf("%d->%d ",i,variables_map[i]);
812 if (i<lowest_imported_global_no)
813 { int32 j = read_marker_address(p, 2,
814 DYNAMIC_ARRAY_ZA, 2*(i-16));
815 set_variable_value(variables_map[i]-16, j);
816 if (linker_trace_level>=2)
817 printf("(set var %d to %d) ",
818 variables_map[i], j);
821 if (linker_trace_level>=2) printf("\n");
823 /* (10) Glue in the dynamic array data */
825 i = m_static_offset - m_vars_offset - MAX_GLOBAL_VARIABLES*2;
826 if (dynamic_array_area_size + i >= MAX_STATIC_DATA)
827 memoryerror("MAX_STATIC_DATA", MAX_STATIC_DATA);
829 if (linker_trace_level >= 2)
830 printf("Inserting dynamic array area, %04x to %04x, at %04x\n",
831 m_vars_offset + MAX_GLOBAL_VARIABLES*2, m_static_offset,
832 variables_offset + dynamic_array_area_size);
834 { dynamic_array_area[dynamic_array_area_size+k]
835 = p[m_vars_offset+MAX_GLOBAL_VARIABLES*2+k];
837 dynamic_array_area_size+=i;
839 /* (11) Glue in the code area */
841 if (linker_trace_level >= 2)
842 printf("Inserting code area, %04x to %04x, at code offset %04x (+%04x)\n",
843 m_code_offset, m_strs_offset, code_offset, zmachine_pc);
845 for (k=m_code_offset;k<m_strs_offset;k++)
846 { if (temporary_files_switch)
847 { fputc(p[k],Temp2_fp);
851 write_byte_to_memory_block(&zcode_area, zmachine_pc++, p[k]);
854 /* (12) Glue in the static strings area */
856 if (linker_trace_level >= 2)
857 printf("Inserting strings area, %04x to %04x, \
858 at strings offset %04x (+%04x)\n",
859 m_strs_offset, link_offset, strings_offset,
860 static_strings_extent);
861 for (k=m_strs_offset;k<link_offset;k++)
862 { if (temporary_files_switch)
863 { fputc(p[k], Temp1_fp);
864 static_strings_extent++;
867 write_byte_to_memory_block(&static_strings_area,
868 static_strings_extent++, p[k]);
871 /* (13) Append the class object-numbers table: note that modules
872 provide extra information in this table */
876 { j = p[i]*256 + p[i+1]; i+=2;
879 class_object_numbers[no_classes] = j + no_objects;
880 j = p[i]*256 + p[i+1]; i+=2;
881 class_begins_at[no_classes++] = j + properties_table_size;
885 /* (14) Glue on the object tree */
887 if ((linker_trace_level>=2) && (m_no_objects>0))
888 printf("Joining on object tree of size %d\n", m_no_objects);
890 for (i=0, k=no_objects, last=m_props_offset;i<m_no_objects;i++)
891 { objectsz[no_objects].atts[0]=p[m_objs_offset+14*i];
892 objectsz[no_objects].atts[1]=p[m_objs_offset+14*i+1];
893 objectsz[no_objects].atts[2]=p[m_objs_offset+14*i+2];
894 objectsz[no_objects].atts[3]=p[m_objs_offset+14*i+3];
895 objectsz[no_objects].atts[4]=p[m_objs_offset+14*i+4];
896 objectsz[no_objects].atts[5]=p[m_objs_offset+14*i+5];
897 objectsz[no_objects].parent =
898 (p[m_objs_offset+14*i+6])*256+p[m_objs_offset+14*i+7];
899 objectsz[no_objects].next =
900 (p[m_objs_offset+14*i+8])*256+p[m_objs_offset+14*i+9];
901 objectsz[no_objects].child =
902 (p[m_objs_offset+14*i+10])*256+p[m_objs_offset+14*i+11];
903 if (linker_trace_level>=4)
904 printf("Module objects[%d] has %d,%d,%d\n",
905 i,objectsz[no_objects].parent,
906 objectsz[no_objects].next,objectsz[no_objects].child);
907 if (objectsz[no_objects].parent == 0x7fff)
908 { objectsz[no_objects].parent = 1;
909 if (objectsz[1].child == 0)
910 { objectsz[1].child = no_objects+1;
913 { int j1 = 0, j2 = objectsz[1].child;
916 j2 = objectsz[j2].next;
918 objectsz[j1].next = no_objects+1;
920 objectsz[no_objects].next = 0;
923 if (objectsz[no_objects].parent>0) objectsz[no_objects].parent += k;
924 if (objectsz[no_objects].next>0) objectsz[no_objects].next += k;
925 if (objectsz[no_objects].child>0) objectsz[no_objects].child += k;
926 objectsz[no_objects].propsize =
927 (p[m_objs_offset+14*i+12])*256+p[m_objs_offset+14*i+13];
928 last += objectsz[no_objects].propsize;
929 if (linker_trace_level>=4)
930 printf("Objects[%d] has %d,%d,%d\n",
931 no_objects,objectsz[no_objects].parent,
932 objectsz[no_objects].next,objectsz[no_objects].child);
936 /* (15) Glue on the properties */
938 if (last>m_props_offset)
939 { i = m_static_offset - m_vars_offset - MAX_GLOBAL_VARIABLES*2;
940 if (dynamic_array_area_size + i >= MAX_STATIC_DATA)
941 memoryerror("MAX_STATIC_DATA", MAX_STATIC_DATA);
943 if (linker_trace_level >= 2)
944 printf("Inserting object properties area, %04x to %04x, at +%04x\n",
945 m_props_offset, last, properties_table_size);
946 for (k=0;k<last-m_props_offset;k++)
947 properties_table[properties_table_size++] = p[m_props_offset+k];
950 /* (16) Bitwise OR Flags 2 (Z-machine requirements flags) */
952 j = p[16]*256 + p[17];
953 for (i=0, k=1;i<16;i++, k=k*2) flags2_requirements[i] |= ((j/k)%2);
955 /* (17) Append the individual property values table */
957 i = m_individuals_length;
958 if (individuals_length + i >= MAX_INDIV_PROP_TABLE_SIZE)
959 memoryerror("MAX_INDIV_PROP_TABLE_SIZE",
960 MAX_INDIV_PROP_TABLE_SIZE);
962 if (linker_trace_level >= 2)
963 printf("Inserting individual prop tables area, %04x to %04x, at +%04x\n",
964 m_individuals_offset, m_individuals_offset + i,
967 { individuals_table[individuals_length + k]
968 = p[m_individuals_offset + k];
970 individuals_length += i;
974 if (linker_trace_level >= 2)
975 printf("Link complete\n");
977 my_free(&p, "link module storage");
978 my_free(&xref_table, "linker cross-references table");
979 my_free(&property_identifier_map, "property identifier map");
980 my_free(&accession_numbers_map, "accession numbers map");
983 /* ========================================================================= */
984 /* Writing imports, exports and markers to the link data table during */
985 /* module compilation */
986 /* ------------------------------------------------------------------------- */
987 /* Writing to the link data table */
988 /* ------------------------------------------------------------------------- */
990 static void write_link_byte(int x)
991 { *link_data_top=(unsigned char) x; link_data_top++; link_data_size++;
992 if (subtract_pointers(link_data_top,link_data_holding_area)
993 >= MAX_LINK_DATA_SIZE)
994 { memoryerror("MAX_LINK_DATA_SIZE",MAX_LINK_DATA_SIZE);
998 extern void flush_link_data(void)
1000 j = subtract_pointers(link_data_top, link_data_holding_area);
1001 if (temporary_files_switch)
1002 for (i=0;i<j;i++) fputc(link_data_holding_area[i], Temp3_fp);
1005 write_byte_to_memory_block(&link_data_area, link_data_size-j+i,
1006 link_data_holding_area[i]);
1007 link_data_top=link_data_holding_area;
1010 static void write_link_word(int32 x)
1011 { write_link_byte(x/256); write_link_byte(x%256);
1014 static void write_link_string(char *s)
1016 for (i=0; s[i]!=0; i++) write_link_byte(s[i]);
1020 /* ------------------------------------------------------------------------- */
1021 /* Exports and imports */
1022 /* ------------------------------------------------------------------------- */
1024 static void export_symbols(void)
1025 { int symbol_number;
1027 for (symbol_number = 0; symbol_number < no_symbols; symbol_number++)
1028 { int export_flag = FALSE, import_flag = FALSE;
1030 if (stypes[symbol_number]==GLOBAL_VARIABLE_T)
1031 { if (svals[symbol_number] < LOWEST_SYSTEM_VAR_NUMBER)
1032 { if (sflags[symbol_number] & IMPORT_SFLAG)
1035 if (!(sflags[symbol_number] & SYSTEM_SFLAG))
1040 { if (!(sflags[symbol_number] & SYSTEM_SFLAG))
1041 { if (sflags[symbol_number] & UNKNOWN_SFLAG)
1042 { if (sflags[symbol_number] & IMPORT_SFLAG)
1046 switch(stypes[symbol_number])
1053 default: export_flag = TRUE;
1059 { if (linker_trace_level >= 1)
1060 { IE.module_value = EXPORT_MV;
1061 IE.symbol_number = symbol_number;
1062 IE.symbol_type = stypes[symbol_number];
1063 IE.symbol_value = svals[symbol_number];
1064 IE.symbol_name = (char *) (symbs[symbol_number]);
1065 describe_importexport(&IE);
1068 if (sflags[symbol_number] & ACTION_SFLAG)
1069 write_link_byte(EXPORTAC_MV);
1071 if (sflags[symbol_number] & INSF_SFLAG)
1072 write_link_byte(EXPORTSF_MV);
1074 write_link_byte(EXPORT_MV);
1076 write_link_word(symbol_number);
1077 write_link_byte(stypes[symbol_number]);
1078 if (sflags[symbol_number] & CHANGE_SFLAG)
1079 write_link_byte(svals[symbol_number] / 0x10000);
1080 else write_link_byte(0);
1081 write_link_word(svals[symbol_number] % 0x10000);
1082 write_link_string((char *) (symbs[symbol_number]));
1087 { if (linker_trace_level >= 1)
1088 { IE.module_value = IMPORT_MV;
1089 IE.symbol_number = symbol_number;
1090 IE.symbol_type = stypes[symbol_number];
1091 IE.symbol_value = svals[symbol_number];
1092 IE.symbol_name = (char *) (symbs[symbol_number]);
1093 describe_importexport(&IE);
1096 write_link_byte(IMPORT_MV);
1097 write_link_word(symbol_number);
1098 write_link_byte(stypes[symbol_number]);
1099 write_link_word(svals[symbol_number]);
1100 write_link_string((char *) (symbs[symbol_number]));
1106 /* ------------------------------------------------------------------------- */
1107 /* Marking for later importation */
1108 /* ------------------------------------------------------------------------- */
1110 int mv_vref=LOWEST_SYSTEM_VAR_NUMBER-1;
1112 void import_symbol(int32 symbol_number)
1113 { sflags[symbol_number] |= IMPORT_SFLAG;
1114 switch(stypes[symbol_number])
1115 { case GLOBAL_VARIABLE_T:
1116 assign_symbol(symbol_number, mv_vref--, stypes[symbol_number]);
1121 /* ========================================================================= */
1122 /* Data structure management routines */
1123 /* ------------------------------------------------------------------------- */
1125 extern void init_linker_vars(void)
1126 { link_data_size = 0;
1127 initialise_memory_block(&link_data_area);
1130 extern void linker_begin_pass(void)
1131 { link_data_top = link_data_holding_area;
1134 extern void linker_endpass(void)
1140 extern void linker_allocate_arrays(void)
1141 { if (!module_switch)
1142 link_data_holding_area
1143 = my_malloc(64, "link data holding area");
1145 link_data_holding_area
1146 = my_malloc(MAX_LINK_DATA_SIZE, "link data holding area");
1149 extern void linker_free_arrays(void)
1150 { my_free(&link_data_holding_area, "link data holding area");
1151 deallocate_memory_block(&link_data_area);
1154 /* ========================================================================= */