From 6371180cd4957875abb0293efc1f07d7392b82a7 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Fri, 12 Jan 2018 22:09:19 +0000 Subject: [PATCH] Allow unaligned access to ELF file data structures. The ELF specification says that all ELF data structures are aligned to their natural alignments both in memory and file. That means when we access mmap'ed ELF files, we could assume that all data structures are aligned properly. However, in reality, we assume that the data structures are aligned only to two bytes because .a files only guarantee that their member files are aligned to two bytes in archive files. So the data access is already unaligned. This patch relaxes the alignment requirement even more, so that we accept unaligned access to all ELF data structures. This patch in particular makes lld bug-compatible with icc. Intel C compiler doesn't seem to care about data alignment and generates unaligned relocation sections (https://bugs.llvm.org/show_bug.cgi?id=35854). I also saw another instance of compatibility issues with our internal tool which creates unaligned section headers. Because GNU linkers are not picky about alignment, looks like it is not uncommon that ELF-generating tools create unaligned files. There is a performance penalty with this patch on host machines on which unaligned access is expensive. x86 and AArch64 are fine. ARMv6 is a problem, but I don't think using ARMv6 machines as hosts is common, so I believe it's not a real problem. Differential Revision: https://reviews.llvm.org/D41978 llvm-svn: 322407 --- llvm/include/llvm/Object/ELFTypes.h | 2 +- .../invalid-sections-address-alignment.x86-64 | Bin 473 -> 0 bytes llvm/test/Object/invalid-alignment.test | 12 ++++++++++-- llvm/test/Object/invalid.test | 4 ---- 4 files changed, 11 insertions(+), 7 deletions(-) delete mode 100644 llvm/test/Object/Inputs/invalid-sections-address-alignment.x86-64 diff --git a/llvm/include/llvm/Object/ELFTypes.h b/llvm/include/llvm/Object/ELFTypes.h index 1bf0faf54e48..aa50a8b62fe9 100644 --- a/llvm/include/llvm/Object/ELFTypes.h +++ b/llvm/include/llvm/Object/ELFTypes.h @@ -44,7 +44,7 @@ template struct Elf_Chdr_Impl; template struct ELFType { private: template - using packed = support::detail::packed_endian_specific_integral; + using packed = support::detail::packed_endian_specific_integral; public: static const endianness TargetEndianness = E; diff --git a/llvm/test/Object/Inputs/invalid-sections-address-alignment.x86-64 b/llvm/test/Object/Inputs/invalid-sections-address-alignment.x86-64 deleted file mode 100644 index c0653d1d1b3e249110cef4ae9f212f6022f5f85c..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 473 zcmb<-^>JfjWMqH=Mg}_u1P?08%D^xarT|DgfY}ZVtZ_FikK&)4iT2aD~ zmY>g%mzoiuR+Ok$TvAk$n8cu0T$u}`K}--(0Fw+X5Q+>^6sU(8q)!4xvv>uRn~EaL z0QU+^7!(K~%mx#93}M*8_-Hh`OSysaK>Y9%L6XkOgwk)qvzZkQD*h OAblV^IH2M%?*afcSro7U diff --git a/llvm/test/Object/invalid-alignment.test b/llvm/test/Object/invalid-alignment.test index 872de8c28a15..f77a3afb3e70 100644 --- a/llvm/test/Object/invalid-alignment.test +++ b/llvm/test/Object/invalid-alignment.test @@ -1,7 +1,15 @@ # RUN: yaml2obj %s -o %t.o -# RUN: not llvm-readobj -r %t.o 2>&1 | FileCheck %s +# RUN: llvm-readobj -r %t.o 2>&1 | FileCheck %s -# CHECK: Error reading file: unaligned data +# CHECK: Format: ELF64-x86-64 +# CHECK-NEXT: Arch: x86_64 +# CHECK-NEXT: AddressSize: 64bit +# CHECK-NEXT: LoadName: +# CHECK-NEXT: Relocations [ +# CHECK-NEXT: Section (2) .rela.foo { +# CHECK-NEXT: 0x0 R_X86_64_NONE - 0x0 +# CHECK-NEXT: } +# CHECK-NEXT: ] --- !ELF FileHeader: diff --git a/llvm/test/Object/invalid.test b/llvm/test/Object/invalid.test index 6899f5ab0572..6f3ab2a17622 100644 --- a/llvm/test/Object/invalid.test +++ b/llvm/test/Object/invalid.test @@ -70,10 +70,6 @@ RUN: not llvm-readobj -r %p/Inputs/invalid-relocation-sec-sh_offset.elf-x86-64 2 RUN: FileCheck --check-prefix=INVALID-RELOC-SH-OFFSET %s INVALID-RELOC-SH-OFFSET: invalid section offset -RUN: not llvm-readobj -t %p/Inputs/invalid-sections-address-alignment.x86-64 2>&1 | \ -RUN: FileCheck --check-prefix=INVALID-SEC-ADDRESS-ALIGNMENT %s -INVALID-SEC-ADDRESS-ALIGNMENT: invalid alignment of section headers - RUN: not llvm-readobj -t %p/Inputs/invalid-section-size2.elf 2>&1 | \ RUN: FileCheck --check-prefix=INVALID-SECTION-SIZE2 %s INVALID-SECTION-SIZE2: invalid section offset