Skip to content

Commit

Permalink
Add support for setting the link section of global variables
Browse files Browse the repository at this point in the history
  • Loading branch information
antoyo committed May 16, 2021
1 parent 81a3a65 commit 999f768
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 14 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
let string = self.context.new_string_literal(&*string);
let sym = self.generate_local_symbol_name("str");
// NOTE: TLS is always off for a string litteral.
let global = self.define_global(&sym, self.val_ty(string), false)
// NOTE: string litterals do not have a link section.
let global = self.define_global(&sym, self.val_ty(string), false, None)
.unwrap_or_else(|| bug!("symbol `{}` is already defined", sym));
self.global_init_block.add_assignment(None, global.dereference(None), string);
global.to_rvalue()
Expand Down
12 changes: 7 additions & 5 deletions src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl<'gcc, 'tcx> StaticMethods for CodegenCx<'gcc, 'tcx> {
let linkage = llvm::LLVMRustGetLinkage(global);
let visibility = llvm::LLVMRustGetVisibility(global);*/

let new_global = self.get_or_insert_global(&name, val_llty, is_tls);
let new_global = self.get_or_insert_global(&name, val_llty, is_tls, attrs.link_section);

/*llvm::LLVMRustSetLinkage(new_global, linkage);
llvm::LLVMRustSetVisibility(new_global, visibility);*/
Expand Down Expand Up @@ -258,7 +258,8 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
Some(kind) if !self.tcx.sess.fewer_names() => {
let name = self.generate_local_symbol_name(kind);
// TODO: check if it's okay that TLS is off here.
let gv = self.define_global(&name[..], self.val_ty(cv), false).unwrap_or_else(|| {
// TODO: check if it's okay that link_section is None here.
let gv = self.define_global(&name[..], self.val_ty(cv), false, None).unwrap_or_else(|| {
bug!("symbol `{}` is already defined", name);
});
//llvm::LLVMRustSetLinkage(gv, llvm::Linkage::PrivateLinkage);
Expand All @@ -284,6 +285,7 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {

pub fn get_static(&self, def_id: DefId) -> RValue<'gcc> {
let instance = Instance::mono(self.tcx, def_id);
let fn_attrs = self.tcx.codegen_fn_attrs(def_id);
if let Some(&global) = self.instances.borrow().get(&instance) {
/*let attrs = self.tcx.codegen_fn_attrs(def_id);
let name = &*self.tcx.symbol_name(instance).name;
Expand Down Expand Up @@ -329,7 +331,7 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
}

let is_tls = attrs.iter().any(|attr| self.tcx.sess.check_name(attr, sym::thread_local));
let global = self.declare_global(&sym, llty, is_tls);
let global = self.declare_global(&sym, llty, is_tls, fn_attrs.link_section);

if !self.tcx.is_reachable_non_generic(def_id) {
/*unsafe {
Expand Down Expand Up @@ -501,7 +503,7 @@ fn check_and_apply_linkage<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, attrs: &Codeg
let mut real_name = "_rust_extern_with_linkage_".to_string();
real_name.push_str(&sym);
let global2 =
cx.define_global(&real_name, llty, is_tls).unwrap_or_else(|| {
cx.define_global(&real_name, llty, is_tls, attrs.link_section).unwrap_or_else(|| {
cx.sess().span_fatal(span, &format!("symbol `{}` is already defined", &sym))
});
//llvm::LLVMRustSetLinkage(global2, llvm::Linkage::InternalLinkage);
Expand All @@ -520,6 +522,6 @@ fn check_and_apply_linkage<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, attrs: &Codeg
// don't do this then linker errors can be generated where the linker
// complains that one object files has a thread local version of the
// symbol and another one doesn't.
cx.declare_global(&sym, llty, is_tls)
cx.declare_global(&sym, llty, is_tls, attrs.link_section)
}
}
14 changes: 9 additions & 5 deletions src/declare.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
use gccjit::{Function, FunctionType, GlobalKind, LValue, RValue, ToRValue, Type};
use rustc_codegen_ssa::traits::BaseTypeMethods;
use rustc_middle::ty::Ty;
use rustc_span::Symbol;
use rustc_target::abi::call::FnAbi;

use crate::abi::FnAbiGccExt;
use crate::context::{CodegenCx, unit_name};

impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
pub fn get_or_insert_global(&self, name: &str, ty: Type<'gcc>, is_tls: bool) -> RValue<'gcc> {
pub fn get_or_insert_global(&self, name: &str, ty: Type<'gcc>, is_tls: bool, link_section: Option<Symbol>) -> RValue<'gcc> {
if self.globals.borrow().contains_key(name) {
let typ = self.globals.borrow().get(name).expect("global").get_type();
let global = self.context.new_global(None, GlobalKind::Imported, typ, name);
if is_tls {
global.set_tls_model(self.tls_model);
}
if let Some(link_section) = link_section {
global.set_link_section(&link_section.as_str());
}
global.get_address(None)
}
else {
self.declare_global(name, ty, is_tls)
self.declare_global(name, ty, is_tls, link_section)
}
}

Expand Down Expand Up @@ -46,7 +50,7 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
unsafe { std::mem::transmute(func) }
}

pub fn declare_global(&self, name: &str, ty: Type<'gcc>, is_tls: bool) -> RValue<'gcc> {
pub fn declare_global(&self, name: &str, ty: Type<'gcc>, is_tls: bool, link_section: Option<Symbol>) -> RValue<'gcc> {
//debug!("declare_global(name={:?})", name);
let global = self.context.new_global(None, GlobalKind::Exported, ty, name);
if is_tls {
Expand Down Expand Up @@ -83,8 +87,8 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
unsafe { std::mem::transmute(func) }
}

pub fn define_global(&self, name: &str, ty: Type<'gcc>, is_tls: bool) -> Option<RValue<'gcc>> {
Some(self.get_or_insert_global(name, ty, is_tls))
pub fn define_global(&self, name: &str, ty: Type<'gcc>, is_tls: bool, link_section: Option<Symbol>) -> Option<RValue<'gcc>> {
Some(self.get_or_insert_global(name, ty, is_tls, link_section))
}

pub fn define_private_global(&self, ty: Type<'gcc>) -> RValue<'gcc> {
Expand Down
2 changes: 1 addition & 1 deletion src/mono_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl<'gcc, 'tcx> PreDefineMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
let gcc_type = self.layout_of(ty).gcc_type(self, true);

let is_tls = attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL);
let global = self.define_global(symbol_name, gcc_type, is_tls).unwrap_or_else(|| {
let global = self.define_global(symbol_name, gcc_type, is_tls, attrs.link_section).unwrap_or_else(|| {
self.sess().span_fatal(
self.tcx.def_span(def_id),
&format!("symbol `{}` is already defined", symbol_name),
Expand Down

0 comments on commit 999f768

Please sign in to comment.