t_bsd.pas 24 KB

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