[macho] save the SDK version stored in module metadata into the version min and

build version load commands in the object file

This commit introduces a new metadata node called "SDK Version". It will be set
by the frontend to mark the platform SDK (macOS/iOS/etc) version which was used
during that particular compilation.
This node is used when machine code is emitted, by either saving the SDK version
into the appropriate macho load command (version min/build version), or by
emitting the assembly for these load commands with the SDK version specified as
well.
The assembly for both load commands is extended by allowing it to contain the
sdk_version X, Y [, Z] trailing directive to represent the SDK version
respectively.

rdar://45774000

Differential Revision: https://reviews.llvm.org/D55612


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@349119 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Alex Lorenz
2018-12-14 01:14:10 +00:00
parent ee2b007283
commit 6592c09789
19 changed files with 297 additions and 53 deletions
+40
View File
@@ -46,6 +46,7 @@
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/RandomNumberGenerator.h"
#include "llvm/Support/VersionTuple.h"
#include <algorithm>
#include <cassert>
#include <cstdint>
@@ -547,6 +548,45 @@ void Module::setRtLibUseGOT() {
addModuleFlag(ModFlagBehavior::Max, "RtLibUseGOT", 1);
}
void Module::setSDKVersion(const VersionTuple &V) {
SmallVector<unsigned, 3> Entries;
Entries.push_back(V.getMajor());
if (auto Minor = V.getMinor()) {
Entries.push_back(*Minor);
if (auto Subminor = V.getSubminor())
Entries.push_back(*Subminor);
// Ignore the 'build' component as it can't be represented in the object
// file.
}
addModuleFlag(ModFlagBehavior::Warning, "SDK Version",
ConstantDataArray::get(Context, Entries));
}
VersionTuple Module::getSDKVersion() const {
auto *CM = dyn_cast_or_null<ConstantAsMetadata>(getModuleFlag("SDK Version"));
if (!CM)
return {};
auto *Arr = dyn_cast_or_null<ConstantDataArray>(CM->getValue());
if (!Arr)
return {};
auto getVersionComponent = [&](unsigned Index) -> Optional<unsigned> {
if (Index >= Arr->getNumElements())
return None;
return (unsigned)Arr->getElementAsInteger(Index);
};
auto Major = getVersionComponent(0);
if (!Major)
return {};
VersionTuple Result = VersionTuple(*Major);
if (auto Minor = getVersionComponent(1)) {
Result = VersionTuple(*Major, *Minor);
if (auto Subminor = getVersionComponent(2)) {
Result = VersionTuple(*Major, *Minor, *Subminor);
}
}
return Result;
}
GlobalVariable *llvm::collectUsedGlobalVariables(
const Module &M, SmallPtrSetImpl<GlobalValue *> &Set, bool CompilerUsed) {
const char *Name = CompilerUsed ? "llvm.compiler.used" : "llvm.used";