t_bsd.pas 27 KB

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