@@ -2,13 +2,14 @@ extern crate gimli;
2
2
3
3
use crate :: prelude:: * ;
4
4
5
+ use std:: path:: Path ;
5
6
use std:: marker:: PhantomData ;
6
7
7
8
use gimli:: write:: {
8
9
Address , AttributeValue , CompilationUnit , DebugAbbrev , DebugInfo , DebugLine , DebugRanges ,
9
10
DebugRngLists , DebugStr , EndianVec , LineProgram , LineProgramId , LineProgramTable , Range ,
10
11
RangeList , RangeListTable , Result , SectionId , StringTable , UnitEntryId , UnitId , UnitTable ,
11
- Writer ,
12
+ Writer , FileId ,
12
13
} ;
13
14
use gimli:: { Endianity , Format , RunTimeEndian } ;
14
15
@@ -23,6 +24,17 @@ fn target_endian(tcx: TyCtxt) -> RunTimeEndian {
23
24
}
24
25
}
25
26
27
+ fn line_program_add_file < P : AsRef < Path > > ( line_program : & mut LineProgram , file : P ) -> FileId {
28
+ let file = file. as_ref ( ) ;
29
+ let dir_id =
30
+ line_program. add_directory ( file. parent ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) . as_bytes ( ) ) ;
31
+ line_program. add_file (
32
+ file. file_name ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) . as_bytes ( ) ,
33
+ dir_id,
34
+ None ,
35
+ )
36
+ }
37
+
26
38
struct DebugReloc {
27
39
offset : u32 ,
28
40
size : u8 ,
@@ -53,7 +65,7 @@ pub struct DebugContext<'tcx> {
53
65
impl < ' a , ' tcx : ' a > DebugContext < ' tcx > {
54
66
pub fn new ( tcx : TyCtxt , address_size : u8 ) -> Self {
55
67
// TODO: this should be configurable
56
- let version = 4 ;
68
+ let version = 3 ; // macOS doesn't seem to support DWARF > 3
57
69
let format = Format :: Dwarf32 ;
58
70
59
71
// FIXME: how to get version when building out of tree?
@@ -71,7 +83,7 @@ impl<'a, 'tcx: 'a> DebugContext<'tcx> {
71
83
let range_lists = RangeListTable :: default ( ) ;
72
84
73
85
let global_line_program = line_programs. add ( LineProgram :: new (
74
- 3 , // FIXME https://github.com/gimli-rs/gimli/issues/363
86
+ version ,
75
87
address_size,
76
88
format,
77
89
1 ,
@@ -135,11 +147,13 @@ impl<'a, 'tcx: 'a> DebugContext<'tcx> {
135
147
fn emit_location ( & mut self , tcx : TyCtxt < ' a , ' tcx , ' tcx > , entry_id : UnitEntryId , span : Span ) {
136
148
let loc = tcx. sess . source_map ( ) . lookup_char_pos ( span. lo ( ) ) ;
137
149
150
+ let line_program = self . line_programs . get_mut ( self . global_line_program ) ;
151
+ let file_id = line_program_add_file ( line_program, loc. file . name . to_string ( ) ) ;
152
+
138
153
let unit = self . units . get_mut ( self . unit_id ) ;
139
154
let entry = unit. get_mut ( entry_id) ;
140
155
141
- let file_id = self . strings . add ( loc. file . name . to_string ( ) ) ;
142
- entry. set ( gimli:: DW_AT_decl_file , AttributeValue :: StringRef ( file_id) ) ;
156
+ entry. set ( gimli:: DW_AT_decl_file , AttributeValue :: FileIndex ( file_id) ) ;
143
157
entry. set (
144
158
gimli:: DW_AT_decl_line ,
145
159
AttributeValue :: Udata ( loc. line as u64 ) ,
@@ -218,20 +232,28 @@ impl<'a, 'tcx: 'a> DebugContext<'tcx> {
218
232
debug_line. 0 . writer . into_vec ( ) ,
219
233
)
220
234
. unwrap ( ) ;
221
- artifact
222
- . declare_with (
223
- SectionId :: DebugRanges . name ( ) ,
224
- Decl :: DebugSection ,
225
- debug_ranges. 0 . writer . into_vec ( ) ,
226
- )
227
- . unwrap ( ) ;
228
- artifact
229
- . declare_with (
230
- SectionId :: DebugRngLists . name ( ) ,
231
- Decl :: DebugSection ,
232
- debug_rnglists. 0 . writer . into_vec ( ) ,
233
- )
234
- . unwrap ( ) ;
235
+
236
+ let debug_ranges_not_empty = !debug_ranges. 0 . writer . slice ( ) . is_empty ( ) ;
237
+ if debug_ranges_not_empty {
238
+ artifact
239
+ . declare_with (
240
+ SectionId :: DebugRanges . name ( ) ,
241
+ Decl :: DebugSection ,
242
+ debug_ranges. 0 . writer . into_vec ( ) ,
243
+ )
244
+ . unwrap ( ) ;
245
+ }
246
+
247
+ let debug_rnglists_not_empty = !debug_rnglists. 0 . writer . slice ( ) . is_empty ( ) ;
248
+ if debug_rnglists_not_empty {
249
+ artifact
250
+ . declare_with (
251
+ SectionId :: DebugRngLists . name ( ) ,
252
+ Decl :: DebugSection ,
253
+ debug_rnglists. 0 . writer . into_vec ( ) ,
254
+ )
255
+ . unwrap ( ) ;
256
+ }
235
257
236
258
for reloc in debug_abbrev. 0 . relocs {
237
259
artifact
@@ -297,36 +319,40 @@ impl<'a, 'tcx: 'a> DebugContext<'tcx> {
297
319
. expect ( "faerie relocation error" ) ;
298
320
}
299
321
300
- for reloc in debug_ranges. 0 . relocs {
301
- artifact
302
- . link_with (
303
- faerie:: Link {
304
- from : SectionId :: DebugRanges . name ( ) ,
305
- to : & reloc. name ,
306
- at : u64:: from ( reloc. offset ) ,
307
- } ,
308
- faerie:: Reloc :: Debug {
309
- size : reloc. size ,
310
- addend : reloc. addend as i32 ,
311
- } ,
312
- )
313
- . expect ( "faerie relocation error" ) ;
322
+ if debug_ranges_not_empty {
323
+ for reloc in debug_ranges. 0 . relocs {
324
+ artifact
325
+ . link_with (
326
+ faerie:: Link {
327
+ from : SectionId :: DebugRanges . name ( ) ,
328
+ to : & reloc. name ,
329
+ at : u64:: from ( reloc. offset ) ,
330
+ } ,
331
+ faerie:: Reloc :: Debug {
332
+ size : reloc. size ,
333
+ addend : reloc. addend as i32 ,
334
+ } ,
335
+ )
336
+ . expect ( "faerie relocation error" ) ;
337
+ }
314
338
}
315
339
316
- for reloc in debug_rnglists. 0 . relocs {
317
- artifact
318
- . link_with (
319
- faerie:: Link {
320
- from : SectionId :: DebugRngLists . name ( ) ,
321
- to : & reloc. name ,
322
- at : u64:: from ( reloc. offset ) ,
323
- } ,
324
- faerie:: Reloc :: Debug {
325
- size : reloc. size ,
326
- addend : reloc. addend as i32 ,
327
- } ,
328
- )
329
- . expect ( "faerie relocation error" ) ;
340
+ if debug_rnglists_not_empty {
341
+ for reloc in debug_rnglists. 0 . relocs {
342
+ artifact
343
+ . link_with (
344
+ faerie:: Link {
345
+ from : SectionId :: DebugRngLists . name ( ) ,
346
+ to : & reloc. name ,
347
+ at : u64:: from ( reloc. offset ) ,
348
+ } ,
349
+ faerie:: Reloc :: Debug {
350
+ size : reloc. size ,
351
+ addend : reloc. addend as i32 ,
352
+ } ,
353
+ )
354
+ . expect ( "faerie relocation error" ) ;
355
+ }
330
356
}
331
357
}
332
358
@@ -423,15 +449,7 @@ impl<'a, 'b, 'tcx: 'b> FunctionDebugContext<'a, 'tcx> {
423
449
424
450
let create_row_for_span = |line_program : & mut LineProgram , span : Span | {
425
451
let loc = tcx. sess . source_map ( ) . lookup_char_pos ( span. lo ( ) ) ;
426
- let file = loc. file . name . to_string ( ) ;
427
- let file = :: std:: path:: Path :: new ( & file) ;
428
- let dir_id =
429
- line_program. add_directory ( file. parent ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) . as_bytes ( ) ) ;
430
- let file_id = line_program. add_file (
431
- file. file_name ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) . as_bytes ( ) ,
432
- dir_id,
433
- None ,
434
- ) ;
452
+ let file_id = line_program_add_file ( line_program, loc. file . name . to_string ( ) ) ;
435
453
436
454
/*println!(
437
455
"srcloc {:>04X} {}:{}:{}",
0 commit comments