]> git.sur5r.net Git - cc65/blob - src/ld65/dbgfile.c
Merge remote-tracking branch 'upstream/master' into a5200
[cc65] / src / ld65 / dbgfile.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 dbgfile.c                                 */
4 /*                                                                           */
5 /*                  Debug file creation for the ld65 linker                  */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2003-2011, Ullrich von Bassewitz                                      */
10 /*                Roemerstrasse 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 "dbgfile.h"
42 #include "dbgsyms.h"
43 #include "error.h"
44 #include "fileinfo.h"
45 #include "global.h"
46 #include "library.h"
47 #include "lineinfo.h"
48 #include "scopes.h"
49 #include "segments.h"
50 #include "span.h"
51 #include "tpool.h"
52
53
54
55 /*****************************************************************************/
56 /*                                   Code                                    */
57 /*****************************************************************************/
58
59
60
61 static void AssignIds (void)
62 /* Assign the base ids for debug info output. Within each module, many of the
63  * items are addressed by ids which are actually the indices of the items in
64  * the collections. To make them unique, we must assign a unique base to each
65  * range.
66  */
67 {
68     /* Walk over all modules */
69     unsigned I;
70     unsigned HLLSymBaseId = 0;
71     unsigned ScopeBaseId  = 0;
72     unsigned SpanBaseId   = 0;
73     unsigned SymBaseId    = 0;
74     for (I = 0; I < CollCount (&ObjDataList); ++I) {
75
76         /* Get this module */
77         ObjData* O = CollAt (&ObjDataList, I);
78
79         /* Assign the module id */
80         O->Id = I;
81
82         /* Assign base ids */
83         O->HLLSymBaseId = HLLSymBaseId;
84         O->ScopeBaseId  = ScopeBaseId;
85         O->SpanBaseId   = SpanBaseId;
86         O->SymBaseId    = SymBaseId;
87
88         /* Bump the base ids */
89         HLLSymBaseId  += CollCount (&O->HLLDbgSyms);
90         ScopeBaseId   += CollCount (&O->Scopes);
91         SpanBaseId    += CollCount (&O->Spans);
92         SymBaseId     += CollCount (&O->DbgSyms);
93     }
94
95     /* Assign the ids to the file infos */
96     AssignFileInfoIds ();
97
98     /* Assign the ids to line infos */
99     AssignLineInfoIds ();
100 }
101
102
103
104 void CreateDbgFile (void)
105 /* Create a debug info file */
106 {
107     /* Open the debug info file */
108     FILE* F = fopen (DbgFileName, "w");
109     if (F == 0) {
110         Error ("Cannot create debug file `%s': %s", DbgFileName, strerror (errno));
111     }
112
113     /* Output version information */
114     fprintf (F, "version\tmajor=2,minor=0\n");
115
116     /* Output a line with the item numbers so the debug info module is able
117      * to preallocate the required memory.
118      */
119     fprintf (
120         F,
121         "info\tcsym=%u,file=%u,lib=%u,line=%u,mod=%u,scope=%u,seg=%u,span=%u,sym=%u,type=%u\n",
122         HLLDbgSymCount (),
123         FileInfoCount (),
124         LibraryCount (),
125         LineInfoCount (),
126         ObjDataCount (),
127         ScopeCount (),
128         SegmentCount (),
129         SpanCount (),
130         DbgSymCount (),
131         TypeCount ()
132     );
133
134     /* Assign the ids to the items */
135     AssignIds ();
136
137     /* Output high level language symbols */
138     PrintHLLDbgSyms (F);
139
140     /* Output files */
141     PrintDbgFileInfo (F);
142
143     /* Output libraries */
144     PrintDbgLibraries (F);
145
146     /* Output line info */
147     PrintDbgLineInfo (F);
148
149     /* Output modules */
150     PrintDbgModules (F);
151
152     /* Output the segment info */
153     PrintDbgSegments (F);
154
155     /* Output spans */
156     PrintDbgSpans (F);
157
158     /* Output scopes */
159     PrintDbgScopes (F);
160
161     /* Output symbols */
162     PrintDbgSyms (F);
163
164     /* Output types */
165     PrintDbgTypes (F);
166
167     /* Close the file */
168     if (fclose (F) != 0) {
169         Error ("Error closing debug file `%s': %s", DbgFileName, strerror (errno));
170     }
171 }