Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load data using the correct byte order. #26

Open
emberian opened this issue May 4, 2016 · 1 comment
Open

Load data using the correct byte order. #26

emberian opened this issue May 4, 2016 · 1 comment

Comments

@emberian
Copy link
Contributor

emberian commented May 4, 2016

Except for the type named HeaderPt1, the rest of the ELF data uses the byte order indicated by the e_ident. Currently data will be read using the host's native byte order, which isn't correct in general.

It's not clear to me how to fix this, as the parsing approach taken by zero isn't particularly friendly to reinterpreting data.

@emberian
Copy link
Contributor Author

emberian commented May 4, 2016

One option is to add a phantom type indicating the byte order of the data, and wrapping all multibyte values with a ByteOrder<E, T>, where T is the primitive numeric type being wrapped and E is the byte order direction. This E phantom type would need to be threaded throughout all of the everything. Parsing an ELF structure would then be some form of:

fn parse_header<E: ByteOrder>(input: &[u8]) -> Option<Header<'a, E>>  {
    let ident = parse_ident(input);
    if E::same_byteorder(ident.data) {
        Some(f(parse_header(input)))
    } else {
        None
    }
}

Most code can be generic over the byteorder marker completely, but it's correctness-critical that constructing the wrapper type enforces the correct byteorder.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant