t_linux.pas 18 KB

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