Passed
Push — master ( a5c131...325eda )
by Shinji
01:37
created

SymbolResolverCreator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of the sj-i/php-profiler 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 PhpProfiler\Lib\Elf\SymbolResolver;
15
16
use PhpProfiler\Lib\Binary\BinaryReader;
17
use PhpProfiler\Lib\Binary\StringByteReader;
18
use PhpProfiler\Lib\Elf\Parser\Elf64Parser;
19
use PhpProfiler\Lib\Elf\Parser\ElfParserException;
20
use PhpProfiler\Lib\File\FileReaderInterface;
21
22
/**
23
 * Class SymbolResolverCreator
24
 * @package PhpProfiler\Lib\Elf
25
 */
26
final class SymbolResolverCreator
27
{
28
    /**
29
     * @var FileReaderInterface
30
     */
31
    private FileReaderInterface $file_reader;
32
33
    /**
34
     * SymbolResolverCreator constructor.
35
     * @param FileReaderInterface $file_reader
36
     */
37
    public function __construct(FileReaderInterface $file_reader)
38
    {
39
        $this->file_reader = $file_reader;
40
    }
41
42
    /**
43
     * @param string $path
44
     * @return Elf64LinearScanSymbolResolver
45
     * @throws ElfParserException
46
     */
47
    public function createLinearScanResolverFromPath(string $path): Elf64LinearScanSymbolResolver
48
    {
49
        $binary = new StringByteReader($this->file_reader->readAll($path));
50
        $parser = new Elf64Parser(new BinaryReader());
51
        $elf_header = $parser->parseElfHeader($binary);
52
        $section_header = $parser->parseSectionHeader($binary, $elf_header);
53
        $symbol_table_section_header_entry = $section_header->findSymbolTableEntry();
54
        $string_table_section_header_entry = $section_header->findStringTableEntry();
55
        if (is_null($symbol_table_section_header_entry)) {
56
            throw new ElfParserException('cannot find symbol table from section header table');
57
        }
58
        if (is_null($string_table_section_header_entry)) {
59
            throw new ElfParserException('cannot find string table from section header table');
60
        }
61
        $symbol_table = $parser->parseSymbolTableFromSectionHeader($binary, $symbol_table_section_header_entry);
62
        $string_table = $parser->parseStringTableFromSectionHeader($binary, $string_table_section_header_entry);
63
        return new Elf64LinearScanSymbolResolver($symbol_table, $string_table);
64
    }
65
66
    /**
67
     * @param string $path
68
     * @return Elf64DynamicSymbolResolver
69
     * @throws ElfParserException
70
     */
71
    public function createDynamicResolverFromPath(string $path): Elf64DynamicSymbolResolver
72
    {
73
        $binary = new StringByteReader($this->file_reader->readAll($path));
74
        $parser = new Elf64Parser(new BinaryReader());
75
        return Elf64DynamicSymbolResolver::load($parser, $binary);
76
    }
77
}
78