Skip to content

Commit c28a7f1

Browse files
committed
Add PROGMEM size check
1 parent 855ea01 commit c28a7f1

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

platform.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ compiler.elf2hex.cmd=avr-objcopy
3535
compiler.ldflags=
3636
compiler.libraries.ldflags=
3737
compiler.size.cmd=avr-size
38+
compiler.symbol.cmd=avr-nm
3839

3940
# This can be overridden in boards.txt
4041
build.extra_flags=
@@ -72,6 +73,10 @@ recipe.c.combine.pattern="{compiler.path}{compiler.c.elf.cmd}" {compiler.c.elf.f
7273
recipe.objcopy.eep.pattern="{compiler.path}{compiler.objcopy.cmd}" {compiler.objcopy.eep.flags} {compiler.objcopy.eep.extra_flags} "{build.path}/{build.project_name}.elf" "{build.path}/{build.project_name}.eep"
7374
recipe.objcopy.hex.pattern="{compiler.path}{compiler.elf2hex.cmd}" {compiler.elf2hex.flags} {compiler.elf2hex.extra_flags} "{build.path}/{build.project_name}.elf" "{build.path}/{build.project_name}.hex"
7475

76+
## Check for progmem overflow
77+
recipe.objcopy.progmem.pattern="/bin/bash" "{runtime.platform.path}/scripts/progmemcheck.sh" "{compiler.path}/{compiler.symbol.cmd}" "{build.path}/{build.project_name}.elf"
78+
recipe.objcopy.progmem.pattern.windows="{runtime.platform.path}/scripts/progmemcheck.bat" "{runtime.tools.avr-gcc.path}\\bin\\{compiler.symbol.cmd}" "{build.path}/{build.project_name}.elf"
79+
7580
## Save hex
7681
recipe.output.tmp_file={build.project_name}.hex
7782
recipe.output.save_file={build.project_name}.{build.variant}.hex

scripts/progmemcheck.bat

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
@echo off
2+
REM Windows batch file equivalent of progmemcheck.sh
3+
REM Checks if PROGMEM section is too large (over 65535 bytes)
4+
REM Usage: progmemcheck.bat <objdump_tool> <object_file>
5+
6+
if "%1"=="" goto usage
7+
if "%2"=="" goto usage
8+
9+
REM Execute objdump command and extract the address (should be decimal with -t d option)
10+
for /f "tokens=1" %%i in ('%1 -t d %2 ^| findstr "__ctors_start"') do set progmem_end=%%i
11+
12+
REM Check if we got a result
13+
if "%progmem_end%"=="" (
14+
exit /b 0
15+
)
16+
17+
REM Remove any leading/trailing spaces
18+
set progmem_end=%progmem_end: =%
19+
20+
REM Remove leading zeros manually (batch treats leading zeros as octal)
21+
:strip_zeros
22+
if "%progmem_end:~0,1%"=="0" if not "%progmem_end%"=="0" (
23+
set progmem_end=%progmem_end:~1%
24+
goto strip_zeros
25+
)
26+
27+
REM If we stripped all digits, it was "000..." so set to 0
28+
if "%progmem_end%"=="" set progmem_end=0
29+
30+
REM Check if progmem_end is greater than 65535
31+
if %progmem_end% LEQ 65535 goto end
32+
33+
REM Calculate the excess bytes
34+
set /a excess=%progmem_end% - 65535
35+
36+
set line="^| Severe Warning: PROGMEM section too large by %excess% bytes. "
37+
echo.
38+
echo _______________________________________________________________
39+
echo %line:~1,63%^|
40+
echo ^| Your program will most probably be unstable! ^|
41+
echo ^| Use the macros PROGMEM_FAR from ^<progmem_far.h^> and ^|
42+
echo ^| pgm_get_far_address/pgm_read_xxxx_far from ^<avr/pgmspace.h^>.^|
43+
echo ^|_____________________________________________________________^|
44+
echo.
45+
goto end
46+
47+
:usage
48+
echo Usage: %0 ^<objdump_tool^> ^<object_file^>
49+
echo Example: %0 avr-objdump firmware.elf
50+
exit /b 1
51+
52+
:end
53+
exit /b 0

scripts/progmemcheck.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
progmem_end=`$1 -t d $2 | grep __ctors_start | cut -d ' ' -f 1`
3+
if [ "x$progmem_end" != "x" ] && [ $progmem_end -gt 65535 ]; then
4+
warning="| Severe Warning: PROGMEM section too large by $(expr $progmem_end - 65535) bytes. "
5+
echo "_______________________________________________________________"
6+
echo "${warning:0:62}|"
7+
echo "| Your program will most probably be unstable! |"
8+
echo "| Use the macros PROGMEM_FAR from <progmem_far.h> and |"
9+
echo "| pgm_get_far_address/pgm_read_xxxx_far from <avr/pgmspace.h>.|"
10+
echo "|_____________________________________________________________|"
11+
fi

0 commit comments

Comments
 (0)