1
1
use crate :: attributes;
2
2
use crate :: llvm;
3
+ use crate :: llvm_util;
3
4
use crate :: debuginfo;
4
5
use crate :: value:: Value ;
5
6
use rustc:: dep_graph:: DepGraphSafe ;
@@ -140,6 +141,11 @@ pub fn is_pie_binary(sess: &Session) -> bool {
140
141
!is_any_library ( sess) && get_reloc_model ( sess) == llvm:: RelocMode :: PIC
141
142
}
142
143
144
+ fn strip_function_ptr_alignment ( data_layout : String ) -> String {
145
+ // FIXME: Make this more general.
146
+ data_layout. replace ( "-Fi8-" , "-" )
147
+ }
148
+
143
149
pub unsafe fn create_module (
144
150
tcx : TyCtxt < ' _ > ,
145
151
llcx : & ' ll llvm:: Context ,
@@ -149,14 +155,19 @@ pub unsafe fn create_module(
149
155
let mod_name = SmallCStr :: new ( mod_name) ;
150
156
let llmod = llvm:: LLVMModuleCreateWithNameInContext ( mod_name. as_ptr ( ) , llcx) ;
151
157
158
+ let mut target_data_layout = sess. target . target . data_layout . clone ( ) ;
159
+ if llvm_util:: get_major_version ( ) < 9 {
160
+ target_data_layout = strip_function_ptr_alignment ( target_data_layout) ;
161
+ }
162
+
152
163
// Ensure the data-layout values hardcoded remain the defaults.
153
164
if sess. target . target . options . is_builtin {
154
165
let tm = crate :: back:: write:: create_informational_target_machine ( & tcx. sess , false ) ;
155
166
llvm:: LLVMRustSetDataLayoutFromTargetMachine ( llmod, tm) ;
156
167
llvm:: LLVMRustDisposeTargetMachine ( tm) ;
157
168
158
- let data_layout = llvm:: LLVMGetDataLayout ( llmod) ;
159
- let data_layout = str:: from_utf8 ( CStr :: from_ptr ( data_layout ) . to_bytes ( ) )
169
+ let llvm_data_layout = llvm:: LLVMGetDataLayout ( llmod) ;
170
+ let llvm_data_layout = str:: from_utf8 ( CStr :: from_ptr ( llvm_data_layout ) . to_bytes ( ) )
160
171
. ok ( ) . expect ( "got a non-UTF8 data-layout from LLVM" ) ;
161
172
162
173
// Unfortunately LLVM target specs change over time, and right now we
@@ -177,16 +188,16 @@ pub unsafe fn create_module(
177
188
let cfg_llvm_root = option_env ! ( "CFG_LLVM_ROOT" ) . unwrap_or ( "" ) ;
178
189
let custom_llvm_used = cfg_llvm_root. trim ( ) != "" ;
179
190
180
- if !custom_llvm_used && sess . target . target . data_layout != data_layout {
191
+ if !custom_llvm_used && target_data_layout != llvm_data_layout {
181
192
bug ! ( "data-layout for builtin `{}` target, `{}`, \
182
193
differs from LLVM default, `{}`",
183
194
sess. target. target. llvm_target,
184
- sess . target . target . data_layout ,
185
- data_layout ) ;
195
+ target_data_layout ,
196
+ llvm_data_layout ) ;
186
197
}
187
198
}
188
199
189
- let data_layout = SmallCStr :: new ( & sess . target . target . data_layout ) ;
200
+ let data_layout = SmallCStr :: new ( & target_data_layout ) ;
190
201
llvm:: LLVMSetDataLayout ( llmod, data_layout. as_ptr ( ) ) ;
191
202
192
203
let llvm_target = SmallCStr :: new ( & sess. target . target . llvm_target ) ;
0 commit comments