t_bsd.pas 35 KB

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