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