]> git.sur5r.net Git - cc65/blob - src/ld65/mapfile.c
Use a string pool to reduce the memory footprint
[cc65] / src / ld65 / mapfile.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 mapfile.c                                 */
4 /*                                                                           */
5 /*                   Map file creation for the ld65 linker                   */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2003 Ullrich von Bassewitz                                       */
10 /*               Römerstrasse 52                                             */
11 /*               D-70794 Filderstadt                                         */
12 /* EMail:        uz@cc65.org                                                 */
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 <string.h>
38 #include <errno.h>
39
40 /* ld65 */
41 #include "config.h"
42 #include "dbginfo.h"
43 #include "dbgsyms.h"
44 #include "exports.h"
45 #include "global.h"
46 #include "error.h"
47 #include "mapfile.h"
48 #include "objdata.h"
49 #include "segments.h"
50 #include "spool.h"
51
52
53
54 /*****************************************************************************/
55 /*                                   Code                                    */
56 /*****************************************************************************/
57
58
59
60 void CreateMapFile (void)
61 /* Create a map file */
62 {
63     ObjData* O;
64     unsigned I;
65
66     /* Open the map file */
67     FILE* F = fopen (MapFileName, "w");
68     if (F == 0) {
69         Error ("Cannot create map file `%s': %s", MapFileName, strerror (errno));
70     }
71
72     /* Write a modules list */
73     fprintf (F, "Modules list:\n"
74                 "-------------\n");
75     O = ObjRoot;
76     while (O) {
77         if (O->Flags & OBJ_HAVEDATA) {
78             /* We've linked this module */
79             if (O->LibName) {
80                 /* The file is from a library */
81                 fprintf (F, "%s(%s):\n", O->LibName, GetObjFileName (O));
82             } else {
83                 fprintf (F, "%s:\n", GetObjFileName (O));
84             }
85             for (I = 0; I < O->SectionCount; ++I) {
86                 const Section* S = O->Sections [I];
87                 /* Don't include zero sized sections if not explicitly
88                  * requested
89                  */
90                 if (VerboseMap || S->Size > 0) {
91                     fprintf (F, "    %-15s   Offs = %06lX   Size = %06lX\n",
92                              GetString (S->Seg->Name), S->Offs, S->Size);
93                 }
94             }
95         }
96         O = O->Next;
97     }
98
99     /* Write the segment list */
100     fprintf (F, "\n\n"
101                 "Segment list:\n"
102                 "-------------\n");
103     PrintSegmentMap (F);
104
105     /* Write the exports list */
106     fprintf (F, "\n\n"
107                 "Exports list:\n"
108                 "-------------\n");
109     PrintExportMap (F);
110
111     /* Write the imports list */
112     fprintf (F, "\n\n"
113                 "Imports list:\n"
114                 "-------------\n");
115     PrintImportMap (F);
116
117     /* Close the file */
118     if (fclose (F) != 0) {
119         Error ("Error closing map file `%s': %s", MapFileName, strerror (errno));
120     }
121 }
122
123
124
125 void CreateLabelFile (void)
126 /* Create a label file */
127 {
128     ObjData* O;
129
130     /* Open the label file */
131     FILE* F = fopen (LabelFileName, "w");
132     if (F == 0) {
133         Error ("Cannot create label file `%s': %s", LabelFileName, strerror (errno));
134     }
135
136     /* Print the labels for the export symbols */
137     PrintExportLabels (F);
138
139     /* Print debug symbols from all modules we have linked into the output file */
140     O = ObjRoot;
141     while (O) {
142         if (O->Flags & OBJ_HAVEDATA) {
143             /* We've linked this module */
144             PrintDbgSymLabels (O, F);
145
146         }
147         O = O->Next;
148     }
149
150     /* If we should mark write protected areas as such, do it */
151     if (WProtSegs) {
152         SegDesc* S = SegDescList;
153         while (S) {
154             /* Is this segment write protected and contains data? */
155             if (S->Flags & SF_WPROT && S->Seg->Size > 0) {
156                 /* Write protect the memory area in VICE */
157                 fprintf (F, "wp %04lX %04lX\n",
158                          S->Seg->PC,
159                          S->Seg->PC + S->Seg->Size - 1);
160             }
161             /* Next segment */
162             S = S->Next;
163         }
164     }
165
166     /* Close the file */
167     if (fclose (F) != 0) {
168         Error ("Error closing map file `%s': %s", LabelFileName, strerror (errno));
169     }
170 }
171
172
173
174 void CreateDbgFile (void)
175 /* Create a debug info file */
176 {
177     ObjData* O;
178
179     /* Open the debug info file */
180     FILE* F = fopen (DbgFileName, "w");
181     if (F == 0) {
182         Error ("Cannot create label file `%s': %s", DbgFileName, strerror (errno));
183     }
184
185     /* Print line infos from all modules we have linked into the output file */
186     O = ObjRoot;
187     while (O) {
188         if (O->Flags & OBJ_HAVEDATA) {
189             /* We've linked this module */
190             PrintDbgInfo (O, F);
191
192         }
193         O = O->Next;
194     }
195
196     /* Close the file */
197     if (fclose (F) != 0) {
198         Error ("Error closing map file `%s': %s", DbgFileName, strerror (errno));
199     }
200 }
201
202
203