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

link_section is not applied to global constants #46968

Closed
mjbshaw opened this issue Dec 23, 2017 · 5 comments
Closed

link_section is not applied to global constants #46968

mjbshaw opened this issue Dec 23, 2017 · 5 comments
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@mjbshaw
Copy link
Contributor

mjbshaw commented Dec 23, 2017

The link_section attribute doesn't get applied to global const values.

Consider the following program (which uses static instead of const, and correctly applies the link_section to HELLO):

#[link_section = ".data,__yes_this_is_a_custom_link_section"]
static HELLO: [u8; 6] = *b"hello\0";

extern {
  fn puts(_str: *const u8) -> i32;
}

fn main() {
  unsafe { puts(&HELLO as *const u8) };
}

As can be seen in the LLVM IR (Playground link), HELLO is correctly given the custom section assignment:

@_ZN10playground5HELLO17h62136d8dd807037cE = internal constant [6 x i8] c"hello\00", section ".data,__yes_this_is_a_custom_link_section", align 1

; playground::main
; Function Attrs: nounwind uwtable
define internal void @_ZN10playground4main17h4b9f3b1aa6035d1eE() unnamed_addr #0 {
start:
  %0 = tail call i32 @puts(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @_ZN10playground5HELLO17h62136d8dd807037cE, i64 0, i64 0))
  ret void
}

But if we change HELLO to be a const instead of static:

#[link_section = ".data,__yes_this_is_a_custom_link_section"]
const HELLO: [u8; 6] = *b"hello\0";

extern {
  fn puts(_str: *const u8) -> i32;
}

fn main() {
  unsafe { puts(&HELLO as *const u8) };
}

Then the string no longer has the custom section applied (Playground link):

@byte_str.0 = private unnamed_addr constant [6 x i8] c"hello\00", align 8

; playground::main
; Function Attrs: nounwind uwtable
define internal void @_ZN10playground4main17h4b9f3b1aa6035d1eE() unnamed_addr #0 {
start:
  %0 = tail call i32 @puts(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @byte_str.0, i64 0, i64 0))
  ret void
}

I think that Rust should propagate the link section to all instances of the string. That is, @byte_str.0 (and any other duplicate instances of the const) should be defined as @byte_str.0 = private unnamed_addr constant [6 x i8] c"hello\00", section ".data,__yes_this_is_a_custom_link_section", align 8.


Meta info: I've tested this in both 1.22.1 and 1.24.0-nightly (2017-12-22 5165ee9). Both produce the same results.

@hanna-kruppe
Copy link
Contributor

As far as Rust-the-language is concerned, consts are entities that don't have any linkage behavior at all. Any use of a const is as if the value as written out as a literal at the use site, and the const itself doesn't have an address or symbol name at all. For example, #[no_mangle] const X ... reports "error: const items should never be #[no_mangle], consider instead using pub static". Likewise there should be an error about other linkage-related attributes on consts.

@mjbshaw
Copy link
Contributor Author

mjbshaw commented Dec 23, 2017

Okay, well then it would make sense to disallow link_section on const. In my particular case I was really hoping to take advantage of the LLVM unnamed_addr attribute that gets applied to const Rust objects, but I can live without it and will use static.

@nagisa
Copy link
Member

nagisa commented Dec 24, 2017

Inapplicable attributes don’t really get "disallowed", that applies for most of them, really.

The principle is that item checks for attributes relevant to itself, rather than all attributes checked against the item they apply to. There are multiple good reasons why it is implemented as such and I don’t see the behaviour changing.

That being said, I find it amusing that the attribute-not-used warning does not get output, as constants should not bother checking for link_section attribute at all.

@nagisa nagisa added the A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. label Dec 24, 2017
@sfackler
Copy link
Member

There's a set of attributes that attribute-not-used lint doesn't look at since they're used in trans but lints run before that.

@XAMPPRocky XAMPPRocky added C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 10, 2018
@Enselic
Copy link
Member

Enselic commented Oct 6, 2023

Triage: Closing as won't fix based on last two comments.

@Enselic Enselic closed this as not planned Won't fix, can't repro, duplicate, stale Oct 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants