From 0a5d4b84724395ae0522235db474e2dba4d9f6e4 Mon Sep 17 00:00:00 2001 From: George Rimar Date: Sun, 24 Mar 2019 13:31:08 +0000 Subject: [PATCH] [llvm-objcopy] - Report SHT_GROUP sections with invalid alignment. This patch fixes the reason of ubsan failure (UB detected) happened after landing the 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 void ELFSectionWriter::visit(const GroupSection &Sec) { ELF::Elf32_Word *Buf = reinterpret_cast(Out.getBufferStart() + Sec.Offset); ... But the test case for D59638 did not set AddressAlign in YAML. So address was not 4 bytes aligned since Sec.Offset was odd. That triggered the issue. This patch teaches llvm-objcopy to report an error for such sections (which should not met in reality), what is better than having UB. Differential revision: https://reviews.llvm.org/D59695 llvm-svn: 356853 --- .../llvm-objcopy/ELF/group-addr-misaligned.test | 38 ++++++++++++++++++++++ llvm/tools/llvm-objcopy/ELF/Object.cpp | 3 ++ 2 files changed, 41 insertions(+) create mode 100644 llvm/test/tools/llvm-objcopy/ELF/group-addr-misaligned.test diff --git a/llvm/test/tools/llvm-objcopy/ELF/group-addr-misaligned.test b/llvm/test/tools/llvm-objcopy/ELF/group-addr-misaligned.test new file mode 100644 index 0000000..43a6028 --- /dev/null +++ b/llvm/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 diff --git a/llvm/tools/llvm-objcopy/ELF/Object.cpp b/llvm/tools/llvm-objcopy/ELF/Object.cpp index ccc6efa..39bee85 100644 --- a/llvm/tools/llvm-objcopy/ELF/Object.cpp +++ b/llvm/tools/llvm-objcopy/ELF/Object.cpp @@ -949,6 +949,9 @@ template void ELFBuilder::readProgramHeaders() { template void ELFBuilder::initGroupSection(GroupSection *GroupSec) { + if (GroupSec->Align % sizeof(ELF::Elf32_Word) != 0) + error("Invalid alignment " + Twine(GroupSec->Align) + " of group section " + + GroupSec->Name); auto SecTable = Obj.sections(); auto SymTab = SecTable.template getSectionOfType( GroupSec->Link, -- 2.7.4