llvm.org GIT mirror llvm / 1fd5bb9
Support for taking the max of module flags when linking, use for PIE/PIC Summary: Add Max ModFlagBehavior, which can be used to take the max of two module flag values when merging modules. Use it for the PIE and PIC levels. This avoids an error when we try to import from a module built -fpic into a module built -fPIC, for example. For both PIE and PIC levels, this will be legal, since the code generation gets more conservative as the level is increased. Therefore we can take the max instead of somehow trying to block importing between modules compiled with different levels. Reviewers: tmsriram, pcc Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D33418 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@303590 91177308-0d34-0410-b5e6-96231b3b80d8 Teresa Johnson 2 years ago
7 changed file(s) with 43 addition(s) and 14 deletion(s). Raw diff Collapse all Expand all
138138 /// during the append operation.
139139 AppendUnique = 6,
140140
141 /// Takes the max of the two values, which are required to be integers.
142 Max = 7,
143
141144 // Markers:
142145 ModFlagBehaviorFirstVal = Error,
143 ModFlagBehaviorLastVal = AppendUnique
146 ModFlagBehaviorLastVal = Max
144147 };
145148
146149 /// Checks if Metadata represents a valid ModFlagBehavior, and stores the
480480 }
481481
482482 void Module::setPICLevel(PICLevel::Level PL) {
483 addModuleFlag(ModFlagBehavior::Error, "PIC Level", PL);
483 addModuleFlag(ModFlagBehavior::Max, "PIC Level", PL);
484484 }
485485
486486 PIELevel::Level Module::getPIELevel() const {
494494 }
495495
496496 void Module::setPIELevel(PIELevel::Level PL) {
497 addModuleFlag(ModFlagBehavior::Error, "PIE Level", PL);
497 addModuleFlag(ModFlagBehavior::Max, "PIE Level", PL);
498498 }
499499
500500 void Module::setProfileSummary(Metadata *M) {
12811281 // These behavior types accept any value.
12821282 break;
12831283
1284 case Module::Max: {
1285 Assert(mdconst::dyn_extract_or_null(Op->getOperand(2)),
1286 "invalid value for 'max' module flag (expected constant integer)",
1287 Op->getOperand(2));
1288 break;
1289 }
1290
12841291 case Module::Require: {
12851292 // The value should itself be an MDNode with two operands, a flag ID (an
12861293 // MDString), and a value.
11561156 mdconst::extract(DstOp->getOperand(0));
11571157 unsigned DstBehaviorValue = DstBehavior->getZExtValue();
11581158
1159 auto overrideDstValue = [&]() {
1160 DstModFlags->setOperand(DstIndex, SrcOp);
1161 Flags[ID].first = SrcOp;
1162 };
1163
11591164 // If either flag has override behavior, handle it first.
11601165 if (DstBehaviorValue == Module::Override) {
11611166 // Diagnose inconsistent flags which both have override behavior.
11661171 continue;
11671172 } else if (SrcBehaviorValue == Module::Override) {
11681173 // Update the destination flag to that of the source.
1169 DstModFlags->setOperand(DstIndex, SrcOp);
1170 Flags[ID].first = SrcOp;
1174 overrideDstValue();
11711175 continue;
11721176 }
11731177
12031207 }
12041208 continue;
12051209 }
1210 case Module::Max: {
1211 ConstantInt *DstValue =
1212 mdconst::extract(DstOp->getOperand(2));
1213 ConstantInt *SrcValue =
1214 mdconst::extract(SrcOp->getOperand(2));
1215 if (SrcValue->getZExtValue() > DstValue->getZExtValue())
1216 overrideDstValue();
1217 break;
1218 }
12061219 case Module::Append: {
12071220 MDNode *DstValue = cast(DstOp->getOperand(2));
12081221 MDNode *SrcValue = cast(SrcOp->getOperand(2));
None !0 = !{ i32 1, !"PIC Level", i32 2 }
0 !0 = !{ i32 7, !"PIC Level", i32 2 }
1 !1 = !{ i32 7, !"PIE Level", i32 2 }
12
2 !llvm.module.flags = !{!0}
3 !llvm.module.flags = !{!0, !1}
None ; RUN: not llvm-link %s %p/Inputs/module-flags-pic-2-b.ll -S -o - 2> %t
1 ; RUN: FileCheck --check-prefix=CHECK-ERRORS < %t %s
0 ; RUN: llvm-link %s %p/Inputs/module-flags-pic-2-b.ll -S -o - | FileCheck %s
21
3 ; test linking modules with two different PIC levels
2 ; test linking modules with two different PIC and PIE levels
43
5 !0 = !{ i32 1, !"PIC Level", i32 1 }
4 !0 = !{ i32 7, !"PIC Level", i32 1 }
5 !1 = !{ i32 7, !"PIE Level", i32 1 }
66
7 !llvm.module.flags = !{!0}
7 !llvm.module.flags = !{!0, !1}
88
9 ; CHECK-ERRORS: ERROR: linking module flags 'PIC Level': IDs have conflicting values
9 ; CHECK: !0 = !{i32 7, !"PIC Level", i32 2}
10 ; CHECK: !1 = !{i32 7, !"PIE Level", i32 2}
4040 ; CHECK-NOT: invalid value for 'append'-type module flag (expected a metadata node)
4141 !18 = !{i32 5, !"flag-4", !{i32 57}}
4242
43 ; Check that any 'max' module flags are valid.
44 ; CHECK: invalid value for 'max' module flag (expected constant integer)
45 !19 = !{i32 7, !"max", !"max"}
46
4347 ; Check that any 'require' module flags are valid.
4448 ; CHECK: invalid requirement on flag, flag is not present in module
4549 !11 = !{i32 3, !"bar", !{!"no-such-flag", i32 52}}
5357
5458 !llvm.module.flags = !{
5559 !0, !1, !2, !3, !4, !5, !6, !7, !8, !9, !10, !11, !12, !13, !14, !15,
56 !16, !17, !18 }
60 !16, !17, !18, !19 }