[lld] r225081 - ReaderWriter: teach the writer about IMAGE_REL_ARM_BLX23T

Saleem Abdulrasool compnerd at compnerd.org
Fri Jan 2 10:51:39 PST 2015


Author: compnerd
Date: Fri Jan  2 12:51:36 2015
New Revision: 225081

URL: http://llvm.org/viewvc/llvm-project?rev=225081&view=rev
Log:
ReaderWriter: teach the writer about IMAGE_REL_ARM_BLX23T

This adds support for IMAGE_REL_ARM_BLX23T relocations.  Similar to the
IMAGE_REL_ARM_MOV32T relocation, this relocation requires munging an
instruction.  This inches us closer to supporting a basic hello world
application.

Added:
    lld/trunk/test/pecoff/Inputs/armnt-blx23t.obj.yaml
    lld/trunk/test/pecoff/Inputs/armnt-blx23t.s
    lld/trunk/test/pecoff/armnt-blx23t.test
Modified:
    lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp

Modified: lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp?rev=225081&r1=225080&r2=225081&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp (original)
+++ lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp Fri Jan  2 12:51:36 2015
@@ -558,6 +558,22 @@ static void applyThumbMoveImmediate(ulit
       mov[1] | (((imm & 0x0700) >> 8) << 12) | (((imm & 0x00ff) >> 0) << 0);
 }
 
+static void applyThumbBranchImmediate(ulittle16_t *bl, int32_t imm) {
+  // BL(T1):  |11110|S|imm10|11|J1|1|J2|imm11|
+  //          imm32 = sext S:I1:I2:imm10:imm11:'0'
+  //
+  //          I1 = ~(J1 ^ S), I2 = ~(J2 ^ S)
+
+  assert((~abs(imm) & (-1 << 24)) && "bl out of range");
+
+  uint32_t S = (imm < 0 ? 1 : 0);
+  uint32_t J1 = ((~imm & 0x00800000) >> 23) ^ S;
+  uint32_t J2 = ((~imm & 0x00400000) >> 22) ^ S;
+
+  bl[0] = bl[0] | (((imm & 0x003ff000) >> 12) << 0) | (S << 10);
+  bl[1] = bl[1] | (((imm & 0x00000ffe) >>  1) << 0) | (J2 << 11) | (J1 << 13);
+}
+
 void AtomChunk::applyRelocationsARM(uint8_t *Buffer,
                                     std::map<const Atom *, uint64_t> &AtomRVA,
                                     std::vector<uint64_t> &SectionRVA,
@@ -586,6 +602,10 @@ void AtomChunk::applyRelocationsARM(uint
         applyThumbMoveImmediate(&RelocSite16[0], (TargetAddr + ImageBase) >>  0);
         applyThumbMoveImmediate(&RelocSite16[2], (TargetAddr + ImageBase) >> 16);
         break;
+      case llvm::COFF::IMAGE_REL_ARM_BLX23T:
+        applyThumbBranchImmediate(RelocSite16,
+                                  TargetAddr - AtomRVA[Atom] - AtomOffset - 4);
+        break;
       }
     }
   }

Added: lld/trunk/test/pecoff/Inputs/armnt-blx23t.obj.yaml
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/pecoff/Inputs/armnt-blx23t.obj.yaml?rev=225081&view=auto
==============================================================================
--- lld/trunk/test/pecoff/Inputs/armnt-blx23t.obj.yaml (added)
+++ lld/trunk/test/pecoff/Inputs/armnt-blx23t.obj.yaml Fri Jan  2 12:51:36 2015
@@ -0,0 +1,39 @@
+---
+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:     704700BF2DE90048EB46202000F000F80130BDE80088
+    Relocations:
+      - VirtualAddress:  12
+        SymbolName:      identity
+        Type:            21
+symbols:
+  - Name:            .text
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          22
+      NumberOfRelocations: 1
+      NumberOfLinenumbers: 0
+      CheckSum:        0
+      Number:          1
+  - Name:            identity
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_FUNCTION
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+  - Name:            function
+    Value:           4
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_FUNCTION
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+...

Added: lld/trunk/test/pecoff/Inputs/armnt-blx23t.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/pecoff/Inputs/armnt-blx23t.s?rev=225081&view=auto
==============================================================================
--- lld/trunk/test/pecoff/Inputs/armnt-blx23t.s (added)
+++ lld/trunk/test/pecoff/Inputs/armnt-blx23t.s Fri Jan  2 12:51:36 2015
@@ -0,0 +1,35 @@
+
+@ __declspec(noinline) int identity(int i) { return i; }
+@ int function() { return identity(32) + 1; }
+
+	.syntax unified
+	.thumb
+	.text
+
+	.def identity
+		.scl 2
+		.type 32
+	.endef
+	.global identity
+	.align 2
+	.code16
+	.thumb_func
+identity:
+	bx lr
+
+	.def function
+		.scl 2
+		.type 32
+	.endef
+	.global function
+	.align 2
+	.code16
+	.thumb_func
+function:
+	push.w {r11, lr}
+	mov r11, sp
+	movs r0, #32
+	bl identity
+	adds r0, #1
+	pop.w {r11, pc}
+

Added: lld/trunk/test/pecoff/armnt-blx23t.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/pecoff/armnt-blx23t.test?rev=225081&view=auto
==============================================================================
--- lld/trunk/test/pecoff/armnt-blx23t.test (added)
+++ lld/trunk/test/pecoff/armnt-blx23t.test Fri Jan  2 12:51:36 2015
@@ -0,0 +1,25 @@
+# RUN: yaml2obj -format coff -o %t.obj %p/Inputs/armnt-blx23t.obj.yaml
+# RUN: llvm-objdump -d %t.obj | FileCheck %s -check-prefix BEFORE
+# RUN: lld -flavor link /entry:function /subsystem:console /out:%t.exe %t.obj
+# RUN: llvm-objdump -d %t.exe | FileCheck %s -check-prefix AFTER
+
+BEFORE: Disassembly of section .text:
+BEFORE:        0: 70 47         bx lr
+BEFORE:        2: 00 bf         nop
+BEFORE:        4: 2d e9 00 48   push.w {r11, lr}
+BEFORE:        8: eb 46         mov r11, sp
+BEFORE:        a: 20 20         movs r0, #32
+BEFORE:        c: 00 f0 00 f8   bl #0
+BEFORE:       10: 01 30         adds r0, #1
+BEFORE:       12: bd e8 00 88   pop.w {r11, pc}
+
+AFTER: Disassembly of section .text:
+AFTER:     1000: 70 47         bx lr
+AFTER:     1002: 00 bf         nop
+AFTER:     1004: 2d e9 00 48   push.w {r11, lr}
+AFTER:     1008: eb 46         mov r11, sp
+AFTER:     100a: 20 20         movs r0, #32
+AFTER:     100c: ff f7 f8 ff   bl #-16
+AFTER:     1010: 01 30         adds r0, #1
+AFTER:     1012: bd e8 00 88   pop.w {r11, pc}
+





More information about the llvm-commits mailing list