Skip to content

Commit 4cd9256

Browse files
kubkonjacobly0
authored andcommitted
link/elf: propagate Haiku requirement of always passing -shared for images
1 parent b98e3be commit 4cd9256

File tree

5 files changed

+33
-23
lines changed

5 files changed

+33
-23
lines changed

src/link/Elf.zig

+17-7
Original file line numberDiff line numberDiff line change
@@ -1461,7 +1461,7 @@ fn dumpArgv(self: *Elf, comp: *Compilation) !void {
14611461
}
14621462
}
14631463

1464-
if (self.base.isDynLib()) {
1464+
if (self.isEffectivelyDynLib()) {
14651465
if (self.soname) |name| {
14661466
try argv.append("-soname");
14671467
try argv.append(name);
@@ -1517,7 +1517,7 @@ fn dumpArgv(self: *Elf, comp: *Compilation) !void {
15171517

15181518
if (self.base.isStatic()) {
15191519
try argv.append("-static");
1520-
} else if (self.base.isDynLib() or (target.os.tag == .haiku and self.base.isExe())) {
1520+
} else if (self.isEffectivelyDynLib()) {
15211521
try argv.append("-shared");
15221522
}
15231523

@@ -1997,15 +1997,15 @@ fn markImportsExports(self: *Elf) void {
19971997
}
19981998
if (file_ptr.index() == file_index) {
19991999
global.flags.@"export" = true;
2000-
if (elf_file.base.isDynLib() and vis != .PROTECTED) {
2000+
if (elf_file.isEffectivelyDynLib() and vis != .PROTECTED) {
20012001
global.flags.import = true;
20022002
}
20032003
}
20042004
}
20052005
}
20062006
}.mark;
20072007

2008-
if (!self.base.isDynLib()) {
2008+
if (!self.isEffectivelyDynLib()) {
20092009
for (self.shared_objects.items) |index| {
20102010
for (self.file(index).?.globals()) |global_index| {
20112011
const global = self.symbol(global_index);
@@ -3117,7 +3117,7 @@ fn addLinkerDefinedSymbols(self: *Elf) !void {
31173117
}
31183118
}
31193119

3120-
if (self.getTarget().cpu.arch == .riscv64 and self.base.isDynLib()) {
3120+
if (self.getTarget().cpu.arch == .riscv64 and self.isEffectivelyDynLib()) {
31213121
self.global_pointer_index = try linker_defined.addGlobal("__global_pointer$", self);
31223122
}
31233123

@@ -3423,7 +3423,7 @@ fn initSyntheticSections(self: *Elf) !void {
34233423
});
34243424
}
34253425

3426-
if (self.base.isDynLib() or self.shared_objects.items.len > 0 or comp.config.pie) {
3426+
if (self.isEffectivelyDynLib() or self.shared_objects.items.len > 0 or comp.config.pie) {
34273427
self.dynstrtab_section_index = try self.addSection(.{
34283428
.name = ".dynstr",
34293429
.flags = elf.SHF_ALLOC,
@@ -3660,7 +3660,7 @@ fn setDynamicSection(self: *Elf, rpaths: []const []const u8) !void {
36603660
try self.dynamic.addNeeded(shared_object, self);
36613661
}
36623662

3663-
if (self.base.isDynLib()) {
3663+
if (self.isEffectivelyDynLib()) {
36643664
if (self.soname) |soname| {
36653665
try self.dynamic.setSoname(soname, self);
36663666
}
@@ -5249,6 +5249,16 @@ const CsuObjects = struct {
52495249
}
52505250
};
52515251

5252+
/// If a target compiles other output modes as dynamic libraries,
5253+
/// this function returns true for those too.
5254+
pub fn isEffectivelyDynLib(self: Elf) bool {
5255+
if (self.base.isDynLib()) return true;
5256+
return switch (self.getTarget().os.tag) {
5257+
.haiku => self.base.isExe(),
5258+
else => false,
5259+
};
5260+
}
5261+
52525262
pub fn isZigSection(self: Elf, shndx: u32) bool {
52535263
inline for (&[_]?u32{
52545264
self.zig_text_section_index,

src/link/Elf/Atom.zig

+2-2
Original file line numberDiff line numberDiff line change
@@ -1054,7 +1054,7 @@ const x86_64 = struct {
10541054
it: *RelocsIterator,
10551055
) !void {
10561056
const is_static = elf_file.base.isStatic();
1057-
const is_dyn_lib = elf_file.base.isDynLib();
1057+
const is_dyn_lib = elf_file.isEffectivelyDynLib();
10581058

10591059
const r_type: elf.R_X86_64 = @enumFromInt(rel.r_type());
10601060
const r_offset = std.math.cast(usize, rel.r_offset) orelse return error.Overflow;
@@ -1599,7 +1599,7 @@ const aarch64 = struct {
15991599
_ = it;
16001600

16011601
const r_type: elf.R_AARCH64 = @enumFromInt(rel.r_type());
1602-
const is_dyn_lib = elf_file.base.isDynLib();
1602+
const is_dyn_lib = elf_file.isEffectivelyDynLib();
16031603

16041604
switch (r_type) {
16051605
.ABS64 => {

src/link/Elf/Object.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ pub fn claimUnresolved(self: *Object, elf_file: *Elf) void {
568568
}
569569

570570
const is_import = blk: {
571-
if (!elf_file.base.isDynLib()) break :blk false;
571+
if (!elf_file.isEffectivelyDynLib()) break :blk false;
572572
const vis = @as(elf.STV, @enumFromInt(esym.st_other));
573573
if (vis == .HIDDEN) break :blk false;
574574
break :blk true;

src/link/Elf/ZigObject.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ pub fn claimUnresolved(self: ZigObject, elf_file: *Elf) void {
367367
}
368368

369369
const is_import = blk: {
370-
if (!elf_file.base.isDynLib()) break :blk false;
370+
if (!elf_file.isEffectivelyDynLib()) break :blk false;
371371
const vis = @as(elf.STV, @enumFromInt(esym.st_other));
372372
if (vis == .HIDDEN) break :blk false;
373373
break :blk true;

src/link/Elf/synthetic_sections.zig

+12-12
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ pub const DynamicSection = struct {
8989
if (elf_file.verneed_section_index != null) nentries += 2; // VERNEED
9090
if (dt.getFlags(elf_file) != null) nentries += 1; // FLAGS
9191
if (dt.getFlags1(elf_file) != null) nentries += 1; // FLAGS_1
92-
if (!elf_file.base.isDynLib()) nentries += 1; // DEBUG
92+
if (!elf_file.isEffectivelyDynLib()) nentries += 1; // DEBUG
9393
nentries += 1; // NULL
9494
return nentries * @sizeOf(elf.Elf64_Dyn);
9595
}
@@ -216,7 +216,7 @@ pub const DynamicSection = struct {
216216
}
217217

218218
// DEBUG
219-
if (!elf_file.base.isDynLib()) try writer.writeStruct(elf.Elf64_Dyn{ .d_tag = elf.DT_DEBUG, .d_val = 0 });
219+
if (!elf_file.isEffectivelyDynLib()) try writer.writeStruct(elf.Elf64_Dyn{ .d_tag = elf.DT_DEBUG, .d_val = 0 });
220220

221221
// NULL
222222
try writer.writeStruct(elf.Elf64_Dyn{ .d_tag = elf.DT_NULL, .d_val = 0 });
@@ -256,7 +256,7 @@ pub const ZigGotSection = struct {
256256
entry.* = sym_index;
257257
const symbol = elf_file.symbol(sym_index);
258258
symbol.flags.has_zig_got = true;
259-
if (elf_file.base.isDynLib() or (elf_file.base.isExe() and comp.config.pie)) {
259+
if (elf_file.isEffectivelyDynLib() or (elf_file.base.isExe() and comp.config.pie)) {
260260
zig_got.flags.needs_rela = true;
261261
}
262262
if (symbol.extra(elf_file)) |extra| {
@@ -495,7 +495,7 @@ pub const GotSection = struct {
495495
const symbol = elf_file.symbol(sym_index);
496496
symbol.flags.has_got = true;
497497
if (symbol.flags.import or symbol.isIFunc(elf_file) or
498-
((elf_file.base.isDynLib() or (elf_file.base.isExe() and comp.config.pie)) and !symbol.isAbs(elf_file)))
498+
((elf_file.isEffectivelyDynLib() or (elf_file.base.isExe() and comp.config.pie)) and !symbol.isAbs(elf_file)))
499499
{
500500
got.flags.needs_rela = true;
501501
}
@@ -528,7 +528,7 @@ pub const GotSection = struct {
528528
entry.symbol_index = sym_index;
529529
const symbol = elf_file.symbol(sym_index);
530530
symbol.flags.has_tlsgd = true;
531-
if (symbol.flags.import or elf_file.base.isDynLib()) got.flags.needs_rela = true;
531+
if (symbol.flags.import or elf_file.isEffectivelyDynLib()) got.flags.needs_rela = true;
532532
if (symbol.extra(elf_file)) |extra| {
533533
var new_extra = extra;
534534
new_extra.tlsgd = index;
@@ -545,7 +545,7 @@ pub const GotSection = struct {
545545
entry.symbol_index = sym_index;
546546
const symbol = elf_file.symbol(sym_index);
547547
symbol.flags.has_gottp = true;
548-
if (symbol.flags.import or elf_file.base.isDynLib()) got.flags.needs_rela = true;
548+
if (symbol.flags.import or elf_file.isEffectivelyDynLib()) got.flags.needs_rela = true;
549549
if (symbol.extra(elf_file)) |extra| {
550550
var new_extra = extra;
551551
new_extra.gottp = index;
@@ -580,7 +580,7 @@ pub const GotSection = struct {
580580

581581
pub fn write(got: GotSection, elf_file: *Elf, writer: anytype) !void {
582582
const comp = elf_file.base.comp;
583-
const is_dyn_lib = elf_file.base.isDynLib();
583+
const is_dyn_lib = elf_file.isEffectivelyDynLib();
584584
const apply_relocs = true; // TODO add user option for this
585585

586586
for (got.entries.items) |entry| {
@@ -595,7 +595,7 @@ pub const GotSection = struct {
595595
if (symbol.?.flags.import) break :blk 0;
596596
if (symbol.?.isIFunc(elf_file))
597597
break :blk if (apply_relocs) value else 0;
598-
if ((elf_file.base.isDynLib() or (elf_file.base.isExe() and comp.config.pie)) and
598+
if ((elf_file.isEffectivelyDynLib() or (elf_file.base.isExe() and comp.config.pie)) and
599599
!symbol.?.isAbs(elf_file))
600600
{
601601
break :blk if (apply_relocs) value else 0;
@@ -653,7 +653,7 @@ pub const GotSection = struct {
653653
pub fn addRela(got: GotSection, elf_file: *Elf) !void {
654654
const comp = elf_file.base.comp;
655655
const gpa = comp.gpa;
656-
const is_dyn_lib = elf_file.base.isDynLib();
656+
const is_dyn_lib = elf_file.isEffectivelyDynLib();
657657
const cpu_arch = elf_file.getTarget().cpu.arch;
658658
try elf_file.rela_dyn.ensureUnusedCapacity(gpa, got.numRela(elf_file));
659659

@@ -683,7 +683,7 @@ pub const GotSection = struct {
683683
});
684684
continue;
685685
}
686-
if ((elf_file.base.isDynLib() or (elf_file.base.isExe() and comp.config.pie)) and
686+
if ((elf_file.isEffectivelyDynLib() or (elf_file.base.isExe() and comp.config.pie)) and
687687
!symbol.?.isAbs(elf_file))
688688
{
689689
elf_file.addRelaDynAssumeCapacity(.{
@@ -758,7 +758,7 @@ pub const GotSection = struct {
758758

759759
pub fn numRela(got: GotSection, elf_file: *Elf) usize {
760760
const comp = elf_file.base.comp;
761-
const is_dyn_lib = elf_file.base.isDynLib();
761+
const is_dyn_lib = elf_file.isEffectivelyDynLib();
762762
var num: usize = 0;
763763
for (got.entries.items) |entry| {
764764
const symbol = switch (entry.tag) {
@@ -767,7 +767,7 @@ pub const GotSection = struct {
767767
};
768768
switch (entry.tag) {
769769
.got => if (symbol.?.flags.import or symbol.?.isIFunc(elf_file) or
770-
((elf_file.base.isDynLib() or (elf_file.base.isExe() and comp.config.pie)) and
770+
((elf_file.isEffectivelyDynLib() or (elf_file.base.isExe() and comp.config.pie)) and
771771
!symbol.?.isAbs(elf_file)))
772772
{
773773
num += 1;

0 commit comments

Comments
 (0)