t_darwin.pas 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  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_darwin;
  20. {$i fpcdefs.inc}
  21. interface
  22. implementation
  23. uses
  24. sysutils,
  25. cutils,cfileutl,cclasses,
  26. verbose,systems,globtype,globals,
  27. symconst,cscript,triplet,
  28. fmodule,aasmbase,aasmtai,aasmdata,aasmcpu,cpubase,symsym,symdef,
  29. import,export,link,comprsrc,rescmn,i_darwin,expunix,
  30. cgutils,cgbase,cgobj,cpuinfo,ogbase;
  31. type
  32. timportlibdarwin=class(timportlib)
  33. procedure generatelib;override;
  34. end;
  35. texportlibdarwin=class(texportlibunix)
  36. procedure setinitname(list: TAsmList; const s: string); override;
  37. procedure setfininame(list: TAsmList; const s: string); override;
  38. end;
  39. tlinkerdarwin=class(texternallinker)
  40. private
  41. LinkSymsFileName : TCmdStr;
  42. function WriteFileList: TCmdStr;
  43. function GetDarwinCrt1ObjName(isdll: boolean): TCmdStr;
  44. Function GetDarwinPrtobjName(isdll: boolean): TCmdStr;
  45. function GetLinkArch: TCmdStr;
  46. function GetLinkVersion: TCmdStr;
  47. function GetSysroot: TCmdStr;
  48. function GetLibSearchPath: TCmdStr;
  49. function GetLibraries: TCmdStr;
  50. public
  51. constructor Create;override;
  52. procedure SetDefaultInfo;override;
  53. function MakeExecutable:boolean;override;
  54. function MakeSharedLibrary:boolean;override;
  55. procedure LoadPredefinedLibraryOrder; override;
  56. procedure InitSysInitUnitName; override;
  57. function postprocessexecutable(const fn : string;isdll:boolean):boolean;
  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. TLINKERBSD
  80. *****************************************************************************}
  81. constructor tlinkerdarwin.Create;
  82. begin
  83. inherited;
  84. if not Dontlinkstdlibpath Then
  85. LibrarySearchPath.AddLibraryPath(sysrootpath,'=/usr/lib',true)
  86. end;
  87. procedure tlinkerdarwin.SetDefaultInfo;
  88. begin
  89. with Info do
  90. begin
  91. {$ifndef cpu64bitaddr}
  92. { Set the size of the page at address zero to 64kb, so nothing
  93. is loaded below that address. This avoids problems with the
  94. strange Windows-compatible resource handling that assumes
  95. that addresses below 64kb do not exist.
  96. On 64bit systems, page zero is 4GB by default, so no problems
  97. there.
  98. }
  99. { In case of valgrind, don't do that, because it cannot deal with
  100. a custom pagezero size -- in general, this should not cause any
  101. problems because the resources are added at the end and most
  102. programs with problems that require Valgrind will have more
  103. than 60KB of data (first 4KB of address space is always invalid)
  104. }
  105. ExeCmd[1]:='ld $PRTOBJ $TARGET $OPT $STATIC $GCSECTIONS $STRIP $MAP $LTO $ORDERSYMS $RPATH -L. -o $EXE $ARCH $VERSION $SYSROOT $LIBSEARCHPATH $FILELIST $LIBRARIES';
  106. if not(cs_gdb_valgrind in current_settings.globalswitches) then
  107. ExeCmd[1]:=ExeCmd[1]+' -pagezero_size 0x10000';
  108. {$else ndef cpu64bitaddr}
  109. ExeCmd[1]:='ld $PRTOBJ $TARGET $OPT $STATIC $GCSECTIONS $STRIP $MAP $LTO $ORDERSYMS $RPATH -L. -o $EXE $ARCH $VERSION $SYSROOT $LIBSEARCHPATH $FILELIST $LIBRARIES';
  110. {$endif ndef cpu64bitaddr}
  111. if (apptype<>app_bundle) then
  112. DllCmd[1]:='ld $PRTOBJ $TARGET $OPT $GCSECTIONS $MAP $LTO $ORDERSYMS $RPATH -dynamic -dylib -L. -o $EXE $ARCH $VERSION $SYSROOT $LIBSEARCHPATH $FILELIST $LIBRARIES'
  113. else
  114. DllCmd[1]:='ld $PRTOBJ $TARGET $OPT $GCSECTIONS $MAP $LTO $ORDERSYMS $RPATH -dynamic -bundle -L. -o $EXE $ARCH $VERSION $SYRSROOT $LIBSEARCHPATH $FILELIST $LIBRARIES';
  115. DllCmd[2]:='strip -x $EXE';
  116. DynamicLinker:='';
  117. end;
  118. end;
  119. procedure tlinkerdarwin.LoadPredefinedLibraryOrder;
  120. begin
  121. // put your linkorder/linkalias overrides here.
  122. // Note: assumes only called when reordering/aliasing is used.
  123. LinkLibraryOrder.add('gcc','',15);
  124. LinkLibraryOrder.add('c','',50);
  125. end;
  126. procedure tlinkerdarwin.InitSysInitUnitName;
  127. begin
  128. SysInitUnit:='sysinit';
  129. end;
  130. function tlinkerdarwin.GetDarwinCrt1ObjName(isdll: boolean): TCmdStr;
  131. begin
  132. if not isdll then
  133. begin
  134. if not(cs_profile in current_settings.moduleswitches) then
  135. begin
  136. case target_info.system of
  137. system_powerpc_darwin,
  138. system_powerpc64_darwin,
  139. system_i386_darwin,
  140. system_x86_64_darwin:
  141. begin
  142. { 10.8 and later: no crt1.* }
  143. if MacOSXVersionMin.relationto(10,8,0)>=0 then
  144. exit('');
  145. { x86: crt1.10.6.o -> crt1.10.5.o -> crt1.o }
  146. { others: crt1.10.5 -> crt1.o }
  147. if (target_info.system in [system_i386_darwin,system_x86_64_darwin]) and
  148. (MacOSXVersionMin.relationto(10,6,0)>=0) then
  149. exit('crt1.10.6.o');
  150. if MacOSXVersionMin.relationto(10,5,0)>=0 then
  151. exit('crt1.10.5.o');
  152. end;
  153. system_arm_ios:
  154. begin
  155. { iOS:
  156. iOS 6 and later: nothing
  157. iOS 3.1 - 5.x: crt1.3.1.o
  158. pre-iOS 3.1: crt1.o
  159. }
  160. if iPhoneOSVersionMin.relationto(6,0,0)>=0 then
  161. exit('');
  162. if iPhoneOSVersionMin.relationto(3,1,0)>=0 then
  163. exit('crt1.3.1.o');
  164. end;
  165. system_i386_iphonesim,
  166. system_x86_64_iphonesim,
  167. system_aarch64_iphonesim:
  168. begin
  169. { "recent versions" must not use anything (https://github.com/llvm-mirror/clang/commit/e6d04f3d152a22077022cf9287d4c538a0918ab0 )
  170. What those recent versions could be, is anyone's guess. It
  171. still seems to work with 8.1 and no longer with 8.3, so use
  172. 8.1 as a cut-off point }
  173. if iPhoneOSVersionMin.relationto(8,1,0)>0 then
  174. exit('');
  175. end;
  176. system_aarch64_ios,
  177. system_aarch64_darwin:
  178. { never anything }
  179. exit('');
  180. else
  181. Internalerror(2019050709);
  182. end;
  183. { nothing special -> default }
  184. result:='crt1.o';
  185. end
  186. else
  187. begin
  188. result:='gcrt1.o';
  189. { 10.8 and later: tell the linker to use 'start' instead of "_main"
  190. as entry point }
  191. if MacOSXVersionMin.relationto(10,8,0)>=0 then
  192. Info.ExeCmd[1]:=Info.ExeCmd[1]+' -no_new_main';
  193. end;
  194. end
  195. else
  196. begin
  197. if (apptype=app_bundle) then
  198. begin
  199. case target_info.system of
  200. system_powerpc_darwin,
  201. system_powerpc64_darwin,
  202. system_i386_darwin,
  203. system_x86_64_darwin:
  204. begin
  205. { < 10.6: bundle1.o
  206. >= 10.6: nothing }
  207. if MacOSXVersionMin.relationto(10,6,0)>=0 then
  208. exit('');
  209. end;
  210. system_arm_ios,
  211. system_aarch64_ios:
  212. begin
  213. { iOS: < 3.1: bundle1.o
  214. >= 3.1: nothing }
  215. if iPhoneOSVersionMin.relationto(3,1,0)>=0 then
  216. exit('');
  217. end;
  218. system_i386_iphonesim,
  219. system_x86_64_iphonesim,
  220. system_aarch64_iphonesim:
  221. begin
  222. { see rule for crt1.o }
  223. if iPhoneOSVersionMin.relationto(8,1,0)>0 then
  224. exit('');
  225. end;
  226. system_aarch64_darwin:
  227. exit('');
  228. else
  229. Internalerror(2019050710);
  230. end;
  231. result:='bundle1.o';
  232. end
  233. else
  234. begin
  235. case target_info.system of
  236. system_powerpc_darwin,
  237. system_powerpc64_darwin,
  238. system_i386_darwin,
  239. system_x86_64_darwin:
  240. begin
  241. { >= 10.6: nothing
  242. = 10.5: dylib1.10.5.o
  243. < 10.5: dylib1.o
  244. }
  245. if MacOSXVersionMin.relationto(10,6,0)>=0 then
  246. exit('');
  247. if MacOSXVersionMin.relationto(10,5,0)>=0 then
  248. exit('dylib1.10.5.o');
  249. end;
  250. system_arm_ios,
  251. system_aarch64_ios:
  252. begin
  253. { iOS: < 3.1: dylib1.o
  254. >= 3.1: nothing }
  255. if iPhoneOSVersionMin.relationto(3,1,0)>=0 then
  256. exit('');
  257. end;
  258. system_i386_iphonesim,
  259. system_x86_64_iphonesim,
  260. system_aarch64_iphonesim:
  261. begin
  262. { see rule for crt1.o }
  263. if iPhoneOSVersionMin.relationto(8,1,0)>0 then
  264. exit('');
  265. end;
  266. system_aarch64_darwin:
  267. exit('');
  268. else
  269. Internalerror(2019050711);
  270. end;
  271. result:='dylib1.o';
  272. end;
  273. end;
  274. end;
  275. function tlinkerdarwin.GetDarwinPrtobjName(isdll: boolean): TCmdStr;
  276. var
  277. startupfile: TCmdStr;
  278. begin
  279. result:='';
  280. startupfile:=GetDarwinCrt1ObjName(isdll);
  281. if startupfile<>'' then
  282. begin
  283. if not librarysearchpath.FindFile(startupfile,false,result) then
  284. result:='/usr/lib/'+startupfile;
  285. end;
  286. result:=maybequoted(result);
  287. end;
  288. function tlinkerdarwin.GetLinkArch: TCmdStr;
  289. begin
  290. case target_info.system of
  291. system_powerpc_darwin:
  292. result:='-arch ppc';
  293. system_i386_darwin,
  294. system_i386_iphonesim:
  295. result:='-arch i386';
  296. system_powerpc64_darwin:
  297. result:='-arch ppc64';
  298. system_x86_64_darwin,
  299. system_x86_64_iphonesim:
  300. result:='-arch x86_64';
  301. system_arm_ios:
  302. { current versions of the linker require the sub-architecture type
  303. to be specified }
  304. result:='-arch '+lower(cputypestr[current_settings.cputype]);
  305. system_aarch64_ios,
  306. system_aarch64_iphonesim,
  307. system_aarch64_darwin:
  308. result:='-arch arm64';
  309. else
  310. internalerror(2014121801);
  311. end;
  312. end;
  313. function tlinkerdarwin.GetLinkVersion: TCmdStr;
  314. begin
  315. if MacOSXVersionMin.isvalid then
  316. begin
  317. result:='-macosx_version_min '+MacOSXVersionMin.str;
  318. end
  319. else if iPhoneOSVersionMin.isvalid then
  320. begin
  321. if target_info.system in [system_i386_iphonesim,system_x86_64_iphonesim,system_aarch64_iphonesim] then
  322. result:='-ios_simulator_version_min '+iPhoneOSVersionMin.str
  323. else
  324. result:='-iphoneos_version_min '+iPhoneOSVersionMin.str;
  325. end
  326. else
  327. begin
  328. internalerror(2022090920)
  329. end;
  330. end;
  331. function tlinkerdarwin.GetSysroot: TCmdStr;
  332. begin
  333. if sysrootpath<>'' then
  334. begin
  335. result:='-syslibroot '+maybequoted(sysrootpath);
  336. end
  337. else
  338. begin
  339. result:='';
  340. end;
  341. end;
  342. function tlinkerdarwin.GetLibSearchPath: TCmdStr;
  343. var
  344. HPath: TCmdStrListItem;
  345. begin
  346. result:='';
  347. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  348. while assigned(HPath) do
  349. begin
  350. result:=result+' '+maybequoted('-L'+HPath.Str);
  351. HPath:=TCmdStrListItem(HPath.Next);
  352. end;
  353. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  354. while assigned(HPath) do
  355. begin
  356. result:=result+' '+maybequoted('-L'+HPath.Str);
  357. HPath:=TCmdStrListItem(HPath.Next);
  358. end;
  359. HPath:=TCmdStrListItem(current_module.localframeworksearchpath.First);
  360. while assigned(HPath) do
  361. begin
  362. result:=result+' '+maybequoted('-F'+HPath.Str);
  363. HPath:=TCmdStrListItem(HPath.Next);
  364. end;
  365. HPath:=TCmdStrListItem(FrameworkSearchPath.First);
  366. while assigned(HPath) do
  367. begin
  368. result:=result+' '+maybequoted('-F'+HPath.Str);
  369. HPath:=TCmdStrListItem(HPath.Next);
  370. end;
  371. end;
  372. function tlinkerdarwin.GetLibraries: TCmdStr;
  373. var
  374. s: TCmdStr;
  375. i: longint;
  376. begin
  377. result:='';
  378. while not SharedLibFiles.Empty do
  379. begin
  380. s:=SharedLibFiles.GetFirst;
  381. if (s<>'c') or ReOrderEntries then
  382. begin
  383. i:=Pos(target_info.sharedlibext,s);
  384. if i>0 then
  385. Delete(s,i,length(s));
  386. result:=result+' '+maybequoted('-l'+s);
  387. end;
  388. { be sure that libc is the last lib }
  389. if not ReOrderEntries then
  390. begin
  391. result:=result+' -lc'
  392. end;
  393. end;
  394. while not FrameworkFiles.empty do
  395. begin
  396. result:=result+' -framework '+maybequoted(FrameworkFiles.GetFirst);
  397. end;
  398. end;
  399. function tlinkerdarwin.WriteFileList: TCmdStr;
  400. Var
  401. FilesList : TScript;
  402. s : TCmdStr;
  403. begin
  404. if ReOrderEntries Then
  405. ExpandAndApplyOrder(SharedLibFiles);
  406. { main objectfiles and static libraries: in filelist file to avoid overflowing command line limit }
  407. result:=UniqueName(outputexedir+'linkfiles')+'.res';
  408. FilesList:=TScript.Create(result);
  409. while not ObjectFiles.Empty do
  410. begin
  411. s:=ObjectFiles.GetFirst;
  412. if s<>'' then
  413. begin
  414. s:=TargetFixFileName(s);
  415. FilesList.Add(s);
  416. end;
  417. { Write staticlibraries }
  418. while not StaticLibFiles.Empty do
  419. begin
  420. s:=StaticLibFiles.GetFirst;
  421. FilesList.Add(s)
  422. end;
  423. end;
  424. FilesList.writetodisk;
  425. FilesList.Free;
  426. end;
  427. function tlinkerdarwin.MakeExecutable:boolean;
  428. var
  429. binstr,
  430. cmdstr,
  431. mapstr,
  432. targetstr,
  433. extdbgbinstr,
  434. extdbgcmdstr,
  435. ltostr,
  436. asanstr,
  437. ordersymfile,
  438. linkfiles: TCmdStr;
  439. GCSectionsStr,
  440. StaticStr,
  441. StripStr,
  442. sanitizerLibraryDir: TCmdStr;
  443. success : boolean;
  444. begin
  445. if not(cs_link_nolink in current_settings.globalswitches) then
  446. Message1(exec_i_linking,current_module.exefilename);
  447. { Create some replacements }
  448. StaticStr:='';
  449. StripStr:='';
  450. GCSectionsStr:='';
  451. mapstr:='';
  452. targetstr:='';
  453. ltostr:='';
  454. if (cs_link_map in current_settings.globalswitches) then
  455. mapstr:='-map '+maybequoted(ChangeFileExt(current_module.exefilename,'.map'));
  456. if (cs_link_staticflag in current_settings.globalswitches) then
  457. StaticStr:='-static';
  458. if (cs_link_strip in current_settings.globalswitches) then
  459. StripStr:='-x';
  460. if (cs_link_smart in current_settings.globalswitches) then
  461. GCSectionsStr:='-dead_strip -no_dead_strip_inits_and_terms';
  462. { add custom LTO library if using custom clang }
  463. if (cs_lto in current_settings.moduleswitches) and
  464. not(cs_link_on_target in current_settings.globalswitches) and
  465. (utilsdirectory<>'') and
  466. FileExists(utilsdirectory+'/../lib/libLTO.dylib',false) then
  467. begin
  468. ltostr:='-lto_library '+maybequoted(utilsdirectory+'/../lib/libLTO.dylib');
  469. end;
  470. { Write symbol order file }
  471. ordersymfile:=WriteSymbolOrderFile;
  472. { Call linker }
  473. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  474. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename));
  475. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  476. Replace(cmdstr,'$TARGET',targetstr);
  477. Replace(cmdstr,'$MAP',mapstr);
  478. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  479. Replace(cmdstr,'$LTO',ltostr);
  480. if ordersymfile<>'' then
  481. Replace(cmdstr,'$ORDERSYMS','-order_file '+maybequoted(ordersymfile))
  482. else
  483. Replace(cmdstr,'$ORDERSYMS','');
  484. if AddSanitizerLibrariesAndGetSearchDir('darwin',sanitizerLibraryDir) then
  485. begin
  486. { also add the executable path as search path in case the asan
  487. library gets copied into the application bundle }
  488. Replace(cmdstr,'$RPATH','-rpath @executable_path -rpath '+maybequoted(sanitizerLibraryDir))
  489. end
  490. else
  491. begin
  492. Replace(cmdstr,'$RPATH','');
  493. end;
  494. { Write list of files to link }
  495. linkfiles:=WriteFileList;
  496. Replace(cmdstr,'$STATIC',StaticStr);
  497. Replace(cmdstr,'$STRIP',StripStr);
  498. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  499. Replace(cmdstr,'$PRTOBJ',GetDarwinPrtobjName(false));
  500. Replace(cmdstr,'$ARCH', GetLinkArch);
  501. Replace(cmdstr,'$VERSION',GetLinkVersion);
  502. Replace(cmdstr,'$SYSROOT',GetSysroot);
  503. Replace(cmdstr,'$LIBSEARCHPATH',GetLibSearchPath);
  504. Replace(cmdstr,'$FILELIST','-filelist '+maybequoted(linkfiles));
  505. Replace(cmdstr,'$LIBRARIES',GetLibraries);
  506. BinStr:=FindUtil(utilsprefix+BinStr);
  507. { create dsym file? }
  508. extdbgbinstr:='';
  509. extdbgcmdstr:='';
  510. if (target_dbg.id in [dbg_dwarf2,dbg_dwarf3,dbg_dwarf4]) and
  511. (cs_link_separate_dbg_file in current_settings.globalswitches) then
  512. begin
  513. extdbgbinstr:=FindUtil(utilsprefix+'dsymutil');
  514. extdbgcmdstr:=maybequoted(current_module.exefilename);
  515. end;
  516. success:=DoExec(BinStr,CmdStr,true,false);
  517. if success and
  518. (extdbgbinstr<>'') then
  519. success:=DoExec(extdbgbinstr,extdbgcmdstr,false,false);
  520. { Remove ReponseFile }
  521. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  522. begin
  523. DeleteFile(outputexedir+Info.ResName);
  524. if ordersymfile<>'' then
  525. DeleteFile(ordersymfile);
  526. DeleteFile(linkfiles);
  527. end;
  528. { Post process }
  529. if success and not(cs_link_nolink in current_settings.globalswitches) then
  530. success:=PostProcessExecutable(current_module.exefilename,false);
  531. MakeExecutable:=success; { otherwise a recursive call to link method }
  532. end;
  533. function tlinkerdarwin.MakeSharedLibrary: boolean;
  534. var
  535. InitStr,
  536. FiniStr,
  537. binstr,
  538. cmdstr,
  539. mapstr,
  540. ltostr,
  541. ordersymfile,
  542. targetstr,
  543. extdbgbinstr,
  544. extdbgcmdstr,
  545. linkfiles,
  546. GCSectionsStr,
  547. sanitizerLibraryDir: TCmdStr;
  548. exportedsyms: text;
  549. success : boolean;
  550. begin
  551. MakeSharedLibrary:=false;
  552. GCSectionsStr:='';
  553. mapstr:='';
  554. ltostr:='';
  555. if not(cs_link_nolink in current_settings.globalswitches) then
  556. Message1(exec_i_linking,current_module.sharedlibfilename);
  557. { Write symbol order file }
  558. ordersymfile:=WriteSymbolOrderFile;
  559. if (cs_link_smart in current_settings.globalswitches) then
  560. GCSectionsStr:='-dead_strip -no_dead_strip_inits_and_terms';
  561. if (cs_link_map in current_settings.globalswitches) then
  562. mapstr:='-map '+maybequoted(ChangeFileExt(current_module.sharedlibfilename,'.map'));
  563. { add custom LTO library if using custom clang }
  564. if (cs_lto in current_settings.moduleswitches) and
  565. not(cs_link_on_target in current_settings.globalswitches) and
  566. (utilsdirectory<>'') and
  567. FileExists(utilsdirectory+'/../lib/libLTO.dylib',false) then
  568. begin
  569. ltostr:='-lto_library '+maybequoted(utilsdirectory+'/../lib/libLTO.dylib');
  570. end;
  571. targetstr:='';
  572. InitStr:='-init FPC_LIB_START';
  573. FiniStr:='-fini FPC_LIB_EXIT';
  574. { Call linker }
  575. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  576. Replace(cmdstr,'$EXE',maybequoted(ExpandFileName(current_module.sharedlibfilename)));
  577. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  578. Replace(cmdstr,'$TARGET',targetstr);
  579. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  580. Replace(cmdstr,'$INIT',InitStr);
  581. Replace(cmdstr,'$FINI',FiniStr);
  582. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  583. Replace(cmdstr,'$MAP',mapstr);
  584. Replace(cmdstr,'$LTO',ltostr);
  585. if ordersymfile<>'' then
  586. Replace(cmdstr,'$ORDERSYMS','-order_file '+maybequoted(ordersymfile))
  587. else
  588. Replace(cmdstr,'$ORDERSYMS','');
  589. { add asan library if known }
  590. if AddSanitizerLibrariesAndGetSearchDir('darwin',sanitizerLibraryDir) then
  591. begin
  592. { also add the executable path as search path in case the asan
  593. library gets copied into the application bundle }
  594. Replace(cmdstr,'$RPATH','-rpath @executable_path -rpath '+maybequoted(sanitizerLibraryDir))
  595. end
  596. else
  597. begin
  598. Replace(cmdstr,'$RPATH','');
  599. end;
  600. { Write list of files to link }
  601. linkfiles:=WriteFileList;
  602. Replace(cmdstr,'$PRTOBJ',GetDarwinPrtobjName(true));
  603. Replace(cmdstr,'$ARCH', GetLinkArch);
  604. Replace(cmdstr,'$VERSION',GetLinkVersion);
  605. Replace(cmdstr,'$SYSROOT',GetSysroot);
  606. Replace(cmdstr,'$LIBSEARCHPATH',GetLibSearchPath);
  607. Replace(cmdstr,'$FILELIST','-filelist '+maybequoted(linkfiles));
  608. Replace(cmdstr,'$LIBRARIES',GetLibraries);
  609. BinStr:=FindUtil(utilsprefix+BinStr);
  610. { create dsym file? }
  611. extdbgbinstr:='';
  612. extdbgcmdstr:='';
  613. if (target_dbg.id in [dbg_dwarf2,dbg_dwarf3,dbg_dwarf4]) and
  614. (cs_link_separate_dbg_file in current_settings.globalswitches) then
  615. begin
  616. extdbgbinstr:=FindUtil(utilsprefix+'dsymutil');
  617. extdbgcmdstr:=maybequoted(current_module.sharedlibfilename);
  618. end;
  619. LinkSymsFileName:='';
  620. if not texportlibunix(exportlib).exportedsymnames.empty then
  621. begin
  622. LinkSymsFileName:=UniqueName('linksyms')+'.fpc';
  623. assign(exportedsyms,outputexedir+LinkSymsFileName);
  624. rewrite(exportedsyms);
  625. repeat
  626. writeln(exportedsyms,texportlibunix(exportlib).exportedsymnames.getfirst);
  627. until texportlibunix(exportlib).exportedsymnames.empty;
  628. close(exportedsyms);
  629. cmdstr:=cmdstr+' -exported_symbols_list '+maybequoted(outputexedir+LinkSymsFileName);
  630. end;
  631. success:=DoExec(BinStr,cmdstr,true,false);
  632. if (success and
  633. (extdbgbinstr<>'') and
  634. (cs_link_nolink in current_settings.globalswitches)) then
  635. success:=DoExec(extdbgbinstr,extdbgcmdstr,false,false);
  636. { Strip the library ? }
  637. if success and (cs_link_strip in current_settings.globalswitches) then
  638. begin
  639. SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  640. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename));
  641. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,false,false);
  642. end;
  643. { Remove temporary files }
  644. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  645. begin
  646. DeleteFile(outputexedir+Info.ResName);
  647. if ordersymfile<>'' then
  648. DeleteFile(ordersymfile);
  649. if LinkSymsFileName<>'' then
  650. DeleteFile(outputexedir+LinkSymsFileName);
  651. DeleteFile(linkfiles);
  652. end;
  653. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  654. end;
  655. function TLinkerDarwin.postprocessexecutable(const fn : string;isdll:boolean):boolean;
  656. begin
  657. Result:=PostProcessMachExecutable(fn,isdll);
  658. end;
  659. {*****************************************************************************
  660. Initialize
  661. *****************************************************************************}
  662. initialization
  663. RegisterLinker(ld_darwin,tlinkerdarwin);
  664. {$ifdef x86_64}
  665. RegisterImport(system_x86_64_darwin,timportlibdarwin);
  666. RegisterExport(system_x86_64_darwin,texportlibdarwin);
  667. RegisterTarget(system_x86_64_darwin_info);
  668. RegisterImport(system_x86_64_iphonesim,timportlibdarwin);
  669. RegisterExport(system_x86_64_iphonesim,texportlibdarwin);
  670. RegisterTarget(system_x86_64_iphonesim_info);
  671. {$endif}
  672. {$ifdef i386}
  673. RegisterImport(system_i386_darwin,timportlibdarwin);
  674. RegisterExport(system_i386_darwin,texportlibdarwin);
  675. RegisterTarget(system_i386_darwin_info);
  676. RegisterImport(system_i386_iphonesim,timportlibdarwin);
  677. RegisterExport(system_i386_iphonesim,texportlibdarwin);
  678. RegisterTarget(system_i386_iphonesim_info);
  679. {$endif i386}
  680. {$ifdef powerpc}
  681. RegisterImport(system_powerpc_darwin,timportlibdarwin);
  682. RegisterExport(system_powerpc_darwin,texportlibdarwin);
  683. RegisterTarget(system_powerpc_darwin_info);
  684. {$endif powerpc}
  685. {$ifdef powerpc64}
  686. RegisterImport(system_powerpc64_darwin,timportlibdarwin);
  687. RegisterExport(system_powerpc64_darwin,texportlibdarwin);
  688. RegisterTarget(system_powerpc64_darwin_info);
  689. {$endif powerpc64}
  690. {$ifdef arm}
  691. RegisterImport(system_arm_ios,timportlibdarwin);
  692. RegisterExport(system_arm_ios,texportlibdarwin);
  693. RegisterTarget(system_arm_ios_info);
  694. {$endif arm}
  695. {$ifdef aarch64}
  696. RegisterImport(system_aarch64_ios,timportlibdarwin);
  697. RegisterExport(system_aarch64_ios,texportlibdarwin);
  698. RegisterTarget(system_aarch64_ios_info);
  699. RegisterImport(system_aarch64_darwin,timportlibdarwin);
  700. RegisterExport(system_aarch64_darwin,texportlibdarwin);
  701. RegisterTarget(system_aarch64_darwin_info);
  702. RegisterImport(system_aarch64_iphonesim,timportlibdarwin);
  703. RegisterExport(system_aarch64_iphonesim,texportlibdarwin);
  704. RegisterTarget(system_aarch64_iphonesim_info);
  705. {$endif aarch64}
  706. RegisterRes(res_macho_info,TWinLikeResourceFile);
  707. RegisterRes(res_macosx_ext_info,TWinLikeResourceFile);
  708. end.