mirror of
https://github.com/EnderIce2/Fennix.git
synced 2025-07-02 02:49:15 +00:00
feat(userspace/libc): support for linux target
Signed-off-by: EnderIce2 <enderice2@protonmail.com>
This commit is contained in:
48
Userspace/libc/sysdeps/linux/x86_64/dl_start.c
Normal file
48
Userspace/libc/sysdeps/linux/x86_64/dl_start.c
Normal file
@ -0,0 +1,48 @@
|
||||
/*
|
||||
This file is part of Fennix C Library.
|
||||
|
||||
Fennix C Library is free software: you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License as
|
||||
published by the Free Software Foundation, either version 3 of
|
||||
the License, or (at your option) any later version.
|
||||
|
||||
Fennix C Library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with Fennix C Library. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifdef FENNIX_DYNAMIC_LOADER
|
||||
__attribute__((naked, used, no_stack_protector)) void _dl_start()
|
||||
{
|
||||
__asm__(
|
||||
"xorq %rbp, %rbp\n" /* Clear rbp */
|
||||
|
||||
"push %rdi\n"
|
||||
"push %rsi\n"
|
||||
"push %rdx\n"
|
||||
"push %rcx\n"
|
||||
"push %r8\n"
|
||||
"push %r9\n"
|
||||
|
||||
"call __init_print_buffer\n" /* Call __init_print_buffer */
|
||||
"call _dl_preload\n" /* Call _dl_preload */
|
||||
"movl %eax, %edi\n" /* Move return value to edi */
|
||||
"cmp $0, %edi\n" /* Check if return value is 0 */
|
||||
"jne _exit\n" /* If not, jump to _exit */
|
||||
|
||||
"pop %r9\n"
|
||||
"pop %r8\n"
|
||||
"pop %rcx\n"
|
||||
"pop %rdx\n"
|
||||
"pop %rsi\n"
|
||||
"pop %rdi\n"
|
||||
|
||||
"call main\n" /* Call _dl_main */
|
||||
"movl %eax, %edi\n" /* Move return value to edi */
|
||||
"call _exit\n"); /* Call _exit */
|
||||
}
|
||||
#endif
|
22
Userspace/libc/sysdeps/linux/x86_64/stack_chk.c
Normal file
22
Userspace/libc/sysdeps/linux/x86_64/stack_chk.c
Normal file
@ -0,0 +1,22 @@
|
||||
/*
|
||||
This file is part of Fennix C Library.
|
||||
|
||||
Fennix C Library is free software: you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License as
|
||||
published by the Free Software Foundation, either version 3 of
|
||||
the License, or (at your option) any later version.
|
||||
|
||||
Fennix C Library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with Fennix C Library. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
__attribute__((noreturn)) __attribute__((no_stack_protector)) __attribute__((weak)) void __stack_chk_fail(void)
|
||||
{
|
||||
_exit(0xbeef);
|
||||
__builtin_unreachable();
|
||||
}
|
24
Userspace/libc/sysdeps/linux/x86_64/syscall_check.cpp
Normal file
24
Userspace/libc/sysdeps/linux/x86_64/syscall_check.cpp
Normal file
@ -0,0 +1,24 @@
|
||||
/*
|
||||
This file is part of Fennix C Library.
|
||||
|
||||
Fennix C Library is free software: you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License as
|
||||
published by the Free Software Foundation, either version 3 of
|
||||
the License, or (at your option) any later version.
|
||||
|
||||
Fennix C Library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with Fennix C Library. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <bits/syscalls.h>
|
||||
#include <sys/mman.h>
|
||||
#include <unistd.h>
|
||||
#include <signal.h>
|
||||
#include <time.h>
|
||||
|
||||
// static_assert( == );
|
193
Userspace/libc/sysdeps/linux/x86_64/syscalls.c
Normal file
193
Userspace/libc/sysdeps/linux/x86_64/syscalls.c
Normal file
@ -0,0 +1,193 @@
|
||||
/*
|
||||
This file is part of Fennix C Library.
|
||||
|
||||
Fennix C Library is free software: you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License as
|
||||
published by the Free Software Foundation, either version 3 of
|
||||
the License, or (at your option) any later version.
|
||||
|
||||
Fennix C Library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with Fennix C Library. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <bits/syscalls.h>
|
||||
#include <bits/libc.h>
|
||||
#include <string.h>
|
||||
|
||||
void sysdep(Exit)(int Status)
|
||||
{
|
||||
syscall1(sys_exit, Status);
|
||||
}
|
||||
|
||||
int sysdep(Accept)(int Socket, struct sockaddr *restrict Address, socklen_t *restrict AddressLength)
|
||||
{
|
||||
return syscall3(sys_accept, Socket, Address, AddressLength);
|
||||
}
|
||||
|
||||
int sysdep(Bind)(int Socket, const struct sockaddr *Address, socklen_t AddressLength)
|
||||
{
|
||||
return syscall3(sys_bind, Socket, Address, AddressLength);
|
||||
}
|
||||
|
||||
int sysdep(Connect)(int Socket, const struct sockaddr *Address, socklen_t AddressLength)
|
||||
{
|
||||
return syscall3(sys_connect, Socket, Address, AddressLength);
|
||||
}
|
||||
|
||||
int sysdep(Listen)(int Socket, int Backlog)
|
||||
{
|
||||
return syscall2(sys_listen, Socket, Backlog);
|
||||
}
|
||||
|
||||
int sysdep(Socket)(int Domain, int Type, int Protocol)
|
||||
{
|
||||
return syscall3(sys_socket, Domain, Type, Protocol);
|
||||
}
|
||||
|
||||
int sysdep(UnixName)(struct utsname *Name)
|
||||
{
|
||||
struct kutsname kname;
|
||||
int result = syscall1(sys_uname, &kname);
|
||||
if (result == 0)
|
||||
{
|
||||
strcpy(Name->sysname, kname.sysname);
|
||||
strcpy(Name->release, kname.release);
|
||||
strcpy(Name->version, kname.version);
|
||||
strcpy(Name->machine, kname.machine);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int sysdep(WaitProcessID)(pid_t ProcessID, int *Status, int Options)
|
||||
{
|
||||
return syscall3(sys_wait4, ProcessID, (scarg)Status, Options);
|
||||
}
|
||||
|
||||
int sysdep(IOControl)(int Descriptor, unsigned long Operation, void *Argument)
|
||||
{
|
||||
return syscall3(sys_ioctl, Descriptor, Operation, (scarg)Argument);
|
||||
}
|
||||
|
||||
void *sysdep(MemoryMap)(void *Address, size_t Length, int Protection, int Flags, int Descriptor, off_t Offset)
|
||||
{
|
||||
return syscall6(sys_mmap, (scarg)Address, Length, Protection, Flags, Descriptor, Offset);
|
||||
}
|
||||
|
||||
int sysdep(MemoryUnmap)(void *Address, size_t Length)
|
||||
{
|
||||
return syscall2(sys_munmap, (scarg)Address, Length);
|
||||
}
|
||||
|
||||
int sysdep(MemoryProtect)(void *Address, size_t Length, int Protection)
|
||||
{
|
||||
return syscall3(sys_mprotect, (scarg)Address, Length, Protection);
|
||||
}
|
||||
|
||||
int sysdep(Fork)(void)
|
||||
{
|
||||
return syscall0(sys_fork);
|
||||
}
|
||||
|
||||
int sysdep(Read)(int Descriptor, void *Buffer, size_t Size)
|
||||
{
|
||||
return syscall3(sys_read, Descriptor, (scarg)Buffer, Size);
|
||||
}
|
||||
|
||||
int sysdep(Write)(int Descriptor, const void *Buffer, size_t Size)
|
||||
{
|
||||
return syscall3(sys_write, Descriptor, (scarg)Buffer, Size);
|
||||
}
|
||||
|
||||
int sysdep(PRead)(int Descriptor, void *Buffer, size_t Size, off_t Offset)
|
||||
{
|
||||
return syscall4(sys_pread64, Descriptor, (scarg)Buffer, Size, Offset);
|
||||
}
|
||||
|
||||
int sysdep(PWrite)(int Descriptor, const void *Buffer, size_t Size, off_t Offset)
|
||||
{
|
||||
return syscall4(sys_pwrite64, Descriptor, (scarg)Buffer, Size, Offset);
|
||||
}
|
||||
|
||||
int sysdep(Open)(const char *Pathname, int Flags, mode_t Mode)
|
||||
{
|
||||
return syscall3(sys_open, (scarg)Pathname, Flags, Mode);
|
||||
}
|
||||
|
||||
int sysdep(Close)(int Descriptor)
|
||||
{
|
||||
return syscall1(sys_close, Descriptor);
|
||||
}
|
||||
|
||||
int sysdep(Access)(const char *Pathname, int Mode)
|
||||
{
|
||||
return syscall2(sys_access, (scarg)Pathname, Mode);
|
||||
}
|
||||
|
||||
int sysdep(Tell)(int Descriptor)
|
||||
{
|
||||
#undef SEEK_CUR
|
||||
#define SEEK_CUR 1
|
||||
return syscall3(sys_lseek, Descriptor, 0, SEEK_CUR);
|
||||
}
|
||||
|
||||
int sysdep(Seek)(int Descriptor, off_t Offset, int Whence)
|
||||
{
|
||||
return syscall3(sys_lseek, Descriptor, Offset, Whence);
|
||||
}
|
||||
|
||||
pid_t sysdep(GetProcessID)(void)
|
||||
{
|
||||
return syscall0(sys_getpid);
|
||||
}
|
||||
|
||||
pid_t sysdep(GetParentProcessID)(void)
|
||||
{
|
||||
return syscall0(sys_getppid);
|
||||
}
|
||||
|
||||
int sysdep(Execve)(const char *Pathname, char *const *Argv, char *const *Envp)
|
||||
{
|
||||
return syscall3(sys_execve, (scarg)Pathname, (scarg)Argv, (scarg)Envp);
|
||||
}
|
||||
|
||||
int sysdep(Kill)(pid_t ProcessID, int Signal)
|
||||
{
|
||||
return syscall2(sys_kill, ProcessID, Signal);
|
||||
}
|
||||
|
||||
int sysdep(Stat)(const char *Pathname, struct stat *Statbuf)
|
||||
{
|
||||
return syscall2(sys_stat, (scarg)Pathname, (scarg)Statbuf);
|
||||
}
|
||||
|
||||
int sysdep(FStat)(int Descriptor, struct stat *Statbuf)
|
||||
{
|
||||
return syscall2(sys_fstat, Descriptor, (scarg)Statbuf);
|
||||
}
|
||||
|
||||
int sysdep(LStat)(const char *Pathname, struct stat *Statbuf)
|
||||
{
|
||||
return syscall2(sys_lstat, (scarg)Pathname, (scarg)Statbuf);
|
||||
}
|
||||
|
||||
int sysdep(Truncate)(const char *Pathname, off_t Length)
|
||||
{
|
||||
return syscall2(sys_truncate, (scarg)Pathname, Length);
|
||||
}
|
||||
|
||||
int sysdep(MakeDirectory)(const char *Pathname, mode_t Mode)
|
||||
{
|
||||
return syscall2(sys_mkdir, (scarg)Pathname, Mode);
|
||||
}
|
||||
|
||||
int sysdep(ProcessControl)(unsigned long Option, unsigned long Arg1, unsigned long Arg2, unsigned long Arg3, unsigned long Arg4)
|
||||
{
|
||||
return syscall5(sys_prctl, Option, Arg1, Arg2, Arg3, Arg4);
|
||||
}
|
46
Userspace/libc/sysdeps/linux/x86_64/tls.c
Normal file
46
Userspace/libc/sysdeps/linux/x86_64/tls.c
Normal file
@ -0,0 +1,46 @@
|
||||
/*
|
||||
This file is part of Fennix C Library.
|
||||
|
||||
Fennix C Library is free software: you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License as
|
||||
published by the Free Software Foundation, either version 3 of
|
||||
the License, or (at your option) any later version.
|
||||
|
||||
Fennix C Library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with Fennix C Library. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <bits/syscalls.h>
|
||||
#include <bits/libc.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/mman.h>
|
||||
#include <inttypes.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#ifndef FENNIX_DYNAMIC_LOADER
|
||||
export __attribute__((naked, used, no_stack_protector)) void *__tls_get_addr(void *__data)
|
||||
{
|
||||
#warning "__tls_get_addr not implemented"
|
||||
#if defined(__amd64__) || defined(__i386__)
|
||||
__asm__("ud2");
|
||||
#endif
|
||||
}
|
||||
|
||||
int __init_pthread(void)
|
||||
{
|
||||
__pthread *ptr = (__pthread *)syscall6(sys_mmap, 0,
|
||||
0x1000,
|
||||
PROT_READ | PROT_WRITE,
|
||||
MAP_ANONYMOUS | MAP_PRIVATE,
|
||||
-1, 0);
|
||||
syscall2(sys_prctl, 0x1002, ptr);
|
||||
ptr->Self = ptr;
|
||||
ptr->CurrentError = 0;
|
||||
return 0;
|
||||
}
|
||||
#endif
|
Reference in New Issue
Block a user