https://github.com/foxoman/nstrip
A Nim utility to strip nonessential bytes from ELF binary files
https://github.com/foxoman/nstrip
Last synced: about 1 month ago
JSON representation
A Nim utility to strip nonessential bytes from ELF binary files
- Host: GitHub
- URL: https://github.com/foxoman/nstrip
- Owner: foxoman
- License: gpl-2.0
- Created: 2025-03-16T20:17:27.000Z (about 2 months ago)
- Default Branch: main
- Last Pushed: 2025-03-16T20:31:58.000Z (about 2 months ago)
- Last Synced: 2025-03-16T21:31:01.714Z (about 2 months ago)
- Language: Nim
- Size: 19.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# nstrip - Advanced ELF Binary Size Optimizer
## Overview
nstrip is a powerful utility written in Nim that reduces the size of ELF executables and shared libraries by removing unnecessary data. Unlike standard tools like `strip`, nstrip goes further by eliminating all data not referenced by the program headers, potentially achieving much greater size reductions.
## Features
- **Deep Stripping**: Removes all data not referenced by program headers
- **Zero Stripping**: Optional removal of trailing zero bytes
- **Non-destructive Mode**: Can save optimized results to a new file while preserving the original
- **Colorful Output**: Clear visual feedback with detailed size reduction statistics
- **Batch Processing**: Process multiple files in a single command## Installation
Install nstrip easily using Nimble, Nim's package manager:
```
nimble install https://github.com/foxoman/nstrip
```This will download, compile, and install the latest version of nstrip to your system.
## How It Works
nstrip performs several sophisticated operations on ELF binaries:
1. **ELF Structure Analysis**: Reads and validates the ELF header and program headers.
2. **Minimum Size Calculation**: Determines the smallest possible file size that preserves all data needed at runtime by analyzing program segments.
3. **Optional Zero Removal**: Can scan for and remove trailing zero bytes for additional size reduction.
4. **Header Updates**: Modifies ELF header and program headers to reflect the new file size and structure.
5. **File Truncation**: Safely resizes the file to remove unnecessary data.### Technical Details
nstrip operates based on a key insight about ELF files: at runtime, the dynamic loader/kernel only needs data explicitly referenced by program headers. This includes:
- The ELF header
- The program header table
- All segments referenced by program headersOther data, including:
- Section header table
- Section headers
- Symbol tables
- String tables
- Debugging information...can be safely removed without affecting the binary's execution.
## Usage
```
nstrip [OPTIONS] FILE...Options:
-z, --zeros Also discard trailing zero bytes
-o, --output FILE Save compressed result to new file, keeping original intact
--help Display this help and exit
--version Display version information and exit
```### Examples
Strip a single executable, modifying it in place:
```
nstrip myprogram
```Strip multiple files, with additional zero byte removal:
```
nstrip -z file1 file2 file3
```Process a file and save the result to a new location:
```
nstrip --output optimized_binary original_binary
```## Performance Considerations
- **Size Reduction**: Typically achieves 10-20% reduction beyond standard strip tools
- **File Types**: Works on executables and shared libraries (ET_EXEC and ET_DYN ELF types)
- **Integrity**: Preserves all data needed for execution while removing unused data
- **Compatibility**: Works with standard ELF binaries on Linux/Unix systems## Implementation Notes
nstrip is implemented in Nim and uses:
- Native ELF structure parsing with no external dependencies
- POSIX file operations for efficient file handling
- Optimized buffer-based I/O for handling large files
- Comprehensive error handling with informative error messages## Limitations
- Only works with ELF format binaries
- Removes information needed for debugging
- Not recommended for use on system libraries that may require section headers## Credits
Based on the original sstrip concept by Brian Raiter.
- https://github.com/BR903/ELFkickers/blob/master/sstrip/sstrip.c
Created by Sultan Al-Isaiee (foxoman).