blob: f206cf86fcba4d164c2652cff5fb91f138b31385 [file] [log] [blame]
Per Åstrand43a84092021-03-26 09:01:38 +01001#!/usr/bin/env python3
2
3#
4# Copyright (c) 2021 Arm Limited. All rights reserved.
5#
6# SPDX-License-Identifier: Apache-2.0
7#
8# Licensed under the Apache License, Version 2.0 (the License); you may
9# not use this file except in compliance with the License.
10# You may obtain a copy of the License at
11#
12# www.apache.org/licenses/LICENSE-2.0
13#
14# Unless required by applicable law or agreed to in writing, software
15# distributed under the License is distributed on an AS IS BASIS, WITHOUT
16# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17# See the License for the specific language governing permissions and
18# limitations under the License.
19#
20
21import argparse
22import os
23import re
24import subprocess
25import sys
26import elftools.elf.elffile as elffile
27
28def generate_binaries(args):
29 load_segments = [];
30 with open(args.input, 'rb') as f:
31 elf = elffile.ELFFile(f)
32 for segment in elf.iter_segments():
33 if segment['p_type'] == 'PT_LOAD' and segment['p_filesz'] > 0:
34 if not os.path.exists(args.output):
35 os.makedirs(args.output)
36 out = os.path.join(
37 args.output,
38 '%s_0x%08x.bin' % (os.path.basename(args.input), segment['p_paddr']))
39 with open(out, 'wb') as of:
40 of.write(segment.data())
41
42if __name__ == '__main__':
43 parser = argparse.ArgumentParser(description='Generate binaries from ELF file')
44 parser.add_argument('-o', '--output', default='.', required=False, help='Output directory for binaries')
45 parser.add_argument('input', metavar='inputfile', type=str, help='ELF file to extract binaries from')
46
47 args = parser.parse_args()
48
49 generate_binaries(args)