authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-05-16 21:20:31+02:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-05-17 04:41:26+02:00
log0d35a7760e419166577b0c032f598dc76d379a39
treeff59120d5301940cff9cb286fb9ad42ec81252ad
parent76d525f74a33e20dfa4c9e84b27ce8ad84529cac
signaturebadge-check Signed by SSH key SHA256:7B/LJ7bpR1eX8aCXSr4mtd5M45VMPKcx9zY8e95b5QM

freebsd: Fix selection of unversioned symbol inclusion.

We want the latest unversioned inclusion that fits the target version. This theoretically matters because it might have a different global vs weak linkage compared to an older inclusion.

1 files changed, 42 insertions(+), 55 deletions(-)

src/libs/freebsd.zig+42-55
......@@ -533,7 +533,6 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
533533 var opt_symbol_name: ?[]const u8 = null;
534534 var versions = try std.DynamicBitSetUnmanaged.initEmpty(arena, metadata.all_versions.len);
535535 var weak_linkages = try std.DynamicBitSetUnmanaged.initEmpty(arena, metadata.all_versions.len);
536 var sym_unversioned = false;
537536
538537 var inc_fbs = std.io.fixedBufferStream(metadata.inclusions);
539538 var inc_reader = inc_fbs.reader();
......@@ -548,11 +547,17 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
548547 opt_symbol_name = sym_name_buf.items;
549548 versions.unsetAll();
550549 weak_linkages.unsetAll();
551 sym_unversioned = false;
552550
553551 break :n sym_name_buf.items;
554552 };
555553
554 // Pick the default symbol version:
555 // - If there are no versions, don't emit it
556 // - Take the greatest one <= than the target one
557 // - If none of them is <= than the
558 // specified one don't pick any default version
559 var chosen_def_ver_index: usize = 255;
560 var chosen_unversioned_ver_index: usize = 255;
556561 {
557562 const targets = try std.leb.readUleb128(u64, inc_reader);
558563 var lib_index = try inc_reader.readByte();
......@@ -574,7 +579,12 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
574579 const ver_i = @as(u7, @truncate(byte));
575580 if (ok_lib_and_target and ver_i <= target_ver_index) {
576581 versions.set(ver_i);
577 if (is_unversioned) sym_unversioned = true;
582 if (chosen_def_ver_index == 255 or ver_i > chosen_def_ver_index) {
583 chosen_def_ver_index = ver_i;
584 }
585 if (is_unversioned and (chosen_unversioned_ver_index == 255 or ver_i > chosen_unversioned_ver_index)) {
586 chosen_unversioned_ver_index = ver_i;
587 }
578588 if (is_weak) weak_linkages.set(ver_i);
579589 }
580590 if (last) break;
......@@ -585,25 +595,10 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
585595 } else continue;
586596 }
587597
588 // Pick the default symbol version:
589 // - If there are no versions, don't emit it
590 // - Take the greatest one <= than the target one
591 // - If none of them is <= than the
592 // specified one don't pick any default version
593 var chosen_def_ver_index: usize = 255;
594 {
595 var versions_iter = versions.iterator(.{});
596 while (versions_iter.next()) |ver_i| {
597 if (chosen_def_ver_index == 255 or ver_i > chosen_def_ver_index) {
598 chosen_def_ver_index = ver_i;
599 }
600 }
601 }
602
603598 {
604599 var versions_iter = versions.iterator(.{});
605600 while (versions_iter.next()) |ver_index| {
606 if (sym_unversioned) {
601 if (chosen_unversioned_ver_index != 255 and ver_index == chosen_unversioned_ver_index) {
607602 // Example:
608603 // .balign 4
609604 // .globl _Exit
......@@ -701,11 +696,17 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
701696 opt_symbol_name = sym_name_buf.items;
702697 versions.unsetAll();
703698 weak_linkages.unsetAll();
704 sym_unversioned = false;
705699
706700 break :n sym_name_buf.items;
707701 };
708702
703 // Pick the default symbol version:
704 // - If there are no versions, don't emit it
705 // - Take the greatest one <= than the target one
706 // - If none of them is <= than the
707 // specified one don't pick any default version
708 var chosen_def_ver_index: usize = 255;
709 var chosen_unversioned_ver_index: usize = 255;
709710 {
710711 const targets = try std.leb.readUleb128(u64, inc_reader);
711712 const size = try std.leb.readUleb128(u16, inc_reader);
......@@ -728,8 +729,13 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
728729 const ver_i = @as(u7, @truncate(byte));
729730 if (ok_lib_and_target and ver_i <= target_ver_index) {
730731 versions.set(ver_i);
732 if (chosen_def_ver_index == 255 or ver_i > chosen_def_ver_index) {
733 chosen_def_ver_index = ver_i;
734 }
735 if (is_unversioned and (chosen_unversioned_ver_index == 255 or ver_i > chosen_unversioned_ver_index)) {
736 chosen_unversioned_ver_index = ver_i;
737 }
731738 sizes[ver_i] = size;
732 if (is_unversioned) sym_unversioned = true;
733739 if (is_weak) weak_linkages.set(ver_i);
734740 }
735741 if (last) break;
......@@ -740,25 +746,10 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
740746 } else continue;
741747 }
742748
743 // Pick the default symbol version:
744 // - If there are no versions, don't emit it
745 // - Take the greatest one <= than the target one
746 // - If none of them is <= than the
747 // specified one don't pick any default version
748 var chosen_def_ver_index: usize = 255;
749 {
750 var versions_iter = versions.iterator(.{});
751 while (versions_iter.next()) |ver_i| {
752 if (chosen_def_ver_index == 255 or ver_i > chosen_def_ver_index) {
753 chosen_def_ver_index = ver_i;
754 }
755 }
756 }
757
758749 {
759750 var versions_iter = versions.iterator(.{});
760751 while (versions_iter.next()) |ver_index| {
761 if (sym_unversioned) {
752 if (chosen_unversioned_ver_index != 255 and ver_index == chosen_unversioned_ver_index) {
762753 // Example:
763754 // .balign 4
764755 // .globl malloc_conf
......@@ -843,11 +834,17 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
843834 opt_symbol_name = sym_name_buf.items;
844835 versions.unsetAll();
845836 weak_linkages.unsetAll();
846 sym_unversioned = false;
847837
848838 break :n sym_name_buf.items;
849839 };
850840
841 // Pick the default symbol version:
842 // - If there are no versions, don't emit it
843 // - Take the greatest one <= than the target one
844 // - If none of them is <= than the
845 // specified one don't pick any default version
846 var chosen_def_ver_index: usize = 255;
847 var chosen_unversioned_ver_index: usize = 255;
851848 {
852849 const targets = try std.leb.readUleb128(u64, inc_reader);
853850 const size = try std.leb.readUleb128(u16, inc_reader);
......@@ -870,8 +867,13 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
870867 const ver_i = @as(u7, @truncate(byte));
871868 if (ok_lib_and_target and ver_i <= target_ver_index) {
872869 versions.set(ver_i);
870 if (chosen_def_ver_index == 255 or ver_i > chosen_def_ver_index) {
871 chosen_def_ver_index = ver_i;
872 }
873 if (is_unversioned and (chosen_unversioned_ver_index == 255 or ver_i > chosen_unversioned_ver_index)) {
874 chosen_unversioned_ver_index = ver_i;
875 }
873876 sizes[ver_i] = size;
874 if (is_unversioned) sym_unversioned = true;
875877 if (is_weak) weak_linkages.set(ver_i);
876878 }
877879 if (last) break;
......@@ -882,25 +884,10 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
882884 } else continue;
883885 }
884886
885 // Pick the default symbol version:
886 // - If there are no versions, don't emit it
887 // - Take the greatest one <= than the target one
888 // - If none of them is <= than the
889 // specified one don't pick any default version
890 var chosen_def_ver_index: usize = 255;
891 {
892 var versions_iter = versions.iterator(.{});
893 while (versions_iter.next()) |ver_i| {
894 if (chosen_def_ver_index == 255 or ver_i > chosen_def_ver_index) {
895 chosen_def_ver_index = ver_i;
896 }
897 }
898 }
899
900887 {
901888 var versions_iter = versions.iterator(.{});
902889 while (versions_iter.next()) |ver_index| {
903 if (sym_unversioned) {
890 if (chosen_unversioned_ver_index != 255 and ver_index == chosen_unversioned_ver_index) {
904891 // Example:
905892 // .balign 4
906893 // .globl _ThreadRuneLocale