Skip to content

Commit 9b2819c

Browse files
andrewrkFri3dNstuff
authored andcommitted
reject crti.o/crtn.o, embrace the future
crti.o/crtn.o is a legacy strategy for calling constructor functions upon object loading that has been superseded by the init_array/fini_array mechanism. Zig code depends on neither, since the language intentionally has no way to initialize data at runtime, but alas the Zig linker still must support this feature since popular languages depend on it. Anyway, the way it works is that crti.o has the machine code prelude of two functions called _init and _fini, each in their own section with the respective name. crtn.o has the machine code instructions comprising the exitlude for each function. In between, objects use the .init and .fini link section to populate the function body. This function is then expected to be called upon object initialization and deinitialization. This mechanism is depended on by libc, for example musl and glibc, but only for older ISAs. By the time the libcs gained support for newer ISAs, they had moved on to the init_array/fini_array mechanism instead. For the Zig linker, we are trying to move the linker towards order-independent objects which is incompatible with the legacy crti/crtn mechanism. Therefore, this commit drops support entirely for crti/crtn mechanism, which is necessary since the other commits in this branch make it nondeterministic in which order the libc objects and the other link inputs are sent to the linker. The linker is still expected to produce a deterministic output, however, by ignoring object input order for the purposes of symbol resolution.
1 parent 74719ba commit 9b2819c

File tree

3 files changed

+2
-72
lines changed

3 files changed

+2
-72
lines changed

lib/std/zig/LibCInstallation.zig

+2-68
Original file line numberDiff line numberDiff line change
@@ -694,10 +694,8 @@ fn appendCcExe(args: *std.ArrayList([]const u8), skip_cc_env_var: bool) !void {
694694
/// `CsuPaths`.
695695
pub const CrtBasenames = struct {
696696
crt0: ?[]const u8 = null,
697-
crti: ?[]const u8 = null,
698697
crtbegin: ?[]const u8 = null,
699698
crtend: ?[]const u8 = null,
700-
crtn: ?[]const u8 = null,
701699

702700
pub const GetArgs = struct {
703701
target: std.Target,
@@ -751,137 +749,96 @@ pub const CrtBasenames = struct {
751749

752750
return switch (target.os.tag) {
753751
.linux => switch (mode) {
754-
.dynamic_lib => .{
755-
.crti = "crti.o",
756-
.crtn = "crtn.o",
757-
},
752+
.dynamic_lib => .{},
758753
.dynamic_exe => .{
759754
.crt0 = "crt1.o",
760-
.crti = "crti.o",
761-
.crtn = "crtn.o",
762755
},
763756
.dynamic_pie => .{
764757
.crt0 = "Scrt1.o",
765-
.crti = "crti.o",
766-
.crtn = "crtn.o",
767758
},
768759
.static_exe => .{
769760
.crt0 = "crt1.o",
770-
.crti = "crti.o",
771-
.crtn = "crtn.o",
772761
},
773762
.static_pie => .{
774763
.crt0 = "rcrt1.o",
775-
.crti = "crti.o",
776-
.crtn = "crtn.o",
777764
},
778765
},
779766
.dragonfly => switch (mode) {
780767
.dynamic_lib => .{
781-
.crti = "crti.o",
782768
.crtbegin = "crtbeginS.o",
783769
.crtend = "crtendS.o",
784-
.crtn = "crtn.o",
785770
},
786771
.dynamic_exe => .{
787772
.crt0 = "crt1.o",
788-
.crti = "crti.o",
789773
.crtbegin = "crtbegin.o",
790774
.crtend = "crtend.o",
791-
.crtn = "crtn.o",
792775
},
793776
.dynamic_pie => .{
794777
.crt0 = "Scrt1.o",
795-
.crti = "crti.o",
796778
.crtbegin = "crtbeginS.o",
797779
.crtend = "crtendS.o",
798-
.crtn = "crtn.o",
799780
},
800781
.static_exe => .{
801782
.crt0 = "crt1.o",
802-
.crti = "crti.o",
803783
.crtbegin = "crtbegin.o",
804784
.crtend = "crtend.o",
805-
.crtn = "crtn.o",
806785
},
807786
.static_pie => .{
808787
.crt0 = "Scrt1.o",
809-
.crti = "crti.o",
810788
.crtbegin = "crtbeginS.o",
811789
.crtend = "crtendS.o",
812-
.crtn = "crtn.o",
813790
},
814791
},
815792
.freebsd => switch (mode) {
816793
.dynamic_lib => .{
817-
.crti = "crti.o",
818794
.crtbegin = "crtbeginS.o",
819795
.crtend = "crtendS.o",
820-
.crtn = "crtn.o",
821796
},
822797
.dynamic_exe => .{
823798
.crt0 = "crt1.o",
824-
.crti = "crti.o",
825799
.crtbegin = "crtbegin.o",
826800
.crtend = "crtend.o",
827-
.crtn = "crtn.o",
828801
},
829802
.dynamic_pie => .{
830803
.crt0 = "Scrt1.o",
831-
.crti = "crti.o",
832804
.crtbegin = "crtbeginS.o",
833805
.crtend = "crtendS.o",
834-
.crtn = "crtn.o",
835806
},
836807
.static_exe => .{
837808
.crt0 = "crt1.o",
838-
.crti = "crti.o",
839809
.crtbegin = "crtbeginT.o",
840810
.crtend = "crtend.o",
841-
.crtn = "crtn.o",
842811
},
843812
.static_pie => .{
844813
.crt0 = "Scrt1.o",
845-
.crti = "crti.o",
846814
.crtbegin = "crtbeginS.o",
847815
.crtend = "crtendS.o",
848-
.crtn = "crtn.o",
849816
},
850817
},
851818
.netbsd => switch (mode) {
852819
.dynamic_lib => .{
853-
.crti = "crti.o",
854820
.crtbegin = "crtbeginS.o",
855821
.crtend = "crtendS.o",
856-
.crtn = "crtn.o",
857822
},
858823
.dynamic_exe => .{
859824
.crt0 = "crt0.o",
860-
.crti = "crti.o",
861825
.crtbegin = "crtbegin.o",
862826
.crtend = "crtend.o",
863-
.crtn = "crtn.o",
864827
},
865828
.dynamic_pie => .{
866829
.crt0 = "crt0.o",
867-
.crti = "crti.o",
868830
.crtbegin = "crtbeginS.o",
869831
.crtend = "crtendS.o",
870-
.crtn = "crtn.o",
871832
},
872833
.static_exe => .{
873834
.crt0 = "crt0.o",
874-
.crti = "crti.o",
875835
.crtbegin = "crtbeginT.o",
876836
.crtend = "crtend.o",
877-
.crtn = "crtn.o",
878837
},
879838
.static_pie => .{
880839
.crt0 = "crt0.o",
881-
.crti = "crti.o",
882840
.crtbegin = "crtbeginT.o",
883841
.crtend = "crtendS.o",
884-
.crtn = "crtn.o",
885842
},
886843
},
887844
.openbsd => switch (mode) {
@@ -902,49 +859,34 @@ pub const CrtBasenames = struct {
902859
},
903860
.haiku => switch (mode) {
904861
.dynamic_lib => .{
905-
.crti = "crti.o",
906862
.crtbegin = "crtbeginS.o",
907863
.crtend = "crtendS.o",
908-
.crtn = "crtn.o",
909864
},
910865
.dynamic_exe => .{
911866
.crt0 = "start_dyn.o",
912-
.crti = "crti.o",
913867
.crtbegin = "crtbegin.o",
914868
.crtend = "crtend.o",
915-
.crtn = "crtn.o",
916869
},
917870
.dynamic_pie => .{
918871
.crt0 = "start_dyn.o",
919-
.crti = "crti.o",
920872
.crtbegin = "crtbeginS.o",
921873
.crtend = "crtendS.o",
922-
.crtn = "crtn.o",
923874
},
924875
.static_exe => .{
925876
.crt0 = "start_dyn.o",
926-
.crti = "crti.o",
927877
.crtbegin = "crtbegin.o",
928878
.crtend = "crtend.o",
929-
.crtn = "crtn.o",
930879
},
931880
.static_pie => .{
932881
.crt0 = "start_dyn.o",
933-
.crti = "crti.o",
934882
.crtbegin = "crtbeginS.o",
935883
.crtend = "crtendS.o",
936-
.crtn = "crtn.o",
937884
},
938885
},
939886
.solaris, .illumos => switch (mode) {
940-
.dynamic_lib => .{
941-
.crti = "crti.o",
942-
.crtn = "crtn.o",
943-
},
887+
.dynamic_lib => .{},
944888
.dynamic_exe, .dynamic_pie => .{
945889
.crt0 = "crt1.o",
946-
.crti = "crti.o",
947-
.crtn = "crtn.o",
948890
},
949891
.static_exe, .static_pie => .{},
950892
},
@@ -955,10 +897,8 @@ pub const CrtBasenames = struct {
955897

956898
pub const CrtPaths = struct {
957899
crt0: ?Path = null,
958-
crti: ?Path = null,
959900
crtbegin: ?Path = null,
960901
crtend: ?Path = null,
961-
crtn: ?Path = null,
962902
};
963903

964904
pub fn resolveCrtPaths(
@@ -980,7 +920,6 @@ pub fn resolveCrtPaths(
980920
}) orelse true) "gcc80" else "gcc54";
981921
return .{
982922
.crt0 = if (crt_basenames.crt0) |basename| try crt_dir_path.join(arena, basename) else null,
983-
.crti = if (crt_basenames.crti) |basename| try crt_dir_path.join(arena, basename) else null,
984923
.crtbegin = if (crt_basenames.crtbegin) |basename| .{
985924
.root_dir = crt_dir_path.root_dir,
986925
.sub_path = try fs.path.join(arena, &.{ crt_dir_path.sub_path, gccv, basename }),
@@ -989,7 +928,6 @@ pub fn resolveCrtPaths(
989928
.root_dir = crt_dir_path.root_dir,
990929
.sub_path = try fs.path.join(arena, &.{ crt_dir_path.sub_path, gccv, basename }),
991930
} else null,
992-
.crtn = if (crt_basenames.crtn) |basename| try crt_dir_path.join(arena, basename) else null,
993931
};
994932
},
995933
.haiku => {
@@ -999,19 +937,15 @@ pub fn resolveCrtPaths(
999937
};
1000938
return .{
1001939
.crt0 = if (crt_basenames.crt0) |basename| try crt_dir_path.join(arena, basename) else null,
1002-
.crti = if (crt_basenames.crti) |basename| try crt_dir_path.join(arena, basename) else null,
1003940
.crtbegin = if (crt_basenames.crtbegin) |basename| try gcc_dir_path.join(arena, basename) else null,
1004941
.crtend = if (crt_basenames.crtend) |basename| try gcc_dir_path.join(arena, basename) else null,
1005-
.crtn = if (crt_basenames.crtn) |basename| try crt_dir_path.join(arena, basename) else null,
1006942
};
1007943
},
1008944
else => {
1009945
return .{
1010946
.crt0 = if (crt_basenames.crt0) |basename| try crt_dir_path.join(arena, basename) else null,
1011-
.crti = if (crt_basenames.crti) |basename| try crt_dir_path.join(arena, basename) else null,
1012947
.crtbegin = if (crt_basenames.crtbegin) |basename| try crt_dir_path.join(arena, basename) else null,
1013948
.crtend = if (crt_basenames.crtend) |basename| try crt_dir_path.join(arena, basename) else null,
1014-
.crtn = if (crt_basenames.crtn) |basename| try crt_dir_path.join(arena, basename) else null,
1015949
};
1016950
},
1017951
}

src/Compilation.zig

-2
Original file line numberDiff line numberDiff line change
@@ -6723,10 +6723,8 @@ fn getCrtPathsInner(
67236723

67246724
return .{
67256725
.crt0 = if (basenames.crt0) |basename| try crtFilePath(crt_files, basename) else null,
6726-
.crti = if (basenames.crti) |basename| try crtFilePath(crt_files, basename) else null,
67276726
.crtbegin = if (basenames.crtbegin) |basename| try crtFilePath(crt_files, basename) else null,
67286727
.crtend = if (basenames.crtend) |basename| try crtFilePath(crt_files, basename) else null,
6729-
.crtn = if (basenames.crtn) |basename| try crtFilePath(crt_files, basename) else null,
67306728
};
67316729
}
67326730

src/link/Elf.zig

-2
Original file line numberDiff line numberDiff line change
@@ -1881,7 +1881,6 @@ fn linkWithLLD(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: s
18811881
// csu prelude
18821882
const csu = try comp.getCrtPaths(arena);
18831883
if (csu.crt0) |p| try argv.append(try p.toString(arena));
1884-
if (csu.crti) |p| try argv.append(try p.toString(arena));
18851884
if (csu.crtbegin) |p| try argv.append(try p.toString(arena));
18861885

18871886
for (self.rpath_table.keys()) |rpath| {
@@ -2073,7 +2072,6 @@ fn linkWithLLD(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: s
20732072

20742073
// crt postlude
20752074
if (csu.crtend) |p| try argv.append(try p.toString(arena));
2076-
if (csu.crtn) |p| try argv.append(try p.toString(arena));
20772075

20782076
if (self.base.allow_shlib_undefined) {
20792077
try argv.append("--allow-shlib-undefined");

0 commit comments

Comments
 (0)