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

Jordan Rupprecht via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 28 11:27:00 PDT 2019


Author: rupprecht
Date: Thu Mar 28 11:27:00 2019
New Revision: 357199

URL: http://llvm.org/viewvc/llvm-project?rev=357199&view=rev
Log:
[llvm-objcopy][NFC] Move ELF-specific logic into /ELF/ directory

Modified:
    llvm/trunk/tools/llvm-objcopy/CopyConfig.cpp
    llvm/trunk/tools/llvm-objcopy/CopyConfig.h
    llvm/trunk/tools/llvm-objcopy/ELF/ELFObjcopy.cpp

Modified: llvm/trunk/tools/llvm-objcopy/CopyConfig.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objcopy/CopyConfig.cpp?rev=357199&r1=357198&r2=357199&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objcopy/CopyConfig.cpp (original)
+++ llvm/trunk/tools/llvm-objcopy/CopyConfig.cpp Thu Mar 28 11:27:00 2019
@@ -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 parseSectionRenameFla
       .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>
     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> parseRena
   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 FlagV
   // 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;

Modified: llvm/trunk/tools/llvm-objcopy/CopyConfig.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objcopy/CopyConfig.h?rev=357199&r1=357198&r2=357199&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objcopy/CopyConfig.h (original)
+++ llvm/trunk/tools/llvm-objcopy/CopyConfig.h Thu Mar 28 11:27:00 2019
@@ -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 {

Modified: llvm/trunk/tools/llvm-objcopy/ELF/ELFObjcopy.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objcopy/ELF/ELFObjcopy.cpp?rev=357199&r1=357198&r2=357199&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objcopy/ELF/ELFObjcopy.cpp (original)
+++ llvm/trunk/tools/llvm-objcopy/ELF/ELFObjcopy.cpp Thu Mar 28 11:27:00 2019
@@ -70,6 +70,21 @@ static bool onlyKeepDWOPred(const Object
   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
         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
       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;




More information about the llvm-commits mailing list