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