diff options
author | Fangrui Song <i@maskray.me> | 2022-08-18 16:28:55 -0700 |
---|---|---|
committer | Fangrui Song <i@maskray.me> | 2022-08-18 16:28:55 -0700 |
commit | c2a38887932e3a46aa3bee35f3f5568ac68282f4 (patch) | |
tree | 903747d76dd1fb4d303c7d8891893975766c0e10 /llvm/lib/IR | |
parent | 37c47b2cacae99d65b4b82eb41655f0820100677 (diff) | |
download | llvm-c2a38887932e3a46aa3bee35f3f5568ac68282f4.zip llvm-c2a38887932e3a46aa3bee35f3f5568ac68282f4.tar.gz llvm-c2a38887932e3a46aa3bee35f3f5568ac68282f4.tar.bz2 |
[IR] Use Min behavior for module flag "PIC Level"
Using Max for both "PIC Level" and "PIE Level" is inconsistent. PIC imposes less
restriction while PIE imposes more restriction. The result generally
picks the more restrictive behavior: Min for PIC.
This choice matches `ld -r`: a non-pic object and a pic object merge into a
result which should be treated as non-pic.
To allow linking "PIC Level" using Error/Max from old bitcode files, upgrade
Error/Max to Min.
Reviewed By: tejohnson
Differential Revision: https://reviews.llvm.org/D130531
Diffstat (limited to 'llvm/lib/IR')
-rw-r--r-- | llvm/lib/IR/AutoUpgrade.cpp | 32 | ||||
-rw-r--r-- | llvm/lib/IR/Module.cpp | 4 |
2 files changed, 23 insertions, 13 deletions
diff --git a/llvm/lib/IR/AutoUpgrade.cpp b/llvm/lib/IR/AutoUpgrade.cpp index 75594f9..18ea2fd 100644 --- a/llvm/lib/IR/AutoUpgrade.cpp +++ b/llvm/lib/IR/AutoUpgrade.cpp @@ -4391,26 +4391,34 @@ bool llvm::UpgradeModuleFlags(Module &M) { MDString *ID = dyn_cast_or_null<MDString>(Op->getOperand(1)); if (!ID) continue; + auto SetBehavior = [&](Module::ModFlagBehavior B) { + Metadata *Ops[3] = {ConstantAsMetadata::get(ConstantInt::get( + Type::getInt32Ty(M.getContext()), B)), + MDString::get(M.getContext(), ID->getString()), + Op->getOperand(2)}; + ModFlags->setOperand(I, MDNode::get(M.getContext(), Ops)); + Changed = true; + }; + if (ID->getString() == "Objective-C Image Info Version") HasObjCFlag = true; if (ID->getString() == "Objective-C Class Properties") HasClassProperties = true; - // Upgrade PIC/PIE Module Flags. The module flag behavior for these two - // field was Error and now they are Max. - if (ID->getString() == "PIC Level" || ID->getString() == "PIE Level") { + // Upgrade PIC from Error/Max to Min. + if (ID->getString() == "PIC Level") { if (auto *Behavior = mdconst::dyn_extract_or_null<ConstantInt>(Op->getOperand(0))) { - if (Behavior->getLimitedValue() == Module::Error) { - Type *Int32Ty = Type::getInt32Ty(M.getContext()); - Metadata *Ops[3] = { - ConstantAsMetadata::get(ConstantInt::get(Int32Ty, Module::Max)), - MDString::get(M.getContext(), ID->getString()), - Op->getOperand(2)}; - ModFlags->setOperand(I, MDNode::get(M.getContext(), Ops)); - Changed = true; - } + uint64_t V = Behavior->getLimitedValue(); + if (V == Module::Error || V == Module::Max) + SetBehavior(Module::Min); } } + // Upgrade "PIE Level" from Error to Max. + if (ID->getString() == "PIE Level") + if (auto *Behavior = + mdconst::dyn_extract_or_null<ConstantInt>(Op->getOperand(0))) + if (Behavior->getLimitedValue() == Module::Error) + SetBehavior(Module::Max); // Upgrade branch protection and return address signing module flags. The // module flag behavior for these fields were Error and now they are Min. diff --git a/llvm/lib/IR/Module.cpp b/llvm/lib/IR/Module.cpp index b51ea45..5dd114c 100644 --- a/llvm/lib/IR/Module.cpp +++ b/llvm/lib/IR/Module.cpp @@ -596,7 +596,9 @@ PICLevel::Level Module::getPICLevel() const { } void Module::setPICLevel(PICLevel::Level PL) { - addModuleFlag(ModFlagBehavior::Max, "PIC Level", PL); + // The merge result of a non-PIC object and a PIC object can only be reliably + // used as a non-PIC object, so use the Min merge behavior. + addModuleFlag(ModFlagBehavior::Min, "PIC Level", PL); } PIELevel::Level Module::getPIELevel() const { |