Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasSte committed Jan 22, 2025
1 parent 838a68c commit 199217f
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 4 deletions.
6 changes: 3 additions & 3 deletions lld/ELF/SyntheticSections.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2178,7 +2178,7 @@ void SymbolTableBaseSection::addSymbol(Symbol *b) {
config->strip != StripPolicy::None) {
// Including the symbol name to the dynamic symbol table may increase the
// contract size in more than 20kb, so whenever we want to strip
// information, we can assign the same symbol to all local functions.
// information, we can assign the same string to all local symbols.
const static unsigned HiddenOffset =
strTabSec.addString("hidden_func", false);
Offset = HiddenOffset;
Expand All @@ -2197,8 +2197,8 @@ void SymbolTableBaseSection::sortAndDedupSymbolsByValue() {
});

// The linker can assign the same address to two different functions if
// their code is the same. When that happens, we must deduplicate symbols
// by st_value.
// their code is the same (function aliasing). When that happens, we must
// deduplicate symbols by st_value.
symbols.erase(
std::unique(symbols.begin(), symbols.end(),
[](const SymbolTableEntry &a, const SymbolTableEntry &b) {
Expand Down
2 changes: 1 addition & 1 deletion lld/ELF/Writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2096,7 +2096,7 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {
addVerneed(sym);
} else if (isSbfV3() && includeInSymtab(*sym) && isa<Defined>(sym) &&
sym->type == STT_FUNC) {
// We need want local functions in the dynamic symbol table for SBFv3
// We want local functions in the dynamic symbol table for SBFv3
const Defined *Def = dyn_cast<Defined>(sym);
if (shouldKeepInSymtab(*Def)) {
partitions[sym->partition - 1].dynSymTab->addSymbol(sym);
Expand Down
Binary file added lld/test/ELF/Inputs/sbf-dynsyms-dup.o
Binary file not shown.
Binary file added lld/test/ELF/Inputs/sbf-dynsyms-local.o
Binary file not shown.
32 changes: 32 additions & 0 deletions lld/test/ELF/sbf-dynsyms-dup.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
; RUN: ld.lld -z notext -shared -Bdynamic -entry entrypoint %S/Inputs/sbf-dynsyms-dup.o -o %t1.so
; RUN: llvm-readelf %t1.so --dyn-syms | FileCheck %s

; Ensure no duplicates in case of function aliasing.
; sbf-dynsyms-dup.o contains the following rust code
;
; #[inline(never)]
; #[no_mangle]
; pub fn func_1(c: u64) -> u64 {
; c % 67
; }
;
; #[inline(never)]
; #[no_mangle]
; pub fn func_2(a: u64) -> u64 {
; return a % 67;
; }
;
; #[no_mangle]
; pub unsafe extern "C" fn entrypoint(a: u64, b: u64) -> u64 {
; let r1 = func_1(a);
; let r2 = func_2(b);
; r1 + r2
; }
;
; Compiled with the command:
; rustc --target sbf-solana-solana --crate-type lib -C panic=abort -C opt-level=2 -C target_cpu=v3 a1.rs -o a1.o

; CHECK: 0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
; CHECK-NEXT: 1: 00000000000002a0 24 FUNC GLOBAL DEFAULT 6 func_1
; CHECK-NEXT: 2: 00000000000002b8 56 FUNC GLOBAL DEFAULT 6 entrypoint
; CHECK-NOT: 3:
44 changes: 44 additions & 0 deletions lld/test/ELF/sbf-dynsyms-local.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
; RUN: ld.lld -z notext -shared -Bdynamic -entry entrypoint %S/Inputs/sbf-dynsyms-local.o -o %t1.so
; RUN: llvm-readelf %t1.so --dyn-syms | FileCheck %s
; RUN: ld.lld -z notext -shared -Bdynamic -entry entrypoint %S/Inputs/sbf-dynsyms-local.o -strip-all -o %t1.so
; RUN: llvm-readelf %t1.so --dyn-syms | FileCheck --check-prefix=CHECK-STRIP %s

; Ensure local symbols appear in the dynamic symbol table
; sbf-dynsyms-local.o contains the following rust code
;
; #[inline(never)]
; fn func_1(c: u64) -> u64 {
; c % 67
; }
;
; #[inline(never)]
; fn func_2(a: u64) -> u64 {
; return a % 67;
; }
;
; #[no_mangle]
; pub unsafe extern "C" fn entrypoint(a: u64, b: u64) -> u64 {
; let r1 = func_1(a);
; let r2 = func_2(b);
; r1 + r2
; }
;
; #[inline(never)]
; fn func_3(c: u64) -> u64 {
; c % 68
; }
;
; Compiled with the command:
; rustc --target sbf-solana-solana --crate-type lib -C panic=abort -C opt-level=2 -C target_cpu=v3 a1.rs -o a1.o

; CHECK: 0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
; CHECK-NEXT: 1: 00000000000002c8 24 FUNC LOCAL DEFAULT 5 _ZN2a16func_117he4a7267c4c063035E
; CHECK-NEXT: 2: 00000000000002e0 104 FUNC GLOBAL DEFAULT 5 entrypoint
; CHECK-NEXT: 3: 0000000000000348 24 FUNC LOCAL DEFAULT 5 _ZN2a16func_317ha1589f0ded44505fE
; CHECK-NOT: 4:

; CHECK-STRIP: 0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
; CHECK-STRIP-NEXT: 1: 0000000000000290 24 FUNC LOCAL DEFAULT 5 hidden_func
; CHECK-STRIP-NEXT: 2: 00000000000002a8 104 FUNC GLOBAL DEFAULT 5 entrypoint
; CHECK-STRIP-NEXT: 3: 0000000000000310 24 FUNC LOCAL DEFAULT 5 hidden_func
; CHECK-STRIP-NOT: 4:

0 comments on commit 199217f

Please sign in to comment.