t_bsd.pas 23 KB

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