t_linux.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  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 $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. end;
  228. end;
  229. Function TLinkerLinux.WriteResponseFile(isdll:boolean) : Boolean;
  230. Var
  231. linkres : TLinkRes;
  232. i : longint;
  233. cprtobj,
  234. gprtobj,
  235. prtobj : string[80];
  236. HPath : TStringListItem;
  237. s,s1,s2 : string;
  238. found1,
  239. found2,
  240. linkdynamic,
  241. linklibc : boolean;
  242. begin
  243. WriteResponseFile:=False;
  244. { set special options for some targets }
  245. linkdynamic:=not(SharedLibFiles.empty);
  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('+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('+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(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(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(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. linkdynamic:=false; { libc will include the ld-linux for us }
  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. if linkdynamic and (Info.DynamicLinker<>'') then
  359. LinkRes.AddFileName(Info.DynamicLinker);
  360. LinkRes.Add(')');
  361. end;
  362. { objects which must be at the end }
  363. if linklibc and (libctype<>uclibc) then
  364. begin
  365. found1:=librarysearchpath.FindFile('crtend.o',s1);
  366. found2:=librarysearchpath.FindFile('crtn.o',s2);
  367. if found1 or found2 then
  368. begin
  369. LinkRes.Add('INPUT(');
  370. if found1 then
  371. LinkRes.AddFileName(s1);
  372. if found2 then
  373. LinkRes.AddFileName(s2);
  374. LinkRes.Add(')');
  375. end;
  376. end;
  377. { Write and Close response }
  378. linkres.writetodisk;
  379. linkres.Free;
  380. WriteResponseFile:=True;
  381. end;
  382. function TLinkerLinux.MakeExecutable:boolean;
  383. var
  384. binstr,
  385. cmdstr : string;
  386. success : boolean;
  387. DynLinkStr : string[60];
  388. StaticStr,
  389. StripStr : string[40];
  390. begin
  391. if not(cs_link_extern in aktglobalswitches) then
  392. Message1(exec_i_linking,current_module.exefilename^);
  393. { Create some replacements }
  394. StaticStr:='';
  395. StripStr:='';
  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_profile in aktmoduleswitches) or
  402. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty)) then
  403. begin
  404. DynLinkStr:='-dynamic-linker='+Info.DynamicLinker;
  405. if cshared Then
  406. DynLinkStr:='--shared ' + DynLinkStr;
  407. if rlinkpath<>'' Then
  408. DynLinkStr:='--rpath-link '+rlinkpath + ' '+ DynLinkStr;
  409. End;
  410. { Write used files and libraries }
  411. WriteResponseFile(false);
  412. { Call linker }
  413. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  414. Replace(cmdstr,'$EXE',current_module.exefilename^);
  415. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  416. Replace(cmdstr,'$RES',outputexedir+Info.ResName);
  417. Replace(cmdstr,'$STATIC',StaticStr);
  418. Replace(cmdstr,'$STRIP',StripStr);
  419. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  420. success:=DoExec(FindUtil(utilsprefix+BinStr),CmdStr,true,false);
  421. { Remove ReponseFile }
  422. if (success) and not(cs_link_extern in aktglobalswitches) then
  423. RemoveFile(outputexedir+Info.ResName);
  424. MakeExecutable:=success; { otherwise a recursive call to link method }
  425. end;
  426. Function TLinkerLinux.MakeSharedLibrary:boolean;
  427. var
  428. InitStr,
  429. FiniStr,
  430. SoNameStr : string[80];
  431. binstr,
  432. cmdstr : string;
  433. success : boolean;
  434. begin
  435. MakeSharedLibrary:=false;
  436. if not(cs_link_extern in aktglobalswitches) then
  437. Message1(exec_i_linking,current_module.sharedlibfilename^);
  438. { Write used files and libraries }
  439. WriteResponseFile(true);
  440. { Create some replacements }
  441. InitStr:='-init FPC_LIB_START';
  442. FiniStr:='-fini FPC_LIB_EXIT';
  443. SoNameStr:='-soname '+SplitFileName(current_module.sharedlibfilename^);
  444. { Call linker }
  445. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  446. Replace(cmdstr,'$EXE',current_module.sharedlibfilename^);
  447. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  448. Replace(cmdstr,'$RES',outputexedir+Info.ResName);
  449. Replace(cmdstr,'$INIT',InitStr);
  450. Replace(cmdstr,'$FINI',FiniStr);
  451. Replace(cmdstr,'$SONAME',SoNameStr);
  452. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  453. { Strip the library ? }
  454. if success and (cs_link_strip in aktglobalswitches) then
  455. begin
  456. SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  457. Replace(cmdstr,'$EXE',current_module.sharedlibfilename^);
  458. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  459. end;
  460. { Remove ReponseFile }
  461. if (success) and not(cs_link_extern in aktglobalswitches) then
  462. RemoveFile(outputexedir+Info.ResName);
  463. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  464. end;
  465. {*****************************************************************************
  466. Initialize
  467. *****************************************************************************}
  468. initialization
  469. {$ifdef i386}
  470. RegisterExternalLinker(system_i386_linux_info,TLinkerLinux);
  471. RegisterImport(system_i386_linux,timportliblinux);
  472. RegisterExport(system_i386_linux,texportliblinux);
  473. RegisterTarget(system_i386_linux_info);
  474. {$endif i386}
  475. {$ifdef m68k}
  476. RegisterExternalLinker(system_m68k_linux_info,TLinkerLinux);
  477. RegisterImport(system_m68k_linux,timportliblinux);
  478. RegisterExport(system_m68k_linux,texportliblinux);
  479. RegisterTarget(system_m68k_linux_info);
  480. {$endif m68k}
  481. {$ifdef powerpc}
  482. RegisterExternalLinker(system_powerpc_linux_info,TLinkerLinux);
  483. RegisterImport(system_powerpc_linux,timportliblinux);
  484. RegisterExport(system_powerpc_linux,texportliblinux);
  485. RegisterTarget(system_powerpc_linux_info);
  486. {$endif powerpc}
  487. {$ifdef alpha}
  488. RegisterExternalLinker(system_alpha_linux_info,TLinkerLinux);
  489. RegisterImport(system_alpha_linux,timportliblinux);
  490. RegisterExport(system_alpha_linux,texportliblinux);
  491. RegisterTarget(system_alpha_linux_info);
  492. {$endif alpha}
  493. {$ifdef x86_64}
  494. RegisterExternalLinker(system_x86_64_linux_info,TLinkerLinux);
  495. RegisterImport(system_x86_64_linux,timportliblinux);
  496. RegisterExport(system_x86_64_linux,texportliblinux);
  497. RegisterTarget(system_x86_64_linux_info);
  498. {$endif x86_64}
  499. {$ifdef SPARC}
  500. RegisterExternalLinker(system_sparc_linux_info,TLinkerLinux);
  501. RegisterImport(system_SPARC_linux,timportliblinux);
  502. RegisterExport(system_SPARC_linux,texportliblinux);
  503. RegisterTarget(system_SPARC_linux_info);
  504. {$endif SPARC}
  505. {$ifdef ARM}
  506. RegisterExternalLinker(system_arm_linux_info,TLinkerLinux);
  507. RegisterImport(system_arm_linux,timportliblinux);
  508. RegisterExport(system_arm_linux,texportliblinux);
  509. RegisterTarget(system_arm_linux_info);
  510. {$endif ARM}
  511. end.
  512. {
  513. $Log$
  514. Revision 1.24 2004-09-25 18:44:12 florian
  515. * fixed dyn. linker name for sparc
  516. Revision 1.23 2004/09/22 15:25:14 mazen
  517. * Fix error committing : previous version must be in branch USE_SYSUTILS
  518. Revision 1.21 2004/09/21 17:25:13 peter
  519. * paraloc branch merged
  520. Revision 1.20.4.1 2004/09/20 15:24:42 peter
  521. * remove gc-sections option until it really works
  522. Revision 1.20 2004/07/08 14:42:54 daniel
  523. * Uclibc detection
  524. Revision 1.19 2004/06/20 08:55:32 florian
  525. * logs truncated
  526. Revision 1.18 2004/06/16 20:07:11 florian
  527. * dwarf branch merged
  528. Revision 1.17.2.3 2004/05/10 21:28:35 peter
  529. * section_smartlink enabled for gas under linux
  530. Revision 1.17.2.2 2004/05/03 20:18:52 peter
  531. * fixes for tprintf
  532. Revision 1.17.2.1 2004/04/08 18:33:22 peter
  533. * rewrite of TAsmSection
  534. Revision 1.17 2004/03/06 20:35:20 florian
  535. * fixed arm compilation
  536. * cleaned up code generation for exported linux procedures
  537. }