t_linux.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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. {$else 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. {$else i386}
  215. {$ifdef x86_64}
  216. DynamicLinker:='/lib/ld-linux-x86-64.so.2';
  217. libctype:=glibc2;
  218. {$else x86_64}
  219. DynamicLinker:='/lib/ld.so.1';
  220. libctype:=glibc2;
  221. {$endif x86_64}
  222. {$endif i386}
  223. {$endif m68k}
  224. end;
  225. end;
  226. Function TLinkerLinux.WriteResponseFile(isdll:boolean) : Boolean;
  227. Var
  228. linkres : TLinkRes;
  229. i : longint;
  230. cprtobj,
  231. gprtobj,
  232. prtobj : string[80];
  233. HPath : TStringListItem;
  234. s,s1,s2 : string;
  235. found1,
  236. found2,
  237. linkdynamic,
  238. linklibc : boolean;
  239. begin
  240. WriteResponseFile:=False;
  241. { set special options for some targets }
  242. linkdynamic:=not(SharedLibFiles.empty);
  243. linklibc:=(SharedLibFiles.Find('c')<>nil);
  244. if isdll then
  245. begin
  246. prtobj:='dllprt0';
  247. cprtobj:='dllprt0';
  248. gprtobj:='dllprt0';
  249. end
  250. else
  251. begin
  252. prtobj:='prt0';
  253. case libctype of
  254. glibc21:
  255. begin
  256. cprtobj:='cprt21';
  257. gprtobj:='gprt21';
  258. end;
  259. uclibc:
  260. begin
  261. cprtobj:='ucprt0';
  262. gprtobj:='ugprt0';
  263. end
  264. else
  265. cprtobj:='cprt0';
  266. gprtobj:='gprt0';
  267. end;
  268. end;
  269. if cs_profile in aktmoduleswitches then
  270. begin
  271. prtobj:=gprtobj;
  272. if not(libctype in [glibc2,glibc21]) then
  273. AddSharedLibrary('gmon');
  274. AddSharedLibrary('c');
  275. linklibc:=true;
  276. end
  277. else
  278. begin
  279. if linklibc then
  280. prtobj:=cprtobj;
  281. end;
  282. { Open link.res file }
  283. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName);
  284. { Write path to search libraries }
  285. HPath:=TStringListItem(current_module.locallibrarysearchpath.First);
  286. while assigned(HPath) do
  287. begin
  288. LinkRes.Add('SEARCH_DIR('+HPath.Str+')');
  289. HPath:=TStringListItem(HPath.Next);
  290. end;
  291. HPath:=TStringListItem(LibrarySearchPath.First);
  292. while assigned(HPath) do
  293. begin
  294. LinkRes.Add('SEARCH_DIR('+HPath.Str+')');
  295. HPath:=TStringListItem(HPath.Next);
  296. end;
  297. LinkRes.Add('INPUT(');
  298. { add objectfiles, start with prt0 always }
  299. if prtobj<>'' then
  300. LinkRes.AddFileName(FindObjectFile(prtobj,'',false));
  301. { try to add crti and crtbegin if linking to C }
  302. if linklibc then
  303. begin
  304. if librarysearchpath.FindFile('crtbegin.o',s) then
  305. LinkRes.AddFileName(s);
  306. if librarysearchpath.FindFile('crti.o',s) then
  307. LinkRes.AddFileName(s);
  308. end;
  309. { main objectfiles }
  310. while not ObjectFiles.Empty do
  311. begin
  312. s:=ObjectFiles.GetFirst;
  313. if s<>'' then
  314. LinkRes.AddFileName(s);
  315. end;
  316. LinkRes.Add(')');
  317. { Write staticlibraries }
  318. if not StaticLibFiles.Empty then
  319. begin
  320. LinkRes.Add('GROUP(');
  321. While not StaticLibFiles.Empty do
  322. begin
  323. S:=StaticLibFiles.GetFirst;
  324. LinkRes.AddFileName(s)
  325. end;
  326. LinkRes.Add(')');
  327. end;
  328. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  329. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  330. if not SharedLibFiles.Empty then
  331. begin
  332. LinkRes.Add('INPUT(');
  333. While not SharedLibFiles.Empty do
  334. begin
  335. S:=SharedLibFiles.GetFirst;
  336. if s<>'c' then
  337. begin
  338. i:=Pos(target_info.sharedlibext,S);
  339. if i>0 then
  340. Delete(S,i,255);
  341. LinkRes.Add('-l'+s);
  342. end
  343. else
  344. begin
  345. linklibc:=true;
  346. linkdynamic:=false; { libc will include the ld-linux for us }
  347. end;
  348. end;
  349. { be sure that libc is the last lib }
  350. if linklibc then
  351. LinkRes.Add('-lc');
  352. { when we have -static for the linker the we also need libgcc }
  353. if (cs_link_staticflag in aktglobalswitches) then
  354. LinkRes.Add('-lgcc');
  355. if linkdynamic and (Info.DynamicLinker<>'') then
  356. LinkRes.AddFileName(Info.DynamicLinker);
  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,
  382. cmdstr : string;
  383. success : boolean;
  384. DynLinkStr : string[60];
  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. DynLinkStr:='';
  394. if (cs_link_staticflag in aktglobalswitches) then
  395. StaticStr:='-static';
  396. if (cs_link_strip in aktglobalswitches) then
  397. StripStr:='-s';
  398. If (cs_profile in aktmoduleswitches) or
  399. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty)) then
  400. begin
  401. DynLinkStr:='-dynamic-linker='+Info.DynamicLinker;
  402. if cshared Then
  403. DynLinkStr:='--shared ' + DynLinkStr;
  404. if rlinkpath<>'' Then
  405. DynLinkStr:='--rpath-link '+rlinkpath + ' '+ DynLinkStr;
  406. End;
  407. { Write used files and libraries }
  408. WriteResponseFile(false);
  409. { Call linker }
  410. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  411. Replace(cmdstr,'$EXE',current_module.exefilename^);
  412. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  413. Replace(cmdstr,'$RES',outputexedir+Info.ResName);
  414. Replace(cmdstr,'$STATIC',StaticStr);
  415. Replace(cmdstr,'$STRIP',StripStr);
  416. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  417. success:=DoExec(FindUtil(utilsprefix+BinStr),CmdStr,true,false);
  418. { Remove ReponseFile }
  419. if (success) and not(cs_link_extern in aktglobalswitches) then
  420. RemoveFile(outputexedir+Info.ResName);
  421. MakeExecutable:=success; { otherwise a recursive call to link method }
  422. end;
  423. Function TLinkerLinux.MakeSharedLibrary:boolean;
  424. var
  425. InitStr,
  426. FiniStr,
  427. SoNameStr : string[80];
  428. binstr,
  429. cmdstr : string;
  430. success : boolean;
  431. begin
  432. MakeSharedLibrary:=false;
  433. if not(cs_link_extern in aktglobalswitches) then
  434. Message1(exec_i_linking,current_module.sharedlibfilename^);
  435. { Write used files and libraries }
  436. WriteResponseFile(true);
  437. { Create some replacements }
  438. InitStr:='-init FPC_LIB_START';
  439. FiniStr:='-fini FPC_LIB_EXIT';
  440. SoNameStr:='-soname '+SplitFileName(current_module.sharedlibfilename^);
  441. { Call linker }
  442. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  443. Replace(cmdstr,'$EXE',current_module.sharedlibfilename^);
  444. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  445. Replace(cmdstr,'$RES',outputexedir+Info.ResName);
  446. Replace(cmdstr,'$INIT',InitStr);
  447. Replace(cmdstr,'$FINI',FiniStr);
  448. Replace(cmdstr,'$SONAME',SoNameStr);
  449. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  450. { Strip the library ? }
  451. if success and (cs_link_strip in aktglobalswitches) then
  452. begin
  453. SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  454. Replace(cmdstr,'$EXE',current_module.sharedlibfilename^);
  455. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  456. end;
  457. { Remove ReponseFile }
  458. if (success) and not(cs_link_extern in aktglobalswitches) then
  459. RemoveFile(outputexedir+Info.ResName);
  460. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  461. end;
  462. {*****************************************************************************
  463. Initialize
  464. *****************************************************************************}
  465. initialization
  466. {$ifdef i386}
  467. RegisterExternalLinker(system_i386_linux_info,TLinkerLinux);
  468. RegisterImport(system_i386_linux,timportliblinux);
  469. RegisterExport(system_i386_linux,texportliblinux);
  470. RegisterTarget(system_i386_linux_info);
  471. {$endif i386}
  472. {$ifdef m68k}
  473. RegisterExternalLinker(system_m68k_linux_info,TLinkerLinux);
  474. RegisterImport(system_m68k_linux,timportliblinux);
  475. RegisterExport(system_m68k_linux,texportliblinux);
  476. RegisterTarget(system_m68k_linux_info);
  477. {$endif m68k}
  478. {$ifdef powerpc}
  479. RegisterExternalLinker(system_powerpc_linux_info,TLinkerLinux);
  480. RegisterImport(system_powerpc_linux,timportliblinux);
  481. RegisterExport(system_powerpc_linux,texportliblinux);
  482. RegisterTarget(system_powerpc_linux_info);
  483. {$endif powerpc}
  484. {$ifdef alpha}
  485. RegisterExternalLinker(system_alpha_linux_info,TLinkerLinux);
  486. RegisterImport(system_alpha_linux,timportliblinux);
  487. RegisterExport(system_alpha_linux,texportliblinux);
  488. RegisterTarget(system_alpha_linux_info);
  489. {$endif alpha}
  490. {$ifdef x86_64}
  491. RegisterExternalLinker(system_x86_64_linux_info,TLinkerLinux);
  492. RegisterImport(system_x86_64_linux,timportliblinux);
  493. RegisterExport(system_x86_64_linux,texportliblinux);
  494. RegisterTarget(system_x86_64_linux_info);
  495. {$endif x86_64}
  496. {$ifdef SPARC}
  497. RegisterExternalLinker(system_sparc_linux_info,TLinkerLinux);
  498. RegisterImport(system_SPARC_linux,timportliblinux);
  499. RegisterExport(system_SPARC_linux,texportliblinux);
  500. RegisterTarget(system_SPARC_linux_info);
  501. {$endif SPARC}
  502. {$ifdef ARM}
  503. RegisterExternalLinker(system_arm_linux_info,TLinkerLinux);
  504. RegisterImport(system_arm_linux,timportliblinux);
  505. RegisterExport(system_arm_linux,texportliblinux);
  506. RegisterTarget(system_arm_linux_info);
  507. {$endif ARM}
  508. end.
  509. {
  510. $Log$
  511. Revision 1.21 2004-09-21 17:25:13 peter
  512. * paraloc branch merged
  513. Revision 1.20.4.1 2004/09/20 15:24:42 peter
  514. * remove gc-sections option until it really works
  515. Revision 1.20 2004/07/08 14:42:54 daniel
  516. * Uclibc detection
  517. Revision 1.19 2004/06/20 08:55:32 florian
  518. * logs truncated
  519. Revision 1.18 2004/06/16 20:07:11 florian
  520. * dwarf branch merged
  521. Revision 1.17.2.3 2004/05/10 21:28:35 peter
  522. * section_smartlink enabled for gas under linux
  523. Revision 1.17.2.2 2004/05/03 20:18:52 peter
  524. * fixes for tprintf
  525. Revision 1.17.2.1 2004/04/08 18:33:22 peter
  526. * rewrite of TAsmSection
  527. Revision 1.17 2004/03/06 20:35:20 florian
  528. * fixed arm compilation
  529. * cleaned up code generation for exported linux procedures
  530. }