2019-08-30 20:26:05 +02:00
|
|
|
//===- llvm-ifs.cpp -------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===-----------------------------------------------------------------------===/
|
|
|
|
|
2021-04-02 05:55:11 +02:00
|
|
|
#include "ErrorCollector.h"
|
2019-08-30 20:26:05 +02:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
|
|
|
#include "llvm/ADT/Triple.h"
|
2021-01-09 01:26:32 +01:00
|
|
|
#include "llvm/InterfaceStub/ELFObjHandler.h"
|
2021-04-08 00:50:12 +02:00
|
|
|
#include "llvm/InterfaceStub/IFSHandler.h"
|
|
|
|
#include "llvm/InterfaceStub/IFSStub.h"
|
2019-08-30 20:26:05 +02:00
|
|
|
#include "llvm/ObjectYAML/yaml2obj.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/Errc.h"
|
|
|
|
#include "llvm/Support/Error.h"
|
|
|
|
#include "llvm/Support/FileOutputBuffer.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
#include "llvm/Support/VersionTuple.h"
|
|
|
|
#include "llvm/Support/WithColor.h"
|
|
|
|
#include "llvm/Support/YAMLTraits.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2021-04-05 18:59:50 +02:00
|
|
|
#include "llvm/TextAPI/InterfaceFile.h"
|
|
|
|
#include "llvm/TextAPI/TextAPIReader.h"
|
|
|
|
#include "llvm/TextAPI/TextAPIWriter.h"
|
2019-08-30 20:26:05 +02:00
|
|
|
#include <set>
|
|
|
|
#include <string>
|
2020-03-28 09:08:27 +01:00
|
|
|
#include <vector>
|
2019-08-30 20:26:05 +02:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::yaml;
|
|
|
|
using namespace llvm::MachO;
|
2021-04-02 05:55:11 +02:00
|
|
|
using namespace llvm::ifs;
|
2019-08-30 20:26:05 +02:00
|
|
|
|
|
|
|
#define DEBUG_TYPE "llvm-ifs"
|
|
|
|
|
|
|
|
namespace {
|
2021-04-02 05:55:11 +02:00
|
|
|
const VersionTuple IfsVersionCurrent(3, 0);
|
2019-08-30 20:26:05 +02:00
|
|
|
|
2021-04-02 05:55:11 +02:00
|
|
|
enum class FileFormat { IFS, ELF, TBD };
|
|
|
|
} // end anonymous namespace
|
2021-01-09 01:26:32 +01:00
|
|
|
|
2021-04-02 05:55:11 +02:00
|
|
|
// TODO: Use OptTable for option parsing in the future.
|
|
|
|
// Command line flags:
|
|
|
|
cl::list<std::string> InputFilePaths(cl::Positional, cl::desc("input"),
|
|
|
|
cl::ZeroOrMore);
|
|
|
|
cl::opt<FileFormat> InputFormat(
|
|
|
|
"input-format", cl::desc("Specify the input file format"),
|
|
|
|
cl::values(clEnumValN(FileFormat::IFS, "IFS", "Text based ELF stub file"),
|
|
|
|
clEnumValN(FileFormat::ELF, "ELF", "ELF object file")));
|
|
|
|
cl::opt<FileFormat> OutputFormat(
|
|
|
|
"output-format", cl::desc("Specify the output file format"),
|
|
|
|
cl::values(clEnumValN(FileFormat::IFS, "IFS", "Text based ELF stub file"),
|
|
|
|
clEnumValN(FileFormat::ELF, "ELF", "ELF stub file"),
|
|
|
|
clEnumValN(FileFormat::TBD, "TBD", "Apple TBD text stub file")),
|
|
|
|
cl::Required);
|
|
|
|
cl::opt<std::string> OptArch("arch",
|
|
|
|
cl::desc("Specify the architecture, e.g. x86_64"));
|
|
|
|
cl::opt<IFSBitWidthType> OptBitWidth(
|
|
|
|
"bitwidth", cl::desc("Specify the bit width"),
|
|
|
|
cl::values(clEnumValN(IFSBitWidthType::IFS32, "32", "32 bits"),
|
|
|
|
clEnumValN(IFSBitWidthType::IFS64, "64", "64 bits")));
|
|
|
|
cl::opt<IFSEndiannessType> OptEndianness(
|
|
|
|
"endianness", cl::desc("Specify the endianness"),
|
|
|
|
cl::values(clEnumValN(IFSEndiannessType::Little, "little", "Little Endian"),
|
|
|
|
clEnumValN(IFSEndiannessType::Big, "big", "Big Endian")));
|
|
|
|
cl::opt<std::string> OptTargetTriple(
|
|
|
|
"target", cl::desc("Specify the target triple, e.g. x86_64-linux-gnu"));
|
|
|
|
cl::opt<std::string> OptTargetTripleHint(
|
|
|
|
"hint-ifs-target",
|
|
|
|
cl::desc("When --output-format is 'IFS', this flag will hint the expected "
|
|
|
|
"target triple for IFS output"));
|
|
|
|
cl::opt<bool> StripIFSArch(
|
|
|
|
"strip-ifs-arch",
|
|
|
|
cl::desc("Strip target architecture information away from IFS output"));
|
|
|
|
cl::opt<bool> StripIFSBitWidth(
|
|
|
|
"strip-ifs-bitwidth",
|
|
|
|
cl::desc("Strip target bit width information away from IFS output"));
|
|
|
|
cl::opt<bool> StripIFSEndiannessWidth(
|
|
|
|
"strip-ifs-endianness",
|
|
|
|
cl::desc("Strip target endianness information away from IFS output"));
|
|
|
|
cl::opt<bool> StripIFSTarget(
|
|
|
|
"strip-ifs-target",
|
|
|
|
cl::desc("Strip all target information away from IFS output"));
|
|
|
|
cl::opt<std::string>
|
|
|
|
SoName("soname",
|
|
|
|
cl::desc("Manually set the DT_SONAME entry of any emitted files"),
|
|
|
|
cl::value_desc("name"));
|
|
|
|
cl::opt<std::string> OutputFilePath("output", cl::desc("Output file"));
|
|
|
|
cl::alias OutputFilePathA("o", cl::desc("Alias for --output"),
|
|
|
|
cl::aliasopt(OutputFilePath));
|
|
|
|
cl::opt<bool> WriteIfChanged(
|
|
|
|
"write-if-changed",
|
|
|
|
cl::desc("Write the output file only if it is new or has changed."));
|
2019-08-30 20:26:05 +02:00
|
|
|
|
2020-09-27 00:57:09 +02:00
|
|
|
static std::string getTypeName(IFSSymbolType Type) {
|
2019-08-30 20:26:05 +02:00
|
|
|
switch (Type) {
|
|
|
|
case IFSSymbolType::NoType:
|
|
|
|
return "NoType";
|
|
|
|
case IFSSymbolType::Func:
|
|
|
|
return "Func";
|
|
|
|
case IFSSymbolType::Object:
|
|
|
|
return "Object";
|
2021-04-02 05:55:11 +02:00
|
|
|
case IFSSymbolType::TLS:
|
|
|
|
return "TLS";
|
2019-08-30 20:26:05 +02:00
|
|
|
case IFSSymbolType::Unknown:
|
|
|
|
return "Unknown";
|
|
|
|
}
|
2019-08-30 21:23:28 +02:00
|
|
|
llvm_unreachable("Unexpected ifs symbol type.");
|
2019-08-30 20:26:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static Expected<std::unique_ptr<IFSStub>> readInputFile(StringRef FilePath) {
|
|
|
|
// Read in file.
|
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrError =
|
2021-04-16 16:02:48 +02:00
|
|
|
MemoryBuffer::getFileOrSTDIN(FilePath, /*IsText=*/true);
|
2019-08-30 20:26:05 +02:00
|
|
|
if (!BufOrError)
|
|
|
|
return createStringError(BufOrError.getError(), "Could not open `%s`",
|
|
|
|
FilePath.data());
|
|
|
|
|
|
|
|
std::unique_ptr<MemoryBuffer> FileReadBuffer = std::move(*BufOrError);
|
2021-04-02 05:55:11 +02:00
|
|
|
ErrorCollector EC(/*UseFatalErrors=*/false);
|
|
|
|
|
|
|
|
// First try to read as a binary (fails fast if not binary).
|
|
|
|
if (InputFormat.getNumOccurrences() == 0 || InputFormat == FileFormat::ELF) {
|
|
|
|
Expected<std::unique_ptr<IFSStub>> StubFromELF =
|
|
|
|
readELFFile(FileReadBuffer->getMemBufferRef());
|
|
|
|
if (StubFromELF) {
|
|
|
|
(*StubFromELF)->IfsVersion = IfsVersionCurrent;
|
|
|
|
return std::move(*StubFromELF);
|
|
|
|
}
|
|
|
|
EC.addError(StubFromELF.takeError(), "BinaryRead");
|
|
|
|
}
|
2019-08-30 20:26:05 +02:00
|
|
|
|
2021-04-02 05:55:11 +02:00
|
|
|
// Fall back to reading as a ifs.
|
|
|
|
if (InputFormat.getNumOccurrences() == 0 || InputFormat == FileFormat::IFS) {
|
|
|
|
Expected<std::unique_ptr<IFSStub>> StubFromIFS =
|
|
|
|
readIFSFromBuffer(FileReadBuffer->getBuffer());
|
|
|
|
if (StubFromIFS) {
|
|
|
|
if ((*StubFromIFS)->IfsVersion > IfsVersionCurrent)
|
|
|
|
EC.addError(
|
|
|
|
createStringError(errc::not_supported,
|
|
|
|
"IFS version " +
|
|
|
|
(*StubFromIFS)->IfsVersion.getAsString() +
|
|
|
|
" is unsupported."),
|
|
|
|
"ReadInputFile");
|
|
|
|
else
|
|
|
|
return std::move(*StubFromIFS);
|
|
|
|
} else {
|
|
|
|
EC.addError(StubFromIFS.takeError(), "YamlParse");
|
|
|
|
}
|
|
|
|
}
|
2020-10-21 02:50:29 +02:00
|
|
|
|
2021-04-02 05:55:11 +02:00
|
|
|
// If both readers fail, build a new error that includes all information.
|
|
|
|
EC.addError(createStringError(errc::not_supported,
|
|
|
|
"No file readers succeeded reading `%s` "
|
|
|
|
"(unsupported/malformed file?)",
|
|
|
|
FilePath.data()),
|
|
|
|
"ReadInputFile");
|
|
|
|
EC.escalateToFatal();
|
|
|
|
return EC.makeError();
|
2019-08-30 20:26:05 +02:00
|
|
|
}
|
|
|
|
|
2020-09-27 00:57:09 +02:00
|
|
|
static int writeTbdStub(const Triple &T, const std::vector<IFSSymbol> &Symbols,
|
|
|
|
const StringRef Format, raw_ostream &Out) {
|
2019-08-30 20:26:05 +02:00
|
|
|
|
2019-09-06 21:59:59 +02:00
|
|
|
auto PlatformKindOrError =
|
|
|
|
[](const llvm::Triple &T) -> llvm::Expected<llvm::MachO::PlatformKind> {
|
|
|
|
if (T.isMacOSX())
|
|
|
|
return llvm::MachO::PlatformKind::macOS;
|
|
|
|
if (T.isTvOS())
|
|
|
|
return llvm::MachO::PlatformKind::tvOS;
|
|
|
|
if (T.isWatchOS())
|
|
|
|
return llvm::MachO::PlatformKind::watchOS;
|
|
|
|
// Note: put isiOS last because tvOS and watchOS are also iOS according
|
|
|
|
// to the Triple.
|
|
|
|
if (T.isiOS())
|
|
|
|
return llvm::MachO::PlatformKind::iOS;
|
|
|
|
|
|
|
|
return createStringError(errc::not_supported, "Invalid Platform.\n");
|
|
|
|
}(T);
|
|
|
|
|
|
|
|
if (!PlatformKindOrError)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
PlatformKind Plat = PlatformKindOrError.get();
|
2019-09-20 16:32:34 +02:00
|
|
|
TargetList Targets({Target(llvm::MachO::mapToArchitecture(T), Plat)});
|
2019-08-30 20:26:05 +02:00
|
|
|
|
|
|
|
InterfaceFile File;
|
2019-09-06 21:59:59 +02:00
|
|
|
File.setFileType(FileType::TBD_V3); // Only supporting v3 for now.
|
2019-09-20 16:32:34 +02:00
|
|
|
File.addTargets(Targets);
|
2019-08-30 20:26:05 +02:00
|
|
|
|
|
|
|
for (const auto &Symbol : Symbols) {
|
|
|
|
auto Name = Symbol.Name;
|
|
|
|
auto Kind = SymbolKind::GlobalSymbol;
|
|
|
|
switch (Symbol.Type) {
|
|
|
|
default:
|
|
|
|
case IFSSymbolType::NoType:
|
|
|
|
Kind = SymbolKind::GlobalSymbol;
|
|
|
|
break;
|
|
|
|
case IFSSymbolType::Object:
|
|
|
|
Kind = SymbolKind::GlobalSymbol;
|
|
|
|
break;
|
|
|
|
case IFSSymbolType::Func:
|
|
|
|
Kind = SymbolKind::GlobalSymbol;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (Symbol.Weak)
|
2019-09-20 16:32:34 +02:00
|
|
|
File.addSymbol(Kind, Name, Targets, SymbolFlags::WeakDefined);
|
2019-08-30 20:26:05 +02:00
|
|
|
else
|
2019-09-20 16:32:34 +02:00
|
|
|
File.addSymbol(Kind, Name, Targets);
|
2019-08-30 20:26:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SmallString<4096> Buffer;
|
|
|
|
raw_svector_ostream OS(Buffer);
|
|
|
|
if (Error Result = TextAPIWriter::writeToStream(OS, File))
|
|
|
|
return -1;
|
|
|
|
Out << OS.str();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-04-02 05:55:11 +02:00
|
|
|
static void fatalError(Error Err) {
|
|
|
|
WithColor::defaultErrorHandler(std::move(Err));
|
|
|
|
exit(1);
|
2019-08-30 20:26:05 +02:00
|
|
|
}
|
|
|
|
|
2021-04-02 05:55:11 +02:00
|
|
|
/// writeIFS() writes a Text-Based ELF stub to a file using the latest version
|
|
|
|
/// of the YAML parser.
|
|
|
|
static Error writeIFS(StringRef FilePath, IFSStub &Stub) {
|
|
|
|
// Write IFS to memory first.
|
|
|
|
std::string IFSStr;
|
|
|
|
raw_string_ostream OutStr(IFSStr);
|
|
|
|
Error YAMLErr = writeIFSToOutputStream(OutStr, Stub);
|
|
|
|
if (YAMLErr)
|
|
|
|
return YAMLErr;
|
|
|
|
OutStr.flush();
|
|
|
|
|
|
|
|
if (WriteIfChanged) {
|
|
|
|
if (ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrError =
|
|
|
|
MemoryBuffer::getFile(FilePath)) {
|
|
|
|
// Compare IFS output with the existing IFS file. If unchanged, avoid changing the file.
|
|
|
|
if ((*BufOrError)->getBuffer() == IFSStr)
|
|
|
|
return Error::success();
|
2021-01-09 01:26:32 +01:00
|
|
|
}
|
|
|
|
}
|
2021-04-02 05:55:11 +02:00
|
|
|
// Open IFS file for writing.
|
2021-01-09 01:26:32 +01:00
|
|
|
std::error_code SysErr;
|
2021-04-02 05:55:11 +02:00
|
|
|
raw_fd_ostream Out(FilePath, SysErr);
|
|
|
|
if (SysErr)
|
|
|
|
return createStringError(SysErr, "Couldn't open `%s` for writing",
|
|
|
|
FilePath.data());
|
|
|
|
Out << IFSStr;
|
|
|
|
return Error::success();
|
2019-08-30 20:26:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
// Parse arguments.
|
|
|
|
cl::ParseCommandLineOptions(argc, argv);
|
|
|
|
|
2021-04-02 05:55:11 +02:00
|
|
|
if (InputFilePaths.empty())
|
|
|
|
InputFilePaths.push_back("-");
|
2019-08-30 20:26:05 +02:00
|
|
|
|
2021-04-02 05:55:11 +02:00
|
|
|
// If input files are more than one, they can only be IFS files.
|
|
|
|
if (InputFilePaths.size() > 1)
|
|
|
|
InputFormat.setValue(FileFormat::IFS);
|
|
|
|
|
|
|
|
// Attempt to merge input.
|
2019-08-30 20:26:05 +02:00
|
|
|
IFSStub Stub;
|
|
|
|
std::map<std::string, IFSSymbol> SymbolMap;
|
2021-01-13 06:43:46 +01:00
|
|
|
std::string PreviousInputFilePath;
|
2021-04-02 05:55:11 +02:00
|
|
|
for (const std::string &InputFilePath : InputFilePaths) {
|
2019-08-30 20:26:05 +02:00
|
|
|
Expected<std::unique_ptr<IFSStub>> StubOrErr = readInputFile(InputFilePath);
|
2021-04-02 05:55:11 +02:00
|
|
|
if (!StubOrErr)
|
|
|
|
fatalError(StubOrErr.takeError());
|
2019-08-30 20:26:05 +02:00
|
|
|
|
2021-04-02 05:55:11 +02:00
|
|
|
std::unique_ptr<IFSStub> TargetStub = std::move(StubOrErr.get());
|
|
|
|
if (PreviousInputFilePath.empty()) {
|
2019-08-30 20:26:05 +02:00
|
|
|
Stub.IfsVersion = TargetStub->IfsVersion;
|
2021-04-02 05:55:11 +02:00
|
|
|
Stub.Target = TargetStub->Target;
|
|
|
|
Stub.SoName = TargetStub->SoName;
|
2019-08-30 20:26:05 +02:00
|
|
|
Stub.NeededLibs = TargetStub->NeededLibs;
|
|
|
|
} else {
|
|
|
|
if (Stub.IfsVersion != TargetStub->IfsVersion) {
|
2021-04-02 05:55:11 +02:00
|
|
|
if (Stub.IfsVersion.getMajor() != IfsVersionCurrent.getMajor()) {
|
2019-08-30 20:26:05 +02:00
|
|
|
WithColor::error()
|
|
|
|
<< "Interface Stub: IfsVersion Mismatch."
|
|
|
|
<< "\nFilenames: " << PreviousInputFilePath << " "
|
|
|
|
<< InputFilePath << "\nIfsVersion Values: " << Stub.IfsVersion
|
|
|
|
<< " " << TargetStub->IfsVersion << "\n";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (TargetStub->IfsVersion > Stub.IfsVersion)
|
|
|
|
Stub.IfsVersion = TargetStub->IfsVersion;
|
|
|
|
}
|
2021-04-02 05:55:11 +02:00
|
|
|
if (Stub.Target != TargetStub->Target && !TargetStub->Target.empty()) {
|
|
|
|
WithColor::error() << "Interface Stub: Target Mismatch."
|
2019-08-30 20:26:05 +02:00
|
|
|
<< "\nFilenames: " << PreviousInputFilePath << " "
|
2021-04-02 05:55:11 +02:00
|
|
|
<< InputFilePath;
|
|
|
|
// << "\nTriple Values: " << Stub.Triple << " "
|
|
|
|
// << TargetStub->Triple << "\n";
|
2019-08-30 20:26:05 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2021-04-02 05:55:11 +02:00
|
|
|
if (Stub.SoName != TargetStub->SoName) {
|
|
|
|
WithColor::error() << "Interface Stub: SoName Mismatch."
|
2019-08-30 20:26:05 +02:00
|
|
|
<< "\nFilenames: " << PreviousInputFilePath << " "
|
|
|
|
<< InputFilePath
|
2021-04-02 05:55:11 +02:00
|
|
|
<< "\nSoName Values: " << Stub.SoName << " "
|
|
|
|
<< TargetStub->SoName << "\n";
|
2019-08-30 20:26:05 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (Stub.NeededLibs != TargetStub->NeededLibs) {
|
|
|
|
WithColor::error() << "Interface Stub: NeededLibs Mismatch."
|
|
|
|
<< "\nFilenames: " << PreviousInputFilePath << " "
|
|
|
|
<< InputFilePath << "\n";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto Symbol : TargetStub->Symbols) {
|
|
|
|
auto SI = SymbolMap.find(Symbol.Name);
|
|
|
|
if (SI == SymbolMap.end()) {
|
|
|
|
SymbolMap.insert(
|
|
|
|
std::pair<std::string, IFSSymbol>(Symbol.Name, Symbol));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(Symbol.Name == SI->second.Name && "Symbol Names Must Match.");
|
|
|
|
|
|
|
|
// Check conflicts:
|
|
|
|
if (Symbol.Type != SI->second.Type) {
|
|
|
|
WithColor::error() << "Interface Stub: Type Mismatch for "
|
|
|
|
<< Symbol.Name << ".\nFilename: " << InputFilePath
|
|
|
|
<< "\nType Values: " << getTypeName(SI->second.Type)
|
|
|
|
<< " " << getTypeName(Symbol.Type) << "\n";
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (Symbol.Size != SI->second.Size) {
|
|
|
|
WithColor::error() << "Interface Stub: Size Mismatch for "
|
|
|
|
<< Symbol.Name << ".\nFilename: " << InputFilePath
|
|
|
|
<< "\nSize Values: " << SI->second.Size << " "
|
|
|
|
<< Symbol.Size << "\n";
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (Symbol.Weak != SI->second.Weak) {
|
2019-11-28 22:43:56 +01:00
|
|
|
Symbol.Weak = false;
|
|
|
|
continue;
|
2019-08-30 20:26:05 +02:00
|
|
|
}
|
|
|
|
// TODO: Not checking Warning. Will be dropped.
|
|
|
|
}
|
|
|
|
|
|
|
|
PreviousInputFilePath = InputFilePath;
|
|
|
|
}
|
|
|
|
|
2021-04-02 05:55:11 +02:00
|
|
|
if (Stub.IfsVersion != IfsVersionCurrent)
|
|
|
|
if (Stub.IfsVersion.getMajor() != IfsVersionCurrent.getMajor()) {
|
2019-08-30 20:26:05 +02:00
|
|
|
WithColor::error() << "Interface Stub: Bad IfsVersion: "
|
|
|
|
<< Stub.IfsVersion << ", llvm-ifs supported version: "
|
2021-04-02 05:55:11 +02:00
|
|
|
<< IfsVersionCurrent << ".\n";
|
2019-08-30 20:26:05 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto &Entry : SymbolMap)
|
2020-03-28 09:08:27 +01:00
|
|
|
Stub.Symbols.push_back(Entry.second);
|
2019-08-30 20:26:05 +02:00
|
|
|
|
2021-04-02 05:55:11 +02:00
|
|
|
// Change SoName before emitting stubs.
|
|
|
|
if (SoName.getNumOccurrences() == 1)
|
|
|
|
Stub.SoName = SoName;
|
|
|
|
Optional<IFSArch> OverrideArch;
|
|
|
|
Optional<IFSEndiannessType> OverrideEndianness;
|
|
|
|
Optional<IFSBitWidthType> OverrideBitWidth;
|
|
|
|
Optional<std::string> OverrideTriple;
|
|
|
|
if (OptArch.getNumOccurrences() == 1)
|
|
|
|
OverrideArch = ELF::convertArchNameToEMachine(OptArch.getValue());
|
|
|
|
if (OptEndianness.getNumOccurrences() == 1)
|
|
|
|
OverrideEndianness = OptEndianness.getValue();
|
|
|
|
if (OptBitWidth.getNumOccurrences() == 1)
|
|
|
|
OverrideBitWidth = OptBitWidth.getValue();
|
|
|
|
if (OptTargetTriple.getNumOccurrences() == 1)
|
|
|
|
OverrideTriple = OptTargetTriple.getValue();
|
|
|
|
Error OverrideError = overrideIFSTarget(
|
|
|
|
Stub, OverrideArch, OverrideEndianness, OverrideBitWidth, OverrideTriple);
|
|
|
|
if (OverrideError)
|
|
|
|
fatalError(std::move(OverrideError));
|
|
|
|
|
|
|
|
switch (OutputFormat.getValue()) {
|
|
|
|
case FileFormat::TBD: {
|
|
|
|
std::error_code SysErr;
|
|
|
|
raw_fd_ostream Out(OutputFilePath, SysErr);
|
|
|
|
if (SysErr) {
|
|
|
|
WithColor::error() << "Couldn't open " << OutputFilePath
|
|
|
|
<< " for writing.\n";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!Stub.Target.Triple) {
|
|
|
|
WithColor::error()
|
|
|
|
<< "Triple should be defined when output format is TBD";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return writeTbdStub(llvm::Triple(Stub.Target.Triple.getValue()),
|
|
|
|
Stub.Symbols, "TBD", Out);
|
|
|
|
}
|
|
|
|
case FileFormat::IFS: {
|
|
|
|
Stub.IfsVersion = IfsVersionCurrent;
|
|
|
|
if (InputFormat.getValue() == FileFormat::ELF &&
|
|
|
|
OptTargetTripleHint.getNumOccurrences() == 1) {
|
|
|
|
std::error_code HintEC(1, std::generic_category());
|
|
|
|
IFSTarget HintTarget = parseTriple(OptTargetTripleHint);
|
|
|
|
if (Stub.Target.Arch.getValue() != HintTarget.Arch.getValue())
|
|
|
|
fatalError(make_error<StringError>(
|
|
|
|
"Triple hint does not match the actual architecture", HintEC));
|
|
|
|
if (Stub.Target.Endianness.getValue() !=
|
|
|
|
HintTarget.Endianness.getValue())
|
|
|
|
fatalError(make_error<StringError>(
|
|
|
|
"Triple hint does not match the actual endianness", HintEC));
|
|
|
|
if (Stub.Target.BitWidth.getValue() != HintTarget.BitWidth.getValue())
|
|
|
|
fatalError(make_error<StringError>(
|
|
|
|
"Triple hint does not match the actual bit width", HintEC));
|
|
|
|
|
|
|
|
stripIFSTarget(Stub, true, false, false, false);
|
|
|
|
Stub.Target.Triple = OptTargetTripleHint.getValue();
|
|
|
|
} else {
|
|
|
|
stripIFSTarget(Stub, StripIFSTarget, StripIFSArch,
|
|
|
|
StripIFSEndiannessWidth, StripIFSBitWidth);
|
|
|
|
}
|
|
|
|
Error IFSWriteError = writeIFS(OutputFilePath.getValue(), Stub);
|
|
|
|
if (IFSWriteError)
|
|
|
|
fatalError(std::move(IFSWriteError));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case FileFormat::ELF: {
|
|
|
|
Error TargetError = validateIFSTarget(Stub, true);
|
|
|
|
if (TargetError)
|
|
|
|
fatalError(std::move(TargetError));
|
|
|
|
Error BinaryWriteError =
|
|
|
|
writeBinaryStub(OutputFilePath, Stub, WriteIfChanged);
|
|
|
|
if (BinaryWriteError)
|
|
|
|
fatalError(std::move(BinaryWriteError));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
2019-08-30 20:26:05 +02:00
|
|
|
}
|