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