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 /*****************************************************************************/
56 /*****************************************************************************/
58 /*****************************************************************************/
63 #define HASHTAB_SIZE 4081
64 static Export* HashTab [HASHTAB_SIZE];
66 /* Import management variables */
67 static unsigned ImpCount = 0; /* Import count */
68 static unsigned ImpOpen = 0; /* Count of open imports */
70 /* Export management variables */
71 static unsigned ExpCount = 0; /* Export count */
72 static Export** ExpPool = 0; /* Exports array */
74 /* Defines for the flags in Export */
75 #define EXP_USERMARK 0x0001
79 /*****************************************************************************/
81 /*****************************************************************************/
85 static Export* NewExport (unsigned char Type, const char* Name, ObjData* Obj);
86 /* Create a new export and initialize it */
90 static Import* NewImport (unsigned char Type, ObjData* Obj)
91 /* Create a new import and initialize it */
94 Import* I = xmalloc (sizeof (Import));
96 /* Initialize the fields */
102 /* Return the new structure */
108 void InsertImport (Import* I)
109 /* Insert an import into the table */
114 /* As long as the import is not inserted, V.Name is valid */
115 const char* Name = I->V.Name;
117 /* Create a hash value for the given name */
118 HashVal = HashStr (Name) % HASHTAB_SIZE;
120 /* Search through the list in that slot and print matching duplicates */
121 if (HashTab [HashVal] == 0) {
122 /* The slot is empty, we need to insert a dummy export */
123 E = HashTab [HashVal] = NewExport (0, Name, 0);
126 E = HashTab [HashVal];
128 if (strcmp (E->Name, Name) == 0) {
129 /* We have an entry, L points to it */
133 /* End of list an entry not found, insert a dummy */
134 E->Next = NewExport (0, Name, 0);
135 E = E->Next; /* Point to dummy */
136 ++ExpCount; /* One export more */
144 /* Ok, E now points to a valid exports entry for the given import. Insert
145 * the import into the imports list and update the counters.
148 I->Next = E->ImpList;
151 ++ImpCount; /* Total import count */
153 /* This is a dummy export */
157 /* Now free the name since it's no longer needed */
163 Import* ReadImport (FILE* F, ObjData* Obj)
164 /* Read an import from a file and return it */
168 /* Read the import type and check it */
169 unsigned char Type = Read8 (F);
170 if (Type != IMP_ZP && Type != IMP_ABS) {
171 Error ("Unknown import type in module `%s': %02X", Obj->Name, Type);
174 /* Create a new import */
175 I = NewImport (Type, Obj);
178 I->V.Name = ReadMallocedStr (F);
180 /* Read the file position */
181 ReadFilePos (F, &I->Pos);
183 /* Return the new import */
189 /*****************************************************************************/
191 /*****************************************************************************/
195 static Export* NewExport (unsigned char Type, const char* Name, ObjData* Obj)
196 /* Create a new export and initialize it */
198 /* Get the length of the symbol name */
199 unsigned Len = strlen (Name);
201 /* Allocate memory */
202 Export* E = xmalloc (sizeof (Export) + Len);
204 /* Initialize the fields */
212 memcpy (E->Name, Name, Len);
213 E->Name [Len] = '\0';
215 /* Return the new entry */
221 void InsertExport (Export* E)
222 /* Insert an exported identifier and check if it's already in the list */
229 /* Create a hash value for the given name */
230 HashVal = HashStr (E->Name) % HASHTAB_SIZE;
232 /* Search through the list in that slot */
233 if (HashTab [HashVal] == 0) {
234 /* The slot is empty */
235 HashTab [HashVal] = E;
240 L = HashTab [HashVal];
242 if (strcmp (L->Name, E->Name) == 0) {
243 /* This may be an unresolved external */
246 /* This *is* an unresolved external */
248 E->ImpCount = L->ImpCount;
249 E->ImpList = L->ImpList;
253 HashTab [HashVal] = E;
255 ImpOpen -= E->ImpCount; /* Decrease open imports now */
257 /* We must run through the import list and change the
258 * export pointer now.
266 /* Duplicate entry, ignore it */
267 Warning ("Duplicate external identifier: `%s'", L->Name);
276 /* Insert export at end of queue */
284 Export* ReadExport (FILE* F, ObjData* O)
285 /* Read an export from a file */
297 /* Create a new export */
298 E = NewExport (Type, Name, O);
301 if (Type & EXP_EXPR) {
302 E->Expr = ReadExpr (F, O);
304 E->Expr = LiteralExpr (Read32 (F), O);
307 /* Last is the file position where the definition was done */
308 ReadFilePos (F, &E->Pos);
310 /* Return the new export */
316 Export* CreateConstExport (const char* Name, long Value)
317 /* Create an export for a literal date */
319 /* Create a new export */
320 Export* E = NewExport (EXP_ABS, Name, 0);
322 /* Assign the value */
323 E->Expr = LiteralExpr (Value, 0);
325 /* Insert the export */
328 /* Return the new export */
334 Export* CreateMemExport (const char* Name, Memory* Mem, unsigned long Offs)
335 /* Create an relative export for a memory area offset */
337 /* Create a new export */
338 Export* E = NewExport (EXP_ABS, Name, 0);
340 /* Assign the value */
341 E->Expr = MemExpr (Mem, Offs, 0);
343 /* Insert the export */
346 /* Return the new export */
352 static Export* FindExport (const char* Name)
353 /* Check for an identifier in the list. Return 0 if not found, otherwise
354 * return a pointer to the export.
357 /* Get a pointer to the list with the symbols hash value */
358 Export* L = HashTab [HashStr (Name) % HASHTAB_SIZE];
360 /* Search through the list in that slot */
361 if (strcmp (L->Name, Name) == 0) {
374 int IsUnresolved (const char* Name)
375 /* Check if this symbol is an unresolved export */
377 /* Find the export */
378 Export* E = FindExport (Name);
380 /* Check if it's unresolved */
381 return E != 0 && E->Expr == 0;
386 int IsConstExport (const Export* E)
387 /* Return true if the expression associated with this export is const */
390 /* External symbols cannot be const */
393 return IsConstExpr (E->Expr);
399 long GetExportVal (const Export* E)
400 /* Get the value of this export */
404 Internal ("`%s' is an undefined external", E->Name);
406 return GetExprVal (E->Expr);
411 static void CheckSymType (Export* E)
412 /* Check the types for one export */
414 /* External with matching imports */
415 Import* Imp = E->ImpList;
416 int ZP = (E->Type & EXP_ZP) != 0;
418 if (ZP != ((Imp->Type & IMP_ZP) != 0)) {
419 /* Export is ZP, import is abs or the other way round */
421 /* User defined export */
422 Warning ("Type mismatch for `%s', export in "
423 "%s(%lu), import in %s(%lu)",
424 E->Name, E->Obj->Files [Imp->Pos.Name],
425 E->Pos.Line, Imp->Obj->Files [Imp->Pos.Name],
428 /* Export created by the linker */
429 Warning ("Type mismatch for `%s', imported from %s(%lu)",
430 E->Name, Imp->Obj->Files [Imp->Pos.Name],
440 static void CheckSymTypes (void)
441 /* Check for symbol tape mismatches */
445 /* Print all open imports */
446 for (I = 0; I < ExpCount; ++I) {
447 Export* E = ExpPool [I];
448 if (E->Expr != 0 && E->ImpCount > 0) {
449 /* External with matching imports */
457 static void PrintUnresolved (ExpCheckFunc F, void* Data)
458 /* Print a list of unresolved symbols. On unresolved symbols, F is
459 * called (see the comments on ExpCheckFunc in the data section).
464 /* Print all open imports */
465 for (I = 0; I < ExpCount; ++I) {
466 Export* E = ExpPool [I];
467 if (E->Expr == 0 && E->ImpCount > 0 && F (E->Name, Data) == 0) {
468 /* Unresolved external */
469 Import* Imp = E->ImpList;
471 "Unresolved external `%s' referenced in:\n",
474 const char* Name = Imp->Obj->Files [Imp->Pos.Name];
475 fprintf (stderr, " %s(%lu)\n", Name, Imp->Pos.Line);
484 static int CmpExpName (const void* K1, const void* K2)
485 /* Compare function for qsort */
487 return strcmp ((*(Export**)K1)->Name, (*(Export**)K2)->Name);
492 static void CreateExportPool (void)
493 /* Create an array with pointer to all exports */
497 /* Allocate memory */
501 ExpPool = xmalloc (ExpCount * sizeof (Export*));
503 /* Walk through the list and insert the exports */
504 for (I = 0, J = 0; I < sizeof (HashTab) / sizeof (HashTab [0]); ++I) {
505 Export* E = HashTab [I];
507 CHECK (J < ExpCount);
513 /* Sort them by name */
514 qsort (ExpPool, ExpCount, sizeof (Export*), CmpExpName);
519 void CheckExports (ExpCheckFunc F, void* Data)
520 /* Check if there are any unresolved symbols. On unresolved symbols, F is
521 * called (see the comments on ExpCheckFunc in the data section).
524 /* Create an export pool */
527 /* Check for symbol type mismatches */
530 /* Check for unresolved externals (check here for special bin formats) */
532 /* Print all open imports */
533 PrintUnresolved (F, Data);
539 void PrintExportMap (FILE* F)
540 /* Print an export map to the given file */
545 /* Print all exports */
547 for (I = 0; I < ExpCount; ++I) {
548 Export* E = ExpPool [I];
550 /* Print unreferenced symbols only if explictly requested */
551 if (VerboseMap || E->ImpCount > 0) {
556 E->ImpCount? 'R' : ' ',
557 (E->Type & EXP_ZP)? 'Z' : ' ');
569 void PrintImportMap (FILE* F)
570 /* Print an import map to the given file */
575 /* Loop over all exports */
576 for (I = 0; I < ExpCount; ++I) {
579 Export* Exp = ExpPool [I];
581 /* Print the symbol only if there are imports, or if a verbose map
584 if (VerboseMap || Exp->ImpCount > 0) {
586 /* Get the name of the object file that exports the symbol.
587 * Beware: There may be no object file if the symbol is a linker
590 const char* ObjName = (Exp->Obj != 0)? Exp->Obj->Name : "linker generated";
592 /* Print the export */
598 /* Print all imports for this symbol */
602 /* Print the import */
606 Imp->Obj->Files [Imp->Pos.Name],
619 void PrintExportLabels (FILE* F)
620 /* Print the exports in a VICE label file */
624 /* Print all exports */
625 for (I = 0; I < ExpCount; ++I) {
626 Export* E = ExpPool [I];
627 fprintf (F, "al %06lX .%s\n", GetExportVal (E), E->Name);
633 void MarkExport (Export* E)
634 /* Mark the export */
636 E->Flags |= EXP_USERMARK;
641 void UnmarkExport (Export* E)
642 /* Remove the mark from the export */
644 E->Flags &= ~EXP_USERMARK;
649 int ExportHasMark (Export* E)
650 /* Return true if the export has a mark */
652 return (E->Flags & EXP_USERMARK) != 0;
657 void CircularRefError (const Export* E)
658 /* Print an error about a circular reference using to define the given export */
660 Error ("Circular reference for symbol `%s', %s(%lu)",
661 E->Name, E->Obj->Files [E->Pos.Name], E->Pos.Line);