[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.

llvm-svn: 221420
This commit is contained in:
Rui Ueyama 2014-11-06 01:03:23 +00:00
parent aa0f673413
commit 30804c4220
4 changed files with 133 additions and 1 deletions

View File

@ -539,7 +539,7 @@ static int getSectionIndex(uint64_t targetAddr,
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");
}

View File

@ -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
...

View File

@ -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
...

View File

@ -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