[lld] r225104 - ReaderWriter: adjust ARM target addresses for exec

Saleem Abdulrasool compnerd at compnerd.org
Fri Jan 2 16:57:10 PST 2015


Author: compnerd
Date: Fri Jan  2 18:57:10 2015
New Revision: 225104

URL: http://llvm.org/viewvc/llvm-project?rev=225104&view=rev
Log:
ReaderWriter: adjust ARM target addresses for exec

ARM NT assumes a THUMB only environment.  As such, any address that is detected
as residing in an executable section is adjusted to have its bottom bit set to
indicate THUMB in case of a mode exchange.

Although the testing here seems insufficient (missing the negative cases) the
existing test cases for the IMAGE_REL_ARM_{ADDR32,MOV32T} are relevant as they
ensure that we do not incorrectly set the bit.

Added:
    lld/trunk/test/pecoff/Inputs/armnt-addr32-exec.obj.yaml
    lld/trunk/test/pecoff/Inputs/armnt-addr32-exec.s
    lld/trunk/test/pecoff/Inputs/armnt-mov32t-exec.obj.yaml
    lld/trunk/test/pecoff/Inputs/armnt-mov32t-exec.s
    lld/trunk/test/pecoff/armnt-addr32-exec.test
    lld/trunk/test/pecoff/armnt-mov32t-exec.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=225104&r1=225103&r2=225104&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp (original)
+++ lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp Fri Jan  2 18:57:10 2015
@@ -587,9 +587,14 @@ void AtomChunk::applyRelocationsARM(uint
       if (R->kindNamespace() != Reference::KindNamespace::COFF)
         continue;
 
+      bool AssumeTHUMBCode = false;
+      if (auto Target = cast_or_null<DefinedAtom>(R->target()))
+        AssumeTHUMBCode = Target->permissions() == DefinedAtom::permR_X ||
+                          Target->permissions() == DefinedAtom::permRWX;
+
       const auto AtomOffset = R->offsetInAtom();
       const auto FileOffset = Layout->_fileOffset;
-      const auto TargetAddr = AtomRVA[R->target()];
+      const auto TargetAddr = AtomRVA[R->target()] | (AssumeTHUMBCode ? 1 : 0);
       auto RelocSite16 =
           reinterpret_cast<ulittle16_t *>(Buffer + FileOffset + AtomOffset);
       auto RelocSite32 =
@@ -605,10 +610,12 @@ void AtomChunk::applyRelocationsARM(uint
         applyThumbMoveImmediate(&RelocSite16[2], (TargetAddr + ImageBase) >> 16);
         break;
       case llvm::COFF::IMAGE_REL_ARM_BRANCH24T:
+        // NOTE: the thumb bit will implicitly be truncated properly
         applyThumbBranchImmediate(RelocSite16,
                                   TargetAddr - AtomRVA[Atom] - AtomOffset - 4);
         break;
       case llvm::COFF::IMAGE_REL_ARM_BLX23T:
+        // NOTE: the thumb bit will implicitly be truncated properly
         applyThumbBranchImmediate(RelocSite16,
                                   TargetAddr - AtomRVA[Atom] - AtomOffset - 4);
         break;

Added: lld/trunk/test/pecoff/Inputs/armnt-addr32-exec.obj.yaml
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/pecoff/Inputs/armnt-addr32-exec.obj.yaml?rev=225104&view=auto
==============================================================================
--- lld/trunk/test/pecoff/Inputs/armnt-addr32-exec.obj.yaml (added)
+++ lld/trunk/test/pecoff/Inputs/armnt-addr32-exec.obj.yaml Fri Jan  2 18:57:10 2015
@@ -0,0 +1,55 @@
+---
+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:     '7047'
+  - Name:            .rdata
+    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     '00000000'
+    Relocations:
+      - VirtualAddress:  0
+        SymbolName:      function
+        Type:            1
+symbols:
+  - Name:            .text
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          2
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        0
+      Number:          1
+  - Name:            .rdata
+    Value:           0
+    SectionNumber:   2
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          4
+      NumberOfRelocations: 1
+      NumberOfLinenumbers: 0
+      CheckSum:        0
+      Number:          2
+  - Name:            function
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_FUNCTION
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+  - Name:            fps
+    Value:           0
+    SectionNumber:   2
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+...

Added: lld/trunk/test/pecoff/Inputs/armnt-addr32-exec.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/pecoff/Inputs/armnt-addr32-exec.s?rev=225104&view=auto
==============================================================================
--- lld/trunk/test/pecoff/Inputs/armnt-addr32-exec.s (added)
+++ lld/trunk/test/pecoff/Inputs/armnt-addr32-exec.s Fri Jan  2 18:57:10 2015
@@ -0,0 +1,24 @@
+
+# __declspec(dllexport) void function(void) { }
+# const void * const fps[] = { &function, };
+
+	.syntax unified
+	.thumb
+	.text
+
+	.def function
+		.scl 2
+		.type 32
+	.endef
+	.global function
+	.align 2
+	.thumb_func
+function:
+	bx lr
+
+	.section .rdata,"rd"
+	.global fps
+	.align 2
+fps:
+	.long function
+

Added: lld/trunk/test/pecoff/Inputs/armnt-mov32t-exec.obj.yaml
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/pecoff/Inputs/armnt-mov32t-exec.obj.yaml?rev=225104&view=auto
==============================================================================
--- lld/trunk/test/pecoff/Inputs/armnt-mov32t-exec.obj.yaml (added)
+++ lld/trunk/test/pecoff/Inputs/armnt-mov32t-exec.obj.yaml Fri Jan  2 18:57:10 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:     704700BF40F20000C0F200007047
+    Relocations:
+      - VirtualAddress:  4
+        SymbolName:      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:          14
+      NumberOfRelocations: 1
+      NumberOfLinenumbers: 0
+      CheckSum:        0
+      Number:          1
+  - Name:            function
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_FUNCTION
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+  - Name:            get_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-mov32t-exec.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/pecoff/Inputs/armnt-mov32t-exec.s?rev=225104&view=auto
==============================================================================
--- lld/trunk/test/pecoff/Inputs/armnt-mov32t-exec.s (added)
+++ lld/trunk/test/pecoff/Inputs/armnt-mov32t-exec.s Fri Jan  2 18:57:10 2015
@@ -0,0 +1,30 @@
+
+# void function(void) { }
+# void *get_function() { return &function; }
+
+	.syntax unified
+	.thumb
+	.text
+
+	.def function
+		.scl 2
+		.type 32
+	.endef
+	.global function
+	.align 2
+	.thumb_func
+function:
+	bx lr
+
+	.def get_function
+		.scl 2
+		.type 32
+	.endef
+	.global get_function
+	.align 2
+	.thumb_func
+get_function:
+	movw r0, :lower16:function
+	movt r0, :upper16:function
+	bx lr
+

Added: lld/trunk/test/pecoff/armnt-addr32-exec.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/pecoff/armnt-addr32-exec.test?rev=225104&view=auto
==============================================================================
--- lld/trunk/test/pecoff/armnt-addr32-exec.test (added)
+++ lld/trunk/test/pecoff/armnt-addr32-exec.test Fri Jan  2 18:57:10 2015
@@ -0,0 +1,11 @@
+# RUN: yaml2obj -format coff -o %t.obj %p/Inputs/armnt-addr32-exec.obj.yaml
+# RUN: llvm-objdump -s %t.obj | FileCheck %s -check-prefix BEFORE
+# RUN: lld -flavor link /out:%t.exe /entry:function /subsystem:console %t.obj
+# RUN: llvm-objdump -s %t.exe | FileCheck %s -check-prefix AFTER
+
+BEFORE: Contents of section .rdata:
+BEFORE:  0000 00000000
+
+AFTER: Contents of section .rdata:
+AFTER:  1000 01204000
+

Added: lld/trunk/test/pecoff/armnt-mov32t-exec.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/pecoff/armnt-mov32t-exec.test?rev=225104&view=auto
==============================================================================
--- lld/trunk/test/pecoff/armnt-mov32t-exec.test (added)
+++ lld/trunk/test/pecoff/armnt-mov32t-exec.test Fri Jan  2 18:57:10 2015
@@ -0,0 +1,19 @@
+# RUN: yaml2obj -format coff -o %t.obj %p/Inputs/armnt-mov32t-exec.obj.yaml
+# RUN: llvm-objdump -d %t.obj | FileCheck %s -check-prefix BEFORE
+# RUN: lld -flavor link /out:%t.exe /subsystem:console /entry:get_function %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: 40 f2 00 00   movw r0, #0
+BEFORE:        8: c0 f2 00 00   movt r0, #0
+BEFORE:        c: 70 47         bx lr
+
+AFTER: Disassembly of section .text:
+AFTER:     1000: 70 47         bx lr
+AFTER:     1002: 00 bf         nop
+AFTER:     1004: 41 f2 01 00   movw r0, #4097
+AFTER:     1008: c0 f2 40 00   movt r0, #64
+AFTER:     100c: 70 47         bx lr
+





More information about the llvm-commits mailing list