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