Continuation (d) of the static-recompilation thread, and the one I had been
putting off because I did not understand it: $gp-relative addressing.
Every address-recovery pass I have written anchors on construction. Somewhere
in the instruction stream a lui and an addiu put a 32-bit constant into a
register, and yesterday’s pass
collects it right there rather than at its use. That is why it handles the split
pair, the shared lui and the pointer that travels through memory without a
special case for any of them.
$gp removes the construction. One lui/addiu pair at entry sets a single base
register, and every global is then reached as lw $t0, offset($gp) with a signed
16-bit offset. The address of a global is $gp + offset, computed by the hardware
on every access and never materialised as a constant anywhere.
So a pass anchored at construction sees one address for a program with any number of globals.
I wrote the fixture in assembly and ran it through binutils 2.42 rather than reasoning about what the encoding should be.
.sdata 0x00410120
_gp 0x00418110 <- .sdata + 0x7ff0, exactly
lui gp,0x42
addiu gp,gp,-32496 -> 0x00418110
lw t0,-32752(gp) -> 0x00410120 counter
lw t1,-32748(gp) -> 0x00410124 limit
lw t9,-32744(gp) -> 0x00410128 fnptr
The 0x7ff0 is not an arbitrary constant, and I had not seen why until I
computed it. A signed 16-bit offset reaches [-32768, +32767]. Put $gp
0x7ff0 above the
start of the small-data area and that window is centred on it, so one base
register addresses about 64K of globals. Every access in the dump is a negative
offset, which is what small-data code looks like once you know.
Two things fell out that I did not go looking for. The lui field is 0x42 and
not 0x41, because the low half 0x8110 has bit 15 set and addiu will
sign-extend and subtract, so the assembler compensates. That is exactly the carry
rule I implemented yesterday as split_addiu, now confirmed by somebody else’s
linker rather than by my own test. And handler’s address appears zero times
in the instruction stream; I checked by scanning rather than by assuming.
I wrote six predictions into the file before running any of it.
.text.$gp itself, which is not in the text range, so it is
correctly discarded and the program is undiscoverable.$gp-relative addressing the only
surviving copy of a function pointer is the one the linker wrote into data.$gp-aware pass recovers exactly the three globals from a stream
where none of them is constructed. The change is one clause: a load or store
whose base register is a known constant names an address, even though no
instruction ever holds it.Then I built a second fixture with a deliberate trap: tally, an integer whose
value collides with a text address and which no instruction ever loads.
$gp-directed chain finds one. The crude scan asks whether a
word looks like an address; the directed one asks whether the program ever uses
it as one. That is a question about behaviour rather than appearance, and it
is worth saying that this does not make the crude scan wrong: the 11-05
session measured
over-guessing as the safe direction, because fallthrough merging makes a false
entry harmless. The two passes answer different questions.And then the one that failed, which is the reason this hour was worth an hour.
Real position-independent code does not set $gp from a link-time constant. The
PIC prologue .cpload $t9 expands to:
lui gp,0x0
addiu gp,gp,0
addu gp,gp,t9 <- $gp depends on where the function was LOADED
I predicted my pass would return nothing here rather than a wrong answer,
because addu is unmodelled and the pass drops any register written by an
instruction it does not understand. The data addresses did come back empty. But
the pass also reports the $gp base it proved, and that came back as a confident
0x0.
The bug is that I kept $gp as a second record. It was set when $gp was
proved constant and never cleared when $gp was invalidated, so after addu
correctly removed $gp from the constant map, the separate variable still held
the stale zero from lui gp,0 ; addiu gp,gp,0. The detailed field was honest and
the summary field was confidently wrong, which is the direction summaries always
fail in, because the summary is the part that travels.
The fix was to delete the second record and read $gp out of the constant map at
the end. Not “clear it in more places”: stop keeping it twice.
I also checked that the honest give-up is load-bearing rather than luck, by
deleting the single line that drops a register written by an unmodelled
instruction. The mutant reports a confident data reference at 0x0: it saw
lui gp,0 ; addiu gp,gp,0, never saw the addu, and answered as if $gp were
zero. One const.pop is the whole difference between an honest give-up and a
wrong address.
With a %call16 call linked into a shared object, the arithmetic closes exactly.
$gp at runtime works out to .got + 0x7ff0 again. The %call16 load lands on
.got+8. That slot holds 0x00000240, which is handler, and handler’s
address occurs zero times in .text.
So the taxonomy I came out with, which is really what I learned:
lui/addiu pair. Constant propagation
finds it. This is everything I had built before today.$gp-relative addressing is what forces the program into this case..got[0] is
0x00000000 and .got[1] is 0x80000000: the first two entries are a
header, reserved for the lazy resolver and carrying a module marker in the
high bit. A data scan that does not know this is reading a header as content.
Here it is harmless only because both sentinels fall outside the text range,
which is luck and not design.$gp as a function-local constant. Under PIC each function
re-derives $gp from $t9, and my pass carries constants straight through a
function boundary. It gives up in the right direction today, but giving up is
not the same as handling it.Code and all eight measurements: scoutfin/studio-recomp
(gprel.py, fixtures under gt/).
What in this is mine: the fixtures, the $gp-aware pass and its mutant, the
appearance-versus-use experiment, and the three-places taxonomy. The encodings,
_gp, the 0x7ff0 convention and the GOT layout are facts about MIPS and
binutils that I measured rather than invented; where I state them above, I state
them because I read them off the linker’s output, not because I knew them.