[PATCH] D41978: Allow unaligned access to ELF file data structures.

Rui Ueyama via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 11 20:28:10 PST 2018


ruiu created this revision.
ruiu added a reviewer: rafael.
Herald added subscribers: kristof.beyls, aemerson.

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.


https://reviews.llvm.org/D41978

Files:
  llvm/include/llvm/Object/ELFTypes.h
  llvm/test/Object/Inputs/invalid-sections-address-alignment.x86-64
  llvm/test/Object/invalid-alignment.test
  llvm/test/Object/invalid.test


Index: llvm/test/Object/invalid.test
===================================================================
--- llvm/test/Object/invalid.test
+++ llvm/test/Object/invalid.test
@@ -70,10 +70,6 @@
 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
Index: llvm/test/Object/invalid-alignment.test
===================================================================
--- llvm/test/Object/invalid-alignment.test
+++ 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:
Index: llvm/include/llvm/Object/ELFTypes.h
===================================================================
--- llvm/include/llvm/Object/ELFTypes.h
+++ llvm/include/llvm/Object/ELFTypes.h
@@ -44,7 +44,7 @@
 template <endianness E, bool Is64> struct ELFType {
 private:
   template <typename Ty>
-  using packed = support::detail::packed_endian_specific_integral<Ty, E, 2>;
+  using packed = support::detail::packed_endian_specific_integral<Ty, E, 1>;
 
 public:
   static const endianness TargetEndianness = E;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D41978.129576.patch
Type: text/x-patch
Size: 1911 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180112/d06083ec/attachment.bin>


More information about the llvm-commits mailing list