system.pp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. {$ifdef CPUI386}
  17. {$DEFINE ELFRES32}
  18. {$endif}
  19. Unit System;
  20. {*****************************************************************************}
  21. interface
  22. {*****************************************************************************}
  23. {$define FPC_IS_SYSTEM}
  24. {$define HAS_CMDLINE}
  25. {$define USE_NOTHREADMANAGER}
  26. {$i osdefs.inc}
  27. {$I sysunixh.inc}
  28. function get_cmdline:Pchar;
  29. property cmdline:Pchar read get_cmdline;
  30. {$if defined(CPUARM) or defined(CPUM68K)}
  31. {$define fpc_softfpu_interface}
  32. {$i softfpu.pp}
  33. {$undef fpc_softfpu_interface}
  34. {$endif defined(CPUARM) or defined(CPUM68K)}
  35. {*****************************************************************************}
  36. implementation
  37. {*****************************************************************************}
  38. {$if defined(CPUI386) and not defined(FPC_USE_LIBC)}
  39. var
  40. sysenter_supported: LongInt = 0;
  41. {$endif}
  42. { Include ELF resources }
  43. {$ifdef ELFRES32}
  44. {$define HAS_RESOURCES}
  45. {$i elfres32.inc}
  46. {$endif}
  47. const calculated_cmdline:Pchar=nil;
  48. {$if defined(CPUARM) or defined(CPUM68K)}
  49. {$define fpc_softfpu_implementation}
  50. {$i softfpu.pp}
  51. {$undef fpc_softfpu_implementation}
  52. { we get these functions and types from the softfpu code }
  53. {$define FPC_SYSTEM_HAS_float64}
  54. {$define FPC_SYSTEM_HAS_float32}
  55. {$define FPC_SYSTEM_HAS_flag}
  56. {$define FPC_SYSTEM_HAS_extractFloat64Frac0}
  57. {$define FPC_SYSTEM_HAS_extractFloat64Frac1}
  58. {$define FPC_SYSTEM_HAS_extractFloat64Exp}
  59. {$define FPC_SYSTEM_HAS_extractFloat64Sign}
  60. {$define FPC_SYSTEM_HAS_ExtractFloat32Frac}
  61. {$define FPC_SYSTEM_HAS_extractFloat32Exp}
  62. {$define FPC_SYSTEM_HAS_extractFloat32Sign}
  63. {$endif defined(CPUARM) or defined(CPUM68K)}
  64. {$I system.inc}
  65. {*****************************************************************************
  66. Misc. System Dependent Functions
  67. *****************************************************************************}
  68. procedure haltproc(e:longint);cdecl;external name '_haltproc';
  69. procedure System_exit;
  70. begin
  71. haltproc(ExitCode);
  72. End;
  73. Function ParamCount: Longint;
  74. Begin
  75. Paramcount:=argc-1
  76. End;
  77. {function BackPos(c:char; const s: shortstring): integer;
  78. var
  79. i: integer;
  80. Begin
  81. for i:=length(s) downto 0 do
  82. if s[i] = c then break;
  83. if i=0 then
  84. BackPos := 0
  85. else
  86. BackPos := i;
  87. end;}
  88. { variable where full path and filename and executable is stored }
  89. { is setup by the startup of the system unit. }
  90. var
  91. execpathstr : shortstring;
  92. function paramstr(l: longint) : string;
  93. begin
  94. { stricly conforming POSIX applications }
  95. { have the executing filename as argv[0] }
  96. if l=0 then
  97. begin
  98. paramstr := execpathstr;
  99. end
  100. else if (l < argc) then
  101. paramstr:=strpas(argv[l])
  102. else
  103. paramstr:='';
  104. end;
  105. Procedure Randomize;
  106. Begin
  107. randseed:=longint(Fptime(nil));
  108. End;
  109. {*****************************************************************************
  110. cmdline
  111. *****************************************************************************}
  112. procedure SetupCmdLine;
  113. var
  114. bufsize,
  115. len,j,
  116. size,i : longint;
  117. found : boolean;
  118. buf : pchar;
  119. procedure AddBuf;
  120. begin
  121. reallocmem(calculated_cmdline,size+bufsize);
  122. move(buf^,calculated_cmdline[size],bufsize);
  123. inc(size,bufsize);
  124. bufsize:=0;
  125. end;
  126. begin
  127. if argc<=0 then
  128. exit;
  129. GetMem(buf,ARG_MAX);
  130. size:=0;
  131. bufsize:=0;
  132. i:=0;
  133. while (i<argc) do
  134. begin
  135. len:=strlen(argv[i]);
  136. if len>ARG_MAX-2 then
  137. len:=ARG_MAX-2;
  138. found:=false;
  139. for j:=1 to len do
  140. if argv[i][j]=' ' then
  141. begin
  142. found:=true;
  143. break;
  144. end;
  145. if bufsize+len>=ARG_MAX-2 then
  146. AddBuf;
  147. if found then
  148. begin
  149. buf[bufsize]:='"';
  150. inc(bufsize);
  151. end;
  152. move(argv[i]^,buf[bufsize],len);
  153. inc(bufsize,len);
  154. if found then
  155. begin
  156. buf[bufsize]:='"';
  157. inc(bufsize);
  158. end;
  159. if i<argc then
  160. buf[bufsize]:=' '
  161. else
  162. buf[bufsize]:=#0;
  163. inc(bufsize);
  164. inc(i);
  165. end;
  166. AddBuf;
  167. FreeMem(buf,ARG_MAX);
  168. end;
  169. function get_cmdline:Pchar;
  170. begin
  171. if calculated_cmdline=nil then
  172. setupcmdline;
  173. get_cmdline:=calculated_cmdline;
  174. end;
  175. {*****************************************************************************
  176. SystemUnit Initialization
  177. *****************************************************************************}
  178. function reenable_signal(sig : longint) : boolean;
  179. var
  180. e : TSigSet;
  181. i,j : byte;
  182. begin
  183. fillchar(e,sizeof(e),#0);
  184. { set is 1 based PM }
  185. dec(sig);
  186. i:=sig mod (sizeof(cuLong) * 8);
  187. j:=sig div (sizeof(cuLong) * 8);
  188. e[j]:=1 shl i;
  189. fpsigprocmask(SIG_UNBLOCK,@e,nil);
  190. reenable_signal:=geterrno=0;
  191. end;
  192. // signal handler is arch dependant due to processorexception to language
  193. // exception translation
  194. {$i sighnd.inc}
  195. var
  196. act: SigActionRec;
  197. Procedure InstallSignals;
  198. begin
  199. { Initialize the sigaction structure }
  200. { all flags and information set to zero }
  201. FillChar(act, sizeof(SigActionRec),0);
  202. { initialize handler }
  203. act.sa_handler := SigActionHandler(@SignalToRunError);
  204. act.sa_flags:=SA_SIGINFO;
  205. FpSigAction(SIGFPE,@act,nil);
  206. FpSigAction(SIGSEGV,@act,nil);
  207. FpSigAction(SIGBUS,@act,nil);
  208. FpSigAction(SIGILL,@act,nil);
  209. end;
  210. procedure SysInitStdIO;
  211. begin
  212. OpenStdIO(Input,fmInput,StdInputHandle);
  213. OpenStdIO(Output,fmOutput,StdOutputHandle);
  214. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  215. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  216. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  217. end;
  218. procedure SysInitExecPath;
  219. var
  220. i : longint;
  221. begin
  222. execpathstr[0]:=#0;
  223. i:=Fpreadlink('/proc/self/exe',@execpathstr[1],high(execpathstr));
  224. { it must also be an absolute filename, linux 2.0 points to a memory
  225. location so this will skip that }
  226. if (i>0) and (execpathstr[1]='/') then
  227. execpathstr[0]:=char(i);
  228. end;
  229. function GetProcessID: SizeUInt;
  230. begin
  231. GetProcessID := SizeUInt (fpGetPID);
  232. end;
  233. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  234. var
  235. limits : TRLimit;
  236. success : boolean;
  237. begin
  238. success := false;
  239. fillchar(limits, sizeof(limits), 0);
  240. {$ifdef has_ugetrlimit}
  241. success := fpugetrlimit(RLIMIT_STACK, @limits)=0;
  242. {$endif}
  243. if (not success) then
  244. success := fpgetrlimit(RLIMIT_STACK, @limits)=0;
  245. if (success) and (limits.rlim_cur < stklen) then
  246. result := limits.rlim_cur
  247. else
  248. result := stklen;
  249. end;
  250. var
  251. initialstkptr : Pointer;external name '__stkptr';
  252. begin
  253. {$if defined(i386) and not defined(FPC_USE_LIBC)}
  254. InitSyscallIntf;
  255. {$endif}
  256. SysResetFPU;
  257. if not(IsLibrary) then
  258. SysInitFPU;
  259. {$if defined(cpupowerpc)}
  260. // some PPC kernels set the exception bits FE0/FE1 in the MSR to zero,
  261. // disabling all FPU exceptions. Enable them again.
  262. fpprctl(PR_SET_FPEXC, PR_FP_EXC_PRECISE);
  263. {$endif}
  264. IsConsole := TRUE;
  265. StackLength := CheckInitialStkLen(initialStkLen);
  266. StackBottom := initialstkptr - StackLength;
  267. { Set up signals handlers }
  268. InstallSignals;
  269. {$if defined(cpui386) or defined(cpuarm)}
  270. fpc_cpucodeinit;
  271. {$endif cpui386}
  272. { Setup heap }
  273. InitHeap;
  274. SysInitExceptions;
  275. { Setup stdin, stdout and stderr }
  276. SysInitStdIO;
  277. { Arguments }
  278. SysInitExecPath;
  279. { Reset IO Error }
  280. InOutRes:=0;
  281. { threading }
  282. InitSystemThreads;
  283. initvariantmanager;
  284. initwidestringmanager;
  285. end.