[lld] r216772 - [PECOFF] Fix AMD64_REL_[1-5] and AMD64_SECTION relocations

Rui Ueyama ruiu at google.com
Fri Aug 29 13:33:28 PDT 2014


Author: ruiu
Date: Fri Aug 29 15:33:27 2014
New Revision: 216772

URL: http://llvm.org/viewvc/llvm-project?rev=216772&view=rev
Log:
[PECOFF] Fix AMD64_REL_[1-5] and AMD64_SECTION relocations

I hope this is the last fix for x64 relocations as I've wasted
a few days on this.

This caused a mysterious issue that some C++ programs crash on
startup. It was because a null pointer is passed as argv to main.
__tmainCRTStartup calls main, but before that it calls all
initialization routines between .text$xc_a and .text$xc_z.
pre_cpp_init is one of such routines, and it is the one who
initializes a heap pointer for argv for later use. That routine
was not called for some reason.

It turned out that __tmainCRTStartup was skipping a block of
code because of the relocation bug. A condition in the function
depends on a memory load, and that memory load was referring
a wrong location. As a result a jump instruction took the
wrong branch, skipping pre_cpp_init and so on.

This patch fixes the issue. Also added more tests to fix them
once and for all.

Added:
    lld/trunk/test/pecoff/Inputs/reloc64.obj.yaml
Removed:
    lld/trunk/test/pecoff/Inputs/reloc64-1.obj.yaml
    lld/trunk/test/pecoff/Inputs/reloc64-2.obj.yaml
Modified:
    lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp
    lld/trunk/test/pecoff/reloc64.test

Modified: lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp?rev=216772&r1=216771&r2=216772&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp (original)
+++ lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp Fri Aug 29 15:33:27 2014
@@ -587,26 +587,26 @@ void AtomChunk::applyRelocations64(uint8
         break;
       case llvm::COFF::IMAGE_REL_AMD64_REL32_1:
         *relocSite32 =
-            *relocSite32 + targetAddr - atomRva[atom] - ref->offsetInAtom() - 3;
+            *relocSite32 + targetAddr - atomRva[atom] - ref->offsetInAtom() - 5;
         break;
       case llvm::COFF::IMAGE_REL_AMD64_REL32_2:
         *relocSite32 =
-            *relocSite32 + targetAddr - atomRva[atom] - ref->offsetInAtom() - 2;
+            *relocSite32 + targetAddr - atomRva[atom] - ref->offsetInAtom() - 6;
         break;
       case llvm::COFF::IMAGE_REL_AMD64_REL32_3:
         *relocSite32 =
-            *relocSite32 + targetAddr - atomRva[atom] - ref->offsetInAtom() - 1;
+            *relocSite32 + targetAddr - atomRva[atom] - ref->offsetInAtom() - 7;
         break;
       case llvm::COFF::IMAGE_REL_AMD64_REL32_4:
         *relocSite32 =
-            *relocSite32 + targetAddr - atomRva[atom] - ref->offsetInAtom();
+            *relocSite32 + targetAddr - atomRva[atom] - ref->offsetInAtom() - 8;
         break;
       case llvm::COFF::IMAGE_REL_AMD64_REL32_5:
         *relocSite32 =
-            *relocSite32 + targetAddr - atomRva[atom] - ref->offsetInAtom() + 1;
+            *relocSite32 + targetAddr - atomRva[atom] - ref->offsetInAtom() - 9;
         break;
       case llvm::COFF::IMAGE_REL_AMD64_SECTION:
-        *relocSite16 = *relocSite16 + getSectionIndex(targetAddr, sectionRva);
+        *relocSite16 = *relocSite16 + getSectionIndex(targetAddr, sectionRva) - 1;
         break;
       case llvm::COFF::IMAGE_REL_AMD64_SECREL:
         *relocSite32 = *relocSite32 + targetAddr -

Removed: lld/trunk/test/pecoff/Inputs/reloc64-1.obj.yaml
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/pecoff/Inputs/reloc64-1.obj.yaml?rev=216771&view=auto
==============================================================================
--- lld/trunk/test/pecoff/Inputs/reloc64-1.obj.yaml (original)
+++ lld/trunk/test/pecoff/Inputs/reloc64-1.obj.yaml (removed)
@@ -1,86 +0,0 @@
----
-header:
-  Machine:         IMAGE_FILE_MACHINE_AMD64
-  Characteristics: [  ]
-sections:
-  - Name:            .text
-    Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
-    Alignment:       16
-    SectionData:     E80000000048b80000000000000000
-    Relocations:
-      - VirtualAddress:  1
-        SymbolName:      fn2
-        Type:            IMAGE_REL_AMD64_REL32
-      - VirtualAddress:  7
-        SymbolName:      fn2
-        Type:            IMAGE_REL_AMD64_ADDR64
-  - Name:            .data
-    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE ]
-    Alignment:       16
-    SectionData:     ''
-  - Name:            '.debug$S'
-    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_DISCARDABLE, IMAGE_SCN_MEM_READ ]
-    Alignment:       1
-    SectionData:     04000000F10000005D0000002200011100000000433A5C63796777696E5C686F6D655C727569755C666E312E6F626A0037003C1103020000D00000000000000000000B00000027C601004D6963726F736F667420285229204D6163726F20417373656D626C65720000000000
-symbols:
-  - Name:            '@comp.id'
-    Value:           13485607
-    SectionNumber:   65535
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_STATIC
-  - Name:            '@feat.00'
-    Value:           16
-    SectionNumber:   65535
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_STATIC
-  - Name:            .text
-    Value:           0
-    SectionNumber:   1
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_STATIC
-    SectionDefinition:
-      Length:          6
-      NumberOfRelocations: 1
-      NumberOfLinenumbers: 0
-      CheckSum:        0
-      Number:          0
-  - Name:            .data
-    Value:           0
-    SectionNumber:   2
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_STATIC
-    SectionDefinition:
-      Length:          0
-      NumberOfRelocations: 0
-      NumberOfLinenumbers: 0
-      CheckSum:        0
-      Number:          0
-  - Name:            '.debug$S'
-    Value:           0
-    SectionNumber:   3
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_STATIC
-    SectionDefinition:
-      Length:          108
-      NumberOfRelocations: 0
-      NumberOfLinenumbers: 0
-      CheckSum:        0
-      Number:          0
-  - Name:            fn2
-    Value:           0
-    SectionNumber:   0
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
-  - Name:            fn1
-    Value:           0
-    SectionNumber:   1
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_FUNCTION
-    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
-...

Removed: lld/trunk/test/pecoff/Inputs/reloc64-2.obj.yaml
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/pecoff/Inputs/reloc64-2.obj.yaml?rev=216771&view=auto
==============================================================================
--- lld/trunk/test/pecoff/Inputs/reloc64-2.obj.yaml (original)
+++ lld/trunk/test/pecoff/Inputs/reloc64-2.obj.yaml (removed)
@@ -1,73 +0,0 @@
----
-header:
-  Machine:         IMAGE_FILE_MACHINE_AMD64
-  Characteristics: [  ]
-sections:
-  - Name:            .text
-    Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
-    Alignment:       16
-    SectionData:     C3
-  - Name:            .data
-    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE ]
-    Alignment:       16
-    SectionData:     ''
-  - Name:            '.debug$S'
-    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_DISCARDABLE, IMAGE_SCN_MEM_READ ]
-    Alignment:       1
-    SectionData:     04000000F10000005D0000002200011100000000433A5C63796777696E5C686F6D655C727569755C666E322E6F626A0037003C1103020000D00000000000000000000B00000027C601004D6963726F736F667420285229204D6163726F20417373656D626C65720000000000
-symbols:
-  - Name:            '@comp.id'
-    Value:           13485607
-    SectionNumber:   65535
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_STATIC
-  - Name:            '@feat.00'
-    Value:           16
-    SectionNumber:   65535
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_STATIC
-  - Name:            .text
-    Value:           0
-    SectionNumber:   1
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_STATIC
-    SectionDefinition:
-      Length:          1
-      NumberOfRelocations: 0
-      NumberOfLinenumbers: 0
-      CheckSum:        0
-      Number:          0
-  - Name:            .data
-    Value:           0
-    SectionNumber:   2
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_STATIC
-    SectionDefinition:
-      Length:          0
-      NumberOfRelocations: 0
-      NumberOfLinenumbers: 0
-      CheckSum:        0
-      Number:          0
-  - Name:            '.debug$S'
-    Value:           0
-    SectionNumber:   3
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_STATIC
-    SectionDefinition:
-      Length:          108
-      NumberOfRelocations: 0
-      NumberOfLinenumbers: 0
-      CheckSum:        0
-      Number:          0
-  - Name:            fn2
-    Value:           0
-    SectionNumber:   1
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_FUNCTION
-    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
-...

Added: lld/trunk/test/pecoff/Inputs/reloc64.obj.yaml
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/pecoff/Inputs/reloc64.obj.yaml?rev=216772&view=auto
==============================================================================
--- lld/trunk/test/pecoff/Inputs/reloc64.obj.yaml (added)
+++ lld/trunk/test/pecoff/Inputs/reloc64.obj.yaml Fri Aug 29 15:33:27 2014
@@ -0,0 +1,63 @@
+---
+header:
+  Machine:         IMAGE_FILE_MACHINE_AMD64
+  Characteristics: []
+sections:
+  - Name:            .text
+    Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
+    Alignment:       16
+    SectionData:     48B800000000000000ffE8000000ffE8000000ffE8000000ffE8000000ffE8000000ffE8000000ffE8000000ffE8000000ffC3
+    Relocations:
+      - VirtualAddress:  2
+        SymbolName:      end
+        Type:            IMAGE_REL_AMD64_ADDR64
+      - VirtualAddress:  11
+        SymbolName:      end
+        Type:            IMAGE_REL_AMD64_REL32
+      - VirtualAddress:  16
+        SymbolName:      end
+        Type:            IMAGE_REL_AMD64_REL32_1
+      - VirtualAddress:  21
+        SymbolName:      end
+        Type:            IMAGE_REL_AMD64_REL32_2
+      - VirtualAddress:  26
+        SymbolName:      end
+        Type:            IMAGE_REL_AMD64_REL32_3
+      - VirtualAddress:  31
+        SymbolName:      end
+        Type:            IMAGE_REL_AMD64_REL32_4
+      - VirtualAddress:  36
+        SymbolName:      end
+        Type:            IMAGE_REL_AMD64_REL32_5
+      - VirtualAddress:  41
+        SymbolName:      end
+        Type:            IMAGE_REL_AMD64_SECTION
+      - VirtualAddress:  46
+        SymbolName:      end
+        Type:            IMAGE_REL_AMD64_SECREL
+symbols:
+  - Name:            .text
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          41
+      NumberOfRelocations: 7
+      NumberOfLinenumbers: 0
+      CheckSum:        0
+      Number:          0
+  - Name:            entry
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+  - Name:            end
+    Value:           40
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+...

Modified: lld/trunk/test/pecoff/reloc64.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/pecoff/reloc64.test?rev=216772&r1=216771&r2=216772&view=diff
==============================================================================
--- lld/trunk/test/pecoff/reloc64.test (original)
+++ lld/trunk/test/pecoff/reloc64.test Fri Aug 29 15:33:27 2014
@@ -1,15 +1,18 @@
-# RUN: yaml2obj %p/Inputs/reloc64-1.obj.yaml > %t1.obj
-# RUN: yaml2obj %p/Inputs/reloc64-2.obj.yaml > %t2.obj
+# RUN: yaml2obj %p/Inputs/reloc64.obj.yaml > %t.obj
 
 # RUN: lld -flavor link /out:%t.exe /subsystem:console /machine:x64 \
-# RUN:   /entry:fn1 -- %t1.obj %t2.obj
-# RUN: llvm-objdump -d %t.exe | FileCheck -check-prefix=TEXT %s
-# RUN: llvm-objdump -s %t.exe | FileCheck -check-prefix=RELOC %s
+# RUN:   /entry:entry -- %t.obj
+# RUN: llvm-objdump -d %t.exe | FileCheck %s
 
-TEXT:      Disassembly of section .text:
-TEXT-NEXT: .text:
-TEXT-NEXT:   1000:  e8 0b 00 00 00   callq 11
-TEXT-NEXT:   1005:  48 b8 10 10 00 40 01 00 00 00 movabsq $5368713232, %rax
-
-RELOC:      Contents of section .reloc:
-RELOC-NEXT: 2000 00100000 0c000000 07a00000 00000000  ................
+CHECK:      Disassembly of section .text:
+CHECK-NEXT: .text:
+CHECK-NEXT:     1000: 48 b8 28 10 00 40 01 00 00 ff
+CHECK-NEXT:     100a: e8 19 00 00 ff
+CHECK-NEXT:     100f: e8 13 00 00 ff
+CHECK-NEXT:     1014: e8 0d 00 00 ff
+CHECK-NEXT:     1019: e8 07 00 00 ff
+CHECK-NEXT:     101e: e8 01 00 00 ff
+CHECK-NEXT:     1023: e8 fb ff ff fe
+CHECK-NEXT:     1028: e8 01 00 00 ff
+CHECK-NEXT:     102d: e8 28 00 00 ff
+CHECK-NEXT:     1032: c3





More information about the llvm-commits mailing list