Skip to content

Instantly share code, notes, and snippets.

@puddingpimp
Created May 12, 2013 10:09
Show Gist options
  • Save puddingpimp/5563051 to your computer and use it in GitHub Desktop.
Save puddingpimp/5563051 to your computer and use it in GitHub Desktop.
Convert binary files to elf symbols
#!/bin/sh
usage() {
echo "usage: $0 <infile> <outelf> [symname]"
echo "Converts binary or text file <outelf> into elf object <outelf> with constant named"
echo "symname defaults to infile if none is specified, with disallowed characters replaced with _"
echo "symname an array containing the <infile>"
echo "symname_length length of array <symname>"
echo "symname_end points to the null byte appended after <symname>"
}
[[ -z "$1" ]] || [[ -z "$2" ]] && usage
relname="$3"
infile="$(echo -n "$1" | sed 's/\"/\\\"/g')"
[[ -z "$relname" ]] && relname="$(basename "$1" | sed 's/[\.,\!\@\#\$\%\^\&\*()-\+\=\`\~\"''\;\:\/\?\>\<\s\t]/_/g ; s/\r?\n$//')"
as - -o "$2" <<EOF
.global ${relname}_size
.global ${relname}
.global ${relname}_end
.section .rodata
${relname}_size:
.long ${relname}_end - ${relname}
${relname}:
.incbin "$infile"
${relname}_end:
.byte 0
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment