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",
174 GetObjFileName (Obj), Type);
177 /* Create a new import */
178 I = NewImport (Type, Obj);
181 I->V.Name = ReadStr (F);
183 /* Read the file position */
184 ReadFilePos (F, &I->Pos);
186 /* Return the new import */
192 /*****************************************************************************/
194 /*****************************************************************************/
198 static Export* NewExport (unsigned char Type, const char* Name, ObjData* Obj)
199 /* Create a new export and initialize it */
201 /* Allocate memory */
202 Export* E = xmalloc (sizeof (Export));
204 /* Initialize the fields */
212 memset (E->ConDes, 0, sizeof (E->ConDes));
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 /* Insert the export into any condes tables if needed */
235 if (IS_EXP_CONDES (E->Type)) {
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 */
298 unsigned ConDesCount;
304 /* Create a new export without a name */
305 E = NewExport (Type, 0, O);
307 /* Read the constructor/destructor decls if we have any */
308 ConDesCount = GET_EXP_CONDES_COUNT (Type);
309 if (ConDesCount > 0) {
311 unsigned char ConDes[CD_TYPE_COUNT];
314 /* Read the data into temp storage */
315 ReadData (F, ConDes, ConDesCount);
317 /* Re-order the data. In the file, each decl is encoded into a byte
318 * which contains the type and the priority. In memory, we will use
319 * an array of types which contain the priority. This array was
320 * cleared by the constructor (NewExport), so we must only set the
321 * fields that contain values.
323 for (I = 0; I < ConDesCount; ++I) {
324 unsigned ConDesType = CD_GET_TYPE (ConDes[I]);
325 unsigned ConDesPrio = CD_GET_PRIO (ConDes[I]);
326 E->ConDes[ConDesType] = ConDesPrio;
331 E->Name = ReadStr (F);
334 if (IS_EXP_EXPR (Type)) {
335 E->Expr = ReadExpr (F, O);
337 E->Expr = LiteralExpr (Read32 (F), O);
340 /* Last is the file position where the definition was done */
341 ReadFilePos (F, &E->Pos);
343 /* Return the new export */
349 Export* CreateConstExport (const char* Name, long Value)
350 /* Create an export for a literal date */
352 /* Create a new export */
353 Export* E = NewExport (EXP_ABS | EXP_CONST | EXP_EQUATE, Name, 0);
355 /* Assign the value */
356 E->Expr = LiteralExpr (Value, 0);
358 /* Insert the export */
361 /* Return the new export */
367 Export* CreateMemExport (const char* Name, Memory* Mem, unsigned long Offs)
368 /* Create an relative export for a memory area offset */
370 /* Create a new export */
371 Export* E = NewExport (EXP_ABS | EXP_EXPR | EXP_LABEL, Name, 0);
373 /* Assign the value */
374 E->Expr = MemExpr (Mem, Offs, 0);
376 /* Insert the export */
379 /* Return the new export */
385 Export* CreateSegExport (const char* Name, Section* Sec, unsigned long Offs)
386 /* Create a relative export to a segment (section) */
388 /* Create a new export */
389 Export* E = NewExport (EXP_ABS | EXP_EXPR | EXP_LABEL, Name, 0);
391 /* Assign the value */
392 E->Expr = SegExpr (Sec, Offs, 0);
394 /* Insert the export */
397 /* Return the new export */
403 Export* FindExport (const char* Name)
404 /* Check for an identifier in the list. Return 0 if not found, otherwise
405 * return a pointer to the export.
408 /* Get a pointer to the list with the symbols hash value */
409 Export* L = HashTab [HashStr (Name) % HASHTAB_SIZE];
411 /* Search through the list in that slot */
412 if (strcmp (L->Name, Name) == 0) {
425 int IsUnresolved (const char* Name)
426 /* Check if this symbol is an unresolved export */
428 /* Find the export */
429 return IsUnresolvedExport (FindExport (Name));
434 int IsUnresolvedExport (const Export* E)
435 /* Return true if the given export is unresolved */
437 /* Check if it's unresolved */
438 return E != 0 && E->Expr == 0;
443 int IsConstExport (const Export* E)
444 /* Return true if the expression associated with this export is const */
447 /* External symbols cannot be const */
450 return IsConstExpr (E->Expr);
456 long GetExportVal (const Export* E)
457 /* Get the value of this export */
461 Internal ("`%s' is an undefined external", E->Name);
463 return GetExprVal (E->Expr);
468 static void CheckSymType (const Export* E)
469 /* Check the types for one export */
471 /* External with matching imports */
472 Import* Imp = E->ImpList;
473 int ZP = IS_EXP_ZP (E->Type);
475 if (ZP != IS_IMP_ZP (Imp->Type)) {
476 /* Export is ZP, import is abs or the other way round */
478 /* User defined export */
479 Warning ("Type mismatch for `%s', export in "
480 "%s(%lu), import in %s(%lu)",
481 E->Name, GetSourceFileName (E->Obj, Imp->Pos.Name),
482 E->Pos.Line, GetSourceFileName (Imp->Obj, Imp->Pos.Name),
485 /* Export created by the linker */
486 Warning ("Type mismatch for `%s', imported from %s(%lu)",
487 E->Name, GetSourceFileName (Imp->Obj, Imp->Pos.Name),
497 static void CheckSymTypes (void)
498 /* Check for symbol tape mismatches */
502 /* Print all open imports */
503 for (I = 0; I < ExpCount; ++I) {
504 const Export* E = ExpPool [I];
505 if (E->Expr != 0 && E->ImpCount > 0) {
506 /* External with matching imports */
514 static void PrintUnresolved (ExpCheckFunc F, void* Data)
515 /* Print a list of unresolved symbols. On unresolved symbols, F is
516 * called (see the comments on ExpCheckFunc in the data section).
521 /* Print all open imports */
522 for (I = 0; I < ExpCount; ++I) {
523 Export* E = ExpPool [I];
524 if (E->Expr == 0 && E->ImpCount > 0 && F (E->Name, Data) == 0) {
525 /* Unresolved external */
526 Import* Imp = E->ImpList;
528 "Unresolved external `%s' referenced in:\n",
531 const char* Name = GetSourceFileName (Imp->Obj, Imp->Pos.Name);
532 fprintf (stderr, " %s(%lu)\n", Name, Imp->Pos.Line);
541 static int CmpExpName (const void* K1, const void* K2)
542 /* Compare function for qsort */
544 return strcmp ((*(Export**)K1)->Name, (*(Export**)K2)->Name);
549 static void CreateExportPool (void)
550 /* Create an array with pointer to all exports */
554 /* Allocate memory */
558 ExpPool = xmalloc (ExpCount * sizeof (Export*));
560 /* Walk through the list and insert the exports */
561 for (I = 0, J = 0; I < sizeof (HashTab) / sizeof (HashTab [0]); ++I) {
562 Export* E = HashTab [I];
564 CHECK (J < ExpCount);
570 /* Sort them by name */
571 qsort (ExpPool, ExpCount, sizeof (Export*), CmpExpName);
576 void CheckExports (ExpCheckFunc F, void* Data)
577 /* Check if there are any unresolved symbols. On unresolved symbols, F is
578 * called (see the comments on ExpCheckFunc in the data section).
581 /* Create an export pool */
584 /* Check for symbol type mismatches */
587 /* Check for unresolved externals (check here for special bin formats) */
589 /* Print all open imports */
590 PrintUnresolved (F, Data);
596 void PrintExportMap (FILE* F)
597 /* Print an export map to the given file */
602 /* Print all exports */
604 for (I = 0; I < ExpCount; ++I) {
605 const Export* E = ExpPool [I];
607 /* Print unreferenced symbols only if explictly requested */
608 if (VerboseMap || E->ImpCount > 0 || IS_EXP_CONDES (E->Type)) {
610 "%-25s %06lX %c%c%c%c ",
613 E->ImpCount? 'R' : ' ',
614 IS_EXP_LABEL (E->Type)? 'L' : 'E',
615 IS_EXP_ZP (E->Type)? 'Z' : ' ',
616 IS_EXP_CONDES (E->Type)? 'I' : ' ');
628 void PrintImportMap (FILE* F)
629 /* Print an import map to the given file */
634 /* Loop over all exports */
635 for (I = 0; I < ExpCount; ++I) {
638 const Export* Exp = ExpPool [I];
640 /* Print the symbol only if there are imports, or if a verbose map
643 if (VerboseMap || Exp->ImpCount > 0) {
645 /* Print the export */
649 GetObjFileName (Exp->Obj));
651 /* Print all imports for this symbol */
655 /* Print the import */
658 GetObjFileName (Imp->Obj),
659 GetSourceFileName (Imp->Obj, Imp->Pos.Name),
672 void PrintExportLabels (FILE* F)
673 /* Print the exports in a VICE label file */
677 /* Print all exports */
678 for (I = 0; I < ExpCount; ++I) {
679 const Export* E = ExpPool [I];
680 fprintf (F, "al %06lX .%s\n", GetExportVal (E), E->Name);
686 void MarkExport (Export* E)
687 /* Mark the export */
689 E->Flags |= EXP_USERMARK;
694 void UnmarkExport (Export* E)
695 /* Remove the mark from the export */
697 E->Flags &= ~EXP_USERMARK;
702 int ExportHasMark (Export* E)
703 /* Return true if the export has a mark */
705 return (E->Flags & EXP_USERMARK) != 0;
710 void CircularRefError (const Export* E)
711 /* Print an error about a circular reference using to define the given export */
713 Error ("Circular reference for symbol `%s', %s(%lu)",
714 E->Name, GetSourceFileName (E->Obj, E->Pos.Name), E->Pos.Line);