]> git.sur5r.net Git - cc65/blob - src/ld65/exports.c
Working on the condes feature
[cc65] / src / ld65 / exports.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 exports.c                                 */
4 /*                                                                           */
5 /*                   Exports handling for the ld65 linker                    */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998     Ullrich von Bassewitz                                        */
10 /*              Wacholderweg 14                                              */
11 /*              D-70597 Stuttgart                                            */
12 /* EMail:       uz@musoftware.de                                             */
13 /*                                                                           */
14 /*                                                                           */
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.                                    */
18 /*                                                                           */
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:                            */
22 /*                                                                           */
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              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39
40 /* common */
41 #include "check.h"
42 #include "coll.h"
43 #include "hashstr.h"
44 #include "symdefs.h"
45 #include "xmalloc.h"
46
47 /* ld65 */
48 #include "condes.h"
49 #include "error.h"
50 #include "fileio.h"
51 #include "global.h"
52 #include "objdata.h"
53 #include "expr.h"
54 #include "exports.h"
55
56
57
58 /*****************************************************************************/
59 /*                                   Data                                    */
60 /*****************************************************************************/
61
62
63
64 /* Hash table */
65 #define HASHTAB_SIZE    4081
66 static Export*          HashTab [HASHTAB_SIZE];
67
68 /* Import management variables */
69 static unsigned         ImpCount = 0;           /* Import count */
70 static unsigned         ImpOpen  = 0;           /* Count of open imports */
71
72 /* Export management variables */
73 static unsigned         ExpCount = 0;           /* Export count */
74 static Export**         ExpPool  = 0;           /* Exports array */
75
76 /* Defines for the flags in Export */
77 #define EXP_USERMARK    0x0001
78
79
80
81 /*****************************************************************************/
82 /*                              Import handling                              */
83 /*****************************************************************************/
84
85
86
87 static Export* NewExport (unsigned char Type, const char* Name, ObjData* Obj);
88 /* Create a new export and initialize it */
89
90
91
92 static Import* NewImport (unsigned char Type, ObjData* Obj)
93 /* Create a new import and initialize it */
94 {
95     /* Allocate memory */
96     Import* I = xmalloc (sizeof (Import));
97
98     /* Initialize the fields */
99     I->Next     = 0;
100     I->Obj      = Obj;
101     I->V.Name   = 0;
102     I->Type     = Type;
103
104     /* Return the new structure */
105     return I;
106 }
107
108
109
110 void InsertImport (Import* I)
111 /* Insert an import into the table */
112 {
113     Export* E;
114     unsigned HashVal;
115
116     /* As long as the import is not inserted, V.Name is valid */
117     const char* Name = I->V.Name;
118
119     /* Create a hash value for the given name */
120     HashVal = HashStr (Name) % HASHTAB_SIZE;
121
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);
126         ++ExpCount;
127     } else {
128         E = HashTab [HashVal];
129         while (1) {
130             if (strcmp (E->Name, Name) == 0) {
131                 /* We have an entry, L points to it */
132                 break;
133             }
134             if (E->Next == 0) {
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 */
139                 break;
140             } else {
141                 E = E->Next;
142             }
143         }
144     }
145
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.
148      */
149     I->V.Exp   = E;
150     I->Next    = E->ImpList;
151     E->ImpList = I;
152     E->ImpCount++;
153     ++ImpCount;                 /* Total import count */
154     if (E->Expr == 0) {
155         /* This is a dummy export */
156         ++ImpOpen;
157     }
158
159     /* Now free the name since it's no longer needed */
160     xfree (Name);
161 }
162
163
164
165 Import* ReadImport (FILE* F, ObjData* Obj)
166 /* Read an import from a file and return it */
167 {
168     Import* I;
169
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);
174     }
175
176     /* Create a new import */
177     I = NewImport (Type, Obj);
178
179     /* Read the name */
180     I->V.Name = ReadStr (F);
181
182     /* Read the file position */
183     ReadFilePos (F, &I->Pos);
184
185     /* Return the new import */
186     return I;
187 }
188
189
190
191 /*****************************************************************************/
192 /*                                   Code                                    */
193 /*****************************************************************************/
194
195
196
197 static Export* NewExport (unsigned char Type, const char* Name, ObjData* Obj)
198 /* Create a new export and initialize it */
199 {
200     /* Allocate memory */
201     Export* E = xmalloc (sizeof (Export));
202
203     /* Initialize the fields */
204     E->Next     = 0;
205     E->Flags    = 0;
206     E->Obj      = Obj;
207     E->ImpCount = 0;
208     E->ImpList  = 0;
209     E->Expr     = 0;
210     E->Type     = Type;
211     memset (E->ConDes, 0, sizeof (E->ConDes));
212     if (Name) {
213         E->Name = xstrdup (Name);
214     } else {
215         /* Name will get added later */
216         E->Name = 0;
217     }
218
219     /* Return the new entry */
220     return E;
221 }
222
223
224
225 void InsertExport (Export* E)
226 /* Insert an exported identifier and check if it's already in the list */
227 {
228     Export* L;
229     Export* Last;
230     Import* Imp;
231     unsigned HashVal;
232
233     /* Insert the export into any condes tables if needed */
234     if (IS_EXP_CONDES (E->Type)) {
235         ConDesAddExport (E);
236     }
237
238     /* Create a hash value for the given name */
239     HashVal = HashStr (E->Name) % HASHTAB_SIZE;
240
241     /* Search through the list in that slot */
242     if (HashTab [HashVal] == 0) {
243         /* The slot is empty */
244         HashTab [HashVal] = E;
245         ++ExpCount;
246     } else {
247
248         Last = 0;
249         L = HashTab [HashVal];
250         do {
251             if (strcmp (L->Name, E->Name) == 0) {
252                 /* This may be an unresolved external */
253                 if (L->Expr == 0) {
254
255                     /* This *is* an unresolved external */
256                     E->Next     = L->Next;
257                     E->ImpCount = L->ImpCount;
258                     E->ImpList  = L->ImpList;
259                     if (Last) {
260                         Last->Next = E;
261                     } else {
262                         HashTab [HashVal] = E;
263                     }
264                     ImpOpen -= E->ImpCount;     /* Decrease open imports now */
265                     xfree (L);
266                     /* We must run through the import list and change the
267                      * export pointer now.
268                      */
269                     Imp = E->ImpList;
270                     while (Imp) {
271                         Imp->V.Exp = E;
272                         Imp = Imp->Next;
273                     }
274                 } else {
275                     /* Duplicate entry, ignore it */
276                     Warning ("Duplicate external identifier: `%s'", L->Name);
277                 }
278                 return;
279             }
280             Last = L;
281             L = L->Next;
282
283         } while (L);
284
285         /* Insert export at end of queue */
286         Last->Next = E;
287         ++ExpCount;
288     }
289 }
290
291
292
293 Export* ReadExport (FILE* F, ObjData* O)
294 /* Read an export from a file */
295 {
296     unsigned char Type;
297     unsigned      ConDesCount;
298     Export* E;
299
300     /* Read the type */
301     Type = Read8 (F);
302
303     /* Create a new export without a name */
304     E = NewExport (Type, 0, O);
305
306     /* Read the constructor/destructor decls if we have any */
307     ConDesCount = GET_EXP_CONDES_COUNT (Type);
308     if (ConDesCount > 0) {
309
310         unsigned char ConDes[CD_TYPE_COUNT];
311         unsigned I;
312
313         /* Read the data into temp storage */
314         ReadData (F, ConDes, ConDesCount);
315
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.
321          */
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;
326         }
327     }
328
329     /* Read the name */
330     E->Name = ReadStr (F);
331
332     /* Read the value */
333     if (IS_EXP_EXPR (Type)) {
334         E->Expr = ReadExpr (F, O);
335     } else {
336         E->Expr = LiteralExpr (Read32 (F), O);
337     }
338
339     /* Last is the file position where the definition was done */
340     ReadFilePos (F, &E->Pos);
341
342     /* Return the new export */
343     return E;
344 }
345
346
347
348 Export* CreateConstExport (const char* Name, long Value)
349 /* Create an export for a literal date */
350 {
351     /* Create a new export */
352     Export* E = NewExport (EXP_ABS, Name, 0);
353
354     /* Assign the value */
355     E->Expr = LiteralExpr (Value, 0);
356
357     /* Insert the export */
358     InsertExport (E);
359
360     /* Return the new export */
361     return E;
362 }
363
364
365
366 Export* CreateMemExport (const char* Name, Memory* Mem, unsigned long Offs)
367 /* Create an relative export for a memory area offset */
368 {
369     /* Create a new export */
370     Export* E = NewExport (EXP_ABS, Name, 0);
371
372     /* Assign the value */
373     E->Expr = MemExpr (Mem, Offs, 0);
374
375     /* Insert the export */
376     InsertExport (E);
377
378     /* Return the new export */
379     return E;
380 }
381
382
383
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.
387  */
388 {
389     /* Get a pointer to the list with the symbols hash value */
390     Export* L = HashTab [HashStr (Name) % HASHTAB_SIZE];
391     while (L) {
392         /* Search through the list in that slot */
393         if (strcmp (L->Name, Name) == 0) {
394             /* Entry found */
395             return L;
396         }
397         L = L->Next;
398     }
399
400     /* Not found */
401     return 0;
402 }
403
404
405
406 int IsUnresolved (const char* Name)
407 /* Check if this symbol is an unresolved export */
408 {
409     /* Find the export */
410     Export* E = FindExport (Name);
411
412     /* Check if it's unresolved */
413     return E != 0 && E->Expr == 0;
414 }
415
416
417
418 int IsConstExport (const Export* E)
419 /* Return true if the expression associated with this export is const */
420 {
421     if (E->Expr == 0) {
422         /* External symbols cannot be const */
423         return 0;
424     } else {
425         return IsConstExpr (E->Expr);
426     }
427 }
428
429
430
431 long GetExportVal (const Export* E)
432 /* Get the value of this export */
433 {
434     if (E->Expr == 0) {
435         /* OOPS */
436         Internal ("`%s' is an undefined external", E->Name);
437     }
438     return GetExprVal (E->Expr);
439 }
440
441
442
443 static void CheckSymType (const Export* E)
444 /* Check the types for one export */
445 {
446     /* External with matching imports */
447     Import* Imp = E->ImpList;
448     int ZP = IS_EXP_ZP (E->Type);
449     while (Imp) {
450         if (ZP != IS_IMP_ZP (Imp->Type)) {
451             /* Export is ZP, import is abs or the other way round */
452             if (E->Obj) {
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],
458                          Imp->Pos.Line);
459             } else {
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],
463                          Imp->Pos.Line);
464             }
465         }
466         Imp = Imp->Next;
467     }
468 }
469
470
471
472 static void CheckSymTypes (void)
473 /* Check for symbol tape mismatches */
474 {
475     unsigned I;
476
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 */
482             CheckSymType (E);
483         }
484     }
485 }
486
487
488
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).
492  */
493 {
494     unsigned I;
495
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;
502             fprintf (stderr,
503                      "Unresolved external `%s' referenced in:\n",
504                      E->Name);
505             while (Imp) {
506                 const char* Name = Imp->Obj->Files [Imp->Pos.Name];
507                 fprintf (stderr, "  %s(%lu)\n", Name, Imp->Pos.Line);
508                 Imp = Imp->Next;
509             }
510         }
511     }
512 }
513
514
515
516 static int CmpExpName (const void* K1, const void* K2)
517 /* Compare function for qsort */
518 {
519     return strcmp ((*(Export**)K1)->Name, (*(Export**)K2)->Name);
520 }
521
522
523
524 static void CreateExportPool (void)
525 /* Create an array with pointer to all exports */
526 {
527     unsigned I, J;
528
529     /* Allocate memory */
530     if (ExpPool) {
531         xfree (ExpPool);
532     }
533     ExpPool = xmalloc (ExpCount * sizeof (Export*));
534
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];
538         while (E) {
539             CHECK (J < ExpCount);
540             ExpPool [J++] = E;
541             E = E->Next;
542         }
543     }
544
545     /* Sort them by name */
546     qsort (ExpPool, ExpCount, sizeof (Export*), CmpExpName);
547 }
548
549
550
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).
554  */
555 {
556     /* Create an export pool */
557     CreateExportPool ();
558
559     /* Check for symbol type mismatches */
560     CheckSymTypes ();
561
562     /* Check for unresolved externals (check here for special bin formats) */
563     if (ImpOpen != 0) {
564         /* Print all open imports */
565         PrintUnresolved (F, Data);
566     }
567 }
568
569
570
571 void PrintExportMap (FILE* F)
572 /* Print an export map to the given file */
573 {
574     unsigned I;
575     unsigned Count;
576
577     /* Print all exports */
578     Count = 0;
579     for (I = 0; I < ExpCount; ++I) {
580         const Export* E = ExpPool [I];
581
582         /* Print unreferenced symbols only if explictly requested */
583         if (VerboseMap || E->ImpCount > 0) {
584             fprintf (F,
585                      "%-25s %06lX %c%c%c   ",
586                      E->Name,
587                      GetExportVal (E),
588                      E->ImpCount? 'R' : ' ',
589                      IS_EXP_ZP (E->Type)? 'Z' : ' ',
590                      IS_EXP_CONDES (E->Type)? 'I' : ' ');
591             if (++Count == 2) {
592                 Count = 0;
593                 fprintf (F, "\n");
594             }
595         }
596     }
597     fprintf (F, "\n");
598 }
599
600
601
602 void PrintImportMap (FILE* F)
603 /* Print an import map to the given file */
604 {
605     unsigned I;
606     const Import* Imp;
607
608     /* Loop over all exports */
609     for (I = 0; I < ExpCount; ++I) {
610
611         /* Get the export */
612         const Export* Exp = ExpPool [I];
613
614         /* Print the symbol only if there are imports, or if a verbose map
615          * file is requested.
616          */
617         if (VerboseMap || Exp->ImpCount > 0) {
618
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
621              * generated symbol.
622              */
623             const char* ObjName = (Exp->Obj != 0)? Exp->Obj->Name : "linker generated";
624
625             /* Print the export */
626             fprintf (F,
627                      "%s (%s):\n",
628                      Exp->Name,
629                      ObjName);
630
631             /* Print all imports for this symbol */
632             Imp = Exp->ImpList;
633             while (Imp) {
634
635                 /* Print the import */
636                 fprintf (F,
637                          "    %-25s %s(%lu)\n",
638                          Imp->Obj->Name,
639                          Imp->Obj->Files [Imp->Pos.Name],
640                          Imp->Pos.Line);
641
642                 /* Next import */
643                 Imp = Imp->Next;
644             }
645         }
646     }
647     fprintf (F, "\n");
648 }
649
650
651
652 void PrintExportLabels (FILE* F)
653 /* Print the exports in a VICE label file */
654 {
655     unsigned I;
656
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);
661     }
662 }
663
664
665
666 void MarkExport (Export* E)
667 /* Mark the export */
668 {
669     E->Flags |= EXP_USERMARK;
670 }
671
672
673
674 void UnmarkExport (Export* E)
675 /* Remove the mark from the export */
676 {
677     E->Flags &= ~EXP_USERMARK;
678 }
679
680
681
682 int ExportHasMark (Export* E)
683 /* Return true if the export has a mark */
684 {
685     return (E->Flags & EXP_USERMARK) != 0;
686 }
687
688
689
690 void CircularRefError (const Export* E)
691 /* Print an error about a circular reference using to define the given export */
692 {
693     Error ("Circular reference for symbol `%s', %s(%lu)",
694            E->Name, E->Obj->Files [E->Pos.Name], E->Pos.Line);
695 }
696
697
698