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

Decide how to handle incorrect alignment #230

Closed
philipc opened this issue May 19, 2020 · 3 comments · Fixed by #231
Closed

Decide how to handle incorrect alignment #230

philipc opened this issue May 19, 2020 · 3 comments · Fixed by #231

Comments

@philipc
Copy link
Contributor

philipc commented May 19, 2020

See gimli-rs/addr2line#170 for an example of an ELF file with incorrect section header alignment.

We've got a few options for how to handle this:

  • Do nothing. Fix your toolchain instead. This option is okay for narrow use cases where we have control of the toolchain, but not suitable for a general purpose library.
  • Always handle any alignment. This means that on some architectures we'll generate slower code, which may be undesirable for use cases where we know the alignment is correct.
  • Relax the alignment constraints as needed. For the ELF file, we would change SectionHeader64 to only use types that require 4-byte alignment.
  • Add a compile time feature to select whether to support any alignment. This could be done by ensuring that types like U32 and U32Bytes have the same API, and making U32 an alias of U32Bytes when this feature is enabled. I think the only downside is that this prevents supporting both cases in the same application.
  • Add a trait parameter to all of the structs so that the application can decide which to support. This is unnecessary complication if one of the other options is good enough.
@philipc
Copy link
Contributor Author

philipc commented May 19, 2020

For prior art, capnp uses a feature flag.

@slonopotamus
Copy link

I believe that capnp is used in a much more controlled environments where it is possible to decide at compile time what alignment needs are. But in object case, files may come from external sources.

@philipc
Copy link
Contributor Author

philipc commented May 19, 2020

While files may come from external sources, they are still generally well formed for the architecture they are intended for. After all, I expect nearly all C code expects the alignment to be correct and reads the headers by directly casting from bytes, and bugs like the one in the PS4 toolchain only go unnoticed because it's for an architecture without alignment constraints.

So I think there are three cases:

  1. all files are for a native architecture with alignment constraints
  2. all files are for a native architecture with no alignment constraints
  3. files may be from a different architecture

If we used the feature flag approach, then it would need to be enabled for uses in cases 2 and 3.

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

Successfully merging a pull request may close this issue.

2 participants