|
11 | 11 |
|
12 | 12 | use back::{link};
|
13 | 13 | use lib::llvm::llvm;
|
14 |
| -use lib::llvm::{ValueRef, CallConv, StructRetAttribute}; |
| 14 | +use lib::llvm::{ValueRef, CallConv, StructRetAttribute, Linkage}; |
15 | 15 | use lib;
|
16 | 16 | use middle::trans::base::push_ctxt;
|
17 | 17 | use middle::trans::base;
|
@@ -105,6 +105,105 @@ pub fn llvm_calling_convention(ccx: &CrateContext,
|
105 | 105 | })
|
106 | 106 | }
|
107 | 107 |
|
| 108 | +pub fn llvm_linkage_by_name(name: &str) -> Option<Linkage> { |
| 109 | + // Use the names from src/llvm/docs/LangRef.rst here. Most types are only |
| 110 | + // applicable to variable declarations and may not really make sense for |
| 111 | + // Rust code in the first place but whitelist them anyway and trust that |
| 112 | + // the user knows what s/he's doing. Who knows, unanticipated use cases |
| 113 | + // may pop up in the future. |
| 114 | + // |
| 115 | + // ghost, dllimport, dllexport and linkonce_odr_autohide are not supported |
| 116 | + // and don't have to be, LLVM treats them as no-ops. |
| 117 | + match name { |
| 118 | + "appending" => Some(lib::llvm::AppendingLinkage), |
| 119 | + "available_externally" => Some(lib::llvm::AvailableExternallyLinkage), |
| 120 | + "common" => Some(lib::llvm::CommonLinkage), |
| 121 | + "extern_weak" => Some(lib::llvm::ExternalWeakLinkage), |
| 122 | + "external" => Some(lib::llvm::ExternalLinkage), |
| 123 | + "internal" => Some(lib::llvm::InternalLinkage), |
| 124 | + "linker_private" => Some(lib::llvm::LinkerPrivateLinkage), |
| 125 | + "linker_private_weak" => Some(lib::llvm::LinkerPrivateWeakLinkage), |
| 126 | + "linkonce" => Some(lib::llvm::LinkOnceAnyLinkage), |
| 127 | + "linkonce_odr" => Some(lib::llvm::LinkOnceODRLinkage), |
| 128 | + "private" => Some(lib::llvm::PrivateLinkage), |
| 129 | + "weak" => Some(lib::llvm::WeakAnyLinkage), |
| 130 | + "weak_odr" => Some(lib::llvm::WeakODRLinkage), |
| 131 | + _ => None, |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +pub fn register_static(ccx: @CrateContext, |
| 136 | + foreign_item: @ast::ForeignItem) -> ValueRef { |
| 137 | + let ty = ty::node_id_to_type(ccx.tcx, foreign_item.id); |
| 138 | + let llty = type_of::type_of(ccx, ty); |
| 139 | + |
| 140 | + // Treat the crate map static specially in order to |
| 141 | + // a weak-linkage-like functionality where it's |
| 142 | + // dynamically resolved at runtime. If we're |
| 143 | + // building a library, then we declare the static |
| 144 | + // with weak linkage, but if we're building a |
| 145 | + // library then we've already declared the crate map |
| 146 | + // so use that instead. |
| 147 | + if attr::contains_name(foreign_item.attrs.as_slice(), "crate_map") { |
| 148 | + return if ccx.sess.building_library.get() { |
| 149 | + let s = "_rust_crate_map_toplevel"; |
| 150 | + let g = unsafe { |
| 151 | + s.with_c_str(|buf| { |
| 152 | + llvm::LLVMAddGlobal(ccx.llmod, llty.to_ref(), buf) |
| 153 | + }) |
| 154 | + }; |
| 155 | + lib::llvm::SetLinkage(g, lib::llvm::ExternalWeakLinkage); |
| 156 | + g |
| 157 | + } else { |
| 158 | + ccx.crate_map |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + let ident = link_name(foreign_item); |
| 163 | + match attr::first_attr_value_str_by_name(foreign_item.attrs.as_slice(), |
| 164 | + "linkage") { |
| 165 | + // If this is a static with a linkage specified, then we need to handle |
| 166 | + // it a little specially. The typesystem prevents things like &T and |
| 167 | + // extern "C" fn() from being non-null, so we can't just declare a |
| 168 | + // static and call it a day. Some linkages (like weak) will make it such |
| 169 | + // that the static actually has a null value. |
| 170 | + Some(name) => { |
| 171 | + let linkage = match llvm_linkage_by_name(name.get()) { |
| 172 | + Some(linkage) => linkage, |
| 173 | + None => { |
| 174 | + ccx.sess.span_fatal(foreign_item.span, |
| 175 | + "invalid linkage specified"); |
| 176 | + } |
| 177 | + }; |
| 178 | + let llty2 = match ty::get(ty).sty { |
| 179 | + ty::ty_ptr(ref mt) => type_of::type_of(ccx, mt.ty), |
| 180 | + _ => { |
| 181 | + ccx.sess.span_fatal(foreign_item.span, |
| 182 | + "must have type `*T` or `*mut T`"); |
| 183 | + } |
| 184 | + }; |
| 185 | + unsafe { |
| 186 | + let g1 = ident.get().with_c_str(|buf| { |
| 187 | + llvm::LLVMAddGlobal(ccx.llmod, llty2.to_ref(), buf) |
| 188 | + }); |
| 189 | + lib::llvm::SetLinkage(g1, linkage); |
| 190 | + |
| 191 | + let real_name = "_rust_extern_with_linkage_" + ident.get(); |
| 192 | + let g2 = real_name.with_c_str(|buf| { |
| 193 | + llvm::LLVMAddGlobal(ccx.llmod, llty.to_ref(), buf) |
| 194 | + }); |
| 195 | + lib::llvm::SetLinkage(g2, lib::llvm::InternalLinkage); |
| 196 | + llvm::LLVMSetInitializer(g2, g1); |
| 197 | + g2 |
| 198 | + } |
| 199 | + } |
| 200 | + None => unsafe { |
| 201 | + ident.get().with_c_str(|buf| { |
| 202 | + llvm::LLVMAddGlobal(ccx.llmod, llty.to_ref(), buf) |
| 203 | + }) |
| 204 | + } |
| 205 | + } |
| 206 | +} |
108 | 207 |
|
109 | 208 | pub fn register_foreign_item_fn(ccx: @CrateContext, abis: AbiSet,
|
110 | 209 | foreign_item: @ast::ForeignItem) -> ValueRef {
|
|
0 commit comments