A sophisticated tool for analyzing stack usage in AVR microcontroller binaries through cycle-accurate instruction simulation.
AVR Stack Analyzer helps you figure out how much stack your AVR microcontroller programs are using. It works by looking at your compiled ELF files and actually simulating the AVR instructions to track stack operations in real-time. This is super helpful for embedded developers who want to avoid those nasty stack overflows that can make AVR-based systems go haywire.
Author: Gary Scott (Dazed_N_Confused)
- We've implemented the full AVR instruction set including:
- All the stack stuff (PUSH/POP)
- Function calls (CALL/RCALL)
- Returns (RET/RETI)
- Those tricky indirect jumps and calls (IJMP/ICALL)
- Every branch instruction you can think of
- Tracks the stack accurately for each cycle
- Smart enough to understand compiler function patterns
- Recognizes different compiler-specific code styles
- Full instruction set support for both classic and modern AVR architectures
- Support for new AVR instruction variants in newer devices
- Builds a complete map of function calls
- Keeps tabs on both direct and indirect function calls
- Handles all those jumps and branches
- Checks multiple possible execution paths
- Catches recursive calls before they bite you
- Works with both relative and absolute addresses
- Can handle address wrapping at 0 (if you need it)
- Support for up to 32 interrupt vectors
- ISR stack usage validation
- Detection of calls from interrupt contexts
- Analysis of interrupt nesting implications
- Special handling of bad_interrupt vector
- ISR safety verification
- Support for both classic and modern AVR interrupt vector tables
- Handles the different vector formats in ATtiny 0/1/2-series and ATmega 0/1-series
- Automatic detection of AVR architecture type
- Precise RAM usage tracking
- Program memory (flash) analysis
- Stack underflow detection
- Memory safety bounds checking
- Multi-space memory management (RAM, flash, EEPROM)
- Maximum stack depth calculation
- NEW: Accurate RAM size detection and tracking
- NEW: RAM usage warnings based on percentage thresholds
- Complete ELF file parsing
- Symbol table processing
- Debug information handling
- Relocation processing
- Text section analysis
- Function entry point detection
- NEW: Section information for reliable text section location
- NEW: Robust ELF parsing for different compiler outputs
- NEW: Support for Intel HEX file format
- NEW: Support for modern AVR microcontrollers (ATtiny 0/1/2-series, ATmega 0/1-series, AVR DA/DB/DD/EA)
- NEW: Automatic architecture detection for proper memory and vector handling
- Standard function prologue detection
- Epilogue pattern identification
- Support for multiple compiler versions
- Optimization level awareness
- Variable-length pattern matching
- Compiler-specific pattern handling
- NEW: Enhanced detection of stack manipulation instructions
- Recognition of compiler-specific stack allocation methods (e.g., "SBIW r28,N" for locals)
- Better handling of specialized stack frames that don't follow standard patterns
- Multiple output formats:
- Human-readable default format
- JSON structured output (pretty or compact)
- v4 and v19 compatibility modes
- Detailed reporting options:
- Per-function stack usage
- Maximum call chain depths
- Interrupt stack requirements
- Memory utilization statistics
- Rust and Cargo (latest stable version recommended)
- Dependencies (automatically installed via Cargo):
- clap - Command-line argument parsing
- object - ELF file parsing
- serde - JSON serialization
- serde_json - JSON formatting and manipulation
- hex - Intel HEX file parsing
-
Clone the repository:
git clone https://github.com/your-username/avr_stack_analyzer.git cd avr_stack_analyzer
-
Build the project:
cargo build --release
-
The compiled binary will be available at
target/release/avr_stack
Basic usage:
avr_stack [OPTIONS] <INPUT_FILE>
--format <FORMAT>
- Output format (v4, v19, or json)--total-only
- Print only total stack usage--allow-calls-from-isr
- Don't error on calls from ISRs--wrap0
- Allow wrapped addresses at address 0--include-bad-interrupt
- Include bad_interrupt in analysis--ignore-icall
- Ignore all indirect calls--memory-report
- Show memory statistics and RAM usage estimates--json
- Output in JSON format--json-compact
- Output compact JSON format--verbose
- Show detailed warnings and analysis messages (disabled by default)--quiet
- Suppress non-essential output
Analyze with full memory reporting:
avr_stack --memory-report path/to/firmware.elf
Generate detailed JSON output:
avr_stack --json --format v19 path/to/firmware.elf
Analyze with ISR call validation disabled:
avr_stack --allow-calls-from-isr path/to/firmware.elf
Analyze with debug information:
avr_stack --debug path/to/firmware.elf
The analysis output includes:
-
Function-level Information:
- Function names and addresses
- Maximum stack usage per function
- Call chains leading to maximum stack usage
- ISR interactions and implications
-
Global Analysis:
- Total maximum stack usage
- Critical call paths
- Potential stack overflow points
- Memory utilization statistics
-
Safety Warnings:
- Stack underflow risks
- Dangerous ISR calls
- Recursive call detection
- Memory boundary issues
- The project uses comprehensive error handling and validation
- Includes extensive safety checks for memory operations
- Employs efficient data structures for performance
- Supports debugging and detailed logging capabilities
If you encounter compilation errors:
- Make sure you have the latest stable Rust toolchain installed
- Try cleaning the build and rebuilding:
cargo clean cargo build --release
- Check for specific error messages in the build output
- Verify your AVR ELF file format is supported
If you see many "Warning: Program data is empty" messages or "No program data available" errors:
-
Your ELF file might be stripped
- Use the original, unstripped ELF file directly from your compiler
- If using avr-gcc, compile with the
-g
flag to include debug information
-
The file format may not be fully supported
- Try using a different output format from your compiler
- HEX files sometimes don't contain all the necessary information
-
The input may not be a valid ELF file
- Verify you're using the correct file
- Some post-processing tools can corrupt ELF files
-
Suppress excessive warnings
- The tool now automatically suppresses most warnings about out-of-bounds addresses
- Use the
--verbose
option if you need to see all warnings for debugging purposes
-
Command to verify your ELF file has code sections:
avr-objdump -h your_file.elf
Look for a
.text
section with non-zero size -
Manual symbol dumping:
avr-nm your_file.elf
This should list function symbols in your code
The analyzer supports both classic and modern AVR microcontrollers:
- ATtiny series (ATtiny13/25/45/85, etc.)
- ATmega series (ATmega8/16/32/328, etc.)
- AT90 series
- ATtiny 0-series (ATtiny202/402/204/404/806, etc.)
- ATtiny 1-series (ATtiny212/412/214/414/816, etc.)
- ATtiny 2-series (ATtiny1624/1626/1627, etc.)
- ATmega 0-series (ATmega808/809/1608/1609)
- ATmega 4809 and similar modern megaAVR devices
- AVR DA/DB/DD/EA series (AVR128DA, AVR64DB, etc.)
Contributions are welcome! Please feel free to submit a Pull Request. When contributing:
- Follow the existing code style
- Add tests for new features
- Update documentation for significant changes
- Ensure all tests pass
This project is licensed under the GPL-3.0.
Current version: 1.7
- Added support for modern AVR microcontrollers:
- ATtiny 0/1/2-series (ATtiny412, etc.)
- ATmega 0/1-series (ATmega4809, etc.)
- AVR DA/DB/DD/EA series
- Automatic detection of AVR architecture for proper analysis
- Enhanced memory reporting for all supported AVR devices
- Improved interrupt vector handling for modern AVR architectures
- Added RAM size detection and tracking
- Improved JSON output formatting
- Enhanced memory usage recommendations
- NEW: Suppressed excessive warning messages by default
- Added
--verbose
option to show all warnings if needed - Better handling of empty program data
- Improved output readability with thousands of functions
- Added
- NEW: Enhanced detection of stack manipulation instructions
- Recognition of compiler-specific stack allocation methods (e.g., "SBIW r28,N" for locals)
- Better handling of specialized stack frames that don't follow standard patterns
- NEW: Section information for reliable text section location
- NEW: Robust ELF parsing for different compiler outputs
- NEW: Support for Intel HEX file format