diff --git a/.gitattributes b/.gitattributes index cc4601f186..276bc3a0a8 100644 --- a/.gitattributes +++ b/.gitattributes @@ -10111,6 +10111,7 @@ rtl/openbsd/errnostr.inc svneol=native#text/plain rtl/openbsd/i386/bsyscall.inc svneol=native#text/plain rtl/openbsd/i386/cprt0.as svneol=native#text/plain rtl/openbsd/i386/dllprt0.as svneol=native#text/plain +rtl/openbsd/i386/openbsd_ident.as svneol=native#text/plain rtl/openbsd/i386/prt0.as svneol=native#text/plain rtl/openbsd/i386/si_c.inc svneol=native#text/plain rtl/openbsd/i386/si_dll.inc svneol=native#text/plain @@ -10146,6 +10147,7 @@ rtl/openbsd/x86_64/cprt0.as svneol=native#text/plain rtl/openbsd/x86_64/crt0.s svneol=native#text/plain rtl/openbsd/x86_64/dllprt0.as svneol=native#text/plain rtl/openbsd/x86_64/gprt0.as svneol=native#text/plain +rtl/openbsd/x86_64/openbsd_ident.as svneol=native#text/plain rtl/openbsd/x86_64/prt0.as svneol=native#text/plain rtl/openbsd/x86_64/si_c.inc svneol=native#text/plain rtl/openbsd/x86_64/si_dll.inc svneol=native#text/plain diff --git a/rtl/openbsd/Makefile b/rtl/openbsd/Makefile index 62ba57ab51..de07bf45d0 100644 --- a/rtl/openbsd/Makefile +++ b/rtl/openbsd/Makefile @@ -355,7 +355,7 @@ override FPCOPT+=-Ur endif CPU_UNITS= SYSINIT_UNITS= -LOADERS=prt0 cprt0 dllprt0 +LOADERS=prt0 cprt0 dllprt0 openbsd_ident ifeq ($(ARCH),x86_64) CPU_UNITS=x86 ports cpu SYSINIT_UNITS=si_prc si_c si_dll @@ -3176,6 +3176,8 @@ cprt0$(OEXT) : $(CPU_TARGET)/cprt0.as $(AS) -o $(UNITTARGETDIRPREFIX)cprt0$(OEXT) $(CPU_TARGET)/cprt0.as dllprt0$(OEXT) : $(CPU_TARGET)/dllprt0.as $(AS) -o $(UNITTARGETDIRPREFIX)dllprt0$(OEXT) $(CPU_TARGET)/dllprt0.as +openbsd_ident$(OEXT) : $(CPU_TARGET)/openbsd_ident.as + $(AS) -o $(UNITTARGETDIRPREFIX)openbsd_ident$(OEXT) $(CPU_TARGET)/openbsd_ident.as si_prc$(PPUEXT) : si_prc.pp si_intf.inc $(ARCH)/si_prc.inc $(SYSTEMUNIT)$(PPUEXT) $(COMPILER) $< si_c$(PPUEXT) : si_c.pp si_intf.inc $(ARCH)/si_c.inc $(SYSTEMUNIT)$(PPUEXT) diff --git a/rtl/openbsd/Makefile.fpc b/rtl/openbsd/Makefile.fpc index 4a40c666a9..f4ed7fa680 100644 --- a/rtl/openbsd/Makefile.fpc +++ b/rtl/openbsd/Makefile.fpc @@ -85,7 +85,7 @@ endif CPU_UNITS= SYSINIT_UNITS= -LOADERS=prt0 cprt0 dllprt0 +LOADERS=prt0 cprt0 dllprt0 openbsd_ident ifeq ($(ARCH),x86_64) CPU_UNITS=x86 ports cpu @@ -141,6 +141,9 @@ cprt0$(OEXT) : $(CPU_TARGET)/cprt0.as dllprt0$(OEXT) : $(CPU_TARGET)/dllprt0.as $(AS) -o $(UNITTARGETDIRPREFIX)dllprt0$(OEXT) $(CPU_TARGET)/dllprt0.as +openbsd_ident$(OEXT) : $(CPU_TARGET)/openbsd_ident.as + $(AS) -o $(UNITTARGETDIRPREFIX)openbsd_ident$(OEXT) $(CPU_TARGET)/openbsd_ident.as + # # $(SYSINIT_UNITS) Units diff --git a/rtl/openbsd/i386/openbsd_ident.as b/rtl/openbsd/i386/openbsd_ident.as new file mode 100644 index 0000000000..49707a6f45 --- /dev/null +++ b/rtl/openbsd/i386/openbsd_ident.as @@ -0,0 +1,7 @@ + .section ".note.openbsd.ident", "a" + .p2align 2 + .long 8 + .long 4 + .long 1 + .ascii "OpenBSD\0" + .long 0 diff --git a/rtl/openbsd/si_c.pp b/rtl/openbsd/si_c.pp index 6e957eb4f5..f883a2d867 100644 --- a/rtl/openbsd/si_c.pp +++ b/rtl/openbsd/si_c.pp @@ -22,6 +22,7 @@ interface implementation +{$i si_impl.inc} {$i si_c.inc} end. diff --git a/rtl/openbsd/si_dll.pp b/rtl/openbsd/si_dll.pp index f8a5cde2c3..0ccf2921b6 100644 --- a/rtl/openbsd/si_dll.pp +++ b/rtl/openbsd/si_dll.pp @@ -22,6 +22,7 @@ interface implementation +{$i si_impl.inc} {$i si_dll.inc} end. diff --git a/rtl/openbsd/si_impl.inc b/rtl/openbsd/si_impl.inc index 4cd19cc467..d0a63ed36b 100644 --- a/rtl/openbsd/si_impl.inc +++ b/rtl/openbsd/si_impl.inc @@ -12,6 +12,8 @@ **********************************************************************} +{$LINK openbsd_ident} + procedure PascalMain; external name 'PASCALMAIN'; var diff --git a/rtl/openbsd/si_intf.inc b/rtl/openbsd/si_intf.inc index 1370396dd5..93e7077366 100644 --- a/rtl/openbsd/si_intf.inc +++ b/rtl/openbsd/si_intf.inc @@ -15,6 +15,7 @@ **********************************************************************} {$SMARTLINK OFF} +{$GOTO ON} var operatingsystem_parameter_envp: ppchar; public name 'operatingsystem_parameter_envp'; diff --git a/rtl/openbsd/x86_64/openbsd_ident.as b/rtl/openbsd/x86_64/openbsd_ident.as new file mode 100644 index 0000000000..49707a6f45 --- /dev/null +++ b/rtl/openbsd/x86_64/openbsd_ident.as @@ -0,0 +1,7 @@ + .section ".note.openbsd.ident", "a" + .p2align 2 + .long 8 + .long 4 + .long 1 + .ascii "OpenBSD\0" + .long 0 diff --git a/rtl/openbsd/x86_64/si_c.inc b/rtl/openbsd/x86_64/si_c.inc index ff272a25b5..fe0d24c485 100644 --- a/rtl/openbsd/x86_64/si_c.inc +++ b/rtl/openbsd/x86_64/si_c.inc @@ -14,4 +14,163 @@ **********************************************************************} +{$asmmode gas} +var + _etext: Byte; external name '_etext'; + _eprol: Byte; external name '_eprol'; + +procedure _mcleanup; external name '_mcleanup'; +procedure atexit; external name 'atexit'; +procedure monstartup; external name 'monstartup'; +procedure _init; external name '_init'; + +procedure _FPC_proc___start; forward; + +procedure _FPC_proc_start; assembler; nostackframe; public name '_start'; public name '__start'; + asm + movq %rbx,%r9 + movq %rcx,%r8 + movq %rdx,%rcx + movq (%rsp),%rdi + leaq 16(%rsp,%rdi,8),%rdx + leaq 8(%rsp),%rsi + subq $8,%rsp + andq $0xFFFFFFFFFFFFFFF0,%rsp + addq $8,%rsp + jmp _FPC_proc___start + end; + +procedure _FPC_proc_haltproc; forward; +function _strrchr(str: PChar; character: LongInt): PChar; forward; + +procedure _FPC_proc___start; assembler; nostackframe; public name '___start'; + asm + pushq %rbp + movq %rsp, %rbp + subq $64, %rsp + movl %edi, -20(%rbp) + movq %rsi, -32(%rbp) + movq %rdx, -40(%rbp) + movq %rcx, -48(%rbp) + movq %r8, -56(%rbp) + movq %r9, -64(%rbp) + movq -40(%rbp), %rax + movq %rax, environ(%rip) + movq %rax,operatingsystem_parameter_envp(%rip) + movq -32(%rbp), %rax + movq (%rax), %rax + movq %rax, -8(%rbp) + cmpq $0, -8(%rbp) + je .L2 + movq -8(%rbp), %rdi + movl $47, %esi + call _strrchr + movq %rax, __progname(%rip) + movq __progname(%rip), %rax + testq %rax, %rax + jne .L4 + movq -8(%rbp), %rax + movq %rax, __progname(%rip) + jmp .L6 +.L4: + movq __progname(%rip), %rax + addq $1, %rax + movq %rax, __progname(%rip) +.L6: + leaq __progname_storage(%rip), %rax + movq %rax, -16(%rbp) + jmp .L7 +.L8: + movq __progname(%rip), %rcx + movzbl (%rcx), %edx + movq -16(%rbp), %rax + movb %dl, (%rax) + addq $1, -16(%rbp) + leaq 1(%rcx), %rax + movq %rax, __progname(%rip) +.L7: + movq __progname(%rip), %rax + movzbl (%rax), %eax + testb %al, %al + je .L9 + leaq __progname_storage+255(%rip), %rax + cmpq %rax, -16(%rbp) + jb .L8 +.L9: + leaq __progname_storage(%rip), %rax + movq %rax, __progname(%rip) + movq -16(%rbp), %rax + movb $0, (%rax) +.L2: + movq _mcleanup@GOTPCREL(%rip), %rdi + call atexit + movq _etext@GOTPCREL(%rip), %rsi + movq _eprol(%rip), %rdi + call monstartup@plt + movl $0, %eax + call _init + movq environ(%rip), %rdx + movq -32(%rbp), %rsi + movl -20(%rbp), %edi + movq %rdi,operatingsystem_parameter_argc(%rip) + movq %rsi,operatingsystem_parameter_argv(%rip) + movl $0, %eax + call PASCALMAIN + // movl %eax, %edi + // call exit + jmp _FPC_proc_haltproc + end; + +procedure _FPC_proc_haltproc; assembler; nostackframe; public name '_haltproc'; + asm + movq $1,%rax + movl operatingsystem_result(%rip),%ebx + pushq %rbx + call .Lactualsyscall + addq $8,%rsp + jmp _FPC_proc_haltproc + +.Lactualsyscall: + int $0x80 + jb .LErrorcode + xor %rbx,%rbx + ret +.LErrorcode: + movq %rax,%rbx + movq $-1,%rax + end; + +function _strrchr(str: PChar; character: LongInt): PChar; assembler; nostackframe; public name '_strrchr'; + asm + pushq %rbp + movq %rsp, %rbp + movq %rdi, -24(%rbp) + movb %sil, -25(%rbp) + movq $0, -8(%rbp) +.L13: + movq -24(%rbp), %rdx + movzbl (%rdx), %eax + cmpb -25(%rbp), %al + jne .L14 + movq -24(%rbp), %rax + movq %rax, -8(%rbp) +.L14: + movq -24(%rbp), %rdx + movzbl (%rdx), %eax + testb %al, %al + jne .L16 + movq -8(%rbp), %rax + movq %rax, -16(%rbp) + jmp .L12 +.L16: + addq $1, -24(%rbp) + jmp .L13 +.L12: + movq -16(%rbp), %rax + leave + end; + +procedure MD_EPROL_LABEL; assembler; nostackframe; public name '_eprol'; + asm + end; diff --git a/rtl/openbsd/x86_64/si_dll.inc b/rtl/openbsd/x86_64/si_dll.inc index 7d43e0aea1..2f1e27c006 100644 --- a/rtl/openbsd/x86_64/si_dll.inc +++ b/rtl/openbsd/x86_64/si_dll.inc @@ -14,4 +14,169 @@ **********************************************************************} +{$asmmode gas} + +procedure _init; external name '_init'; + +procedure _FPC_proc___start; forward; + +procedure _FPC_proc_start; assembler; nostackframe; public name '_start'; public name '__start'; + asm + movq %rbx,%r9 + movq %rcx,%r8 + movq %rdx,%rcx + movq (%rsp),%rdi + leaq 16(%rsp,%rdi,8),%rdx + leaq 8(%rsp),%rsi + subq $8,%rsp + andq $0xFFFFFFFFFFFFFFF0,%rsp + addq $8,%rsp + jmp _FPC_proc___start + end; + +procedure _FPC_proc_haltproc; forward; +function _strrchr(str: PChar; character: LongInt): PChar; forward; + +procedure _FPC_proc___start; assembler; nostackframe; public name '___start'; + asm + pushq %rbp + movq %rsp, %rbp + subq $64, %rsp + movl %edi, -20(%rbp) + movq %rsi, -32(%rbp) + movq %rdx, -40(%rbp) + movq %rcx, -48(%rbp) + movq %r8, -56(%rbp) + movq %r9, -64(%rbp) + movq environ@GOTPCREL(%rip), %rdx + movq -40(%rbp), %rax + movq %rax, (%rdx) + movq operatingsystem_parameter_envp@GOTPCREL(%rip), %rdx + movq -40(%rbp), %rax + movq %rax, (%rdx) + + movl -20(%rbp), %eax + movslq %eax,%rdx + movq operatingsystem_parameter_argc@GOTPCREL(%rip), %rax + movq %rdx, (%rax) + movq operatingsystem_parameter_argv@GOTPCREL(%rip), %rdx + movq -32(%rbp), %rax + movq %rax, (%rdx) + movq -32(%rbp), %rax + movq (%rax), %rax + movq %rax, -8(%rbp) + cmpq $0, -8(%rbp) + je .L2 + movq -8(%rbp), %rdi + movl $47, %esi + call _strrchr + movq %rax, %rdx + movq __progname@GOTPCREL(%rip), %rax + movq %rdx, (%rax) + movq __progname@GOTPCREL(%rip), %rax + movq (%rax), %rax + testq %rax, %rax + jne .L4 + movq __progname@GOTPCREL(%rip), %rdx + movq -8(%rbp), %rax + movq %rax, (%rdx) + jmp .L6 +.L4: + movq __progname@GOTPCREL(%rip), %rax + movq (%rax), %rax + leaq 1(%rax), %rdx + movq __progname@GOTPCREL(%rip), %rax + movq %rdx, (%rax) +.L6: + movq __progname_storage@GOTPCREL(%rip), %rax + movq %rax, -16(%rbp) + jmp .L7 +.L8: + movq __progname@GOTPCREL(%rip), %rax + movq (%rax), %rcx + movzbl (%rcx), %edx + movq -16(%rbp), %rax + movb %dl, (%rax) + addq $1, -16(%rbp) + leaq 1(%rcx), %rdx + movq __progname@GOTPCREL(%rip), %rax + movq %rdx, (%rax) +.L7: + movq __progname@GOTPCREL(%rip), %rax + movq (%rax), %rax + movzbl (%rax), %eax + testb %al, %al + je .L9 + movq __progname_storage@GOTPCREL(%rip), %rax + leaq 255(%rax), %rax + cmpq %rax, -16(%rbp) + jb .L8 +.L9: + movq -16(%rbp), %rax + movb $0, (%rax) + movq __progname@GOTPCREL(%rip), %rdx + movq __progname_storage@GOTPCREL(%rip), %rax + movq %rax, (%rdx) +.L2: + movl $0, %eax + call _init@PLT + movq environ@GOTPCREL(%rip), %rax + movq (%rax), %rdx + movq -32(%rbp), %rsi + movl -20(%rbp), %edi + movl $0, %eax + call PASCALMAIN@PLT + // movl %eax, %edi + // call exit + jmp _FPC_proc_haltproc@PLT + end; + +procedure _FPC_proc_haltproc; assembler; nostackframe; public name '_haltproc'; + asm + movq $1,%rax + movl operatingsystem_result(%rip),%ebx + pushq %rbx + call .Lactualsyscall + addq $8,%rsp + jmp _FPC_proc_haltproc + +.Lactualsyscall: + int $0x80 + jb .LErrorcode + xor %rbx,%rbx + ret +.LErrorcode: + movq %rax,%rbx + movq $-1,%rax + end; + +function _strrchr(str: PChar; character: LongInt): PChar; assembler; nostackframe; public name '_strrchr'; + asm + pushq %rbp + movq %rsp, %rbp + movq %rdi, -24(%rbp) + movb %sil, -25(%rbp) + movq $0, -8(%rbp) +.L13: + movq -24(%rbp), %rdx + movzbl (%rdx), %eax + cmpb -25(%rbp), %al + jne .L14 + movq -24(%rbp), %rax + movq %rax, -8(%rbp) +.L14: + movq -24(%rbp), %rdx + movzbl (%rdx), %eax + testb %al, %al + jne .L16 + movq -8(%rbp), %rax + movq %rax, -16(%rbp) + jmp .L12 +.L16: + addq $1, -24(%rbp) + jmp .L13 +.L12: + movq -16(%rbp), %rax + leave + end;