-
Notifications
You must be signed in to change notification settings - Fork 8
/
merge.py
executable file
·40 lines (30 loc) · 1013 Bytes
/
merge.py
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
#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-2.0-only
# Copyright (C) 2022 Stephan Gerhold
from __future__ import annotations
import argparse
from pathlib import Path
from typing import List
from fw.elf import Elf
def _merge_elfs(elfs: List[Elf], out: Path):
print(f"Input:{elfs}")
main = elfs[0]
for elf in elfs[1:]:
main.phdrs += elf.phdrs
main.update()
print(f"Merged: {main}")
with open(out, 'wb') as f:
main.save(f)
parser = argparse.ArgumentParser(description="""
Merge program headers and segments from multiple ELF files into a single ELF image.
The ELF header is taken from the first ELF image, then program headers
and segments are appended from all others.
""")
parser.add_argument('elf', type=argparse.FileType('rb'), help="ELF images", nargs='+')
parser.add_argument('-o', '--output', type=Path, help="Output file", required=True)
args = parser.parse_args()
elfs = []
for elf in args.elf:
with elf:
elfs.append(Elf.parse(elf.read()))
_merge_elfs(elfs, args.output)