Skip to content

Commit 608fb46

Browse files
authored
[lld] Discard SHT_LLVM_LTO sections in relocatable links (#92825)
So long as ld -r links using bitcode always result in an ELF object, and not a merged bitcode object, the output form a relocatable link using FatLTO objects should not have a .llvm.lto section. Prior to this, using the object code sections would cause the bitcode section in the output of a relocatable link to be corrupted, by concatenating all the .llvm.lto sections together. This patch discards SHT_LLVM_LTO sections when not using --fat-lto-objects, so that the relocatable ELF output won't contain inalid bitcode.
1 parent 96af114 commit 608fb46

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

lld/ELF/InputFiles.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,16 @@ void ObjFile<ELFT>::initializeSections(bool ignoreComdats,
844844
this->sections[i] =
845845
createInputSection(i, sec, check(obj.getSectionName(sec, shstrtab)));
846846
break;
847+
case SHT_LLVM_LTO:
848+
// Discard .llvm.lto in a relocatable link that does not use the bitcode.
849+
// The concatenated output does not properly reflect the linking
850+
// semantics. In addition, since we do not use the bitcode wrapper format,
851+
// the concatenated raw bitcode would be invalid.
852+
if (config->relocatable && !config->fatLTOObjects) {
853+
sections[i] = &InputSection::discarded;
854+
break;
855+
}
856+
[[fallthrough]];
847857
default:
848858
this->sections[i] =
849859
createInputSection(i, sec, check(obj.getSectionName(sec, shstrtab)));

lld/test/ELF/fatlto/fatlto.test

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
; RUN: opt < a-LTO.ll --module-summary -o a-fatLTO.bc
99
; RUN: llvm-objcopy --add-section=.llvm.lto=a-fatLTO.bc --set-section-flags=.llvm.lto=exclude --set-section-type=.llvm.lto=0x6fff4c0c a-fatLTO.o
1010

11-
1211
; RUN: llc main-LTO.ll --filetype=obj -o main-fatLTO.o --relocation-model=pic
1312
; RUN: opt < main-LTO.ll --module-summary -o main-fatLTO.bc
1413
; RUN: llvm-objcopy --add-section=.llvm.lto=main-fatLTO.bc --set-section-flags=.llvm.lto=exclude --set-section-type=.llvm.lto=0x6fff4c0c main-fatLTO.o
@@ -17,11 +16,6 @@
1716
; RUN: llvm-readelf -S main-fatLTO.o | FileCheck --check-prefix=HAS_LLVM_LTO %s
1817

1918
;; Make sure that the section flags are set correctly
20-
; HA_LLVM_LTO: Name: .llvm.lto
21-
; HA_LLVM_LTO-NEXT: Type: SHT_LLVM_LTO
22-
; HA_LLVM_LTO-NEXT: Flags
23-
; HA_LLVM_LTO-NEXT: SHF_EXCLUDE
24-
2519
; HAS_LLVM_LTO: Name Type Address Off Size ES Flg Lk Inf Al
2620
; HAS_LLVM_LTO: .llvm.lto LLVM_LTO {{.*}} 00 WE 0 0 1
2721

@@ -64,16 +58,13 @@
6458
; RUN: ld.lld -o foo-fatLTO.archive a.a main-LTO.bc --fat-lto-objects
6559
; RUN: cmp foo-fatLTO.archive foo-LTO
6660

67-
;; Test FatLTO works with relocatable links using PIC objects
68-
;; Currently, with PIC relocatable links, FatLTO sections are treated as
69-
;; orphan sections and incorrectly concatenated together. This test verifies
70-
;; the current behavior, but should be fixed to either merge those sections
71-
;; correctly, or to drop them altogether.
61+
;; Test FatLTO works with relocatable links using PIC objects, and that
62+
;; SHT_LLVM_LTO sections are discarded.
7263
; RUN: llvm-ar rcs fatLTO-pic.a a-fatLTO.o main-fatLTO.o
7364
; RUN: llvm-readelf -S fatLTO-pic.a | FileCheck --check-prefix=HAS_LLVM_LTO %s
7465

75-
; RUN: ld.lld --whole-archive fatLTO-pic.a -r -o fatLTO-pic-reolcatable.o
76-
; RUN: llvm-readelf -S fatLTO-pic-reolcatable.o | FileCheck --check-prefix=HAS_LLVM_LTO %s
66+
; RUN: ld.lld --whole-archive fatLTO-pic.a -r -o fatLTO-pic-relocatable.o
67+
; RUN: llvm-readelf -S fatLTO-pic-relocatable.o | FileCheck --check-prefix=CHECK-NON-LTO-TARGET %s
7768

7869
;--- a-LTO.ll
7970
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"

0 commit comments

Comments
 (0)