summaryrefslogtreecommitdiff
path: root/golden/extract_lsdump.py
blob: fd42be622a3dee5cab67ba2b41181ff323cdb580 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#!/usr/bin/env python
#
# Copyright (C) 2018 The Android Open Source Project
#
# 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 argparse
import gzip
import json
import os
import zipfile


class LsdumpError(Exception):
    """The exception raised by ParseLsdumpFile."""
    pass


class AttrDict(dict):
    """A dictionary with attribute accessors."""

    def __getattr__(self, key):
        """Returns self[key]."""
        try:
            return self[key]
        except KeyError:
            raise AttributeError('Failed to get attribute: %s' % key)

    def __setattr__(self, key, value):
        """Assigns value to self[key]."""
        self[key] = value


def _GetTypeSymbol(record_type):
    """Gets the mangled name of a record type.

    Before Android R, unique_id was mangled name starting with "_ZTS".
    linker_set_key was unmangled.
    Since Android R, unique_id has been removed, and linker_set_key has
    been changed to mangled name starting with "_ZTI".

    Args:
        record_type: An AttrDict, a record type in lsdump.

    Returns:
        A string, the mangled name starting with "_ZTI".
    """
    if "unique_id" in record_type:
        return record_type["unique_id"].replace("_ZTS", "_ZTI", 1)
    return record_type["linker_set_key"]


def _OpenFileOrGzipped(file_name):
    """Opens a file that is either in gzip or uncompressed format.

    If file_name ends with '.gz' then return gzip.open(file_name, 'rb'),
    else return open(file_name, 'rb').

    Args:
        file_name: The file name to open.

    Returns:
        A file object.

    Raises:
        IOError if fails to open.
    """
    if file_name.endswith('.gz'):
        return gzip.open(file_name, 'rb')
    return open(file_name, 'rb')


def _ConsumeOffset(tok, beg=0):
    """Consumes a <offset-number> in a thunk symbol."""
    pos = tok.find('_', beg) + 1
    return tok[:pos], tok[pos:]


def _ConsumeCallOffset(tok):
    """Consumes a <call-offset> in a thunk symbol."""
    if tok[:1] == 'h':
        lhs, rhs = _ConsumeOffset(tok, 1)
    elif tok[:1] == 'v':
        lhs, rhs = _ConsumeOffset(tok, 1)
        lhs2, rhs = _ConsumeOffset(rhs)
        if lhs and lhs2:
            lhs = lhs + lhs2
        else:
            lhs, rhs = '', tok
    else:
        lhs, rhs = '', tok
    return lhs, rhs


def _FindThunkTarget(name):
    """Finds thunk symbol's target function.

    <thunk-symbol> ::= _ZT <call-offset> <base-encoding>
                     | _ZTc <call-offset> <call-offset> <base-encoding>
    <call-offset>  ::= h <nv-offset>
                     | v <v-offset>
    <nv-offset>    ::= <offset-number> _
    <v-offset>     ::= <offset-number> _ <offset-number> _

    Args:
        name: A string, the symbol name to resolve.

    Returns:
        A string, symbol name of the nominal target function.
        The input symbol name if it is not a thunk symbol.
    """
    if name.startswith('_ZTh') or name.startswith('_ZTv'):
        lhs, rhs = _ConsumeCallOffset(name[len('_ZT'):])
        if lhs:
            return '_Z' + rhs
    if name.startswith('_ZTc'):
        lhs, rhs = _ConsumeCallOffset(name[len('_ZTc'):])
        lhs2, rhs = _ConsumeCallOffset(rhs)
        if lhs and lhs2:
            return '_Z' + rhs
    return name


def _FilterElfFunctions(lsdump):
    """Finds exported functions and thunks in lsdump.

    Args:
        lsdump: An AttrDict object containing the lsdump.

    Yields:
        The AttrDict objects in lsdump.elf_functions.
    """
    functions = {function.linker_set_key for function in lsdump.functions}

    for elf_function in lsdump.elf_functions:
        if elf_function.name in functions:
            yield elf_function
        elif _FindThunkTarget(elf_function.name) in functions:
            yield elf_function


def _FilterElfObjects(lsdump):
    """Finds exported variables, type info, and vtables in lsdump.

    Args:
        lsdump: An AttrDict object containing the lsdump.

    Yields:
        The AttrDict objects in lsdump.elf_objects.
    """
    global_vars = {global_var.linker_set_key
                   for global_var in lsdump.global_vars}
    record_names = {_GetTypeSymbol(record_type)[len('_ZTI'):]
                    for record_type in lsdump.record_types}

    for elf_object in lsdump.elf_objects:
        name = elf_object.name
        if name in global_vars:
            yield elf_object
        elif (name[:len('_ZTI')] in {'_ZTV', '_ZTT', '_ZTI', '_ZTS'} and
                name[len('_ZTI'):] in record_names):
            yield elf_object


def _ParseSymbolsFromLsdump(lsdump, output_dump):
    """Parses symbols from an lsdump.

    Args:
        lsdump: An AttrDict object containing the lsdump.
        output_dump: An AttrDict object containing the output.
    """
    output_dump.elf_functions = list(_FilterElfFunctions(lsdump))
    output_dump.elf_objects = list(_FilterElfObjects(lsdump))


def _ParseVtablesFromLsdump(lsdump, output_dump):
    """Parses vtables from an lsdump.

    Args:
        lsdump: An AttrDict object containing the lsdump.
        output_dump: An AttrDict object containing the output.
    """
    vtable_symbols = {elf_object.name for elf_object in lsdump.elf_objects
                      if elf_object.name.startswith('_ZTV')}

    output_dump.record_types = []
    for lsdump_record_type in lsdump.record_types:
        type_symbol = _GetTypeSymbol(lsdump_record_type)
        vtable_symbol = '_ZTV' + type_symbol[len('_ZTI'):]
        if vtable_symbol not in vtable_symbols:
            continue
        record_type = AttrDict()
        record_type.linker_set_key = type_symbol
        record_type.vtable_components = lsdump_record_type.vtable_components
        output_dump.record_types.append(record_type)


def _ParseLsdumpFileObject(lsdump_file):
    """Converts an lsdump file to a dump object.

    Args:
        lsdump_file: The file object of the input lsdump.

    Returns:
        The AttrDict object converted from the lsdump file.

    Raises:
        LsdumpError if fails to create the dump file.
    """
    try:
        lsdump = json.load(lsdump_file, object_hook=AttrDict)
    except ValueError:
        raise LsdumpError(e)

    try:
        output_dump = AttrDict()
        _ParseVtablesFromLsdump(lsdump, output_dump)
        _ParseSymbolsFromLsdump(lsdump, output_dump)
        return output_dump
    except AttributeError as e:
        raise LsdumpError(e)


def _ParseLsdumpZipFile(lsdump_zip, output_zip):
    """Converts zipped lsdump files to the dump files for ABI test."""
    for name in lsdump_zip.namelist():
        if name.endswith(".lsdump"):
            with lsdump_zip.open(name, mode="r") as lsdump_file:
                output_dump = _ParseLsdumpFileObject(lsdump_file)
            output_str = json.dumps(output_dump, indent=1,
                                    separators=(',', ':'))
            output_zip.writestr(name, output_str)


def ParseLsdumpFile(input_path, output_path):
    """Converts an lsdump file to a dump file for the ABI test.

    Args:
        input_path: The path to the (gzipped) lsdump file.
        output_path: The path to the output dump file.

    Raises:
        LsdumpError if fails to create the dump file.
    """
    abs_output_path = os.path.abspath(output_path)
    abs_output_dir = os.path.dirname(abs_output_path)

    try:
        if abs_output_dir and not os.path.exists(abs_output_dir):
            os.makedirs(abs_output_dir)

        with _OpenFileOrGzipped(input_path) as lsdump_file:
            output_dump = _ParseLsdumpFileObject(lsdump_file)
        with open(output_path, 'wb') as output_file:
            json.dump(output_dump, output_file,
                      indent=1, separators=(',', ':'))
    except (IOError, OSError) as e:
        raise LsdumpError(e)


def main():
    arg_parser = argparse.ArgumentParser(
        description='This script converts an lsdump file to a dump file for '
                    'the ABI test in VTS.')
    arg_parser.add_argument('input_path',
                            help='input lsdump file path.')
    arg_parser.add_argument('output_path',
                            help='output dump file path.')
    args = arg_parser.parse_args()

    # TODO(b/150663999): Remove this when the build system is able to process
    #                    the large file group.
    if zipfile.is_zipfile(args.input_path):
        with zipfile.ZipFile(args.input_path, mode='r') as input_zip:
            # The zip will be added to a Python package. It is not necessary
            # to reduce the file size.
            with zipfile.ZipFile(args.output_path, mode='w',
                                 compression=zipfile.ZIP_STORED) as output_zip:
                _ParseLsdumpZipFile(input_zip, output_zip)
        exit(0)

    try:
        ParseLsdumpFile(args.input_path, args.output_path)
    except LsdumpError as e:
        print(e)
        exit(1)


if __name__ == '__main__':
    main()