Skip to content

Commit 92e63a6

Browse files
authored
Fix relocations in dynamic linking (#9)
The patch implements the required changes to support runtime relocations in MinGW. For 26-bit relocations, the linker generates a jump stub, as a single opcode is not sufficient for relocation. Co-authored-by: Evgeny Karpov <[email protected]>
1 parent 7e62dc7 commit 92e63a6

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

mingw-w64-crt/crt/pseudo-reloc.c

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -507,23 +507,28 @@ do_pseudo_reloc (void * start, void * end, void * base)
507507
break;
508508
#ifdef __aarch64__
509509
case 12:
510-
reldata = reldata & 0xfff;
511-
opcode &= 0xffc003ff;
512-
opcode |= reldata << 10;
510+
/* Replace add Xn, Xn, :lo12:label with ldr Xn, [Xn, :lo12:__imp__func].
511+
That loads the address of _func into Xn. */
512+
opcode = 0xf9400000 | (opcode & 0x3ff); // ldr
513+
reldata = ((ptrdiff_t) base + r->sym) & ((1 << 12) - 1);
514+
reldata >>= 3;
515+
opcode |= reldata << 10;
513516
__write_memory ((void *) reloc_target, &opcode, 4);
514517
break;
515518
case 21:
519+
/* Replace adrp Xn, label with adrp Xn, __imp__func. */
516520
opcode &= 0x9f00001f;
517-
reldata >>= 12;
518-
opcode |= (reldata & 0x3) << 29;
519-
opcode |= (reldata & 0x1ffffc) << 3;
520-
__write_memory ((void *) reloc_target, &opcode, 4);
521-
break;
522-
case 26:
523-
opcode &= 0xfc000000;
524-
opcode |= (reldata >> 2) & 0x3ffffff;
521+
reldata = (((ptrdiff_t) base + r->sym) >> 12)
522+
- (((ptrdiff_t) base + r->target) >> 12);
523+
reldata &= (1 << 21) - 1;
524+
opcode |= (reldata & 3) << 29;
525+
reldata >>= 2;
526+
opcode |= reldata << 5;
525527
__write_memory ((void *) reloc_target, &opcode, 4);
526528
break;
529+
/* A note regarding 26 bits relocation.
530+
A single opcode is not sufficient for 26 bits relocation in dynamic linking.
531+
The linker generates a jump stub instead. */
527532
#endif
528533
case 32:
529534
__write_memory ((void *) reloc_target, &reldata, 4);

0 commit comments

Comments
 (0)