authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-12-16 13:48:06-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-01-01 17:51:20-07:00
loge3409c0bce4a0aaa2ec3f536eabf31061919dde4
tree5ae9cb10c174878b11d4dccb1e83c145209854d4
parent0be97b5ae3d32ca9cd7fbb68d5972538b48d8c76

Elf: fix createEmpty not creating the file


1 files changed, 75 insertions(+), 82 deletions(-)

src/link/Elf.zig+75-82
...@@ -224,7 +224,7 @@ pub const HashStyle = enum { sysv, gnu, both };...@@ -224,7 +224,7 @@ pub const HashStyle = enum { sysv, gnu, both };
224pub const CompressDebugSections = enum { none, zlib, zstd };224pub const CompressDebugSections = enum { none, zlib, zstd };
225pub const SortSection = enum { name, alignment };225pub const SortSection = enum { name, alignment };
226226
227pub fn open(227pub fn createEmpty(
228 arena: Allocator,228 arena: Allocator,
229 comp: *Compilation,229 comp: *Compilation,
230 emit: Compilation.Emit,230 emit: Compilation.Emit,
...@@ -238,8 +238,79 @@ pub fn open(...@@ -238,8 +238,79 @@ pub fn open(
238 const opt_zcu = comp.module;238 const opt_zcu = comp.module;
239 const output_mode = comp.config.output_mode;239 const output_mode = comp.config.output_mode;
240 const link_mode = comp.config.link_mode;240 const link_mode = comp.config.link_mode;
241 const optimize_mode = comp.root_mod.optimize_mode;
242 const is_native_os = comp.root_mod.resolved_target.is_native_os;
243 const ptr_width: PtrWidth = switch (target.ptrBitWidth()) {
244 0...32 => .p32,
245 33...64 => .p64,
246 else => return error.UnsupportedELFArchitecture,
247 };
248
249 const page_size: u32 = switch (target.cpu.arch) {
250 .powerpc64le => 0x10000,
251 .sparc64 => 0x2000,
252 else => 0x1000,
253 };
254 const is_dyn_lib = output_mode == .Lib and link_mode == .Dynamic;
255 const default_sym_version: elf.Elf64_Versym = if (is_dyn_lib or comp.config.rdynamic)
256 elf.VER_NDX_GLOBAL
257 else
258 elf.VER_NDX_LOCAL;
259
260 const self = try arena.create(Elf);
261 self.* = .{
262 .base = .{
263 .tag = .elf,
264 .comp = comp,
265 .emit = emit,
266 .gc_sections = options.gc_sections orelse (optimize_mode != .Debug and output_mode != .Obj),
267 .print_gc_sections = options.print_gc_sections,
268 .stack_size = options.stack_size orelse 16777216,
269 .allow_shlib_undefined = options.allow_shlib_undefined orelse !is_native_os,
270 .file = null,
271 .disable_lld_caching = options.disable_lld_caching,
272 .build_id = options.build_id,
273 .rpath_list = options.rpath_list,
274 .force_undefined_symbols = options.force_undefined_symbols,
275 },
276 .ptr_width = ptr_width,
277 .page_size = page_size,
278 .default_sym_version = default_sym_version,
279
280 .image_base = b: {
281 if (is_dyn_lib) break :b 0;
282 if (output_mode == .Exe and comp.config.pie) break :b 0;
283 break :b options.image_base orelse switch (ptr_width) {
284 .p32 => 0x1000,
285 .p64 => 0x1000000,
286 };
287 },
241288
242 const self = try createEmpty(arena, comp, emit, options);289 .eh_frame_hdr = options.eh_frame_hdr,
290 .emit_relocs = options.emit_relocs,
291 .z_nodelete = options.z_nodelete,
292 .z_notext = options.z_notext,
293 .z_defs = options.z_defs,
294 .z_origin = options.z_origin,
295 .z_nocopyreloc = options.z_nocopyreloc,
296 .z_now = options.z_now,
297 .z_relro = options.z_relro,
298 .z_common_page_size = options.z_common_page_size,
299 .z_max_page_size = options.z_max_page_size,
300 .lib_dirs = options.lib_dirs,
301 .hash_style = options.hash_style,
302 .compress_debug_sections = options.compress_debug_sections,
303 .symbol_wrap_set = options.symbol_wrap_set,
304 .each_lib_rpath = options.each_lib_rpath,
305 .sort_section = options.sort_section,
306 .soname = options.soname,
307 .bind_global_refs_locally = options.bind_global_refs_locally,
308 .linker_script = options.linker_script,
309 .version_script = options.version_script,
310 };
311 if (use_llvm and comp.config.have_zcu) {
312 self.llvm_object = try LlvmObject.create(arena, comp);
313 }
243 errdefer self.base.destroy();314 errdefer self.base.destroy();
244315
245 if (use_lld and use_llvm) {316 if (use_lld and use_llvm) {
...@@ -343,91 +414,13 @@ pub fn open(...@@ -343,91 +414,13 @@ pub fn open(
343 return self;414 return self;
344}415}
345416
346pub fn createEmpty(417pub fn open(
347 arena: Allocator,418 arena: Allocator,
348 comp: *Compilation,419 comp: *Compilation,
349 emit: Compilation.Emit,420 emit: Compilation.Emit,
350 options: link.File.OpenOptions,421 options: link.File.OpenOptions,
351) !*Elf {422) !*Elf {
352 const use_llvm = comp.config.use_llvm;423 return createEmpty(arena, comp, emit, options);
353 const optimize_mode = comp.root_mod.optimize_mode;
354 const target = comp.root_mod.resolved_target.result;
355 const output_mode = comp.config.output_mode;
356 const link_mode = comp.config.link_mode;
357 const is_native_os = comp.root_mod.resolved_target.is_native_os;
358 const ptr_width: PtrWidth = switch (target.ptrBitWidth()) {
359 0...32 => .p32,
360 33...64 => .p64,
361 else => return error.UnsupportedELFArchitecture,
362 };
363 const self = try arena.create(Elf);
364
365 const page_size: u32 = switch (target.cpu.arch) {
366 .powerpc64le => 0x10000,
367 .sparc64 => 0x2000,
368 else => 0x1000,
369 };
370 const is_dyn_lib = output_mode == .Lib and link_mode == .Dynamic;
371 const default_sym_version: elf.Elf64_Versym = if (is_dyn_lib or comp.config.rdynamic)
372 elf.VER_NDX_GLOBAL
373 else
374 elf.VER_NDX_LOCAL;
375
376 self.* = .{
377 .base = .{
378 .tag = .elf,
379 .comp = comp,
380 .emit = emit,
381 .gc_sections = options.gc_sections orelse (optimize_mode != .Debug and output_mode != .Obj),
382 .print_gc_sections = options.print_gc_sections,
383 .stack_size = options.stack_size orelse 16777216,
384 .allow_shlib_undefined = options.allow_shlib_undefined orelse !is_native_os,
385 .file = null,
386 .disable_lld_caching = options.disable_lld_caching,
387 .build_id = options.build_id,
388 .rpath_list = options.rpath_list,
389 .force_undefined_symbols = options.force_undefined_symbols,
390 },
391 .ptr_width = ptr_width,
392 .page_size = page_size,
393 .default_sym_version = default_sym_version,
394
395 .image_base = b: {
396 if (is_dyn_lib) break :b 0;
397 if (output_mode == .Exe and comp.config.pie) break :b 0;
398 break :b options.image_base orelse switch (ptr_width) {
399 .p32 => 0x1000,
400 .p64 => 0x1000000,
401 };
402 },
403
404 .eh_frame_hdr = options.eh_frame_hdr,
405 .emit_relocs = options.emit_relocs,
406 .z_nodelete = options.z_nodelete,
407 .z_notext = options.z_notext,
408 .z_defs = options.z_defs,
409 .z_origin = options.z_origin,
410 .z_nocopyreloc = options.z_nocopyreloc,
411 .z_now = options.z_now,
412 .z_relro = options.z_relro,
413 .z_common_page_size = options.z_common_page_size,
414 .z_max_page_size = options.z_max_page_size,
415 .lib_dirs = options.lib_dirs,
416 .hash_style = options.hash_style,
417 .compress_debug_sections = options.compress_debug_sections,
418 .symbol_wrap_set = options.symbol_wrap_set,
419 .each_lib_rpath = options.each_lib_rpath,
420 .sort_section = options.sort_section,
421 .soname = options.soname,
422 .bind_global_refs_locally = options.bind_global_refs_locally,
423 .linker_script = options.linker_script,
424 .version_script = options.version_script,
425 };
426 if (use_llvm and comp.config.have_zcu) {
427 self.llvm_object = try LlvmObject.create(arena, comp);
428 }
429
430 return self;
431}424}
432425
433pub fn deinit(self: *Elf) void {426pub fn deinit(self: *Elf) void {