llvm.org GIT mirror llvm / 76c17d3
IR: Don't allow non-default visibility on local linkage Visibilities of `hidden` and `protected` are meaningless for symbols with local linkage. - Change the assembler to reject non-default visibility on symbols with local linkage. - Change the bitcode reader to auto-upgrade `hidden` and `protected` to `default` when the linkage is local. - Update LangRef. <rdar://problem/16141113> git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@208263 91177308-0d34-0410-b5e6-96231b3b80d8 Duncan P. N. Exon Smith 6 years ago
23 changed file(s) with 199 addition(s) and 25 deletion(s). Raw diff Collapse all Expand all
438438 placed in the dynamic symbol table, but that references within the
439439 defining module will bind to the local symbol. That is, the symbol
440440 cannot be overridden by another module.
441
442 A symbol with ``internal`` or ``private`` linkage must have ``default``
443 visibility.
441444
442445 .. _namedtypes:
443446
621621 return false;
622622 }
623623
624 static bool isValidVisibilityForLinkage(unsigned V, unsigned L) {
625 return !GlobalValue::isLocalLinkage((GlobalValue::LinkageTypes)L) ||
626 (GlobalValue::VisibilityTypes)V == GlobalValue::DefaultVisibility;
627 }
628
624629 /// ParseAlias:
625630 /// ::= GlobalVar '=' OptionalVisibility OptionalDLLStorageClass 'alias'
626631 /// OptionalLinkage Aliasee
645650 if(!GlobalAlias::isValidLinkage(Linkage))
646651 return Error(LinkageLoc, "invalid linkage type for alias");
647652
653 if (!isValidVisibilityForLinkage(Visibility, L))
654 return Error(LinkageLoc,
655 "symbol with local linkage must have default visibility");
656
648657 Constant *Aliasee;
649658 LocTy AliaseeLoc = Lex.getLoc();
650659 if (Lex.getKind() != lltok::kw_bitcast &&
713722 bool LLParser::ParseGlobal(const std::string &Name, LocTy NameLoc,
714723 unsigned Linkage, bool HasLinkage,
715724 unsigned Visibility, unsigned DLLStorageClass) {
725 if (!isValidVisibilityForLinkage(Visibility, Linkage))
726 return Error(NameLoc,
727 "symbol with local linkage must have default visibility");
728
716729 unsigned AddrSpace;
717730 bool IsConstant, UnnamedAddr, IsExternallyInitialized;
718731 GlobalVariable::ThreadLocalMode TLM;
30133026 return Error(LinkageLoc, "invalid function linkage type");
30143027 }
30153028
3029 if (!isValidVisibilityForLinkage(Visibility, Linkage))
3030 return Error(LinkageLoc,
3031 "symbol with local linkage must have default visibility");
3032
30163033 if (!FunctionType::isValidReturnType(RetType))
30173034 return Error(RetTypeLoc, "invalid function return type");
30183035
18551855 Section = SectionTable[Record[5]-1];
18561856 }
18571857 GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility;
1858 if (Record.size() > 6)
1858 // Local linkage must have default visibility.
1859 if (Record.size() > 6 && !GlobalValue::isLocalLinkage(Linkage))
1860 // FIXME: Change to an error if non-default in 4.0.
18591861 Visibility = GetDecodedVisibility(Record[6]);
18601862
18611863 GlobalVariable::ThreadLocalMode TLM = GlobalVariable::NotThreadLocal;
19211923 return Error(InvalidID);
19221924 Func->setSection(SectionTable[Record[6]-1]);
19231925 }
1924 Func->setVisibility(GetDecodedVisibility(Record[7]));
1926 // Local linkage must have default visibility.
1927 if (!Func->hasLocalLinkage())
1928 // FIXME: Change to an error if non-default in 4.0.
1929 Func->setVisibility(GetDecodedVisibility(Record[7]));
19251930 if (Record.size() > 8 && Record[8]) {
19261931 if (Record[8]-1 > GCTable.size())
19271932 return Error(InvalidID);
19631968 GlobalAlias *NewGA = new GlobalAlias(Ty, GetDecodedLinkage(Record[2]),
19641969 "", nullptr, TheModule);
19651970 // Old bitcode files didn't have visibility field.
1966 if (Record.size() > 3)
1971 // Local linkage must have default visibility.
1972 if (Record.size() > 3 && !NewGA->hasLocalLinkage())
1973 // FIXME: Change to an error if non-default in 4.0.
19671974 NewGA->setVisibility(GetDecodedVisibility(Record[3]));
19681975 if (Record.size() > 4)
19691976 NewGA->setDLLStorageClass(GetDecodedDLLStorageClass(Record[4]));
0 ; RUN: not llvm-as < %s -o /dev/null 2>&1 | FileCheck %s
1
2 @global = global i32 0
3
4 @alias = hidden alias internal i32* @global
5 ; CHECK: symbol with local linkage must have default visibility
0 ; RUN: not llvm-as < %s -o /dev/null 2>&1 | FileCheck %s
1
2 define internal hidden void @function() {
3 ; CHECK: symbol with local linkage must have default visibility
4 entry:
5 ret void
6 }
0 ; RUN: not llvm-as < %s -o /dev/null 2>&1 | FileCheck %s
1
2 @var = internal hidden global i32 0
3 ; CHECK: symbol with local linkage must have default visibility
0 ; RUN: not llvm-as < %s -o /dev/null 2>&1 | FileCheck %s
1
2 @global = global i32 0
3
4 @alias = protected alias internal i32* @global
5 ; CHECK: symbol with local linkage must have default visibility
0 ; RUN: not llvm-as < %s -o /dev/null 2>&1 | FileCheck %s
1
2 define internal protected void @function() {
3 ; CHECK: symbol with local linkage must have default visibility
4 entry:
5 ret void
6 }
0 ; RUN: not llvm-as < %s -o /dev/null 2>&1 | FileCheck %s
1
2 @var = internal protected global i32 0
3 ; CHECK: symbol with local linkage must have default visibility
0 ; RUN: not llvm-as < %s -o /dev/null 2>&1 | FileCheck %s
1
2 @global = global i32 0
3
4 @alias = hidden alias private i32* @global
5 ; CHECK: symbol with local linkage must have default visibility
0 ; RUN: not llvm-as < %s -o /dev/null 2>&1 | FileCheck %s
1
2 define private hidden void @function() {
3 ; CHECK: symbol with local linkage must have default visibility
4 entry:
5 ret void
6 }
0 ; RUN: not llvm-as < %s -o /dev/null 2>&1 | FileCheck %s
1
2 @var = private hidden global i32 0
3 ; CHECK: symbol with local linkage must have default visibility
0 ; RUN: not llvm-as < %s -o /dev/null 2>&1 | FileCheck %s
1
2 @global = global i32 0
3
4 @alias = protected alias private i32* @global
5 ; CHECK: symbol with local linkage must have default visibility
0 ; RUN: not llvm-as < %s -o /dev/null 2>&1 | FileCheck %s
1
2 define private protected void @function() {
3 ; CHECK: symbol with local linkage must have default visibility
4 entry:
5 ret void
6 }
0 ; RUN: not llvm-as < %s -o /dev/null 2>&1 | FileCheck %s
1
2 @var = private protected global i32 0
3 ; CHECK: symbol with local linkage must have default visibility
0 ; RUN: llvm-dis < %s.bc | FileCheck %s
1
2 ; local-linkage-default-visibility.3.4.ll.bc was generated by passing this file
3 ; to llvm-as-3.4. The test checks that LLVM upgrades visibility of symbols
4 ; with local linkage to default visibility.
5
6 @default.internal.var = internal global i32 0
7 ; CHECK: @default.internal.var = internal global i32 0
8
9 @hidden.internal.var = internal hidden global i32 0
10 ; CHECK: @hidden.internal.var = internal global i32 0
11
12 @protected.internal.var = internal protected global i32 0
13 ; CHECK: @protected.internal.var = internal global i32 0
14
15 @default.private.var = private global i32 0
16 ; CHECK: @default.private.var = private global i32 0
17
18 @hidden.private.var = private hidden global i32 0
19 ; CHECK: @hidden.private.var = private global i32 0
20
21 @protected.private.var = private protected global i32 0
22 ; CHECK: @protected.private.var = private global i32 0
23
24 @global = global i32 0
25
26 @default.internal.alias = alias internal i32* @global
27 ; CHECK: @default.internal.alias = alias internal i32* @global
28
29 @hidden.internal.alias = hidden alias internal i32* @global
30 ; CHECK: @hidden.internal.alias = alias internal i32* @global
31
32 @protected.internal.alias = protected alias internal i32* @global
33 ; CHECK: @protected.internal.alias = alias internal i32* @global
34
35 @default.private.alias = alias private i32* @global
36 ; CHECK: @default.private.alias = alias private i32* @global
37
38 @hidden.private.alias = hidden alias private i32* @global
39 ; CHECK: @hidden.private.alias = alias private i32* @global
40
41 @protected.private.alias = protected alias private i32* @global
42 ; CHECK: @protected.private.alias = alias private i32* @global
43
44 define internal void @default.internal() {
45 ; CHECK: define internal void @default.internal
46 entry:
47 ret void
48 }
49
50 define internal hidden void @hidden.internal() {
51 ; CHECK: define internal void @hidden.internal
52 entry:
53 ret void
54 }
55
56 define internal protected void @protected.internal() {
57 ; CHECK: define internal void @protected.internal
58 entry:
59 ret void
60 }
61
62 define private void @default.private() {
63 ; CHECK: define private void @default.private
64 entry:
65 ret void
66 }
67
68 define private hidden void @hidden.private() {
69 ; CHECK: define private void @hidden.private
70 entry:
71 ret void
72 }
73
74 define private protected void @protected.private() {
75 ; CHECK: define private void @protected.private
76 entry:
77 ret void
78 }
1919 ret i32 0
2020 }
2121
22 define internal hidden i32 @printf(i8* readonly nocapture %fmt, ...) {
22 define internal i32 @printf(i8* readonly nocapture %fmt, ...) {
2323 entry:
2424 %ret = call i32 @bar(i8* %fmt)
2525 ret i32 %ret
2626 }
2727
2828 ; CHECK: define {{.*}} @puts(
29 define internal hidden i32 @puts(i8* %s) {
29 define internal i32 @puts(i8* %s) {
3030 entry:
3131 %ret = call i32 @bar(i8* %s)
3232 ret i32 %ret
0 ; RUN: opt -S -globalopt < %s | FileCheck %s
11
22 @_Z17in_custom_section = internal global i8 42, section "CUSTOM"
3 @in_custom_section = protected dllexport alias internal i8* @_Z17in_custom_section
3 @in_custom_section = dllexport alias internal i8* @_Z17in_custom_section
44
5 ; CHECK: @in_custom_section = internal protected dllexport global i8 42, section "CUSTOM"
5 ; CHECK: @in_custom_section = internal dllexport global i8 42, section "CUSTOM"
66
77 @llvm.used = appending global [1 x i8*] [i8* @in_custom_section], section "llvm.metadata"
0 ; RUN: opt < %s -globalopt -S | FileCheck %s
11
22 ; CHECK: ModuleID
3 define internal hidden i32 @__cxa_atexit(void (i8*)* nocapture %func, i8* nocapture %arg, i8* nocapture %dso_handle) nounwind readnone optsize noimplicitfloat {
3 define internal i32 @__cxa_atexit(void (i8*)* nocapture %func, i8* nocapture %arg, i8* nocapture %dso_handle) nounwind readnone optsize noimplicitfloat {
44 unreachable
55 }
77 %.qux.2585 = type { i32, i32, i8* }
88
99 @g2 = external unnamed_addr constant [9 x i8], align 1
10 @g3 = internal hidden unnamed_addr constant [1 x i8*] [i8* bitcast (i8* (%.qux.2585*)* @func35 to i8*)]
10 @g3 = internal unnamed_addr constant [1 x i8*] [i8* bitcast (i8* (%.qux.2585*)* @func35 to i8*)]
1111
12 define internal hidden i32 @func1(i32* %ptr, { i32, i32 }* nocapture %method) align 2 {
12 define internal i32 @func1(i32* %ptr, { i32, i32 }* nocapture %method) align 2 {
1313 br label %1
1414
1515 ;
1919 ret i32 undef
2020 }
2121
22 define internal hidden i32 @func10(%.qux.2496* nocapture %this) align 2 {
22 define internal i32 @func10(%.qux.2496* nocapture %this) align 2 {
2323 %1 = getelementptr inbounds %.qux.2496* %this, i32 0, i32 1, i32 1
2424 %2 = load i32* %1, align 4
2525 ret i32 %2
2626 }
2727
28 define internal hidden i8* @func29(i32* nocapture %this) align 2 {
28 define internal i8* @func29(i32* nocapture %this) align 2 {
2929 ret i8* getelementptr inbounds ([9 x i8]* @g2, i32 0, i32 0)
3030 }
3131
32 define internal hidden i32* @func33(%.qux.2585* nocapture %this) align 2 {
32 define internal i32* @func33(%.qux.2585* nocapture %this) align 2 {
3333 ret i32* undef
3434 }
3535
36 define internal hidden i32* @func34(%.qux.2585* nocapture %this) align 2 {
36 define internal i32* @func34(%.qux.2585* nocapture %this) align 2 {
3737 %1 = getelementptr inbounds %.qux.2585* %this, i32 0
3838 ret i32* undef
3939 }
4040
41 define internal hidden i8* @func35(%.qux.2585* nocapture %this) align 2 {
41 define internal i8* @func35(%.qux.2585* nocapture %this) align 2 {
4242 %1 = getelementptr inbounds %.qux.2585* %this, i32 0, i32 2
4343 %2 = load i8** %1, align 4
4444 ret i8* %2
55 %.qux.2585 = type { i32, i32, i8* }
66
77 @g2 = external addrspace(1) constant [9 x i8], align 1
8 @g3 = internal hidden unnamed_addr constant [1 x i8*] [i8* bitcast (i8* (%.qux.2585 addrspace(1)*)* @func35 to i8*)]
8 @g3 = internal unnamed_addr constant [1 x i8*] [i8* bitcast (i8* (%.qux.2585 addrspace(1)*)* @func35 to i8*)]
99
1010
11 define internal hidden i32 @func10(%.qux.2496 addrspace(1)* nocapture %this) align 2 {
11 define internal i32 @func10(%.qux.2496 addrspace(1)* nocapture %this) align 2 {
1212 bb:
1313 %tmp = getelementptr inbounds %.qux.2496 addrspace(1)* %this, i32 0, i32 1, i32 1
1414 %tmp1 = load i32 addrspace(1)* %tmp, align 4
1616 }
1717
1818 ; Check for pointer bitwidth equal assertion failure
19 define internal hidden i8* @func35(%.qux.2585 addrspace(1)* nocapture %this) align 2 {
19 define internal i8* @func35(%.qux.2585 addrspace(1)* nocapture %this) align 2 {
2020 bb:
2121 ; CHECK-LABEL: @func35(
2222 ; CHECK: %[[V2:.+]] = bitcast %.qux.2585 addrspace(1)* %{{.*}} to %.qux.2496 addrspace(1)*
77 %.qux.2585 = type { i32, i32, i8* }
88
99 @g2 = external unnamed_addr constant [9 x i8], align 1
10 @g3 = internal hidden unnamed_addr constant [1 x i8*] [i8* bitcast (i8* (%.qux.2585*)* @func35 to i8*)]
10 @g3 = internal unnamed_addr constant [1 x i8*] [i8* bitcast (i8* (%.qux.2585*)* @func35 to i8*)]
1111
12 define internal hidden i32 @func1(i32* %ptr, { i32, i32 }* nocapture %method) align 2 {
12 define internal i32 @func1(i32* %ptr, { i32, i32 }* nocapture %method) align 2 {
1313 bb:
1414 br label %bb1
1515
2020 ret i32 undef
2121 }
2222
23 define internal hidden i32 @func10(%.qux.2496* nocapture %this) align 2 {
23 define internal i32 @func10(%.qux.2496* nocapture %this) align 2 {
2424 bb:
2525 %tmp = getelementptr inbounds %.qux.2496* %this, i32 0, i32 1, i32 1
2626 %tmp1 = load i32* %tmp, align 4
2727 ret i32 %tmp1
2828 }
2929
30 define internal hidden i8* @func29(i32* nocapture %this) align 2 {
30 define internal i8* @func29(i32* nocapture %this) align 2 {
3131 bb:
3232 ret i8* getelementptr inbounds ([9 x i8]* @g2, i32 0, i32 0)
3333 }
3434
35 define internal hidden i32* @func33(%.qux.2585* nocapture %this) align 2 {
35 define internal i32* @func33(%.qux.2585* nocapture %this) align 2 {
3636 bb:
3737 ret i32* undef
3838 }
3939
40 define internal hidden i32* @func34(%.qux.2585* nocapture %this) align 2 {
40 define internal i32* @func34(%.qux.2585* nocapture %this) align 2 {
4141 bb:
4242 %tmp = getelementptr inbounds %.qux.2585* %this, i32 0
4343 ret i32* undef
4444 }
4545
46 define internal hidden i8* @func35(%.qux.2585* nocapture %this) align 2 {
46 define internal i8* @func35(%.qux.2585* nocapture %this) align 2 {
4747 bb:
4848 ; CHECK-LABEL: @func35(
4949 ; CHECK: %[[V2:.+]] = bitcast %.qux.2585* %{{.*}} to %.qux.2496*