[lld] r221420 - [PECOFF] Fix SECREL relocations.

Rui Ueyama ruiu at google.com
Wed Nov 5 17:03:24 PST 2014


Author: ruiu
Date: Wed Nov  5 19:03:23 2014
New Revision: 221420

URL: http://llvm.org/viewvc/llvm-project?rev=221420&view=rev
Log:
[PECOFF] Fix SECREL relocations.

SECREL relocation's value is the offset to the beginning of the section.
Because of the off-by-one error, if a SECREL relocation target is at the
beginning of a section, it got wrong value.

Added a test that would have caught this.

Added:
    lld/trunk/test/pecoff/Inputs/secrel1.obj.yaml
    lld/trunk/test/pecoff/Inputs/secrel2.obj.yaml
    lld/trunk/test/pecoff/secrel.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=221420&r1=221419&r2=221420&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp (original)
+++ lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp Wed Nov  5 19:03:23 2014
@@ -539,7 +539,7 @@ static int getSectionIndex(uint64_t targ
 static uint32_t getSectionStartAddr(uint64_t targetAddr,
                                     const std::vector<uint64_t> &sectionRva) {
   for (int i = 0, e = sectionRva.size(); i < e; ++i)
-    if (i == e - 1 || (sectionRva[i] <= targetAddr && targetAddr <= sectionRva[i + 1]))
+    if (i == e - 1 || (sectionRva[i] <= targetAddr && targetAddr < sectionRva[i + 1]))
       return sectionRva[i];
   llvm_unreachable("Section missing");
 }

Added: lld/trunk/test/pecoff/Inputs/secrel1.obj.yaml
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/pecoff/Inputs/secrel1.obj.yaml?rev=221420&view=auto
==============================================================================
--- lld/trunk/test/pecoff/Inputs/secrel1.obj.yaml (added)
+++ lld/trunk/test/pecoff/Inputs/secrel1.obj.yaml Wed Nov  5 19:03:23 2014
@@ -0,0 +1,69 @@
+---
+header:
+  Machine:         IMAGE_FILE_MACHINE_I386
+  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:       4
+    SectionData:     00000000000000000000000000000000
+    Relocations:
+      - VirtualAddress:  0
+        SymbolName:      .data
+        Type:            IMAGE_REL_I386_SECREL
+  - Name:            .data2
+    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE ]
+    Alignment:       4
+    SectionData:     00000000000000000000000000000000
+    Relocations:
+      - VirtualAddress:  0
+        SymbolName:      .data2
+        Type:            IMAGE_REL_I386_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:          60
+      NumberOfRelocations: 1
+      NumberOfLinenumbers: 0
+      CheckSum:        0
+      Number:          1
+  - Name:            .data
+    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:            .data2
+    Value:           0
+    SectionNumber:   3
+    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:          3
+  - Name:            _main
+    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/secrel2.obj.yaml
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/pecoff/Inputs/secrel2.obj.yaml?rev=221420&view=auto
==============================================================================
--- lld/trunk/test/pecoff/Inputs/secrel2.obj.yaml (added)
+++ lld/trunk/test/pecoff/Inputs/secrel2.obj.yaml Wed Nov  5 19:03:23 2014
@@ -0,0 +1,47 @@
+---
+header:
+  Machine:         IMAGE_FILE_MACHINE_I386
+  Characteristics: [  ]
+sections:
+  - Name:            .data
+    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE ]
+    Alignment:       4
+    SectionData:     00000000000000000000000000000000
+    Relocations:
+      - VirtualAddress:  0
+        SymbolName:      .data
+        Type:            IMAGE_REL_I386_SECREL
+  - Name:            .data2
+    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE ]
+    Alignment:       4
+    SectionData:     00000000000000000000000000000000
+    Relocations:
+      - VirtualAddress:  0
+        SymbolName:      .data2
+        Type:            IMAGE_REL_I386_SECREL
+symbols:
+  - Name:            .data
+    Value:           0
+    SectionNumber:   1
+    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:          1
+  - Name:            .data2
+    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
+...

Added: lld/trunk/test/pecoff/secrel.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/pecoff/secrel.test?rev=221420&view=auto
==============================================================================
--- lld/trunk/test/pecoff/secrel.test (added)
+++ lld/trunk/test/pecoff/secrel.test Wed Nov  5 19:03:23 2014
@@ -0,0 +1,16 @@
+# RUN: yaml2obj %p/Inputs/secrel1.obj.yaml > %t1.obj
+# RUN: yaml2obj %p/Inputs/secrel2.obj.yaml > %t2.obj
+# RUN: yaml2obj %p/Inputs/secrel2.obj.yaml > %t3.obj
+#
+# RUN: lld -flavor link /out:%t.exe /subsystem:console /entry:main \
+# RUN:   -- %t1.obj %t2.obj %t3.obj
+# RUN: llvm-objdump -s %t.exe | FileCheck %s
+
+CHECK: Contents of section .data:
+CHECK:  1000 00000000 00000000 00000000 00000000
+CHECK:  1010 10000000 00000000 00000000 00000000
+CHECK:  1020 20000000 00000000 00000000 00000000
+CHECK: Contents of section .data2:
+CHECK:  2000 00000000 00000000 00000000 00000000
+CHECK:  2010 10000000 00000000 00000000 00000000
+CHECK:  2020 20000000 00000000 00000000 00000000





More information about the llvm-commits mailing list