1 /****************************************************************************** 2 * Copyright (c) 2022 Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK") 3 * All rights reserved. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 *****************************************************************************/ 18 19 #include <stdlib.h> 20 21 #include <los_compiler.h> 22 23 #include <soc.h> 24 25 #include "canary.h" 26 27 #ifdef __cplusplus 28 #if __cplusplus 29 extern "C" { 30 #endif /* __cplusplus */ 31 #endif /* __cplusplus */ 32 33 #ifdef __GNUC__ ArchGetTimerCnt(VOID)34STATIC UINT32 ArchGetTimerCnt(VOID) 35 { 36 UINT32 cntpct; 37 cntpct = READ_CSR(0xB00); /* MCYCLE REG Address */ 38 return cntpct; 39 } 40 41 /* 42 * If the SP compiling options:-fstack-protector-strong or -fstack-protector-all is enabled, 43 * We recommend to implement true random number generator function for __stack_chk_guard 44 * value to replace the function implementation template shown as below. 45 */ 46 #pragma GCC push_options 47 #pragma GCC optimize("-fno-stack-protector") ArchStackGuardInit(VOID)48LITE_OS_SEC_TEXT_INIT WEAK VOID ArchStackGuardInit(VOID) 49 { 50 int rnd; 51 UINT32 seed; 52 53 seed = ArchGetTimerCnt(); 54 srand(seed); 55 rnd = rand(); 56 __stack_chk_guard = (UINTPTR)rnd; 57 } 58 #pragma GCC pop_options 59 #endif 60 61 #ifdef __cplusplus 62 #if __cplusplus 63 } 64 #endif /* __cplusplus */ 65 #endif /* __cplusplus */ 66