1 /*****************************************************************************/
5 /* Exports handling for the ld65 linker */
9 /* (C) 1998-2013, Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
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. */
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: */
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 */
32 /*****************************************************************************/
62 /*****************************************************************************/
64 /*****************************************************************************/
69 #define HASHTAB_MASK 0x0FFFU
70 #define HASHTAB_SIZE (HASHTAB_MASK + 1)
71 static Export* HashTab[HASHTAB_SIZE];
73 /* Import management variables */
74 static unsigned ImpCount = 0; /* Import count */
75 static unsigned ImpOpen = 0; /* Count of open imports */
77 /* Export management variables */
78 static unsigned ExpCount = 0; /* Export count */
79 static Export** ExpPool = 0; /* Exports array */
81 /* Defines for the flags in Import */
82 #define IMP_INLIST 0x0001U /* Import is in exports list */
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 */
90 /*****************************************************************************/
92 /*****************************************************************************/
96 static Export* NewExport (unsigned Type, unsigned char AddrSize,
97 unsigned Name, ObjData* Obj);
98 /* Create a new export and initialize it */
102 static Import* NewImport (unsigned char AddrSize, ObjData* Obj)
103 /* Create a new import and initialize it */
105 /* Allocate memory */
106 Import* I = xmalloc (sizeof (Import));
108 /* Initialize the fields */
111 I->DefLines = EmptyCollection;
112 I->RefLines = EmptyCollection;
114 I->Name = INVALID_STRING_ID;
116 I->AddrSize = AddrSize;
118 /* Return the new structure */
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).
131 PRECONDITION ((I->Flags & IMP_INLIST) == 0);
133 /* Free the line info collections */
134 DoneCollection (&I->DefLines);
135 DoneCollection (&I->RefLines);
137 /* Free the struct */
143 Import* ReadImport (FILE* F, ObjData* Obj)
144 /* Read an import from a file and return it */
148 /* Read the import address size */
149 unsigned char AddrSize = Read8 (F);
151 /* Create a new import */
152 I = NewImport (AddrSize, Obj);
155 I->Name = MakeGlobalStringId (Obj, ReadVar (F));
157 /* Read the line infos */
158 ReadLineInfoList (F, Obj, &I->DefLines);
159 ReadLineInfoList (F, Obj, &I->RefLines);
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.
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",
175 Error ("Invalid import size in for `%s', imported from %s: 0x%02X",
177 GetObjFileName (I->Obj),
182 /* Return the new import */
188 Import* GenImport (unsigned Name, unsigned char AddrSize)
189 /* Generate a new import with the given name and address size and return it */
191 /* Create a new import */
192 Import* I = NewImport (AddrSize, 0);
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
202 Error ("Invalid import size 0x%02X for symbol `%s'",
204 GetString (I->Name));
207 /* Return the new import */
213 Import* InsertImport (Import* I)
214 /* Insert an import into the table, return I */
218 /* As long as the import is not inserted, V.Name is valid */
219 unsigned Name = I->Name;
221 /* Create a hash value for the given name */
222 unsigned Hash = (Name & HASHTAB_MASK);
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);
232 if (E->Name == Name) {
233 /* We have an entry, L points to it */
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 */
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.
252 I->Next = E->ImpList;
255 ++ImpCount; /* Total import count */
257 /* This is a dummy export */
261 /* Mark the import so we know it's in the list */
262 I->Flags |= IMP_INLIST;
264 /* Return the import to allow shorter code */
270 const LineInfo* GetImportPos (const Import* Imp)
271 /* Return the basic line info of an import */
273 /* Search in DefLines, then in RefLines */
274 const LineInfo* LI = GetAsmLineInfo (&Imp->DefLines);
276 LI = GetAsmLineInfo (&Imp->RefLines);
283 /*****************************************************************************/
285 /*****************************************************************************/
289 static Export* NewExport (unsigned Type, unsigned char AddrSize,
290 unsigned Name, ObjData* Obj)
291 /* Create a new export and initialize it */
295 /* Allocate memory */
296 Export* E = xmalloc (sizeof (Export));
298 /* Initialize the fields */
307 E->DefLines = EmptyCollection;
308 E->RefLines = EmptyCollection;
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;
316 /* Return the new entry */
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).
329 PRECONDITION ((E->Flags & EXP_INLIST) == 0);
331 /* Free the line infos */
332 DoneCollection (&E->DefLines);
333 DoneCollection (&E->RefLines);
335 /* Free the export expression */
338 /* Free the struct */
344 Export* ReadExport (FILE* F, ObjData* O)
345 /* Read an export from a file */
347 unsigned ConDesCount;
352 unsigned Type = ReadVar (F);
354 /* Read the address size */
355 unsigned char AddrSize = Read8 (F);
357 /* Create a new export without a name */
358 E = NewExport (Type, AddrSize, INVALID_STRING_ID, O);
360 /* Read the constructor/destructor decls if we have any */
361 ConDesCount = SYM_GET_CONDES_COUNT (Type);
362 if (ConDesCount > 0) {
364 unsigned char ConDes[CD_TYPE_COUNT];
366 /* Read the data into temp storage */
367 ReadData (F, ConDes, ConDesCount);
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.
373 for (I = 0; I < ConDesCount; ++I) {
374 E->ConDes[CD_GET_TYPE (ConDes[I])] = CD_GET_PRIO (ConDes[I]);
379 E->Name = MakeGlobalStringId (O, ReadVar (F));
382 if (SYM_IS_EXPR (Type)) {
383 E->Expr = ReadExpr (F, O);
385 E->Expr = LiteralExpr (Read32 (F), O);
389 if (SYM_HAS_SIZE (Type)) {
390 E->Size = ReadVar (F);
393 /* Last are the locations */
394 ReadLineInfoList (F, O, &E->DefLines);
395 ReadLineInfoList (F, O, &E->RefLines);
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.
400 for (I = 0; I < CD_TYPE_COUNT; ++I) {
401 const ConDesImport* CDI;
403 if (E->ConDes[I] != CD_PRIO_NONE && (CDI = ConDesGetImport (I)) != 0) {
406 /* Generate a new import, and add it to the module's import list. */
407 Import* Imp = GenImport (CDI->Name, CDI->AddrSize);
410 CollAppend (&O->Imports, Imp);
412 /* Add line info for the export that is actually the condes that
413 * forces the import. Then, add line info for the config. file.
414 * The export's info is added first because the import pretends
415 * that it came from the object module instead of the config. file.
417 for (J = 0; J < CollCount (&E->DefLines); ++J) {
418 CollAppend (&Imp->RefLines, DupLineInfo (CollAt (&E->DefLines, J)));
420 CollAppend (&Imp->RefLines, GenLineInfo (&CDI->Pos));
424 /* Return the new export */
430 void InsertExport (Export* E)
431 /* Insert an exported identifier and check if it's already in the list */
438 /* Mark the export as inserted */
439 E->Flags |= EXP_INLIST;
441 /* Insert the export into any condes tables if needed */
442 if (SYM_IS_CONDES (E->Type)) {
446 /* Create a hash value for the given name */
447 Hash = (E->Name & HASHTAB_MASK);
449 /* Search through the list in that slot */
450 if (HashTab[Hash] == 0) {
451 /* The slot is empty */
459 if (L->Name == E->Name) {
460 /* This may be an unresolved external */
463 /* This *is* an unresolved external. Use the actual export
464 * in E instead of the dummy one in L.
467 E->ImpCount = L->ImpCount;
468 E->ImpList = L->ImpList;
474 ImpOpen -= E->ImpCount; /* Decrease open imports now */
476 /* We must run through the import list and change the
477 * export pointer now.
485 /* Duplicate entry, ignore it */
486 Warning ("Duplicate external identifier: `%s'",
487 GetString (L->Name));
496 /* Insert export at end of queue */
504 const LineInfo* GetExportPos (const Export* E)
505 /* Return the basic line info of an export */
507 /* Search in DefLines, then in RefLines */
508 const LineInfo* LI = GetAsmLineInfo (&E->DefLines);
510 LI = GetAsmLineInfo (&E->RefLines);
517 Export* CreateConstExport (unsigned Name, long Value)
518 /* Create an export for a literal date */
520 /* Create a new export */
521 Export* E = NewExport (SYM_CONST|SYM_EQUATE, ADDR_SIZE_ABS, Name, 0);
523 /* Assign the value */
524 E->Expr = LiteralExpr (Value, 0);
526 /* Insert the export */
529 /* Return the new export */
535 Export* CreateExprExport (unsigned Name, ExprNode* Expr, unsigned char AddrSize)
536 /* Create an export for an expression */
538 /* Create a new export */
539 Export* E = NewExport (SYM_EXPR|SYM_EQUATE, AddrSize, Name, 0);
541 /* Assign the value expression */
544 /* Insert the export */
547 /* Return the new export */
553 Export* CreateMemoryExport (unsigned Name, MemoryArea* Mem, unsigned long Offs)
554 /* Create an relative export for a memory area offset */
556 /* Create a new export */
557 Export* E = NewExport (SYM_EXPR | SYM_LABEL, ADDR_SIZE_ABS, Name, 0);
559 /* Assign the value */
560 E->Expr = MemoryExpr (Mem, Offs, 0);
562 /* Insert the export */
565 /* Return the new export */
571 Export* CreateSegmentExport (unsigned Name, Segment* Seg, unsigned long Offs)
572 /* Create a relative export to a segment */
574 /* Create a new export */
575 Export* E = NewExport (SYM_EXPR | SYM_LABEL, Seg->AddrSize, Name, 0);
577 /* Assign the value */
578 E->Expr = SegmentExpr (Seg, Offs, 0);
580 /* Insert the export */
583 /* Return the new export */
589 Export* CreateSectionExport (unsigned Name, Section* Sec, unsigned long Offs)
590 /* Create a relative export to a section */
592 /* Create a new export */
593 Export* E = NewExport (SYM_EXPR | SYM_LABEL, Sec->AddrSize, Name, 0);
595 /* Assign the value */
596 E->Expr = SectionExpr (Sec, Offs, 0);
598 /* Insert the export */
601 /* Return the new export */
607 Export* FindExport (unsigned Name)
608 /* Check for an identifier in the list. Return 0 if not found, otherwise
609 * return a pointer to the export.
612 /* Get a pointer to the list with the symbols hash value */
613 Export* L = HashTab[Name & HASHTAB_MASK];
615 /* Search through the list in that slot */
616 if (L->Name == Name) {
629 int IsUnresolved (unsigned Name)
630 /* Check if this symbol is an unresolved export */
632 /* Find the export */
633 return IsUnresolvedExport (FindExport (Name));
638 int IsUnresolvedExport (const Export* E)
639 /* Return true if the given export is unresolved */
641 /* Check if it's unresolved */
642 return E != 0 && E->Expr == 0;
647 int IsConstExport (const Export* E)
648 /* Return true if the expression associated with this export is const */
651 /* External symbols cannot be const */
654 return IsConstExpr (E->Expr);
660 long GetExportVal (const Export* E)
661 /* Get the value of this export */
665 Internal ("`%s' is an undefined external", GetString (E->Name));
667 return GetExprVal (E->Expr);
672 static void CheckSymType (const Export* E)
673 /* Check the types for one export */
675 /* External with matching imports */
676 Import* I = E->ImpList;
678 if (E->AddrSize != I->AddrSize) {
679 /* Export and import address sizes do not match */
680 StrBuf ExportLoc = STATIC_STRBUF_INITIALIZER;
681 StrBuf ImportLoc = STATIC_STRBUF_INITIALIZER;
682 const char* ExpAddrSize = AddrSizeToStr ((unsigned char) E->AddrSize);
683 const char* ImpAddrSize = AddrSizeToStr ((unsigned char) I->AddrSize);
684 const LineInfo* ExportLI = GetExportPos (E);
685 const LineInfo* ImportLI = GetImportPos (I);
687 /* Generate strings that describe the location of the im- and
688 * exports. This depends on the place from where they come:
689 * Object file or linker config.
692 /* The export comes from an object file */
693 SB_Printf (&ExportLoc, "%s, %s(%u)",
694 GetString (E->Obj->Name),
695 GetSourceName (ExportLI),
696 GetSourceLine (ExportLI));
698 SB_Printf (&ExportLoc, "%s(%u)",
699 GetSourceName (ExportLI),
700 GetSourceLine (ExportLI));
703 /* The import comes from an object file */
704 SB_Printf (&ImportLoc, "%s, %s(%u)",
705 GetString (I->Obj->Name),
706 GetSourceName (ImportLI),
707 GetSourceLine (ImportLI));
708 } else if (ImportLI) {
709 /* The import is linker generated and we have line
712 SB_Printf (&ImportLoc, "%s(%u)",
713 GetSourceName (ImportLI),
714 GetSourceLine (ImportLI));
716 /* The import is linker generated and we don't have line
719 SB_Printf (&ImportLoc, "%s", GetObjFileName (I->Obj));
722 /* Output the diagnostic */
723 Warning ("Address size mismatch for `%s': "
724 "Exported from %s as `%s', "
725 "import in %s as `%s'",
727 SB_GetConstBuf (&ExportLoc),
729 SB_GetConstBuf (&ImportLoc),
732 /* Free the temporary strings */
733 SB_Done (&ExportLoc);
734 SB_Done (&ImportLoc);
742 static void CheckSymTypes (void)
743 /* Check for symbol tape mismatches */
747 /* Print all open imports */
748 for (I = 0; I < ExpCount; ++I) {
749 const Export* E = ExpPool [I];
750 if (E->Expr != 0 && E->ImpCount > 0) {
751 /* External with matching imports */
759 static void PrintUnresolved (ExpCheckFunc F, void* Data)
760 /* Print a list of unresolved symbols. On unresolved symbols, F is
761 * called (see the comments on ExpCheckFunc in the data section).
766 /* Print all open imports */
767 for (I = 0; I < ExpCount; ++I) {
768 Export* E = ExpPool [I];
769 if (E->Expr == 0 && E->ImpCount > 0 && F (E->Name, Data) == 0) {
770 /* Unresolved external */
771 Import* Imp = E->ImpList;
773 "Unresolved external `%s' referenced in:\n",
774 GetString (E->Name));
777 for (J = 0; J < CollCount (&Imp->RefLines); ++J) {
778 const LineInfo* LI = CollConstAt (&Imp->RefLines, J);
792 static int CmpExpName (const void* K1, const void* K2)
793 /* Compare function for qsort */
795 return SB_Compare (GetStrBuf ((*(Export**)K1)->Name),
796 GetStrBuf ((*(Export**)K2)->Name));
801 static void CreateExportPool (void)
802 /* Create an array with pointer to all exports */
806 /* Allocate memory */
810 ExpPool = xmalloc (ExpCount * sizeof (Export*));
812 /* Walk through the list and insert the exports */
813 for (I = 0, J = 0; I < sizeof (HashTab) / sizeof (HashTab [0]); ++I) {
814 Export* E = HashTab[I];
816 CHECK (J < ExpCount);
822 /* Sort them by name */
823 qsort (ExpPool, ExpCount, sizeof (Export*), CmpExpName);
828 void CheckExports (void)
829 /* Setup the list of all exports and check for export/import symbol type
833 /* Create an export pool */
836 /* Check for symbol type mismatches */
842 void CheckUnresolvedImports (ExpCheckFunc F, void* Data)
843 /* Check if there are any unresolved imports. On unresolved imports, F is
844 * called (see the comments on ExpCheckFunc in the data section).
847 /* Check for unresolved externals */
849 /* Print all open imports */
850 PrintUnresolved (F, Data);
856 static char GetAddrSizeCode (unsigned char AddrSize)
857 /* Get a one char code for the address size */
860 case ADDR_SIZE_ZP: return 'Z';
861 case ADDR_SIZE_ABS: return 'A';
862 case ADDR_SIZE_FAR: return 'F';
863 case ADDR_SIZE_LONG: return 'L';
865 Internal ("Invalid address size: %u", AddrSize);
873 void PrintExportMapByName (FILE* F)
874 /* Print an export map, sorted by symbol name, to the given file */
879 /* Print all exports */
881 for (I = 0; I < ExpCount; ++I) {
882 const Export* E = ExpPool [I];
884 /* Print unreferenced symbols only if explictly requested */
885 if (VerboseMap || E->ImpCount > 0 || SYM_IS_CONDES (E->Type)) {
887 "%-25s %06lX %c%c%c%c ",
890 E->ImpCount? 'R' : ' ',
891 SYM_IS_LABEL (E->Type)? 'L' : 'E',
892 GetAddrSizeCode ((unsigned char) E->AddrSize),
893 SYM_IS_CONDES (E->Type)? 'I' : ' ');
905 static int CmpExpValue (const void* I1, const void* I2)
906 /* Compare function for qsort */
908 long V1 = GetExportVal (ExpPool [*(unsigned *)I1]);
909 long V2 = GetExportVal (ExpPool [*(unsigned *)I2]);
911 return V1 < V2 ? -1 : V1 == V2 ? 0 : 1;
916 void PrintExportMapByValue (FILE* F)
917 /* Print an export map, sorted by symbol value, to the given file */
921 unsigned *ExpValXlat;
923 /* Create a translation table where the symbols are sorted by value. */
924 ExpValXlat = xmalloc (ExpCount * sizeof (unsigned));
925 for (I = 0; I < ExpCount; ++I) {
926 /* Initialize table with current sort order. */
930 /* Sort them by value */
931 qsort (ExpValXlat, ExpCount, sizeof (unsigned), CmpExpValue);
933 /* Print all exports */
935 for (I = 0; I < ExpCount; ++I) {
936 const Export* E = ExpPool [ExpValXlat [I]];
938 /* Print unreferenced symbols only if explictly requested */
939 if (VerboseMap || E->ImpCount > 0 || SYM_IS_CONDES (E->Type)) {
941 "%-25s %06lX %c%c%c%c ",
944 E->ImpCount? 'R' : ' ',
945 SYM_IS_LABEL (E->Type)? 'L' : 'E',
946 GetAddrSizeCode ((unsigned char) E->AddrSize),
947 SYM_IS_CONDES (E->Type)? 'I' : ' ');
960 void PrintImportMap (FILE* F)
961 /* Print an import map to the given file */
966 /* Loop over all exports */
967 for (I = 0; I < ExpCount; ++I) {
970 const Export* Exp = ExpPool [I];
972 /* Print the symbol only if there are imports, or if a verbose map
975 if (VerboseMap || Exp->ImpCount > 0) {
977 /* Print the export */
980 GetString (Exp->Name),
981 GetObjFileName (Exp->Obj));
983 /* Print all imports for this symbol */
987 /* Print the import. Beware: The import might be linker
988 * generated, in which case there is no object file and
989 * sometimes no line information.
991 const LineInfo* LI = GetImportPos (Imp);
995 GetObjFileName (Imp->Obj),
1001 GetObjFileName (Imp->Obj));
1014 void PrintExportLabels (FILE* F)
1015 /* Print the exports in a VICE label file */
1019 /* Print all exports */
1020 for (I = 0; I < ExpCount; ++I) {
1021 const Export* E = ExpPool [I];
1022 fprintf (F, "al %06lX .%s\n", GetExportVal (E), GetString (E->Name));
1028 void MarkExport (Export* E)
1029 /* Mark the export */
1031 E->Flags |= EXP_USERMARK;
1036 void UnmarkExport (Export* E)
1037 /* Remove the mark from the export */
1039 E->Flags &= ~EXP_USERMARK;
1044 int ExportHasMark (Export* E)
1045 /* Return true if the export has a mark */
1047 return (E->Flags & EXP_USERMARK) != 0;
1052 void CircularRefError (const Export* E)
1053 /* Print an error about a circular reference using to define the given export */
1055 const LineInfo* LI = GetExportPos (E);
1056 Error ("Circular reference for symbol `%s', %s(%u)",
1057 GetString (E->Name),
1059 GetSourceLine (LI));