[PATCH] D56570: [llvm-objcopy] Use SHT_NOTE for added note sections.

Jordan Rupprecht via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 10 17:21:46 PST 2019


rupprecht created this revision.
rupprecht added reviewers: jhenderson, alexshap, jakehehrlich.
Herald added subscribers: llvm-commits, arichardson, emaste.
Herald added a reviewer: espindola.

Fix llvm-objcopy to add .note sections as SHT_NOTEs. GNU objcopy overrides section flags for special sections. For `.note` sections (with the exception of `.note.GNU-stack`), SHT_NOTE is used.

Many other sections are special cased by libbfd, but `.note` is the only special section I can seem to find being used with objcopy --add-section.

See `.note` in context of the full list of special sections here: https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;a=blob;f=bfd/elf.c;h=eb3e1828e9c651678b95a1dcbc3b124783d1d2be;hb=HEAD#l2675


Repository:
  rL LLVM

https://reviews.llvm.org/D56570

Files:
  test/tools/llvm-objcopy/ELF/add-section-special.test
  tools/llvm-objcopy/ELF/ELFObjcopy.cpp


Index: tools/llvm-objcopy/ELF/ELFObjcopy.cpp
===================================================================
--- tools/llvm-objcopy/ELF/ELFObjcopy.cpp
+++ tools/llvm-objcopy/ELF/ELFObjcopy.cpp
@@ -508,8 +508,10 @@
       auto Buf = std::move(*BufOrErr);
       auto BufPtr = reinterpret_cast<const uint8_t *>(Buf->getBufferStart());
       auto BufSize = Buf->getBufferSize();
-      Obj.addSection<OwnedDataSection>(SecName,
-                                       ArrayRef<uint8_t>(BufPtr, BufSize));
+      auto &NewSection = Obj.addSection<OwnedDataSection>(
+          SecName, ArrayRef<uint8_t>(BufPtr, BufSize));
+      if (SecName.startswith(".note") && SecName != ".note.GNU-stack")
+        NewSection.Type = SHT_NOTE;
     }
   }
 
Index: test/tools/llvm-objcopy/ELF/add-section-special.test
===================================================================
--- /dev/null
+++ test/tools/llvm-objcopy/ELF/add-section-special.test
@@ -0,0 +1,15 @@
+# RUN: yaml2obj %s > %t.o
+# RUN: llvm-objcopy --add-section=.note.foo=/dev/null %t.o %t-regular-note.o
+# RUN: llvm-objcopy --add-section=.note.GNU-stack=/dev/null %t.o %t-gnu-stack.o
+# RUN: llvm-readelf --sections %t-regular-note.o | FileCheck %s --check-prefix=NOTE
+# RUN: llvm-readelf --sections %t-gnu-stack.o | FileCheck %s --check-prefix=GNU-STACK
+
+!ELF
+FileHeader:
+  Class:           ELFCLASS64
+  Data:            ELFDATA2LSB
+  Type:            ET_REL
+  Machine:         EM_X86_64
+
+# NOTE:      .note.foo       NOTE
+# GNU-STACK: .note.GNU-stack PROGBITS


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D56570.181197.patch
Type: text/x-patch
Size: 1538 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190111/f8f6be0f/attachment.bin>


More information about the llvm-commits mailing list