t_linux.pas 18 KB

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