t_bsd.pas 32 KB

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