Index: COFF/CMakeLists.txt =================================================================== --- COFF/CMakeLists.txt +++ COFF/CMakeLists.txt @@ -18,7 +18,7 @@ MarkLive.cpp MinGW.cpp PDB.cpp - Strings.cpp + COFFStrings.cpp SymbolTable.cpp Symbols.cpp Writer.cpp Index: COFF/COFFStrings.h =================================================================== --- /dev/null +++ COFF/COFFStrings.h @@ -0,0 +1,23 @@ +//===- COFFStrings.h --------------------------------------------*- C++ -*-===// +// +// The LLVM Linker +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLD_COFF_STRINGS_H +#define LLD_COFF_STRINGS_H + +#include "llvm/ADT/Optional.h" +#include "llvm/ADT/StringRef.h" +#include + +namespace lld { +namespace coff { +llvm::Optional demangleMSVC(llvm::StringRef S); +} +} + +#endif Index: COFF/COFFStrings.cpp =================================================================== --- /dev/null +++ COFF/COFFStrings.cpp @@ -0,0 +1,35 @@ +//===- Strings.cpp -------------------------------------------------------===// +// +// The LLVM Linker +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "COFFStrings.h" +#include + +#if defined(_MSC_VER) +#include +#include +#pragma comment(lib, "dbghelp.lib") +#endif + +using namespace lld; +using namespace lld::coff; +using namespace llvm; + +Optional coff::demangleMSVC(StringRef S) { +#if defined(_MSC_VER) + // UnDecorateSymbolName is not thread-safe, so we need a mutex. + static std::mutex Mu; + std::lock_guard Lock(Mu); + + char Buf[4096]; + if (S.startswith("?")) + if (size_t Len = UnDecorateSymbolName(S.str().c_str(), Buf, sizeof(Buf), 0)) + return std::string(Buf, Len); +#endif + return None; +} Index: COFF/Strings.h =================================================================== --- COFF/Strings.h +++ /dev/null @@ -1,23 +0,0 @@ -//===- Strings.h ------------------------------------------------*- C++ -*-===// -// -// The LLVM Linker -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -#ifndef LLD_COFF_STRINGS_H -#define LLD_COFF_STRINGS_H - -#include "llvm/ADT/Optional.h" -#include "llvm/ADT/StringRef.h" -#include - -namespace lld { -namespace coff { -llvm::Optional demangleMSVC(llvm::StringRef S); -} -} - -#endif Index: COFF/Strings.cpp =================================================================== --- COFF/Strings.cpp +++ /dev/null @@ -1,35 +0,0 @@ -//===- Strings.cpp -------------------------------------------------------===// -// -// The LLVM Linker -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -#include "Strings.h" -#include - -#if defined(_MSC_VER) -#include -#include -#pragma comment(lib, "dbghelp.lib") -#endif - -using namespace lld; -using namespace lld::coff; -using namespace llvm; - -Optional coff::demangleMSVC(StringRef S) { -#if defined(_MSC_VER) - // UnDecorateSymbolName is not thread-safe, so we need a mutex. - static std::mutex Mu; - std::lock_guard Lock(Mu); - - char Buf[4096]; - if (S.startswith("?")) - if (size_t Len = UnDecorateSymbolName(S.str().c_str(), Buf, sizeof(Buf), 0)) - return std::string(Buf, Len); -#endif - return None; -} Index: COFF/Symbols.cpp =================================================================== --- COFF/Symbols.cpp +++ COFF/Symbols.cpp @@ -9,7 +9,7 @@ #include "Symbols.h" #include "InputFiles.h" -#include "Strings.h" +#include "COFFStrings.h" #include "lld/Common/ErrorHandler.h" #include "lld/Common/Memory.h" #include "llvm/ADT/STLExtras.h" Index: ELF/AArch64ErrataFix.cpp =================================================================== --- ELF/AArch64ErrataFix.cpp +++ ELF/AArch64ErrataFix.cpp @@ -34,7 +34,7 @@ #include "LinkerScript.h" #include "OutputSections.h" #include "Relocations.h" -#include "Strings.h" +#include "ELFStrings.h" #include "Symbols.h" #include "SyntheticSections.h" #include "Target.h" Index: ELF/CMakeLists.txt =================================================================== --- ELF/CMakeLists.txt +++ ELF/CMakeLists.txt @@ -35,7 +35,7 @@ Relocations.cpp ScriptLexer.cpp ScriptParser.cpp - Strings.cpp + ELFStrings.cpp SymbolTable.cpp Symbols.cpp SyntheticSections.cpp Index: ELF/Driver.cpp =================================================================== --- ELF/Driver.cpp +++ ELF/Driver.cpp @@ -33,7 +33,7 @@ #include "MarkLive.h" #include "OutputSections.h" #include "ScriptParser.h" -#include "Strings.h" +#include "ELFStrings.h" #include "SymbolTable.h" #include "Symbols.h" #include "SyntheticSections.h" Index: ELF/ELFStrings.h =================================================================== --- /dev/null +++ ELF/ELFStrings.h @@ -0,0 +1,75 @@ +//===- ELFStrings.h ---------------------------------------------*- C++ -*-===// +// +// The LLVM Linker +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLD_ELF_STRINGS_H +#define LLD_ELF_STRINGS_H + +#include "lld/Common/LLVM.h" +#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/BitVector.h" +#include "llvm/ADT/Optional.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/GlobPattern.h" +#include + +namespace lld { +namespace elf { + +std::vector parseHex(StringRef S); +bool isValidCIdentifier(StringRef S); + +// This is a lazy version of StringRef. String size is computed lazily +// when it is needed. It is more efficient than StringRef to instantiate +// if you have a string whose size is unknown. +// +// ELF string tables contain a lot of null-terminated strings. +// Most of them are not necessary for the linker because they are names +// of local symbols and the linker doesn't use local symbol names for +// name resolution. So, we use this class to represents strings read +// from string tables. +class StringRefZ { +public: + StringRefZ() : Start(nullptr), Size(0) {} + StringRefZ(const char *S, size_t Size) : Start(S), Size(Size) {} + + /*implicit*/ StringRefZ(const char *S) : Start(S), Size(-1) {} + + /*implicit*/ StringRefZ(llvm::StringRef S) + : Start(S.data()), Size(S.size()) {} + + operator llvm::StringRef() const { + if (Size == (size_t)-1) + Size = strlen(Start); + return {Start, Size}; + } + +private: + const char *Start; + mutable size_t Size; +}; + +// This class represents multiple glob patterns. +class StringMatcher { +public: + StringMatcher() = default; + explicit StringMatcher(ArrayRef Pat); + + bool match(StringRef S) const; + +private: + std::vector Patterns; +}; + +inline ArrayRef toArrayRef(StringRef S) { + return {(const uint8_t *)S.data(), S.size()}; +} +} // namespace elf +} // namespace lld + +#endif Index: ELF/ELFStrings.cpp =================================================================== --- /dev/null +++ ELF/ELFStrings.cpp @@ -0,0 +1,60 @@ +//===- Strings.cpp -------------------------------------------------------===// +// +// The LLVM Linker +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "ELFStrings.h" +#include "Config.h" +#include "lld/Common/ErrorHandler.h" +#include "llvm/ADT/Twine.h" +#include "llvm/Demangle/Demangle.h" +#include +#include + +using namespace llvm; +using namespace lld; +using namespace lld::elf; + +StringMatcher::StringMatcher(ArrayRef Pat) { + for (StringRef S : Pat) { + Expected Pat = GlobPattern::create(S); + if (!Pat) + error(toString(Pat.takeError())); + else + Patterns.push_back(*Pat); + } +} + +bool StringMatcher::match(StringRef S) const { + for (const GlobPattern &Pat : Patterns) + if (Pat.match(S)) + return true; + return false; +} + +// Converts a hex string (e.g. "deadbeef") to a vector. +std::vector elf::parseHex(StringRef S) { + std::vector Hex; + while (!S.empty()) { + StringRef B = S.substr(0, 2); + S = S.substr(2); + uint8_t H; + if (!to_integer(B, H, 16)) { + error("not a hexadecimal value: " + B); + return {}; + } + Hex.push_back(H); + } + return Hex; +} + +// Returns true if S is valid as a C language identifier. +bool elf::isValidCIdentifier(StringRef S) { + return !S.empty() && (isAlpha(S[0]) || S[0] == '_') && + std::all_of(S.begin() + 1, S.end(), + [](char C) { return C == '_' || isAlnum(C); }); +} Index: ELF/EhFrame.cpp =================================================================== --- ELF/EhFrame.cpp +++ ELF/EhFrame.cpp @@ -20,7 +20,7 @@ #include "Config.h" #include "InputSection.h" #include "Relocations.h" -#include "Strings.h" +#include "ELFStrings.h" #include "lld/Common/ErrorHandler.h" #include "llvm/BinaryFormat/Dwarf.h" #include "llvm/Object/ELF.h" Index: ELF/LinkerScript.h =================================================================== --- ELF/LinkerScript.h +++ ELF/LinkerScript.h @@ -11,7 +11,7 @@ #define LLD_ELF_LINKER_SCRIPT_H #include "Config.h" -#include "Strings.h" +#include "ELFStrings.h" #include "Writer.h" #include "lld/Common/LLVM.h" #include "llvm/ADT/ArrayRef.h" Index: ELF/LinkerScript.cpp =================================================================== --- ELF/LinkerScript.cpp +++ ELF/LinkerScript.cpp @@ -15,7 +15,7 @@ #include "Config.h" #include "InputSection.h" #include "OutputSections.h" -#include "Strings.h" +#include "ELFStrings.h" #include "SymbolTable.h" #include "Symbols.h" #include "SyntheticSections.h" Index: ELF/MapFile.cpp =================================================================== --- ELF/MapFile.cpp +++ ELF/MapFile.cpp @@ -23,7 +23,7 @@ #include "InputFiles.h" #include "LinkerScript.h" #include "OutputSections.h" -#include "Strings.h" +#include "ELFStrings.h" #include "SymbolTable.h" #include "Symbols.h" #include "SyntheticSections.h" Index: ELF/MarkLive.cpp =================================================================== --- ELF/MarkLive.cpp +++ ELF/MarkLive.cpp @@ -24,7 +24,7 @@ #include "InputSection.h" #include "LinkerScript.h" #include "OutputSections.h" -#include "Strings.h" +#include "ELFStrings.h" #include "SymbolTable.h" #include "Symbols.h" #include "Target.h" Index: ELF/OutputSections.cpp =================================================================== --- ELF/OutputSections.cpp +++ ELF/OutputSections.cpp @@ -10,7 +10,7 @@ #include "OutputSections.h" #include "Config.h" #include "LinkerScript.h" -#include "Strings.h" +#include "ELFStrings.h" #include "SymbolTable.h" #include "SyntheticSections.h" #include "Target.h" Index: ELF/Relocations.cpp =================================================================== --- ELF/Relocations.cpp +++ ELF/Relocations.cpp @@ -45,7 +45,7 @@ #include "Config.h" #include "LinkerScript.h" #include "OutputSections.h" -#include "Strings.h" +#include "ELFStrings.h" #include "SymbolTable.h" #include "Symbols.h" #include "SyntheticSections.h" Index: ELF/Strings.h =================================================================== --- ELF/Strings.h +++ /dev/null @@ -1,75 +0,0 @@ -//===- Strings.h ------------------------------------------------*- C++ -*-===// -// -// The LLVM Linker -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -#ifndef LLD_ELF_STRINGS_H -#define LLD_ELF_STRINGS_H - -#include "lld/Common/LLVM.h" -#include "llvm/ADT/ArrayRef.h" -#include "llvm/ADT/BitVector.h" -#include "llvm/ADT/Optional.h" -#include "llvm/ADT/StringRef.h" -#include "llvm/Support/GlobPattern.h" -#include - -namespace lld { -namespace elf { - -std::vector parseHex(StringRef S); -bool isValidCIdentifier(StringRef S); - -// This is a lazy version of StringRef. String size is computed lazily -// when it is needed. It is more efficient than StringRef to instantiate -// if you have a string whose size is unknown. -// -// ELF string tables contain a lot of null-terminated strings. -// Most of them are not necessary for the linker because they are names -// of local symbols and the linker doesn't use local symbol names for -// name resolution. So, we use this class to represents strings read -// from string tables. -class StringRefZ { -public: - StringRefZ() : Start(nullptr), Size(0) {} - StringRefZ(const char *S, size_t Size) : Start(S), Size(Size) {} - - /*implicit*/ StringRefZ(const char *S) : Start(S), Size(-1) {} - - /*implicit*/ StringRefZ(llvm::StringRef S) - : Start(S.data()), Size(S.size()) {} - - operator llvm::StringRef() const { - if (Size == (size_t)-1) - Size = strlen(Start); - return {Start, Size}; - } - -private: - const char *Start; - mutable size_t Size; -}; - -// This class represents multiple glob patterns. -class StringMatcher { -public: - StringMatcher() = default; - explicit StringMatcher(ArrayRef Pat); - - bool match(StringRef S) const; - -private: - std::vector Patterns; -}; - -inline ArrayRef toArrayRef(StringRef S) { - return {(const uint8_t *)S.data(), S.size()}; -} -} // namespace elf -} // namespace lld - -#endif Index: ELF/Strings.cpp =================================================================== --- ELF/Strings.cpp +++ /dev/null @@ -1,60 +0,0 @@ -//===- Strings.cpp -------------------------------------------------------===// -// -// The LLVM Linker -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -#include "Strings.h" -#include "Config.h" -#include "lld/Common/ErrorHandler.h" -#include "llvm/ADT/Twine.h" -#include "llvm/Demangle/Demangle.h" -#include -#include - -using namespace llvm; -using namespace lld; -using namespace lld::elf; - -StringMatcher::StringMatcher(ArrayRef Pat) { - for (StringRef S : Pat) { - Expected Pat = GlobPattern::create(S); - if (!Pat) - error(toString(Pat.takeError())); - else - Patterns.push_back(*Pat); - } -} - -bool StringMatcher::match(StringRef S) const { - for (const GlobPattern &Pat : Patterns) - if (Pat.match(S)) - return true; - return false; -} - -// Converts a hex string (e.g. "deadbeef") to a vector. -std::vector elf::parseHex(StringRef S) { - std::vector Hex; - while (!S.empty()) { - StringRef B = S.substr(0, 2); - S = S.substr(2); - uint8_t H; - if (!to_integer(B, H, 16)) { - error("not a hexadecimal value: " + B); - return {}; - } - Hex.push_back(H); - } - return Hex; -} - -// Returns true if S is valid as a C language identifier. -bool elf::isValidCIdentifier(StringRef S) { - return !S.empty() && (isAlpha(S[0]) || S[0] == '_') && - std::all_of(S.begin() + 1, S.end(), - [](char C) { return C == '_' || isAlnum(C); }); -} Index: ELF/SymbolTable.h =================================================================== --- ELF/SymbolTable.h +++ ELF/SymbolTable.h @@ -12,7 +12,7 @@ #include "InputFiles.h" #include "LTO.h" -#include "Strings.h" +#include "ELFStrings.h" #include "llvm/ADT/CachedHashString.h" #include "llvm/ADT/DenseMap.h" Index: ELF/Symbols.h =================================================================== --- ELF/Symbols.h +++ ELF/Symbols.h @@ -16,7 +16,7 @@ #define LLD_ELF_SYMBOLS_H #include "InputSection.h" -#include "Strings.h" +#include "ELFStrings.h" #include "lld/Common/LLVM.h" #include "llvm/Object/Archive.h" #include "llvm/Object/ELF.h" Index: ELF/SyntheticSections.cpp =================================================================== --- ELF/SyntheticSections.cpp +++ ELF/SyntheticSections.cpp @@ -20,7 +20,7 @@ #include "InputFiles.h" #include "LinkerScript.h" #include "OutputSections.h" -#include "Strings.h" +#include "ELFStrings.h" #include "SymbolTable.h" #include "Symbols.h" #include "Target.h" Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -15,7 +15,7 @@ #include "MapFile.h" #include "OutputSections.h" #include "Relocations.h" -#include "Strings.h" +#include "ELFStrings.h" #include "SymbolTable.h" #include "Symbols.h" #include "SyntheticSections.h"