@@ -16,10 +16,11 @@ use llvm_sys::{
1616 LLVMAttributeFunctionIndex , LLVMLinkage , LLVMVisibility ,
1717 bit_reader:: LLVMParseBitcodeInContext2 ,
1818 core:: {
19- LLVMCreateMemoryBufferWithMemoryRange , LLVMDisposeMemoryBuffer , LLVMDisposeMessage ,
20- LLVMGetEnumAttributeKindForName , LLVMGetMDString , LLVMGetModuleInlineAsm , LLVMGetTarget ,
21- LLVMGetValueName2 , LLVMRemoveEnumAttributeAtIndex , LLVMSetLinkage , LLVMSetModuleInlineAsm2 ,
22- LLVMSetVisibility ,
19+ LLVMCountBasicBlocks , LLVMCreateMemoryBufferWithMemoryRange , LLVMDisposeMemoryBuffer ,
20+ LLVMDisposeMessage , LLVMGetEnumAttributeKindForName , LLVMGetMDString ,
21+ LLVMGetModuleInlineAsm , LLVMGetTarget , LLVMGetValueName2 , LLVMIsAFunction ,
22+ LLVMIsAGlobalVariable , LLVMIsDeclaration , LLVMRemoveEnumAttributeAtIndex , LLVMSetLinkage ,
23+ LLVMSetModuleInlineAsm2 , LLVMSetSection , LLVMSetVisibility ,
2324 } ,
2425 error:: {
2526 LLVMDisposeErrorMessage , LLVMGetErrorMessage , LLVMGetErrorTypeId , LLVMGetStringErrorTypeId ,
@@ -41,6 +42,7 @@ use llvm_sys::{
4142 LLVMCreatePassBuilderOptions , LLVMDisposePassBuilderOptions , LLVMRunPasses ,
4243 } ,
4344} ;
45+ use log:: info;
4446use tracing:: { debug, error} ;
4547pub ( crate ) use types:: {
4648 context:: { InstalledDiagnosticHandler , LLVMContext } ,
@@ -261,6 +263,28 @@ pub(crate) fn internalize(
261263 export_symbols : & HashSet < Cow < ' _ , [ u8 ] > > ,
262264) {
263265 if !name. starts_with ( b"llvm." ) && !export_symbols. contains ( name) {
266+ if unsafe { !LLVMIsAFunction ( value) . is_null ( ) } {
267+ let num_blocks = unsafe { LLVMCountBasicBlocks ( value) } ;
268+ if num_blocks == 0 {
269+ unsafe { LLVMSetSection ( value, c".ksyms" . as_ptr ( ) ) } ;
270+ info ! (
271+ "not internalizing undefined function {}" ,
272+ str :: from_utf8( name) . unwrap_or( "<invalid utf8>" )
273+ ) ;
274+ return ;
275+ }
276+ }
277+ if unsafe { !LLVMIsAGlobalVariable ( value) . is_null ( ) } {
278+ if unsafe { LLVMIsDeclaration ( value) != 0 } {
279+ unsafe { LLVMSetSection ( value, c".ksyms" . as_ptr ( ) ) } ;
280+ info ! (
281+ "not internalizing undefined global variable {}" ,
282+ str :: from_utf8( name) . unwrap_or( "<invalid utf8>" )
283+ ) ;
284+ return ;
285+ }
286+ }
287+
264288 unsafe { LLVMSetLinkage ( value, LLVMLinkage :: LLVMInternalLinkage ) } ;
265289 unsafe { LLVMSetVisibility ( value, LLVMVisibility :: LLVMDefaultVisibility ) } ;
266290 }
0 commit comments