Skip to content

Commit e8f3b07

Browse files
committedMar 13, 2018
[LTO/gold] Support --wrap
Summary: 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 llvm-svn: 327459
1 parent b9d2908 commit e8f3b07

File tree

5 files changed

+174
-0
lines changed

5 files changed

+174
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
2+
target triple = "x86_64-unknown-linux-gnu"
3+
4+
define hidden void @bar() {
5+
ret void
6+
}
7+
8+
define hidden void @__real_bar() {
9+
ret void
10+
}
11+
12+
define hidden void @__wrap_bar() {
13+
ret void
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import re
2+
import subprocess
3+
4+
def is_gold_v1_16_linker_available():
5+
6+
if not config.gold_executable:
7+
return False
8+
try:
9+
ld_cmd = subprocess.Popen([config.gold_executable, '-v'],
10+
stdout = subprocess.PIPE,
11+
stderr = subprocess.PIPE)
12+
ld_out, _ = ld_cmd.communicate()
13+
ld_out = ld_out.decode()
14+
except:
15+
return False
16+
17+
match = re.search(r'GNU gold \(.*\) (\d+)\.(\d+)', ld_out)
18+
if not match:
19+
return False
20+
major = int(match.group(1))
21+
minor = int(match.group(2))
22+
if major < 1 or (major == 1 and minor < 16):
23+
return False
24+
25+
return True
26+
27+
if not is_gold_v1_16_linker_available():
28+
config.unsupported = True
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
; LTO
2+
; RUN: llvm-as %s -o %t.o
3+
; RUN: %gold -plugin %llvmshlibdir/LLVMgold%shlibext %t.o -o %t.out -wrap=bar -plugin-opt=save-temps
4+
; RUN: llvm-readobj -t %t.out | FileCheck %s
5+
; RUN: cat %t.out.resolution.txt | FileCheck -check-prefix=RESOLS %s
6+
7+
; ThinLTO
8+
; RUN: opt -module-summary %s -o %t.o
9+
; RUN: %gold -plugin %llvmshlibdir/LLVMgold%shlibext %t.o -o %t.out -wrap=bar -plugin-opt=save-temps
10+
; RUN: llvm-readobj -t %t.out | FileCheck %s
11+
; RUN: cat %t.out.resolution.txt | FileCheck -check-prefix=RESOLS %s
12+
13+
; CHECK: Name: __wrap_bar
14+
; CHECK-NEXT: Value:
15+
; CHECK-NEXT: Size:
16+
; CHECK-NEXT: Binding: Global
17+
; CHECK-NEXT: Type: Function
18+
19+
; Make sure that the 'r' (linker redefined) bit is set for bar and __real_bar
20+
; in the resolutions file, and that the 'x' (visible to regular obj) bit is set
21+
; for bar and __wrap_bar.
22+
; RESOLS: ,bar,lxr
23+
; RESOLS: ,__wrap_bar,plx
24+
; RESOLS: ,__real_bar,plr
25+
26+
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
27+
target triple = "x86_64-unknown-linux-gnu"
28+
29+
declare void @bar()
30+
31+
define void @_start() {
32+
call void @bar()
33+
ret void
34+
}
35+
36+
define void @__wrap_bar() {
37+
ret void
38+
}
39+
40+
define void @__real_bar() {
41+
ret void
42+
}
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
; LTO
2+
; This doesn't currently work with gold, because it does not apply defsym
3+
; renaming to symbols in the same module (apparently by design for consistency
4+
; with GNU ld). Because regular LTO hands back a single object file to gold,
5+
; it doesn't perform the desired defsym renaming. This isn't an issue with
6+
; ThinLTO which hands back multiple native objects to gold. For regular
7+
; LTO defsym handling, gold will need a fix (not the gold plugin).
8+
; RUN-TODO: llvm-as %s -o %t.o
9+
; RUN-TODO: llvm-as %S/Inputs/wrap-bar.ll -o %t1.o
10+
; RUN-TODO: %gold -plugin %llvmshlibdir/LLVMgold%shlibext %t.o %t1.o -shared -o %t.so -wrap=bar
11+
; RUN-TODO: llvm-objdump -d %t.so | FileCheck %s
12+
; RUN-TODO: llvm-readobj -t %t.so | FileCheck -check-prefix=BIND %s
13+
14+
; ThinLTO
15+
; RUN: opt -module-summary %s -o %t.o
16+
; RUN: opt -module-summary %S/Inputs/wrap-bar.ll -o %t1.o
17+
; RUN: %gold -plugin %llvmshlibdir/LLVMgold%shlibext %t.o %t1.o -shared -o %t.so -wrap=bar
18+
; RUN: llvm-objdump -d %t.so | FileCheck %s -check-prefix=THIN
19+
; RUN: llvm-readobj -t %t.so | FileCheck -check-prefix=BIND %s
20+
21+
; Make sure that calls in foo() are not eliminated and that bar is
22+
; routed to __wrap_bar and __real_bar is routed to bar.
23+
24+
; CHECK: foo:
25+
; CHECK-NEXT: pushq %rax
26+
; CHECK-NEXT: callq{{.*}}<__wrap_bar>
27+
; CHECK-NEXT: callq{{.*}}<bar>
28+
29+
; THIN: foo:
30+
; THIN-NEXT: pushq %rax
31+
; THIN-NEXT: callq{{.*}}<__wrap_bar>
32+
; THIN-NEXT: popq %rax
33+
; THIN-NEXT: jmp{{.*}}<bar>
34+
35+
; Check that bar and __wrap_bar retain their original binding.
36+
; BIND: Name: bar
37+
; BIND-NEXT: Value:
38+
; BIND-NEXT: Size:
39+
; BIND-NEXT: Binding: Local
40+
; BIND: Name: __wrap_bar
41+
; BIND-NEXT: Value:
42+
; BIND-NEXT: Size:
43+
; BIND-NEXT: Binding: Local
44+
45+
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
46+
target triple = "x86_64-unknown-linux-gnu"
47+
48+
declare void @bar()
49+
declare void @__real_bar()
50+
51+
define void @foo() {
52+
call void @bar()
53+
call void @__real_bar()
54+
ret void
55+
}

‎llvm/tools/gold/gold-plugin.cpp

+35
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ static ld_plugin_status discard_message(int level, const char *format, ...) {
5656
static ld_plugin_release_input_file release_input_file = nullptr;
5757
static ld_plugin_get_input_file get_input_file = nullptr;
5858
static ld_plugin_message message = discard_message;
59+
static ld_plugin_get_wrap_symbols get_wrap_symbols = nullptr;
5960

6061
namespace {
6162
struct claimed_file {
@@ -93,6 +94,8 @@ struct PluginInputFile {
9394
struct ResolutionInfo {
9495
bool CanOmitFromDynSym = true;
9596
bool DefaultVisibility = true;
97+
bool CanInline = true;
98+
bool IsUsedInRegularObj = false;
9699
};
97100

98101
}
@@ -367,6 +370,9 @@ ld_plugin_status onload(ld_plugin_tv *tv) {
367370
case LDPT_MESSAGE:
368371
message = tv->tv_u.tv_message;
369372
break;
373+
case LDPT_GET_WRAP_SYMBOLS:
374+
get_wrap_symbols = tv->tv_u.tv_get_wrap_symbols;
375+
break;
370376
default:
371377
break;
372378
}
@@ -563,6 +569,29 @@ static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
563569
}
564570
}
565571

572+
// Handle any --wrap options passed to gold, which are than passed
573+
// along to the plugin.
574+
if (get_wrap_symbols) {
575+
const char **wrap_symbols;
576+
uint64_t count = 0;
577+
if (get_wrap_symbols(&count, &wrap_symbols) != LDPS_OK) {
578+
message(LDPL_ERROR, "Unable to get wrap symbols!");
579+
return LDPS_ERR;
580+
}
581+
for (uint64_t i = 0; i < count; i++) {
582+
StringRef Name = wrap_symbols[i];
583+
ResolutionInfo &Res = ResInfo[Name];
584+
ResolutionInfo &WrapRes = ResInfo["__wrap_" + Name.str()];
585+
ResolutionInfo &RealRes = ResInfo["__real_" + Name.str()];
586+
// Tell LTO not to inline symbols that will be overwritten.
587+
Res.CanInline = false;
588+
RealRes.CanInline = false;
589+
// Tell LTO not to eliminate symbols that will be used after renaming.
590+
Res.IsUsedInRegularObj = true;
591+
WrapRes.IsUsedInRegularObj = true;
592+
}
593+
}
594+
566595
return LDPS_OK;
567596
}
568597

@@ -686,6 +715,12 @@ static void addModule(LTO &Lto, claimed_file &F, const void *View,
686715
(IsExecutable || !Res.DefaultVisibility))
687716
R.FinalDefinitionInLinkageUnit = true;
688717

718+
if (!Res.CanInline)
719+
R.LinkerRedefined = true;
720+
721+
if (Res.IsUsedInRegularObj)
722+
R.VisibleToRegularObj = true;
723+
689724
freeSymName(Sym);
690725
}
691726

0 commit comments

Comments
 (0)
Please sign in to comment.