[llvm] r371591 - [llvm-objcopy] Simplify --prefix-alloc-sections

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 11 00:23:35 PDT 2019


Author: maskray
Date: Wed Sep 11 00:23:35 2019
New Revision: 371591

URL: http://llvm.org/viewvc/llvm-project?rev=371591&view=rev
Log:
[llvm-objcopy] Simplify --prefix-alloc-sections

Handle --prefix-alloc-sections after --rename-sections so that --prefix-alloc-sections code
does not have to check if renaming has been performed.

Reviewed By: jhenderson

Differential Revision: https://reviews.llvm.org/D66931

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

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=371591&r1=371590&r2=371591&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objcopy/ELF/ELFObjcopy.cpp (original)
+++ llvm/trunk/tools/llvm-objcopy/ELF/ELFObjcopy.cpp Wed Sep 11 00:23:35 2019
@@ -613,9 +613,8 @@ static Error handleArgs(const CopyConfig
   if (Error E = updateAndRemoveSymbols(Config, Obj))
     return E;
 
-  if (!Config.SectionsToRename.empty() || !Config.AllocSectionsPrefix.empty()) {
-    DenseSet<SectionBase *> PrefixedSections;
-    for (auto &Sec : Obj.sections()) {
+  if (!Config.SectionsToRename.empty()) {
+    for (SectionBase &Sec : Obj.sections()) {
       const auto Iter = Config.SectionsToRename.find(Sec.Name);
       if (Iter != Config.SectionsToRename.end()) {
         const SectionRename &SR = Iter->second;
@@ -623,58 +622,49 @@ static Error handleArgs(const CopyConfig
         if (SR.NewFlags.hasValue())
           setSectionFlagsAndType(Sec, SR.NewFlags.getValue());
       }
+    }
+  }
 
-      // Add a prefix to allocated sections and their relocation sections. This
-      // should be done after renaming the section by Config.SectionToRename to
-      // imitate the GNU objcopy behavior.
-      if (!Config.AllocSectionsPrefix.empty()) {
-        if (Sec.Flags & SHF_ALLOC) {
-          Sec.Name = (Config.AllocSectionsPrefix + Sec.Name).str();
-          PrefixedSections.insert(&Sec);
-
-          // Rename relocation sections associated to the allocated sections.
-          // For example, if we rename .text to .prefix.text, we also rename
-          // .rel.text to .rel.prefix.text.
-          //
-          // Dynamic relocation sections (SHT_REL[A] with SHF_ALLOC) are handled
-          // above, e.g., .rela.plt is renamed to .prefix.rela.plt, not
-          // .rela.prefix.plt since GNU objcopy does so.
-        } else if (auto *RelocSec = dyn_cast<RelocationSectionBase>(&Sec)) {
-          auto *TargetSec = RelocSec->getSection();
-          if (TargetSec && (TargetSec->Flags & SHF_ALLOC)) {
-            StringRef prefix;
-            switch (Sec.Type) {
-            case SHT_REL:
-              prefix = ".rel";
-              break;
-            case SHT_RELA:
-              prefix = ".rela";
-              break;
-            default:
-              continue;
-            }
-
-            // If the relocation section comes *after* the target section, we
-            // don't add Config.AllocSectionsPrefix because we've already added
-            // the prefix to TargetSec->Name. Otherwise, if the relocation
-            // section comes *before* the target section, we add the prefix.
-            if (PrefixedSections.count(TargetSec)) {
-              Sec.Name = (prefix + TargetSec->Name).str();
-            } else {
-              const auto Iter = Config.SectionsToRename.find(TargetSec->Name);
-              if (Iter != Config.SectionsToRename.end()) {
-                // Both `--rename-section` and `--prefix-alloc-sections` are
-                // given but the target section is not yet renamed.
-                Sec.Name =
-                    (prefix + Config.AllocSectionsPrefix + Iter->second.NewName)
-                        .str();
-              } else {
-                Sec.Name =
-                    (prefix + Config.AllocSectionsPrefix + TargetSec->Name)
-                        .str();
-              }
-            }
+  // Add a prefix to allocated sections and their relocation sections. This
+  // should be done after renaming the section by Config.SectionToRename to
+  // imitate the GNU objcopy behavior.
+  if (!Config.AllocSectionsPrefix.empty()) {
+    DenseSet<SectionBase *> PrefixedSections;
+    for (SectionBase &Sec : Obj.sections()) {
+      if (Sec.Flags & SHF_ALLOC) {
+        Sec.Name = (Config.AllocSectionsPrefix + Sec.Name).str();
+        PrefixedSections.insert(&Sec);
+      } else if (auto *RelocSec = dyn_cast<RelocationSectionBase>(&Sec)) {
+        // Rename relocation sections associated to the allocated sections.
+        // For example, if we rename .text to .prefix.text, we also rename
+        // .rel.text to .rel.prefix.text.
+        //
+        // Dynamic relocation sections (SHT_REL[A] with SHF_ALLOC) are handled
+        // above, e.g., .rela.plt is renamed to .prefix.rela.plt, not
+        // .rela.prefix.plt since GNU objcopy does so.
+        const SectionBase *TargetSec = RelocSec->getSection();
+        if (TargetSec && (TargetSec->Flags & SHF_ALLOC)) {
+          StringRef prefix;
+          switch (Sec.Type) {
+          case SHT_REL:
+            prefix = ".rel";
+            break;
+          case SHT_RELA:
+            prefix = ".rela";
+            break;
+          default:
+            llvm_unreachable("not a relocation section");
           }
+
+          // If the relocation section comes *after* the target section, we
+          // don't add Config.AllocSectionsPrefix because we've already added
+          // the prefix to TargetSec->Name. Otherwise, if the relocation
+          // section comes *before* the target section, we add the prefix.
+          if (PrefixedSections.count(TargetSec))
+            Sec.Name = (prefix + TargetSec->Name).str();
+          else
+            Sec.Name =
+                (prefix + Config.AllocSectionsPrefix + TargetSec->Name).str();
         }
       }
     }




More information about the llvm-commits mailing list