t_bsd.pas 27 KB

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