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 */
558 /* User defined export */
559 Warning ("Address size mismatch for `%s', export in "
560 "%s(%lu), import in %s(%lu)",
562 GetSourceFileName (E->Obj, E->Pos.Name),
564 GetSourceFileName (Imp->Obj, Imp->Pos.Name),
567 /* Export created by the linker */
568 Warning ("Address size mismatch for `%s', imported from %s(%lu)",
570 GetSourceFileName (Imp->Obj, Imp->Pos.Name),
580 static void CheckSymTypes (void)
581 /* Check for symbol tape mismatches */
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 */
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).
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;
611 "Unresolved external `%s' referenced in:\n",
612 GetString (E->Name));
614 const char* Name = GetSourceFileName (Imp->Obj, Imp->Pos.Name);
615 fprintf (stderr, " %s(%lu)\n", Name, Imp->Pos.Line);
624 static int CmpExpName (const void* K1, const void* K2)
625 /* Compare function for qsort */
627 return strcmp (GetString ((*(Export**)K1)->Name),
628 GetString ((*(Export**)K2)->Name));
633 static void CreateExportPool (void)
634 /* Create an array with pointer to all exports */
638 /* Allocate memory */
642 ExpPool = xmalloc (ExpCount * sizeof (Export*));
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];
648 CHECK (J < ExpCount);
654 /* Sort them by name */
655 qsort (ExpPool, ExpCount, sizeof (Export*), CmpExpName);
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).
665 /* Create an export pool */
668 /* Check for symbol type mismatches */
671 /* Check for unresolved externals (check here for special bin formats) */
673 /* Print all open imports */
674 PrintUnresolved (F, Data);
680 static char GetAddrSizeCode (unsigned char AddrSize)
681 /* Get a one char code for the address size */
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';
689 Internal ("Invalid address size: %u", AddrSize);
697 void PrintExportMap (FILE* F)
698 /* Print an export map to the given file */
703 /* Print all exports */
705 for (I = 0; I < ExpCount; ++I) {
706 const Export* E = ExpPool [I];
708 /* Print unreferenced symbols only if explictly requested */
709 if (VerboseMap || E->ImpCount > 0 || IS_EXP_CONDES (E->Type)) {
711 "%-25s %06lX %c%c%c%c ",
714 E->ImpCount? 'R' : ' ',
715 IS_EXP_LABEL (E->Type)? 'L' : 'E',
716 GetAddrSizeCode (E->AddrSize),
717 IS_EXP_CONDES (E->Type)? 'I' : ' ');
729 void PrintImportMap (FILE* F)
730 /* Print an import map to the given file */
735 /* Loop over all exports */
736 for (I = 0; I < ExpCount; ++I) {
739 const Export* Exp = ExpPool [I];
741 /* Print the symbol only if there are imports, or if a verbose map
744 if (VerboseMap || Exp->ImpCount > 0) {
746 /* Print the export */
749 GetString (Exp->Name),
750 GetObjFileName (Exp->Obj));
752 /* Print all imports for this symbol */
756 /* Print the import */
759 GetObjFileName (Imp->Obj),
760 GetSourceFileName (Imp->Obj, Imp->Pos.Name),
773 void PrintExportLabels (FILE* F)
774 /* Print the exports in a VICE label file */
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));
787 void MarkExport (Export* E)
788 /* Mark the export */
790 E->Flags |= EXP_USERMARK;
795 void UnmarkExport (Export* E)
796 /* Remove the mark from the export */
798 E->Flags &= ~EXP_USERMARK;
803 int ExportHasMark (Export* E)
804 /* Return true if the export has a mark */
806 return (E->Flags & EXP_USERMARK) != 0;
811 void CircularRefError (const Export* E)
812 /* Print an error about a circular reference using to define the given export */
814 Error ("Circular reference for symbol `%s', %s(%lu)",
816 GetSourceFileName (E->Obj, E->Pos.Name),