progmem.sh 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #!/bin/sh
  2. # progmem.sh - multi-language support low-level script
  3. # for examining content of progmem sections (default is progmem1)
  4. #
  5. # Input files:
  6. # $OUTDIR/Firmware.ino.elf
  7. # $OUTDIR/sketch/*.o (all object files)
  8. #
  9. # Output files:
  10. # text.sym
  11. # $PROGMEM.sym
  12. # $PROGMEM.lss
  13. # $PROGMEM.hex
  14. # $PROGMEM.chr
  15. # $PROGMEM.var
  16. # $PROGMEM.txt
  17. #
  18. # Program memory used
  19. PROGMEM=progmem$1
  20. if [ -z "$1" ]; then PROGMEM=progmem1; fi
  21. #
  22. # Output folder and elf file:
  23. OUTDIR="../../output"
  24. OUTELF="$OUTDIR/Firmware.ino.elf"
  25. #
  26. # AVR gcc tools used:
  27. OBJDUMP=C:/arduino-1.6.8/hardware/tools/avr/bin/avr-objdump.exe
  28. #
  29. #
  30. # Description of process:
  31. # 0. check input files
  32. # 1. remove output files
  33. # 2. list symbol table of section '.text' from output elf file to text.sym (sorted by address)
  34. # 3. list symbol table of section '.$PROGMEM' from all output object files to $PROGMEM.sym
  35. # 4. filter only $PROGMEM symbols from text.sym and store it back to $PROGMEM.sym with absolute address
  36. # 5. calculate start and stop address of section '.$PROGMEM'
  37. # 6. extract string data from elf file to $PROGMEM.hex
  38. # 7. prepare string data for character check and conversion (output to $PROGMEM.chr)
  39. # 8. perform character check and conversion (output to $PROGMEM.var and $PROGMEM.txt)
  40. #
  41. echo "progmem.sh started" >&2
  42. # (0)
  43. echo " progmem.sh (0) - checking input files" >&2
  44. if [ ! -e $OUTDIR ]; then echo "progmem.sh - file '$OUTELF' not found!" >&2; exit 1; fi
  45. # (1)
  46. echo " progmem.sh (1) - removing output files" >&2
  47. #remove output files if exists
  48. if [ -e text.sym ]; then rm text.sym; fi
  49. if [ -e $PROGMEM.sym ]; then rm $PROGMEM.sym; fi
  50. if [ -e $PROGMEM.lss ]; then rm $PROGMEM.lss; fi
  51. if [ -e $PROGMEM.hex ]; then rm $PROGMEM.hex; fi
  52. if [ -e $PROGMEM.chr ]; then rm $PROGMEM.chr; fi
  53. if [ -e $PROGMEM.var ]; then rm $PROGMEM.var; fi
  54. if [ -e $PROGMEM.txt ]; then rm $PROGMEM.txt; fi
  55. # (2)
  56. echo " progmem.sh (2) - listing symbol table of section '.text'" >&2
  57. #list symbols from section '.text' into file text.sym (only address, size and name)
  58. $OBJDUMP -t -j ".text" $OUTELF | tail -n +5 | grep -E "^[0-9a-f]{8} [gl] O" | cut -c1-9,28-36,37- | sed "/^$/d" | sort >> text.sym
  59. # (3)
  60. echo " progmem.sh (3) - listing symbol table of section '.$PROGMEM'" >&2
  61. #loop over all object files
  62. ls "$OUTDIR"/sketch/*.o | while read fn; do
  63. echo " processing $fn" >&2
  64. #list symbols from section $PROGMEM (only address, size and name)
  65. $OBJDUMP -t -j ".$PROGMEM" $fn 2>/dev/null | tail -n +5 | cut -c1-9,28-36,37- | sed "/^$/d" | sort >> $PROGMEM.sym
  66. done
  67. # (4)
  68. echo " progmem.sh (4) - filtering $PROGMEM symbols" >&2
  69. #create list of $PROGMEM symbol names separated by '\|'
  70. progmem=$(cut -c19- $PROGMEM.sym)
  71. progmem=$(echo $progmem | sed "s/ /\\\b\\\|\\\b/g")
  72. progmem='\b'$progmem'\b'
  73. #filter $PROGMEM symbols from section '.text' (result file will contain list sorted by absolute address)
  74. cat text.sym | grep $progmem > $PROGMEM.sym
  75. # (5)
  76. echo " progmem.sh (5) - calculating start and stop address" >&2
  77. #calculate start addres of section ".$PROGMEM"
  78. PROGMEM_BEG=$(head -n1 $PROGMEM.sym | while read offs size name; do echo "0x"$offs; done)
  79. #calculate stop addres of section ".$PROGMEM"
  80. PROGMEM_END=$(tail -n1 $PROGMEM.sym | while read offs size name; do printf "0x%x" $(("0x"$offs + "0x"$size)); done)
  81. echo " START address = "$PROGMEM_BEG >&2
  82. echo " STOP address = "$PROGMEM_END >&2
  83. # (6)
  84. echo " progmem.sh (6) - extracting string data from elf" >&2
  85. #dump $PROGMEM data in hex format, cut textual data (keep hex data only)
  86. $OBJDUMP -d -j ".text" -w -z --start-address=$PROGMEM_BEG --stop-address=$PROGMEM_END $OUTELF | cut -c1-57 > $PROGMEM.lss
  87. #convert $PROGMEM.lss to $PROGMEM.hex:
  88. # replace empty lines with '|' (variables separated by empty lines)
  89. # remove address from multiline variables (keep address at first variable line only)
  90. # remove '<' and '>:', remove whitespace at end of lines
  91. # remove line-endings, replace separator with '\n' (join hex data lines - each line will contain single variable)
  92. # filter progmem symbols
  93. cat $PROGMEM.lss | tail -n +7 | sed -E 's/^$/|/;s/^........:\t/ /;s/<//g;s/>:/ /g;s/[ \t]*$//' |\
  94. tr -d '\n' | sed "s/[|]/\n/g" | grep $progmem > $PROGMEM.hex
  95. # (7)
  96. echo " progmem.sh (7) - preparing string data" >&2
  97. #convert $PROGMEM.hex to $PROGMEM.chr (prepare string data for character check and conversion)
  98. # replace first space with tab
  99. # replace second space with tab and space
  100. # replace all remaining spaces with '\x'
  101. # replace all tabs with spaces
  102. cat $PROGMEM.hex | sed 's/ /\t/;s/ /\t /;s/ /\\x/g;s/\t/ /g' > $PROGMEM.chr
  103. # (8)
  104. #convert $PROGMEM.chr to $PROGMEM.var (convert data to text)
  105. echo " progmem.sh (8) - converting string data" >&2
  106. cat $PROGMEM.chr | \
  107. sed 's/ \\xff\\xff/ /;' | \
  108. sed 's/\\x22/\\\\\\x22/g;' | \
  109. sed 's/\\x1b/\\\\\\x1b/g;' | \
  110. sed 's/\\x01/\\\\\\x01/g;' | \
  111. sed 's/\\xf8/\\\\\\xf8/g;' | \
  112. sed 's/\\x00$/\\x0a/;s/^/printf "/;s/$/"/' | sh > $PROGMEM.var
  113. cat $PROGMEM.var | sed 's/\r/\n/g' |sed -E 's/^[0-9a-f]{8} [^ ]* //' >$PROGMEM.txt
  114. echo "progmem.sh finished" >&2
  115. exit 0