-
Notifications
You must be signed in to change notification settings - Fork 7
/
strip.py
executable file
·38 lines (28 loc) · 887 Bytes
/
strip.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
#!/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 fw import hashseg
from fw.elf import Elf
def _strip_elf(elf: Elf, out: Path):
print(f"Before: {elf}")
hashseg.drop(elf)
elf.update()
print(f"After: {elf}")
with open(out, 'wb') as f:
elf.save(f)
parser = argparse.ArgumentParser(description="""
Strip ELF file (remove section header table and drop hash segment).
""")
parser.add_argument('elf', type=argparse.FileType('rb'), help="ELF image to strip")
parser.add_argument('-o', '--output', type=Path, help="Output file")
args = parser.parse_args()
with args.elf:
elf = Elf.parse(args.elf.read())
out = args.output
if not out:
elf_path = Path(args.elf.name)
out = elf_path.with_name(elf_path.stem + "-stripped.mbn")
_strip_elf(elf, out)