Compare commits

...

2 Commits

2 changed files with 41 additions and 13 deletions

View File

@ -1,20 +1,20 @@
all: print print_x64 print_aarch64 print_mips32
clean:
rm print
rm print_x64
rm print_aarch64
rm print_mips32
if test -f print; then rm print; fi
if test -f print_x64; then rm print_x64; fi
if test -f print_aarch64; then rm print_aarch64; fi
if test -f print_mips32; then rm print_mips32; fi
.PHONY: all clean
print:
gcc -static -nostdlib -o print print.c
gcc -static -nostdlib -fno-stack-protector -o print print.c
print_x64: print.c
clang --target=x86_64-linux-gnu -nostdlib -static -fuse-ld=lld -o print_x64 print.c
clang --target=x86_64-linux-gnu -nostdlib -static -fuse-ld=lld -fno-stack-protector -o print_x64 print.c
print_aarch64: print.c
clang --target=aarch64-linux-gnu -nostdlib -static -fuse-ld=lld -o print_aarch64 print.c
clang --target=aarch64-linux-gnu -nostdlib -static -fuse-ld=lld -fno-stack-protector -o print_aarch64 print.c
print_mips32: print.c
clang --target=mips-linux-gnu -nostdlib -static -fuse-ld=lld -fno-pic -o print_mips32 print.c
clang --target=mips-linux-gnu -nostdlib -static -fuse-ld=lld -fno-stack-protector -fno-pic -o print_mips32 print.c

38
print.c
View File

@ -30,9 +30,9 @@ long print(const void *buf, long count) {
return 0;
}
#if defined(__aarch64__)
#define CLONE_CHILD_SETTID 0x1000000
#define CLONE_CHILD_CLEARTID 0x200000
#if defined(__aarch64__)
long int fork() {
long int rtn;
long int flags = CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID;
@ -42,8 +42,8 @@ long int fork() {
"mov x0, %1\n" // clone flags
"mov x1, #0\n" // child stack pointer
"mov x2, #0\n" // parent_tidptr
"mov x3, #0\n" // child_tidptr
"mov x4, #0\n" // tls
"mov x3, #0\n" // tls
"mov x4, #0\n" // child_tidptr
"svc #0\n" // Make the syscall
"mov %0, x0\n" // save return value
// Output operands
@ -55,6 +55,34 @@ long int fork() {
);
return rtn;
}
#elif defined(__x86_64__)
long int fork() {
long int rtn;
/*
long int flags = CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID;
asm volatile(
"movq %%rax, 56\n" // syscall number for 'clone' is 56 for x86-64
"movq %%rdi, %1\n" // clone flags
"movq %%rsi, 0\n" // child stack pointer
"movq %%rdx, 0\n" // parent tidptr
"movq %%r10, 0\n" // child tidptr
"movq %%r8, 0\n" // tls
"syscall\n" // syscall
"movq %0, %%rax\n" // save return value
: "=r"(rtn)
: "r"(flags)
: "rax", "rdi", "rsi", "rdx", "r10", "r8", "r9", "r10", "r11"
);
*/
asm volatile(
"syscall\n" // syscall
"movq %0, %%rax\n" // save return value
: "=r"(rtn)
: "a"(57)
:
);
return rtn;
}
#endif
void exit(int status) {
@ -87,7 +115,7 @@ void exit(int status) {
int main() {
#if defined(__aarch64__)
#if defined(__x86_64__) || defined(__aarch64__)
long int pid = fork();
char msg[17] = {' '};
msg[16] = '\n';
@ -99,7 +127,7 @@ int main() {
msg[i] = c;
}
print(msg, 17);
#elif
#else
char *msg = "Hello World!\n";
print(msg, 13);
#endif