t_bsd.pas 28 KB

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