[llvm] ee142c4 - [llvm-objcopy] Make --set-section-flags work with --add-section

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 4 09:39:20 PST 2020


Author: Fangrui Song
Date: 2020-11-04T09:39:14-08:00
New Revision: ee142c4988f491f3eef0f28739e6e99867912731

URL: https://github.com/llvm/llvm-project/commit/ee142c4988f491f3eef0f28739e6e99867912731
DIFF: https://github.com/llvm/llvm-project/commit/ee142c4988f491f3eef0f28739e6e99867912731.diff

LOG: [llvm-objcopy] Make --set-section-flags work with --add-section

This matches behavior GNU objcopy and can simplify clang-offload-bundler
(which currently works around the issue by invoking llvm-objcopy twice).

Reviewed By: jhenderson

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

Added: 
    llvm/test/tools/llvm-objcopy/ELF/add-section-and-set-flags.test

Modified: 
    llvm/test/tools/llvm-objcopy/ELF/add-symbol-new-symtab.test
    llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/test/tools/llvm-objcopy/ELF/add-section-and-set-flags.test b/llvm/test/tools/llvm-objcopy/ELF/add-section-and-set-flags.test
new file mode 100644
index 000000000000..0beb1a7b9a60
--- /dev/null
+++ b/llvm/test/tools/llvm-objcopy/ELF/add-section-and-set-flags.test
@@ -0,0 +1,20 @@
+## Check --set-section-flags works with sections added by --add-section.
+
+# RUN: yaml2obj %s -o %t
+# RUN: llvm-objcopy --add-section=foo=/dev/null --set-section-flags=foo=alloc,exclude %t %t.out
+# RUN: llvm-readobj -S %t.out | FileCheck %s
+
+# CHECK:      Name: foo
+# CHECK-NEXT: Type: SHT_PROGBITS
+# CHECK-NEXT: Flags [
+# CHECK-NEXT:   SHF_ALLOC
+# CHECK-NEXT:   SHF_EXCLUDE
+# CHECK-NEXT:   SHF_WRITE
+# CHECK-NEXT: ]
+
+--- !ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_EXEC
+  Machine: EM_X86_64

diff  --git a/llvm/test/tools/llvm-objcopy/ELF/add-symbol-new-symtab.test b/llvm/test/tools/llvm-objcopy/ELF/add-symbol-new-symtab.test
index f14625de4f58..ec97e37fec21 100644
--- a/llvm/test/tools/llvm-objcopy/ELF/add-symbol-new-symtab.test
+++ b/llvm/test/tools/llvm-objcopy/ELF/add-symbol-new-symtab.test
@@ -18,6 +18,12 @@
 # CHECK-NEXT:     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT   UND
 # CHECK-NEXT:     1: 00000000000004d2     0 NOTYPE  GLOBAL DEFAULT   ABS newsym
 
+## --set-section-flags works for newly created .symtab, which GNU objcopy does not do.
+# RUN: llvm-objcopy -R .symtab --add-symbol newsym=1234 --set-section-flags .symtab=readonly,exclude %t %t3
+# RUN: llvm-readelf -S %t3 | FileCheck %s --check-prefix=CHECK2
+
+# CHECK2:     [ 4] .symtab   SYMTAB   0000000000000000 {{.+}} 000030 18  E   3  1   8
+
 --- !ELF
 FileHeader:
   Class:   ELFCLASS64

diff  --git a/llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp b/llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp
index f0bb594b499c..16984ad266e3 100644
--- a/llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp
+++ b/llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp
@@ -719,16 +719,6 @@ static Error handleArgs(const CopyConfig &Config, Object &Obj,
     }
   }
 
-  if (!Config.SetSectionFlags.empty()) {
-    for (auto &Sec : Obj.sections()) {
-      const auto Iter = Config.SetSectionFlags.find(Sec.Name);
-      if (Iter != Config.SetSectionFlags.end()) {
-        const SectionFlagsUpdate &SFU = Iter->second;
-        setSectionFlagsAndType(Sec, SFU.NewFlags);
-      }
-    }
-  }
-
   if (Config.OnlyKeepDebug)
     for (auto &Sec : Obj.sections())
       if (Sec.Flags & SHF_ALLOC && Sec.Type != SHT_NOTE)
@@ -770,6 +760,17 @@ static Error handleArgs(const CopyConfig &Config, Object &Obj,
         Sec ? (uint16_t)SYMBOL_SIMPLE_INDEX : (uint16_t)SHN_ABS, 0);
   }
 
+  // --set-section-flags works with sections added by --add-section.
+  if (!Config.SetSectionFlags.empty()) {
+    for (auto &Sec : Obj.sections()) {
+      const auto Iter = Config.SetSectionFlags.find(Sec.Name);
+      if (Iter != Config.SetSectionFlags.end()) {
+        const SectionFlagsUpdate &SFU = Iter->second;
+        setSectionFlagsAndType(Sec, SFU.NewFlags);
+      }
+    }
+  }
+
   if (Config.EntryExpr)
     Obj.Entry = Config.EntryExpr(Obj.Entry);
   return Error::success();


        


More information about the llvm-commits mailing list