t_bsd.pas 32 KB

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