system.pp 19 KB

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