1 /*****************************************************************************/
5 /* Exports handling for the ld65 linker */
9 /* (C) 1998-2003 Ullrich von Bassewitz */
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 /*****************************************************************************/
60 /*****************************************************************************/
62 /*****************************************************************************/
67 #define HASHTAB_MASK 0x0FFFU
68 #define HASHTAB_SIZE (HASHTAB_MASK + 1)
69 static Export* HashTab[HASHTAB_SIZE];
71 /* Import management variables */
72 static unsigned ImpCount = 0; /* Import count */
73 static unsigned ImpOpen = 0; /* Count of open imports */
75 /* Export management variables */
76 static unsigned ExpCount = 0; /* Export count */
77 static Export** ExpPool = 0; /* Exports array */
79 /* Defines for the flags in Import */
80 #define IMP_INLIST 0x0001U /* Import is in exports list */
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 */
88 /*****************************************************************************/
90 /*****************************************************************************/
94 static Export* NewExport (unsigned char Type, unsigned char AddrSize,
95 unsigned Name, ObjData* Obj);
96 /* Create a new export and initialize it */
100 static Import* NewImport (unsigned char AddrSize, ObjData* Obj)
101 /* Create a new import and initialize it */
103 /* Allocate memory */
104 Import* I = xmalloc (sizeof (Import));
106 /* Initialize the fields */
110 I->Name = INVALID_STRING_ID;
112 I->AddrSize = AddrSize;
114 /* Return the new structure */
120 void InsertImport (Import* I)
121 /* Insert an import into the table */
125 /* As long as the import is not inserted, V.Name is valid */
126 unsigned Name = I->Name;
128 /* Create a hash value for the given name */
129 unsigned Hash = (Name & HASHTAB_MASK);
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);
139 if (E->Name == Name) {
140 /* We have an entry, L points to it */
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 */
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.
159 I->Next = E->ImpList;
162 ++ImpCount; /* Total import count */
164 /* This is a dummy export */
168 /* Mark the import so we know it's in the list */
169 I->Flags |= IMP_INLIST;
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).
181 PRECONDITION ((I->Flags & IMP_INLIST) == 0);
183 /* Free the struct */
189 Import* ReadImport (FILE* F, ObjData* Obj)
190 /* Read an import from a file and return it */
194 /* Read the import address size */
195 unsigned char AddrSize = Read8 (F);
197 /* Create a new import */
198 I = NewImport (AddrSize, Obj);
201 I->Name = MakeGlobalStringId (Obj, ReadVar (F));
203 /* Read the file position */
204 ReadFilePos (F, &I->Pos);
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.
212 if (ObjHasFiles (I->Obj)) {
213 Error ("Invalid import size in for `%s', imported from %s(%lu): 0x%02X",
215 GetSourceFileName (I->Obj, I->Pos.Name),
219 Error ("Invalid import size in for `%s', imported from %s: 0x%02X",
221 GetObjFileName (I->Obj),
226 /* Return the new import */
232 /*****************************************************************************/
234 /*****************************************************************************/
238 static Export* NewExport (unsigned char Type, unsigned char AddrSize,
239 unsigned Name, ObjData* Obj)
240 /* Create a new export and initialize it */
242 /* Allocate memory */
243 Export* E = xmalloc (sizeof (Export));
245 /* Initialize the fields */
254 E->AddrSize = AddrSize;
255 memset (E->ConDes, 0, sizeof (E->ConDes));
257 /* Return the new entry */
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).
270 PRECONDITION ((E->Flags & EXP_INLIST) == 0);
272 /* Free the export expression */
275 /* Free the struct */
281 void InsertExport (Export* E)
282 /* Insert an exported identifier and check if it's already in the list */
289 /* Mark the export as inserted */
290 E->Flags |= EXP_INLIST;
292 /* Insert the export into any condes tables if needed */
293 if (IS_EXP_CONDES (E->Type)) {
297 /* Create a hash value for the given name */
298 Hash = (E->Name & HASHTAB_MASK);
300 /* Search through the list in that slot */
301 if (HashTab[Hash] == 0) {
302 /* The slot is empty */
310 if (L->Name == E->Name) {
311 /* This may be an unresolved external */
314 /* This *is* an unresolved external. Use the actual export
315 * in E instead of the dummy one in L.
318 E->ImpCount = L->ImpCount;
319 E->ImpList = L->ImpList;
325 ImpOpen -= E->ImpCount; /* Decrease open imports now */
327 /* We must run through the import list and change the
328 * export pointer now.
336 /* Duplicate entry, ignore it */
337 Warning ("Duplicate external identifier: `%s'",
338 GetString (L->Name));
347 /* Insert export at end of queue */
355 Export* ReadExport (FILE* F, ObjData* O)
356 /* Read an export from a file */
358 unsigned ConDesCount;
362 unsigned char Type = Read8 (F);
364 /* Read the address size */
365 unsigned char AddrSize = Read8 (F);
367 /* Create a new export without a name */
368 E = NewExport (Type, AddrSize, INVALID_STRING_ID, O);
370 /* Read the constructor/destructor decls if we have any */
371 ConDesCount = GET_EXP_CONDES_COUNT (Type);
372 if (ConDesCount > 0) {
374 unsigned char ConDes[CD_TYPE_COUNT];
377 /* Read the data into temp storage */
378 ReadData (F, ConDes, ConDesCount);
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.
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;
394 E->Name = MakeGlobalStringId (O, ReadVar (F));
397 if (IS_EXP_EXPR (Type)) {
398 E->Expr = ReadExpr (F, O);
400 E->Expr = LiteralExpr (Read32 (F), O);
403 /* Last is the file position where the definition was done */
404 ReadFilePos (F, &E->Pos);
406 /* Return the new export */
412 Export* CreateConstExport (unsigned Name, long Value)
413 /* Create an export for a literal date */
415 /* Create a new export */
416 Export* E = NewExport (EXP_CONST | EXP_EQUATE, ADDR_SIZE_ABS, Name, 0);
418 /* Assign the value */
419 E->Expr = LiteralExpr (Value, 0);
421 /* Insert the export */
424 /* Return the new export */
430 Export* CreateMemoryExport (unsigned Name, Memory* Mem, unsigned long Offs)
431 /* Create an relative export for a memory area offset */
433 /* Create a new export */
434 Export* E = NewExport (EXP_EXPR | EXP_LABEL, ADDR_SIZE_ABS, Name, 0);
436 /* Assign the value */
437 E->Expr = MemoryExpr (Mem, Offs, 0);
439 /* Insert the export */
442 /* Return the new export */
448 Export* CreateSegmentExport (unsigned Name, Segment* Seg, unsigned long Offs)
449 /* Create a relative export to a segment */
451 /* Create a new export */
452 Export* E = NewExport (EXP_EXPR | EXP_LABEL, Seg->AddrSize, Name, 0);
454 /* Assign the value */
455 E->Expr = SegmentExpr (Seg, Offs, 0);
457 /* Insert the export */
460 /* Return the new export */
466 Export* CreateSectionExport (unsigned Name, Section* Sec, unsigned long Offs)
467 /* Create a relative export to a section */
469 /* Create a new export */
470 Export* E = NewExport (EXP_EXPR | EXP_LABEL, Sec->AddrSize, Name, 0);
472 /* Assign the value */
473 E->Expr = SectionExpr (Sec, Offs, 0);
475 /* Insert the export */
478 /* Return the new export */
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.
489 /* Get a pointer to the list with the symbols hash value */
490 Export* L = HashTab[Name & HASHTAB_MASK];
492 /* Search through the list in that slot */
493 if (L->Name == Name) {
506 int IsUnresolved (unsigned Name)
507 /* Check if this symbol is an unresolved export */
509 /* Find the export */
510 return IsUnresolvedExport (FindExport (Name));
515 int IsUnresolvedExport (const Export* E)
516 /* Return true if the given export is unresolved */
518 /* Check if it's unresolved */
519 return E != 0 && E->Expr == 0;
524 int IsConstExport (const Export* E)
525 /* Return true if the expression associated with this export is const */
528 /* External symbols cannot be const */
531 return IsConstExpr (E->Expr);
537 long GetExportVal (const Export* E)
538 /* Get the value of this export */
542 Internal ("`%s' is an undefined external", GetString (E->Name));
544 return GetExprVal (E->Expr);
549 static void CheckSymType (const Export* E)
550 /* Check the types for one export */
552 /* External with matching imports */
553 Import* Imp = E->ImpList;
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);
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'",
567 GetSourceFileName (E->Obj, E->Pos.Name),
571 GetSourceFileName (Imp->Obj, Imp->Pos.Name),
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'",
581 GetSourceFileName (Imp->Obj, Imp->Pos.Name),
592 static void CheckSymTypes (void)
593 /* Check for symbol tape mismatches */
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 */
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).
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;
623 "Unresolved external `%s' referenced in:\n",
624 GetString (E->Name));
626 const char* Name = GetSourceFileName (Imp->Obj, Imp->Pos.Name);
627 fprintf (stderr, " %s(%lu)\n", Name, Imp->Pos.Line);
636 static int CmpExpName (const void* K1, const void* K2)
637 /* Compare function for qsort */
639 return strcmp (GetString ((*(Export**)K1)->Name),
640 GetString ((*(Export**)K2)->Name));
645 static void CreateExportPool (void)
646 /* Create an array with pointer to all exports */
650 /* Allocate memory */
654 ExpPool = xmalloc (ExpCount * sizeof (Export*));
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];
660 CHECK (J < ExpCount);
666 /* Sort them by name */
667 qsort (ExpPool, ExpCount, sizeof (Export*), CmpExpName);
672 void CheckExports (ExpCheckFunc F, void* Data)
673 /* Check if there are any unresolved symbols. On unresolved symbols, F is
674 * called (see the comments on ExpCheckFunc in the data section).
677 /* Create an export pool */
680 /* Check for symbol type mismatches */
683 /* Check for unresolved externals (check here for special bin formats) */
685 /* Print all open imports */
686 PrintUnresolved (F, Data);
692 static char GetAddrSizeCode (unsigned char AddrSize)
693 /* Get a one char code for the address size */
696 case ADDR_SIZE_ZP: return 'Z';
697 case ADDR_SIZE_ABS: return 'A';
698 case ADDR_SIZE_FAR: return 'F';
699 case ADDR_SIZE_LONG: return 'L';
701 Internal ("Invalid address size: %u", AddrSize);
709 void PrintExportMap (FILE* F)
710 /* Print an export map to the given file */
715 /* Print all exports */
717 for (I = 0; I < ExpCount; ++I) {
718 const Export* E = ExpPool [I];
720 /* Print unreferenced symbols only if explictly requested */
721 if (VerboseMap || E->ImpCount > 0 || IS_EXP_CONDES (E->Type)) {
723 "%-25s %06lX %c%c%c%c ",
726 E->ImpCount? 'R' : ' ',
727 IS_EXP_LABEL (E->Type)? 'L' : 'E',
728 GetAddrSizeCode (E->AddrSize),
729 IS_EXP_CONDES (E->Type)? 'I' : ' ');
741 void PrintImportMap (FILE* F)
742 /* Print an import map to the given file */
747 /* Loop over all exports */
748 for (I = 0; I < ExpCount; ++I) {
751 const Export* Exp = ExpPool [I];
753 /* Print the symbol only if there are imports, or if a verbose map
756 if (VerboseMap || Exp->ImpCount > 0) {
758 /* Print the export */
761 GetString (Exp->Name),
762 GetObjFileName (Exp->Obj));
764 /* Print all imports for this symbol */
768 /* Print the import */
771 GetObjFileName (Imp->Obj),
772 GetSourceFileName (Imp->Obj, Imp->Pos.Name),
785 void PrintExportLabels (FILE* F)
786 /* Print the exports in a VICE label file */
790 /* Print all exports */
791 for (I = 0; I < ExpCount; ++I) {
792 const Export* E = ExpPool [I];
793 fprintf (F, "al %06lX .%s\n", GetExportVal (E), GetString (E->Name));
799 void MarkExport (Export* E)
800 /* Mark the export */
802 E->Flags |= EXP_USERMARK;
807 void UnmarkExport (Export* E)
808 /* Remove the mark from the export */
810 E->Flags &= ~EXP_USERMARK;
815 int ExportHasMark (Export* E)
816 /* Return true if the export has a mark */
818 return (E->Flags & EXP_USERMARK) != 0;
823 void CircularRefError (const Export* E)
824 /* Print an error about a circular reference using to define the given export */
826 Error ("Circular reference for symbol `%s', %s(%lu)",
828 GetSourceFileName (E->Obj, E->Pos.Name),