diff options
author | Haowei Wu <haowei@google.com> | 2021-04-07 15:50:12 -0700 |
---|---|---|
committer | Haowei Wu <haowei@google.com> | 2021-07-19 11:23:00 -0700 |
commit | 61fa9afe4c5b014181ce41a35c84fb0f3be975db (patch) | |
tree | 5212007a0ef0d231f45b8a83f89d42ac7e013c5d /llvm/tools | |
parent | 8b4acb067fd38ac33a63669ef46966dfee59257e (diff) | |
download | llvm-61fa9afe4c5b014181ce41a35c84fb0f3be975db.zip llvm-61fa9afe4c5b014181ce41a35c84fb0f3be975db.tar.gz llvm-61fa9afe4c5b014181ce41a35c84fb0f3be975db.tar.bz2 |
[ifs] Prepare llvm-ifs for elfabi/ifs merging.
This diff changes llvm-ifs to use unified IFS file format
and perform other renaming changes in preparation for the
merging between elfabi/ifs.
Differential Revision: https://reviews.llvm.org/D99810
Diffstat (limited to 'llvm/tools')
-rw-r--r-- | llvm/tools/llvm-elfabi/llvm-elfabi.cpp | 96 | ||||
-rw-r--r-- | llvm/tools/llvm-ifs/llvm-ifs.cpp | 35 |
2 files changed, 65 insertions, 66 deletions
diff --git a/llvm/tools/llvm-elfabi/llvm-elfabi.cpp b/llvm/tools/llvm-elfabi/llvm-elfabi.cpp index b94d075..4e80fca 100644 --- a/llvm/tools/llvm-elfabi/llvm-elfabi.cpp +++ b/llvm/tools/llvm-elfabi/llvm-elfabi.cpp @@ -8,7 +8,7 @@ #include "ErrorCollector.h" #include "llvm/InterfaceStub/ELFObjHandler.h" -#include "llvm/InterfaceStub/TBEHandler.h" +#include "llvm/InterfaceStub/IFSHandler.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Errc.h" #include "llvm/Support/FileOutputBuffer.h" @@ -21,9 +21,9 @@ namespace llvm { namespace elfabi { -enum class FileFormat { TBE, ELF }; +enum class FileFormat { IFS, ELF }; -} // end namespace elfabi +} // namespace elfabi } // end namespace llvm using namespace llvm; @@ -34,28 +34,28 @@ cl::opt<std::string> InputFilePath(cl::Positional, cl::desc("input"), cl::Required); cl::opt<FileFormat> InputFormat( "input-format", cl::desc("Specify the input file format"), - cl::values(clEnumValN(FileFormat::TBE, "TBE", "Text based ELF stub file"), + 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::TBE, "TBE", "Text based ELF stub file"), + cl::values(clEnumValN(FileFormat::IFS, "IFS", "Text based ELF stub file"), clEnumValN(FileFormat::ELF, "ELF", "ELF stub file")), cl::Required); cl::opt<std::string> OptArch("arch", cl::desc("Specify the architecture, e.g. x86_64")); -cl::opt<ELFBitWidthType> OptBitWidth( +cl::opt<IFSBitWidthType> OptBitWidth( "bitwidth", cl::desc("Specify the bit width"), - cl::values(clEnumValN(ELFBitWidthType::ELF32, "32", "32 bits"), - clEnumValN(ELFBitWidthType::ELF64, "64", "64 bits"))); -cl::opt<ELFEndiannessType> OptEndianness( + 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(ELFEndiannessType::Little, "little", "Little Endian"), - clEnumValN(ELFEndiannessType::Big, "big", "Big Endian"))); + 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 'TBE', this flag will hint the expected " + 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", @@ -78,13 +78,13 @@ cl::opt<bool> WriteIfChanged( "write-if-changed", cl::desc("Write the output file only if it is new or has changed.")); -/// writeTBE() writes a Text-Based ELF stub to a file using the latest version +/// writeIFS() writes a Text-Based ELF stub to a file using the latest version /// of the YAML parser. -static Error writeTBE(StringRef FilePath, ELFStub &Stub) { - // Write TBE to memory first. - std::string TBEStr; - raw_string_ostream OutStr(TBEStr); - Error YAMLErr = writeTBEToOutputStream(OutStr, Stub); +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(); @@ -92,25 +92,25 @@ static Error writeTBE(StringRef FilePath, ELFStub &Stub) { if (WriteIfChanged) { if (ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrError = MemoryBuffer::getFile(FilePath)) { - // Compare TBE output with existing TBE file. - // If TBE file unchanged, abort updating. - if ((*BufOrError)->getBuffer() == TBEStr) + // Compare IFS output with existing IFS file. + // If IFS file unchanged, abort updating. + if ((*BufOrError)->getBuffer() == IFSStr) return Error::success(); } } - // Open TBE file for writing. + // Open IFS file for writing. std::error_code SysErr; raw_fd_ostream Out(FilePath, SysErr); if (SysErr) return createStringError(SysErr, "Couldn't open `%s` for writing", FilePath.data()); - Out << TBEStr; + Out << IFSStr; return Error::success(); } -/// readInputFile populates an ELFStub by attempting to read the -/// input file using both the TBE and binary ELF parsers. -static Expected<std::unique_ptr<ELFStub>> readInputFile(StringRef FilePath) { +/// readInputFile populates an IFSStub by attempting to read the +/// input file using both the IFS and binary ELF parsers. +static Expected<std::unique_ptr<IFSStub>> readInputFile(StringRef FilePath) { // Read in file. ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrError = MemoryBuffer::getFile(FilePath); @@ -124,7 +124,7 @@ static Expected<std::unique_ptr<ELFStub>> readInputFile(StringRef FilePath) { // First try to read as a binary (fails fast if not binary). if (InputFormat.getNumOccurrences() == 0 || InputFormat == FileFormat::ELF) { - Expected<std::unique_ptr<ELFStub>> StubFromELF = + Expected<std::unique_ptr<IFSStub>> StubFromELF = readELFFile(FileReadBuffer->getMemBufferRef()); if (StubFromELF) { return std::move(*StubFromELF); @@ -132,14 +132,14 @@ static Expected<std::unique_ptr<ELFStub>> readInputFile(StringRef FilePath) { EC.addError(StubFromELF.takeError(), "BinaryRead"); } - // Fall back to reading as a tbe. - if (InputFormat.getNumOccurrences() == 0 || InputFormat == FileFormat::TBE) { - Expected<std::unique_ptr<ELFStub>> StubFromTBE = - readTBEFromBuffer(FileReadBuffer->getBuffer()); - if (StubFromTBE) { - return std::move(*StubFromTBE); + // 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) { + return std::move(*StubFromIFS); } - EC.addError(StubFromTBE.takeError(), "YamlParse"); + EC.addError(StubFromIFS.takeError(), "YamlParse"); } // If both readers fail, build a new error that includes all information. @@ -160,18 +160,18 @@ static void fatalError(Error Err) { int main(int argc, char *argv[]) { // Parse arguments. cl::ParseCommandLineOptions(argc, argv); - Expected<std::unique_ptr<ELFStub>> StubOrErr = readInputFile(InputFilePath); + Expected<std::unique_ptr<IFSStub>> StubOrErr = readInputFile(InputFilePath); if (!StubOrErr) fatalError(StubOrErr.takeError()); - std::unique_ptr<ELFStub> TargetStub = std::move(StubOrErr.get()); + std::unique_ptr<IFSStub> TargetStub = std::move(StubOrErr.get()); // Change SoName before emitting stubs. if (SOName.getNumOccurrences() == 1) TargetStub->SoName = SOName; - Optional<ELFArch> OverrideArch; - Optional<ELFEndiannessType> OverrideEndianness; - Optional<ELFBitWidthType> OverrideBitWidth; + Optional<IFSArch> OverrideArch; + Optional<IFSEndiannessType> OverrideEndianness; + Optional<IFSBitWidthType> OverrideBitWidth; Optional<std::string> OverrideTriple; if (OptArch.getNumOccurrences() == 1) { OverrideArch = ELF::convertArchNameToEMachine(OptArch.getValue()); @@ -183,13 +183,13 @@ int main(int argc, char *argv[]) { if (OptTargetTriple.getNumOccurrences() == 1) OverrideTriple = OptTargetTriple.getValue(); Error OverrideError = - overrideTBETarget(*TargetStub, OverrideArch, OverrideEndianness, + overrideIFSTarget(*TargetStub, OverrideArch, OverrideEndianness, OverrideBitWidth, OverrideTriple); if (OverrideError) fatalError(std::move(OverrideError)); switch (OutputFormat.getValue()) { - case FileFormat::TBE: { - TargetStub->TbeVersion = TBEVersionCurrent; + case FileFormat::IFS: { + TargetStub->IfsVersion = IFSVersionCurrent; if (InputFormat.getValue() == FileFormat::ELF && OptTargetTripleHint.getNumOccurrences() == 1) { std::error_code HintEC(1, std::generic_category()); @@ -208,19 +208,19 @@ int main(int argc, char *argv[]) { fatalError(make_error<StringError>( "Triple hint does not match the actual bit width", HintEC)); } - stripTBETarget(*TargetStub, true, false, false, false); + stripIFSTarget(*TargetStub, true, false, false, false); TargetStub->Target.Triple = OptTargetTripleHint.getValue(); } else { - stripTBETarget(*TargetStub, StripIFSTarget, StripIFSArch, + stripIFSTarget(*TargetStub, StripIFSTarget, StripIFSArch, StripIFSEndiannessWidth, StripIFSBitWidth); } - Error TBEWriteError = writeTBE(OutputFilePath.getValue(), *TargetStub); - if (TBEWriteError) - fatalError(std::move(TBEWriteError)); + Error IFSWriteError = writeIFS(OutputFilePath.getValue(), *TargetStub); + if (IFSWriteError) + fatalError(std::move(IFSWriteError)); break; } case FileFormat::ELF: { - Error TargetError = validateTBETarget(*TargetStub, true); + Error TargetError = validateIFSTarget(*TargetStub, true); if (TargetError) fatalError(std::move(TargetError)); Error BinaryWriteError = diff --git a/llvm/tools/llvm-ifs/llvm-ifs.cpp b/llvm/tools/llvm-ifs/llvm-ifs.cpp index 8ab5ef9..41c193c 100644 --- a/llvm/tools/llvm-ifs/llvm-ifs.cpp +++ b/llvm/tools/llvm-ifs/llvm-ifs.cpp @@ -10,8 +10,8 @@ #include "llvm/ADT/StringSwitch.h" #include "llvm/ADT/Triple.h" #include "llvm/InterfaceStub/ELFObjHandler.h" -#include "llvm/InterfaceStub/ELFStub.h" -#include "llvm/InterfaceStub/TBEHandler.h" +#include "llvm/InterfaceStub/IFSHandler.h" +#include "llvm/InterfaceStub/IFSStub.h" #include "llvm/ObjectYAML/yaml2obj.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Debug.h" @@ -38,7 +38,7 @@ using namespace llvm::MachO; #define DEBUG_TYPE "llvm-ifs" namespace { -const VersionTuple IFSVersionCurrent(2, 0); +const VersionTuple IFSVersionCurrent(3, 0); } // end anonymous namespace static cl::opt<std::string> Action("action", cl::desc("<llvm-ifs action>"), @@ -164,14 +164,13 @@ namespace yaml { /// YAML traits for IFSStub objects. template <> struct MappingTraits<IFSStub> { static void mapping(IO &IO, IFSStub &Stub) { - if (!IO.mapTag("!experimental-ifs-v2", true)) + if (!IO.mapTag("!ifs-v1", true)) IO.setError("Not a .ifs YAML file."); auto OldContext = IO.getContext(); IO.setContext(&Stub); IO.mapRequired("IfsVersion", Stub.IfsVersion); - IO.mapOptional("Triple", Stub.Triple); - IO.mapOptional("ObjectFileFormat", Stub.ObjectFileFormat); + IO.mapOptional("Target", Stub.Triple); IO.mapOptional("SOName", Stub.SOName); IO.mapOptional("NeededLibs", Stub.NeededLibs); IO.mapRequired("Symbols", Stub.Symbols); @@ -350,25 +349,25 @@ static int writeElfStub(const Triple &T, const std::vector<IFSSymbol> &Symbols, return convertYAML(YIn, Out, ErrHandler) ? 0 : 1; } -static Error convertIFSStub(const IFSStub &IfsStub, elfabi::ELFStub &ElfStub) { - ElfStub.TbeVersion = IfsStub.IfsVersion; +static Error convertIFSStub(const IFSStub &IfsStub, elfabi::IFSStub &ElfStub) { + ElfStub.IfsVersion = IfsStub.IfsVersion; ElfStub.SoName = IfsStub.SOName; ElfStub.Target.Triple = IfsStub.Triple; ElfStub.NeededLibs = IfsStub.NeededLibs; for (const IFSSymbol &IfsSymbol : IfsStub.Symbols) { - elfabi::ELFSymbol ElfSymbol(IfsSymbol.Name); + elfabi::IFSSymbol ElfSymbol(IfsSymbol.Name); switch (IfsSymbol.Type) { case IFSSymbolType::Func: - ElfSymbol.Type = elfabi::ELFSymbolType::Func; + ElfSymbol.Type = elfabi::IFSSymbolType::Func; break; case IFSSymbolType::NoType: - ElfSymbol.Type = elfabi::ELFSymbolType::NoType; + ElfSymbol.Type = elfabi::IFSSymbolType::NoType; break; case IFSSymbolType::Object: - ElfSymbol.Type = elfabi::ELFSymbolType::Object; + ElfSymbol.Type = elfabi::IFSSymbolType::Object; break; default: - ElfSymbol.Type = elfabi::ELFSymbolType::Unknown; + ElfSymbol.Type = elfabi::IFSSymbolType::Unknown; break; // TODO: Add support for TLS? } @@ -378,22 +377,22 @@ static Error convertIFSStub(const IFSStub &IfsStub, elfabi::ELFStub &ElfStub) { ElfSymbol.Warning = IfsSymbol.Warning; ElfStub.Symbols.push_back(ElfSymbol); } - return llvm::elfabi::validateTBETarget(ElfStub, true); + return llvm::elfabi::validateIFSTarget(ElfStub, true); } static int writeIfso(const IFSStub &Stub, bool IsWriteIfs) { std::string ObjectFileFormat = - ForceFormat.empty() ? Stub.ObjectFileFormat : ForceFormat; + ForceFormat.empty() ? std::string("ELF") : ForceFormat; // Use InterfaceStub library if the option is enabled and output // format is ELF. if (UseInterfaceStub && (!IsWriteIfs) && ObjectFileFormat != "TBD") { - elfabi::ELFStub ElfStub; - Error ConvertError = convertIFSStub(Stub, ElfStub); + elfabi::IFSStub IfsStub; + Error ConvertError = convertIFSStub(Stub, IfsStub); if (ConvertError) { return -1; } - Error BinaryWriteError = elfabi::writeBinaryStub(OutputFilename, ElfStub); + Error BinaryWriteError = elfabi::writeBinaryStub(OutputFilename, IfsStub); if (BinaryWriteError) { return -1; } |