Skip to content

Instantly share code, notes, and snippets.

@heiher
Created November 15, 2023 15:44
Show Gist options
  • Save heiher/bd7398397f3cb5598dce35cbc04d0075 to your computer and use it in GitHub Desktop.
Save heiher/bd7398397f3cb5598dce35cbc04d0075 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
int
main (int argc, char *argv[])
{
long target;
long dest;
long pc;
long delta;
long pcala_hi20;
long pcala_lo12;
long pcala64_lo20;
long pcala64_hi12;
dest = strtoul (argv[1], NULL, 16);
pc = strtoul (argv[2], NULL, 16);
delta = dest - (pc & ~0xfffUL);
delta = delta + ((delta & 0x800UL) << 1);
delta = delta + ((delta & 0x80000000UL) << 1);
delta = delta - ((delta & 0x800UL) << 21);
pcala_hi20 = (delta >> 12) & 0xfffffUL;
pcala_lo12 = delta & 0xfffUL;
pcala64_lo20 = (delta >> 32) & 0xfffffUL;
pcala64_hi12 = (delta >> 52) & 0xfffUL;
printf ("R_LARCH_PCALA_HI20 = 0x%lx\n", pcala_hi20);
printf ("R_LARCH_PCALA_LO12 = 0x%lx\n", pcala_lo12);
printf ("R_LARCH_PCALA64_LO20 = 0x%lx\n", pcala64_lo20);
printf ("R_LARCH_PCALA64_HI12 = 0x%lx\n", pcala64_hi12);
target = (pc & ~0xfffUL) + ((pcala_hi20 << 44) >> 32) +
((pcala64_hi12 << 52) | (pcala64_lo20 << 32) | (((pcala_lo12 << 52) >> 52) & 0xffffffffUL));
if (target != dest) {
printf ("%0lx != %0lx\n", dest, target);
return -1;
}
return 0;
}
@heiher
Copy link
Author

heiher commented Nov 16, 2023

Yes. The instruction's free encoding bit-field can be used to maintain the relationship before relocation. But this is no longer compatible with what it used to be.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment