2
0

t_bsd.pas 25 KB

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