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* CreateMemoryExport (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 = MemoryExpr (Mem, Offs, 0);
376 /* Insert the export */
379 /* Return the new export */
385 Export* CreateSegmentExport (const char* Name, Segment* Seg, unsigned long Offs)
386 /* Create a relative export to a segment */
388 /* Create a new export */
389 Export* E = NewExport (EXP_ABS | EXP_EXPR | EXP_LABEL, Name, 0);
391 /* Assign the value */
392 E->Expr = SegmentExpr (Seg, Offs, 0);
394 /* Insert the export */
397 /* Return the new export */
403 Export* CreateSectionExport (const char* Name, Section* Sec, unsigned long Offs)
404 /* Create a relative export to a section */
406 /* Create a new export */
407 Export* E = NewExport (EXP_ABS | EXP_EXPR | EXP_LABEL, Name, 0);
409 /* Assign the value */
410 E->Expr = SectionExpr (Sec, Offs, 0);
412 /* Insert the export */
415 /* Return the new export */
421 Export* FindExport (const char* Name)
422 /* Check for an identifier in the list. Return 0 if not found, otherwise
423 * return a pointer to the export.
426 /* Get a pointer to the list with the symbols hash value */
427 Export* L = HashTab [HashStr (Name) % HASHTAB_SIZE];
429 /* Search through the list in that slot */
430 if (strcmp (L->Name, Name) == 0) {
443 int IsUnresolved (const char* Name)
444 /* Check if this symbol is an unresolved export */
446 /* Find the export */
447 return IsUnresolvedExport (FindExport (Name));
452 int IsUnresolvedExport (const Export* E)
453 /* Return true if the given export is unresolved */
455 /* Check if it's unresolved */
456 return E != 0 && E->Expr == 0;
461 int IsConstExport (const Export* E)
462 /* Return true if the expression associated with this export is const */
465 /* External symbols cannot be const */
468 return IsConstExpr (E->Expr);
474 long GetExportVal (const Export* E)
475 /* Get the value of this export */
479 Internal ("`%s' is an undefined external", E->Name);
481 return GetExprVal (E->Expr);
486 static void CheckSymType (const Export* E)
487 /* Check the types for one export */
489 /* External with matching imports */
490 Import* Imp = E->ImpList;
491 int ZP = IS_EXP_ZP (E->Type);
493 if (ZP != IS_IMP_ZP (Imp->Type)) {
494 /* Export is ZP, import is abs or the other way round */
496 /* User defined export */
497 Warning ("Type mismatch for `%s', export in "
498 "%s(%lu), import in %s(%lu)",
499 E->Name, GetSourceFileName (E->Obj, Imp->Pos.Name),
500 E->Pos.Line, GetSourceFileName (Imp->Obj, Imp->Pos.Name),
503 /* Export created by the linker */
504 Warning ("Type mismatch for `%s', imported from %s(%lu)",
505 E->Name, GetSourceFileName (Imp->Obj, Imp->Pos.Name),
515 static void CheckSymTypes (void)
516 /* Check for symbol tape mismatches */
520 /* Print all open imports */
521 for (I = 0; I < ExpCount; ++I) {
522 const Export* E = ExpPool [I];
523 if (E->Expr != 0 && E->ImpCount > 0) {
524 /* External with matching imports */
532 static void PrintUnresolved (ExpCheckFunc F, void* Data)
533 /* Print a list of unresolved symbols. On unresolved symbols, F is
534 * called (see the comments on ExpCheckFunc in the data section).
539 /* Print all open imports */
540 for (I = 0; I < ExpCount; ++I) {
541 Export* E = ExpPool [I];
542 if (E->Expr == 0 && E->ImpCount > 0 && F (E->Name, Data) == 0) {
543 /* Unresolved external */
544 Import* Imp = E->ImpList;
546 "Unresolved external `%s' referenced in:\n",
549 const char* Name = GetSourceFileName (Imp->Obj, Imp->Pos.Name);
550 fprintf (stderr, " %s(%lu)\n", Name, Imp->Pos.Line);
559 static int CmpExpName (const void* K1, const void* K2)
560 /* Compare function for qsort */
562 return strcmp ((*(Export**)K1)->Name, (*(Export**)K2)->Name);
567 static void CreateExportPool (void)
568 /* Create an array with pointer to all exports */
572 /* Allocate memory */
576 ExpPool = xmalloc (ExpCount * sizeof (Export*));
578 /* Walk through the list and insert the exports */
579 for (I = 0, J = 0; I < sizeof (HashTab) / sizeof (HashTab [0]); ++I) {
580 Export* E = HashTab [I];
582 CHECK (J < ExpCount);
588 /* Sort them by name */
589 qsort (ExpPool, ExpCount, sizeof (Export*), CmpExpName);
594 void CheckExports (ExpCheckFunc F, void* Data)
595 /* Check if there are any unresolved symbols. On unresolved symbols, F is
596 * called (see the comments on ExpCheckFunc in the data section).
599 /* Create an export pool */
602 /* Check for symbol type mismatches */
605 /* Check for unresolved externals (check here for special bin formats) */
607 /* Print all open imports */
608 PrintUnresolved (F, Data);
614 void PrintExportMap (FILE* F)
615 /* Print an export map to the given file */
620 /* Print all exports */
622 for (I = 0; I < ExpCount; ++I) {
623 const Export* E = ExpPool [I];
625 /* Print unreferenced symbols only if explictly requested */
626 if (VerboseMap || E->ImpCount > 0 || IS_EXP_CONDES (E->Type)) {
628 "%-25s %06lX %c%c%c%c ",
631 E->ImpCount? 'R' : ' ',
632 IS_EXP_LABEL (E->Type)? 'L' : 'E',
633 IS_EXP_ZP (E->Type)? 'Z' : ' ',
634 IS_EXP_CONDES (E->Type)? 'I' : ' ');
646 void PrintImportMap (FILE* F)
647 /* Print an import map to the given file */
652 /* Loop over all exports */
653 for (I = 0; I < ExpCount; ++I) {
656 const Export* Exp = ExpPool [I];
658 /* Print the symbol only if there are imports, or if a verbose map
661 if (VerboseMap || Exp->ImpCount > 0) {
663 /* Print the export */
667 GetObjFileName (Exp->Obj));
669 /* Print all imports for this symbol */
673 /* Print the import */
676 GetObjFileName (Imp->Obj),
677 GetSourceFileName (Imp->Obj, Imp->Pos.Name),
690 void PrintExportLabels (FILE* F)
691 /* Print the exports in a VICE label file */
695 /* Print all exports */
696 for (I = 0; I < ExpCount; ++I) {
697 const Export* E = ExpPool [I];
698 fprintf (F, "al %06lX .%s\n", GetExportVal (E), E->Name);
704 void MarkExport (Export* E)
705 /* Mark the export */
707 E->Flags |= EXP_USERMARK;
712 void UnmarkExport (Export* E)
713 /* Remove the mark from the export */
715 E->Flags &= ~EXP_USERMARK;
720 int ExportHasMark (Export* E)
721 /* Return true if the export has a mark */
723 return (E->Flags & EXP_USERMARK) != 0;
728 void CircularRefError (const Export* E)
729 /* Print an error about a circular reference using to define the given export */
731 Error ("Circular reference for symbol `%s', %s(%lu)",
732 E->Name, GetSourceFileName (E->Obj, E->Pos.Name), E->Pos.Line);