]> git.sur5r.net Git - cc65/blob - src/ld65/exports.c
Fixed a bug: Wrong variable size trucates data.
[cc65] / src / ld65 / exports.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 exports.c                                 */
4 /*                                                                           */
5 /*                   Exports handling for the ld65 linker                    */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2011, Ullrich von Bassewitz                                      */
10 /*                Roemerstrasse 52                                           */
11 /*                D-70794 Filderstadt                                        */
12 /* EMail:         uz@cc65.org                                                */
13 /*                                                                           */
14 /*                                                                           */
15 /* This software is provided 'as-is', without any expressed or implied       */
16 /* warranty.  In no event will the authors be held liable for any damages    */
17 /* arising from the use of this software.                                    */
18 /*                                                                           */
19 /* Permission is granted to anyone to use this software for any purpose,     */
20 /* including commercial applications, and to alter it and redistribute it    */
21 /* freely, subject to the following restrictions:                            */
22 /*                                                                           */
23 /* 1. The origin of this software must not be misrepresented; you must not   */
24 /*    claim that you wrote the original software. If you use this software   */
25 /*    in a product, an acknowledgment in the product documentation would be  */
26 /*    appreciated but is not required.                                       */
27 /* 2. Altered source versions must be plainly marked as such, and must not   */
28 /*    be misrepresented as being the original software.                      */
29 /* 3. This notice may not be removed or altered from any source              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39
40 /* common */
41 #include "addrsize.h"
42 #include "check.h"
43 #include "hashstr.h"
44 #include "symdefs.h"
45 #include "xmalloc.h"
46
47 /* ld65 */
48 #include "condes.h"
49 #include "error.h"
50 #include "exports.h"
51 #include "expr.h"
52 #include "fileio.h"
53 #include "global.h"
54 #include "lineinfo.h"
55 #include "memarea.h"
56 #include "objdata.h"
57 #include "spool.h"
58
59
60
61 /*****************************************************************************/
62 /*                                   Data                                    */
63 /*****************************************************************************/
64
65
66
67 /* Hash table */
68 #define HASHTAB_MASK    0x0FFFU
69 #define HASHTAB_SIZE    (HASHTAB_MASK + 1)
70 static Export*          HashTab[HASHTAB_SIZE];
71
72 /* Import management variables */
73 static unsigned         ImpCount = 0;           /* Import count */
74 static unsigned         ImpOpen  = 0;           /* Count of open imports */
75
76 /* Export management variables */
77 static unsigned         ExpCount = 0;           /* Export count */
78 static Export**         ExpPool  = 0;           /* Exports array */
79
80 /* Defines for the flags in Import */
81 #define IMP_INLIST      0x0001U                 /* Import is in exports list */
82
83 /* Defines for the flags in Export */
84 #define EXP_INLIST      0x0001U                 /* Export is in exports list */
85 #define EXP_USERMARK    0x0002U                 /* User setable flag */
86
87
88
89 /*****************************************************************************/
90 /*                              Import handling                              */
91 /*****************************************************************************/
92
93
94
95 static Export* NewExport (unsigned char Type, unsigned char AddrSize,
96                           unsigned Name, ObjData* Obj);
97 /* Create a new export and initialize it */
98
99
100
101 static Import* NewImport (unsigned char AddrSize, ObjData* Obj)
102 /* Create a new import and initialize it */
103 {
104     /* Allocate memory */
105     Import* I    = xmalloc (sizeof (Import));
106
107     /* Initialize the fields */
108     I->Next      = 0;
109     I->Obj       = Obj;
110     I->LineInfos = EmptyCollection;
111     I->Exp       = 0;
112     I->Name      = INVALID_STRING_ID;
113     I->Flags     = 0;
114     I->AddrSize  = AddrSize;
115
116     /* Return the new structure */
117     return I;
118 }
119
120
121
122 void FreeImport (Import* I)
123 /* Free an import. NOTE: This won't remove the import from the exports table,
124  * so it may only be called for unused imports (imports from modules that
125  * aren't referenced).
126  */
127 {
128     /* Safety */
129     PRECONDITION ((I->Flags & IMP_INLIST) == 0);
130
131     /* Free the line info collection */
132     DoneCollection (&I->LineInfos);
133
134     /* Free the struct */
135     xfree (I);
136 }
137
138
139
140 Import* ReadImport (FILE* F, ObjData* Obj)
141 /* Read an import from a file and return it */
142 {
143     Import* I;
144
145     /* Read the import address size */
146     unsigned char AddrSize = Read8 (F);
147
148     /* Create a new import */
149     I = NewImport (AddrSize, Obj);
150
151     /* Read the name */
152     I->Name = MakeGlobalStringId (Obj, ReadVar (F));
153
154     /* Read the line infos */
155     ReadLineInfoList (F, Obj, &I->LineInfos);
156
157     /* Check the address size */
158     if (I->AddrSize == ADDR_SIZE_DEFAULT || I->AddrSize > ADDR_SIZE_LONG) {
159         /* Beware: This function may be called in cases where the object file
160          * is not read completely into memory. In this case, the file list is
161          * invalid. Be sure not to access it in this case.
162          */
163         if (ObjHasFiles (I->Obj)) {
164             const LineInfo* LI = GetImportPos (I);
165             Error ("Invalid import size in for `%s', imported from %s(%lu): 0x%02X",
166                    GetString (I->Name),
167                    GetSourceName (LI),
168                    GetSourceLine (LI),
169                    I->AddrSize);
170         } else {
171             Error ("Invalid import size in for `%s', imported from %s: 0x%02X",
172                    GetString (I->Name),
173                    GetObjFileName (I->Obj),
174                    I->AddrSize);
175         }
176     }
177
178     /* Return the new import */
179     return I;
180 }
181
182
183
184 Import* GenImport (unsigned Name, unsigned char AddrSize)
185 /* Generate a new import with the given name and address size and return it */
186 {
187     /* Create a new import */
188     Import* I = NewImport (AddrSize, 0);
189
190     /* Read the name */
191     I->Name = Name;
192
193     /* Check the address size */
194     if (I->AddrSize == ADDR_SIZE_DEFAULT || I->AddrSize > ADDR_SIZE_LONG) {
195         /* Beware: This function may be called in cases where the object file
196          * is not read completely into memory. In this case, the file list is
197          * invalid. Be sure not to access it in this case.
198          */
199         if (ObjHasFiles (I->Obj)) {
200             const LineInfo* LI = GetImportPos (I);
201             Error ("Invalid import size in for `%s', imported from %s(%lu): 0x%02X",
202                    GetString (I->Name),
203                    GetSourceName (LI),
204                    GetSourceLine (LI),
205                    I->AddrSize);
206         } else {
207             Error ("Invalid import size in for `%s', imported from %s: 0x%02X",
208                    GetString (I->Name),
209                    GetObjFileName (I->Obj),
210                    I->AddrSize);
211         }
212     }
213
214     /* Return the new import */
215     return I;
216 }
217
218
219
220 Import* InsertImport (Import* I)
221 /* Insert an import into the table, return I */
222 {
223     Export* E;
224
225     /* As long as the import is not inserted, V.Name is valid */
226     unsigned Name = I->Name;
227
228     /* Create a hash value for the given name */
229     unsigned Hash = (Name & HASHTAB_MASK);
230
231     /* Search through the list in that slot and print matching duplicates */
232     if (HashTab[Hash] == 0) {
233         /* The slot is empty, we need to insert a dummy export */
234         E = HashTab[Hash] = NewExport (0, ADDR_SIZE_DEFAULT, Name, 0);
235         ++ExpCount;
236     } else {
237         E = HashTab [Hash];
238         while (1) {
239             if (E->Name == Name) {
240                 /* We have an entry, L points to it */
241                 break;
242             }
243             if (E->Next == 0) {
244                 /* End of list an entry not found, insert a dummy */
245                 E->Next = NewExport (0, ADDR_SIZE_DEFAULT, Name, 0);
246                 E = E->Next;            /* Point to dummy */
247                 ++ExpCount;             /* One export more */
248                 break;
249             } else {
250                 E = E->Next;
251             }
252         }
253     }
254
255     /* Ok, E now points to a valid exports entry for the given import. Insert
256      * the import into the imports list and update the counters.
257      */
258     I->Exp     = E;
259     I->Next    = E->ImpList;
260     E->ImpList = I;
261     E->ImpCount++;
262     ++ImpCount;                 /* Total import count */
263     if (E->Expr == 0) {
264         /* This is a dummy export */
265         ++ImpOpen;
266     }
267
268     /* Mark the import so we know it's in the list */
269     I->Flags |= IMP_INLIST;
270
271     /* Return the import to allow shorter code */
272     return I;
273 }
274
275
276
277 const LineInfo* GetImportPos (const Import* I)
278 /* Return the basic line info of an import */
279 {
280     /* Source file position is always in slot zero */
281     return CollConstAt (&I->LineInfos, 0);
282 }
283
284
285
286 /*****************************************************************************/
287 /*                                   Code                                    */
288 /*****************************************************************************/
289
290
291
292 static Export* NewExport (unsigned char Type, unsigned char AddrSize,
293                           unsigned Name, ObjData* Obj)
294 /* Create a new export and initialize it */
295 {
296     /* Allocate memory */
297     Export* E = xmalloc (sizeof (Export));
298
299     /* Initialize the fields */
300     E->Name      = Name;
301     E->Next      = 0;
302     E->Flags     = 0;
303     E->Obj       = Obj;
304     E->ImpCount  = 0;
305     E->ImpList   = 0;
306     E->Expr      = 0;
307     E->LineInfos = EmptyCollection;
308     E->Type      = Type;
309     E->AddrSize  = AddrSize;
310     memset (E->ConDes, 0, sizeof (E->ConDes));
311
312     /* Return the new entry */
313     return E;
314 }
315
316
317
318 void FreeExport (Export* E)
319 /* Free an export. NOTE: This won't remove the export from the exports table,
320  * so it may only be called for unused exports (exports from modules that
321  * aren't referenced).
322  */
323 {
324     /* Safety */
325     PRECONDITION ((E->Flags & EXP_INLIST) == 0);
326
327     /* Free the line infos */
328     DoneCollection (&E->LineInfos);
329
330     /* Free the export expression */
331     FreeExpr (E->Expr);
332
333     /* Free the struct */
334     xfree (E);
335 }
336
337
338
339 Export* ReadExport (FILE* F, ObjData* O)
340 /* Read an export from a file */
341 {
342     unsigned      ConDesCount;
343     Export* E;
344
345     /* Read the type */
346     unsigned Type = ReadVar (F);
347
348     /* Read the address size */
349     unsigned char AddrSize = Read8 (F);
350
351     /* Create a new export without a name */
352     E = NewExport (Type, AddrSize, INVALID_STRING_ID, O);
353
354     /* Read the constructor/destructor decls if we have any */
355     ConDesCount = SYM_GET_CONDES_COUNT (Type);
356     if (ConDesCount > 0) {
357
358         unsigned char ConDes[CD_TYPE_COUNT];
359         unsigned I;
360
361         /* Read the data into temp storage */
362         ReadData (F, ConDes, ConDesCount);
363
364         /* Re-order the data. In the file, each decl is encoded into a byte
365          * which contains the type and the priority. In memory, we will use
366          * an array of types which contain the priority. This array was
367          * cleared by the constructor (NewExport), so we must only set the
368          * fields that contain values.
369          */
370         for (I = 0; I < ConDesCount; ++I) {
371             unsigned ConDesType = CD_GET_TYPE (ConDes[I]);
372             unsigned ConDesPrio = CD_GET_PRIO (ConDes[I]);
373             E->ConDes[ConDesType] = ConDesPrio;
374         }
375     }
376
377     /* Read the name */
378     E->Name = MakeGlobalStringId (O, ReadVar (F));
379
380     /* Read the value */
381     if (SYM_IS_EXPR (Type)) {
382         E->Expr = ReadExpr (F, O);
383     } else {
384         E->Expr = LiteralExpr (Read32 (F), O);
385     }
386
387     /* Last is the file position where the definition was done */
388     ReadLineInfoList (F, O, &E->LineInfos);
389
390     /* Return the new export */
391     return E;
392 }
393
394
395
396 void InsertExport (Export* E)
397 /* Insert an exported identifier and check if it's already in the list */
398 {
399     Export* L;
400     Export* Last;
401     Import* Imp;
402     unsigned Hash;
403
404     /* Mark the export as inserted */
405     E->Flags |= EXP_INLIST;
406
407     /* Insert the export into any condes tables if needed */
408     if (SYM_IS_CONDES (E->Type)) {
409         ConDesAddExport (E);
410     }
411
412     /* Create a hash value for the given name */
413     Hash = (E->Name & HASHTAB_MASK);
414
415     /* Search through the list in that slot */
416     if (HashTab[Hash] == 0) {
417         /* The slot is empty */
418         HashTab[Hash] = E;
419         ++ExpCount;
420     } else {
421
422         Last = 0;
423         L = HashTab[Hash];
424         do {
425             if (L->Name == E->Name) {
426                 /* This may be an unresolved external */
427                 if (L->Expr == 0) {
428
429                     /* This *is* an unresolved external. Use the actual export
430                      * in E instead of the dummy one in L.
431                      */
432                     E->Next     = L->Next;
433                     E->ImpCount = L->ImpCount;
434                     E->ImpList  = L->ImpList;
435                     if (Last) {
436                         Last->Next = E;
437                     } else {
438                         HashTab[Hash] = E;
439                     }
440                     ImpOpen -= E->ImpCount;     /* Decrease open imports now */
441                     xfree (L);
442                     /* We must run through the import list and change the
443                      * export pointer now.
444                      */
445                     Imp = E->ImpList;
446                     while (Imp) {
447                         Imp->Exp = E;
448                         Imp = Imp->Next;
449                     }
450                 } else {
451                     /* Duplicate entry, ignore it */
452                     Warning ("Duplicate external identifier: `%s'",
453                              GetString (L->Name));
454                 }
455                 return;
456             }
457             Last = L;
458             L = L->Next;
459
460         } while (L);
461
462         /* Insert export at end of queue */
463         Last->Next = E;
464         ++ExpCount;
465     }
466 }
467
468
469
470 const LineInfo* GetExportPos (const Export* E)
471 /* Return the basic line info of an export */
472 {
473     /* Source file position is always in slot zero */
474     return CollConstAt (&E->LineInfos, 0);
475 }
476
477
478
479 Export* CreateConstExport (unsigned Name, long Value)
480 /* Create an export for a literal date */
481 {
482     /* Create a new export */
483     Export* E = NewExport (SYM_CONST | SYM_EQUATE, ADDR_SIZE_ABS, Name, 0);
484
485     /* Assign the value */
486     E->Expr = LiteralExpr (Value, 0);
487
488     /* Insert the export */
489     InsertExport (E);
490
491     /* Return the new export */
492     return E;
493 }
494
495
496
497 Export* CreateExprExport (unsigned Name, ExprNode* Expr, unsigned char AddrSize)
498 /* Create an export for an expression */
499 {
500     /* Create a new export */
501     Export* E = NewExport (SYM_EXPR | SYM_EQUATE, AddrSize, Name, 0);
502
503     /* Assign the value expression */
504     E->Expr = Expr;
505
506     /* Insert the export */
507     InsertExport (E);
508
509     /* Return the new export */
510     return E;
511 }
512
513
514
515 Export* CreateMemoryExport (unsigned Name, MemoryArea* Mem, unsigned long Offs)
516 /* Create an relative export for a memory area offset */
517 {
518     /* Create a new export */
519     Export* E = NewExport (SYM_EXPR | SYM_LABEL, ADDR_SIZE_ABS, Name, 0);
520
521     /* Assign the value */
522     E->Expr = MemoryExpr (Mem, Offs, 0);
523
524     /* Insert the export */
525     InsertExport (E);
526
527     /* Return the new export */
528     return E;
529 }
530
531
532
533 Export* CreateSegmentExport (unsigned Name, Segment* Seg, unsigned long Offs)
534 /* Create a relative export to a segment */
535 {
536     /* Create a new export */
537     Export* E = NewExport (SYM_EXPR | SYM_LABEL, Seg->AddrSize, Name, 0);
538
539     /* Assign the value */
540     E->Expr = SegmentExpr (Seg, Offs, 0);
541
542     /* Insert the export */
543     InsertExport (E);
544
545     /* Return the new export */
546     return E;
547 }
548
549
550
551 Export* CreateSectionExport (unsigned Name, Section* Sec, unsigned long Offs)
552 /* Create a relative export to a section */
553 {
554     /* Create a new export */
555     Export* E = NewExport (SYM_EXPR | SYM_LABEL, Sec->AddrSize, Name, 0);
556
557     /* Assign the value */
558     E->Expr = SectionExpr (Sec, Offs, 0);
559
560     /* Insert the export */
561     InsertExport (E);
562
563     /* Return the new export */
564     return E;
565 }
566
567
568
569 Export* FindExport (unsigned Name)
570 /* Check for an identifier in the list. Return 0 if not found, otherwise
571  * return a pointer to the export.
572  */
573 {
574     /* Get a pointer to the list with the symbols hash value */
575     Export* L = HashTab[Name & HASHTAB_MASK];
576     while (L) {
577         /* Search through the list in that slot */
578         if (L->Name == Name) {
579             /* Entry found */
580             return L;
581         }
582         L = L->Next;
583     }
584
585     /* Not found */
586     return 0;
587 }
588
589
590
591 int IsUnresolved (unsigned Name)
592 /* Check if this symbol is an unresolved export */
593 {
594     /* Find the export */
595     return IsUnresolvedExport (FindExport (Name));
596 }
597
598
599
600 int IsUnresolvedExport (const Export* E)
601 /* Return true if the given export is unresolved */
602 {
603     /* Check if it's unresolved */
604     return E != 0 && E->Expr == 0;
605 }
606
607
608
609 int IsConstExport (const Export* E)
610 /* Return true if the expression associated with this export is const */
611 {
612     if (E->Expr == 0) {
613         /* External symbols cannot be const */
614         return 0;
615     } else {
616         return IsConstExpr (E->Expr);
617     }
618 }
619
620
621
622 long GetExportVal (const Export* E)
623 /* Get the value of this export */
624 {
625     if (E->Expr == 0) {
626         /* OOPS */
627         Internal ("`%s' is an undefined external", GetString (E->Name));
628     }
629     return GetExprVal (E->Expr);
630 }
631
632
633
634 static void CheckSymType (const Export* E)
635 /* Check the types for one export */
636 {
637     /* External with matching imports */
638     Import* I = E->ImpList;
639     while (I) {
640         if (E->AddrSize != I->AddrSize) {
641             /* Export and import address sizes do not match */
642             StrBuf ExportLoc = STATIC_STRBUF_INITIALIZER;
643             StrBuf ImportLoc = STATIC_STRBUF_INITIALIZER;
644             const char* ExpAddrSize = AddrSizeToStr (E->AddrSize);
645             const char* ImpAddrSize = AddrSizeToStr (I->AddrSize);
646             const LineInfo* ExportLI = GetExportPos (E);
647             const LineInfo* ImportLI = GetImportPos (I);
648
649             /* Generate strings that describe the location of the im- and
650              * exports. This depends on the place from where they come:
651              * Object file or linker config.
652              */
653             if (E->Obj) {
654                 /* The export comes from an object file */
655                 SB_Printf (&ExportLoc, "%s, %s(%lu)",
656                            GetString (E->Obj->Name),
657                            GetSourceName (ExportLI),
658                            GetSourceLine (ExportLI));
659             } else {
660                 SB_Printf (&ExportLoc, "%s(%lu)",
661                            GetSourceName (ExportLI),
662                            GetSourceLine (ExportLI));
663             }
664             if (I->Obj) {
665                 /* The import comes from an object file */
666                 SB_Printf (&ImportLoc, "%s, %s(%lu)",
667                            GetString (I->Obj->Name),
668                            GetSourceName (ImportLI),
669                            GetSourceLine (ImportLI));
670             } else {
671                 SB_Printf (&ImportLoc, "%s(%lu)",
672                            GetSourceName (ImportLI),
673                            GetSourceLine (ImportLI));
674             }
675
676             /* Output the diagnostic */
677             Warning ("Address size mismatch for `%s': "
678                      "Exported from %s as `%s', "
679                      "import in %s as `%s'",
680                      GetString (E->Name),
681                      SB_GetConstBuf (&ExportLoc),
682                      ExpAddrSize,
683                      SB_GetConstBuf (&ImportLoc),
684                      ImpAddrSize);
685
686             /* Free the temporary strings */
687             SB_Done (&ExportLoc);
688             SB_Done (&ImportLoc);
689         }
690         I = I->Next;
691     }
692 }
693
694
695
696 static void CheckSymTypes (void)
697 /* Check for symbol tape mismatches */
698 {
699     unsigned I;
700
701     /* Print all open imports */
702     for (I = 0; I < ExpCount; ++I) {
703         const Export* E = ExpPool [I];
704         if (E->Expr != 0 && E->ImpCount > 0) {
705             /* External with matching imports */
706             CheckSymType (E);
707         }
708     }
709 }
710
711
712
713 static void PrintUnresolved (ExpCheckFunc F, void* Data)
714 /* Print a list of unresolved symbols. On unresolved symbols, F is
715  * called (see the comments on ExpCheckFunc in the data section).
716  */
717 {
718     unsigned I;
719
720     /* Print all open imports */
721     for (I = 0; I < ExpCount; ++I) {
722         Export* E = ExpPool [I];
723         if (E->Expr == 0 && E->ImpCount > 0 && F (E->Name, Data) == 0) {
724             /* Unresolved external */
725             Import* Imp = E->ImpList;
726             fprintf (stderr,
727                      "Unresolved external `%s' referenced in:\n",
728                      GetString (E->Name));
729             while (Imp) {
730                 const LineInfo* LI = GetImportPos (Imp);
731                 fprintf (stderr,
732                          "  %s(%lu)\n",
733                          GetSourceName (LI),
734                          GetSourceLine (LI));
735                 Imp = Imp->Next;
736             }
737         }
738     }
739 }
740
741
742
743 static int CmpExpName (const void* K1, const void* K2)
744 /* Compare function for qsort */
745 {
746     return SB_Compare (GetStrBuf ((*(Export**)K1)->Name),
747                        GetStrBuf ((*(Export**)K2)->Name));
748 }
749
750
751
752 static void CreateExportPool (void)
753 /* Create an array with pointer to all exports */
754 {
755     unsigned I, J;
756
757     /* Allocate memory */
758     if (ExpPool) {
759         xfree (ExpPool);
760     }
761     ExpPool = xmalloc (ExpCount * sizeof (Export*));
762
763     /* Walk through the list and insert the exports */
764     for (I = 0, J = 0; I < sizeof (HashTab) / sizeof (HashTab [0]); ++I) {
765         Export* E = HashTab[I];
766         while (E) {
767             CHECK (J < ExpCount);
768             ExpPool[J++] = E;
769             E = E->Next;
770         }
771     }
772
773     /* Sort them by name */
774     qsort (ExpPool, ExpCount, sizeof (Export*), CmpExpName);
775 }
776
777
778
779 void CheckExports (void)
780 /* Setup the list of all exports and check for export/import symbol type
781  * mismatches.
782  */
783 {
784     /* Create an export pool */
785     CreateExportPool ();
786
787     /* Check for symbol type mismatches */
788     CheckSymTypes ();
789 }
790
791
792
793 void CheckUnresolvedImports (ExpCheckFunc F, void* Data)
794 /* Check if there are any unresolved imports. On unresolved imports, F is
795  * called (see the comments on ExpCheckFunc in the data section).
796  */
797 {
798     /* Check for unresolved externals */
799     if (ImpOpen != 0) {
800         /* Print all open imports */
801         PrintUnresolved (F, Data);
802     }
803 }
804
805
806
807 static char GetAddrSizeCode (unsigned char AddrSize)
808 /* Get a one char code for the address size */
809 {
810     switch (AddrSize) {
811         case ADDR_SIZE_ZP:      return 'Z';
812         case ADDR_SIZE_ABS:     return 'A';
813         case ADDR_SIZE_FAR:     return 'F';
814         case ADDR_SIZE_LONG:    return 'L';
815         default:
816             Internal ("Invalid address size: %u", AddrSize);
817             /* NOTREACHED */
818             return '-';
819     }
820 }
821
822
823
824 void PrintExportMap (FILE* F)
825 /* Print an export map to the given file */
826 {
827     unsigned I;
828     unsigned Count;
829
830     /* Print all exports */
831     Count = 0;
832     for (I = 0; I < ExpCount; ++I) {
833         const Export* E = ExpPool [I];
834
835         /* Print unreferenced symbols only if explictly requested */
836         if (VerboseMap || E->ImpCount > 0 || SYM_IS_CONDES (E->Type)) {
837             fprintf (F,
838                      "%-25s %06lX %c%c%c%c   ",
839                      GetString (E->Name),
840                      GetExportVal (E),
841                      E->ImpCount? 'R' : ' ',
842                      SYM_IS_LABEL (E->Type)? 'L' : 'E',
843                      GetAddrSizeCode (E->AddrSize),
844                      SYM_IS_CONDES (E->Type)? 'I' : ' ');
845             if (++Count == 2) {
846                 Count = 0;
847                 fprintf (F, "\n");
848             }
849         }
850     }
851     fprintf (F, "\n");
852 }
853
854
855
856 void PrintImportMap (FILE* F)
857 /* Print an import map to the given file */
858 {
859     unsigned I;
860     const Import* Imp;
861
862     /* Loop over all exports */
863     for (I = 0; I < ExpCount; ++I) {
864
865         /* Get the export */
866         const Export* Exp = ExpPool [I];
867
868         /* Print the symbol only if there are imports, or if a verbose map
869          * file is requested.
870          */
871         if (VerboseMap || Exp->ImpCount > 0) {
872
873             /* Print the export */
874             fprintf (F,
875                      "%s (%s):\n",
876                      GetString (Exp->Name),
877                      GetObjFileName (Exp->Obj));
878
879             /* Print all imports for this symbol */
880             Imp = Exp->ImpList;
881             while (Imp) {
882
883                 /* Print the import */
884                 const LineInfo* LI = GetImportPos (Imp);
885                 fprintf (F,
886                          "    %-25s %s(%lu)\n",
887                          GetObjFileName (Imp->Obj),
888                          GetSourceName (LI),
889                          GetSourceLine (LI));
890
891                 /* Next import */
892                 Imp = Imp->Next;
893             }
894         }
895     }
896     fprintf (F, "\n");
897 }
898
899
900
901 void PrintExportLabels (FILE* F)
902 /* Print the exports in a VICE label file */
903 {
904     unsigned I;
905
906     /* Print all exports */
907     for (I = 0; I < ExpCount; ++I) {
908         const Export* E = ExpPool [I];
909         fprintf (F, "al %06lX .%s\n", GetExportVal (E), GetString (E->Name));
910     }
911 }
912
913
914
915 void MarkExport (Export* E)
916 /* Mark the export */
917 {
918     E->Flags |= EXP_USERMARK;
919 }
920
921
922
923 void UnmarkExport (Export* E)
924 /* Remove the mark from the export */
925 {
926     E->Flags &= ~EXP_USERMARK;
927 }
928
929
930
931 int ExportHasMark (Export* E)
932 /* Return true if the export has a mark */
933 {
934     return (E->Flags & EXP_USERMARK) != 0;
935 }
936
937
938
939 void CircularRefError (const Export* E)
940 /* Print an error about a circular reference using to define the given export */
941 {
942     const LineInfo* LI = GetExportPos (E);
943     Error ("Circular reference for symbol `%s', %s(%lu)",
944            GetString (E->Name),
945            GetSourceName (LI),
946            GetSourceLine (LI));
947 }
948
949
950
951