]> git.sur5r.net Git - cc65/blob - src/ld65/dbgfile.c
Central management of the debug info base ids.
[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 "lineinfo.h"
47 #include "scopes.h"
48 #include "segments.h"
49
50
51
52 /*****************************************************************************/
53 /*                                   Code                                    */
54 /*****************************************************************************/
55
56
57
58 static void AssignBaseIds (void)
59 /* Assign the base ids for debug info output. Within each module, many of the
60  * items are addressed by ids which are actually the indices of the items in
61  * the collections. To make them unique, we must assign a unique base to each
62  * range.
63  */
64 {
65     unsigned I;
66
67     /* Walk over all modules */
68     unsigned FileBaseId  = 0;
69     unsigned SymBaseId   = 0;
70     unsigned ScopeBaseId = 0;
71     for (I = 0; I < CollCount (&ObjDataList); ++I) {
72
73         /* Get this module */
74         ObjData* O = CollAt (&ObjDataList, I);
75
76         /* Assign ids */
77         O->FileBaseId  = FileBaseId;
78         O->SymBaseId   = SymBaseId;
79         O->ScopeBaseId = ScopeBaseId;
80
81         /* Bump the base ids */
82         FileBaseId    += CollCount (&O->Files);
83         SymBaseId     += CollCount (&O->DbgSyms);
84         ScopeBaseId   += CollCount (&O->Scopes);
85     }
86 }
87
88
89
90 void CreateDbgFile (void)
91 /* Create a debug info file */
92 {
93     unsigned I;
94
95     /* Open the debug info file */
96     FILE* F = fopen (DbgFileName, "w");
97     if (F == 0) {
98         Error ("Cannot create debug file `%s': %s", DbgFileName, strerror (errno));
99     }
100
101     /* Output version information */
102     fprintf (F, "version\tmajor=1,minor=2\n");
103
104     /* Assign the base ids to the modules */
105     AssignBaseIds ();
106
107     /* Output modules */             
108     for (I = 0; I < CollCount (&ObjDataList); ++I) {
109
110         /* Get this object file */
111         const ObjData* O = CollConstAt (&ObjDataList, I);
112
113         /* The main source file is the one at index zero */
114         const FileInfo* Source = CollConstAt (&O->Files, 0);
115
116         /* Output the module line */
117         fprintf (F,
118                  "mod\tid=%u,name=\"%s\",file=%u",
119                  I,
120                  GetObjFileName (O),
121                  Source->Id);
122
123         /* Add library if any */
124         if (O->LibName != INVALID_STRING_ID) {
125             fprintf (F,
126                      ",lib=\"%s\",mtime=0x%08lX",
127                      GetString (O->LibName),
128                      O->MTime);
129         }
130
131         /* Terminate the output line */
132         fputc ('\n', F);
133     }
134
135     /* Output the segment info */
136     PrintDbgSegments (F);
137
138     /* Output files */
139     PrintDbgFileInfo (F);
140
141     /* Output line info */
142     PrintDbgLineInfo (F);
143
144     /* Output symbols */
145     PrintDbgSyms (F);
146
147     /* Output scopes */
148     PrintDbgScopes (F);
149
150     /* Close the file */
151     if (fclose (F) != 0) {
152         Error ("Error closing debug file `%s': %s", DbgFileName, strerror (errno));
153     }
154 }
155
156
157