fix(native): bound metadata projection and compact Mach-O bind runs
Validation: integrated changes passed formatting, typecheck, lint, resolved module-boundary verification and git diff --check. Tests and real-provider workflows were not run.
This commit is contained in:
@@ -69,6 +69,13 @@ interface Import {
|
||||
readonly addend: bigint;
|
||||
}
|
||||
|
||||
interface BindRun {
|
||||
readonly start: bigint;
|
||||
count: bigint;
|
||||
readonly stride: bigint;
|
||||
readonly binding: Import;
|
||||
}
|
||||
|
||||
/** Library ordinal to install name; 0 and negative ordinals are special lookups. */
|
||||
const libraryName = (
|
||||
ordinal: number,
|
||||
@@ -307,6 +314,7 @@ const sleb = (bytes: Buffer, cursor: { at: number }, end: number): bigint => {
|
||||
/** Registers of the `LC_DYLD_INFO` bind state machine. */
|
||||
interface BindState {
|
||||
ordinal: number;
|
||||
segmentIndex: number;
|
||||
symbol: string;
|
||||
weak: boolean;
|
||||
addend: bigint;
|
||||
@@ -351,6 +359,7 @@ const setBindRegister = (
|
||||
const segment = image.segments[immediate];
|
||||
if (segment === undefined)
|
||||
throw new RangeError("Bind segment index is out of range");
|
||||
state.segmentIndex = immediate;
|
||||
state.address = segment.address + uleb(bytes, cursor, end);
|
||||
return true;
|
||||
}
|
||||
@@ -368,7 +377,7 @@ const setBindRegister = (
|
||||
const readBindStream = (
|
||||
image: FixupImage,
|
||||
range: FixupCommands["binds"][number],
|
||||
binds: Map<bigint, Import>,
|
||||
binds: BindRun[],
|
||||
): void => {
|
||||
const { bytes } = image;
|
||||
const end = range.offset + range.size;
|
||||
@@ -377,13 +386,27 @@ const readBindStream = (
|
||||
const cursor = { at: range.offset };
|
||||
const state: BindState = {
|
||||
ordinal: 0,
|
||||
segmentIndex: -1,
|
||||
symbol: "",
|
||||
weak: false,
|
||||
addend: 0n,
|
||||
address: 0n,
|
||||
};
|
||||
const bind = (skip: bigint): void => {
|
||||
binds.set(state.address, {
|
||||
const preflightRun = (count: bigint, skip: bigint): void => {
|
||||
if (count === 0n) return;
|
||||
const segment = image.segments[state.segmentIndex];
|
||||
if (segment === undefined)
|
||||
throw new RangeError("Bind location has no selected segment");
|
||||
const stride = 8n + skip;
|
||||
const end = state.address + (count - 1n) * stride + 8n;
|
||||
if (state.address < segment.address || end > segment.address + segment.size)
|
||||
throw new RangeError("Bind expansion leaves its file-backed segment");
|
||||
};
|
||||
const bind = (count: bigint, skip: bigint): void => {
|
||||
if (count === 0n) return;
|
||||
preflightRun(count, skip);
|
||||
const stride = 8n + skip;
|
||||
const binding: Import = {
|
||||
symbol: state.symbol,
|
||||
library:
|
||||
range.stream === "weak"
|
||||
@@ -391,8 +414,22 @@ const readBindStream = (
|
||||
: libraryName(state.ordinal, image.dylibs),
|
||||
weak: state.weak,
|
||||
addend: state.addend,
|
||||
});
|
||||
state.address += 8n + skip;
|
||||
};
|
||||
const previous = binds.at(-1);
|
||||
if (
|
||||
previous !== undefined &&
|
||||
previous.stride === stride &&
|
||||
previous.start + previous.count * stride === state.address &&
|
||||
previous.binding.symbol === binding.symbol &&
|
||||
previous.binding.library === binding.library &&
|
||||
previous.binding.weak === binding.weak &&
|
||||
previous.binding.addend === binding.addend
|
||||
) {
|
||||
previous.count += count;
|
||||
} else {
|
||||
binds.push({ start: state.address, count, stride, binding });
|
||||
}
|
||||
state.address += count * stride;
|
||||
};
|
||||
while (cursor.at < end) {
|
||||
const byte = bytes[cursor.at++] ?? 0;
|
||||
@@ -403,18 +440,22 @@ const readBindStream = (
|
||||
if (range.stream !== "lazy") return;
|
||||
break;
|
||||
case 0x90:
|
||||
bind(0n);
|
||||
bind(1n, 0n);
|
||||
break;
|
||||
case 0xa0:
|
||||
bind(uleb(bytes, cursor, end));
|
||||
bind(1n, uleb(bytes, cursor, end));
|
||||
break;
|
||||
case 0xb0:
|
||||
bind(BigInt(byte & 0x0f) * 8n);
|
||||
bind(1n, BigInt(byte & 0x0f) * 8n);
|
||||
break;
|
||||
case 0xc0: {
|
||||
if (range.stream === "lazy")
|
||||
throw new RangeError(
|
||||
"Lazy bind streams do not support repeated bind opcodes",
|
||||
);
|
||||
const count = uleb(bytes, cursor, end);
|
||||
const skip = uleb(bytes, cursor, end);
|
||||
for (let index = 0n; index < count; index++) bind(skip);
|
||||
bind(count, skip);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
@@ -451,7 +492,7 @@ export const parsePointerFixups = (
|
||||
unreadRebaseBytes,
|
||||
decode: (_address, raw) => ({ kind: "rebase", target: raw }),
|
||||
};
|
||||
const binds = new Map<bigint, Import>();
|
||||
const binds: BindRun[] = [];
|
||||
const failures: string[] = [];
|
||||
for (const stream of commands.binds)
|
||||
try {
|
||||
@@ -471,10 +512,26 @@ export const parsePointerFixups = (
|
||||
examined: commands.binds.length,
|
||||
unreadRebaseBytes,
|
||||
decode(address, raw) {
|
||||
const bound = binds.get(address);
|
||||
return bound === undefined
|
||||
let match: BindRun | undefined;
|
||||
for (const run of binds) {
|
||||
const { start, count, stride } = run;
|
||||
const distance = address - start;
|
||||
if (
|
||||
distance >= 0n &&
|
||||
distance % stride === 0n &&
|
||||
distance / stride < count
|
||||
) {
|
||||
if (match !== undefined)
|
||||
return {
|
||||
kind: "unsupported",
|
||||
reason: `Ambiguous bind records target pointer location 0x${address.toString(16)}`,
|
||||
};
|
||||
match = run;
|
||||
}
|
||||
}
|
||||
return match === undefined
|
||||
? { kind: "rebase", target: raw }
|
||||
: { kind: "bind", ...bound };
|
||||
: { kind: "bind", ...match.binding };
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user