| 1 |
// Copyright 2017 Lovell Fuller and others. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
'use strict'; |
| 5 |
|
| 6 |
const interpreterPath = (elf) => { |
| 7 |
if (elf.length < 64) { |
| 8 |
return null; |
| 9 |
} |
| 10 |
if (elf.readUInt32BE(0) !== 0x7F454C46) { |
| 11 |
// Unexpected magic bytes |
| 12 |
return null; |
| 13 |
} |
| 14 |
if (elf.readUInt8(4) !== 2) { |
| 15 |
// Not a 64-bit ELF |
| 16 |
return null; |
| 17 |
} |
| 18 |
if (elf.readUInt8(5) !== 1) { |
| 19 |
// Not little-endian |
| 20 |
return null; |
| 21 |
} |
| 22 |
const offset = elf.readUInt32LE(32); |
| 23 |
const size = elf.readUInt16LE(54); |
| 24 |
const count = elf.readUInt16LE(56); |
| 25 |
for (let i = 0; i < count; i++) { |
| 26 |
const headerOffset = offset + (i * size); |
| 27 |
const type = elf.readUInt32LE(headerOffset); |
| 28 |
if (type === 3) { |
| 29 |
const fileOffset = elf.readUInt32LE(headerOffset + 8); |
| 30 |
const fileSize = elf.readUInt32LE(headerOffset + 32); |
| 31 |
return elf.subarray(fileOffset, fileOffset + fileSize).toString().replace(/\0.*$/g, ''); |
| 32 |
} |
| 33 |
} |
| 34 |
return null; |
| 35 |
}; |
| 36 |
|
| 37 |
module.exports = { |
| 38 |
interpreterPath |
| 39 |
}; |