authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-05-17 20:10:11+02:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-05-17 20:13:02+02:00
logcd1eea09648f9ca6eeaf3882e1f74f90fe6c8ab3
tree730cb3e49322a60fa86886470364c06c1d243508
parenta97e417ab1e555d14e6d76df722de92cca85ba2f
signaturebadge-check Signed by SSH key SHA256:7B/LJ7bpR1eX8aCXSr4mtd5M45VMPKcx9zY8e95b5QM

freebsd: Fix stub libraries containing versioned symbols that shouldn't be.

Closes #23911.

1 files changed, 132 insertions(+), 132 deletions(-)

src/libs/freebsd.zig+132-132
...@@ -539,6 +539,14 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye...@@ -539,6 +539,14 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
539539
540 const fn_inclusions_len = try inc_reader.readInt(u16, .little);540 const fn_inclusions_len = try inc_reader.readInt(u16, .little);
541541
542 // Pick the default symbol version:
543 // - If there are no versions, don't emit it
544 // - Take the greatest one <= than the target one
545 // - If none of them is <= than the
546 // specified one don't pick any default version
547 var chosen_def_ver_index: usize = 255;
548 var chosen_unversioned_ver_index: usize = 255;
549
542 while (sym_i < fn_inclusions_len) : (sym_i += 1) {550 while (sym_i < fn_inclusions_len) : (sym_i += 1) {
543 const sym_name = opt_symbol_name orelse n: {551 const sym_name = opt_symbol_name orelse n: {
544 sym_name_buf.clearRetainingCapacity();552 sym_name_buf.clearRetainingCapacity();
...@@ -547,17 +555,11 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye...@@ -547,17 +555,11 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
547 opt_symbol_name = sym_name_buf.items;555 opt_symbol_name = sym_name_buf.items;
548 versions.unsetAll();556 versions.unsetAll();
549 weak_linkages.unsetAll();557 weak_linkages.unsetAll();
558 chosen_def_ver_index = 255;
559 chosen_unversioned_ver_index = 255;
550560
551 break :n sym_name_buf.items;561 break :n sym_name_buf.items;
552 };562 };
553
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;
561 {563 {
562 const targets = try std.leb.readUleb128(u64, inc_reader);564 const targets = try std.leb.readUleb128(u64, inc_reader);
563 var lib_index = try inc_reader.readByte();565 var lib_index = try inc_reader.readByte();
...@@ -578,14 +580,19 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye...@@ -578,14 +580,19 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
578 const last = (byte & 0b1000_0000) != 0;580 const last = (byte & 0b1000_0000) != 0;
579 const ver_i = @as(u7, @truncate(byte));581 const ver_i = @as(u7, @truncate(byte));
580 if (ok_lib_and_target and ver_i <= target_ver_index) {582 if (ok_lib_and_target and ver_i <= target_ver_index) {
581 versions.set(ver_i);583 if (is_unversioned) {
582 if (chosen_def_ver_index == 255 or ver_i > chosen_def_ver_index) {584 if (chosen_unversioned_ver_index == 255 or ver_i > chosen_unversioned_ver_index) {
583 chosen_def_ver_index = ver_i;585 chosen_unversioned_ver_index = ver_i;
586 }
587 } else {
588 if (chosen_def_ver_index == 255 or ver_i > chosen_def_ver_index) {
589 chosen_def_ver_index = ver_i;
590 }
591
592 versions.set(ver_i);
584 }593 }
585 if (is_unversioned and (chosen_unversioned_ver_index == 255 or ver_i > chosen_unversioned_ver_index)) {594
586 chosen_unversioned_ver_index = ver_i;595 weak_linkages.setValue(ver_i, is_weak);
587 }
588 if (is_weak) weak_linkages.set(ver_i);
589 }596 }
590 if (last) break;597 if (last) break;
591 }598 }
...@@ -595,31 +602,31 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye...@@ -595,31 +602,31 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
595 } else continue;602 } else continue;
596 }603 }
597604
605 if (chosen_unversioned_ver_index != 255) {
606 // Example:
607 // .balign 4
608 // .globl _Exit
609 // .type _Exit, %function
610 // _Exit: .long 0
611 try stubs_writer.print(
612 \\.balign {d}
613 \\.{s} {s}
614 \\.type {s}, %function
615 \\{s}: {s} 0
616 \\
617 , .{
618 target.ptrBitWidth() / 8,
619 if (weak_linkages.isSet(chosen_unversioned_ver_index)) "weak" else "globl",
620 sym_name,
621 sym_name,
622 sym_name,
623 wordDirective(target),
624 });
625 }
626
598 {627 {
599 var versions_iter = versions.iterator(.{});628 var versions_iter = versions.iterator(.{});
600 while (versions_iter.next()) |ver_index| {629 while (versions_iter.next()) |ver_index| {
601 if (chosen_unversioned_ver_index != 255 and ver_index == chosen_unversioned_ver_index) {
602 // Example:
603 // .balign 4
604 // .globl _Exit
605 // .type _Exit, %function
606 // _Exit: .long 0
607 try stubs_writer.print(
608 \\.balign {d}
609 \\.{s} {s}
610 \\.type {s}, %function
611 \\{s}: {s} 0
612 \\
613 , .{
614 target.ptrBitWidth() / 8,
615 if (weak_linkages.isSet(ver_index)) "weak" else "globl",
616 sym_name,
617 sym_name,
618 sym_name,
619 wordDirective(target),
620 });
621 }
622
623 // Example:630 // Example:
624 // .balign 4631 // .balign 4
625 // .globl _Exit_1_0632 // .globl _Exit_1_0
...@@ -627,10 +634,6 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye...@@ -627,10 +634,6 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
627 // .symver _Exit_1_0, _Exit@@FBSD_1.0, remove634 // .symver _Exit_1_0, _Exit@@FBSD_1.0, remove
628 // _Exit_1_0: .long 0635 // _Exit_1_0: .long 0
629 const ver = metadata.all_versions[ver_index];636 const ver = metadata.all_versions[ver_index];
630
631 // Default symbol version definition vs normal symbol version definition
632 const want_default = chosen_def_ver_index != 255 and ver_index == chosen_def_ver_index;
633 const at_sign_str: []const u8 = if (want_default) "@@" else "@";
634 const sym_plus_ver = try std.fmt.allocPrint(637 const sym_plus_ver = try std.fmt.allocPrint(
635 arena,638 arena,
636 "{s}_FBSD_{d}_{d}",639 "{s}_FBSD_{d}_{d}",
...@@ -651,7 +654,8 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye...@@ -651,7 +654,8 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
651 sym_plus_ver,654 sym_plus_ver,
652 sym_plus_ver,655 sym_plus_ver,
653 sym_name,656 sym_name,
654 at_sign_str,657 // Default symbol version definition vs normal symbol version definition
658 if (chosen_def_ver_index != 255 and ver_index == chosen_def_ver_index) "@@" else "@",
655 ver.major,659 ver.major,
656 ver.minor,660 ver.minor,
657 sym_plus_ver,661 sym_plus_ver,
...@@ -688,6 +692,7 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye...@@ -688,6 +692,7 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
688692
689 sym_i = 0;693 sym_i = 0;
690 opt_symbol_name = null;694 opt_symbol_name = null;
695
691 while (sym_i < obj_inclusions_len) : (sym_i += 1) {696 while (sym_i < obj_inclusions_len) : (sym_i += 1) {
692 const sym_name = opt_symbol_name orelse n: {697 const sym_name = opt_symbol_name orelse n: {
693 sym_name_buf.clearRetainingCapacity();698 sym_name_buf.clearRetainingCapacity();
...@@ -696,17 +701,12 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye...@@ -696,17 +701,12 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
696 opt_symbol_name = sym_name_buf.items;701 opt_symbol_name = sym_name_buf.items;
697 versions.unsetAll();702 versions.unsetAll();
698 weak_linkages.unsetAll();703 weak_linkages.unsetAll();
704 chosen_def_ver_index = 255;
705 chosen_unversioned_ver_index = 255;
699706
700 break :n sym_name_buf.items;707 break :n sym_name_buf.items;
701 };708 };
702709
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;
710 {710 {
711 const targets = try std.leb.readUleb128(u64, inc_reader);711 const targets = try std.leb.readUleb128(u64, inc_reader);
712 const size = try std.leb.readUleb128(u16, inc_reader);712 const size = try std.leb.readUleb128(u16, inc_reader);
...@@ -728,15 +728,20 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye...@@ -728,15 +728,20 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
728 const last = (byte & 0b1000_0000) != 0;728 const last = (byte & 0b1000_0000) != 0;
729 const ver_i = @as(u7, @truncate(byte));729 const ver_i = @as(u7, @truncate(byte));
730 if (ok_lib_and_target and ver_i <= target_ver_index) {730 if (ok_lib_and_target and ver_i <= target_ver_index) {
731 versions.set(ver_i);731 if (is_unversioned) {
732 if (chosen_def_ver_index == 255 or ver_i > chosen_def_ver_index) {732 if (chosen_unversioned_ver_index == 255 or ver_i > chosen_unversioned_ver_index) {
733 chosen_def_ver_index = ver_i;733 chosen_unversioned_ver_index = ver_i;
734 }734 }
735 if (is_unversioned and (chosen_unversioned_ver_index == 255 or ver_i > chosen_unversioned_ver_index)) {735 } else {
736 chosen_unversioned_ver_index = ver_i;736 if (chosen_def_ver_index == 255 or ver_i > chosen_def_ver_index) {
737 chosen_def_ver_index = ver_i;
738 }
739
740 versions.set(ver_i);
737 }741 }
742
738 sizes[ver_i] = size;743 sizes[ver_i] = size;
739 if (is_weak) weak_linkages.set(ver_i);744 weak_linkages.setValue(ver_i, is_weak);
740 }745 }
741 if (last) break;746 if (last) break;
742 }747 }
...@@ -746,35 +751,35 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye...@@ -746,35 +751,35 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
746 } else continue;751 } else continue;
747 }752 }
748753
754 if (chosen_unversioned_ver_index != 255) {
755 // Example:
756 // .balign 4
757 // .globl malloc_conf
758 // .type malloc_conf, %object
759 // .size malloc_conf, 4
760 // malloc_conf: .fill 4, 1, 0
761 try stubs_writer.print(
762 \\.balign {d}
763 \\.{s} {s}
764 \\.type {s}, %object
765 \\.size {s}, {d}
766 \\{s}: {s} 0
767 \\
768 , .{
769 target.ptrBitWidth() / 8,
770 if (weak_linkages.isSet(chosen_unversioned_ver_index)) "weak" else "globl",
771 sym_name,
772 sym_name,
773 sym_name,
774 sizes[chosen_unversioned_ver_index],
775 sym_name,
776 wordDirective(target),
777 });
778 }
779
749 {780 {
750 var versions_iter = versions.iterator(.{});781 var versions_iter = versions.iterator(.{});
751 while (versions_iter.next()) |ver_index| {782 while (versions_iter.next()) |ver_index| {
752 if (chosen_unversioned_ver_index != 255 and ver_index == chosen_unversioned_ver_index) {
753 // Example:
754 // .balign 4
755 // .globl malloc_conf
756 // .type malloc_conf, %object
757 // .size malloc_conf, 4
758 // malloc_conf: .fill 4, 1, 0
759 try stubs_writer.print(
760 \\.balign {d}
761 \\.{s} {s}
762 \\.type {s}, %object
763 \\.size {s}, {d}
764 \\{s}: {s} 0
765 \\
766 , .{
767 target.ptrBitWidth() / 8,
768 if (weak_linkages.isSet(ver_index)) "weak" else "globl",
769 sym_name,
770 sym_name,
771 sym_name,
772 sizes[ver_index],
773 sym_name,
774 wordDirective(target),
775 });
776 }
777
778 // Example:783 // Example:
779 // .balign 4784 // .balign 4
780 // .globl malloc_conf_1_3785 // .globl malloc_conf_1_3
...@@ -783,10 +788,6 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye...@@ -783,10 +788,6 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
783 // .symver malloc_conf_1_3, malloc_conf@@FBSD_1.3788 // .symver malloc_conf_1_3, malloc_conf@@FBSD_1.3
784 // malloc_conf_1_3: .fill 4, 1, 0789 // malloc_conf_1_3: .fill 4, 1, 0
785 const ver = metadata.all_versions[ver_index];790 const ver = metadata.all_versions[ver_index];
786
787 // Default symbol version definition vs normal symbol version definition
788 const want_default = chosen_def_ver_index != 255 and ver_index == chosen_def_ver_index;
789 const at_sign_str: []const u8 = if (want_default) "@@" else "@";
790 const sym_plus_ver = try std.fmt.allocPrint(791 const sym_plus_ver = try std.fmt.allocPrint(
791 arena,792 arena,
792 "{s}_FBSD_{d}_{d}",793 "{s}_FBSD_{d}_{d}",
...@@ -810,7 +811,8 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye...@@ -810,7 +811,8 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
810 sizes[ver_index],811 sizes[ver_index],
811 sym_plus_ver,812 sym_plus_ver,
812 sym_name,813 sym_name,
813 at_sign_str,814 // Default symbol version definition vs normal symbol version definition
815 if (chosen_def_ver_index != 255 and ver_index == chosen_def_ver_index) "@@" else "@",
814 ver.major,816 ver.major,
815 ver.minor,817 ver.minor,
816 sym_plus_ver,818 sym_plus_ver,
...@@ -826,6 +828,7 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye...@@ -826,6 +828,7 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
826828
827 sym_i = 0;829 sym_i = 0;
828 opt_symbol_name = null;830 opt_symbol_name = null;
831
829 while (sym_i < tls_inclusions_len) : (sym_i += 1) {832 while (sym_i < tls_inclusions_len) : (sym_i += 1) {
830 const sym_name = opt_symbol_name orelse n: {833 const sym_name = opt_symbol_name orelse n: {
831 sym_name_buf.clearRetainingCapacity();834 sym_name_buf.clearRetainingCapacity();
...@@ -834,17 +837,12 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye...@@ -834,17 +837,12 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
834 opt_symbol_name = sym_name_buf.items;837 opt_symbol_name = sym_name_buf.items;
835 versions.unsetAll();838 versions.unsetAll();
836 weak_linkages.unsetAll();839 weak_linkages.unsetAll();
840 chosen_def_ver_index = 255;
841 chosen_unversioned_ver_index = 255;
837842
838 break :n sym_name_buf.items;843 break :n sym_name_buf.items;
839 };844 };
840845
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;
848 {846 {
849 const targets = try std.leb.readUleb128(u64, inc_reader);847 const targets = try std.leb.readUleb128(u64, inc_reader);
850 const size = try std.leb.readUleb128(u16, inc_reader);848 const size = try std.leb.readUleb128(u16, inc_reader);
...@@ -866,15 +864,20 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye...@@ -866,15 +864,20 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
866 const last = (byte & 0b1000_0000) != 0;864 const last = (byte & 0b1000_0000) != 0;
867 const ver_i = @as(u7, @truncate(byte));865 const ver_i = @as(u7, @truncate(byte));
868 if (ok_lib_and_target and ver_i <= target_ver_index) {866 if (ok_lib_and_target and ver_i <= target_ver_index) {
869 versions.set(ver_i);867 if (is_unversioned) {
870 if (chosen_def_ver_index == 255 or ver_i > chosen_def_ver_index) {868 if (chosen_unversioned_ver_index == 255 or ver_i > chosen_unversioned_ver_index) {
871 chosen_def_ver_index = ver_i;869 chosen_unversioned_ver_index = ver_i;
872 }870 }
873 if (is_unversioned and (chosen_unversioned_ver_index == 255 or ver_i > chosen_unversioned_ver_index)) {871 } else {
874 chosen_unversioned_ver_index = ver_i;872 if (chosen_def_ver_index == 255 or ver_i > chosen_def_ver_index) {
873 chosen_def_ver_index = ver_i;
874 }
875
876 versions.set(ver_i);
875 }877 }
878
876 sizes[ver_i] = size;879 sizes[ver_i] = size;
877 if (is_weak) weak_linkages.set(ver_i);880 weak_linkages.setValue(ver_i, is_weak);
878 }881 }
879 if (last) break;882 if (last) break;
880 }883 }
...@@ -884,35 +887,35 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye...@@ -884,35 +887,35 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
884 } else continue;887 } else continue;
885 }888 }
886889
890 if (chosen_unversioned_ver_index != 255) {
891 // Example:
892 // .balign 4
893 // .globl _ThreadRuneLocale
894 // .type _ThreadRuneLocale, %object
895 // .size _ThreadRuneLocale, 4
896 // _ThreadRuneLocale: .fill 4, 1, 0
897 try stubs_writer.print(
898 \\.balign {d}
899 \\.{s} {s}
900 \\.type {s}, %tls_object
901 \\.size {s}, {d}
902 \\{s}: {s} 0
903 \\
904 , .{
905 target.ptrBitWidth() / 8,
906 if (weak_linkages.isSet(chosen_unversioned_ver_index)) "weak" else "globl",
907 sym_name,
908 sym_name,
909 sym_name,
910 sizes[chosen_unversioned_ver_index],
911 sym_name,
912 wordDirective(target),
913 });
914 }
915
887 {916 {
888 var versions_iter = versions.iterator(.{});917 var versions_iter = versions.iterator(.{});
889 while (versions_iter.next()) |ver_index| {918 while (versions_iter.next()) |ver_index| {
890 if (chosen_unversioned_ver_index != 255 and ver_index == chosen_unversioned_ver_index) {
891 // Example:
892 // .balign 4
893 // .globl _ThreadRuneLocale
894 // .type _ThreadRuneLocale, %object
895 // .size _ThreadRuneLocale, 4
896 // _ThreadRuneLocale: .fill 4, 1, 0
897 try stubs_writer.print(
898 \\.balign {d}
899 \\.{s} {s}
900 \\.type {s}, %tls_object
901 \\.size {s}, {d}
902 \\{s}: {s} 0
903 \\
904 , .{
905 target.ptrBitWidth() / 8,
906 if (weak_linkages.isSet(ver_index)) "weak" else "globl",
907 sym_name,
908 sym_name,
909 sym_name,
910 sizes[ver_index],
911 sym_name,
912 wordDirective(target),
913 });
914 }
915
916 // Example:919 // Example:
917 // .balign 4920 // .balign 4
918 // .globl _ThreadRuneLocale_1_3921 // .globl _ThreadRuneLocale_1_3
...@@ -921,10 +924,6 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye...@@ -921,10 +924,6 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
921 // .symver _ThreadRuneLocale_1_3, _ThreadRuneLocale@@FBSD_1.3924 // .symver _ThreadRuneLocale_1_3, _ThreadRuneLocale@@FBSD_1.3
922 // _ThreadRuneLocale_1_3: .fill 4, 1, 0925 // _ThreadRuneLocale_1_3: .fill 4, 1, 0
923 const ver = metadata.all_versions[ver_index];926 const ver = metadata.all_versions[ver_index];
924
925 // Default symbol version definition vs normal symbol version definition
926 const want_default = chosen_def_ver_index != 255 and ver_index == chosen_def_ver_index;
927 const at_sign_str: []const u8 = if (want_default) "@@" else "@";
928 const sym_plus_ver = try std.fmt.allocPrint(927 const sym_plus_ver = try std.fmt.allocPrint(
929 arena,928 arena,
930 "{s}_FBSD_{d}_{d}",929 "{s}_FBSD_{d}_{d}",
...@@ -948,7 +947,8 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye...@@ -948,7 +947,8 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
948 sizes[ver_index],947 sizes[ver_index],
949 sym_plus_ver,948 sym_plus_ver,
950 sym_name,949 sym_name,
951 at_sign_str,950 // Default symbol version definition vs normal symbol version definition
951 if (chosen_def_ver_index != 255 and ver_index == chosen_def_ver_index) "@@" else "@",
952 ver.major,952 ver.major,
953 ver.minor,953 ver.minor,
954 sym_plus_ver,954 sym_plus_ver,