COFF: ARM: Support import functions.

llvm-svn: 243205
diff --git a/lld/COFF/Chunks.cpp b/lld/COFF/Chunks.cpp
index 6d13f99..0e14eff 100644
--- a/lld/COFF/Chunks.cpp
+++ b/lld/COFF/Chunks.cpp
@@ -313,6 +313,16 @@
   write32le(Buf + FileOff + 2, ImpSymbol->getRVA() + Config->ImageBase);
 }
 
+void ImportThunkChunkARM::getBaserels(std::vector<Baserel> *Res) {
+  Res->emplace_back(getRVA(), IMAGE_REL_BASED_ARM_MOV32T);
+}
+
+void ImportThunkChunkARM::writeTo(uint8_t *Buf) {
+  memcpy(Buf + FileOff, ImportThunkARM, sizeof(ImportThunkARM));
+  // Fix mov.w and mov.t operands.
+  applyMOV32T(Buf + FileOff, ImpSymbol->getRVA() + Config->ImageBase);
+}
+
 void LocalImportChunk::getBaserels(std::vector<Baserel> *Res) {
   Res->emplace_back(getRVA());
 }
diff --git a/lld/COFF/Chunks.h b/lld/COFF/Chunks.h
index dcb9bc0..c360be9 100644
--- a/lld/COFF/Chunks.h
+++ b/lld/COFF/Chunks.h
@@ -229,6 +229,12 @@
     0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // JMP *0x0
 };
 
+static const uint8_t ImportThunkARM[] = {
+    0x40, 0xf2, 0x00, 0x0c, // mov.w ip, #0
+    0xc0, 0xf2, 0x00, 0x0c, // mov.t ip, #0
+    0xdc, 0xf8, 0x00, 0xf0, // ldr.w pc, [ip]
+};
+
 // Windows-specific.
 // A chunk for DLL import jump table entry. In a final output, it's
 // contents will be a JMP instruction to some __imp_ symbol.
@@ -253,6 +259,17 @@
   Defined *ImpSymbol;
 };
 
+class ImportThunkChunkARM : public Chunk {
+public:
+  explicit ImportThunkChunkARM(Defined *S) : ImpSymbol(S) {}
+  size_t getSize() const override { return sizeof(ImportThunkARM); }
+  void getBaserels(std::vector<Baserel> *Res) override;
+  void writeTo(uint8_t *Buf) override;
+
+private:
+  Defined *ImpSymbol;
+};
+
 // Windows-specific.
 // See comments for DefinedLocalImport class.
 class LocalImportChunk : public Chunk {
diff --git a/lld/test/COFF/Inputs/library.lib b/lld/test/COFF/Inputs/library.lib
new file mode 100755
index 0000000..2f4207d
--- /dev/null
+++ b/lld/test/COFF/Inputs/library.lib
Binary files differ
diff --git a/lld/test/COFF/armnt-imports.test b/lld/test/COFF/armnt-imports.test
new file mode 100644
index 0000000..4432eea
--- /dev/null
+++ b/lld/test/COFF/armnt-imports.test
@@ -0,0 +1,51 @@
+# RUN: yaml2obj < %s > %t.obj
+# RUN: lld -flavor link /out:%t.exe /base:0x400000 /subsystem:console \
+# RUN:   %t.obj %p/Inputs/library.lib
+# RUN: llvm-readobj -coff-imports %t.exe | FileCheck %s
+
+# CHECK: Import {
+# CHECK:   Name: library.dll
+# CHECK:   ImportLookupTableRVA: 0x4000
+# CHECK:   ImportAddressTableRVA: 0x2000
+# CHECK:   Symbol: function (0)
+# CHECK: }
+
+---
+header:
+  Machine:         IMAGE_FILE_MACHINE_ARMNT
+  Characteristics: [  ]
+sections:
+  - Name:            .text
+    Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_PURGEABLE, IMAGE_SCN_MEM_16BIT, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     40F20000C0F2000000680047
+    Relocations:
+      - VirtualAddress:  0
+        SymbolName:      __imp_function
+        Type:            17
+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: 1
+      NumberOfLinenumbers: 0
+      CheckSum:        0
+      Number:          1
+  - Name:            mainCRTStartup
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_FUNCTION
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+  - Name:            __imp_function
+    Value:           0
+    SectionNumber:   0
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+...