]> git.sur5r.net Git - cc65/blob - src/ld65/exports.c
Fixed a watcom warning
[cc65] / src / ld65 / exports.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 exports.c                                 */
4 /*                                                                           */
5 /*                   Exports handling for the ld65 linker                    */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2003 Ullrich von Bassewitz                                       */
10 /*               Römerstraße 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 "coll.h"
44 #include "hashstr.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 "objdata.h"
56 #include "spool.h"
57
58
59
60 /*****************************************************************************/
61 /*                                   Data                                    */
62 /*****************************************************************************/
63
64
65
66 /* Hash table */
67 #define HASHTAB_MASK    0x0FFFU
68 #define HASHTAB_SIZE    (HASHTAB_MASK + 1)
69 static Export*          HashTab[HASHTAB_SIZE];
70
71 /* Import management variables */
72 static unsigned         ImpCount = 0;           /* Import count */
73 static unsigned         ImpOpen  = 0;           /* Count of open imports */
74
75 /* Export management variables */
76 static unsigned         ExpCount = 0;           /* Export count */
77 static Export**         ExpPool  = 0;           /* Exports array */
78
79 /* Defines for the flags in Import */
80 #define IMP_INLIST      0x0001U                 /* Import is in exports list */
81
82 /* Defines for the flags in Export */
83 #define EXP_INLIST      0x0001U                 /* Export is in exports list */
84 #define EXP_USERMARK    0x0002U                 /* User setable flag */
85
86
87
88 /*****************************************************************************/
89 /*                              Import handling                              */
90 /*****************************************************************************/
91
92
93
94 static Export* NewExport (unsigned char Type, unsigned char AddrSize,
95                           unsigned Name, ObjData* Obj);
96 /* Create a new export and initialize it */
97
98
99
100 static Import* NewImport (unsigned char AddrSize, ObjData* Obj)
101 /* Create a new import and initialize it */
102 {
103     /* Allocate memory */
104     Import* I = xmalloc (sizeof (Import));
105
106     /* Initialize the fields */
107     I->Next     = 0;
108     I->Obj      = Obj;
109     I->Exp      = 0;
110     I->Name     = INVALID_STRING_ID;
111     I->Flags    = 0;
112     I->AddrSize = AddrSize;
113
114     /* Return the new structure */
115     return I;
116 }
117
118
119
120 void InsertImport (Import* I)
121 /* Insert an import into the table */
122 {
123     Export* E;
124
125     /* As long as the import is not inserted, V.Name is valid */
126     unsigned Name = I->Name;
127
128     /* Create a hash value for the given name */
129     unsigned Hash = (Name & HASHTAB_MASK);
130
131     /* Search through the list in that slot and print matching duplicates */
132     if (HashTab[Hash] == 0) {
133         /* The slot is empty, we need to insert a dummy export */
134         E = HashTab[Hash] = NewExport (0, ADDR_SIZE_DEFAULT, Name, 0);
135         ++ExpCount;
136     } else {
137         E = HashTab [Hash];
138         while (1) {
139             if (E->Name == Name) {
140                 /* We have an entry, L points to it */
141                 break;
142             }
143             if (E->Next == 0) {
144                 /* End of list an entry not found, insert a dummy */
145                 E->Next = NewExport (0, ADDR_SIZE_DEFAULT, Name, 0);
146                 E = E->Next;            /* Point to dummy */
147                 ++ExpCount;             /* One export more */
148                 break;
149             } else {
150                 E = E->Next;
151             }
152         }
153     }
154
155     /* Ok, E now points to a valid exports entry for the given import. Insert
156      * the import into the imports list and update the counters.
157      */
158     I->Exp     = E;
159     I->Next    = E->ImpList;
160     E->ImpList = I;
161     E->ImpCount++;
162     ++ImpCount;                 /* Total import count */
163     if (E->Expr == 0) {
164         /* This is a dummy export */
165         ++ImpOpen;
166     }
167
168     /* Mark the import so we know it's in the list */
169     I->Flags |= IMP_INLIST;
170 }
171
172
173
174 void FreeImport (Import* I)
175 /* Free an import. NOTE: This won't remove the import from the exports table,
176  * so it may only be called for unused imports (imports from modules that
177  * aren't referenced).
178  */
179 {
180     /* Safety */
181     PRECONDITION ((I->Flags & IMP_INLIST) == 0);
182
183     /* Free the struct */
184     xfree (I);
185 }
186
187
188
189 Import* ReadImport (FILE* F, ObjData* Obj)
190 /* Read an import from a file and return it */
191 {
192     Import* I;
193
194     /* Read the import address size */
195     unsigned char AddrSize = Read8 (F);
196
197     /* Create a new import */
198     I = NewImport (AddrSize, Obj);
199
200     /* Read the name */
201     I->Name = MakeGlobalStringId (Obj, ReadVar (F));
202
203     /* Read the file position */
204     ReadFilePos (F, &I->Pos);
205
206     /* Check the address size */
207     if (I->AddrSize == ADDR_SIZE_DEFAULT || I->AddrSize > ADDR_SIZE_LONG) {
208         /* Beware: This function may be called in cases where the object file
209          * is not read completely into memory. In this case, the file list is
210          * invalid. Be sure not to access it in this case.
211          */
212         if (ObjHasFiles (I->Obj)) {
213             Error ("Invalid import size in for `%s', imported from %s(%lu): 0x%02X",
214                    GetString (I->Name),
215                    GetSourceFileName (I->Obj, I->Pos.Name),
216                    I->Pos.Line,
217                    I->AddrSize);
218         } else {
219             Error ("Invalid import size in for `%s', imported from %s: 0x%02X",
220                    GetString (I->Name),
221                    GetObjFileName (I->Obj),
222                    I->AddrSize);
223         }
224     }
225
226     /* Return the new import */
227     return I;
228 }
229
230
231
232 /*****************************************************************************/
233 /*                                   Code                                    */
234 /*****************************************************************************/
235
236
237
238 static Export* NewExport (unsigned char Type, unsigned char AddrSize,
239                           unsigned Name, ObjData* Obj)
240 /* Create a new export and initialize it */
241 {
242     /* Allocate memory */
243     Export* E = xmalloc (sizeof (Export));
244
245     /* Initialize the fields */
246     E->Name     = Name;
247     E->Next     = 0;
248     E->Flags    = 0;
249     E->Obj      = Obj;
250     E->ImpCount = 0;
251     E->ImpList  = 0;
252     E->Expr     = 0;
253     E->Type     = Type;
254     E->AddrSize = AddrSize;
255     memset (E->ConDes, 0, sizeof (E->ConDes));
256
257     /* Return the new entry */
258     return E;
259 }
260
261
262
263 void FreeExport (Export* E)
264 /* Free an export. NOTE: This won't remove the export from the exports table,
265  * so it may only be called for unused exports (exports from modules that
266  * aren't referenced).
267  */
268 {
269     /* Safety */
270     PRECONDITION ((E->Flags & EXP_INLIST) == 0);
271
272     /* Free the export expression */
273     FreeExpr (E->Expr);
274
275     /* Free the struct */
276     xfree (E);
277 }
278
279
280
281 void InsertExport (Export* E)
282 /* Insert an exported identifier and check if it's already in the list */
283 {
284     Export* L;
285     Export* Last;
286     Import* Imp;
287     unsigned Hash;
288
289     /* Mark the export as inserted */
290     E->Flags |= EXP_INLIST;
291
292     /* Insert the export into any condes tables if needed */
293     if (IS_EXP_CONDES (E->Type)) {
294         ConDesAddExport (E);
295     }
296
297     /* Create a hash value for the given name */
298     Hash = (E->Name & HASHTAB_MASK);
299
300     /* Search through the list in that slot */
301     if (HashTab[Hash] == 0) {
302         /* The slot is empty */
303         HashTab[Hash] = E;
304         ++ExpCount;
305     } else {
306
307         Last = 0;
308         L = HashTab[Hash];
309         do {
310             if (L->Name == E->Name) {
311                 /* This may be an unresolved external */
312                 if (L->Expr == 0) {
313
314                     /* This *is* an unresolved external. Use the actual export
315                      * in E instead of the dummy one in L.
316                      */
317                     E->Next     = L->Next;
318                     E->ImpCount = L->ImpCount;
319                     E->ImpList  = L->ImpList;
320                     if (Last) {
321                         Last->Next = E;
322                     } else {
323                         HashTab[Hash] = E;
324                     }
325                     ImpOpen -= E->ImpCount;     /* Decrease open imports now */
326                     xfree (L);
327                     /* We must run through the import list and change the
328                      * export pointer now.
329                      */
330                     Imp = E->ImpList;
331                     while (Imp) {
332                         Imp->Exp = E;
333                         Imp = Imp->Next;
334                     }
335                 } else {
336                     /* Duplicate entry, ignore it */
337                     Warning ("Duplicate external identifier: `%s'",
338                              GetString (L->Name));
339                 }
340                 return;
341             }
342             Last = L;
343             L = L->Next;
344
345         } while (L);
346
347         /* Insert export at end of queue */
348         Last->Next = E;
349         ++ExpCount;
350     }
351 }
352
353
354
355 Export* ReadExport (FILE* F, ObjData* O)
356 /* Read an export from a file */
357 {
358     unsigned      ConDesCount;
359     Export* E;
360
361     /* Read the type */
362     unsigned char Type = Read8 (F);
363
364     /* Read the address size */
365     unsigned char AddrSize = Read8 (F);
366
367     /* Create a new export without a name */
368     E = NewExport (Type, AddrSize, INVALID_STRING_ID, O);
369
370     /* Read the constructor/destructor decls if we have any */
371     ConDesCount = GET_EXP_CONDES_COUNT (Type);
372     if (ConDesCount > 0) {
373
374         unsigned char ConDes[CD_TYPE_COUNT];
375         unsigned I;
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. This array was
383          * cleared by the constructor (NewExport), so we must only set the
384          * fields that contain values.
385          */
386         for (I = 0; I < ConDesCount; ++I) {
387             unsigned ConDesType = CD_GET_TYPE (ConDes[I]);
388             unsigned ConDesPrio = CD_GET_PRIO (ConDes[I]);
389             E->ConDes[ConDesType] = ConDesPrio;
390         }
391     }
392
393     /* Read the name */
394     E->Name = MakeGlobalStringId (O, ReadVar (F));
395
396     /* Read the value */
397     if (IS_EXP_EXPR (Type)) {
398         E->Expr = ReadExpr (F, O);
399     } else {
400         E->Expr = LiteralExpr (Read32 (F), O);
401     }
402
403     /* Last is the file position where the definition was done */
404     ReadFilePos (F, &E->Pos);
405
406     /* Return the new export */
407     return E;
408 }
409
410
411
412 Export* CreateConstExport (unsigned Name, long Value)
413 /* Create an export for a literal date */
414 {
415     /* Create a new export */
416     Export* E = NewExport (EXP_CONST | EXP_EQUATE, ADDR_SIZE_ABS, Name, 0);
417
418     /* Assign the value */
419     E->Expr = LiteralExpr (Value, 0);
420
421     /* Insert the export */
422     InsertExport (E);
423
424     /* Return the new export */
425     return E;
426 }
427
428
429
430 Export* CreateMemoryExport (unsigned Name, Memory* Mem, unsigned long Offs)
431 /* Create an relative export for a memory area offset */
432 {
433     /* Create a new export */
434     Export* E = NewExport (EXP_EXPR | EXP_LABEL, ADDR_SIZE_ABS, Name, 0);
435
436     /* Assign the value */
437     E->Expr = MemoryExpr (Mem, Offs, 0);
438
439     /* Insert the export */
440     InsertExport (E);
441
442     /* Return the new export */
443     return E;
444 }
445
446
447
448 Export* CreateSegmentExport (unsigned Name, Segment* Seg, unsigned long Offs)
449 /* Create a relative export to a segment */
450 {
451     /* Create a new export */
452     Export* E = NewExport (EXP_EXPR | EXP_LABEL, Seg->AddrSize, Name, 0);
453
454     /* Assign the value */
455     E->Expr = SegmentExpr (Seg, Offs, 0);
456
457     /* Insert the export */
458     InsertExport (E);
459
460     /* Return the new export */
461     return E;
462 }
463
464
465
466 Export* CreateSectionExport (unsigned Name, Section* Sec, unsigned long Offs)
467 /* Create a relative export to a section */
468 {
469     /* Create a new export */
470     Export* E = NewExport (EXP_EXPR | EXP_LABEL, Sec->AddrSize, Name, 0);
471
472     /* Assign the value */
473     E->Expr = SectionExpr (Sec, Offs, 0);
474
475     /* Insert the export */
476     InsertExport (E);
477
478     /* Return the new export */
479     return E;
480 }
481
482
483
484 Export* FindExport (unsigned Name)
485 /* Check for an identifier in the list. Return 0 if not found, otherwise
486  * return a pointer to the export.
487  */
488 {
489     /* Get a pointer to the list with the symbols hash value */
490     Export* L = HashTab[Name & HASHTAB_MASK];
491     while (L) {
492         /* Search through the list in that slot */
493         if (L->Name == Name) {
494             /* Entry found */
495             return L;
496         }
497         L = L->Next;
498     }
499
500     /* Not found */
501     return 0;
502 }
503
504
505
506 int IsUnresolved (unsigned Name)
507 /* Check if this symbol is an unresolved export */
508 {
509     /* Find the export */
510     return IsUnresolvedExport (FindExport (Name));
511 }
512
513
514
515 int IsUnresolvedExport (const Export* E)
516 /* Return true if the given export is unresolved */
517 {
518     /* Check if it's unresolved */
519     return E != 0 && E->Expr == 0;
520 }
521
522
523
524 int IsConstExport (const Export* E)
525 /* Return true if the expression associated with this export is const */
526 {
527     if (E->Expr == 0) {
528         /* External symbols cannot be const */
529         return 0;
530     } else {
531         return IsConstExpr (E->Expr);
532     }
533 }
534
535
536
537 long GetExportVal (const Export* E)
538 /* Get the value of this export */
539 {
540     if (E->Expr == 0) {
541         /* OOPS */
542         Internal ("`%s' is an undefined external", GetString (E->Name));
543     }
544     return GetExprVal (E->Expr);
545 }
546
547
548
549 static void CheckSymType (const Export* E)
550 /* Check the types for one export */
551 {
552     /* External with matching imports */
553     Import* Imp = E->ImpList;
554     while (Imp) {
555         if (E->AddrSize != Imp->AddrSize) {
556             /* Export is ZP, import is abs or the other way round */
557             if (E->Obj) {
558                 /* User defined export */
559                 Warning ("Address size mismatch for `%s', export in "
560                          "%s(%lu), import in %s(%lu)",
561                          GetString (E->Name),
562                          GetSourceFileName (E->Obj, E->Pos.Name),
563                          E->Pos.Line,
564                          GetSourceFileName (Imp->Obj, Imp->Pos.Name),
565                          Imp->Pos.Line);
566             } else {
567                 /* Export created by the linker */
568                 Warning ("Address size mismatch for `%s', imported from %s(%lu)",
569                          GetString (E->Name),
570                          GetSourceFileName (Imp->Obj, Imp->Pos.Name),
571                          Imp->Pos.Line);
572             }
573         }
574         Imp = Imp->Next;
575     }
576 }
577
578
579
580 static void CheckSymTypes (void)
581 /* Check for symbol tape mismatches */
582 {
583     unsigned I;
584
585     /* Print all open imports */
586     for (I = 0; I < ExpCount; ++I) {
587         const Export* E = ExpPool [I];
588         if (E->Expr != 0 && E->ImpCount > 0) {
589             /* External with matching imports */
590             CheckSymType (E);
591         }
592     }
593 }
594
595
596
597 static void PrintUnresolved (ExpCheckFunc F, void* Data)
598 /* Print a list of unresolved symbols. On unresolved symbols, F is
599  * called (see the comments on ExpCheckFunc in the data section).
600  */
601 {
602     unsigned I;
603
604     /* Print all open imports */
605     for (I = 0; I < ExpCount; ++I) {
606         Export* E = ExpPool [I];
607         if (E->Expr == 0 && E->ImpCount > 0 && F (E->Name, Data) == 0) {
608             /* Unresolved external */
609             Import* Imp = E->ImpList;
610             fprintf (stderr,
611                      "Unresolved external `%s' referenced in:\n",
612                      GetString (E->Name));
613             while (Imp) {
614                 const char* Name = GetSourceFileName (Imp->Obj, Imp->Pos.Name);
615                 fprintf (stderr, "  %s(%lu)\n", Name, Imp->Pos.Line);
616                 Imp = Imp->Next;
617             }
618         }
619     }
620 }
621
622
623
624 static int CmpExpName (const void* K1, const void* K2)
625 /* Compare function for qsort */
626 {
627     return strcmp (GetString ((*(Export**)K1)->Name),
628                    GetString ((*(Export**)K2)->Name));
629 }
630
631
632
633 static void CreateExportPool (void)
634 /* Create an array with pointer to all exports */
635 {
636     unsigned I, J;
637
638     /* Allocate memory */
639     if (ExpPool) {
640         xfree (ExpPool);
641     }
642     ExpPool = xmalloc (ExpCount * sizeof (Export*));
643
644     /* Walk through the list and insert the exports */
645     for (I = 0, J = 0; I < sizeof (HashTab) / sizeof (HashTab [0]); ++I) {
646         Export* E = HashTab[I];
647         while (E) {
648             CHECK (J < ExpCount);
649             ExpPool[J++] = E;
650             E = E->Next;
651         }
652     }
653
654     /* Sort them by name */
655     qsort (ExpPool, ExpCount, sizeof (Export*), CmpExpName);
656 }
657
658
659
660 void CheckExports (ExpCheckFunc F, void* Data)
661 /* Check if there are any unresolved symbols. On unresolved symbols, F is
662  * called (see the comments on ExpCheckFunc in the data section).
663  */
664 {
665     /* Create an export pool */
666     CreateExportPool ();
667
668     /* Check for symbol type mismatches */
669     CheckSymTypes ();
670
671     /* Check for unresolved externals (check here for special bin formats) */
672     if (ImpOpen != 0) {
673         /* Print all open imports */
674         PrintUnresolved (F, Data);
675     }
676 }
677
678
679
680 static char GetAddrSizeCode (unsigned char AddrSize)
681 /* Get a one char code for the address size */
682 {
683     switch (AddrSize) {
684         case ADDR_SIZE_ZP:      return 'Z';
685         case ADDR_SIZE_ABS:     return 'A';
686         case ADDR_SIZE_FAR:     return 'F';
687         case ADDR_SIZE_LONG:    return 'L';
688         default:
689             Internal ("Invalid address size: %u", AddrSize);
690             /* NOTREACHED */
691             return '-';
692     }
693 }
694
695
696
697 void PrintExportMap (FILE* F)
698 /* Print an export map to the given file */
699 {
700     unsigned I;
701     unsigned Count;
702
703     /* Print all exports */
704     Count = 0;
705     for (I = 0; I < ExpCount; ++I) {
706         const Export* E = ExpPool [I];
707
708         /* Print unreferenced symbols only if explictly requested */
709         if (VerboseMap || E->ImpCount > 0 || IS_EXP_CONDES (E->Type)) {
710             fprintf (F,
711                      "%-25s %06lX %c%c%c%c   ",
712                      GetString (E->Name),
713                      GetExportVal (E),
714                      E->ImpCount? 'R' : ' ',
715                      IS_EXP_LABEL (E->Type)? 'L' : 'E',
716                      GetAddrSizeCode (E->AddrSize),
717                      IS_EXP_CONDES (E->Type)? 'I' : ' ');
718             if (++Count == 2) {
719                 Count = 0;
720                 fprintf (F, "\n");
721             }
722         }
723     }
724     fprintf (F, "\n");
725 }
726
727
728
729 void PrintImportMap (FILE* F)
730 /* Print an import map to the given file */
731 {
732     unsigned I;
733     const Import* Imp;
734
735     /* Loop over all exports */
736     for (I = 0; I < ExpCount; ++I) {
737
738         /* Get the export */
739         const Export* Exp = ExpPool [I];
740
741         /* Print the symbol only if there are imports, or if a verbose map
742          * file is requested.
743          */
744         if (VerboseMap || Exp->ImpCount > 0) {
745
746             /* Print the export */
747             fprintf (F,
748                      "%s (%s):\n",
749                      GetString (Exp->Name),
750                      GetObjFileName (Exp->Obj));
751
752             /* Print all imports for this symbol */
753             Imp = Exp->ImpList;
754             while (Imp) {
755
756                 /* Print the import */
757                 fprintf (F,
758                          "    %-25s %s(%lu)\n",
759                          GetObjFileName (Imp->Obj),
760                          GetSourceFileName (Imp->Obj, Imp->Pos.Name),
761                          Imp->Pos.Line);
762
763                 /* Next import */
764                 Imp = Imp->Next;
765             }
766         }
767     }
768     fprintf (F, "\n");
769 }
770
771
772
773 void PrintExportLabels (FILE* F)
774 /* Print the exports in a VICE label file */
775 {
776     unsigned I;
777
778     /* Print all exports */
779     for (I = 0; I < ExpCount; ++I) {
780         const Export* E = ExpPool [I];
781         fprintf (F, "al %06lX .%s\n", GetExportVal (E), GetString (E->Name));
782     }
783 }
784
785
786
787 void MarkExport (Export* E)
788 /* Mark the export */
789 {
790     E->Flags |= EXP_USERMARK;
791 }
792
793
794
795 void UnmarkExport (Export* E)
796 /* Remove the mark from the export */
797 {
798     E->Flags &= ~EXP_USERMARK;
799 }
800
801
802
803 int ExportHasMark (Export* E)
804 /* Return true if the export has a mark */
805 {
806     return (E->Flags & EXP_USERMARK) != 0;
807 }
808
809
810
811 void CircularRefError (const Export* E)
812 /* Print an error about a circular reference using to define the given export */
813 {
814     Error ("Circular reference for symbol `%s', %s(%lu)",
815            GetString (E->Name),
816            GetSourceFileName (E->Obj, E->Pos.Name),
817            E->Pos.Line);
818 }
819
820
821
822