1 /*****************************************************************************/
5 /* Exports handing for the ld65 linker */
9 /* (C) 1998 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
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 /*****************************************************************************/
40 #include "../common/symdefs.h"
41 #include "../common/hashstr.h"
42 #include "../common/xmalloc.h"
53 /*****************************************************************************/
55 /*****************************************************************************/
60 #define HASHTAB_SIZE 4081
61 static Export* HashTab [HASHTAB_SIZE];
63 /* Import management variables */
64 static unsigned ImpCount = 0; /* Import count */
65 static unsigned ImpOpen = 0; /* Count of open imports */
67 /* Export management variables */
68 static unsigned ExpCount = 0; /* Export count */
69 static Export** ExpPool = 0; /* Exports array */
71 /* Defines for the flags in Export */
72 #define EXP_USERMARK 0x0001
76 /*****************************************************************************/
78 /*****************************************************************************/
82 static Export* NewExport (unsigned char Type, const char* Name, ObjData* Obj);
83 /* Create a new export and initialize it */
87 static Import* NewImport (unsigned char Type, ObjData* Obj)
88 /* Create a new import and initialize it */
91 Import* I = xmalloc (sizeof (Import));
93 /* Initialize the fields */
99 /* Return the new structure */
105 void InsertImport (Import* I)
106 /* Insert an import into the table */
111 /* As long as the import is not inserted, V.Name is valid */
112 const char* Name = I->V.Name;
114 /* Create a hash value for the given name */
115 HashVal = HashStr (Name) % HASHTAB_SIZE;
117 /* Search through the list in that slot and print matching duplicates */
118 if (HashTab [HashVal] == 0) {
119 /* The slot is empty, we need to insert a dummy export */
120 E = HashTab [HashVal] = NewExport (0, Name, 0);
123 E = HashTab [HashVal];
125 if (strcmp (E->Name, Name) == 0) {
126 /* We have an entry, L points to it */
130 /* End of list an entry not found, insert a dummy */
131 E->Next = NewExport (0, Name, 0);
132 E = E->Next; /* Point to dummy */
133 ++ExpCount; /* One export more */
141 /* Ok, E now points to a valid exports entry for the given import. Insert
142 * the import into the imports list and update the counters.
145 I->Next = E->ImpList;
148 ++ImpCount; /* Total import count */
150 /* This is a dummy export */
154 /* Now free the name since it's no longer needed */
160 Import* ReadImport (FILE* F, ObjData* Obj)
161 /* Read an import from a file and return it */
165 /* Read the import type and check it */
166 unsigned char Type = Read8 (F);
167 if (Type != IMP_ZP && Type != IMP_ABS) {
168 Error ("Unknown import type in module `%s': %02X", Obj->Name, Type);
171 /* Create a new import */
172 I = NewImport (Type, Obj);
175 I->V.Name = ReadMallocedStr (F);
177 /* Read the file position */
178 ReadFilePos (F, &I->Pos);
180 /* Return the new import */
186 /*****************************************************************************/
188 /*****************************************************************************/
192 static Export* NewExport (unsigned char Type, const char* Name, ObjData* Obj)
193 /* Create a new export and initialize it */
195 /* Get the length of the symbol name */
196 unsigned Len = strlen (Name);
198 /* Allocate memory */
199 Export* E = xmalloc (sizeof (Export) + Len);
201 /* Initialize the fields */
209 memcpy (E->Name, Name, Len);
210 E->Name [Len] = '\0';
212 /* Return the new entry */
218 void InsertExport (Export* E)
219 /* Insert an exported identifier and check if it's already in the list */
226 /* Create a hash value for the given name */
227 HashVal = HashStr (E->Name) % HASHTAB_SIZE;
229 /* Search through the list in that slot */
230 if (HashTab [HashVal] == 0) {
231 /* The slot is empty */
232 HashTab [HashVal] = E;
237 L = HashTab [HashVal];
239 if (strcmp (L->Name, E->Name) == 0) {
240 /* This may be an unresolved external */
243 /* This *is* an unresolved external */
245 E->ImpCount = L->ImpCount;
246 E->ImpList = L->ImpList;
250 HashTab [HashVal] = E;
252 ImpOpen -= E->ImpCount; /* Decrease open imports now */
254 /* We must run through the import list and change the
255 * export pointer now.
263 /* Duplicate entry, ignore it */
264 Warning ("Duplicate external identifier: `%s'", L->Name);
273 /* Insert export at end of queue */
281 Export* ReadExport (FILE* F, ObjData* O)
282 /* Read an export from a file */
294 /* Create a new export */
295 E = NewExport (Type, Name, O);
298 if (Type & EXP_EXPR) {
299 E->Expr = ReadExpr (F, O);
301 E->Expr = LiteralExpr (Read32 (F), O);
304 /* Last is the file position where the definition was done */
305 ReadFilePos (F, &E->Pos);
307 /* Return the new export */
313 Export* CreateConstExport (const char* Name, long Value)
314 /* Create an export for a literal date */
316 /* Create a new export */
317 Export* E = NewExport (EXP_ABS, Name, 0);
319 /* Assign the value */
320 E->Expr = LiteralExpr (Value, 0);
322 /* Insert the export */
325 /* Return the new export */
331 Export* CreateMemExport (const char* Name, Memory* Mem, unsigned long Offs)
332 /* Create an relative export for a memory area offset */
334 /* Create a new export */
335 Export* E = NewExport (EXP_ABS, Name, 0);
337 /* Assign the value */
338 E->Expr = MemExpr (Mem, Offs, 0);
340 /* Insert the export */
343 /* Return the new export */
349 static Export* FindExport (const char* Name)
350 /* Check for an identifier in the list. Return 0 if not found, otherwise
351 * return a pointer to the export.
354 /* Get a pointer to the list with the symbols hash value */
355 Export* L = HashTab [HashStr (Name) % HASHTAB_SIZE];
357 /* Search through the list in that slot */
358 if (strcmp (L->Name, Name) == 0) {
371 int IsUnresolved (const char* Name)
372 /* Check if this symbol is an unresolved export */
374 /* Find the export */
375 Export* E = FindExport (Name);
377 /* Check if it's unresolved */
378 return E != 0 && E->Expr == 0;
383 int IsConstExport (const Export* E)
384 /* Return true if the expression associated with this export is const */
387 /* External symbols cannot be const */
390 return IsConstExpr (E->Expr);
396 long GetExportVal (const Export* E)
397 /* Get the value of this export */
401 Internal ("`%s' is an undefined external", E->Name);
403 return GetExprVal (E->Expr);
408 static void CheckSymType (Export* E)
409 /* Check the types for one export */
411 /* External with matching imports */
412 Import* Imp = E->ImpList;
413 int ZP = (E->Type & EXP_ZP) != 0;
415 if (ZP != ((Imp->Type & IMP_ZP) != 0)) {
416 /* Export is ZP, import is abs or the other way round */
418 /* User defined export */
419 Warning ("Type mismatch for `%s', export in "
420 "%s(%lu), import in %s(%lu)",
421 E->Name, E->Obj->Files [Imp->Pos.Name],
422 E->Pos.Line, Imp->Obj->Files [Imp->Pos.Name],
425 /* Export created by the linker */
426 Warning ("Type mismatch for `%s', imported from %s(%lu)",
427 E->Name, Imp->Obj->Files [Imp->Pos.Name],
437 static void CheckSymTypes (void)
438 /* Check for symbol tape mismatches */
442 /* Print all open imports */
443 for (I = 0; I < ExpCount; ++I) {
444 Export* E = ExpPool [I];
445 if (E->Expr != 0 && E->ImpCount > 0) {
446 /* External with matching imports */
454 static void PrintUnresolved (ExpCheckFunc F, void* Data)
455 /* Print a list of unresolved symbols. On unresolved symbols, F is
456 * called (see the comments on ExpCheckFunc in the data section).
461 /* Print all open imports */
462 for (I = 0; I < ExpCount; ++I) {
463 Export* E = ExpPool [I];
464 if (E->Expr == 0 && E->ImpCount > 0 && F (E->Name, Data) == 0) {
465 /* Unresolved external */
466 Import* Imp = E->ImpList;
468 "Unresolved external `%s' referenced in:\n",
471 const char* Name = Imp->Obj->Files [Imp->Pos.Name];
472 fprintf (stderr, " %s(%lu)\n", Name, Imp->Pos.Line);
481 static int CmpExpName (const void* K1, const void* K2)
482 /* Compare function for qsort */
484 return strcmp ((*(Export**)K1)->Name, (*(Export**)K2)->Name);
489 static void CreateExportPool (void)
490 /* Create an array with pointer to all exports */
494 /* Allocate memory */
498 ExpPool = xmalloc (ExpCount * sizeof (Export*));
500 /* Walk through the list and insert the exports */
501 for (I = 0, J = 0; I < sizeof (HashTab) / sizeof (HashTab [0]); ++I) {
502 Export* E = HashTab [I];
504 CHECK (J < ExpCount);
510 /* Sort them by name */
511 qsort (ExpPool, ExpCount, sizeof (Export*), CmpExpName);
516 void CheckExports (ExpCheckFunc F, void* Data)
517 /* Check if there are any unresolved symbols. On unresolved symbols, F is
518 * called (see the comments on ExpCheckFunc in the data section).
521 /* Create an export pool */
524 /* Check for symbol type mismatches */
527 /* Check for unresolved externals (check here for special bin formats) */
529 /* Print all open imports */
530 PrintUnresolved (F, Data);
536 void PrintExportMap (FILE* F)
537 /* Print an export map to the given file */
542 /* Print all exports */
544 for (I = 0; I < ExpCount; ++I) {
545 Export* E = ExpPool [I];
547 /* Print unreferenced symbols only if explictly requested */
548 if (VerboseMap || E->ImpCount > 0) {
553 E->ImpCount? 'R' : ' ',
554 (E->Type & EXP_ZP)? 'Z' : ' ');
566 void PrintImportMap (FILE* F)
567 /* Print an import map to the given file */
572 /* Loop over all exports */
573 for (I = 0; I < ExpCount; ++I) {
576 Export* Exp = ExpPool [I];
578 /* Print the symbol only if there are imports, or if a verbose map
581 if (VerboseMap || Exp->ImpCount > 0) {
583 /* Get the name of the object file that exports the symbol.
584 * Beware: There may be no object file if the symbol is a linker
587 const char* ObjName = (Exp->Obj != 0)? Exp->Obj->Name : "linker generated";
589 /* Print the export */
595 /* Print all imports for this symbol */
599 /* Print the import */
603 Imp->Obj->Files [Imp->Pos.Name],
616 void PrintExportLabels (FILE* F)
617 /* Print the exports in a VICE label file */
621 /* Print all exports */
622 for (I = 0; I < ExpCount; ++I) {
623 Export* E = ExpPool [I];
624 fprintf (F, "al %06lX .%s\n", GetExportVal (E), E->Name);
630 void MarkExport (Export* E)
631 /* Mark the export */
633 E->Flags |= EXP_USERMARK;
638 void UnmarkExport (Export* E)
639 /* Remove the mark from the export */
641 E->Flags &= ~EXP_USERMARK;
646 int ExportHasMark (Export* E)
647 /* Return true if the export has a mark */
649 return (E->Flags & EXP_USERMARK) != 0;
654 void CircularRefError (const Export* E)
655 /* Print an error about a circular reference using to define the given export */
657 Error ("Circular reference for symbol `%s', %s(%lu)",
658 E->Name, E->Obj->Files [E->Pos.Name], E->Pos.Line);