forked from StarWolf3000/vasm-mirror
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dwarf.c
489 lines (409 loc) · 15.3 KB
/
dwarf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/* dwarf.c - DWARF debugging sections */
/* (c) in 2018 by Frank Wille */
#include "vasm.h"
#include "dwarf.h"
#include "osdep.h"
struct DWinclude {
struct DWinclude *next;
const char *name;
};
struct DWfile {
struct DWfile *next;
const char *name;
int incidx;
};
static const unsigned char stdopclengths[] = {
0,1,1,1,1,0,0,0,1
};
static unsigned char dw2_compile_unit_abbrev[] = {
DW_TAG_compile_unit,
0, /* no children */
DW_AT_producer,DW_FORM_string,
DW_AT_language,DW_FORM_data2,
DW_AT_name,DW_FORM_string,
DW_AT_comp_dir,DW_FORM_string,
DW_AT_stmt_list,DW_FORM_data4,
DW_AT_low_pc,DW_FORM_addr,
DW_AT_high_pc,DW_FORM_addr,
0,0
};
static unsigned char dw3_compile_unit_abbrev[] = {
DW_TAG_compile_unit,
0, /* no children */
DW_AT_producer,DW_FORM_string,
DW_AT_language,DW_FORM_data2,
DW_AT_name,DW_FORM_string,
DW_AT_comp_dir,DW_FORM_string,
DW_AT_stmt_list,DW_FORM_data4,
DW_AT_ranges,DW_FORM_data4,
0,0
};
static struct DWinclude *first_dwinc;
static struct DWfile *first_dwfil;
static struct DWinclude *new_dwinc(const char *name)
{
struct DWinclude *new = mymalloc(sizeof(struct DWinclude));
new->next = NULL;
new->name = name;
return new;
}
/* DWARF needs source file lists with the file name part only, but without
any path in it. Include paths are put into another list. */
static void make_file_lists(struct source_file *first_source)
{
struct source_file *srcnode;
struct include_path *incnode;
struct DWfile *newfil,*dwfil;
struct DWinclude *dwinc;
char pathbuf[MAXPATHLEN];
const char *filepart;
int include_idx = 0;
int file_idx = 0;
int i;
first_dwfil = NULL;
first_dwinc = NULL;
for (srcnode=first_source; srcnode; srcnode=srcnode->next) {
newfil = mymalloc(sizeof(struct DWfile));
newfil->next = NULL;
pathbuf[0] = '\0';
if ((incnode = srcnode->incpath) != NULL) {
if (incnode->compdir_based) {
if (compile_dir != NULL)
strcpy(pathbuf,compile_dir);
else
ierror(0);
}
strcat(pathbuf,incnode->path);
}
if ((filepart = get_filepart(srcnode->name)) != srcnode->name) {
/* add path part from source file name */
size_t len = filepart - srcnode->name;
char *p = strrchr(pathbuf,'\0');
memcpy(p,srcnode->name,len);
*(p+len) = '\0';
}
if (pathbuf[0]) {
char *newpath = remove_path_delimiter(pathbuf);
/* check if this path already exists in the list, otherwise add it */
if (first_dwinc) {
for (i=1,dwinc=first_dwinc; ; i++,dwinc=dwinc->next) {
if (!filenamecmp(newpath,dwinc->name)) {
myfree(newpath);
newfil->incidx = i; /* use existing include index */
break;
}
if (dwinc->next == NULL) {
if (include_idx != i)
ierror(0);
dwinc->next = new_dwinc(newpath);
newfil->incidx = ++include_idx; /* new include index */
break;
}
}
}
else {
first_dwinc = new_dwinc(newpath);
newfil->incidx = ++include_idx; /* should be index 1 */
}
}
else
newfil->incidx = 0; /* no path, file is in current work directory */
/* append new file node */
if (++file_idx != srcnode->index)
ierror(0);
newfil->name = filepart;
if (dwfil = first_dwfil) {
while (dwfil->next != NULL)
dwfil = dwfil->next;
dwfil->next = newfil;
}
else
first_dwfil = newfil;
}
}
void dwarf_init(struct dwarf_info *dinfo,
struct include_path *first_incpath,
struct source_file *first_source)
{
atom *a,*dinfoatom,*dabbratom,*drangatom,*dlineatom;
void *lengthptr;
symbol *tmpsym;
section *dsec;
struct DWinclude *dwinc;
struct DWfile *dwfil;
int i;
if (dinfo->version < 2)
ierror(0);
/* init dwarf_info for DWARF2 and CPU architecture */
dinfo->code_sections = 0;
dinfo->addr_len = bytespertaddr;
dinfo->min_inst_len = INST_ALIGN;
dinfo->default_is_stmt = 1;
dinfo->line_base = -8;
dinfo->line_range = 16;
dinfo->opcode_base = sizeof(stdopclengths) + 1;
dinfo->max_pcadvance = (255 - dinfo->opcode_base) / dinfo->line_range;
dinfo->max_lnadvance_hipc = 255 - dinfo->max_pcadvance * dinfo->line_range -
dinfo->opcode_base;
dinfo->asec = dsec = new_section(".debug_aranges","r",1);
/* compilation unit header */
a = add_data_atom(dsec,4,1,0); /* total range-table size */
dinfo->range_length = a->content.db->data;
add_data_atom(dsec,2,1,dinfo->version);
dinfoatom = add_data_atom(dsec,4,1,0);/* 32-bit reloc to .debug_info */
add_data_atom(dsec,1,1,dinfo->addr_len);
add_data_atom(dsec,1,1,0); /* segment-descriptor size - unused */
dsec = new_section(".debug_info","r",1);
/* make 32-bit reloc for info-start label */
tmpsym = new_tmplabel(dsec);
add_extnreloc(&dinfoatom->content.db->relocs,tmpsym,tmpsym->pc,
REL_ABS,0,32,0);
/* compilation unit header */
a = add_data_atom(dsec,4,1,0); /* total compilation unit size */
lengthptr = a->content.db->data;
add_data_atom(dsec,2,1,dinfo->version);
dabbratom = add_data_atom(dsec,4,1,0);/* 32-bit reloc to .debug_abbrev */
add_data_atom(dsec,1,1,dinfo->addr_len);
/* first DIE, using abbrev. no. 1 */
add_leb128_atom(dsec,1);
/* DW_TAG_compile_unit */
add_char_atom(dsec,dinfo->producer,strlen(dinfo->producer));
add_data_atom(dsec,1,1,' ');
add_string_atom(dsec,cpuname); /* vasm version and cpu-name */
add_data_atom(dsec,2,1,(taddr)DW_LANG_ASSEMBLER);
add_string_atom(dsec,getdebugname());
add_string_atom(dsec,get_workdir());
dlineatom = add_data_atom(dsec,4,1,0);/* 32-bit reloc to .debug_line */
if (dinfo->version < 3) {
/* DWARF2:
low_pc/high_pc does not work for multiple sections, so the workaround
is to set low_pc=0 and high_pc=~0 to cover the whole address space. */
dinfo->lowpc_atom = add_data_atom(dsec,dinfo->addr_len,1,0);
dinfo->highpc_atom = add_data_atom(dsec,dinfo->addr_len,1,~0);
}
else {
/* address ranges defined in .debug_ranges since DWARF3 */
drangatom = add_data_atom(dsec,4,1,0);/* 32-bit reloc to .debug_ranges */
dinfo->lowpc_atom = dinfo->highpc_atom = NULL;
}
add_leb128_atom(dsec,0); /* no more DIEs */
setval(BIGENDIAN,lengthptr,4,dsec->pc-4);
dsec = new_section(".debug_abbrev","r",1);
/* make 32-bit reloc for abbreviations-start label */
tmpsym = new_tmplabel(dsec);
add_extnreloc(&dabbratom->content.db->relocs,tmpsym,tmpsym->pc,
REL_ABS,0,32,0);
add_leb128_atom(dsec,1); /* abbrev. no. 1 */
if (dinfo->version < 3)
add_char_atom(dsec,dw2_compile_unit_abbrev,
sizeof(dw2_compile_unit_abbrev));
else
add_char_atom(dsec,dw3_compile_unit_abbrev,
sizeof(dw3_compile_unit_abbrev));
add_leb128_atom(dsec,0); /* end of abbreviations */
if (dinfo->version >= 3) {
dinfo->rsec = dsec = new_section(".debug_ranges","r",2*dinfo->addr_len);
/* make 32-bit reloc for ranges-start label */
tmpsym = new_tmplabel(dsec);
add_extnreloc(&drangatom->content.db->relocs,tmpsym,tmpsym->pc,
REL_ABS,0,32,0);
/* @@@ highest and lowest possible address in first entry? */
add_data_atom(dsec,dinfo->addr_len,dinfo->addr_len,-1);
add_data_atom(dsec,dinfo->addr_len,dinfo->addr_len,0);
}
else
dinfo->rsec = NULL;
dinfo->lsec = dsec = new_section(".debug_line","r",1);
/* make 32-bit reloc for linedebug-start label */
tmpsym = new_tmplabel(dsec);
add_extnreloc(&dlineatom->content.db->relocs,tmpsym,tmpsym->pc,
REL_ABS,0,32,0);
/* make .debug_line prologue */
a = add_data_atom(dsec,4,1,0); /* line debug size for comp.unit */
dinfo->line_length = a->content.db->data;
add_data_atom(dsec,2,1,dinfo->version);
a = add_data_atom(dsec,4,1,0); /* byte-offset to statement program */
lengthptr = a->content.db->data;
add_data_atom(dsec,1,1,dinfo->min_inst_len);
add_data_atom(dsec,1,1,dinfo->default_is_stmt);
add_data_atom(dsec,1,1,dinfo->line_base);
add_data_atom(dsec,1,1,dinfo->line_range);
add_data_atom(dsec,1,1,dinfo->opcode_base);
/* define standard opcode lengths */
for (i=0; i<sizeof(stdopclengths); i++)
add_data_atom(dsec,1,1,stdopclengths[i]);
/* make lists of DWARF directories and files */
make_file_lists(first_source);
/* list of include directories, CWD-entry (index 0) is not written */
for (dwinc=first_dwinc; dwinc; dwinc=dwinc->next)
add_string_atom(dsec,dwinc->name);
add_data_atom(dsec,1,1,0); /* list is terminated by a 0-byte */
/* list of file names, with directory-index, last-modif.-time and size */
for (dwfil=first_dwfil; dwfil; dwfil=dwfil->next) {
add_string_atom(dsec,dwfil->name);
add_leb128_atom(dsec,dwfil->incidx);
add_leb128_atom(dsec,0); /* time */
add_leb128_atom(dsec,0); /* size */
}
add_data_atom(dsec,1,1,0); /* list is terminated by a 0-byte */
/* start of statement program */
setval(BIGENDIAN,lengthptr,4,dsec->pc-10);
dinfo->end_sequence = 1; /* we have to start a new sequence */
}
void dwarf_finish(struct dwarf_info *dinfo)
{
/* close .debug_aranges table with two NULL-entries and set its size */
add_data_atom(dinfo->asec,dinfo->addr_len,dinfo->addr_len,0);
add_data_atom(dinfo->asec,dinfo->addr_len,dinfo->addr_len,0);
setval(BIGENDIAN,dinfo->range_length,4,dinfo->asec->pc-4);
if (dinfo->rsec) {
/* close .debug_ranges table with two NULL-entries */
add_data_atom(dinfo->rsec,dinfo->addr_len,dinfo->addr_len,0);
add_data_atom(dinfo->rsec,dinfo->addr_len,dinfo->addr_len,0);
}
/* set .debug_line compilation unit size */
setval(BIGENDIAN,dinfo->line_length,4,dinfo->lsec->pc-4);
}
static void dwarf_set_address(struct dwarf_info *dinfo,symbol *sym)
{
static unsigned char opcode[3] = { 0,0,DW_LNE_set_address };
atom *a;
/* extended opcode to set address for current cpu including relocation */
opcode[1] = dinfo->addr_len + 1;
add_char_atom(dinfo->lsec,opcode,3);
a = add_data_atom(dinfo->lsec,dinfo->addr_len,1,sym->pc);
add_extnreloc(&a->content.db->relocs,sym,sym->pc,REL_ABS,
0,dinfo->addr_len*BITSPERBYTE,0);
}
static void set_atom_label(atom *a,size_t addrlen,symbol *sym)
{
setval(BIGENDIAN,a->content.db->data,addrlen,sym->pc);
add_extnreloc(&a->content.db->relocs,sym,sym->pc,REL_ABS,0,
addrlen*BITSPERBYTE,0);
}
static void set_atom_val(atom *a,size_t len,taddr val)
{
setval(BIGENDIAN,a->content.db->data,len,val);
a->content.db->relocs = NULL; /* clear relocs */
}
void dwarf_end_sequence(struct dwarf_info *dinfo,section *sec)
{
if (!dinfo->end_sequence) {
static unsigned char opcode[3] = { 0,1,DW_LNE_end_sequence };
symbol *sym = new_tmplabel(sec); /* label at end of section */
atom *a;
dwarf_set_address(dinfo,sym);
add_char_atom(dinfo->lsec,opcode,3);
dinfo->end_sequence = 1;
/* enter section size for this sequence into the address-range table */
add_data_atom(dinfo->asec,dinfo->addr_len,dinfo->addr_len,sec->pc);
if (dinfo->highpc_atom) {
if (dinfo->code_sections > 1)
/* low_pc=0, high_pc=~0 for multiple sections as workaround */
set_atom_val(dinfo->highpc_atom,dinfo->addr_len,~0);
else
/* we can set high_pc, when there is just a single code section */
set_atom_label(dinfo->highpc_atom,dinfo->addr_len,sym);
}
if (dinfo->rsec) {
/* enter end-of-section label reference into the ranges table */
a = add_data_atom(dinfo->rsec,dinfo->addr_len,dinfo->addr_len,sym->pc);
add_extnreloc(&a->content.db->relocs,sym,sym->pc,REL_ABS,
0,dinfo->addr_len*BITSPERBYTE,0);
}
}
}
void dwarf_line(struct dwarf_info *dinfo,section *sec,int file,int line)
{
if (file==0 || line==0)
ierror(0);
if (dinfo->end_sequence) {
symbol *sym;
atom *a;
/* start a new sequence, reset state machine */
dinfo->code_sections++;
dinfo->file = 1;
dinfo->line = 1;
dinfo->column = 0;
dinfo->is_stmt = dinfo->default_is_stmt;
dinfo->basic_block = 0;
dinfo->end_sequence = 0;
/* set label at start of the instruction's section */
sym = new_tmplabel(sec);
sym->pc = 0;
/* make new entry into the address-range table: .debug_aranges */
a = new_space_atom(number_expr(0),1,NULL);
a->align = 2 * dinfo->addr_len;
add_atom(dinfo->asec,a); /* align to double address-length */
a = add_data_atom(dinfo->asec,dinfo->addr_len,dinfo->addr_len,0);
add_extnreloc(&a->content.db->relocs,sym,sym->pc,REL_ABS,
0,dinfo->addr_len*BITSPERBYTE,0);
if (dinfo->lowpc_atom) {
if (dinfo->code_sections > 1)
/* low_pc=0, high_pc=~0 for multiple sections as workaround */
set_atom_val(dinfo->lowpc_atom,dinfo->addr_len,0);
else
/* we can set low_pc, when there is just a single code section */
set_atom_label(dinfo->lowpc_atom,dinfo->addr_len,sym);
}
if (dinfo->rsec) {
/* make new entry into the ranges table: .debug_ranges */
a = add_data_atom(dinfo->rsec,dinfo->addr_len,dinfo->addr_len,0);
add_extnreloc(&a->content.db->relocs,sym,sym->pc,REL_ABS,
0,dinfo->addr_len*BITSPERBYTE,0);
}
/* set relocatable address of first instruction, then advance line, etc.*/
dinfo->address = sec->pc;
dwarf_set_address(dinfo,new_tmplabel(sec));
if (file != dinfo->file) {
add_data_atom(dinfo->lsec,1,1,DW_LNS_set_file);
add_leb128_atom(dinfo->lsec,file);
dinfo->file = file;
}
if (line != dinfo->line) {
add_data_atom(dinfo->lsec,1,1,DW_LNS_advance_line);
add_sleb128_atom(dinfo->lsec,line-dinfo->line);
dinfo->line = line;
}
add_data_atom(dinfo->lsec,1,1,DW_LNS_copy);
}
else {
int lineoffs = line - dinfo->line;
int instoffs = (sec->pc - dinfo->address) / dinfo->min_inst_len;
if (file != dinfo->file) {
add_data_atom(dinfo->lsec,1,1,DW_LNS_set_file);
add_leb128_atom(dinfo->lsec,file);
dinfo->file = file;
}
if (instoffs > dinfo->max_pcadvance) {
if (instoffs - dinfo->max_pcadvance <= dinfo->max_pcadvance) {
/* const_add_pc for up to twice the maximum special opcode advance */
add_data_atom(dinfo->lsec,1,1,DW_LNS_const_add_pc);
instoffs -= dinfo->max_pcadvance;
}
else {
/* advance address by standard opcode */
add_data_atom(dinfo->lsec,1,1,DW_LNS_advance_pc);
add_leb128_atom(dinfo->lsec,instoffs);
instoffs = 0;
}
}
if (lineoffs < dinfo->line_base ||
lineoffs >= dinfo->line_base + dinfo->line_range ||
(instoffs == dinfo->max_pcadvance &&
lineoffs > dinfo->line_base + dinfo->max_lnadvance_hipc)) {
/* we have to advance line by standard opcode */
add_data_atom(dinfo->lsec,1,1,DW_LNS_advance_line);
add_sleb128_atom(dinfo->lsec,lineoffs);
lineoffs = 0;
}
/* construct special opcode for simultaneous inst./pc-advancement */
add_data_atom(dinfo->lsec,1,1,
dinfo->opcode_base +
instoffs*dinfo->line_range +
(lineoffs-dinfo->line_base));
/* update line/address */
dinfo->address = sec->pc;
dinfo->line = line;
}
}