Remove print.c
This commit is contained in:
parent
a72a47e0a3
commit
31df6146bf
14
Makefile
14
Makefile
@ -1,4 +1,4 @@
|
|||||||
all: print print_x64 print_aarch64 print_mips32 buildtest
|
all: buildtest
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
if test -f print; then rm print; fi
|
if test -f print; then rm print; fi
|
||||||
@ -15,18 +15,6 @@ buildtest: bldtst bldtst_llvm bldtst_x64 bldtst_aarch64 bldtst_mips32
|
|||||||
|
|
||||||
.PHONY: all buildtest clean
|
.PHONY: all buildtest clean
|
||||||
|
|
||||||
print:
|
|
||||||
gcc -static -nostdlib -Wno-builtin-declaration-mismatch -fno-stack-protector -o print print.c
|
|
||||||
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 -fno-stack-protector -o print_aarch64 print.c
|
|
||||||
|
|
||||||
print_mips32: print.c
|
|
||||||
clang --target=mips-linux-gnu -nostdlib -static -fuse-ld=lld -fno-stack-protector -fno-pic -o print_mips32 print.c
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
bldtst: buildtest.c arch/*.c
|
bldtst: buildtest.c arch/*.c
|
||||||
gcc -static -nostdlib -Wno-builtin-declaration-mismatch -fno-stack-protector -o bldtst buildtest.c arch/*.c
|
gcc -static -nostdlib -Wno-builtin-declaration-mismatch -fno-stack-protector -o bldtst buildtest.c arch/*.c
|
||||||
|
|||||||
173
print.c
173
print.c
@ -1,173 +0,0 @@
|
|||||||
long print(const void *buf, long count) {
|
|
||||||
#if defined(__x86_64__)
|
|
||||||
asm volatile (
|
|
||||||
"syscall"
|
|
||||||
:
|
|
||||||
: "a"(1), "D"(1), "S"(buf), "d"(count) // RDI, RSI, RDX are used in x86-64 for write args
|
|
||||||
: "rcx", "r11", "memory"
|
|
||||||
);
|
|
||||||
#elif defined(__aarch64__)
|
|
||||||
asm ( "mov x0, #1\n" // Move the exit status into register x0
|
|
||||||
"mov x1, %0\n" // Syscall number for 'exit' is 93 in AArch64
|
|
||||||
"mov x2, %1\n" // Syscall number for 'exit' is 93 in AArch64
|
|
||||||
"mov x8, #64\n" // Syscall number for 'exit' is 93 in AArch64
|
|
||||||
"svc #0\n" // Make the syscall
|
|
||||||
: // No output operands
|
|
||||||
: "r" (buf), "r"(count) // Input operand: status
|
|
||||||
: "x0", "x8" // Clobbered registers
|
|
||||||
);
|
|
||||||
#elif defined(__mips__)
|
|
||||||
asm ( "li $a0, 1\n"
|
|
||||||
"move $a1, %0\n"
|
|
||||||
"move $a2, %1\n"
|
|
||||||
"li $v0, 4004\n"
|
|
||||||
"syscall"
|
|
||||||
:
|
|
||||||
: "r"(buf), "r"(count)
|
|
||||||
: "a0", "a1", "a2", "v0" //TODO: temp registers clobbered by syscall should probably also be listed but this fn returns right away so it should be fine?
|
|
||||||
);
|
|
||||||
#endif
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
#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;
|
|
||||||
asm volatile (
|
|
||||||
// Assembly Instructions
|
|
||||||
"mov x8, #220\n" // Syscall number for 'clone' is 220 in AArch64
|
|
||||||
"mov x0, %1\n" // clone flags
|
|
||||||
"mov x1, #0\n" // child stack pointer
|
|
||||||
"mov x2, #0\n" // parent_tidptr
|
|
||||||
"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
|
|
||||||
: "=r" (rtn)
|
|
||||||
// Input operand
|
|
||||||
: "r" (flags)
|
|
||||||
// Clobbered registers
|
|
||||||
: "x0", "x1", "x2", "x3", "x4", "x8"
|
|
||||||
);
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
#elif defined(__mips__)
|
|
||||||
long int fork() {
|
|
||||||
int rtn;
|
|
||||||
asm (
|
|
||||||
"li $v0, 4002\n"
|
|
||||||
"syscall\n"
|
|
||||||
"move %0, $v0"
|
|
||||||
:"=r" (rtn)
|
|
||||||
:
|
|
||||||
: "a0", "v0"
|
|
||||||
);
|
|
||||||
return (long int)rtn;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
void exit(int status) {
|
|
||||||
#if defined(__x86_64__)
|
|
||||||
asm volatile(
|
|
||||||
"syscall"
|
|
||||||
:
|
|
||||||
: "a"(60), "D"(status)
|
|
||||||
:
|
|
||||||
);
|
|
||||||
#elif defined(__aarch64__)
|
|
||||||
asm ( "mov x0, %0\n" // Move the exit status into register x0
|
|
||||||
"mov x8, #93\n" // Syscall number for 'exit' is 93 in AArch64
|
|
||||||
"svc #0\n" // Make the syscall
|
|
||||||
: // No output operands
|
|
||||||
: "r" ((long)status) // Input operand: status
|
|
||||||
: "x0", "x8" // Clobbered registers
|
|
||||||
);
|
|
||||||
#elif defined(__mips__)
|
|
||||||
asm ( "move $a0, %0\n"
|
|
||||||
"li $v0, 4001\n"
|
|
||||||
"syscall"
|
|
||||||
:
|
|
||||||
: "r" (status)
|
|
||||||
: "a0", "v0"
|
|
||||||
);
|
|
||||||
#endif
|
|
||||||
for(;;);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int main() {
|
|
||||||
#if defined(__x86_64__) || defined(__aarch64__) || defined(__mips__)
|
|
||||||
long int pid = fork();
|
|
||||||
char msg[17] = {' '};
|
|
||||||
msg[16] = '\n';
|
|
||||||
for(int i = 0; i < 16; ++i) {
|
|
||||||
char nibble = (pid >> ((15 - i) * 4)) & 0xf;
|
|
||||||
char c;
|
|
||||||
if (nibble > 9) {c = nibble + '7';}
|
|
||||||
else {c = nibble + '0';}
|
|
||||||
msg[i] = c;
|
|
||||||
}
|
|
||||||
print(msg, 17);
|
|
||||||
#else
|
|
||||||
char *msg = "Hello World!\n";
|
|
||||||
print(msg, 13);
|
|
||||||
#endif
|
|
||||||
return 69;
|
|
||||||
}
|
|
||||||
void __libc_start_main() {exit(main());}
|
|
||||||
|
|
||||||
void _start() {
|
|
||||||
__libc_start_main();
|
|
||||||
}
|
|
||||||
|
|
||||||
void __start() {
|
|
||||||
_start();
|
|
||||||
}
|
|
||||||
/*
|
|
||||||
#if defined(__x86_64__)
|
|
||||||
printf("Compiled for x86_64 architecture\n");
|
|
||||||
#elif defined(__i386__)
|
|
||||||
printf("Compiled for x86 architecture\n");
|
|
||||||
#elif defined(__arm__)
|
|
||||||
printf("Compiled for ARM architecture\n");
|
|
||||||
#elif defined(__aarch64__)
|
|
||||||
printf("Compiled for ARM64 architecture\n");
|
|
||||||
#elif defined(__mips__)
|
|
||||||
printf("Compiled for ARM architecture\n");
|
|
||||||
#else
|
|
||||||
printf("Unknown architecture\n");
|
|
||||||
#endif
|
|
||||||
*/
|
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user