Skip to content

[LTO][Legacy] Add new C APIs to query undefined symbols in assembly #145413

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion llvm/include/llvm-c/lto.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ typedef bool lto_bool_t;
* @{
*/

#define LTO_API_VERSION 29
#define LTO_API_VERSION 30

/**
* \since prior to LTO_API_VERSION=3
Expand Down Expand Up @@ -286,6 +286,21 @@ lto_module_get_symbol_name(lto_module_t mod, unsigned int index);
extern lto_symbol_attributes
lto_module_get_symbol_attribute(lto_module_t mod, unsigned int index);

/**
* Returns the number of asm undefined symbols in the object module.
*
* \since prior to LTO_API_VERSION=30
*/
extern unsigned int lto_module_get_num_asm_undef_symbols(lto_module_t mod);

/**
* Returns the name of the ith asm undefined symbol in the object module.
*
* \since prior to LTO_API_VERSION=30
*/
extern const char *lto_module_get_asm_undef_symbol_name(lto_module_t mod,
unsigned int index);

/**
* Returns the module's linker options.
*
Expand Down
8 changes: 8 additions & 0 deletions llvm/include/llvm/LTO/legacy/LTOModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,14 @@ struct LTOModule {
return StringRef();
}

uint32_t getAsmUndefSymbolCount() { return _asm_undefines.size(); }

StringRef getAsmUndefSymbolName(uint32_t index) {
if (index < _asm_undefines.size())
return _asm_undefines[index];
return StringRef();
}

const GlobalValue *getSymbolGV(uint32_t index) {
if (index < _symbols.size())
return _symbols[index].symbol;
Expand Down
21 changes: 21 additions & 0 deletions llvm/test/LTO/AArch64/module-asm.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
; RUN: llvm-as %s -o %t.o
; RUN: llvm-lto %t.o --list-symbols-only | FileCheck %s

; CHECK: ___foo { function defined hidden }
; CHECK: ___bar { function defined default }
; CHECK: _foo { data defined default }
; CHECK: ___foo { asm extern }

target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-n32:64-S128-Fn32"
target triple = "arm64-apple-macosx12.0.0"

module asm ".globl _foo"
module asm "_foo = ___foo"

define hidden i32 @__foo() {
ret i32 0
}

define i32 @__bar() {
ret i32 0
}
2 changes: 2 additions & 0 deletions llvm/tools/llvm-lto/llvm-lto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,8 @@ static void testLTOModule(const TargetOptions &Options) {
printLTOSymbolAttributes(Module->getSymbolAttributes(I));
outs() << "\n";
}
for (int I = 0, E = Module->getAsmUndefSymbolCount(); I != E; ++I)
outs() << Module->getAsmUndefSymbolName(I) << " { asm extern }\n";
}
if (QueryHasCtorDtor)
outs() << Filename
Expand Down
9 changes: 9 additions & 0 deletions llvm/tools/lto/lto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,15 @@ lto_symbol_attributes lto_module_get_symbol_attribute(lto_module_t mod,
return unwrap(mod)->getSymbolAttributes(index);
}

unsigned int lto_module_get_num_asm_undef_symbols(lto_module_t mod) {
return unwrap(mod)->getAsmUndefSymbolCount();
}

const char *lto_module_get_asm_undef_symbol_name(lto_module_t mod,
unsigned int index) {
return unwrap(mod)->getAsmUndefSymbolName(index).data();
}

const char* lto_module_get_linkeropts(lto_module_t mod) {
return unwrap(mod)->getLinkerOpts().data();
}
Expand Down
2 changes: 2 additions & 0 deletions llvm/tools/lto/lto.exports
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ lto_module_get_macho_cputype
lto_module_get_num_symbols
lto_module_get_symbol_attribute
lto_module_get_symbol_name
lto_module_get_num_asm_undef_symbols
lto_module_get_asm_undef_symbol_name
lto_module_get_target_triple
lto_module_set_target_triple
lto_module_is_object_file
Expand Down
Loading