Elf64ProgramHeaderTable::findLoad()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 9
rs 10
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\Structure\Elf64;
15
16
use PhpProfiler\Lib\Integer\UInt64;
17
18
final class Elf64ProgramHeaderTable
19
{
20
    /** @var Elf64ProgramHeaderEntry[] */
21
    private array $entries;
22
23
    public function __construct(Elf64ProgramHeaderEntry ...$entries)
24
    {
25
        $this->entries = $entries;
26
    }
27
28
    /**
29
     * @return Elf64ProgramHeaderEntry[]
30
     */
31
    public function findLoad(): array
32
    {
33
        $result = [];
34
        foreach ($this->entries as $entry) {
35
            if ($entry->isLoad()) {
36
                $result[] = $entry;
37
            }
38
        }
39
        return $result;
40
    }
41
42
    public function findBaseAddress(): UInt64
43
    {
44
        $base_address = new UInt64(0, 0);
45
        foreach ($this->findLoad() as $pt_load) {
46
            if ($pt_load->p_vaddr->hi < $base_address->hi) {
47
                $base_address = $pt_load->p_vaddr;
48
            } elseif ($pt_load->p_vaddr->hi === $base_address->hi) {
49
                if ($pt_load->p_vaddr->lo < $base_address->lo) {
50
                    $base_address = $pt_load->p_vaddr;
51
                }
52
            }
53
        }
54
        return $base_address;
55
    }
56
57
    /**
58
     * @return Elf64ProgramHeaderEntry[]
59
     */
60
    public function findDynamic(): array
61
    {
62
        $result = [];
63
        foreach ($this->entries as $entry) {
64
            if ($entry->isDynamic()) {
65
                $result[] = $entry;
66
            }
67
        }
68
        return $result;
69
    }
70
}
71