t_bsd.pas 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272
  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,cscript,
  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. prtobj : string[80];
  49. ReOrder : Boolean;
  50. linklibc : boolean;
  51. Function WriteResponseFile(isdll:boolean) : Boolean;
  52. function GetDarwinCrt1ObjName(isdll: boolean): TCmdStr;
  53. Function GetDarwinPrtobjName(isdll: boolean): TCmdStr;
  54. Function WriteSymbolOrderFile: TCmdStr;
  55. public
  56. constructor Create;override;
  57. procedure SetDefaultInfo;override;
  58. function MakeExecutable:boolean;override;
  59. function MakeSharedLibrary:boolean;override;
  60. procedure LoadPredefinedLibraryOrder; override;
  61. procedure InitSysInitUnitName; override;
  62. end;
  63. function ModulesLinkToLibc:boolean;
  64. var
  65. hp: tmodule;
  66. begin
  67. { This is called very early, ImportLibraryList is not yet merged into linkothersharedlibs.
  68. The former contains library names qualified with prefix and suffix (coming from
  69. "external 'c' name 'foo' declarations), the latter contains raw names (from "$linklib c"
  70. directives). }
  71. hp:=tmodule(loaded_units.first);
  72. while assigned(hp) do
  73. begin
  74. result:=Assigned(hp.ImportLibraryList.find(target_info.sharedClibprefix+'c'+target_info.sharedClibext));
  75. if result then break;
  76. result:=hp.linkothersharedlibs.find(target_info.sharedClibprefix+'c'+target_info.sharedClibext);
  77. if result then break;
  78. result:=hp.linkothersharedlibs.find('c');
  79. if result then break;
  80. hp:=tmodule(hp.next);
  81. end;
  82. end;
  83. {*****************************************************************************
  84. TIMPORTLIBDARWIN
  85. *****************************************************************************}
  86. procedure timportlibdarwin.generatelib;
  87. begin
  88. end;
  89. {*****************************************************************************
  90. TEXPORTLIBDARWIN
  91. *****************************************************************************}
  92. procedure texportlibdarwin.setinitname(list: TAsmList; const s: string);
  93. begin
  94. new_section(list,sec_init_func,'',sizeof(pint));
  95. list.concat(Tai_const.Createname(s,0));
  96. end;
  97. procedure texportlibdarwin.setfininame(list: TAsmList; const s: string);
  98. begin
  99. new_section(list,sec_term_func,'',sizeof(pint));
  100. list.concat(Tai_const.Createname(s,0));
  101. end;
  102. {*****************************************************************************
  103. TIMPORTLIBBSD
  104. *****************************************************************************}
  105. procedure timportlibbsd.generatelib;
  106. var
  107. i : longint;
  108. ImportLibrary : TImportLibrary;
  109. begin
  110. for i:=0 to current_module.ImportLibraryList.Count-1 do
  111. begin
  112. ImportLibrary:=TImportLibrary(current_module.ImportLibraryList[i]);
  113. current_module.linkothersharedlibs.add(ImportLibrary.Name,link_always);
  114. end;
  115. end;
  116. {*****************************************************************************
  117. TLINKERBSD
  118. *****************************************************************************}
  119. Constructor TLinkerBSD.Create;
  120. begin
  121. Inherited Create;
  122. if not Dontlinkstdlibpath Then
  123. if target_info.system in systems_darwin then
  124. { Mac OS X doesn't have a /lib }
  125. LibrarySearchPath.AddPath(sysrootpath,'=/usr/lib',true)
  126. else if target_info.system in systems_openbsd then
  127. LibrarySearchPath.AddPath(sysrootpath,'=/usr/lib;=$OPENBSD_X11BASE/lib;=$OPENBSD_LOCALBASE/lib',true)
  128. else
  129. LibrarySearchPath.AddPath(sysrootpath,'=/lib;=/usr/lib;=/usr/X11R6/lib',true);
  130. end;
  131. procedure TLinkerBSD.SetDefaultInfo;
  132. {
  133. This will also detect which libc version will be used
  134. }
  135. var
  136. LdProgram: string='ld';
  137. begin
  138. if target_info.system in systems_openbsd then
  139. LdProgram:='ld.bfd';
  140. LibrarySuffix:=' ';
  141. LdSupportsNoResponseFile := (target_info.system in ([system_m68k_netbsd]+systems_darwin));
  142. with Info do
  143. begin
  144. if LdSupportsNoResponseFile then
  145. begin
  146. if not(target_info.system in systems_darwin) then
  147. begin
  148. ExeCmd[1]:=LdProgram+' $TARGET $EMUL $OPT $DYNLINK $STATIC $GCSECTIONS $STRIP $MAP $LTO $ORDERSYMS -L. -o $EXE $CATRES $FILELIST';
  149. DllCmd[1]:=LdProgram+' $TARGET $EMUL $OPT $MAP $LTO $ORDERSYMS -shared -L. -o $EXE $CATRES $FILELIST'
  150. end
  151. else
  152. begin
  153. {$ifndef cpu64bitaddr}
  154. { Set the size of the page at address zero to 64kb, so nothing
  155. is loaded below that address. This avoids problems with the
  156. strange Windows-compatible resource handling that assumes
  157. that addresses below 64kb do not exist.
  158. On 64bit systems, page zero is 4GB by default, so no problems
  159. there.
  160. }
  161. { In case of valgrind, don't do that, because it cannot deal with
  162. a custom pagezero size -- in general, this should not cause any
  163. problems because the resources are added at the end and most
  164. programs with problems that require Valgrind will have more
  165. than 60KB of data (first 4KB of address space is always invalid)
  166. }
  167. ExeCmd[1]:=LdProgram+' $PRTOBJ $TARGET $EMUL $OPT $DYNLINK $STATIC $GCSECTIONS $STRIP $MAP $LTO $ORDERSYMS -multiply_defined suppress -L. -o $EXE $CATRES $FILELIST';
  168. if not(cs_gdb_valgrind in current_settings.globalswitches) then
  169. ExeCmd[1]:=ExeCmd[1]+' -pagezero_size 0x10000';
  170. {$else ndef cpu64bitaddr}
  171. ExeCmd[1]:=LdProgram+' $PRTOBJ $TARGET $EMUL $OPT $DYNLINK $STATIC $GCSECTIONS $STRIP $MAP $LTO $ORDERSYMS -multiply_defined suppress -L. -o $EXE $CATRES $FILELIST';
  172. {$endif ndef cpu64bitaddr}
  173. if (apptype<>app_bundle) then
  174. DllCmd[1]:=LdProgram+' $PRTOBJ $TARGET $EMUL $OPT $GCSECTIONS $MAP $LTO $ORDERSYMS -dynamic -dylib -multiply_defined suppress -L. -o $EXE $CATRES $FILELIST'
  175. else
  176. DllCmd[1]:=LdProgram+' $PRTOBJ $TARGET $EMUL $OPT $GCSECTIONS $MAP $LTO $ORDERSYMS -dynamic -bundle -multiply_defined suppress -L. -o $EXE $CATRES $FILELIST'
  177. end
  178. end
  179. else
  180. begin
  181. ExeCmd[1]:=LdProgram+' $TARGET $EMUL $OPT $DYNLINK $STATIC $GCSECTIONS $STRIP $MAP $LTO $ORDERSYMS -L. -o $EXE $RES';
  182. DllCmd[1]:=LdProgram+' $TARGET $EMUL $OPT $INIT $FINI $SONAME $MAP $LTO $ORDERSYMS -shared -L. -o $EXE $RES';
  183. end;
  184. if not(target_info.system in systems_darwin) then
  185. DllCmd[2]:='strip --strip-unneeded $EXE'
  186. else
  187. DllCmd[2]:='strip -x $EXE';
  188. { OpenBSD seems to use a wrong dynamic linker by default }
  189. if target_info.system in systems_openbsd then
  190. DynamicLinker:='/usr/libexec/ld.so'
  191. else if target_info.system in systems_netbsd then
  192. DynamicLinker:='/usr/libexec/ld.elf_so'
  193. else if target_info.system=system_x86_64_dragonfly then
  194. DynamicLinker:='/libexec/ld-elf.so.2'
  195. else
  196. DynamicLinker:='';
  197. end;
  198. end;
  199. procedure TLinkerBSD.LoadPredefinedLibraryOrder;
  200. // put your linkorder/linkalias overrides here.
  201. // Note: assumes only called when reordering/aliasing is used.
  202. Begin
  203. if not(target_info.system in systems_darwin) then
  204. begin
  205. if (target_info.system =system_i386_freebsd) and
  206. not (cs_link_no_default_lib_order in current_settings.globalswitches) Then
  207. Begin
  208. LinkLibraryOrder.add('gcc','',15);
  209. LinkLibraryOrder.add('c','',50); // c and c_p mutual. excl?
  210. LinkLibraryOrder.add('c_p','',55);
  211. LinkLibraryOrder.add('pthread','',75); // pthread and c_r should be mutually exclusive
  212. LinkLibraryOrder.add('c_r','',76);
  213. LinkLibraryOrder.add('kvm','',80); // must be before ncurses
  214. if (cs_link_pthread in current_settings.globalswitches) Then // convert libpthread to libc_r.
  215. LinkLibraryAliases.add('pthread','c_r');
  216. end;
  217. end
  218. else
  219. begin
  220. LinkLibraryOrder.add('gcc','',15);
  221. LinkLibraryOrder.add('c','',50);
  222. end;
  223. End;
  224. procedure TLinkerBSD.InitSysInitUnitName;
  225. var
  226. cprtobj,
  227. gprtobj,
  228. si_cprt,
  229. si_gprt : string[80];
  230. begin
  231. if target_info.system in systems_darwin then
  232. begin
  233. { for darwin: always link dynamically against libc }
  234. linklibc := true;
  235. reorder:=reorderentries;
  236. prtobj:='';
  237. SysInitUnit:='sysinit';
  238. end
  239. else
  240. begin
  241. linklibc:=ModulesLinkToLibc;
  242. if current_module.islibrary and
  243. (target_info.system in systems_bsd) then
  244. begin
  245. prtobj:='dllprt0';
  246. cprtobj:='dllprt0';
  247. gprtobj:='dllprt0';
  248. SysInitUnit:='si_dll';
  249. si_cprt:='si_dll';
  250. si_gprt:='si_dll';
  251. end
  252. else
  253. begin
  254. prtobj:='prt0';
  255. cprtobj:='cprt0';
  256. gprtobj:='gprt0';
  257. SysInitUnit:='si_prc';
  258. si_cprt:='si_c';
  259. si_gprt:='si_g';
  260. end;
  261. // this one is a bit complex.
  262. // Only reorder for now if -XL or -XO params are given
  263. // or when -Xf.
  264. reorder:= linklibc and
  265. (
  266. ReorderEntries
  267. or
  268. (cs_link_pthread in current_settings.globalswitches));
  269. if cs_profile in current_settings.moduleswitches then
  270. begin
  271. prtobj:=gprtobj;
  272. SysInitUnit:=si_gprt;
  273. AddSharedLibrary('c');
  274. LibrarySuffix:='p';
  275. linklibc:=true;
  276. end
  277. else
  278. begin
  279. if linklibc then
  280. begin
  281. prtobj:=cprtobj;
  282. SysInitUnit:=si_cprt;
  283. end;
  284. end;
  285. end;
  286. end;
  287. function TLinkerBSD.GetDarwinCrt1ObjName(isdll: boolean): TCmdStr;
  288. begin
  289. if not isdll then
  290. begin
  291. if not(cs_profile in current_settings.moduleswitches) then
  292. begin
  293. case target_info.system of
  294. system_powerpc_darwin,
  295. system_powerpc64_darwin,
  296. system_i386_darwin,
  297. system_x86_64_darwin:
  298. begin
  299. { 10.8 and later: no crt1.* }
  300. if CompareVersionStrings(MacOSXVersionMin,'10.8')>=0 then
  301. exit('');
  302. { x86: crt1.10.6.o -> crt1.10.5.o -> crt1.o }
  303. { others: crt1.10.5 -> crt1.o }
  304. if (target_info.system in [system_i386_darwin,system_x86_64_darwin]) and
  305. (CompareVersionStrings(MacOSXVersionMin,'10.6')>=0) then
  306. exit('crt1.10.6.o');
  307. if CompareVersionStrings(MacOSXVersionMin,'10.5')>=0 then
  308. exit('crt1.10.5.o');
  309. end;
  310. system_arm_darwin:
  311. begin
  312. { iOS:
  313. iOS 6 and later: nothing
  314. iOS 3.1 - 5.x: crt1.3.1.o
  315. pre-iOS 3.1: crt1.o
  316. }
  317. if (CompareVersionStrings(iPhoneOSVersionMin,'6.0')>=0) then
  318. exit('');
  319. if (CompareVersionStrings(iPhoneOSVersionMin,'3.1')>=0) then
  320. exit('crt1.3.1.o');
  321. end;
  322. system_i386_iphonesim,
  323. system_x86_64_iphonesim:
  324. begin
  325. { "recent versions" must not use anything (https://github.com/llvm-mirror/clang/commit/e6d04f3d152a22077022cf9287d4c538a0918ab0 )
  326. What those recent versions could be, is anyone's guess. It
  327. still seems to work with 8.1 and no longer with 8.3, so use
  328. 8.1 as a cut-off point }
  329. if (CompareVersionStrings(iPhoneOSVersionMin,'8.1')>0) then
  330. exit('');
  331. end;
  332. system_aarch64_darwin:
  333. { never anything }
  334. exit('');
  335. else
  336. Internalerror(2019050709);
  337. end;
  338. { nothing special -> default }
  339. result:='crt1.o';
  340. end
  341. else
  342. begin
  343. result:='gcrt1.o';
  344. { 10.8 and later: tell the linker to use 'start' instead of "_main"
  345. as entry point }
  346. if CompareVersionStrings(MacOSXVersionMin,'10.8')>=0 then
  347. Info.ExeCmd[1]:=Info.ExeCmd[1]+' -no_new_main';
  348. end;
  349. end
  350. else
  351. begin
  352. if (apptype=app_bundle) then
  353. begin
  354. case target_info.system of
  355. system_powerpc_darwin,
  356. system_powerpc64_darwin,
  357. system_i386_darwin,
  358. system_x86_64_darwin:
  359. begin
  360. { < 10.6: bundle1.o
  361. >= 10.6: nothing }
  362. if CompareVersionStrings(MacOSXVersionMin,'10.6')>=0 then
  363. exit('');
  364. end;
  365. system_arm_darwin,
  366. system_aarch64_darwin:
  367. begin
  368. { iOS: < 3.1: bundle1.o
  369. >= 3.1: nothing }
  370. if (CompareVersionStrings(iPhoneOSVersionMin,'3.1')>=0) then
  371. exit('');
  372. end;
  373. system_i386_iphonesim,
  374. system_x86_64_iphonesim:
  375. begin
  376. { see rule for crt1.o }
  377. if (CompareVersionStrings(iPhoneOSVersionMin,'8.1')>0) then
  378. exit('');
  379. end;
  380. else
  381. Internalerror(2019050710);
  382. end;
  383. result:='bundle1.o';
  384. end
  385. else
  386. begin
  387. case target_info.system of
  388. system_powerpc_darwin,
  389. system_powerpc64_darwin,
  390. system_i386_darwin,
  391. system_x86_64_darwin:
  392. begin
  393. { >= 10.6: nothing
  394. = 10.5: dylib1.10.5.o
  395. < 10.5: dylib1.o
  396. }
  397. if CompareVersionStrings(MacOSXVersionMin,'10.6')>=0 then
  398. exit('');
  399. if CompareVersionStrings(MacOSXVersionMin,'10.5')>=0 then
  400. exit('dylib1.10.5.o');
  401. end;
  402. system_arm_darwin,
  403. system_aarch64_darwin:
  404. begin
  405. { iOS: < 3.1: dylib1.o
  406. >= 3.1: nothing }
  407. if (CompareVersionStrings(iPhoneOSVersionMin,'3.1')>=0) then
  408. exit('');
  409. end;
  410. system_i386_iphonesim,
  411. system_x86_64_iphonesim:
  412. begin
  413. { see rule for crt1.o }
  414. if (CompareVersionStrings(iPhoneOSVersionMin,'8.1')>0) then
  415. exit('');
  416. end;
  417. else
  418. Internalerror(2019050711);
  419. end;
  420. result:='dylib1.o';
  421. end;
  422. end;
  423. end;
  424. Function TLinkerBSD.GetDarwinPrtobjName(isdll: boolean): TCmdStr;
  425. var
  426. startupfile: TCmdStr;
  427. begin
  428. result:='';
  429. startupfile:=GetDarwinCrt1ObjName(isdll);
  430. if startupfile<>'' then
  431. begin
  432. if not librarysearchpath.FindFile(startupfile,false,result) then
  433. result:='/usr/lib/'+startupfile;
  434. end;
  435. result:=maybequoted(result);
  436. end;
  437. function tlinkerbsd.WriteSymbolOrderFile: TCmdStr;
  438. var
  439. item: TCmdStrListItem;
  440. symfile: TScript;
  441. begin
  442. result:='';
  443. { only for darwin for now; can also enable for other platforms when using
  444. the LLVM linker }
  445. if (OrderedSymbols.Empty) or
  446. not(tf_supports_symbolorderfile in target_info.flags) then
  447. exit;
  448. symfile:=TScript.Create(outputexedir+'symbol_order.fpc');
  449. item:=TCmdStrListItem(OrderedSymbols.First);
  450. while assigned(item) do
  451. begin
  452. symfile.add(item.str);
  453. item:=TCmdStrListItem(item.next);
  454. end;
  455. symfile.WriteToDisk;
  456. result:=symfile.fn;
  457. symfile.Free;
  458. end;
  459. Function TLinkerBSD.WriteResponseFile(isdll:boolean) : Boolean;
  460. Var
  461. linkres : TLinkRes;
  462. FilesList : TLinkRes;
  463. i : longint;
  464. HPath : TCmdStrListItem;
  465. s,s1,s2 : TCmdStr;
  466. linkdynamic : boolean;
  467. Fl1,Fl2 : Boolean;
  468. IsDarwin : Boolean;
  469. begin
  470. WriteResponseFile:=False;
  471. ReOrder:=False;
  472. linkdynamic:=False;
  473. IsDarwin:=target_info.system in systems_darwin;
  474. { set special options for some targets }
  475. if not IsDarwin Then
  476. begin
  477. linkdynamic:=not(SharedLibFiles.empty);
  478. // after this point addition of shared libs not allowed.
  479. end
  480. else
  481. begin
  482. {$ifdef MACOSX104ORHIGHER}
  483. { not sure what this is for, but gcc always links against it }
  484. if not(cs_profile in current_settings.moduleswitches) then
  485. AddSharedLibrary('SystemStubs')
  486. else
  487. AddSharedLibrary('SystemStubs_profile');
  488. {$endif MACOSX104ORHIGHER}
  489. end;
  490. if reorder Then
  491. ExpandAndApplyOrder(SharedLibFiles);
  492. { Open link.res file }
  493. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,not LdSupportsNoResponseFile);
  494. if (target_info.system in systems_darwin) and
  495. (sysrootpath<>'') then
  496. begin
  497. LinkRes.Add('-syslibroot');
  498. LinkRes.Add(sysrootpath);
  499. end;
  500. if (target_info.system in systems_darwin) then
  501. begin
  502. LinkRes.Add('-arch');
  503. case target_info.system of
  504. system_powerpc_darwin:
  505. LinkRes.Add('ppc');
  506. system_i386_darwin,
  507. system_i386_iphonesim:
  508. LinkRes.Add('i386');
  509. system_powerpc64_darwin:
  510. LinkRes.Add('ppc64');
  511. system_x86_64_darwin,
  512. system_x86_64_iphonesim:
  513. LinkRes.Add('x86_64');
  514. system_arm_darwin:
  515. { current versions of the linker require the sub-architecture type
  516. to be specified }
  517. LinkRes.Add(lower(cputypestr[current_settings.cputype]));
  518. system_aarch64_darwin:
  519. LinkRes.Add('arm64');
  520. else
  521. internalerror(2014121801);
  522. end;
  523. if MacOSXVersionMin<>'' then
  524. begin
  525. LinkRes.Add('-macosx_version_min');
  526. LinkRes.Add(MacOSXVersionMin);
  527. end
  528. else if iPhoneOSVersionMin<>'' then
  529. begin
  530. LinkRes.Add('-iphoneos_version_min');
  531. LinkRes.Add(iPhoneOSVersionMin);
  532. end;
  533. end;
  534. { Write path to search libraries }
  535. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  536. while assigned(HPath) do
  537. begin
  538. if LdSupportsNoResponseFile then
  539. LinkRes.Add('-L'+HPath.Str)
  540. else
  541. LinkRes.Add('SEARCH_DIR("'+HPath.Str+'")');
  542. HPath:=TCmdStrListItem(HPath.Next);
  543. end;
  544. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  545. while assigned(HPath) do
  546. begin
  547. if LdSupportsNoResponseFile then
  548. LinkRes.Add('-L'+HPath.Str)
  549. else
  550. LinkRes.Add('SEARCH_DIR("'+HPath.Str+'")');
  551. HPath:=TCmdStrListItem(HPath.Next);
  552. end;
  553. if (target_info.system in systems_darwin) then
  554. begin
  555. HPath:=TCmdStrListItem(current_module.localframeworksearchpath.First);
  556. while assigned(HPath) do
  557. begin
  558. LinkRes.Add('-F'+HPath.Str);
  559. HPath:=TCmdStrListItem(HPath.Next);
  560. end;
  561. HPath:=TCmdStrListItem(FrameworkSearchPath.First);
  562. while assigned(HPath) do
  563. begin
  564. LinkRes.Add('-F'+HPath.Str);
  565. HPath:=TCmdStrListItem(HPath.Next);
  566. end;
  567. end;
  568. { force local symbol resolution (i.e., inside the shared }
  569. { library itself) for all non-exorted symbols, otherwise }
  570. { several RTL symbols of FPC-compiled shared libraries }
  571. { will be bound to those of a single shared library or }
  572. { to the main program }
  573. if (isdll) and (target_info.system in systems_bsd) then
  574. begin
  575. LinkRes.add('VERSION');
  576. LinkRes.add('{');
  577. LinkRes.add(' {');
  578. if not texportlibunix(exportlib).exportedsymnames.empty then
  579. begin
  580. LinkRes.add(' global:');
  581. repeat
  582. LinkRes.add(' '+texportlibunix(exportlib).exportedsymnames.getfirst+';');
  583. until texportlibunix(exportlib).exportedsymnames.empty;
  584. end;
  585. LinkRes.add(' local:');
  586. LinkRes.add(' *;');
  587. LinkRes.add(' };');
  588. LinkRes.add('}');
  589. end;
  590. if not LdSupportsNoResponseFile then
  591. LinkRes.Add('INPUT(');
  592. { add objectfiles, start with prt0 always }
  593. if not (target_info.system in systems_internal_sysinit) and (prtobj<>'') then
  594. LinkRes.AddFileName(FindObjectFile(prtobj,'',false));
  595. { try to add crti and crtbegin if linking to C }
  596. if linklibc and
  597. not IsDarwin Then
  598. begin
  599. if librarysearchpath.FindFile('crti.o',false,s) then
  600. LinkRes.AddFileName(s);
  601. if ((cs_create_pic in current_settings.moduleswitches) and
  602. not (target_info.system in systems_openbsd)) or
  603. (current_module.islibrary and
  604. (target_info.system in systems_openbsd)) then
  605. begin
  606. if librarysearchpath.FindFile('crtbeginS.o',false,s) then
  607. LinkRes.AddFileName(s);
  608. end
  609. else
  610. if (cs_link_staticflag in current_settings.globalswitches) and
  611. librarysearchpath.FindFile('crtbeginT.o',false,s) then
  612. LinkRes.AddFileName(s)
  613. else if librarysearchpath.FindFile('crtbegin.o',false,s) then
  614. LinkRes.AddFileName(s);
  615. end;
  616. { main objectfiles }
  617. { Generate linkfiles.res file if needed }
  618. { Only needed on Windows, due to the limitation of 8196 characters for command line }
  619. if (LdSupportsNoResponseFile) and
  620. (source_info.system in systems_all_windows) then
  621. begin
  622. FilesList:=TLinkRes.Create(outputexedir+'linkfiles.res',false);
  623. while not ObjectFiles.Empty do
  624. begin
  625. s:=ObjectFiles.GetFirst;
  626. if s<>'' then
  627. begin
  628. repeat
  629. i:=Pos(source_info.dirsep,s);
  630. if i>0 then
  631. s[i]:=target_info.dirsep;
  632. until i=0;
  633. FilesList.Add(s);
  634. end;
  635. end;
  636. FilesList.writetodisk;
  637. FilesList.Free;
  638. end
  639. else
  640. begin
  641. while not ObjectFiles.Empty do
  642. begin
  643. s:=ObjectFiles.GetFirst;
  644. if s<>'' then
  645. if LdSupportsNoResponseFile then
  646. LinkRes.AddFileName(s)
  647. else
  648. LinkRes.AddFileName(maybequoted(s));
  649. end;
  650. end;
  651. if not LdSupportsNoResponseFile then
  652. LinkRes.Add(')');
  653. { Write staticlibraries }
  654. if not StaticLibFiles.Empty then
  655. begin
  656. if not LdSupportsNoResponseFile then
  657. LinkRes.Add('GROUP(');
  658. While not StaticLibFiles.Empty do
  659. begin
  660. S:=StaticLibFiles.GetFirst;
  661. if LdSupportsNoResponseFile then
  662. LinkRes.AddFileName(s)
  663. else
  664. LinkRes.AddFileName(maybequoted(s))
  665. end;
  666. if not LdSupportsNoResponseFile then
  667. LinkRes.Add(')');
  668. end;
  669. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  670. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  671. if not SharedLibFiles.Empty then
  672. begin
  673. if not LdSupportsNoResponseFile then
  674. LinkRes.Add('INPUT(');
  675. While not SharedLibFiles.Empty do
  676. begin
  677. S:=SharedLibFiles.GetFirst;
  678. if (s<>'c') or reorder then
  679. begin
  680. i:=Pos(target_info.sharedlibext,S);
  681. if i>0 then
  682. Delete(S,i,255);
  683. LinkRes.Add('-l'+s);
  684. end
  685. else
  686. begin
  687. linklibc:=true;
  688. linkdynamic:=false; { libc will include the ld-* for us }
  689. end;
  690. end;
  691. { be sure that libc is the last lib }
  692. if linklibc and not reorder then
  693. Begin
  694. If LibrarySuffix=' ' Then
  695. LinkRes.Add('-lc')
  696. else
  697. LinkRes.Add('-lc_'+LibrarySuffix);
  698. If LibrarySuffix='r' Then
  699. LinkRes.Add('-lc');
  700. end;
  701. { when we have -static for the linker the we also need libgcc }
  702. if (cs_link_staticflag in current_settings.globalswitches) then
  703. LinkRes.Add('-lgcc');
  704. if linkdynamic and (Info.DynamicLinker<>'') and
  705. not(target_info.system in systems_openbsd) then
  706. LinkRes.AddFileName(Info.DynamicLinker);
  707. if not LdSupportsNoResponseFile then
  708. LinkRes.Add(')');
  709. end;
  710. { frameworks for Darwin }
  711. if IsDarwin then
  712. while not FrameworkFiles.empty do
  713. begin
  714. LinkRes.Add('-framework');
  715. LinkRes.Add(FrameworkFiles.GetFirst);
  716. end;
  717. { objects which must be at the end }
  718. if linklibc and
  719. not IsDarwin Then
  720. begin
  721. if ((cs_create_pic in current_settings.moduleswitches) and
  722. not (target_info.system in systems_openbsd)) or
  723. (current_module.islibrary and
  724. (target_info.system in systems_openbsd)) then
  725. Fl1:=librarysearchpath.FindFile('crtendS.o',false,s1)
  726. else
  727. Fl1:=librarysearchpath.FindFile('crtend.o',false,s1);
  728. Fl2:=librarysearchpath.FindFile('crtn.o',false,s2);
  729. if Fl1 or Fl2 then
  730. begin
  731. LinkRes.Add('INPUT(');
  732. If Fl1 Then
  733. LinkRes.AddFileName(s1);
  734. If Fl2 Then
  735. LinkRes.AddFileName(s2);
  736. LinkRes.Add(')');
  737. end;
  738. end;
  739. { Write and Close response }
  740. linkres.writetodisk;
  741. linkres.Free;
  742. WriteResponseFile:=True;
  743. end;
  744. function TLinkerBSD.MakeExecutable:boolean;
  745. var
  746. binstr,
  747. cmdstr,
  748. mapstr,
  749. targetstr,
  750. emulstr,
  751. extdbgbinstr,
  752. extdbgcmdstr,
  753. ltostr,
  754. ordersymfile: TCmdStr;
  755. linkscript: TAsmScript;
  756. DynLinkStr : string[60];
  757. GCSectionsStr,
  758. StaticStr,
  759. StripStr : string[63];
  760. success,
  761. useshell : boolean;
  762. begin
  763. if not(cs_link_nolink in current_settings.globalswitches) then
  764. Message1(exec_i_linking,current_module.exefilename);
  765. { Create some replacements }
  766. StaticStr:='';
  767. StripStr:='';
  768. DynLinkStr:='';
  769. GCSectionsStr:='';
  770. linkscript:=nil;
  771. mapstr:='';
  772. ltostr:='';
  773. if (cs_link_map in current_settings.globalswitches) then
  774. mapstr:='-Map '+maybequoted(ChangeFileExt(current_module.exefilename,'.map'));
  775. { i386_freebsd needs -b elf32-i386-freebsd and -m elf_i386_fbsd
  776. to avoid creation of a i386:x86_64 arch binary }
  777. if target_info.system=system_i386_freebsd then
  778. begin
  779. targetstr:='-b elf32-i386-freebsd';
  780. emulstr:='-m elf_i386_fbsd';
  781. end
  782. else
  783. begin
  784. targetstr:='';
  785. emulstr:='';
  786. end;
  787. if (cs_link_staticflag in current_settings.globalswitches) then
  788. begin
  789. if (target_info.system=system_m68k_netbsd) and
  790. ((cs_link_on_target in current_settings.globalswitches) or
  791. (target_info.system=source_info.system)) then
  792. StaticStr:='-Bstatic'
  793. else
  794. StaticStr:='-static';
  795. end;
  796. if (cs_link_strip in current_settings.globalswitches) then
  797. if (target_info.system in systems_darwin) then
  798. StripStr:='-x'
  799. else
  800. StripStr:='-s';
  801. if (cs_link_smart in current_settings.globalswitches) and
  802. (tf_smartlink_sections in target_info.flags) then
  803. if not(target_info.system in systems_darwin) then
  804. GCSectionsStr:='--gc-sections'
  805. else
  806. GCSectionsStr:='-dead_strip -no_dead_strip_inits_and_terms';
  807. if(not(target_info.system in systems_darwin) and
  808. (cs_profile in current_settings.moduleswitches)) or
  809. ((Info.DynamicLinker<>'') and
  810. ((not SharedLibFiles.Empty) or
  811. (target_info.system in systems_openbsd))) then
  812. DynLinkStr:='-dynamic-linker='+Info.DynamicLinker;
  813. if CShared Then
  814. begin
  815. if not(target_info.system in systems_darwin) then
  816. DynLinKStr:=DynLinkStr+' --shared'
  817. else
  818. DynLinKStr:=DynLinkStr+' -dynamic'; // one dash!
  819. end;
  820. { add custom LTO library if using custom clang }
  821. if (target_info.system in systems_darwin) and
  822. (cs_lto in current_settings.moduleswitches) and
  823. not(cs_link_on_target in current_settings.globalswitches) and
  824. (utilsdirectory<>'') and
  825. FileExists(utilsdirectory+'/../lib/libLTO.dylib',true) then
  826. begin
  827. ltostr:='-lto_library '+maybequoted(utilsdirectory+'/../lib/libLTO.dylib');
  828. end;
  829. { Use -nopie on OpenBSD if PIC support is turned off }
  830. if (target_info.system in systems_openbsd) and
  831. not(cs_create_pic in current_settings.moduleswitches) then
  832. Info.ExtraOptions:=Info.ExtraOptions+' -nopie';
  833. { -N seems to be needed on NetBSD/earm }
  834. if (target_info.system in [system_arm_netbsd]) then
  835. Info.ExtraOptions:=Info.ExtraOptions+' -N';
  836. { Write used files and libraries }
  837. WriteResponseFile(false);
  838. { Write symbol order file }
  839. ordersymfile:=WriteSymbolOrderFile;
  840. { Call linker }
  841. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  842. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename));
  843. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  844. Replace(cmdstr,'$TARGET',targetstr);
  845. Replace(cmdstr,'$EMUL',EmulStr);
  846. Replace(cmdstr,'$MAP',mapstr);
  847. Replace(cmdstr,'$CATRES',CatFileContent(outputexedir+Info.ResName));
  848. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  849. Replace(cmdstr,'$LTO',ltostr);
  850. if ordersymfile<>'' then
  851. begin
  852. if target_info.system in systems_darwin then
  853. Replace(cmdstr,'$ORDERSYMS','-order_file '+maybequoted(ordersymfile))
  854. else
  855. Replace(cmdstr,'$ORDERSYMS','--symbol-ordering-file '+maybequoted(ordersymfile))
  856. end
  857. else
  858. Replace(cmdstr,'$ORDERSYMS','');
  859. if (LdSupportsNoResponseFile) and (source_info.system in systems_all_windows) then
  860. Replace(cmdstr,'$FILELIST','-filelist '+maybequoted(outputexedir+'linkfiles.res'))
  861. else
  862. Replace(cmdstr,'$FILELIST','');
  863. Replace(cmdstr,'$STATIC',StaticStr);
  864. Replace(cmdstr,'$STRIP',StripStr);
  865. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  866. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  867. if (target_info.system in systems_darwin) then
  868. Replace(cmdstr,'$PRTOBJ',GetDarwinPrtobjName(false));
  869. BinStr:=FindUtil(utilsprefix+BinStr);
  870. { create dsym file? }
  871. extdbgbinstr:='';
  872. extdbgcmdstr:='';
  873. if (target_info.system in systems_darwin) and
  874. (target_dbg.id in [dbg_dwarf2,dbg_dwarf3]) and
  875. (cs_link_separate_dbg_file in current_settings.globalswitches) then
  876. begin
  877. extdbgbinstr:=FindUtil(utilsprefix+'dsymutil');
  878. extdbgcmdstr:=maybequoted(current_module.exefilename);
  879. end;
  880. if (LdSupportsNoResponseFile) and
  881. not(cs_link_nolink in current_settings.globalswitches) then
  882. begin
  883. { we have to use a script to use the IFS hack }
  884. linkscript:=GenerateScript(outputexedir+'ppaslink');
  885. linkscript.AddLinkCommand(BinStr,CmdStr,'');
  886. if (extdbgcmdstr<>'') then
  887. linkscript.AddLinkCommand(extdbgbinstr,extdbgcmdstr,'');
  888. linkscript.WriteToDisk;
  889. BinStr:=linkscript.fn;
  890. if not path_absolute(BinStr) then
  891. if cs_link_on_target in current_settings.globalswitches then
  892. BinStr:='.'+target_info.dirsep+BinStr
  893. else
  894. BinStr:='.'+source_info.dirsep+BinStr;
  895. CmdStr:='';
  896. end;
  897. useshell:=not (tf_no_backquote_support in source_info.flags);
  898. success:=DoExec(BinStr,CmdStr,true,LdSupportsNoResponseFile or useshell);
  899. if (success and
  900. (extdbgbinstr<>'') and
  901. (cs_link_nolink in current_settings.globalswitches)) then
  902. success:=DoExec(extdbgbinstr,extdbgcmdstr,false,LdSupportsNoResponseFile);
  903. { Remove ReponseFile }
  904. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  905. begin
  906. DeleteFile(outputexedir+Info.ResName);
  907. if ordersymfile<>'' then
  908. DeleteFile(ordersymfile);
  909. if LdSupportsNoResponseFile Then
  910. begin
  911. DeleteFile(linkscript.fn);
  912. linkscript.free
  913. end;
  914. end;
  915. { Remove linkfiles.res }
  916. if (success) and (LdSupportsNoResponseFile) and (source_info.system in systems_all_windows) then
  917. DeleteFile(outputexedir+'linkfiles.res');
  918. MakeExecutable:=success; { otherwise a recursive call to link method }
  919. end;
  920. Function TLinkerBSD.MakeSharedLibrary:boolean;
  921. var
  922. InitStr,
  923. FiniStr,
  924. SoNameStr : string[80];
  925. linkscript: TAsmScript;
  926. binstr,
  927. cmdstr,
  928. mapstr,
  929. ltostr,
  930. ordersymfile,
  931. targetstr,
  932. emulstr,
  933. extdbgbinstr,
  934. extdbgcmdstr : TCmdStr;
  935. GCSectionsStr : string[63];
  936. exportedsyms: text;
  937. success : boolean;
  938. begin
  939. MakeSharedLibrary:=false;
  940. GCSectionsStr:='';
  941. mapstr:='';
  942. ltostr:='';
  943. linkscript:=nil;
  944. if not(cs_link_nolink in current_settings.globalswitches) then
  945. Message1(exec_i_linking,current_module.sharedlibfilename);
  946. { Write used files and libraries }
  947. WriteResponseFile(true);
  948. { Write symbol order file }
  949. ordersymfile:=WriteSymbolOrderFile;
  950. if (cs_link_smart in current_settings.globalswitches) and
  951. (tf_smartlink_sections in target_info.flags) then
  952. if not(target_info.system in systems_darwin) then
  953. { disabled because not tested
  954. GCSectionsStr:='--gc-sections' }
  955. else
  956. GCSectionsStr:='-dead_strip -no_dead_strip_inits_and_terms';
  957. if (cs_link_map in current_settings.globalswitches) then
  958. mapstr:='-Map '+maybequoted(ChangeFileExt(current_module.sharedlibfilename,'.map'));
  959. { add custom LTO library if using custom clang }
  960. if (target_info.system in systems_darwin) and
  961. (cs_lto in current_settings.moduleswitches) and
  962. not(cs_link_on_target in current_settings.globalswitches) and
  963. (utilsdirectory<>'') and
  964. FileExists(utilsdirectory+'/../lib/libLTO.dylib',true) then
  965. begin
  966. ltostr:='-lto_library '+maybequoted(utilsdirectory+'/../lib/libLTO.dylib');
  967. end;
  968. { i386_freebsd needs -b elf32-i386-freebsd and -m elf_i386_fbsd
  969. to avoid creation of a i386:x86_64 arch binary }
  970. if target_info.system=system_i386_freebsd then
  971. begin
  972. targetstr:='-b elf32-i386-freebsd';
  973. emulstr:='-m elf_i386_fbsd';
  974. end
  975. else
  976. begin
  977. targetstr:='';
  978. emulstr:='';
  979. end;
  980. InitStr:='-init FPC_LIB_START';
  981. FiniStr:='-fini FPC_LIB_EXIT';
  982. SoNameStr:='-soname '+ExtractFileName(current_module.sharedlibfilename);
  983. { Call linker }
  984. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  985. {$ifndef darwin}
  986. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename));
  987. {$else darwin}
  988. Replace(cmdstr,'$EXE',maybequoted(ExpandFileName(current_module.sharedlibfilename)));
  989. {$endif darwin}
  990. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  991. Replace(cmdstr,'$TARGET',targetstr);
  992. Replace(cmdstr,'$EMUL',EmulStr);
  993. Replace(cmdstr,'$CATRES',CatFileContent(outputexedir+Info.ResName));
  994. if (LdSupportsNoResponseFile) and (source_info.system in systems_all_windows) then
  995. Replace(cmdstr,'$FILELIST','-filelist '+maybequoted(outputexedir+'linkfiles.res'))
  996. else
  997. Replace(cmdstr,'$FILELIST','');
  998. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  999. Replace(cmdstr,'$INIT',InitStr);
  1000. Replace(cmdstr,'$FINI',FiniStr);
  1001. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  1002. Replace(cmdstr,'$SONAME',SoNameStr);
  1003. Replace(cmdstr,'$MAP',mapstr);
  1004. Replace(cmdstr,'$LTO',ltostr);
  1005. if ordersymfile<>'' then
  1006. begin
  1007. if target_info.system in systems_darwin then
  1008. Replace(cmdstr,'$ORDERSYMS','-order_file '+maybequoted(ordersymfile))
  1009. else
  1010. Replace(cmdstr,'$ORDERSYMS','--symbol-ordering-file '+maybequoted(ordersymfile))
  1011. end
  1012. else
  1013. Replace(cmdstr,'$ORDERSYMS','');
  1014. if (target_info.system in systems_darwin) then
  1015. Replace(cmdstr,'$PRTOBJ',GetDarwinPrtobjName(true));
  1016. BinStr:=FindUtil(utilsprefix+BinStr);
  1017. { create dsym file? }
  1018. extdbgbinstr:='';
  1019. extdbgcmdstr:='';
  1020. if (target_info.system in systems_darwin) and
  1021. (target_dbg.id in [dbg_dwarf2,dbg_dwarf3]) and
  1022. (cs_link_separate_dbg_file in current_settings.globalswitches) then
  1023. begin
  1024. extdbgbinstr:=FindUtil(utilsprefix+'dsymutil');
  1025. extdbgcmdstr:=maybequoted(current_module.sharedlibfilename);
  1026. end;
  1027. if (target_info.system in systems_darwin) then
  1028. begin
  1029. { exported symbols for darwin }
  1030. if not texportlibunix(exportlib).exportedsymnames.empty then
  1031. begin
  1032. assign(exportedsyms,outputexedir+'linksyms.fpc');
  1033. rewrite(exportedsyms);
  1034. repeat
  1035. writeln(exportedsyms,texportlibunix(exportlib).exportedsymnames.getfirst);
  1036. until texportlibunix(exportlib).exportedsymnames.empty;
  1037. close(exportedsyms);
  1038. cmdstr:=cmdstr+' -exported_symbols_list '+maybequoted(outputexedir)+'linksyms.fpc';
  1039. end;
  1040. end;
  1041. if (LdSupportsNoResponseFile) and
  1042. not(cs_link_nolink in current_settings.globalswitches) then
  1043. begin
  1044. { we have to use a script to use the IFS hack }
  1045. linkscript:=GenerateScript(outputexedir+'ppaslink');
  1046. linkscript.AddLinkCommand(BinStr,CmdStr,'');
  1047. if (extdbgbinstr<>'') then
  1048. linkscript.AddLinkCommand(extdbgbinstr,extdbgcmdstr,'');
  1049. linkscript.WriteToDisk;
  1050. BinStr:=linkscript.fn;
  1051. if not path_absolute(BinStr) then
  1052. if cs_link_on_target in current_settings.globalswitches then
  1053. BinStr:='.'+target_info.dirsep+BinStr
  1054. else
  1055. BinStr:='.'+source_info.dirsep+BinStr;
  1056. CmdStr:='';
  1057. end;
  1058. success:=DoExec(BinStr,cmdstr,true,LdSupportsNoResponseFile);
  1059. if (success and
  1060. (extdbgbinstr<>'') and
  1061. (cs_link_nolink in current_settings.globalswitches)) then
  1062. success:=DoExec(extdbgbinstr,extdbgcmdstr,false,LdSupportsNoResponseFile);
  1063. { Strip the library ? }
  1064. if success and (cs_link_strip in current_settings.globalswitches) then
  1065. begin
  1066. SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  1067. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename));
  1068. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,false,false);
  1069. end;
  1070. { Remove ReponseFile }
  1071. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  1072. begin
  1073. DeleteFile(outputexedir+Info.ResName);
  1074. if ordersymfile<>'' then
  1075. DeleteFile(ordersymfile);
  1076. if LdSupportsNoResponseFile Then
  1077. begin
  1078. DeleteFile(linkscript.fn);
  1079. linkscript.free
  1080. end;
  1081. if (target_info.system in systems_darwin) then
  1082. DeleteFile(outputexedir+'linksyms.fpc');
  1083. end;
  1084. { Remove linkfiles.res }
  1085. if (success) and (LdSupportsNoResponseFile) and (source_info.system in systems_all_windows) then
  1086. DeleteFile(outputexedir+'linkfiles.res');
  1087. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  1088. end;
  1089. {*****************************************************************************
  1090. Initialize
  1091. *****************************************************************************}
  1092. initialization
  1093. RegisterLinker(ld_bsd,TLinkerBSD);
  1094. {$ifdef x86_64}
  1095. RegisterImport(system_x86_64_dragonfly,timportlibbsd);
  1096. RegisterExport(system_x86_64_dragonfly,texportlibbsd);
  1097. RegisterTarget(system_x86_64_dragonfly_info);
  1098. RegisterImport(system_x86_64_freebsd,timportlibbsd);
  1099. RegisterExport(system_x86_64_freebsd,texportlibbsd);
  1100. RegisterTarget(system_x86_64_freebsd_info);
  1101. RegisterImport(system_x86_64_openbsd,timportlibbsd);
  1102. RegisterExport(system_x86_64_openbsd,texportlibbsd);
  1103. RegisterTarget(system_x86_64_openbsd_info);
  1104. RegisterImport(system_x86_64_netbsd,timportlibbsd);
  1105. RegisterExport(system_x86_64_netbsd,texportlibbsd);
  1106. RegisterTarget(system_x86_64_netbsd_info);
  1107. RegisterImport(system_x86_64_darwin,timportlibdarwin);
  1108. RegisterExport(system_x86_64_darwin,texportlibdarwin);
  1109. RegisterTarget(system_x86_64_darwin_info);
  1110. RegisterImport(system_x86_64_iphonesim,timportlibdarwin);
  1111. RegisterExport(system_x86_64_iphonesim,texportlibdarwin);
  1112. RegisterTarget(system_x86_64_iphonesim_info);
  1113. {$endif}
  1114. {$ifdef i386}
  1115. RegisterImport(system_i386_freebsd,timportlibbsd);
  1116. RegisterExport(system_i386_freebsd,texportlibbsd);
  1117. RegisterTarget(system_i386_freebsd_info);
  1118. RegisterImport(system_i386_netbsd,timportlibbsd);
  1119. RegisterExport(system_i386_netbsd,texportlibbsd);
  1120. RegisterTarget(system_i386_netbsd_info);
  1121. RegisterImport(system_i386_openbsd,timportlibbsd);
  1122. RegisterExport(system_i386_openbsd,texportlibbsd);
  1123. RegisterTarget(system_i386_openbsd_info);
  1124. RegisterImport(system_i386_darwin,timportlibdarwin);
  1125. RegisterExport(system_i386_darwin,texportlibdarwin);
  1126. RegisterTarget(system_i386_darwin_info);
  1127. RegisterImport(system_i386_iphonesim,timportlibdarwin);
  1128. RegisterExport(system_i386_iphonesim,texportlibdarwin);
  1129. RegisterTarget(system_i386_iphonesim_info);
  1130. {$endif i386}
  1131. {$ifdef m68k}
  1132. RegisterImport(system_m68k_netbsd,timportlibbsd);
  1133. RegisterExport(system_m68k_netbsd,texportlibbsd);
  1134. RegisterTarget(system_m68k_netbsd_info);
  1135. {$endif m68k}
  1136. {$ifdef powerpc}
  1137. RegisterImport(system_powerpc_darwin,timportlibdarwin);
  1138. RegisterExport(system_powerpc_darwin,texportlibdarwin);
  1139. RegisterTarget(system_powerpc_darwin_info);
  1140. RegisterImport(system_powerpc_netbsd,timportlibbsd);
  1141. RegisterExport(system_powerpc_netbsd,texportlibbsd);
  1142. RegisterTarget(system_powerpc_netbsd_info);
  1143. {$endif powerpc}
  1144. {$ifdef powerpc64}
  1145. RegisterImport(system_powerpc64_darwin,timportlibdarwin);
  1146. RegisterExport(system_powerpc64_darwin,texportlibdarwin);
  1147. RegisterTarget(system_powerpc64_darwin_info);
  1148. {$endif powerpc64}
  1149. {$ifdef arm}
  1150. RegisterImport(system_arm_darwin,timportlibdarwin);
  1151. RegisterExport(system_arm_darwin,texportlibdarwin);
  1152. RegisterTarget(system_arm_darwin_info);
  1153. RegisterImport(system_arm_netbsd,timportlibbsd);
  1154. RegisterExport(system_arm_netbsd,texportlibbsd);
  1155. RegisterTarget(system_arm_netbsd_info);
  1156. {$endif arm}
  1157. {$ifdef aarch64}
  1158. RegisterImport(system_aarch64_darwin,timportlibdarwin);
  1159. RegisterExport(system_aarch64_darwin,texportlibdarwin);
  1160. RegisterTarget(system_aarch64_darwin_info);
  1161. {$endif aarch64}
  1162. RegisterRes(res_elf_info,TWinLikeResourceFile);
  1163. RegisterRes(res_macho_info,TWinLikeResourceFile);
  1164. end.