authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-08-10 14:08:09-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-08-10 16:43:30-07:00
logd267f0f968c97d77d8434ef9918c71fb4c11f45d
tree7ccb96996094065e66b21a59f5c1eff7a8268806
parente8d02c5b90c0833401a70cc797a28da57a356927

LLVM: respect linksection for exported variables


3 files changed, 15 insertions(+), 0 deletions(-)

src/Sema.zig+1
......@@ -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+11
......@@ -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+3
......@@ -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