diff --git a/lld/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp b/lld/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp index 18ef69b9905a..b397222b9928 100644 --- a/lld/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp +++ b/lld/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp @@ -539,7 +539,7 @@ static int getSectionIndex(uint64_t targetAddr, static uint32_t getSectionStartAddr(uint64_t targetAddr, const std::vector §ionRva) { 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"); } diff --git a/lld/test/pecoff/Inputs/secrel1.obj.yaml b/lld/test/pecoff/Inputs/secrel1.obj.yaml new file mode 100644 index 000000000000..1c49261bbc43 --- /dev/null +++ b/lld/test/pecoff/Inputs/secrel1.obj.yaml @@ -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 +... diff --git a/lld/test/pecoff/Inputs/secrel2.obj.yaml b/lld/test/pecoff/Inputs/secrel2.obj.yaml new file mode 100644 index 000000000000..2885fd07bb3a --- /dev/null +++ b/lld/test/pecoff/Inputs/secrel2.obj.yaml @@ -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 +... diff --git a/lld/test/pecoff/secrel.test b/lld/test/pecoff/secrel.test new file mode 100644 index 000000000000..05014c47af48 --- /dev/null +++ b/lld/test/pecoff/secrel.test @@ -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