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
539539
540540 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
542550 while (sym_i < fn_inclusions_len) : (sym_i += 1) {
543551 const sym_name = opt_symbol_name orelse n: {
544552 sym_name_buf.clearRetainingCapacity();
......@@ -547,17 +555,11 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
547555 opt_symbol_name = sym_name_buf.items;
548556 versions.unsetAll();
549557 weak_linkages.unsetAll();
558 chosen_def_ver_index = 255;
559 chosen_unversioned_ver_index = 255;
550560
551561 break :n sym_name_buf.items;
552562 };
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;
561563 {
562564 const targets = try std.leb.readUleb128(u64, inc_reader);
563565 var lib_index = try inc_reader.readByte();
......@@ -578,14 +580,19 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
578580 const last = (byte & 0b1000_0000) != 0;
579581 const ver_i = @as(u7, @truncate(byte));
580582 if (ok_lib_and_target and ver_i <= target_ver_index) {
581 versions.set(ver_i);
582 if (chosen_def_ver_index == 255 or ver_i > chosen_def_ver_index) {
583 chosen_def_ver_index = ver_i;
583 if (is_unversioned) {
584 if (chosen_unversioned_ver_index == 255 or ver_i > chosen_unversioned_ver_index) {
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);
584593 }
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 }
588 if (is_weak) weak_linkages.set(ver_i);
594
595 weak_linkages.setValue(ver_i, is_weak);
589596 }
590597 if (last) break;
591598 }
......@@ -595,31 +602,31 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
595602 } else continue;
596603 }
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
598627 {
599628 var versions_iter = versions.iterator(.{});
600629 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
623630 // Example:
624631 // .balign 4
625632 // .globl _Exit_1_0
......@@ -627,10 +634,6 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
627634 // .symver _Exit_1_0, _Exit@@FBSD_1.0, remove
628635 // _Exit_1_0: .long 0
629636 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 "@";
634637 const sym_plus_ver = try std.fmt.allocPrint(
635638 arena,
636639 "{s}_FBSD_{d}_{d}",
......@@ -651,7 +654,8 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
651654 sym_plus_ver,
652655 sym_plus_ver,
653656 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 "@",
655659 ver.major,
656660 ver.minor,
657661 sym_plus_ver,
......@@ -688,6 +692,7 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
688692
689693 sym_i = 0;
690694 opt_symbol_name = null;
695
691696 while (sym_i < obj_inclusions_len) : (sym_i += 1) {
692697 const sym_name = opt_symbol_name orelse n: {
693698 sym_name_buf.clearRetainingCapacity();
......@@ -696,17 +701,12 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
696701 opt_symbol_name = sym_name_buf.items;
697702 versions.unsetAll();
698703 weak_linkages.unsetAll();
704 chosen_def_ver_index = 255;
705 chosen_unversioned_ver_index = 255;
699706
700707 break :n sym_name_buf.items;
701708 };
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;
710710 {
711711 const targets = try std.leb.readUleb128(u64, inc_reader);
712712 const size = try std.leb.readUleb128(u16, inc_reader);
......@@ -728,15 +728,20 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
728728 const last = (byte & 0b1000_0000) != 0;
729729 const ver_i = @as(u7, @truncate(byte));
730730 if (ok_lib_and_target and ver_i <= target_ver_index) {
731 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;
731 if (is_unversioned) {
732 if (chosen_unversioned_ver_index == 255 or ver_i > chosen_unversioned_ver_index) {
733 chosen_unversioned_ver_index = ver_i;
734 }
735 } else {
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);
737741 }
742
738743 sizes[ver_i] = size;
739 if (is_weak) weak_linkages.set(ver_i);
744 weak_linkages.setValue(ver_i, is_weak);
740745 }
741746 if (last) break;
742747 }
......@@ -746,35 +751,35 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
746751 } else continue;
747752 }
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
749780 {
750781 var versions_iter = versions.iterator(.{});
751782 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
778783 // Example:
779784 // .balign 4
780785 // .globl malloc_conf_1_3
......@@ -783,10 +788,6 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
783788 // .symver malloc_conf_1_3, malloc_conf@@FBSD_1.3
784789 // malloc_conf_1_3: .fill 4, 1, 0
785790 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 "@";
790791 const sym_plus_ver = try std.fmt.allocPrint(
791792 arena,
792793 "{s}_FBSD_{d}_{d}",
......@@ -810,7 +811,8 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
810811 sizes[ver_index],
811812 sym_plus_ver,
812813 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 "@",
814816 ver.major,
815817 ver.minor,
816818 sym_plus_ver,
......@@ -826,6 +828,7 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
826828
827829 sym_i = 0;
828830 opt_symbol_name = null;
831
829832 while (sym_i < tls_inclusions_len) : (sym_i += 1) {
830833 const sym_name = opt_symbol_name orelse n: {
831834 sym_name_buf.clearRetainingCapacity();
......@@ -834,17 +837,12 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
834837 opt_symbol_name = sym_name_buf.items;
835838 versions.unsetAll();
836839 weak_linkages.unsetAll();
840 chosen_def_ver_index = 255;
841 chosen_unversioned_ver_index = 255;
837842
838843 break :n sym_name_buf.items;
839844 };
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;
848846 {
849847 const targets = try std.leb.readUleb128(u64, inc_reader);
850848 const size = try std.leb.readUleb128(u16, inc_reader);
......@@ -866,15 +864,20 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
866864 const last = (byte & 0b1000_0000) != 0;
867865 const ver_i = @as(u7, @truncate(byte));
868866 if (ok_lib_and_target and ver_i <= target_ver_index) {
869 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;
867 if (is_unversioned) {
868 if (chosen_unversioned_ver_index == 255 or ver_i > chosen_unversioned_ver_index) {
869 chosen_unversioned_ver_index = ver_i;
870 }
871 } else {
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);
875877 }
878
876879 sizes[ver_i] = size;
877 if (is_weak) weak_linkages.set(ver_i);
880 weak_linkages.setValue(ver_i, is_weak);
878881 }
879882 if (last) break;
880883 }
......@@ -884,35 +887,35 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
884887 } else continue;
885888 }
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
887916 {
888917 var versions_iter = versions.iterator(.{});
889918 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
916919 // Example:
917920 // .balign 4
918921 // .globl _ThreadRuneLocale_1_3
......@@ -921,10 +924,6 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
921924 // .symver _ThreadRuneLocale_1_3, _ThreadRuneLocale@@FBSD_1.3
922925 // _ThreadRuneLocale_1_3: .fill 4, 1, 0
923926 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 "@";
928927 const sym_plus_ver = try std.fmt.allocPrint(
929928 arena,
930929 "{s}_FBSD_{d}_{d}",
......@@ -948,7 +947,8 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
948947 sizes[ver_index],
949948 sym_plus_ver,
950949 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 "@",
952952 ver.major,
953953 ver.minor,
954954 sym_plus_ver,