t_linux.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. {
  2. Copyright (c) 1998-2002 by Peter Vreman
  3. This unit implements support import,export,link routines
  4. for the (i386) Linux target
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit t_linux;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. symsym,symdef,
  23. import,export,link;
  24. type
  25. timportliblinux=class(timportlib)
  26. procedure preparelib(const s:string);override;
  27. procedure importprocedure(aprocdef:tprocdef;const module:string;index:longint;const name:string);override;
  28. procedure importvariable(vs:tglobalvarsym;const name,module:string);override;
  29. procedure generatelib;override;
  30. end;
  31. texportliblinux=class(texportlib)
  32. procedure preparelib(const s : string);override;
  33. procedure exportprocedure(hp : texported_item);override;
  34. procedure exportvar(hp : texported_item);override;
  35. procedure generatelib;override;
  36. end;
  37. tlinkerlinux=class(texternallinker)
  38. private
  39. libctype:(libc5,glibc2,glibc21,uclibc);
  40. Function WriteResponseFile(isdll:boolean) : Boolean;
  41. public
  42. constructor Create;override;
  43. procedure SetDefaultInfo;override;
  44. function MakeExecutable:boolean;override;
  45. function MakeSharedLibrary:boolean;override;
  46. end;
  47. implementation
  48. uses
  49. cutils,cclasses,
  50. verbose,systems,globtype,globals,
  51. symconst,script,
  52. fmodule,dos
  53. ,aasmbase,aasmtai,aasmcpu,cpubase,cgobj
  54. ,i_linux
  55. ;
  56. {*****************************************************************************
  57. TIMPORTLIBLINUX
  58. *****************************************************************************}
  59. procedure timportliblinux.preparelib(const s : string);
  60. begin
  61. end;
  62. procedure timportliblinux.importprocedure(aprocdef:tprocdef;const module:string;index:longint;const name:string);
  63. begin
  64. { insert sharedlibrary }
  65. current_module.linkothersharedlibs.add(SplitName(module),link_allways);
  66. end;
  67. procedure timportliblinux.importvariable(vs:tglobalvarsym;const name,module:string);
  68. begin
  69. { insert sharedlibrary }
  70. current_module.linkothersharedlibs.add(SplitName(module),link_allways);
  71. { reset the mangledname and turn off the dll_var option }
  72. vs.set_mangledname(name);
  73. exclude(vs.varoptions,vo_is_dll_var);
  74. end;
  75. procedure timportliblinux.generatelib;
  76. begin
  77. end;
  78. {*****************************************************************************
  79. TEXPORTLIBLINUX
  80. *****************************************************************************}
  81. procedure texportliblinux.preparelib(const s:string);
  82. begin
  83. end;
  84. procedure texportliblinux.exportprocedure(hp : texported_item);
  85. var
  86. hp2 : texported_item;
  87. begin
  88. { first test the index value }
  89. if (hp.options and eo_index)<>0 then
  90. begin
  91. Message1(parser_e_no_export_with_index_for_target,'linux');
  92. exit;
  93. end;
  94. { now place in correct order }
  95. hp2:=texported_item(current_module._exports.first);
  96. while assigned(hp2) and
  97. (hp.name^>hp2.name^) do
  98. hp2:=texported_item(hp2.next);
  99. { insert hp there !! }
  100. if assigned(hp2) and (hp2.name^=hp.name^) then
  101. begin
  102. { this is not allowed !! }
  103. Message1(parser_e_export_name_double,hp.name^);
  104. exit;
  105. end;
  106. if hp2=texported_item(current_module._exports.first) then
  107. current_module._exports.concat(hp)
  108. else if assigned(hp2) then
  109. begin
  110. hp.next:=hp2;
  111. hp.previous:=hp2.previous;
  112. if assigned(hp2.previous) then
  113. hp2.previous.next:=hp;
  114. hp2.previous:=hp;
  115. end
  116. else
  117. current_module._exports.concat(hp);
  118. end;
  119. procedure texportliblinux.exportvar(hp : texported_item);
  120. begin
  121. hp.is_var:=true;
  122. exportprocedure(hp);
  123. end;
  124. procedure texportliblinux.generatelib;
  125. var
  126. hp2 : texported_item;
  127. begin
  128. new_section(codesegment,sec_code,'',0);
  129. hp2:=texported_item(current_module._exports.first);
  130. while assigned(hp2) do
  131. begin
  132. if (not hp2.is_var) and
  133. (hp2.sym.typ=procsym) then
  134. begin
  135. { the manglednames can already be the same when the procedure
  136. is declared with cdecl }
  137. if tprocsym(hp2.sym).first_procdef.mangledname<>hp2.name^ then
  138. begin
  139. { place jump in codesegment }
  140. codesegment.concat(tai_align.create(target_info.alignment.procalign));
  141. codeSegment.concat(Tai_symbol.Createname_global(hp2.name^,AT_FUNCTION,0));
  142. cg.a_jmp_name(codesegment,tprocsym(hp2.sym).first_procdef.mangledname);
  143. codeSegment.concat(Tai_symbol_end.Createname(hp2.name^));
  144. end;
  145. end
  146. else
  147. Message1(parser_e_no_export_of_variables_for_target,'linux');
  148. hp2:=texported_item(hp2.next);
  149. end;
  150. end;
  151. {*****************************************************************************
  152. TLINKERLINUX
  153. *****************************************************************************}
  154. Constructor TLinkerLinux.Create;
  155. begin
  156. Inherited Create;
  157. if not Dontlinkstdlibpath Then
  158. {$ifdef x86_64}
  159. LibrarySearchPath.AddPath('/lib64;/usr/lib64;/usr/X11R6/lib64',true);
  160. {$else}
  161. LibrarySearchPath.AddPath('/lib;/usr/lib;/usr/X11R6/lib',true);
  162. {$endif x86_64}
  163. end;
  164. procedure TLinkerLinux.SetDefaultInfo;
  165. {
  166. This will also detect which libc version will be used
  167. }
  168. {$ifdef m68k}
  169. var
  170. St : SearchRec;
  171. {$endif m68k}
  172. begin
  173. with Info do
  174. begin
  175. ExeCmd[1]:='ld $OPT $DYNLINK $STATIC $GCSECTIONS $STRIP -L. -o $EXE $RES';
  176. DllCmd[1]:='ld $OPT $INIT $FINI $SONAME -shared -L. -o $EXE $RES';
  177. DllCmd[2]:='strip --strip-unneeded $EXE';
  178. {$ifdef m68k}
  179. libctype:=glibc2;
  180. FindFirst('/lib/ld*',AnyFile,st);
  181. while DosError=0 do
  182. begin
  183. if copy(st.name,1,5)='ld-2.' then
  184. begin
  185. DynamicLinker:='/lib/'+St.name;
  186. if st.name[6]<>'0' then
  187. libctype:=glibc21;
  188. break;
  189. end;
  190. FindNext(St);
  191. end;
  192. FindClose(St);
  193. {$endif m68k}
  194. {$ifdef i386}
  195. { first try glibc2 }
  196. DynamicLinker:='/lib/ld-linux.so.2';
  197. if FileExists(DynamicLinker) then
  198. { Check for 2.0 files, else use the glibc 2.1 stub }
  199. if FileExists('/lib/ld-2.0.*') then
  200. libctype:=glibc2
  201. else
  202. libctype:=glibc21
  203. else
  204. if fileexists('/lib/ld-uClibc.so.0') then
  205. begin
  206. libctype:=uclibc;
  207. dynamiclinker:='/lib/ld-uClibc.so.0';
  208. end
  209. else
  210. DynamicLinker:='/lib/ld-linux.so.1';
  211. {$endif i386}
  212. {$ifdef x86_64}
  213. DynamicLinker:='/lib64/ld-linux-x86-64.so.2';
  214. libctype:=glibc2;
  215. {$endif x86_64}
  216. {$ifdef sparc}
  217. DynamicLinker:='/lib/ld-linux.so.2';
  218. libctype:=glibc2;
  219. {$endif sparc}
  220. {$ifdef powerpc}
  221. DynamicLinker:='/lib/ld.so.1';
  222. libctype:=glibc2;
  223. {$endif powerpc}
  224. {$ifdef arm}
  225. DynamicLinker:='/lib/ld-linux.so.2';
  226. libctype:=glibc2;
  227. {$endif arm}
  228. end;
  229. end;
  230. Function TLinkerLinux.WriteResponseFile(isdll:boolean) : Boolean;
  231. Var
  232. linkres : TLinkRes;
  233. i : longint;
  234. cprtobj,
  235. gprtobj,
  236. prtobj : string[80];
  237. HPath : TStringListItem;
  238. s,s1,s2 : string;
  239. found1,
  240. found2,
  241. linklibc : boolean;
  242. begin
  243. WriteResponseFile:=False;
  244. { set special options for some targets }
  245. linklibc:=(SharedLibFiles.Find('c')<>nil);
  246. if isdll then
  247. begin
  248. prtobj:='dllprt0';
  249. cprtobj:='dllprt0';
  250. gprtobj:='dllprt0';
  251. end
  252. else
  253. begin
  254. prtobj:='prt0';
  255. case libctype of
  256. glibc21:
  257. begin
  258. cprtobj:='cprt21';
  259. gprtobj:='gprt21';
  260. end;
  261. uclibc:
  262. begin
  263. cprtobj:='ucprt0';
  264. gprtobj:='ugprt0';
  265. end
  266. else
  267. cprtobj:='cprt0';
  268. gprtobj:='gprt0';
  269. end;
  270. end;
  271. if cs_profile in aktmoduleswitches then
  272. begin
  273. prtobj:=gprtobj;
  274. if not(libctype in [glibc2,glibc21]) then
  275. AddSharedLibrary('gmon');
  276. AddSharedLibrary('c');
  277. linklibc:=true;
  278. end
  279. else
  280. begin
  281. if linklibc then
  282. prtobj:=cprtobj;
  283. end;
  284. { Open link.res file }
  285. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName);
  286. { Write path to search libraries }
  287. HPath:=TStringListItem(current_module.locallibrarysearchpath.First);
  288. while assigned(HPath) do
  289. begin
  290. LinkRes.Add('SEARCH_DIR('+maybequoted(HPath.Str)+')');
  291. HPath:=TStringListItem(HPath.Next);
  292. end;
  293. HPath:=TStringListItem(LibrarySearchPath.First);
  294. while assigned(HPath) do
  295. begin
  296. LinkRes.Add('SEARCH_DIR('+maybequoted(HPath.Str)+')');
  297. HPath:=TStringListItem(HPath.Next);
  298. end;
  299. LinkRes.Add('INPUT(');
  300. { add objectfiles, start with prt0 always }
  301. if prtobj<>'' then
  302. LinkRes.AddFileName(maybequoted(FindObjectFile(prtobj,'',false)));
  303. { try to add crti and crtbegin if linking to C }
  304. if linklibc then
  305. begin
  306. if librarysearchpath.FindFile('crtbegin.o',s) then
  307. LinkRes.AddFileName(s);
  308. if librarysearchpath.FindFile('crti.o',s) then
  309. LinkRes.AddFileName(s);
  310. end;
  311. { main objectfiles }
  312. while not ObjectFiles.Empty do
  313. begin
  314. s:=ObjectFiles.GetFirst;
  315. if s<>'' then
  316. LinkRes.AddFileName(maybequoted(s));
  317. end;
  318. LinkRes.Add(')');
  319. { Write staticlibraries }
  320. if not StaticLibFiles.Empty then
  321. begin
  322. LinkRes.Add('GROUP(');
  323. While not StaticLibFiles.Empty do
  324. begin
  325. S:=StaticLibFiles.GetFirst;
  326. LinkRes.AddFileName(maybequoted(s))
  327. end;
  328. LinkRes.Add(')');
  329. end;
  330. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  331. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  332. if not SharedLibFiles.Empty then
  333. begin
  334. LinkRes.Add('INPUT(');
  335. While not SharedLibFiles.Empty do
  336. begin
  337. S:=SharedLibFiles.GetFirst;
  338. if s<>'c' then
  339. begin
  340. i:=Pos(target_info.sharedlibext,S);
  341. if i>0 then
  342. Delete(S,i,255);
  343. LinkRes.Add('-l'+s);
  344. end
  345. else
  346. begin
  347. linklibc:=true;
  348. end;
  349. end;
  350. { be sure that libc is the last lib }
  351. if linklibc then
  352. LinkRes.Add('-lc');
  353. { when we have -static for the linker the we also need libgcc }
  354. if (cs_link_staticflag in aktglobalswitches) then
  355. LinkRes.Add('-lgcc');
  356. LinkRes.Add(')');
  357. end;
  358. { objects which must be at the end }
  359. if linklibc and (libctype<>uclibc) then
  360. begin
  361. found1:=librarysearchpath.FindFile('crtend.o',s1);
  362. found2:=librarysearchpath.FindFile('crtn.o',s2);
  363. if found1 or found2 then
  364. begin
  365. LinkRes.Add('INPUT(');
  366. if found1 then
  367. LinkRes.AddFileName(s1);
  368. if found2 then
  369. LinkRes.AddFileName(s2);
  370. LinkRes.Add(')');
  371. end;
  372. end;
  373. { Write and Close response }
  374. linkres.writetodisk;
  375. linkres.Free;
  376. WriteResponseFile:=True;
  377. end;
  378. function TLinkerLinux.MakeExecutable:boolean;
  379. var
  380. binstr : String;
  381. cmdstr : TCmdStr;
  382. success : boolean;
  383. DynLinkStr : string[60];
  384. GCSectionsStr,
  385. StaticStr,
  386. StripStr : string[40];
  387. begin
  388. if not(cs_link_extern in aktglobalswitches) then
  389. Message1(exec_i_linking,current_module.exefilename^);
  390. { Create some replacements }
  391. StaticStr:='';
  392. StripStr:='';
  393. GCSectionsStr:='';
  394. DynLinkStr:='';
  395. if (cs_link_staticflag in aktglobalswitches) then
  396. StaticStr:='-static';
  397. if (cs_link_strip in aktglobalswitches) then
  398. StripStr:='-s';
  399. if (cs_link_smart in aktglobalswitches) and
  400. (tf_smartlink_sections in target_info.flags) then
  401. GCSectionsStr:='--gc-sections';
  402. If (cs_profile in aktmoduleswitches) or
  403. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty)) then
  404. begin
  405. DynLinkStr:='-dynamic-linker='+Info.DynamicLinker;
  406. if cshared Then
  407. DynLinkStr:='--shared ' + DynLinkStr;
  408. if rlinkpath<>'' Then
  409. DynLinkStr:='--rpath-link '+rlinkpath + ' '+ DynLinkStr;
  410. End;
  411. { Write used files and libraries }
  412. WriteResponseFile(false);
  413. { Call linker }
  414. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  415. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename^));
  416. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  417. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  418. Replace(cmdstr,'$STATIC',StaticStr);
  419. Replace(cmdstr,'$STRIP',StripStr);
  420. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  421. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  422. success:=DoExec(FindUtil(utilsprefix+BinStr),CmdStr,true,false);
  423. { Remove ReponseFile }
  424. if (success) and not(cs_link_extern in aktglobalswitches) then
  425. RemoveFile(outputexedir+Info.ResName);
  426. MakeExecutable:=success; { otherwise a recursive call to link method }
  427. end;
  428. Function TLinkerLinux.MakeSharedLibrary:boolean;
  429. var
  430. InitStr,
  431. FiniStr,
  432. SoNameStr : string[80];
  433. binstr : String;
  434. cmdstr : TCmdStr;
  435. success : boolean;
  436. begin
  437. MakeSharedLibrary:=false;
  438. if not(cs_link_extern in aktglobalswitches) then
  439. Message1(exec_i_linking,current_module.sharedlibfilename^);
  440. { Write used files and libraries }
  441. WriteResponseFile(true);
  442. { Create some replacements }
  443. InitStr:='-init FPC_LIB_START';
  444. FiniStr:='-fini FPC_LIB_EXIT';
  445. SoNameStr:='-soname '+SplitFileName(current_module.sharedlibfilename^);
  446. { Call linker }
  447. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  448. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename^));
  449. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  450. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  451. Replace(cmdstr,'$INIT',InitStr);
  452. Replace(cmdstr,'$FINI',FiniStr);
  453. Replace(cmdstr,'$SONAME',SoNameStr);
  454. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  455. { Strip the library ? }
  456. if success and (cs_link_strip in aktglobalswitches) then
  457. begin
  458. SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  459. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename^));
  460. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  461. end;
  462. { Remove ReponseFile }
  463. if (success) and not(cs_link_extern in aktglobalswitches) then
  464. RemoveFile(outputexedir+Info.ResName);
  465. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  466. end;
  467. {*****************************************************************************
  468. Initialize
  469. *****************************************************************************}
  470. initialization
  471. {$ifdef i386}
  472. RegisterExternalLinker(system_i386_linux_info,TLinkerLinux);
  473. RegisterImport(system_i386_linux,timportliblinux);
  474. RegisterExport(system_i386_linux,texportliblinux);
  475. RegisterTarget(system_i386_linux_info);
  476. {$endif i386}
  477. {$ifdef m68k}
  478. RegisterExternalLinker(system_m68k_linux_info,TLinkerLinux);
  479. RegisterImport(system_m68k_linux,timportliblinux);
  480. RegisterExport(system_m68k_linux,texportliblinux);
  481. RegisterTarget(system_m68k_linux_info);
  482. {$endif m68k}
  483. {$ifdef powerpc}
  484. RegisterExternalLinker(system_powerpc_linux_info,TLinkerLinux);
  485. RegisterImport(system_powerpc_linux,timportliblinux);
  486. RegisterExport(system_powerpc_linux,texportliblinux);
  487. RegisterTarget(system_powerpc_linux_info);
  488. {$endif powerpc}
  489. {$ifdef alpha}
  490. RegisterExternalLinker(system_alpha_linux_info,TLinkerLinux);
  491. RegisterImport(system_alpha_linux,timportliblinux);
  492. RegisterExport(system_alpha_linux,texportliblinux);
  493. RegisterTarget(system_alpha_linux_info);
  494. {$endif alpha}
  495. {$ifdef x86_64}
  496. RegisterExternalLinker(system_x86_64_linux_info,TLinkerLinux);
  497. RegisterImport(system_x86_64_linux,timportliblinux);
  498. RegisterExport(system_x86_64_linux,texportliblinux);
  499. RegisterTarget(system_x86_64_linux_info);
  500. {$endif x86_64}
  501. {$ifdef SPARC}
  502. RegisterExternalLinker(system_sparc_linux_info,TLinkerLinux);
  503. RegisterImport(system_SPARC_linux,timportliblinux);
  504. RegisterExport(system_SPARC_linux,texportliblinux);
  505. RegisterTarget(system_SPARC_linux_info);
  506. {$endif SPARC}
  507. {$ifdef ARM}
  508. RegisterExternalLinker(system_arm_linux_info,TLinkerLinux);
  509. RegisterImport(system_arm_linux,timportliblinux);
  510. RegisterExport(system_arm_linux,texportliblinux);
  511. RegisterTarget(system_arm_linux_info);
  512. {$endif ARM}
  513. end.