Skip to content

Commit cd5f673

Browse files
authored
Merge pull request #20909 from alexrp/glibc-riscv
Support building glibc for riscv32/riscv64
2 parents 8184912 + f43c056 commit cd5f673

36 files changed

+59
-48
lines changed

lib/libc/include/riscv64-linux-gnu/gnu/lib-names-lp64.h renamed to lib/libc/include/riscv32-linux-gnu/gnu/lib-names-ilp32d.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/* This file is automatically generated. */
22
#ifndef __GNU_LIB_NAMES_H
3-
# error "Never use <gnu/lib-names-lp64.h> directly; include <gnu/lib-names.h> instead."
3+
# error "Never use <gnu/lib-names-ilp32d.h> directly; include <gnu/lib-names.h> instead."
44
#endif
55

6-
#define LD_LINUX_RISCV64_LP64_SO "ld-linux-riscv64-lp64.so.1"
7-
#define LD_SO "ld-linux-riscv64-lp64.so.1"
6+
#define LD_LINUX_RISCV32_ILP32D_SO "ld-linux-riscv32-ilp32d.so.1"
7+
#define LD_SO "ld-linux-riscv32-ilp32d.so.1"
88
#define LIBANL_SO "libanl.so.1"
99
#define LIBBROKENLOCALE_SO "libBrokenLocale.so.1"
1010
#define LIBC_MALLOC_DEBUG_SO "libc_malloc_debug.so.0"
@@ -24,4 +24,4 @@
2424
#define LIBRESOLV_SO "libresolv.so.2"
2525
#define LIBRT_SO "librt.so.1"
2626
#define LIBTHREAD_DB_SO "libthread_db.so.1"
27-
#define LIBUTIL_SO "libutil.so.1"
27+
#define LIBUTIL_SO "libutil.so.1"

lib/libc/include/riscv32-linux-gnuilp32/gnu/stubs-ilp32.h renamed to lib/libc/include/riscv32-linux-gnu/gnu/stubs-ilp32d.h

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,11 @@
1414
#define __stub___compat_uselib
1515
#define __stub_chflags
1616
#define __stub_fchflags
17-
#define __stub_feclearexcept
1817
#define __stub_fedisableexcept
1918
#define __stub_feenableexcept
20-
#define __stub_fegetenv
2119
#define __stub_fegetexcept
22-
#define __stub_fegetexceptflag
23-
#define __stub_fegetmode
24-
#define __stub_fegetround
25-
#define __stub_feholdexcept
26-
#define __stub_feraiseexcept
27-
#define __stub_fesetenv
28-
#define __stub_fesetexcept
29-
#define __stub_fesetexceptflag
30-
#define __stub_fesetmode
31-
#define __stub_fesetround
32-
#define __stub_fetestexcept
33-
#define __stub_feupdateenv
3420
#define __stub_gtty
3521
#define __stub_revoke
3622
#define __stub_setlogin
3723
#define __stub_sigreturn
38-
#define __stub_stty
24+
#define __stub_stty

lib/libc/include/riscv32-linux-gnuilp32/gnu/lib-names-ilp32.h renamed to lib/libc/include/riscv64-linux-gnu/gnu/lib-names-lp64d.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/* This file is automatically generated. */
22
#ifndef __GNU_LIB_NAMES_H
3-
# error "Never use <gnu/lib-names-ilp32.h> directly; include <gnu/lib-names.h> instead."
3+
# error "Never use <gnu/lib-names-lp64d.h> directly; include <gnu/lib-names.h> instead."
44
#endif
55

6-
#define LD_LINUX_RISCV32_ILP32_SO "ld-linux-riscv32-ilp32.so.1"
7-
#define LD_SO "ld-linux-riscv32-ilp32.so.1"
6+
#define LD_LINUX_RISCV64_LP64D_SO "ld-linux-riscv64-lp64d.so.1"
7+
#define LD_SO "ld-linux-riscv64-lp64d.so.1"
88
#define LIBANL_SO "libanl.so.1"
99
#define LIBBROKENLOCALE_SO "libBrokenLocale.so.1"
1010
#define LIBC_MALLOC_DEBUG_SO "libc_malloc_debug.so.0"
@@ -24,4 +24,4 @@
2424
#define LIBRESOLV_SO "libresolv.so.2"
2525
#define LIBRT_SO "librt.so.1"
2626
#define LIBTHREAD_DB_SO "libthread_db.so.1"
27-
#define LIBUTIL_SO "libutil.so.1"
27+
#define LIBUTIL_SO "libutil.so.1"

lib/libc/include/riscv64-linux-gnu/gnu/stubs-lp64.h renamed to lib/libc/include/riscv64-linux-gnu/gnu/stubs-lp64d.h

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,11 @@
1414
#define __stub___compat_uselib
1515
#define __stub_chflags
1616
#define __stub_fchflags
17-
#define __stub_feclearexcept
1817
#define __stub_fedisableexcept
1918
#define __stub_feenableexcept
20-
#define __stub_fegetenv
2119
#define __stub_fegetexcept
22-
#define __stub_fegetexceptflag
23-
#define __stub_fegetmode
24-
#define __stub_fegetround
25-
#define __stub_feholdexcept
26-
#define __stub_feraiseexcept
27-
#define __stub_fesetenv
28-
#define __stub_fesetexcept
29-
#define __stub_fesetexceptflag
30-
#define __stub_fesetmode
31-
#define __stub_fesetround
32-
#define __stub_fetestexcept
33-
#define __stub_feupdateenv
3420
#define __stub_gtty
3521
#define __stub_revoke
3622
#define __stub_setlogin
3723
#define __stub_sigreturn
38-
#define __stub_stty
24+
#define __stub_stty

lib/std/Target.zig

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,21 @@ pub const Os = struct {
483483
.min = .{ .major = 4, .minor = 19, .patch = 0 },
484484
.max = .{ .major = 6, .minor = 5, .patch = 7 },
485485
},
486-
.glibc = .{ .major = 2, .minor = 28, .patch = 0 },
486+
.glibc = blk: {
487+
const default_min = .{ .major = 2, .minor = 28, .patch = 0 };
488+
489+
for (std.zig.target.available_libcs) |libc| {
490+
// We don't know the ABI here. We can get away with not checking it
491+
// for now, but that may not always remain true.
492+
if (libc.os != tag or libc.arch != arch) continue;
493+
494+
if (libc.glibc_min) |min| {
495+
if (min.order(default_min) == .gt) break :blk min;
496+
}
497+
}
498+
499+
break :blk default_min;
500+
},
487501
},
488502
},
489503

@@ -1750,8 +1764,8 @@ pub const DynamicLinker = struct {
17501764
else => "/lib64/ld-linux-x86-64.so.2",
17511765
}),
17521766

1753-
.riscv32 => init("/lib/ld-linux-riscv32-ilp32.so.1"),
1754-
.riscv64 => init("/lib/ld-linux-riscv64-lp64.so.1"),
1767+
.riscv32 => init("/lib/ld-linux-riscv32-ilp32d.so.1"),
1768+
.riscv64 => init("/lib/ld-linux-riscv64-lp64d.so.1"),
17551769

17561770
// Architectures in this list have been verified as not having a standard
17571771
// dynamic linker path.

lib/std/zig/target.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub const available_libcs = [_]ArchOsAbi{
5757
.{ .arch = .powerpc, .os = .linux, .abi = .gnueabi },
5858
.{ .arch = .powerpc, .os = .linux, .abi = .gnueabihf },
5959
.{ .arch = .powerpc, .os = .linux, .abi = .musl },
60-
.{ .arch = .riscv32, .os = .linux, .abi = .gnuilp32, .glibc_min = .{ .major = 2, .minor = 33, .patch = 0 } },
60+
.{ .arch = .riscv32, .os = .linux, .abi = .gnu, .glibc_min = .{ .major = 2, .minor = 33, .patch = 0 } },
6161
.{ .arch = .riscv32, .os = .linux, .abi = .musl },
6262
.{ .arch = .riscv64, .os = .linux, .abi = .gnu, .glibc_min = .{ .major = 2, .minor = 27, .patch = 0 } },
6363
.{ .arch = .riscv64, .os = .linux, .abi = .musl },

src/glibc.zig

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,21 @@ pub fn loadMetaData(gpa: Allocator, contents: []const u8) LoadMetaDataError!*ABI
153153
return abi;
154154
}
155155

156+
fn useElfInitFini(target: std.Target) bool {
157+
// Legacy architectures use _init/_fini.
158+
return switch (target.cpu.arch) {
159+
.arm, .armeb, .thumb, .thumbeb => true,
160+
.aarch64, .aarch64_be => true,
161+
.m68k => true,
162+
.mips, .mipsel, .mips64, .mips64el => true,
163+
.powerpc, .powerpcle, .powerpc64, .powerpc64le => true,
164+
.s390x => true,
165+
.sparc, .sparc64 => true,
166+
.x86, .x86_64 => true,
167+
else => false,
168+
};
169+
}
170+
156171
pub const CRTFile = enum {
157172
crti_o,
158173
crtn_o,
@@ -348,8 +363,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: std.Progre
348363
"-std=gnu11",
349364
"-fgnu89-inline",
350365
"-fmerge-all-constants",
351-
// glibc sets this flag but clang does not support it.
352-
// "-frounding-math",
366+
"-frounding-math",
353367
"-fno-stack-protector",
354368
"-fno-common",
355369
"-fmath-errno",
@@ -358,6 +372,10 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: std.Progre
358372
});
359373
try add_include_dirs(comp, arena, &args);
360374

375+
if (!useElfInitFini(target)) {
376+
try args.append("-DNO_INITFINI");
377+
}
378+
361379
if (target.cpu.arch == .x86) {
362380
// This prevents i386/sysdep.h from trying to do some
363381
// silly and unnecessary inline asm hack that uses weird

src/main.zig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3412,6 +3412,14 @@ fn buildOutputType(
34123412
std.log.info("zig can provide libc for related target {s}-{s}.{d}-{s}", .{
34133413
@tagName(t.arch), @tagName(t.os), os_ver.major, @tagName(t.abi),
34143414
});
3415+
} else if (t.glibc_min) |glibc_min| {
3416+
std.log.info("zig can provide libc for related target {s}-{s}-{s}.{d}.{d}", .{
3417+
@tagName(t.arch),
3418+
@tagName(t.os),
3419+
@tagName(t.abi),
3420+
glibc_min.major,
3421+
glibc_min.minor,
3422+
});
34153423
} else {
34163424
std.log.info("zig can provide libc for related target {s}-{s}-{s}", .{
34173425
@tagName(t.arch), @tagName(t.os), @tagName(t.abi),

src/target.zig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,6 @@ pub fn addrSpaceCastIsValid(
369369

370370
pub fn llvmMachineAbi(target: std.Target) ?[:0]const u8 {
371371
const have_float = switch (target.abi) {
372-
.gnuilp32 => return "ilp32",
373372
.gnueabihf, .musleabihf, .eabihf => true,
374373
else => false,
375374
};

tools/process_headers.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,12 @@ const glibc_targets = [_]LibCTarget{
165165
.abi = MultiAbi{ .specific = Abi.gnueabi },
166166
},
167167
LibCTarget{
168-
.name = "riscv32-linux-gnu-rv32imac-ilp32",
168+
.name = "riscv32-linux-gnu-rv32imafdc-ilp32d",
169169
.arch = MultiArch{ .specific = Arch.riscv32 },
170-
.abi = MultiAbi{ .specific = Abi.gnuilp32 },
170+
.abi = MultiAbi{ .specific = Abi.gnu },
171171
},
172172
LibCTarget{
173-
.name = "riscv64-linux-gnu-rv64imac-lp64",
173+
.name = "riscv64-linux-gnu-rv64imafdc-lp64d",
174174
.arch = MultiArch{ .specific = Arch.riscv64 },
175175
.abi = MultiAbi{ .specific = Abi.gnu },
176176
},

0 commit comments

Comments
 (0)