Skip to content

Commit 4edc6cf

Browse files
committed
Update readme
1 parent a640ac5 commit 4edc6cf

File tree

3 files changed

+31
-25
lines changed

3 files changed

+31
-25
lines changed

doc/elf_class_diagram.png

26.5 KB
Loading

doc/readme.md

+27-23
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,48 @@
22

33
This is the manual of LibObjectFile with the following API covered:
44

5-
- [ELF Object File Format](#elf-object-file-format) via the `ElfObjectFile` API
5+
- [ELF Object File Format](#elf-object-file-format) via the `ElfFile` API
66
- [Archive ar File Format](#archive-ar-file-format) via the `ArArchiveFile` API
7+
- [PE File Format](#pe-object-file-format) via the `PEFile` API
78

89
## ELF Object File Format
910

1011
### Overview
1112

12-
The main entry-point for reading/writing ELF Object file is the [`ElfObjectFile`](https://github.com/xoofx/LibObjectFile/blob/master/src/LibObjectFile/Elf/ElfObjectFile.cs) class.
13+
The main entry-point for reading/writing ELF Object file is the [`ElfFile`](https://github.com/xoofx/LibObjectFile/blob/master/src/LibObjectFile/Elf/ElfFile.cs) class.
1314

1415
This class is the equivalent of the ELF Header and contains also sections and segments (program headers)
1516

1617
![ELF class diagram](elf_class_diagram.png)
1718

1819
### ELF Reading
1920

20-
The ELF API allows to read from a `System.IO.Stream` via the method `ElfObjectFile.Read`:
21+
The ELF API allows to read from a `System.IO.Stream` via the method `ElfFile.Read`:
2122

2223
```C#
23-
ElfObjectFile elf = ElfObjectFile.Read(inputStream);
24+
ElfFile elf = ElfFile.Read(inputStream);
2425
```
2526

2627
### ELF Writing
2728

2829
You can create an ELF object in memory and save it to the disk:
2930

3031
```c#
31-
var elf = new ElfObjectFile();
32+
var elf = new ElfFile();
3233

3334
var codeStream = new MemoryStream();
3435
codeStream.Write(Encoding.UTF8.GetBytes("This is a text"));
3536
codeStream.Position = 0;
3637

3738
// Create a .text code section
3839
var codeSection = new ElfCustomSection(codeStream).ConfigureAs(ElfSectionSpecialType.Text);
39-
elf.AddSection(codeSection);
40+
elf.Add(codeSection);
4041

4142
// Always required if sections are added
42-
elf.AddSection(new ElfSectionHeaderStringTable());
43+
elf.Add(new ElfSectionHeaderStringTable());
44+
45+
// Always required if sections are added
46+
elf.Add(new ElfSectionHeaderTable());
4347

4448
using var outputStream = File.OpenWrite("test.out");
4549
elf.Write(outputStream);
@@ -49,7 +53,7 @@ elf.Write(outputStream);
4953

5054
#### Print
5155

52-
You can print an object to a similar textual format than `readelf` by using the extension method `ElfObjectFile.Print(TextWriter)`:
56+
You can print an object to a similar textual format than `readelf` by using the extension method `ElfFile.Print(TextWriter)`:
5357

5458
```c#
5559
// Using the previous code to create an ELF with a code section
@@ -108,13 +112,13 @@ The `Print` is trying to follow `readelf` from as compiled on `Ubuntu 18.04`. It
108112

109113
#### Section Header String Table
110114

111-
When sections are added to an `ElfObjectFile.Sections`, it is required to store their names in a Section Header String Table (`.shstrtab`)
115+
When sections are added to an `ElfFile.Sections`, it is required to store their names in a Section Header String Table (`.shstrtab`)
112116

113117
In that case, you need to add explicitly an `ElfSectionHeaderStringTable` to the sections:
114118

115119
```C#
116120
// Always required if sections are added
117-
elf.AddSection(new ElfSectionHeaderStringTable());
121+
elf.Add(new ElfSectionHeaderStringTable());
118122
```
119123

120124
This section can be put at any places in the sections, but is usually put at the end.
@@ -125,43 +129,43 @@ There is a type of section called `ElfShadowSection` which are only valid at run
125129

126130
A shadow section is used by an `ElfSegment` for which a region of data might not be associated with an existing section. In that case, you still want to associate data with the segment.
127131

128-
This is specially required when working with executable that don't have any sections but have only segments/program headers. In that case, `ElfObjectFile.Read` will create `ElfCustomShadowSection` for each part of the file that are being referenced by an `ElfSegment`.
132+
This is specially required when working with executable that don't have any sections but have only segments/program headers. In that case, `ElfFile.Read` will create `ElfCustomShadowSection` for each part of the file that are being referenced by an `ElfSegment`.
129133

130134
#### Null section and Program Header Table
131135

132-
The null section `ElfNullSection` must be put as the first section of an `ElfObjectFile`. It is the default when creating an `ElfObjectFile`.
136+
The null section `ElfNullSection` must be put as the first section of an `ElfFile`. It is the default when creating an `ElfFile`.
133137

134138
The Program Header Table is implemented as the `ElfProgramHeaderTable` shadow section and is added right after the `NullSection`. This is required because a segment of type `PHDR` will reference it while it is not an actual section in the original ELF file.
135139

136140
#### ELF Layout
137141

138-
An `ElfObjectFile` represents an ELF Object File in memory that can be freely modified. Unlike its serialized version on the disk, the offsets and size of the sections and segments references can be changed dynamically.
142+
An `ElfFile` represents an ELF Object File in memory that can be freely modified. Unlike its serialized version on the disk, the offsets and size of the sections and segments references can be changed dynamically.
139143

140-
You can force the computation of the layout of an ELF object file before saving it to the disk by using the method `ElfObjectFile.UpdateLayout`:
144+
You can force the computation of the layout of an ELF object file before saving it to the disk by using the method `ElfFile.UpdateLayout`:
141145

142146
```C#
143-
ElfObjectFile elf = ...;
147+
ElfFile elf = ...;
144148

145149
// Update layout (ObjectFile.Layout, all offsets of Sections and Segments)
146150
elf.UpdateLayout();
147151

148152
foreach(var section in elf.Sections)
149153
{
150-
// Section.Offset is now calculated as it was on the disk
151-
Console.WriteLine($"Section {section} Offset: 0x{section.Offset:x16}");
154+
// Section.Position is now calculated as it was on the disk
155+
Console.WriteLine($"Section {section} Offset: 0x{section.Position:x16}");
152156
}
153157
```
154158

155159
#### Diagnostics and verification
156160

157-
An `ElfObjectFile` can be created in memory with an invalid configuration (e.g missing a link between a symbol table and a string table).
161+
An `ElfFile` can be created in memory with an invalid configuration (e.g missing a link between a symbol table and a string table).
158162

159-
You can verify the validity of an `ElfObjectFile` by calling `ElfObjectFile.Verify`
163+
You can verify the validity of an `ElfFile` by calling `ElfFile.Verify`
160164

161165
```C#
162-
ElfObjectFile elf = ...;
166+
ElfFile elf = ...;
163167

164-
// Verify the validity of an ElfObjectFile instance
168+
// Verify the validity of an ElfFile instance
165169
var diagnostics = elf.Verify();
166170

167171
// If we have any errors, we can iterate on diagnostic messages
@@ -215,7 +219,7 @@ arFile.Write(outputStream);
215219

216220
Although the example above is storing a text, one of the main usage of an `ar` archive is to store object-file format (e.g `ELF`)
217221

218-
If you want to store direct an `ElfObjectFile` you can use the `ArElfFile` to add an ELF object-file directly to an archive.
222+
If you want to store direct an `ElfFile` you can use the `ArElfFile` to add an ELF object-file directly to an archive.
219223

220224
### Symbol Table
221225

@@ -231,7 +235,7 @@ var arFile = new ArArchiveFile();
231235
var symbolTable = new ArSymbolTable();
232236
arFile.AddFile(symbolTable);
233237
// Create an ELF
234-
var elf = new ElfObjectFile();
238+
var elf = new ElfFile();
235239
// ... fill elf, add symbols
236240
arFile.AddFile(elf);
237241
// Add a symbol entry

readme.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,29 @@ LibObjectFile is a .NET library to read, manipulate and write linker and executa
1919
```C#
2020
// Reads an ELF file
2121
using var inStream = File.OpenRead("helloworld");
22-
var elf = ElfObjectFile.Read(inStream);
22+
var elf = ElfFile.Read(inStream);
2323
foreach(var section in elf.Sections)
2424
{
2525
Console.WriteLine(section.Name);
2626
}
2727
// Print the content of the ELF as readelf output
2828
elf.Print(Console.Out);
29-
// Write the ElfObjectFile to another file on the disk
29+
// Write the ElfFile to another file on the disk
3030
using var outStream = File.OpenWrite("helloworld2");
3131
elf.Write(outStream);
3232
```
3333

3434
## Features
3535
- Full support of **Archive `ar` file format** including Common, GNU and BSD variants.
3636
- Full support for the **PE file format**
37+
- Support byte-to-byte roundtrip
3738
- Read and write from/to a `System.IO.Stream`
3839
- All PE Directories are supported
3940
- `PEFile.Relocate` to relocate the image base of a PE file
4041
- `PEFile.Print` to print the content of a PE file to a textual representation
4142
- Support for calculating the checksum of a PE file
4243
- - Good support for the **ELF file format**:
44+
- Support byte-to-byte roundtrip
4345
- Read and write from/to a `System.IO.Stream`
4446
- Handling of LSB/MSB
4547
- Support the following sections:

0 commit comments

Comments
 (0)