Skip to content

Implemented a linkage attribute for functions and foreign statics. #9966

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
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions src/librustc/middle/trans/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,8 +503,40 @@ pub fn set_inline_hint(f: ValueRef) {
lib::llvm::SetFunctionAttribute(f, lib::llvm::InlineHintAttribute)
}

pub fn set_llvm_linkage_attr(attrs: &[ast::Attribute], ll: ValueRef) {
use lib::llvm::*;
use syntax::attr::*;

// Convert to the LLVM linkage values.
let linkage = match find_linkage_attr(attrs) {
LinkageDefault => { return; },
LinkageExternal => ExternalLinkage,
LinkageAvailableExternally => AvailableExternallyLinkage,
LinkageLinkOnceAny => LinkOnceAnyLinkage,
LinkageLinkOnceODR => LinkOnceODRLinkage,
LinkageLinkOnceODRAutoHide => LinkOnceODRAutoHideLinkage,
LinkageWeakAny => WeakAnyLinkage,
LinkageWeakODR => WeakODRLinkage,
LinkageAppending => AppendingLinkage,
LinkageInternal => InternalLinkage,
LinkagePrivate => PrivateLinkage,
LinkageDLLImport => DLLImportLinkage,
LinkageDLLExport => DLLExportLinkage,
LinkageExternalWeak => ExternalWeakLinkage,
LinkageGhost => GhostLinkage,
LinkageCommon => CommonLinkage,
LinkageLinkerPrivate => LinkerPrivateLinkage,
LinkageLinkerPrivateWeak => LinkerPrivateWeakLinkage
};
lib::llvm::SetLinkage(ll, linkage);
}

pub fn set_llvm_fn_attrs(attrs: &[ast::Attribute], llfn: ValueRef) {
use syntax::attr::*;

// Change the function linkage if requested.
set_llvm_linkage_attr(attrs, llfn);

// Set the inline hint if there is one
match find_inline_attr(attrs) {
InlineHint => set_inline_hint(llfn),
Expand Down Expand Up @@ -2645,9 +2677,7 @@ pub fn get_item_val(ccx: @mut CrateContext, id: ast::NodeId) -> ValueRef {
let ty = type_of(ccx, ty);
llvm::LLVMAddGlobal(ccx.llmod, ty.to_ref(), buf)
};
if attr::contains_name(ni.attrs, "weak_linkage") {
lib::llvm::SetLinkage(g, lib::llvm::ExternalWeakLinkage);
}
set_llvm_linkage_attr(ni.attrs, g);
g
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/libstd/rt/crate_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ pub struct CrateMap<'self> {
#[cfg(not(windows))]
pub fn get_crate_map() -> Option<&'static CrateMap<'static>> {
extern {
#[weak_linkage]
#[weak_linkage] // NOTE: remove after next snapshot
#[linkage(external_weak)]
#[link_name = "_rust_crate_map_toplevel"]
static CRATE_MAP: CrateMap<'static>;
}
Expand Down
56 changes: 56 additions & 0 deletions src/libsyntax/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,62 @@ pub fn find_inline_attr(attrs: &[Attribute]) -> InlineAttr {
}
}

#[deriving(Eq)]
pub enum LinkageAttr {
LinkageDefault,
LinkageExternal,
LinkageAvailableExternally,
LinkageLinkOnceAny,
LinkageLinkOnceODR,
LinkageLinkOnceODRAutoHide,
LinkageWeakAny,
LinkageWeakODR,
LinkageAppending,
LinkageInternal,
LinkagePrivate,
LinkageDLLImport,
LinkageDLLExport,
LinkageExternalWeak,
LinkageGhost,
LinkageCommon,
LinkageLinkerPrivate,
LinkageLinkerPrivateWeak
}

/// Find and translate #[linkage(name)] in the list of attrs.
pub fn find_linkage_attr(attrs: &[Attribute]) -> LinkageAttr {
do attrs.iter().fold(LinkageDefault) |la,attr| {
match attr.node.value.node {
MetaList(n, ref items) if "linkage" == n => {
do items.iter().fold(la) |la,item| {
let linkage : &str = item.name();
match linkage {
"external" => LinkageExternal,
"available_externally" => LinkageAvailableExternally,
"link_once_any" => LinkageLinkOnceAny,
"link_once_odr" => LinkageLinkOnceODR,
"link_once_odr_auto_hide" => LinkageLinkOnceODRAutoHide,
"weak_any" => LinkageWeakAny,
"weak_odr" => LinkageWeakODR,
"appending" => LinkageAppending,
"internal" => LinkageInternal,
"private" => LinkagePrivate,
"dll_import" => LinkageDLLImport,
"dll_export" => LinkageDLLExport,
"external_weak" => LinkageExternalWeak,
"ghost" => LinkageGhost,
"common" => LinkageCommon,
"linker_private" => LinkageLinkerPrivate,
"linker_private_weak" => LinkageLinkerPrivateWeak,
_ => la
}
}
}
_ => la
}
}
}

/// Tests if any `cfg(...)` meta items in `metas` match `cfg`. e.g.
///
/// test_cfg(`[foo="a", bar]`, `[cfg(foo), cfg(bar)]`) == true
Expand Down
21 changes: 21 additions & 0 deletions src/test/auxiliary/extern_foreign.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#[link(name="extern_foreign", vers="0.0")];

extern "C" {
fn foreign();
}

#[fixed_stack_segment]
pub unsafe fn doer() {
foreign();
}

23 changes: 23 additions & 0 deletions src/test/run-pass/attr-linkage-external-foreign.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// aux-build:extern_foreign.rs

extern mod extern_foreign;

#[linkage(external)]
#[no_mangle]
pub extern "C" fn foreign() {
}

pub fn main() {
unsafe { extern_foreign::doer() };
}

34 changes: 34 additions & 0 deletions src/test/run-pass/attr-linkage-external.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#[linkage(external)]
#[inline(always)] // Force LLVM to consider the function for removal.
#[no_mangle]
fn foobar() {}

// The following tests call foobar directly via inline assembly, using bl on ARM and call on x86.
// This is done so that LLVM's decisions aren't affected by the reference to foobar, and without
// #[linkage(external)], LLVM fails with the error "undefined reference to `foobar'".

#[cfg(target_arch = "arm")]
pub fn main() {
foobar();
unsafe { asm!("bl foobar"); }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we have a comment about why this test is using asm!?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It calls foobar via inline asm to check that LLVM keeps the symbol around.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that, but a comment (in the code) makes it much easier for a person to work out what the test does when they accidentally break it in a year (or whenever).

}

#[cfg(target_arch = "x86")]
#[cfg(target_arch = "x86_64")]
pub fn main() {
foobar();
unsafe { asm!("call foobar"); }
}

#[cfg(not(target_arch = "arm"), not(target_arch = "x86"), not(target_arch = "x86_64"))]
pub fn main() {}