system.pp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  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. function paramstr(l: longint) : string;
  366. begin
  367. { stricly conforming POSIX applications }
  368. { have the executing filename as argv[0] }
  369. if l=0 then
  370. begin
  371. paramstr := execpathstr;
  372. end
  373. else if (l < argc) then
  374. paramstr:=strpas(argv[l])
  375. else
  376. paramstr:='';
  377. end;
  378. Procedure Randomize;
  379. Begin
  380. randseed:=longint(Fptime(nil));
  381. End;
  382. {*****************************************************************************
  383. cmdline
  384. *****************************************************************************}
  385. procedure SetupCmdLine;
  386. var
  387. bufsize,
  388. len,j,
  389. size,i : longint;
  390. found : boolean;
  391. buf : pchar;
  392. procedure AddBuf;
  393. var
  394. p : Pchar;
  395. begin
  396. p:=SysGetmem(size+bufsize);
  397. move(calculated_cmdline^,p^,size);
  398. move(buf^,p[size],bufsize);
  399. inc(size,bufsize);
  400. sysfreemem(calculated_cmdline);
  401. calculated_cmdline:=p;
  402. bufsize:=0;
  403. end;
  404. begin
  405. if argc<=0 then
  406. exit;
  407. Buf:=SysGetMem(ARG_MAX);
  408. size:=0;
  409. bufsize:=0;
  410. i:=0;
  411. while (i<argc) do
  412. begin
  413. len:=strlen(argv[i]);
  414. if len>ARG_MAX-2 then
  415. len:=ARG_MAX-2;
  416. found:=false;
  417. for j:=1 to len do
  418. if argv[i][j]=' ' then
  419. begin
  420. found:=true;
  421. break;
  422. end;
  423. found:=found or (len=0); // also quote if len=0, bug 19114
  424. if bufsize+len>=ARG_MAX-2 then
  425. AddBuf;
  426. if found then
  427. begin
  428. buf[bufsize]:='"';
  429. inc(bufsize);
  430. end;
  431. if len>0 then
  432. begin
  433. move(argv[i]^,buf[bufsize],len);
  434. inc(bufsize,len);
  435. end;
  436. if found then
  437. begin
  438. buf[bufsize]:='"';
  439. inc(bufsize);
  440. end;
  441. if i<argc-1 then
  442. buf[bufsize]:=' '
  443. else
  444. buf[bufsize]:=#0;
  445. inc(bufsize);
  446. inc(i);
  447. end;
  448. AddBuf;
  449. SysFreeMem(buf);
  450. end;
  451. function get_cmdline:Pchar;
  452. begin
  453. if calculated_cmdline=nil then
  454. setupcmdline;
  455. get_cmdline:=calculated_cmdline;
  456. end;
  457. {*****************************************************************************
  458. SystemUnit Initialization
  459. *****************************************************************************}
  460. function reenable_signal(sig : longint) : boolean;
  461. var
  462. e : TSigSet;
  463. i,j : byte;
  464. olderrno: cint;
  465. begin
  466. fillchar(e,sizeof(e),#0);
  467. { set is 1 based PM }
  468. dec(sig);
  469. i:=sig mod (sizeof(cuLong) * 8);
  470. j:=sig div (sizeof(cuLong) * 8);
  471. e[j]:=1 shl i;
  472. { this routine is called from a signal handler, so must not change errno }
  473. olderrno:=geterrno;
  474. fpsigprocmask(SIG_UNBLOCK,@e,nil);
  475. reenable_signal:=geterrno=0;
  476. seterrno(olderrno);
  477. end;
  478. // signal handler is arch dependant due to processorexception to language
  479. // exception translation
  480. {$i sighnd.inc}
  481. procedure InstallDefaultSignalHandler(signum: longint; out oldact: SigActionRec); public name '_FPC_INSTALLDEFAULTSIGHANDLER';
  482. var
  483. act: SigActionRec;
  484. begin
  485. { Initialize the sigaction structure }
  486. { all flags and information set to zero }
  487. FillChar(act, sizeof(SigActionRec),0);
  488. { initialize handler }
  489. act.sa_handler := SigActionHandler(@SignalToRunError);
  490. act.sa_flags:=SA_SIGINFO;
  491. FpSigAction(signum,@act,@oldact);
  492. end;
  493. var
  494. oldsigfpe: SigActionRec; public name '_FPC_OLDSIGFPE';
  495. oldsigsegv: SigActionRec; public name '_FPC_OLDSIGSEGV';
  496. oldsigbus: SigActionRec; public name '_FPC_OLDSIGBUS';
  497. oldsigill: SigActionRec; public name '_FPC_OLDSIGILL';
  498. Procedure InstallSignals;
  499. begin
  500. InstallDefaultSignalHandler(SIGFPE,oldsigfpe);
  501. InstallDefaultSignalHandler(SIGSEGV,oldsigsegv);
  502. InstallDefaultSignalHandler(SIGBUS,oldsigbus);
  503. InstallDefaultSignalHandler(SIGILL,oldsigill);
  504. end;
  505. procedure SysInitStdIO;
  506. begin
  507. OpenStdIO(Input,fmInput,StdInputHandle);
  508. OpenStdIO(Output,fmOutput,StdOutputHandle);
  509. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  510. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  511. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  512. {$ifdef android}
  513. InitStdIOAndroid;
  514. {$endif android}
  515. end;
  516. Procedure RestoreOldSignalHandlers;
  517. begin
  518. FpSigAction(SIGFPE,@oldsigfpe,nil);
  519. FpSigAction(SIGSEGV,@oldsigsegv,nil);
  520. FpSigAction(SIGBUS,@oldsigbus,nil);
  521. FpSigAction(SIGILL,@oldsigill,nil);
  522. end;
  523. procedure SysInitExecPath;
  524. var
  525. i : longint;
  526. begin
  527. execpathstr[0]:=#0;
  528. i:=Fpreadlink('/proc/self/exe',@execpathstr[1],high(execpathstr));
  529. { it must also be an absolute filename, linux 2.0 points to a memory
  530. location so this will skip that }
  531. if (i>0) and (execpathstr[1]='/') then
  532. execpathstr[0]:=char(i);
  533. end;
  534. function GetProcessID: SizeUInt;
  535. begin
  536. GetProcessID := SizeUInt (fpGetPID);
  537. end;
  538. {$ifdef FPC_USE_LIBC}
  539. {$ifdef HAS_UGETRLIMIT}
  540. { there is no ugetrlimit libc call, just map it to the getrlimit call in these cases }
  541. function FpUGetRLimit(resource : cInt; rlim : PRLimit) : cInt; cdecl; external clib name 'getrlimit';
  542. {$endif}
  543. {$endif}
  544. {$if defined(CPUPOWERPC) or defined(CPUPOWERPC64)}
  545. const
  546. page_size = $10000;
  547. {$define LAST_PAGE_GENERATES_SIGNAL}
  548. {$else}
  549. const
  550. page_size = $1000;
  551. {$endif}
  552. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  553. var
  554. limits : TRLimit;
  555. success : boolean;
  556. begin
  557. success := false;
  558. fillchar(limits, sizeof(limits), 0);
  559. {$ifdef has_ugetrlimit}
  560. success := fpugetrlimit(RLIMIT_STACK, @limits)=0;
  561. {$endif}
  562. {$ifndef NO_SYSCALL_GETRLIMIT}
  563. if (not success) then
  564. success := fpgetrlimit(RLIMIT_STACK, @limits)=0;
  565. {$endif}
  566. if (success) and (limits.rlim_cur < stklen) then
  567. result := limits.rlim_cur
  568. else
  569. result := stklen;
  570. end;
  571. {$if FPC_FULLVERSION>30200}
  572. {$if defined(CPUI386) or defined(CPUARM)}
  573. {$I abitag.inc}
  574. {$endif defined(CPUI386) or defined(CPUARM)}
  575. {$endif FPC_FULLVERSION>30200}
  576. begin
  577. {$if defined(i386) and not defined(FPC_USE_LIBC)}
  578. InitSyscallIntf;
  579. {$endif}
  580. {$ifndef FPUNONE}
  581. {$if defined(cpupowerpc)}
  582. // some PPC kernels set the exception bits FE0/FE1 in the MSR to zero,
  583. // disabling all FPU exceptions. Enable them again.
  584. fpprctl(PR_SET_FPEXC, PR_FP_EXC_PRECISE);
  585. {$endif}
  586. {$endif}
  587. IsConsole := TRUE;
  588. StackLength := CheckInitialStkLen(initialStkLen);
  589. StackBottom := pointer(ptruint((ptruint(initialstkptr) or (page_size - 1)) + 1 - StackLength));
  590. {$ifdef LAST_PAGE_GENERATES_SIGNAL}
  591. StackBottom:=StackBottom + page_size;
  592. {$endif}
  593. { Set up signals handlers (may be needed by init code to test cpu features) }
  594. InstallSignals;
  595. {$if defined(cpui386) or defined(cpuarm)}
  596. fpc_cpucodeinit;
  597. {$endif cpui386}
  598. { Setup heap }
  599. InitHeap;
  600. SysInitExceptions;
  601. initunicodestringmanager;
  602. { Setup stdin, stdout and stderr }
  603. SysInitStdIO;
  604. { Arguments }
  605. SysInitExecPath;
  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.