|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the reliforp/reli-prof package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) sji <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Reli\Lib\Elf\SymbolResolver; |
|
15
|
|
|
|
|
16
|
|
|
use Reli\Lib\ByteStream\ByteReaderInterface; |
|
17
|
|
|
use Reli\Lib\Elf\Parser\Elf64Parser; |
|
18
|
|
|
use Reli\Lib\Elf\Parser\ElfParserException; |
|
19
|
|
|
use Reli\Lib\Elf\Structure\Elf64\Elf64GnuHashTable; |
|
20
|
|
|
use Reli\Lib\Elf\Structure\Elf64\Elf64StringTable; |
|
21
|
|
|
use Reli\Lib\Elf\Structure\Elf64\Elf64SymbolTable; |
|
22
|
|
|
use Reli\Lib\Elf\Structure\Elf64\Elf64SymbolTableEntry; |
|
23
|
|
|
|
|
24
|
|
|
final class Elf64DynamicSymbolResolver implements Elf64SymbolResolver |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @throws ElfParserException |
|
28
|
|
|
*/ |
|
29
|
|
|
public static function load(Elf64Parser $parser, ByteReaderInterface $php_binary): self |
|
30
|
|
|
{ |
|
31
|
|
|
$elf_header = $parser->parseElfHeader($php_binary); |
|
32
|
|
|
$elf_program_header = $parser->parseProgramHeader($php_binary, $elf_header); |
|
33
|
|
|
$elf_dynamic_array = $parser->parseDynamicStructureArray($php_binary, $elf_program_header->findDynamic()[0]); |
|
34
|
|
|
$elf_string_table = $parser->parseStringTable($php_binary, $elf_dynamic_array); |
|
35
|
|
|
$elf_gnu_hash_table = $parser->parseGnuHashTable($php_binary, $elf_dynamic_array); |
|
36
|
|
|
if (is_null($elf_gnu_hash_table)) { |
|
37
|
|
|
throw new ElfParserException('cannot find gnu hash table'); |
|
38
|
|
|
} |
|
39
|
|
|
$elf_symbol_table = $parser->parseSymbolTableFromDynamic( |
|
40
|
|
|
$php_binary, |
|
41
|
|
|
$elf_dynamic_array, |
|
42
|
|
|
$elf_gnu_hash_table->getNumberOfSymbols() |
|
43
|
|
|
); |
|
44
|
|
|
return new self( |
|
45
|
|
|
$elf_symbol_table, |
|
46
|
|
|
$elf_gnu_hash_table, |
|
47
|
|
|
$elf_string_table |
|
48
|
|
|
); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function __construct( |
|
52
|
|
|
private Elf64SymbolTable $symbol_table, |
|
53
|
|
|
private Elf64GnuHashTable $hash_table, |
|
54
|
|
|
private Elf64StringTable $string_table, |
|
55
|
|
|
) { |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function resolve(string $symbol_name): Elf64SymbolTableEntry |
|
59
|
|
|
{ |
|
60
|
|
|
$index = $this->hash_table->lookup($symbol_name, function (string $name, int $index) { |
|
61
|
|
|
$symbol = $this->symbol_table->lookup($index); |
|
62
|
|
|
return $name === $this->string_table->lookup($symbol->st_name); |
|
63
|
|
|
}); |
|
64
|
|
|
return $this->symbol_table->lookup($index); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|