Debian Codegraph @

cc/ccom/dwarf.c

raw symbols SCIP
1 /* $Id: dwarf.c,v 1.2 2016/03/08 18:17:45 ragge Exp $ */
2
3 /*
4 * Copyright (c) 2016 Anders Magnusson ([email protected]).
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 /*
29 * Expect just ELF for now.
30 */
31 #include "pass1.h"
32
33 #ifdef DWARF
34
35 #include <sys/param.h>
36 #include <unistd.h>
37
38 #include "dwarf.h"
39
40 /*
41 * Basic DWARF definitions, from specification.
42 */
43 #define DVERSION 3 /* dwarf version */
44 #define DLABEL ".LD%d"
45
46 #define DW64 (SZPOINT(VOID)/SZCHAR == 8)
47
48 static int dwseg = -1;
49 enum { DINFO, DABBREV, DSTR };
50 static char *segn[] = { ".debug_info", ".debug_abbrev", ".debug_str" };
51
52 static int debug_info_sz, debug_info_sz_lbl;
53 static int debug_abbrev_beg_lbl;
54
55 static int dwcfl, dwbtext, dwetext;
56
57 static char *dwname;
58
59 static int
60 dwlab(void)
61 {
62 static int dwlbl;
63 return dwlbl++;
64 }
65
66 static void
67 dwarfseg(int seg)
68 {
69 if (seg == dwseg)
70 return;
71 /* XXX target code? */
72 printf("\t.section\t%s,\"\",@progbits\n", segn[seg]);
73 lastloc = NOSEG;
74 dwseg = seg;
75 }
76
77 static void
78 dwslab(int seg, int lbl)
79 {
80 dwarfseg(seg);
81 printf(PRTPREF DLABEL ":\n", lbl);
82 }
83
84 static void
85 p1b(int v)
86 {
87 printf(PRTPREF "%s 0x%x\n", astypnames[CHAR], v);
88 }
89
90 static void
91 p2w(int v)
92 {
93 printf(PRTPREF "%s 0x%x\n", astypnames[SHORT], v);
94 }
95
96 static int
97 leb128(int d)
98 {
99 if (d > 127)
100 cerror("FIXME: leb128 > 127");
101 p1b(d);
102 return 1;
103 }
104
105 static void
106 al128(int d)
107 {
108 dwarfseg(DABBREV);
109 leb128(d);
110 }
111
112 static void
113 apair(int d, int e)
114 {
115 dwarfseg(DABBREV);
116 leb128(d);
117 leb128(e);
118 }
119
120 static void
121 strng(char *s)
122 {
123 printf(PRTPREF "\t.ascii \"%s\\0\"\n", s); /* XXX common code? */
124 }
125
126 static void
127 istring(char *str)
128 {
129 dwarfseg(DINFO);
130 strng(str);
131 debug_info_sz += (strlen(str) + 1);
132 }
133
134 static void
135 dwslabstr(int lbl, char *s)
136 {
137 dwslab(DSTR, lbl);
138 strng(s);
139 }
140
141 static void
142 ilbl(int l)
143 {
144 dwarfseg(DINFO);
145 printf(PRTPREF "%s " DLABEL "\n", astypnames[LONG], l);
146 debug_info_sz += (DW64 * 4 + 4);
147 }
148
149 static void
150 il128(int d)
151 {
152 dwarfseg(DINFO);
153 debug_info_sz += leb128(d);
154 }
155
156 static void
157 p412l(int lbl)
158 {
159 if (DW64)
160 printf(PRTPREF "%s 0xffffff00\n", astypnames[INT]);
161 printf(PRTPREF "%s " DLABEL "\n", astypnames[LONG], lbl);
162 }
163
164 static void
165 info1b(int v)
166 {
167 dwarfseg(DINFO);
168 p1b(v);
169 debug_info_sz++;
170 }
171
172 static void
173 info2w(int v)
174 {
175 dwarfseg(DINFO);
176 p2w(v);
177 debug_info_sz += 2;
178 }
179
180 static void
181 info412l(int lbl)
182 {
183 dwarfseg(DINFO);
184 p412l(lbl);
185 debug_info_sz += (DW64 * 8 + 4);
186 }
187
188 void
189 dwarf_init(char *iname)
190 {
191 char buf[MAXPATHLEN];
192 /*
193 * Setup sections.
194 * We'll print out the basic stuff early so that not
195 * so much context must be kept around.
196 */
197 dwname = iname;
198 debug_info_sz_lbl = dwlab();
199 debug_abbrev_beg_lbl = dwlab();
200
201 /* begin with setting up the compilation unit header */
202 info412l(debug_info_sz_lbl); debug_info_sz = 0;
203 info2w(DVERSION);
204 info412l(debug_abbrev_beg_lbl);
205 info1b(SZPOINT(VOID)/SZCHAR);
206
207 /* Define abbrev table */
208 dwslab(DABBREV, debug_abbrev_beg_lbl);
209
210 /* compile unit follows, both in abbrev and info table */
211 /* Recommended stuff here as in the documentation */
212 al128(1); /* always first entry */
213 al128(DW_TAG_compile_unit);
214 al128(DW_CHILDREN_yes);
215
216 apair(DW_AT_name, DW_FORM_strp); /* file name; e.g. foo.c */
217 apair(DW_AT_producer, DW_FORM_string); /* producer; PCC 1.2.3 */
218 apair(DW_AT_comp_dir, DW_FORM_string); /* working directory */
219 apair(DW_AT_language, DW_FORM_data1); /* C99 */
220 apair(DW_AT_low_pc, DW_FORM_addr); /* start text */
221 apair(DW_AT_high_pc, DW_FORM_addr); /* end text */
222 apair(0, 0);
223
224 /* Above in info table */
225 il128(1); /* first entry */
226 ilbl(dwcfl = dwlab());
227 istring(PACKAGE_STRING);
228 istring(getcwd(buf, MAXPATHLEN));
229 il128(DW_LANG_C99);
230 ilbl(dwbtext = dwlab());
231 ilbl(dwetext = dwlab());
232
233 locctr(PROG, NULL);
234 printf(PRTPREF DLABEL ":\n", dwbtext);
235 }
236
237 void
238 dwarf_file(char *fn)
239 {
240 /* if first file name, print out as initial and remember */
241 if (dwcfl) {
242 dwslabstr(dwcfl, fn);
243 dwcfl = 0;
244 }
245 /* XXX add more here */
246 }
247
248 void
249 dwarf_end()
250 {
251 locctr(PROG, NULL);
252 printf(PRTPREF DLABEL ":\n", dwetext);
253 if (dwcfl)
254 dwslabstr(dwcfl, dwname ? dwname : "<unknown>");
255 printf(PRTPREF "\t.set " DLABEL ",%d\n",
256 debug_info_sz_lbl, debug_info_sz);
257 }
258
259 #endif