llvm.org GIT mirror llvm / ead8765
[LTO/gold] Support --wrap Summary: (Restores r327459 with handling for old plugin-api.h) Utilize new gold plugin api interface for obtaining --wrap option arguments, and LTO API handling (added for --wrap support in lld LTO), to mark symbols so that LTO does not optimize them inappropriately. Note the test cases will be in a new gold test subdirectory that is dependent on the next release of gold which will contain the new interfaces. Reviewers: pcc, tmsriram Subscribers: mehdi_amini, llvm-commits, inglorion Differential Revision: https://reviews.llvm.org/D44235 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@327506 91177308-0d34-0410-b5e6-96231b3b80d8 Teresa Johnson 2 years ago
5 changed file(s) with 187 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
1 target triple = "x86_64-unknown-linux-gnu"
2
3 define hidden void @bar() {
4 ret void
5 }
6
7 define hidden void @__real_bar() {
8 ret void
9 }
10
11 define hidden void @__wrap_bar() {
12 ret void
13 }
0 import re
1 import subprocess
2
3 def is_gold_v1_16_linker_available():
4
5 if not config.gold_executable:
6 return False
7 try:
8 ld_cmd = subprocess.Popen([config.gold_executable, '-v'],
9 stdout = subprocess.PIPE,
10 stderr = subprocess.PIPE)
11 ld_out, _ = ld_cmd.communicate()
12 ld_out = ld_out.decode()
13 except:
14 return False
15
16 match = re.search(r'GNU gold \(.*\) (\d+)\.(\d+)', ld_out)
17 if not match:
18 return False
19 major = int(match.group(1))
20 minor = int(match.group(2))
21 if major < 1 or (major == 1 and minor < 16):
22 return False
23
24 return True
25
26 if not is_gold_v1_16_linker_available():
27 config.unsupported = True
0 ; LTO
1 ; RUN: llvm-as %s -o %t.o
2 ; RUN: %gold -plugin %llvmshlibdir/LLVMgold%shlibext %t.o -o %t.out -wrap=bar -plugin-opt=save-temps
3 ; RUN: llvm-readobj -t %t.out | FileCheck %s
4 ; RUN: cat %t.out.resolution.txt | FileCheck -check-prefix=RESOLS %s
5
6 ; ThinLTO
7 ; RUN: opt -module-summary %s -o %t.o
8 ; RUN: %gold -plugin %llvmshlibdir/LLVMgold%shlibext %t.o -o %t.out -wrap=bar -plugin-opt=save-temps
9 ; RUN: llvm-readobj -t %t.out | FileCheck %s
10 ; RUN: cat %t.out.resolution.txt | FileCheck -check-prefix=RESOLS %s
11
12 ; CHECK: Name: __wrap_bar
13 ; CHECK-NEXT: Value:
14 ; CHECK-NEXT: Size:
15 ; CHECK-NEXT: Binding: Global
16 ; CHECK-NEXT: Type: Function
17
18 ; Make sure that the 'r' (linker redefined) bit is set for bar and __real_bar
19 ; in the resolutions file, and that the 'x' (visible to regular obj) bit is set
20 ; for bar and __wrap_bar.
21 ; RESOLS: ,bar,lxr
22 ; RESOLS: ,__wrap_bar,plx
23 ; RESOLS: ,__real_bar,plr
24
25 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
26 target triple = "x86_64-unknown-linux-gnu"
27
28 declare void @bar()
29
30 define void @_start() {
31 call void @bar()
32 ret void
33 }
34
35 define void @__wrap_bar() {
36 ret void
37 }
38
39 define void @__real_bar() {
40 ret void
41 }
0 ; LTO
1 ; This doesn't currently work with gold, because it does not apply defsym
2 ; renaming to symbols in the same module (apparently by design for consistency
3 ; with GNU ld). Because regular LTO hands back a single object file to gold,
4 ; it doesn't perform the desired defsym renaming. This isn't an issue with
5 ; ThinLTO which hands back multiple native objects to gold. For regular
6 ; LTO defsym handling, gold will need a fix (not the gold plugin).
7 ; RUN-TODO: llvm-as %s -o %t.o
8 ; RUN-TODO: llvm-as %S/Inputs/wrap-bar.ll -o %t1.o
9 ; RUN-TODO: %gold -plugin %llvmshlibdir/LLVMgold%shlibext %t.o %t1.o -shared -o %t.so -wrap=bar
10 ; RUN-TODO: llvm-objdump -d %t.so | FileCheck %s
11 ; RUN-TODO: llvm-readobj -t %t.so | FileCheck -check-prefix=BIND %s
12
13 ; ThinLTO
14 ; RUN: opt -module-summary %s -o %t.o
15 ; RUN: opt -module-summary %S/Inputs/wrap-bar.ll -o %t1.o
16 ; RUN: %gold -plugin %llvmshlibdir/LLVMgold%shlibext %t.o %t1.o -shared -o %t.so -wrap=bar
17 ; RUN: llvm-objdump -d %t.so | FileCheck %s -check-prefix=THIN
18 ; RUN: llvm-readobj -t %t.so | FileCheck -check-prefix=BIND %s
19
20 ; Make sure that calls in foo() are not eliminated and that bar is
21 ; routed to __wrap_bar and __real_bar is routed to bar.
22
23 ; CHECK: foo:
24 ; CHECK-NEXT: pushq %rax
25 ; CHECK-NEXT: callq{{.*}}<__wrap_bar>
26 ; CHECK-NEXT: callq{{.*}}
27
28 ; THIN: foo:
29 ; THIN-NEXT: pushq %rax
30 ; THIN-NEXT: callq{{.*}}<__wrap_bar>
31 ; THIN-NEXT: popq %rax
32 ; THIN-NEXT: jmp{{.*}}
33
34 ; Check that bar and __wrap_bar retain their original binding.
35 ; BIND: Name: bar
36 ; BIND-NEXT: Value:
37 ; BIND-NEXT: Size:
38 ; BIND-NEXT: Binding: Local
39 ; BIND: Name: __wrap_bar
40 ; BIND-NEXT: Value:
41 ; BIND-NEXT: Size:
42 ; BIND-NEXT: Binding: Local
43
44 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
45 target triple = "x86_64-unknown-linux-gnu"
46
47 declare void @bar()
48 declare void @__real_bar()
49
50 define void @foo() {
51 call void @bar()
52 call void @__real_bar()
53 ret void
54 }
4343
4444 #define LDPT_GET_SYMBOLS_V3 28
4545
46 // FIXME: Remove when binutils 2.31 (containing gold 1.16) is the minimum
47 // required version.
48 #define LDPT_GET_WRAP_SYMBOLS 32
49
4650 using namespace llvm;
4751 using namespace lto;
52
53 // FIXME: Remove when binutils 2.31 (containing gold 1.16) is the minimum
54 // required version.
55 typedef enum ld_plugin_status (*ld_plugin_get_wrap_symbols)(
56 uint64_t *num_symbols, const char ***wrap_symbol_list);
4857
4958 static ld_plugin_status discard_message(int level, const char *format, ...) {
5059 // Die loudly. Recent versions of Gold pass ld_plugin_message as the first
5564 static ld_plugin_release_input_file release_input_file = nullptr;
5665 static ld_plugin_get_input_file get_input_file = nullptr;
5766 static ld_plugin_message message = discard_message;
67 static ld_plugin_get_wrap_symbols get_wrap_symbols = nullptr;
5868
5969 namespace {
6070 struct claimed_file {
92102 struct ResolutionInfo {
93103 bool CanOmitFromDynSym = true;
94104 bool DefaultVisibility = true;
105 bool CanInline = true;
106 bool IsUsedInRegularObj = false;
95107 };
96108
97109 }
366378 case LDPT_MESSAGE:
367379 message = tv->tv_u.tv_message;
368380 break;
381 case LDPT_GET_WRAP_SYMBOLS:
382 // FIXME: When binutils 2.31 (containing gold 1.16) is the minimum
383 // required version, this should be changed to:
384 // get_wrap_symbols = tv->tv_u.tv_get_wrap_symbols;
385 get_wrap_symbols =
386 (ld_plugin_get_wrap_symbols)tv->tv_u.tv_register_new_input;
387 break;
369388 default:
370389 break;
371390 }
562581 }
563582 }
564583
584 // Handle any --wrap options passed to gold, which are than passed
585 // along to the plugin.
586 if (get_wrap_symbols) {
587 const char **wrap_symbols;
588 uint64_t count = 0;
589 if (get_wrap_symbols(&count, &wrap_symbols) != LDPS_OK) {
590 message(LDPL_ERROR, "Unable to get wrap symbols!");
591 return LDPS_ERR;
592 }
593 for (uint64_t i = 0; i < count; i++) {
594 StringRef Name = wrap_symbols[i];
595 ResolutionInfo &Res = ResInfo[Name];
596 ResolutionInfo &WrapRes = ResInfo["__wrap_" + Name.str()];
597 ResolutionInfo &RealRes = ResInfo["__real_" + Name.str()];
598 // Tell LTO not to inline symbols that will be overwritten.
599 Res.CanInline = false;
600 RealRes.CanInline = false;
601 // Tell LTO not to eliminate symbols that will be used after renaming.
602 Res.IsUsedInRegularObj = true;
603 WrapRes.IsUsedInRegularObj = true;
604 }
605 }
606
565607 return LDPS_OK;
566608 }
567609
684726 if (Resolution != LDPR_RESOLVED_DYN && Resolution != LDPR_UNDEF &&
685727 (IsExecutable || !Res.DefaultVisibility))
686728 R.FinalDefinitionInLinkageUnit = true;
729
730 if (!Res.CanInline)
731 R.LinkerRedefined = true;
732
733 if (Res.IsUsedInRegularObj)
734 R.VisibleToRegularObj = true;
687735
688736 freeSymName(Sym);
689737 }