1 /*****************************************************************************/
5 /* Exports handling for the ld65 linker */
9 /* (C) 1998-2003 Ullrich von Bassewitz */
10 /* Römerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
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 */
162 Import* ReadImport (FILE* F, ObjData* Obj)
163 /* Read an import from a file and return it */
167 /* Read the import type and check it */
168 unsigned char Type = Read8 (F);
169 if (Type != IMP_ZP && Type != IMP_ABS) {
170 Error ("Unknown import type in module `%s': %02X",
171 GetObjFileName (Obj), Type);
174 /* Create a new import */
175 I = NewImport (Type, Obj);
178 I->V.Name = GetObjString (Obj, ReadVar (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 /* Allocate memory */
199 Export* E = xmalloc (sizeof (Export));
201 /* Initialize the fields */
209 memset (E->ConDes, 0, sizeof (E->ConDes));
211 E->Name = xstrdup (Name);
213 /* Name will get added later */
217 /* Return the new entry */
223 void InsertExport (Export* E)
224 /* Insert an exported identifier and check if it's already in the list */
231 /* Insert the export into any condes tables if needed */
232 if (IS_EXP_CONDES (E->Type)) {
236 /* Create a hash value for the given name */
237 HashVal = HashStr (E->Name) % HASHTAB_SIZE;
239 /* Search through the list in that slot */
240 if (HashTab [HashVal] == 0) {
241 /* The slot is empty */
242 HashTab [HashVal] = E;
247 L = HashTab [HashVal];
249 if (strcmp (L->Name, E->Name) == 0) {
250 /* This may be an unresolved external */
253 /* This *is* an unresolved external */
255 E->ImpCount = L->ImpCount;
256 E->ImpList = L->ImpList;
260 HashTab [HashVal] = E;
262 ImpOpen -= E->ImpCount; /* Decrease open imports now */
264 /* We must run through the import list and change the
265 * export pointer now.
273 /* Duplicate entry, ignore it */
274 Warning ("Duplicate external identifier: `%s'", L->Name);
283 /* Insert export at end of queue */
291 Export* ReadExport (FILE* F, ObjData* O)
292 /* Read an export from a file */
295 unsigned ConDesCount;
301 /* Create a new export without a name */
302 E = NewExport (Type, 0, O);
304 /* Read the constructor/destructor decls if we have any */
305 ConDesCount = GET_EXP_CONDES_COUNT (Type);
306 if (ConDesCount > 0) {
308 unsigned char ConDes[CD_TYPE_COUNT];
311 /* Read the data into temp storage */
312 ReadData (F, ConDes, ConDesCount);
314 /* Re-order the data. In the file, each decl is encoded into a byte
315 * which contains the type and the priority. In memory, we will use
316 * an array of types which contain the priority. This array was
317 * cleared by the constructor (NewExport), so we must only set the
318 * fields that contain values.
320 for (I = 0; I < ConDesCount; ++I) {
321 unsigned ConDesType = CD_GET_TYPE (ConDes[I]);
322 unsigned ConDesPrio = CD_GET_PRIO (ConDes[I]);
323 E->ConDes[ConDesType] = ConDesPrio;
328 E->Name = GetObjString (O, ReadVar (F));
331 if (IS_EXP_EXPR (Type)) {
332 E->Expr = ReadExpr (F, O);
334 E->Expr = LiteralExpr (Read32 (F), O);
337 /* Last is the file position where the definition was done */
338 ReadFilePos (F, &E->Pos);
340 /* Return the new export */
346 Export* CreateConstExport (const char* Name, long Value)
347 /* Create an export for a literal date */
349 /* Create a new export */
350 Export* E = NewExport (EXP_ABS | EXP_CONST | EXP_EQUATE, Name, 0);
352 /* Assign the value */
353 E->Expr = LiteralExpr (Value, 0);
355 /* Insert the export */
358 /* Return the new export */
364 Export* CreateMemoryExport (const char* Name, Memory* Mem, unsigned long Offs)
365 /* Create an relative export for a memory area offset */
367 /* Create a new export */
368 Export* E = NewExport (EXP_ABS | EXP_EXPR | EXP_LABEL, Name, 0);
370 /* Assign the value */
371 E->Expr = MemoryExpr (Mem, Offs, 0);
373 /* Insert the export */
376 /* Return the new export */
382 Export* CreateSegmentExport (const char* Name, Segment* Seg, unsigned long Offs)
383 /* Create a relative export to a segment */
385 /* Create a new export */
386 Export* E = NewExport (EXP_ABS | EXP_EXPR | EXP_LABEL, Name, 0);
388 /* Assign the value */
389 E->Expr = SegmentExpr (Seg, Offs, 0);
391 /* Insert the export */
394 /* Return the new export */
400 Export* CreateSectionExport (const char* Name, Section* Sec, unsigned long Offs)
401 /* Create a relative export to a section */
403 /* Create a new export */
404 Export* E = NewExport (EXP_ABS | EXP_EXPR | EXP_LABEL, Name, 0);
406 /* Assign the value */
407 E->Expr = SectionExpr (Sec, Offs, 0);
409 /* Insert the export */
412 /* Return the new export */
418 Export* FindExport (const char* Name)
419 /* Check for an identifier in the list. Return 0 if not found, otherwise
420 * return a pointer to the export.
423 /* Get a pointer to the list with the symbols hash value */
424 Export* L = HashTab [HashStr (Name) % HASHTAB_SIZE];
426 /* Search through the list in that slot */
427 if (strcmp (L->Name, Name) == 0) {
440 int IsUnresolved (const char* Name)
441 /* Check if this symbol is an unresolved export */
443 /* Find the export */
444 return IsUnresolvedExport (FindExport (Name));
449 int IsUnresolvedExport (const Export* E)
450 /* Return true if the given export is unresolved */
452 /* Check if it's unresolved */
453 return E != 0 && E->Expr == 0;
458 int IsConstExport (const Export* E)
459 /* Return true if the expression associated with this export is const */
462 /* External symbols cannot be const */
465 return IsConstExpr (E->Expr);
471 long GetExportVal (const Export* E)
472 /* Get the value of this export */
476 Internal ("`%s' is an undefined external", E->Name);
478 return GetExprVal (E->Expr);
483 static void CheckSymType (const Export* E)
484 /* Check the types for one export */
486 /* External with matching imports */
487 Import* Imp = E->ImpList;
488 int ZP = IS_EXP_ZP (E->Type);
490 if (ZP != IS_IMP_ZP (Imp->Type)) {
491 /* Export is ZP, import is abs or the other way round */
493 /* User defined export */
494 Warning ("Type mismatch for `%s', export in "
495 "%s(%lu), import in %s(%lu)",
496 E->Name, GetSourceFileName (E->Obj, Imp->Pos.Name),
497 E->Pos.Line, GetSourceFileName (Imp->Obj, Imp->Pos.Name),
500 /* Export created by the linker */
501 Warning ("Type mismatch for `%s', imported from %s(%lu)",
502 E->Name, GetSourceFileName (Imp->Obj, Imp->Pos.Name),
512 static void CheckSymTypes (void)
513 /* Check for symbol tape mismatches */
517 /* Print all open imports */
518 for (I = 0; I < ExpCount; ++I) {
519 const Export* E = ExpPool [I];
520 if (E->Expr != 0 && E->ImpCount > 0) {
521 /* External with matching imports */
529 static void PrintUnresolved (ExpCheckFunc F, void* Data)
530 /* Print a list of unresolved symbols. On unresolved symbols, F is
531 * called (see the comments on ExpCheckFunc in the data section).
536 /* Print all open imports */
537 for (I = 0; I < ExpCount; ++I) {
538 Export* E = ExpPool [I];
539 if (E->Expr == 0 && E->ImpCount > 0 && F (E->Name, Data) == 0) {
540 /* Unresolved external */
541 Import* Imp = E->ImpList;
543 "Unresolved external `%s' referenced in:\n",
546 const char* Name = GetSourceFileName (Imp->Obj, Imp->Pos.Name);
547 fprintf (stderr, " %s(%lu)\n", Name, Imp->Pos.Line);
556 static int CmpExpName (const void* K1, const void* K2)
557 /* Compare function for qsort */
559 return strcmp ((*(Export**)K1)->Name, (*(Export**)K2)->Name);
564 static void CreateExportPool (void)
565 /* Create an array with pointer to all exports */
569 /* Allocate memory */
573 ExpPool = xmalloc (ExpCount * sizeof (Export*));
575 /* Walk through the list and insert the exports */
576 for (I = 0, J = 0; I < sizeof (HashTab) / sizeof (HashTab [0]); ++I) {
577 Export* E = HashTab [I];
579 CHECK (J < ExpCount);
585 /* Sort them by name */
586 qsort (ExpPool, ExpCount, sizeof (Export*), CmpExpName);
591 void CheckExports (ExpCheckFunc F, void* Data)
592 /* Check if there are any unresolved symbols. On unresolved symbols, F is
593 * called (see the comments on ExpCheckFunc in the data section).
596 /* Create an export pool */
599 /* Check for symbol type mismatches */
602 /* Check for unresolved externals (check here for special bin formats) */
604 /* Print all open imports */
605 PrintUnresolved (F, Data);
611 void PrintExportMap (FILE* F)
612 /* Print an export map to the given file */
617 /* Print all exports */
619 for (I = 0; I < ExpCount; ++I) {
620 const Export* E = ExpPool [I];
622 /* Print unreferenced symbols only if explictly requested */
623 if (VerboseMap || E->ImpCount > 0 || IS_EXP_CONDES (E->Type)) {
625 "%-25s %06lX %c%c%c%c ",
628 E->ImpCount? 'R' : ' ',
629 IS_EXP_LABEL (E->Type)? 'L' : 'E',
630 IS_EXP_ZP (E->Type)? 'Z' : ' ',
631 IS_EXP_CONDES (E->Type)? 'I' : ' ');
643 void PrintImportMap (FILE* F)
644 /* Print an import map to the given file */
649 /* Loop over all exports */
650 for (I = 0; I < ExpCount; ++I) {
653 const Export* Exp = ExpPool [I];
655 /* Print the symbol only if there are imports, or if a verbose map
658 if (VerboseMap || Exp->ImpCount > 0) {
660 /* Print the export */
664 GetObjFileName (Exp->Obj));
666 /* Print all imports for this symbol */
670 /* Print the import */
673 GetObjFileName (Imp->Obj),
674 GetSourceFileName (Imp->Obj, Imp->Pos.Name),
687 void PrintExportLabels (FILE* F)
688 /* Print the exports in a VICE label file */
692 /* Print all exports */
693 for (I = 0; I < ExpCount; ++I) {
694 const Export* E = ExpPool [I];
695 fprintf (F, "al %06lX .%s\n", GetExportVal (E), E->Name);
701 void MarkExport (Export* E)
702 /* Mark the export */
704 E->Flags |= EXP_USERMARK;
709 void UnmarkExport (Export* E)
710 /* Remove the mark from the export */
712 E->Flags &= ~EXP_USERMARK;
717 int ExportHasMark (Export* E)
718 /* Return true if the export has a mark */
720 return (E->Flags & EXP_USERMARK) != 0;
725 void CircularRefError (const Export* E)
726 /* Print an error about a circular reference using to define the given export */
728 Error ("Circular reference for symbol `%s', %s(%lu)",
729 E->Name, GetSourceFileName (E->Obj, E->Pos.Name), E->Pos.Line);