2
0

t_bsd.pas 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  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,i_bsd,
  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(texportlib)
  39. procedure preparelib(const s : string);override;
  40. procedure exportprocedure(hp : texported_item);override;
  41. procedure exportvar(hp : texported_item);override;
  42. procedure generatelib;override;
  43. end;
  44. tlinkerbsd=class(texternallinker)
  45. private
  46. LdSupportsNoResponseFile : boolean;
  47. LibrarySuffix : Char;
  48. Function WriteResponseFile(isdll:boolean) : Boolean;
  49. public
  50. constructor Create;override;
  51. procedure SetDefaultInfo;override;
  52. function MakeExecutable:boolean;override;
  53. function MakeSharedLibrary:boolean;override;
  54. procedure LoadPredefinedLibraryOrder; override;
  55. end;
  56. {*****************************************************************************
  57. TIMPORTLIBDARWIN
  58. *****************************************************************************}
  59. procedure timportlibdarwin.generatelib;
  60. begin
  61. end;
  62. {*****************************************************************************
  63. TIMPORTLIBBSD
  64. *****************************************************************************}
  65. procedure timportlibbsd.generatelib;
  66. var
  67. i : longint;
  68. ImportLibrary : TImportLibrary;
  69. begin
  70. for i:=0 to current_module.ImportLibraryList.Count-1 do
  71. begin
  72. ImportLibrary:=TImportLibrary(current_module.ImportLibraryList[i]);
  73. current_module.linkothersharedlibs.add(ImportLibrary.Name,link_always);
  74. end;
  75. end;
  76. {*****************************************************************************
  77. TEXPORTLIBBSD
  78. *****************************************************************************}
  79. procedure texportlibbsd.preparelib(const s:string);
  80. begin
  81. end;
  82. procedure texportlibbsd.exportprocedure(hp : texported_item);
  83. var
  84. hp2 : texported_item;
  85. begin
  86. { first test the index value }
  87. if (hp.options and eo_index)<>0 then
  88. begin
  89. Message1(parser_e_no_export_with_index_for_target,'*bsd/darwin');
  90. exit;
  91. end;
  92. { now place in correct order }
  93. hp2:=texported_item(current_module._exports.first);
  94. while assigned(hp2) and
  95. (hp.name^>hp2.name^) do
  96. hp2:=texported_item(hp2.next);
  97. { insert hp there !! }
  98. if assigned(hp2) and (hp2.name^=hp.name^) then
  99. begin
  100. { this is not allowed !! }
  101. Message1(parser_e_export_name_double,hp.name^);
  102. exit;
  103. end;
  104. if hp2=texported_item(current_module._exports.first) then
  105. current_module._exports.concat(hp)
  106. else if assigned(hp2) then
  107. begin
  108. hp.next:=hp2;
  109. hp.previous:=hp2.previous;
  110. if assigned(hp2.previous) then
  111. hp2.previous.next:=hp;
  112. hp2.previous:=hp;
  113. end
  114. else
  115. current_module._exports.concat(hp);
  116. end;
  117. procedure texportlibbsd.exportvar(hp : texported_item);
  118. begin
  119. hp.is_var:=true;
  120. exportprocedure(hp);
  121. end;
  122. procedure texportlibbsd.generatelib; // straight t_linux copy for now.
  123. var
  124. hp2 : texported_item;
  125. pd : tprocdef;
  126. {$ifdef x86}
  127. sym : tasmsymbol;
  128. r : treference;
  129. {$endif x86}
  130. begin
  131. new_section(current_asmdata.asmlists[al_procedures],sec_code,'',0);
  132. hp2:=texported_item(current_module._exports.first);
  133. while assigned(hp2) do
  134. begin
  135. if (not hp2.is_var) and
  136. (hp2.sym.typ=procsym) then
  137. begin
  138. { the manglednames can already be the same when the procedure
  139. is declared with cdecl }
  140. pd:=tprocdef(tprocsym(hp2.sym).ProcdefList[0]);
  141. if pd.mangledname<>hp2.name^ then
  142. begin
  143. { place jump in al_procedures }
  144. current_asmdata.asmlists[al_procedures].concat(tai_align.create(target_info.alignment.procalign));
  145. current_asmdata.asmlists[al_procedures].concat(Tai_symbol.Createname_global(hp2.name^,AT_FUNCTION,0));
  146. if (cs_create_pic in current_settings.moduleswitches) and
  147. { other targets need to be checked how it works }
  148. (target_info.system in [system_i386_freebsd]) then
  149. begin
  150. {$ifdef x86}
  151. sym:=current_asmdata.RefAsmSymbol(pd.mangledname);
  152. reference_reset_symbol(r,sym,0);
  153. if cs_create_pic in current_settings.moduleswitches then
  154. r.refaddr:=addr_pic
  155. else
  156. r.refaddr:=addr_full;
  157. current_asmdata.asmlists[al_procedures].concat(taicpu.op_ref(A_JMP,S_NO,r));
  158. {$endif x86}
  159. end
  160. else
  161. cg.a_jmp_name(current_asmdata.asmlists[al_procedures],pd.mangledname);
  162. current_asmdata.asmlists[al_procedures].concat(Tai_symbol_end.Createname(hp2.name^));
  163. end;
  164. end
  165. else
  166. Message1(parser_e_no_export_of_variables_for_target,'*bsd/darwin');
  167. hp2:=texported_item(hp2.next);
  168. end;
  169. end;
  170. {*****************************************************************************
  171. TLINKERBSD
  172. *****************************************************************************}
  173. Constructor TLinkerBSD.Create;
  174. begin
  175. Inherited Create;
  176. if not Dontlinkstdlibpath Then
  177. if not(target_info.system in systems_darwin) then
  178. LibrarySearchPath.AddPath('/lib;/usr/lib;/usr/X11R6/lib',true)
  179. else
  180. { Mac OS X doesn't have a /lib }
  181. LibrarySearchPath.AddPath('/usr/lib',true)
  182. end;
  183. procedure TLinkerBSD.SetDefaultInfo;
  184. {
  185. This will also detect which libc version will be used
  186. }
  187. begin
  188. LibrarySuffix:=' ';
  189. LdSupportsNoResponseFile := (target_info.system in ([system_m68k_netbsd]+systems_darwin));
  190. with Info do
  191. begin
  192. if LdSupportsNoResponseFile then
  193. begin
  194. if not(target_info.system in systems_darwin) then
  195. begin
  196. ExeCmd[1]:='ld $OPT $DYNLINK $STATIC $GCSECTIONS $STRIP -L. -o $EXE `cat $RES`';
  197. DllCmd[1]:='ld $OPT -shared -L. -o $EXE `cat $RES`'
  198. end
  199. else
  200. begin
  201. ExeCmd[1]:='ld $OPT $DYNLINK $STATIC $GCSECTIONS $STRIP -multiply_defined suppress -L. -o $EXE `cat $RES`';
  202. if (not RelocSection) then
  203. DllCmd[1]:='libtool $OPT -dynamic -multiply_defined suppress -L. -o $EXE `cat $RES`'
  204. else
  205. DllCmd[1]:='ld $OPT -dynamic -bundle -multiply_defined suppress -L. -o $EXE `cat $RES`'
  206. end
  207. end
  208. else
  209. begin
  210. ExeCmd[1]:='ld $OPT $DYNLINK $STATIC $GCSECTIONS $STRIP -L. -o $EXE $RES';
  211. DllCmd[1]:='ld $OPT $INIT $FINI $SONAME -shared -L. -o $EXE $RES';
  212. end;
  213. if not(target_info.system in systems_darwin) then
  214. DllCmd[2]:='strip --strip-unneeded $EXE'
  215. else
  216. DllCmd[2]:='strip -x $EXE';
  217. DynamicLinker:='';
  218. end;
  219. end;
  220. procedure TLinkerBSD.LoadPredefinedLibraryOrder;
  221. // put your linkorder/linkalias overrides here.
  222. // Note: assumes only called when reordering/aliasing is used.
  223. Begin
  224. if not(target_info.system in systems_darwin) then
  225. begin
  226. if (target_info.system =system_i386_freebsd) and
  227. not (cs_link_no_default_lib_order in current_settings.globalswitches) Then
  228. Begin
  229. LinkLibraryOrder.add('gcc','',15);
  230. LinkLibraryOrder.add('c','',50); // c and c_p mutual. excl?
  231. LinkLibraryOrder.add('c_p','',55);
  232. LinkLibraryOrder.add('pthread','',75); // pthread and c_r should be mutually exclusive
  233. LinkLibraryOrder.add('c_r','',76);
  234. LinkLibraryOrder.add('kvm','',80); // must be before ncurses
  235. if (cs_link_pthread in current_settings.globalswitches) Then // convert libpthread to libc_r.
  236. LinkLibraryAliases.add('pthread','c_r');
  237. end;
  238. end
  239. else
  240. begin
  241. LinkLibraryOrder.add('gcc','',15);
  242. LinkLibraryOrder.add('c','',50);
  243. end;
  244. End;
  245. Function TLinkerBSD.WriteResponseFile(isdll:boolean) : Boolean;
  246. Var
  247. linkres : TLinkRes;
  248. i : longint;
  249. cprtobj,
  250. gprtobj,
  251. prtobj : string[80];
  252. HPath : TCmdStrListItem;
  253. s,s1,s2 : TCmdStr;
  254. linkdynamic,
  255. linklibc : boolean;
  256. Fl1,Fl2 : Boolean;
  257. IsDarwin : Boolean;
  258. ReOrder : Boolean;
  259. begin
  260. WriteResponseFile:=False;
  261. ReOrder:=False;
  262. IsDarwin:=target_info.system in systems_darwin;
  263. { set special options for some targets }
  264. if not IsDarwin Then
  265. begin
  266. prtobj:='prt0';
  267. cprtobj:='cprt0';
  268. gprtobj:='gprt0';
  269. linkdynamic:=not(SharedLibFiles.empty);
  270. linklibc:=(SharedLibFiles.Find('c')<>nil);
  271. // this one is a bit complex.
  272. // Only reorder for now if -XL or -XO params are given
  273. // or when -Xf.
  274. reorder:= linklibc and
  275. (
  276. ReorderEntries
  277. or
  278. (cs_link_pthread in current_settings.globalswitches));
  279. if cs_profile in current_settings.moduleswitches then
  280. begin
  281. prtobj:=gprtobj;
  282. AddSharedLibrary('c');
  283. LibrarySuffix:='p';
  284. linklibc:=true;
  285. end
  286. else
  287. begin
  288. if linklibc then
  289. prtobj:=cprtobj;
  290. end;
  291. // after this point addition of shared libs not allowed.
  292. end
  293. else
  294. begin
  295. { for darwin: always link dynamically against libc }
  296. linklibc := true;
  297. {$ifdef MACOSX104ORHIGHER}
  298. { not sure what this is for, but gcc always links against it }
  299. if not(cs_profile in current_settings.moduleswitches) then
  300. AddSharedLibrary('SystemStubs')
  301. else
  302. AddSharedLibrary('SystemStubs_profile');
  303. {$endif MACOSX104ORHIGHER}
  304. reorder:=reorderentries;
  305. if not(isdll) then
  306. if not(cs_profile in current_settings.moduleswitches) then
  307. begin
  308. if librarysearchpath.FindFile('crt1.o',false,s) then
  309. prtobj:=s
  310. else
  311. prtobj:='/usr/lib/crt1.o';
  312. end
  313. else
  314. begin
  315. if librarysearchpath.FindFile('gcrt1.o',false,s) then
  316. prtobj:=s
  317. else
  318. prtobj:='/usr/lib/gcrt1.o';
  319. end
  320. else
  321. begin
  322. if RelocSection then
  323. begin
  324. if librarysearchpath.FindFile('bundle1.o',false,s) then
  325. prtobj:=s
  326. else
  327. prtobj:='/usr/lib/bundle1.o'
  328. end
  329. else
  330. prtobj:=''
  331. end;
  332. end;
  333. if reorder Then
  334. ExpandAndApplyOrder(SharedLibFiles);
  335. { Open link.res file }
  336. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName);
  337. if (not isdll) then
  338. begin
  339. if (target_info.system in systems_darwin) then
  340. begin
  341. LinkRes.Add('-arch');
  342. case target_info.system of
  343. system_powerpc_darwin:
  344. LinkRes.Add('ppc');
  345. system_i386_darwin:
  346. LinkRes.Add('i386');
  347. system_powerpc64_darwin:
  348. LinkRes.Add('ppc64');
  349. system_x86_64_darwin:
  350. LinkRes.Add('x86_64');
  351. end;
  352. end;
  353. end;
  354. { Write path to search libraries }
  355. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  356. while assigned(HPath) do
  357. begin
  358. if LdSupportsNoResponseFile then
  359. LinkRes.Add('-L'+HPath.Str)
  360. else
  361. LinkRes.Add('SEARCH_DIR('+maybequoted(HPath.Str)+')');
  362. HPath:=TCmdStrListItem(HPath.Next);
  363. end;
  364. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  365. while assigned(HPath) do
  366. begin
  367. if LdSupportsNoResponseFile then
  368. LinkRes.Add('-L'+HPath.Str)
  369. else
  370. LinkRes.Add('SEARCH_DIR('+maybequoted(HPath.Str)+')');
  371. HPath:=TCmdStrListItem(HPath.Next);
  372. end;
  373. if (target_info.system in systems_darwin) then
  374. begin
  375. HPath:=TCmdStrListItem(current_module.localframeworksearchpath.First);
  376. while assigned(HPath) do
  377. begin
  378. LinkRes.Add('-F'+HPath.Str);
  379. HPath:=TCmdStrListItem(HPath.Next);
  380. end;
  381. HPath:=TCmdStrListItem(FrameworkSearchPath.First);
  382. while assigned(HPath) do
  383. begin
  384. LinkRes.Add('-F'+HPath.Str);
  385. HPath:=TCmdStrListItem(HPath.Next);
  386. end;
  387. end;
  388. if not LdSupportsNoResponseFile then
  389. LinkRes.Add('INPUT(');
  390. { add objectfiles, start with prt0 always }
  391. if prtobj<>'' then
  392. LinkRes.AddFileName(FindObjectFile(prtobj,'',false));
  393. { try to add crti and crtbegin if linking to C }
  394. if linklibc and
  395. not IsDarwin Then
  396. begin
  397. if librarysearchpath.FindFile('crtbegin.o',false,s) then
  398. LinkRes.AddFileName(s);
  399. if librarysearchpath.FindFile('crti.o',false,s) then
  400. LinkRes.AddFileName(s);
  401. end;
  402. { main objectfiles }
  403. while not ObjectFiles.Empty do
  404. begin
  405. s:=ObjectFiles.GetFirst;
  406. if s<>'' then
  407. if LdSupportsNoResponseFile then
  408. LinkRes.AddFileName(s)
  409. else
  410. LinkRes.AddFileName(maybequoted(s));
  411. end;
  412. if not LdSupportsNoResponseFile then
  413. LinkRes.Add(')');
  414. { Write staticlibraries }
  415. if not StaticLibFiles.Empty then
  416. begin
  417. if not LdSupportsNoResponseFile then
  418. LinkRes.Add('GROUP(');
  419. While not StaticLibFiles.Empty do
  420. begin
  421. S:=StaticLibFiles.GetFirst;
  422. if LdSupportsNoResponseFile then
  423. LinkRes.AddFileName(s)
  424. else
  425. LinkRes.AddFileName(maybequoted(s))
  426. end;
  427. if not LdSupportsNoResponseFile then
  428. LinkRes.Add(')');
  429. end;
  430. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  431. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  432. if not SharedLibFiles.Empty then
  433. begin
  434. if not LdSupportsNoResponseFile then
  435. LinkRes.Add('INPUT(');
  436. While not SharedLibFiles.Empty do
  437. begin
  438. S:=SharedLibFiles.GetFirst;
  439. if (s<>'c') or reorder then
  440. begin
  441. i:=Pos(target_info.sharedlibext,S);
  442. if i>0 then
  443. Delete(S,i,255);
  444. LinkRes.Add('-l'+s);
  445. end
  446. else
  447. begin
  448. linklibc:=true;
  449. linkdynamic:=false; { libc will include the ld-* for us }
  450. end;
  451. end;
  452. { be sure that libc is the last lib }
  453. if linklibc and not reorder then
  454. Begin
  455. If LibrarySuffix=' ' Then
  456. LinkRes.Add('-lc')
  457. else
  458. LinkRes.Add('-lc_'+LibrarySuffix);
  459. If LibrarySuffix='r' Then
  460. LinkRes.Add('-lc');
  461. end;
  462. { when we have -static for the linker the we also need libgcc }
  463. if (cs_link_staticflag in current_settings.globalswitches) then
  464. LinkRes.Add('-lgcc');
  465. if linkdynamic and (Info.DynamicLinker<>'') then
  466. LinkRes.AddFileName(Info.DynamicLinker);
  467. if not LdSupportsNoResponseFile then
  468. LinkRes.Add(')');
  469. end;
  470. { frameworks for Darwin }
  471. if IsDarwin then
  472. while not FrameworkFiles.empty do
  473. begin
  474. LinkRes.Add('-framework');
  475. LinkRes.Add(FrameworkFiles.GetFirst);
  476. end;
  477. { objects which must be at the end }
  478. if linklibc and
  479. not IsDarwin Then
  480. begin
  481. Fl1:=librarysearchpath.FindFile('crtend.o',false,s1);
  482. Fl2:=librarysearchpath.FindFile('crtn.o',false,s2);
  483. if Fl1 or Fl2 then
  484. begin
  485. LinkRes.Add('INPUT(');
  486. If Fl1 Then
  487. LinkRes.AddFileName(s1);
  488. If Fl2 Then
  489. LinkRes.AddFileName(s2);
  490. LinkRes.Add(')');
  491. end;
  492. end;
  493. { ignore the fact that our relocations are in non-writable sections, }
  494. { will be fixed once we have pic support }
  495. if isdll and IsDarwin Then
  496. begin
  497. LinkRes.Add('-read_only_relocs');
  498. LinkRes.Add('suppress');
  499. end;
  500. { Write and Close response }
  501. linkres.writetodisk;
  502. linkres.Free;
  503. WriteResponseFile:=True;
  504. end;
  505. function TLinkerBSD.MakeExecutable:boolean;
  506. var
  507. binstr,
  508. cmdstr : TCmdStr;
  509. success : boolean;
  510. DynLinkStr : string[60];
  511. GCSectionsStr,
  512. StaticStr,
  513. StripStr : string[40];
  514. begin
  515. if not(cs_link_nolink in current_settings.globalswitches) then
  516. Message1(exec_i_linking,current_module.exefilename^);
  517. { Create some replacements }
  518. StaticStr:='';
  519. StripStr:='';
  520. DynLinkStr:='';
  521. GCSectionsStr:='';
  522. if (cs_link_staticflag in current_settings.globalswitches) then
  523. begin
  524. if (target_info.system=system_m68k_netbsd) and
  525. ((cs_link_on_target in current_settings.globalswitches) or
  526. (target_info.system=source_info.system)) then
  527. StaticStr:='-Bstatic'
  528. else
  529. StaticStr:='-static';
  530. end;
  531. if (cs_link_strip in current_settings.globalswitches) then
  532. StripStr:='-s';
  533. if (cs_link_smart in current_settings.globalswitches) and
  534. (tf_smartlink_sections in target_info.flags) then
  535. if not(target_info.system in systems_darwin) then
  536. GCSectionsStr:='--gc-sections'
  537. else
  538. // warning: this option only exists for 32 bit under Mac OS X, maybe the default for 64 bit?
  539. GCSectionsStr:='-dead_strip';
  540. if(not(target_info.system in systems_darwin) and
  541. (cs_profile in current_settings.moduleswitches)) or
  542. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty)) then
  543. DynLinkStr:='-dynamic-linker='+Info.DynamicLinker;
  544. if CShared Then
  545. begin
  546. if not(target_info.system in systems_darwin) then
  547. DynLinKStr:=DynLinkStr+' --shared'
  548. else
  549. DynLinKStr:=DynLinkStr+' -dynamic'; // one dash!
  550. end;
  551. { Write used files and libraries }
  552. WriteResponseFile(false);
  553. { Call linker }
  554. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  555. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename^));
  556. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  557. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  558. Replace(cmdstr,'$STATIC',StaticStr);
  559. Replace(cmdstr,'$STRIP',StripStr);
  560. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  561. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  562. success:=DoExec(FindUtil(utilsprefix+BinStr),CmdStr,true,LdSupportsNoResponseFile);
  563. { Remove ReponseFile }
  564. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  565. DeleteFile(outputexedir+Info.ResName);
  566. MakeExecutable:=success; { otherwise a recursive call to link method }
  567. end;
  568. Function TLinkerBSD.MakeSharedLibrary:boolean;
  569. var
  570. InitStr,
  571. FiniStr,
  572. SoNameStr : string[80];
  573. binstr,
  574. cmdstr : TCmdStr;
  575. success : boolean;
  576. begin
  577. MakeSharedLibrary:=false;
  578. if not(cs_link_nolink in current_settings.globalswitches) then
  579. Message1(exec_i_linking,current_module.sharedlibfilename^);
  580. { Write used files and libraries }
  581. WriteResponseFile(true);
  582. InitStr:='-init FPC_LIB_START';
  583. FiniStr:='-fini FPC_LIB_EXIT';
  584. SoNameStr:='-soname '+ExtractFileName(current_module.sharedlibfilename^);
  585. { Call linker }
  586. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  587. {$ifndef darwin}
  588. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename^));
  589. {$else darwin}
  590. Replace(cmdstr,'$EXE',maybequoted(ExpandFileName(current_module.sharedlibfilename^)));
  591. {$endif darwin}
  592. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  593. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  594. Replace(cmdstr,'$INIT',InitStr);
  595. Replace(cmdstr,'$FINI',FiniStr);
  596. Replace(cmdstr,'$SONAME',SoNameStr);
  597. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,LdSupportsNoResponseFile);
  598. { Strip the library ? }
  599. if success and (cs_link_strip in current_settings.globalswitches) then
  600. begin
  601. SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  602. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename^));
  603. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  604. end;
  605. { Remove ReponseFile }
  606. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  607. DeleteFile(outputexedir+Info.ResName);
  608. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  609. end;
  610. {*****************************************************************************
  611. Initialize
  612. *****************************************************************************}
  613. initialization
  614. {$ifdef x86_64}
  615. RegisterExternalLinker(system_x86_64_FreeBSD_info,TLinkerBSD);
  616. RegisterImport(system_x86_64_freebsd,timportlibbsd);
  617. RegisterExport(system_x86_64_freebsd,texportlibbsd);
  618. RegisterTarget(system_x86_64_freebsd_info);
  619. {$endif}
  620. {$ifdef i386}
  621. RegisterExternalLinker(system_i386_FreeBSD_info,TLinkerBSD);
  622. RegisterExternalLinker(system_i386_NetBSD_info,TLinkerBSD);
  623. RegisterExternalLinker(system_i386_OpenBSD_info,TLinkerBSD);
  624. RegisterImport(system_i386_freebsd,timportlibbsd);
  625. RegisterExport(system_i386_freebsd,texportlibbsd);
  626. RegisterTarget(system_i386_freebsd_info);
  627. RegisterImport(system_i386_netbsd,timportlibbsd);
  628. RegisterExport(system_i386_netbsd,texportlibbsd);
  629. RegisterTarget(system_i386_netbsd_info);
  630. RegisterImport(system_i386_openbsd,timportlibbsd);
  631. RegisterExport(system_i386_openbsd,texportlibbsd);
  632. RegisterTarget(system_i386_openbsd_info);
  633. RegisterExternalLinker(system_i386_darwin_info,TLinkerBSD);
  634. RegisterImport(system_i386_darwin,timportlibdarwin);
  635. RegisterExport(system_i386_darwin,texportlibbsd);
  636. RegisterTarget(system_i386_darwin_info);
  637. {$endif i386}
  638. {$ifdef m68k}
  639. // RegisterExternalLinker(system_m68k_FreeBSD_info,TLinkerBSD);
  640. RegisterExternalLinker(system_m68k_NetBSD_info,TLinkerBSD);
  641. RegisterImport(system_m68k_netbsd,timportlibbsd);
  642. RegisterExport(system_m68k_netbsd,texportlibbsd);
  643. RegisterTarget(system_m68k_netbsd_info);
  644. {$endif m68k}
  645. {$ifdef powerpc}
  646. // RegisterExternalLinker(system_m68k_FreeBSD_info,TLinkerBSD);
  647. RegisterExternalLinker(system_powerpc_darwin_info,TLinkerBSD);
  648. RegisterImport(system_powerpc_darwin,timportlibdarwin);
  649. RegisterExport(system_powerpc_darwin,texportlibbsd);
  650. RegisterTarget(system_powerpc_darwin_info);
  651. RegisterExternalLinker(system_powerpc_netbsd_info,TLinkerBSD);
  652. RegisterImport(system_powerpc_netbsd,timportlibbsd);
  653. RegisterExport(system_powerpc_netbsd,texportlibbsd);
  654. RegisterTarget(system_powerpc_netbsd_info);
  655. {$endif powerpc}
  656. {$ifdef powerpc64}
  657. RegisterExternalLinker(system_powerpc64_darwin_info,TLinkerBSD);
  658. RegisterImport(system_powerpc64_darwin,timportlibdarwin);
  659. RegisterExport(system_powerpc64_darwin,texportlibbsd);
  660. RegisterTarget(system_powerpc64_darwin_info);
  661. {$endif powerpc64}
  662. end.