2
0

t_bsd.pas 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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+systems_freebsd+[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 $ORDERSYMS -L. -o $EXE $RES';
  120. DllCmd[1]:=LdProgram+' $TARGET $EMUL $OPT $INIT $FINI $SONAME $MAP $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. FilesList : 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. ordersymfile: TCmdStr;
  392. linkscript: TAsmScript;
  393. DynLinkStr : string[60];
  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. if (cs_link_map in current_settings.globalswitches) then
  410. mapstr:='-Map '+maybequoted(ChangeFileExt(current_module.exefilename,'.map'));
  411. { i386_freebsd needs -b elf32-i386-freebsd and -m elf_i386_fbsd
  412. to avoid creation of a i386:x86_64 arch binary }
  413. if target_info.system=system_i386_freebsd then
  414. begin
  415. targetstr:='-b elf32-i386-freebsd';
  416. emulstr:='-m elf_i386_fbsd';
  417. end
  418. else
  419. begin
  420. targetstr:='';
  421. emulstr:='';
  422. end;
  423. if (cs_link_staticflag in current_settings.globalswitches) then
  424. begin
  425. if (target_info.system=system_m68k_netbsd) and
  426. ((cs_link_on_target in current_settings.globalswitches) or
  427. (target_info.system=source_info.system)) then
  428. StaticStr:='-Bstatic'
  429. else
  430. StaticStr:='-static';
  431. end;
  432. if (cs_link_strip in current_settings.globalswitches) then
  433. StripStr:='-s';
  434. if (cs_link_smart in current_settings.globalswitches) and
  435. (tf_smartlink_sections in target_info.flags) then
  436. GCSectionsStr:='--gc-sections';
  437. if(cs_profile in current_settings.moduleswitches) or
  438. ((Info.DynamicLinker<>'') and
  439. ((not SharedLibFiles.Empty) or
  440. (target_info.system in systems_openbsd))) then
  441. DynLinkStr:='-dynamic-linker='+Info.DynamicLinker;
  442. if CShared Then
  443. begin
  444. DynLinKStr:=DynLinkStr+' --shared'
  445. end;
  446. { Use -nopie on OpenBSD if PIC support is turned off }
  447. if (target_info.system in systems_openbsd) and
  448. not(cs_create_pic in current_settings.moduleswitches) then
  449. Info.ExtraOptions:=Info.ExtraOptions+' -nopie';
  450. { -N seems to be needed on NetBSD/earm }
  451. if (target_info.system in [system_arm_netbsd]) then
  452. Info.ExtraOptions:=Info.ExtraOptions+' -N';
  453. { Write used files and libraries }
  454. WriteResponseFile(false);
  455. { Write symbol order file }
  456. ordersymfile:=WriteSymbolOrderFile;
  457. { Call linker }
  458. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  459. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename));
  460. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  461. Replace(cmdstr,'$TARGET',targetstr);
  462. Replace(cmdstr,'$EMUL',EmulStr);
  463. Replace(cmdstr,'$MAP',mapstr);
  464. Replace(cmdstr,'$CATRES',CatFileContent(outputexedir+Info.ResName));
  465. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  466. if ordersymfile<>'' then
  467. Replace(cmdstr,'$ORDERSYMS','--symbol-ordering-file '+maybequoted(ordersymfile))
  468. else
  469. Replace(cmdstr,'$ORDERSYMS','');
  470. Replace(cmdstr,'$FILELIST','');
  471. Replace(cmdstr,'$STATIC',StaticStr);
  472. Replace(cmdstr,'$STRIP',StripStr);
  473. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  474. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  475. BinStr:=FindUtil(utilsprefix+BinStr);
  476. if (LdSupportsNoResponseFile) and
  477. not(cs_link_nolink in current_settings.globalswitches) then
  478. begin
  479. { we have to use a script to use the IFS hack }
  480. linkscript:=GenerateScript(outputexedir+'ppaslink');
  481. linkscript.AddLinkCommand(BinStr,CmdStr,'');
  482. linkscript.WriteToDisk;
  483. BinStr:=linkscript.fn;
  484. if not path_absolute(BinStr) then
  485. if cs_link_on_target in current_settings.globalswitches then
  486. BinStr:='.'+target_info.dirsep+BinStr
  487. else
  488. BinStr:='.'+source_info.dirsep+BinStr;
  489. CmdStr:='';
  490. end;
  491. useshell:=not (tf_no_backquote_support in source_info.flags);
  492. success:=DoExec(BinStr,CmdStr,true,LdSupportsNoResponseFile or useshell);
  493. { Remove ReponseFile }
  494. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  495. begin
  496. DeleteFile(outputexedir+Info.ResName);
  497. if ordersymfile<>'' then
  498. DeleteFile(ordersymfile);
  499. if LdSupportsNoResponseFile Then
  500. begin
  501. DeleteFile(linkscript.fn);
  502. linkscript.free
  503. end;
  504. end;
  505. MakeExecutable:=success; { otherwise a recursive call to link method }
  506. end;
  507. Function TLinkerBSD.MakeSharedLibrary:boolean;
  508. var
  509. InitStr,
  510. FiniStr,
  511. SoNameStr : string[80];
  512. linkscript: TAsmScript;
  513. binstr,
  514. cmdstr,
  515. mapstr,
  516. ordersymfile,
  517. targetstr,
  518. emulstr : TCmdStr;
  519. GCSectionsStr : string[63];
  520. exportedsyms: text;
  521. success : boolean;
  522. begin
  523. MakeSharedLibrary:=false;
  524. GCSectionsStr:='';
  525. mapstr:='';
  526. linkscript:=nil;
  527. if not(cs_link_nolink in current_settings.globalswitches) then
  528. Message1(exec_i_linking,current_module.sharedlibfilename);
  529. { Write used files and libraries }
  530. WriteResponseFile(true);
  531. { Write symbol order file }
  532. ordersymfile:=WriteSymbolOrderFile;
  533. if (cs_link_smart in current_settings.globalswitches) and
  534. (tf_smartlink_sections in target_info.flags) then
  535. { disabled because not tested
  536. GCSectionsStr:='--gc-sections' }
  537. ;
  538. if (cs_link_map in current_settings.globalswitches) then
  539. mapstr:='-Map '+maybequoted(ChangeFileExt(current_module.sharedlibfilename,'.map'));
  540. { i386_freebsd needs -b elf32-i386-freebsd and -m elf_i386_fbsd
  541. to avoid creation of a i386:x86_64 arch binary }
  542. if target_info.system=system_i386_freebsd then
  543. begin
  544. targetstr:='-b elf32-i386-freebsd';
  545. emulstr:='-m elf_i386_fbsd';
  546. end
  547. else
  548. begin
  549. targetstr:='';
  550. emulstr:='';
  551. end;
  552. InitStr:='-init FPC_LIB_START';
  553. FiniStr:='-fini FPC_LIB_EXIT';
  554. SoNameStr:='-soname '+ExtractFileName(current_module.sharedlibfilename);
  555. { Call linker }
  556. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  557. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename));
  558. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  559. Replace(cmdstr,'$TARGET',targetstr);
  560. Replace(cmdstr,'$EMUL',EmulStr);
  561. Replace(cmdstr,'$CATRES',CatFileContent(outputexedir+Info.ResName));
  562. Replace(cmdstr,'$FILELIST','');
  563. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  564. Replace(cmdstr,'$INIT',InitStr);
  565. Replace(cmdstr,'$FINI',FiniStr);
  566. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  567. Replace(cmdstr,'$SONAME',SoNameStr);
  568. Replace(cmdstr,'$MAP',mapstr);
  569. if ordersymfile<>'' then
  570. Replace(cmdstr,'$ORDERSYMS','--symbol-ordering-file '+maybequoted(ordersymfile))
  571. else
  572. Replace(cmdstr,'$ORDERSYMS','');
  573. BinStr:=FindUtil(utilsprefix+BinStr);
  574. if (LdSupportsNoResponseFile) and
  575. not(cs_link_nolink in current_settings.globalswitches) then
  576. begin
  577. { we have to use a script to use the IFS hack }
  578. linkscript:=GenerateScript(outputexedir+'ppaslink');
  579. linkscript.AddLinkCommand(BinStr,CmdStr,'');
  580. linkscript.WriteToDisk;
  581. BinStr:=linkscript.fn;
  582. if not path_absolute(BinStr) then
  583. if cs_link_on_target in current_settings.globalswitches then
  584. BinStr:='.'+target_info.dirsep+BinStr
  585. else
  586. BinStr:='.'+source_info.dirsep+BinStr;
  587. CmdStr:='';
  588. end;
  589. success:=DoExec(BinStr,cmdstr,true,LdSupportsNoResponseFile);
  590. { Strip the library ? }
  591. if success and (cs_link_strip in current_settings.globalswitches) then
  592. begin
  593. SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  594. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename));
  595. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,false,false);
  596. end;
  597. { Remove ReponseFile }
  598. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  599. begin
  600. DeleteFile(outputexedir+Info.ResName);
  601. if ordersymfile<>'' then
  602. DeleteFile(ordersymfile);
  603. if LdSupportsNoResponseFile Then
  604. begin
  605. DeleteFile(linkscript.fn);
  606. linkscript.free
  607. end;
  608. end;
  609. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  610. end;
  611. {*****************************************************************************
  612. Initialize
  613. *****************************************************************************}
  614. initialization
  615. RegisterLinker(ld_bsd,TLinkerBSD);
  616. {$ifdef x86_64}
  617. RegisterImport(system_x86_64_dragonfly,timportlibbsd);
  618. RegisterExport(system_x86_64_dragonfly,texportlibbsd);
  619. RegisterTarget(system_x86_64_dragonfly_info);
  620. RegisterImport(system_x86_64_freebsd,timportlibbsd);
  621. RegisterExport(system_x86_64_freebsd,texportlibbsd);
  622. RegisterTarget(system_x86_64_freebsd_info);
  623. RegisterImport(system_x86_64_openbsd,timportlibbsd);
  624. RegisterExport(system_x86_64_openbsd,texportlibbsd);
  625. RegisterTarget(system_x86_64_openbsd_info);
  626. RegisterImport(system_x86_64_netbsd,timportlibbsd);
  627. RegisterExport(system_x86_64_netbsd,texportlibbsd);
  628. RegisterTarget(system_x86_64_netbsd_info);
  629. {$endif}
  630. {$ifdef i386}
  631. RegisterImport(system_i386_freebsd,timportlibbsd);
  632. RegisterExport(system_i386_freebsd,texportlibbsd);
  633. RegisterTarget(system_i386_freebsd_info);
  634. RegisterImport(system_i386_netbsd,timportlibbsd);
  635. RegisterExport(system_i386_netbsd,texportlibbsd);
  636. RegisterTarget(system_i386_netbsd_info);
  637. RegisterImport(system_i386_openbsd,timportlibbsd);
  638. RegisterExport(system_i386_openbsd,texportlibbsd);
  639. RegisterTarget(system_i386_openbsd_info);
  640. {$endif i386}
  641. {$ifdef m68k}
  642. RegisterImport(system_m68k_netbsd,timportlibbsd);
  643. RegisterExport(system_m68k_netbsd,texportlibbsd);
  644. RegisterTarget(system_m68k_netbsd_info);
  645. {$endif m68k}
  646. {$ifdef powerpc}
  647. RegisterImport(system_powerpc_netbsd,timportlibbsd);
  648. RegisterExport(system_powerpc_netbsd,texportlibbsd);
  649. RegisterTarget(system_powerpc_netbsd_info);
  650. {$endif powerpc}
  651. {$ifdef arm}
  652. RegisterImport(system_arm_netbsd,timportlibbsd);
  653. RegisterExport(system_arm_netbsd,texportlibbsd);
  654. RegisterTarget(system_arm_netbsd_info);
  655. {$endif arm}
  656. RegisterRes(res_elf_info,TWinLikeResourceFile);
  657. end.