#include <stdint.h>
#include <stdlib.h>
#include "stack_protection.h"

#ifdef ARM64
uint64_t __stack_chk_guard = 0;
#else // ARM32
uint32_t __stack_chk_guard = 0;
#endif /* ifdef ARM64 */

#ifdef NO_STACK_CHK_IMPLEMENTED
void stack_protection_init(void)
{
    size_t length = sizeof(__stack_chk_guard);

    tlApiRandomGenerateData(TLAPI_ALG_SECURE_RANDOM, (uint8_t *)&__stack_chk_guard, &length);
}

/*
 * Function that is called when stack corruption detected
 * It is referenced only by a compiler, so no need to declare it in a header
 * In case of specific chipset, already defined into TlEntry.lib.
 */
#ifndef STACK_CHK_FAIL_IMPLEMENTED
void __stack_chk_fail(void)
{
    abort();
}
#endif /* #ifndef STACK_CHK_FAIL_IMPLEMENTED */

#endif /* ifndef NO_STACK_CHK_IMPLEMENTED */
