t_linux.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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,
  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. end;
  47. implementation
  48. uses
  49. cutils,cclasses,
  50. verbose,systems,globtype,globals,
  51. symconst,script,
  52. fmodule,dos
  53. ,aasmbase,aasmtai,aasmcpu,cpubase,cgobj
  54. ,i_linux
  55. ;
  56. {*****************************************************************************
  57. TIMPORTLIBLINUX
  58. *****************************************************************************}
  59. procedure timportliblinux.preparelib(const s : string);
  60. begin
  61. end;
  62. procedure timportliblinux.importprocedure(aprocdef:tprocdef;const module:string;index:longint;const name:string);
  63. begin
  64. { insert sharedlibrary }
  65. current_module.linkothersharedlibs.add(SplitName(module),link_allways);
  66. end;
  67. procedure timportliblinux.importvariable(vs:tglobalvarsym;const name,module:string);
  68. begin
  69. { insert sharedlibrary }
  70. current_module.linkothersharedlibs.add(SplitName(module),link_allways);
  71. { reset the mangledname and turn off the dll_var option }
  72. vs.set_mangledname(name);
  73. exclude(vs.varoptions,vo_is_dll_var);
  74. end;
  75. procedure timportliblinux.generatelib;
  76. begin
  77. end;
  78. {*****************************************************************************
  79. TEXPORTLIBLINUX
  80. *****************************************************************************}
  81. procedure texportliblinux.preparelib(const s:string);
  82. begin
  83. end;
  84. procedure texportliblinux.exportprocedure(hp : texported_item);
  85. var
  86. hp2 : texported_item;
  87. begin
  88. { first test the index value }
  89. if (hp.options and eo_index)<>0 then
  90. begin
  91. Message1(parser_e_no_export_with_index_for_target,'linux');
  92. exit;
  93. end;
  94. { now place in correct order }
  95. hp2:=texported_item(current_module._exports.first);
  96. while assigned(hp2) and
  97. (hp.name^>hp2.name^) do
  98. hp2:=texported_item(hp2.next);
  99. { insert hp there !! }
  100. if assigned(hp2) and (hp2.name^=hp.name^) then
  101. begin
  102. { this is not allowed !! }
  103. Message1(parser_e_export_name_double,hp.name^);
  104. exit;
  105. end;
  106. if hp2=texported_item(current_module._exports.first) then
  107. current_module._exports.concat(hp)
  108. else if assigned(hp2) then
  109. begin
  110. hp.next:=hp2;
  111. hp.previous:=hp2.previous;
  112. if assigned(hp2.previous) then
  113. hp2.previous.next:=hp;
  114. hp2.previous:=hp;
  115. end
  116. else
  117. current_module._exports.concat(hp);
  118. end;
  119. procedure texportliblinux.exportvar(hp : texported_item);
  120. begin
  121. hp.is_var:=true;
  122. exportprocedure(hp);
  123. end;
  124. procedure texportliblinux.generatelib;
  125. var
  126. hp2 : texported_item;
  127. begin
  128. new_section(codesegment,sec_code,'',0);
  129. hp2:=texported_item(current_module._exports.first);
  130. while assigned(hp2) do
  131. begin
  132. if (not hp2.is_var) and
  133. (hp2.sym.typ=procsym) then
  134. begin
  135. { the manglednames can already be the same when the procedure
  136. is declared with cdecl }
  137. if tprocsym(hp2.sym).first_procdef.mangledname<>hp2.name^ then
  138. begin
  139. { place jump in codesegment }
  140. codesegment.concat(tai_align.create(target_info.alignment.procalign));
  141. codeSegment.concat(Tai_symbol.Createname_global(hp2.name^,AT_FUNCTION,0));
  142. cg.a_jmp_name(codesegment,tprocsym(hp2.sym).first_procdef.mangledname);
  143. codeSegment.concat(Tai_symbol_end.Createname(hp2.name^));
  144. end;
  145. end
  146. else
  147. Message1(parser_e_no_export_of_variables_for_target,'linux');
  148. hp2:=texported_item(hp2.next);
  149. end;
  150. end;
  151. {*****************************************************************************
  152. TLINKERLINUX
  153. *****************************************************************************}
  154. Constructor TLinkerLinux.Create;
  155. begin
  156. Inherited Create;
  157. if not Dontlinkstdlibpath Then
  158. {$ifdef x86_64}
  159. LibrarySearchPath.AddPath('/lib64;/usr/lib64;/usr/X11R6/lib64',true);
  160. {$else}
  161. LibrarySearchPath.AddPath('/lib;/usr/lib;/usr/X11R6/lib',true);
  162. {$endif x86_64}
  163. end;
  164. procedure TLinkerLinux.SetDefaultInfo;
  165. {
  166. This will also detect which libc version will be used
  167. }
  168. const
  169. {$ifdef i386} platform_select='-b elf32-i386 -m elf_i386';{$endif}
  170. {$ifdef x86_64} platform_select='-b elf64-x86-64 -m elf_x86_64';{$endif}
  171. {$ifdef powerpc}platform_select='-b elf32-powerpc -m elf32ppclinux';{$endif}
  172. {$ifdef sparc} platform_select='-b elf32-sparc -m elf32_sparc';{$endif}
  173. {$ifdef arm} platform_select='';{$endif} {unknown :( }
  174. {$ifdef m68k}
  175. var
  176. St : SearchRec;
  177. {$endif m68k}
  178. begin
  179. with Info do
  180. begin
  181. ExeCmd[1]:='ld '+platform_select+' $OPT $DYNLINK $STATIC $GCSECTIONS $STRIP -L. -o $EXE $RES';
  182. DllCmd[1]:='ld '+platform_select+' $OPT $INIT $FINI $SONAME -shared -L. -o $EXE $RES';
  183. DllCmd[2]:='strip --strip-unneeded $EXE';
  184. {$ifdef m68k}
  185. libctype:=glibc2;
  186. FindFirst('/lib/ld*',AnyFile,st);
  187. while DosError=0 do
  188. begin
  189. if copy(st.name,1,5)='ld-2.' then
  190. begin
  191. DynamicLinker:='/lib/'+St.name;
  192. if st.name[6]<>'0' then
  193. libctype:=glibc21;
  194. break;
  195. end;
  196. FindNext(St);
  197. end;
  198. FindClose(St);
  199. {$endif m68k}
  200. {$ifdef i386}
  201. { first try glibc2 }
  202. DynamicLinker:='/lib/ld-linux.so.2';
  203. if FileExists(DynamicLinker) then
  204. { Check for 2.0 files, else use the glibc 2.1 stub }
  205. if FileExists('/lib/ld-2.0.*') then
  206. libctype:=glibc2
  207. else
  208. libctype:=glibc21
  209. else
  210. if fileexists('/lib/ld-uClibc.so.0') then
  211. begin
  212. libctype:=uclibc;
  213. dynamiclinker:='/lib/ld-uClibc.so.0';
  214. end
  215. else
  216. DynamicLinker:='/lib/ld-linux.so.1';
  217. {$endif i386}
  218. {$ifdef x86_64}
  219. DynamicLinker:='/lib64/ld-linux-x86-64.so.2';
  220. libctype:=glibc2;
  221. {$endif x86_64}
  222. {$ifdef sparc}
  223. DynamicLinker:='/lib/ld-linux.so.2';
  224. libctype:=glibc2;
  225. {$endif sparc}
  226. {$ifdef powerpc}
  227. DynamicLinker:='/lib/ld.so.1';
  228. libctype:=glibc2;
  229. {$endif powerpc}
  230. {$ifdef arm}
  231. DynamicLinker:='/lib/ld-linux.so.2';
  232. libctype:=glibc2;
  233. {$endif arm}
  234. end;
  235. end;
  236. Function TLinkerLinux.WriteResponseFile(isdll:boolean) : Boolean;
  237. Var
  238. linkres : TLinkRes;
  239. i : longint;
  240. cprtobj,
  241. gprtobj,
  242. prtobj : string[80];
  243. HPath : TStringListItem;
  244. s,s1,s2 : string;
  245. found1,
  246. found2,
  247. linklibc : boolean;
  248. begin
  249. WriteResponseFile:=False;
  250. { set special options for some targets }
  251. linklibc:=(SharedLibFiles.Find('c')<>nil);
  252. if isdll then
  253. begin
  254. prtobj:='dllprt0';
  255. cprtobj:='dllprt0';
  256. gprtobj:='dllprt0';
  257. end
  258. else
  259. begin
  260. prtobj:='prt0';
  261. case libctype of
  262. glibc21:
  263. begin
  264. cprtobj:='cprt21';
  265. gprtobj:='gprt21';
  266. end;
  267. uclibc:
  268. begin
  269. cprtobj:='ucprt0';
  270. gprtobj:='ugprt0';
  271. end
  272. else
  273. cprtobj:='cprt0';
  274. gprtobj:='gprt0';
  275. end;
  276. end;
  277. if cs_profile in aktmoduleswitches then
  278. begin
  279. prtobj:=gprtobj;
  280. if not(libctype in [glibc2,glibc21]) then
  281. AddSharedLibrary('gmon');
  282. AddSharedLibrary('c');
  283. linklibc:=true;
  284. end
  285. else
  286. begin
  287. if linklibc then
  288. prtobj:=cprtobj;
  289. end;
  290. { Open link.res file }
  291. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName);
  292. { Write path to search libraries }
  293. HPath:=TStringListItem(current_module.locallibrarysearchpath.First);
  294. while assigned(HPath) do
  295. begin
  296. LinkRes.Add('SEARCH_DIR('+maybequoted(HPath.Str)+')');
  297. HPath:=TStringListItem(HPath.Next);
  298. end;
  299. HPath:=TStringListItem(LibrarySearchPath.First);
  300. while assigned(HPath) do
  301. begin
  302. LinkRes.Add('SEARCH_DIR('+maybequoted(HPath.Str)+')');
  303. HPath:=TStringListItem(HPath.Next);
  304. end;
  305. LinkRes.Add('INPUT(');
  306. { add objectfiles, start with prt0 always }
  307. if prtobj<>'' then
  308. LinkRes.AddFileName(maybequoted(FindObjectFile(prtobj,'',false)));
  309. { try to add crti and crtbegin if linking to C }
  310. if linklibc then
  311. begin
  312. if librarysearchpath.FindFile('crtbegin.o',s) then
  313. LinkRes.AddFileName(s);
  314. if librarysearchpath.FindFile('crti.o',s) then
  315. LinkRes.AddFileName(s);
  316. end;
  317. { main objectfiles }
  318. while not ObjectFiles.Empty do
  319. begin
  320. s:=ObjectFiles.GetFirst;
  321. if s<>'' then
  322. LinkRes.AddFileName(maybequoted(s));
  323. end;
  324. LinkRes.Add(')');
  325. { Write staticlibraries }
  326. if not StaticLibFiles.Empty then
  327. begin
  328. LinkRes.Add('GROUP(');
  329. While not StaticLibFiles.Empty do
  330. begin
  331. S:=StaticLibFiles.GetFirst;
  332. LinkRes.AddFileName(maybequoted(s))
  333. end;
  334. LinkRes.Add(')');
  335. end;
  336. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  337. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  338. if not SharedLibFiles.Empty then
  339. begin
  340. LinkRes.Add('INPUT(');
  341. While not SharedLibFiles.Empty do
  342. begin
  343. S:=SharedLibFiles.GetFirst;
  344. if s<>'c' then
  345. begin
  346. i:=Pos(target_info.sharedlibext,S);
  347. if i>0 then
  348. Delete(S,i,255);
  349. LinkRes.Add('-l'+s);
  350. end
  351. else
  352. begin
  353. linklibc:=true;
  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. LinkRes.Add(')');
  363. end;
  364. { objects which must be at the end }
  365. if linklibc and (libctype<>uclibc) then
  366. begin
  367. found1:=librarysearchpath.FindFile('crtend.o',s1);
  368. found2:=librarysearchpath.FindFile('crtn.o',s2);
  369. if found1 or found2 then
  370. begin
  371. LinkRes.Add('INPUT(');
  372. if found1 then
  373. LinkRes.AddFileName(s1);
  374. if found2 then
  375. LinkRes.AddFileName(s2);
  376. LinkRes.Add(')');
  377. end;
  378. end;
  379. { Write and Close response }
  380. linkres.writetodisk;
  381. linkres.Free;
  382. WriteResponseFile:=True;
  383. end;
  384. function TLinkerLinux.MakeExecutable:boolean;
  385. var
  386. binstr : String;
  387. cmdstr : TCmdStr;
  388. success : boolean;
  389. DynLinkStr : string[60];
  390. GCSectionsStr,
  391. StaticStr,
  392. StripStr : string[40];
  393. begin
  394. if not(cs_link_extern in aktglobalswitches) then
  395. Message1(exec_i_linking,current_module.exefilename^);
  396. { Create some replacements }
  397. StaticStr:='';
  398. StripStr:='';
  399. GCSectionsStr:='';
  400. DynLinkStr:='';
  401. if (cs_link_staticflag in aktglobalswitches) then
  402. StaticStr:='-static';
  403. if (cs_link_strip in aktglobalswitches) then
  404. StripStr:='-s';
  405. if (cs_link_smart in aktglobalswitches) and
  406. (tf_smartlink_sections in target_info.flags) then
  407. GCSectionsStr:='--gc-sections';
  408. If (cs_profile in aktmoduleswitches) or
  409. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty)) then
  410. begin
  411. DynLinkStr:='-dynamic-linker='+Info.DynamicLinker;
  412. if cshared Then
  413. DynLinkStr:='--shared ' + DynLinkStr;
  414. if rlinkpath<>'' Then
  415. DynLinkStr:='--rpath-link '+rlinkpath + ' '+ DynLinkStr;
  416. End;
  417. { Write used files and libraries }
  418. WriteResponseFile(false);
  419. { Call linker }
  420. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  421. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename^));
  422. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  423. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  424. Replace(cmdstr,'$STATIC',StaticStr);
  425. Replace(cmdstr,'$STRIP',StripStr);
  426. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  427. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  428. success:=DoExec(FindUtil(utilsprefix+BinStr),CmdStr,true,false);
  429. { Remove ReponseFile }
  430. if (success) and not(cs_link_extern in aktglobalswitches) then
  431. RemoveFile(outputexedir+Info.ResName);
  432. MakeExecutable:=success; { otherwise a recursive call to link method }
  433. end;
  434. Function TLinkerLinux.MakeSharedLibrary:boolean;
  435. var
  436. InitStr,
  437. FiniStr,
  438. SoNameStr : string[80];
  439. binstr : String;
  440. cmdstr : TCmdStr;
  441. success : boolean;
  442. begin
  443. MakeSharedLibrary:=false;
  444. if not(cs_link_extern in aktglobalswitches) then
  445. Message1(exec_i_linking,current_module.sharedlibfilename^);
  446. { Write used files and libraries }
  447. WriteResponseFile(true);
  448. { Create some replacements }
  449. InitStr:='-init FPC_LIB_START';
  450. FiniStr:='-fini FPC_LIB_EXIT';
  451. SoNameStr:='-soname '+SplitFileName(current_module.sharedlibfilename^);
  452. { Call linker }
  453. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  454. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename^));
  455. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  456. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  457. Replace(cmdstr,'$INIT',InitStr);
  458. Replace(cmdstr,'$FINI',FiniStr);
  459. Replace(cmdstr,'$SONAME',SoNameStr);
  460. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  461. { Strip the library ? }
  462. if success and (cs_link_strip in aktglobalswitches) then
  463. begin
  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. {*****************************************************************************
  474. Initialize
  475. *****************************************************************************}
  476. initialization
  477. {$ifdef i386}
  478. RegisterExternalLinker(system_i386_linux_info,TLinkerLinux);
  479. RegisterImport(system_i386_linux,timportliblinux);
  480. RegisterExport(system_i386_linux,texportliblinux);
  481. RegisterTarget(system_i386_linux_info);
  482. {$endif i386}
  483. {$ifdef m68k}
  484. RegisterExternalLinker(system_m68k_linux_info,TLinkerLinux);
  485. RegisterImport(system_m68k_linux,timportliblinux);
  486. RegisterExport(system_m68k_linux,texportliblinux);
  487. RegisterTarget(system_m68k_linux_info);
  488. {$endif m68k}
  489. {$ifdef powerpc}
  490. RegisterExternalLinker(system_powerpc_linux_info,TLinkerLinux);
  491. RegisterImport(system_powerpc_linux,timportliblinux);
  492. RegisterExport(system_powerpc_linux,texportliblinux);
  493. RegisterTarget(system_powerpc_linux_info);
  494. {$endif powerpc}
  495. {$ifdef alpha}
  496. RegisterExternalLinker(system_alpha_linux_info,TLinkerLinux);
  497. RegisterImport(system_alpha_linux,timportliblinux);
  498. RegisterExport(system_alpha_linux,texportliblinux);
  499. RegisterTarget(system_alpha_linux_info);
  500. {$endif alpha}
  501. {$ifdef x86_64}
  502. RegisterExternalLinker(system_x86_64_linux_info,TLinkerLinux);
  503. RegisterImport(system_x86_64_linux,timportliblinux);
  504. RegisterExport(system_x86_64_linux,texportliblinux);
  505. RegisterTarget(system_x86_64_linux_info);
  506. {$endif x86_64}
  507. {$ifdef SPARC}
  508. RegisterExternalLinker(system_sparc_linux_info,TLinkerLinux);
  509. RegisterImport(system_SPARC_linux,timportliblinux);
  510. RegisterExport(system_SPARC_linux,texportliblinux);
  511. RegisterTarget(system_SPARC_linux_info);
  512. {$endif SPARC}
  513. {$ifdef ARM}
  514. RegisterExternalLinker(system_arm_linux_info,TLinkerLinux);
  515. RegisterImport(system_arm_linux,timportliblinux);
  516. RegisterExport(system_arm_linux,texportliblinux);
  517. RegisterTarget(system_arm_linux_info);
  518. {$endif ARM}
  519. end.