Skip to content

Allow specifying function alignment #75072

Closed

Description

@repnop

I've been recently writing a kernel for the RISC-V architecture, using strictly as much Rust as I can get away with and during which I've run into the need to have certain functions aligned to specific byte alignments (ex. any function placed in mtvec MUST be 4 byte aligned), and while I can seemingly accomplish this using asm!(".align <alignment>") I think it would make sense for that to be an attribute of the function itself, whether it be limited to extern "C" fns or valid on all ABIs. Example of what I am asking for:

#[no_mangle]
#[repr(align(4))]
extern "C" fn gets_put_in_mtvec() {
    // ... content here ...
}

Activity

  1. added
    C-feature-request
    T-lang
    on Aug 2, 2020
  2. hanna-kruppe commented on Aug 3, 2020

    @hanna-kruppe
    Contributor

    For context, how is this requirement addressed by C toolchains?

  3. repnop commented on Aug 3, 2020

    @repnop
    ContributorAuthor

    Using https://c.godbolt.org/z/E7ETx8 to verify, it looks like both GCC and Clang support using __attribute__((aligned(<alignment>))) to specify function alignment on a per-function basis

  4. nbdd0121 commented on Aug 3, 2020

    @nbdd0121
    Member

    I believe you would need to use assembly for interrupt handlers because naked functions don't work as intended anyway.

  5. Skallwar commented on Nov 17, 2020

    @Skallwar

    I'm on the exact same situation as @repnop, with the exact same context.
    asm!(".align <alignment>") inside the function worked for you?

  6. Skallwar commented on Nov 19, 2020

    @Skallwar

    If this is something that should be part of the language, I would gladly try to implement it

  7. sampersand commented on Jan 14, 2021

    @sampersand

    I'd also find this quite helpful for some low-level bit twiddling I'm doing.

  8. repnop commented on Jan 21, 2021

    @repnop
    ContributorAuthor

    @Skallwar oh, somehow I missed the notification from this issue. yeah, currently I'm (ab)using that in a #[naked] fn (I have no idea if it works for non-naked fns).

    Opened a PR so we'll see where this goes :)

  9. added a commit that references this issue on Apr 6, 2021
    a6e7a5a
  10. memoryruins commented on Apr 6, 2021

    @memoryruins
    Contributor

    The tracking issue for #![feature(fn_align)] is now #82232

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-feature-requestT-lang

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions