Skip to content

Commit d267f0f

Browse files
committed
LLVM: respect linksection for exported variables
1 parent e8d02c5 commit d267f0f

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

src/Sema.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6351,6 +6351,7 @@ fn instantiateGenericCall(
63516351
new_decl.is_exported = fn_owner_decl.is_exported;
63526352
new_decl.has_align = fn_owner_decl.has_align;
63536353
new_decl.has_linksection_or_addrspace = fn_owner_decl.has_linksection_or_addrspace;
6354+
new_decl.@"linksection" = fn_owner_decl.@"linksection";
63546355
new_decl.@"addrspace" = fn_owner_decl.@"addrspace";
63556356
new_decl.zir_decl_index = fn_owner_decl.zir_decl_index;
63566357
new_decl.alive = true; // This Decl is called at runtime.

src/codegen/llvm.zig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,10 @@ pub const Object = struct {
722722
dg.addFnAttrString(llvm_func, "no-stack-arg-probe", "");
723723
}
724724

725+
if (decl.@"linksection") |section| {
726+
llvm_func.setSection(section);
727+
}
728+
725729
// Remove all the basic blocks of a function in order to start over, generating
726730
// LLVM IR from an empty function body.
727731
while (llvm_func.getFirstBasicBlock()) |bb| {
@@ -1107,6 +1111,11 @@ pub const Object = struct {
11071111
.hidden => llvm_global.setVisibility(.Hidden),
11081112
.protected => llvm_global.setVisibility(.Protected),
11091113
}
1114+
if (exports[0].options.section) |section| {
1115+
const section_z = try module.gpa.dupeZ(u8, section);
1116+
defer module.gpa.free(section_z);
1117+
llvm_global.setSection(section_z);
1118+
}
11101119
if (decl.val.castTag(.variable)) |variable| {
11111120
if (variable.data.is_threadlocal) {
11121121
llvm_global.setThreadLocalMode(.GeneralDynamicTLSModel);
@@ -2183,6 +2192,7 @@ pub const DeclGen = struct {
21832192
const target = dg.module.getTarget();
21842193
var global = try dg.resolveGlobalDecl(decl_index);
21852194
global.setAlignment(decl.getAlignment(target));
2195+
if (decl.@"linksection") |section| global.setSection(section);
21862196
assert(decl.has_tv);
21872197
const init_val = if (decl.val.castTag(.variable)) |payload| init_val: {
21882198
const variable = payload.data;
@@ -2216,6 +2226,7 @@ pub const DeclGen = struct {
22162226
new_global.setLinkage(global.getLinkage());
22172227
new_global.setUnnamedAddr(global.getUnnamedAddress());
22182228
new_global.setAlignment(global.getAlignment());
2229+
if (decl.@"linksection") |section| new_global.setSection(section);
22192230
new_global.setInitializer(llvm_init);
22202231
// replaceAllUsesWith requires the type to be unchanged. So we bitcast
22212232
// the new global to the old type and use that as the thing to replace

src/codegen/llvm/bindings.zig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ pub const Value = opaque {
126126
pub const setThreadLocalMode = LLVMSetThreadLocalMode;
127127
extern fn LLVMSetThreadLocalMode(Global: *const Value, Mode: ThreadLocalMode) void;
128128

129+
pub const setSection = LLVMSetSection;
130+
extern fn LLVMSetSection(Global: *const Value, Section: [*:0]const u8) void;
131+
129132
pub const deleteGlobal = LLVMDeleteGlobal;
130133
extern fn LLVMDeleteGlobal(GlobalVar: *const Value) void;
131134

0 commit comments

Comments
 (0)