Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/plos-clan/libelf-parse
C binding of elf crate.
https://github.com/plos-clan/libelf-parse
Last synced: 7 days ago
JSON representation
C binding of elf crate.
- Host: GitHub
- URL: https://github.com/plos-clan/libelf-parse
- Owner: plos-clan
- License: mit
- Created: 2024-11-02T23:05:07.000Z (3 months ago)
- Default Branch: main
- Last Pushed: 2024-11-03T07:34:29.000Z (3 months ago)
- Last Synced: 2024-11-17T07:16:53.827Z (2 months ago)
- Language: Rust
- Homepage:
- Size: 7.81 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# libelf-parse
C binding of `elf` crate for x86 or x86_64 OS to parse ELF files easily.
## Usage
Download the header file and lib from [releases](https://github.com/plos-clan/libelf-parse/releases/tag/release).
Link the library to your project.
## Build
Build directly to get the two target files:
```bash
cargo build --release
```The production build will be in `target/release//` directory.
And use `cbindgen` to generate the header file:
```bash
cargo install cbindgen
cbindgen --output elf_parse.h
```## Example
```c
#include
#include "elf_parse.h"extern void *page_table;
void segment_callback(struct Segment segment) {
printk("Segment address: 0x%lx\n", segment.address);
printk("Segment size: %lu bytes\n", segment.size);
alloc_and_map(page_table, segment.address, segment.size);
write_to_mapped_address(page_table, segment.address, segment.size, segment.data);
}void elf_load_example() {
const uint8_t elf_data[] = {};
size_t elf_size = sizeof(elf_data);struct ElfParseResult result = parse_elf(elf_data, elf_size, segment_callback);
switch (result.tag) {
case EntryPoint:
printk("ELF Entry Point: 0x%lx\n", result.entry_point);
break;
case InvalidElfData:
printk("Invalid ELF data.\n");
break;
case ElfContainsNoSegments:
printk("ELF contains no segments.\n");
break;
case FailedToGetSegmentData:
printk("Failed to get segment data.\n");
break;
case AllocFunctionNotProvided:
printk("Allocation function not provided.\n");
break;
default:
printk("Unknown error.\n");
break;
}
}
```