420 Commits

Author SHA1 Message Date
Christopher Cole
94305929e1
Update README.md example to use open_stream 2022-10-10 22:28:41 -07:00
Christopher Cole
8219e2727b
Convert symbol parsing to use ReadExt
This allows us to remove the last usages of the non-trait read_u* methods in
parse. Now all endian-aware integer reads go through ReadExt.
2022-10-10 20:03:58 -07:00
Christopher Cole
039ba0e17a
Run rustfmt 2022-10-10 19:43:12 -07:00
Christopher Cole
d31168c9eb
Change the parse module to be internal to the crate
There's no current need for users of this elf crate to implement their own
parsers, so this doesn't need to be public and clutter the public interface.
2022-10-10 19:41:52 -07:00
Christopher Cole
ae7103c9d4
Move File into file.rs
lib.rs now only contains mod and use statements :)
2022-10-10 19:40:21 -07:00
Christopher Cole
aa0235195e
Move ParseError into parse.rs 2022-10-10 19:33:48 -07:00
Christopher Cole
2693cbf659
Move Section into section.rs 2022-10-10 19:28:09 -07:00
Christopher Cole
9530771c45
Add seek and read_exact onto ReadExt trait
This will allow parse methods that take a generic ReadExt to use those methods too
2022-10-10 19:06:12 -07:00
Christopher Cole
3455f4dd1e
Add a simple StringTable wrapper around string table section bytes
This interprets the string table section's data bytes as &str references with
lifetime checking bound to the underlying section data lifetime, so the
underyling data bytes for the string contents aren't copied or re-allocated
when getting a string from the table.

It does do two read passes over the string contents, first to find the
terminating NUL byte and second to do Utf8 validation checking.

I opted to do the Utf8 checking because my goal for this crate is to have zero
unsafe code in it, and the unchecked from_utf8 is unsafe. I have not yet
thought through what opinion I want this crate to have w.r.t. reading malformed
data in general but so far I've been making it return errors as it encounters
it.
2022-10-10 13:55:46 -07:00
Christopher Cole
a7ce73ee98
Add a delegating read_exact() on Reader so we can use the same Reader for more things 2022-10-07 15:26:07 -07:00
Christopher Cole
81112fcd51
Add a delegating seek() on Reader so we can use the same Reader for more things 2022-10-07 15:22:42 -07:00
Christopher Cole
e2ead26514
Change ReadExt trait bounds to require both std::io::Read and std::io::Seek
The addition of Seek will allow ReadExt/Reader to be used in more places (such as
reading section data which requires seeking to new offsets.
2022-10-07 15:05:55 -07:00
Christopher Cole
3fc22a31bf
Change the Parse trait to take a ReadExt
This eliminates the Endian argument from Parse::parse by bundling it into the ReadExt's responsibility and
allows implementors of Parse to not have to care about endianness and plumbing it around correctly.

Also, change FileHeader::parse to no longer implement Parse. It previously needed to take bogus unused Endian and Class
specifications. There's a unique bootstrapping situation with parsing the FileHeader, as it is the structure that informs
the Endian and Class specifications used by all the other parsers. I opted to simply make this implement parse() (but not
for the trait Parse) by taking the underlying delegate reader rather than a ReadExt which seemed intuitive to me.
2022-10-07 12:53:36 -07:00
Christopher Cole
79a54e21cb
Update the Parse implementers to use ReadExt/Reader
This is a partial patch that starts us using the new interface in a minimal but non-optimal way
The next step is to change the Parse trait to take a ReadExt instead of a std::io::Read
2022-10-07 12:02:36 -07:00
Christopher Cole
28badf52f7
Add a ReadExt trait and a Reader impl that can encapsulate the endianness config
This will be used to obviate the need to pass around the Endian, Class tuples all over the place
when parsing, as the reader will remember them.
My longer-term thought is that the File object will create and remember one of these Readers which
will be able to also be used to make some of this parsing lazy.
2022-10-07 11:57:47 -07:00
Christopher Cole
e14e083ad3 Move endianness-aware read_u* parsing methods into parse.rs
This is getting us closer to having a more centralized trait-driven parser combinator approach
that can hopefully clean up the code a bit and make it easier to follow.
2022-10-06 18:02:24 -07:00
Christopher Cole
eca60d8f49 Decouple the Ehdr.e_data endianness byte and the parsing endianness switch
This patch adds verification to the initial e_ident[] parsing verification which returns
an error if the file's e_ident contains an unknown endianness value. This allows the later
integer parsing methods (read_u*) to assume that they are given a valid endianness configuration
(either little or big).
2022-10-06 17:53:40 -07:00
Christopher Cole
34ab5c54b7 Move STT_* and STB_* and STV_* constants into the gabi module 2022-10-06 15:14:32 -07:00
Christopher Cole
11b7addf97 Specify edition 2021 and fix it up
```
$ cargo fix --edition
    Checking elf v0.0.12 (/Users/chris/code/rust-elf)
   Migrating src/lib.rs from 2015 edition to 2018
       Fixed src/lib.rs (1 fix)
       Fixed src/file.rs (3 fixes)
       Fixed src/parse.rs (1 fix)
       Fixed src/section.rs (4 fixes)
       Fixed src/segment.rs (4 fixes)
       Fixed src/utils.rs (3 fixes)
       Fixed src/segment.rs (4 fixes)
       Fixed src/lib.rs (1 fix)
       Fixed src/section.rs (4 fixes)
       Fixed src/file.rs (3 fixes)
    Finished dev [unoptimized + debuginfo] target(s) in 0.47s
```
2022-10-06 13:49:07 -07:00
Christopher Cole
8427ef124e Move Symbol type definitions into symbol.rs
Also, remove types.rs now that its empty! Yay!
2022-10-06 13:35:10 -07:00
Christopher Cole
c6e4073af4 Implement FileHeader parsing with the Parse trait
Also move FileHeader into file.rs
2022-10-06 13:26:19 -07:00
Christopher Cole
3abd5b445e Move SHT_* and SHF_* constants into the gabi module 2022-10-06 12:07:04 -07:00
Christopher Cole
d9dd934f8c Change Parse trait implementers to shorten/rename utils::read_* imports to just the method names
This declutters the code a bit. It's obvious what these methods are doing and
its not important to state which module they're defined in on each invocation.
2022-10-06 01:17:41 -07:00
Christopher Cole
fcac7d79cc Implement SectionHeader parsing with the Parse trait
Also move SectionHeader into section.rs
Also, refactor the code for reading section data and section names from the section string table to be more idiomatic
2022-10-06 01:06:04 -07:00
Christopher Cole
ae9e24420d Simplify ParseError to implement the std::error::Error trait
This addresses issue #15
2022-10-05 22:39:04 -07:00
Christopher Cole
c3b560d3ae Move PF_* and PT_* constants into the gabi module 2022-10-05 20:47:07 -07:00
Christopher Cole
ea991f2c7f Implement ProgramHeader parsing with the Parse trait
Also move ProgramHeader into segment.rs
2022-10-05 20:18:09 -07:00
Christopher Cole
9aab0d1096 Add simple Parse trait
My plan is to make the various ELF data structures implement this Parse trait which knows
how to parse that structure based on the given endianness and data width (32- or 64-bit).
2022-10-05 20:14:41 -07:00
Christopher Cole
a1ac44be39 Remove unused .travis.yml now that we use github actions 2022-10-05 02:10:01 -07:00
Christopher Cole
f9d4b29ea9 Remove unnecessary macro_use from utils
It no longer implements any macros
Also, remove the pub on mod utils, since it's only needed internally and shouldn't be considered part of the public interface
2022-10-04 17:19:24 -07:00
Christopher Cole
8fabd83f8b Remove unnecessary File::new and FileHeader::new 2022-10-04 17:06:13 -07:00
Christopher Cole
3354ce5553 Extract parse_ehdr function that handles 32/64 bit Elf_Ehdr parsing
with some simple unit tests
2022-10-04 16:49:04 -07:00
Christopher Cole
147b8c404b Run rustfmt with default config 2022-10-04 15:30:06 -07:00
Christopher Cole
75adb4f1a5 Extract ELF File Headdr e_ident[] parsing into its own tested function 2022-10-04 13:57:39 -07:00
Christopher Cole
8373835dd8 Compare ELFMAGIC bits as a byte slice 2022-10-04 12:31:52 -07:00
Christopher Cole
aff2d776ee Add the rest of the ELF File Header e_* fields to FileHeader
These were already being parsed but not put onto the struct.
2022-10-03 22:45:17 -07:00
Christopher Cole
4db2b9065e Split Generic System V Application Binary Interface ELF File Header constants out into gabi.rs
This patch starts changing the type representations for our ELF types. It splits out the raw constants defined in
the Generic System V Application Binary Interface out into its own file (gabi.rs) as native type constants. The
type definitions in types.rs are now left to be higher level semantic representations of these fields which may
or may not directly map to the on-disk ELF data layout.

The intent here is to structure the code such that the opinionated rust representation of these parsed ELF structures
live separately from the official gabi definitions. This could set us up for a future where consumers of this library that
desire their own different rust representations of parsed ELF structures to consume the gabi definitions without
also having to much about with our representations here.
2022-10-03 20:34:23 -07:00
Christopher Cole
4965746e79 Update Cargo.toml documentation link to point to docs.rs 2022-10-03 17:57:45 -07:00
Christopher Cole
6a4bd829cf Bump crate version to 0.0.12 to remove byteorder dependency 2022-10-03 17:44:47 -07:00
Christopher Cole
9b152e99de Replace byteorder crate dependency with safe transmute methods from std:: integer types 2022-10-03 17:38:22 -07:00
Christopher Cole
1354885276
Use docs.rs for Documentation link in README.md 2022-10-03 12:33:37 -07:00
Christopher Cole
c772d63b93
Change README.md badges
Point them to github action results, crates.io, and docs.rs
2022-10-03 12:28:09 -07:00
Christopher Cole
f9cfa292c1
Add initial github action CI configuration 2022-10-03 12:12:52 -07:00
Christopher Cole
a42f74c08d Add EM_BPF for linux eBPF vm programs 2022-10-03 00:33:15 -07:00
Christopher Cole
2c428df23c Sync ELF File Header e_machine definitions into type definitions
Values and Names pulled from http://www.sco.com/developers/gabi/latest/ch4.eheader.html
2022-10-03 00:14:39 -07:00
Christopher Cole
ce65abaecc Update byteorder to 1.x and bump our crate version now that this builds on rust 1.64.0 2022-10-02 23:19:44 -07:00
Christopher Cole
c4a9732fef Prefix unused parsing vars with _ to signal to the compiler that its intended 2022-10-02 23:04:19 -07:00
Christopher Cole
e1ba759253 Use a ParseError::EndianError rather than panicking 2022-10-02 23:02:43 -07:00
Christopher Cole
e604816c15 Replace usages of try with the ? operator 2022-10-02 23:02:07 -07:00
Christopher Cole
48e4313ce4 Use the dyn keyword to specify parse_symbol takes a ref to a Read trait object. 2022-10-02 22:43:27 -07:00