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

Invalid ELF header size or alignment #24

Closed
tw4452852 opened this issue Jun 30, 2021 · 3 comments
Closed

Invalid ELF header size or alignment #24

tw4452852 opened this issue Jun 30, 2021 · 3 comments

Comments

@tw4452852
Copy link
Contributor

A weird issue occurred today that puzzled me a lot. When I used load_file(xxx), everything worked fine. But if I used load(include_bytes!(xxx), None), it produced this error:

Error: error parsing BPF object

Caused by:
    0: error parsing ELF data
    1: Invalid ELF header size or alignment

Anyone knows why? Thanks in advance.

@ecarrara
Copy link

@tw4452852 i think you should align you array of bytes.

Zero-copy buffer mode
bpf devices may also operate in the BPF_BUFMODE_ZEROCOPY mode, in which
packet data is written directly into two user memory buffers by the ker-
nel, avoiding both system call and copying overhead. Buffers are of
fixed (and equal) size, page-aligned, and an even multiple of the page
size. The maximum zero-copy buffer size is returned by the BIOCGETZMAX
ioctl. Note that an individual packet larger than the buffer size is
necessarily truncated.
https://www.freebsd.org/cgi/man.cgi?bpf(4)

What works for me:

macro_rules! include_bytes_align_as {
    ($align_ty:ty, $path:literal) => {{
        #[repr(C)]
        pub struct AlignedAs<Align, Bytes: ?Sized> {
            pub _align: [Align; 0],
            pub bytes: Bytes,
        }

        static ALIGNED: &AlignedAs<$align_ty, [u8]> = &AlignedAs {
            _align: [],
            bytes: *include_bytes!($path),
        };

        &ALIGNED.bytes
    }};
}

static PROGRAM_DATA: &'static [u8] = include_bytes_align_as!(
    u32,
    "../../target/bpfel-unknown-none/release/libpq-tracer-bpf"
);

    // ..
    let mut bpf = Bpf::load(PROGRAM_DATA.into(), None)?;

I copied the include_bytes_align_as macro from here.

@tw4452852
Copy link
Contributor Author

Wow, thanks @ecarrara , Now it's clear now.

@willfindlay
Copy link
Member

willfindlay commented Oct 25, 2021

Just an FYI for folks coming here from their favourite search engine: As of #76 we will have aya::include_bytes_aligned which can be used instead.

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

3 participants