Including sysutil in embedded project crashes app
Original Reporter info from Mantis: MiR
-
Reporter name: Michael Ring
Original Reporter info from Mantis: MiR
- Reporter name: Michael Ring
Description:
Problem is that TEncoding uses Criticalsection:
class constructor TEncoding.Create;
var
E: TStandardEncoding;
begin
for E := Low(FStandardEncodings) to High(FStandardEncodings) do
FStandardEncodings[E] := nil;
{$ifdef FPC_HAS_FEATURE_THREADING}
InitCriticalSection(FLock);
{$endif}
end;
which needs an instanciated ThreadManager, but that one is initialized after TEncoding so we get a null pointer exception:
#0 RP2040_$$__HARDFAULT_HANDLER at arm/cortexm0p_start.inc:22
#1 <signal handler called> at :0
#2 ?? at :0
#3 INITCRITICALSECTION({__M_RESERVED = 0, __M_COUNT = 0, __M_OWNER = 0x0, __M_KIND = 0, __M_LOCK = {__STATUS = 0, __SPINLOCK = 0}}) at ../inc/thread.inc:230
#4 create at ../objpas/sysutils/sysencoding.inc:214
#5 SYSUTILS_$$_init$ at sysutils.pp:291
#6 FPC_INIT_FUNC_TABLE at :0
#7 main at i2c_ds3231.lpr:28
#8 STARTUP at arm/cortexm0p_start.inc:90
Here's the piece of code that is causing the exception, what I do not understand is where the dependency comes from, it looks injected by the compiler:
../objpas/sysutils/sysutils.inc:667
1000C230 0ab0 add sp, #40 ; 0x28
1000C232 30bd pop {r4, r5, pc}
1000C234 b406 lsls r4, r6, #26
1000C236 0020 movs r0, #0
1000C238 00b5 push {lr}
1000C23A 88b0 sub sp, #32
1000C23C fff7e2f9 bl 0x1000b604 &LtPos;SYSUTILS_
TENCODING___
$_$create>
1000C240 fff780ff bl 0x1000c144 &LtPos;SYSUTILS_$$_INITEXCEPTIONS>
1000C244 08b0 add sp, #32
1000C246 00bd pop {pc}
Steps to reproduce:
Build a simple elf file for arm-embedded (see attachment)
and then run:
arm-none-eabi-objdump project1.elf -d >somefile.txt
then open the file and search for
SYSUTILS_$_init
0800bf94 <SYSUTILS_$_init
>:
800bf94: b500 push {lr}
800bf96: b088 sub sp, #32
800bf98: f7ff f9e2 bl 800b360 <SYSUTILS$_$TENCODING_$__$$_$create>
800bf9c: f7ff ff80 bl 800bea0 <SYSUTILS_$$_INITEXCEPTIONS>
800bfa0: b008 add sp, #32
800bfa2: bd00 pop {pc}
there you will find the wrong initialization order
Mantis conversion info:
- Mantis ID: 38864
- OS: embedded
- OS Build: trunk
- Build: 49341
- Platform: embedded
- Version: 3.3.1
- Monitored by: » @ccrause (Christo Crause)