[PATCH] D59695: [llvm-objcopy] - Report SHT_GROUP sections with invalid alignment.

George Rimar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 22 07:00:04 PDT 2019


grimar created this revision.
grimar added reviewers: grimar, jakehehrlich, rupprecht, alexshap.
Herald added subscribers: MaskRay, arichardson, emaste.
Herald added a reviewer: espindola.
Herald added a reviewer: jhenderson.

This patch fixes the reason of ubsan failure happened after landing the D59638 <https://reviews.llvm.org/D59638> (I had to revert it).
(http://lab.llvm.org:8011/builders/sanitizer-x86_64-linux-bootstrap-ubsan/builds/11760/steps/check-llvm%20ubsan/logs/stdio)

Problem is the following. Our implementation of `GroupSection` assumes that
its address is 4 bytes aligned when writes it:

  template <class ELFT>
  void ELFSectionWriter<ELFT>::visit(const GroupSection &Sec) {
    ELF::Elf32_Word *Buf =
        reinterpret_cast<ELF::Elf32_Word *>(Out.getBufferStart() + Sec.Offset);
  ...

But the test case for D59638 <https://reviews.llvm.org/D59638> did not set `AddressAlign` in YAML. So address was
not aligned because `Sec.Offset` was odd. That triggered the issue.

Since such group sections should not be met in reality (I think), I would suggest to ban them.
This patch does that.


https://reviews.llvm.org/D59695

Files:
  test/tools/llvm-objcopy/ELF/group-addr-misaligned.test
  tools/llvm-objcopy/ELF/Object.cpp


Index: tools/llvm-objcopy/ELF/Object.cpp
===================================================================
--- tools/llvm-objcopy/ELF/Object.cpp
+++ tools/llvm-objcopy/ELF/Object.cpp
@@ -957,6 +957,9 @@
 template <class ELFT>
 void ELFBuilder<ELFT>::initGroupSection(GroupSection *GroupSec) {
   auto SecTable = Obj.sections();
+  if (GroupSec->Align % sizeof(ELF::Elf32_Word) != 0)
+    error("Invalid alignment " + Twine(GroupSec->Align) + " of group section " +
+          GroupSec->Name);
   auto SymTab = SecTable.template getSectionOfType<SymbolTableSection>(
       GroupSec->Link,
       "Link field value " + Twine(GroupSec->Link) + " in section " +
Index: test/tools/llvm-objcopy/ELF/group-addr-misaligned.test
===================================================================
--- /dev/null
+++ test/tools/llvm-objcopy/ELF/group-addr-misaligned.test
@@ -0,0 +1,38 @@
+# RUN: yaml2obj %s > %t
+# RUN: not llvm-objcopy %t %t2 2>&1 | FileCheck %s
+# CHECK: error: Invalid alignment 1 of group section .group.
+
+# In this test, we check that llvm-objcopy reports an error
+# for SHT_GROUP section with invalid alignment (not a multiple of 4). 
+
+--- !ELF
+FileHeader:
+  Class:           ELFCLASS64
+  Data:            ELFDATA2LSB
+  Type:            ET_REL
+  Machine:         EM_X86_64
+Sections:
+## It is not important for passing the test case to have this placeholder,
+## but having it would trigger ubsan failure when writing the group section
+## into a file if the error tested would not be reported by llvm-objcopy.
+  - Name:            .placeholder
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
+    AddressAlign:    0x0000000000000001
+    Content:         "00"
+  - Name:            .group
+    Type:            SHT_GROUP
+    Link:            .symtab
+    AddressAlign:    0x0000000000000001
+    Info:            foo
+    Members:
+      - SectionOrType:   GRP_COMDAT
+      - SectionOrType:   .text.foo
+  - Name:            .text.foo
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR, SHF_GROUP ]
+    AddressAlign:    0x0000000000000001
+Symbols:
+  Local:
+    - Name:            foo
+      Section:         .group


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D59695.191874.patch
Type: text/x-patch
Size: 2222 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190322/30a6fe0c/attachment.bin>


More information about the llvm-commits mailing list