Skip to content

Commit a93eb14

Browse files
[LTO][Legacy] Add new C APIs to query undefined symbols in assembly (#145413)
Add new APIs to legacy LTO C API to surface undefined symbols that parsed from assembly. This information is needed by thin LTO to figure out the symbols not to dead strip, while such information is automatically forwarded in full LTO already. Linker needs to fetch the information and adds the undefs from assembly to MustPreserveSymbols list just like treating undefs from a non-LTO object file. Resolves: #29340
1 parent 0a3c5c4 commit a93eb14

File tree

6 files changed

+58
-1
lines changed

6 files changed

+58
-1
lines changed

llvm/include/llvm-c/lto.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ typedef bool lto_bool_t;
4646
* @{
4747
*/
4848

49-
#define LTO_API_VERSION 29
49+
#define LTO_API_VERSION 30
5050

5151
/**
5252
* \since prior to LTO_API_VERSION=3
@@ -286,6 +286,21 @@ lto_module_get_symbol_name(lto_module_t mod, unsigned int index);
286286
extern lto_symbol_attributes
287287
lto_module_get_symbol_attribute(lto_module_t mod, unsigned int index);
288288

289+
/**
290+
* Returns the number of asm undefined symbols in the object module.
291+
*
292+
* \since prior to LTO_API_VERSION=30
293+
*/
294+
extern unsigned int lto_module_get_num_asm_undef_symbols(lto_module_t mod);
295+
296+
/**
297+
* Returns the name of the ith asm undefined symbol in the object module.
298+
*
299+
* \since prior to LTO_API_VERSION=30
300+
*/
301+
extern const char *lto_module_get_asm_undef_symbol_name(lto_module_t mod,
302+
unsigned int index);
303+
289304
/**
290305
* Returns the module's linker options.
291306
*

llvm/include/llvm/LTO/legacy/LTOModule.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,14 @@ struct LTOModule {
143143
return StringRef();
144144
}
145145

146+
uint32_t getAsmUndefSymbolCount() { return _asm_undefines.size(); }
147+
148+
StringRef getAsmUndefSymbolName(uint32_t index) {
149+
if (index < _asm_undefines.size())
150+
return _asm_undefines[index];
151+
return StringRef();
152+
}
153+
146154
const GlobalValue *getSymbolGV(uint32_t index) {
147155
if (index < _symbols.size())
148156
return _symbols[index].symbol;

llvm/test/LTO/AArch64/module-asm.ll

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
; RUN: llvm-as %s -o %t.o
2+
; RUN: llvm-lto %t.o --list-symbols-only | FileCheck %s
3+
4+
; CHECK: ___foo { function defined hidden }
5+
; CHECK: ___bar { function defined default }
6+
; CHECK: _foo { data defined default }
7+
; CHECK: ___foo { asm extern }
8+
9+
target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-n32:64-S128-Fn32"
10+
target triple = "arm64-apple-macosx12.0.0"
11+
12+
module asm ".globl _foo"
13+
module asm "_foo = ___foo"
14+
15+
define hidden i32 @__foo() {
16+
ret i32 0
17+
}
18+
19+
define i32 @__bar() {
20+
ret i32 0
21+
}

llvm/tools/llvm-lto/llvm-lto.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,8 @@ static void testLTOModule(const TargetOptions &Options) {
475475
printLTOSymbolAttributes(Module->getSymbolAttributes(I));
476476
outs() << "\n";
477477
}
478+
for (int I = 0, E = Module->getAsmUndefSymbolCount(); I != E; ++I)
479+
outs() << Module->getAsmUndefSymbolName(I) << " { asm extern }\n";
478480
}
479481
if (QueryHasCtorDtor)
480482
outs() << Filename

llvm/tools/lto/lto.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,15 @@ lto_symbol_attributes lto_module_get_symbol_attribute(lto_module_t mod,
322322
return unwrap(mod)->getSymbolAttributes(index);
323323
}
324324

325+
unsigned int lto_module_get_num_asm_undef_symbols(lto_module_t mod) {
326+
return unwrap(mod)->getAsmUndefSymbolCount();
327+
}
328+
329+
const char *lto_module_get_asm_undef_symbol_name(lto_module_t mod,
330+
unsigned int index) {
331+
return unwrap(mod)->getAsmUndefSymbolName(index).data();
332+
}
333+
325334
const char* lto_module_get_linkeropts(lto_module_t mod) {
326335
return unwrap(mod)->getLinkerOpts().data();
327336
}

llvm/tools/lto/lto.exports

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ lto_module_get_macho_cputype
1414
lto_module_get_num_symbols
1515
lto_module_get_symbol_attribute
1616
lto_module_get_symbol_name
17+
lto_module_get_num_asm_undef_symbols
18+
lto_module_get_asm_undef_symbol_name
1719
lto_module_get_target_triple
1820
lto_module_set_target_triple
1921
lto_module_is_object_file

0 commit comments

Comments
 (0)