Skip to content

link_section attribute misleadingly allowed on modules and impls  #65128

Closed

Description

@Earlz

The #[link_section] attribute seems to be allowed on modules and impl blocks, but it will not do anything. I would expect either all functions/statics within the module/impl to be placed into the specified link_section, but this does not happen.

Code used:

#[link_section = ".textaux1"]
mod foo{
    //#[link_section = ".textaux1"]
    pub fn testing() -> u32{
        20
    }
}
mod foo2{
    pub struct Whatever{}
    #[link_section = ".textaux1"]
    impl Whatever{
        //#[link_section = ".textaux1"]
        pub fn watt(&self) -> u32{
            50
        }
    }
}

I've verified the functions are linked in and not just optimized out etc. As-is the functions will be placed into the default .text section, but if I uncomment the link_section attributes then they are properly placed in the .textaux1 section. No compiler errors or warnings are generated

rustc version:
rustc 1.40.0-nightly (2daa404 2019-10-02)
binary: rustc
commit-hash: 2daa404
commit-date: 2019-10-02
host: x86_64-apple-darwin
release: 1.40.0-nightly
LLVM version: 9.0

Activity

  1. added
    A-attributesArea: Attributes (`#[…]`, `#![…]`)
    A-linkageArea: linking into static, shared libraries and binaries
    C-bugCategory: This is a bug.
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Oct 5, 2019
  2. hannobraun commented on Nov 27, 2019

    @hannobraun
    Contributor

    The same seems to be true for extern functions. I would have expected the following code to link the extern function into .data, but it was linked into .text:

    extern {
        #[link_section=".data"]
        #[inline(never)]
        fn write_half_page(address: *mut u32, words: *const u32);
    }

    No warning or error was emitted. The link_section attribute was just ignored, it seems.

  3. ehuss commented on Sep 15, 2020

    @ehuss
    Contributor

    A warning for this has been added in #73461.

  4. bjorn3 commented on Jul 29, 2023

    @bjorn3
    Member

    There is a future compat lint for this. Should this be closed?

  5. bjorn3 commented on Aug 28, 2025

    @bjorn3
    Member

    I'm going to close this given the future compat lint that warns against this.

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

    A-attributesArea: Attributes (`#[…]`, `#![…]`)A-linkageArea: linking into static, shared libraries and binariesC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions