2012-09-27 04:11:54 +08:00
|
|
|
/* Copyright (c) 2009, 2010, 2011, 2012 ARM Ltd. All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
1. Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
|
|
|
3. The name of the company may not be used to endorse or promote
|
|
|
|
products derived from this software without specific prior written
|
|
|
|
permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
|
|
|
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
|
|
|
|
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
|
|
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
|
|
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
|
|
|
|
|
|
|
|
#include "newlib.h"
|
|
|
|
#include "svc.h"
|
|
|
|
|
|
|
|
/* ANSI concatenation macros. */
|
|
|
|
#define CONCAT(a, b) CONCAT2(a, b)
|
|
|
|
#define CONCAT2(a, b) a ## b
|
|
|
|
|
|
|
|
#ifdef __USER_LABEL_PREFIX__
|
|
|
|
#define FUNCTION( name ) CONCAT (__USER_LABEL_PREFIX__, name)
|
|
|
|
#else
|
|
|
|
#error __USER_LABEL_PREFIX is not defined
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_INITFINI_ARRAY
|
|
|
|
#define _init __libc_init_array
|
|
|
|
#define _fini __libc_fini_array
|
2013-06-25 20:06:15 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* In ELF64, the large addressing model is used and R_AARCH64_ABS64
|
|
|
|
reloc is generated to relocate a 64-bit address. Since 64-bit
|
|
|
|
relocation is not available in ELF32, in order to have
|
|
|
|
a single code path for both ELF64 and ELF32 classes, we synthesize
|
|
|
|
a 64-bit relocation by using R_AARCH64_P32_ABS32 on one of the two
|
|
|
|
.word directives, depending on the endianness. */
|
|
|
|
|
|
|
|
.macro GEN_DWORD name
|
|
|
|
#if defined(__ILP32__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
|
|
|
.word \name
|
|
|
|
.word 0
|
|
|
|
#elif defined(__ILP32__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
|
|
|
|
.word 0
|
|
|
|
.word \name
|
|
|
|
#else
|
|
|
|
.dword \name
|
|
|
|
#endif
|
|
|
|
.endm
|
|
|
|
|
|
|
|
/* Help tackle the pointer size difference between ELF64 and ELF32. */
|
|
|
|
#ifdef __ILP32__
|
|
|
|
#define PTR_REG(n) w##n
|
|
|
|
#define PTR_SIZE 4
|
|
|
|
#define PTR_LOG_SIZE 2
|
|
|
|
#else
|
|
|
|
#define PTR_REG(n) x##n
|
|
|
|
#define PTR_SIZE 8
|
|
|
|
#define PTR_LOG_SIZE 3
|
2012-09-27 04:11:54 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
.text
|
|
|
|
.macro FUNC_START name
|
|
|
|
.global \name
|
|
|
|
\name:
|
|
|
|
.endm
|
|
|
|
|
|
|
|
.align 2
|
|
|
|
|
|
|
|
FUNC_START _mainCRTStartup
|
|
|
|
FUNC_START _start
|
|
|
|
|
|
|
|
/* Start by setting up a stack */
|
2017-08-11 21:47:02 +08:00
|
|
|
#ifdef ARM_RDI_MONITOR
|
2012-09-27 04:11:54 +08:00
|
|
|
/* Issue Angel SVC to read memory info.
|
|
|
|
|
|
|
|
ptr to ptr to 4 words to receive data. */
|
|
|
|
adr x1, .LC0
|
|
|
|
mov w0, #AngelSVC_Reason_HeapInfo
|
|
|
|
AngelSVCAsm AngelSVC
|
|
|
|
|
|
|
|
/* Initialise the stack pointer */
|
|
|
|
|
|
|
|
/* We currently choose to use the heap_limit field rather than
|
|
|
|
stack_base because the AEM validation model
|
|
|
|
returns sane values in the heap fields, but 0 in the stack
|
|
|
|
fields. Note on the VE AEM model it is necessary to pass
|
|
|
|
command line options to the AEM in order to define the values
|
|
|
|
exposed here in the HeapInfo Angel call. */
|
|
|
|
ldr x0, .LC0 /* point at returned values */
|
2013-06-25 20:06:15 +08:00
|
|
|
ldr x1, [x0, #8] /* get heap_limit */
|
2017-08-22 22:06:54 +08:00
|
|
|
|
2017-10-11 19:52:20 +08:00
|
|
|
/* Set __heap_limit. */
|
|
|
|
#ifdef __ILP32__
|
|
|
|
/* Sanity check on the __heap_limit. */
|
|
|
|
tst x1, #0xffffffff00000000
|
|
|
|
bne .Linsanepar
|
|
|
|
#endif
|
|
|
|
cmp x1, xzr
|
|
|
|
beq .LC4
|
|
|
|
adrp x2, __heap_limit
|
|
|
|
add x2, x2, #:lo12:__heap_limit
|
|
|
|
str x1, [x2]
|
|
|
|
.LC4:
|
|
|
|
|
|
|
|
ldr x1, [x0] /* get heap_base */
|
2013-06-25 20:06:15 +08:00
|
|
|
#ifdef __ILP32__
|
|
|
|
/* Sanity check on the heap base. */
|
|
|
|
tst x1, #0xffffffff00000000
|
2017-10-11 19:52:20 +08:00
|
|
|
bne .Linsanepar
|
2013-06-25 20:06:15 +08:00
|
|
|
#endif
|
2017-10-11 19:52:20 +08:00
|
|
|
cmp x1, xzr
|
|
|
|
bne .LC5
|
|
|
|
/* If the heap base value [x0, #0] is 0 then the heap base is actually
|
|
|
|
at the end of program data (i.e. __end__) */
|
|
|
|
ldr x1, .LC3
|
|
|
|
str x1, [x0, #0]
|
|
|
|
.LC5:
|
|
|
|
ldr x1, [x0, #16] /* get stack_base */
|
|
|
|
|
|
|
|
#ifdef __ILP32__
|
|
|
|
/* Sanity check on the stack_base. */
|
|
|
|
tst x1, #0xffffffff00000000
|
|
|
|
bne .Linsanepar
|
|
|
|
#endif
|
|
|
|
cmp x1, xzr
|
|
|
|
bne .LC6
|
2017-08-22 22:06:54 +08:00
|
|
|
#endif
|
2017-10-11 19:52:20 +08:00
|
|
|
ldr x1, .Lstack /* Set up the stack pointer to a fixed value */
|
|
|
|
.LC6:
|
2012-09-27 04:11:54 +08:00
|
|
|
|
|
|
|
/* Ensure quad-word stack alignment. */
|
2013-06-25 20:06:15 +08:00
|
|
|
and x0, x1, #~15
|
2012-09-27 04:11:54 +08:00
|
|
|
mov sp, x0
|
|
|
|
|
|
|
|
/* Setup an initial dummy frame with saved fp=0 and saved lr=0 */
|
|
|
|
mov x29, 0
|
|
|
|
stp x29, x29, [sp, #-16]!
|
|
|
|
mov x29, sp
|
|
|
|
|
|
|
|
/* Initialize exception vector table, flatmap, etc. */
|
|
|
|
bl FUNCTION (_cpu_init_hook)
|
|
|
|
|
|
|
|
/* Zero the memory in the .bss section. */
|
|
|
|
ldr x0, .LC1 /* First arg: start of memory block */
|
|
|
|
mov w1, #0 /* Second arg: fill value */
|
|
|
|
ldr x2, .LC2
|
|
|
|
sub x2, x2, x0 /* Third arg: length of block */
|
|
|
|
bl FUNCTION (memset)
|
|
|
|
|
2017-07-13 18:52:42 +08:00
|
|
|
#ifdef ARM_RDI_MONITOR
|
2012-09-27 04:11:54 +08:00
|
|
|
/* Need to set up standard file handles */
|
|
|
|
bl FUNCTION (initialise_monitor_handles)
|
2017-07-13 18:52:42 +08:00
|
|
|
#endif
|
2012-09-27 04:11:54 +08:00
|
|
|
|
|
|
|
/* .init and .fini sections are used to create constructors
|
|
|
|
and destructors. Here we call the _init function and arrange
|
|
|
|
for _fini to be called at program exit. */
|
|
|
|
ldr x0, .Lfini
|
|
|
|
bl FUNCTION (atexit)
|
|
|
|
|
|
|
|
bl FUNCTION (_init)
|
|
|
|
|
2017-08-09 22:37:57 +08:00
|
|
|
#ifdef ARM_RDI_MONITOR
|
2012-09-27 04:11:54 +08:00
|
|
|
/* Fetch and parse the command line. */
|
2017-04-18 23:50:16 +08:00
|
|
|
ldr x1, .Lcmdline /* Command line descriptor. */
|
2012-09-27 04:11:54 +08:00
|
|
|
mov w0, #AngelSVC_Reason_GetCmdLine
|
|
|
|
AngelSVCAsm AngelSVC
|
|
|
|
ldr x8, .Lcmdline
|
2017-04-18 23:50:16 +08:00
|
|
|
ldr x8, [x8]
|
2012-09-27 04:11:54 +08:00
|
|
|
|
|
|
|
mov x0, #0 /* argc */
|
|
|
|
mov x1, sp /* argv */
|
|
|
|
ldr x2, .Lenvp /* envp */
|
|
|
|
|
|
|
|
/* Put NULL at end of argv array. */
|
2013-06-25 20:06:15 +08:00
|
|
|
str PTR_REG (0), [x1, #-PTR_SIZE]!
|
2012-09-27 04:11:54 +08:00
|
|
|
|
|
|
|
/* Skip leading blanks. */
|
|
|
|
.Lnext: ldrb w3, [x8], #1
|
|
|
|
cbz w3, .Lendstr
|
|
|
|
cmp w3, #' '
|
|
|
|
b.eq .Lnext
|
|
|
|
|
|
|
|
mov w4, #' ' /* Terminator is space. */
|
|
|
|
|
|
|
|
/* See whether we are scanning a quoted string by checking for
|
|
|
|
opening quote (" or '). */
|
|
|
|
subs w9, w3, #'\"'
|
|
|
|
sub x8, x8, #1 /* Backup if no match. */
|
|
|
|
ccmp w9, #('\'' - '\"'), 0x4 /* FLG_Z */, ne
|
|
|
|
csel w4, w3, w4, eq /* Terminator = quote if match. */
|
|
|
|
cinc x8, x8, eq
|
|
|
|
|
|
|
|
/* Push arg pointer to argv, and bump argc. */
|
2013-06-25 20:06:15 +08:00
|
|
|
str PTR_REG (8), [x1, #-PTR_SIZE]!
|
2012-09-27 04:11:54 +08:00
|
|
|
add x0, x0, #1
|
|
|
|
|
|
|
|
/* Find end of arg string. */
|
|
|
|
1: ldrb w3, [x8], #1
|
|
|
|
cbz w3, .Lendstr
|
|
|
|
cmp w4, w3 /* Reached terminator? */
|
|
|
|
b.ne 1b
|
|
|
|
|
|
|
|
/* Terminate the arg string with NUL char. */
|
|
|
|
mov w4, #0
|
|
|
|
strb w4, [x8, #-1]
|
|
|
|
b .Lnext
|
|
|
|
|
|
|
|
/* Reverse argv array. */
|
|
|
|
.Lendstr:
|
2013-06-25 20:06:15 +08:00
|
|
|
add x3, x1, #0 /* sp = &argv[0] */
|
|
|
|
add x4, x1, w0, uxtw #PTR_LOG_SIZE /* ep = &argv[argc] */
|
2012-09-27 04:11:54 +08:00
|
|
|
cmp x4, x3
|
|
|
|
b.lo 2f
|
2013-06-25 20:06:15 +08:00
|
|
|
1: ldr PTR_REG (5), [x4, #-PTR_SIZE] /* PTR_REG (5) = ep[-1] */
|
|
|
|
ldr PTR_REG (6), [x3] /* PTR_REG (6) = *sp */
|
|
|
|
str PTR_REG (6), [x4, #-PTR_SIZE]! /* *--ep = PTR_REG (6) */
|
|
|
|
str PTR_REG (5), [x3], #PTR_SIZE /* *sp++ = PTR_REG (5) */
|
2012-09-27 04:11:54 +08:00
|
|
|
cmp x4, x3
|
|
|
|
b.hi 1b
|
|
|
|
2:
|
|
|
|
/* Move sp to the 16B boundary below argv. */
|
|
|
|
and x4, x1, ~15
|
|
|
|
mov sp, x4
|
|
|
|
|
2017-08-09 22:37:57 +08:00
|
|
|
#else
|
|
|
|
mov x0, #0 /* argc = 0 */
|
|
|
|
mov x1, #0 /* argv = NULL */
|
|
|
|
#endif
|
|
|
|
|
2012-09-27 04:11:54 +08:00
|
|
|
bl FUNCTION (main)
|
|
|
|
|
|
|
|
b FUNCTION (exit) /* Cannot return. */
|
|
|
|
|
2017-10-11 19:52:20 +08:00
|
|
|
#if defined (ARM_RDI_MONITOR) && defined (__ILP32__)
|
|
|
|
.Linsanepar:
|
|
|
|
/* Exit with 1 if the parameter is not within the 32-bit address
|
|
|
|
space. */
|
|
|
|
mov x1, ADP_Stopped_ApplicationExit & 0xff
|
|
|
|
movk x1, ADP_Stopped_ApplicationExit >> 16, lsl #16
|
|
|
|
adrp x0, HeapBase /* Reuse to construct the parameter block. */
|
|
|
|
add x0, x0, #:lo12:HeapBase
|
|
|
|
str x1, [x0]
|
|
|
|
mov x1, 1
|
|
|
|
str x1, [x0, #8]
|
|
|
|
mov w1, #AngelSVC_Reason_ReportException
|
|
|
|
AngelSVCAsm AngelSVC
|
|
|
|
b .
|
|
|
|
#endif
|
|
|
|
|
2012-09-27 04:11:54 +08:00
|
|
|
/* Function initializing exception vector table, flatmap, etc.
|
|
|
|
Declared as weak symbol so that user can override this definition
|
|
|
|
by linking in their own version of the function. */
|
|
|
|
.weak FUNCTION (_cpu_init_hook)
|
|
|
|
FUNCTION (_cpu_init_hook):
|
|
|
|
ret
|
|
|
|
|
|
|
|
.align 3
|
2017-08-15 22:01:05 +08:00
|
|
|
#ifdef ARM_RDI_MONITOR
|
2012-09-27 04:11:54 +08:00
|
|
|
.LC0:
|
2013-06-25 20:06:15 +08:00
|
|
|
GEN_DWORD HeapBase
|
2017-10-11 19:52:20 +08:00
|
|
|
.LC3:
|
|
|
|
GEN_DWORD __end__
|
|
|
|
#endif
|
2017-08-11 21:47:02 +08:00
|
|
|
.Lstack:
|
|
|
|
GEN_DWORD __stack
|
|
|
|
.weak __stack
|
2017-10-11 19:52:20 +08:00
|
|
|
|
2012-09-27 04:11:54 +08:00
|
|
|
.LC1:
|
2013-06-25 20:06:15 +08:00
|
|
|
GEN_DWORD __bss_start__
|
2012-09-27 04:11:54 +08:00
|
|
|
.LC2:
|
2013-06-25 20:06:15 +08:00
|
|
|
GEN_DWORD __bss_end__
|
2012-09-27 04:11:54 +08:00
|
|
|
.Lfini:
|
2013-06-25 20:06:15 +08:00
|
|
|
GEN_DWORD FUNCTION(_fini)
|
2017-08-11 21:47:02 +08:00
|
|
|
#ifdef ARM_RDI_MONITOR
|
2012-09-27 04:11:54 +08:00
|
|
|
.Lenvp:
|
2013-06-25 20:06:15 +08:00
|
|
|
GEN_DWORD env
|
2012-09-27 04:11:54 +08:00
|
|
|
.Lcmdline:
|
2017-04-18 23:50:16 +08:00
|
|
|
GEN_DWORD AngelSVCArgs
|
2012-09-27 04:11:54 +08:00
|
|
|
/* Workspace for Angel calls. */
|
|
|
|
.data
|
|
|
|
.align 3
|
|
|
|
/* Data returned by monitor SVC. */
|
2013-06-25 20:06:15 +08:00
|
|
|
#if defined(__ILP32__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
|
|
|
|
.set __stack_base__, StackBase + 4
|
|
|
|
#else
|
|
|
|
.set __stack_base__, StackBase
|
|
|
|
#endif
|
2012-09-27 04:11:54 +08:00
|
|
|
.global __stack_base__
|
|
|
|
HeapBase: .dword 0
|
|
|
|
HeapLimit: .dword 0
|
2013-06-25 20:06:15 +08:00
|
|
|
StackBase: .dword 0
|
2012-09-27 04:11:54 +08:00
|
|
|
StackLimit: .dword 0
|
|
|
|
env: .dword 0 /* Dummy environment array */
|
|
|
|
CommandLine: .space 256,0 /* Maximum length of 255 chars handled. */
|
2017-04-18 23:50:16 +08:00
|
|
|
AngelSVCArgs:
|
|
|
|
GEN_DWORD CommandLine
|
|
|
|
.dword 255
|
2017-08-11 21:47:02 +08:00
|
|
|
#endif
|