t_bsd.pas 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. {
  2. Copyright (c) 1998-2002 by Peter Vreman (original Linux)
  3. (c) 2000 by Marco van de Voort (FreeBSD mods)
  4. This unit implements support import,export,link routines
  5. for the (i386)FreeBSD 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_bsd;
  20. {$i fpcdefs.inc}
  21. interface
  22. implementation
  23. uses
  24. sysutils,
  25. cutils,cfileutl,cclasses,
  26. verbose,systems,globtype,globals,
  27. symconst,script,
  28. fmodule,aasmbase,aasmtai,aasmdata,aasmcpu,cpubase,symsym,symdef,
  29. import,export,link,comprsrc,rescmn,i_bsd,expunix,
  30. cgutils,cgbase,cgobj,cpuinfo,ogbase;
  31. type
  32. timportlibdarwin=class(timportlib)
  33. procedure generatelib;override;
  34. end;
  35. timportlibbsd=class(timportlib)
  36. procedure generatelib;override;
  37. end;
  38. texportlibbsd=class(texportlibunix)
  39. end;
  40. texportlibdarwin=class(texportlibbsd)
  41. procedure setinitname(list: TAsmList; const s: string); override;
  42. procedure setfininame(list: TAsmList; const s: string); override;
  43. end;
  44. tlinkerbsd=class(texternallinker)
  45. private
  46. LdSupportsNoResponseFile : boolean;
  47. LibrarySuffix : Char;
  48. Function WriteResponseFile(isdll:boolean) : Boolean;
  49. public
  50. constructor Create;override;
  51. procedure SetDefaultInfo;override;
  52. function MakeExecutable:boolean;override;
  53. function MakeSharedLibrary:boolean;override;
  54. procedure LoadPredefinedLibraryOrder; override;
  55. end;
  56. {*****************************************************************************
  57. TIMPORTLIBDARWIN
  58. *****************************************************************************}
  59. procedure timportlibdarwin.generatelib;
  60. begin
  61. end;
  62. {*****************************************************************************
  63. TEXPORTLIBDARWIN
  64. *****************************************************************************}
  65. procedure texportlibdarwin.setinitname(list: TAsmList; const s: string);
  66. begin
  67. list.concat(tai_directive.create(asd_mod_init_func,''));
  68. list.concat(tai_align.create(sizeof(pint)));
  69. list.concat(Tai_const.Createname(s,0));
  70. end;
  71. procedure texportlibdarwin.setfininame(list: TAsmList; const s: string);
  72. begin
  73. list.concat(tai_directive.create(asd_mod_term_func,''));
  74. list.concat(tai_align.create(sizeof(pint)));
  75. list.concat(Tai_const.Createname(s,0));
  76. end;
  77. {*****************************************************************************
  78. TIMPORTLIBBSD
  79. *****************************************************************************}
  80. procedure timportlibbsd.generatelib;
  81. var
  82. i : longint;
  83. ImportLibrary : TImportLibrary;
  84. begin
  85. for i:=0 to current_module.ImportLibraryList.Count-1 do
  86. begin
  87. ImportLibrary:=TImportLibrary(current_module.ImportLibraryList[i]);
  88. current_module.linkothersharedlibs.add(ImportLibrary.Name,link_always);
  89. end;
  90. end;
  91. {*****************************************************************************
  92. TLINKERBSD
  93. *****************************************************************************}
  94. Constructor TLinkerBSD.Create;
  95. begin
  96. Inherited Create;
  97. if not Dontlinkstdlibpath Then
  98. if not(target_info.system in systems_darwin) then
  99. LibrarySearchPath.AddPath(sysrootpath,'/lib;/usr/lib;/usr/X11R6/lib',true)
  100. else
  101. { Mac OS X doesn't have a /lib }
  102. LibrarySearchPath.AddPath(sysrootpath,'/usr/lib',true)
  103. end;
  104. procedure TLinkerBSD.SetDefaultInfo;
  105. {
  106. This will also detect which libc version will be used
  107. }
  108. begin
  109. LibrarySuffix:=' ';
  110. LdSupportsNoResponseFile := (target_info.system in ([system_m68k_netbsd]+systems_darwin));
  111. with Info do
  112. begin
  113. if LdSupportsNoResponseFile then
  114. begin
  115. if not(target_info.system in systems_darwin) then
  116. begin
  117. ExeCmd[1]:='ld $OPT $DYNLINK $STATIC $GCSECTIONS $STRIP -L. -o $EXE `cat $RES`';
  118. DllCmd[1]:='ld $OPT -shared -L. -o $EXE `cat $RES`'
  119. end
  120. else
  121. begin
  122. ExeCmd[1]:='ld $OPT $DYNLINK $STATIC $GCSECTIONS $STRIP -multiply_defined suppress -L. -o $EXE `cat $RES`';
  123. if (apptype<>app_bundle) then
  124. DllCmd[1]:='libtool $OPT -dynamic -multiply_defined suppress -L. -o $EXE `cat $RES`'
  125. else
  126. DllCmd[1]:='ld $OPT -dynamic -bundle -multiply_defined suppress -L. -o $EXE `cat $RES`'
  127. end
  128. end
  129. else
  130. begin
  131. ExeCmd[1]:='ld $OPT $DYNLINK $STATIC $GCSECTIONS $STRIP -L. -o $EXE $RES';
  132. DllCmd[1]:='ld $OPT $INIT $FINI $SONAME -shared -L. -o $EXE $RES';
  133. end;
  134. if not(target_info.system in systems_darwin) then
  135. DllCmd[2]:='strip --strip-unneeded $EXE'
  136. else
  137. DllCmd[2]:='strip -x $EXE';
  138. DynamicLinker:='';
  139. end;
  140. end;
  141. procedure TLinkerBSD.LoadPredefinedLibraryOrder;
  142. // put your linkorder/linkalias overrides here.
  143. // Note: assumes only called when reordering/aliasing is used.
  144. Begin
  145. if not(target_info.system in systems_darwin) then
  146. begin
  147. if (target_info.system =system_i386_freebsd) and
  148. not (cs_link_no_default_lib_order in current_settings.globalswitches) Then
  149. Begin
  150. LinkLibraryOrder.add('gcc','',15);
  151. LinkLibraryOrder.add('c','',50); // c and c_p mutual. excl?
  152. LinkLibraryOrder.add('c_p','',55);
  153. LinkLibraryOrder.add('pthread','',75); // pthread and c_r should be mutually exclusive
  154. LinkLibraryOrder.add('c_r','',76);
  155. LinkLibraryOrder.add('kvm','',80); // must be before ncurses
  156. if (cs_link_pthread in current_settings.globalswitches) Then // convert libpthread to libc_r.
  157. LinkLibraryAliases.add('pthread','c_r');
  158. end;
  159. end
  160. else
  161. begin
  162. LinkLibraryOrder.add('gcc','',15);
  163. LinkLibraryOrder.add('c','',50);
  164. end;
  165. End;
  166. Function TLinkerBSD.WriteResponseFile(isdll:boolean) : Boolean;
  167. Var
  168. linkres : TLinkRes;
  169. i : longint;
  170. cprtobj,
  171. gprtobj,
  172. prtobj : string[80];
  173. HPath : TCmdStrListItem;
  174. s,s1,s2 : TCmdStr;
  175. linkdynamic,
  176. linklibc : boolean;
  177. Fl1,Fl2 : Boolean;
  178. IsDarwin : Boolean;
  179. ReOrder : Boolean;
  180. begin
  181. WriteResponseFile:=False;
  182. ReOrder:=False;
  183. IsDarwin:=target_info.system in systems_darwin;
  184. { set special options for some targets }
  185. if not IsDarwin Then
  186. begin
  187. prtobj:='prt0';
  188. cprtobj:='cprt0';
  189. gprtobj:='gprt0';
  190. linkdynamic:=not(SharedLibFiles.empty);
  191. linklibc:=(SharedLibFiles.Find('c')<>nil);
  192. // this one is a bit complex.
  193. // Only reorder for now if -XL or -XO params are given
  194. // or when -Xf.
  195. reorder:= linklibc and
  196. (
  197. ReorderEntries
  198. or
  199. (cs_link_pthread in current_settings.globalswitches));
  200. if cs_profile in current_settings.moduleswitches then
  201. begin
  202. prtobj:=gprtobj;
  203. AddSharedLibrary('c');
  204. LibrarySuffix:='p';
  205. linklibc:=true;
  206. end
  207. else
  208. begin
  209. if linklibc then
  210. prtobj:=cprtobj;
  211. end;
  212. // after this point addition of shared libs not allowed.
  213. end
  214. else
  215. begin
  216. { for darwin: always link dynamically against libc }
  217. linklibc := true;
  218. {$ifdef MACOSX104ORHIGHER}
  219. { not sure what this is for, but gcc always links against it }
  220. if not(cs_profile in current_settings.moduleswitches) then
  221. AddSharedLibrary('SystemStubs')
  222. else
  223. AddSharedLibrary('SystemStubs_profile');
  224. {$endif MACOSX104ORHIGHER}
  225. reorder:=reorderentries;
  226. if not(isdll) then
  227. if not(cs_profile in current_settings.moduleswitches) then
  228. begin
  229. if librarysearchpath.FindFile('crt1.o',false,s) then
  230. prtobj:=s
  231. else
  232. prtobj:='/usr/lib/crt1.o';
  233. end
  234. else
  235. begin
  236. if librarysearchpath.FindFile('gcrt1.o',false,s) then
  237. prtobj:=s
  238. else
  239. prtobj:='/usr/lib/gcrt1.o';
  240. end
  241. else
  242. begin
  243. if (apptype=app_bundle) then
  244. begin
  245. if librarysearchpath.FindFile('bundle1.o',false,s) then
  246. prtobj:=s
  247. else
  248. prtobj:='/usr/lib/bundle1.o'
  249. end
  250. else
  251. prtobj:=''
  252. end;
  253. end;
  254. if reorder Then
  255. ExpandAndApplyOrder(SharedLibFiles);
  256. { Open link.res file }
  257. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName);
  258. if (target_info.system in systems_darwin) and
  259. (sysrootpath<>'') then
  260. begin
  261. LinkRes.Add('-syslibroot');
  262. LinkRes.Add(sysrootpath);
  263. end;
  264. if (not isdll) or
  265. (apptype=app_bundle) then
  266. begin
  267. if (target_info.system in systems_darwin) then
  268. begin
  269. LinkRes.Add('-arch');
  270. case target_info.system of
  271. system_powerpc_darwin:
  272. LinkRes.Add('ppc');
  273. system_i386_darwin:
  274. LinkRes.Add('i386');
  275. system_powerpc64_darwin:
  276. LinkRes.Add('ppc64');
  277. system_x86_64_darwin:
  278. LinkRes.Add('x86_64');
  279. end;
  280. end;
  281. end;
  282. { Write path to search libraries }
  283. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  284. while assigned(HPath) do
  285. begin
  286. if LdSupportsNoResponseFile then
  287. LinkRes.Add('-L'+HPath.Str)
  288. else
  289. LinkRes.Add('SEARCH_DIR('+maybequoted(HPath.Str)+')');
  290. HPath:=TCmdStrListItem(HPath.Next);
  291. end;
  292. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  293. while assigned(HPath) do
  294. begin
  295. if LdSupportsNoResponseFile then
  296. LinkRes.Add('-L'+HPath.Str)
  297. else
  298. LinkRes.Add('SEARCH_DIR('+maybequoted(HPath.Str)+')');
  299. HPath:=TCmdStrListItem(HPath.Next);
  300. end;
  301. if (target_info.system in systems_darwin) then
  302. begin
  303. HPath:=TCmdStrListItem(current_module.localframeworksearchpath.First);
  304. while assigned(HPath) do
  305. begin
  306. LinkRes.Add('-F'+HPath.Str);
  307. HPath:=TCmdStrListItem(HPath.Next);
  308. end;
  309. HPath:=TCmdStrListItem(FrameworkSearchPath.First);
  310. while assigned(HPath) do
  311. begin
  312. LinkRes.Add('-F'+HPath.Str);
  313. HPath:=TCmdStrListItem(HPath.Next);
  314. end;
  315. end;
  316. if not LdSupportsNoResponseFile then
  317. LinkRes.Add('INPUT(');
  318. { add objectfiles, start with prt0 always }
  319. if prtobj<>'' then
  320. LinkRes.AddFileName(FindObjectFile(prtobj,'',false));
  321. { try to add crti and crtbegin if linking to C }
  322. if linklibc and
  323. not IsDarwin Then
  324. begin
  325. if librarysearchpath.FindFile('crtbegin.o',false,s) then
  326. LinkRes.AddFileName(s);
  327. if librarysearchpath.FindFile('crti.o',false,s) then
  328. LinkRes.AddFileName(s);
  329. end;
  330. { main objectfiles }
  331. while not ObjectFiles.Empty do
  332. begin
  333. s:=ObjectFiles.GetFirst;
  334. if s<>'' then
  335. if LdSupportsNoResponseFile then
  336. LinkRes.AddFileName(s)
  337. else
  338. LinkRes.AddFileName(maybequoted(s));
  339. end;
  340. if not LdSupportsNoResponseFile then
  341. LinkRes.Add(')');
  342. { Write staticlibraries }
  343. if not StaticLibFiles.Empty then
  344. begin
  345. if not LdSupportsNoResponseFile then
  346. LinkRes.Add('GROUP(');
  347. While not StaticLibFiles.Empty do
  348. begin
  349. S:=StaticLibFiles.GetFirst;
  350. if LdSupportsNoResponseFile then
  351. LinkRes.AddFileName(s)
  352. else
  353. LinkRes.AddFileName(maybequoted(s))
  354. end;
  355. if not LdSupportsNoResponseFile then
  356. LinkRes.Add(')');
  357. end;
  358. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  359. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  360. if not SharedLibFiles.Empty then
  361. begin
  362. if not LdSupportsNoResponseFile then
  363. LinkRes.Add('INPUT(');
  364. While not SharedLibFiles.Empty do
  365. begin
  366. S:=SharedLibFiles.GetFirst;
  367. if (s<>'c') or reorder then
  368. begin
  369. i:=Pos(target_info.sharedlibext,S);
  370. if i>0 then
  371. Delete(S,i,255);
  372. LinkRes.Add('-l'+s);
  373. end
  374. else
  375. begin
  376. linklibc:=true;
  377. linkdynamic:=false; { libc will include the ld-* for us }
  378. end;
  379. end;
  380. { be sure that libc is the last lib }
  381. if linklibc and not reorder then
  382. Begin
  383. If LibrarySuffix=' ' Then
  384. LinkRes.Add('-lc')
  385. else
  386. LinkRes.Add('-lc_'+LibrarySuffix);
  387. If LibrarySuffix='r' Then
  388. LinkRes.Add('-lc');
  389. end;
  390. { when we have -static for the linker the we also need libgcc }
  391. if (cs_link_staticflag in current_settings.globalswitches) then
  392. LinkRes.Add('-lgcc');
  393. if linkdynamic and (Info.DynamicLinker<>'') then
  394. LinkRes.AddFileName(Info.DynamicLinker);
  395. if not LdSupportsNoResponseFile then
  396. LinkRes.Add(')');
  397. end;
  398. { frameworks for Darwin }
  399. if IsDarwin then
  400. while not FrameworkFiles.empty do
  401. begin
  402. LinkRes.Add('-framework');
  403. LinkRes.Add(FrameworkFiles.GetFirst);
  404. end;
  405. { objects which must be at the end }
  406. if linklibc and
  407. not IsDarwin Then
  408. begin
  409. Fl1:=librarysearchpath.FindFile('crtend.o',false,s1);
  410. Fl2:=librarysearchpath.FindFile('crtn.o',false,s2);
  411. if Fl1 or Fl2 then
  412. begin
  413. LinkRes.Add('INPUT(');
  414. If Fl1 Then
  415. LinkRes.AddFileName(s1);
  416. If Fl2 Then
  417. LinkRes.AddFileName(s2);
  418. LinkRes.Add(')');
  419. end;
  420. end;
  421. { Write and Close response }
  422. linkres.writetodisk;
  423. linkres.Free;
  424. WriteResponseFile:=True;
  425. end;
  426. function TLinkerBSD.MakeExecutable:boolean;
  427. var
  428. binstr,
  429. cmdstr,
  430. extdbgbinstr,
  431. extdbgcmdstr: TCmdStr;
  432. linkscript: TAsmScript;
  433. DynLinkStr : string[60];
  434. GCSectionsStr,
  435. StaticStr,
  436. StripStr : string[40];
  437. success : boolean;
  438. begin
  439. if not(cs_link_nolink in current_settings.globalswitches) then
  440. Message1(exec_i_linking,current_module.exefilename^);
  441. { Create some replacements }
  442. StaticStr:='';
  443. StripStr:='';
  444. DynLinkStr:='';
  445. GCSectionsStr:='';
  446. if (cs_link_staticflag in current_settings.globalswitches) then
  447. begin
  448. if (target_info.system=system_m68k_netbsd) and
  449. ((cs_link_on_target in current_settings.globalswitches) or
  450. (target_info.system=source_info.system)) then
  451. StaticStr:='-Bstatic'
  452. else
  453. StaticStr:='-static';
  454. end;
  455. if (cs_link_strip in current_settings.globalswitches) then
  456. if (target_info.system in systems_darwin) then
  457. StripStr:='-x'
  458. else
  459. StripStr:='-s';
  460. if (cs_link_smart in current_settings.globalswitches) and
  461. (tf_smartlink_sections in target_info.flags) then
  462. if not(target_info.system in systems_darwin) then
  463. GCSectionsStr:='--gc-sections'
  464. else
  465. GCSectionsStr:='-dead_strip';
  466. if(not(target_info.system in systems_darwin) and
  467. (cs_profile in current_settings.moduleswitches)) or
  468. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty)) then
  469. DynLinkStr:='-dynamic-linker='+Info.DynamicLinker;
  470. if CShared Then
  471. begin
  472. if not(target_info.system in systems_darwin) then
  473. DynLinKStr:=DynLinkStr+' --shared'
  474. else
  475. DynLinKStr:=DynLinkStr+' -dynamic'; // one dash!
  476. end;
  477. { Write used files and libraries }
  478. WriteResponseFile(false);
  479. { Call linker }
  480. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  481. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename^));
  482. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  483. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  484. Replace(cmdstr,'$STATIC',StaticStr);
  485. Replace(cmdstr,'$STRIP',StripStr);
  486. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  487. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  488. BinStr:=FindUtil(utilsprefix+BinStr);
  489. { create dsym file? }
  490. extdbgbinstr:='';
  491. extdbgcmdstr:='';
  492. if (target_info.system in systems_darwin) and
  493. (target_dbg.id in [dbg_dwarf2,dbg_dwarf3]) and
  494. (cs_link_separate_dbg_file in current_settings.globalswitches) then
  495. begin
  496. extdbgbinstr:=FindUtil(utilsprefix+'dsymutil');
  497. extdbgcmdstr:=maybequoted(current_module.exefilename^);
  498. end;
  499. if (LdSupportsNoResponseFile) and
  500. not(cs_link_nolink in current_settings.globalswitches) then
  501. begin
  502. { we have to use a script to use the IFS hack }
  503. linkscript:=TAsmScriptUnix.create(outputexedir+'ppaslink');
  504. linkscript.AddLinkCommand(BinStr,CmdStr,'');
  505. if (extdbgcmdstr<>'') then
  506. linkscript.AddLinkCommand(extdbgbinstr,extdbgcmdstr,'');
  507. linkscript.WriteToDisk;
  508. BinStr:=linkscript.fn;
  509. if not path_absolute(BinStr) then
  510. BinStr:='./'+BinStr;
  511. CmdStr:='';
  512. end;
  513. success:=DoExec(BinStr,CmdStr,true,LdSupportsNoResponseFile);
  514. if (success and
  515. (extdbgbinstr<>'') and
  516. (cs_link_nolink in current_settings.globalswitches)) then
  517. success:=DoExec(extdbgbinstr,extdbgcmdstr,false,LdSupportsNoResponseFile);
  518. { Remove ReponseFile }
  519. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  520. begin
  521. DeleteFile(outputexedir+Info.ResName);
  522. if LdSupportsNoResponseFile Then
  523. begin
  524. DeleteFile(linkscript.fn);
  525. linkscript.free
  526. end;
  527. end;
  528. MakeExecutable:=success; { otherwise a recursive call to link method }
  529. end;
  530. Function TLinkerBSD.MakeSharedLibrary:boolean;
  531. var
  532. InitStr,
  533. FiniStr,
  534. SoNameStr : string[80];
  535. linkscript: TAsmScript;
  536. binstr,
  537. cmdstr,
  538. extdbgbinstr,
  539. extdbgcmdstr : TCmdStr;
  540. exportedsyms: text;
  541. success : boolean;
  542. begin
  543. MakeSharedLibrary:=false;
  544. if not(cs_link_nolink in current_settings.globalswitches) then
  545. Message1(exec_i_linking,current_module.sharedlibfilename^);
  546. { Write used files and libraries }
  547. WriteResponseFile(true);
  548. InitStr:='-init FPC_LIB_START';
  549. FiniStr:='-fini FPC_LIB_EXIT';
  550. SoNameStr:='-soname '+ExtractFileName(current_module.sharedlibfilename^);
  551. { Call linker }
  552. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  553. {$ifndef darwin}
  554. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename^));
  555. {$else darwin}
  556. Replace(cmdstr,'$EXE',maybequoted(ExpandFileName(current_module.sharedlibfilename^)));
  557. {$endif darwin}
  558. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  559. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  560. Replace(cmdstr,'$INIT',InitStr);
  561. Replace(cmdstr,'$FINI',FiniStr);
  562. Replace(cmdstr,'$SONAME',SoNameStr);
  563. BinStr:=FindUtil(utilsprefix+BinStr);
  564. { create dsym file? }
  565. extdbgbinstr:='';
  566. extdbgcmdstr:='';
  567. if (target_info.system in systems_darwin) and
  568. (target_dbg.id in [dbg_dwarf2,dbg_dwarf3]) and
  569. (cs_link_separate_dbg_file in current_settings.globalswitches) then
  570. begin
  571. extdbgbinstr:=FindUtil(utilsprefix+'dsymutil');
  572. extdbgcmdstr:=maybequoted(current_module.sharedlibfilename^);
  573. end;
  574. if (target_info.system in systems_darwin) then
  575. begin
  576. { exported symbols for darwin }
  577. if not texportlibunix(exportlib).exportedsymnames.empty then
  578. begin
  579. assign(exportedsyms,outputexedir+'linksyms.fpc');
  580. rewrite(exportedsyms);
  581. repeat
  582. writeln(exportedsyms,texportlibunix(exportlib).exportedsymnames.getfirst);
  583. until texportlibunix(exportlib).exportedsymnames.empty;
  584. close(exportedsyms);
  585. cmdstr:=cmdstr+' -exported_symbols_list '+maybequoted(outputexedir)+'linksyms.fpc';
  586. end;
  587. end;
  588. if (LdSupportsNoResponseFile) and
  589. not(cs_link_nolink in current_settings.globalswitches) then
  590. begin
  591. { we have to use a script to use the IFS hack }
  592. linkscript:=TAsmScriptUnix.create(outputexedir+'ppaslink');
  593. linkscript.AddLinkCommand(BinStr,CmdStr,'');
  594. if (extdbgbinstr<>'') then
  595. linkscript.AddLinkCommand(extdbgbinstr,extdbgcmdstr,'');
  596. linkscript.WriteToDisk;
  597. BinStr:=linkscript.fn;
  598. if not path_absolute(BinStr) then
  599. BinStr:='./'+BinStr;
  600. CmdStr:='';
  601. end;
  602. success:=DoExec(BinStr,cmdstr,true,LdSupportsNoResponseFile);
  603. if (success and
  604. (extdbgbinstr<>'') and
  605. (cs_link_nolink in current_settings.globalswitches)) then
  606. success:=DoExec(extdbgbinstr,extdbgcmdstr,false,LdSupportsNoResponseFile);
  607. { Strip the library ? }
  608. if success and (cs_link_strip in current_settings.globalswitches) then
  609. begin
  610. SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  611. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename^));
  612. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,false,false);
  613. end;
  614. { Remove ReponseFile }
  615. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  616. begin
  617. DeleteFile(outputexedir+Info.ResName);
  618. if LdSupportsNoResponseFile Then
  619. begin
  620. DeleteFile(linkscript.fn);
  621. linkscript.free
  622. end;
  623. if (target_info.system in systems_darwin) then
  624. DeleteFile(outputexedir+'linksyms.fpc');
  625. end;
  626. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  627. end;
  628. {*****************************************************************************
  629. Initialize
  630. *****************************************************************************}
  631. initialization
  632. {$ifdef x86_64}
  633. RegisterExternalLinker(system_x86_64_FreeBSD_info,TLinkerBSD);
  634. RegisterImport(system_x86_64_freebsd,timportlibbsd);
  635. RegisterExport(system_x86_64_freebsd,texportlibbsd);
  636. RegisterTarget(system_x86_64_freebsd_info);
  637. RegisterExternalLinker(system_x86_64_darwin_info,TLinkerBSD);
  638. RegisterImport(system_x86_64_darwin,timportlibdarwin);
  639. RegisterExport(system_x86_64_darwin,texportlibdarwin);
  640. RegisterTarget(system_x86_64_darwin_info);
  641. {$endif}
  642. {$ifdef i386}
  643. RegisterExternalLinker(system_i386_FreeBSD_info,TLinkerBSD);
  644. RegisterExternalLinker(system_i386_NetBSD_info,TLinkerBSD);
  645. RegisterExternalLinker(system_i386_OpenBSD_info,TLinkerBSD);
  646. RegisterImport(system_i386_freebsd,timportlibbsd);
  647. RegisterExport(system_i386_freebsd,texportlibbsd);
  648. RegisterTarget(system_i386_freebsd_info);
  649. RegisterImport(system_i386_netbsd,timportlibbsd);
  650. RegisterExport(system_i386_netbsd,texportlibbsd);
  651. RegisterTarget(system_i386_netbsd_info);
  652. RegisterImport(system_i386_openbsd,timportlibbsd);
  653. RegisterExport(system_i386_openbsd,texportlibbsd);
  654. RegisterTarget(system_i386_openbsd_info);
  655. RegisterExternalLinker(system_i386_darwin_info,TLinkerBSD);
  656. RegisterImport(system_i386_darwin,timportlibdarwin);
  657. RegisterExport(system_i386_darwin,texportlibdarwin);
  658. RegisterTarget(system_i386_darwin_info);
  659. {$endif i386}
  660. {$ifdef m68k}
  661. // RegisterExternalLinker(system_m68k_FreeBSD_info,TLinkerBSD);
  662. RegisterExternalLinker(system_m68k_NetBSD_info,TLinkerBSD);
  663. RegisterImport(system_m68k_netbsd,timportlibbsd);
  664. RegisterExport(system_m68k_netbsd,texportlibbsd);
  665. RegisterTarget(system_m68k_netbsd_info);
  666. {$endif m68k}
  667. {$ifdef powerpc}
  668. // RegisterExternalLinker(system_m68k_FreeBSD_info,TLinkerBSD);
  669. RegisterExternalLinker(system_powerpc_darwin_info,TLinkerBSD);
  670. RegisterImport(system_powerpc_darwin,timportlibdarwin);
  671. RegisterExport(system_powerpc_darwin,texportlibdarwin);
  672. RegisterTarget(system_powerpc_darwin_info);
  673. RegisterExternalLinker(system_powerpc_netbsd_info,TLinkerBSD);
  674. RegisterImport(system_powerpc_netbsd,timportlibbsd);
  675. RegisterExport(system_powerpc_netbsd,texportlibbsd);
  676. RegisterTarget(system_powerpc_netbsd_info);
  677. {$endif powerpc}
  678. {$ifdef powerpc64}
  679. RegisterExternalLinker(system_powerpc64_darwin_info,TLinkerBSD);
  680. RegisterImport(system_powerpc64_darwin,timportlibdarwin);
  681. RegisterExport(system_powerpc64_darwin,texportlibdarwin);
  682. RegisterTarget(system_powerpc64_darwin_info);
  683. {$endif powerpc64}
  684. RegisterRes(res_elf_info,TWinLikeResourceFile);
  685. RegisterRes(res_macho_info,TWinLikeResourceFile);
  686. end.