Skip to content

Instantly share code, notes, and snippets.

Created July 18, 2015 08:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/7a87824d6c5430008e87 to your computer and use it in GitHub Desktop.
Save anonymous/7a87824d6c5430008e87 to your computer and use it in GitHub Desktop.
Fix invalid Mach-O object output from binutils' objcopy
#!/bin/sh -e
# usage
# objcopy-wrapper -I binary -O mach-o-x86-64 test test.o
# change path
~/binutils-2.25/binutils/objcopy "$@"
for last; do :; done
printf '\x00' | dd conv=notrunc of=$last bs=1 seek=11
symoff=$(otool -l $last | grep '\Wsymoff\W' | awk '{ print $2 }')
for sym in $(nm -ap $last | awk '{ print $(NF) }'); do
case "$sym" in
*_size)pp
# does not link correctly for arch x86-64. `-O mach-o-i386` and `-arch i386` to compilers will fix.
# http://stackoverflow.com/questions/31485714/mac-osx-ld-report-32-bit-rip-relative-reference-out-of-range-error-for-absolute
printf '\x03\x00\x00\x00' | dd conv=notrunc of=$last bs=4 seek=$(echo "($symoff+4)/4" | bc)
;;
*_end|*_start)
printf '\x0f\x01\x00\x00' | dd conv=notrunc of=$last bs=4 seek=$(echo "($symoff+4)/4" | bc)
;;
*)
echo "Unexpected symbol pattern: $sym"
exit 1
;;
esac
# change to 12 for i386
symoff=$(echo "$symoff+16" | bc)
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment