[lld] r308585 - [COFF] Align import address chunks to the pointer size

Martin Storsjo via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 19 22:49:54 PDT 2017


Author: mstorsjo
Date: Wed Jul 19 22:49:54 2017
New Revision: 308585

URL: http://llvm.org/viewvc/llvm-project?rev=308585&view=rev
Log:
[COFF] Align import address chunks to the pointer size

This fixes cases on ARM64 when importing from more than one DLL,
in case the imports from the first DLL ended up unaligned.

When fixing up a IMAGE_REL_ARM64_PAGEOFFSET_12L, which shifts the
offset by the load/store size, check that the shift doesn't discard
any bits. (This would also detect if the import address chunks were
unaligned.)

Differential revision: https://reviews.llvm.org/D35640

Added:
    lld/trunk/test/COFF/Inputs/library2-arm64.lib
    lld/trunk/test/COFF/Inputs/library2.def
    lld/trunk/test/COFF/arm64-import2.test
Modified:
    lld/trunk/COFF/Chunks.cpp
    lld/trunk/COFF/DLL.cpp

Modified: lld/trunk/COFF/Chunks.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Chunks.cpp?rev=308585&r1=308584&r2=308585&view=diff
==============================================================================
--- lld/trunk/COFF/Chunks.cpp (original)
+++ lld/trunk/COFF/Chunks.cpp Wed Jul 19 22:49:54 2017
@@ -184,6 +184,8 @@ static void applyArm64Imm(uint8_t *Off,
 
 static void applyArm64Ldr(uint8_t *Off, uint64_t Imm) {
   int Size = read32le(Off) >> 30;
+  if ((Imm & ((1 << Size) - 1)) != 0)
+    fatal("misaligned ldr/str offset");
   Imm >>= Size;
   applyArm64Imm(Off, Imm);
 }

Modified: lld/trunk/COFF/DLL.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/DLL.cpp?rev=308585&r1=308584&r2=308585&view=diff
==============================================================================
--- lld/trunk/COFF/DLL.cpp (original)
+++ lld/trunk/COFF/DLL.cpp Wed Jul 19 22:49:54 2017
@@ -61,7 +61,7 @@ private:
 // A chunk for the import descriptor table.
 class LookupChunk : public Chunk {
 public:
-  explicit LookupChunk(Chunk *C) : HintName(C) {}
+  explicit LookupChunk(Chunk *C) : HintName(C) { Align = ptrSize(); }
   size_t getSize() const override { return ptrSize(); }
 
   void writeTo(uint8_t *Buf) const override {
@@ -76,7 +76,7 @@ public:
 // See Microsoft PE/COFF spec 7.1. Import Header for details.
 class OrdinalOnlyChunk : public Chunk {
 public:
-  explicit OrdinalOnlyChunk(uint16_t V) : Ordinal(V) {}
+  explicit OrdinalOnlyChunk(uint16_t V) : Ordinal(V) { Align = ptrSize(); }
   size_t getSize() const override { return ptrSize(); }
 
   void writeTo(uint8_t *Buf) const override {
@@ -262,7 +262,7 @@ public:
 // A chunk for the import descriptor table.
 class DelayAddressChunk : public Chunk {
 public:
-  explicit DelayAddressChunk(Chunk *C) : Thunk(C) {}
+  explicit DelayAddressChunk(Chunk *C) : Thunk(C) { Align = ptrSize(); }
   size_t getSize() const override { return ptrSize(); }
 
   void writeTo(uint8_t *Buf) const override {

Added: lld/trunk/test/COFF/Inputs/library2-arm64.lib
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/COFF/Inputs/library2-arm64.lib?rev=308585&view=auto
==============================================================================
Binary files lld/trunk/test/COFF/Inputs/library2-arm64.lib (added) and lld/trunk/test/COFF/Inputs/library2-arm64.lib Wed Jul 19 22:49:54 2017 differ

Added: lld/trunk/test/COFF/Inputs/library2.def
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/COFF/Inputs/library2.def?rev=308585&view=auto
==============================================================================
--- lld/trunk/test/COFF/Inputs/library2.def (added)
+++ lld/trunk/test/COFF/Inputs/library2.def Wed Jul 19 22:49:54 2017
@@ -0,0 +1,3 @@
+LIBRARY library2
+EXPORTS
+  function2

Added: lld/trunk/test/COFF/arm64-import2.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/COFF/arm64-import2.test?rev=308585&view=auto
==============================================================================
--- lld/trunk/test/COFF/arm64-import2.test (added)
+++ lld/trunk/test/COFF/arm64-import2.test Wed Jul 19 22:49:54 2017
@@ -0,0 +1,85 @@
+# REQUIRES: aarch64
+
+# RUN: yaml2obj < %s > %t.obj
+# RUN: llvm-objdump -d %t.obj | FileCheck %s -check-prefix BEFORE
+# RUN: lld-link /entry:main /subsystem:console /out:%t.exe %t.obj %p/Inputs/library-arm64.lib %p/Inputs/library2-arm64.lib
+# RUN: llvm-objdump -d %t.exe | FileCheck %s -check-prefix AFTER
+# RUN: llvm-readobj -coff-imports %t.exe | FileCheck %s -check-prefix IMPORTS
+
+# BEFORE: Disassembly of section .text:
+# BEFORE:        0:       00 00 00 94     bl      #0
+# BEFORE:        4:       00 00 00 94     bl      #0
+# BEFORE:        8:       c0 03 5f d6     ret
+
+# AFTER: Disassembly of section .text:
+# AFTER:  140001000:      03 00 00 94     bl      #12
+# AFTER:  140001004:      05 00 00 94     bl      #20
+# AFTER:  140001008:      c0 03 5f d6     ret
+# AFTER:  14000100c:      10 00 00 b0     adrp    x16, #4096
+# AFTER:  140001010:      10 32 40 f9     ldr     x16, [x16, #96]
+# AFTER:  140001014:      00 02 1f d6     br      x16
+# AFTER:  140001018:      10 00 00 b0     adrp    x16, #4096
+# AFTER:  14000101c:      10 3a 40 f9     ldr     x16, [x16, #112]
+# AFTER:  140001020:      00 02 1f d6     br      x16
+
+# IMPORTS: Import {
+# IMPORTS:   Name: library.dll
+# IMPORTS:   ImportLookupTableRVA: 0x2040
+# IMPORTS:   ImportAddressTableRVA: 0x2060
+# IMPORTS:   Symbol: function (2)
+# IMPORTS: }
+# IMPORTS: Import {
+# IMPORTS:   Name: library2.dll
+# IMPORTS:   ImportLookupTableRVA: 0x2050
+# IMPORTS:   ImportAddressTableRVA: 0x2070
+# IMPORTS:   Symbol: function2 (0)
+# IMPORTS: }
+
+--- !COFF
+header:
+  Machine:         IMAGE_FILE_MACHINE_ARM64
+  Characteristics: [  ]
+sections:
+  - Name:            .text
+    Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     0000009400000094C0035FD6
+    Relocations:
+      - VirtualAddress:  0
+        SymbolName:      function
+        Type:            3
+      - VirtualAddress:  4
+        SymbolName:      function2
+        Type:            3
+symbols:
+  - Name:            .text
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          12
+      NumberOfRelocations: 2
+      NumberOfLinenumbers: 0
+      CheckSum:        1438860354
+      Number:          1
+  - Name:            main
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+  - Name:            function
+    Value:           0
+    SectionNumber:   0
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+  - Name:            function2
+    Value:           0
+    SectionNumber:   0
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+...




More information about the llvm-commits mailing list