Skip to content

Can't link to an external thread local symbol #30795

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

Closed
mneumann opened this issue Jan 9, 2016 · 4 comments
Closed

Can't link to an external thread local symbol #30795

mneumann opened this issue Jan 9, 2016 · 4 comments

Comments

@mneumann
Copy link
Contributor

mneumann commented Jan 9, 2016

I am trying to link to

extern __thread int errno;

I try it with:

extern "C" {
    #[thread_local]
    #[link_name = "errno"]
    static errno: c_int;
}

But it seem that it omits the thread_local part. Any ideas how to get it to work? The linker tells me that I have both thread-local and non-thread local references to errno defined, so above links to the non-thread local.

@alexcrichton
Copy link
Member

I think this is because we don't recognize #[thread_local] on extern globals, for example:

#![feature(thread_local)]

extern {
    #[thread_local]
    #[link_name = "errno"]
    pub static errno: i32;
}

pub fn foo() -> i32 {
    unsafe { errno }
}
; ModuleID = 'foo.0.rs'
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-apple-darwin"

@errno = external global i32

; Function Attrs: uwtable
define i32 @_ZN3foo20h5ca9ce4ccbe13eabhaaE() unnamed_addr #0 {
entry-block:
  %0 = load i32, i32* @errno, align 4
  ret i32 %0
}

attributes #0 = { uwtable "no-frame-pointer-elim"="true" }

(note the lack of thread_local on the extern declaration)

@mneumann
Copy link
Contributor Author

@alexcrichton: DragonFly would need it in order to support errno. Otherwise we need a shim C wrapper again (__dfly_error). This was removed in some earlier commit. In my local patches I have added a rt/errno.c again, but in the long term this clearly is not the solution.

@alexcrichton
Copy link
Member

This may actually be easy enough to add to trans, I think you'll basically need to copy this code over into the foreign module as well.

It's possible that we could add back the C shim wrapper, although I'd prefer not to do so!

@mneumann
Copy link
Contributor Author

@alexcrichton : Thanks for pointing out the solution! I have a pull request (#30856) that fixes that. Much better than requiring a C wrapper! With that fix and some other libc related things, I can now build rust on DragonFly again.

bors added a commit that referenced this issue Feb 25, 2016
This will correctly add the thread_local attribute to the external static variable ```errno```:

```rust
extern {
     #[thread_local]
     static errno: c_int;
}
```

Before this commit, the thread_local attribute is ignored. Fixes #30795.

Thanks @alexcrichton for pointing out the solution.
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

2 participants