-
Notifications
You must be signed in to change notification settings - Fork 17
/
tcprsimg.cpp
2049 lines (1771 loc) · 64.7 KB
/
tcprsimg.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifdef RCSID
static char RCSid[] =
"$Header: d:/cvsroot/tads/tads3/TCPRSIMG.CPP,v 1.1 1999/07/11 00:46:53 MJRoberts Exp $";
#endif
/*
* Copyright (c) 1999, 2002 Michael J. Roberts. All Rights Reserved.
*
* Please see the accompanying license file, LICENSE.TXT, for information
* on using and copying this software.
*/
/*
Name
tcprsimg.cpp - TADS 3 Compiler Parser - image writing functions
Function
Notes
Modified
04/30/99 MJRoberts - Creation
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "os.h"
#include "t3std.h"
#include "tcprs.h"
#include "tctarg.h"
#include "tcgen.h"
#include "vmhash.h"
#include "tcmain.h"
#include "vmfile.h"
#include "tctok.h"
/* ------------------------------------------------------------------------ */
/*
* Read an object file and load the global symbol table
*/
int CTcParser::load_object_file(class CVmFile *fp, const textchar_t *fname,
tctarg_obj_id_t *obj_xlat,
tctarg_prop_id_t *prop_xlat,
ulong *enum_xlat)
{
ulong sym_cnt;
ulong dict_cnt;
ulong i;
ulong anon_cnt;
ulong nonsym_cnt;
ulong prod_cnt;
ulong exp_cnt;
/* read the number of symbol index entries */
sym_cnt = fp->read_uint4();
if (sym_cnt != 0)
{
/* allocate space for the symbol index list */
obj_sym_list_ = (CTcSymbol **)
t3malloc(sym_cnt * sizeof(obj_sym_list_[0]));
/* the list is empty so far */
obj_file_sym_idx_ = 0;
}
/* read the number of dictionary symbols */
dict_cnt = fp->read_uint4();
/* if there are any symbols, read them */
if (dict_cnt != 0)
{
/* allocate space for the dictionary index list */
obj_dict_list_ = (CTcDictEntry **)
t3malloc(dict_cnt * sizeof(obj_dict_list_[0]));
/* nothing in the list yet */
obj_file_dict_idx_ = 0;
}
/* read the number of symbols in the file */
sym_cnt = fp->read_uint4();
/* read the symbols */
for (i = 0 ; i < sym_cnt ; ++i)
{
/* load a symbol */
if (CTcSymbol::load_from_obj_file(fp, fname,
obj_xlat, prop_xlat, enum_xlat))
return 1;
}
/* read the number of anonymous object symbols */
anon_cnt = fp->read_uint4();
/* read the anonymous object symbols */
for (i = 0 ; i < anon_cnt ; ++i)
{
/* load the next anonymous object symbol */
if (CTcSymObj::load_from_obj_file(fp, fname, obj_xlat, TRUE))
return 1;
}
/* read the non-symbol object ID's */
nonsym_cnt = fp->read_uint4();
for (i = 0 ; i < nonsym_cnt ; ++i)
{
tctarg_obj_id_t id;
/* read the next non-symbol object ID */
id = (tctarg_obj_id_t)fp->read_uint4();
/*
* allocate a new ID for the object, and set the translation
* table for the new ID - this will ensure that references to
* this non-symbol object are properly fixed up
*/
obj_xlat[id] = G_cg->new_obj_id();
}
/* read the number of symbol cross-reference sections in the file */
sym_cnt = fp->read_uint4();
/* read the symbol cross-references */
for (i = 0 ; i < sym_cnt ; ++i)
{
ulong idx;
CTcSymbol *sym;
/* read the symbol index */
idx = fp->read_uint4();
/* get the symbol from the index list */
sym = get_objfile_sym(idx);
/* load the symbol's reference information */
sym->load_refs_from_obj_file(fp, fname, obj_xlat, prop_xlat);
}
/* read the number of anonymous object cross-references */
anon_cnt = fp->read_uint4();
/* read the anonymous object cross-references */
for (i = 0 ; i < anon_cnt ; ++i)
{
ulong idx;
CTcSymbol *sym;
/* read the symbol index */
idx = fp->read_uint4();
/* get the symbol from the index list */
sym = get_objfile_sym(idx);
/* load the symbol's reference information */
sym->load_refs_from_obj_file(fp, fname, obj_xlat, prop_xlat);
}
/* read the master grammar rule count */
prod_cnt = fp->read_uint4();
/* read the master grammar rule list */
for (i = 0 ; i < prod_cnt ; ++i)
{
/* read the next grammar production */
CTcGramProdEntry::load_from_obj_file(fp, prop_xlat, enum_xlat, 0);
}
/* read the number of named grammar rules */
prod_cnt = fp->read_uint4();
/* read the private grammar rules */
for (i = 0 ; i < prod_cnt ; ++i)
{
CTcSymObj *match_sym;
/* read the match object defining the rule */
match_sym = get_objfile_objsym(fp->read_uint4());
/* read the private rule list */
CTcGramProdEntry::load_from_obj_file(
fp, prop_xlat, enum_xlat, match_sym);
}
/* read the export symbol list */
exp_cnt = fp->read_uint4();
for (i = 0 ; i < exp_cnt ; ++i)
{
CTcPrsExport *exp;
/* read the next entry */
exp = CTcPrsExport::read_from_obj_file(fp);
/* if that failed, the whole load fails */
if (exp == 0)
return 1;
/* add it to our list */
add_export_to_list(exp);
}
/* done with the symbol index list - free it */
if (obj_sym_list_ != 0)
{
/* free it and forget it */
t3free(obj_sym_list_);
obj_sym_list_ = 0;
}
/* done with the dictionary index list - free it */
if (obj_dict_list_ != 0)
{
/* free the memory and forget it */
t3free(obj_dict_list_);
obj_dict_list_ = 0;
}
/* success */
return 0;
}
/* ------------------------------------------------------------------------ */
/*
* Generate code and write the image file
*/
void CTPNStmProg::build_image(class CVmFile *image_fp, uchar xor_mask,
const char tool_data[4])
{
/* generate code */
if (gen_code_for_build())
return;
/* scan the symbol table for unresolved external references */
if (G_prs->check_unresolved_externs())
return;
/*
* Finally, our task of constructing the program is complete. All
* that remains is to write the image file. Tell the code generator
* to begin the process.
*/
G_cg->write_to_image(image_fp, xor_mask, tool_data);
}
/* ------------------------------------------------------------------------ */
/*
* Generate code and write the object file
*/
void CTPNStmProg::build_object_file(class CVmFile *object_fp,
class CTcMake *make_obj)
{
/* generate code */
if (gen_code_for_build())
return;
/*
* Finally, our task of constructing the program is complete. All
* that remains is to write the image file. Tell the code generator
* to begin the process.
*/
G_cg->write_to_object_file(object_fp, make_obj);
}
/* ------------------------------------------------------------------------ */
/*
* Generate code for a build, in preparation for writing an image file
* or an object file.
*/
int CTPNStmProg::gen_code_for_build()
{
/* notify the tokenizer that parsing is done */
G_tok->parsing_done();
/* notify the code generator that we're finished parsing */
G_cg->parsing_done();
/* set the global symbol table in the code streams */
G_cs_main->set_symtab(G_prs->get_global_symtab());
G_cs_static->set_symtab(G_prs->get_global_symtab());
/* generate code for the entire program */
gen_code(TRUE, TRUE);
/*
* if we encountered any errors generating code, don't bother
* writing the image
*/
if (G_tcmain->get_error_count() != 0)
return 1;
/* return success */
return 0;
}
/* ------------------------------------------------------------------------ */
/*
* Check for unresolved external symbols. Logs an error for each
* unresolved external.
*/
int CTcParser::check_unresolved_externs()
{
int errcnt;
/* generate any synthesized code */
G_cg->build_synthesized_code();
/* note the previous error count */
errcnt = G_tcmain->get_error_count();
/* enumerate the entries with our unresolved check callback */
get_global_symtab()->enum_entries(&enum_sym_extref, this);
/*
* if the error count increased, we logged errors for unresolved
* symbols
*/
return (G_tcmain->get_error_count() > errcnt);
}
/*
* Enumeration callback - check for unresolved external references. For
* each object or function still marked "external," we'll log an error.
*/
void CTcParser::enum_sym_extref(void *, CTcSymbol *sym)
{
/* if it's an external symbol, log an error */
if (sym->is_unresolved_extern())
G_tcmain->log_error(0, 0, TC_SEV_ERROR, TCERR_UNRESOLVED_EXTERN,
(int)sym->get_sym_len(), sym->get_sym());
}
/* ------------------------------------------------------------------------ */
/*
* Build dictionaries. We go through all objects and insert their
* vocabulary words into their dictionaries.
*/
void CTcParser::build_dictionaries()
{
CTcDictEntry *dict;
CTcSymObj *sym;
/*
* enumerate our symbols to insert dictionary words - this will
* populate each dictionary's hash table with a complete list of the
* words and object associations for the dictionary
*/
get_global_symtab()->enum_entries(&enum_sym_dict, this);
/* do the same for the anonymous objects */
for (sym = anon_obj_head_ ; sym != 0 ; sym = (CTcSymObj *)sym->nxt_)
sym->build_dictionary();
/* generate the object stream for each dictionary */
for (dict = dict_head_ ; dict != 0 ; dict = dict->get_next())
{
/* generate the code (static data, actually) for this dictionary */
G_cg->gen_code_for_dict(dict);
}
}
/*
* enumeration callback - build dictionaries
*/
void CTcParser::enum_sym_dict(void *, CTcSymbol *sym)
{
/* tell this symbol to build its dictionary entries */
sym->build_dictionary();
}
/* ------------------------------------------------------------------------ */
/*
* Build grammar productions
*/
void CTcParser::build_grammar_productions()
{
CTcGramProdEntry *entry;
/*
* First, run through the symbol table and merge all of the private
* grammar rules into the master grammar rule list. Since we've
* finished linking, we've already applied all modify/replace
* overrides, hence each symbol table entry referring to an object
* will contain its final private grammar rule list. So, we can
* safely merge the private lists into the master lists at this point,
* since no more modifications to private lists are possible.
*/
get_global_symtab()->enum_entries(&build_grammar_cb, this);
/*
* iterate over the master list of productions and generate the image
* data for each one
*/
for (entry = gramprod_head_ ; entry != 0 ; entry = entry->get_next())
{
/* build this entry */
G_cg->gen_code_for_gramprod(entry);
}
}
/*
* Symbol table enumeration callback - merge match object private grammar
* rules into the master grammar rule list.
*/
void CTcParser::build_grammar_cb(void *, CTcSymbol *sym)
{
/* if this is an object, merge its private grammar list */
if (sym->get_type() == TC_SYM_OBJ)
((CTcSymObj *)sym)->merge_grammar_entry();
}
/* ------------------------------------------------------------------------ */
/*
* Apply self-reference object ID fixups. This traverses the symbol
* table and applies each object's list of fixups. This can be called
* once after loading all object files.
*/
void CTcParser::apply_internal_fixups()
{
CTcSymObj *anon_obj;
/* enumerate the entries with our callback */
get_global_symtab()->enum_entries(&enum_sym_internal_fixup, this);
/* apply internal fixups to our anonymous objects */
for (anon_obj = anon_obj_head_ ; anon_obj != 0 ;
anon_obj = (CTcSymObj *)anon_obj->nxt_)
{
/* apply internal fixups to this symbol */
anon_obj->apply_internal_fixups();
}
}
/*
* Enumeration callback - apply internal ID fixups
*/
void CTcParser::enum_sym_internal_fixup(void *, CTcSymbol *sym)
{
/* apply its self-reference fixups */
sym->apply_internal_fixups();
}
/* ------------------------------------------------------------------------ */
/*
* Basic symbol class - image/object file functions
*/
/*
* Read a symbol from an object file
*/
int CTcSymbolBase::load_from_obj_file(CVmFile *fp, const textchar_t *fname,
tctarg_obj_id_t *obj_xlat,
tctarg_prop_id_t *prop_xlat,
ulong *enum_xlat)
{
tc_symtype_t typ;
/*
* read the type - this is the one thing we know is always present
* for every symbol (the rest of the data might vary per subclass)
*/
typ = (tc_symtype_t)fp->read_uint2();
/* create the object based on the type */
switch(typ)
{
case TC_SYM_FUNC:
return CTcSymFunc::load_from_obj_file(fp, fname);
case TC_SYM_OBJ:
return CTcSymObj::load_from_obj_file(fp, fname, obj_xlat, FALSE);
case TC_SYM_PROP:
return CTcSymProp::load_from_obj_file(fp, fname, prop_xlat);
case TC_SYM_ENUM:
return CTcSymEnum::load_from_obj_file(fp, fname, enum_xlat);
case TC_SYM_BIF:
return CTcSymBif::load_from_obj_file(fp, fname);
case TC_SYM_METACLASS:
return CTcSymMetaclass::load_from_obj_file(fp, fname, obj_xlat);
default:
/* other types should not be in an object file */
G_tcmain->log_error(0, 0, TC_SEV_ERROR, TCERR_OBJFILE_INV_TYPE);
/* return an error indication */
return 1;
}
}
/*
* Log a conflict with another symbol from an object file
*/
void CTcSymbolBase::log_objfile_conflict(const textchar_t *fname,
tc_symtype_t new_type) const
{
static const textchar_t *type_name[] =
{
"unknown", "function", "object", "property", "local",
"parameter", "intrinsic function", "native function", "code label",
"intrinsic class", "enum"
};
/*
* if the types differ, log an error indicating the different types;
* otherwise, simply log an error indicating the redefinition
*/
if (new_type != get_type())
{
/* the types differ - show the two types */
G_tcmain->log_error(0, 0, TC_SEV_ERROR, TCERR_OBJFILE_REDEF_SYM_TYPE,
(int)get_sym_len(), get_sym(),
type_name[get_type()], type_name[new_type],
fname);
}
else
{
/* the types are the same */
G_tcmain->log_error(0, 0, TC_SEV_ERROR, TCERR_OBJFILE_REDEF_SYM,
(int)get_sym_len(), get_sym(),
type_name[new_type], fname);
}
}
/* ------------------------------------------------------------------------ */
/*
* Function Symbol subclass - image/object file functions
*/
/*
* Load from an object file
*/
int CTcSymFuncBase::load_from_obj_file(CVmFile *fp,
const textchar_t *fname)
{
char txtbuf[4096];
const char *txt;
size_t len;
char buf[12];
int is_extern;
int ext_replace;
int ext_modify;
int has_retval;
int varargs;
int argc;
int opt_argc;
int is_multimethod, is_multimethod_base;
int mod_base_cnt;
CTcSymFunc *sym;
/*
* Read the symbol name. Use a custom reader instead of the base
* reader, because function symbols can be quite long, due to
* multimethod name decoration.
*/
if ((txt = CTcParser::read_len_prefix_str(
fp, txtbuf, sizeof(txtbuf), 0, TCERR_SYMEXP_SYM_TOO_LONG)) == 0)
return 1;
len = strlen(txt);
/* read our extra data */
fp->read_bytes(buf, 12);
argc = osrp2(buf);
opt_argc = osrp2(buf + 2);
varargs = buf[4];
has_retval = buf[5];
is_extern = buf[6];
ext_replace = buf[7];
ext_modify = buf[8];
is_multimethod = (buf[9] & 1) != 0;
is_multimethod_base = (buf[9] & 2) != 0;
mod_base_cnt = osrp2(buf + 10);
/* look up any existing symbol */
sym = (CTcSymFunc *)G_prs->get_global_symtab()->find(txt, len);
/*
* If this symbol is already defined, make sure the original
* definition is a function, and make sure that it's only defined
* (not referenced as external) once. If it's not defined, define
* it anew.
*/
if (sym == 0)
{
/*
* It's not defined yet - create the new definition and add it
* to the symbol table.
*/
sym = new CTcSymFunc(txt, len, FALSE, argc, opt_argc, varargs,
has_retval, is_multimethod, is_multimethod_base,
is_extern, TRUE);
G_prs->get_global_symtab()->add_entry(sym);
/* it's an error if we're replacing a previously undefined function */
if (ext_replace || ext_modify)
G_tcmain->log_error(0, 0, TC_SEV_ERROR,
TCERR_OBJFILE_REPFUNC_BEFORE_ORIG,
(int)len, txt, fname);
}
else if (sym->get_type() != TC_SYM_FUNC
|| (!sym->is_extern()
&& !is_extern && !ext_replace && !ext_modify))
{
/*
* It's already defined, but it's not a function, or this is a
* non-extern/replaced definition and the symbol is already
* defined non-extern - log a symbol type conflict error.
*/
sym->log_objfile_conflict(fname, TC_SYM_FUNC);
/*
* proceed despite the error, since this is merely a symbol
* conflict and not a file corruption - create a fake symbol to
* hold the information, so that we can read the data and thus
* keep in sync with the file, but don't bother adding the fake
* symbol object to the symbol table
*/
sym = new CTcSymFunc(txt, len, FALSE, argc, opt_argc, varargs,
has_retval, is_multimethod, is_multimethod_base,
is_extern, TRUE);
}
else if (sym->get_argc() != argc
|| sym->is_varargs() != varargs
|| sym->has_retval() != has_retval)
{
/* the symbol has an incompatible definition - log the error */
G_tcmain->log_error(0, 0, TC_SEV_ERROR, TCERR_OBJFILE_FUNC_INCOMPAT,
(int)len, txt, fname);
}
else if (sym->is_multimethod() != is_multimethod
|| sym->is_multimethod_base() != is_multimethod_base)
{
/* the multi-method status conflicts */
G_tcmain->log_error(0, 0, TC_SEV_ERROR, TCERR_OBJFILE_MMFUNC_INCOMPAT,
(int)len, txt, fname);
}
/*
* if this is a non-extern definition, we now have the object
* defined -- remove the 'extern' flag from the symbol table entry
* in this case
*/
if (!is_extern)
{
/* mark the symbol as defined */
sym->set_extern(FALSE);
/*
* if we're replacing it, delete the original; if we're modifying
* it, chain the original into our modify list
*/
if (ext_replace)
{
int i;
/*
* mark the previous code anchor as obsolete so that we
* don't write its code to the image file
*/
if (sym->get_anchor() != 0)
sym->get_anchor()->set_replaced(TRUE);
/*
* Mark all of the modified base function code offsets as
* replaced as well.
*/
for (i = 0 ; i < sym->get_mod_base_offset_count() ; ++i)
{
CTcStreamAnchor *anchor;
/* get the anchor for this offset */
anchor = G_cs->find_anchor(sym->get_mod_base_offset(i));
/* mark it as replaced */
if (anchor != 0)
anchor->set_replaced(TRUE);
}
/*
* We can now forget everything in the modify base list, as
* everything in the list is being replaced and is thus no
* longer relevant.
*/
sym->clear_mod_base_offsets();
}
else if (ext_modify)
{
/*
* We're modifying an external symbol. The anchor to the code
* stream object that we previously loaded is actually the
* anchor to the modified base object, not to the new meaning
* of the symbol, so detach the anchor from our symbol.
*/
sym->get_anchor()->detach_from_symbol();
/*
* The object file has a fixup list for references to the
* external base object that we're modifying. In other words,
* these are external references from the object file we're
* loading to the now-nameless code stream object that we're
* replacing, which is the code stream object at our anchor.
* So, load those fixups into the anchor's new internal fixup
* list. It's important to note that these aren't references
* to this symbol - they're specifically references to the
* modified base code stream object.
*/
CTcAbsFixup::load_fixup_list_from_object_file(
fp, fname, sym->get_anchor()->fixup_list_head_);
/*
* Add the old code stream anchor to the list of modified base
* offsets for the function. The function we're reading from
* the object file modifies this as a base function, so we need
* to add this to the list of modified base functions.
*/
sym->add_mod_base_offset(sym->get_anchor()->get_ofs());
/*
* Complete the dissociation from the anchor by forgetting the
* anchor in the symbol. This will allow the code stream
* object that's associated with this symbol in the current
* file to take over the anchor duty for this symbol, which
* will ensure that all fixups that reference this symbol will
* be resolved to the new code stream object.
*/
sym->set_anchor(0);
}
}
/*
* Read the list of modified base function offsets. Each entry is a
* code stream offset, so adjust each using the base code stream offset
* for this object file.
*/
for ( ; mod_base_cnt != 0 ; --mod_base_cnt)
{
int i;
/* read them */
for (i = 0 ; i < mod_base_cnt ; ++i)
{
/* read the offset, adjusting to the object file start position */
ulong ofs = fp->read_uint4() + G_cs->get_object_file_start_ofs();
/* append this item */
sym->add_mod_base_offset(ofs);
}
}
/* if it's extern, load the fixup list */
if (is_extern)
{
/*
* This is an external reference, so we must load our fixup
* list, adding it to any fixup list that already exists with
* the symbol.
*/
CTcAbsFixup::
load_fixup_list_from_object_file(fp, fname, &sym->fixups_);
}
/* success */
return 0;
}
/* ------------------------------------------------------------------------ */
/*
* object symbol entry base - image/object file functions
*/
/*
* Load from an object file
*/
int CTcSymObjBase::load_from_obj_file(CVmFile *fp,
const textchar_t *fname,
tctarg_obj_id_t *obj_xlat,
int anon)
{
/*
* do the main loading - if it fails to return a symbol, return
* failure (i.e., non-zero)
*/
return (load_from_obj_file_main(fp, fname, obj_xlat, 0, 0, anon) == 0);
}
/*
* Load a modified base object from an object file
*/
CTcSymObj *CTcSymObjBase::
load_from_obj_file_modbase(class CVmFile *fp, const textchar_t *fname,
tctarg_obj_id_t *obj_xlat,
const textchar_t *mod_name,
size_t mod_name_len, int anon)
{
/* skip the type prefix - we know it's an object */
fp->read_uint2();
/* load the object and return the symbol */
return load_from_obj_file_main(fp, fname, obj_xlat,
mod_name, mod_name_len, anon);
}
/*
* Load from an object file. This main routine does most of the work,
* and returns the loaded symbol.
*
* 'mod_name' is the primary symbol name for a stack of 'modify'
* objects. Each of the objects in a 'modify' stack, except for the
* topmost (i.e., last defined) object, has a fake symbol name, since
* the program can't refer directly to the base object once modified.
* However, while loading, we must know the actual name for the entire
* stack, so that we can link the bottom of the stack in this object
* file to the top of the stack in another object file if the bottom of
* our stack is declared external (i.e., this object file's source code
* used 'modify' with an external object). If we're loading a top-level
* object, not a modified object, 'mod_name' should be null.
*/
CTcSymObj *CTcSymObjBase::
load_from_obj_file_main(CVmFile *fp, const textchar_t *fname,
tctarg_obj_id_t *obj_xlat,
const textchar_t *mod_name, size_t mod_name_len,
int anon)
{
/* presume we won't have to use a fake symbol */
int use_fake_sym = FALSE;
/* presume we won't be able to read a stream offset */
int stream_ofs_valid = FALSE;
ulong stream_ofs = 0;
/* read the symbol name information if it's not anonymous */
const char *txt;
if (!anon)
{
/* read the symbol name */
if ((txt = base_read_from_sym_file(fp)) == 0)
return 0;
}
else
{
/* use ".anon" as our symbol name placeholder */
txt = ".anon";
}
/* get the symbol len */
size_t len = strlen(txt);
/* read our extra data */
char buf[32];
fp->read_bytes(buf, 17);
ulong id = t3rp4u(buf);
int is_extern = buf[4];
int ext_replace_flag = buf[5];
int modified_flag = buf[6];
int modify_flag = buf[7];
int ext_modify_flag = buf[8];
int class_flag = buf[9];
int trans_flag = buf[10];
tc_metaclass_t meta = (tc_metaclass_t)osrp2(buf + 11);
uint dict_idx = osrp2(buf + 13);
uint obj_file_idx = osrp2(buf + 15);
/*
* if we're not external, read our stream offset, and adjust for the
* object stream base in the object file
*/
if (!is_extern)
{
/* get the appropriate stream */
CTcDataStream *stream = get_stream_from_meta(meta);
/* read the relative stream offset */
stream_ofs = fp->read_uint4();
/*
* Ensure the stream offset was actually valid. It must be valid
* unless the object has no stream (for example, dictionary and
* grammar production objects are not generated until link time,
* hence they don't have to have - indeed, can't have - valid
* stream offsets when we're loading an object file).
*/
assert(stream_ofs != 0xffffffff || stream == 0);
/* determine if it's valid */
if (stream_ofs != 0xffffffff)
{
/* adjust it relative to this object file's stream base */
stream_ofs += stream->get_object_file_start_ofs();
/* note that it's valid */
stream_ofs_valid = TRUE;
}
else
{
/* the stream offset is not valid */
stream_ofs_valid = FALSE;
}
}
/* we have no deleted properties yet */
CTcObjPropDel *del_prop_head = 0;
/* if this is a 'modify' object, read some additional data */
if (modify_flag)
{
uint cnt;
/* read the deleted property list */
for (cnt = fp->read_uint2() ; cnt != 0 ; --cnt)
{
const char *prop_name;
CTcSymProp *prop_sym;
/* read the symbol name from the file */
prop_name = base_read_from_sym_file(fp);
if (prop_name == 0)
return 0;
/*
* find the property symbol, or define it if it's not
* already defined as a property
*/
prop_sym = (CTcSymProp *)G_prs->get_global_symtab()
->find_or_def_prop(prop_name, strlen(prop_name),
FALSE);
/* make sure it's a property */
if (prop_sym->get_type() != TC_SYM_PROP)
{
/* it's not a property - log the conflict */
prop_sym->log_objfile_conflict(fname, TC_SYM_PROP);
}
else
{
/* add the entry to my list */
add_del_prop_to_list(&del_prop_head, prop_sym);
}
}
}
/* read the self-reference fixup list */
CTcIdFixup *fixups = 0;
CTcIdFixup::load_object_file(fp, 0, 0, TCGEN_XLAT_OBJ,
4, fname, &fixups);
/*
* if this is a 'modify' object, load the base object - this is the
* original version of the object, which this object modifies
*/
CTcSymObj *mod_base_sym = 0;
if (modify_flag)
{
/*
* Load the base object - pass the top-level object's name
* (which is our own name if the caller didn't pass an enclosing
* top-level object to us). Note that we must read, and can
* immediately discard, the type data in the object file - we
* know that the base symbol is going to be an object, since we
* always write it out at this specific place in the file, but
* we will have written the type information anyway; thus, we
* don't need the type information, but we must at least skip it
* in the file.
*/
mod_base_sym =
load_from_obj_file_modbase(fp, fname, obj_xlat,
mod_name != 0 ? mod_name : txt,
mod_name != 0 ? mod_name_len : len,
FALSE);
/* if that failed, return failure */
if (mod_base_sym == 0)
return 0;
}
/*
* If this is a 'modifed extern' symbol, it's just a placeholder to
* connect the bottom object in the stack of modified objects in
* this file with the top object in another object file.
*/
CTcSymObj *sym = 0;
if (is_extern && modified_flag)
{
/*
* We're modifying an external object. This must be the bottom
* object in the stack for this object file, and serves as a
* placeholder for the top object in a stack in another object
* file. We must find the object with the name of our top-level
* object (not the fake name for this modified base object, but
* the real name for the top-level object, because the symbol
* we're modifying in the other file is the top object in its
* stack, if any). So, look up the symbol in the other file,
* which must already be loaded.
*/
sym = (CTcSymObj *)