Skip to content

Instantly share code, notes, and snippets.

@domenukk
Forked from ssvb/bin2elf.sh
Last active September 28, 2024 00:46
Show Gist options
  • Save domenukk/c6b0603e39b60807bb8a714a72ff6e6b to your computer and use it in GitHub Desktop.
Save domenukk/c6b0603e39b60807bb8a714a72ff6e6b to your computer and use it in GitHub Desktop.
Convert a memory dump/raw binary image into an ELF file
#!/bin/sh
# Convert a raw binary image into an ELF file suitable for loading into a disassembler
#
# Usage: bin2elf input_binary_file output_elf_file base_address
cat > raw$$.ld <<EOF
SECTIONS
{
EOF
echo " . = $3;" >> raw$$.ld
cat >> raw$$.ld <<EOF
.text : { *(.text) }
}
EOF
CROSS_PREFIX=arm-none-eabi-
${CROSS_PREFIX}ld -b binary -r -o raw$$.elf $1
${CROSS_PREFIX}objcopy --rename-section .data=.text \
--set-section-flags .data=alloc,code,load \
--input-target=binary --output-target=elf32-little \
raw$$ raw$$.elf
${CROSS_PREFIX}ld raw$$.elf -T raw$$.ld -o $2
${CROSS_PREFIX}strip -s $2
rm -rf raw$$.elf raw$$.ld
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment