Skip to content

Commit f8d8e78

Browse files
committed
docs: update README.md
1 parent 8d12806 commit f8d8e78

File tree

5 files changed

+81
-168
lines changed

5 files changed

+81
-168
lines changed

src/image/kinds/dylib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,12 @@ impl<D> RawDylib<D> {
131131
self.inner.name()
132132
}
133133

134+
/// Returns the short name of the ELF object.
135+
#[inline]
136+
pub fn short_name(&self) -> &str {
137+
self.inner.core_ref().short_name()
138+
}
139+
134140
/// Returns the program headers of the ELF object.
135141
pub fn phdrs(&self) -> &[ElfPhdr] {
136142
self.inner.phdrs()

src/input/backend.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,7 @@ impl ElfFile {
7979
#[cfg(feature = "log")]
8080
log::debug!("Opening ELF file: {}", path);
8181

82-
let inner = RawFile::from_path(path).map_err(|e| {
83-
#[cfg(feature = "log")]
84-
log::error!("Failed to open ELF file {}: {:?}", path, e);
85-
e
86-
})?;
87-
82+
let inner = RawFile::from_path(path)?;
8883
Ok(ElfFile { inner })
8984
}
9085
}

tools/gen-elf/README.md

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,44 @@
11
# gen-elf
22

3-
`gen-elf` is a utility for generating ELF files (Shared Objects and Relocatable Objects) specifically designed for testing ELF loaders. It simplifies the process of creating binaries with specific symbol structures and relocation entries for `elf_loader` verification.
3+
`gen-elf` is a utility for generating ELF files (Shared Objects and Relocatable Objects) specifically designed for testing ELF loaders. It simplifies the process of creating binaries with specific symbol structures, relocation entries, and memory layouts for `elf_loader` verification.
44

55
## Features
66

77
- **Multi-architecture Support**: Supports x86_64, x86, Aarch64, Riscv64, Riscv32, Arm, and Loongarch64.
8-
- **Dynamic Library Generation**: Generates Shared Objects (.so) with `.dynamic` sections, relocation tables (RELA/REL), and symbol tables.
8+
- **Dynamic Library Generation**: Generates Shared Objects (.so) with `.dynamic` sections, relocation tables (RELA/REL), symbol tables, and GOT/PLT.
9+
- **TLS and IFUNC Support**: Easily generate Thread-Local Storage (TLS) symbols and Indirect Functions (IFUNC) with automatic resolver generation.
910
- **Relocatable Object Generation**: Generates standard relocatable object files (.o).
11+
- **Customizable Layout**: Configure base address and page size for memory mapping tests.
1012
- **High-level API**: Provides intent-based interfaces like `RelocEntry::jump_slot` and `SymbolDesc::global_func`, abstracting away complex ELF constants.
11-
- **Metadata Export**: Exports relocation addresses and other metadata alongside the ELF data for easy verification in tests.
13+
- **Metadata Export**: Exports detailed relocation information and section addresses alongside the ELF data for easy verification in tests.
1214

1315
## Core Interfaces
1416

1517
### `DylibWriter`
16-
Used for generating dynamic libraries.
18+
Used for generating dynamic libraries. You can customize the generation using `ElfWriterConfig`.
1719

1820
```rust
19-
use gen_elf::{Arch, DylibWriter, RelocEntry, SymbolDesc};
21+
use gen_elf::{Arch, DylibWriter, ElfWriterConfig, RelocEntry, SymbolDesc};
2022

21-
let arch = Arch::X86_64;
22-
let writer = DylibWriter::new(arch);
23+
let arch = Arch::current();
24+
let config = ElfWriterConfig::default()
25+
.with_base_addr(0x400000)
26+
.with_page_size(0x1000);
27+
let writer = DylibWriter::with_config(arch, config);
2328

2429
let relocs = vec![
2530
RelocEntry::jump_slot("external_func", arch),
31+
RelocEntry::irelative(arch), // Test IFUNC
2632
];
2733

2834
let symbols = vec![
2935
SymbolDesc::global_object("my_var", &[1, 2, 3, 4]),
36+
SymbolDesc::global_tls("my_tls", &[0xaa, 0xbb]),
3037
SymbolDesc::undefined_func("external_func"),
3138
];
3239

33-
let output = writer.write_file("libtest.so", &relocs, &symbols)?;
34-
println!("Generated ELF at base address: {:#x}", output.base_addr);
40+
let output = writer.write(&relocs, &symbols)?;
41+
println!("Generated ELF with {} relocations", output.relocations.len());
3542
```
3643

3744
### `ObjectWriter`
@@ -45,31 +52,34 @@ let writer = ObjectWriter::new(arch);
4552

4653
let symbols = vec![
4754
SymbolDesc::global_func("my_func", &[0x90, 0xc3]), // nop; ret
55+
SymbolDesc::global_object("data_var", &[0x01, 0x02]),
4856
];
4957

58+
// Symbols and Relocs
5059
writer.write_file("test.o", &symbols, &[])?;
5160
```
5261

53-
### `Arch`
54-
Represents the target architecture and provides methods to retrieve architecture-specific relocation types.
55-
56-
- `Arch::current()`: Returns the architecture of the current host.
57-
- `jump_slot_reloc()`: Returns the `JUMP_SLOT` relocation type for the architecture.
62+
### `RelocEntry`
63+
Common relocation types are available as high-level methods:
5864

59-
## CLI Tool
65+
- `RelocEntry::jump_slot(name, arch)`: PLT-based functions.
66+
- `RelocEntry::glob_dat(name, arch)`: Global variable references.
67+
- `RelocEntry::relative(arch)`: Base-relative relocations.
68+
- `RelocEntry::irelative(arch)`: Indirect (IFUNC) relocations.
69+
- `RelocEntry::copy(name, arch)`: Copy relocations for variables.
70+
- `RelocEntry::dtpmod(name, arch)` / `RelocEntry::dtpoff(name, arch)`: TLS-related relocations.
6071

61-
`gen-elf` can also be used as a standalone command-line tool:
72+
### `SymbolDesc`
73+
Describe symbols with various scopes and types:
6274

63-
```bash
64-
# Generate a default x86_64 dynamic library
65-
cargo run -p gen-elf -- --dynamic -o ./out
66-
67-
# Generate for a specific architecture
68-
cargo run -p gen-elf -- --target aarch64 --dynamic -o ./out
69-
```
75+
- `SymbolDesc::global_func(name, code)`: A global function.
76+
- `SymbolDesc::global_object(name, data)`: A global variable.
77+
- `SymbolDesc::global_tls(name, data)`: A thread-local variable.
78+
- `SymbolDesc::undefined_func(name)`: Reference to an external function.
79+
- `.with_scope(SymbolScope::Weak)`: Mark a symbol as weak.
7080

7181
## Usage in Tests
7282

73-
This tool is particularly useful for integration testing of `elf_loader`. You can dynamically generate an ELF with specific relocation types, load it with your loader, and verify that relocations are applied correctly.
83+
This tool is particularly useful for integration testing of `elf_loader`. You can dynamically generate an ELF with specific relocation types, load it with your loader, and verify that relocations are applied correctly by inspecting `ElfWriteOutput`.
7484

75-
See `tests/gen_elf.rs` for examples.
85+
See `tests/gen_elf.rs` for comprehensive examples.

tools/gen-elf/README_zh.md

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,44 @@
11
# gen-elf
22

3-
`gen-elf` 是一个用于生成测试用 ELF 文件(动态库和可重定位对象文件)的辅助工具。它旨在简化在测试 `elf_loader` 时创建具有特定符号和重定位结构的二进制文件的过程
3+
`gen-elf` 是一个用于生成测试用 ELF 文件(共享对象和可重定位对象)的辅助工具,专为测试 ELF 加载器而设计。它简化了创建具有特定符号结构、重定位条目和内存布局的二进制文件的过程,以便对 `elf_loader` 进行验证
44

55
## 功能特性
66

7-
- **多架构支持**:支持 x86_64, x86, Aarch64, Riscv64, Riscv32, Arm, Loongarch64。
8-
- **动态库生成**:支持生成带有 `.dynamic` 段、重定位表(RELA/REL)和符号表的共享对象(.so)。
9-
- **可重定位文件生成**:支持生成标准的 `.o` 文件。
10-
- **高层级 API**:提供意图导向的接口,如 `RelocEntry::jump_slot``SymbolDesc::global_func`,无需手动处理复杂的 ELF 常量。
11-
- **元数据导出**:生成 ELF 的同时导出重定位地址等元数据,方便在测试中进行验证。
7+
- **多架构支持**:支持 x86_64, x86, Aarch64, Riscv64, Riscv32, Arm 和 Loongarch64。
8+
- **动态库生成**:生成带有 `.dynamic` 段、重定位表(RELA/REL)、符号表以及 GOT/PLT 的共享对象 (.so)。
9+
- **TLS 和 IFUNC 支持**:轻松生成线程本地存储 (TLS) 符号和间接函数 (IFUNC),并自动生成 resolver。
10+
- **可重定位对象生成**:生成标准的可重定位对象文件 (.o)。
11+
- **可自定义布局**:配置用于内存映射测试的基地址和页面大小。
12+
- **高层级 API**:提供意图导向的接口,如 `RelocEntry::jump_slot``SymbolDesc::global_func`,抽象掉复杂的 ELF 常量。
13+
- **元数据导出**:导出详细的重定位信息和段地址,方便在测试中进行验证。
1214

1315
## 核心接口
1416

1517
### `DylibWriter`
16-
用于生成动态库。
18+
用于生成动态库。你可以使用 `ElfWriterConfig` 自定义生成过程。
1719

1820
```rust
19-
use gen_elf::{Arch, DylibWriter, RelocEntry, SymbolDesc};
21+
use gen_elf::{Arch, DylibWriter, ElfWriterConfig, RelocEntry, SymbolDesc};
2022

21-
let arch = Arch::X86_64;
22-
let writer = DylibWriter::new(arch);
23+
let arch = Arch::current();
24+
let config = ElfWriterConfig::default()
25+
.with_base_addr(0x400000)
26+
.with_page_size(0x1000);
27+
let writer = DylibWriter::with_config(arch, config);
2328

2429
let relocs = vec![
2530
RelocEntry::jump_slot("external_func", arch),
31+
RelocEntry::irelative(arch), // 测试 IFUNC
2632
];
2733

2834
let symbols = vec![
2935
SymbolDesc::global_object("my_var", &[1, 2, 3, 4]),
36+
SymbolDesc::global_tls("my_tls", &[0xaa, 0xbb]),
3037
SymbolDesc::undefined_func("external_func"),
3138
];
3239

33-
let output = writer.write_file("libtest.so", &relocs, &symbols)?;
34-
println!("Generated ELF at base address: {:#x}", output.base_addr);
40+
let output = writer.write(&relocs, &symbols)?;
41+
println!("Generated ELF with {} relocations", output.relocations.len());
3542
```
3643

3744
### `ObjectWriter`
@@ -45,29 +52,34 @@ let writer = ObjectWriter::new(arch);
4552

4653
let symbols = vec![
4754
SymbolDesc::global_func("my_func", &[0x90, 0xc3]), // nop; ret
55+
SymbolDesc::global_object("data_var", &[0x01, 0x02]),
4856
];
4957

58+
// 符号和重定位
5059
writer.write_file("test.o", &symbols, &[])?;
5160
```
5261

53-
### `Arch`
54-
表示目标架构,提供架构相关的重定位类型获取方法。
55-
56-
- `Arch::current()`: 获取当前主机的架构。
57-
- `jump_slot_reloc()`: 获取该架构的 `JUMP_SLOT` 重定位类型。
62+
### `RelocEntry`
63+
常见的重定位类型可以通过高层级方法使用:
5864

59-
## 命令行工具
65+
- `RelocEntry::jump_slot(name, arch)`:基于 PLT 的函数。
66+
- `RelocEntry::glob_dat(name, arch)`:全局变量引用。
67+
- `RelocEntry::relative(arch)`:基地址相对重定位。
68+
- `RelocEntry::irelative(arch)`:间接 (IFUNC) 重定位。
69+
- `RelocEntry::copy(name, arch)`:变量的复制重定位。
70+
- `RelocEntry::dtpmod(name, arch)` / `RelocEntry::dtpoff(name, arch)`:TLS 相关重定位。
6071

61-
`gen-elf` 也可以作为一个独立的命令行工具使用:
72+
### `SymbolDesc`
73+
描述具有各种作用域和类型的符号:
6274

63-
```bash
64-
# 生成默认的 x86_64 动态库
65-
cargo run -p gen-elf -- --dynamic -o ./out
66-
67-
# 为指定架构生成
68-
cargo run -p gen-elf -- --target aarch64 --dynamic -o ./out
69-
```
75+
- `SymbolDesc::global_func(name, code)`:全局函数。
76+
- `SymbolDesc::global_object(name, data)`:全局变量。
77+
- `SymbolDesc::global_tls(name, data)`:线程本地变量。
78+
- `SymbolDesc::undefined_func(name)`:对外部函数的引用。
79+
- `.with_scope(SymbolScope::Weak)`:将符号标记为弱符号 (weak)。
7080

7181
## 在测试中使用
7282

73-
该工具特别适合用于 `elf_loader` 的集成测试。你可以动态生成一个具有特定重定位类型的 ELF,然后使用你的加载器加载它,并验证重定位是否正确应用。
83+
该工具特别适用于 `elf_loader` 的集成测试。你可以动态生成具有特定重定位类型的 ELF,使用加载器加载它,并通过检查 `ElfWriteOutput` 验证重定位是否正确应用。
84+
85+
有关全面示例,请参阅 `tests/gen_elf.rs`

tools/gen-elf/src/main.rs

Lines changed: 0 additions & 110 deletions
This file was deleted.

0 commit comments

Comments
 (0)