]> git.sur5r.net Git - cc65/blob - src/ld65/exports.c
Add #pragma charmap()
[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",
174                GetObjFileName (Obj), Type);
175     }
176
177     /* Create a new import */
178     I = NewImport (Type, Obj);
179
180     /* Read the name */
181     I->V.Name = ReadStr (F);
182
183     /* Read the file position */
184     ReadFilePos (F, &I->Pos);
185
186     /* Return the new import */
187     return I;
188 }
189
190
191
192 /*****************************************************************************/
193 /*                                   Code                                    */
194 /*****************************************************************************/
195
196
197
198 static Export* NewExport (unsigned char Type, const char* Name, ObjData* Obj)
199 /* Create a new export and initialize it */
200 {
201     /* Allocate memory */
202     Export* E = xmalloc (sizeof (Export));
203
204     /* Initialize the fields */
205     E->Next     = 0;
206     E->Flags    = 0;
207     E->Obj      = Obj;
208     E->ImpCount = 0;
209     E->ImpList  = 0;
210     E->Expr     = 0;
211     E->Type     = Type;
212     memset (E->ConDes, 0, sizeof (E->ConDes));
213     if (Name) {
214         E->Name = xstrdup (Name);
215     } else {
216         /* Name will get added later */
217         E->Name = 0;
218     }
219
220     /* Return the new entry */
221     return E;
222 }
223
224
225
226 void InsertExport (Export* E)
227 /* Insert an exported identifier and check if it's already in the list */
228 {
229     Export* L;
230     Export* Last;
231     Import* Imp;
232     unsigned HashVal;
233
234     /* Insert the export into any condes tables if needed */
235     if (IS_EXP_CONDES (E->Type)) {
236         ConDesAddExport (E);
237     }
238
239     /* Create a hash value for the given name */
240     HashVal = HashStr (E->Name) % HASHTAB_SIZE;
241
242     /* Search through the list in that slot */
243     if (HashTab [HashVal] == 0) {
244         /* The slot is empty */
245         HashTab [HashVal] = E;
246         ++ExpCount;
247     } else {
248
249         Last = 0;
250         L = HashTab [HashVal];
251         do {
252             if (strcmp (L->Name, E->Name) == 0) {
253                 /* This may be an unresolved external */
254                 if (L->Expr == 0) {
255
256                     /* This *is* an unresolved external */
257                     E->Next     = L->Next;
258                     E->ImpCount = L->ImpCount;
259                     E->ImpList  = L->ImpList;
260                     if (Last) {
261                         Last->Next = E;
262                     } else {
263                         HashTab [HashVal] = E;
264                     }
265                     ImpOpen -= E->ImpCount;     /* Decrease open imports now */
266                     xfree (L);
267                     /* We must run through the import list and change the
268                      * export pointer now.
269                      */
270                     Imp = E->ImpList;
271                     while (Imp) {
272                         Imp->V.Exp = E;
273                         Imp = Imp->Next;
274                     }
275                 } else {
276                     /* Duplicate entry, ignore it */
277                     Warning ("Duplicate external identifier: `%s'", L->Name);
278                 }
279                 return;
280             }
281             Last = L;
282             L = L->Next;
283
284         } while (L);
285
286         /* Insert export at end of queue */
287         Last->Next = E;
288         ++ExpCount;
289     }
290 }
291
292
293
294 Export* ReadExport (FILE* F, ObjData* O)
295 /* Read an export from a file */
296 {
297     unsigned char Type;
298     unsigned      ConDesCount;
299     Export* E;
300
301     /* Read the type */
302     Type = Read8 (F);
303
304     /* Create a new export without a name */
305     E = NewExport (Type, 0, O);
306
307     /* Read the constructor/destructor decls if we have any */
308     ConDesCount = GET_EXP_CONDES_COUNT (Type);
309     if (ConDesCount > 0) {
310
311         unsigned char ConDes[CD_TYPE_COUNT];
312         unsigned I;
313
314         /* Read the data into temp storage */
315         ReadData (F, ConDes, ConDesCount);
316
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.
322          */
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;
327         }
328     }
329
330     /* Read the name */
331     E->Name = ReadStr (F);
332
333     /* Read the value */
334     if (IS_EXP_EXPR (Type)) {
335         E->Expr = ReadExpr (F, O);
336     } else {
337         E->Expr = LiteralExpr (Read32 (F), O);
338     }
339
340     /* Last is the file position where the definition was done */
341     ReadFilePos (F, &E->Pos);
342
343     /* Return the new export */
344     return E;
345 }
346
347
348
349 Export* CreateConstExport (const char* Name, long Value)
350 /* Create an export for a literal date */
351 {
352     /* Create a new export */
353     Export* E = NewExport (EXP_ABS | EXP_CONST | EXP_EQUATE, Name, 0);
354
355     /* Assign the value */
356     E->Expr = LiteralExpr (Value, 0);
357
358     /* Insert the export */
359     InsertExport (E);
360
361     /* Return the new export */
362     return E;
363 }
364
365
366
367 Export* CreateMemExport (const char* Name, Memory* Mem, unsigned long Offs)
368 /* Create an relative export for a memory area offset */
369 {
370     /* Create a new export */
371     Export* E = NewExport (EXP_ABS | EXP_EXPR | EXP_LABEL, Name, 0);
372
373     /* Assign the value */
374     E->Expr = MemExpr (Mem, Offs, 0);
375
376     /* Insert the export */
377     InsertExport (E);
378
379     /* Return the new export */
380     return E;
381 }
382
383
384
385 Export* CreateSegExport (const char* Name, Section* Sec, unsigned long Offs)
386 /* Create a relative export to a segment (section) */
387 {
388     /* Create a new export */
389     Export* E = NewExport (EXP_ABS | EXP_EXPR | EXP_LABEL, Name, 0);
390
391     /* Assign the value */
392     E->Expr = SegExpr (Sec, Offs, 0);
393
394     /* Insert the export */
395     InsertExport (E);
396
397     /* Return the new export */
398     return E;
399 }
400
401
402
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.
406  */
407 {
408     /* Get a pointer to the list with the symbols hash value */
409     Export* L = HashTab [HashStr (Name) % HASHTAB_SIZE];
410     while (L) {
411         /* Search through the list in that slot */
412         if (strcmp (L->Name, Name) == 0) {
413             /* Entry found */
414             return L;
415         }
416         L = L->Next;
417     }
418
419     /* Not found */
420     return 0;
421 }
422
423
424
425 int IsUnresolved (const char* Name)
426 /* Check if this symbol is an unresolved export */
427 {
428     /* Find the export */
429     return IsUnresolvedExport (FindExport (Name));
430 }
431
432
433
434 int IsUnresolvedExport (const Export* E)
435 /* Return true if the given export is unresolved */
436 {
437     /* Check if it's unresolved */
438     return E != 0 && E->Expr == 0;
439 }
440
441
442
443 int IsConstExport (const Export* E)
444 /* Return true if the expression associated with this export is const */
445 {
446     if (E->Expr == 0) {
447         /* External symbols cannot be const */
448         return 0;
449     } else {
450         return IsConstExpr (E->Expr);
451     }
452 }
453
454
455
456 long GetExportVal (const Export* E)
457 /* Get the value of this export */
458 {
459     if (E->Expr == 0) {
460         /* OOPS */
461         Internal ("`%s' is an undefined external", E->Name);
462     }
463     return GetExprVal (E->Expr);
464 }
465
466
467
468 static void CheckSymType (const Export* E)
469 /* Check the types for one export */
470 {
471     /* External with matching imports */
472     Import* Imp = E->ImpList;
473     int ZP = IS_EXP_ZP (E->Type);
474     while (Imp) {
475         if (ZP != IS_IMP_ZP (Imp->Type)) {
476             /* Export is ZP, import is abs or the other way round */
477             if (E->Obj) {
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),
483                          Imp->Pos.Line);
484             } else {
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),
488                          Imp->Pos.Line);
489             }
490         }
491         Imp = Imp->Next;
492     }
493 }
494
495
496
497 static void CheckSymTypes (void)
498 /* Check for symbol tape mismatches */
499 {
500     unsigned I;
501
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 */
507             CheckSymType (E);
508         }
509     }
510 }
511
512
513
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).
517  */
518 {
519     unsigned I;
520
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;
527             fprintf (stderr,
528                      "Unresolved external `%s' referenced in:\n",
529                      E->Name);
530             while (Imp) {
531                 const char* Name = GetSourceFileName (Imp->Obj, Imp->Pos.Name);
532                 fprintf (stderr, "  %s(%lu)\n", Name, Imp->Pos.Line);
533                 Imp = Imp->Next;
534             }
535         }
536     }
537 }
538
539
540
541 static int CmpExpName (const void* K1, const void* K2)
542 /* Compare function for qsort */
543 {
544     return strcmp ((*(Export**)K1)->Name, (*(Export**)K2)->Name);
545 }
546
547
548
549 static void CreateExportPool (void)
550 /* Create an array with pointer to all exports */
551 {
552     unsigned I, J;
553
554     /* Allocate memory */
555     if (ExpPool) {
556         xfree (ExpPool);
557     }
558     ExpPool = xmalloc (ExpCount * sizeof (Export*));
559
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];
563         while (E) {
564             CHECK (J < ExpCount);
565             ExpPool [J++] = E;
566             E = E->Next;
567         }
568     }
569
570     /* Sort them by name */
571     qsort (ExpPool, ExpCount, sizeof (Export*), CmpExpName);
572 }
573
574
575
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).
579  */
580 {
581     /* Create an export pool */
582     CreateExportPool ();
583
584     /* Check for symbol type mismatches */
585     CheckSymTypes ();
586
587     /* Check for unresolved externals (check here for special bin formats) */
588     if (ImpOpen != 0) {
589         /* Print all open imports */
590         PrintUnresolved (F, Data);
591     }
592 }
593
594
595
596 void PrintExportMap (FILE* F)
597 /* Print an export map to the given file */
598 {
599     unsigned I;
600     unsigned Count;
601
602     /* Print all exports */
603     Count = 0;
604     for (I = 0; I < ExpCount; ++I) {
605         const Export* E = ExpPool [I];
606
607         /* Print unreferenced symbols only if explictly requested */
608         if (VerboseMap || E->ImpCount > 0 || IS_EXP_CONDES (E->Type)) {
609             fprintf (F,
610                      "%-25s %06lX %c%c%c%c   ",
611                      E->Name,
612                      GetExportVal (E),
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' : ' ');
617             if (++Count == 2) {
618                 Count = 0;
619                 fprintf (F, "\n");
620             }
621         }
622     }
623     fprintf (F, "\n");
624 }
625
626
627
628 void PrintImportMap (FILE* F)
629 /* Print an import map to the given file */
630 {
631     unsigned I;
632     const Import* Imp;
633
634     /* Loop over all exports */
635     for (I = 0; I < ExpCount; ++I) {
636
637         /* Get the export */
638         const Export* Exp = ExpPool [I];
639
640         /* Print the symbol only if there are imports, or if a verbose map
641          * file is requested.
642          */
643         if (VerboseMap || Exp->ImpCount > 0) {
644
645             /* Print the export */
646             fprintf (F,
647                      "%s (%s):\n",
648                      Exp->Name,
649                      GetObjFileName (Exp->Obj));
650
651             /* Print all imports for this symbol */
652             Imp = Exp->ImpList;
653             while (Imp) {
654
655                 /* Print the import */
656                 fprintf (F,
657                          "    %-25s %s(%lu)\n",
658                          GetObjFileName (Imp->Obj),
659                          GetSourceFileName (Imp->Obj, Imp->Pos.Name),
660                          Imp->Pos.Line);
661
662                 /* Next import */
663                 Imp = Imp->Next;
664             }
665         }
666     }
667     fprintf (F, "\n");
668 }
669
670
671
672 void PrintExportLabels (FILE* F)
673 /* Print the exports in a VICE label file */
674 {
675     unsigned I;
676
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);
681     }
682 }
683
684
685
686 void MarkExport (Export* E)
687 /* Mark the export */
688 {
689     E->Flags |= EXP_USERMARK;
690 }
691
692
693
694 void UnmarkExport (Export* E)
695 /* Remove the mark from the export */
696 {
697     E->Flags &= ~EXP_USERMARK;
698 }
699
700
701
702 int ExportHasMark (Export* E)
703 /* Return true if the export has a mark */
704 {
705     return (E->Flags & EXP_USERMARK) != 0;
706 }
707
708
709
710 void CircularRefError (const Export* E)
711 /* Print an error about a circular reference using to define the given export */
712 {
713     Error ("Circular reference for symbol `%s', %s(%lu)",
714            E->Name, GetSourceFileName (E->Obj, E->Pos.Name), E->Pos.Line);
715 }
716
717
718