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(...@@ -6351,6 +6351,7 @@ fn instantiateGenericCall(
6351 new_decl.is_exported = fn_owner_decl.is_exported;6351 new_decl.is_exported = fn_owner_decl.is_exported;
6352 new_decl.has_align = fn_owner_decl.has_align;6352 new_decl.has_align = fn_owner_decl.has_align;
6353 new_decl.has_linksection_or_addrspace = fn_owner_decl.has_linksection_or_addrspace;6353 new_decl.has_linksection_or_addrspace = fn_owner_decl.has_linksection_or_addrspace;
6354 new_decl.@"linksection" = fn_owner_decl.@"linksection";
6354 new_decl.@"addrspace" = fn_owner_decl.@"addrspace";6355 new_decl.@"addrspace" = fn_owner_decl.@"addrspace";
6355 new_decl.zir_decl_index = fn_owner_decl.zir_decl_index;6356 new_decl.zir_decl_index = fn_owner_decl.zir_decl_index;
6356 new_decl.alive = true; // This Decl is called at runtime.6357 new_decl.alive = true; // This Decl is called at runtime.
src/codegen/llvm.zig+11
...@@ -722,6 +722,10 @@ pub const Object = struct {...@@ -722,6 +722,10 @@ pub const Object = struct {
722 dg.addFnAttrString(llvm_func, "no-stack-arg-probe", "");722 dg.addFnAttrString(llvm_func, "no-stack-arg-probe", "");
723 }723 }
724724
725 if (decl.@"linksection") |section| {
726 llvm_func.setSection(section);
727 }
728
725 // Remove all the basic blocks of a function in order to start over, generating729 // Remove all the basic blocks of a function in order to start over, generating
726 // LLVM IR from an empty function body.730 // LLVM IR from an empty function body.
727 while (llvm_func.getFirstBasicBlock()) |bb| {731 while (llvm_func.getFirstBasicBlock()) |bb| {
...@@ -1107,6 +1111,11 @@ pub const Object = struct {...@@ -1107,6 +1111,11 @@ pub const Object = struct {
1107 .hidden => llvm_global.setVisibility(.Hidden),1111 .hidden => llvm_global.setVisibility(.Hidden),
1108 .protected => llvm_global.setVisibility(.Protected),1112 .protected => llvm_global.setVisibility(.Protected),
1109 }1113 }
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 }
1110 if (decl.val.castTag(.variable)) |variable| {1119 if (decl.val.castTag(.variable)) |variable| {
1111 if (variable.data.is_threadlocal) {1120 if (variable.data.is_threadlocal) {
1112 llvm_global.setThreadLocalMode(.GeneralDynamicTLSModel);1121 llvm_global.setThreadLocalMode(.GeneralDynamicTLSModel);
...@@ -2183,6 +2192,7 @@ pub const DeclGen = struct {...@@ -2183,6 +2192,7 @@ pub const DeclGen = struct {
2183 const target = dg.module.getTarget();2192 const target = dg.module.getTarget();
2184 var global = try dg.resolveGlobalDecl(decl_index);2193 var global = try dg.resolveGlobalDecl(decl_index);
2185 global.setAlignment(decl.getAlignment(target));2194 global.setAlignment(decl.getAlignment(target));
2195 if (decl.@"linksection") |section| global.setSection(section);
2186 assert(decl.has_tv);2196 assert(decl.has_tv);
2187 const init_val = if (decl.val.castTag(.variable)) |payload| init_val: {2197 const init_val = if (decl.val.castTag(.variable)) |payload| init_val: {
2188 const variable = payload.data;2198 const variable = payload.data;
...@@ -2216,6 +2226,7 @@ pub const DeclGen = struct {...@@ -2216,6 +2226,7 @@ pub const DeclGen = struct {
2216 new_global.setLinkage(global.getLinkage());2226 new_global.setLinkage(global.getLinkage());
2217 new_global.setUnnamedAddr(global.getUnnamedAddress());2227 new_global.setUnnamedAddr(global.getUnnamedAddress());
2218 new_global.setAlignment(global.getAlignment());2228 new_global.setAlignment(global.getAlignment());
2229 if (decl.@"linksection") |section| new_global.setSection(section);
2219 new_global.setInitializer(llvm_init);2230 new_global.setInitializer(llvm_init);
2220 // replaceAllUsesWith requires the type to be unchanged. So we bitcast2231 // replaceAllUsesWith requires the type to be unchanged. So we bitcast
2221 // the new global to the old type and use that as the thing to replace2232 // 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 {...@@ -126,6 +126,9 @@ pub const Value = opaque {
126 pub const setThreadLocalMode = LLVMSetThreadLocalMode;126 pub const setThreadLocalMode = LLVMSetThreadLocalMode;
127 extern fn LLVMSetThreadLocalMode(Global: *const Value, Mode: ThreadLocalMode) void;127 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
129 pub const deleteGlobal = LLVMDeleteGlobal;132 pub const deleteGlobal = LLVMDeleteGlobal;
130 extern fn LLVMDeleteGlobal(GlobalVar: *const Value) void;133 extern fn LLVMDeleteGlobal(GlobalVar: *const Value) void;
131134