system.pp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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
  101. paramstr:=strpas(argv[l]);
  102. end;
  103. Procedure Randomize;
  104. Begin
  105. randseed:=longint(Fptime(nil));
  106. End;
  107. {*****************************************************************************
  108. cmdline
  109. *****************************************************************************}
  110. procedure SetupCmdLine;
  111. var
  112. bufsize,
  113. len,j,
  114. size,i : longint;
  115. found : boolean;
  116. buf : pchar;
  117. procedure AddBuf;
  118. begin
  119. reallocmem(calculated_cmdline,size+bufsize);
  120. move(buf^,calculated_cmdline[size],bufsize);
  121. inc(size,bufsize);
  122. bufsize:=0;
  123. end;
  124. begin
  125. if argc<=0 then
  126. exit;
  127. GetMem(buf,ARG_MAX);
  128. size:=0;
  129. bufsize:=0;
  130. i:=0;
  131. while (i<argc) do
  132. begin
  133. len:=strlen(argv[i]);
  134. if len>ARG_MAX-2 then
  135. len:=ARG_MAX-2;
  136. found:=false;
  137. for j:=1 to len do
  138. if argv[i][j]=' ' then
  139. begin
  140. found:=true;
  141. break;
  142. end;
  143. if bufsize+len>=ARG_MAX-2 then
  144. AddBuf;
  145. if found then
  146. begin
  147. buf[bufsize]:='"';
  148. inc(bufsize);
  149. end;
  150. move(argv[i]^,buf[bufsize],len);
  151. inc(bufsize,len);
  152. if found then
  153. begin
  154. buf[bufsize]:='"';
  155. inc(bufsize);
  156. end;
  157. if i<argc then
  158. buf[bufsize]:=' '
  159. else
  160. buf[bufsize]:=#0;
  161. inc(bufsize);
  162. inc(i);
  163. end;
  164. AddBuf;
  165. FreeMem(buf,ARG_MAX);
  166. end;
  167. function get_cmdline:Pchar;
  168. begin
  169. if calculated_cmdline=nil then
  170. setupcmdline;
  171. get_cmdline:=calculated_cmdline;
  172. end;
  173. {*****************************************************************************
  174. SystemUnit Initialization
  175. *****************************************************************************}
  176. function reenable_signal(sig : longint) : boolean;
  177. var
  178. e : TSigSet;
  179. i,j : byte;
  180. begin
  181. fillchar(e,sizeof(e),#0);
  182. { set is 1 based PM }
  183. dec(sig);
  184. i:=sig mod (sizeof(cuLong) * 8);
  185. j:=sig div (sizeof(cuLong) * 8);
  186. e[j]:=1 shl i;
  187. fpsigprocmask(SIG_UNBLOCK,@e,nil);
  188. reenable_signal:=geterrno=0;
  189. end;
  190. // signal handler is arch dependant due to processorexception to language
  191. // exception translation
  192. {$i sighnd.inc}
  193. var
  194. act: SigActionRec;
  195. Procedure InstallSignals;
  196. begin
  197. { Initialize the sigaction structure }
  198. { all flags and information set to zero }
  199. FillChar(act, sizeof(SigActionRec),0);
  200. { initialize handler }
  201. act.sa_handler := SigActionHandler(@SignalToRunError);
  202. act.sa_flags:=SA_SIGINFO;
  203. FpSigAction(SIGFPE,@act,nil);
  204. FpSigAction(SIGSEGV,@act,nil);
  205. FpSigAction(SIGBUS,@act,nil);
  206. FpSigAction(SIGILL,@act,nil);
  207. end;
  208. procedure SysInitStdIO;
  209. begin
  210. OpenStdIO(Input,fmInput,StdInputHandle);
  211. OpenStdIO(Output,fmOutput,StdOutputHandle);
  212. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  213. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  214. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  215. end;
  216. procedure SysInitExecPath;
  217. var
  218. i : longint;
  219. begin
  220. execpathstr[0]:=#0;
  221. i:=Fpreadlink('/proc/self/exe',@execpathstr[1],high(execpathstr));
  222. { it must also be an absolute filename, linux 2.0 points to a memory
  223. location so this will skip that }
  224. if (i>0) and (execpathstr[1]='/') then
  225. execpathstr[0]:=char(i);
  226. end;
  227. function GetProcessID: SizeUInt;
  228. begin
  229. GetProcessID := SizeUInt (fpGetPID);
  230. end;
  231. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  232. var
  233. limits : TRLimit;
  234. success : boolean;
  235. begin
  236. success := false;
  237. fillchar(limits, sizeof(limits), 0);
  238. {$ifdef has_ugetrlimit}
  239. success := fpugetrlimit(RLIMIT_STACK, @limits)=0;
  240. {$endif}
  241. if (not success) then
  242. success := fpgetrlimit(RLIMIT_STACK, @limits)=0;
  243. if (success) and (limits.rlim_cur < stklen) then
  244. result := limits.rlim_cur
  245. else
  246. result := stklen;
  247. end;
  248. var
  249. initialstkptr : Pointer;external name '__stkptr';
  250. begin
  251. {$if defined(i386) and not defined(FPC_USE_LIBC)}
  252. InitSyscallIntf;
  253. {$endif}
  254. SysResetFPU;
  255. if not(IsLibrary) then
  256. SysInitFPU;
  257. {$if defined(cpupowerpc)}
  258. // some PPC kernels set the exception bits FE0/FE1 in the MSR to zero,
  259. // disabling all FPU exceptions. Enable them again.
  260. fpprctl(PR_SET_FPEXC, PR_FP_EXC_PRECISE);
  261. {$endif}
  262. IsConsole := TRUE;
  263. StackLength := CheckInitialStkLen(initialStkLen);
  264. StackBottom := initialstkptr - StackLength;
  265. { Set up signals handlers }
  266. InstallSignals;
  267. {$if defined(cpui386) or defined(cpuarm)}
  268. fpc_cpucodeinit;
  269. {$endif cpui386}
  270. { Setup heap }
  271. InitHeap;
  272. SysInitExceptions;
  273. { Setup stdin, stdout and stderr }
  274. SysInitStdIO;
  275. { Arguments }
  276. SysInitExecPath;
  277. { Reset IO Error }
  278. InOutRes:=0;
  279. initvariantmanager;
  280. initwidestringmanager;
  281. end.