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 /*****************************************************************************/
57 /*****************************************************************************/
59 /*****************************************************************************/
64 #define HASHTAB_SIZE 4081
65 static Export* HashTab [HASHTAB_SIZE];
67 /* Import management variables */
68 static unsigned ImpCount = 0; /* Import count */
69 static unsigned ImpOpen = 0; /* Count of open imports */
71 /* Export management variables */
72 static unsigned ExpCount = 0; /* Export count */
73 static Export** ExpPool = 0; /* Exports array */
75 /* Defines for the flags in Export */
76 #define EXP_USERMARK 0x0001
78 /* List of all exports that are also initializers */
79 static Collection Initializers = STATIC_COLLECTION_INITIALIZER;
83 /*****************************************************************************/
85 /*****************************************************************************/
89 static Export* NewExport (unsigned char Type, const char* Name, ObjData* Obj);
90 /* Create a new export and initialize it */
94 static Import* NewImport (unsigned char Type, ObjData* Obj)
95 /* Create a new import and initialize it */
98 Import* I = xmalloc (sizeof (Import));
100 /* Initialize the fields */
106 /* Return the new structure */
112 void InsertImport (Import* I)
113 /* Insert an import into the table */
118 /* As long as the import is not inserted, V.Name is valid */
119 const char* Name = I->V.Name;
121 /* Create a hash value for the given name */
122 HashVal = HashStr (Name) % HASHTAB_SIZE;
124 /* Search through the list in that slot and print matching duplicates */
125 if (HashTab [HashVal] == 0) {
126 /* The slot is empty, we need to insert a dummy export */
127 E = HashTab [HashVal] = NewExport (0, Name, 0);
130 E = HashTab [HashVal];
132 if (strcmp (E->Name, Name) == 0) {
133 /* We have an entry, L points to it */
137 /* End of list an entry not found, insert a dummy */
138 E->Next = NewExport (0, Name, 0);
139 E = E->Next; /* Point to dummy */
140 ++ExpCount; /* One export more */
148 /* Ok, E now points to a valid exports entry for the given import. Insert
149 * the import into the imports list and update the counters.
152 I->Next = E->ImpList;
155 ++ImpCount; /* Total import count */
157 /* This is a dummy export */
161 /* Now free the name since it's no longer needed */
167 Import* ReadImport (FILE* F, ObjData* Obj)
168 /* Read an import from a file and return it */
172 /* Read the import type and check it */
173 unsigned char Type = Read8 (F);
174 if (Type != IMP_ZP && Type != IMP_ABS) {
175 Error ("Unknown import type in module `%s': %02X", Obj->Name, Type);
178 /* Create a new import */
179 I = NewImport (Type, Obj);
182 I->V.Name = ReadStr (F);
184 /* Read the file position */
185 ReadFilePos (F, &I->Pos);
187 /* Return the new import */
193 /*****************************************************************************/
195 /*****************************************************************************/
199 static Export* NewExport (unsigned char Type, const char* Name, ObjData* Obj)
200 /* Create a new export and initialize it */
202 /* Allocate memory */
203 Export* E = xmalloc (sizeof (Export));
205 /* Initialize the fields */
214 E->Name = xstrdup (Name);
216 /* Name will get added later */
220 /* Return the new entry */
226 void InsertExport (Export* E)
227 /* Insert an exported identifier and check if it's already in the list */
234 /* If this is an initializer, insert it into the initializer list */
235 if (IS_EXP_INIT (E->Type)) {
236 CollAppend (&Initializers, E);
239 /* Create a hash value for the given name */
240 HashVal = HashStr (E->Name) % HASHTAB_SIZE;
242 /* Search through the list in that slot */
243 if (HashTab [HashVal] == 0) {
244 /* The slot is empty */
245 HashTab [HashVal] = E;
250 L = HashTab [HashVal];
252 if (strcmp (L->Name, E->Name) == 0) {
253 /* This may be an unresolved external */
256 /* This *is* an unresolved external */
258 E->ImpCount = L->ImpCount;
259 E->ImpList = L->ImpList;
263 HashTab [HashVal] = E;
265 ImpOpen -= E->ImpCount; /* Decrease open imports now */
267 /* We must run through the import list and change the
268 * export pointer now.
276 /* Duplicate entry, ignore it */
277 Warning ("Duplicate external identifier: `%s'", L->Name);
286 /* Insert export at end of queue */
294 Export* ReadExport (FILE* F, ObjData* O)
295 /* Read an export from a file */
303 /* Create a new export without a name */
304 E = NewExport (Type, 0, O);
307 E->Name = ReadStr (F);
310 if (IS_EXP_EXPR (Type)) {
311 E->Expr = ReadExpr (F, O);
313 E->Expr = LiteralExpr (Read32 (F), O);
316 /* Last is the file position where the definition was done */
317 ReadFilePos (F, &E->Pos);
319 /* Return the new export */
325 Export* CreateConstExport (const char* Name, long Value)
326 /* Create an export for a literal date */
328 /* Create a new export */
329 Export* E = NewExport (EXP_ABS, Name, 0);
331 /* Assign the value */
332 E->Expr = LiteralExpr (Value, 0);
334 /* Insert the export */
337 /* Return the new export */
343 Export* CreateMemExport (const char* Name, Memory* Mem, unsigned long Offs)
344 /* Create an relative export for a memory area offset */
346 /* Create a new export */
347 Export* E = NewExport (EXP_ABS, Name, 0);
349 /* Assign the value */
350 E->Expr = MemExpr (Mem, Offs, 0);
352 /* Insert the export */
355 /* Return the new export */
361 static Export* FindExport (const char* Name)
362 /* Check for an identifier in the list. Return 0 if not found, otherwise
363 * return a pointer to the export.
366 /* Get a pointer to the list with the symbols hash value */
367 Export* L = HashTab [HashStr (Name) % HASHTAB_SIZE];
369 /* Search through the list in that slot */
370 if (strcmp (L->Name, Name) == 0) {
383 int IsUnresolved (const char* Name)
384 /* Check if this symbol is an unresolved export */
386 /* Find the export */
387 Export* E = FindExport (Name);
389 /* Check if it's unresolved */
390 return E != 0 && E->Expr == 0;
395 int IsConstExport (const Export* E)
396 /* Return true if the expression associated with this export is const */
399 /* External symbols cannot be const */
402 return IsConstExpr (E->Expr);
408 long GetExportVal (const Export* E)
409 /* Get the value of this export */
413 Internal ("`%s' is an undefined external", E->Name);
415 return GetExprVal (E->Expr);
420 static void CheckSymType (const Export* E)
421 /* Check the types for one export */
423 /* External with matching imports */
424 Import* Imp = E->ImpList;
425 int ZP = IS_EXP_ZP (E->Type);
427 if (ZP != IS_IMP_ZP (Imp->Type)) {
428 /* Export is ZP, import is abs or the other way round */
430 /* User defined export */
431 Warning ("Type mismatch for `%s', export in "
432 "%s(%lu), import in %s(%lu)",
433 E->Name, E->Obj->Files [Imp->Pos.Name],
434 E->Pos.Line, Imp->Obj->Files [Imp->Pos.Name],
437 /* Export created by the linker */
438 Warning ("Type mismatch for `%s', imported from %s(%lu)",
439 E->Name, Imp->Obj->Files [Imp->Pos.Name],
449 static void CheckSymTypes (void)
450 /* Check for symbol tape mismatches */
454 /* Print all open imports */
455 for (I = 0; I < ExpCount; ++I) {
456 const Export* E = ExpPool [I];
457 if (E->Expr != 0 && E->ImpCount > 0) {
458 /* External with matching imports */
466 static void PrintUnresolved (ExpCheckFunc F, void* Data)
467 /* Print a list of unresolved symbols. On unresolved symbols, F is
468 * called (see the comments on ExpCheckFunc in the data section).
473 /* Print all open imports */
474 for (I = 0; I < ExpCount; ++I) {
475 Export* E = ExpPool [I];
476 if (E->Expr == 0 && E->ImpCount > 0 && F (E->Name, Data) == 0) {
477 /* Unresolved external */
478 Import* Imp = E->ImpList;
480 "Unresolved external `%s' referenced in:\n",
483 const char* Name = Imp->Obj->Files [Imp->Pos.Name];
484 fprintf (stderr, " %s(%lu)\n", Name, Imp->Pos.Line);
493 static int CmpExpName (const void* K1, const void* K2)
494 /* Compare function for qsort */
496 return strcmp ((*(Export**)K1)->Name, (*(Export**)K2)->Name);
501 static void CreateExportPool (void)
502 /* Create an array with pointer to all exports */
506 /* Allocate memory */
510 ExpPool = xmalloc (ExpCount * sizeof (Export*));
512 /* Walk through the list and insert the exports */
513 for (I = 0, J = 0; I < sizeof (HashTab) / sizeof (HashTab [0]); ++I) {
514 Export* E = HashTab [I];
516 CHECK (J < ExpCount);
522 /* Sort them by name */
523 qsort (ExpPool, ExpCount, sizeof (Export*), CmpExpName);
528 void CheckExports (ExpCheckFunc F, void* Data)
529 /* Check if there are any unresolved symbols. On unresolved symbols, F is
530 * called (see the comments on ExpCheckFunc in the data section).
533 /* Create an export pool */
536 /* Check for symbol type mismatches */
539 /* Check for unresolved externals (check here for special bin formats) */
541 /* Print all open imports */
542 PrintUnresolved (F, Data);
548 void PrintExportMap (FILE* F)
549 /* Print an export map to the given file */
554 /* Print all exports */
556 for (I = 0; I < ExpCount; ++I) {
557 const Export* E = ExpPool [I];
559 /* Print unreferenced symbols only if explictly requested */
560 if (VerboseMap || E->ImpCount > 0) {
562 "%-25s %06lX %c%c%c ",
565 E->ImpCount? 'R' : ' ',
566 IS_EXP_ZP (E->Type)? 'Z' : ' ',
567 IS_EXP_INIT (E->Type)? 'I' : ' ');
579 void PrintImportMap (FILE* F)
580 /* Print an import map to the given file */
585 /* Loop over all exports */
586 for (I = 0; I < ExpCount; ++I) {
589 const Export* Exp = ExpPool [I];
591 /* Print the symbol only if there are imports, or if a verbose map
594 if (VerboseMap || Exp->ImpCount > 0) {
596 /* Get the name of the object file that exports the symbol.
597 * Beware: There may be no object file if the symbol is a linker
600 const char* ObjName = (Exp->Obj != 0)? Exp->Obj->Name : "linker generated";
602 /* Print the export */
608 /* Print all imports for this symbol */
612 /* Print the import */
616 Imp->Obj->Files [Imp->Pos.Name],
629 void PrintExportLabels (FILE* F)
630 /* Print the exports in a VICE label file */
634 /* Print all exports */
635 for (I = 0; I < ExpCount; ++I) {
636 const Export* E = ExpPool [I];
637 fprintf (F, "al %06lX .%s\n", GetExportVal (E), E->Name);
643 void MarkExport (Export* E)
644 /* Mark the export */
646 E->Flags |= EXP_USERMARK;
651 void UnmarkExport (Export* E)
652 /* Remove the mark from the export */
654 E->Flags &= ~EXP_USERMARK;
659 int ExportHasMark (Export* E)
660 /* Return true if the export has a mark */
662 return (E->Flags & EXP_USERMARK) != 0;
667 void CircularRefError (const Export* E)
668 /* Print an error about a circular reference using to define the given export */
670 Error ("Circular reference for symbol `%s', %s(%lu)",
671 E->Name, E->Obj->Files [E->Pos.Name], E->Pos.Line);