t_bsd.pas 25 KB

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