t_bsd.pas 26 KB

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