t_bsd.pas 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121
  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. 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 $FILELIST';
  119. DllCmd[1]:='ld $TARGET $EMUL $OPT -shared -L. -o $EXE $CATRES $FILELIST'
  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 $FILELIST';
  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 $FILELIST';
  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 $FILELIST'
  145. else
  146. DllCmd[1]:='ld $PRTOBJ $TARGET $EMUL $OPT $GCSECTIONS -dynamic -bundle -multiply_defined suppress -L. -o $EXE $CATRES $FILELIST'
  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. result:='';
  337. startupfile:=GetDarwinCrt1ObjName(isdll);
  338. if startupfile<>'' then
  339. begin
  340. if not librarysearchpath.FindFile(startupfile,false,result) then
  341. result:='/usr/lib/'+startupfile;
  342. end;
  343. result:=maybequoted(result);
  344. end;
  345. Function TLinkerBSD.WriteResponseFile(isdll:boolean) : Boolean;
  346. Var
  347. linkres : TLinkRes;
  348. FilesList : 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. { Generate linkfiles.res file if needed }
  544. { Only needed on Windows, due to the limitation of 8196 characters for command line }
  545. if (LdSupportsNoResponseFile) and
  546. (source_info.system in systems_all_windows) then
  547. begin
  548. FilesList:=TLinkRes.Create(outputexedir+'linkfiles.res',false);
  549. while not ObjectFiles.Empty do
  550. begin
  551. s:=ObjectFiles.GetFirst;
  552. if s<>'' then
  553. begin
  554. repeat
  555. i:=Pos(source_info.dirsep,s);
  556. if i>0 then
  557. s[i]:=target_info.dirsep;
  558. until i=0;
  559. FilesList.Add(s);
  560. end;
  561. end;
  562. FilesList.writetodisk;
  563. FilesList.Free;
  564. end
  565. else
  566. begin
  567. while not ObjectFiles.Empty do
  568. begin
  569. s:=ObjectFiles.GetFirst;
  570. if s<>'' then
  571. if LdSupportsNoResponseFile then
  572. LinkRes.AddFileName(s)
  573. else
  574. LinkRes.AddFileName(maybequoted(s));
  575. end;
  576. end;
  577. if not LdSupportsNoResponseFile then
  578. LinkRes.Add(')');
  579. { Write staticlibraries }
  580. if not StaticLibFiles.Empty then
  581. begin
  582. if not LdSupportsNoResponseFile then
  583. LinkRes.Add('GROUP(');
  584. While not StaticLibFiles.Empty do
  585. begin
  586. S:=StaticLibFiles.GetFirst;
  587. if LdSupportsNoResponseFile then
  588. LinkRes.AddFileName(s)
  589. else
  590. LinkRes.AddFileName(maybequoted(s))
  591. end;
  592. if not LdSupportsNoResponseFile then
  593. LinkRes.Add(')');
  594. end;
  595. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  596. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  597. if not SharedLibFiles.Empty then
  598. begin
  599. if not LdSupportsNoResponseFile then
  600. LinkRes.Add('INPUT(');
  601. While not SharedLibFiles.Empty do
  602. begin
  603. S:=SharedLibFiles.GetFirst;
  604. if (s<>'c') or reorder then
  605. begin
  606. i:=Pos(target_info.sharedlibext,S);
  607. if i>0 then
  608. Delete(S,i,255);
  609. LinkRes.Add('-l'+s);
  610. end
  611. else
  612. begin
  613. linklibc:=true;
  614. linkdynamic:=false; { libc will include the ld-* for us }
  615. end;
  616. end;
  617. { be sure that libc is the last lib }
  618. if linklibc and not reorder then
  619. Begin
  620. If LibrarySuffix=' ' Then
  621. LinkRes.Add('-lc')
  622. else
  623. LinkRes.Add('-lc_'+LibrarySuffix);
  624. If LibrarySuffix='r' Then
  625. LinkRes.Add('-lc');
  626. end;
  627. { when we have -static for the linker the we also need libgcc }
  628. if (cs_link_staticflag in current_settings.globalswitches) then
  629. LinkRes.Add('-lgcc');
  630. if linkdynamic and (Info.DynamicLinker<>'') then
  631. LinkRes.AddFileName(Info.DynamicLinker);
  632. if not LdSupportsNoResponseFile then
  633. LinkRes.Add(')');
  634. end;
  635. { frameworks for Darwin }
  636. if IsDarwin then
  637. while not FrameworkFiles.empty do
  638. begin
  639. LinkRes.Add('-framework');
  640. LinkRes.Add(FrameworkFiles.GetFirst);
  641. end;
  642. { objects which must be at the end }
  643. if linklibc and
  644. not IsDarwin Then
  645. begin
  646. if cs_create_pic in current_settings.moduleswitches then
  647. Fl1:=librarysearchpath.FindFile('crtendS.o',false,s1)
  648. else
  649. Fl1:=librarysearchpath.FindFile('crtend.o',false,s1);
  650. Fl2:=librarysearchpath.FindFile('crtn.o',false,s2);
  651. if Fl1 or Fl2 then
  652. begin
  653. LinkRes.Add('INPUT(');
  654. If Fl1 Then
  655. LinkRes.AddFileName(s1);
  656. If Fl2 Then
  657. LinkRes.AddFileName(s2);
  658. LinkRes.Add(')');
  659. end;
  660. end;
  661. { Write and Close response }
  662. linkres.writetodisk;
  663. linkres.Free;
  664. WriteResponseFile:=True;
  665. end;
  666. function TLinkerBSD.MakeExecutable:boolean;
  667. var
  668. binstr,
  669. cmdstr,
  670. targetstr,
  671. emulstr,
  672. extdbgbinstr,
  673. extdbgcmdstr: TCmdStr;
  674. linkscript: TAsmScript;
  675. DynLinkStr : string[60];
  676. GCSectionsStr,
  677. StaticStr,
  678. StripStr : string[63];
  679. success,
  680. useshell : boolean;
  681. begin
  682. if not(cs_link_nolink in current_settings.globalswitches) then
  683. Message1(exec_i_linking,current_module.exefilename);
  684. { Create some replacements }
  685. StaticStr:='';
  686. StripStr:='';
  687. DynLinkStr:='';
  688. GCSectionsStr:='';
  689. linkscript:=nil;
  690. { i386_freebsd needs -b elf32-i386-freebsd and -m elf_i386_fbsd
  691. to avoid creation of a i386:x86_64 arch binary }
  692. if target_info.system=system_i386_freebsd then
  693. begin
  694. targetstr:='-b elf32-i386-freebsd';
  695. emulstr:='-m elf_i386_fbsd';
  696. end
  697. else
  698. begin
  699. targetstr:='';
  700. emulstr:='';
  701. end;
  702. if (cs_link_staticflag in current_settings.globalswitches) then
  703. begin
  704. if (target_info.system=system_m68k_netbsd) and
  705. ((cs_link_on_target in current_settings.globalswitches) or
  706. (target_info.system=source_info.system)) then
  707. StaticStr:='-Bstatic'
  708. else
  709. StaticStr:='-static';
  710. end;
  711. if (cs_link_strip in current_settings.globalswitches) then
  712. if (target_info.system in systems_darwin) then
  713. StripStr:='-x'
  714. else
  715. StripStr:='-s';
  716. if (cs_link_smart in current_settings.globalswitches) and
  717. (tf_smartlink_sections in target_info.flags) then
  718. if not(target_info.system in systems_darwin) then
  719. GCSectionsStr:='--gc-sections'
  720. else
  721. GCSectionsStr:='-dead_strip -no_dead_strip_inits_and_terms';
  722. if(not(target_info.system in systems_darwin) and
  723. (cs_profile in current_settings.moduleswitches)) or
  724. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty)) then
  725. DynLinkStr:='-dynamic-linker='+Info.DynamicLinker;
  726. if CShared Then
  727. begin
  728. if not(target_info.system in systems_darwin) then
  729. DynLinKStr:=DynLinkStr+' --shared'
  730. else
  731. DynLinKStr:=DynLinkStr+' -dynamic'; // one dash!
  732. end;
  733. { Use -nopie on OpenBSD }
  734. if (target_info.system in systems_openbsd) and
  735. (target_info.system <> system_x86_64_openbsd) then
  736. Info.ExtraOptions:=Info.ExtraOptions+' -nopie';
  737. { -N seems to be needed on NetBSD/earm }
  738. if (target_info.system in [system_arm_netbsd]) then
  739. Info.ExtraOptions:=Info.ExtraOptions+' -N';
  740. { Write used files and libraries }
  741. WriteResponseFile(false);
  742. { Call linker }
  743. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  744. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename));
  745. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  746. Replace(cmdstr,'$TARGET',targetstr);
  747. Replace(cmdstr,'$EMUL',EmulStr);
  748. Replace(cmdstr,'$CATRES',CatFileContent(outputexedir+Info.ResName));
  749. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  750. if (LdSupportsNoResponseFile) and (source_info.system in systems_all_windows) then
  751. Replace(cmdstr,'$FILELIST','-filelist '+maybequoted(outputexedir+'linkfiles.res'))
  752. else
  753. Replace(cmdstr,'$FILELIST','');
  754. Replace(cmdstr,'$STATIC',StaticStr);
  755. Replace(cmdstr,'$STRIP',StripStr);
  756. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  757. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  758. if (target_info.system in systems_darwin) then
  759. Replace(cmdstr,'$PRTOBJ',GetDarwinPrtobjName(false));
  760. BinStr:=FindUtil(utilsprefix+BinStr);
  761. { create dsym file? }
  762. extdbgbinstr:='';
  763. extdbgcmdstr:='';
  764. if (target_info.system in systems_darwin) and
  765. (target_dbg.id in [dbg_dwarf2,dbg_dwarf3]) and
  766. (cs_link_separate_dbg_file in current_settings.globalswitches) then
  767. begin
  768. extdbgbinstr:=FindUtil(utilsprefix+'dsymutil');
  769. extdbgcmdstr:=maybequoted(current_module.exefilename);
  770. end;
  771. if (LdSupportsNoResponseFile) and
  772. not(cs_link_nolink in current_settings.globalswitches) then
  773. begin
  774. { we have to use a script to use the IFS hack }
  775. linkscript:=GenerateScript(outputexedir+'ppaslink');
  776. linkscript.AddLinkCommand(BinStr,CmdStr,'');
  777. if (extdbgcmdstr<>'') then
  778. linkscript.AddLinkCommand(extdbgbinstr,extdbgcmdstr,'');
  779. linkscript.WriteToDisk;
  780. BinStr:=linkscript.fn;
  781. if not path_absolute(BinStr) then
  782. if cs_link_on_target in current_settings.globalswitches then
  783. BinStr:='.'+target_info.dirsep+BinStr
  784. else
  785. BinStr:='.'+source_info.dirsep+BinStr;
  786. CmdStr:='';
  787. end;
  788. useshell:=not (tf_no_backquote_support in source_info.flags);
  789. success:=DoExec(BinStr,CmdStr,true,LdSupportsNoResponseFile or useshell);
  790. if (success and
  791. (extdbgbinstr<>'') and
  792. (cs_link_nolink in current_settings.globalswitches)) then
  793. success:=DoExec(extdbgbinstr,extdbgcmdstr,false,LdSupportsNoResponseFile);
  794. { Remove ReponseFile }
  795. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  796. begin
  797. DeleteFile(outputexedir+Info.ResName);
  798. if LdSupportsNoResponseFile Then
  799. begin
  800. DeleteFile(linkscript.fn);
  801. linkscript.free
  802. end;
  803. end;
  804. { Remove linkfiles.res }
  805. if (success) and (LdSupportsNoResponseFile) and (source_info.system in systems_all_windows) then
  806. DeleteFile(outputexedir+'linkfiles.res');
  807. MakeExecutable:=success; { otherwise a recursive call to link method }
  808. end;
  809. Function TLinkerBSD.MakeSharedLibrary:boolean;
  810. var
  811. InitStr,
  812. FiniStr,
  813. SoNameStr : string[80];
  814. linkscript: TAsmScript;
  815. binstr,
  816. cmdstr,
  817. targetstr,
  818. emulstr,
  819. extdbgbinstr,
  820. extdbgcmdstr : TCmdStr;
  821. GCSectionsStr : string[63];
  822. exportedsyms: text;
  823. success : boolean;
  824. begin
  825. MakeSharedLibrary:=false;
  826. GCSectionsStr:='';
  827. linkscript:=nil;
  828. if not(cs_link_nolink in current_settings.globalswitches) then
  829. Message1(exec_i_linking,current_module.sharedlibfilename);
  830. { Write used files and libraries }
  831. WriteResponseFile(true);
  832. if (cs_link_smart in current_settings.globalswitches) and
  833. (tf_smartlink_sections in target_info.flags) then
  834. if not(target_info.system in systems_darwin) then
  835. { disabled because not tested
  836. GCSectionsStr:='--gc-sections' }
  837. else
  838. GCSectionsStr:='-dead_strip -no_dead_strip_inits_and_terms';
  839. { i386_freebsd needs -b elf32-i386-freebsd and -m elf_i386_fbsd
  840. to avoid creation of a i386:x86_64 arch binary }
  841. if target_info.system=system_i386_freebsd then
  842. begin
  843. targetstr:='-b elf32-i386-freebsd';
  844. emulstr:='-m elf_i386_fbsd';
  845. end
  846. else
  847. begin
  848. targetstr:='';
  849. emulstr:='';
  850. end;
  851. InitStr:='-init FPC_LIB_START';
  852. FiniStr:='-fini FPC_LIB_EXIT';
  853. SoNameStr:='-soname '+ExtractFileName(current_module.sharedlibfilename);
  854. { Call linker }
  855. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  856. {$ifndef darwin}
  857. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename));
  858. {$else darwin}
  859. Replace(cmdstr,'$EXE',maybequoted(ExpandFileName(current_module.sharedlibfilename)));
  860. {$endif darwin}
  861. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  862. Replace(cmdstr,'$TARGET',targetstr);
  863. Replace(cmdstr,'$EMUL',EmulStr);
  864. Replace(cmdstr,'$CATRES',CatFileContent(outputexedir+Info.ResName));
  865. if (LdSupportsNoResponseFile) and (source_info.system in systems_all_windows) then
  866. Replace(cmdstr,'$FILELIST','-filelist '+maybequoted(outputexedir+'linkfiles.res'))
  867. else
  868. Replace(cmdstr,'$FILELIST','');
  869. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  870. Replace(cmdstr,'$INIT',InitStr);
  871. Replace(cmdstr,'$FINI',FiniStr);
  872. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  873. Replace(cmdstr,'$SONAME',SoNameStr);
  874. if (target_info.system in systems_darwin) then
  875. Replace(cmdstr,'$PRTOBJ',GetDarwinPrtobjName(true));
  876. BinStr:=FindUtil(utilsprefix+BinStr);
  877. { create dsym file? }
  878. extdbgbinstr:='';
  879. extdbgcmdstr:='';
  880. if (target_info.system in systems_darwin) and
  881. (target_dbg.id in [dbg_dwarf2,dbg_dwarf3]) and
  882. (cs_link_separate_dbg_file in current_settings.globalswitches) then
  883. begin
  884. extdbgbinstr:=FindUtil(utilsprefix+'dsymutil');
  885. extdbgcmdstr:=maybequoted(current_module.sharedlibfilename);
  886. end;
  887. if (target_info.system in systems_darwin) then
  888. begin
  889. { exported symbols for darwin }
  890. if not texportlibunix(exportlib).exportedsymnames.empty then
  891. begin
  892. assign(exportedsyms,outputexedir+'linksyms.fpc');
  893. rewrite(exportedsyms);
  894. repeat
  895. writeln(exportedsyms,texportlibunix(exportlib).exportedsymnames.getfirst);
  896. until texportlibunix(exportlib).exportedsymnames.empty;
  897. close(exportedsyms);
  898. cmdstr:=cmdstr+' -exported_symbols_list '+maybequoted(outputexedir)+'linksyms.fpc';
  899. end;
  900. end;
  901. if (LdSupportsNoResponseFile) and
  902. not(cs_link_nolink in current_settings.globalswitches) then
  903. begin
  904. { we have to use a script to use the IFS hack }
  905. linkscript:=GenerateScript(outputexedir+'ppaslink');
  906. linkscript.AddLinkCommand(BinStr,CmdStr,'');
  907. if (extdbgbinstr<>'') then
  908. linkscript.AddLinkCommand(extdbgbinstr,extdbgcmdstr,'');
  909. linkscript.WriteToDisk;
  910. BinStr:=linkscript.fn;
  911. if not path_absolute(BinStr) then
  912. if cs_link_on_target in current_settings.globalswitches then
  913. BinStr:='.'+target_info.dirsep+BinStr
  914. else
  915. BinStr:='.'+source_info.dirsep+BinStr;
  916. CmdStr:='';
  917. end;
  918. success:=DoExec(BinStr,cmdstr,true,LdSupportsNoResponseFile);
  919. if (success and
  920. (extdbgbinstr<>'') and
  921. (cs_link_nolink in current_settings.globalswitches)) then
  922. success:=DoExec(extdbgbinstr,extdbgcmdstr,false,LdSupportsNoResponseFile);
  923. { Strip the library ? }
  924. if success and (cs_link_strip in current_settings.globalswitches) then
  925. begin
  926. SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  927. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename));
  928. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,false,false);
  929. end;
  930. { Remove ReponseFile }
  931. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  932. begin
  933. DeleteFile(outputexedir+Info.ResName);
  934. if LdSupportsNoResponseFile Then
  935. begin
  936. DeleteFile(linkscript.fn);
  937. linkscript.free
  938. end;
  939. if (target_info.system in systems_darwin) then
  940. DeleteFile(outputexedir+'linksyms.fpc');
  941. end;
  942. { Remove linkfiles.res }
  943. if (success) and (LdSupportsNoResponseFile) and (source_info.system in systems_all_windows) then
  944. DeleteFile(outputexedir+'linkfiles.res');
  945. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  946. end;
  947. {*****************************************************************************
  948. Initialize
  949. *****************************************************************************}
  950. initialization
  951. RegisterLinker(ld_bsd,TLinkerBSD);
  952. {$ifdef x86_64}
  953. RegisterImport(system_x86_64_dragonfly,timportlibbsd);
  954. RegisterExport(system_x86_64_dragonfly,texportlibbsd);
  955. RegisterTarget(system_x86_64_dragonfly_info);
  956. RegisterImport(system_x86_64_freebsd,timportlibbsd);
  957. RegisterExport(system_x86_64_freebsd,texportlibbsd);
  958. RegisterTarget(system_x86_64_freebsd_info);
  959. RegisterImport(system_x86_64_openbsd,timportlibbsd);
  960. RegisterExport(system_x86_64_openbsd,texportlibbsd);
  961. RegisterTarget(system_x86_64_openbsd_info);
  962. RegisterImport(system_x86_64_netbsd,timportlibbsd);
  963. RegisterExport(system_x86_64_netbsd,texportlibbsd);
  964. RegisterTarget(system_x86_64_netbsd_info);
  965. RegisterImport(system_x86_64_darwin,timportlibdarwin);
  966. RegisterExport(system_x86_64_darwin,texportlibdarwin);
  967. RegisterTarget(system_x86_64_darwin_info);
  968. RegisterImport(system_x86_64_iphonesim,timportlibdarwin);
  969. RegisterExport(system_x86_64_iphonesim,texportlibdarwin);
  970. RegisterTarget(system_x86_64_iphonesim_info);
  971. {$endif}
  972. {$ifdef i386}
  973. RegisterImport(system_i386_freebsd,timportlibbsd);
  974. RegisterExport(system_i386_freebsd,texportlibbsd);
  975. RegisterTarget(system_i386_freebsd_info);
  976. RegisterImport(system_i386_netbsd,timportlibbsd);
  977. RegisterExport(system_i386_netbsd,texportlibbsd);
  978. RegisterTarget(system_i386_netbsd_info);
  979. RegisterImport(system_i386_openbsd,timportlibbsd);
  980. RegisterExport(system_i386_openbsd,texportlibbsd);
  981. RegisterTarget(system_i386_openbsd_info);
  982. RegisterImport(system_i386_darwin,timportlibdarwin);
  983. RegisterExport(system_i386_darwin,texportlibdarwin);
  984. RegisterTarget(system_i386_darwin_info);
  985. RegisterImport(system_i386_iphonesim,timportlibdarwin);
  986. RegisterExport(system_i386_iphonesim,texportlibdarwin);
  987. RegisterTarget(system_i386_iphonesim_info);
  988. {$endif i386}
  989. {$ifdef m68k}
  990. RegisterImport(system_m68k_netbsd,timportlibbsd);
  991. RegisterExport(system_m68k_netbsd,texportlibbsd);
  992. RegisterTarget(system_m68k_netbsd_info);
  993. {$endif m68k}
  994. {$ifdef powerpc}
  995. RegisterImport(system_powerpc_darwin,timportlibdarwin);
  996. RegisterExport(system_powerpc_darwin,texportlibdarwin);
  997. RegisterTarget(system_powerpc_darwin_info);
  998. RegisterImport(system_powerpc_netbsd,timportlibbsd);
  999. RegisterExport(system_powerpc_netbsd,texportlibbsd);
  1000. RegisterTarget(system_powerpc_netbsd_info);
  1001. {$endif powerpc}
  1002. {$ifdef powerpc64}
  1003. RegisterImport(system_powerpc64_darwin,timportlibdarwin);
  1004. RegisterExport(system_powerpc64_darwin,texportlibdarwin);
  1005. RegisterTarget(system_powerpc64_darwin_info);
  1006. {$endif powerpc64}
  1007. {$ifdef arm}
  1008. RegisterImport(system_arm_darwin,timportlibdarwin);
  1009. RegisterExport(system_arm_darwin,texportlibdarwin);
  1010. RegisterTarget(system_arm_darwin_info);
  1011. RegisterImport(system_arm_netbsd,timportlibbsd);
  1012. RegisterExport(system_arm_netbsd,texportlibbsd);
  1013. RegisterTarget(system_arm_netbsd_info);
  1014. {$endif arm}
  1015. {$ifdef aarch64}
  1016. RegisterImport(system_aarch64_darwin,timportlibdarwin);
  1017. RegisterExport(system_aarch64_darwin,texportlibdarwin);
  1018. RegisterTarget(system_aarch64_darwin_info);
  1019. {$endif aarch64}
  1020. RegisterRes(res_elf_info,TWinLikeResourceFile);
  1021. RegisterRes(res_macho_info,TWinLikeResourceFile);
  1022. end.