llvm-project/lld/test/ELF/i386-reloc-range.s

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

25 lines
890 B
ArmAsm
Raw Normal View History

// REQUIRES: x86
// RUN: llvm-mc %s -o %t.o -triple i386-pc-linux-code16 -filetype=obj
Relax the overflow checking of R_386_PC16. Currently the freebsd early boot code fails to link. The issue reduces to 16 bit code at position 0x7000 wanting to jump to position 0x9000. That is represented in the .o file as a relocation with no symbol and an addend of 0x9000 - 2 (The -2 is because i386 uses the ip after the current instruction for jumps). If the addend is interpreted as signed (it should), it is -28674. In a 32 bit architecture, that is the address 0xffff8ffe. To get there from 0x7000 we have to add 4294909950 (too big) or subtract 57346 (too small). We then produce an error. What lld is missing is the fact that at runtime this will actually be a 16 bit architecture and adding 0x1ffe produces 0x8ffe which is the correct result in 16 bits (-28674). Since we have a 16 bit addend and a 16 bit PC, the relocation can move the PC to any 16 bit address and that is the only thing we really need to check: if the address we are pointing to fits in 16 bits. This is unfortunately hard to do since we have to delay subtracting the PC and if we want to do that outside of Target.cpp, we have to move the overflow check out too. An incomplete patch that tries to do that is at https://reviews.llvm.org/D34070 This patch instead just relaxes the check. Since the value we have is the destination minus the PC and the PC is 16 bits, it should fit in 17 bits if the destination fits in 16 too. bfd had a similar issue for some time and got a similar fix: https://sourceware.org/ml/binutils/2005-08/msg00001.html llvm-svn: 305135
2017-06-10 09:56:58 +08:00
// RUN: echo ".global foo; foo = 0x10202" > %t1.s
// RUN: llvm-mc %t1.s -o %t1.o -triple i386-pc-linux -filetype=obj
Relax the overflow checking of R_386_PC16. Currently the freebsd early boot code fails to link. The issue reduces to 16 bit code at position 0x7000 wanting to jump to position 0x9000. That is represented in the .o file as a relocation with no symbol and an addend of 0x9000 - 2 (The -2 is because i386 uses the ip after the current instruction for jumps). If the addend is interpreted as signed (it should), it is -28674. In a 32 bit architecture, that is the address 0xffff8ffe. To get there from 0x7000 we have to add 4294909950 (too big) or subtract 57346 (too small). We then produce an error. What lld is missing is the fact that at runtime this will actually be a 16 bit architecture and adding 0x1ffe produces 0x8ffe which is the correct result in 16 bits (-28674). Since we have a 16 bit addend and a 16 bit PC, the relocation can move the PC to any 16 bit address and that is the only thing we really need to check: if the address we are pointing to fits in 16 bits. This is unfortunately hard to do since we have to delay subtracting the PC and if we want to do that outside of Target.cpp, we have to move the overflow check out too. An incomplete patch that tries to do that is at https://reviews.llvm.org/D34070 This patch instead just relaxes the check. Since the value we have is the destination minus the PC and the PC is 16 bits, it should fit in 17 bits if the destination fits in 16 too. bfd had a similar issue for some time and got a similar fix: https://sourceware.org/ml/binutils/2005-08/msg00001.html llvm-svn: 305135
2017-06-10 09:56:58 +08:00
// RUN: echo ".global foo; foo = 0x10203" > %t2.s
// RUN: llvm-mc %t2.s -o %t2.o -triple i386-pc-linux -filetype=obj
// RUN: ld.lld -Ttext 0x200 %t.o %t1.o -o %t1
// RUN: llvm-objdump -d --no-show-raw-insn --triple=i386-pc-linux-code16 %t1 | FileCheck %s
// CHECK: Disassembly of section .text:
// CHECK-EMPTY:
// CHECK-NEXT: <_start>:
// CHECK-NEXT: 200: jmp 0x202
Relax the overflow checking of R_386_PC16. Currently the freebsd early boot code fails to link. The issue reduces to 16 bit code at position 0x7000 wanting to jump to position 0x9000. That is represented in the .o file as a relocation with no symbol and an addend of 0x9000 - 2 (The -2 is because i386 uses the ip after the current instruction for jumps). If the addend is interpreted as signed (it should), it is -28674. In a 32 bit architecture, that is the address 0xffff8ffe. To get there from 0x7000 we have to add 4294909950 (too big) or subtract 57346 (too small). We then produce an error. What lld is missing is the fact that at runtime this will actually be a 16 bit architecture and adding 0x1ffe produces 0x8ffe which is the correct result in 16 bits (-28674). Since we have a 16 bit addend and a 16 bit PC, the relocation can move the PC to any 16 bit address and that is the only thing we really need to check: if the address we are pointing to fits in 16 bits. This is unfortunately hard to do since we have to delay subtracting the PC and if we want to do that outside of Target.cpp, we have to move the overflow check out too. An incomplete patch that tries to do that is at https://reviews.llvm.org/D34070 This patch instead just relaxes the check. Since the value we have is the destination minus the PC and the PC is 16 bits, it should fit in 17 bits if the destination fits in 16 too. bfd had a similar issue for some time and got a similar fix: https://sourceware.org/ml/binutils/2005-08/msg00001.html llvm-svn: 305135
2017-06-10 09:56:58 +08:00
// 0x10202 - 0x203 == 0xffff
// RUN: not ld.lld -Ttext 0x200 %t.o %t2.o -o /dev/null 2>&1 | FileCheck --check-prefix=ERR %s
// ERR: {{.*}}:(.text+0x1): relocation R_386_PC16 out of range: 65536 is not in [-65536, 65535]; references foo
.global _start
_start:
jmp foo