system.pp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2000 by Marco van de Voort
  4. member of the Free Pascal development team.
  5. System unit for Linux.
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. { These things are set in the makefile, }
  13. { But you can override them here.}
  14. { If you use an aout system, set the conditional AOUT}
  15. { $Define AOUT}
  16. Unit System;
  17. {*****************************************************************************}
  18. interface
  19. {*****************************************************************************}
  20. {$define FPC_IS_SYSTEM}
  21. {$define HAS_CMDLINE}
  22. {$define USE_NOTHREADMANAGER}
  23. {$ifdef CPUM68K}
  24. {$define FPC_68K_SYSTEM_HAS_FPU_EXCEPTIONS}
  25. {$endif}
  26. {$i osdefs.inc}
  27. {$I sysunixh.inc}
  28. {$if defined(VER3_0) and defined(CPUX86_64)}
  29. {$define FPC_BOOTSTRAP_INDIRECT_ENTRY}
  30. const
  31. { this constant only exists during bootstrapping of the RTL with FPC 3.0.x,
  32. so that the whole condition doesn't need to be repeated in si_intf }
  33. indirect_bootstrap = true;
  34. {$endif defined(VER3_0) and defined(CPUX86_64)}
  35. function get_cmdline:Pchar; deprecated 'use paramstr' ;
  36. property cmdline:Pchar read get_cmdline;
  37. {$if defined(CPURISCV32) or defined(CPURISCV64) or defined(CPUARM) or defined(CPUM68K) or (defined(CPUSPARC) and defined(VER2_6))}
  38. {$define FPC_LOAD_SOFTFPU}
  39. {$endif defined(CPURISCV32) or defined(CPURISCV64) or defined(CPUARM) or defined(CPUM68K) or (defined(CPUSPARC) and defined(VER2_6))}
  40. {$ifdef FPC_SOFT_FPUX80}
  41. {$define FPC_SOFTFLOAT_FLOATX80}
  42. {$define LOAD_SOFTFPU}
  43. {$endif}
  44. {$ifdef FPC_SOFT_FPU128}
  45. {$define FPC_SOFTFLOAT_FLOAT128}
  46. {$define FPC_LOAD_SOFTFPU}
  47. {$endif}
  48. {$ifdef FPC_LOAD_SOFTFPU}
  49. {$define fpc_softfpu_interface}
  50. {$i softfpu.pp}
  51. {$undef fpc_softfpu_interface}
  52. {$endif FPC_LOAD_SOFTFPU}
  53. {$ifdef android}
  54. {$I sysandroidh.inc}
  55. {$endif android}
  56. {*****************************************************************************}
  57. implementation
  58. {*****************************************************************************}
  59. {$if defined(CPUI386) and not defined(FPC_USE_LIBC)}
  60. var
  61. sysenter_supported: LongInt = 0;
  62. {$endif}
  63. const
  64. calculated_cmdline:Pchar=nil;
  65. {$ifdef FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  66. {$define FPC_SYSTEM_HAS_OSSETUPENTRYINFORMATION}
  67. procedure OsSetupEntryInformation(constref info: TEntryInformation); forward;
  68. {$endif FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  69. {$ifdef FPC_LOAD_SOFTFPU}
  70. {$define fpc_softfpu_implementation}
  71. {$if defined(CPUM68K)}
  72. {$define softfpu_compiler_mul32to64}
  73. {$define softfpu_inline}
  74. {$endif}
  75. {$i softfpu.pp}
  76. {$undef fpc_softfpu_implementation}
  77. { we get these functions and types from the softfpu code }
  78. {$define FPC_SYSTEM_HAS_float64}
  79. {$define FPC_SYSTEM_HAS_float32}
  80. {$define FPC_SYSTEM_HAS_flag}
  81. {$define FPC_SYSTEM_HAS_extractFloat64Frac0}
  82. {$define FPC_SYSTEM_HAS_extractFloat64Frac1}
  83. {$define FPC_SYSTEM_HAS_extractFloat64Exp}
  84. {$define FPC_SYSTEM_HAS_extractFloat64Sign}
  85. {$define FPC_SYSTEM_HAS_ExtractFloat32Frac}
  86. {$define FPC_SYSTEM_HAS_extractFloat32Exp}
  87. {$define FPC_SYSTEM_HAS_extractFloat32Sign}
  88. {$endif FPC_LOAD_SOFTFPU}
  89. {$I system.inc}
  90. {$ifdef android}
  91. {$I sysandroid.inc}
  92. {$endif android}
  93. {*****************************************************************************
  94. TLS handling
  95. *****************************************************************************}
  96. {$if defined(CPUARM)}
  97. {$define INITTLS}
  98. Function fpset_tls(p : pointer;size : SizeUInt):cint;
  99. begin
  100. Result:=do_syscall(syscall_nr___ARM_NR_set_tls,TSysParam(p));
  101. end;
  102. {$endif defined(CPUARM)}
  103. {$if defined(CPUI386)}
  104. {$define INITTLS}
  105. Function fpset_tls(p : pointer;size : SizeUInt):cint;
  106. var
  107. desc : record
  108. entry_number : dword;
  109. base_addr : dword;
  110. limit : dword;
  111. flags : dword;
  112. end;
  113. selector : word;
  114. begin
  115. // get descriptor from the kernel
  116. desc.entry_number:=$ffffffff;
  117. // TLS is accessed by negative offsets, only the TCB pointer is at offset 0
  118. desc.base_addr:=dword(p)+size-SizeOf(Pointer);
  119. // 4 GB, limit is given in pages
  120. desc.limit:=$fffff;
  121. // seg_32bit:1, contents:0, read_exec_only:0, limit_in_pages:1, seg_not_present:0, useable:1
  122. desc.flags:=%1010001;
  123. Result:=do_syscall(syscall_nr_set_thread_area,TSysParam(@desc));
  124. if Result=0 then
  125. begin
  126. selector:=desc.entry_number*8+3;
  127. asm
  128. movw selector,%gs
  129. movl desc.base_addr,%eax
  130. movl %eax,%gs:0
  131. end;
  132. end;
  133. end;
  134. {$endif defined(CPUI386)}
  135. {$if defined(CPUX86_64)}
  136. {$define INITTLS}
  137. const
  138. ARCH_SET_FS = $1002;
  139. Function fpset_tls(p : pointer;size : SizeUInt):cint;
  140. begin
  141. p:=pointer(qword(p)+size-SizeOf(Pointer));
  142. Result:=do_syscall(syscall_nr_arch_prctl,TSysParam(ARCH_SET_FS),TSysParam(p));
  143. if Result=0 then
  144. begin
  145. asm
  146. movq p,%rax
  147. movq %rax,%fs:0
  148. end;
  149. end;
  150. end;
  151. {$endif defined(CPUX86_64)}
  152. {$ifdef INITTLS}
  153. { This code initialized the TLS segment for single threaded and static programs.
  154. In case of multithreaded and/or dynamically linked programs it is assumed that they
  155. dependent anyways on glibc which initializes tls properly.
  156. As soon as a purely FPC dynamic loading or multithreading is implemented, the code
  157. needs to be extended to handle these cases as well.
  158. }
  159. procedure InitTLS; [public,alias:'FPC_INITTLS'];
  160. const
  161. PT_TLS = 7;
  162. PT_DYNAMIC = 2;
  163. type
  164. {$ifdef CPU64}
  165. tphdr = record
  166. p_type,
  167. p_flags : dword;
  168. p_offset,
  169. p_vaddr,
  170. p_paddr,
  171. p_filesz,
  172. p_memsz,
  173. p_align : qword;
  174. end;
  175. {$else CPU64}
  176. tphdr = record
  177. p_type,
  178. p_offset,
  179. p_vaddr,
  180. p_paddr,
  181. p_filesz,
  182. p_memsz,
  183. p_flags,
  184. p_align : dword;
  185. end;
  186. {$endif CPU64}
  187. pphdr = ^tphdr;
  188. var
  189. phdr : pphdr;
  190. phnum : dword;
  191. i : integer;
  192. tls : pointer;
  193. auxp : ppointer;
  194. found : boolean;
  195. size : SizeUInt;
  196. begin
  197. auxp:=ppointer(envp);
  198. { skip environment }
  199. while assigned(auxp^) do
  200. inc(auxp);
  201. inc(auxp);
  202. phdr:=nil;
  203. phnum:=0;
  204. { now we are at the auxillary vector }
  205. while assigned(auxp^) do
  206. begin
  207. case plongint(auxp)^ of
  208. 3:
  209. phdr:=pphdr(ppointer(auxp+1)^);
  210. 5:
  211. phnum:=pdword(auxp+1)^;
  212. end;
  213. inc(auxp,2);
  214. end;
  215. found:=false;
  216. size:=0;
  217. for i:=1 to phnum do
  218. begin
  219. case phdr^.p_type of
  220. PT_TLS:
  221. begin
  222. found:=true;
  223. inc(size,phdr^.p_memsz);
  224. size:=Align(size,phdr^.p_align);
  225. end;
  226. PT_DYNAMIC:
  227. { if the program header contains a dynamic section, the program
  228. is linked dynamically so the dynamic linker takes care of the
  229. allocation of the TLS segment.
  230. We cannot allocate it by ourself anyways as PT_TLS provides only
  231. the size of TLS data of the executable itself
  232. }
  233. exit;
  234. end;
  235. inc(phdr);
  236. end;
  237. if found then
  238. begin
  239. {$ifdef CPUI386}
  240. { threadvars should start at a page boundary,
  241. add extra space for the TCB }
  242. size:=Align(size,4096)+sizeof(Pointer);
  243. {$endif CPUI386}
  244. {$ifdef CPUX86_64}
  245. { threadvars should start at a page boundary,
  246. add extra space for the TCB }
  247. size:=Align(size,4096)+sizeof(Pointer);
  248. {$endif CPUX86_64}
  249. tls:=Fpmmap(nil,size,3,MAP_PRIVATE+MAP_ANONYMOUS,-1,0);
  250. fpset_tls(tls,size);
  251. end;
  252. end;
  253. {$endif INITTLS}
  254. {*****************************************************************************
  255. Indirect Entry Point
  256. *****************************************************************************}
  257. {$ifdef FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  258. var
  259. initialstkptr : Pointer;
  260. procedure OsSetupEntryInformation(constref info: TEntryInformation);
  261. begin
  262. argc := info.OS.argc;
  263. argv := info.OS.argv;
  264. envp := info.OS.envp;
  265. initialstkptr := info.OS.stkptr;
  266. initialstklen := info.OS.stklen;
  267. end;
  268. { we need two variants here because TLS must be initialized by FPC only if no libc is linked however,
  269. InitTLS cannot be called from the start up files because when they are run, envp is not setup yet.}
  270. procedure SysEntry(constref info: TEntryInformation);[public,alias:'FPC_SysEntry'];
  271. begin
  272. SetupEntryInformation(info);
  273. {$ifdef cpui386}
  274. Set8087CW(Default8087CW);
  275. {$endif cpui386}
  276. info.PascalMain();
  277. end;
  278. procedure SysEntry_InitTLS(constref info: TEntryInformation);[public,alias:'FPC_SysEntry_InitTLS'];
  279. begin
  280. SetupEntryInformation(info);
  281. {$ifdef INITTLS}
  282. InitTLS;
  283. {$endif INITTLS}
  284. {$ifdef cpui386}
  285. Set8087CW(Default8087CW);
  286. {$endif cpui386}
  287. info.PascalMain();
  288. end;
  289. {$else}
  290. var
  291. {$ifndef FPC_BOOTSTRAP_INDIRECT_ENTRY}
  292. initialstkptr : Pointer;external name '__stkptr';
  293. {$else FPC_BOOTSTRAP_INDIRECT_ENTRY}
  294. initialstkptr : Pointer; public name '__stkptr';
  295. operatingsystem_parameter_envp : Pointer; public name 'operatingsystem_parameter_envp';
  296. operatingsystem_parameter_argc : LongInt; public name 'operatingsystem_parameter_argc';
  297. operatingsystem_parameter_argv : Pointer; public name 'operatingsystem_parameter_argv';
  298. { we need two variants here because TLS must be initialized by FPC only if no libc is linked however,
  299. InitTLS cannot be called from the start up files because when they are run, envp is not setup yet.}
  300. procedure SysEntry(constref info: TEntryInformation);[public,alias:'FPC_SysEntry'];
  301. begin
  302. initialstkptr := info.OS.stkptr;
  303. operatingsystem_parameter_envp := info.OS.envp;
  304. operatingsystem_parameter_argc := info.OS.argc;
  305. operatingsystem_parameter_argv := info.OS.argv;
  306. {$ifdef cpui386}
  307. Set8087CW(Default8087CW);
  308. {$endif cpui386}
  309. info.PascalMain();
  310. end;
  311. procedure SysEntry_InitTLS(constref info: TEntryInformation);[public,alias:'FPC_SysEntry_InitTLS'];
  312. begin
  313. initialstkptr := info.OS.stkptr;
  314. operatingsystem_parameter_envp := info.OS.envp;
  315. operatingsystem_parameter_argc := info.OS.argc;
  316. operatingsystem_parameter_argv := info.OS.argv;
  317. {$ifdef INITTLS}
  318. InitTLS;
  319. {$endif INITTLS}
  320. {$ifdef cpui386}
  321. Set8087CW(Default8087CW);
  322. {$endif cpui386}
  323. info.PascalMain();
  324. end;
  325. {$endif FPC_BOOTSTRAP_INDIRECT_ENTRY}
  326. {$if defined(CPUARM) and defined(FPC_ABI_EABI)}
  327. procedure haltproc(e:longint);cdecl;external name '_haltproc_eabi';
  328. {$else}
  329. procedure haltproc(e:longint);cdecl;external name '_haltproc';
  330. {$endif}
  331. {$endif FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  332. {*****************************************************************************
  333. Misc. System Dependent Functions
  334. *****************************************************************************}
  335. {$ifdef FPC_USE_LIBC}
  336. function FpPrCtl(options : cInt; const args : ptruint) : cint; cdecl; external clib name 'prctl';
  337. {$endif}
  338. procedure System_exit;
  339. begin
  340. {$ifdef FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  341. EntryInformation.OS.haltproc(ExitCode);
  342. {$else FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  343. haltproc(ExitCode);
  344. {$endif FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  345. End;
  346. Function ParamCount: Longint;
  347. Begin
  348. Paramcount:=argc-1
  349. End;
  350. {function BackPos(c:char; const s: shortstring): integer;
  351. var
  352. i: integer;
  353. Begin
  354. for i:=length(s) downto 0 do
  355. if s[i] = c then break;
  356. if i=0 then
  357. BackPos := 0
  358. else
  359. BackPos := i;
  360. end;}
  361. { variable where full path and filename and executable is stored }
  362. { is setup by the startup of the system unit. }
  363. var
  364. execpathstr : shortstring;
  365. procedure SysInitExecPath;
  366. var
  367. i : longint;
  368. begin
  369. execpathstr[0]:=#0;
  370. i:=Fpreadlink('/proc/self/exe',@execpathstr[1],high(execpathstr));
  371. { it must also be an absolute filename, linux 2.0 points to a memory
  372. location so this will skip that }
  373. if (i>0) and (execpathstr[1]='/') then
  374. execpathstr[0]:=char(i);
  375. end;
  376. function paramstr(l: longint) : string;
  377. begin
  378. { stricly conforming POSIX applications }
  379. { have the executing filename as argv[0] }
  380. if l=0 then
  381. begin
  382. if execpathstr='' then
  383. SysInitExecPath;
  384. paramstr := execpathstr;
  385. end
  386. else if (l < argc) then
  387. paramstr:=strpas(argv[l])
  388. else
  389. paramstr:='';
  390. end;
  391. Procedure Randomize;
  392. Begin
  393. randseed:=longint(Fptime(nil));
  394. End;
  395. {*****************************************************************************
  396. cmdline
  397. *****************************************************************************}
  398. procedure SetupCmdLine;
  399. var
  400. bufsize,
  401. len,j,
  402. size,i : longint;
  403. found : boolean;
  404. buf : pchar;
  405. procedure AddBuf;
  406. var
  407. p : Pchar;
  408. begin
  409. p:=SysGetmem(size+bufsize);
  410. move(calculated_cmdline^,p^,size);
  411. move(buf^,p[size],bufsize);
  412. inc(size,bufsize);
  413. sysfreemem(calculated_cmdline);
  414. calculated_cmdline:=p;
  415. bufsize:=0;
  416. end;
  417. begin
  418. if argc<=0 then
  419. exit;
  420. Buf:=SysGetMem(ARG_MAX);
  421. size:=0;
  422. bufsize:=0;
  423. i:=0;
  424. while (i<argc) do
  425. begin
  426. len:=strlen(argv[i]);
  427. if len>ARG_MAX-2 then
  428. len:=ARG_MAX-2;
  429. found:=false;
  430. for j:=1 to len do
  431. if argv[i][j]=' ' then
  432. begin
  433. found:=true;
  434. break;
  435. end;
  436. found:=found or (len=0); // also quote if len=0, bug 19114
  437. if bufsize+len>=ARG_MAX-2 then
  438. AddBuf;
  439. if found then
  440. begin
  441. buf[bufsize]:='"';
  442. inc(bufsize);
  443. end;
  444. if len>0 then
  445. begin
  446. move(argv[i]^,buf[bufsize],len);
  447. inc(bufsize,len);
  448. end;
  449. if found then
  450. begin
  451. buf[bufsize]:='"';
  452. inc(bufsize);
  453. end;
  454. if i<argc-1 then
  455. buf[bufsize]:=' '
  456. else
  457. buf[bufsize]:=#0;
  458. inc(bufsize);
  459. inc(i);
  460. end;
  461. AddBuf;
  462. SysFreeMem(buf);
  463. end;
  464. function get_cmdline:Pchar;
  465. begin
  466. if calculated_cmdline=nil then
  467. setupcmdline;
  468. get_cmdline:=calculated_cmdline;
  469. end;
  470. {*****************************************************************************
  471. SystemUnit Initialization
  472. *****************************************************************************}
  473. function reenable_signal(sig : longint) : boolean;
  474. var
  475. e : TSigSet;
  476. i,j : byte;
  477. olderrno: cint;
  478. begin
  479. fillchar(e,sizeof(e),#0);
  480. { set is 1 based PM }
  481. dec(sig);
  482. i:=sig mod (sizeof(cuLong) * 8);
  483. j:=sig div (sizeof(cuLong) * 8);
  484. e[j]:=1 shl i;
  485. { this routine is called from a signal handler, so must not change errno }
  486. olderrno:=geterrno;
  487. fpsigprocmask(SIG_UNBLOCK,@e,nil);
  488. reenable_signal:=geterrno=0;
  489. seterrno(olderrno);
  490. end;
  491. // signal handler is arch dependant due to processorexception to language
  492. // exception translation
  493. {$i sighnd.inc}
  494. procedure InstallDefaultSignalHandler(signum: longint; out oldact: SigActionRec); public name '_FPC_INSTALLDEFAULTSIGHANDLER';
  495. var
  496. act: SigActionRec;
  497. begin
  498. { Initialize the sigaction structure }
  499. { all flags and information set to zero }
  500. FillChar(act, sizeof(SigActionRec),0);
  501. { initialize handler }
  502. act.sa_handler := SigActionHandler(@SignalToRunError);
  503. act.sa_flags:=SA_SIGINFO;
  504. FpSigAction(signum,@act,@oldact);
  505. end;
  506. var
  507. oldsigfpe: SigActionRec; public name '_FPC_OLDSIGFPE';
  508. oldsigsegv: SigActionRec; public name '_FPC_OLDSIGSEGV';
  509. oldsigbus: SigActionRec; public name '_FPC_OLDSIGBUS';
  510. oldsigill: SigActionRec; public name '_FPC_OLDSIGILL';
  511. Procedure InstallSignals;
  512. begin
  513. InstallDefaultSignalHandler(SIGFPE,oldsigfpe);
  514. InstallDefaultSignalHandler(SIGSEGV,oldsigsegv);
  515. InstallDefaultSignalHandler(SIGBUS,oldsigbus);
  516. InstallDefaultSignalHandler(SIGILL,oldsigill);
  517. end;
  518. procedure SysInitStdIO;
  519. begin
  520. OpenStdIO(Input,fmInput,StdInputHandle);
  521. OpenStdIO(Output,fmOutput,StdOutputHandle);
  522. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  523. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  524. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  525. {$ifdef android}
  526. InitStdIOAndroid;
  527. {$endif android}
  528. end;
  529. Procedure RestoreOldSignalHandlers;
  530. begin
  531. FpSigAction(SIGFPE,@oldsigfpe,nil);
  532. FpSigAction(SIGSEGV,@oldsigsegv,nil);
  533. FpSigAction(SIGBUS,@oldsigbus,nil);
  534. FpSigAction(SIGILL,@oldsigill,nil);
  535. end;
  536. function GetProcessID: SizeUInt;
  537. begin
  538. GetProcessID := SizeUInt (fpGetPID);
  539. end;
  540. {$ifdef FPC_USE_LIBC}
  541. {$ifdef HAS_UGETRLIMIT}
  542. { there is no ugetrlimit libc call, just map it to the getrlimit call in these cases }
  543. function FpUGetRLimit(resource : cInt; rlim : PRLimit) : cInt; cdecl; external clib name 'getrlimit';
  544. {$endif}
  545. {$endif}
  546. {$if defined(CPUPOWERPC) or defined(CPUPOWERPC64)}
  547. const
  548. page_size = $10000;
  549. {$define LAST_PAGE_GENERATES_SIGNAL}
  550. {$else}
  551. const
  552. page_size = $1000;
  553. {$endif}
  554. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  555. var
  556. limits : TRLimit;
  557. success : boolean;
  558. begin
  559. success := false;
  560. fillchar(limits, sizeof(limits), 0);
  561. {$ifdef has_ugetrlimit}
  562. success := fpugetrlimit(RLIMIT_STACK, @limits)=0;
  563. {$endif}
  564. {$ifndef NO_SYSCALL_GETRLIMIT}
  565. if (not success) then
  566. success := fpgetrlimit(RLIMIT_STACK, @limits)=0;
  567. {$endif}
  568. if (success) and (limits.rlim_cur < stklen) then
  569. result := limits.rlim_cur
  570. else
  571. result := stklen;
  572. end;
  573. {$if FPC_FULLVERSION>30200}
  574. {$if defined(CPUI386) or defined(CPUARM)}
  575. {$I abitag.inc}
  576. {$endif defined(CPUI386) or defined(CPUARM)}
  577. {$endif FPC_FULLVERSION>30200}
  578. begin
  579. {$if defined(i386) and not defined(FPC_USE_LIBC)}
  580. InitSyscallIntf;
  581. {$endif}
  582. {$ifndef FPUNONE}
  583. {$if defined(cpupowerpc)}
  584. // some PPC kernels set the exception bits FE0/FE1 in the MSR to zero,
  585. // disabling all FPU exceptions. Enable them again.
  586. fpprctl(PR_SET_FPEXC, PR_FP_EXC_PRECISE);
  587. {$endif}
  588. {$endif}
  589. IsConsole := TRUE;
  590. StackLength := CheckInitialStkLen(initialStkLen);
  591. StackBottom := pointer(ptruint((ptruint(initialstkptr) or (page_size - 1)) + 1 - StackLength));
  592. {$ifdef LAST_PAGE_GENERATES_SIGNAL}
  593. StackBottom:=StackBottom + page_size;
  594. {$endif}
  595. { Set up signals handlers (may be needed by init code to test cpu features) }
  596. InstallSignals;
  597. {$if defined(cpui386) or defined(cpuarm)}
  598. fpc_cpucodeinit;
  599. {$endif cpui386}
  600. { Setup heap }
  601. InitHeap;
  602. SysInitExceptions;
  603. initunicodestringmanager;
  604. { Setup stdin, stdout and stderr }
  605. SysInitStdIO;
  606. { Reset IO Error }
  607. InOutRes:=0;
  608. { threading }
  609. InitSystemThreads;
  610. { dynamic libraries }
  611. InitSystemDynLibs;
  612. {$ifdef android}
  613. InitAndroid;
  614. {$endif android}
  615. { restore original signal handlers in case this is a library }
  616. if IsLibrary then
  617. RestoreOldSignalHandlers;
  618. end.