| ... | ... | @@ -224,7 +224,7 @@ pub const HashStyle = enum { sysv, gnu, both }; |
| 224 | 224 | pub const CompressDebugSections = enum { none, zlib, zstd }; |
| 225 | 225 | pub const SortSection = enum { name, alignment }; |
| 226 | 226 | |
| 227 | | pub fn open( |
| 227 | pub fn createEmpty( |
| 228 | 228 | arena: Allocator, |
| 229 | 229 | comp: *Compilation, |
| 230 | 230 | emit: Compilation.Emit, |
| ... | ... | @@ -238,8 +238,79 @@ pub fn open( |
| 238 | 238 | const opt_zcu = comp.module; |
| 239 | 239 | const output_mode = comp.config.output_mode; |
| 240 | 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 | }, |
| 241 | 288 | |
| 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 | 314 | errdefer self.base.destroy(); |
| 244 | 315 | |
| 245 | 316 | if (use_lld and use_llvm) { |
| ... | ... | @@ -343,91 +414,13 @@ pub fn open( |
| 343 | 414 | return self; |
| 344 | 415 | } |
| 345 | 416 | |
| 346 | | pub fn createEmpty( |
| 417 | pub fn open( |
| 347 | 418 | arena: Allocator, |
| 348 | 419 | comp: *Compilation, |
| 349 | 420 | emit: Compilation.Emit, |
| 350 | 421 | options: link.File.OpenOptions, |
| 351 | 422 | ) !*Elf { |
| 352 | | const use_llvm = comp.config.use_llvm; |
| 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; |
| 423 | return createEmpty(arena, comp, emit, options); |
| 431 | 424 | } |
| 432 | 425 | |
| 433 | 426 | pub fn deinit(self: *Elf) void { |