t_bsd.pas 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  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,cscript,
  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. timportlibbsd=class(timportlib)
  33. procedure generatelib;override;
  34. end;
  35. texportlibbsd=class(texportlibunix)
  36. end;
  37. tlinkerbsd=class(texternallinker)
  38. private
  39. LdSupportsNoResponseFile : boolean;
  40. LibrarySuffix : Char;
  41. prtobj : string[80];
  42. ReOrder : Boolean;
  43. linklibc : boolean;
  44. Function WriteResponseFile(isdll:boolean) : Boolean;
  45. public
  46. constructor Create;override;
  47. procedure SetDefaultInfo;override;
  48. function MakeExecutable:boolean;override;
  49. function MakeSharedLibrary:boolean;override;
  50. procedure LoadPredefinedLibraryOrder; override;
  51. procedure InitSysInitUnitName; override;
  52. end;
  53. function ModulesLinkToLibc:boolean;
  54. var
  55. hp: tmodule;
  56. begin
  57. { This is called very early, ImportLibraryList is not yet merged into linkothersharedlibs.
  58. The former contains library names qualified with prefix and suffix (coming from
  59. "external 'c' name 'foo' declarations), the latter contains raw names (from "$linklib c"
  60. directives). }
  61. hp:=tmodule(loaded_units.first);
  62. while assigned(hp) do
  63. begin
  64. result:=Assigned(hp.ImportLibraryList.find(target_info.sharedClibprefix+'c'+target_info.sharedClibext));
  65. if result then break;
  66. result:=hp.linkothersharedlibs.find(target_info.sharedClibprefix+'c'+target_info.sharedClibext);
  67. if result then break;
  68. result:=hp.linkothersharedlibs.find('c');
  69. if result then break;
  70. hp:=tmodule(hp.next);
  71. end;
  72. end;
  73. {*****************************************************************************
  74. TIMPORTLIBBSD
  75. *****************************************************************************}
  76. procedure timportlibbsd.generatelib;
  77. var
  78. i : longint;
  79. ImportLibrary : TImportLibrary;
  80. begin
  81. for i:=0 to current_module.ImportLibraryList.Count-1 do
  82. begin
  83. ImportLibrary:=TImportLibrary(current_module.ImportLibraryList[i]);
  84. current_module.linkothersharedlibs.add(ImportLibrary.Name,link_always);
  85. end;
  86. end;
  87. {*****************************************************************************
  88. TLINKERBSD
  89. *****************************************************************************}
  90. Constructor TLinkerBSD.Create;
  91. begin
  92. Inherited Create;
  93. if not Dontlinkstdlibpath Then
  94. if target_info.system in systems_openbsd then
  95. LibrarySearchPath.AddLibraryPath(sysrootpath,'=/usr/lib;=$OPENBSD_X11BASE/lib;=$OPENBSD_LOCALBASE/lib',true)
  96. else
  97. LibrarySearchPath.AddLibraryPath(sysrootpath,'=/lib;=/usr/lib;=/usr/X11R6/lib',true);
  98. end;
  99. procedure TLinkerBSD.SetDefaultInfo;
  100. {
  101. This will also detect which libc version will be used
  102. }
  103. var
  104. LdProgram: string='ld';
  105. begin
  106. if target_info.system in (systems_openbsd+[system_x86_64_dragonfly]) then
  107. LdProgram:='ld.bfd';
  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. ExeCmd[1]:=LdProgram+' $TARGET $EMUL $OPT $DYNLINK $STATIC $GCSECTIONS $STRIP $MAP $LTO $ORDERSYMS -L. -o $EXE $CATRES $FILELIST';
  115. DllCmd[1]:=LdProgram+' $TARGET $EMUL $OPT $MAP $LTO $ORDERSYMS -shared -L. -o $EXE $CATRES $FILELIST'
  116. end
  117. else
  118. begin
  119. ExeCmd[1]:=LdProgram+' $TARGET $EMUL $OPT $DYNLINK $STATIC $GCSECTIONS $STRIP $MAP $LTO $ORDERSYMS -L. -o $EXE $RES';
  120. DllCmd[1]:=LdProgram+' $TARGET $EMUL $OPT $INIT $FINI $SONAME $MAP $LTO $ORDERSYMS -shared -L. -o $EXE $RES';
  121. end;
  122. DllCmd[2]:='strip --strip-unneeded $EXE';
  123. { OpenBSD seems to use a wrong dynamic linker by default }
  124. if target_info.system in systems_openbsd then
  125. DynamicLinker:='/usr/libexec/ld.so'
  126. else if target_info.system in systems_netbsd then
  127. DynamicLinker:='/usr/libexec/ld.elf_so'
  128. else if target_info.system=system_x86_64_dragonfly then
  129. DynamicLinker:='/libexec/ld-elf.so.2'
  130. else
  131. DynamicLinker:='';
  132. end;
  133. end;
  134. procedure TLinkerBSD.LoadPredefinedLibraryOrder;
  135. // put your linkorder/linkalias overrides here.
  136. // Note: assumes only called when reordering/aliasing is used.
  137. Begin
  138. if (target_info.system =system_i386_freebsd) and
  139. not (cs_link_no_default_lib_order in current_settings.globalswitches) Then
  140. Begin
  141. LinkLibraryOrder.add('gcc','',15);
  142. LinkLibraryOrder.add('c','',50); // c and c_p mutual. excl?
  143. LinkLibraryOrder.add('c_p','',55);
  144. LinkLibraryOrder.add('pthread','',75); // pthread and c_r should be mutually exclusive
  145. LinkLibraryOrder.add('c_r','',76);
  146. LinkLibraryOrder.add('kvm','',80); // must be before ncurses
  147. if (cs_link_pthread in current_settings.globalswitches) Then // convert libpthread to libc_r.
  148. LinkLibraryAliases.add('pthread','c_r');
  149. end;
  150. End;
  151. procedure TLinkerBSD.InitSysInitUnitName;
  152. var
  153. cprtobj,
  154. gprtobj,
  155. si_cprt,
  156. si_gprt : string[80];
  157. begin
  158. linklibc:=ModulesLinkToLibc;
  159. if current_module.islibrary and
  160. (target_info.system in systems_bsd) then
  161. begin
  162. prtobj:='dllprt0';
  163. cprtobj:='dllprt0';
  164. gprtobj:='dllprt0';
  165. SysInitUnit:='si_dll';
  166. si_cprt:='si_dll';
  167. si_gprt:='si_dll';
  168. end
  169. else
  170. begin
  171. prtobj:='prt0';
  172. cprtobj:='cprt0';
  173. gprtobj:='gprt0';
  174. SysInitUnit:='si_prc';
  175. si_cprt:='si_c';
  176. si_gprt:='si_g';
  177. end;
  178. // this one is a bit complex.
  179. // Only reorder for now if -XL or -XO params are given
  180. // or when -Xf.
  181. reorder:= linklibc and
  182. (
  183. ReorderEntries
  184. or
  185. (cs_link_pthread in current_settings.globalswitches));
  186. if cs_profile in current_settings.moduleswitches then
  187. begin
  188. prtobj:=gprtobj;
  189. SysInitUnit:=si_gprt;
  190. AddSharedLibrary('c');
  191. LibrarySuffix:='p';
  192. linklibc:=true;
  193. end
  194. else
  195. begin
  196. if linklibc then
  197. begin
  198. prtobj:=cprtobj;
  199. SysInitUnit:=si_cprt;
  200. end;
  201. end;
  202. end;
  203. Function TLinkerBSD.WriteResponseFile(isdll:boolean) : Boolean;
  204. Var
  205. linkres : TLinkRes;
  206. i : longint;
  207. HPath : TCmdStrListItem;
  208. s,s1,s2 : TCmdStr;
  209. linkdynamic : boolean;
  210. Fl1,Fl2 : Boolean;
  211. begin
  212. WriteResponseFile:=False;
  213. ReOrder:=False;
  214. linkdynamic:=False;
  215. { set special options for some targets }
  216. linkdynamic:=not(SharedLibFiles.empty);
  217. // after this point addition of shared libs not allowed.
  218. if reorder Then
  219. ExpandAndApplyOrder(SharedLibFiles);
  220. { Open link.res file }
  221. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,not LdSupportsNoResponseFile);
  222. { Write path to search libraries }
  223. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  224. while assigned(HPath) do
  225. begin
  226. if LdSupportsNoResponseFile then
  227. LinkRes.Add('-L'+HPath.Str)
  228. else
  229. LinkRes.Add('SEARCH_DIR("'+HPath.Str+'")');
  230. HPath:=TCmdStrListItem(HPath.Next);
  231. end;
  232. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  233. while assigned(HPath) do
  234. begin
  235. if LdSupportsNoResponseFile then
  236. LinkRes.Add('-L'+HPath.Str)
  237. else
  238. LinkRes.Add('SEARCH_DIR("'+HPath.Str+'")');
  239. HPath:=TCmdStrListItem(HPath.Next);
  240. end;
  241. { force local symbol resolution (i.e., inside the shared }
  242. { library itself) for all non-exorted symbols, otherwise }
  243. { several RTL symbols of FPC-compiled shared libraries }
  244. { will be bound to those of a single shared library or }
  245. { to the main program }
  246. if (isdll) and (target_info.system in systems_bsd) then
  247. begin
  248. LinkRes.add('VERSION');
  249. LinkRes.add('{');
  250. LinkRes.add(' {');
  251. if not texportlibunix(exportlib).exportedsymnames.empty then
  252. begin
  253. LinkRes.add(' global:');
  254. repeat
  255. LinkRes.add(' '+texportlibunix(exportlib).exportedsymnames.getfirst+';');
  256. until texportlibunix(exportlib).exportedsymnames.empty;
  257. end;
  258. LinkRes.add(' local:');
  259. LinkRes.add(' *;');
  260. LinkRes.add(' };');
  261. LinkRes.add('}');
  262. end;
  263. if not LdSupportsNoResponseFile then
  264. LinkRes.Add('INPUT(');
  265. { add objectfiles, start with prt0 always }
  266. if not (target_info.system in systems_internal_sysinit) and (prtobj<>'') then
  267. LinkRes.AddFileName(FindObjectFile(prtobj,'',false));
  268. { try to add crti and crtbegin if linking to C }
  269. if linklibc then
  270. begin
  271. if librarysearchpath.FindFile('crti.o',false,s) then
  272. LinkRes.AddFileName(s);
  273. if ((cs_create_pic in current_settings.moduleswitches) and
  274. not (target_info.system in systems_openbsd)) or
  275. (current_module.islibrary and
  276. (target_info.system in systems_openbsd)) then
  277. begin
  278. if librarysearchpath.FindFile('crtbeginS.o',false,s) then
  279. LinkRes.AddFileName(s);
  280. end
  281. else
  282. if (cs_link_staticflag in current_settings.globalswitches) and
  283. librarysearchpath.FindFile('crtbeginT.o',false,s) then
  284. LinkRes.AddFileName(s)
  285. else if librarysearchpath.FindFile('crtbegin.o',false,s) then
  286. LinkRes.AddFileName(s);
  287. end;
  288. { main objectfiles }
  289. while not ObjectFiles.Empty do
  290. begin
  291. s:=ObjectFiles.GetFirst;
  292. if s<>'' then
  293. if LdSupportsNoResponseFile then
  294. LinkRes.AddFileName(s)
  295. else
  296. LinkRes.AddFileName(maybequoted(s));
  297. end;
  298. if not LdSupportsNoResponseFile then
  299. LinkRes.Add(')');
  300. { Write staticlibraries }
  301. if not StaticLibFiles.Empty then
  302. begin
  303. if not LdSupportsNoResponseFile then
  304. LinkRes.Add('GROUP(');
  305. While not StaticLibFiles.Empty do
  306. begin
  307. S:=StaticLibFiles.GetFirst;
  308. if LdSupportsNoResponseFile then
  309. LinkRes.AddFileName(s)
  310. else
  311. LinkRes.AddFileName(maybequoted(s))
  312. end;
  313. if not LdSupportsNoResponseFile then
  314. LinkRes.Add(')');
  315. end;
  316. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  317. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  318. if not SharedLibFiles.Empty then
  319. begin
  320. if not LdSupportsNoResponseFile then
  321. LinkRes.Add('INPUT(');
  322. While not SharedLibFiles.Empty do
  323. begin
  324. S:=SharedLibFiles.GetFirst;
  325. if (s<>'c') or reorder then
  326. begin
  327. i:=Pos(target_info.sharedlibext,S);
  328. if i>0 then
  329. Delete(S,i,255);
  330. LinkRes.Add('-l'+s);
  331. end
  332. else
  333. begin
  334. linklibc:=true;
  335. linkdynamic:=false; { libc will include the ld-* for us }
  336. end;
  337. end;
  338. { be sure that libc is the last lib }
  339. if linklibc and not reorder then
  340. Begin
  341. If LibrarySuffix=' ' Then
  342. LinkRes.Add('-lc')
  343. else
  344. LinkRes.Add('-lc_'+LibrarySuffix);
  345. If LibrarySuffix='r' Then
  346. LinkRes.Add('-lc');
  347. end;
  348. { when we have -static for the linker the we also need libgcc }
  349. if (cs_link_staticflag in current_settings.globalswitches) then
  350. LinkRes.Add('-lgcc');
  351. if linkdynamic and (Info.DynamicLinker<>'') and
  352. not(target_info.system in systems_openbsd) then
  353. LinkRes.AddFileName(Info.DynamicLinker);
  354. if not LdSupportsNoResponseFile then
  355. LinkRes.Add(')');
  356. end;
  357. { objects which must be at the end }
  358. if linklibc then
  359. begin
  360. if ((cs_create_pic in current_settings.moduleswitches) and
  361. not (target_info.system in systems_openbsd)) or
  362. (current_module.islibrary and
  363. (target_info.system in systems_openbsd)) then
  364. Fl1:=librarysearchpath.FindFile('crtendS.o',false,s1)
  365. else
  366. Fl1:=librarysearchpath.FindFile('crtend.o',false,s1);
  367. Fl2:=librarysearchpath.FindFile('crtn.o',false,s2);
  368. if Fl1 or Fl2 then
  369. begin
  370. LinkRes.Add('INPUT(');
  371. If Fl1 Then
  372. LinkRes.AddFileName(s1);
  373. If Fl2 Then
  374. LinkRes.AddFileName(s2);
  375. LinkRes.Add(')');
  376. end;
  377. end;
  378. { Write and Close response }
  379. linkres.writetodisk;
  380. linkres.Free;
  381. WriteResponseFile:=True;
  382. end;
  383. function TLinkerBSD.MakeExecutable:boolean;
  384. var
  385. binstr,
  386. cmdstr,
  387. mapstr,
  388. targetstr,
  389. emulstr,
  390. ltostr,
  391. ordersymfile: TCmdStr;
  392. linkscript: TAsmScript;
  393. DynLinkStr : ansistring;
  394. GCSectionsStr,
  395. StaticStr,
  396. StripStr : string[63];
  397. success,
  398. useshell : boolean;
  399. begin
  400. if not(cs_link_nolink in current_settings.globalswitches) then
  401. Message1(exec_i_linking,current_module.exefilename);
  402. { Create some replacements }
  403. StaticStr:='';
  404. StripStr:='';
  405. DynLinkStr:='';
  406. GCSectionsStr:='';
  407. linkscript:=nil;
  408. mapstr:='';
  409. ltostr:='';
  410. if (cs_link_map in current_settings.globalswitches) then
  411. mapstr:='-Map '+maybequoted(ChangeFileExt(current_module.exefilename,'.map'));
  412. { i386_freebsd needs -b elf32-i386-freebsd and -m elf_i386_fbsd
  413. to avoid creation of a i386:x86_64 arch binary }
  414. if target_info.system=system_i386_freebsd then
  415. begin
  416. targetstr:='-b elf32-i386-freebsd';
  417. emulstr:='-m elf_i386_fbsd';
  418. end
  419. else
  420. begin
  421. targetstr:='';
  422. emulstr:='';
  423. end;
  424. if (cs_link_staticflag in current_settings.globalswitches) then
  425. begin
  426. if (target_info.system=system_m68k_netbsd) and
  427. ((cs_link_on_target in current_settings.globalswitches) or
  428. (target_info.system=source_info.system)) then
  429. StaticStr:='-Bstatic'
  430. else
  431. StaticStr:='-static';
  432. end;
  433. if (cs_link_strip in current_settings.globalswitches) then
  434. StripStr:='-s';
  435. if (cs_link_smart in current_settings.globalswitches) and
  436. (tf_smartlink_sections in target_info.flags) then
  437. GCSectionsStr:='--gc-sections';
  438. if (cs_profile in current_settings.moduleswitches) or
  439. ((Info.DynamicLinker<>'') and
  440. ((not SharedLibFiles.Empty) or
  441. (target_info.system in systems_openbsd))) then
  442. DynLinkStr:='-dynamic-linker='+Info.DynamicLinker;
  443. if rlinkpath<>'' then
  444. DynLinkStr:=DynLinkStr+' --rpath-link '+rlinkpath;
  445. if CShared Then
  446. begin
  447. DynLinKStr:=DynLinkStr+' --shared'
  448. end;
  449. { Use -nopie on OpenBSD if PIC support is turned off }
  450. if (target_info.system in systems_openbsd) and
  451. not(cs_create_pic in current_settings.moduleswitches) then
  452. Info.ExtraOptions:=Info.ExtraOptions+' -nopie';
  453. { -N seems to be needed on NetBSD/earm }
  454. if (target_info.system in [system_arm_netbsd]) then
  455. Info.ExtraOptions:=Info.ExtraOptions+' -N';
  456. { Write used files and libraries }
  457. WriteResponseFile(false);
  458. { Write symbol order file }
  459. ordersymfile:=WriteSymbolOrderFile;
  460. { Call linker }
  461. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  462. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename));
  463. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  464. Replace(cmdstr,'$TARGET',targetstr);
  465. Replace(cmdstr,'$EMUL',EmulStr);
  466. Replace(cmdstr,'$MAP',mapstr);
  467. Replace(cmdstr,'$CATRES',CatFileContent(outputexedir+Info.ResName));
  468. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  469. Replace(cmdstr,'$LTO',ltostr);
  470. if ordersymfile<>'' then
  471. Replace(cmdstr,'$ORDERSYMS','--symbol-ordering-file '+maybequoted(ordersymfile))
  472. else
  473. Replace(cmdstr,'$ORDERSYMS','');
  474. Replace(cmdstr,'$FILELIST','');
  475. Replace(cmdstr,'$STATIC',StaticStr);
  476. Replace(cmdstr,'$STRIP',StripStr);
  477. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  478. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  479. BinStr:=FindUtil(utilsprefix+BinStr);
  480. if (LdSupportsNoResponseFile) and
  481. not(cs_link_nolink in current_settings.globalswitches) then
  482. begin
  483. { we have to use a script to use the IFS hack }
  484. linkscript:=GenerateScript(outputexedir+'ppaslink');
  485. linkscript.AddLinkCommand(BinStr,CmdStr,'');
  486. linkscript.WriteToDisk;
  487. BinStr:=linkscript.fn;
  488. if not path_absolute(BinStr) then
  489. if cs_link_on_target in current_settings.globalswitches then
  490. BinStr:='.'+target_info.dirsep+BinStr
  491. else
  492. BinStr:='.'+source_info.dirsep+BinStr;
  493. CmdStr:='';
  494. end;
  495. useshell:=not (tf_no_backquote_support in source_info.flags);
  496. success:=DoExec(BinStr,CmdStr,true,LdSupportsNoResponseFile or useshell);
  497. { Remove ReponseFile }
  498. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  499. begin
  500. DeleteFile(outputexedir+Info.ResName);
  501. if ordersymfile<>'' then
  502. DeleteFile(ordersymfile);
  503. if LdSupportsNoResponseFile Then
  504. begin
  505. DeleteFile(linkscript.fn);
  506. linkscript.free
  507. end;
  508. end;
  509. MakeExecutable:=success; { otherwise a recursive call to link method }
  510. end;
  511. Function TLinkerBSD.MakeSharedLibrary:boolean;
  512. var
  513. InitStr,
  514. FiniStr,
  515. SoNameStr : string[80];
  516. linkscript: TAsmScript;
  517. binstr,
  518. cmdstr,
  519. mapstr,
  520. ltostr,
  521. ordersymfile,
  522. targetstr,
  523. emulstr : TCmdStr;
  524. GCSectionsStr : string[63];
  525. success : boolean;
  526. begin
  527. MakeSharedLibrary:=false;
  528. GCSectionsStr:='';
  529. mapstr:='';
  530. ltostr:='';
  531. linkscript:=nil;
  532. if not(cs_link_nolink in current_settings.globalswitches) then
  533. Message1(exec_i_linking,current_module.sharedlibfilename);
  534. { Write used files and libraries }
  535. WriteResponseFile(true);
  536. { Write symbol order file }
  537. ordersymfile:=WriteSymbolOrderFile;
  538. if (cs_link_smart in current_settings.globalswitches) and
  539. (tf_smartlink_sections in target_info.flags) then
  540. { disabled because not tested
  541. GCSectionsStr:='--gc-sections' }
  542. ;
  543. if (cs_link_map in current_settings.globalswitches) then
  544. mapstr:='-Map '+maybequoted(ChangeFileExt(current_module.sharedlibfilename,'.map'));
  545. { i386_freebsd needs -b elf32-i386-freebsd and -m elf_i386_fbsd
  546. to avoid creation of a i386:x86_64 arch binary }
  547. if target_info.system=system_i386_freebsd then
  548. begin
  549. targetstr:='-b elf32-i386-freebsd';
  550. emulstr:='-m elf_i386_fbsd';
  551. end
  552. else
  553. begin
  554. targetstr:='';
  555. emulstr:='';
  556. end;
  557. InitStr:='-init FPC_LIB_START';
  558. FiniStr:='-fini FPC_LIB_EXIT';
  559. SoNameStr:='-soname '+ExtractFileName(current_module.sharedlibfilename);
  560. { Call linker }
  561. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  562. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename));
  563. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  564. Replace(cmdstr,'$TARGET',targetstr);
  565. Replace(cmdstr,'$EMUL',EmulStr);
  566. Replace(cmdstr,'$CATRES',CatFileContent(outputexedir+Info.ResName));
  567. Replace(cmdstr,'$FILELIST','');
  568. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  569. Replace(cmdstr,'$INIT',InitStr);
  570. Replace(cmdstr,'$FINI',FiniStr);
  571. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  572. Replace(cmdstr,'$SONAME',SoNameStr);
  573. Replace(cmdstr,'$MAP',mapstr);
  574. Replace(cmdstr,'$LTO',ltostr);
  575. if ordersymfile<>'' then
  576. Replace(cmdstr,'$ORDERSYMS','--symbol-ordering-file '+maybequoted(ordersymfile))
  577. else
  578. Replace(cmdstr,'$ORDERSYMS','');
  579. BinStr:=FindUtil(utilsprefix+BinStr);
  580. if (LdSupportsNoResponseFile) and
  581. not(cs_link_nolink in current_settings.globalswitches) then
  582. begin
  583. { we have to use a script to use the IFS hack }
  584. linkscript:=GenerateScript(outputexedir+'ppaslink');
  585. linkscript.AddLinkCommand(BinStr,CmdStr,'');
  586. linkscript.WriteToDisk;
  587. BinStr:=linkscript.fn;
  588. if not path_absolute(BinStr) then
  589. if cs_link_on_target in current_settings.globalswitches then
  590. BinStr:='.'+target_info.dirsep+BinStr
  591. else
  592. BinStr:='.'+source_info.dirsep+BinStr;
  593. CmdStr:='';
  594. end;
  595. success:=DoExec(BinStr,cmdstr,true,LdSupportsNoResponseFile);
  596. { Strip the library ? }
  597. if success and (cs_link_strip in current_settings.globalswitches) then
  598. begin
  599. SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  600. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename));
  601. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,false,false);
  602. end;
  603. { Remove ReponseFile }
  604. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  605. begin
  606. DeleteFile(outputexedir+Info.ResName);
  607. if ordersymfile<>'' then
  608. DeleteFile(ordersymfile);
  609. if LdSupportsNoResponseFile Then
  610. begin
  611. DeleteFile(linkscript.fn);
  612. linkscript.free
  613. end;
  614. end;
  615. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  616. end;
  617. {*****************************************************************************
  618. Initialize
  619. *****************************************************************************}
  620. initialization
  621. RegisterLinker(ld_bsd,TLinkerBSD);
  622. {$ifdef aarch64}
  623. RegisterImport(system_aarch64_freebsd,timportlibbsd);
  624. RegisterExport(system_aarch64_freebsd,texportlibbsd);
  625. RegisterTarget(system_aarch64_freebsd_info);
  626. {$endif aarch64}
  627. {$ifdef x86_64}
  628. RegisterImport(system_x86_64_dragonfly,timportlibbsd);
  629. RegisterExport(system_x86_64_dragonfly,texportlibbsd);
  630. RegisterTarget(system_x86_64_dragonfly_info);
  631. RegisterImport(system_x86_64_freebsd,timportlibbsd);
  632. RegisterExport(system_x86_64_freebsd,texportlibbsd);
  633. RegisterTarget(system_x86_64_freebsd_info);
  634. RegisterImport(system_x86_64_openbsd,timportlibbsd);
  635. RegisterExport(system_x86_64_openbsd,texportlibbsd);
  636. RegisterTarget(system_x86_64_openbsd_info);
  637. RegisterImport(system_x86_64_netbsd,timportlibbsd);
  638. RegisterExport(system_x86_64_netbsd,texportlibbsd);
  639. RegisterTarget(system_x86_64_netbsd_info);
  640. {$endif}
  641. {$ifdef i386}
  642. RegisterImport(system_i386_freebsd,timportlibbsd);
  643. RegisterExport(system_i386_freebsd,texportlibbsd);
  644. RegisterTarget(system_i386_freebsd_info);
  645. RegisterImport(system_i386_netbsd,timportlibbsd);
  646. RegisterExport(system_i386_netbsd,texportlibbsd);
  647. RegisterTarget(system_i386_netbsd_info);
  648. RegisterImport(system_i386_openbsd,timportlibbsd);
  649. RegisterExport(system_i386_openbsd,texportlibbsd);
  650. RegisterTarget(system_i386_openbsd_info);
  651. {$endif i386}
  652. {$ifdef m68k}
  653. RegisterImport(system_m68k_netbsd,timportlibbsd);
  654. RegisterExport(system_m68k_netbsd,texportlibbsd);
  655. RegisterTarget(system_m68k_netbsd_info);
  656. {$endif m68k}
  657. {$ifdef powerpc}
  658. RegisterImport(system_powerpc_netbsd,timportlibbsd);
  659. RegisterExport(system_powerpc_netbsd,texportlibbsd);
  660. RegisterTarget(system_powerpc_netbsd_info);
  661. {$endif powerpc}
  662. {$ifdef arm}
  663. RegisterImport(system_arm_netbsd,timportlibbsd);
  664. RegisterExport(system_arm_netbsd,texportlibbsd);
  665. RegisterTarget(system_arm_netbsd_info);
  666. {$endif arm}
  667. RegisterRes(res_elf_info,TWinLikeResourceFile);
  668. end.