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 */
211 memset (E->ConDes, 0, sizeof (E->ConDes));
213 E->Name = xstrdup (Name);
215 /* Name will get added later */
219 /* Return the new entry */
225 void InsertExport (Export* E)
226 /* Insert an exported identifier and check if it's already in the list */
233 /* Insert the export into any condes tables if needed */
234 if (IS_EXP_CONDES (E->Type)) {
238 /* Create a hash value for the given name */
239 HashVal = HashStr (E->Name) % HASHTAB_SIZE;
241 /* Search through the list in that slot */
242 if (HashTab [HashVal] == 0) {
243 /* The slot is empty */
244 HashTab [HashVal] = E;
249 L = HashTab [HashVal];
251 if (strcmp (L->Name, E->Name) == 0) {
252 /* This may be an unresolved external */
255 /* This *is* an unresolved external */
257 E->ImpCount = L->ImpCount;
258 E->ImpList = L->ImpList;
262 HashTab [HashVal] = E;
264 ImpOpen -= E->ImpCount; /* Decrease open imports now */
266 /* We must run through the import list and change the
267 * export pointer now.
275 /* Duplicate entry, ignore it */
276 Warning ("Duplicate external identifier: `%s'", L->Name);
285 /* Insert export at end of queue */
293 Export* ReadExport (FILE* F, ObjData* O)
294 /* Read an export from a file */
297 unsigned ConDesCount;
303 /* Create a new export without a name */
304 E = NewExport (Type, 0, O);
306 /* Read the constructor/destructor decls if we have any */
307 ConDesCount = GET_EXP_CONDES_COUNT (Type);
308 if (ConDesCount > 0) {
310 unsigned char ConDes[CD_TYPE_COUNT];
313 /* Read the data into temp storage */
314 ReadData (F, ConDes, ConDesCount);
316 /* Re-order the data. In the file, each decl is encoded into a byte
317 * which contains the type and the priority. In memory, we will use
318 * an array of types which contain the priority. This array was
319 * cleared by the constructor (NewExport), so we must only set the
320 * fields that contain values.
322 for (I = 0; I < ConDesCount; ++I) {
323 unsigned ConDesType = CD_GET_TYPE (ConDes[I]);
324 unsigned ConDesPrio = CD_GET_PRIO (ConDes[I]);
325 E->ConDes[ConDesType] = ConDesPrio;
330 E->Name = ReadStr (F);
333 if (IS_EXP_EXPR (Type)) {
334 E->Expr = ReadExpr (F, O);
336 E->Expr = LiteralExpr (Read32 (F), O);
339 /* Last is the file position where the definition was done */
340 ReadFilePos (F, &E->Pos);
342 /* Return the new export */
348 Export* CreateConstExport (const char* Name, long Value)
349 /* Create an export for a literal date */
351 /* Create a new export */
352 Export* E = NewExport (EXP_ABS, Name, 0);
354 /* Assign the value */
355 E->Expr = LiteralExpr (Value, 0);
357 /* Insert the export */
360 /* Return the new export */
366 Export* CreateMemExport (const char* Name, Memory* Mem, unsigned long Offs)
367 /* Create an relative export for a memory area offset */
369 /* Create a new export */
370 Export* E = NewExport (EXP_ABS, Name, 0);
372 /* Assign the value */
373 E->Expr = MemExpr (Mem, Offs, 0);
375 /* Insert the export */
378 /* Return the new export */
384 static Export* FindExport (const char* Name)
385 /* Check for an identifier in the list. Return 0 if not found, otherwise
386 * return a pointer to the export.
389 /* Get a pointer to the list with the symbols hash value */
390 Export* L = HashTab [HashStr (Name) % HASHTAB_SIZE];
392 /* Search through the list in that slot */
393 if (strcmp (L->Name, Name) == 0) {
406 int IsUnresolved (const char* Name)
407 /* Check if this symbol is an unresolved export */
409 /* Find the export */
410 Export* E = FindExport (Name);
412 /* Check if it's unresolved */
413 return E != 0 && E->Expr == 0;
418 int IsConstExport (const Export* E)
419 /* Return true if the expression associated with this export is const */
422 /* External symbols cannot be const */
425 return IsConstExpr (E->Expr);
431 long GetExportVal (const Export* E)
432 /* Get the value of this export */
436 Internal ("`%s' is an undefined external", E->Name);
438 return GetExprVal (E->Expr);
443 static void CheckSymType (const Export* E)
444 /* Check the types for one export */
446 /* External with matching imports */
447 Import* Imp = E->ImpList;
448 int ZP = IS_EXP_ZP (E->Type);
450 if (ZP != IS_IMP_ZP (Imp->Type)) {
451 /* Export is ZP, import is abs or the other way round */
453 /* User defined export */
454 Warning ("Type mismatch for `%s', export in "
455 "%s(%lu), import in %s(%lu)",
456 E->Name, E->Obj->Files [Imp->Pos.Name],
457 E->Pos.Line, Imp->Obj->Files [Imp->Pos.Name],
460 /* Export created by the linker */
461 Warning ("Type mismatch for `%s', imported from %s(%lu)",
462 E->Name, Imp->Obj->Files [Imp->Pos.Name],
472 static void CheckSymTypes (void)
473 /* Check for symbol tape mismatches */
477 /* Print all open imports */
478 for (I = 0; I < ExpCount; ++I) {
479 const Export* E = ExpPool [I];
480 if (E->Expr != 0 && E->ImpCount > 0) {
481 /* External with matching imports */
489 static void PrintUnresolved (ExpCheckFunc F, void* Data)
490 /* Print a list of unresolved symbols. On unresolved symbols, F is
491 * called (see the comments on ExpCheckFunc in the data section).
496 /* Print all open imports */
497 for (I = 0; I < ExpCount; ++I) {
498 Export* E = ExpPool [I];
499 if (E->Expr == 0 && E->ImpCount > 0 && F (E->Name, Data) == 0) {
500 /* Unresolved external */
501 Import* Imp = E->ImpList;
503 "Unresolved external `%s' referenced in:\n",
506 const char* Name = Imp->Obj->Files [Imp->Pos.Name];
507 fprintf (stderr, " %s(%lu)\n", Name, Imp->Pos.Line);
516 static int CmpExpName (const void* K1, const void* K2)
517 /* Compare function for qsort */
519 return strcmp ((*(Export**)K1)->Name, (*(Export**)K2)->Name);
524 static void CreateExportPool (void)
525 /* Create an array with pointer to all exports */
529 /* Allocate memory */
533 ExpPool = xmalloc (ExpCount * sizeof (Export*));
535 /* Walk through the list and insert the exports */
536 for (I = 0, J = 0; I < sizeof (HashTab) / sizeof (HashTab [0]); ++I) {
537 Export* E = HashTab [I];
539 CHECK (J < ExpCount);
545 /* Sort them by name */
546 qsort (ExpPool, ExpCount, sizeof (Export*), CmpExpName);
551 void CheckExports (ExpCheckFunc F, void* Data)
552 /* Check if there are any unresolved symbols. On unresolved symbols, F is
553 * called (see the comments on ExpCheckFunc in the data section).
556 /* Create an export pool */
559 /* Check for symbol type mismatches */
562 /* Check for unresolved externals (check here for special bin formats) */
564 /* Print all open imports */
565 PrintUnresolved (F, Data);
571 void PrintExportMap (FILE* F)
572 /* Print an export map to the given file */
577 /* Print all exports */
579 for (I = 0; I < ExpCount; ++I) {
580 const Export* E = ExpPool [I];
582 /* Print unreferenced symbols only if explictly requested */
583 if (VerboseMap || E->ImpCount > 0) {
585 "%-25s %06lX %c%c%c ",
588 E->ImpCount? 'R' : ' ',
589 IS_EXP_ZP (E->Type)? 'Z' : ' ',
590 IS_EXP_CONDES (E->Type)? 'I' : ' ');
602 void PrintImportMap (FILE* F)
603 /* Print an import map to the given file */
608 /* Loop over all exports */
609 for (I = 0; I < ExpCount; ++I) {
612 const Export* Exp = ExpPool [I];
614 /* Print the symbol only if there are imports, or if a verbose map
617 if (VerboseMap || Exp->ImpCount > 0) {
619 /* Get the name of the object file that exports the symbol.
620 * Beware: There may be no object file if the symbol is a linker
623 const char* ObjName = (Exp->Obj != 0)? Exp->Obj->Name : "linker generated";
625 /* Print the export */
631 /* Print all imports for this symbol */
635 /* Print the import */
639 Imp->Obj->Files [Imp->Pos.Name],
652 void PrintExportLabels (FILE* F)
653 /* Print the exports in a VICE label file */
657 /* Print all exports */
658 for (I = 0; I < ExpCount; ++I) {
659 const Export* E = ExpPool [I];
660 fprintf (F, "al %06lX .%s\n", GetExportVal (E), E->Name);
666 void MarkExport (Export* E)
667 /* Mark the export */
669 E->Flags |= EXP_USERMARK;
674 void UnmarkExport (Export* E)
675 /* Remove the mark from the export */
677 E->Flags &= ~EXP_USERMARK;
682 int ExportHasMark (Export* E)
683 /* Return true if the export has a mark */
685 return (E->Flags & EXP_USERMARK) != 0;
690 void CircularRefError (const Export* E)
691 /* Print an error about a circular reference using to define the given export */
693 Error ("Circular reference for symbol `%s', %s(%lu)",
694 E->Name, E->Obj->Files [E->Pos.Name], E->Pos.Line);