t_bsd.pas 24 KB

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