t_bsd.pas 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  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. system_arm_darwin:
  279. LinkRes.Add('arm');
  280. end;
  281. end;
  282. end;
  283. { Write path to search libraries }
  284. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  285. while assigned(HPath) do
  286. begin
  287. if LdSupportsNoResponseFile then
  288. LinkRes.Add('-L'+HPath.Str)
  289. else
  290. LinkRes.Add('SEARCH_DIR('+maybequoted(HPath.Str)+')');
  291. HPath:=TCmdStrListItem(HPath.Next);
  292. end;
  293. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  294. while assigned(HPath) do
  295. begin
  296. if LdSupportsNoResponseFile then
  297. LinkRes.Add('-L'+HPath.Str)
  298. else
  299. LinkRes.Add('SEARCH_DIR('+maybequoted(HPath.Str)+')');
  300. HPath:=TCmdStrListItem(HPath.Next);
  301. end;
  302. if (target_info.system in systems_darwin) then
  303. begin
  304. HPath:=TCmdStrListItem(current_module.localframeworksearchpath.First);
  305. while assigned(HPath) do
  306. begin
  307. LinkRes.Add('-F'+HPath.Str);
  308. HPath:=TCmdStrListItem(HPath.Next);
  309. end;
  310. HPath:=TCmdStrListItem(FrameworkSearchPath.First);
  311. while assigned(HPath) do
  312. begin
  313. LinkRes.Add('-F'+HPath.Str);
  314. HPath:=TCmdStrListItem(HPath.Next);
  315. end;
  316. end;
  317. if not LdSupportsNoResponseFile then
  318. LinkRes.Add('INPUT(');
  319. { add objectfiles, start with prt0 always }
  320. if prtobj<>'' then
  321. LinkRes.AddFileName(FindObjectFile(prtobj,'',false));
  322. { try to add crti and crtbegin if linking to C }
  323. if linklibc and
  324. not IsDarwin Then
  325. begin
  326. if librarysearchpath.FindFile('crtbegin.o',false,s) then
  327. LinkRes.AddFileName(s);
  328. if librarysearchpath.FindFile('crti.o',false,s) then
  329. LinkRes.AddFileName(s);
  330. end;
  331. { main objectfiles }
  332. while not ObjectFiles.Empty do
  333. begin
  334. s:=ObjectFiles.GetFirst;
  335. if s<>'' then
  336. if LdSupportsNoResponseFile then
  337. LinkRes.AddFileName(s)
  338. else
  339. LinkRes.AddFileName(maybequoted(s));
  340. end;
  341. if not LdSupportsNoResponseFile then
  342. LinkRes.Add(')');
  343. { Write staticlibraries }
  344. if not StaticLibFiles.Empty then
  345. begin
  346. if not LdSupportsNoResponseFile then
  347. LinkRes.Add('GROUP(');
  348. While not StaticLibFiles.Empty do
  349. begin
  350. S:=StaticLibFiles.GetFirst;
  351. if LdSupportsNoResponseFile then
  352. LinkRes.AddFileName(s)
  353. else
  354. LinkRes.AddFileName(maybequoted(s))
  355. end;
  356. if not LdSupportsNoResponseFile then
  357. LinkRes.Add(')');
  358. end;
  359. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  360. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  361. if not SharedLibFiles.Empty then
  362. begin
  363. if not LdSupportsNoResponseFile then
  364. LinkRes.Add('INPUT(');
  365. While not SharedLibFiles.Empty do
  366. begin
  367. S:=SharedLibFiles.GetFirst;
  368. if (s<>'c') or reorder then
  369. begin
  370. i:=Pos(target_info.sharedlibext,S);
  371. if i>0 then
  372. Delete(S,i,255);
  373. LinkRes.Add('-l'+s);
  374. end
  375. else
  376. begin
  377. linklibc:=true;
  378. linkdynamic:=false; { libc will include the ld-* for us }
  379. end;
  380. end;
  381. { be sure that libc is the last lib }
  382. if linklibc and not reorder then
  383. Begin
  384. If LibrarySuffix=' ' Then
  385. LinkRes.Add('-lc')
  386. else
  387. LinkRes.Add('-lc_'+LibrarySuffix);
  388. If LibrarySuffix='r' Then
  389. LinkRes.Add('-lc');
  390. end;
  391. { when we have -static for the linker the we also need libgcc }
  392. if (cs_link_staticflag in current_settings.globalswitches) then
  393. LinkRes.Add('-lgcc');
  394. if linkdynamic and (Info.DynamicLinker<>'') then
  395. LinkRes.AddFileName(Info.DynamicLinker);
  396. if not LdSupportsNoResponseFile then
  397. LinkRes.Add(')');
  398. end;
  399. { frameworks for Darwin }
  400. if IsDarwin then
  401. while not FrameworkFiles.empty do
  402. begin
  403. LinkRes.Add('-framework');
  404. LinkRes.Add(FrameworkFiles.GetFirst);
  405. end;
  406. { objects which must be at the end }
  407. if linklibc and
  408. not IsDarwin Then
  409. begin
  410. Fl1:=librarysearchpath.FindFile('crtend.o',false,s1);
  411. Fl2:=librarysearchpath.FindFile('crtn.o',false,s2);
  412. if Fl1 or Fl2 then
  413. begin
  414. LinkRes.Add('INPUT(');
  415. If Fl1 Then
  416. LinkRes.AddFileName(s1);
  417. If Fl2 Then
  418. LinkRes.AddFileName(s2);
  419. LinkRes.Add(')');
  420. end;
  421. end;
  422. { Write and Close response }
  423. linkres.writetodisk;
  424. linkres.Free;
  425. WriteResponseFile:=True;
  426. end;
  427. function TLinkerBSD.MakeExecutable:boolean;
  428. var
  429. binstr,
  430. cmdstr,
  431. extdbgbinstr,
  432. extdbgcmdstr: TCmdStr;
  433. linkscript: TAsmScript;
  434. DynLinkStr : string[60];
  435. GCSectionsStr,
  436. StaticStr,
  437. StripStr : string[40];
  438. success : boolean;
  439. begin
  440. if not(cs_link_nolink in current_settings.globalswitches) then
  441. Message1(exec_i_linking,current_module.exefilename^);
  442. { Create some replacements }
  443. StaticStr:='';
  444. StripStr:='';
  445. DynLinkStr:='';
  446. GCSectionsStr:='';
  447. if (cs_link_staticflag in current_settings.globalswitches) then
  448. begin
  449. if (target_info.system=system_m68k_netbsd) and
  450. ((cs_link_on_target in current_settings.globalswitches) or
  451. (target_info.system=source_info.system)) then
  452. StaticStr:='-Bstatic'
  453. else
  454. StaticStr:='-static';
  455. end;
  456. if (cs_link_strip in current_settings.globalswitches) then
  457. if (target_info.system in systems_darwin) then
  458. StripStr:='-x'
  459. else
  460. StripStr:='-s';
  461. if (cs_link_smart in current_settings.globalswitches) and
  462. (tf_smartlink_sections in target_info.flags) then
  463. if not(target_info.system in systems_darwin) then
  464. GCSectionsStr:='--gc-sections'
  465. else
  466. GCSectionsStr:='-dead_strip';
  467. if(not(target_info.system in systems_darwin) and
  468. (cs_profile in current_settings.moduleswitches)) or
  469. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty)) then
  470. DynLinkStr:='-dynamic-linker='+Info.DynamicLinker;
  471. if CShared Then
  472. begin
  473. if not(target_info.system in systems_darwin) then
  474. DynLinKStr:=DynLinkStr+' --shared'
  475. else
  476. DynLinKStr:=DynLinkStr+' -dynamic'; // one dash!
  477. end;
  478. { Write used files and libraries }
  479. WriteResponseFile(false);
  480. { Call linker }
  481. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  482. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename^));
  483. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  484. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  485. Replace(cmdstr,'$STATIC',StaticStr);
  486. Replace(cmdstr,'$STRIP',StripStr);
  487. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  488. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  489. if (target_info.system in systems_darwin) then
  490. Replace(cmdstr,'$PRTOBJ',GetDarwinPrtobjName(false));
  491. BinStr:=FindUtil(utilsprefix+BinStr);
  492. { create dsym file? }
  493. extdbgbinstr:='';
  494. extdbgcmdstr:='';
  495. if (target_info.system in systems_darwin) and
  496. (target_dbg.id in [dbg_dwarf2,dbg_dwarf3]) and
  497. (cs_link_separate_dbg_file in current_settings.globalswitches) then
  498. begin
  499. extdbgbinstr:=FindUtil(utilsprefix+'dsymutil');
  500. extdbgcmdstr:=maybequoted(current_module.exefilename^);
  501. end;
  502. if (LdSupportsNoResponseFile) and
  503. not(cs_link_nolink in current_settings.globalswitches) then
  504. begin
  505. { we have to use a script to use the IFS hack }
  506. linkscript:=TAsmScriptUnix.create(outputexedir+'ppaslink');
  507. linkscript.AddLinkCommand(BinStr,CmdStr,'');
  508. if (extdbgcmdstr<>'') then
  509. linkscript.AddLinkCommand(extdbgbinstr,extdbgcmdstr,'');
  510. linkscript.WriteToDisk;
  511. BinStr:=linkscript.fn;
  512. if not path_absolute(BinStr) then
  513. BinStr:='./'+BinStr;
  514. CmdStr:='';
  515. end;
  516. success:=DoExec(BinStr,CmdStr,true,LdSupportsNoResponseFile);
  517. if (success and
  518. (extdbgbinstr<>'') and
  519. (cs_link_nolink in current_settings.globalswitches)) then
  520. success:=DoExec(extdbgbinstr,extdbgcmdstr,false,LdSupportsNoResponseFile);
  521. { Remove ReponseFile }
  522. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  523. begin
  524. DeleteFile(outputexedir+Info.ResName);
  525. if LdSupportsNoResponseFile Then
  526. begin
  527. DeleteFile(linkscript.fn);
  528. linkscript.free
  529. end;
  530. end;
  531. MakeExecutable:=success; { otherwise a recursive call to link method }
  532. end;
  533. Function TLinkerBSD.MakeSharedLibrary:boolean;
  534. var
  535. InitStr,
  536. FiniStr,
  537. SoNameStr : string[80];
  538. linkscript: TAsmScript;
  539. binstr,
  540. cmdstr,
  541. extdbgbinstr,
  542. extdbgcmdstr : TCmdStr;
  543. exportedsyms: text;
  544. success : boolean;
  545. begin
  546. MakeSharedLibrary:=false;
  547. if not(cs_link_nolink in current_settings.globalswitches) then
  548. Message1(exec_i_linking,current_module.sharedlibfilename^);
  549. { Write used files and libraries }
  550. WriteResponseFile(true);
  551. InitStr:='-init FPC_LIB_START';
  552. FiniStr:='-fini FPC_LIB_EXIT';
  553. SoNameStr:='-soname '+ExtractFileName(current_module.sharedlibfilename^);
  554. { Call linker }
  555. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  556. {$ifndef darwin}
  557. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename^));
  558. {$else darwin}
  559. Replace(cmdstr,'$EXE',maybequoted(ExpandFileName(current_module.sharedlibfilename^)));
  560. {$endif darwin}
  561. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  562. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  563. Replace(cmdstr,'$INIT',InitStr);
  564. Replace(cmdstr,'$FINI',FiniStr);
  565. Replace(cmdstr,'$SONAME',SoNameStr);
  566. if (target_info.system in systems_darwin) then
  567. Replace(cmdstr,'$PRTOBJ',GetDarwinPrtobjName(true));
  568. BinStr:=FindUtil(utilsprefix+BinStr);
  569. { create dsym file? }
  570. extdbgbinstr:='';
  571. extdbgcmdstr:='';
  572. if (target_info.system in systems_darwin) and
  573. (target_dbg.id in [dbg_dwarf2,dbg_dwarf3]) and
  574. (cs_link_separate_dbg_file in current_settings.globalswitches) then
  575. begin
  576. extdbgbinstr:=FindUtil(utilsprefix+'dsymutil');
  577. extdbgcmdstr:=maybequoted(current_module.sharedlibfilename^);
  578. end;
  579. if (target_info.system in systems_darwin) then
  580. begin
  581. { exported symbols for darwin }
  582. if not texportlibunix(exportlib).exportedsymnames.empty then
  583. begin
  584. assign(exportedsyms,outputexedir+'linksyms.fpc');
  585. rewrite(exportedsyms);
  586. repeat
  587. writeln(exportedsyms,texportlibunix(exportlib).exportedsymnames.getfirst);
  588. until texportlibunix(exportlib).exportedsymnames.empty;
  589. close(exportedsyms);
  590. cmdstr:=cmdstr+' -exported_symbols_list '+maybequoted(outputexedir)+'linksyms.fpc';
  591. end;
  592. end;
  593. if (LdSupportsNoResponseFile) and
  594. not(cs_link_nolink in current_settings.globalswitches) then
  595. begin
  596. { we have to use a script to use the IFS hack }
  597. linkscript:=TAsmScriptUnix.create(outputexedir+'ppaslink');
  598. linkscript.AddLinkCommand(BinStr,CmdStr,'');
  599. if (extdbgbinstr<>'') then
  600. linkscript.AddLinkCommand(extdbgbinstr,extdbgcmdstr,'');
  601. linkscript.WriteToDisk;
  602. BinStr:=linkscript.fn;
  603. if not path_absolute(BinStr) then
  604. BinStr:='./'+BinStr;
  605. CmdStr:='';
  606. end;
  607. success:=DoExec(BinStr,cmdstr,true,LdSupportsNoResponseFile);
  608. if (success and
  609. (extdbgbinstr<>'') and
  610. (cs_link_nolink in current_settings.globalswitches)) then
  611. success:=DoExec(extdbgbinstr,extdbgcmdstr,false,LdSupportsNoResponseFile);
  612. { Strip the library ? }
  613. if success and (cs_link_strip in current_settings.globalswitches) then
  614. begin
  615. SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  616. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename^));
  617. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,false,false);
  618. end;
  619. { Remove ReponseFile }
  620. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  621. begin
  622. DeleteFile(outputexedir+Info.ResName);
  623. if LdSupportsNoResponseFile Then
  624. begin
  625. DeleteFile(linkscript.fn);
  626. linkscript.free
  627. end;
  628. if (target_info.system in systems_darwin) then
  629. DeleteFile(outputexedir+'linksyms.fpc');
  630. end;
  631. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  632. end;
  633. {*****************************************************************************
  634. Initialize
  635. *****************************************************************************}
  636. initialization
  637. {$ifdef x86_64}
  638. RegisterExternalLinker(system_x86_64_FreeBSD_info,TLinkerBSD);
  639. RegisterImport(system_x86_64_freebsd,timportlibbsd);
  640. RegisterExport(system_x86_64_freebsd,texportlibbsd);
  641. RegisterTarget(system_x86_64_freebsd_info);
  642. RegisterExternalLinker(system_x86_64_darwin_info,TLinkerBSD);
  643. RegisterImport(system_x86_64_darwin,timportlibdarwin);
  644. RegisterExport(system_x86_64_darwin,texportlibdarwin);
  645. RegisterTarget(system_x86_64_darwin_info);
  646. {$endif}
  647. {$ifdef i386}
  648. RegisterExternalLinker(system_i386_FreeBSD_info,TLinkerBSD);
  649. RegisterExternalLinker(system_i386_NetBSD_info,TLinkerBSD);
  650. RegisterExternalLinker(system_i386_OpenBSD_info,TLinkerBSD);
  651. RegisterImport(system_i386_freebsd,timportlibbsd);
  652. RegisterExport(system_i386_freebsd,texportlibbsd);
  653. RegisterTarget(system_i386_freebsd_info);
  654. RegisterImport(system_i386_netbsd,timportlibbsd);
  655. RegisterExport(system_i386_netbsd,texportlibbsd);
  656. RegisterTarget(system_i386_netbsd_info);
  657. RegisterImport(system_i386_openbsd,timportlibbsd);
  658. RegisterExport(system_i386_openbsd,texportlibbsd);
  659. RegisterTarget(system_i386_openbsd_info);
  660. RegisterExternalLinker(system_i386_darwin_info,TLinkerBSD);
  661. RegisterImport(system_i386_darwin,timportlibdarwin);
  662. RegisterExport(system_i386_darwin,texportlibdarwin);
  663. RegisterTarget(system_i386_darwin_info);
  664. {$endif i386}
  665. {$ifdef m68k}
  666. // RegisterExternalLinker(system_m68k_FreeBSD_info,TLinkerBSD);
  667. RegisterExternalLinker(system_m68k_NetBSD_info,TLinkerBSD);
  668. RegisterImport(system_m68k_netbsd,timportlibbsd);
  669. RegisterExport(system_m68k_netbsd,texportlibbsd);
  670. RegisterTarget(system_m68k_netbsd_info);
  671. {$endif m68k}
  672. {$ifdef powerpc}
  673. // RegisterExternalLinker(system_m68k_FreeBSD_info,TLinkerBSD);
  674. RegisterExternalLinker(system_powerpc_darwin_info,TLinkerBSD);
  675. RegisterImport(system_powerpc_darwin,timportlibdarwin);
  676. RegisterExport(system_powerpc_darwin,texportlibdarwin);
  677. RegisterTarget(system_powerpc_darwin_info);
  678. RegisterExternalLinker(system_powerpc_netbsd_info,TLinkerBSD);
  679. RegisterImport(system_powerpc_netbsd,timportlibbsd);
  680. RegisterExport(system_powerpc_netbsd,texportlibbsd);
  681. RegisterTarget(system_powerpc_netbsd_info);
  682. {$endif powerpc}
  683. {$ifdef powerpc64}
  684. RegisterExternalLinker(system_powerpc64_darwin_info,TLinkerBSD);
  685. RegisterImport(system_powerpc64_darwin,timportlibdarwin);
  686. RegisterExport(system_powerpc64_darwin,texportlibdarwin);
  687. RegisterTarget(system_powerpc64_darwin_info);
  688. {$endif powerpc64}
  689. {$ifdef arm}
  690. RegisterExternalLinker(system_arm_darwin_info,TLinkerBSD);
  691. RegisterImport(system_arm_darwin,timportlibdarwin);
  692. RegisterExport(system_arm_darwin,texportlibdarwin);
  693. RegisterTarget(system_arm_darwin_info);
  694. {$endif arm}
  695. RegisterRes(res_elf_info,TWinLikeResourceFile);
  696. RegisterRes(res_macho_info,TWinLikeResourceFile);
  697. end.