aboutsummaryrefslogtreecommitdiff
path: root/rpathtool.py
blob: 42fda213d69cf96b23aa5b79495406e65e0ac919 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/python3 -tt

# Copyright 2013 Jussi Pakkanen

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

#     http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys, struct

SHT_STRTAB = 3
DT_NEEDED = 1
DT_RPATH = 15
DT_STRTAB = 5
DT_SONAME = 14

class DynamicEntry():
    def __init__(self, ifile):
        self.d_tag = struct.unpack('Q', ifile.read(8))[0];
        self.val = struct.unpack('Q', ifile.read(8))[0];

class SectionHeader():
    def __init__(self, ifile):
#Elf64_Word
        self.sh_name = struct.unpack('I', ifile.read(4))[0];
#Elf64_Word
        self.sh_type = struct.unpack('I', ifile.read(4))[0]
#Elf64_Xword
        self.sh_flags = struct.unpack('Q', ifile.read(8))[0];
#Elf64_Addr
        self.sh_addr = struct.unpack('Q', ifile.read(8))[0];
#Elf64_Off
        self.sh_offset = struct.unpack('Q', ifile.read(8))[0]
#Elf64_Xword
        self.sh_size = struct.unpack('Q', ifile.read(8))[0];
#Elf64_Word
        self.sh_link = struct.unpack('I', ifile.read(4))[0];
#Elf64_Word
        self.sh_info = struct.unpack('I', ifile.read(4))[0];
#Elf64_Xword
        self.sh_addralign = struct.unpack('Q', ifile.read(8))[0];
#Elf64_Xword
        self.sh_entsize = struct.unpack('Q', ifile.read(8))[0];

class Elf():

    def __init__(self, bfile):
        self.bfile = bfile
        self.bf = open(bfile, 'rb')
        self.parse_header()
        self.parse_sections()
        self.parse_dynamic()

    def parse_header(self):
        self.e_ident = struct.unpack('16s', self.bf.read(16))[0]
        if self.e_ident[1:4] != b'ELF':
            raise RuntimeError('File "%s" is not an ELF file.' % self.bfile)
        self.e_type = struct.unpack('h', self.bf.read(2))[0]
        self.e_machine = struct.unpack('h', self.bf.read(2))[0]
        self.e_version = struct.unpack('i', self.bf.read(4))[0]
        self.e_entry = struct.unpack('Q', self.bf.read(8))[0]
        self.e_phoff = struct.unpack('Q', self.bf.read(8))[0]
        self.e_shoff = struct.unpack('Q', self.bf.read(8))[0]
        self.e_flags = struct.unpack('i', self.bf.read(4))[0]
        self.e_ehsize = struct.unpack('h', self.bf.read(2))[0]
        self.e_phentsize = struct.unpack('h', self.bf.read(2))[0]
        self.e_phnum = struct.unpack('h', self.bf.read(2))[0]
        self.e_shentsize = struct.unpack('h', self.bf.read(2))[0]
        self.e_shnum = struct.unpack('h', self.bf.read(2))[0]
        self.e_shstrndx = struct.unpack('h', self.bf.read(2))[0]

    def parse_sections(self):
        self.bf.seek(self.e_shoff)
        self.sections = []
        for i in range(self.e_shnum):
            self.sections.append(SectionHeader(self.bf))

    def read_str(self):
        arr = []
        x = self.bf.read(1)
        while x != b'\0':
            arr.append(x)
            x = self.bf.read(1)
            if x == b'':
                raise RuntimeError('Tried to read past the end of the file')
        return b''.join(arr)

    def find_section(self, target_name):
        section_names = self.sections[self.e_shstrndx]
        for i in self.sections:
            self.bf.seek(section_names.sh_offset + i.sh_name)
            name = self.read_str()
            if name == target_name:
                return i

    def parse_dynamic(self):
        sec = self.find_section(b'.dynamic')
        self.dynamic = []
        self.bf.seek(sec.sh_offset)
        while True:
            e = DynamicEntry(self.bf)
            self.dynamic.append(e)
            if e.d_tag == 0:
                break

    def print_section_names(self):
        section_names = self.sections[self.e_shstrndx]
        for i in self.sections:
            self.bf.seek(section_names.sh_offset + i.sh_name)
            name = self.read_str()
            print(name.decode())

    def print_soname(self):
        soname = None
        strtab = None
        for i in self.dynamic:
            if i.d_tag == DT_SONAME:
                soname = i
            if i.d_tag == DT_STRTAB:
                strtab = i
        self.bf.seek(strtab.val + soname.val)
        print(self.read_str())

    def print_deps(self):
        sec = self.find_section(b'.dynstr')
        deps = []
        for i in self.dynamic:
            if i.d_tag == DT_NEEDED:
                deps.append(i)
        for i in deps:
            offset = sec.sh_offset + i.val
            self.bf.seek(offset)
            name = self.read_str()
            print(name)

if __name__ == '__main__':
    if len(sys.argv) != 2:
        print('%s: <binary file>' % sys.argv[0])
        exit(1)
    e = Elf(sys.argv[1])
    e.print_deps()