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