t_bsd.pas 35 KB

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