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

Add link_section attribute for static and fn items #7958

Closed
wants to merge 2 commits into from

Conversation

kemurphy
Copy link
Contributor

This allows for control over the section placement of static, static
mut, and fn items. One caveat is that if a static and a static mut are
placed in the same section, the static is declared first, and the static
mut is assigned to, the generated program crashes. For example:

[link_section=".boot"]

static foo : uint = 0xdeadbeef;

[link_section=".boot"]

static mut bar : uint = 0xcafebabe;

Declaring bar first would mark .bootdata as writable, preventing the
crash when bar is written to.

@alexcrichton
Copy link
Member

That seems odd that the program crashes as a result of that. Could you detect this to generate a lint/error? That definitely seems like non-obvious behavior which might bite some people.

This allows for control over the section placement of static, static
mut, and fn items.  One caveat is that if a static and a static mut are
placed in the same section, the static is declared first, and the static
mut is assigned to, the generated program crashes.  For example:

#[link_section=".boot"]
static foo : uint = 0xdeadbeef;

#[link_section=".boot"]
static mut bar : uint = 0xcafebabe;

Declaring bar first would mark .bootdata as writable, preventing the
crash when bar is written to.
@kemurphy
Copy link
Contributor Author

@alexcrichton: This seems expected to me. By defualt, rustc tells LLVM to emit static items in a read-only section (typically .rodata), and static mut items in a writable section (.data). Whatever new section gets defined takes the mode of the first thing that goes in it. The equivalent code in C is:

const unsigned foo attribute ((section ("boot"))) = 10;
unsigned bar attribute ((section ("boot"))) = 15;

GCC will throw an error on that, saying there's a section type mismatch. Clang, on the other hand, accepts it and segfaults if you assign to bar. This makes me think it's LLVM's problem.

@alexcrichton
Copy link
Member

Looking at the LLVM source:

class GlobalValue {
  ...
  std::string Section;
  ...
  void setSection(StringRef S) { Section = S; }
}

added with

class StringRef {
  ...
  std::string str() const {             
    if (Data == 0) return std::string();
    return std::string(Data, Length);   
  }                                     
  operator std::string() const {
    return str();               
  }                             
}

I think that assigning into the GlobalValue actually does copy the data so it's OK that the pointer doesn't last forever.

@alexcrichton
Copy link
Member

@kemurphy Is there a reason to call it link_section instead of section like with C?

@kemurphy
Copy link
Contributor Author

@alexcrichton: Not really, it was arbitrary; "section" just seemed a little bare and there's already another attribute prefixed with "link_" somewhere. Is "section" preferred? #rust didn't seem to have a preference when I asked about it.

@alexcrichton
Copy link
Member

Hmm... I'd be fine with link_section then. Could you add a test to at least just exercise to make sure that it doesn't die when talking to LLVM? I'm not sure if you could write a test to ensure that it actually ends up in the specified section, but a test could certainly at least exercise the attribute.

@kemurphy
Copy link
Contributor Author

dbaupp suggested to let it land and then ask graydon what the test is capable of doing, but yeah I can add a test to just exercise the path.

@huonw
Copy link
Member

huonw commented Jul 23, 2013

@graydon: is it possible for the test runner to somehow check that a certain symbol ends up in a specific section (even just on one platform)?

(@kemurphy, I'm dbaupp on IRC, btw.)

@kemurphy
Copy link
Contributor Author

@huonw: Ah, cool. :) I did think of one failing test idea: put a static mut in .rodata and make sure writing to it causes a segfault. Also, putting a function in .data might have an interesting failure mode.

bors added a commit that referenced this pull request Jul 24, 2013
This allows for control over the section placement of static, static
mut, and fn items.  One caveat is that if a static and a static mut are
placed in the same section, the static is declared first, and the static
mut is assigned to, the generated program crashes.  For example:

#[link_section=".boot"]
static foo : uint = 0xdeadbeef;

#[link_section=".boot"]
static mut bar : uint = 0xcafebabe;

Declaring bar first would mark .bootdata as writable, preventing the
crash when bar is written to.
@bors bors closed this Jul 24, 2013
@kemurphy kemurphy deleted the link-section branch April 30, 2014 10:48
flip1995 pushed a commit to flip1995/rust that referenced this pull request Mar 14, 2022
…les, r=camsteffen

Improve styles of filtering options for Clippy's lint list

Partially solves rust-lang#7958

Updated styles for filtering options. It now uses dropdown menus.

![image](https://user-images.githubusercontent.com/19844144/144608479-cdd9de0b-f101-4d49-a135-0969efb01a11.png)

changelog: none
xFrednet pushed a commit to xFrednet/rust that referenced this pull request May 21, 2022
Add version filtering option to the lint list

I'm no web dev, so I don't know if this is the best execution 😄.

Here's how it looks:

![Desktop](https://user-images.githubusercontent.com/69764315/165403602-9e39fe0f-6a96-46cb-ba51-8b035e74cce4.gif)

And on mobile:

![Mobile](https://user-images.githubusercontent.com/69764315/165403908-fc4a0051-2ed4-48a3-b45b-b261f49ce259.png)

I've split this into two commits, in the second one I moved the JS into its own file to make it easier to work on. Is that alright? And if so, could the same thing be done to the css?

changelog: none
cc: rust-lang#7958, `@repi`
r? `@xFrednet`
flip1995 pushed a commit to flip1995/rust that referenced this pull request Oct 20, 2022
…=xFrednet

Add Default to Clippy Lints Lint groups

- related to rust-lang#7958

This PR adds a default (reset) button to Clippy Lints Lint groups. (change for website)
[The page](https://rust-lang.github.io/rust-clippy/master/index.html) sets only `Deprecated` to false by default.
Certainly it is easy to set only `deprecated` to false, but it may be a bit lazy for beginners.

https://user-images.githubusercontent.com/38400669/194831117-3ade7e0d-c4de-4189-9daf-3be8ea3cdd18.mov

changelog: none
flip1995 pushed a commit to flip1995/rust that referenced this pull request Jun 27, 2024
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 this pull request may close these issues.

5 participants