Commit 2bb8e1ff55

LemonBoy <thatlemon@gmail.com>
2021-05-11 12:43:58
stage2: Change libc components' linking order
Use the same order as Clang (and, by extension, GCC) for the three most important libc components: lm comes first, followed by lpthread and then lc.
1 parent 780f510
Changed files (2)
src/link/Elf.zig
@@ -1648,17 +1648,12 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
             // libc dep
             if (self.base.options.link_libc) {
                 if (self.base.options.libc_installation != null) {
-                    if (self.base.options.link_mode == .Static) {
-                        try argv.append("--start-group");
-                        try argv.append("-lc");
-                        try argv.append("-lm");
-                        try argv.append("--end-group");
-                    } else {
-                        try argv.append("-lc");
-                        try argv.append("-lm");
-                    }
-
+                    const needs_grouping = self.base.options.link_mode == .Static;
+                    if (needs_grouping) try argv.append("--start-group");
+                    try argv.append("-lm");
                     try argv.append("-lpthread");
+                    try argv.append("-lc");
+                    if (needs_grouping) try argv.append("--end-group");
                 } else if (target.isGnuLibC()) {
                     try argv.append(comp.libunwind_static_lib.?.full_object_path);
                     for (glibc.libs) |lib| {
src/glibc.zig
@@ -40,10 +40,11 @@ pub const ABI = struct {
     }
 };
 
+// The order of the elements in this array defines the linking order.
 pub const libs = [_]Lib{
-    .{ .name = "c", .sover = 6 },
     .{ .name = "m", .sover = 6 },
     .{ .name = "pthread", .sover = 0 },
+    .{ .name = "c", .sover = 6 },
     .{ .name = "dl", .sover = 2 },
     .{ .name = "rt", .sover = 1 },
     .{ .name = "ld", .sover = 2 },