t_bsd.pas 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  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,script,
  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. timportlibdarwin=class(timportlib)
  33. procedure generatelib;override;
  34. end;
  35. timportlibbsd=class(timportlib)
  36. procedure generatelib;override;
  37. end;
  38. texportlibbsd=class(texportlibunix)
  39. end;
  40. texportlibdarwin=class(texportlibbsd)
  41. procedure setinitname(list: TAsmList; const s: string); override;
  42. procedure setfininame(list: TAsmList; const s: string); override;
  43. end;
  44. tlinkerbsd=class(texternallinker)
  45. private
  46. LdSupportsNoResponseFile : boolean;
  47. LibrarySuffix : Char;
  48. Function WriteResponseFile(isdll:boolean) : Boolean;
  49. function GetDarwinCrt1ObjName(isdll: boolean): TCmdStr;
  50. Function GetDarwinPrtobjName(isdll: boolean): TCmdStr;
  51. public
  52. constructor Create;override;
  53. procedure SetDefaultInfo;override;
  54. function MakeExecutable:boolean;override;
  55. function MakeSharedLibrary:boolean;override;
  56. procedure LoadPredefinedLibraryOrder; override;
  57. end;
  58. {*****************************************************************************
  59. TIMPORTLIBDARWIN
  60. *****************************************************************************}
  61. procedure timportlibdarwin.generatelib;
  62. begin
  63. end;
  64. {*****************************************************************************
  65. TEXPORTLIBDARWIN
  66. *****************************************************************************}
  67. procedure texportlibdarwin.setinitname(list: TAsmList; const s: string);
  68. begin
  69. new_section(list,sec_init_func,'',sizeof(pint));
  70. list.concat(Tai_const.Createname(s,0));
  71. end;
  72. procedure texportlibdarwin.setfininame(list: TAsmList; const s: string);
  73. begin
  74. new_section(list,sec_term_func,'',sizeof(pint));
  75. list.concat(Tai_const.Createname(s,0));
  76. end;
  77. {*****************************************************************************
  78. TIMPORTLIBBSD
  79. *****************************************************************************}
  80. procedure timportlibbsd.generatelib;
  81. var
  82. i : longint;
  83. ImportLibrary : TImportLibrary;
  84. begin
  85. for i:=0 to current_module.ImportLibraryList.Count-1 do
  86. begin
  87. ImportLibrary:=TImportLibrary(current_module.ImportLibraryList[i]);
  88. current_module.linkothersharedlibs.add(ImportLibrary.Name,link_always);
  89. end;
  90. end;
  91. {*****************************************************************************
  92. TLINKERBSD
  93. *****************************************************************************}
  94. Constructor TLinkerBSD.Create;
  95. begin
  96. Inherited Create;
  97. if not Dontlinkstdlibpath Then
  98. if not(target_info.system in systems_darwin) then
  99. LibrarySearchPath.AddPath(sysrootpath,'/lib;/usr/lib;/usr/X11R6/lib',true)
  100. else
  101. { Mac OS X doesn't have a /lib }
  102. LibrarySearchPath.AddPath(sysrootpath,'/usr/lib',true)
  103. end;
  104. procedure TLinkerBSD.SetDefaultInfo;
  105. {
  106. This will also detect which libc version will be used
  107. }
  108. begin
  109. LibrarySuffix:=' ';
  110. LdSupportsNoResponseFile := (target_info.system in ([system_m68k_netbsd]+systems_darwin));
  111. with Info do
  112. begin
  113. if LdSupportsNoResponseFile then
  114. begin
  115. if not(target_info.system in systems_darwin) then
  116. begin
  117. ExeCmd[1]:='ld $OPT $DYNLINK $STATIC $GCSECTIONS $STRIP -L. -o $EXE `cat $RES`';
  118. DllCmd[1]:='ld $OPT -shared -L. -o $EXE `cat $RES`'
  119. end
  120. else
  121. begin
  122. {$ifndef cpu64bitaddr}
  123. { Set the size of the page at address zero to 64kb, so nothing
  124. is loaded below that address. This avoids problems with the
  125. strange Windows-compatible resource handling that assumes
  126. that addresses below 64kb do not exist.
  127. On 64bit systems, page zero is 4GB by default, so no problems
  128. there.
  129. }
  130. { In case of valgrind, don't do that, because it cannot deal with
  131. a custom pagezero size -- in general, this should not cause any
  132. problems because the resources are added at the end and most
  133. programs with problems that require Valgrind will have more
  134. than 60KB of data (first 4KB of address space is always invalid)
  135. }
  136. ExeCmd[1]:='ld $PRTOBJ $OPT $DYNLINK $STATIC $GCSECTIONS $STRIP -multiply_defined suppress -L. -o $EXE `cat $RES`';
  137. if not(cs_gdb_valgrind in current_settings.globalswitches) then
  138. ExeCmd[1]:=ExeCmd[1]+' -pagezero_size 0x10000';
  139. {$else ndef cpu64bitaddr}
  140. ExeCmd[1]:='ld $PRTOBJ $OPT $DYNLINK $STATIC $GCSECTIONS $STRIP -multiply_defined suppress -L. -o $EXE `cat $RES`';
  141. {$endif ndef cpu64bitaddr}
  142. if (apptype<>app_bundle) then
  143. DllCmd[1]:='ld $PRTOBJ $OPT $GCSECTIONS -dynamic -dylib -multiply_defined suppress -L. -o $EXE `cat $RES`'
  144. else
  145. DllCmd[1]:='ld $PRTOBJ $OPT $GCSECTIONS -dynamic -bundle -multiply_defined suppress -L. -o $EXE `cat $RES`'
  146. end
  147. end
  148. else
  149. begin
  150. ExeCmd[1]:='ld $OPT $DYNLINK $STATIC $GCSECTIONS $STRIP -L. -o $EXE $RES';
  151. DllCmd[1]:='ld $OPT $INIT $FINI $SONAME -shared -L. -o $EXE $RES';
  152. end;
  153. if not(target_info.system in systems_darwin) then
  154. DllCmd[2]:='strip --strip-unneeded $EXE'
  155. else
  156. DllCmd[2]:='strip -x $EXE';
  157. { OpenBSD seems to use a wrong dynamic linker by default }
  158. if target_info.system in systems_openbsd then
  159. DynamicLinker:='/usr/libexec/ld.so'
  160. else if target_info.system in systems_netbsd then
  161. DynamicLinker:='/usr/libexec/ld.elf_so'
  162. else
  163. DynamicLinker:='';
  164. end;
  165. end;
  166. procedure TLinkerBSD.LoadPredefinedLibraryOrder;
  167. // put your linkorder/linkalias overrides here.
  168. // Note: assumes only called when reordering/aliasing is used.
  169. Begin
  170. if not(target_info.system in systems_darwin) then
  171. begin
  172. if (target_info.system =system_i386_freebsd) and
  173. not (cs_link_no_default_lib_order in current_settings.globalswitches) Then
  174. Begin
  175. LinkLibraryOrder.add('gcc','',15);
  176. LinkLibraryOrder.add('c','',50); // c and c_p mutual. excl?
  177. LinkLibraryOrder.add('c_p','',55);
  178. LinkLibraryOrder.add('pthread','',75); // pthread and c_r should be mutually exclusive
  179. LinkLibraryOrder.add('c_r','',76);
  180. LinkLibraryOrder.add('kvm','',80); // must be before ncurses
  181. if (cs_link_pthread in current_settings.globalswitches) Then // convert libpthread to libc_r.
  182. LinkLibraryAliases.add('pthread','c_r');
  183. end;
  184. end
  185. else
  186. begin
  187. LinkLibraryOrder.add('gcc','',15);
  188. LinkLibraryOrder.add('c','',50);
  189. end;
  190. End;
  191. function TLinkerBSD.GetDarwinCrt1ObjName(isdll: boolean): TCmdStr;
  192. begin
  193. if not isdll then
  194. begin
  195. if not(cs_profile in current_settings.moduleswitches) then
  196. begin
  197. { x86: crt1.10.6.o -> crt1.10.5.o -> crt1.o }
  198. { others: crt1.10.5 -> crt1.o }
  199. {$if defined(i386) or defined(x86_64)}
  200. if CompareVersionStrings(MacOSXVersionMin,'10.6')>=0 then
  201. exit('crt1.10.6.o');
  202. {$endif}
  203. if CompareVersionStrings(MacOSXVersionMin,'10.5')>=0 then
  204. exit('crt1.10.5.o');
  205. { iOS/simulator: crt1.3.1.o -> crt1.o }
  206. {$if defined(i386) or defined(arm)}
  207. if {$ifdef i386}(target_info.system=system_i386_iphonesim) and{$endif}
  208. (CompareVersionStrings(iPhoneOSVersionMin,'3.1')>=0) then
  209. exit('crt1.3.1.o');
  210. {$endif}
  211. { nothing special -> default }
  212. result:='crt1.o';
  213. end
  214. else
  215. result:='gcrt1.o';
  216. end
  217. else
  218. begin
  219. if (apptype=app_bundle) then
  220. begin
  221. { < 10.6: bundle1.o
  222. >= 10.6: nothing }
  223. if CompareVersionStrings(MacOSXVersionMin,'10.6')>=0 then
  224. exit('');
  225. { iOS/simulator: < 3.1: bundle1.o
  226. >= 3.1: nothing }
  227. {$if defined(i386) or defined(arm)}
  228. if {$ifdef i386}(target_info.system=system_i386_iphonesim) and{$endif}
  229. (CompareVersionStrings(iPhoneOSVersionMin,'3.1')>=0) then
  230. exit('');
  231. {$endif}
  232. result:='bundle1.o';
  233. end
  234. else
  235. begin
  236. { >= 10.6: nothing
  237. = 10.5: dylib1.10.5.o
  238. < 10.5: dylib1.o
  239. }
  240. if CompareVersionStrings(MacOSXVersionMin,'10.6')>=0 then
  241. exit('');
  242. if CompareVersionStrings(MacOSXVersionMin,'10.5')>=0 then
  243. exit('dylib1.10.5.o');
  244. { iOS/simulator: < 3.1: dylib1.o
  245. >= 3.1: nothing }
  246. {$if defined(i386) or defined(arm)}
  247. if {$ifdef i386}(target_info.system=system_i386_iphonesim) and{$endif}
  248. (CompareVersionStrings(iPhoneOSVersionMin,'3.1')>=0) then
  249. exit('');
  250. {$endif}
  251. result:='dylib1.o';
  252. end;
  253. end;
  254. end;
  255. Function TLinkerBSD.GetDarwinPrtobjName(isdll: boolean): TCmdStr;
  256. var
  257. startupfile: TCmdStr;
  258. begin
  259. startupfile:=GetDarwinCrt1ObjName(isdll);
  260. if startupfile<>'' then
  261. begin
  262. if not librarysearchpath.FindFile(startupfile,false,result) then
  263. result:='/usr/lib/'+startupfile
  264. end
  265. else
  266. result:='';
  267. result:=maybequoted(result);
  268. end;
  269. Function TLinkerBSD.WriteResponseFile(isdll:boolean) : Boolean;
  270. Var
  271. linkres : TLinkRes;
  272. i : longint;
  273. cprtobj,
  274. gprtobj,
  275. prtobj : string[80];
  276. HPath : TCmdStrListItem;
  277. s,s1,s2 : TCmdStr;
  278. linkdynamic,
  279. linklibc : boolean;
  280. Fl1,Fl2 : Boolean;
  281. IsDarwin : Boolean;
  282. ReOrder : Boolean;
  283. begin
  284. WriteResponseFile:=False;
  285. ReOrder:=False;
  286. IsDarwin:=target_info.system in systems_darwin;
  287. { set special options for some targets }
  288. if not IsDarwin Then
  289. begin
  290. if isdll and (target_info.system in systems_freebsd) then
  291. begin
  292. prtobj:='dllprt0';
  293. cprtobj:='dllprt0';
  294. gprtobj:='dllprt0';
  295. end
  296. else
  297. begin
  298. prtobj:='prt0';
  299. cprtobj:='cprt0';
  300. gprtobj:='gprt0';
  301. end;
  302. linkdynamic:=not(SharedLibFiles.empty);
  303. linklibc:=(SharedLibFiles.Find('c')<>nil);
  304. // this one is a bit complex.
  305. // Only reorder for now if -XL or -XO params are given
  306. // or when -Xf.
  307. reorder:= linklibc and
  308. (
  309. ReorderEntries
  310. or
  311. (cs_link_pthread in current_settings.globalswitches));
  312. if cs_profile in current_settings.moduleswitches then
  313. begin
  314. prtobj:=gprtobj;
  315. AddSharedLibrary('c');
  316. LibrarySuffix:='p';
  317. linklibc:=true;
  318. end
  319. else
  320. begin
  321. if linklibc then
  322. prtobj:=cprtobj;
  323. end;
  324. // after this point addition of shared libs not allowed.
  325. end
  326. else
  327. begin
  328. { for darwin: always link dynamically against libc }
  329. linklibc := true;
  330. {$ifdef MACOSX104ORHIGHER}
  331. { not sure what this is for, but gcc always links against it }
  332. if not(cs_profile in current_settings.moduleswitches) then
  333. AddSharedLibrary('SystemStubs')
  334. else
  335. AddSharedLibrary('SystemStubs_profile');
  336. {$endif MACOSX104ORHIGHER}
  337. reorder:=reorderentries;
  338. prtobj:='';
  339. end;
  340. if reorder Then
  341. ExpandAndApplyOrder(SharedLibFiles);
  342. { Open link.res file }
  343. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName);
  344. if (target_info.system in systems_darwin) and
  345. (sysrootpath<>'') then
  346. begin
  347. LinkRes.Add('-syslibroot');
  348. LinkRes.Add(sysrootpath);
  349. end;
  350. if (target_info.system in systems_darwin) then
  351. begin
  352. LinkRes.Add('-arch');
  353. case target_info.system of
  354. system_powerpc_darwin:
  355. LinkRes.Add('ppc');
  356. system_i386_darwin,
  357. system_i386_iphonesim:
  358. LinkRes.Add('i386');
  359. system_powerpc64_darwin:
  360. LinkRes.Add('ppc64');
  361. system_x86_64_darwin:
  362. LinkRes.Add('x86_64');
  363. system_arm_darwin:
  364. { current versions of the linker require the sub-architecture type
  365. to be specified }
  366. LinkRes.Add(lower(cputypestr[current_settings.cputype]));
  367. end;
  368. if MacOSXVersionMin<>'' then
  369. begin
  370. LinkRes.Add('-macosx_version_min');
  371. LinkRes.Add(MacOSXVersionMin);
  372. end
  373. else if iPhoneOSVersionMin<>'' then
  374. begin
  375. LinkRes.Add('-ios_version_min');
  376. LinkRes.Add(iPhoneOSVersionMin);
  377. end;
  378. end;
  379. { Write path to search libraries }
  380. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  381. while assigned(HPath) do
  382. begin
  383. if LdSupportsNoResponseFile then
  384. LinkRes.Add('-L'+HPath.Str)
  385. else
  386. LinkRes.Add('SEARCH_DIR('+maybequoted(HPath.Str)+')');
  387. HPath:=TCmdStrListItem(HPath.Next);
  388. end;
  389. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  390. while assigned(HPath) do
  391. begin
  392. if LdSupportsNoResponseFile then
  393. LinkRes.Add('-L'+HPath.Str)
  394. else
  395. LinkRes.Add('SEARCH_DIR('+maybequoted(HPath.Str)+')');
  396. HPath:=TCmdStrListItem(HPath.Next);
  397. end;
  398. if (target_info.system in systems_darwin) then
  399. begin
  400. HPath:=TCmdStrListItem(current_module.localframeworksearchpath.First);
  401. while assigned(HPath) do
  402. begin
  403. LinkRes.Add('-F'+HPath.Str);
  404. HPath:=TCmdStrListItem(HPath.Next);
  405. end;
  406. HPath:=TCmdStrListItem(FrameworkSearchPath.First);
  407. while assigned(HPath) do
  408. begin
  409. LinkRes.Add('-F'+HPath.Str);
  410. HPath:=TCmdStrListItem(HPath.Next);
  411. end;
  412. end;
  413. { force local symbol resolution (i.e., inside the shared }
  414. { library itself) for all non-exorted symbols, otherwise }
  415. { several RTL symbols of FPC-compiled shared libraries }
  416. { will be bound to those of a single shared library or }
  417. { to the main program }
  418. if (isdll) and (target_info.system in systems_freebsd) then
  419. begin
  420. LinkRes.add('VERSION');
  421. LinkRes.add('{');
  422. LinkRes.add(' {');
  423. if not texportlibunix(exportlib).exportedsymnames.empty then
  424. begin
  425. LinkRes.add(' global:');
  426. repeat
  427. LinkRes.add(' '+texportlibunix(exportlib).exportedsymnames.getfirst+';');
  428. until texportlibunix(exportlib).exportedsymnames.empty;
  429. end;
  430. LinkRes.add(' local:');
  431. LinkRes.add(' *;');
  432. LinkRes.add(' };');
  433. LinkRes.add('}');
  434. end;
  435. if not LdSupportsNoResponseFile then
  436. LinkRes.Add('INPUT(');
  437. { add objectfiles, start with prt0 always }
  438. if prtobj<>'' then
  439. LinkRes.AddFileName(FindObjectFile(prtobj,'',false));
  440. { try to add crti and crtbegin if linking to C }
  441. if linklibc and
  442. not IsDarwin Then
  443. begin
  444. if librarysearchpath.FindFile('crtbegin.o',false,s) then
  445. LinkRes.AddFileName(s);
  446. if librarysearchpath.FindFile('crti.o',false,s) then
  447. LinkRes.AddFileName(s);
  448. end;
  449. { main objectfiles }
  450. while not ObjectFiles.Empty do
  451. begin
  452. s:=ObjectFiles.GetFirst;
  453. if s<>'' then
  454. if LdSupportsNoResponseFile then
  455. LinkRes.AddFileName(s)
  456. else
  457. LinkRes.AddFileName(maybequoted(s));
  458. end;
  459. if not LdSupportsNoResponseFile then
  460. LinkRes.Add(')');
  461. { Write staticlibraries }
  462. if not StaticLibFiles.Empty then
  463. begin
  464. if not LdSupportsNoResponseFile then
  465. LinkRes.Add('GROUP(');
  466. While not StaticLibFiles.Empty do
  467. begin
  468. S:=StaticLibFiles.GetFirst;
  469. if LdSupportsNoResponseFile then
  470. LinkRes.AddFileName(s)
  471. else
  472. LinkRes.AddFileName(maybequoted(s))
  473. end;
  474. if not LdSupportsNoResponseFile then
  475. LinkRes.Add(')');
  476. end;
  477. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  478. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  479. if not SharedLibFiles.Empty then
  480. begin
  481. if not LdSupportsNoResponseFile then
  482. LinkRes.Add('INPUT(');
  483. While not SharedLibFiles.Empty do
  484. begin
  485. S:=SharedLibFiles.GetFirst;
  486. if (s<>'c') or reorder then
  487. begin
  488. i:=Pos(target_info.sharedlibext,S);
  489. if i>0 then
  490. Delete(S,i,255);
  491. LinkRes.Add('-l'+s);
  492. end
  493. else
  494. begin
  495. linklibc:=true;
  496. linkdynamic:=false; { libc will include the ld-* for us }
  497. end;
  498. end;
  499. { be sure that libc is the last lib }
  500. if linklibc and not reorder then
  501. Begin
  502. If LibrarySuffix=' ' Then
  503. LinkRes.Add('-lc')
  504. else
  505. LinkRes.Add('-lc_'+LibrarySuffix);
  506. If LibrarySuffix='r' Then
  507. LinkRes.Add('-lc');
  508. end;
  509. { when we have -static for the linker the we also need libgcc }
  510. if (cs_link_staticflag in current_settings.globalswitches) then
  511. LinkRes.Add('-lgcc');
  512. if linkdynamic and (Info.DynamicLinker<>'') then
  513. LinkRes.AddFileName(Info.DynamicLinker);
  514. if not LdSupportsNoResponseFile then
  515. LinkRes.Add(')');
  516. end;
  517. { frameworks for Darwin }
  518. if IsDarwin then
  519. while not FrameworkFiles.empty do
  520. begin
  521. LinkRes.Add('-framework');
  522. LinkRes.Add(FrameworkFiles.GetFirst);
  523. end;
  524. { objects which must be at the end }
  525. if linklibc and
  526. not IsDarwin Then
  527. begin
  528. Fl1:=librarysearchpath.FindFile('crtend.o',false,s1);
  529. Fl2:=librarysearchpath.FindFile('crtn.o',false,s2);
  530. if Fl1 or Fl2 then
  531. begin
  532. LinkRes.Add('INPUT(');
  533. If Fl1 Then
  534. LinkRes.AddFileName(s1);
  535. If Fl2 Then
  536. LinkRes.AddFileName(s2);
  537. LinkRes.Add(')');
  538. end;
  539. end;
  540. { Write and Close response }
  541. linkres.writetodisk;
  542. linkres.Free;
  543. WriteResponseFile:=True;
  544. end;
  545. function TLinkerBSD.MakeExecutable:boolean;
  546. var
  547. binstr,
  548. cmdstr,
  549. extdbgbinstr,
  550. extdbgcmdstr: TCmdStr;
  551. linkscript: TAsmScript;
  552. DynLinkStr : string[60];
  553. GCSectionsStr,
  554. StaticStr,
  555. StripStr : string[63];
  556. success : boolean;
  557. begin
  558. if not(cs_link_nolink in current_settings.globalswitches) then
  559. Message1(exec_i_linking,current_module.exefilename^);
  560. { Create some replacements }
  561. StaticStr:='';
  562. StripStr:='';
  563. DynLinkStr:='';
  564. GCSectionsStr:='';
  565. if (cs_link_staticflag in current_settings.globalswitches) then
  566. begin
  567. if (target_info.system=system_m68k_netbsd) and
  568. ((cs_link_on_target in current_settings.globalswitches) or
  569. (target_info.system=source_info.system)) then
  570. StaticStr:='-Bstatic'
  571. else
  572. StaticStr:='-static';
  573. end;
  574. if (cs_link_strip in current_settings.globalswitches) then
  575. if (target_info.system in systems_darwin) then
  576. StripStr:='-x'
  577. else
  578. StripStr:='-s';
  579. if (cs_link_smart in current_settings.globalswitches) and
  580. (tf_smartlink_sections in target_info.flags) then
  581. if not(target_info.system in systems_darwin) then
  582. GCSectionsStr:='--gc-sections'
  583. else
  584. GCSectionsStr:='-dead_strip -no_dead_strip_inits_and_terms';
  585. if(not(target_info.system in systems_darwin) and
  586. (cs_profile in current_settings.moduleswitches)) or
  587. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty)) then
  588. DynLinkStr:='-dynamic-linker='+Info.DynamicLinker;
  589. if CShared Then
  590. begin
  591. if not(target_info.system in systems_darwin) then
  592. DynLinKStr:=DynLinkStr+' --shared'
  593. else
  594. DynLinKStr:=DynLinkStr+' -dynamic'; // one dash!
  595. end;
  596. { Write used files and libraries }
  597. WriteResponseFile(false);
  598. { Call linker }
  599. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  600. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename^));
  601. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  602. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  603. Replace(cmdstr,'$STATIC',StaticStr);
  604. Replace(cmdstr,'$STRIP',StripStr);
  605. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  606. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  607. if (target_info.system in systems_darwin) then
  608. Replace(cmdstr,'$PRTOBJ',GetDarwinPrtobjName(false));
  609. BinStr:=FindUtil(utilsprefix+BinStr);
  610. { create dsym file? }
  611. extdbgbinstr:='';
  612. extdbgcmdstr:='';
  613. if (target_info.system in systems_darwin) and
  614. (target_dbg.id in [dbg_dwarf2,dbg_dwarf3]) and
  615. (cs_link_separate_dbg_file in current_settings.globalswitches) then
  616. begin
  617. extdbgbinstr:=FindUtil(utilsprefix+'dsymutil');
  618. extdbgcmdstr:=maybequoted(current_module.exefilename^);
  619. end;
  620. if (LdSupportsNoResponseFile) and
  621. not(cs_link_nolink in current_settings.globalswitches) then
  622. begin
  623. { we have to use a script to use the IFS hack }
  624. linkscript:=TAsmScriptUnix.create(outputexedir+'ppaslink');
  625. linkscript.AddLinkCommand(BinStr,CmdStr,'');
  626. if (extdbgcmdstr<>'') then
  627. linkscript.AddLinkCommand(extdbgbinstr,extdbgcmdstr,'');
  628. linkscript.WriteToDisk;
  629. BinStr:=linkscript.fn;
  630. if not path_absolute(BinStr) then
  631. BinStr:='./'+BinStr;
  632. CmdStr:='';
  633. end;
  634. success:=DoExec(BinStr,CmdStr,true,LdSupportsNoResponseFile);
  635. if (success and
  636. (extdbgbinstr<>'') and
  637. (cs_link_nolink in current_settings.globalswitches)) then
  638. success:=DoExec(extdbgbinstr,extdbgcmdstr,false,LdSupportsNoResponseFile);
  639. { Remove ReponseFile }
  640. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  641. begin
  642. DeleteFile(outputexedir+Info.ResName);
  643. if LdSupportsNoResponseFile Then
  644. begin
  645. DeleteFile(linkscript.fn);
  646. linkscript.free
  647. end;
  648. end;
  649. MakeExecutable:=success; { otherwise a recursive call to link method }
  650. end;
  651. Function TLinkerBSD.MakeSharedLibrary:boolean;
  652. var
  653. InitStr,
  654. FiniStr,
  655. SoNameStr : string[80];
  656. linkscript: TAsmScript;
  657. binstr,
  658. cmdstr,
  659. extdbgbinstr,
  660. extdbgcmdstr : TCmdStr;
  661. GCSectionsStr : string[63];
  662. exportedsyms: text;
  663. success : boolean;
  664. begin
  665. MakeSharedLibrary:=false;
  666. GCSectionsStr:='';
  667. if not(cs_link_nolink in current_settings.globalswitches) then
  668. Message1(exec_i_linking,current_module.sharedlibfilename^);
  669. { Write used files and libraries }
  670. WriteResponseFile(true);
  671. if (cs_link_smart in current_settings.globalswitches) and
  672. (tf_smartlink_sections in target_info.flags) then
  673. if not(target_info.system in systems_darwin) then
  674. { disabled because not tested
  675. GCSectionsStr:='--gc-sections' }
  676. else
  677. GCSectionsStr:='-dead_strip -no_dead_strip_inits_and_terms';
  678. InitStr:='-init FPC_LIB_START';
  679. FiniStr:='-fini FPC_LIB_EXIT';
  680. SoNameStr:='-soname '+ExtractFileName(current_module.sharedlibfilename^);
  681. { Call linker }
  682. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  683. {$ifndef darwin}
  684. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename^));
  685. {$else darwin}
  686. Replace(cmdstr,'$EXE',maybequoted(ExpandFileName(current_module.sharedlibfilename^)));
  687. {$endif darwin}
  688. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  689. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  690. Replace(cmdstr,'$INIT',InitStr);
  691. Replace(cmdstr,'$FINI',FiniStr);
  692. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  693. Replace(cmdstr,'$SONAME',SoNameStr);
  694. if (target_info.system in systems_darwin) then
  695. Replace(cmdstr,'$PRTOBJ',GetDarwinPrtobjName(true));
  696. BinStr:=FindUtil(utilsprefix+BinStr);
  697. { create dsym file? }
  698. extdbgbinstr:='';
  699. extdbgcmdstr:='';
  700. if (target_info.system in systems_darwin) and
  701. (target_dbg.id in [dbg_dwarf2,dbg_dwarf3]) and
  702. (cs_link_separate_dbg_file in current_settings.globalswitches) then
  703. begin
  704. extdbgbinstr:=FindUtil(utilsprefix+'dsymutil');
  705. extdbgcmdstr:=maybequoted(current_module.sharedlibfilename^);
  706. end;
  707. if (target_info.system in systems_darwin) then
  708. begin
  709. { exported symbols for darwin }
  710. if not texportlibunix(exportlib).exportedsymnames.empty then
  711. begin
  712. assign(exportedsyms,outputexedir+'linksyms.fpc');
  713. rewrite(exportedsyms);
  714. repeat
  715. writeln(exportedsyms,texportlibunix(exportlib).exportedsymnames.getfirst);
  716. until texportlibunix(exportlib).exportedsymnames.empty;
  717. close(exportedsyms);
  718. cmdstr:=cmdstr+' -exported_symbols_list '+maybequoted(outputexedir)+'linksyms.fpc';
  719. end;
  720. end;
  721. if (LdSupportsNoResponseFile) and
  722. not(cs_link_nolink in current_settings.globalswitches) then
  723. begin
  724. { we have to use a script to use the IFS hack }
  725. linkscript:=TAsmScriptUnix.create(outputexedir+'ppaslink');
  726. linkscript.AddLinkCommand(BinStr,CmdStr,'');
  727. if (extdbgbinstr<>'') then
  728. linkscript.AddLinkCommand(extdbgbinstr,extdbgcmdstr,'');
  729. linkscript.WriteToDisk;
  730. BinStr:=linkscript.fn;
  731. if not path_absolute(BinStr) then
  732. BinStr:='./'+BinStr;
  733. CmdStr:='';
  734. end;
  735. success:=DoExec(BinStr,cmdstr,true,LdSupportsNoResponseFile);
  736. if (success and
  737. (extdbgbinstr<>'') and
  738. (cs_link_nolink in current_settings.globalswitches)) then
  739. success:=DoExec(extdbgbinstr,extdbgcmdstr,false,LdSupportsNoResponseFile);
  740. { Strip the library ? }
  741. if success and (cs_link_strip in current_settings.globalswitches) then
  742. begin
  743. SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  744. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename^));
  745. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,false,false);
  746. end;
  747. { Remove ReponseFile }
  748. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  749. begin
  750. DeleteFile(outputexedir+Info.ResName);
  751. if LdSupportsNoResponseFile Then
  752. begin
  753. DeleteFile(linkscript.fn);
  754. linkscript.free
  755. end;
  756. if (target_info.system in systems_darwin) then
  757. DeleteFile(outputexedir+'linksyms.fpc');
  758. end;
  759. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  760. end;
  761. {*****************************************************************************
  762. Initialize
  763. *****************************************************************************}
  764. initialization
  765. {$ifdef x86_64}
  766. RegisterExternalLinker(system_x86_64_FreeBSD_info,TLinkerBSD);
  767. RegisterImport(system_x86_64_freebsd,timportlibbsd);
  768. RegisterExport(system_x86_64_freebsd,texportlibbsd);
  769. RegisterTarget(system_x86_64_freebsd_info);
  770. RegisterExternalLinker(system_x86_64_OpenBSD_info,TLinkerBSD);
  771. RegisterImport(system_x86_64_openbsd,timportlibbsd);
  772. RegisterExport(system_x86_64_openbsd,texportlibbsd);
  773. RegisterTarget(system_x86_64_openbsd_info);
  774. RegisterExternalLinker(system_x86_64_NetBSD_info,TLinkerBSD);
  775. RegisterImport(system_x86_64_netbsd,timportlibbsd);
  776. RegisterExport(system_x86_64_netbsd,texportlibbsd);
  777. RegisterTarget(system_x86_64_netbsd_info);
  778. RegisterExternalLinker(system_x86_64_darwin_info,TLinkerBSD);
  779. RegisterImport(system_x86_64_darwin,timportlibdarwin);
  780. RegisterExport(system_x86_64_darwin,texportlibdarwin);
  781. RegisterTarget(system_x86_64_darwin_info);
  782. {$endif}
  783. {$ifdef i386}
  784. RegisterExternalLinker(system_i386_FreeBSD_info,TLinkerBSD);
  785. RegisterExternalLinker(system_i386_NetBSD_info,TLinkerBSD);
  786. RegisterExternalLinker(system_i386_OpenBSD_info,TLinkerBSD);
  787. RegisterImport(system_i386_freebsd,timportlibbsd);
  788. RegisterExport(system_i386_freebsd,texportlibbsd);
  789. RegisterTarget(system_i386_freebsd_info);
  790. RegisterImport(system_i386_netbsd,timportlibbsd);
  791. RegisterExport(system_i386_netbsd,texportlibbsd);
  792. RegisterTarget(system_i386_netbsd_info);
  793. RegisterImport(system_i386_openbsd,timportlibbsd);
  794. RegisterExport(system_i386_openbsd,texportlibbsd);
  795. RegisterTarget(system_i386_openbsd_info);
  796. RegisterExternalLinker(system_i386_darwin_info,TLinkerBSD);
  797. RegisterImport(system_i386_darwin,timportlibdarwin);
  798. RegisterExport(system_i386_darwin,texportlibdarwin);
  799. RegisterTarget(system_i386_darwin_info);
  800. RegisterExternalLinker(system_i386_iphonesim_info,TLinkerBSD);
  801. RegisterImport(system_i386_iphonesim,timportlibdarwin);
  802. RegisterExport(system_i386_iphonesim,texportlibdarwin);
  803. RegisterTarget(system_i386_iphonesim_info);
  804. {$endif i386}
  805. {$ifdef m68k}
  806. // RegisterExternalLinker(system_m68k_FreeBSD_info,TLinkerBSD);
  807. RegisterExternalLinker(system_m68k_NetBSD_info,TLinkerBSD);
  808. RegisterImport(system_m68k_netbsd,timportlibbsd);
  809. RegisterExport(system_m68k_netbsd,texportlibbsd);
  810. RegisterTarget(system_m68k_netbsd_info);
  811. {$endif m68k}
  812. {$ifdef powerpc}
  813. // RegisterExternalLinker(system_m68k_FreeBSD_info,TLinkerBSD);
  814. RegisterExternalLinker(system_powerpc_darwin_info,TLinkerBSD);
  815. RegisterImport(system_powerpc_darwin,timportlibdarwin);
  816. RegisterExport(system_powerpc_darwin,texportlibdarwin);
  817. RegisterTarget(system_powerpc_darwin_info);
  818. RegisterExternalLinker(system_powerpc_netbsd_info,TLinkerBSD);
  819. RegisterImport(system_powerpc_netbsd,timportlibbsd);
  820. RegisterExport(system_powerpc_netbsd,texportlibbsd);
  821. RegisterTarget(system_powerpc_netbsd_info);
  822. {$endif powerpc}
  823. {$ifdef powerpc64}
  824. RegisterExternalLinker(system_powerpc64_darwin_info,TLinkerBSD);
  825. RegisterImport(system_powerpc64_darwin,timportlibdarwin);
  826. RegisterExport(system_powerpc64_darwin,texportlibdarwin);
  827. RegisterTarget(system_powerpc64_darwin_info);
  828. {$endif powerpc64}
  829. {$ifdef arm}
  830. RegisterExternalLinker(system_arm_darwin_info,TLinkerBSD);
  831. RegisterImport(system_arm_darwin,timportlibdarwin);
  832. RegisterExport(system_arm_darwin,texportlibdarwin);
  833. RegisterTarget(system_arm_darwin_info);
  834. {$endif arm}
  835. RegisterRes(res_elf_info,TWinLikeResourceFile);
  836. RegisterRes(res_macho_info,TWinLikeResourceFile);
  837. end.