Skip to content

Commit a9ab0ff

Browse files
committed
[LoongArch] Fix -mcmodel=extreme
Based on MQ-mengqing's patch. Fixes #1131
1 parent 206bf65 commit a9ab0ff

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

elf/arch-loongarch.cc

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,12 @@ static u64 hi64(u64 val, u64 pc) {
6565
// LU52I.D simply set bits to [51:31] and to [63:53], respectively.
6666
//
6767
// Compensating all the sign-extensions is a bit complicated.
68-
bool x = val & 0x800;
69-
bool y = (page(val + 0x800) - page(pc)) & 0x8000'0000;
70-
71-
if (x && !y)
72-
return val - 0x1'0000'0000;
73-
if (!x && y)
74-
return val + 0x1'0000'0000;
75-
return val;
68+
u64 x = page(val) - page(pc);
69+
if (val & 0x800)
70+
x += 0x1000 - 0x1'0000'0000;
71+
if (x & 0x8000'0000)
72+
x += 0x1'0000'0000;
73+
return x;
7674
}
7775

7876
static u64 higher20(u64 val, u64 pc) {
@@ -328,7 +326,6 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
328326
write_k12(loc, S + A);
329327
break;
330328
case R_LARCH_PCALA_HI20:
331-
check(S + A - P, -(1LL << 31), 1LL << 31);
332329
write_j20(loc, hi20(S + A, P));
333330
break;
334331
case R_LARCH_PCALA64_LO20:
@@ -587,6 +584,7 @@ void InputSection<E>::scan_relocations(Context<E> &ctx) {
587584
scan_dyn_absrel(ctx, sym, rel);
588585
break;
589586
case R_LARCH_B26:
587+
case R_LARCH_PCALA_HI20:
590588
if (sym.is_imported)
591589
sym.flags |= NEEDS_PLT;
592590
break;
@@ -620,7 +618,6 @@ void InputSection<E>::scan_relocations(Context<E> &ctx) {
620618
case R_LARCH_ABS_LO12:
621619
case R_LARCH_ABS64_LO20:
622620
case R_LARCH_ABS64_HI12:
623-
case R_LARCH_PCALA_HI20:
624621
case R_LARCH_PCALA_LO12:
625622
case R_LARCH_PCALA64_LO20:
626623
case R_LARCH_PCALA64_HI12:
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
. $(dirname $0)/common.inc
3+
4+
cat <<EOF | $CC -o $t/a.o -c -xc - -mcmodel=extreme
5+
#include <stdio.h>
6+
char msg[] = "Hello world\n";
7+
int main() { printf(msg); }
8+
EOF
9+
10+
$CC -B. -o $t/exe1 $t/a.o
11+
$QEMU $t/exe1 | grep -q 'Hello world'
12+
13+
$CC -B. -o $t/exe2 $t/a.o -Wl,-Ttext=0x100000000,-Tdata=0x500000000
14+
$QEMU $t/exe2 | grep -q 'Hello world'

0 commit comments

Comments
 (0)