t_linux.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  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. libctype:=glibc21;
  197. { first try glibc2 }
  198. DynamicLinker:='/lib/ld-linux.so.2';
  199. if FileExists(DynamicLinker) then
  200. { Check for 2.0 files, else use the glibc 2.1 stub }
  201. if FileExists('/lib/ld-2.0.*') then
  202. libctype:=glibc2
  203. else
  204. libctype:=glibc21
  205. else
  206. if fileexists('/lib/ld-uClibc.so.0') then
  207. begin
  208. libctype:=uclibc;
  209. dynamiclinker:='/lib/ld-uClibc.so.0';
  210. end
  211. else
  212. DynamicLinker:='/lib/ld-linux.so.1';
  213. {$endif i386}
  214. {$ifdef x86_64}
  215. DynamicLinker:='/lib64/ld-linux-x86-64.so.2';
  216. libctype:=glibc2;
  217. {$endif x86_64}
  218. {$ifdef sparc}
  219. DynamicLinker:='/lib/ld-linux.so.2';
  220. libctype:=glibc2;
  221. {$endif sparc}
  222. {$ifdef powerpc}
  223. DynamicLinker:='/lib/ld.so.1';
  224. libctype:=glibc2;
  225. {$endif powerpc}
  226. {$ifdef arm}
  227. DynamicLinker:='/lib/ld-linux.so.2';
  228. libctype:=glibc2;
  229. {$endif arm}
  230. end;
  231. end;
  232. Function TLinkerLinux.WriteResponseFile(isdll:boolean) : Boolean;
  233. Var
  234. linkres : TLinkRes;
  235. i : longint;
  236. cprtobj,
  237. gprtobj,
  238. prtobj : string[80];
  239. HPath : TStringListItem;
  240. s,s1,s2 : string;
  241. found1,
  242. found2,
  243. linklibc : boolean;
  244. begin
  245. WriteResponseFile:=False;
  246. { set special options for some targets }
  247. linklibc:=(SharedLibFiles.Find('c')<>nil);
  248. if isdll then
  249. begin
  250. prtobj:='dllprt0';
  251. cprtobj:='dllprt0';
  252. gprtobj:='dllprt0';
  253. end
  254. else
  255. begin
  256. prtobj:='prt0';
  257. case libctype of
  258. glibc21:
  259. begin
  260. cprtobj:='cprt21';
  261. gprtobj:='gprt21';
  262. end;
  263. uclibc:
  264. begin
  265. cprtobj:='ucprt0';
  266. gprtobj:='ugprt0';
  267. end
  268. else
  269. cprtobj:='cprt0';
  270. gprtobj:='gprt0';
  271. end;
  272. end;
  273. if cs_profile in aktmoduleswitches then
  274. begin
  275. prtobj:=gprtobj;
  276. if not(libctype in [glibc2,glibc21]) then
  277. AddSharedLibrary('gmon');
  278. AddSharedLibrary('c');
  279. linklibc:=true;
  280. end
  281. else
  282. begin
  283. if linklibc then
  284. prtobj:=cprtobj;
  285. end;
  286. { Open link.res file }
  287. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName);
  288. { Write path to search libraries }
  289. HPath:=TStringListItem(current_module.locallibrarysearchpath.First);
  290. while assigned(HPath) do
  291. begin
  292. LinkRes.Add('SEARCH_DIR('+maybequoted(HPath.Str)+')');
  293. HPath:=TStringListItem(HPath.Next);
  294. end;
  295. HPath:=TStringListItem(LibrarySearchPath.First);
  296. while assigned(HPath) do
  297. begin
  298. LinkRes.Add('SEARCH_DIR('+maybequoted(HPath.Str)+')');
  299. HPath:=TStringListItem(HPath.Next);
  300. end;
  301. LinkRes.Add('INPUT(');
  302. { add objectfiles, start with prt0 always }
  303. if prtobj<>'' then
  304. LinkRes.AddFileName(maybequoted(FindObjectFile(prtobj,'',false)));
  305. { try to add crti and crtbegin if linking to C }
  306. if linklibc then
  307. begin
  308. if librarysearchpath.FindFile('crtbegin.o',s) then
  309. LinkRes.AddFileName(s);
  310. if librarysearchpath.FindFile('crti.o',s) then
  311. LinkRes.AddFileName(s);
  312. end;
  313. { main objectfiles }
  314. while not ObjectFiles.Empty do
  315. begin
  316. s:=ObjectFiles.GetFirst;
  317. if s<>'' then
  318. LinkRes.AddFileName(maybequoted(s));
  319. end;
  320. LinkRes.Add(')');
  321. { Write staticlibraries }
  322. if not StaticLibFiles.Empty then
  323. begin
  324. LinkRes.Add('GROUP(');
  325. While not StaticLibFiles.Empty do
  326. begin
  327. S:=StaticLibFiles.GetFirst;
  328. LinkRes.AddFileName(maybequoted(s))
  329. end;
  330. LinkRes.Add(')');
  331. end;
  332. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  333. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  334. if not SharedLibFiles.Empty then
  335. begin
  336. LinkRes.Add('INPUT(');
  337. While not SharedLibFiles.Empty do
  338. begin
  339. S:=SharedLibFiles.GetFirst;
  340. if s<>'c' then
  341. begin
  342. i:=Pos(target_info.sharedlibext,S);
  343. if i>0 then
  344. Delete(S,i,255);
  345. LinkRes.Add('-l'+s);
  346. end
  347. else
  348. begin
  349. linklibc:=true;
  350. end;
  351. end;
  352. { be sure that libc is the last lib }
  353. if linklibc then
  354. LinkRes.Add('-lc');
  355. { when we have -static for the linker the we also need libgcc }
  356. if (cs_link_staticflag in aktglobalswitches) then
  357. LinkRes.Add('-lgcc');
  358. LinkRes.Add(')');
  359. end;
  360. { objects which must be at the end }
  361. if linklibc and (libctype<>uclibc) then
  362. begin
  363. found1:=librarysearchpath.FindFile('crtend.o',s1);
  364. found2:=librarysearchpath.FindFile('crtn.o',s2);
  365. if found1 or found2 then
  366. begin
  367. LinkRes.Add('INPUT(');
  368. if found1 then
  369. LinkRes.AddFileName(s1);
  370. if found2 then
  371. LinkRes.AddFileName(s2);
  372. LinkRes.Add(')');
  373. end;
  374. end;
  375. { Write and Close response }
  376. linkres.writetodisk;
  377. linkres.Free;
  378. WriteResponseFile:=True;
  379. end;
  380. function TLinkerLinux.MakeExecutable:boolean;
  381. var
  382. binstr : String;
  383. cmdstr : TCmdStr;
  384. success : boolean;
  385. DynLinkStr : string[60];
  386. GCSectionsStr,
  387. StaticStr,
  388. StripStr : string[40];
  389. begin
  390. if not(cs_link_extern in aktglobalswitches) then
  391. Message1(exec_i_linking,current_module.exefilename^);
  392. { Create some replacements }
  393. StaticStr:='';
  394. StripStr:='';
  395. GCSectionsStr:='';
  396. DynLinkStr:='';
  397. if (cs_link_staticflag in aktglobalswitches) then
  398. StaticStr:='-static';
  399. if (cs_link_strip in aktglobalswitches) then
  400. StripStr:='-s';
  401. if (cs_link_smart in aktglobalswitches) and
  402. (tf_smartlink_sections in target_info.flags) then
  403. GCSectionsStr:='--gc-sections';
  404. If (cs_profile in aktmoduleswitches) or
  405. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty)) then
  406. begin
  407. DynLinkStr:='-dynamic-linker='+Info.DynamicLinker;
  408. if cshared Then
  409. DynLinkStr:='--shared ' + DynLinkStr;
  410. if rlinkpath<>'' Then
  411. DynLinkStr:='--rpath-link '+rlinkpath + ' '+ DynLinkStr;
  412. End;
  413. { Write used files and libraries }
  414. WriteResponseFile(false);
  415. { Call linker }
  416. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  417. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename^));
  418. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  419. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  420. Replace(cmdstr,'$STATIC',StaticStr);
  421. Replace(cmdstr,'$STRIP',StripStr);
  422. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  423. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  424. success:=DoExec(FindUtil(utilsprefix+BinStr),CmdStr,true,false);
  425. { Remove ReponseFile }
  426. if (success) and not(cs_link_extern in aktglobalswitches) then
  427. RemoveFile(outputexedir+Info.ResName);
  428. if (success) then
  429. success:=PostProcessExecutable(current_module.exefilename^,false);
  430. MakeExecutable:=success; { otherwise a recursive call to link method }
  431. end;
  432. Function TLinkerLinux.MakeSharedLibrary:boolean;
  433. var
  434. InitStr,
  435. FiniStr,
  436. SoNameStr : string[80];
  437. binstr : String;
  438. cmdstr : TCmdStr;
  439. success : boolean;
  440. begin
  441. MakeSharedLibrary:=false;
  442. if not(cs_link_extern in aktglobalswitches) then
  443. Message1(exec_i_linking,current_module.sharedlibfilename^);
  444. { Write used files and libraries }
  445. WriteResponseFile(true);
  446. { Create some replacements }
  447. InitStr:='-init FPC_LIB_START';
  448. FiniStr:='-fini FPC_LIB_EXIT';
  449. SoNameStr:='-soname '+SplitFileName(current_module.sharedlibfilename^);
  450. { Call linker }
  451. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  452. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename^));
  453. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  454. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  455. Replace(cmdstr,'$INIT',InitStr);
  456. Replace(cmdstr,'$FINI',FiniStr);
  457. Replace(cmdstr,'$SONAME',SoNameStr);
  458. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  459. { Strip the library ? }
  460. if success and (cs_link_strip in aktglobalswitches) then
  461. begin
  462. { only remove non global symbols and debugging info for a library }
  463. Info.DllCmd[2]:='strip --discard-all --strip-debug $EXE';
  464. SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  465. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename^));
  466. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  467. end;
  468. { Remove ReponseFile }
  469. if (success) and not(cs_link_extern in aktglobalswitches) then
  470. RemoveFile(outputexedir+Info.ResName);
  471. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  472. end;
  473. function tlinkerLinux.postprocessexecutable(const fn : string;isdll:boolean):boolean;
  474. Var
  475. cmdstr: string;
  476. found : boolean;
  477. hp : tused_unit;
  478. begin
  479. postprocessexecutable:=True;
  480. if target_res.id=res_elf then
  481. begin
  482. found:=((current_module.flags and uf_has_resourcefiles)=uf_has_resourcefiles);
  483. if not found then
  484. begin
  485. hp:=tused_unit(usedunits.first);
  486. While Assigned(hp) and not Found do
  487. begin
  488. Found:=((hp.u.flags and uf_has_resourcefiles)=uf_has_resourcefiles);
  489. hp:=tused_unit(hp.next);
  490. end;
  491. end;
  492. if found then
  493. begin
  494. cmdstr:=' -f -i '+maybequoted(fn);
  495. postprocessexecutable:=DoExec(FindUtil(utilsprefix+'fpcres'),cmdstr,false,false);
  496. end;
  497. end;
  498. end;
  499. {*****************************************************************************
  500. Initialize
  501. *****************************************************************************}
  502. initialization
  503. {$ifdef i386}
  504. RegisterExternalLinker(system_i386_linux_info,TLinkerLinux);
  505. RegisterImport(system_i386_linux,timportliblinux);
  506. RegisterExport(system_i386_linux,texportliblinux);
  507. RegisterTarget(system_i386_linux_info);
  508. RegisterRes(res_elf32_info);
  509. RegisterExternalLinker(system_x86_6432_linux_info,TLinkerLinux);
  510. RegisterImport(system_x86_6432_linux,timportliblinux);
  511. RegisterExport(system_x86_6432_linux,texportliblinux);
  512. RegisterTarget(system_x86_6432_linux_info);
  513. {$endif i386}
  514. {$ifdef m68k}
  515. RegisterExternalLinker(system_m68k_linux_info,TLinkerLinux);
  516. RegisterImport(system_m68k_linux,timportliblinux);
  517. RegisterExport(system_m68k_linux,texportliblinux);
  518. RegisterTarget(system_m68k_linux_info);
  519. {$endif m68k}
  520. {$ifdef powerpc}
  521. RegisterExternalLinker(system_powerpc_linux_info,TLinkerLinux);
  522. RegisterImport(system_powerpc_linux,timportliblinux);
  523. RegisterExport(system_powerpc_linux,texportliblinux);
  524. RegisterTarget(system_powerpc_linux_info);
  525. {$endif powerpc}
  526. {$ifdef alpha}
  527. RegisterExternalLinker(system_alpha_linux_info,TLinkerLinux);
  528. RegisterImport(system_alpha_linux,timportliblinux);
  529. RegisterExport(system_alpha_linux,texportliblinux);
  530. RegisterTarget(system_alpha_linux_info);
  531. {$endif alpha}
  532. {$ifdef x86_64}
  533. RegisterExternalLinker(system_x86_64_linux_info,TLinkerLinux);
  534. RegisterImport(system_x86_64_linux,timportliblinux);
  535. RegisterExport(system_x86_64_linux,texportliblinux);
  536. RegisterTarget(system_x86_64_linux_info);
  537. RegisterRes(res_elf64_info);
  538. {$endif x86_64}
  539. {$ifdef SPARC}
  540. RegisterExternalLinker(system_sparc_linux_info,TLinkerLinux);
  541. RegisterImport(system_SPARC_linux,timportliblinux);
  542. RegisterExport(system_SPARC_linux,texportliblinux);
  543. RegisterTarget(system_SPARC_linux_info);
  544. {$endif SPARC}
  545. {$ifdef ARM}
  546. RegisterExternalLinker(system_arm_linux_info,TLinkerLinux);
  547. RegisterImport(system_arm_linux,timportliblinux);
  548. RegisterExport(system_arm_linux,texportliblinux);
  549. RegisterTarget(system_arm_linux_info);
  550. {$endif ARM}
  551. end.