forked from OSchip/llvm-project
[llvm-objcopy][NFC] Move ELF-specific logic into /ELF/ directory
llvm-svn: 357199
This commit is contained in:
parent
c712bac78b
commit
bd95a9f46d
|
@ -8,7 +8,6 @@
|
||||||
|
|
||||||
#include "CopyConfig.h"
|
#include "CopyConfig.h"
|
||||||
|
|
||||||
#include "llvm/ADT/BitmaskEnum.h"
|
|
||||||
#include "llvm/ADT/Optional.h"
|
#include "llvm/ADT/Optional.h"
|
||||||
#include "llvm/ADT/SmallVector.h"
|
#include "llvm/ADT/SmallVector.h"
|
||||||
#include "llvm/ADT/StringRef.h"
|
#include "llvm/ADT/StringRef.h"
|
||||||
|
@ -91,23 +90,6 @@ public:
|
||||||
StripOptTable() : OptTable(StripInfoTable) {}
|
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
|
} // namespace
|
||||||
|
|
||||||
static SectionFlag parseSectionRenameFlag(StringRef SectionName) {
|
static SectionFlag parseSectionRenameFlag(StringRef SectionName) {
|
||||||
|
@ -127,7 +109,7 @@ static SectionFlag parseSectionRenameFlag(StringRef SectionName) {
|
||||||
.Default(SectionFlag::SecNone);
|
.Default(SectionFlag::SecNone);
|
||||||
}
|
}
|
||||||
|
|
||||||
static Expected<uint64_t>
|
static Expected<SectionFlag>
|
||||||
parseSectionFlagSet(ArrayRef<StringRef> SectionFlags) {
|
parseSectionFlagSet(ArrayRef<StringRef> SectionFlags) {
|
||||||
SectionFlag ParsedFlags = SectionFlag::SecNone;
|
SectionFlag ParsedFlags = SectionFlag::SecNone;
|
||||||
for (StringRef Flag : SectionFlags) {
|
for (StringRef Flag : SectionFlags) {
|
||||||
|
@ -142,18 +124,7 @@ parseSectionFlagSet(ArrayRef<StringRef> SectionFlags) {
|
||||||
ParsedFlags |= ParsedFlag;
|
ParsedFlags |= ParsedFlag;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t NewFlags = 0;
|
return ParsedFlags;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static Expected<SectionRename> parseRenameSectionValue(StringRef FlagValue) {
|
static Expected<SectionRename> parseRenameSectionValue(StringRef FlagValue) {
|
||||||
|
@ -172,7 +143,7 @@ static Expected<SectionRename> parseRenameSectionValue(StringRef FlagValue) {
|
||||||
SR.NewName = NameAndFlags[0];
|
SR.NewName = NameAndFlags[0];
|
||||||
|
|
||||||
if (NameAndFlags.size() > 1) {
|
if (NameAndFlags.size() > 1) {
|
||||||
Expected<uint64_t> ParsedFlagSet =
|
Expected<SectionFlag> ParsedFlagSet =
|
||||||
parseSectionFlagSet(makeArrayRef(NameAndFlags).drop_front());
|
parseSectionFlagSet(makeArrayRef(NameAndFlags).drop_front());
|
||||||
if (!ParsedFlagSet)
|
if (!ParsedFlagSet)
|
||||||
return ParsedFlagSet.takeError();
|
return ParsedFlagSet.takeError();
|
||||||
|
@ -196,7 +167,7 @@ parseSetSectionFlagValue(StringRef FlagValue) {
|
||||||
// Flags split: "f1" "f2" ...
|
// Flags split: "f1" "f2" ...
|
||||||
SmallVector<StringRef, 6> SectionFlags;
|
SmallVector<StringRef, 6> SectionFlags;
|
||||||
Section2Flags.second.split(SectionFlags, ',');
|
Section2Flags.second.split(SectionFlags, ',');
|
||||||
Expected<uint64_t> ParsedFlagSet = parseSectionFlagSet(SectionFlags);
|
Expected<SectionFlag> ParsedFlagSet = parseSectionFlagSet(SectionFlags);
|
||||||
if (!ParsedFlagSet)
|
if (!ParsedFlagSet)
|
||||||
return ParsedFlagSet.takeError();
|
return ParsedFlagSet.takeError();
|
||||||
SFU.NewFlags = *ParsedFlagSet;
|
SFU.NewFlags = *ParsedFlagSet;
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
#define LLVM_TOOLS_LLVM_OBJCOPY_COPY_CONFIG_H
|
#define LLVM_TOOLS_LLVM_OBJCOPY_COPY_CONFIG_H
|
||||||
|
|
||||||
#include "llvm/ADT/ArrayRef.h"
|
#include "llvm/ADT/ArrayRef.h"
|
||||||
|
#include "llvm/ADT/BitmaskEnum.h"
|
||||||
#include "llvm/ADT/Optional.h"
|
#include "llvm/ADT/Optional.h"
|
||||||
#include "llvm/ADT/SmallVector.h"
|
#include "llvm/ADT/SmallVector.h"
|
||||||
#include "llvm/ADT/StringMap.h"
|
#include "llvm/ADT/StringMap.h"
|
||||||
|
@ -35,15 +36,35 @@ struct MachineInfo {
|
||||||
bool IsLittleEndian;
|
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 {
|
struct SectionRename {
|
||||||
StringRef OriginalName;
|
StringRef OriginalName;
|
||||||
StringRef NewName;
|
StringRef NewName;
|
||||||
Optional<uint64_t> NewFlags;
|
Optional<SectionFlag> NewFlags;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct SectionFlagsUpdate {
|
struct SectionFlagsUpdate {
|
||||||
StringRef Name;
|
StringRef Name;
|
||||||
uint64_t NewFlags;
|
SectionFlag NewFlags;
|
||||||
};
|
};
|
||||||
|
|
||||||
enum class DiscardType {
|
enum class DiscardType {
|
||||||
|
|
|
@ -70,6 +70,21 @@ static bool onlyKeepDWOPred(const Object &Obj, const SectionBase &Sec) {
|
||||||
return !isDWOSection(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,
|
static uint64_t setSectionFlagsPreserveMask(uint64_t OldFlags,
|
||||||
uint64_t NewFlags) {
|
uint64_t NewFlags) {
|
||||||
// Preserve some flags which should not be dropped when setting flags.
|
// 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;
|
const SectionRename &SR = Iter->second;
|
||||||
Sec.Name = SR.NewName;
|
Sec.Name = SR.NewName;
|
||||||
if (SR.NewFlags.hasValue())
|
if (SR.NewFlags.hasValue())
|
||||||
Sec.Flags =
|
Sec.Flags = setSectionFlagsPreserveMask(
|
||||||
setSectionFlagsPreserveMask(Sec.Flags, SR.NewFlags.getValue());
|
Sec.Flags, getNewShfFlags(SR.NewFlags.getValue()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -570,7 +585,8 @@ static Error handleArgs(const CopyConfig &Config, Object &Obj,
|
||||||
const auto Iter = Config.SetSectionFlags.find(Sec.Name);
|
const auto Iter = Config.SetSectionFlags.find(Sec.Name);
|
||||||
if (Iter != Config.SetSectionFlags.end()) {
|
if (Iter != Config.SetSectionFlags.end()) {
|
||||||
const SectionFlagsUpdate &SFU = Iter->second;
|
const SectionFlagsUpdate &SFU = Iter->second;
|
||||||
Sec.Flags = setSectionFlagsPreserveMask(Sec.Flags, SFU.NewFlags);
|
Sec.Flags = setSectionFlagsPreserveMask(Sec.Flags,
|
||||||
|
getNewShfFlags(SFU.NewFlags));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue