Skip to content

[lld] Discard SHT_LLVM_LTO sections in relocatable links #92825

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 3 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
10 changes: 10 additions & 0 deletions lld/ELF/InputFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,16 @@ void ObjFile<ELFT>::initializeSections(bool ignoreComdats,
this->sections[i] =
createInputSection(i, sec, check(obj.getSectionName(sec, shstrtab)));
break;
case SHT_LLVM_LTO:
// Discard .llvm.lto in a relocatable link that does not use the bitcode.
// The concatenated output does not properly reflect the linking
// semantics. In addition, since we do not use the bitcode wrapper format,
// the concatenated raw bitcode would be invalid.
if (config->relocatable && !config->fatLTOObjects) {
sections[i] = &InputSection::discarded;
break;
}
[[fallthrough]];
default:
this->sections[i] =
createInputSection(i, sec, check(obj.getSectionName(sec, shstrtab)));
Expand Down
38 changes: 38 additions & 0 deletions lld/test/ELF/fatlto/fatlto.test
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,22 @@
; RUN: ld.lld -o %t/foo-fatLTO.archive %t/a.a %t/main-LTO.bc --fat-lto-objects
; RUN: cmp %t/foo-fatLTO.archive %t/foo-LTO

;; Test FatLTO works with relocatable links using PIC objects
; RUN: opt < %t/a-LTO.ll -passes="embed-bitcode<thinlto;emit-summary>" | llc --relocation-model=pic --filetype=obj -o %t/a-fat-pic.o
; RUN: llvm-readobj -S %t/a-fat-pic.o | FileCheck --check-prefix=HAS_LLVM_LTO %s

; RUN: opt < %t/b-LTO.ll -passes="embed-bitcode<thinlto;emit-summary>" | llc --relocation-model=pic --filetype=obj -o %t/b-fat-pic.o
; RUN: llvm-readobj -S %t/b-fat-pic.o | FileCheck --check-prefix=HAS_LLVM_LTO %s

; RUN: llvm-ar rcs %t/fat.pic.archive %t/a-fat-pic.o %t/b-fat-pic.o
; RUN: llvm-readobj -S %t/fat.pic.archive | FileCheck --check-prefix=HAS_LLVM_LTO %s

; RUN: ld.lld --whole-archive %t/fat.pic.archive -r -o %t/fat-pic-relocatable.o
; RUN: llvm-readobj -S %t/fat-pic-relocatable.o | FileCheck --check-prefix=CHECK-NON-LTO-TARGET %s

; HAS_LLVM_LTO: Name: .llvm.lto
; HAS_LLVM_LTO: Type: SHT_LLVM_LTO

;--- a-LTO.ll
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"
Expand All @@ -71,6 +87,28 @@ attributes #0 = { noinline nounwind uwtable }
!5 = !{i32 1, !"ThinLTO", i32 0}
!6 = !{i32 1, !"EnableSplitLTOUnit", i32 1}

;--- b-LTO.ll
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

; Function Attrs: noinline nounwind uwtable
define dso_local i32 @foo() #0 {
entry:
ret i32 0
}

attributes #0 = { noinline nounwind uwtable }

!llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 7, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{i32 7, !"frame-pointer", i32 2}
!5 = !{i32 1, !"ThinLTO", i32 0}
!6 = !{i32 1, !"EnableSplitLTOUnit", i32 1}

;--- main-LTO.ll
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"
Expand Down
3 changes: 3 additions & 0 deletions llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,9 @@ static unsigned getELFSectionType(StringRef Name, SectionKind K) {
if (K.isBSS() || K.isThreadBSS())
return ELF::SHT_NOBITS;

if(hasPrefix(Name, ".llvm.lto"))
return ELF::SHT_LLVM_LTO;

return ELF::SHT_PROGBITS;
}

Expand Down
2 changes: 1 addition & 1 deletion llvm/test/CodeGen/X86/fat-lto-section.ll
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
; RUN: | FileCheck %s --check-prefix=EXCLUDE

; EXCLUDE: Name Type {{.*}} ES Flg Lk Inf Al
; EXCLUDE: .llvm.lto PROGBITS {{.*}} 00 E 0 0 1
; EXCLUDE: .llvm.lto LLVM_LTO {{.*}} 00 E 0 0 1

@a = global i32 1
Loading