Passed
Push — master ( 505eba...bf554b )
by Shinji
01:14
created

createLinearScanResolverFromPath()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 13
nc 3
nop 1
dl 0
loc 17
rs 9.8333
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 Elf64SymbolResolverCreator implements SymbolResolverCreatorInterface
27
{
28
    private FileReaderInterface $file_reader;
29
30
    /**
31
     * SymbolResolverCreator constructor.
32
     * @param FileReaderInterface $file_reader
33
     */
34
    public function __construct(FileReaderInterface $file_reader)
35
    {
36
        $this->file_reader = $file_reader;
37
    }
38
39
    /**
40
     * @param string $path
41
     * @return Elf64LinearScanSymbolResolver
42
     * @throws ElfParserException
43
     */
44
    public function createLinearScanResolverFromPath(string $path): Elf64LinearScanSymbolResolver
45
    {
46
        $binary = new StringByteReader($this->file_reader->readAll($path));
47
        $parser = new Elf64Parser(new BinaryReader());
48
        $elf_header = $parser->parseElfHeader($binary);
49
        $section_header = $parser->parseSectionHeader($binary, $elf_header);
50
        $symbol_table_section_header_entry = $section_header->findSymbolTableEntry();
51
        $string_table_section_header_entry = $section_header->findStringTableEntry();
52
        if (is_null($symbol_table_section_header_entry)) {
53
            throw new ElfParserException('cannot find symbol table from section header table');
54
        }
55
        if (is_null($string_table_section_header_entry)) {
56
            throw new ElfParserException('cannot find string table from section header table');
57
        }
58
        $symbol_table = $parser->parseSymbolTableFromSectionHeader($binary, $symbol_table_section_header_entry);
59
        $string_table = $parser->parseStringTableFromSectionHeader($binary, $string_table_section_header_entry);
60
        return new Elf64LinearScanSymbolResolver($symbol_table, $string_table);
61
    }
62
63
    /**
64
     * @param string $path
65
     * @return Elf64DynamicSymbolResolver
66
     * @throws ElfParserException
67
     */
68
    public function createDynamicResolverFromPath(string $path): Elf64DynamicSymbolResolver
69
    {
70
        $binary = new StringByteReader($this->file_reader->readAll($path));
71
        $parser = new Elf64Parser(new BinaryReader());
72
        return Elf64DynamicSymbolResolver::load($parser, $binary);
73
    }
74
}
75