t_bsd.pas 28 KB

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