t_linux.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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:tglobalvarsym;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. 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. MakeExecutable:=success; { otherwise a recursive call to link method }
  428. end;
  429. Function TLinkerLinux.MakeSharedLibrary:boolean;
  430. var
  431. InitStr,
  432. FiniStr,
  433. SoNameStr : string[80];
  434. binstr : String;
  435. cmdstr : TCmdStr;
  436. success : boolean;
  437. begin
  438. MakeSharedLibrary:=false;
  439. if not(cs_link_extern in aktglobalswitches) then
  440. Message1(exec_i_linking,current_module.sharedlibfilename^);
  441. { Write used files and libraries }
  442. WriteResponseFile(true);
  443. { Create some replacements }
  444. InitStr:='-init FPC_LIB_START';
  445. FiniStr:='-fini FPC_LIB_EXIT';
  446. SoNameStr:='-soname '+SplitFileName(current_module.sharedlibfilename^);
  447. { Call linker }
  448. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  449. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename^));
  450. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  451. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  452. Replace(cmdstr,'$INIT',InitStr);
  453. Replace(cmdstr,'$FINI',FiniStr);
  454. Replace(cmdstr,'$SONAME',SoNameStr);
  455. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  456. { Strip the library ? }
  457. if success and (cs_link_strip in aktglobalswitches) then
  458. begin
  459. SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  460. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename^));
  461. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  462. end;
  463. { Remove ReponseFile }
  464. if (success) and not(cs_link_extern in aktglobalswitches) then
  465. RemoveFile(outputexedir+Info.ResName);
  466. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  467. end;
  468. {*****************************************************************************
  469. Initialize
  470. *****************************************************************************}
  471. initialization
  472. {$ifdef i386}
  473. RegisterExternalLinker(system_i386_linux_info,TLinkerLinux);
  474. RegisterImport(system_i386_linux,timportliblinux);
  475. RegisterExport(system_i386_linux,texportliblinux);
  476. RegisterTarget(system_i386_linux_info);
  477. {$endif i386}
  478. {$ifdef m68k}
  479. RegisterExternalLinker(system_m68k_linux_info,TLinkerLinux);
  480. RegisterImport(system_m68k_linux,timportliblinux);
  481. RegisterExport(system_m68k_linux,texportliblinux);
  482. RegisterTarget(system_m68k_linux_info);
  483. {$endif m68k}
  484. {$ifdef powerpc}
  485. RegisterExternalLinker(system_powerpc_linux_info,TLinkerLinux);
  486. RegisterImport(system_powerpc_linux,timportliblinux);
  487. RegisterExport(system_powerpc_linux,texportliblinux);
  488. RegisterTarget(system_powerpc_linux_info);
  489. {$endif powerpc}
  490. {$ifdef alpha}
  491. RegisterExternalLinker(system_alpha_linux_info,TLinkerLinux);
  492. RegisterImport(system_alpha_linux,timportliblinux);
  493. RegisterExport(system_alpha_linux,texportliblinux);
  494. RegisterTarget(system_alpha_linux_info);
  495. {$endif alpha}
  496. {$ifdef x86_64}
  497. RegisterExternalLinker(system_x86_64_linux_info,TLinkerLinux);
  498. RegisterImport(system_x86_64_linux,timportliblinux);
  499. RegisterExport(system_x86_64_linux,texportliblinux);
  500. RegisterTarget(system_x86_64_linux_info);
  501. {$endif x86_64}
  502. {$ifdef SPARC}
  503. RegisterExternalLinker(system_sparc_linux_info,TLinkerLinux);
  504. RegisterImport(system_SPARC_linux,timportliblinux);
  505. RegisterExport(system_SPARC_linux,texportliblinux);
  506. RegisterTarget(system_SPARC_linux_info);
  507. {$endif SPARC}
  508. {$ifdef ARM}
  509. RegisterExternalLinker(system_arm_linux_info,TLinkerLinux);
  510. RegisterImport(system_arm_linux,timportliblinux);
  511. RegisterExport(system_arm_linux,texportliblinux);
  512. RegisterTarget(system_arm_linux_info);
  513. {$endif ARM}
  514. end.
  515. {
  516. $Log$
  517. Revision 1.35 2005-03-07 17:10:00 peter
  518. * use lib64 for x86_64
  519. Revision 1.34 2005/02/14 17:13:10 peter
  520. * truncate log
  521. Revision 1.33 2005/01/10 15:56:13 peter
  522. * generate export jmps in .text section
  523. }