t_bsd.pas 24 KB

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