11use crate :: attributes;
22use crate :: llvm;
3+ use crate :: llvm_util;
34use crate :: debuginfo;
45use crate :: value:: Value ;
56use rustc:: dep_graph:: DepGraphSafe ;
@@ -140,6 +141,11 @@ pub fn is_pie_binary(sess: &Session) -> bool {
140141 !is_any_library ( sess) && get_reloc_model ( sess) == llvm:: RelocMode :: PIC
141142}
142143
144+ fn strip_function_ptr_alignment ( data_layout : String ) -> String {
145+ // FIXME: Make this more general.
146+ data_layout. replace ( "-Fi8-" , "-" )
147+ }
148+
143149pub unsafe fn create_module (
144150 tcx : TyCtxt < ' _ > ,
145151 llcx : & ' ll llvm:: Context ,
@@ -149,14 +155,19 @@ pub unsafe fn create_module(
149155 let mod_name = SmallCStr :: new ( mod_name) ;
150156 let llmod = llvm:: LLVMModuleCreateWithNameInContext ( mod_name. as_ptr ( ) , llcx) ;
151157
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+
152163 // Ensure the data-layout values hardcoded remain the defaults.
153164 if sess. target . target . options . is_builtin {
154165 let tm = crate :: back:: write:: create_informational_target_machine ( & tcx. sess , false ) ;
155166 llvm:: LLVMRustSetDataLayoutFromTargetMachine ( llmod, tm) ;
156167 llvm:: LLVMRustDisposeTargetMachine ( tm) ;
157168
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 ( ) )
160171 . ok ( ) . expect ( "got a non-UTF8 data-layout from LLVM" ) ;
161172
162173 // Unfortunately LLVM target specs change over time, and right now we
@@ -177,16 +188,16 @@ pub unsafe fn create_module(
177188 let cfg_llvm_root = option_env ! ( "CFG_LLVM_ROOT" ) . unwrap_or ( "" ) ;
178189 let custom_llvm_used = cfg_llvm_root. trim ( ) != "" ;
179190
180- if !custom_llvm_used && sess . target . target . data_layout != data_layout {
191+ if !custom_llvm_used && target_data_layout != llvm_data_layout {
181192 bug ! ( "data-layout for builtin `{}` target, `{}`, \
182193 differs from LLVM default, `{}`",
183194 sess. target. target. llvm_target,
184- sess . target . target . data_layout ,
185- data_layout ) ;
195+ target_data_layout ,
196+ llvm_data_layout ) ;
186197 }
187198 }
188199
189- let data_layout = SmallCStr :: new ( & sess . target . target . data_layout ) ;
200+ let data_layout = SmallCStr :: new ( & target_data_layout ) ;
190201 llvm:: LLVMSetDataLayout ( llmod, data_layout. as_ptr ( ) ) ;
191202
192203 let llvm_target = SmallCStr :: new ( & sess. target . target . llvm_target ) ;
0 commit comments