as86_to_data 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/bin/sh -
  2. #
  3. # This file is simply an example of what can be done using the new binary
  4. # and symbol table output functions. It produces a byte array with the
  5. # symbols in the object as array references within it.
  6. #
  7. # The output is a Linux OMAGIC binary created by ld86 -N, this means the
  8. # object can be linked directly to C-functions created by the same GCC that
  9. # compiled ld86.
  10. #
  11. # Use it in a makefile:
  12. #
  13. # .s86.o:
  14. # as86_to_data $*.s86 $*.o $(AS86FLAGS)
  15. #
  16. [ $# -lt 2 ] && {
  17. echo "Usage: `basename $0` infile outfile [as86 opts]" 1>&2
  18. exit 1
  19. }
  20. trap "rm -f _$$.* ; exit 99" 1 2 3 15
  21. LIBDIR='%%LIBDIR%%' # Set by make install
  22. [ -d "$LIBDIR" ] || LIBDIR='%%BINDIR%%'
  23. [ -d "$LIBDIR" ] || LIBDIR=/usr/bin
  24. IFILE="$1"
  25. OFILE="$2"
  26. shift ; shift
  27. RV=0
  28. $LIBDIR/as86 "$@" "$IFILE" -b _$$.bin -s _$$.sym || RV=$?
  29. [ "$RV" = 0 ] && {
  30. (
  31. cat _$$.sym
  32. echo %%%%
  33. od -v -t uC _$$.bin
  34. echo %%%%
  35. ) | \
  36. awk > _$$.v ' BEGIN{
  37. startaddr="";
  38. printf ".text\n.data\n";
  39. }
  40. /^%%%%$/ { flg++;
  41. next;
  42. }
  43. flg==0 {
  44. if(NF == 0) next;
  45. if( substr($2,1,4) == "0000" ) $2=substr($2,5);
  46. if( $1 == "+" && $4 == "$start" )
  47. {
  48. if( $2 != "0000" ) startaddr=" - $" $2;
  49. }
  50. else if( substr($3, 1, 1) == "E" && $4 != "start" && $4 != "size" && $4 != "data" )
  51. {
  52. printf "export _%s\n", $4
  53. printf "_%s = * + $%s%s\n\n", $4, $2, startaddr;
  54. }
  55. next;
  56. }
  57. flg==1 {
  58. if(NF <= 1) next;
  59. printf " .byte ";
  60. for(i=2;i<NF;i++) printf("%3d,", $i);
  61. printf("%3d", $NF);
  62. printf "\n";
  63. }
  64. '
  65. RV=$?
  66. }
  67. [ "$RV" = 0 ] || { rm -f _$$.* ; exit $RV ; }
  68. # If you want to see the assembler.
  69. # cp _$$.v `basename $2 .o`.asm
  70. $LIBDIR/as86 -o _$$.o86 _$$.v || RV=$?
  71. [ "$RV" = 0 ] || { rm -f _$$.* ; exit $RV ; }
  72. $LIBDIR/ld86 -r -N _$$.o86 -o _$$.o || RV=$?
  73. [ "$RV" = 0 ] || { rm -f _$$.* ; exit $RV ; }
  74. if [ "X$OFILE" = "X-" ]
  75. then cat _$$.o
  76. else mv -f _$$.o "$OFILE" || RV=$?
  77. fi
  78. rm -f _$$.*
  79. exit $RV