From adc2d31acbcd85d1d636fe025f48c616ba8db3c6 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Fri, 19 Jan 2024 20:45:16 +0000 Subject: [PATCH] [lld][ELF] Add `--why-extract` for bitcode libcalls The Wasm linker already records these and its seems useful to do so. --- lld/ELF/Driver.cpp | 5 ++++- lld/test/ELF/lto/libcall-archive.ll | 6 +++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp index 048ef0a606188..f14a2376601b0 100644 --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -2114,8 +2114,11 @@ static void handleUndefinedGlob(StringRef arg) { static void handleLibcall(StringRef name) { Symbol *sym = symtab.find(name); - if (sym && sym->isLazy() && isa(sym->file)) + if (sym && sym->isLazy() && isa(sym->file)) { + if (!config->whyExtract.empty()) + ctx.whyExtractRecords.emplace_back("", sym->file, *sym); sym->extract(); + } } static void writeArchiveStats() { diff --git a/lld/test/ELF/lto/libcall-archive.ll b/lld/test/ELF/lto/libcall-archive.ll index bd91d0391dc27..84ddc1f555872 100644 --- a/lld/test/ELF/lto/libcall-archive.ll +++ b/lld/test/ELF/lto/libcall-archive.ll @@ -4,11 +4,15 @@ ; RUN: llvm-as -o %t2.o %S/Inputs/libcall-archive.ll ; RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux -o %t3.o %S/Inputs/libcall-archive.s ; RUN: llvm-ar rcs %t.a %t2.o %t3.o -; RUN: ld.lld -o %t %t.o %t.a +; RUN: ld.lld --why-extract=why.txt -o %t %t.o %t.a +; RUN: FileCheck %s --input-file=why.txt --check-prefix=CHECK-WHY ; RUN: llvm-nm %t | FileCheck %s ; RUN: ld.lld -o %t2 %t.o --start-lib %t2.o %t3.o --end-lib ; RUN: llvm-nm %t2 | FileCheck %s +; CHECK-WHY: reference extracted symbol +; CHECK-WHY-NEXT: {{.*}}tmp.a({{.*}}tmp2.o) memcpy + ; CHECK-NOT: T __sync_val_compare_and_swap_8 ; CHECK: T _start ; CHECK: T memcpy