#ifndef LLVM_OBJCOPY_COMMONCONFIG_H
#define LLVM_OBJCOPY_COMMONCONFIG_H
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/CachedHashString.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Object/ELFTypes.h"
#include "llvm/Support/GlobPattern.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Regex.h"
#include "llvm/Target/TargetOptions.h"
#include <vector>
namespace llvm {
namespace objcopy {
enum class FileFormat {
Unspecified,
ELF,
Binary,
IHex,
};
struct MachineInfo {
MachineInfo(uint16_t EM, uint8_t ABI, bool Is64, bool IsLittle)
: EMachine(EM), OSABI(ABI), Is64Bit(Is64), IsLittleEndian(IsLittle) {}
MachineInfo(uint16_t EM, bool Is64, bool IsLittle)
: MachineInfo(EM, ELF::ELFOSABI_NONE, Is64, IsLittle) {}
MachineInfo() : MachineInfo(0, 0, false, false) {}
uint16_t EMachine;
uint8_t OSABI;
bool Is64Bit;
bool IsLittleEndian;
};
enum SectionFlag {
SecNone = 0,
SecAlloc = 1 << 0,
SecLoad = 1 << 1,
SecNoload = 1 << 2,
SecReadonly = 1 << 3,
SecDebug = 1 << 4,
SecCode = 1 << 5,
SecData = 1 << 6,
SecRom = 1 << 7,
SecMerge = 1 << 8,
SecStrings = 1 << 9,
SecContents = 1 << 10,
SecShare = 1 << 11,
SecExclude = 1 << 12,
LLVM_MARK_AS_BITMASK_ENUM(SecExclude)
};
struct SectionRename {
StringRef OriginalName;
StringRef NewName;
Optional<SectionFlag> NewFlags;
};
struct SectionFlagsUpdate {
StringRef Name;
SectionFlag NewFlags;
};
enum class DiscardType {
None, All, Locals, };
enum class MatchStyle {
Literal, Wildcard, Regex, };
class NameOrPattern {
StringRef Name;
std::shared_ptr<Regex> R;
std::shared_ptr<GlobPattern> G;
bool IsPositiveMatch = true;
NameOrPattern(StringRef N) : Name(N) {}
NameOrPattern(std::shared_ptr<Regex> R) : R(R) {}
NameOrPattern(std::shared_ptr<GlobPattern> G, bool IsPositiveMatch)
: G(G), IsPositiveMatch(IsPositiveMatch) {}
public:
static Expected<NameOrPattern>
create(StringRef Pattern, MatchStyle MS,
llvm::function_ref<Error(Error)> ErrorCallback);
bool isPositiveMatch() const { return IsPositiveMatch; }
Optional<StringRef> getName() const {
if (!R && !G)
return Name;
return None;
}
bool operator==(StringRef S) const {
return R ? R->match(S) : G ? G->match(S) : Name == S;
}
bool operator!=(StringRef S) const { return !operator==(S); }
};
class NameMatcher {
DenseSet<CachedHashStringRef> PosNames;
std::vector<NameOrPattern> PosPatterns;
std::vector<NameOrPattern> NegMatchers;
public:
Error addMatcher(Expected<NameOrPattern> Matcher) {
if (!Matcher)
return Matcher.takeError();
if (Matcher->isPositiveMatch()) {
if (Optional<StringRef> MaybeName = Matcher->getName())
PosNames.insert(CachedHashStringRef(*MaybeName));
else
PosPatterns.push_back(std::move(*Matcher));
} else {
NegMatchers.push_back(std::move(*Matcher));
}
return Error::success();
}
bool matches(StringRef S) const {
return (PosNames.contains(CachedHashStringRef(S)) ||
is_contained(PosPatterns, S)) &&
!is_contained(NegMatchers, S);
}
bool empty() const {
return PosNames.empty() && PosPatterns.empty() && NegMatchers.empty();
}
};
enum class SymbolFlag {
Global,
Local,
Weak,
Default,
Hidden,
Protected,
File,
Section,
Object,
Function,
IndirectFunction,
Debug,
Constructor,
Warning,
Indirect,
Synthetic,
UniqueObject,
};
struct NewSymbolInfo {
StringRef SymbolName;
StringRef SectionName;
uint64_t Value = 0;
std::vector<SymbolFlag> Flags;
std::vector<StringRef> BeforeSyms;
};
struct NewSectionInfo {
NewSectionInfo() = default;
NewSectionInfo(StringRef Name, std::unique_ptr<MemoryBuffer> &&Buffer)
: SectionName(Name), SectionData(std::move(Buffer)) {}
StringRef SectionName;
std::shared_ptr<MemoryBuffer> SectionData;
};
struct CommonConfig {
StringRef InputFilename;
FileFormat InputFormat = FileFormat::Unspecified;
StringRef OutputFilename;
FileFormat OutputFormat = FileFormat::Unspecified;
Optional<MachineInfo> OutputArch;
StringRef AddGnuDebugLink;
uint32_t GnuDebugLinkCRC32;
Optional<StringRef> ExtractPartition;
StringRef SplitDWO;
StringRef SymbolsPrefix;
StringRef AllocSectionsPrefix;
DiscardType DiscardMode = DiscardType::None;
std::vector<NewSectionInfo> AddSection;
std::vector<StringRef> DumpSection;
std::vector<NewSectionInfo> UpdateSection;
NameMatcher KeepSection;
NameMatcher OnlySection;
NameMatcher ToRemove;
NameMatcher SymbolsToGlobalize;
NameMatcher SymbolsToKeep;
NameMatcher SymbolsToLocalize;
NameMatcher SymbolsToRemove;
NameMatcher UnneededSymbolsToRemove;
NameMatcher SymbolsToWeaken;
NameMatcher SymbolsToKeepGlobal;
StringMap<SectionRename> SectionsToRename;
StringMap<uint64_t> SetSectionAlignment;
StringMap<SectionFlagsUpdate> SetSectionFlags;
StringMap<uint64_t> SetSectionType;
StringMap<StringRef> SymbolsToRename;
std::vector<NewSymbolInfo> SymbolsToAdd;
bool DeterministicArchives = true;
bool ExtractDWO = false;
bool ExtractMainPartition = false;
bool OnlyKeepDebug = false;
bool PreserveDates = false;
bool StripAll = false;
bool StripAllGNU = false;
bool StripDWO = false;
bool StripDebug = false;
bool StripNonAlloc = false;
bool StripSections = false;
bool StripUnneeded = false;
bool Weaken = false;
bool DecompressDebugSections = false;
DebugCompressionType CompressionType = DebugCompressionType::None;
};
} }
#endif