progmem.sh 5.5 KB

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