]> git.sur5r.net Git - cc65/blob - src/ld65/exports.c
Mark the symbol that is the name of a scope with the size of that scope
[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->Size      = 0;
308     E->LineInfos = EmptyCollection;
309     E->Type      = Type;
310     E->AddrSize  = AddrSize;
311     memset (E->ConDes, 0, sizeof (E->ConDes));
312
313     /* Return the new entry */
314     return E;
315 }
316
317
318
319 void FreeExport (Export* E)
320 /* Free an export. NOTE: This won't remove the export from the exports table,
321  * so it may only be called for unused exports (exports from modules that
322  * aren't referenced).
323  */
324 {
325     /* Safety */
326     PRECONDITION ((E->Flags & EXP_INLIST) == 0);
327
328     /* Free the line infos */
329     DoneCollection (&E->LineInfos);
330
331     /* Free the export expression */
332     FreeExpr (E->Expr);
333
334     /* Free the struct */
335     xfree (E);
336 }
337
338
339
340 Export* ReadExport (FILE* F, ObjData* O)
341 /* Read an export from a file */
342 {
343     unsigned      ConDesCount;
344     Export* E;
345
346     /* Read the type */
347     unsigned Type = ReadVar (F);
348
349     /* Read the address size */
350     unsigned char AddrSize = Read8 (F);
351
352     /* Create a new export without a name */
353     E = NewExport (Type, AddrSize, INVALID_STRING_ID, O);
354
355     /* Read the constructor/destructor decls if we have any */
356     ConDesCount = SYM_GET_CONDES_COUNT (Type);
357     if (ConDesCount > 0) {
358
359         unsigned char ConDes[CD_TYPE_COUNT];
360         unsigned I;
361
362         /* Read the data into temp storage */
363         ReadData (F, ConDes, ConDesCount);
364
365         /* Re-order the data. In the file, each decl is encoded into a byte
366          * which contains the type and the priority. In memory, we will use
367          * an array of types which contain the priority. This array was
368          * cleared by the constructor (NewExport), so we must only set the
369          * fields that contain values.
370          */
371         for (I = 0; I < ConDesCount; ++I) {
372             unsigned ConDesType = CD_GET_TYPE (ConDes[I]);
373             unsigned ConDesPrio = CD_GET_PRIO (ConDes[I]);
374             E->ConDes[ConDesType] = ConDesPrio;
375         }
376     }
377
378     /* Read the name */
379     E->Name = MakeGlobalStringId (O, ReadVar (F));
380
381     /* Read the value */
382     if (SYM_IS_EXPR (Type)) {
383         E->Expr = ReadExpr (F, O);
384     } else {
385         E->Expr = LiteralExpr (Read32 (F), O);
386     }
387
388     /* Read the size */
389     if (SYM_HAS_SIZE (Type)) {
390         E->Size = ReadVar (F);
391     }
392
393     /* Last is the file position where the definition was done */
394     ReadLineInfoList (F, O, &E->LineInfos);
395
396     /* Return the new export */
397     return E;
398 }
399
400
401
402 void InsertExport (Export* E)
403 /* Insert an exported identifier and check if it's already in the list */
404 {
405     Export* L;
406     Export* Last;
407     Import* Imp;
408     unsigned Hash;
409
410     /* Mark the export as inserted */
411     E->Flags |= EXP_INLIST;
412
413     /* Insert the export into any condes tables if needed */
414     if (SYM_IS_CONDES (E->Type)) {
415         ConDesAddExport (E);
416     }
417
418     /* Create a hash value for the given name */
419     Hash = (E->Name & HASHTAB_MASK);
420
421     /* Search through the list in that slot */
422     if (HashTab[Hash] == 0) {
423         /* The slot is empty */
424         HashTab[Hash] = E;
425         ++ExpCount;
426     } else {
427
428         Last = 0;
429         L = HashTab[Hash];
430         do {
431             if (L->Name == E->Name) {
432                 /* This may be an unresolved external */
433                 if (L->Expr == 0) {
434
435                     /* This *is* an unresolved external. Use the actual export
436                      * in E instead of the dummy one in L.
437                      */
438                     E->Next     = L->Next;
439                     E->ImpCount = L->ImpCount;
440                     E->ImpList  = L->ImpList;
441                     if (Last) {
442                         Last->Next = E;
443                     } else {
444                         HashTab[Hash] = E;
445                     }
446                     ImpOpen -= E->ImpCount;     /* Decrease open imports now */
447                     xfree (L);
448                     /* We must run through the import list and change the
449                      * export pointer now.
450                      */
451                     Imp = E->ImpList;
452                     while (Imp) {
453                         Imp->Exp = E;
454                         Imp = Imp->Next;
455                     }
456                 } else {
457                     /* Duplicate entry, ignore it */
458                     Warning ("Duplicate external identifier: `%s'",
459                              GetString (L->Name));
460                 }
461                 return;
462             }
463             Last = L;
464             L = L->Next;
465
466         } while (L);
467
468         /* Insert export at end of queue */
469         Last->Next = E;
470         ++ExpCount;
471     }
472 }
473
474
475
476 const LineInfo* GetExportPos (const Export* E)
477 /* Return the basic line info of an export */
478 {
479     /* Source file position is always in slot zero */
480     return CollConstAt (&E->LineInfos, 0);
481 }
482
483
484
485 Export* CreateConstExport (unsigned Name, long Value)
486 /* Create an export for a literal date */
487 {
488     /* Create a new export */
489     Export* E = NewExport (SYM_CONST | SYM_EQUATE, ADDR_SIZE_ABS, Name, 0);
490
491     /* Assign the value */
492     E->Expr = LiteralExpr (Value, 0);
493
494     /* Insert the export */
495     InsertExport (E);
496
497     /* Return the new export */
498     return E;
499 }
500
501
502
503 Export* CreateExprExport (unsigned Name, ExprNode* Expr, unsigned char AddrSize)
504 /* Create an export for an expression */
505 {
506     /* Create a new export */
507     Export* E = NewExport (SYM_EXPR | SYM_EQUATE, AddrSize, Name, 0);
508
509     /* Assign the value expression */
510     E->Expr = Expr;
511
512     /* Insert the export */
513     InsertExport (E);
514
515     /* Return the new export */
516     return E;
517 }
518
519
520
521 Export* CreateMemoryExport (unsigned Name, MemoryArea* Mem, unsigned long Offs)
522 /* Create an relative export for a memory area offset */
523 {
524     /* Create a new export */
525     Export* E = NewExport (SYM_EXPR | SYM_LABEL, ADDR_SIZE_ABS, Name, 0);
526
527     /* Assign the value */
528     E->Expr = MemoryExpr (Mem, Offs, 0);
529
530     /* Insert the export */
531     InsertExport (E);
532
533     /* Return the new export */
534     return E;
535 }
536
537
538
539 Export* CreateSegmentExport (unsigned Name, Segment* Seg, unsigned long Offs)
540 /* Create a relative export to a segment */
541 {
542     /* Create a new export */
543     Export* E = NewExport (SYM_EXPR | SYM_LABEL, Seg->AddrSize, Name, 0);
544
545     /* Assign the value */
546     E->Expr = SegmentExpr (Seg, Offs, 0);
547
548     /* Insert the export */
549     InsertExport (E);
550
551     /* Return the new export */
552     return E;
553 }
554
555
556
557 Export* CreateSectionExport (unsigned Name, Section* Sec, unsigned long Offs)
558 /* Create a relative export to a section */
559 {
560     /* Create a new export */
561     Export* E = NewExport (SYM_EXPR | SYM_LABEL, Sec->AddrSize, Name, 0);
562
563     /* Assign the value */
564     E->Expr = SectionExpr (Sec, Offs, 0);
565
566     /* Insert the export */
567     InsertExport (E);
568
569     /* Return the new export */
570     return E;
571 }
572
573
574
575 Export* FindExport (unsigned Name)
576 /* Check for an identifier in the list. Return 0 if not found, otherwise
577  * return a pointer to the export.
578  */
579 {
580     /* Get a pointer to the list with the symbols hash value */
581     Export* L = HashTab[Name & HASHTAB_MASK];
582     while (L) {
583         /* Search through the list in that slot */
584         if (L->Name == Name) {
585             /* Entry found */
586             return L;
587         }
588         L = L->Next;
589     }
590
591     /* Not found */
592     return 0;
593 }
594
595
596
597 int IsUnresolved (unsigned Name)
598 /* Check if this symbol is an unresolved export */
599 {
600     /* Find the export */
601     return IsUnresolvedExport (FindExport (Name));
602 }
603
604
605
606 int IsUnresolvedExport (const Export* E)
607 /* Return true if the given export is unresolved */
608 {
609     /* Check if it's unresolved */
610     return E != 0 && E->Expr == 0;
611 }
612
613
614
615 int IsConstExport (const Export* E)
616 /* Return true if the expression associated with this export is const */
617 {
618     if (E->Expr == 0) {
619         /* External symbols cannot be const */
620         return 0;
621     } else {
622         return IsConstExpr (E->Expr);
623     }
624 }
625
626
627
628 long GetExportVal (const Export* E)
629 /* Get the value of this export */
630 {
631     if (E->Expr == 0) {
632         /* OOPS */
633         Internal ("`%s' is an undefined external", GetString (E->Name));
634     }
635     return GetExprVal (E->Expr);
636 }
637
638
639
640 static void CheckSymType (const Export* E)
641 /* Check the types for one export */
642 {
643     /* External with matching imports */
644     Import* I = E->ImpList;
645     while (I) {
646         if (E->AddrSize != I->AddrSize) {
647             /* Export and import address sizes do not match */
648             StrBuf ExportLoc = STATIC_STRBUF_INITIALIZER;
649             StrBuf ImportLoc = STATIC_STRBUF_INITIALIZER;
650             const char* ExpAddrSize = AddrSizeToStr (E->AddrSize);
651             const char* ImpAddrSize = AddrSizeToStr (I->AddrSize);
652             const LineInfo* ExportLI = GetExportPos (E);
653             const LineInfo* ImportLI = GetImportPos (I);
654
655             /* Generate strings that describe the location of the im- and
656              * exports. This depends on the place from where they come:
657              * Object file or linker config.
658              */
659             if (E->Obj) {
660                 /* The export comes from an object file */
661                 SB_Printf (&ExportLoc, "%s, %s(%lu)",
662                            GetString (E->Obj->Name),
663                            GetSourceName (ExportLI),
664                            GetSourceLine (ExportLI));
665             } else {
666                 SB_Printf (&ExportLoc, "%s(%lu)",
667                            GetSourceName (ExportLI),
668                            GetSourceLine (ExportLI));
669             }
670             if (I->Obj) {
671                 /* The import comes from an object file */
672                 SB_Printf (&ImportLoc, "%s, %s(%lu)",
673                            GetString (I->Obj->Name),
674                            GetSourceName (ImportLI),
675                            GetSourceLine (ImportLI));
676             } else {
677                 SB_Printf (&ImportLoc, "%s(%lu)",
678                            GetSourceName (ImportLI),
679                            GetSourceLine (ImportLI));
680             }
681
682             /* Output the diagnostic */
683             Warning ("Address size mismatch for `%s': "
684                      "Exported from %s as `%s', "
685                      "import in %s as `%s'",
686                      GetString (E->Name),
687                      SB_GetConstBuf (&ExportLoc),
688                      ExpAddrSize,
689                      SB_GetConstBuf (&ImportLoc),
690                      ImpAddrSize);
691
692             /* Free the temporary strings */
693             SB_Done (&ExportLoc);
694             SB_Done (&ImportLoc);
695         }
696         I = I->Next;
697     }
698 }
699
700
701
702 static void CheckSymTypes (void)
703 /* Check for symbol tape mismatches */
704 {
705     unsigned I;
706
707     /* Print all open imports */
708     for (I = 0; I < ExpCount; ++I) {
709         const Export* E = ExpPool [I];
710         if (E->Expr != 0 && E->ImpCount > 0) {
711             /* External with matching imports */
712             CheckSymType (E);
713         }
714     }
715 }
716
717
718
719 static void PrintUnresolved (ExpCheckFunc F, void* Data)
720 /* Print a list of unresolved symbols. On unresolved symbols, F is
721  * called (see the comments on ExpCheckFunc in the data section).
722  */
723 {
724     unsigned I;
725
726     /* Print all open imports */
727     for (I = 0; I < ExpCount; ++I) {
728         Export* E = ExpPool [I];
729         if (E->Expr == 0 && E->ImpCount > 0 && F (E->Name, Data) == 0) {
730             /* Unresolved external */
731             Import* Imp = E->ImpList;
732             fprintf (stderr,
733                      "Unresolved external `%s' referenced in:\n",
734                      GetString (E->Name));
735             while (Imp) {
736                 const LineInfo* LI = GetImportPos (Imp);
737                 fprintf (stderr,
738                          "  %s(%lu)\n",
739                          GetSourceName (LI),
740                          GetSourceLine (LI));
741                 Imp = Imp->Next;
742             }
743         }
744     }
745 }
746
747
748                      
749 static int CmpExpName (const void* K1, const void* K2)
750 /* Compare function for qsort */
751 {
752     return SB_Compare (GetStrBuf ((*(Export**)K1)->Name),
753                        GetStrBuf ((*(Export**)K2)->Name));
754 }
755
756
757
758 static void CreateExportPool (void)
759 /* Create an array with pointer to all exports */
760 {
761     unsigned I, J;
762
763     /* Allocate memory */
764     if (ExpPool) {
765         xfree (ExpPool);
766     }
767     ExpPool = xmalloc (ExpCount * sizeof (Export*));
768
769     /* Walk through the list and insert the exports */
770     for (I = 0, J = 0; I < sizeof (HashTab) / sizeof (HashTab [0]); ++I) {
771         Export* E = HashTab[I];
772         while (E) {
773             CHECK (J < ExpCount);
774             ExpPool[J++] = E;
775             E = E->Next;
776         }
777     }
778
779     /* Sort them by name */
780     qsort (ExpPool, ExpCount, sizeof (Export*), CmpExpName);
781 }
782
783
784
785 void CheckExports (void)
786 /* Setup the list of all exports and check for export/import symbol type
787  * mismatches.
788  */
789 {
790     /* Create an export pool */
791     CreateExportPool ();
792
793     /* Check for symbol type mismatches */
794     CheckSymTypes ();
795 }
796
797
798
799 void CheckUnresolvedImports (ExpCheckFunc F, void* Data)
800 /* Check if there are any unresolved imports. On unresolved imports, F is
801  * called (see the comments on ExpCheckFunc in the data section).
802  */
803 {
804     /* Check for unresolved externals */
805     if (ImpOpen != 0) {
806         /* Print all open imports */
807         PrintUnresolved (F, Data);
808     }
809 }
810
811
812
813 static char GetAddrSizeCode (unsigned char AddrSize)
814 /* Get a one char code for the address size */
815 {
816     switch (AddrSize) {
817         case ADDR_SIZE_ZP:      return 'Z';
818         case ADDR_SIZE_ABS:     return 'A';
819         case ADDR_SIZE_FAR:     return 'F';
820         case ADDR_SIZE_LONG:    return 'L';
821         default:
822             Internal ("Invalid address size: %u", AddrSize);
823             /* NOTREACHED */
824             return '-';
825     }
826 }
827
828
829
830 void PrintExportMap (FILE* F)
831 /* Print an export map to the given file */
832 {
833     unsigned I;
834     unsigned Count;
835
836     /* Print all exports */
837     Count = 0;
838     for (I = 0; I < ExpCount; ++I) {
839         const Export* E = ExpPool [I];
840
841         /* Print unreferenced symbols only if explictly requested */
842         if (VerboseMap || E->ImpCount > 0 || SYM_IS_CONDES (E->Type)) {
843             fprintf (F,
844                      "%-25s %06lX %c%c%c%c   ",
845                      GetString (E->Name),
846                      GetExportVal (E),
847                      E->ImpCount? 'R' : ' ',
848                      SYM_IS_LABEL (E->Type)? 'L' : 'E',
849                      GetAddrSizeCode (E->AddrSize),
850                      SYM_IS_CONDES (E->Type)? 'I' : ' ');
851             if (++Count == 2) {
852                 Count = 0;
853                 fprintf (F, "\n");
854             }
855         }
856     }
857     fprintf (F, "\n");
858 }
859
860
861
862 void PrintImportMap (FILE* F)
863 /* Print an import map to the given file */
864 {
865     unsigned I;
866     const Import* Imp;
867
868     /* Loop over all exports */
869     for (I = 0; I < ExpCount; ++I) {
870
871         /* Get the export */
872         const Export* Exp = ExpPool [I];
873
874         /* Print the symbol only if there are imports, or if a verbose map
875          * file is requested.
876          */
877         if (VerboseMap || Exp->ImpCount > 0) {
878
879             /* Print the export */
880             fprintf (F,
881                      "%s (%s):\n",
882                      GetString (Exp->Name),
883                      GetObjFileName (Exp->Obj));
884
885             /* Print all imports for this symbol */
886             Imp = Exp->ImpList;
887             while (Imp) {
888
889                 /* Print the import */
890                 const LineInfo* LI = GetImportPos (Imp);
891                 fprintf (F,
892                          "    %-25s %s(%lu)\n",
893                          GetObjFileName (Imp->Obj),
894                          GetSourceName (LI),
895                          GetSourceLine (LI));
896
897                 /* Next import */
898                 Imp = Imp->Next;
899             }
900         }
901     }
902     fprintf (F, "\n");
903 }
904
905
906
907 void PrintExportLabels (FILE* F)
908 /* Print the exports in a VICE label file */
909 {
910     unsigned I;
911
912     /* Print all exports */
913     for (I = 0; I < ExpCount; ++I) {
914         const Export* E = ExpPool [I];
915         fprintf (F, "al %06lX .%s\n", GetExportVal (E), GetString (E->Name));
916     }
917 }
918
919
920
921 void MarkExport (Export* E)
922 /* Mark the export */
923 {
924     E->Flags |= EXP_USERMARK;
925 }
926
927
928
929 void UnmarkExport (Export* E)
930 /* Remove the mark from the export */
931 {
932     E->Flags &= ~EXP_USERMARK;
933 }
934
935
936
937 int ExportHasMark (Export* E)
938 /* Return true if the export has a mark */
939 {
940     return (E->Flags & EXP_USERMARK) != 0;
941 }
942
943
944
945 void CircularRefError (const Export* E)
946 /* Print an error about a circular reference using to define the given export */
947 {
948     const LineInfo* LI = GetExportPos (E);
949     Error ("Circular reference for symbol `%s', %s(%lu)",
950            GetString (E->Name),
951            GetSourceName (LI),
952            GetSourceLine (LI));
953 }
954
955
956
957