t_bsd.pas 24 KB

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