1 /*****************************************************************************/
5 /* Exports handling for the ld65 linker */
9 /* (C) 1998-2010, 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 /*****************************************************************************/
61 /*****************************************************************************/
63 /*****************************************************************************/
68 #define HASHTAB_MASK 0x0FFFU
69 #define HASHTAB_SIZE (HASHTAB_MASK + 1)
70 static Export* HashTab[HASHTAB_SIZE];
72 /* Import management variables */
73 static unsigned ImpCount = 0; /* Import count */
74 static unsigned ImpOpen = 0; /* Count of open imports */
76 /* Export management variables */
77 static unsigned ExpCount = 0; /* Export count */
78 static Export** ExpPool = 0; /* Exports array */
80 /* Defines for the flags in Import */
81 #define IMP_INLIST 0x0001U /* Import is in exports list */
83 /* Defines for the flags in Export */
84 #define EXP_INLIST 0x0001U /* Export is in exports list */
85 #define EXP_USERMARK 0x0002U /* User setable flag */
89 /*****************************************************************************/
91 /*****************************************************************************/
95 static Export* NewExport (unsigned char Type, unsigned char AddrSize,
96 unsigned Name, ObjData* Obj);
97 /* Create a new export and initialize it */
101 static Import* NewImport (unsigned char AddrSize, ObjData* Obj)
102 /* Create a new import and initialize it */
104 /* Allocate memory */
105 Import* I = xmalloc (sizeof (Import));
107 /* Initialize the fields */
110 InitFilePos (&I->Pos);
112 I->Name = INVALID_STRING_ID;
114 I->AddrSize = AddrSize;
116 /* Return the new structure */
122 void FreeImport (Import* I)
123 /* Free an import. NOTE: This won't remove the import from the exports table,
124 * so it may only be called for unused imports (imports from modules that
125 * aren't referenced).
129 PRECONDITION ((I->Flags & IMP_INLIST) == 0);
131 /* Free the struct */
137 Import* ReadImport (FILE* F, ObjData* Obj)
138 /* Read an import from a file and return it */
142 /* Read the import address size */
143 unsigned char AddrSize = Read8 (F);
145 /* Create a new import */
146 I = NewImport (AddrSize, Obj);
149 I->Name = MakeGlobalStringId (Obj, ReadVar (F));
151 /* Read the file position */
152 ReadFilePos (F, &I->Pos);
154 /* Check the address size */
155 if (I->AddrSize == ADDR_SIZE_DEFAULT || I->AddrSize > ADDR_SIZE_LONG) {
156 /* Beware: This function may be called in cases where the object file
157 * is not read completely into memory. In this case, the file list is
158 * invalid. Be sure not to access it in this case.
160 if (ObjHasFiles (I->Obj)) {
161 Error ("Invalid import size in for `%s', imported from %s(%lu): 0x%02X",
163 GetSourceFileName (I->Obj, I->Pos.Name),
167 Error ("Invalid import size in for `%s', imported from %s: 0x%02X",
169 GetObjFileName (I->Obj),
174 /* Return the new import */
180 Import* GenImport (unsigned Name, unsigned char AddrSize)
181 /* Generate a new import with the given name and address size and return it */
183 /* Create a new import */
184 Import* I = NewImport (AddrSize, 0);
189 /* Check the address size */
190 if (I->AddrSize == ADDR_SIZE_DEFAULT || I->AddrSize > ADDR_SIZE_LONG) {
191 /* Beware: This function may be called in cases where the object file
192 * is not read completely into memory. In this case, the file list is
193 * invalid. Be sure not to access it in this case.
195 if (ObjHasFiles (I->Obj)) {
196 Error ("Invalid import size in for `%s', imported from %s(%lu): 0x%02X",
198 GetSourceFileName (I->Obj, I->Pos.Name),
202 Error ("Invalid import size in for `%s', imported from %s: 0x%02X",
204 GetObjFileName (I->Obj),
209 /* Return the new import */
215 Import* InsertImport (Import* I)
216 /* Insert an import into the table, return I */
220 /* As long as the import is not inserted, V.Name is valid */
221 unsigned Name = I->Name;
223 /* Create a hash value for the given name */
224 unsigned Hash = (Name & HASHTAB_MASK);
226 /* Search through the list in that slot and print matching duplicates */
227 if (HashTab[Hash] == 0) {
228 /* The slot is empty, we need to insert a dummy export */
229 E = HashTab[Hash] = NewExport (0, ADDR_SIZE_DEFAULT, Name, 0);
234 if (E->Name == Name) {
235 /* We have an entry, L points to it */
239 /* End of list an entry not found, insert a dummy */
240 E->Next = NewExport (0, ADDR_SIZE_DEFAULT, Name, 0);
241 E = E->Next; /* Point to dummy */
242 ++ExpCount; /* One export more */
250 /* Ok, E now points to a valid exports entry for the given import. Insert
251 * the import into the imports list and update the counters.
254 I->Next = E->ImpList;
257 ++ImpCount; /* Total import count */
259 /* This is a dummy export */
263 /* Mark the import so we know it's in the list */
264 I->Flags |= IMP_INLIST;
266 /* Return the import to allow shorter code */
272 /*****************************************************************************/
274 /*****************************************************************************/
278 static Export* NewExport (unsigned char Type, unsigned char AddrSize,
279 unsigned Name, ObjData* Obj)
280 /* Create a new export and initialize it */
282 /* Allocate memory */
283 Export* E = xmalloc (sizeof (Export));
285 /* Initialize the fields */
293 InitFilePos (&E->Pos);
295 E->AddrSize = AddrSize;
296 memset (E->ConDes, 0, sizeof (E->ConDes));
298 /* Return the new entry */
304 void FreeExport (Export* E)
305 /* Free an export. NOTE: This won't remove the export from the exports table,
306 * so it may only be called for unused exports (exports from modules that
307 * aren't referenced).
311 PRECONDITION ((E->Flags & EXP_INLIST) == 0);
313 /* Free the export expression */
316 /* Free the struct */
322 void InsertExport (Export* E)
323 /* Insert an exported identifier and check if it's already in the list */
330 /* Mark the export as inserted */
331 E->Flags |= EXP_INLIST;
333 /* Insert the export into any condes tables if needed */
334 if (SYM_IS_CONDES (E->Type)) {
338 /* Create a hash value for the given name */
339 Hash = (E->Name & HASHTAB_MASK);
341 /* Search through the list in that slot */
342 if (HashTab[Hash] == 0) {
343 /* The slot is empty */
351 if (L->Name == E->Name) {
352 /* This may be an unresolved external */
355 /* This *is* an unresolved external. Use the actual export
356 * in E instead of the dummy one in L.
359 E->ImpCount = L->ImpCount;
360 E->ImpList = L->ImpList;
366 ImpOpen -= E->ImpCount; /* Decrease open imports now */
368 /* We must run through the import list and change the
369 * export pointer now.
377 /* Duplicate entry, ignore it */
378 Warning ("Duplicate external identifier: `%s'",
379 GetString (L->Name));
388 /* Insert export at end of queue */
396 Export* ReadExport (FILE* F, ObjData* O)
397 /* Read an export from a file */
399 unsigned ConDesCount;
403 unsigned char Type = ReadVar (F);
405 /* Read the address size */
406 unsigned char AddrSize = Read8 (F);
408 /* Create a new export without a name */
409 E = NewExport (Type, AddrSize, INVALID_STRING_ID, O);
411 /* Read the constructor/destructor decls if we have any */
412 ConDesCount = SYM_GET_CONDES_COUNT (Type);
413 if (ConDesCount > 0) {
415 unsigned char ConDes[CD_TYPE_COUNT];
418 /* Read the data into temp storage */
419 ReadData (F, ConDes, ConDesCount);
421 /* Re-order the data. In the file, each decl is encoded into a byte
422 * which contains the type and the priority. In memory, we will use
423 * an array of types which contain the priority. This array was
424 * cleared by the constructor (NewExport), so we must only set the
425 * fields that contain values.
427 for (I = 0; I < ConDesCount; ++I) {
428 unsigned ConDesType = CD_GET_TYPE (ConDes[I]);
429 unsigned ConDesPrio = CD_GET_PRIO (ConDes[I]);
430 E->ConDes[ConDesType] = ConDesPrio;
435 E->Name = MakeGlobalStringId (O, ReadVar (F));
438 if (SYM_IS_EXPR (Type)) {
439 E->Expr = ReadExpr (F, O);
441 E->Expr = LiteralExpr (Read32 (F), O);
444 /* Last is the file position where the definition was done */
445 ReadFilePos (F, &E->Pos);
447 /* Return the new export */
453 Export* CreateConstExport (unsigned Name, long Value)
454 /* Create an export for a literal date */
456 /* Create a new export */
457 Export* E = NewExport (SYM_CONST | SYM_EQUATE, ADDR_SIZE_ABS, Name, 0);
459 /* Assign the value */
460 E->Expr = LiteralExpr (Value, 0);
462 /* Insert the export */
465 /* Return the new export */
471 Export* CreateExprExport (unsigned Name, ExprNode* Expr, unsigned char AddrSize)
472 /* Create an export for an expression */
474 /* Create a new export */
475 Export* E = NewExport (SYM_EXPR | SYM_EQUATE, AddrSize, Name, 0);
477 /* Assign the value expression */
480 /* Insert the export */
483 /* Return the new export */
489 Export* CreateMemoryExport (unsigned Name, MemoryArea* Mem, unsigned long Offs)
490 /* Create an relative export for a memory area offset */
492 /* Create a new export */
493 Export* E = NewExport (SYM_EXPR | SYM_LABEL, ADDR_SIZE_ABS, Name, 0);
495 /* Assign the value */
496 E->Expr = MemoryExpr (Mem, Offs, 0);
498 /* Insert the export */
501 /* Return the new export */
507 Export* CreateSegmentExport (unsigned Name, Segment* Seg, unsigned long Offs)
508 /* Create a relative export to a segment */
510 /* Create a new export */
511 Export* E = NewExport (SYM_EXPR | SYM_LABEL, Seg->AddrSize, Name, 0);
513 /* Assign the value */
514 E->Expr = SegmentExpr (Seg, Offs, 0);
516 /* Insert the export */
519 /* Return the new export */
525 Export* CreateSectionExport (unsigned Name, Section* Sec, unsigned long Offs)
526 /* Create a relative export to a section */
528 /* Create a new export */
529 Export* E = NewExport (SYM_EXPR | SYM_LABEL, Sec->AddrSize, Name, 0);
531 /* Assign the value */
532 E->Expr = SectionExpr (Sec, Offs, 0);
534 /* Insert the export */
537 /* Return the new export */
543 Export* FindExport (unsigned Name)
544 /* Check for an identifier in the list. Return 0 if not found, otherwise
545 * return a pointer to the export.
548 /* Get a pointer to the list with the symbols hash value */
549 Export* L = HashTab[Name & HASHTAB_MASK];
551 /* Search through the list in that slot */
552 if (L->Name == Name) {
565 int IsUnresolved (unsigned Name)
566 /* Check if this symbol is an unresolved export */
568 /* Find the export */
569 return IsUnresolvedExport (FindExport (Name));
574 int IsUnresolvedExport (const Export* E)
575 /* Return true if the given export is unresolved */
577 /* Check if it's unresolved */
578 return E != 0 && E->Expr == 0;
583 int IsConstExport (const Export* E)
584 /* Return true if the expression associated with this export is const */
587 /* External symbols cannot be const */
590 return IsConstExpr (E->Expr);
596 long GetExportVal (const Export* E)
597 /* Get the value of this export */
601 Internal ("`%s' is an undefined external", GetString (E->Name));
603 return GetExprVal (E->Expr);
608 static void CheckSymType (const Export* E)
609 /* Check the types for one export */
611 /* External with matching imports */
612 Import* I = E->ImpList;
614 if (E->AddrSize != I->AddrSize) {
615 /* Export and import address sizes do not match */
616 StrBuf ExportLoc = STATIC_STRBUF_INITIALIZER;
617 StrBuf ImportLoc = STATIC_STRBUF_INITIALIZER;
618 const char* ExpAddrSize = AddrSizeToStr (E->AddrSize);
619 const char* ImpAddrSize = AddrSizeToStr (I->AddrSize);
621 /* Generate strings that describe the location of the im- and
622 * exports. This depends on the place from where they come:
623 * Object file or linker config.
626 /* The export comes from an object file */
627 SB_Printf (&ExportLoc, "%s, %s(%lu)",
628 GetString (E->Obj->Name),
629 GetSourceFileName (E->Obj, E->Pos.Name),
632 SB_Printf (&ExportLoc, "%s(%lu)",
633 GetSourceFileName (E->Obj, E->Pos.Name),
637 /* The import comes from an object file */
638 SB_Printf (&ImportLoc, "%s, %s(%lu)",
639 GetString (I->Obj->Name),
640 GetSourceFileName (I->Obj, I->Pos.Name),
643 SB_Printf (&ImportLoc, "%s(%lu)",
644 GetSourceFileName (I->Obj, I->Pos.Name),
648 /* Output the diagnostic */
649 Warning ("Address size mismatch for `%s': "
650 "Exported from %s as `%s', "
651 "import in %s as `%s'",
653 SB_GetConstBuf (&ExportLoc),
655 SB_GetConstBuf (&ImportLoc),
658 /* Free the temporary strings */
659 SB_Done (&ExportLoc);
660 SB_Done (&ImportLoc);
668 static void CheckSymTypes (void)
669 /* Check for symbol tape mismatches */
673 /* Print all open imports */
674 for (I = 0; I < ExpCount; ++I) {
675 const Export* E = ExpPool [I];
676 if (E->Expr != 0 && E->ImpCount > 0) {
677 /* External with matching imports */
685 static void PrintUnresolved (ExpCheckFunc F, void* Data)
686 /* Print a list of unresolved symbols. On unresolved symbols, F is
687 * called (see the comments on ExpCheckFunc in the data section).
692 /* Print all open imports */
693 for (I = 0; I < ExpCount; ++I) {
694 Export* E = ExpPool [I];
695 if (E->Expr == 0 && E->ImpCount > 0 && F (E->Name, Data) == 0) {
696 /* Unresolved external */
697 Import* Imp = E->ImpList;
699 "Unresolved external `%s' referenced in:\n",
700 GetString (E->Name));
702 const char* Name = GetSourceFileName (Imp->Obj, Imp->Pos.Name);
703 fprintf (stderr, " %s(%lu)\n", Name, Imp->Pos.Line);
712 static int CmpExpName (const void* K1, const void* K2)
713 /* Compare function for qsort */
715 return SB_Compare (GetStrBuf ((*(Export**)K1)->Name),
716 GetStrBuf ((*(Export**)K2)->Name));
721 static void CreateExportPool (void)
722 /* Create an array with pointer to all exports */
726 /* Allocate memory */
730 ExpPool = xmalloc (ExpCount * sizeof (Export*));
732 /* Walk through the list and insert the exports */
733 for (I = 0, J = 0; I < sizeof (HashTab) / sizeof (HashTab [0]); ++I) {
734 Export* E = HashTab[I];
736 CHECK (J < ExpCount);
742 /* Sort them by name */
743 qsort (ExpPool, ExpCount, sizeof (Export*), CmpExpName);
748 void CheckExports (void)
749 /* Setup the list of all exports and check for export/import symbol type
753 /* Create an export pool */
756 /* Check for symbol type mismatches */
762 void CheckUnresolvedImports (ExpCheckFunc F, void* Data)
763 /* Check if there are any unresolved imports. On unresolved imports, F is
764 * called (see the comments on ExpCheckFunc in the data section).
767 /* Check for unresolved externals */
769 /* Print all open imports */
770 PrintUnresolved (F, Data);
776 static char GetAddrSizeCode (unsigned char AddrSize)
777 /* Get a one char code for the address size */
780 case ADDR_SIZE_ZP: return 'Z';
781 case ADDR_SIZE_ABS: return 'A';
782 case ADDR_SIZE_FAR: return 'F';
783 case ADDR_SIZE_LONG: return 'L';
785 Internal ("Invalid address size: %u", AddrSize);
793 void PrintExportMap (FILE* F)
794 /* Print an export map to the given file */
799 /* Print all exports */
801 for (I = 0; I < ExpCount; ++I) {
802 const Export* E = ExpPool [I];
804 /* Print unreferenced symbols only if explictly requested */
805 if (VerboseMap || E->ImpCount > 0 || SYM_IS_CONDES (E->Type)) {
807 "%-25s %06lX %c%c%c%c ",
810 E->ImpCount? 'R' : ' ',
811 SYM_IS_LABEL (E->Type)? 'L' : 'E',
812 GetAddrSizeCode (E->AddrSize),
813 SYM_IS_CONDES (E->Type)? 'I' : ' ');
825 void PrintImportMap (FILE* F)
826 /* Print an import map to the given file */
831 /* Loop over all exports */
832 for (I = 0; I < ExpCount; ++I) {
835 const Export* Exp = ExpPool [I];
837 /* Print the symbol only if there are imports, or if a verbose map
840 if (VerboseMap || Exp->ImpCount > 0) {
842 /* Print the export */
845 GetString (Exp->Name),
846 GetObjFileName (Exp->Obj));
848 /* Print all imports for this symbol */
852 /* Print the import */
855 GetObjFileName (Imp->Obj),
856 GetSourceFileName (Imp->Obj, Imp->Pos.Name),
869 void PrintExportLabels (FILE* F)
870 /* Print the exports in a VICE label file */
874 /* Print all exports */
875 for (I = 0; I < ExpCount; ++I) {
876 const Export* E = ExpPool [I];
877 fprintf (F, "al %06lX .%s\n", GetExportVal (E), GetString (E->Name));
883 void MarkExport (Export* E)
884 /* Mark the export */
886 E->Flags |= EXP_USERMARK;
891 void UnmarkExport (Export* E)
892 /* Remove the mark from the export */
894 E->Flags &= ~EXP_USERMARK;
899 int ExportHasMark (Export* E)
900 /* Return true if the export has a mark */
902 return (E->Flags & EXP_USERMARK) != 0;
907 void CircularRefError (const Export* E)
908 /* Print an error about a circular reference using to define the given export */
910 Error ("Circular reference for symbol `%s', %s(%lu)",
912 GetSourceFileName (E->Obj, E->Pos.Name),