llvm.org GIT mirror llvm / 6a7c74d
Revert r226242 - Revert Revert Don't create new comdats in CodeGen This breaks AddressSanitizer (ninja check-asan) on Windows git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@226251 91177308-0d34-0410-b5e6-96231b3b80d8 Timur Iskhodzhanov 5 years ago
15 changed file(s) with 138 addition(s) and 64 deletion(s). Raw diff Collapse all Expand all
155155 return false;
156156 }
157157
158 static bool hasImplicitComdat(size_t Val) {
159 switch (Val) {
160 default:
161 return false;
162 case 1: // Old WeakAnyLinkage
163 case 4: // Old LinkOnceAnyLinkage
164 case 10: // Old WeakODRLinkage
165 case 11: // Old LinkOnceODRLinkage
166 return true;
167 }
168 }
169
170158 static GlobalValue::LinkageTypes getDecodedLinkage(unsigned Val) {
171159 switch (Val) {
172160 default: // Map unknown/new linkages to external
173161 case 0:
174162 return GlobalValue::ExternalLinkage;
163 case 1:
164 return GlobalValue::WeakAnyLinkage;
175165 case 2:
176166 return GlobalValue::AppendingLinkage;
177167 case 3:
178168 return GlobalValue::InternalLinkage;
169 case 4:
170 return GlobalValue::LinkOnceAnyLinkage;
179171 case 5:
180172 return GlobalValue::ExternalLinkage; // Obsolete DLLImportLinkage
181173 case 6:
186178 return GlobalValue::CommonLinkage;
187179 case 9:
188180 return GlobalValue::PrivateLinkage;
181 case 10:
182 return GlobalValue::WeakODRLinkage;
183 case 11:
184 return GlobalValue::LinkOnceODRLinkage;
189185 case 12:
190186 return GlobalValue::AvailableExternallyLinkage;
191187 case 13:
194190 return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateWeakLinkage
195191 case 15:
196192 return GlobalValue::ExternalLinkage; // Obsolete LinkOnceODRAutoHideLinkage
197 case 1: // Old value with implicit comdat.
198 case 16:
199 return GlobalValue::WeakAnyLinkage;
200 case 10: // Old value with implicit comdat.
201 case 17:
202 return GlobalValue::WeakODRLinkage;
203 case 4: // Old value with implicit comdat.
204 case 18:
205 return GlobalValue::LinkOnceAnyLinkage;
206 case 11: // Old value with implicit comdat.
207 case 19:
208 return GlobalValue::LinkOnceODRLinkage;
209193 }
210194 }
211195
11331117 Value *V = ValueList[ValueID];
11341118
11351119 V->setName(StringRef(ValueName.data(), ValueName.size()));
1136 if (auto *GO = dyn_cast(V)) {
1137 if (GO->getComdat() == reinterpret_cast(1))
1138 GO->setComdat(TheModule->getOrInsertComdat(V->getName()));
1139 }
11401120 ValueName.clear();
11411121 break;
11421122 }
21592139 Ty = cast(Ty)->getElementType();
21602140
21612141 bool isConstant = Record[1];
2162 uint64_t RawLinkage = Record[3];
2163 GlobalValue::LinkageTypes Linkage = getDecodedLinkage(RawLinkage);
2142 GlobalValue::LinkageTypes Linkage = getDecodedLinkage(Record[3]);
21642143 unsigned Alignment = (1 << Record[4]) >> 1;
21652144 std::string Section;
21662145 if (Record[5]) {
21982177 if (Record.size() > 10)
21992178 NewGV->setDLLStorageClass(GetDecodedDLLStorageClass(Record[10]));
22002179 else
2201 UpgradeDLLImportExportLinkage(NewGV, RawLinkage);
2180 UpgradeDLLImportExportLinkage(NewGV, Record[3]);
22022181
22032182 ValueList.push_back(NewGV);
22042183
22062185 if (unsigned InitID = Record[2])
22072186 GlobalInits.push_back(std::make_pair(NewGV, InitID-1));
22082187
2209 if (Record.size() > 11) {
2188 if (Record.size() > 11)
22102189 if (unsigned ComdatID = Record[11]) {
22112190 assert(ComdatID <= ComdatList.size());
22122191 NewGV->setComdat(ComdatList[ComdatID - 1]);
22132192 }
2214 } else if (hasImplicitComdat(RawLinkage)) {
2215 NewGV->setComdat(reinterpret_cast(1));
2216 }
22172193 break;
22182194 }
22192195 // FUNCTION: [type, callingconv, isproto, linkage, paramattr,
22372213
22382214 Func->setCallingConv(static_cast(Record[1]));
22392215 bool isProto = Record[2];
2240 uint64_t RawLinkage = Record[3];
2241 Func->setLinkage(getDecodedLinkage(RawLinkage));
2216 Func->setLinkage(getDecodedLinkage(Record[3]));
22422217 Func->setAttributes(getAttributes(Record[4]));
22432218
22442219 Func->setAlignment((1 << Record[5]) >> 1);
22662241 if (Record.size() > 11)
22672242 Func->setDLLStorageClass(GetDecodedDLLStorageClass(Record[11]));
22682243 else
2269 UpgradeDLLImportExportLinkage(Func, RawLinkage);
2270
2271 if (Record.size() > 12) {
2244 UpgradeDLLImportExportLinkage(Func, Record[3]);
2245
2246 if (Record.size() > 12)
22722247 if (unsigned ComdatID = Record[12]) {
22732248 assert(ComdatID <= ComdatList.size());
22742249 Func->setComdat(ComdatList[ComdatID - 1]);
22752250 }
2276 } else if (hasImplicitComdat(RawLinkage)) {
2277 Func->setComdat(reinterpret_cast(1));
2278 }
22792251
22802252 if (Record.size() > 13 && Record[13] != 0)
22812253 FunctionPrefixes.push_back(std::make_pair(Func, Record[13]-1));
479479 case GlobalValue::ExternalLinkage:
480480 return 0;
481481 case GlobalValue::WeakAnyLinkage:
482 return 16;
482 return 1;
483483 case GlobalValue::AppendingLinkage:
484484 return 2;
485485 case GlobalValue::InternalLinkage:
486486 return 3;
487487 case GlobalValue::LinkOnceAnyLinkage:
488 return 18;
488 return 4;
489489 case GlobalValue::ExternalWeakLinkage:
490490 return 7;
491491 case GlobalValue::CommonLinkage:
493493 case GlobalValue::PrivateLinkage:
494494 return 9;
495495 case GlobalValue::WeakODRLinkage:
496 return 17;
496 return 10;
497497 case GlobalValue::LinkOnceODRLinkage:
498 return 19;
498 return 11;
499499 case GlobalValue::AvailableExternallyLinkage:
500500 return 12;
501501 }
628628 Log2_32_Ceil(MaxGlobalType+1)));
629629 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Constant.
630630 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Initializer.
631 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5)); // Linkage.
631 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // Linkage.
632632 if (MaxAlignment == 0) // Alignment.
633633 Abbv->Add(BitCodeAbbrevOp(0));
634634 else {
256256
257257 // If this global is linkonce/weak and the target handles this by emitting it
258258 // into a 'uniqued' section name, create and return the section now.
259 if ((EmitUniquedSection && !Kind.isCommon()) || GV->hasComdat()) {
259 if ((GV->isWeakForLinker() || EmitUniquedSection || GV->hasComdat()) &&
260 !Kind.isCommon()) {
260261 StringRef Prefix = getSectionPrefixForGlobal(Kind);
261262
262263 SmallString<128> Name(Prefix);
264265
265266 StringRef Group = "";
266267 unsigned Flags = getELFSectionFlags(Kind);
267 if (const Comdat *C = getELFComdat(GV)) {
268 if (GV->isWeakForLinker() || GV->hasComdat()) {
269 if (const Comdat *C = getELFComdat(GV))
270 Group = C->getName();
271 else
272 Group = Name.substr(Prefix.size());
268273 Flags |= ELF::SHF_GROUP;
269 Group = C->getName();
270274 }
271275
272276 return getContext().getELFSection(Name.str(),
796800 unsigned Characteristics = getCOFFSectionFlags(Kind);
797801 StringRef Name = GV->getSection();
798802 StringRef COMDATSymName = "";
799 if (GV->hasComdat()) {
803 if ((GV->isWeakForLinker() || GV->hasComdat()) && !Kind.isCommon()) {
800804 Selection = getSelectionForCOFF(GV);
801805 const GlobalValue *ComdatGV;
802806 if (Selection == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
843847 else
844848 EmitUniquedSection = TM.getDataSections();
845849
846 if ((EmitUniquedSection && !Kind.isCommon()) || GV->hasComdat()) {
850 // If this global is linkonce/weak and the target handles this by emitting it
851 // into a 'uniqued' section name, create and return the section now.
852 // Section names depend on the name of the symbol which is not feasible if the
853 // symbol has private linkage.
854 if ((GV->isWeakForLinker() || EmitUniquedSection || GV->hasComdat()) &&
855 !Kind.isCommon()) {
847856 const char *Name = getCOFFSectionNameForUniqueGlobal(Kind);
848857 unsigned Characteristics = getCOFFSectionFlags(Kind);
849858
104104 OutContext));
105105 if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
106106 GV->hasCommonLinkage()) {
107 // TODO Use COMDAT groups for LinkOnceLinkage
107108 OutStreamer.EmitSymbolAttribute(SymGlob, MCSA_Weak);
108109 }
109110 }
138139 emitArrayBound(GVSym, GV);
139140 OutStreamer.EmitSymbolAttribute(GVSym, MCSA_Global);
140141
142 // TODO Use COMDAT groups for LinkOnceLinkage
141143 if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
142144 GV->hasCommonLinkage())
143145 OutStreamer.EmitSymbolAttribute(GVSym, MCSA_Weak);
3232 ; CHECK: @available_externally.var = available_externally constant i32 0{{$}}
3333
3434 @linkonce.var = linkonce constant i32 0
35 ; CHECK: @linkonce.var = linkonce constant i32 0, comdat{{$}}
35 ; CHECK: @linkonce.var = linkonce constant i32 0{{$}}
3636
3737 @weak.var = weak constant i32 0
38 ; CHECK: @weak.var = weak constant i32 0, comdat{{$}}
38 ; CHECK: @weak.var = weak constant i32 0{{$}}
3939
4040 @linkonce_odr.var = linkonce_odr constant i32 0
41 ; CHECK: @linkonce_odr.var = linkonce_odr constant i32 0, comdat{{$}}
41 ; CHECK: @linkonce_odr.var = linkonce_odr constant i32 0{{$}}
4242
4343 @linkonce_odr_auto_hide.var = linkonce_odr_auto_hide constant i32 0
4444 ; CHECK: @linkonce_odr_auto_hide.var = constant i32 0{{$}}
8989 }
9090
9191 define linkonce void @linkonce()
92 ; CHECK: define linkonce void @linkonce() comdat {
92 ; CHECK: define linkonce void @linkonce() {
9393 {
9494 ret void
9595 }
9696
9797 define weak void @weak()
98 ; CHECK: define weak void @weak() comdat {
98 ; CHECK: define weak void @weak() {
9999 {
100100 ret void
101101 }
102102
103103 define linkonce_odr void @linkonce_odr()
104 ; CHECK: define linkonce_odr void @linkonce_odr() comdat {
104 ; CHECK: define linkonce_odr void @linkonce_odr() {
105105 {
106106 ret void
107107 }
0 ; RUN: llc < %s -mtriple=arm-linux-gnueabi | FileCheck %s -check-prefix=ARMGNUEABI
1
2 ; Checking that a comdat group gets generated correctly for a static member
3 ; of instantiated C++ templates.
4 ; see http://sourcery.mentor.com/public/cxx-abi/abi.html#vague-itemplate
5 ; section 5.2.6 Instantiated templates
6 ; "Any static member data object is emitted in a COMDAT identified by its mangled
7 ; name, in any object file with a reference to its name symbol."
8
9 ; Case 1: variable is not explicitly initialized, and ends up in a .bss section
10 ; ARMGNUEABI: .section .bss._ZN1CIiE1iE,"aGw",%nobits,_ZN1CIiE1iE,comdat
11 @_ZN1CIiE1iE = weak_odr global i32 0, align 4
12
13 ; Case 2: variable is explicitly initialized, and ends up in a .data section
14 ; ARMGNUEABI: .section .data._ZN1CIiE1jE,"aGw",%progbits,_ZN1CIiE1jE,comdat
15 @_ZN1CIiE1jE = weak_odr global i32 12, align 4
1515 ret void
1616 }
1717
18 ; CHECK: .text
18 ; CHECK: .section .text.test3,"axG",%progbits,test3,comdat
1919 ; CHECK: .weak test3
2020 ; CHECK: .type test3,%function
2121 define linkonce_odr void @test3() {
1616 ret void
1717 }
1818
19 ; CHECK: .section .text,"xr",discard,lnk1
1920 ; CHECK: .globl lnk1
2021 define linkonce_odr dllexport void @lnk1() {
2122 ret void
2223 }
2324
25 ; CHECK: .section .text,"xr",discard,lnk2
2426 ; CHECK: .globl lnk2
2527 define linkonce_odr dllexport void @lnk2() alwaysinline {
2628 ret void
2729 }
2830
31 ; CHECK: .section .text,"xr",discard,weak1
2932 ; CHECK: .globl weak1
3033 define weak_odr dllexport void @weak1() {
3134 ret void
4346 ; CHECK: .comm Var3
4447 @Var3 = common dllexport global i32 0, align 4
4548
49 ; CHECK: .section .data,"wd",discard,WeakVar1
4650 ; CHECK: .globl WeakVar1
4751 @WeakVar1 = weak_odr dllexport global i32 1, align 4
4852
53 ; CHECK: .section .rdata,"rd",discard,WeakVar2
4954 ; CHECK: .globl WeakVar2
5055 @WeakVar2 = weak_odr dllexport unnamed_addr constant i32 1
5156
3535 ret void
3636 }
3737
38 ; CHECK: .section .text,"xr",discard,_lnk1
3839 ; CHECK: .globl _lnk1
3940 define linkonce_odr dllexport void @lnk1() {
4041 ret void
4142 }
4243
44 ; CHECK: .section .text,"xr",discard,_lnk2
4345 ; CHECK: .globl _lnk2
4446 define linkonce_odr dllexport void @lnk2() alwaysinline {
4547 ret void
4648 }
4749
50 ; CHECK: .section .text,"xr",discard,_weak1
4851 ; CHECK: .globl _weak1
4952 define weak_odr dllexport void @weak1() {
5053 ret void
6265 ; CHECK: .comm _Var3
6366 @Var3 = common dllexport global i32 0, align 4
6467
68 ; CHECK: .section .data,"wd",discard,_WeakVar1
6569 ; CHECK: .globl _WeakVar1
6670 @WeakVar1 = weak_odr dllexport global i32 1, align 4
6771
72 ; CHECK: .section .rdata,"rd",discard,_WeakVar2
6873 ; CHECK: .globl _WeakVar2
6974 @WeakVar2 = weak_odr dllexport unnamed_addr constant i32 1
7075
8484 @"foo bar" = linkonce global i32 42
8585
8686 ; LINUX: .type "foo bar",@object
87 ; LINUX: .section ".data.foo bar","aGw",@progbits,"foo bar",comdat
8788 ; LINUX: .weak "foo bar"
8889 ; LINUX: "foo bar":
8990
9697 @G6 = weak_odr unnamed_addr constant [1 x i8] c"\01"
9798
9899 ; LINUX: .type G6,@object
100 ; LINUX: .section .rodata.G6,"aG",@progbits,G6,comdat
99101 ; LINUX: .weak G6
100102 ; LINUX: G6:
101103 ; LINUX: .byte 1
0 ; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu | FileCheck %s -check-prefix=X86LINUX
1
2 ; Checking that a comdat group gets generated correctly for a static member
3 ; of instantiated C++ templates.
4 ; see http://sourcery.mentor.com/public/cxx-abi/abi.html#vague-itemplate
5 ; section 5.2.6 Instantiated templates
6 ; "Any static member data object is emitted in a COMDAT identified by its mangled
7 ; name, in any object file with a reference to its name symbol."
8
9 ; Case 1: variable is not explicitly initialized, and ends up in a .bss section
10 ; X86LINUX: .section .bss._ZN1CIiE1iE,"aGw",@nobits,_ZN1CIiE1iE,comdat
11 @_ZN1CIiE1iE = weak_odr global i32 0, align 4
12
13 ; Case 2: variable is explicitly initialized, and ends up in a .data section
14 ; X86LINUX: .section .data._ZN1CIiE1jE,"aGw",@progbits,_ZN1CIiE1jE,comdat
15 @_ZN1CIiE1jE = weak_odr global i32 12, align 4
99
1010 declare void @_Z3bari(i32)
1111
12 ; CHECK-LINUX: _Z3fooILi1EEvi:
12 ; CHECK-LINUX: .text._Z3fooILi1EEvi,"axG",@progbits,_Z3fooILi1EEvi,comdat
1313 define linkonce void @_Z3fooILi1EEvi(i32 %Y) nounwind {
1414 entry:
1515 ; CHECK: L0$pb
44 @"\01?thingy@@3Ufoo@@B" = global %struct.foo zeroinitializer, align 4
55 ; CHECK: .bss
66
7 $thingy_linkonce = comdat any
8 @thingy_linkonce = linkonce_odr global %struct.foo zeroinitializer, comdat, align 4
7 @thingy_linkonce = linkonce_odr global %struct.foo zeroinitializer, align 4
98 ; CHECK: .section .bss,"wb",discard,_thingy_linkonce
77 ; CHECK: .section .CRT$XLB,"rd"
88
99 @weak_array = weak_odr unnamed_addr constant [1 x i8*] [i8* bitcast (void ()* @f to i8*)]
10 ; CHECK: .section .rdata,"rd"
10 ; CHECK: .section .rdata,"rd",discard,weak_array
0 ; Test that weak functions and globals are placed into selectany COMDAT
1 ; sections with the mangled name as suffix. Ensure that the weak linkage
2 ; type is not ignored by the backend if the section was specialized.
3 ;
4 ; RUN: llc -mtriple=i686-pc-win32 %s -o - | FileCheck %s --check-prefix=X86
5 ; RUN: llc -mtriple=i686-pc-mingw32 %s -o - | FileCheck %s --check-prefix=X86
6 ; RUN: llc -mtriple=x86_64-pc-win32 %s -o - | FileCheck %s --check-prefix=X64
7 ; RUN: llc -mtriple=x86_64-pc-mingw32 %s -o - | FileCheck %s --check-prefix=X64
8
9 ; Mangled function
10 ; X86: .section .text,"xr",discard,__Z3foo
11 ; X86: .globl __Z3foo
12 ;
13 ; X64: .section .text,"xr",discard,_Z3foo
14 ; X64: .globl _Z3foo
15 define weak void @_Z3foo() {
16 ret void
17 }
18
19 ; Unmangled function
20 ; X86: .section .sect,"xr",discard,_f
21 ; X86: .globl _f
22 ;
23 ; X64: .section .sect,"xr",discard,f
24 ; X64: .globl f
25 define weak void @f() section ".sect" {
26 ret void
27 }
28
29 ; Weak global
30 ; X86: .section .data,"rd",discard,_a
31 ; X86: .globl _a
32 ; X86: .zero 12
33 ;
34 ; X64: .section .data,"rd",discard,a
35 ; X64: .globl a
36 ; X64: .zero 12
37 @a = weak unnamed_addr constant { i32, i32, i32 } { i32 0, i32 0, i32 0}, section ".data"
38
39 ; X86: .section .tls$,"wd",discard,_b
40 ; X86: .globl _b
41 ; X86: .long 0
42 ;
43 ; X64: .section .tls$,"wd",discard,b
44 ; X64: .globl b
45 ; X64: .long 0
46
47 @b = weak_odr thread_local global i32 0, align 4