diff --git a/src/Module.zig b/src/Module.zig index e66982fa3af97ce9d4ff70522d8bd2073560ed51..5154b853641c4ed2af01fd0aed1b8c1f911204a6 100644 --- a/src/Module.zig +++ b/src/Module.zig @@ -174,7 +174,7 @@ pub fn create(arena: Allocator, options: CreateOptions) !*Module { }; const pic = b: { - if (target_util.requiresPic(target, options.global.link_libc)) { + if (target_util.requiresPic(target)) { if (options.inherited.pic == false) return error.TargetRequiresPic; break :b true; @@ -184,7 +184,10 @@ pub fn create(arena: Allocator, options: CreateOptions) !*Module { return error.PieRequiresPic; break :b true; } - if (options.global.link_mode == .dynamic and target_util.requiresPicForDynamicLink(target)) { + if (options.global.output_mode == .Lib and + options.global.link_mode == .dynamic and + target_util.requiresPicForDynamicLink(target)) + { if (options.inherited.pic == false) return error.DynamicLinkingRequiresPic; break :b true; @@ -192,10 +195,9 @@ pub fn create(arena: Allocator, options: CreateOptions) !*Module { if (options.inherited.pic) |x| break :b x; if (options.parent) |p| break :b p.pic; - // Default to PIC on targets where we default to producing PIEs to make - // the common case of linking objects and static libraries into an - // executable work out of the box. - break :b target_util.defaultPie(target); + // Default to PIC for objects and static libraries to make the common + // cases of linking those into a PIE or a shared library just work. + break :b options.global.output_mode != .Exe; }; const red_zone = b: { diff --git a/src/libs/glibc.zig b/src/libs/glibc.zig index 86c5360cc17cce248afeac8ddcfb5bb4b2f9ceca..024588644a8c69c12fc323c4c812468abff565d1 100644 --- a/src/libs/glibc.zig +++ b/src/libs/glibc.zig @@ -243,7 +243,9 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre }; var files = [_]Compilation.CSourceFile{ start_o, abi_note_o, init_o }; const basename = if (comp.config.output_mode == .Exe and !comp.config.pie) "crt1" else "Scrt1"; - return comp.build_crt_file(basename, .Obj, .@"glibc Scrt1.o", prog_node, &files, .{}); + return comp.build_crt_file(basename, .Obj, .@"glibc Scrt1.o", prog_node, &files, .{ + .pic = true, + }); }, .libc_nonshared_a => { const s = path.sep_str; @@ -356,7 +358,9 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre files_index += 1; } const files = files_buf[0..files_index]; - return comp.build_crt_file("c_nonshared", .Lib, .@"glibc libc_nonshared.a", prog_node, files, .{}); + return comp.build_crt_file("c_nonshared", .Lib, .@"glibc libc_nonshared.a", prog_node, files, .{ + .pic = true, + }); }, } } diff --git a/src/link/Elf2.zig b/src/link/Elf2.zig index a08d5add1320da6a52a8bc63e75765aa97588a85..b94a2099056b1a997b4fda6dc28f892c4c490b90 100644 --- a/src/link/Elf2.zig +++ b/src/link/Elf2.zig @@ -9070,11 +9070,10 @@ fn addRelocAssumeCapacity( .PLT32 => try elf.addSymbolRelocAssumeCapacity(node, offset, target, addend, .simple(.pltabs, .{ .dest = .@"32", .cast = .unsigned, .shift = .@"0" })), .PLT64 => try elf.addSymbolRelocAssumeCapacity(node, offset, target, addend, .simple(.pltabs, .{ .dest = .@"64", .cast = .unsigned, .shift = .@"0" })), - .WDISP30 => try elf.addSymbolRelocAssumeCapacity(node, offset, target, addend, .simple(.rel, .{ .dest = .@"32[29:0]", .cast = .signed, .shift = .@"2_exact" })), - .WPLT30 => try elf.addSymbolRelocAssumeCapacity(node, offset, target, addend, .simple(.pltrel, .{ .dest = .@"32[29:0]", .cast = .signed, .shift = .@"2_exact" })), - .PC22 => try elf.addSymbolRelocAssumeCapacity(node, offset, target, addend, .simple(.rel, .{ .dest = .@"32[21:0]", .cast = .unsigned, .shift = .@"10" })), - .H44 => try elf.addSymbolRelocAssumeCapacity(node, offset, target, addend, .simple(.abs, .{ .dest = .@"32[21:0]", .cast = .unsigned, .shift = .@"22" })), - .M44 => try elf.addSymbolRelocAssumeCapacity(node, offset, target, addend, .simple(.abs, .{ .dest = .@"32[9:0]", .cast = .trunc, .shift = .@"12" })), + .WDISP30, .WPLT30 => try elf.addSymbolRelocAssumeCapacity(node, offset, target, addend, .simple(.pltrel, .{ .dest = .@"32[29:0]", .cast = .signed, .shift = .@"2_exact" })), + .PC22 => try elf.addSymbolRelocAssumeCapacity(node, offset, target, addend, .simple(.rel, .{ .dest = .@"32[21:0]", .cast = .unsigned, .shift = .@"10" })), + .H44 => try elf.addSymbolRelocAssumeCapacity(node, offset, target, addend, .simple(.abs, .{ .dest = .@"32[21:0]", .cast = .unsigned, .shift = .@"22" })), + .M44 => try elf.addSymbolRelocAssumeCapacity(node, offset, target, addend, .simple(.abs, .{ .dest = .@"32[9:0]", .cast = .trunc, .shift = .@"12" })), .TLS_LDO_HIX22 => try elf.addSymbolRelocAssumeCapacity(node, offset, target, addend, .simple(.dtpoff, .{ .dest = .@"32[21:0]", .cast = .trunc, .shift = .@"10" })), .TLS_LE_HIX22 => try elf.addSymbolRelocAssumeCapacity(node, offset, target, addend, .special(.sparc_le_hix22)), @@ -11944,7 +11943,7 @@ fn ensurePltEntry(elf: *Elf, gsi: Symbol.Global.Index) Error!bool { // sethi (. - .plt[0]), %g1 .{ .imm22 = .{ .imm = @truncate(got_plt_offset), .op = 0b0000001100 } }, // ba,a %xcc, .plt[1] - .{ .disp19 = .{ .disp = @truncate((got_plt_offset + 4 - 32) >> 2), .op = 0b0011000001101 } }, + .{ .disp19 = .{ .disp = @truncate((32 -% (got_plt_offset + 4)) >> 2), .op = 0b0011000001101 } }, // nop .{ .raw = 0x0100_0000 }, // nop diff --git a/src/target.zig b/src/target.zig index 244b95d154fa12af2e70b8cc3cd39f0322272def..f3fe06954f6789aec3a18bb1d91f2fb1ef6642b2 100644 --- a/src/target.zig +++ b/src/target.zig @@ -72,10 +72,8 @@ pub fn requiresPie(target: *const std.Target, link_mode: std.lang.LinkMode) bool } /// This function returns whether non-pic code is completely invalid on the given target. -pub fn requiresPic(target: *const std.Target, linking_libc: bool) bool { - return ((target.os.tag == .windows or target.os.tag == .uefi) and (target.cpu.arch == .aarch64 or target.cpu.arch == .x86_64)) or - target.requiresLibC() or - (linking_libc and target.isGnuLibC()); +pub fn requiresPic(target: *const std.Target) bool { + return (target.os.tag == .windows or target.os.tag == .uefi) and (target.cpu.arch == .aarch64 or target.cpu.arch == .x86_64); } pub fn requiresPicForDynamicLink(target: *const std.Target) bool {