1 /*****************************************************************************/
5 /* Exports handling 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 /*****************************************************************************/
58 /*****************************************************************************/
60 /*****************************************************************************/
65 #define HASHTAB_SIZE 4081
66 static Export* HashTab [HASHTAB_SIZE];
68 /* Import management variables */
69 static unsigned ImpCount = 0; /* Import count */
70 static unsigned ImpOpen = 0; /* Count of open imports */
72 /* Export management variables */
73 static unsigned ExpCount = 0; /* Export count */
74 static Export** ExpPool = 0; /* Exports array */
76 /* Defines for the flags in Export */
77 #define EXP_USERMARK 0x0001
81 /*****************************************************************************/
83 /*****************************************************************************/
87 static Export* NewExport (unsigned char Type, const char* Name, ObjData* Obj);
88 /* Create a new export and initialize it */
92 static Import* NewImport (unsigned char Type, ObjData* Obj)
93 /* Create a new import and initialize it */
96 Import* I = xmalloc (sizeof (Import));
98 /* Initialize the fields */
104 /* Return the new structure */
110 void InsertImport (Import* I)
111 /* Insert an import into the table */
116 /* As long as the import is not inserted, V.Name is valid */
117 const char* Name = I->V.Name;
119 /* Create a hash value for the given name */
120 HashVal = HashStr (Name) % HASHTAB_SIZE;
122 /* Search through the list in that slot and print matching duplicates */
123 if (HashTab [HashVal] == 0) {
124 /* The slot is empty, we need to insert a dummy export */
125 E = HashTab [HashVal] = NewExport (0, Name, 0);
128 E = HashTab [HashVal];
130 if (strcmp (E->Name, Name) == 0) {
131 /* We have an entry, L points to it */
135 /* End of list an entry not found, insert a dummy */
136 E->Next = NewExport (0, Name, 0);
137 E = E->Next; /* Point to dummy */
138 ++ExpCount; /* One export more */
146 /* Ok, E now points to a valid exports entry for the given import. Insert
147 * the import into the imports list and update the counters.
150 I->Next = E->ImpList;
153 ++ImpCount; /* Total import count */
155 /* This is a dummy export */
159 /* Now free the name since it's no longer needed */
165 Import* ReadImport (FILE* F, ObjData* Obj)
166 /* Read an import from a file and return it */
170 /* Read the import type and check it */
171 unsigned char Type = Read8 (F);
172 if (Type != IMP_ZP && Type != IMP_ABS) {
173 Error ("Unknown import type in module `%s': %02X", Obj->Name, Type);
176 /* Create a new import */
177 I = NewImport (Type, Obj);
180 I->V.Name = ReadStr (F);
182 /* Read the file position */
183 ReadFilePos (F, &I->Pos);
185 /* Return the new import */
191 /*****************************************************************************/
193 /*****************************************************************************/
197 static Export* NewExport (unsigned char Type, const char* Name, ObjData* Obj)
198 /* Create a new export and initialize it */
200 /* Allocate memory */
201 Export* E = xmalloc (sizeof (Export));
203 /* Initialize the fields */
212 E->Name = xstrdup (Name);
214 /* Name will get added later */
218 /* Return the new entry */
224 void InsertExport (Export* E)
225 /* Insert an exported identifier and check if it's already in the list */
232 /* If this is an initializer, insert it into the initializer list */
233 if (IS_EXP_INIT (E->Type)) {
237 /* Create a hash value for the given name */
238 HashVal = HashStr (E->Name) % HASHTAB_SIZE;
240 /* Search through the list in that slot */
241 if (HashTab [HashVal] == 0) {
242 /* The slot is empty */
243 HashTab [HashVal] = E;
248 L = HashTab [HashVal];
250 if (strcmp (L->Name, E->Name) == 0) {
251 /* This may be an unresolved external */
254 /* This *is* an unresolved external */
256 E->ImpCount = L->ImpCount;
257 E->ImpList = L->ImpList;
261 HashTab [HashVal] = E;
263 ImpOpen -= E->ImpCount; /* Decrease open imports now */
265 /* We must run through the import list and change the
266 * export pointer now.
274 /* Duplicate entry, ignore it */
275 Warning ("Duplicate external identifier: `%s'", L->Name);
284 /* Insert export at end of queue */
292 Export* ReadExport (FILE* F, ObjData* O)
293 /* Read an export from a file */
301 /* Create a new export without a name */
302 E = NewExport (Type, 0, O);
305 E->Name = ReadStr (F);
308 if (IS_EXP_EXPR (Type)) {
309 E->Expr = ReadExpr (F, O);
311 E->Expr = LiteralExpr (Read32 (F), O);
314 /* Last is the file position where the definition was done */
315 ReadFilePos (F, &E->Pos);
317 /* Return the new export */
323 Export* CreateConstExport (const char* Name, long Value)
324 /* Create an export for a literal date */
326 /* Create a new export */
327 Export* E = NewExport (EXP_ABS, Name, 0);
329 /* Assign the value */
330 E->Expr = LiteralExpr (Value, 0);
332 /* Insert the export */
335 /* Return the new export */
341 Export* CreateMemExport (const char* Name, Memory* Mem, unsigned long Offs)
342 /* Create an relative export for a memory area offset */
344 /* Create a new export */
345 Export* E = NewExport (EXP_ABS, Name, 0);
347 /* Assign the value */
348 E->Expr = MemExpr (Mem, Offs, 0);
350 /* Insert the export */
353 /* Return the new export */
359 static Export* FindExport (const char* Name)
360 /* Check for an identifier in the list. Return 0 if not found, otherwise
361 * return a pointer to the export.
364 /* Get a pointer to the list with the symbols hash value */
365 Export* L = HashTab [HashStr (Name) % HASHTAB_SIZE];
367 /* Search through the list in that slot */
368 if (strcmp (L->Name, Name) == 0) {
381 int IsUnresolved (const char* Name)
382 /* Check if this symbol is an unresolved export */
384 /* Find the export */
385 Export* E = FindExport (Name);
387 /* Check if it's unresolved */
388 return E != 0 && E->Expr == 0;
393 int IsConstExport (const Export* E)
394 /* Return true if the expression associated with this export is const */
397 /* External symbols cannot be const */
400 return IsConstExpr (E->Expr);
406 long GetExportVal (const Export* E)
407 /* Get the value of this export */
411 Internal ("`%s' is an undefined external", E->Name);
413 return GetExprVal (E->Expr);
418 static void CheckSymType (const Export* E)
419 /* Check the types for one export */
421 /* External with matching imports */
422 Import* Imp = E->ImpList;
423 int ZP = IS_EXP_ZP (E->Type);
425 if (ZP != IS_IMP_ZP (Imp->Type)) {
426 /* Export is ZP, import is abs or the other way round */
428 /* User defined export */
429 Warning ("Type mismatch for `%s', export in "
430 "%s(%lu), import in %s(%lu)",
431 E->Name, E->Obj->Files [Imp->Pos.Name],
432 E->Pos.Line, Imp->Obj->Files [Imp->Pos.Name],
435 /* Export created by the linker */
436 Warning ("Type mismatch for `%s', imported from %s(%lu)",
437 E->Name, Imp->Obj->Files [Imp->Pos.Name],
447 static void CheckSymTypes (void)
448 /* Check for symbol tape mismatches */
452 /* Print all open imports */
453 for (I = 0; I < ExpCount; ++I) {
454 const Export* E = ExpPool [I];
455 if (E->Expr != 0 && E->ImpCount > 0) {
456 /* External with matching imports */
464 static void PrintUnresolved (ExpCheckFunc F, void* Data)
465 /* Print a list of unresolved symbols. On unresolved symbols, F is
466 * called (see the comments on ExpCheckFunc in the data section).
471 /* Print all open imports */
472 for (I = 0; I < ExpCount; ++I) {
473 Export* E = ExpPool [I];
474 if (E->Expr == 0 && E->ImpCount > 0 && F (E->Name, Data) == 0) {
475 /* Unresolved external */
476 Import* Imp = E->ImpList;
478 "Unresolved external `%s' referenced in:\n",
481 const char* Name = Imp->Obj->Files [Imp->Pos.Name];
482 fprintf (stderr, " %s(%lu)\n", Name, Imp->Pos.Line);
491 static int CmpExpName (const void* K1, const void* K2)
492 /* Compare function for qsort */
494 return strcmp ((*(Export**)K1)->Name, (*(Export**)K2)->Name);
499 static void CreateExportPool (void)
500 /* Create an array with pointer to all exports */
504 /* Allocate memory */
508 ExpPool = xmalloc (ExpCount * sizeof (Export*));
510 /* Walk through the list and insert the exports */
511 for (I = 0, J = 0; I < sizeof (HashTab) / sizeof (HashTab [0]); ++I) {
512 Export* E = HashTab [I];
514 CHECK (J < ExpCount);
520 /* Sort them by name */
521 qsort (ExpPool, ExpCount, sizeof (Export*), CmpExpName);
526 void CheckExports (ExpCheckFunc F, void* Data)
527 /* Check if there are any unresolved symbols. On unresolved symbols, F is
528 * called (see the comments on ExpCheckFunc in the data section).
531 /* Create an export pool */
534 /* Check for symbol type mismatches */
537 /* Check for unresolved externals (check here for special bin formats) */
539 /* Print all open imports */
540 PrintUnresolved (F, Data);
546 void PrintExportMap (FILE* F)
547 /* Print an export map to the given file */
552 /* Print all exports */
554 for (I = 0; I < ExpCount; ++I) {
555 const Export* E = ExpPool [I];
557 /* Print unreferenced symbols only if explictly requested */
558 if (VerboseMap || E->ImpCount > 0) {
560 "%-25s %06lX %c%c%c ",
563 E->ImpCount? 'R' : ' ',
564 IS_EXP_ZP (E->Type)? 'Z' : ' ',
565 IS_EXP_INIT (E->Type)? 'I' : ' ');
577 void PrintImportMap (FILE* F)
578 /* Print an import map to the given file */
583 /* Loop over all exports */
584 for (I = 0; I < ExpCount; ++I) {
587 const Export* Exp = ExpPool [I];
589 /* Print the symbol only if there are imports, or if a verbose map
592 if (VerboseMap || Exp->ImpCount > 0) {
594 /* Get the name of the object file that exports the symbol.
595 * Beware: There may be no object file if the symbol is a linker
598 const char* ObjName = (Exp->Obj != 0)? Exp->Obj->Name : "linker generated";
600 /* Print the export */
606 /* Print all imports for this symbol */
610 /* Print the import */
614 Imp->Obj->Files [Imp->Pos.Name],
627 void PrintExportLabels (FILE* F)
628 /* Print the exports in a VICE label file */
632 /* Print all exports */
633 for (I = 0; I < ExpCount; ++I) {
634 const Export* E = ExpPool [I];
635 fprintf (F, "al %06lX .%s\n", GetExportVal (E), E->Name);
641 void MarkExport (Export* E)
642 /* Mark the export */
644 E->Flags |= EXP_USERMARK;
649 void UnmarkExport (Export* E)
650 /* Remove the mark from the export */
652 E->Flags &= ~EXP_USERMARK;
657 int ExportHasMark (Export* E)
658 /* Return true if the export has a mark */
660 return (E->Flags & EXP_USERMARK) != 0;
665 void CircularRefError (const Export* E)
666 /* Print an error about a circular reference using to define the given export */
668 Error ("Circular reference for symbol `%s', %s(%lu)",
669 E->Name, E->Obj->Files [E->Pos.Name], E->Pos.Line);