| 1 |
From 8482662676a4b6ef79a718c8c09943cb15241664 Mon Sep 17 00:00:00 2001 |
| 2 |
From: Tom Stellard <[email protected]> |
| 3 |
Date: Tue, 21 Jun 2022 22:22:11 -0700 |
| 4 |
Subject: [PATCH] [gold] Ignore bitcode from sections inside object files |
| 5 |
|
| 6 |
-fembed-bitcode will put bitcode into special sections within object |
| 7 |
files, but this is not meant to be used by LTO, so the gold plugin |
| 8 |
should ignore it. |
| 9 |
|
| 10 |
https://github.com/llvm/llvm-project/issues/47216 |
| 11 |
|
| 12 |
Reviewed By: tejohnson, MaskRay |
| 13 |
|
| 14 |
Differential Revision: https://reviews.llvm.org/D116995 |
| 15 |
--- |
| 16 |
llvm/docs/BitCodeFormat.rst | 3 ++- |
| 17 |
llvm/docs/GoldPlugin.rst | 4 ++++ |
| 18 |
.../tools/gold/X86/Inputs/bcsection-lib.ll | 6 +++++ |
| 19 |
llvm/test/tools/gold/X86/Inputs/bcsection.s | 5 ++++ |
| 20 |
llvm/test/tools/gold/X86/bcsection.ll | 23 +++++++++++++++---- |
| 21 |
llvm/tools/gold/gold-plugin.cpp | 8 +++++++ |
| 22 |
6 files changed, 43 insertions(+), 6 deletions(-) |
| 23 |
create mode 100644 llvm/test/tools/gold/X86/Inputs/bcsection-lib.ll |
| 24 |
|
| 25 |
diff --git a/llvm/docs/BitCodeFormat.rst b/llvm/docs/BitCodeFormat.rst |
| 26 |
index 8e81a7daa459..df1f6915d7d5 100644 |
| 27 |
--- a/llvm/docs/BitCodeFormat.rst |
| 28 |
+++ b/llvm/docs/BitCodeFormat.rst |
| 29 |
@@ -475,7 +475,8 @@ formats. This wrapper format is useful for accommodating LTO in compilation |
| 30 |
pipelines where intermediate objects must be native object files which contain |
| 31 |
metadata in other sections. |
| 32 |
|
| 33 |
-Not all tools support this format. |
| 34 |
+Not all tools support this format. For example, lld and the gold plugin will |
| 35 |
+ignore these sections when linking object files. |
| 36 |
|
| 37 |
.. _encoding of LLVM IR: |
| 38 |
|
| 39 |
diff --git a/llvm/docs/GoldPlugin.rst b/llvm/docs/GoldPlugin.rst |
| 40 |
index ce310bc2cf3c..07d2fc203eba 100644 |
| 41 |
--- a/llvm/docs/GoldPlugin.rst |
| 42 |
+++ b/llvm/docs/GoldPlugin.rst |
| 43 |
@@ -17,6 +17,10 @@ and above also supports LTO via plugins. However, usage of the LLVM |
| 44 |
gold plugin with ld.bfd is not tested and therefore not officially |
| 45 |
supported or recommended. |
| 46 |
|
| 47 |
+As of LLVM 15, the gold plugin will ignore bitcode from the ``.llvmbc`` |
| 48 |
+section inside of ELF object files. However, LTO with bitcode files |
| 49 |
+is still supported. |
| 50 |
+ |
| 51 |
.. _`gold linker`: http://sourceware.org/binutils |
| 52 |
.. _`GCC LTO`: http://gcc.gnu.org/wiki/LinkTimeOptimization |
| 53 |
.. _`gold plugin interface`: http://gcc.gnu.org/wiki/whopr/driver |
| 54 |
diff --git a/llvm/test/tools/gold/X86/Inputs/bcsection-lib.ll b/llvm/test/tools/gold/X86/Inputs/bcsection-lib.ll |
| 55 |
new file mode 100644 |
| 56 |
index 000000000000..ef3557c19cdc |
| 57 |
--- /dev/null |
| 58 |
+++ b/llvm/test/tools/gold/X86/Inputs/bcsection-lib.ll |
| 59 |
@@ -0,0 +1,6 @@ |
| 60 |
+declare void @elf_func() |
| 61 |
+ |
| 62 |
+define i32 @lib_func() { |
| 63 |
+ call void @elf_func() |
| 64 |
+ ret i32 0 |
| 65 |
+} |
| 66 |
diff --git a/llvm/test/tools/gold/X86/Inputs/bcsection.s b/llvm/test/tools/gold/X86/Inputs/bcsection.s |
| 67 |
index ede1e5c532dd..c523612563b4 100644 |
| 68 |
--- a/llvm/test/tools/gold/X86/Inputs/bcsection.s |
| 69 |
+++ b/llvm/test/tools/gold/X86/Inputs/bcsection.s |
| 70 |
@@ -1,2 +1,7 @@ |
| 71 |
+.global elf_func |
| 72 |
+ |
| 73 |
+elf_func: |
| 74 |
+ ret |
| 75 |
+ |
| 76 |
.section .llvmbc |
| 77 |
.incbin "bcsection.bc" |
| 78 |
diff --git a/llvm/test/tools/gold/X86/bcsection.ll b/llvm/test/tools/gold/X86/bcsection.ll |
| 79 |
index 6d3481f8f966..09882d83fe91 100644 |
| 80 |
--- a/llvm/test/tools/gold/X86/bcsection.ll |
| 81 |
+++ b/llvm/test/tools/gold/X86/bcsection.ll |
| 82 |
@@ -2,16 +2,29 @@ |
| 83 |
; RUN: llvm-as -o %t/bcsection.bc %s |
| 84 |
|
| 85 |
; RUN: llvm-mc -I=%t -filetype=obj -triple=x86_64-unknown-unknown -o %t/bcsection.bco %p/Inputs/bcsection.s |
| 86 |
-; RUN: llvm-nm --no-llvm-bc %t/bcsection.bco 2>&1 | FileCheck %s -check-prefix=NO-SYMBOLS |
| 87 |
-; NO-SYMBOLS: no symbols |
| 88 |
+; RUN: llc -filetype=obj -mtriple=x86_64-unknown-unknown -o %t/bcsection-lib.o %p/Inputs/bcsection-lib.ll |
| 89 |
|
| 90 |
-; RUN: %gold -r -o %t/bcsection.o -m elf_x86_64 -plugin %llvmshlibdir/LLVMgold%shlibext %t/bcsection.bco |
| 91 |
-; RUN: llvm-nm --no-llvm-bc %t/bcsection.o | FileCheck %s |
| 92 |
+; RUN: %gold -shared --no-undefined -o %t/bcsection.so -m elf_x86_64 -plugin %llvmshlibdir/LLVMgold%shlibext %t/bcsection.bco %t/bcsection-lib.o |
| 93 |
+ |
| 94 |
+; This test checks that the gold plugin does not attempt to use the bitcode |
| 95 |
+; in the .llvmbc section for LTO. bcsection-lib.o calls a function that is |
| 96 |
+; present the symbol table of bcsection.bco, but not included in the embedded |
| 97 |
+; bitcode. If the linker were to use the bitcode, then the symbols in the |
| 98 |
+; symbol table of bcsection.bco will be ignored and the link will fail. |
| 99 |
+; |
| 100 |
+; bcsection.bco: |
| 101 |
+; .text: |
| 102 |
+; elf_func |
| 103 |
+; .llvmbc: |
| 104 |
+; bitcode_func |
| 105 |
+; |
| 106 |
+; bcsection-lib.o: |
| 107 |
+; calls elf_func() |
| 108 |
|
| 109 |
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" |
| 110 |
target triple = "x86_64-unknown-unknown" |
| 111 |
|
| 112 |
; CHECK: main |
| 113 |
-define i32 @main() { |
| 114 |
+define i32 @bitcode_func() { |
| 115 |
ret i32 0 |
| 116 |
} |
| 117 |
diff --git a/llvm/tools/gold/gold-plugin.cpp b/llvm/tools/gold/gold-plugin.cpp |
| 118 |
index 180c181368e3..294c7a3d6178 100644 |
| 119 |
--- a/llvm/tools/gold/gold-plugin.cpp |
| 120 |
+++ b/llvm/tools/gold/gold-plugin.cpp |
| 121 |
@@ -540,6 +540,14 @@ static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file, |
| 122 |
BufferRef = Buffer->getMemBufferRef(); |
| 123 |
} |
| 124 |
|
| 125 |
+ // Only use bitcode files for LTO. InputFile::create() will load bitcode |
| 126 |
+ // from the .llvmbc section within a binary object, this bitcode is typically |
| 127 |
+ // generated by -fembed-bitcode and is not to be used by LLVMgold.so for LTO. |
| 128 |
+ if (identify_magic(BufferRef.getBuffer()) != file_magic::bitcode) { |
| 129 |
+ *claimed = 0; |
| 130 |
+ return LDPS_OK; |
| 131 |
+ } |
| 132 |
+ |
| 133 |
*claimed = 1; |
| 134 |
|
| 135 |
Expected<std::unique_ptr<InputFile>> ObjOrErr = InputFile::create(BufferRef); |
| 136 |
-- |
| 137 |
2.37.1.1.g659da70093 |
| 138 |
|