[llvm-objcopy][NFC] Move ELF-specific logic into /ELF/ directory

llvm-svn: 357199
This commit is contained in:
Jordan Rupprecht 2019-03-28 18:27:00 +00:00
parent c712bac78b
commit bd95a9f46d
3 changed files with 47 additions and 39 deletions

View File

@ -8,7 +8,6 @@
#include "CopyConfig.h"
#include "llvm/ADT/BitmaskEnum.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
@ -91,23 +90,6 @@ public:
StripOptTable() : OptTable(StripInfoTable) {}
};
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,
LLVM_MARK_AS_BITMASK_ENUM(/* LargestValue = */ SecShare)
};
} // namespace
static SectionFlag parseSectionRenameFlag(StringRef SectionName) {
@ -127,7 +109,7 @@ static SectionFlag parseSectionRenameFlag(StringRef SectionName) {
.Default(SectionFlag::SecNone);
}
static Expected<uint64_t>
static Expected<SectionFlag>
parseSectionFlagSet(ArrayRef<StringRef> SectionFlags) {
SectionFlag ParsedFlags = SectionFlag::SecNone;
for (StringRef Flag : SectionFlags) {
@ -142,18 +124,7 @@ parseSectionFlagSet(ArrayRef<StringRef> SectionFlags) {
ParsedFlags |= ParsedFlag;
}
uint64_t NewFlags = 0;
if (ParsedFlags & SectionFlag::SecAlloc)
NewFlags |= ELF::SHF_ALLOC;
if (!(ParsedFlags & SectionFlag::SecReadonly))
NewFlags |= ELF::SHF_WRITE;
if (ParsedFlags & SectionFlag::SecCode)
NewFlags |= ELF::SHF_EXECINSTR;
if (ParsedFlags & SectionFlag::SecMerge)
NewFlags |= ELF::SHF_MERGE;
if (ParsedFlags & SectionFlag::SecStrings)
NewFlags |= ELF::SHF_STRINGS;
return NewFlags;
return ParsedFlags;
}
static Expected<SectionRename> parseRenameSectionValue(StringRef FlagValue) {
@ -172,7 +143,7 @@ static Expected<SectionRename> parseRenameSectionValue(StringRef FlagValue) {
SR.NewName = NameAndFlags[0];
if (NameAndFlags.size() > 1) {
Expected<uint64_t> ParsedFlagSet =
Expected<SectionFlag> ParsedFlagSet =
parseSectionFlagSet(makeArrayRef(NameAndFlags).drop_front());
if (!ParsedFlagSet)
return ParsedFlagSet.takeError();
@ -196,7 +167,7 @@ parseSetSectionFlagValue(StringRef FlagValue) {
// Flags split: "f1" "f2" ...
SmallVector<StringRef, 6> SectionFlags;
Section2Flags.second.split(SectionFlags, ',');
Expected<uint64_t> ParsedFlagSet = parseSectionFlagSet(SectionFlags);
Expected<SectionFlag> ParsedFlagSet = parseSectionFlagSet(SectionFlags);
if (!ParsedFlagSet)
return ParsedFlagSet.takeError();
SFU.NewFlags = *ParsedFlagSet;

View File

@ -10,6 +10,7 @@
#define LLVM_TOOLS_LLVM_OBJCOPY_COPY_CONFIG_H
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/BitmaskEnum.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringMap.h"
@ -35,15 +36,35 @@ struct MachineInfo {
bool IsLittleEndian;
};
// Flags set by --set-section-flags or --rename-section. Interpretation of these
// is format-specific and not all flags are meaningful for all object file
// formats. This is a bitmask; many section flags may be set.
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,
LLVM_MARK_AS_BITMASK_ENUM(/* LargestValue = */ SecShare)
};
struct SectionRename {
StringRef OriginalName;
StringRef NewName;
Optional<uint64_t> NewFlags;
Optional<SectionFlag> NewFlags;
};
struct SectionFlagsUpdate {
StringRef Name;
uint64_t NewFlags;
SectionFlag NewFlags;
};
enum class DiscardType {

View File

@ -70,6 +70,21 @@ static bool onlyKeepDWOPred(const Object &Obj, const SectionBase &Sec) {
return !isDWOSection(Sec);
}
uint64_t getNewShfFlags(SectionFlag AllFlags) {
uint64_t NewFlags = 0;
if (AllFlags & SectionFlag::SecAlloc)
NewFlags |= ELF::SHF_ALLOC;
if (!(AllFlags & SectionFlag::SecReadonly))
NewFlags |= ELF::SHF_WRITE;
if (AllFlags & SectionFlag::SecCode)
NewFlags |= ELF::SHF_EXECINSTR;
if (AllFlags & SectionFlag::SecMerge)
NewFlags |= ELF::SHF_MERGE;
if (AllFlags & SectionFlag::SecStrings)
NewFlags |= ELF::SHF_STRINGS;
return NewFlags;
}
static uint64_t setSectionFlagsPreserveMask(uint64_t OldFlags,
uint64_t NewFlags) {
// Preserve some flags which should not be dropped when setting flags.
@ -559,8 +574,8 @@ static Error handleArgs(const CopyConfig &Config, Object &Obj,
const SectionRename &SR = Iter->second;
Sec.Name = SR.NewName;
if (SR.NewFlags.hasValue())
Sec.Flags =
setSectionFlagsPreserveMask(Sec.Flags, SR.NewFlags.getValue());
Sec.Flags = setSectionFlagsPreserveMask(
Sec.Flags, getNewShfFlags(SR.NewFlags.getValue()));
}
}
}
@ -570,11 +585,12 @@ static Error handleArgs(const CopyConfig &Config, Object &Obj,
const auto Iter = Config.SetSectionFlags.find(Sec.Name);
if (Iter != Config.SetSectionFlags.end()) {
const SectionFlagsUpdate &SFU = Iter->second;
Sec.Flags = setSectionFlagsPreserveMask(Sec.Flags, SFU.NewFlags);
Sec.Flags = setSectionFlagsPreserveMask(Sec.Flags,
getNewShfFlags(SFU.NewFlags));
}
}
}
for (const auto &Flag : Config.AddSection) {
std::pair<StringRef, StringRef> SecPair = Flag.split("=");
StringRef SecName = SecPair.first;