|
| 1 | +//===-- Implementation of crt for arm -------------------------------------===// |
| 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 "src/__support/macros/config.h" |
| 10 | +#include "src/stdlib/atexit.h" |
| 11 | +#include "src/stdlib/exit.h" |
| 12 | +#include "src/string/memcpy.h" |
| 13 | +#include "src/string/memset.h" |
| 14 | +#include "startup/baremetal/fini.h" |
| 15 | +#include "startup/baremetal/init.h" |
| 16 | + |
| 17 | +#include <stdint.h> |
| 18 | + |
| 19 | +extern "C" { |
| 20 | +int main(int argc, char **argv); |
| 21 | +void _start(); |
| 22 | + |
| 23 | +// Semihosting library initialisation if applicable. Required for printf, etc. |
| 24 | +[[gnu::weak]] void _platform_init() {} |
| 25 | + |
| 26 | +// These symbols are provided by the linker. The exact names are not defined by |
| 27 | +// a standard. |
| 28 | +extern uintptr_t __stack; |
| 29 | +extern uintptr_t __data_source[]; |
| 30 | +extern uintptr_t __data_start[]; |
| 31 | +extern uintptr_t __data_size[]; |
| 32 | +extern uintptr_t __bss_start[]; |
| 33 | +extern uintptr_t __bss_size[]; |
| 34 | + |
| 35 | +// Based on |
| 36 | +// https://developer.arm.com/documentation/107565/0101/Use-case-examples/Generic-Information/What-is-inside-a-program-image-/Vector-table |
| 37 | +void NMI_Handler() {} |
| 38 | +void HardFault_Handler() { LIBC_NAMESPACE::exit(1); } |
| 39 | +void MemManage_Handler() { LIBC_NAMESPACE::exit(1); } |
| 40 | +void BusFault_Handler() { LIBC_NAMESPACE::exit(1); } |
| 41 | +void UsageFault_Handler() { LIBC_NAMESPACE::exit(1); } |
| 42 | +void SVC_Handler() {} |
| 43 | +void DebugMon_Handler() {} |
| 44 | +void PendSV_Handler() {} |
| 45 | +void SysTick_Handler() {} |
| 46 | + |
| 47 | +// Architecturally the bottom 7 bits of VTOR are zero, meaning the vector table |
| 48 | +// has to be 128-byte aligned, however an implementation can require more bits |
| 49 | +// to be zero and Cortex-M23 can require up to 10, so 1024-byte align the vector |
| 50 | +// table. |
| 51 | +using HandlerType = void (*)(void); |
| 52 | +const HandlerType vector_table[] |
| 53 | + __attribute__((section(".vectors"), aligned(1024), used)) = { |
| 54 | + (HandlerType)&__stack, // SP |
| 55 | + _start, // Reset |
| 56 | + NMI_Handler, // NMI Handler |
| 57 | + HardFault_Handler, // Hard Fault Handlerß |
| 58 | + MemManage_Handler, // MPU Fault Han`dler |
| 59 | + BusFault_Handler, // Bus Fault Handler |
| 60 | + UsageFault_Handler, // Usage Fault Handler |
| 61 | + 0, // Reserved |
| 62 | + 0, // Reserved |
| 63 | + 0, // Reserved |
| 64 | + 0, // Reserved |
| 65 | + SVC_Handler, // SVC Handler |
| 66 | + DebugMon_Handler, // Debug Monitor Handler |
| 67 | + 0, // Reserved |
| 68 | + PendSV_Handler, // PendSV Handler |
| 69 | + SysTick_Handler, // SysTick Handler |
| 70 | + // Unused |
| 71 | +}; |
| 72 | +} // extern "C" |
| 73 | + |
| 74 | +namespace LIBC_NAMESPACE_DECL { |
| 75 | +[[noreturn]] void do_start() { |
| 76 | + // FIXME: set up the QEMU test environment |
| 77 | + |
| 78 | + // Perform the equivalent of scatterloading |
| 79 | + LIBC_NAMESPACE::memcpy(__data_start, __data_source, (uintptr_t)__data_size); |
| 80 | + LIBC_NAMESPACE::memset(__bss_start, '\0', (uintptr_t)__bss_size); |
| 81 | + __libc_init_array(); |
| 82 | + |
| 83 | + _platform_init(); |
| 84 | + LIBC_NAMESPACE::atexit(&__libc_fini_array); |
| 85 | + LIBC_NAMESPACE::exit(main(0, 0)); |
| 86 | +} |
| 87 | +} // namespace LIBC_NAMESPACE_DECL |
| 88 | + |
| 89 | +extern "C" void _start() { |
| 90 | + asm volatile("mov sp, %0" : : "r"(&__stack)); |
| 91 | + asm volatile("bl %0" : : "X"(LIBC_NAMESPACE::do_start)); |
| 92 | +} |
0 commit comments