Merge pull request #20909 from alexrp/glibc-riscv

Support building glibc for riscv32/riscv64
This commit is contained in:
Andrew Kelley 2024-08-07 01:14:54 -07:00 committed by GitHub
commit cd5f673cae
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
36 changed files with 59 additions and 48 deletions

View file

@ -1,10 +1,10 @@
/* This file is automatically generated. */ /* This file is automatically generated. */
#ifndef __GNU_LIB_NAMES_H #ifndef __GNU_LIB_NAMES_H
# error "Never use <gnu/lib-names-lp64.h> directly; include <gnu/lib-names.h> instead." # error "Never use <gnu/lib-names-ilp32d.h> directly; include <gnu/lib-names.h> instead."
#endif #endif
#define LD_LINUX_RISCV64_LP64_SO "ld-linux-riscv64-lp64.so.1" #define LD_LINUX_RISCV32_ILP32D_SO "ld-linux-riscv32-ilp32d.so.1"
#define LD_SO "ld-linux-riscv64-lp64.so.1" #define LD_SO "ld-linux-riscv32-ilp32d.so.1"
#define LIBANL_SO "libanl.so.1" #define LIBANL_SO "libanl.so.1"
#define LIBBROKENLOCALE_SO "libBrokenLocale.so.1" #define LIBBROKENLOCALE_SO "libBrokenLocale.so.1"
#define LIBC_MALLOC_DEBUG_SO "libc_malloc_debug.so.0" #define LIBC_MALLOC_DEBUG_SO "libc_malloc_debug.so.0"

View file

@ -14,23 +14,9 @@
#define __stub___compat_uselib #define __stub___compat_uselib
#define __stub_chflags #define __stub_chflags
#define __stub_fchflags #define __stub_fchflags
#define __stub_feclearexcept
#define __stub_fedisableexcept #define __stub_fedisableexcept
#define __stub_feenableexcept #define __stub_feenableexcept
#define __stub_fegetenv
#define __stub_fegetexcept #define __stub_fegetexcept
#define __stub_fegetexceptflag
#define __stub_fegetmode
#define __stub_fegetround
#define __stub_feholdexcept
#define __stub_feraiseexcept
#define __stub_fesetenv
#define __stub_fesetexcept
#define __stub_fesetexceptflag
#define __stub_fesetmode
#define __stub_fesetround
#define __stub_fetestexcept
#define __stub_feupdateenv
#define __stub_gtty #define __stub_gtty
#define __stub_revoke #define __stub_revoke
#define __stub_setlogin #define __stub_setlogin

View file

@ -1,10 +1,10 @@
/* This file is automatically generated. */ /* This file is automatically generated. */
#ifndef __GNU_LIB_NAMES_H #ifndef __GNU_LIB_NAMES_H
# error "Never use <gnu/lib-names-ilp32.h> directly; include <gnu/lib-names.h> instead." # error "Never use <gnu/lib-names-lp64d.h> directly; include <gnu/lib-names.h> instead."
#endif #endif
#define LD_LINUX_RISCV32_ILP32_SO "ld-linux-riscv32-ilp32.so.1" #define LD_LINUX_RISCV64_LP64D_SO "ld-linux-riscv64-lp64d.so.1"
#define LD_SO "ld-linux-riscv32-ilp32.so.1" #define LD_SO "ld-linux-riscv64-lp64d.so.1"
#define LIBANL_SO "libanl.so.1" #define LIBANL_SO "libanl.so.1"
#define LIBBROKENLOCALE_SO "libBrokenLocale.so.1" #define LIBBROKENLOCALE_SO "libBrokenLocale.so.1"
#define LIBC_MALLOC_DEBUG_SO "libc_malloc_debug.so.0" #define LIBC_MALLOC_DEBUG_SO "libc_malloc_debug.so.0"

View file

@ -14,23 +14,9 @@
#define __stub___compat_uselib #define __stub___compat_uselib
#define __stub_chflags #define __stub_chflags
#define __stub_fchflags #define __stub_fchflags
#define __stub_feclearexcept
#define __stub_fedisableexcept #define __stub_fedisableexcept
#define __stub_feenableexcept #define __stub_feenableexcept
#define __stub_fegetenv
#define __stub_fegetexcept #define __stub_fegetexcept
#define __stub_fegetexceptflag
#define __stub_fegetmode
#define __stub_fegetround
#define __stub_feholdexcept
#define __stub_feraiseexcept
#define __stub_fesetenv
#define __stub_fesetexcept
#define __stub_fesetexceptflag
#define __stub_fesetmode
#define __stub_fesetround
#define __stub_fetestexcept
#define __stub_feupdateenv
#define __stub_gtty #define __stub_gtty
#define __stub_revoke #define __stub_revoke
#define __stub_setlogin #define __stub_setlogin

View file

@ -483,7 +483,21 @@ pub const Os = struct {
.min = .{ .major = 4, .minor = 19, .patch = 0 }, .min = .{ .major = 4, .minor = 19, .patch = 0 },
.max = .{ .major = 6, .minor = 5, .patch = 7 }, .max = .{ .major = 6, .minor = 5, .patch = 7 },
}, },
.glibc = .{ .major = 2, .minor = 28, .patch = 0 }, .glibc = blk: {
const default_min = .{ .major = 2, .minor = 28, .patch = 0 };
for (std.zig.target.available_libcs) |libc| {
// We don't know the ABI here. We can get away with not checking it
// for now, but that may not always remain true.
if (libc.os != tag or libc.arch != arch) continue;
if (libc.glibc_min) |min| {
if (min.order(default_min) == .gt) break :blk min;
}
}
break :blk default_min;
},
}, },
}, },
@ -1750,8 +1764,8 @@ pub const DynamicLinker = struct {
else => "/lib64/ld-linux-x86-64.so.2", else => "/lib64/ld-linux-x86-64.so.2",
}), }),
.riscv32 => init("/lib/ld-linux-riscv32-ilp32.so.1"), .riscv32 => init("/lib/ld-linux-riscv32-ilp32d.so.1"),
.riscv64 => init("/lib/ld-linux-riscv64-lp64.so.1"), .riscv64 => init("/lib/ld-linux-riscv64-lp64d.so.1"),
// Architectures in this list have been verified as not having a standard // Architectures in this list have been verified as not having a standard
// dynamic linker path. // dynamic linker path.

View file

@ -57,7 +57,7 @@ pub const available_libcs = [_]ArchOsAbi{
.{ .arch = .powerpc, .os = .linux, .abi = .gnueabi }, .{ .arch = .powerpc, .os = .linux, .abi = .gnueabi },
.{ .arch = .powerpc, .os = .linux, .abi = .gnueabihf }, .{ .arch = .powerpc, .os = .linux, .abi = .gnueabihf },
.{ .arch = .powerpc, .os = .linux, .abi = .musl }, .{ .arch = .powerpc, .os = .linux, .abi = .musl },
.{ .arch = .riscv32, .os = .linux, .abi = .gnuilp32, .glibc_min = .{ .major = 2, .minor = 33, .patch = 0 } }, .{ .arch = .riscv32, .os = .linux, .abi = .gnu, .glibc_min = .{ .major = 2, .minor = 33, .patch = 0 } },
.{ .arch = .riscv32, .os = .linux, .abi = .musl }, .{ .arch = .riscv32, .os = .linux, .abi = .musl },
.{ .arch = .riscv64, .os = .linux, .abi = .gnu, .glibc_min = .{ .major = 2, .minor = 27, .patch = 0 } }, .{ .arch = .riscv64, .os = .linux, .abi = .gnu, .glibc_min = .{ .major = 2, .minor = 27, .patch = 0 } },
.{ .arch = .riscv64, .os = .linux, .abi = .musl }, .{ .arch = .riscv64, .os = .linux, .abi = .musl },

View file

@ -153,6 +153,21 @@ pub fn loadMetaData(gpa: Allocator, contents: []const u8) LoadMetaDataError!*ABI
return abi; return abi;
} }
fn useElfInitFini(target: std.Target) bool {
// Legacy architectures use _init/_fini.
return switch (target.cpu.arch) {
.arm, .armeb, .thumb, .thumbeb => true,
.aarch64, .aarch64_be => true,
.m68k => true,
.mips, .mipsel, .mips64, .mips64el => true,
.powerpc, .powerpcle, .powerpc64, .powerpc64le => true,
.s390x => true,
.sparc, .sparc64 => true,
.x86, .x86_64 => true,
else => false,
};
}
pub const CRTFile = enum { pub const CRTFile = enum {
crti_o, crti_o,
crtn_o, crtn_o,
@ -348,8 +363,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: std.Progre
"-std=gnu11", "-std=gnu11",
"-fgnu89-inline", "-fgnu89-inline",
"-fmerge-all-constants", "-fmerge-all-constants",
// glibc sets this flag but clang does not support it. "-frounding-math",
// "-frounding-math",
"-fno-stack-protector", "-fno-stack-protector",
"-fno-common", "-fno-common",
"-fmath-errno", "-fmath-errno",
@ -358,6 +372,10 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: std.Progre
}); });
try add_include_dirs(comp, arena, &args); try add_include_dirs(comp, arena, &args);
if (!useElfInitFini(target)) {
try args.append("-DNO_INITFINI");
}
if (target.cpu.arch == .x86) { if (target.cpu.arch == .x86) {
// This prevents i386/sysdep.h from trying to do some // This prevents i386/sysdep.h from trying to do some
// silly and unnecessary inline asm hack that uses weird // silly and unnecessary inline asm hack that uses weird

View file

@ -3412,6 +3412,14 @@ fn buildOutputType(
std.log.info("zig can provide libc for related target {s}-{s}.{d}-{s}", .{ std.log.info("zig can provide libc for related target {s}-{s}.{d}-{s}", .{
@tagName(t.arch), @tagName(t.os), os_ver.major, @tagName(t.abi), @tagName(t.arch), @tagName(t.os), os_ver.major, @tagName(t.abi),
}); });
} else if (t.glibc_min) |glibc_min| {
std.log.info("zig can provide libc for related target {s}-{s}-{s}.{d}.{d}", .{
@tagName(t.arch),
@tagName(t.os),
@tagName(t.abi),
glibc_min.major,
glibc_min.minor,
});
} else { } else {
std.log.info("zig can provide libc for related target {s}-{s}-{s}", .{ std.log.info("zig can provide libc for related target {s}-{s}-{s}", .{
@tagName(t.arch), @tagName(t.os), @tagName(t.abi), @tagName(t.arch), @tagName(t.os), @tagName(t.abi),

View file

@ -369,7 +369,6 @@ pub fn addrSpaceCastIsValid(
pub fn llvmMachineAbi(target: std.Target) ?[:0]const u8 { pub fn llvmMachineAbi(target: std.Target) ?[:0]const u8 {
const have_float = switch (target.abi) { const have_float = switch (target.abi) {
.gnuilp32 => return "ilp32",
.gnueabihf, .musleabihf, .eabihf => true, .gnueabihf, .musleabihf, .eabihf => true,
else => false, else => false,
}; };

View file

@ -165,12 +165,12 @@ const glibc_targets = [_]LibCTarget{
.abi = MultiAbi{ .specific = Abi.gnueabi }, .abi = MultiAbi{ .specific = Abi.gnueabi },
}, },
LibCTarget{ LibCTarget{
.name = "riscv32-linux-gnu-rv32imac-ilp32", .name = "riscv32-linux-gnu-rv32imafdc-ilp32d",
.arch = MultiArch{ .specific = Arch.riscv32 }, .arch = MultiArch{ .specific = Arch.riscv32 },
.abi = MultiAbi{ .specific = Abi.gnuilp32 }, .abi = MultiAbi{ .specific = Abi.gnu },
}, },
LibCTarget{ LibCTarget{
.name = "riscv64-linux-gnu-rv64imac-lp64", .name = "riscv64-linux-gnu-rv64imafdc-lp64d",
.arch = MultiArch{ .specific = Arch.riscv64 }, .arch = MultiArch{ .specific = Arch.riscv64 },
.abi = MultiAbi{ .specific = Abi.gnu }, .abi = MultiAbi{ .specific = Abi.gnu },
}, },