]> git.sur5r.net Git - cc65/blob - src/ld65/exports.c
Make much more usage of dynamic strings (StrBufs) instead of char* and
[cc65] / src / ld65 / exports.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 exports.c                                 */
4 /*                                                                           */
5 /*                   Exports handling for the ld65 linker                    */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2008 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 "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             const char* ExpAddrSize = AddrSizeToStr (E->AddrSize);
558             const char* ImpAddrSize = AddrSizeToStr (Imp->AddrSize);
559             const char* ExpObjName  = GetObjFileName (E->Obj);
560             const char* ImpObjName  = GetObjFileName (Imp->Obj);
561             if (E->Obj) {
562                 /* User defined export */
563                 Warning ("Address size mismatch for `%s': Exported from %s, "
564                          "%s(%lu) as `%s', import in %s, %s(%lu) as `%s'",
565                          GetString (E->Name),
566                          ExpObjName,
567                          GetSourceFileName (E->Obj, E->Pos.Name),
568                          E->Pos.Line,
569                          ExpAddrSize,
570                          ImpObjName,
571                          GetSourceFileName (Imp->Obj, Imp->Pos.Name),
572                          Imp->Pos.Line,
573                          ImpAddrSize);
574             } else {
575                 /* Export created by the linker */
576                 Warning ("Address size mismatch for `%s': Symbol is `%s'"
577                          ", but imported from %s, %s(%lu) as `%s'",
578                          GetString (E->Name),
579                          ExpAddrSize,
580                          ImpObjName,
581                          GetSourceFileName (Imp->Obj, Imp->Pos.Name),
582                          Imp->Pos.Line,
583                          ImpAddrSize);
584             }
585         }
586         Imp = Imp->Next;
587     }
588 }
589
590
591
592 static void CheckSymTypes (void)
593 /* Check for symbol tape mismatches */
594 {
595     unsigned I;
596
597     /* Print all open imports */
598     for (I = 0; I < ExpCount; ++I) {
599         const Export* E = ExpPool [I];
600         if (E->Expr != 0 && E->ImpCount > 0) {
601             /* External with matching imports */
602             CheckSymType (E);
603         }
604     }
605 }
606
607
608
609 static void PrintUnresolved (ExpCheckFunc F, void* Data)
610 /* Print a list of unresolved symbols. On unresolved symbols, F is
611  * called (see the comments on ExpCheckFunc in the data section).
612  */
613 {
614     unsigned I;
615
616     /* Print all open imports */
617     for (I = 0; I < ExpCount; ++I) {
618         Export* E = ExpPool [I];
619         if (E->Expr == 0 && E->ImpCount > 0 && F (E->Name, Data) == 0) {
620             /* Unresolved external */
621             Import* Imp = E->ImpList;
622             fprintf (stderr,
623                      "Unresolved external `%s' referenced in:\n",
624                      GetString (E->Name));
625             while (Imp) {
626                 const char* Name = GetSourceFileName (Imp->Obj, Imp->Pos.Name);
627                 fprintf (stderr, "  %s(%lu)\n", Name, Imp->Pos.Line);
628                 Imp = Imp->Next;
629             }
630         }
631     }
632 }
633
634
635
636 static int CmpExpName (const void* K1, const void* K2)
637 /* Compare function for qsort */
638 {
639     return SB_Compare (GetStrBuf ((*(Export**)K1)->Name),
640                        GetStrBuf ((*(Export**)K2)->Name));
641 }
642
643
644
645 static void CreateExportPool (void)
646 /* Create an array with pointer to all exports */
647 {
648     unsigned I, J;
649
650     /* Allocate memory */
651     if (ExpPool) {
652         xfree (ExpPool);
653     }
654     ExpPool = xmalloc (ExpCount * sizeof (Export*));
655
656     /* Walk through the list and insert the exports */
657     for (I = 0, J = 0; I < sizeof (HashTab) / sizeof (HashTab [0]); ++I) {
658         Export* E = HashTab[I];
659         while (E) {
660             CHECK (J < ExpCount);
661             ExpPool[J++] = E;
662             E = E->Next;
663         }
664     }
665
666     /* Sort them by name */
667     qsort (ExpPool, ExpCount, sizeof (Export*), CmpExpName);
668 }
669
670
671
672 void CheckExports (void)
673 /* Setup the list of all exports and check for export/import symbol type
674  * mismatches.
675  */
676 {
677     /* Create an export pool */
678     CreateExportPool ();
679
680     /* Check for symbol type mismatches */
681     CheckSymTypes ();
682 }
683
684
685
686 void CheckUnresolvedImports (ExpCheckFunc F, void* Data)
687 /* Check if there are any unresolved imports. On unresolved imports, F is
688  * called (see the comments on ExpCheckFunc in the data section).
689  */
690 {
691     /* Check for unresolved externals */
692     if (ImpOpen != 0) {
693         /* Print all open imports */
694         PrintUnresolved (F, Data);
695     }
696 }
697
698
699
700 static char GetAddrSizeCode (unsigned char AddrSize)
701 /* Get a one char code for the address size */
702 {
703     switch (AddrSize) {
704         case ADDR_SIZE_ZP:      return 'Z';
705         case ADDR_SIZE_ABS:     return 'A';
706         case ADDR_SIZE_FAR:     return 'F';
707         case ADDR_SIZE_LONG:    return 'L';
708         default:
709             Internal ("Invalid address size: %u", AddrSize);
710             /* NOTREACHED */
711             return '-';
712     }
713 }
714
715
716
717 void PrintExportMap (FILE* F)
718 /* Print an export map to the given file */
719 {
720     unsigned I;
721     unsigned Count;
722
723     /* Print all exports */
724     Count = 0;
725     for (I = 0; I < ExpCount; ++I) {
726         const Export* E = ExpPool [I];
727
728         /* Print unreferenced symbols only if explictly requested */
729         if (VerboseMap || E->ImpCount > 0 || IS_EXP_CONDES (E->Type)) {
730             fprintf (F,
731                      "%-25s %06lX %c%c%c%c   ",
732                      GetString (E->Name),
733                      GetExportVal (E),
734                      E->ImpCount? 'R' : ' ',
735                      IS_EXP_LABEL (E->Type)? 'L' : 'E',
736                      GetAddrSizeCode (E->AddrSize),
737                      IS_EXP_CONDES (E->Type)? 'I' : ' ');
738             if (++Count == 2) {
739                 Count = 0;
740                 fprintf (F, "\n");
741             }
742         }
743     }
744     fprintf (F, "\n");
745 }
746
747
748
749 void PrintImportMap (FILE* F)
750 /* Print an import map to the given file */
751 {
752     unsigned I;
753     const Import* Imp;
754
755     /* Loop over all exports */
756     for (I = 0; I < ExpCount; ++I) {
757
758         /* Get the export */
759         const Export* Exp = ExpPool [I];
760
761         /* Print the symbol only if there are imports, or if a verbose map
762          * file is requested.
763          */
764         if (VerboseMap || Exp->ImpCount > 0) {
765
766             /* Print the export */
767             fprintf (F,
768                      "%s (%s):\n",
769                      GetString (Exp->Name),
770                      GetObjFileName (Exp->Obj));
771
772             /* Print all imports for this symbol */
773             Imp = Exp->ImpList;
774             while (Imp) {
775
776                 /* Print the import */
777                 fprintf (F,
778                          "    %-25s %s(%lu)\n",
779                          GetObjFileName (Imp->Obj),
780                          GetSourceFileName (Imp->Obj, Imp->Pos.Name),
781                          Imp->Pos.Line);
782
783                 /* Next import */
784                 Imp = Imp->Next;
785             }
786         }
787     }
788     fprintf (F, "\n");
789 }
790
791
792
793 void PrintExportLabels (FILE* F)
794 /* Print the exports in a VICE label file */
795 {
796     unsigned I;
797
798     /* Print all exports */
799     for (I = 0; I < ExpCount; ++I) {
800         const Export* E = ExpPool [I];
801         fprintf (F, "al %06lX .%s\n", GetExportVal (E), GetString (E->Name));
802     }
803 }
804
805
806
807 void MarkExport (Export* E)
808 /* Mark the export */
809 {
810     E->Flags |= EXP_USERMARK;
811 }
812
813
814
815 void UnmarkExport (Export* E)
816 /* Remove the mark from the export */
817 {
818     E->Flags &= ~EXP_USERMARK;
819 }
820
821
822
823 int ExportHasMark (Export* E)
824 /* Return true if the export has a mark */
825 {
826     return (E->Flags & EXP_USERMARK) != 0;
827 }
828
829
830
831 void CircularRefError (const Export* E)
832 /* Print an error about a circular reference using to define the given export */
833 {
834     Error ("Circular reference for symbol `%s', %s(%lu)",
835            GetString (E->Name),
836            GetSourceFileName (E->Obj, E->Pos.Name),
837            E->Pos.Line);
838 }
839
840
841
842