1
- pub trait Product {
1
+ use std:: collections:: HashMap ;
2
+
3
+ use cranelift_module:: FuncId ;
4
+
5
+ use faerie:: * ;
6
+ use object:: { SectionKind , RelocationKind , RelocationEncoding } ;
7
+ use object:: write:: * ;
8
+ use cranelift_object:: * ;
9
+
10
+ use gimli:: SectionId ;
11
+
12
+ use crate :: debuginfo:: { DebugReloc , DebugRelocName } ;
13
+
14
+ pub trait WriteMetadata {
2
15
fn add_rustc_section ( & mut self , symbol_name : String , data : Vec < u8 > , is_like_osx : bool ) ;
3
16
}
4
17
5
- impl Product for faerie:: Artifact {
18
+ impl WriteMetadata for faerie:: Artifact {
6
19
fn add_rustc_section ( & mut self , symbol_name : String , data : Vec < u8 > , is_like_osx : bool ) {
7
20
self
8
21
. declare ( ".rustc" , faerie:: Decl :: section ( faerie:: SectionKind :: Data ) )
@@ -25,7 +38,7 @@ impl Product for faerie::Artifact {
25
38
}
26
39
}
27
40
28
- impl Product for object:: write:: Object {
41
+ impl WriteMetadata for object:: write:: Object {
29
42
fn add_rustc_section ( & mut self , symbol_name : String , data : Vec < u8 > , is_like_osx : bool ) {
30
43
let segment = self . segment_name ( object:: write:: StandardSegment :: Data ) . to_vec ( ) ;
31
44
let section_id = self . add_section ( segment, b".rustc" . to_vec ( ) , object:: SectionKind :: Data ) ;
@@ -46,3 +59,91 @@ impl Product for object::write::Object {
46
59
}
47
60
}
48
61
}
62
+
63
+ pub trait WriteDebugInfo {
64
+ type SectionId ;
65
+
66
+ fn add_debug_section ( & mut self , name : SectionId , data : Vec < u8 > ) -> Self :: SectionId ;
67
+ fn add_debug_reloc (
68
+ & mut self ,
69
+ section_map : & HashMap < SectionId , Self :: SectionId > ,
70
+ symbol_map : & indexmap:: IndexSet < ( String , FuncId ) > ,
71
+ from : & Self :: SectionId ,
72
+ reloc : & DebugReloc ,
73
+ ) ;
74
+ }
75
+
76
+ impl WriteDebugInfo for Artifact {
77
+ type SectionId = SectionId ;
78
+
79
+ fn add_debug_section ( & mut self , id : SectionId , data : Vec < u8 > ) -> SectionId {
80
+ self . declare_with ( id. name ( ) , Decl :: section ( faerie:: SectionKind :: Debug ) , data) . unwrap ( ) ;
81
+ id
82
+ }
83
+
84
+ fn add_debug_reloc (
85
+ & mut self ,
86
+ _section_map : & HashMap < SectionId , Self :: SectionId > ,
87
+ symbol_map : & indexmap:: IndexSet < ( String , FuncId ) > ,
88
+ from : & Self :: SectionId ,
89
+ reloc : & DebugReloc ,
90
+ ) {
91
+ self
92
+ . link_with (
93
+ faerie:: Link {
94
+ from : from. name ( ) ,
95
+ to : match reloc. name {
96
+ DebugRelocName :: Section ( id) => id. name ( ) ,
97
+ DebugRelocName :: Symbol ( index) => & symbol_map. get_index ( index) . unwrap ( ) . 0 ,
98
+ } ,
99
+ at : u64:: from ( reloc. offset ) ,
100
+ } ,
101
+ faerie:: Reloc :: Debug {
102
+ size : reloc. size ,
103
+ addend : reloc. addend as i32 ,
104
+ } ,
105
+ )
106
+ . expect ( "faerie relocation error" ) ;
107
+ }
108
+ }
109
+
110
+ impl WriteDebugInfo for ObjectProduct {
111
+ type SectionId = ( object:: write:: SectionId , object:: write:: SymbolId ) ;
112
+
113
+ fn add_debug_section (
114
+ & mut self ,
115
+ id : SectionId ,
116
+ data : Vec < u8 > ,
117
+ ) -> ( object:: write:: SectionId , object:: write:: SymbolId ) {
118
+ let segment = self . object . segment_name ( StandardSegment :: Debug ) . to_vec ( ) ;
119
+ let name = id. name ( ) . as_bytes ( ) . to_vec ( ) ;
120
+ let section_id = self . object . add_section ( segment, name, SectionKind :: Debug ) ;
121
+ self . object . section_mut ( section_id) . set_data ( data, 1 ) ;
122
+ let symbol_id = self . object . section_symbol ( section_id) ;
123
+ ( section_id, symbol_id)
124
+ }
125
+
126
+ fn add_debug_reloc (
127
+ & mut self ,
128
+ section_map : & HashMap < SectionId , Self :: SectionId > ,
129
+ symbol_map : & indexmap:: IndexSet < ( String , FuncId ) > ,
130
+ from : & Self :: SectionId ,
131
+ reloc : & DebugReloc ,
132
+ ) {
133
+ let symbol = match reloc. name {
134
+ DebugRelocName :: Section ( id) => section_map. get ( & id) . unwrap ( ) . 1 ,
135
+ DebugRelocName :: Symbol ( id) => {
136
+ let ( _func_name, func_id) = symbol_map. get_index ( id) . unwrap ( ) ;
137
+ self . function_symbol ( * func_id)
138
+ }
139
+ } ;
140
+ self . object . add_relocation ( from. 0 , Relocation {
141
+ offset : u64:: from ( reloc. offset ) ,
142
+ symbol,
143
+ kind : RelocationKind :: Absolute ,
144
+ encoding : RelocationEncoding :: Generic ,
145
+ size : reloc. size * 8 ,
146
+ addend : reloc. addend ,
147
+ } ) . unwrap ( ) ;
148
+ }
149
+ }
0 commit comments