1 //===-- Implementation of crt for aarch64 ---------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "config/linux/app.h"
10 #include "src/__support/OSUtil/syscall.h"
11 
12 #include <linux/auxvec.h>
13 #include <linux/elf.h>
14 #include <stdint.h>
15 #include <sys/syscall.h>
16 
17 extern "C" int main(int, char **, char **);
18 
19 // Source documentation:
20 // https://github.com/ARM-software/abi-aa/tree/main/sysvabi64
21 
22 namespace __llvm_libc {
23 
24 AppProperties app;
25 
26 } // namespace __llvm_libc
27 
28 using __llvm_libc::app;
29 
30 // TODO: Would be nice to use the aux entry structure from elf.h when available.
31 struct AuxEntry {
32   uint64_t type;
33   uint64_t value;
34 };
35 
36 extern "C" void _start() {
37   // Skip the Frame Pointer and the Link Register
38   // https://github.com/ARM-software/abi-aa/blob/main/aapcs64/aapcs64.rst
39   // Section 6.2.3
40   app.args = reinterpret_cast<__llvm_libc::Args *>(
41       reinterpret_cast<uintptr_t *>(__builtin_frame_address(0)) + 2);
42 
43   // After the argv array, is a 8-byte long NULL value before the array of env
44   // values. The end of the env values is marked by another 8-byte long NULL
45   // value. We step over it (the "+ 1" below) to get to the env values.
46   uint64_t *env_ptr = app.args->argv + app.args->argc + 1;
47   uint64_t *env_end_marker = env_ptr;
48   while (*env_end_marker)
49     ++env_end_marker;
50 
51   // After the env array, is the aux-vector. The end of the aux-vector is
52   // denoted by an AT_NULL entry.
53   for (AuxEntry *aux_entry = reinterpret_cast<AuxEntry *>(env_end_marker + 1);
54        aux_entry->type != AT_NULL; ++aux_entry) {
55     switch (aux_entry->type) {
56     case AT_PAGESZ:
57       app.pageSize = aux_entry->value;
58       break;
59     default:
60       break; // TODO: Read other useful entries from the aux vector.
61     }
62   }
63 
64   // TODO: Init TLS
65 
66   __llvm_libc::syscall(SYS_exit, main(app.args->argc,
67                                       reinterpret_cast<char **>(app.args->argv),
68                                       reinterpret_cast<char **>(env_ptr)));
69 }
70