peachsim 0.1.2600 → 0.1.2602
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cli/commands/detect.ts +16 -10
- package/dist-lib/agent-daemon-client.cjs +1 -1
- package/dist-lib/agent-events.cjs +1 -1
- package/dist-lib/attached-projects.cjs +1 -1
- package/dist-lib/auth/shared-session.cjs +1 -1
- package/dist-lib/backend-origin.cjs +1 -1
- package/dist-lib/beta.cjs +1 -1
- package/dist-lib/beta.mjs +2 -2
- package/dist-lib/bridge-constants.cjs +1 -1
- package/dist-lib/bridge-contract-input.cjs +1 -1
- package/dist-lib/bridge-contract-input.mjs +5 -5
- package/dist-lib/bridge-contract.cjs +1 -1
- package/dist-lib/bridge-contract.mjs +3 -3
- package/dist-lib/bundle-channel.cjs +11432 -11074
- package/dist-lib/capture-contract.cjs +1 -1
- package/dist-lib/capture-contract.mjs +3 -3
- package/dist-lib/cloud-contract.cjs +1 -1
- package/dist-lib/cloud-contract.mjs +3 -3
- package/dist-lib/cloud-test-args.cjs +1 -1
- package/dist-lib/cloud-test-args.mjs +2 -2
- package/dist-lib/cloud.cjs +1 -1
- package/dist-lib/cloud.mjs +7 -7
- package/dist-lib/config.cjs +1 -1
- package/dist-lib/config.mjs +5 -5
- package/dist-lib/detox/index.cjs +11 -2276
- package/dist-lib/dev-bundle-resolution.cjs +1 -1
- package/dist-lib/esm-chunks/{acorn-FGRSVFK5.mjs → acorn-OY6WD7QV.mjs} +3 -3
- package/dist-lib/esm-chunks/{chunk-GTJLYHLX.mjs → chunk-4RQOWOMT.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-RFLRGBWJ.mjs → chunk-7T3ACQVD.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-ZPZHTSVV.mjs → chunk-A4ONOIAH.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-7VUNHZJ4.mjs → chunk-DW7F2DCV.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-F72K7HJL.mjs → chunk-FTNF3J4Y.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-DB24TNAU.mjs → chunk-GCFOJXHC.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-23YC72IU.mjs → chunk-HA4NBBPR.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-66EJ2CSG.mjs → chunk-I47W3MVR.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-GNN4EGGX.mjs → chunk-I7DN6IVP.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-R5YW43IZ.mjs → chunk-IH7VXRII.mjs} +4 -4
- package/dist-lib/esm-chunks/{chunk-CC6C72OW.mjs → chunk-IUZHDZYD.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-FQRSMKOT.mjs → chunk-JFRVR54X.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-46MZJPMK.mjs → chunk-K5IL7A47.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-BVRFDE5A.mjs → chunk-OPEVMW4Z.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-C2BC7GUJ.mjs → chunk-PRC3KSZK.mjs} +2 -2
- package/dist-lib/esm-chunks/{chunk-7O5GHZNG.mjs → chunk-QREYIKY5.mjs} +5 -5
- package/dist-lib/esm-chunks/{chunk-IEXENIW2.mjs → chunk-RS3CUH42.mjs} +3 -3
- package/dist-lib/esm-chunks/{chunk-IANEFUXF.mjs → chunk-T22TWE3B.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-LLIEH2SU.mjs → chunk-T6MXHYHI.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-T7BVUONU.mjs → chunk-VZORKVTY.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-C3YFMHVW.mjs → chunk-WKCN2CSK.mjs} +5 -5
- package/dist-lib/esm-chunks/{chunk-G2OL3VLT.mjs → chunk-ZDI63AW4.mjs} +1024 -653
- package/dist-lib/esm-chunks/{metro-layout-parser-WVDFEJTE.mjs → metro-layout-parser-2ERWNLZ7.mjs} +3 -3
- package/dist-lib/esm-chunks/{swift-package-prebuilt-FRKYWMMQ.mjs → swift-package-prebuilt-SP4MCJOF.mjs} +10 -10
- package/dist-lib/esm-chunks/{swift-sdk-app-module-TMDS6TCR.mjs → swift-sdk-app-module-LXIPYGVM.mjs} +14 -14
- package/dist-lib/esm-chunks/{swift-sdk-image-73TU5HWR.mjs → swift-sdk-image-RTRI5IV4.mjs} +14 -14
- package/dist-lib/esm-chunks/{swift-sdk-image-producer-WJTPYSJ4.mjs → swift-sdk-image-producer-7NXHRRJE.mjs} +4 -4
- package/dist-lib/expo-swift-bundle.cjs +12833 -12475
- package/dist-lib/expo-swift-metadata.cjs +1 -1
- package/dist-lib/expo-swift-metadata.mjs +7 -7
- package/dist-lib/home-paths.cjs +1 -1
- package/dist-lib/host/bridge-host.cjs +12809 -12451
- package/dist-lib/host/fetch-proxy-handler.cjs +5434 -5422
- package/dist-lib/host/fetch-proxy-overrides.cjs +1 -1
- package/dist-lib/host/fetch-proxy-overrides.mjs +2 -2
- package/dist-lib/host/replacement-module-handler.cjs +1 -1
- package/dist-lib/host/websocket-proxy.cjs +1 -1
- package/dist-lib/index.cjs +10 -2275
- package/dist-lib/kotlin-host.cjs +1 -1
- package/dist-lib/menu.cjs +1 -1
- package/dist-lib/menu.mjs +3 -3
- package/dist-lib/metro-fingerprint-registry.cjs +1 -1
- package/dist-lib/metro-fingerprint-registry.mjs +3 -3
- package/dist-lib/metro-production-bundle.cjs +1 -1
- package/dist-lib/metro-production-bundle.mjs +4 -4
- package/dist-lib/native-app-identity-project.cjs +23 -11
- package/dist-lib/native-app-identity-project.mjs +16 -16
- package/dist-lib/native-app-identity.cjs +1 -1
- package/dist-lib/native-app-identity.mjs +6 -6
- package/dist-lib/native-firebase-metadata.cjs +1 -1
- package/dist-lib/native-firebase-metadata.mjs +3 -3
- package/dist-lib/pdfium-raster.cjs +1 -1
- package/dist-lib/pdfium-raster.mjs +2 -2
- package/dist-lib/plist.cjs +1 -1
- package/dist-lib/plist.mjs +3 -3
- package/dist-lib/profiles.cjs +1 -1
- package/dist-lib/public-brand.cjs +1 -1
- package/dist-lib/public-origins-lib-esm.json +82 -81
- package/dist-lib/public-origins-lib.json +372 -429
- package/dist-lib/react-native-host-modules.cjs +1 -1
- package/dist-lib/react-native-host-modules.mjs +2 -2
- package/dist-lib/reap-browser.cjs +1 -1
- package/dist-lib/scripts/dev-server-scanner.cjs +1 -1
- package/dist-lib/sdk.cjs +1 -1
- package/dist-lib/sdk.mjs +8 -8
- package/dist-lib/skills.cjs +1093 -735
- package/dist-lib/swift-compile.cjs +1029 -652
- package/dist-lib/swift-compile.mjs +10 -10
- package/dist-lib/swift-package.cjs +1 -1
- package/dist-lib/swift-package.mjs +3 -3
- package/dist-lib/swift-submit.cjs +1 -1
- package/dist-lib/swift-submit.mjs +6 -6
- package/dist-lib/swift-svg-raster.cjs +1 -1
- package/dist-lib/swift-svg-raster.mjs +2 -2
- package/dist-lib/swift-toolchain.cjs +1 -1
- package/dist-lib/swift.cjs +1 -1
- package/dist-lib/vendor/@peculiar/x509.cjs +1 -1
- package/dist-lib/vendor/@ruby/prism/src/nodes.js.cjs +1 -1
- package/dist-lib/vendor/@ruby/prism/src/parsePrism.js.cjs +1 -1
- package/dist-lib/vendor/@xmldom/xmldom.cjs +1 -1
- package/dist-lib/vendor/acorn.cjs +1 -1
- package/dist-lib/vendor/bplist-parser.cjs +1 -1
- package/dist-lib/vendor/ignore.cjs +1 -1
- package/dist-lib/vendor/magic-string.cjs +1 -1
- package/dist-lib/vendor/plist.cjs +1 -1
- package/dist-lib/vendor/stream-json.cjs +1 -1
- package/dist-lib/vendor/sucrase/dist/index.js.cjs +1 -1
- package/dist-lib/vendor/sucrase/dist/parser/index.js.cjs +1 -1
- package/dist-lib/vendor/sucrase/dist/parser/tokenizer/index.js.cjs +1 -1
- package/dist-lib/vendor/sucrase/dist/parser/tokenizer/types.js.cjs +1 -1
- package/dist-lib/vendor/sucrase.cjs +1 -1
- package/dist-lib/vendor/terser.cjs +1 -1
- package/dist-lib/vendor/valibot.cjs +1 -1
- package/dist-lib/vendor/ws.cjs +1 -1
- package/dist-lib/vendor/zod.cjs +1 -1
- package/dist-lib/worklet-executable.cjs +1 -1
- package/dist-lib/worklet-executable.mjs +3 -3
- package/java-platform/licenses/android.txt +3 -0
- package/java-platform/licenses/openjdk.txt +9 -2
- package/kotlin/sdk.tar.gz +0 -0
- package/package.json +1 -2
- package/src/device-catalog.ts +4 -5
- package/src/ios-version.ts +8 -0
- package/src/swift-guest-compile.ts +5 -3
- package/src/swift-guest-image-ledger.ts +563 -264
- package/src/swift-guest-link.ts +628 -468
- package/src/wasm-binary-sections.ts +1 -1
|
@@ -222,6 +222,41 @@ function llvmType(value: number): string {
|
|
|
222
222
|
const OBJC_INIT_RECORD = '.objc_init'
|
|
223
223
|
const TABLE_INDEX_RELOCATIONS = new Set([1, 2, 12, 18, 19, 24])
|
|
224
224
|
|
|
225
|
+
interface ObjectSectionScan {
|
|
226
|
+
readonly importHeaderStart: number
|
|
227
|
+
readonly importStart: number
|
|
228
|
+
readonly importEnd: number
|
|
229
|
+
readonly linkingHeaderStart: number
|
|
230
|
+
readonly linkingStart: number
|
|
231
|
+
readonly linkingEnd: number
|
|
232
|
+
readonly usesDlsym: boolean
|
|
233
|
+
readonly usesDlerror: boolean
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function concatParts(parts: readonly Uint8Array[]): Buffer {
|
|
237
|
+
let total = 0
|
|
238
|
+
for (let i = 0; i < parts.length; i++) total += parts[i]!.byteLength
|
|
239
|
+
const out = Buffer.allocUnsafe(total)
|
|
240
|
+
let offset = 0
|
|
241
|
+
for (let i = 0; i < parts.length; i++) {
|
|
242
|
+
const part = parts[i]!
|
|
243
|
+
out.set(part, offset)
|
|
244
|
+
offset += part.byteLength
|
|
245
|
+
}
|
|
246
|
+
return out
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
const sha256PairHex: (first: string, second: string) => string =
|
|
250
|
+
typeof Bun !== 'undefined'
|
|
251
|
+
? (first, second) =>
|
|
252
|
+
new Bun.CryptoHasher('sha256')
|
|
253
|
+
.update(first)
|
|
254
|
+
.update('\0')
|
|
255
|
+
.update(second)
|
|
256
|
+
.digest('hex')
|
|
257
|
+
: (first, second) =>
|
|
258
|
+
createHash('sha256').update(first).update('\0').update(second).digest('hex')
|
|
259
|
+
|
|
225
260
|
// change definition names in the linking symbol table, preserving symbol
|
|
226
261
|
// indices and relocations. no app or dependency source bodies are changed.
|
|
227
262
|
function rewriteObject(
|
|
@@ -235,12 +270,16 @@ function rewriteObject(
|
|
|
235
270
|
sdkLookupOnly?: boolean
|
|
236
271
|
sdkProduct?: string
|
|
237
272
|
collectRewrittenSymbols?: { referenced: Set<string>; defined: Set<string> }
|
|
273
|
+
scan?: ObjectSectionScan
|
|
238
274
|
} = {},
|
|
239
275
|
): {
|
|
240
276
|
readonly bytes: Uint8Array
|
|
277
|
+
readonly parts?: readonly Uint8Array[]
|
|
278
|
+
readonly scan?: ObjectSectionScan
|
|
241
279
|
readonly definitions: readonly ObjectDefinition[]
|
|
242
280
|
readonly functionImports: ReadonlyMap<string, FunctionType>
|
|
243
281
|
readonly usesDlsym: boolean
|
|
282
|
+
readonly usesDlerror: boolean
|
|
244
283
|
readonly usedSymbols: ReadonlySet<string>
|
|
245
284
|
readonly addressTaken: readonly string[]
|
|
246
285
|
} {
|
|
@@ -248,11 +287,10 @@ function rewriteObject(
|
|
|
248
287
|
const rawBuf = Buffer.isBuffer(bytes)
|
|
249
288
|
? bytes
|
|
250
289
|
: Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength)
|
|
251
|
-
const { sdkLookupOnly, sdkProduct, collectRewrittenSymbols } = options
|
|
290
|
+
const { sdkLookupOnly, sdkProduct, collectRewrittenSymbols, scan } = options
|
|
252
291
|
const comdatScope =
|
|
253
292
|
sdkProduct === undefined ? callerAlias : `${callerAlias}:${sdkProduct}`
|
|
254
293
|
const rewrite = binding !== undefined
|
|
255
|
-
const sections: Uint8Array[] | undefined = rewrite ? [bytes.subarray(0, 8)] : undefined
|
|
256
294
|
const types: FunctionType[] = []
|
|
257
295
|
const functionTypes: number[] = []
|
|
258
296
|
const definitions: ObjectDefinition[] = []
|
|
@@ -263,10 +301,400 @@ function rewriteObject(
|
|
|
263
301
|
const eventImportNames: string[] = []
|
|
264
302
|
const functionSymbols: (string | undefined)[] = []
|
|
265
303
|
const addressTaken = new Set<string>()
|
|
266
|
-
let usesDlsym = false
|
|
267
|
-
let usesDlerror = false
|
|
304
|
+
let usesDlsym = scan?.usesDlsym ?? false
|
|
305
|
+
let usesDlerror = scan?.usesDlerror ?? false
|
|
268
306
|
const dlerrorAlias = sdkLookupOnly ? '__peach_sdk_dlerror' : '__peach_dlerror'
|
|
269
307
|
const usedSymbols = new Set<string>()
|
|
308
|
+
|
|
309
|
+
const rewriteImportSection = (start: number, end: number): Uint8Array => {
|
|
310
|
+
let offset = start
|
|
311
|
+
const number = (): number => {
|
|
312
|
+
if (offset < end) {
|
|
313
|
+
const first = bytes[offset]!
|
|
314
|
+
if (first < 128) {
|
|
315
|
+
offset++
|
|
316
|
+
return first
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
let value = 0
|
|
320
|
+
let shift = 0
|
|
321
|
+
for (;;) {
|
|
322
|
+
if (offset >= end || shift > 63) throw new Error('invalid wasm integer')
|
|
323
|
+
const byte = bytes[offset++]!
|
|
324
|
+
value += (byte & 127) * 2 ** shift
|
|
325
|
+
if (!(byte & 128)) return value
|
|
326
|
+
shift += 7
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
const string = (): string => {
|
|
330
|
+
const length = number()
|
|
331
|
+
const strEnd = offset + length
|
|
332
|
+
if (strEnd > end) throw new Error('invalid wasm string')
|
|
333
|
+
const value = rawBuf.toString('utf8', offset, strEnd)
|
|
334
|
+
offset = strEnd
|
|
335
|
+
return value
|
|
336
|
+
}
|
|
337
|
+
const count = number()
|
|
338
|
+
const importWriter = new ByteWriter(end - start + 64)
|
|
339
|
+
importWriter.uleb(count)
|
|
340
|
+
for (let i = 0; i < count; i++) {
|
|
341
|
+
const entryStart = offset
|
|
342
|
+
const module = string()
|
|
343
|
+
const nameStart = offset
|
|
344
|
+
const name = string()
|
|
345
|
+
const nameEnd = offset
|
|
346
|
+
const kind = bytes[offset++]!
|
|
347
|
+
if (kind === 0) {
|
|
348
|
+
number()
|
|
349
|
+
if (collectRewrittenSymbols) functionImportNames.push(name)
|
|
350
|
+
} else if (kind === 1) {
|
|
351
|
+
const referenceType = bytes[offset++]
|
|
352
|
+
if (referenceType !== 0x70 && referenceType !== 0x6f)
|
|
353
|
+
throw new Error('unsupported table type')
|
|
354
|
+
const flags = number()
|
|
355
|
+
if (flags & ~7) throw new Error('unsupported table limits')
|
|
356
|
+
number()
|
|
357
|
+
if (flags & 1) number()
|
|
358
|
+
} else if (kind === 2) {
|
|
359
|
+
const flags = number()
|
|
360
|
+
if (flags & ~7) throw new Error('unsupported memory limits')
|
|
361
|
+
number()
|
|
362
|
+
if (flags & 1) number()
|
|
363
|
+
} else if (kind === 3) offset += 2
|
|
364
|
+
else if (kind === 4) {
|
|
365
|
+
number()
|
|
366
|
+
number()
|
|
367
|
+
} else throw new Error('unsupported wasm import')
|
|
368
|
+
const isDlsym =
|
|
369
|
+
instrumentDynamicLookups && module === 'env' && name === 'dlsym' && kind === 0
|
|
370
|
+
const isDlerror =
|
|
371
|
+
instrumentDynamicLookups && module === 'env' && name === 'dlerror' && kind === 0
|
|
372
|
+
const rewrittenName = isDlsym
|
|
373
|
+
? callerAlias
|
|
374
|
+
: isDlerror
|
|
375
|
+
? dlerrorAlias
|
|
376
|
+
: module === 'env' && name !== 'dlsym' && name !== 'dlerror'
|
|
377
|
+
? (binding!(name) ?? name)
|
|
378
|
+
: name
|
|
379
|
+
if (rewrittenName === name) {
|
|
380
|
+
importWriter.bytes(bytes, entryStart, offset)
|
|
381
|
+
} else {
|
|
382
|
+
importWriter.bytes(bytes, entryStart, nameStart)
|
|
383
|
+
importWriter.string(rewrittenName)
|
|
384
|
+
importWriter.bytes(bytes, nameEnd, offset)
|
|
385
|
+
}
|
|
386
|
+
if (collectRewrittenSymbols) {
|
|
387
|
+
if (kind === 0)
|
|
388
|
+
functionImportNames[functionImportNames.length - 1] = rewrittenName
|
|
389
|
+
else if (kind === 1) tableImportNames.push(rewrittenName)
|
|
390
|
+
else if (kind === 3) globalImportNames.push(rewrittenName)
|
|
391
|
+
else if (kind === 4) eventImportNames.push(rewrittenName)
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
if (offset !== end) throw new Error('invalid wasm import section')
|
|
395
|
+
return importWriter.finish()
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
const rewriteLinkingSection = (start: number, end: number): Uint8Array => {
|
|
399
|
+
let offset = start
|
|
400
|
+
const number = (): number => {
|
|
401
|
+
if (offset < end) {
|
|
402
|
+
const first = bytes[offset]!
|
|
403
|
+
if (first < 128) {
|
|
404
|
+
offset++
|
|
405
|
+
return first
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
let value = 0
|
|
409
|
+
let shift = 0
|
|
410
|
+
for (;;) {
|
|
411
|
+
if (offset >= end || shift > 63) throw new Error('invalid wasm integer')
|
|
412
|
+
const byte = bytes[offset++]!
|
|
413
|
+
value += (byte & 127) * 2 ** shift
|
|
414
|
+
if (!(byte & 128)) return value
|
|
415
|
+
shift += 7
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
const string = (): string => {
|
|
419
|
+
const length = number()
|
|
420
|
+
const strEnd = offset + length
|
|
421
|
+
if (strEnd > end) throw new Error('invalid wasm string')
|
|
422
|
+
const value = rawBuf.toString('utf8', offset, strEnd)
|
|
423
|
+
offset = strEnd
|
|
424
|
+
return value
|
|
425
|
+
}
|
|
426
|
+
const sectionName = string()
|
|
427
|
+
const version = number()
|
|
428
|
+
if (version !== 2) throw new Error('unsupported wasm linking version')
|
|
429
|
+
const subsections: { kind: number; parts: Uint8Array[]; byteLength: number }[] = []
|
|
430
|
+
let objcLoader: number | undefined
|
|
431
|
+
while (offset < end) {
|
|
432
|
+
const kind = bytes[offset++]!
|
|
433
|
+
const length = number()
|
|
434
|
+
const stop = offset + length
|
|
435
|
+
if (stop > end) throw new Error('invalid linking subsection')
|
|
436
|
+
const contentStart = offset
|
|
437
|
+
if (kind === 7 && !sdkLookupOnly) {
|
|
438
|
+
const comdatWriter = new ByteWriter(length + 64)
|
|
439
|
+
const count = number()
|
|
440
|
+
comdatWriter.uleb(count)
|
|
441
|
+
for (let i = 0; i < count; i++) {
|
|
442
|
+
const group = string()
|
|
443
|
+
const begin = offset
|
|
444
|
+
number()
|
|
445
|
+
const members = number()
|
|
446
|
+
for (let member = 0; member < members * 2; member++) number()
|
|
447
|
+
comdatWriter.string(
|
|
448
|
+
group === OBJC_INIT_RECORD ? group : `${comdatScope}:${group}`,
|
|
449
|
+
)
|
|
450
|
+
comdatWriter.bytes(bytes, begin, offset)
|
|
451
|
+
}
|
|
452
|
+
if (offset !== stop) throw new Error('invalid comdat subsection')
|
|
453
|
+
const buf = comdatWriter.finish()
|
|
454
|
+
subsections.push({ kind, parts: [buf], byteLength: buf.length })
|
|
455
|
+
} else if (kind === 8) {
|
|
456
|
+
const entryWriter = new ByteWriter(length + 128)
|
|
457
|
+
let extraWriter: ByteWriter | undefined
|
|
458
|
+
const count = number()
|
|
459
|
+
let added = 0
|
|
460
|
+
for (let i = 0; i < count; i++) {
|
|
461
|
+
const begin = offset
|
|
462
|
+
const symbolKind = bytes[offset++]!
|
|
463
|
+
const flags = number()
|
|
464
|
+
const flagsEnd = offset
|
|
465
|
+
let index = -1
|
|
466
|
+
let name = ''
|
|
467
|
+
let nameStart = -1
|
|
468
|
+
let nameEnd = -1
|
|
469
|
+
const isLocalDefined = !(flags & 16) && (flags & 3) === 2
|
|
470
|
+
if (
|
|
471
|
+
symbolKind === 0 ||
|
|
472
|
+
symbolKind === 2 ||
|
|
473
|
+
symbolKind === 4 ||
|
|
474
|
+
symbolKind === 5
|
|
475
|
+
) {
|
|
476
|
+
index = number()
|
|
477
|
+
if (!(flags & 16) || flags & 64) {
|
|
478
|
+
nameStart = offset
|
|
479
|
+
const nameLen = number()
|
|
480
|
+
nameEnd = offset + nameLen
|
|
481
|
+
if (nameEnd > end) throw new Error('invalid wasm string')
|
|
482
|
+
if (
|
|
483
|
+
!isLocalDefined ||
|
|
484
|
+
(!sdkLookupOnly &&
|
|
485
|
+
symbolKind === 0 &&
|
|
486
|
+
nameLen === OBJC_LOAD_FUNCTION.length)
|
|
487
|
+
) {
|
|
488
|
+
name = rawBuf.toString('utf8', offset, nameEnd)
|
|
489
|
+
}
|
|
490
|
+
offset = nameEnd
|
|
491
|
+
}
|
|
492
|
+
} else if (symbolKind === 1) {
|
|
493
|
+
nameStart = offset
|
|
494
|
+
const nameLen = number()
|
|
495
|
+
nameEnd = offset + nameLen
|
|
496
|
+
if (nameEnd > end) throw new Error('invalid wasm string')
|
|
497
|
+
if (!isLocalDefined) name = rawBuf.toString('utf8', offset, nameEnd)
|
|
498
|
+
offset = nameEnd
|
|
499
|
+
if (!(flags & 16)) {
|
|
500
|
+
if ((flags & 3) !== 3) {
|
|
501
|
+
number()
|
|
502
|
+
number()
|
|
503
|
+
}
|
|
504
|
+
number()
|
|
505
|
+
if ((flags & 3) === 3) offset++
|
|
506
|
+
}
|
|
507
|
+
} else if (symbolKind === 3) number()
|
|
508
|
+
else throw new Error('unsupported wasm symbol kind')
|
|
509
|
+
if (
|
|
510
|
+
!sdkLookupOnly &&
|
|
511
|
+
symbolKind === 0 &&
|
|
512
|
+
!(flags & 16) &&
|
|
513
|
+
name === OBJC_LOAD_FUNCTION
|
|
514
|
+
) {
|
|
515
|
+
objcLoader = i
|
|
516
|
+
}
|
|
517
|
+
if (isLocalDefined) {
|
|
518
|
+
entryWriter.bytes(bytes, begin, offset)
|
|
519
|
+
continue
|
|
520
|
+
}
|
|
521
|
+
let rewrittenSym = name
|
|
522
|
+
let addedCanonical: string | undefined
|
|
523
|
+
let bound: string | undefined
|
|
524
|
+
let alias: string | undefined
|
|
525
|
+
if (
|
|
526
|
+
((name === 'dlsym' && usesDlsym) || (name === 'dlerror' && usesDlerror)) &&
|
|
527
|
+
flags & 16 &&
|
|
528
|
+
flags & 64 &&
|
|
529
|
+
symbolKind === 0
|
|
530
|
+
) {
|
|
531
|
+
rewrittenSym = name === 'dlsym' ? callerAlias : dlerrorAlias
|
|
532
|
+
entryWriter.bytes(bytes, begin, nameStart)
|
|
533
|
+
entryWriter.string(rewrittenSym)
|
|
534
|
+
entryWriter.bytes(bytes, nameEnd, offset)
|
|
535
|
+
} else if (
|
|
536
|
+
flags & 16 &&
|
|
537
|
+
name !== 'dlsym' &&
|
|
538
|
+
name !== 'dlerror' &&
|
|
539
|
+
(bound = binding!(name)) !== undefined
|
|
540
|
+
) {
|
|
541
|
+
rewrittenSym = bound
|
|
542
|
+
entryWriter.bytes(bytes, begin, nameStart)
|
|
543
|
+
entryWriter.string(rewrittenSym)
|
|
544
|
+
entryWriter.bytes(bytes, nameEnd, offset)
|
|
545
|
+
} else if (!(flags & 16) && (alias = names.get(name)) !== undefined) {
|
|
546
|
+
if (symbolKind !== 0 && symbolKind !== 1)
|
|
547
|
+
throw new Error(`unsupported symbol ${name}`)
|
|
548
|
+
if (preserveCanonical.has(name)) {
|
|
549
|
+
entryWriter.bytes(bytes, begin, offset)
|
|
550
|
+
extraWriter ??= new ByteWriter(256)
|
|
551
|
+
extraWriter.byte(bytes[begin]!)
|
|
552
|
+
extraWriter.uleb(flags & ~0x20)
|
|
553
|
+
extraWriter.bytes(bytes, flagsEnd, nameStart)
|
|
554
|
+
extraWriter.string(alias)
|
|
555
|
+
extraWriter.bytes(bytes, nameEnd, offset)
|
|
556
|
+
added++
|
|
557
|
+
addedCanonical = alias
|
|
558
|
+
} else {
|
|
559
|
+
rewrittenSym = alias
|
|
560
|
+
entryWriter.bytes(bytes, begin, nameStart)
|
|
561
|
+
entryWriter.string(alias)
|
|
562
|
+
entryWriter.bytes(bytes, nameEnd, offset)
|
|
563
|
+
}
|
|
564
|
+
} else {
|
|
565
|
+
entryWriter.bytes(bytes, begin, offset)
|
|
566
|
+
}
|
|
567
|
+
if (collectRewrittenSymbols) {
|
|
568
|
+
if (flags & 16) {
|
|
569
|
+
const resolved =
|
|
570
|
+
flags & 64 || symbolKind === 1
|
|
571
|
+
? rewrittenSym
|
|
572
|
+
: symbolKind === 0
|
|
573
|
+
? (functionImportNames[index] ?? '')
|
|
574
|
+
: symbolKind === 2
|
|
575
|
+
? (globalImportNames[index] ?? '')
|
|
576
|
+
: symbolKind === 4
|
|
577
|
+
? (eventImportNames[index] ?? '')
|
|
578
|
+
: symbolKind === 5
|
|
579
|
+
? (tableImportNames[index] ?? '')
|
|
580
|
+
: ''
|
|
581
|
+
if (resolved) collectRewrittenSymbols.referenced.add(resolved)
|
|
582
|
+
} else {
|
|
583
|
+
if (rewrittenSym) collectRewrittenSymbols.defined.add(rewrittenSym)
|
|
584
|
+
if (addedCanonical) collectRewrittenSymbols.defined.add(addedCanonical)
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
if (offset !== stop) throw new Error('invalid symbol subsection')
|
|
589
|
+
const countBytes = encodeNumber(count + added)
|
|
590
|
+
const entryBuf = entryWriter.finish()
|
|
591
|
+
if (extraWriter) {
|
|
592
|
+
const extraBuf = extraWriter.finish()
|
|
593
|
+
subsections.push({
|
|
594
|
+
kind,
|
|
595
|
+
parts: [countBytes, entryBuf, extraBuf],
|
|
596
|
+
byteLength: countBytes.length + entryBuf.length + extraBuf.length,
|
|
597
|
+
})
|
|
598
|
+
} else {
|
|
599
|
+
subsections.push({
|
|
600
|
+
kind,
|
|
601
|
+
parts: [countBytes, entryBuf],
|
|
602
|
+
byteLength: countBytes.length + entryBuf.length,
|
|
603
|
+
})
|
|
604
|
+
}
|
|
605
|
+
} else {
|
|
606
|
+
subsections.push({
|
|
607
|
+
kind,
|
|
608
|
+
parts: [bytes.subarray(contentStart, stop)],
|
|
609
|
+
byteLength: length,
|
|
610
|
+
})
|
|
611
|
+
}
|
|
612
|
+
offset = stop
|
|
613
|
+
}
|
|
614
|
+
const linkingWriter = new ByteWriter(end - start + 128)
|
|
615
|
+
linkingWriter.string(sectionName)
|
|
616
|
+
linkingWriter.uleb(version)
|
|
617
|
+
for (const subsection of subsections) {
|
|
618
|
+
if (subsection.kind === 6 && objcLoader !== undefined) {
|
|
619
|
+
const content = subsection.parts[0]!
|
|
620
|
+
const [count, entriesStart] = readNumber(content, 0)
|
|
621
|
+
const initWriter = new ByteWriter(content.length + 16)
|
|
622
|
+
initWriter.bytes(content, 0, entriesStart)
|
|
623
|
+
let entryStart = entriesStart
|
|
624
|
+
for (let i = 0; i < count; i++) {
|
|
625
|
+
const [, symbolStart] = readNumber(content, entryStart)
|
|
626
|
+
const [symbol, entryEnd] = readNumber(content, symbolStart)
|
|
627
|
+
if (symbol === objcLoader) {
|
|
628
|
+
initWriter.uleb(1)
|
|
629
|
+
initWriter.bytes(content, symbolStart, entryEnd)
|
|
630
|
+
} else {
|
|
631
|
+
initWriter.bytes(content, entryStart, entryEnd)
|
|
632
|
+
}
|
|
633
|
+
entryStart = entryEnd
|
|
634
|
+
}
|
|
635
|
+
if (entryStart !== content.length)
|
|
636
|
+
throw new Error('invalid init functions subsection')
|
|
637
|
+
const initBuf = initWriter.finish()
|
|
638
|
+
linkingWriter.byte(subsection.kind)
|
|
639
|
+
linkingWriter.uleb(initBuf.length)
|
|
640
|
+
linkingWriter.bytes(initBuf)
|
|
641
|
+
} else {
|
|
642
|
+
linkingWriter.byte(subsection.kind)
|
|
643
|
+
linkingWriter.uleb(subsection.byteLength)
|
|
644
|
+
for (const part of subsection.parts) linkingWriter.bytes(part)
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
return linkingWriter.finish()
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
// fast path: pass 1 already located the import and linking sections, so pass 2
|
|
651
|
+
// splices only those two sections without re-walking types, code, data or relocs.
|
|
652
|
+
if (
|
|
653
|
+
rewrite &&
|
|
654
|
+
scan &&
|
|
655
|
+
(scan.importHeaderStart < 0 ||
|
|
656
|
+
scan.linkingHeaderStart < 0 ||
|
|
657
|
+
scan.importHeaderStart < scan.linkingHeaderStart)
|
|
658
|
+
) {
|
|
659
|
+
const parts: Uint8Array[] = []
|
|
660
|
+
let cursor = 0
|
|
661
|
+
if (scan.importHeaderStart >= 0) {
|
|
662
|
+
if (scan.importHeaderStart > cursor)
|
|
663
|
+
parts.push(bytes.subarray(cursor, scan.importHeaderStart))
|
|
664
|
+
const rewrittenImport = rewriteImportSection(scan.importStart, scan.importEnd)
|
|
665
|
+
parts.push(Buffer.from([2]), encodeNumber(rewrittenImport.length), rewrittenImport)
|
|
666
|
+
cursor = scan.importEnd
|
|
667
|
+
}
|
|
668
|
+
if (scan.linkingHeaderStart >= 0) {
|
|
669
|
+
if (scan.linkingHeaderStart > cursor)
|
|
670
|
+
parts.push(bytes.subarray(cursor, scan.linkingHeaderStart))
|
|
671
|
+
const rewrittenLinking = rewriteLinkingSection(scan.linkingStart, scan.linkingEnd)
|
|
672
|
+
parts.push(
|
|
673
|
+
Buffer.from([0]),
|
|
674
|
+
encodeNumber(rewrittenLinking.length),
|
|
675
|
+
rewrittenLinking,
|
|
676
|
+
)
|
|
677
|
+
cursor = scan.linkingEnd
|
|
678
|
+
}
|
|
679
|
+
if (cursor < bytes.length) parts.push(bytes.subarray(cursor))
|
|
680
|
+
return {
|
|
681
|
+
bytes: concatParts(parts),
|
|
682
|
+
definitions,
|
|
683
|
+
functionImports,
|
|
684
|
+
usesDlsym,
|
|
685
|
+
usesDlerror,
|
|
686
|
+
usedSymbols,
|
|
687
|
+
addressTaken: [],
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
const sections: Uint8Array[] | undefined = rewrite ? [bytes.subarray(0, 8)] : undefined
|
|
692
|
+
let importHeaderStart = -1
|
|
693
|
+
let importStart = -1
|
|
694
|
+
let importEnd = -1
|
|
695
|
+
let linkingHeaderStart = -1
|
|
696
|
+
let linkingStart = -1
|
|
697
|
+
let linkingEnd = -1
|
|
270
698
|
let position = 8
|
|
271
699
|
while (position < bytes.length) {
|
|
272
700
|
const headerStart = position
|
|
@@ -330,6 +758,9 @@ function rewriteObject(
|
|
|
330
758
|
types.push({ parameters, results })
|
|
331
759
|
}
|
|
332
760
|
} else if (id === 2) {
|
|
761
|
+
importHeaderStart = headerStart
|
|
762
|
+
importStart = start
|
|
763
|
+
importEnd = end
|
|
333
764
|
const count = number()
|
|
334
765
|
const importWriter = rewrite ? new ByteWriter(size + 64) : undefined
|
|
335
766
|
importWriter?.uleb(count)
|
|
@@ -415,255 +846,97 @@ function rewriteObject(
|
|
|
415
846
|
} else if (id === 0) {
|
|
416
847
|
const sectionName = string()
|
|
417
848
|
if (sectionName === 'linking') {
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
const count = number()
|
|
439
|
-
comdatWriter.uleb(count)
|
|
440
|
-
for (let i = 0; i < count; i++) {
|
|
441
|
-
const group = string()
|
|
442
|
-
const begin = offset
|
|
443
|
-
number()
|
|
444
|
-
const members = number()
|
|
445
|
-
for (let member = 0; member < members * 2; member++) number()
|
|
446
|
-
comdatWriter.string(
|
|
447
|
-
group === OBJC_INIT_RECORD ? group : `${comdatScope}:${group}`,
|
|
448
|
-
)
|
|
449
|
-
comdatWriter.bytes(bytes, begin, offset)
|
|
450
|
-
}
|
|
451
|
-
if (offset !== stop) throw new Error('invalid comdat subsection')
|
|
452
|
-
const buf = comdatWriter.finish()
|
|
453
|
-
subsections!.push({ kind, parts: [buf], byteLength: buf.length })
|
|
454
|
-
} else if (kind === 8) {
|
|
455
|
-
const entryWriter = rewrite ? new ByteWriter(length + 128) : undefined
|
|
456
|
-
let extraWriter: ByteWriter | undefined
|
|
457
|
-
const count = number()
|
|
458
|
-
let added = 0
|
|
459
|
-
for (let i = 0; i < count; i++) {
|
|
460
|
-
const begin = offset
|
|
461
|
-
const symbolKind = bytes[offset++]!
|
|
462
|
-
const flags = number()
|
|
463
|
-
const flagsEnd = offset
|
|
464
|
-
let index = -1
|
|
465
|
-
let name = ''
|
|
466
|
-
let nameStart = -1
|
|
467
|
-
let nameEnd = -1
|
|
468
|
-
if (
|
|
469
|
-
symbolKind === 0 ||
|
|
470
|
-
symbolKind === 2 ||
|
|
471
|
-
symbolKind === 4 ||
|
|
472
|
-
symbolKind === 5
|
|
473
|
-
) {
|
|
474
|
-
index = number()
|
|
475
|
-
if (!(flags & 16) || flags & 64) {
|
|
476
|
-
nameStart = offset
|
|
477
|
-
name = string()
|
|
478
|
-
nameEnd = offset
|
|
479
|
-
}
|
|
480
|
-
} else if (symbolKind === 1) {
|
|
481
|
-
nameStart = offset
|
|
482
|
-
name = string()
|
|
483
|
-
nameEnd = offset
|
|
484
|
-
if (!(flags & 16)) {
|
|
485
|
-
if ((flags & 3) !== 3) {
|
|
486
|
-
number()
|
|
487
|
-
number()
|
|
488
|
-
}
|
|
489
|
-
number()
|
|
490
|
-
if ((flags & 3) === 3) offset++
|
|
491
|
-
}
|
|
492
|
-
} else if (symbolKind === 3) number()
|
|
493
|
-
else throw new Error('unsupported wasm symbol kind')
|
|
494
|
-
if (!rewrite && symbolKind === 0)
|
|
495
|
-
functionSymbols[i] = name || functionImportNames[index]
|
|
496
|
-
if (
|
|
497
|
-
rewrite &&
|
|
498
|
-
!sdkLookupOnly &&
|
|
499
|
-
name === OBJC_LOAD_FUNCTION &&
|
|
500
|
-
symbolKind === 0 &&
|
|
501
|
-
!(flags & 16)
|
|
502
|
-
) {
|
|
503
|
-
objcLoader = i
|
|
504
|
-
}
|
|
505
|
-
const alias = names.get(name)
|
|
506
|
-
// an undefined data symbol has no wasm import, only this linking entry.
|
|
507
|
-
if (!rewrite && flags & 16 && symbolKind === 1 && alias)
|
|
508
|
-
usedSymbols.add(name)
|
|
509
|
-
if (entryWriter && binding) {
|
|
510
|
-
let rewrittenSym = name
|
|
511
|
-
let addedCanonical: string | undefined
|
|
849
|
+
linkingHeaderStart = headerStart
|
|
850
|
+
linkingStart = start
|
|
851
|
+
linkingEnd = end
|
|
852
|
+
if (rewrite) {
|
|
853
|
+
rewrittenSection = rewriteLinkingSection(start, end)
|
|
854
|
+
} else {
|
|
855
|
+
const version = number()
|
|
856
|
+
if (version !== 2) throw new Error('unsupported wasm linking version')
|
|
857
|
+
while (offset < end) {
|
|
858
|
+
const kind = bytes[offset++]!
|
|
859
|
+
const length = number()
|
|
860
|
+
const stop = offset + length
|
|
861
|
+
if (stop > end) throw new Error('invalid linking subsection')
|
|
862
|
+
if (kind === 8) {
|
|
863
|
+
const count = number()
|
|
864
|
+
for (let i = 0; i < count; i++) {
|
|
865
|
+
const symbolKind = bytes[offset++]!
|
|
866
|
+
const flags = number()
|
|
867
|
+
let index = -1
|
|
868
|
+
let name = ''
|
|
512
869
|
if (
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
symbolKind === 0
|
|
518
|
-
) {
|
|
519
|
-
rewrittenSym = name === 'dlsym' ? callerAlias : dlerrorAlias
|
|
520
|
-
entryWriter.bytes(bytes, begin, nameStart)
|
|
521
|
-
entryWriter.string(rewrittenSym)
|
|
522
|
-
entryWriter.bytes(bytes, nameEnd, offset)
|
|
523
|
-
} else if (
|
|
524
|
-
flags & 16 &&
|
|
525
|
-
name !== 'dlsym' &&
|
|
526
|
-
name !== 'dlerror' &&
|
|
527
|
-
binding(name) !== undefined
|
|
870
|
+
symbolKind === 0 ||
|
|
871
|
+
symbolKind === 2 ||
|
|
872
|
+
symbolKind === 4 ||
|
|
873
|
+
symbolKind === 5
|
|
528
874
|
) {
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
875
|
+
index = number()
|
|
876
|
+
if (!(flags & 16) || flags & 64) name = string()
|
|
877
|
+
} else if (symbolKind === 1) {
|
|
878
|
+
const nameLen = number()
|
|
879
|
+
const nameEnd = offset + nameLen
|
|
880
|
+
if (nameEnd > end) throw new Error('invalid wasm string')
|
|
881
|
+
// skip UTF-8 decoding for local data symbols: never exported, never in functionSymbols.
|
|
882
|
+
if (flags & 16 || (flags & 3) !== 2)
|
|
883
|
+
name = rawBuf.toString('utf8', offset, nameEnd)
|
|
884
|
+
offset = nameEnd
|
|
885
|
+
if (!(flags & 16)) {
|
|
886
|
+
if ((flags & 3) !== 3) {
|
|
887
|
+
number()
|
|
888
|
+
number()
|
|
889
|
+
}
|
|
890
|
+
number()
|
|
891
|
+
if ((flags & 3) === 3) offset++
|
|
892
|
+
}
|
|
893
|
+
} else if (symbolKind === 3) number()
|
|
894
|
+
else throw new Error('unsupported wasm symbol kind')
|
|
895
|
+
if (symbolKind === 0)
|
|
896
|
+
functionSymbols[i] = name || functionImportNames[index]
|
|
897
|
+
// an undefined data symbol has no wasm import, only this linking entry.
|
|
898
|
+
if (flags & 16) {
|
|
899
|
+
if (symbolKind === 1 && names.has(name)) usedSymbols.add(name)
|
|
900
|
+
} else if ((flags & 3) !== 2 && names.has(name)) {
|
|
534
901
|
if (symbolKind !== 0 && symbolKind !== 1)
|
|
535
902
|
throw new Error(`unsupported symbol ${name}`)
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
} else {
|
|
548
|
-
rewrittenSym = alias
|
|
549
|
-
entryWriter.bytes(bytes, begin, nameStart)
|
|
550
|
-
entryWriter.string(alias)
|
|
551
|
-
entryWriter.bytes(bytes, nameEnd, offset)
|
|
552
|
-
}
|
|
553
|
-
} else {
|
|
554
|
-
entryWriter.bytes(bytes, begin, offset)
|
|
555
|
-
}
|
|
556
|
-
if (collectRewrittenSymbols) {
|
|
557
|
-
if (flags & 16) {
|
|
558
|
-
const resolved =
|
|
559
|
-
flags & 64 || symbolKind === 1
|
|
560
|
-
? rewrittenSym
|
|
561
|
-
: symbolKind === 0
|
|
562
|
-
? (functionImportNames[index] ?? '')
|
|
563
|
-
: symbolKind === 2
|
|
564
|
-
? (globalImportNames[index] ?? '')
|
|
565
|
-
: symbolKind === 4
|
|
566
|
-
? (eventImportNames[index] ?? '')
|
|
567
|
-
: symbolKind === 5
|
|
568
|
-
? (tableImportNames[index] ?? '')
|
|
569
|
-
: ''
|
|
570
|
-
if (resolved) collectRewrittenSymbols.referenced.add(resolved)
|
|
571
|
-
} else if ((flags & 3) !== 2) {
|
|
572
|
-
if (rewrittenSym) collectRewrittenSymbols.defined.add(rewrittenSym)
|
|
573
|
-
if (addedCanonical)
|
|
574
|
-
collectRewrittenSymbols.defined.add(addedCanonical)
|
|
575
|
-
}
|
|
903
|
+
const type = symbolKind === 0 ? types[functionTypes[index]!] : undefined
|
|
904
|
+
if (symbolKind === 0 && !type)
|
|
905
|
+
throw new Error(`missing type for ${name}`)
|
|
906
|
+
definitions.push({
|
|
907
|
+
name,
|
|
908
|
+
kind: symbolKind === 0 ? 'function' : 'data',
|
|
909
|
+
weak: (flags & 3) === 1,
|
|
910
|
+
exportName: (flags & 0x20) !== 0,
|
|
911
|
+
hidden: (flags & 4) !== 0,
|
|
912
|
+
type,
|
|
913
|
+
})
|
|
576
914
|
}
|
|
577
|
-
} else if (alias && !(flags & 16) && (flags & 3) !== 2) {
|
|
578
|
-
if (symbolKind !== 0 && symbolKind !== 1)
|
|
579
|
-
throw new Error(`unsupported symbol ${name}`)
|
|
580
|
-
const type = symbolKind === 0 ? types[functionTypes[index]!] : undefined
|
|
581
|
-
if (symbolKind === 0 && !type) throw new Error(`missing type for ${name}`)
|
|
582
|
-
definitions.push({
|
|
583
|
-
name,
|
|
584
|
-
kind: symbolKind === 0 ? 'function' : 'data',
|
|
585
|
-
weak: (flags & 3) === 1,
|
|
586
|
-
exportName: (flags & 0x20) !== 0,
|
|
587
|
-
hidden: (flags & 4) !== 0,
|
|
588
|
-
type,
|
|
589
|
-
})
|
|
590
|
-
}
|
|
591
|
-
}
|
|
592
|
-
if (offset !== stop) throw new Error('invalid symbol subsection')
|
|
593
|
-
if (entryWriter) {
|
|
594
|
-
const countBytes = encodeNumber(count + added)
|
|
595
|
-
const entryBuf = entryWriter.finish()
|
|
596
|
-
if (extraWriter) {
|
|
597
|
-
const extraBuf = extraWriter.finish()
|
|
598
|
-
subsections!.push({
|
|
599
|
-
kind,
|
|
600
|
-
parts: [countBytes, entryBuf, extraBuf],
|
|
601
|
-
byteLength: countBytes.length + entryBuf.length + extraBuf.length,
|
|
602
|
-
})
|
|
603
|
-
} else {
|
|
604
|
-
subsections!.push({
|
|
605
|
-
kind,
|
|
606
|
-
parts: [countBytes, entryBuf],
|
|
607
|
-
byteLength: countBytes.length + entryBuf.length,
|
|
608
|
-
})
|
|
609
915
|
}
|
|
916
|
+
if (offset !== stop) throw new Error('invalid symbol subsection')
|
|
610
917
|
}
|
|
611
|
-
|
|
612
|
-
subsections.push({
|
|
613
|
-
kind,
|
|
614
|
-
parts: [bytes.subarray(contentStart, stop)],
|
|
615
|
-
byteLength: length,
|
|
616
|
-
})
|
|
918
|
+
offset = stop
|
|
617
919
|
}
|
|
618
|
-
offset = stop
|
|
619
|
-
}
|
|
620
|
-
if (subsections) {
|
|
621
|
-
const linkingWriter = new ByteWriter(size + 128)
|
|
622
|
-
linkingWriter.string(sectionName)
|
|
623
|
-
linkingWriter.uleb(version)
|
|
624
|
-
for (const subsection of subsections) {
|
|
625
|
-
if (subsection.kind === 6 && objcLoader !== undefined) {
|
|
626
|
-
const content = subsection.parts[0]!
|
|
627
|
-
const [count, entriesStart] = readNumber(content, 0)
|
|
628
|
-
const initWriter = new ByteWriter(content.length + 16)
|
|
629
|
-
initWriter.bytes(content, 0, entriesStart)
|
|
630
|
-
let entryStart = entriesStart
|
|
631
|
-
for (let i = 0; i < count; i++) {
|
|
632
|
-
const [, symbolStart] = readNumber(content, entryStart)
|
|
633
|
-
const [symbol, entryEnd] = readNumber(content, symbolStart)
|
|
634
|
-
// load the merged objc metadata after Swift registration (0) and before user constructors (101+).
|
|
635
|
-
if (symbol === objcLoader) {
|
|
636
|
-
initWriter.uleb(1)
|
|
637
|
-
initWriter.bytes(content, symbolStart, entryEnd)
|
|
638
|
-
} else {
|
|
639
|
-
initWriter.bytes(content, entryStart, entryEnd)
|
|
640
|
-
}
|
|
641
|
-
entryStart = entryEnd
|
|
642
|
-
}
|
|
643
|
-
if (entryStart !== content.length)
|
|
644
|
-
throw new Error('invalid init functions subsection')
|
|
645
|
-
const initBuf = initWriter.finish()
|
|
646
|
-
linkingWriter.byte(subsection.kind)
|
|
647
|
-
linkingWriter.uleb(initBuf.length)
|
|
648
|
-
linkingWriter.bytes(initBuf)
|
|
649
|
-
} else {
|
|
650
|
-
linkingWriter.byte(subsection.kind)
|
|
651
|
-
linkingWriter.uleb(subsection.byteLength)
|
|
652
|
-
for (const part of subsection.parts) linkingWriter.bytes(part)
|
|
653
|
-
}
|
|
654
|
-
}
|
|
655
|
-
rewrittenSection = linkingWriter.finish()
|
|
656
920
|
}
|
|
657
921
|
} else if (!rewrite && sectionName.startsWith('reloc.')) {
|
|
658
922
|
// the analysis pass records which functions any object takes the address of.
|
|
659
923
|
number()
|
|
660
924
|
for (let count = number(); count > 0; count--) {
|
|
661
|
-
const
|
|
662
|
-
number()
|
|
663
|
-
|
|
664
|
-
if (
|
|
665
|
-
|
|
666
|
-
|
|
925
|
+
const b0 = bytes[offset]!
|
|
926
|
+
const type = b0 < 128 ? (offset++, b0) : number()
|
|
927
|
+
// skip offset
|
|
928
|
+
if (offset < end && bytes[offset]! < 128) offset++
|
|
929
|
+
else number()
|
|
930
|
+
const b2 = bytes[offset]!
|
|
931
|
+
const symIdx = b2 < 128 ? (offset++, b2) : number()
|
|
932
|
+
if (ADDEND_RELOCATIONS.has(type)) {
|
|
933
|
+
if (offset < end && bytes[offset]! < 128) offset++
|
|
934
|
+
else number()
|
|
935
|
+
}
|
|
936
|
+
if (TABLE_INDEX_RELOCATIONS.has(type)) {
|
|
937
|
+
const name = functionSymbols[symIdx]
|
|
938
|
+
if (name !== undefined) addressTaken.add(name)
|
|
939
|
+
}
|
|
667
940
|
}
|
|
668
941
|
if (offset !== end) throw new Error(`invalid relocation section ${sectionName}`)
|
|
669
942
|
}
|
|
@@ -682,9 +955,23 @@ function rewriteObject(
|
|
|
682
955
|
}
|
|
683
956
|
return {
|
|
684
957
|
bytes: sections ? Buffer.concat(sections) : bytes,
|
|
958
|
+
parts: sections,
|
|
959
|
+
scan: !rewrite
|
|
960
|
+
? {
|
|
961
|
+
importHeaderStart,
|
|
962
|
+
importStart,
|
|
963
|
+
importEnd,
|
|
964
|
+
linkingHeaderStart,
|
|
965
|
+
linkingStart,
|
|
966
|
+
linkingEnd,
|
|
967
|
+
usesDlsym,
|
|
968
|
+
usesDlerror,
|
|
969
|
+
}
|
|
970
|
+
: undefined,
|
|
685
971
|
definitions,
|
|
686
972
|
functionImports,
|
|
687
973
|
usesDlsym,
|
|
974
|
+
usesDlerror,
|
|
688
975
|
usedSymbols,
|
|
689
976
|
addressTaken: [...addressTaken],
|
|
690
977
|
}
|
|
@@ -720,6 +1007,7 @@ export function createGuestImageLedger(input: GuestImageLedgerInput): GuestImage
|
|
|
720
1007
|
aliases: Pick<ReadonlyMap<string, string>, 'get' | 'has'>
|
|
721
1008
|
usedSymbols: ReadonlySet<string>
|
|
722
1009
|
functionImports: ReadonlyMap<string, FunctionType>
|
|
1010
|
+
scan?: ObjectSectionScan
|
|
723
1011
|
}[] = []
|
|
724
1012
|
const callersUsingDlsym = new Set<number>()
|
|
725
1013
|
const addressTaken = new Set<string>()
|
|
@@ -767,6 +1055,7 @@ export function createGuestImageLedger(input: GuestImageLedgerInput): GuestImage
|
|
|
767
1055
|
aliases,
|
|
768
1056
|
usedSymbols: rewritten.usedSymbols,
|
|
769
1057
|
functionImports: rewritten.functionImports,
|
|
1058
|
+
scan: rewritten.scan,
|
|
770
1059
|
})
|
|
771
1060
|
for (const symbol of rewritten.definitions) {
|
|
772
1061
|
if (symbol.type && symbol.type.results.length > 1)
|
|
@@ -815,10 +1104,24 @@ export function createGuestImageLedger(input: GuestImageLedgerInput): GuestImage
|
|
|
815
1104
|
)
|
|
816
1105
|
}
|
|
817
1106
|
const preserveCanonical = new Set<string>()
|
|
818
|
-
|
|
1107
|
+
let definitionsByName: Map<string, GuestImageDefinition[]> | undefined
|
|
1108
|
+
const neededNames = new Set<string>()
|
|
1109
|
+
if (symbolIndices.has('__cxa_throw')) neededNames.add('__cxa_throw')
|
|
1110
|
+
for (const binding of input.directBindings ?? []) neededNames.add(binding.symbol)
|
|
1111
|
+
if (input.sdkWeakReferences) {
|
|
1112
|
+
definitionsByName = Map.groupBy(definitions, (definition) => definition.name)
|
|
1113
|
+
} else if (neededNames.size > 0) {
|
|
1114
|
+
definitionsByName = new Map()
|
|
1115
|
+
for (const definition of definitions) {
|
|
1116
|
+
if (!neededNames.has(definition.name)) continue
|
|
1117
|
+
let list = definitionsByName.get(definition.name)
|
|
1118
|
+
if (!list) definitionsByName.set(definition.name, (list = []))
|
|
1119
|
+
list.push(definition)
|
|
1120
|
+
}
|
|
1121
|
+
}
|
|
819
1122
|
if (symbolIndices.has('__cxa_throw')) {
|
|
820
1123
|
const name = '__cxa_throw'
|
|
821
|
-
const matching = definitionsByName
|
|
1124
|
+
const matching = definitionsByName?.get(name) ?? []
|
|
822
1125
|
if (
|
|
823
1126
|
new Set(matching.map((definition) => definition.image)).size >= 2 &&
|
|
824
1127
|
matching.some((definition) => definition.weak)
|
|
@@ -934,7 +1237,7 @@ export function createGuestImageLedger(input: GuestImageLedgerInput): GuestImage
|
|
|
934
1237
|
const unitsByPath = new Map(units.map((unit) => [unit.object.path, unit]))
|
|
935
1238
|
for (const binding of input.directBindings ?? []) {
|
|
936
1239
|
const unit = unitsByPath.get(binding.object)
|
|
937
|
-
const matches = (definitionsByName
|
|
1240
|
+
const matches = (definitionsByName?.get(binding.symbol) ?? []).filter(
|
|
938
1241
|
(definition) =>
|
|
939
1242
|
definition.object === binding.providerObject &&
|
|
940
1243
|
definition.image === binding.providerImage,
|
|
@@ -974,7 +1277,7 @@ export function createGuestImageLedger(input: GuestImageLedgerInput): GuestImage
|
|
|
974
1277
|
}
|
|
975
1278
|
return value
|
|
976
1279
|
}
|
|
977
|
-
for (const [name, definitions] of definitionsByName) {
|
|
1280
|
+
for (const [name, definitions] of definitionsByName!) {
|
|
978
1281
|
if (preserveCanonical.has(name)) continue
|
|
979
1282
|
const strong = definitions.filter((definition) => !definition.weak)
|
|
980
1283
|
const candidates = strong.length ? strong : definitions
|
|
@@ -992,7 +1295,7 @@ export function createGuestImageLedger(input: GuestImageLedgerInput): GuestImage
|
|
|
992
1295
|
const appSymbols = input.registerAppTables
|
|
993
1296
|
? { referenced: new Set<string>(), defined: new Set<string>() }
|
|
994
1297
|
: undefined
|
|
995
|
-
const objects = units.map(({ image, object, aliases, functionImports }) => {
|
|
1298
|
+
const objects = units.map(({ image, object, aliases, functionImports, scan }) => {
|
|
996
1299
|
const shared = sharedLookupByImage.get(image)!
|
|
997
1300
|
const own = startup.get(object.path)
|
|
998
1301
|
const bindings = new Map(own)
|
|
@@ -1026,23 +1329,24 @@ export function createGuestImageLedger(input: GuestImageLedgerInput): GuestImage
|
|
|
1026
1329
|
}
|
|
1027
1330
|
bindings.set(name, wrapper.symbol)
|
|
1028
1331
|
}
|
|
1332
|
+
const rewritten = rewriteObject(
|
|
1333
|
+
object.bytes,
|
|
1334
|
+
aliases,
|
|
1335
|
+
`__peach_dlsym_caller_${image}`,
|
|
1336
|
+
binding,
|
|
1337
|
+
object.instrumentDynamicLookups,
|
|
1338
|
+
preserveCanonical,
|
|
1339
|
+
{
|
|
1340
|
+
sdkProduct: object.sdkProduct,
|
|
1341
|
+
collectRewrittenSymbols: object.sdkProduct === undefined ? appSymbols : undefined,
|
|
1342
|
+
scan,
|
|
1343
|
+
},
|
|
1344
|
+
)
|
|
1029
1345
|
return {
|
|
1030
1346
|
path: object.path,
|
|
1031
1347
|
instrumentDynamicLookups: object.instrumentDynamicLookups,
|
|
1032
1348
|
sdkProduct: object.sdkProduct,
|
|
1033
|
-
bytes:
|
|
1034
|
-
object.bytes,
|
|
1035
|
-
aliases,
|
|
1036
|
-
`__peach_dlsym_caller_${image}`,
|
|
1037
|
-
binding,
|
|
1038
|
-
object.instrumentDynamicLookups,
|
|
1039
|
-
preserveCanonical,
|
|
1040
|
-
{
|
|
1041
|
-
sdkProduct: object.sdkProduct,
|
|
1042
|
-
collectRewrittenSymbols:
|
|
1043
|
-
object.sdkProduct === undefined ? appSymbols : undefined,
|
|
1044
|
-
},
|
|
1045
|
-
).bytes,
|
|
1349
|
+
bytes: rewritten.bytes,
|
|
1046
1350
|
}
|
|
1047
1351
|
})
|
|
1048
1352
|
const exported = new Map<string, GuestImageDefinition>()
|
|
@@ -1254,12 +1558,7 @@ export function sdkGuestCallerId(product: string): number {
|
|
|
1254
1558
|
}
|
|
1255
1559
|
|
|
1256
1560
|
export function sdkGuestSymbolName(product: string, name: string): string {
|
|
1257
|
-
|
|
1258
|
-
.update(product)
|
|
1259
|
-
.update('\0')
|
|
1260
|
-
.update(name)
|
|
1261
|
-
.digest('hex')
|
|
1262
|
-
return `__peach_sdk_symbol_${digest.slice(0, 32)}`
|
|
1561
|
+
return `__peach_sdk_symbol_${sha256PairHex(product, name).slice(0, 32)}`
|
|
1263
1562
|
}
|
|
1264
1563
|
|
|
1265
1564
|
export function rewriteSdkGuestLookup(bytes: Uint8Array, callerId: number): Uint8Array {
|