From: Jordan Rupprecht Date: Thu, 28 Mar 2019 18:27:00 +0000 (+0000) Subject: [llvm-objcopy][NFC] Move ELF-specific logic into /ELF/ directory X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d7f746ca03b53cf199465acdd36833837af71e1a;p=llvm [llvm-objcopy][NFC] Move ELF-specific logic into /ELF/ directory git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@357199 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/tools/llvm-objcopy/CopyConfig.cpp b/tools/llvm-objcopy/CopyConfig.cpp index bc2b384f73c..cf0ed2fc8a0 100644 --- a/tools/llvm-objcopy/CopyConfig.cpp +++ b/tools/llvm-objcopy/CopyConfig.cpp @@ -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 +static Expected parseSectionFlagSet(ArrayRef SectionFlags) { SectionFlag ParsedFlags = SectionFlag::SecNone; for (StringRef Flag : SectionFlags) { @@ -142,18 +124,7 @@ parseSectionFlagSet(ArrayRef 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 parseRenameSectionValue(StringRef FlagValue) { @@ -172,7 +143,7 @@ static Expected parseRenameSectionValue(StringRef FlagValue) { SR.NewName = NameAndFlags[0]; if (NameAndFlags.size() > 1) { - Expected ParsedFlagSet = + Expected ParsedFlagSet = parseSectionFlagSet(makeArrayRef(NameAndFlags).drop_front()); if (!ParsedFlagSet) return ParsedFlagSet.takeError(); @@ -196,7 +167,7 @@ parseSetSectionFlagValue(StringRef FlagValue) { // Flags split: "f1" "f2" ... SmallVector SectionFlags; Section2Flags.second.split(SectionFlags, ','); - Expected ParsedFlagSet = parseSectionFlagSet(SectionFlags); + Expected ParsedFlagSet = parseSectionFlagSet(SectionFlags); if (!ParsedFlagSet) return ParsedFlagSet.takeError(); SFU.NewFlags = *ParsedFlagSet; diff --git a/tools/llvm-objcopy/CopyConfig.h b/tools/llvm-objcopy/CopyConfig.h index 1c1d74fc3f7..3fe4ab4f42f 100644 --- a/tools/llvm-objcopy/CopyConfig.h +++ b/tools/llvm-objcopy/CopyConfig.h @@ -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 NewFlags; + Optional NewFlags; }; struct SectionFlagsUpdate { StringRef Name; - uint64_t NewFlags; + SectionFlag NewFlags; }; enum class DiscardType { diff --git a/tools/llvm-objcopy/ELF/ELFObjcopy.cpp b/tools/llvm-objcopy/ELF/ELFObjcopy.cpp index 0cf195de113..26f6987d808 100644 --- a/tools/llvm-objcopy/ELF/ELFObjcopy.cpp +++ b/tools/llvm-objcopy/ELF/ELFObjcopy.cpp @@ -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 SecPair = Flag.split("="); StringRef SecName = SecPair.first;