Per Åstrand | 43a8409 | 2021-03-26 09:01:38 +0100 | [diff] [blame] | 1 | #!/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 | |
| 21 | import argparse |
| 22 | import os |
| 23 | import re |
| 24 | import subprocess |
| 25 | import sys |
| 26 | import elftools.elf.elffile as elffile |
| 27 | |
| 28 | def 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: |
Per Åstrand | b47ec63 | 2021-05-07 08:55:10 +0200 | [diff] [blame] | 34 | os.makedirs(args.output, exist_ok=True) |
Per Åstrand | 43a8409 | 2021-03-26 09:01:38 +0100 | [diff] [blame] | 35 | out = os.path.join( |
| 36 | args.output, |
| 37 | '%s_0x%08x.bin' % (os.path.basename(args.input), segment['p_paddr'])) |
| 38 | with open(out, 'wb') as of: |
| 39 | of.write(segment.data()) |
| 40 | |
| 41 | if __name__ == '__main__': |
| 42 | parser = argparse.ArgumentParser(description='Generate binaries from ELF file') |
| 43 | parser.add_argument('-o', '--output', default='.', required=False, help='Output directory for binaries') |
| 44 | parser.add_argument('input', metavar='inputfile', type=str, help='ELF file to extract binaries from') |
| 45 | |
| 46 | args = parser.parse_args() |
| 47 | |
| 48 | generate_binaries(args) |