From a143a2811e2897745c9080372c54bc54a7a4e657 Mon Sep 17 00:00:00 2001 From: Lewis Wang Date: Thu, 19 Nov 2026 19:42:42 +0800 Subject: [PATCH] fix(native): size link-time relocation kinds in x86-64 and AArch64 objects (#12235) * fix(native): keep an assumed relocation width from reporting a section overrun inspect_native_initializers assumed that a relocation of a type it does not size writes a whole word. For a 4-byte field that ends its section, such as R_X86_64_PLT32 or R_AARCH64_CALL26 on a tail call that ends .text, the assumed word ran past the section, so the object was reported incomplete with "A section relocation extends beyond its target mapping". An assumed width is now narrowed to the bytes its mapping holds. A relocation of known width that runs past its section is still reported. Co-Authored-By: Claude Fable 5.2 * test(native): use the tail-call relocation clang emits for AArch64 Co-Authored-By: Claude Fable 5.2 * fix(native): size link-time relocation kinds instead of narrowing an assumed width Review found that narrowing an assumed word to its mapping hid a real overrun of an unlisted kind and, in a linked image, a slot behind zero-fill bytes. The mapping remainder does not establish what a relocation writes. The section walk is restored to its previous logic. The width table now sizes the link-time kinds clang emits into x86-64 and AArch64 objects, as the i386 row already does, so a call that ends .text is a known 4-byte field. A kind the table does not hold keeps its assumed word and its overrun note. Co-Authored-By: Claude Fable 5.2 * test(native): cover a newly sized kind in the loader relocation walk Co-Authored-By: Claude Fable 5.2 --------- Co-authored-by: Claude Fable 5.2 --- .../native/elfPointerRelocations.test.ts | 26 +++++++ src/domain/native/elfPointerRelocations.ts | 36 ++++++++++ .../NativeInitializersParsers.test.ts | 71 +++++++++++++++++++ 3 files changed, 133 insertions(+) diff --git a/src/domain/native/elfPointerRelocations.test.ts b/src/domain/native/elfPointerRelocations.test.ts index a18eeee744..e7e66c221e 100644 --- a/src/domain/native/elfPointerRelocations.test.ts +++ b/src/domain/native/elfPointerRelocations.test.ts @@ -643,6 +643,32 @@ it("reports a TLS descriptor as a two-word write", () => { expect(elfRelocationWidth(3, 41, 32)).toBe(8); }); +it("reports a sized link-time kind in a dynamic table as a write of unknown value", () => { + // R_X86_64_PLT32, as a text relocation would carry it. + const { writes, result } = walk(symbolicImage(4n, 0n)); + expect(writes).toMatchObject([{ address: 0x300n, width: 4, value: null }]); + expect(result.pointer_coverage_known).toBe(true); +}); + +it("sizes the link-time kinds compilers emit into x86-64 and AArch64 objects", () => { + // Kind numbers and names were paired from clang output with llvm-objdump. + for (const kind of [4, 9, 19, 20, 21, 22, 23, 34, 41, 42]) + expect(elfRelocationWidth(62, kind, 64)).toBe(4); + for (const kind of [25, 27, 29]) + expect(elfRelocationWidth(62, kind, 64)).toBe(8); + // Each of these patches one 32-bit A64 instruction. + for (const kind of [ + 273, 274, 275, 277, 278, 282, 283, 284, 285, 286, 299, 309, 311, 312, 541, + 542, 543, 549, 551, 562, 563, 564, + ]) + expect(elfRelocationWidth(183, kind, 64)).toBe(4); + // TLSDESC_CALL marks an instruction and has no field of its own. + expect(elfRelocationWidth(62, 35, 64)).toBeUndefined(); + expect(elfRelocationWidth(183, 569, 64)).toBeUndefined(); + // A kind outside the table keeps no width. + expect(elfRelocationWidth(62, 99, 64)).toBeUndefined(); +}); + /** Three fully decoded relative values; empty REL declarations add no state. */ const relativeValues = (duplicate = false): Buffer => { const bytes = image(); diff --git a/src/domain/native/elfPointerRelocations.ts b/src/domain/native/elfPointerRelocations.ts index ef94477bdf..3c8a977368 100644 --- a/src/domain/native/elfPointerRelocations.ts +++ b/src/domain/native/elfPointerRelocations.ts @@ -302,6 +302,29 @@ const RELOCATION_KINDS = [ [260, 8], [261, 4], [262, 2], + // Link-time kinds that patch one 32-bit instruction. + [273, 4], // LD_PREL_LO19 + [274, 4], // ADR_PREL_LO21 + [275, 4], // ADR_PREL_PG_HI21 + [277, 4], // ADD_ABS_LO12_NC + [278, 4], // LDST8_ABS_LO12_NC + [282, 4], // JUMP26 + [283, 4], // CALL26 + [284, 4], // LDST16_ABS_LO12_NC + [285, 4], // LDST32_ABS_LO12_NC + [286, 4], // LDST64_ABS_LO12_NC + [299, 4], // LDST128_ABS_LO12_NC + [309, 4], // GOT_LD_PREL19 + [311, 4], // ADR_GOT_PAGE + [312, 4], // LD64_GOT_LO12_NC + [541, 4], // TLSIE_ADR_GOTTPREL_PAGE21 + [542, 4], // TLSIE_LD64_GOTTPREL_LO12_NC + [543, 4], // TLSIE_LD_GOTTPREL_PREL19 + [549, 4], // TLSLE_ADD_TPREL_HI12 + [551, 4], // TLSLE_ADD_TPREL_LO12_NC + [562, 4], // TLSDESC_ADR_PAGE21 + [563, 4], // TLSDESC_LD64_LO12 + [564, 4], // TLSDESC_ADD_LO12 [1025, 8], [1026, 8], [1028, 8], @@ -321,16 +344,29 @@ const RELOCATION_KINDS = [ widths: new Map([ [1, 8], [2, 4], + [4, 4], // PLT32 [6, 8], [7, 8], + [9, 4], // GOTPCREL [10, 4], [11, 4], [16, 8], [17, 8], [18, 8], + [19, 4], // TLSGD + [20, 4], // TLSLD + [21, 4], // DTPOFF32 + [22, 4], // GOTTPOFF + [23, 4], // TPOFF32 [24, 8], + [25, 8], // GOTOFF64 + [27, 8], // GOT64 + [29, 8], // GOTPC64 + [34, 4], // GOTPC32_TLSDESC [36, 16], [37, 8], + [41, 4], // GOTPCRELX + [42, 4], // REX_GOTPCRELX ]), }, { diff --git a/src/native/initializers/NativeInitializersParsers.test.ts b/src/native/initializers/NativeInitializersParsers.test.ts index 29f18fe577..f90ce1e15e 100644 --- a/src/native/initializers/NativeInitializersParsers.test.ts +++ b/src/native/initializers/NativeInitializersParsers.test.ts @@ -678,6 +678,77 @@ describe("ELF incomplete inputs", () => { }); }); +describe("ELF section relocation extents", () => { + /** + * The relocatable fixture for `machine`, whose two slot relocations use its + * word-sized absolute kind, optionally with one `.rela.text` record of + * `type` at `offset` of the 16-byte `.text`. + */ + const object = ( + machine: number, + absolute: number, + record?: { readonly type: number; readonly offset: bigint }, + ): Buffer => { + const entry = Buffer.alloc(24); + entry.writeBigUInt64LE(record?.offset ?? 0n, 0); + entry.writeBigUInt64LE((1n << 32n) | BigInt(record?.type ?? 0), 8); + const bytes = + record === undefined + ? elfRelocatableCtors() + : withExtraSections(entry, (at) => [ + sectionHeader(4, at, 24, 4, 24, 1), + ]); + bytes.writeUInt16LE(machine, 18); + bytes.writeUInt32LE(absolute, 0x308); + bytes.writeUInt32LE(absolute, 0x320); + return bytes; + }; + const OVERRUN = + "A section relocation extends beyond its target mapping; affected slots are unresolved."; + const facts = (report: Awaited>) => ({ + status: report.status, + coverage: report.coverage.status, + limitations: report.limitations, + rows: row(report), + }); + + it.each([ + { name: "R_X86_64_PLT32", machine: 62, absolute: 1, type: 4 }, + { name: "R_AARCH64_JUMP26", machine: 183, absolute: 257, type: 282 }, + ])( + "reports nothing for $name relocating the last four bytes of .text", + async ({ machine, absolute, type }) => { + const report = await read( + object(machine, absolute, { type, offset: 12n }), + ); + expect(report.limitations).not.toContain(OVERRUN); + // The object without that record is the working comparison. + expect(facts(report)).toEqual( + facts(await read(object(machine, absolute))), + ); + }, + ); + + it("keeps an x86-64 object complete beside a call that ends .text", async () => { + const report = await read(object(62, 1, { type: 4, offset: 12n })); + expect(report.status).toBe("parsed"); + expect(report.coverage.status).toBe("complete"); + }); + + it.each([ + // R_X86_64_64 writes eight bytes, four of them beyond .text. + { name: "an eight-byte R_X86_64_64 at offset 12", type: 1, offset: 12n }, + // The four-byte field starts two bytes before the end of .text. + { name: "a four-byte R_X86_64_PLT32 at offset 14", type: 4, offset: 14n }, + // No width is known for kind 99, so the assumed word still counts. + { name: "an unlisted kind at offset 12", type: 99, offset: 12n }, + ])("reports $name as running past its section", async (record) => { + const report = await read(object(62, 1, record)); + expect(report.limitations).toContain(OVERRUN); + expect(report.status).toBe("incomplete"); + }); +}); + describe("ELF table budgets", () => { it("caps symbols in total across every symbol table, not per table", async () => { const perTable = 40_000;