Forráskód Böngészése

* Android: Reworked the startup code to use no assembly instructions. Generic assembler startup files contains only section data and are compiled for each CPU.
* Android: argc and argv are correct for shared libraries.

git-svn-id: trunk@39905 -

yury 6 éve
szülő
commit
9d7308df42

+ 2 - 8
.gitattributes

@@ -8789,16 +8789,11 @@ rtl/amiga/system.pp svneol=native#text/plain
 rtl/amiga/timerd.inc svneol=native#text/plain
 rtl/android/Makefile svneol=native#text/plain
 rtl/android/Makefile.fpc svneol=native#text/plain
-rtl/android/aarch64/dllprt0.as svneol=native#text/plain
-rtl/android/aarch64/prt0.as svneol=native#text/plain
 rtl/android/aarch64/sysnr.inc svneol=native#text/plain
-rtl/android/arm/dllprt0.as svneol=native#text/plain
-rtl/android/arm/prt0.as svneol=native#text/plain
 rtl/android/arm/sysnr.inc svneol=native#text/plain
 rtl/android/cwstring.pp svneol=native#text/plain
 rtl/android/dlandroid.inc svneol=native#text/plain
-rtl/android/i386/dllprt0.as svneol=native#text/plain
-rtl/android/i386/prt0.as svneol=native#text/plain
+rtl/android/dllprt0.as svneol=native#text/plain
 rtl/android/i386/sysnr.inc svneol=native#text/plain
 rtl/android/jvm/Makefile svneol=native#text/plain
 rtl/android/jvm/Makefile.fpc svneol=native#text/plain
@@ -8807,9 +8802,8 @@ rtl/android/jvm/androidr14.pas svneol=native#text/plain
 rtl/android/jvm/java_sys_android.inc svneol=native#text/plain
 rtl/android/jvm/java_sysh_android.inc svneol=native#text/plain
 rtl/android/jvm/rtl.cfg svneol=native#text/plain
-rtl/android/mipsel/dllprt0.as svneol=native#text/plain
-rtl/android/mipsel/prt0.as svneol=native#text/plain
 rtl/android/mipsel/sysnr.inc svneol=native#text/plain
+rtl/android/prt0.as svneol=native#text/plain
 rtl/android/sysandroid.inc svneol=native#text/plain
 rtl/android/sysandroidh.inc svneol=native#text/plain
 rtl/android/unixandroid.inc svneol=native#text/plain

+ 1 - 1
compiler/systems/t_android.pas

@@ -169,7 +169,7 @@ begin
    begin
      { Specify correct max-page-size and common-page-size to prevent big gaps between sections in resulting executable }
      s:='ld -z max-page-size=0x1000 -z common-page-size=0x1000 -z noexecstack -z now $OPT -L. -T $RES -o $EXE';
-     ExeCmd[1]:=s + ' --entry=_fpc_start';
+     ExeCmd[1]:=s + ' --entry=_start';
      DllCmd[1]:=s + ' -shared -soname $SONAME';
      DllCmd[2]:='strip --strip-unneeded $EXE';
      ExtDbgCmd[1]:='objcopy --only-keep-debug $EXE $DBG';

+ 9 - 4
rtl/android/Makefile

@@ -3753,10 +3753,15 @@ endif
 ifeq ($(ARCH),mipsel)
   ASTARGET=-mips32 -KPIC
 endif
-prt0$(OEXT) : $(ARCH)/prt0.as
-	$(AS) $(ASTARGET) -o $(UNITTARGETDIRPREFIX)prt0$(OEXT) $(ARCH)/prt0.as
-dllprt0$(OEXT) : $(ARCH)/dllprt0.as
-	$(AS) $(ASTARGET) -o $(UNITTARGETDIRPREFIX)dllprt0$(OEXT) $(ARCH)/dllprt0.as
+ifneq ($(findstring $(ARCH),aarch64 x86_64),)
+  CPUBITS=64
+else
+  CPUBITS=32
+endif
+prt0$(OEXT) : prt0.as
+	$(AS) $(ASTARGET) -o $(UNITTARGETDIRPREFIX)prt0$(OEXT) --defsym CPU$(CPUBITS)=1 prt0.as
+dllprt0$(OEXT) : dllprt0.as
+	$(AS) $(ASTARGET) -o $(UNITTARGETDIRPREFIX)dllprt0$(OEXT) --defsym CPU$(CPUBITS)=1 dllprt0.as
 $(SYSTEMUNIT)$(PPUEXT) : $(LINUXINC)/$(SYSTEMUNIT).pp $(SYSDEPS)
 	$(COMPILER) $(FPC_SYSTEM_OPT) -Us -Sg $(LINUXINC)/$(SYSTEMUNIT).pp
 uuchar$(PPUEXT): $(SYSTEMUNIT)$(PPUEXT) $(INC)/uuchar.pp

+ 9 - 4
rtl/android/Makefile.fpc

@@ -107,16 +107,21 @@ ifeq ($(ARCH),mipsel)
   ASTARGET=-mips32 -KPIC
 endif
 
+ifneq ($(findstring $(ARCH),aarch64 x86_64),)
+  CPUBITS=64
+else
+  CPUBITS=32
+endif
 
 #
 # Loaders
 #
 
-prt0$(OEXT) : $(ARCH)/prt0.as
-        $(AS) $(ASTARGET) -o $(UNITTARGETDIRPREFIX)prt0$(OEXT) $(ARCH)/prt0.as
+prt0$(OEXT) : prt0.as
+        $(AS) $(ASTARGET) -o $(UNITTARGETDIRPREFIX)prt0$(OEXT) --defsym CPU$(CPUBITS)=1 prt0.as
 
-dllprt0$(OEXT) : $(ARCH)/dllprt0.as
-        $(AS) $(ASTARGET) -o $(UNITTARGETDIRPREFIX)dllprt0$(OEXT) $(ARCH)/dllprt0.as
+dllprt0$(OEXT) : dllprt0.as
+        $(AS) $(ASTARGET) -o $(UNITTARGETDIRPREFIX)dllprt0$(OEXT) --defsym CPU$(CPUBITS)=1 dllprt0.as
 
 #
 # $(SYSTEMUNIT) Units ($(SYSTEMUNIT), Objpas, Strings)

+ 0 - 84
rtl/android/aarch64/dllprt0.as

@@ -1,84 +0,0 @@
-#
-#   This file is part of the Free Pascal run time library.
-#   Copyright (c) 2018 by Yuriy Sydorov and other
-#   members of the Free Pascal development team.
-#
-#   See the file COPYING.FPC, included in this distribution,
-#   for details about the copyright.
-#
-#   This program 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.
-#
-#**********************************************************************}
-#
-# Shared library startup code for Free Pascal. Android-ARM target.
-#
-
-.file   "dllprt0.as"
-.text
-.align 2
-        .globl  FPC_SHARED_LIB_START
-        .type   FPC_SHARED_LIB_START,#function
-FPC_SHARED_LIB_START:
-        stp	x29,x30,[sp, #-16]!
-
-        /* Save initial stackpointer */
-        adrp x14,:got:__stkptr
-        ldr	x14,[x14,#:got_lo12:__stkptr]
-        mov x13,sp
-        str x13,[x14]
-
-        /* Get environment info from libc */
-        adrp x14,:got:environ
-        ldr	x14,[x14,#:got_lo12:environ]
-        ldr x13,[x14]
-        /* Check if environment is NULL */
-        cmp x13,#0
-        b.ne .Lenvok
-
-        adrp x14,:got:EmptyEnv
-        ldr	x13,[x14,#:got_lo12:EmptyEnv]
-
-.Lenvok:
-        adrp x14,:got:operatingsystem_parameter_envp
-        ldr	x14,[x14,#:got_lo12:operatingsystem_parameter_envp]
-        str x13,[x14]
-
-        /* Call main */
-        bl FPC_LIB_MAIN_ANDROID
-        /* Call library init */
-        bl FPC_LIB_INIT_ANDROID
-
-        ldp	x29,x30,[sp], #16
-        ret
-
-/* --------------------------------------------------------- */
-        .globl  _haltproc
-        .type   _haltproc,#function
-_haltproc:
-        /* Simply call libc exit(). _haltproc has the same declaration as exit. */
-        bl exit
-
-/* --------------------------------------------------------- */
-.data
-        .comm __stkptr,8
-        .comm operatingsystem_parameter_envp,8
-operatingsystem_parameter_argc:
-        .global operatingsystem_parameter_argc
-        .long 1
-operatingsystem_parameter_argv:
-        .global operatingsystem_parameter_argv
-        .quad EmptyCmdLine
-EmptyCmdLine:
-        .quad EmptyCmdStr
-EmptyCmdStr:
-        .ascii "\0"
-EmptyEnv:
-        .quad 0
-        .quad 0
-        .quad 0
-
-/* --------------------------------------------------------- */
-      	.section .init_array, "aw"
-	      .quad FPC_SHARED_LIB_START

+ 0 - 91
rtl/android/aarch64/prt0.as

@@ -1,91 +0,0 @@
-#
-#   This file is part of the Free Pascal run time library.
-#   Copyright (c) 2018 by Yuriy Sydorov and other
-#   members of the Free Pascal development team.
-#
-#   See the file COPYING.FPC, included in this distribution,
-#   for details about the copyright.
-#
-#   This program 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.
-#
-#**********************************************************************}
-#
-# Program startup code for Free Pascal. Android-aarch64 target.
-#
-
-/* At this entry point, most registers' values are unspecified, except:
-
-   sp           The stack contains the arguments and environment:
-                0(sp)                   argc
-                8(sp)                   argv[0]
-                ...
-                (8*argc)(sp)            NULL
-                (8*(argc+1))(sp)        envp[0]
-                ...
-                                        NULL
-*/
-
-/*
-   In our entry point we should save pointers to cmd line arguments
-   and environment vars, then pass control to libc startup code.
-   It will call "PASCALMAIN" via alias "main".
-*/
-
-.file   "prt0.as"
-.text
-.align 2
-        .globl _fpc_start
-        .type _fpc_start,#function
-_fpc_start:
-        /* Clear the frame pointer since this is the outermost frame.  */
-        mov x29,#0
-        /* Save initial stackpointer and slightly adjust it */
-        adrp x14,:got:__stkptr
-        ldr	x14,[x14,#:got_lo12:__stkptr]
-        mov x0,sp
-        add x1,x0,#-64
-        str x1,[x14]
-        /* Get argc off the stack and save a pointer to argv */
-        ldr w1,[x0]
-        adrp x14,:got:operatingsystem_parameter_argc
-        ldr	x14,[x14,#:got_lo12:operatingsystem_parameter_argc]
-        str w1,[x14]
-        adrp x14,:got:operatingsystem_parameter_argv
-        ldr	x14,[x14,#:got_lo12:operatingsystem_parameter_argv]
-        add x0,x0,#8
-        str x0,[x14]
-
-        /* calc envp */
-        add x1,x1,#1
-        add x1,x0,x1,LSL #3
-        adrp x14,:got:operatingsystem_parameter_envp
-        ldr	x14,[x14,#:got_lo12:operatingsystem_parameter_envp]
-        str x1,[x14]
-        
-        /* Finally go to libc startup code. It will call "PASCALMAIN" via alias "main" */
-        b _start
-
-/* --------------------------------------------------------- */
-        .globl  _haltproc
-        .type   _haltproc,#function
-_haltproc:
-        /* Simply call libc exit(). _haltproc has the same declaration as exit. */
-        bl exit
-
-/* --------------------------------------------------------- */
-.data
-/* Define a symbol for the first piece of initialized data.  */
-        .globl __data_start
-__data_start:
-        .long 0
-        .weak data_start
-        data_start = __data_start
-
-/* --------------------------------------------------------- */
-.bss
-        .comm __stkptr,8
-        .comm operatingsystem_parameter_envp,8
-        .comm operatingsystem_parameter_argc,4
-        .comm operatingsystem_parameter_argv,8

+ 0 - 98
rtl/android/arm/dllprt0.as

@@ -1,98 +0,0 @@
-#
-#   This file is part of the Free Pascal run time library.
-#   Copyright (c) 2013 by Yury Sidorov and other
-#   members of the Free Pascal development team.
-#
-#   See the file COPYING.FPC, included in this distribution,
-#   for details about the copyright.
-#
-#   This program 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.
-#
-#**********************************************************************}
-#
-# Shared library startup code for Free Pascal. Android-ARM target.
-#
-
-.file   "dllprt0.as"
-.text
-        .globl  FPC_SHARED_LIB_START
-        .type   FPC_SHARED_LIB_START,#function
-FPC_SHARED_LIB_START:
-        mov ip, sp
-        stmfd sp!,{fp, ip, lr, pc}
-        sub fp, ip, #4
-
-        /* Get GOT */
-        ldr r3,.L_GOT1
-.LPIC1:
-        add r3,pc,r3
-
-        /* Save initial stackpointer */
-        ldr ip,.L__stkptr
-        ldr ip,[r3, ip]
-        str sp,[ip]
-
-        /* Get environment info from libc */
-        ldr ip,.Lenviron
-        ldr ip,[r3, ip]
-        ldr r0,[ip]
-        /* Check if environment is NULL */
-        cmp r0,#0
-        ldreq r0,.LEmptyEnv
-        ldreq r0,[r3, r0]
-        ldr ip,.Loperatingsystem_parameter_envp
-        ldr ip,[r3, ip]
-        str r0,[ip]
-
-        /* Call main */
-        bl FPC_LIB_MAIN_ANDROID
-        /* Call library init */
-        bl FPC_LIB_INIT_ANDROID
-
-        ldmea fp, {fp, sp, pc}
-
-.L_GOT1:
-        .long _GLOBAL_OFFSET_TABLE_-.LPIC1-8
-.L__stkptr:
-        .word __stkptr(GOT)
-.Lenviron:
-        .word environ(GOT)
-.LEmptyEnv:
-        .word EmptyEnv(GOT)
-.Loperatingsystem_parameter_envp:
-        .word operatingsystem_parameter_envp(GOT)
-
-/* --------------------------------------------------------- */
-        .globl  _haltproc
-        .type   _haltproc,#function
-_haltproc:
-        .globl  _haltproc_eabi
-        .type   _haltproc_eabi,#function
-_haltproc_eabi:
-        /* Simply call libc exit(). _haltproc has the same declaration as exit. */
-        bl exit
-
-/* --------------------------------------------------------- */
-.data
-        .comm __stkptr,4
-        .comm operatingsystem_parameter_envp,4
-operatingsystem_parameter_argc:
-        .global operatingsystem_parameter_argc
-        .long 1
-operatingsystem_parameter_argv:
-        .global operatingsystem_parameter_argv
-        .long EmptyCmdLine
-EmptyCmdLine:
-        .long EmptyCmdStr
-EmptyCmdStr:
-        .ascii "\0"
-EmptyEnv:
-        .long 0
-        .long 0
-        .long 0
-
-/* --------------------------------------------------------- */
-      	.section .init_array, "aw"
-	      .long FPC_SHARED_LIB_START

+ 0 - 110
rtl/android/arm/prt0.as

@@ -1,110 +0,0 @@
-#
-#   This file is part of the Free Pascal run time library.
-#   Copyright (c) 2013 by Yury Sidorov and other
-#   members of the Free Pascal development team.
-#
-#   See the file COPYING.FPC, included in this distribution,
-#   for details about the copyright.
-#
-#   This program 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.
-#
-#**********************************************************************}
-#
-# Program startup code for Free Pascal. Android-ARM target.
-#
-
-/* At this entry point, most registers' values are unspecified, except:
-
-   sp           The stack contains the arguments and environment:
-                0(sp)                   argc
-                4(sp)                   argv[0]
-                ...
-                (4*argc)(sp)            NULL
-                (4*(argc+1))(sp)        envp[0]
-                ...
-                                        NULL
-*/
-
-/*
-   In our entry point we should save pointers to cmd line arguments
-   and environment vars, then pass control to libc startup code.
-   It will call "PASCALMAIN" via alias "main".
-*/
-
-.text
-        .globl _fpc_start
-        .type _fpc_start,#function
-_fpc_start:
-        /* Get GOT */
-        ldr r3,.L_GOT1
-.LPIC1:
-        add r3,pc,r3
-
-        /* Clear the frame pointer since this is the outermost frame.  */
-        mov fp, #0
-        /* Save initial stackpointer */
-        ldr ip,.L__stkptr
-        ldr ip,[r3, ip]
-        str sp,[ip]
-        mov r0,sp
-        /* Pop argc off the stack and save a pointer to argv */
-        ldmia r0!, {r1}
-        ldr ip,.Loperatingsystem_parameter_argc
-        ldr ip,[r3, ip]
-        str r1,[ip]
-        ldr ip,.Loperatingsystem_parameter_argv
-        ldr ip,[r3, ip]
-        str r0,[ip]
-
-        /* calc envp */
-        add r1,r1,#1
-        add r1,r0,r1,LSL #2
-        ldr ip,.Loperatingsystem_parameter_envp
-        ldr ip,[r3, ip]
-        str r1,[ip]
-        
-        /* Finally go to libc startup code. It will call "PASCALMAIN" via alias "main" */
-        ldr ip,.L_start
-        ldr ip,[r3, ip]
-        bx ip
-
-.L_GOT1:
-        .long _GLOBAL_OFFSET_TABLE_-.LPIC1-8
-.L__stkptr:
-        .word __stkptr(GOT)
-.L_start:
-        .word _start(GOT)
-.Loperatingsystem_parameter_argc:
-        .word operatingsystem_parameter_argc(GOT)
-.Loperatingsystem_parameter_argv:
-        .word operatingsystem_parameter_argv(GOT)
-.Loperatingsystem_parameter_envp:
-        .word operatingsystem_parameter_envp(GOT)
-
-/* --------------------------------------------------------- */
-        .globl  _haltproc
-        .type   _haltproc,#function
-_haltproc:
-        .globl  _haltproc_eabi
-        .type   _haltproc_eabi,#function
-_haltproc_eabi:
-        /* Simply call libc exit(). _haltproc has the same declaration as exit. */
-        bl exit
-
-/* --------------------------------------------------------- */
-.data
-/* Define a symbol for the first piece of initialized data.  */
-        .globl __data_start
-__data_start:
-        .long 0
-        .weak data_start
-        data_start = __data_start
-
-/* --------------------------------------------------------- */
-.bss
-        .comm __stkptr,4
-        .comm operatingsystem_parameter_envp,4
-        .comm operatingsystem_parameter_argc,4
-        .comm operatingsystem_parameter_argv,4

+ 25 - 0
rtl/android/dllprt0.as

@@ -0,0 +1,25 @@
+#
+#   This file is part of the Free Pascal run time library.
+#   Copyright (c) 2018 by Yuriy Sydorov and other
+#   members of the Free Pascal development team.
+#
+#   See the file COPYING.FPC, included in this distribution,
+#   for details about the copyright.
+#
+#   This program 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.
+#
+#**********************************************************************}
+#
+# Shared library startup code for Free Pascal. Android target.
+#
+
+/* --------------------------------------------------------- */
+  .section .init_array, "aw"
+
+.ifdef CPU64
+  .quad FPC_LIB_START_ANDROID
+.else
+  .long FPC_LIB_START_ANDROID
+.endif

+ 0 - 92
rtl/android/i386/dllprt0.as

@@ -1,92 +0,0 @@
-#
-#   This file is part of the Free Pascal run time library.
-#   Copyright (c) 2013 by Yury Sidorov and other
-#   members of the Free Pascal development team.
-#
-#   See the file COPYING.FPC, included in this distribution,
-#   for details about the copyright.
-#
-#   This program 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.
-#
-#**********************************************************************}
-#
-# Shared library startup code for Free Pascal. Android-i386 target.
-#
-
-.file   "dllprt0.as"
-.text
-        .globl  FPC_SHARED_LIB_START
-        .type   FPC_SHARED_LIB_START,@function
-FPC_SHARED_LIB_START:
-        pushl   %ebp
-        movl    %esp,%ebp
-        /* Align the stack to a 16 byte boundary */
-        andl $~15, %esp
-
-        /* Save ebx */
-        pushl   %ebx
-
-        /* GOT init */
-        call    fpc_geteipasebx
-        addl    $_GLOBAL_OFFSET_TABLE_,%ebx
-
-        /* Save initial stackpointer */
-        movl    __stkptr@GOT(%ebx),%eax
-        movl    %esp,(%eax)
-
-        /* Get environment info from libc */
-        movl    environ@GOT(%ebx),%eax
-        movl    (%eax),%eax
-        /* Check if environment is NULL */
-        test    %eax,%eax
-        jne     env_ok
-        movl    EmptyEnv@GOT(%ebx),%eax
-env_ok:
-        movl    operatingsystem_parameter_envp@GOT(%ebx),%edx
-        movl    %eax,(%edx)
-
-        /* Restore ebx */
-        popl    %ebx
-
-        /* Call main */
-        call    FPC_LIB_MAIN_ANDROID@PLT
-        /* Call library init */
-        call    FPC_LIB_INIT_ANDROID@PLT
-
-        leave
-        ret
-
-/* --------------------------------------------------------- */
-        .globl  _haltproc
-        .type   _haltproc,@function
-_haltproc:
-        /* GOT init */
-        call    fpc_geteipasebx
-        addl    $_GLOBAL_OFFSET_TABLE_,%ebx
-        /* Jump to libc exit(). _haltproc has the same declaration as exit. */
-        jmp     exit@PLT
-
-/* --------------------------------------------------------- */
-.data
-        .comm __stkptr,4
-        .comm operatingsystem_parameter_envp,4
-operatingsystem_parameter_argc:
-        .global operatingsystem_parameter_argc
-        .long 1
-operatingsystem_parameter_argv:
-        .global operatingsystem_parameter_argv
-        .long EmptyCmdLine
-EmptyCmdLine:
-        .long EmptyCmdStr
-EmptyCmdStr:
-        .ascii "\0"
-EmptyEnv:
-        .long 0
-        .long 0
-        .long 0
-
-/* --------------------------------------------------------- */
-      	.section .init_array, "aw"
-	      .long FPC_SHARED_LIB_START

+ 0 - 98
rtl/android/i386/prt0.as

@@ -1,98 +0,0 @@
-#
-#   This file is part of the Free Pascal run time library.
-#   Copyright (c) 2013 by Yury Sidorov and other
-#   members of the Free Pascal development team.
-#
-#   See the file COPYING.FPC, included in this distribution,
-#   for details about the copyright.
-#
-#   This program 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.
-#
-#**********************************************************************}
-#
-# Program startup code for Free Pascal. Android-i386 target.
-#
-# Stack layout at program start:
-#
-#         nil
-#         envn
-#         ....
-#         ....           ENVIRONMENT VARIABLES
-#         env1
-#         env0
-#         nil
-#         argn
-#         ....
-#         ....           COMMAND LINE OPTIONS
-#         arg1
-#         arg0
-#         argc <--- esp
-#
-
-/*
-   In our entry point we should save pointers to cmd line arguments
-   and environment vars, then pass control to libc startup code.
-   It will call "PASCALMAIN" via alias "main".
-*/
-        .file   "prt0.as"
-.text
-        .align 4
-        .globl _fpc_start
-        .type _fpc_start,@function
-_fpc_start:
-        /* GOT init */
-        call    fpc_geteipasebx
-        addl    $_GLOBAL_OFFSET_TABLE_,%ebx
-        /* Clear the frame pointer since this is the outermost frame.  */
-        xorl    %ebp,%ebp
-        /* Save initial stackpointer */
-        movl    __stkptr@GOT(%ebx),%eax
-        movl    %esp,(%eax)
-        /* First locate the start of the environment variables */
-        /* Get argc in ecx */
-        movl    (%esp),%ecx
-        /* Save argc */
-        movl    operatingsystem_parameter_argc@GOT(%ebx),%eax
-        movl    %ecx,(%eax)
-        /* Get argv pointer in edx */
-        leal    4(%esp),%edx
-        /* Save argv */
-        movl    operatingsystem_parameter_argv@GOT(%ebx),%eax
-        movl    %edx,(%eax)
-        /* The start of the environment is: esp+ecx*4+12 */
-        leal    12(%esp,%ecx,4),%edx
-        /* Save envp */
-        movl    operatingsystem_parameter_envp@GOT(%ebx),%eax
-        movl    %edx,(%eax)
-        
-        /* Finally go to libc startup code. It will call "PASCALMAIN" via alias "main". */
-        /* No need to align stack since it will aligned by libc. */
-        jmp _start
-
-/* --------------------------------------------------------- */
-        .globl  _haltproc
-        .type   _haltproc,@function
-_haltproc:
-        /* GOT init */
-        call    fpc_geteipasebx
-        addl    $_GLOBAL_OFFSET_TABLE_,%ebx
-        /* Jump to libc exit(). _haltproc has the same declaration as exit. */
-        jmp     exit@PLT
-
-/* --------------------------------------------------------- */
-.data
-/* Define a symbol for the first piece of initialized data.  */
-        .globl __data_start
-__data_start:
-        .long 0
-        .weak data_start
-        data_start = __data_start
-
-/* --------------------------------------------------------- */
-.bss
-        .comm __stkptr,4
-        .comm operatingsystem_parameter_envp,4
-        .comm operatingsystem_parameter_argc,4
-        .comm operatingsystem_parameter_argv,4

+ 0 - 95
rtl/android/mipsel/dllprt0.as

@@ -1,95 +0,0 @@
-/*
-Library startup code for Android/MIPS
-by Vsevolod Alekseyev
-*/
-
-    .set noat
-    .section ".text"
-    .align 4
-
-    .global FPC_SHARED_LIB_START
-FPC_SHARED_LIB_START:
-    .ent FPC_SHARED_LIB_START
-    .frame $sp, 32, $ra
-    .set noreorder
-    .cpload $t9
-    /*
-    Stack structure:
-    sp+16 - gp
-    sp+20 - sp
-    sp+24 - ra
-    */
-
-    /* Save old sp, align the stack, set up the frame, save regs */
-    move $t0, $sp
-    li $t1, -8
-    and $sp, $sp, $t1
-    subu $sp, $sp, 32
-    .cprestore 16
-    sw $t0, 20($sp)
-    sw $ra, 24($sp)
-
-    /* Save initial stack pointer, return address */
-    la $t0, __stkptr
-    sw $sp, ($t0)
-
-    /* Get environment from libc */
-    la $t0, environ
-    lw $t0, ($t0)
-    bne $t0, $zero, GotEnv
-    nop
-    la $t0, EmptyEnv
-GotEnv:
-    la $t1, operatingsystem_parameter_envp
-    sw $t0, ($t1)
-
-    /* Call main */
-    jal FPC_LIB_MAIN_ANDROID
-    nop
-    /* Call library init */
-    jal FPC_LIB_INIT_ANDROID
-    nop
-
-    /* restore registers, exit */
-    lw $ra, 24($sp)
-    jr $ra
-    lw $sp, 20($sp)
-    .end FPC_SHARED_LIB_START
-    .size FPC_SHARED_LIB_START, .-FPC_SHARED_LIB_START
-
-/*************************/
-
-    .global _haltproc
-_haltproc:
-    .ent _haltproc
-    .set noreorder
-    .cpload $t9
-    la $t9, exit
-    jr $t9
-    nop
-    .end _haltproc
-    .size _haltproc, .-_haltproc
-
-/*************************/
-
-    .comm __stkptr,4
-    .comm operatingsystem_parameter_envp,4
-operatingsystem_parameter_argc:
-    .global operatingsystem_parameter_argc
-    .long 1
-operatingsystem_parameter_argv:
-    .global operatingsystem_parameter_argv
-    .long EmptyCmdLine
-EmptyCmdLine:
-    .long EmptyCmdStr
-EmptyCmdStr:
-    .ascii "\0"
-
-EmptyEnv:
-    .long 0
-    .long 0
-    .long 0
-
-/* --------------------------------------------------------- */
-   	.section .init_array, "aw"
-    .long FPC_SHARED_LIB_START

+ 0 - 57
rtl/android/mipsel/prt0.as

@@ -1,57 +0,0 @@
-/*
-Startup code for Android/MIPS
-by Vsevolod Alekseyev
-*/
-
-    .set at
-    .section ".text"
-    .align 4
-
-    .global _fpc_start
-_fpc_start:
-    .ent _fpc_start
-    .set noreorder
-    .cpload $t9
-
-    /*Align the stack, save the old sp */
-    move $t0, $sp
-    li $t1, -8
-    and $sp, $sp, $t1
-    sw $t0, __stkptr
-
-    /* Get argc/argv/envp from the stack; old sp is in t0 */
-    lw $t1, ($t0)
-    sw $t1, (operatingsystem_parameter_argc)
-    addiu $t2, $t0, 4
-    sw $t2, (operatingsystem_parameter_argv)
-    addiu $t3, $t1, 1
-    sll $t3, $t3, 2
-    add $t2, $t2, $t3
-    sw $t2, (operatingsystem_parameter_envp)
-
-    la $t9, _start
-    jr $t9
-    nop
-    .end _fpc_start
-
-/*************************/
-
-    .global _haltproc
-_haltproc:
-    .ent _haltproc
-    .set noreorder
-    .cpload $t9
-    lw $a0, (operatingsystem_result)
-    la $t9, exit
-    jr $t9
-    nop
-    .end _haltproc
-    .size _haltproc, .-_haltproc
-
-/*************************/
-
-    .comm __stkptr,4
-
-    .comm operatingsystem_parameter_envp,4
-    .comm operatingsystem_parameter_argc,4
-    .comm operatingsystem_parameter_argv,4

+ 34 - 0
rtl/android/prt0.as

@@ -0,0 +1,34 @@
+#
+#   This file is part of the Free Pascal run time library.
+#   Copyright (c) 2018 by Yuriy Sydorov and other
+#   members of the Free Pascal development team.
+#
+#   See the file COPYING.FPC, included in this distribution,
+#   for details about the copyright.
+#
+#   This program 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.
+#
+#**********************************************************************}
+#
+# Program startup code for Free Pascal. Android target.
+#
+
+/* --------------------------------------------------------- */
+.data
+/* Define a symbol for the first piece of initialized data.  */
+  .globl __data_start
+__data_start:
+  .long 0
+  .weak data_start
+  data_start = __data_start
+
+/* --------------------------------------------------------- */
+  .section .init_array, "aw"
+
+.ifdef CPU64
+  .quad FPC_PROG_START_ANDROID
+.else
+  .long FPC_PROG_START_ANDROID
+.endif

+ 105 - 23
rtl/android/sysandroid.inc

@@ -1,6 +1,6 @@
 {
     This file is part of the Free Pascal run time library.
-    Copyright (c) 2015 by Yury Sidorov,
+    Copyright (c) 2015-2018 by Yuriy Sydorov,
     member of the Free Pascal development team.
 
     Android-specific part of the System unit.
@@ -13,6 +13,70 @@
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  **********************************************************************}
 
+var
+  __stkptr : Pointer; public name '__stkptr';
+  operatingsystem_parameter_envp : Pointer; public name 'operatingsystem_parameter_envp';
+  operatingsystem_parameter_argc : LongInt; public name 'operatingsystem_parameter_argc';
+  operatingsystem_parameter_argv : Pointer; public name 'operatingsystem_parameter_argv';
+
+  _environ: pointer external name 'environ';
+
+procedure CommonMainAndroid;
+const
+  EmptyEnv: array[0..2] of PAnsiChar = (nil, nil, nil);
+  EmptyCmdLine: array[0..0] of PAnsiChar = ( '' );
+var
+  i: longint;
+  p: PPAnsiChar;
+begin
+  // Get the current stack pointer, adjust and save it
+  __stkptr:=pointer(ptruint(Sptr) or $FFFF);
+  // Get the environment from the environ variable of libc
+  p:=_environ;
+  if p = nil then
+    operatingsystem_parameter_envp:=@EmptyEnv
+  else
+    begin
+      operatingsystem_parameter_envp:=p;
+      // Finding argc and argv. They are placed before envp
+      Dec(p);
+      if p^ = nil then
+        begin
+          i:=0;
+          while i < 200 do
+            begin
+              Dec(p);
+              if ptrint(p^) = i then
+                begin
+                  // argc found
+                  operatingsystem_parameter_argc:=i;
+                  operatingsystem_parameter_argv:=p + 1;
+                  break;
+                end;
+              Inc(i);
+            end;
+        end;
+    end;
+
+  if operatingsystem_parameter_argc = 0 then
+    begin
+      // argc and argv are not available
+      operatingsystem_parameter_argc:=1;
+      operatingsystem_parameter_argv:=@EmptyCmdLine;
+    end;
+end;
+
+// ************* Program startup code
+
+procedure ProgMainAndroid; cdecl; [public, alias:'FPC_PROG_START_ANDROID'];
+begin
+  CommonMainAndroid;
+end;
+
+// ************* Shared library startup code
+
+procedure LibMainAndroid; external name 'FPC_LIB_MAIN_ANDROID';
+
 procedure atexit(p: pointer); cdecl; external;
 
 var
@@ -44,8 +108,15 @@ begin
     end;
 end;
 
-procedure SysInitAndroidLib; [public, alias:'FPC_LIB_INIT_ANDROID'];
+// This procedure is called first when a shared library is loaded
+
+procedure AndroidLibStart; cdecl; [public, alias:'FPC_LIB_START_ANDROID'];
 begin
+  CommonMainAndroid;
+  // Call main code FPC_LIB_MAIN_ANDROID of the library.
+  // It points either to a standard PASCALMAIN or FPC_JNI_LIB_MAIN_ANDROID if JNI_OnLoad is exported by the library
+  // The linker makes all the magic.
+  LibMainAndroid;
   { Starting from Android 4.4 stdio handles are closed by libc prior to calling
     finalization routines of shared libraries. This causes a error while trying to
     writeln during library finalization and finally a crash because the error can
@@ -59,6 +130,38 @@ begin
   atexit(@SysAndroidLibExit);
 end;
 
+// ************* JNI init
+
+function JNI_OnLoad_Real(vm: pointer; reserved: pointer): longint;{$ifdef windows} stdcall {$else} cdecl {$endif}; external name 'FPC_JNI_ON_LOAD';
+procedure PascalMain; external name 'PASCALMAIN';
+
+// This proxy function is called when JVM calls the JNI_OnLoad() exported function
+function JNI_OnLoad_Proxy(vm: pointer; reserved: pointer): longint;{$ifdef windows} stdcall {$else} cdecl {$endif}; [public, alias:'FPC_JNI_ON_LOAD_PROXY'];
+begin
+  IsJniLibrary:=True;
+  // Call library initialization
+  PascalMain;
+  // Call user's JNI_OnLoad().
+  Result:=JNI_OnLoad_Real(vm, reserved);
+end;
+
+// This procedure is called instead of library initialization when JNI_OnLoad is exported
+procedure JniLibMain; [public, alias:'FPC_JNI_LIB_MAIN_ANDROID'];
+begin
+  // Must be empty.
+end;
+
+// ************* haltproc
+
+procedure _exit(e:longint); cdecl; external name 'exit';
+
+procedure _haltproc(e:longint);cdecl; [public, alias: {$if defined(CPUARM) and defined(FPC_ABI_EABI)}  '_haltproc_eabi' {$else} '_haltproc' {$endif}];
+begin
+  _exit(e);
+end;
+
+// ************* Misc functions
+
 function __system_property_get(name:Pchar; value:Pchar):longint;cdecl;external 'c' name '__system_property_get';
 
 function GetSystemProperty(Name: PAnsiChar): shortstring;
@@ -206,27 +309,6 @@ begin
   DefaultLogTag[len + 1]:=#0;
 end;
 
-// ************* JNI init
-
-function JNI_OnLoad_Real(vm: pointer; reserved: pointer): longint;{$ifdef windows} stdcall {$else} cdecl {$endif}; external name 'FPC_JNI_ON_LOAD';
-procedure PascalMain; external name 'PASCALMAIN';
-
-// This proxy function is called when JVM calls the JNI_OnLoad() exported function
-function JNI_OnLoad_Proxy(vm: pointer; reserved: pointer): longint;{$ifdef windows} stdcall {$else} cdecl {$endif}; [public, alias:'FPC_JNI_ON_LOAD_PROXY'];
-begin
-  IsJniLibrary:=True;
-  // Call library initialization
-  PascalMain;
-  // Call user's JNI_OnLoad().
-  Result:=JNI_OnLoad_Real(vm, reserved);
-end;
-
-// This procedure is called instead of library initialization when JNI_OnLoad is exported
-procedure JniLibMain; [public, alias:'FPC_JNI_LIB_MAIN_ANDROID'];
-begin
-  // Must be empty.
-end;
-
 // ************* System init
 
 procedure InitAndroid;