diff --git a/tools/llvm-elfabi/ELFObjHandler.h b/include/llvm/InterfaceStub/ELFObjHandler.h similarity index 96% rename from tools/llvm-elfabi/ELFObjHandler.h rename to include/llvm/InterfaceStub/ELFObjHandler.h index 97f0d68f4d4..1ffd9a614ee 100644 --- a/tools/llvm-elfabi/ELFObjHandler.h +++ b/include/llvm/InterfaceStub/ELFObjHandler.h @@ -13,9 +13,9 @@ #ifndef LLVM_TOOLS_ELFABI_ELFOBJHANDLER_H #define LLVM_TOOLS_ELFABI_ELFOBJHANDLER_H +#include "llvm/InterfaceStub/ELFStub.h" #include "llvm/Object/ELFObjectFile.h" #include "llvm/Object/ELFTypes.h" -#include "llvm/TextAPI/ELF/ELFStub.h" namespace llvm { diff --git a/include/llvm/TextAPI/ELF/ELFStub.h b/include/llvm/InterfaceStub/ELFStub.h similarity index 93% rename from include/llvm/TextAPI/ELF/ELFStub.h rename to include/llvm/InterfaceStub/ELFStub.h index 76b2af12166..7832c1c7413 100644 --- a/include/llvm/TextAPI/ELF/ELFStub.h +++ b/include/llvm/InterfaceStub/ELFStub.h @@ -16,8 +16,8 @@ #include "llvm/BinaryFormat/ELF.h" #include "llvm/Support/VersionTuple.h" -#include #include +#include namespace llvm { namespace elfabi { @@ -42,15 +42,13 @@ struct ELFSymbol { bool Undefined; bool Weak; Optional Warning; - bool operator<(const ELFSymbol &RHS) const { - return Name < RHS.Name; - } + bool operator<(const ELFSymbol &RHS) const { return Name < RHS.Name; } }; // A cumulative representation of ELF stubs. // Both textual and binary stubs will read into and write from this object. class ELFStub { -// TODO: Add support for symbol versioning. + // TODO: Add support for symbol versioning. public: VersionTuple TbeVersion; Optional SoName; diff --git a/include/llvm/TextAPI/ELF/TBEHandler.h b/include/llvm/InterfaceStub/TBEHandler.h similarity index 100% rename from include/llvm/TextAPI/ELF/TBEHandler.h rename to include/llvm/InterfaceStub/TBEHandler.h index 76484410987..5c523eba037 100644 --- a/include/llvm/TextAPI/ELF/TBEHandler.h +++ b/include/llvm/InterfaceStub/TBEHandler.h @@ -15,8 +15,8 @@ #ifndef LLVM_TEXTAPI_ELF_TBEHANDLER_H #define LLVM_TEXTAPI_ELF_TBEHANDLER_H -#include "llvm/Support/VersionTuple.h" #include "llvm/Support/Error.h" +#include "llvm/Support/VersionTuple.h" #include namespace llvm { diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index abe3ec59aec..35d204d7d63 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -3,6 +3,7 @@ add_subdirectory(IR) add_subdirectory(FuzzMutate) +add_subdirectory(InterfaceStub) add_subdirectory(IRReader) add_subdirectory(CodeGen) add_subdirectory(BinaryFormat) diff --git a/lib/InterfaceStub/CMakeLists.txt b/lib/InterfaceStub/CMakeLists.txt new file mode 100644 index 00000000000..be2529f6d60 --- /dev/null +++ b/lib/InterfaceStub/CMakeLists.txt @@ -0,0 +1,8 @@ +add_llvm_component_library(LLVMInterfaceStub + ELFObjHandler.cpp + ELFStub.cpp + TBEHandler.cpp + + ADDITIONAL_HEADER_DIRS + "${LLVM_MAIN_INCLUDE_DIR}/llvm/InterfaceStub" +) diff --git a/tools/llvm-elfabi/ELFObjHandler.cpp b/lib/InterfaceStub/ELFObjHandler.cpp similarity index 91% rename from tools/llvm-elfabi/ELFObjHandler.cpp rename to lib/InterfaceStub/ELFObjHandler.cpp index 124fffbb9cf..82e7a3c8b1b 100644 --- a/tools/llvm-elfabi/ELFObjHandler.cpp +++ b/lib/InterfaceStub/ELFObjHandler.cpp @@ -6,14 +6,14 @@ // //===-----------------------------------------------------------------------===/ -#include "ELFObjHandler.h" +#include "llvm/InterfaceStub/ELFObjHandler.h" +#include "llvm/InterfaceStub/ELFStub.h" #include "llvm/Object/Binary.h" #include "llvm/Object/ELFObjectFile.h" #include "llvm/Object/ELFTypes.h" #include "llvm/Support/Errc.h" #include "llvm/Support/Error.h" #include "llvm/Support/MemoryBuffer.h" -#include "llvm/TextAPI/ELF/ELFStub.h" using llvm::MemoryBufferRef; using llvm::object::ELFObjectFile; @@ -128,19 +128,17 @@ static Error populateDynamic(DynamicEntries &Dyn, "Couldn't locate dynamic symbol table (no DT_SYMTAB entry)"); } if (Dyn.SONameOffset.hasValue() && *Dyn.SONameOffset >= Dyn.StrSize) { - return createStringError( - object_error::parse_failed, - "DT_SONAME string offset (0x%016" PRIx64 - ") outside of dynamic string table", - *Dyn.SONameOffset); + return createStringError(object_error::parse_failed, + "DT_SONAME string offset (0x%016" PRIx64 + ") outside of dynamic string table", + *Dyn.SONameOffset); } for (uint64_t Offset : Dyn.NeededLibNames) { if (Offset >= Dyn.StrSize) { - return createStringError( - object_error::parse_failed, - "DT_NEEDED string offset (0x%016" PRIx64 - ") outside of dynamic string table", - Offset); + return createStringError(object_error::parse_failed, + "DT_NEEDED string offset (0x%016" PRIx64 + ") outside of dynamic string table", + Offset); } } @@ -212,16 +210,16 @@ static Expected getNumSyms(DynamicEntries &Dyn, static ELFSymbolType convertInfoToType(uint8_t Info) { Info = Info & 0xf; switch (Info) { - case ELF::STT_NOTYPE: - return ELFSymbolType::NoType; - case ELF::STT_OBJECT: - return ELFSymbolType::Object; - case ELF::STT_FUNC: - return ELFSymbolType::Func; - case ELF::STT_TLS: - return ELFSymbolType::TLS; - default: - return ELFSymbolType::Unknown; + case ELF::STT_NOTYPE: + return ELFSymbolType::NoType; + case ELF::STT_OBJECT: + return ELFSymbolType::Object; + case ELF::STT_FUNC: + return ELFSymbolType::Func; + case ELF::STT_TLS: + return ELFSymbolType::TLS; + default: + return ELFSymbolType::Unknown; } } @@ -259,8 +257,8 @@ static ELFSymbol createELFSym(StringRef SymName, /// @param DynStr StringRef to the dynamic string table. template static Error populateSymbols(ELFStub &TargetStub, - const typename ELFT::SymRange DynSym, - StringRef DynStr) { + const typename ELFT::SymRange DynSym, + StringRef DynStr) { // Skips the first symbol since it's the NULL symbol. for (auto RawSym : DynSym.drop_front(1)) { // If a symbol does not have global or weak binding, ignore it. @@ -311,7 +309,7 @@ buildStub(const ELFObjectFile &ElfObj) { if (Error Err = populateDynamic(DynEnt, *DynTable)) return std::move(Err); - // Get pointer to in-memory location of .dynstr section. + // Get pointer to in-memory location of .dynstr section. Expected DynStrPtr = ElfFile->toMappedAddr(DynEnt.StrTabAddr); if (!DynStrPtr) @@ -355,9 +353,8 @@ buildStub(const ELFObjectFile &ElfObj) { if (!DynSymPtr) return appendToError(DynSymPtr.takeError(), "when locating .dynsym section contents"); - Elf_Sym_Range DynSyms = - ArrayRef(reinterpret_cast(*DynSymPtr), - *SymCount); + Elf_Sym_Range DynSyms = ArrayRef( + reinterpret_cast(*DynSymPtr), *SymCount); Error SymReadError = populateSymbols(*DestStub, DynSyms, DynStr); if (SymReadError) return appendToError(std::move(SymReadError), diff --git a/lib/TextAPI/ELF/ELFStub.cpp b/lib/InterfaceStub/ELFStub.cpp similarity index 95% rename from lib/TextAPI/ELF/ELFStub.cpp rename to lib/InterfaceStub/ELFStub.cpp index f8463497093..3c637695d8e 100644 --- a/lib/TextAPI/ELF/ELFStub.cpp +++ b/lib/InterfaceStub/ELFStub.cpp @@ -6,7 +6,7 @@ // //===-----------------------------------------------------------------------===/ -#include "llvm/TextAPI/ELF/ELFStub.h" +#include "llvm/InterfaceStub/ELFStub.h" using namespace llvm; using namespace llvm::elfabi; diff --git a/lib/InterfaceStub/LLVMBuild.txt b/lib/InterfaceStub/LLVMBuild.txt new file mode 100644 index 00000000000..e69544d4f5f --- /dev/null +++ b/lib/InterfaceStub/LLVMBuild.txt @@ -0,0 +1,21 @@ +;===- ./lib/InterfaceStub/LLVMBuild.txt ------------------------*- Conf -*--===; +; +; 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 +; +;===------------------------------------------------------------------------===; +; +; This is an LLVMBuild description file for the components in this subdirectory. +; +; For more information on the LLVMBuild system, please see: +; +; http://llvm.org/docs/LLVMBuild.html +; +;===------------------------------------------------------------------------===; + +[component_0] +type = Library +name = InterfaceStub +parent = Libraries +required_libraries = Object Support diff --git a/lib/TextAPI/ELF/TBEHandler.cpp b/lib/InterfaceStub/TBEHandler.cpp similarity index 98% rename from lib/TextAPI/ELF/TBEHandler.cpp rename to lib/InterfaceStub/TBEHandler.cpp index cb597d8896e..06a2f9bf245 100644 --- a/lib/TextAPI/ELF/TBEHandler.cpp +++ b/lib/InterfaceStub/TBEHandler.cpp @@ -6,12 +6,12 @@ // //===-----------------------------------------------------------------------===/ -#include "llvm/TextAPI/ELF/TBEHandler.h" -#include "llvm/ADT/StringSwitch.h" +#include "llvm/InterfaceStub/TBEHandler.h" #include "llvm/ADT/StringRef.h" +#include "llvm/ADT/StringSwitch.h" +#include "llvm/InterfaceStub/ELFStub.h" #include "llvm/Support/Error.h" #include "llvm/Support/YAMLTraits.h" -#include "llvm/TextAPI/ELF/ELFStub.h" using namespace llvm; using namespace llvm::elfabi; diff --git a/lib/LLVMBuild.txt b/lib/LLVMBuild.txt index 824abd36fc9..a81c6a1fe3f 100644 --- a/lib/LLVMBuild.txt +++ b/lib/LLVMBuild.txt @@ -30,6 +30,7 @@ subdirectories = FuzzMutate LineEditor Linker + InterfaceStub IR IRReader LTO diff --git a/lib/TextAPI/CMakeLists.txt b/lib/TextAPI/CMakeLists.txt index 36528f0995d..b63bc64b31f 100644 --- a/lib/TextAPI/CMakeLists.txt +++ b/lib/TextAPI/CMakeLists.txt @@ -1,6 +1,4 @@ add_llvm_component_library(LLVMTextAPI - ELF/ELFStub.cpp - ELF/TBEHandler.cpp MachO/Architecture.cpp MachO/ArchitectureSet.cpp MachO/InterfaceFile.cpp diff --git a/tools/llvm-elfabi/CMakeLists.txt b/tools/llvm-elfabi/CMakeLists.txt index bd3ec851887..43b4b5b5faa 100644 --- a/tools/llvm-elfabi/CMakeLists.txt +++ b/tools/llvm-elfabi/CMakeLists.txt @@ -1,11 +1,11 @@ set(LLVM_LINK_COMPONENTS + InterfaceStub Object Support TextAPI ) add_llvm_tool(llvm-elfabi - ELFObjHandler.cpp ErrorCollector.cpp llvm-elfabi.cpp ) diff --git a/tools/llvm-elfabi/LLVMBuild.txt b/tools/llvm-elfabi/LLVMBuild.txt index b1a80e9e4a1..4e8021442fb 100644 --- a/tools/llvm-elfabi/LLVMBuild.txt +++ b/tools/llvm-elfabi/LLVMBuild.txt @@ -18,4 +18,4 @@ type = Tool name = llvm-elfabi parent = Tools -required_libraries = Object Support TextAPI +required_libraries = InterfaceStub Object Support TextAPI diff --git a/tools/llvm-elfabi/llvm-elfabi.cpp b/tools/llvm-elfabi/llvm-elfabi.cpp index 044b5f77c6d..8bf2ad4ed53 100644 --- a/tools/llvm-elfabi/llvm-elfabi.cpp +++ b/tools/llvm-elfabi/llvm-elfabi.cpp @@ -6,16 +6,16 @@ // //===-----------------------------------------------------------------------===/ -#include "ELFObjHandler.h" #include "ErrorCollector.h" +#include "llvm/InterfaceStub/ELFObjHandler.h" +#include "llvm/InterfaceStub/TBEHandler.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Errc.h" #include "llvm/Support/FileOutputBuffer.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/Path.h" -#include "llvm/Support/raw_ostream.h" #include "llvm/Support/WithColor.h" -#include "llvm/TextAPI/ELF/TBEHandler.h" +#include "llvm/Support/raw_ostream.h" #include namespace llvm { diff --git a/unittests/CMakeLists.txt b/unittests/CMakeLists.txt index d7dbaeaa32f..850bc14b207 100644 --- a/unittests/CMakeLists.txt +++ b/unittests/CMakeLists.txt @@ -26,6 +26,7 @@ add_subdirectory(Demangle) add_subdirectory(ExecutionEngine) add_subdirectory(Frontend) add_subdirectory(FuzzMutate) +add_subdirectory(InterfaceStub) add_subdirectory(IR) add_subdirectory(LineEditor) add_subdirectory(Linker) diff --git a/unittests/InterfaceStub/CMakeLists.txt b/unittests/InterfaceStub/CMakeLists.txt new file mode 100644 index 00000000000..22ff0af67a3 --- /dev/null +++ b/unittests/InterfaceStub/CMakeLists.txt @@ -0,0 +1,9 @@ +set(LLVM_LINK_COMPONENTS + InterfaceStub +) + +add_llvm_unittest(InterfaceStubTests + ELFYAMLTest.cpp +) + +target_link_libraries(InterfaceStubTests PRIVATE LLVMTestingSupport) diff --git a/unittests/TextAPI/ELFYAMLTest.cpp b/unittests/InterfaceStub/ELFYAMLTest.cpp similarity index 98% rename from unittests/TextAPI/ELFYAMLTest.cpp rename to unittests/InterfaceStub/ELFYAMLTest.cpp index 8217507b5a5..2b86b89fbbb 100644 --- a/unittests/TextAPI/ELFYAMLTest.cpp +++ b/unittests/InterfaceStub/ELFYAMLTest.cpp @@ -7,8 +7,8 @@ //===-----------------------------------------------------------------------===/ #include "llvm/ADT/StringRef.h" -#include "llvm/TextAPI/ELF/ELFStub.h" -#include "llvm/TextAPI/ELF/TBEHandler.h" +#include "llvm/InterfaceStub/ELFStub.h" +#include "llvm/InterfaceStub/TBEHandler.h" #include "llvm/Support/Error.h" #include "llvm/Testing/Support/Error.h" #include "gtest/gtest.h" diff --git a/unittests/TextAPI/CMakeLists.txt b/unittests/TextAPI/CMakeLists.txt index 775ec2f1d1e..d575d57610b 100644 --- a/unittests/TextAPI/CMakeLists.txt +++ b/unittests/TextAPI/CMakeLists.txt @@ -3,7 +3,6 @@ set(LLVM_LINK_COMPONENTS ) add_llvm_unittest(TextAPITests - ELFYAMLTest.cpp TextStubV1Tests.cpp TextStubV2Tests.cpp TextStubV3Tests.cpp