Merge pull request #22049 from alexrp/musl-gen-stubs

`gen_stubs`: No compiler-rt hacks, more compact output, and more arch support
This commit is contained in:
Andrew Kelley 2024-11-23 23:50:36 -05:00 committed by GitHub
commit ed8955cea6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 393 additions and 749 deletions

View File

@ -3,6 +3,7 @@ __asm__(
".weak _DYNAMIC \n"
".hidden _DYNAMIC \n"
".global " START "\n"
".type " START ",%function \n"
START ":\n"
" xor %ebp,%ebp \n"
" mov %esp,%eax \n"

View File

@ -1,6 +1,7 @@
__asm__(
".text \n"
".global " START " \n"
".type " START ",%function \n"
START ": \n"
" xor %rbp,%rbp \n"
" mov %rsp,%rdi \n"

160
lib/libc/musl/libc.S vendored
View File

@ -7,6 +7,13 @@
#define PTR_SIZE_BYTES 4
#define PTR2_SIZE_BYTES 8
#endif
#ifdef TIME32
#define WEAKTIME64 .globl
#else
#define WEAKTIME64 .weak
#endif
.bss
.weak ___environ
.type ___environ, %object;
@ -168,18 +175,64 @@ _IO_putc:
.weak _IO_putc_unlocked
.type _IO_putc_unlocked, %function;
_IO_putc_unlocked:
#if !defined(ARCH_riscv64) && !defined(ARCH_mips) && !defined(ARCH_mips64) && !defined(ARCH_x86_64) && !defined(ARCH_powerpc) && !defined(ARCH_powerpc64) && !defined(ARCH_aarch64) && !defined(ARCH_riscv32) && !defined(ARCH_loongarch64)
#ifdef ARCH_i386
.globl ___tls_get_addr
.type ___tls_get_addr, %function;
___tls_get_addr:
#endif
#ifdef PTR32
#ifdef TIME32
.globl __adjtime64
.type __adjtime64, %function;
__adjtime64:
.globl __adjtimex_time64
.type __adjtimex_time64, %function;
__adjtimex_time64:
#endif
#ifdef ARCH_arm
.globl __aeabi_atexit
.type __aeabi_atexit, %function;
__aeabi_atexit:
.globl __aeabi_memclr
.type __aeabi_memclr, %function;
__aeabi_memclr:
.globl __aeabi_memclr4
.type __aeabi_memclr4, %function;
__aeabi_memclr4:
.globl __aeabi_memclr8
.type __aeabi_memclr8, %function;
__aeabi_memclr8:
.globl __aeabi_memcpy
.type __aeabi_memcpy, %function;
__aeabi_memcpy:
.globl __aeabi_memcpy4
.type __aeabi_memcpy4, %function;
__aeabi_memcpy4:
.globl __aeabi_memcpy8
.type __aeabi_memcpy8, %function;
__aeabi_memcpy8:
.globl __aeabi_memmove
.type __aeabi_memmove, %function;
__aeabi_memmove:
.globl __aeabi_memmove4
.type __aeabi_memmove4, %function;
__aeabi_memmove4:
.globl __aeabi_memmove8
.type __aeabi_memmove8, %function;
__aeabi_memmove8:
.globl __aeabi_memset
.type __aeabi_memset, %function;
__aeabi_memset:
.globl __aeabi_memset4
.type __aeabi_memset4, %function;
__aeabi_memset4:
.globl __aeabi_memset8
.type __aeabi_memset8, %function;
__aeabi_memset8:
.globl __aeabi_read_tp
.type __aeabi_read_tp, %function;
__aeabi_read_tp:
#endif
#ifdef TIME32
.globl __aio_suspend_time64
.type __aio_suspend_time64, %function;
__aio_suspend_time64:
@ -187,12 +240,12 @@ __aio_suspend_time64:
.globl __assert_fail
.type __assert_fail, %function;
__assert_fail:
#if !defined(ARCH_riscv64) && !defined(ARCH_x86) && !defined(ARCH_x86_64) && !defined(ARCH_powerpc) && !defined(ARCH_powerpc64) && !defined(ARCH_aarch64) && !defined(ARCH_riscv32) && !defined(ARCH_loongarch64)
#ifdef FAMILY_mips
.globl __cachectl
.type __cachectl, %function;
__cachectl:
#endif
#ifdef PTR32
#ifdef TIME32
.globl __clock_adjtime64
.type __clock_adjtime64, %function;
__clock_adjtime64:
@ -236,7 +289,7 @@ __cxa_atexit:
.globl __cxa_finalize
.type __cxa_finalize, %function;
__cxa_finalize:
#ifdef PTR32
#ifdef TIME32
.globl __difftime64
.type __difftime64, %function;
__difftime64:
@ -247,7 +300,12 @@ __dls2b:
.globl __dls3
.type __dls3, %function;
__dls3:
#ifdef PTR32
#ifdef FAMILY_mips
.globl __dlstart
.type __dlstart, %function;
__dlstart:
#endif
#ifdef TIME32
.globl __dlsym_time64
.type __dlsym_time64, %function;
__dlsym_time64:
@ -312,7 +370,7 @@ __fseterr:
.globl __fsetlocking
.type __fsetlocking, %function;
__fsetlocking:
#ifdef PTR32
#ifdef TIME32
.weak __fstat_time64
.type __fstat_time64, %function;
__fstat_time64:
@ -347,7 +405,7 @@ __fxstatat:
.weak __getdelim
.type __getdelim, %function;
__getdelim:
#ifdef PTR32
#ifdef TIME32
.globl __getitimer_time64
.type __getitimer_time64, %function;
__getitimer_time64:
@ -364,6 +422,11 @@ __gmtime64:
.type __gmtime64_r, %function;
__gmtime64_r:
#endif
#ifdef ARCH_arm
.globl __gnu_Unwind_Find_exidx
.type __gnu_Unwind_Find_exidx, %function;
__gnu_Unwind_Find_exidx:
#endif
.globl __h_errno_location
.type __h_errno_location, %function;
__h_errno_location:
@ -490,7 +553,7 @@ __libc_current_sigrtmin:
.globl __libc_start_main
.type __libc_start_main, %function;
__libc_start_main:
#ifdef PTR32
#ifdef TIME32
.globl __localtime64
.type __localtime64, %function;
__localtime64:
@ -498,12 +561,12 @@ __localtime64:
.type __localtime64_r, %function;
__localtime64_r:
#endif
#if !defined(ARCH_mips) && !defined(ARCH_mips64) && !defined(ARCH_x86) && !defined(ARCH_x86_64) && !defined(ARCH_powerpc) && !defined(ARCH_powerpc64) && !defined(ARCH_aarch64) && !defined(ARCH_loongarch64)
#ifdef FAMILY_riscv
.globl __longjmp
.type __longjmp, %function;
__longjmp:
#endif
#ifdef PTR32
#ifdef TIME32
.globl __lstat_time64
.type __lstat_time64, %function;
__lstat_time64:
@ -514,7 +577,7 @@ __lutimes_time64:
.globl __lxstat
.type __lxstat, %function;
__lxstat:
#ifdef PTR32
#ifdef TIME32
.globl __mktime64
.type __mktime64, %function;
__mktime64:
@ -547,7 +610,7 @@ __overflow:
.weak __posix_getopt
.type __posix_getopt, %function;
__posix_getopt:
#ifdef PTR32
#ifdef TIME32
.globl __ppoll_time64
.type __ppoll_time64, %function;
__ppoll_time64:
@ -576,17 +639,7 @@ __recvmmsg_time64:
.globl __res_state
.type __res_state, %function;
__res_state:
#if !defined(ARCH_mips) && !defined(ARCH_mips64) && !defined(ARCH_x86) && !defined(ARCH_x86_64) && !defined(ARCH_powerpc) && !defined(ARCH_powerpc64) && !defined(ARCH_aarch64) && !defined(ARCH_riscv32) && !defined(ARCH_loongarch64)
.globl __restore
.type __restore, %function;
__restore:
#endif
#if !defined(ARCH_mips) && !defined(ARCH_mips64) && !defined(ARCH_x86) && !defined(ARCH_x86_64) && !defined(ARCH_powerpc) && !defined(ARCH_powerpc64) && !defined(ARCH_aarch64) && !defined(ARCH_riscv32) && !defined(ARCH_loongarch64)
.globl __restore_rt
.type __restore_rt, %function;
__restore_rt:
#endif
#if !defined(ARCH_mips) && !defined(ARCH_mips64) && !defined(ARCH_x86) && !defined(ARCH_x86_64) && !defined(ARCH_powerpc) && !defined(ARCH_powerpc64) && !defined(ARCH_aarch64) && !defined(ARCH_loongarch64)
#ifdef FAMILY_riscv
.globl __riscv_flush_icache
.type __riscv_flush_icache, %function;
__riscv_flush_icache:
@ -594,7 +647,7 @@ __riscv_flush_icache:
.globl __sched_cpucount
.type __sched_cpucount, %function;
__sched_cpucount:
#ifdef PTR32
#ifdef TIME32
.globl __sched_rr_get_interval_time64
.type __sched_rr_get_interval_time64, %function;
__sched_rr_get_interval_time64:
@ -614,7 +667,7 @@ __setitimer_time64:
.globl __setjmp
.type __setjmp, %function;
__setjmp:
#ifdef PTR32
#ifdef TIME32
.globl __settimeofday_time64
.type __settimeofday_time64, %function;
__settimeofday_time64:
@ -631,7 +684,7 @@ __signbitl:
.globl __sigsetjmp
.type __sigsetjmp, %function;
__sigsetjmp:
#ifdef PTR32
#ifdef TIME32
.globl __sigtimedwait_time64
.type __sigtimedwait_time64, %function;
__sigtimedwait_time64:
@ -639,7 +692,7 @@ __sigtimedwait_time64:
.globl __stack_chk_fail
.type __stack_chk_fail, %function;
__stack_chk_fail:
#ifdef PTR32
#ifdef TIME32
.globl __stat_time64
.type __stat_time64, %function;
__stat_time64:
@ -692,7 +745,7 @@ __strxfrm_l:
.weak __sysv_signal
.type __sysv_signal, %function;
__sysv_signal:
#ifdef PTR32
#ifdef TIME32
.globl __thrd_sleep_time64
.type __thrd_sleep_time64, %function;
__thrd_sleep_time64:
@ -718,9 +771,16 @@ __timerfd_settime64:
.type __timespec_get_time64, %function;
__timespec_get_time64:
#endif
#if !defined(ARCH_s390x)
.globl __tls_get_addr
.type __tls_get_addr, %function;
__tls_get_addr:
#endif
#ifdef ARCH_s390x
.globl __tls_get_offset
.type __tls_get_offset, %function;
__tls_get_offset:
#endif
.globl __tolower_l
.type __tolower_l, %function;
__tolower_l:
@ -743,7 +803,7 @@ __uflow:
.globl __uselocale
.type __uselocale, %function;
__uselocale:
#ifdef PTR32
#ifdef TIME32
.globl __utime64
.type __utime64, %function;
__utime64:
@ -796,7 +856,7 @@ _dl_debug_state:
.globl _dlstart
.type _dlstart, %function;
_dlstart:
#if !defined(ARCH_riscv64) && !defined(ARCH_mips) && !defined(ARCH_x86) && !defined(ARCH_x86_64) && !defined(ARCH_powerpc) && !defined(ARCH_powerpc64) && !defined(ARCH_aarch64) && !defined(ARCH_riscv32) && !defined(ARCH_loongarch64)
#ifdef FAMILY_mips
.globl _dlstart_data
.type _dlstart_data, %function;
_dlstart_data:
@ -807,7 +867,7 @@ _exit:
.weak _fini
.type _fini, %function;
_fini:
#if !defined(ARCH_riscv64) && !defined(ARCH_x86) && !defined(ARCH_x86_64) && !defined(ARCH_powerpc) && !defined(ARCH_powerpc64) && !defined(ARCH_aarch64) && !defined(ARCH_riscv32) && !defined(ARCH_loongarch64)
#ifdef FAMILY_mips
.globl _flush_cache
.type _flush_cache, %function;
_flush_cache:
@ -908,7 +968,7 @@ aligned_alloc:
.globl alphasort
.type alphasort, %function;
alphasort:
#if !defined(ARCH_riscv64) && !defined(ARCH_mips) && !defined(ARCH_mips64) && !defined(ARCH_powerpc) && !defined(ARCH_powerpc64) && !defined(ARCH_aarch64) && !defined(ARCH_riscv32) && !defined(ARCH_loongarch64)
#ifdef FAMILY_x86
.globl arch_prctl
.type arch_prctl, %function;
arch_prctl:
@ -1033,12 +1093,10 @@ cabsf:
.globl cabsl
.type cabsl, %function;
cabsl:
#if !defined(ARCH_riscv64) && !defined(ARCH_x86) && !defined(ARCH_x86_64) && !defined(ARCH_powerpc) && !defined(ARCH_powerpc64) && !defined(ARCH_aarch64) && !defined(ARCH_riscv32) && !defined(ARCH_loongarch64)
#ifdef FAMILY_mips
.weak cachectl
.type cachectl, %function;
cachectl:
#endif
#if !defined(ARCH_riscv64) && !defined(ARCH_x86) && !defined(ARCH_x86_64) && !defined(ARCH_powerpc) && !defined(ARCH_powerpc64) && !defined(ARCH_aarch64) && !defined(ARCH_riscv32) && !defined(ARCH_loongarch64)
.weak cacheflush
.type cacheflush, %function;
cacheflush:
@ -1232,10 +1290,10 @@ clock_getcpuclockid:
.globl clock_getres
.type clock_getres, %function;
clock_getres:
WEAK64 clock_gettime
WEAKTIME64 clock_gettime
.type clock_gettime, %function;
clock_gettime:
WEAK64 clock_nanosleep
WEAKTIME64 clock_nanosleep
.type clock_nanosleep, %function;
clock_nanosleep:
.globl clock_settime
@ -2018,10 +2076,10 @@ fsetpos:
.globl fsetxattr
.type fsetxattr, %function;
fsetxattr:
WEAK64 fstat
WEAKTIME64 fstat
.type fstat, %function;
fstat:
WEAK64 fstatat
WEAKTIME64 fstatat
.type fstatat, %function;
fstatat:
.weak fstatfs
@ -2063,7 +2121,7 @@ futimens:
.globl futimes
.type futimes, %function;
futimes:
WEAK64 futimesat
WEAKTIME64 futimesat
.type futimesat, %function;
futimesat:
.globl fwide
@ -2408,7 +2466,7 @@ globfree:
.globl gmtime
.type gmtime, %function;
gmtime:
WEAK64 gmtime_r
WEAKTIME64 gmtime_r
.type gmtime_r, %function;
gmtime_r:
.globl grantpt
@ -2549,12 +2607,12 @@ insque:
.globl ioctl
.type ioctl, %function;
ioctl:
#if !defined(ARCH_riscv64) && !defined(ARCH_mips64) && !defined(ARCH_aarch64) && !defined(ARCH_riscv32) && !defined(ARCH_loongarch64)
#if !defined(ARCH_aarch64) && !defined(ARCH_arm) && !defined(ARCH_loongarch64) && !defined(ARCH_mips64) && !defined(ARCH_mipsn32) && !defined(ARCH_riscv32) && !defined(ARCH_riscv64) && !defined(ARCH_s390x)
.globl ioperm
.type ioperm, %function;
ioperm:
#endif
#if !defined(ARCH_riscv64) && !defined(ARCH_mips64) && !defined(ARCH_aarch64) && !defined(ARCH_riscv32) && !defined(ARCH_loongarch64)
#if !defined(ARCH_aarch64) && !defined(ARCH_arm) && !defined(ARCH_loongarch64) && !defined(ARCH_mips64) && !defined(ARCH_mipsn32) && !defined(ARCH_riscv32) && !defined(ARCH_riscv64) && !defined(ARCH_s390x)
.globl iopl
.type iopl, %function;
iopl:
@ -2853,7 +2911,7 @@ localeconv:
.globl localtime
.type localtime, %function;
localtime:
WEAK64 localtime_r
WEAKTIME64 localtime_r
.type localtime_r, %function;
localtime_r:
.globl lockf
@ -3552,7 +3610,7 @@ pthread_cond_init:
.globl pthread_cond_signal
.type pthread_cond_signal, %function;
pthread_cond_signal:
WEAK64 pthread_cond_timedwait
WEAKTIME64 pthread_cond_timedwait
.type pthread_cond_timedwait, %function;
pthread_cond_timedwait:
.globl pthread_cond_wait
@ -3642,7 +3700,7 @@ pthread_mutex_lock:
.globl pthread_mutex_setprioceiling
.type pthread_mutex_setprioceiling, %function;
pthread_mutex_setprioceiling:
WEAK64 pthread_mutex_timedlock
WEAKTIME64 pthread_mutex_timedlock
.type pthread_mutex_timedlock, %function;
pthread_mutex_timedlock:
.weak pthread_mutex_trylock
@ -3693,10 +3751,10 @@ pthread_rwlock_init:
.weak pthread_rwlock_rdlock
.type pthread_rwlock_rdlock, %function;
pthread_rwlock_rdlock:
WEAK64 pthread_rwlock_timedrdlock
WEAKTIME64 pthread_rwlock_timedrdlock
.type pthread_rwlock_timedrdlock, %function;
pthread_rwlock_timedrdlock:
WEAK64 pthread_rwlock_timedwrlock
WEAKTIME64 pthread_rwlock_timedwrlock
.type pthread_rwlock_timedwrlock, %function;
pthread_rwlock_timedwrlock:
.weak pthread_rwlock_tryrdlock
@ -3774,7 +3832,7 @@ pthread_spin_unlock:
.weak pthread_testcancel
.type pthread_testcancel, %function;
pthread_testcancel:
WEAK64 pthread_timedjoin_np
WEAKTIME64 pthread_timedjoin_np
.type pthread_timedjoin_np, %function;
pthread_timedjoin_np:
.weak pthread_tryjoin_np
@ -3999,7 +4057,7 @@ rintf:
.globl rintl
.type rintl, %function;
rintl:
#if !defined(ARCH_mips) && !defined(ARCH_mips64) && !defined(ARCH_x86) && !defined(ARCH_x86_64) && !defined(ARCH_powerpc) && !defined(ARCH_powerpc64) && !defined(ARCH_aarch64) && !defined(ARCH_loongarch64)
#ifdef FAMILY_riscv
.weak riscv_flush_icache
.type riscv_flush_icache, %function;
riscv_flush_icache:

View File

@ -1,7 +1,9 @@
.global __restore
.hidden __restore
.type __restore, %function
__restore:
.global __restore_rt
.hidden __restore_rt
.type __restore_rt, %function
__restore_rt:
li a7, 139 # SYS_rt_sigreturn

View File

@ -1,7 +1,9 @@
.global __restore
.hidden __restore
.type __restore, %function
__restore:
.global __restore_rt
.hidden __restore_rt
.type __restore_rt, %function
__restore_rt:
li a7, 139 # SYS_rt_sigreturn

View File

@ -5,6 +5,7 @@ __tls_get_offset:
aghi %r15, -160
la %r2, 0(%r2, %r12)
.hidden __tls_get_addr
brasl %r14, __tls_get_addr
ear %r1, %a0

View File

@ -5884,7 +5884,9 @@ pub const FileExt = enum {
};
pub fn hasObjectExt(filename: []const u8) bool {
return mem.endsWith(u8, filename, ".o") or mem.endsWith(u8, filename, ".obj");
return mem.endsWith(u8, filename, ".o") or
mem.endsWith(u8, filename, ".lo") or
mem.endsWith(u8, filename, ".obj");
}
pub fn hasStaticLibraryExt(filename: []const u8) bool {

View File

@ -138,17 +138,6 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro
try addSrcFile(arena, &source_table, src_file);
}
const time32_compat_arch_list = [_][]const u8{
"arm",
"i386",
"m68k",
"microblaze",
"mips",
"mipsn32",
"or1k",
"powerpc",
"sh",
};
for (time32_compat_arch_list) |time32_compat_arch| {
if (mem.eql(u8, arch_name, time32_compat_arch)) {
for (compat_time32_files) |compat_time32_file| {
@ -239,13 +228,29 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro
});
const target = comp.root_mod.resolved_target.result;
const arch_define = try std.fmt.allocPrint(arena, "-DARCH_{s}", .{
@tagName(target.cpu.arch),
});
const arch_name = std.zig.target.muslArchName(target.cpu.arch, target.abi);
const time32 = for (time32_compat_arch_list) |time32_compat_arch| {
if (mem.eql(u8, arch_name, time32_compat_arch)) break true;
} else false;
const arch_define = try std.fmt.allocPrint(arena, "-DARCH_{s}", .{arch_name});
const family_define = switch (target.cpu.arch) {
.arm, .armeb, .thumb, .thumbeb => "-DFAMILY_arm",
.aarch64, .aarch64_be => "-DFAMILY_aarch64",
.loongarch64 => "-DFAMILY_loongarch",
.m68k => "-DFAMILY_m68k",
.mips, .mipsel, .mips64, .mips64el => "-DFAMILY_mips",
.powerpc, .powerpc64, .powerpc64le => "-DFAMILY_powerpc",
.riscv32, .riscv64 => "-DFAMILY_riscv",
.s390x => "-DFAMILY_s390x",
.x86, .x86_64 => "-DFAMILY_x86",
else => unreachable,
};
const cc_argv: []const []const u8 = if (target.ptrBitWidth() == 64)
&.{ "-DPTR64", arch_define }
&.{ "-DPTR64", arch_define, family_define }
else if (time32)
&.{ "-DTIME32", arch_define, family_define }
else
&.{arch_define};
&.{ arch_define, family_define };
const root_mod = try Module.create(arena, .{
.global_cache_directory = comp.global_cache_directory,
@ -347,6 +352,18 @@ pub fn needsCrt0(output_mode: std.builtin.OutputMode, link_mode: std.builtin.Lin
};
}
const time32_compat_arch_list = [_][]const u8{
"arm",
"i386",
"m68k",
"microblaze",
"mips",
"mipsn32",
"or1k",
"powerpc",
"sh",
};
fn isArchName(name: []const u8) bool {
const musl_arch_names = [_][]const u8{
"aarch64",

File diff suppressed because it is too large Load Diff