t_bsd.pas 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  1. {
  2. Copyright (c) 1998-2002 by Peter Vreman (original Linux)
  3. (c) 2000 by Marco van de Voort (FreeBSD mods)
  4. This unit implements support import,export,link routines
  5. for the (i386)FreeBSD target
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************
  18. }
  19. unit t_bsd;
  20. {$i fpcdefs.inc}
  21. interface
  22. implementation
  23. uses
  24. sysutils,
  25. cutils,cfileutl,cclasses,
  26. verbose,systems,globtype,globals,
  27. symconst,script,
  28. fmodule,aasmbase,aasmtai,aasmdata,aasmcpu,cpubase,symsym,symdef,
  29. import,export,link,comprsrc,rescmn,i_bsd,expunix,
  30. cgutils,cgbase,cgobj,cpuinfo,ogbase;
  31. type
  32. timportlibdarwin=class(timportlib)
  33. procedure generatelib;override;
  34. end;
  35. timportlibbsd=class(timportlib)
  36. procedure generatelib;override;
  37. end;
  38. texportlibbsd=class(texportlibunix)
  39. end;
  40. texportlibdarwin=class(texportlibbsd)
  41. procedure setinitname(list: TAsmList; const s: string); override;
  42. procedure setfininame(list: TAsmList; const s: string); override;
  43. end;
  44. tlinkerbsd=class(texternallinker)
  45. private
  46. LdSupportsNoResponseFile : boolean;
  47. LibrarySuffix : Char;
  48. Function WriteResponseFile(isdll:boolean) : Boolean;
  49. function GetDarwinCrt1ObjName(isdll: boolean): TCmdStr;
  50. Function GetDarwinPrtobjName(isdll: boolean): TCmdStr;
  51. public
  52. constructor Create;override;
  53. procedure SetDefaultInfo;override;
  54. function MakeExecutable:boolean;override;
  55. function MakeSharedLibrary:boolean;override;
  56. procedure LoadPredefinedLibraryOrder; override;
  57. end;
  58. {*****************************************************************************
  59. TIMPORTLIBDARWIN
  60. *****************************************************************************}
  61. procedure timportlibdarwin.generatelib;
  62. begin
  63. end;
  64. {*****************************************************************************
  65. TEXPORTLIBDARWIN
  66. *****************************************************************************}
  67. procedure texportlibdarwin.setinitname(list: TAsmList; const s: string);
  68. begin
  69. new_section(list,sec_init_func,'',sizeof(pint));
  70. list.concat(Tai_const.Createname(s,0));
  71. end;
  72. procedure texportlibdarwin.setfininame(list: TAsmList; const s: string);
  73. begin
  74. new_section(list,sec_term_func,'',sizeof(pint));
  75. list.concat(Tai_const.Createname(s,0));
  76. end;
  77. {*****************************************************************************
  78. TIMPORTLIBBSD
  79. *****************************************************************************}
  80. procedure timportlibbsd.generatelib;
  81. var
  82. i : longint;
  83. ImportLibrary : TImportLibrary;
  84. begin
  85. for i:=0 to current_module.ImportLibraryList.Count-1 do
  86. begin
  87. ImportLibrary:=TImportLibrary(current_module.ImportLibraryList[i]);
  88. current_module.linkothersharedlibs.add(ImportLibrary.Name,link_always);
  89. end;
  90. end;
  91. {*****************************************************************************
  92. TLINKERBSD
  93. *****************************************************************************}
  94. Constructor TLinkerBSD.Create;
  95. begin
  96. Inherited Create;
  97. if not Dontlinkstdlibpath Then
  98. if not(target_info.system in systems_darwin) then
  99. LibrarySearchPath.AddPath(sysrootpath,'/lib;/usr/lib;/usr/X11R6/lib',true)
  100. else
  101. { Mac OS X doesn't have a /lib }
  102. LibrarySearchPath.AddPath(sysrootpath,'/usr/lib',true)
  103. end;
  104. procedure TLinkerBSD.SetDefaultInfo;
  105. {
  106. This will also detect which libc version will be used
  107. }
  108. begin
  109. LibrarySuffix:=' ';
  110. LdSupportsNoResponseFile := (target_info.system in ([system_m68k_netbsd]+systems_darwin));
  111. with Info do
  112. begin
  113. if LdSupportsNoResponseFile then
  114. begin
  115. if not(target_info.system in systems_darwin) then
  116. begin
  117. ExeCmd[1]:='ld $TARGET $EMUL $OPT $DYNLINK $STATIC $GCSECTIONS $STRIP -L. -o $EXE $CATRES';
  118. DllCmd[1]:='ld $TARGET $EMUL $OPT -shared -L. -o $EXE $CATRES'
  119. end
  120. else
  121. begin
  122. {$ifndef cpu64bitaddr}
  123. { Set the size of the page at address zero to 64kb, so nothing
  124. is loaded below that address. This avoids problems with the
  125. strange Windows-compatible resource handling that assumes
  126. that addresses below 64kb do not exist.
  127. On 64bit systems, page zero is 4GB by default, so no problems
  128. there.
  129. }
  130. { In case of valgrind, don't do that, because it cannot deal with
  131. a custom pagezero size -- in general, this should not cause any
  132. problems because the resources are added at the end and most
  133. programs with problems that require Valgrind will have more
  134. than 60KB of data (first 4KB of address space is always invalid)
  135. }
  136. ExeCmd[1]:='ld $PRTOBJ $TARGET $EMUL $OPT $DYNLINK $STATIC $GCSECTIONS $STRIP -multiply_defined suppress -L. -o $EXE $CATRES';
  137. if not(cs_gdb_valgrind in current_settings.globalswitches) then
  138. ExeCmd[1]:=ExeCmd[1]+' -pagezero_size 0x10000';
  139. {$else ndef cpu64bitaddr}
  140. ExeCmd[1]:='ld $PRTOBJ $TARGET $EMUL $OPT $DYNLINK $STATIC $GCSECTIONS $STRIP -multiply_defined suppress -L. -o $EXE $CATRES';
  141. {$endif ndef cpu64bitaddr}
  142. if (apptype<>app_bundle) then
  143. DllCmd[1]:='ld $PRTOBJ $TARGET $EMUL $OPT $GCSECTIONS -dynamic -dylib -multiply_defined suppress -L. -o $EXE $CATRES'
  144. else
  145. DllCmd[1]:='ld $PRTOBJ $TARGET $EMUL $OPT $GCSECTIONS -dynamic -bundle -multiply_defined suppress -L. -o $EXE $CATRES'
  146. end
  147. end
  148. else
  149. begin
  150. ExeCmd[1]:='ld $TARGET $EMUL $OPT $DYNLINK $STATIC $GCSECTIONS $STRIP -L. -o $EXE $RES';
  151. DllCmd[1]:='ld $TARGET $EMUL $OPT $INIT $FINI $SONAME -shared -L. -o $EXE $RES';
  152. end;
  153. if not(target_info.system in systems_darwin) then
  154. DllCmd[2]:='strip --strip-unneeded $EXE'
  155. else
  156. DllCmd[2]:='strip -x $EXE';
  157. { OpenBSD seems to use a wrong dynamic linker by default }
  158. if target_info.system in systems_openbsd then
  159. DynamicLinker:='/usr/libexec/ld.so'
  160. else if target_info.system in systems_netbsd then
  161. DynamicLinker:='/usr/libexec/ld.elf_so'
  162. else if target_info.system=system_x86_64_dragonfly then
  163. DynamicLinker:='/libexec/ld-elf.so.2'
  164. else
  165. DynamicLinker:='';
  166. end;
  167. end;
  168. procedure TLinkerBSD.LoadPredefinedLibraryOrder;
  169. // put your linkorder/linkalias overrides here.
  170. // Note: assumes only called when reordering/aliasing is used.
  171. Begin
  172. if not(target_info.system in systems_darwin) then
  173. begin
  174. if (target_info.system =system_i386_freebsd) and
  175. not (cs_link_no_default_lib_order in current_settings.globalswitches) Then
  176. Begin
  177. LinkLibraryOrder.add('gcc','',15);
  178. LinkLibraryOrder.add('c','',50); // c and c_p mutual. excl?
  179. LinkLibraryOrder.add('c_p','',55);
  180. LinkLibraryOrder.add('pthread','',75); // pthread and c_r should be mutually exclusive
  181. LinkLibraryOrder.add('c_r','',76);
  182. LinkLibraryOrder.add('kvm','',80); // must be before ncurses
  183. if (cs_link_pthread in current_settings.globalswitches) Then // convert libpthread to libc_r.
  184. LinkLibraryAliases.add('pthread','c_r');
  185. end;
  186. end
  187. else
  188. begin
  189. LinkLibraryOrder.add('gcc','',15);
  190. LinkLibraryOrder.add('c','',50);
  191. end;
  192. End;
  193. function TLinkerBSD.GetDarwinCrt1ObjName(isdll: boolean): TCmdStr;
  194. begin
  195. if not isdll then
  196. begin
  197. if not(cs_profile in current_settings.moduleswitches) then
  198. begin
  199. case target_info.system of
  200. system_powerpc_darwin,
  201. system_powerpc64_darwin,
  202. system_i386_darwin,
  203. system_x86_64_darwin:
  204. begin
  205. { 10.8 and later: no crt1.* }
  206. if CompareVersionStrings(MacOSXVersionMin,'10.8')>=0 then
  207. exit('');
  208. { x86: crt1.10.6.o -> crt1.10.5.o -> crt1.o }
  209. { others: crt1.10.5 -> crt1.o }
  210. if (target_info.system in [system_i386_darwin,system_x86_64_darwin]) and
  211. (CompareVersionStrings(MacOSXVersionMin,'10.6')>=0) then
  212. exit('crt1.10.6.o');
  213. if CompareVersionStrings(MacOSXVersionMin,'10.5')>=0 then
  214. exit('crt1.10.5.o');
  215. end;
  216. system_arm_darwin:
  217. begin
  218. { iOS:
  219. iOS 6 and later: nothing
  220. iOS 3.1 - 5.x: crt1.3.1.o
  221. pre-iOS 3.1: crt1.o
  222. }
  223. if (CompareVersionStrings(iPhoneOSVersionMin,'6.0')>=0) then
  224. exit('');
  225. if (CompareVersionStrings(iPhoneOSVersionMin,'3.1')>=0) then
  226. exit('crt1.3.1.o');
  227. end;
  228. system_i386_iphonesim,
  229. system_x86_64_iphonesim:
  230. begin
  231. { "recent versions" must not use anything (https://github.com/llvm-mirror/clang/commit/e6d04f3d152a22077022cf9287d4c538a0918ab0 )
  232. What those recent versions could be, is anyone's guess. It
  233. still seems to work with 8.1 and no longer with 8.3, so use
  234. 8.1 as a cut-off point }
  235. if (CompareVersionStrings(iPhoneOSVersionMin,'8.1')>0) then
  236. exit('');
  237. end;
  238. system_aarch64_darwin:
  239. { never anything }
  240. exit('');
  241. end;
  242. { nothing special -> default }
  243. result:='crt1.o';
  244. end
  245. else
  246. begin
  247. result:='gcrt1.o';
  248. { 10.8 and later: tell the linker to use 'start' instead of "_main"
  249. as entry point }
  250. if CompareVersionStrings(MacOSXVersionMin,'10.8')>=0 then
  251. Info.ExeCmd[1]:=Info.ExeCmd[1]+' -no_new_main';
  252. end;
  253. end
  254. else
  255. begin
  256. if (apptype=app_bundle) then
  257. begin
  258. case target_info.system of
  259. system_powerpc_darwin,
  260. system_powerpc64_darwin,
  261. system_i386_darwin,
  262. system_x86_64_darwin:
  263. begin
  264. { < 10.6: bundle1.o
  265. >= 10.6: nothing }
  266. if CompareVersionStrings(MacOSXVersionMin,'10.6')>=0 then
  267. exit('');
  268. end;
  269. system_arm_darwin,
  270. system_aarch64_darwin:
  271. begin
  272. { iOS: < 3.1: bundle1.o
  273. >= 3.1: nothing }
  274. if (CompareVersionStrings(iPhoneOSVersionMin,'3.1')>=0) then
  275. exit('');
  276. end;
  277. system_i386_iphonesim,
  278. system_x86_64_iphonesim:
  279. begin
  280. { see rule for crt1.o }
  281. if (CompareVersionStrings(iPhoneOSVersionMin,'8.1')>0) then
  282. exit('');
  283. end;
  284. end;
  285. result:='bundle1.o';
  286. end
  287. else
  288. begin
  289. case target_info.system of
  290. system_powerpc_darwin,
  291. system_powerpc64_darwin,
  292. system_i386_darwin,
  293. system_x86_64_darwin:
  294. begin
  295. { >= 10.6: nothing
  296. = 10.5: dylib1.10.5.o
  297. < 10.5: dylib1.o
  298. }
  299. if CompareVersionStrings(MacOSXVersionMin,'10.6')>=0 then
  300. exit('');
  301. if CompareVersionStrings(MacOSXVersionMin,'10.5')>=0 then
  302. exit('dylib1.10.5.o');
  303. end;
  304. system_arm_darwin,
  305. system_aarch64_darwin:
  306. begin
  307. { iOS: < 3.1: dylib1.o
  308. >= 3.1: nothing }
  309. if (CompareVersionStrings(iPhoneOSVersionMin,'3.1')>=0) then
  310. exit('');
  311. end;
  312. system_i386_iphonesim,
  313. system_x86_64_iphonesim:
  314. begin
  315. { see rule for crt1.o }
  316. if (CompareVersionStrings(iPhoneOSVersionMin,'8.1')>0) then
  317. exit('');
  318. end;
  319. end;
  320. result:='dylib1.o';
  321. end;
  322. end;
  323. end;
  324. Function TLinkerBSD.GetDarwinPrtobjName(isdll: boolean): TCmdStr;
  325. var
  326. startupfile: TCmdStr;
  327. begin
  328. startupfile:=GetDarwinCrt1ObjName(isdll);
  329. if startupfile<>'' then
  330. begin
  331. if not librarysearchpath.FindFile(startupfile,false,result) then
  332. result:='/usr/lib/'+startupfile
  333. end
  334. else
  335. result:='';
  336. result:=maybequoted(result);
  337. end;
  338. Function TLinkerBSD.WriteResponseFile(isdll:boolean) : Boolean;
  339. Var
  340. linkres : TLinkRes;
  341. i : longint;
  342. cprtobj,
  343. gprtobj,
  344. prtobj : string[80];
  345. HPath : TCmdStrListItem;
  346. s,s1,s2 : TCmdStr;
  347. linkdynamic,
  348. linklibc : boolean;
  349. Fl1,Fl2 : Boolean;
  350. IsDarwin : Boolean;
  351. ReOrder : Boolean;
  352. begin
  353. WriteResponseFile:=False;
  354. ReOrder:=False;
  355. linkdynamic:=False;
  356. IsDarwin:=target_info.system in systems_darwin;
  357. { set special options for some targets }
  358. if not IsDarwin Then
  359. begin
  360. if isdll and
  361. (target_info.system in systems_bsd) then
  362. begin
  363. prtobj:='dllprt0';
  364. cprtobj:='dllprt0';
  365. gprtobj:='dllprt0';
  366. end
  367. else
  368. begin
  369. prtobj:='prt0';
  370. cprtobj:='cprt0';
  371. gprtobj:='gprt0';
  372. end;
  373. linkdynamic:=not(SharedLibFiles.empty);
  374. linklibc:=(SharedLibFiles.Find('c')<>nil);
  375. // this one is a bit complex.
  376. // Only reorder for now if -XL or -XO params are given
  377. // or when -Xf.
  378. reorder:= linklibc and
  379. (
  380. ReorderEntries
  381. or
  382. (cs_link_pthread in current_settings.globalswitches));
  383. if cs_profile in current_settings.moduleswitches then
  384. begin
  385. prtobj:=gprtobj;
  386. AddSharedLibrary('c');
  387. LibrarySuffix:='p';
  388. linklibc:=true;
  389. end
  390. else
  391. begin
  392. if linklibc then
  393. prtobj:=cprtobj;
  394. end;
  395. // after this point addition of shared libs not allowed.
  396. end
  397. else
  398. begin
  399. { for darwin: always link dynamically against libc }
  400. linklibc := true;
  401. {$ifdef MACOSX104ORHIGHER}
  402. { not sure what this is for, but gcc always links against it }
  403. if not(cs_profile in current_settings.moduleswitches) then
  404. AddSharedLibrary('SystemStubs')
  405. else
  406. AddSharedLibrary('SystemStubs_profile');
  407. {$endif MACOSX104ORHIGHER}
  408. reorder:=reorderentries;
  409. prtobj:='';
  410. end;
  411. if reorder Then
  412. ExpandAndApplyOrder(SharedLibFiles);
  413. { Open link.res file }
  414. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,not LdSupportsNoResponseFile);
  415. if (target_info.system in systems_darwin) and
  416. (sysrootpath<>'') then
  417. begin
  418. LinkRes.Add('-syslibroot');
  419. LinkRes.Add(sysrootpath);
  420. end;
  421. if (target_info.system in systems_darwin) then
  422. begin
  423. LinkRes.Add('-arch');
  424. case target_info.system of
  425. system_powerpc_darwin:
  426. LinkRes.Add('ppc');
  427. system_i386_darwin,
  428. system_i386_iphonesim:
  429. LinkRes.Add('i386');
  430. system_powerpc64_darwin:
  431. LinkRes.Add('ppc64');
  432. system_x86_64_darwin,
  433. system_x86_64_iphonesim:
  434. LinkRes.Add('x86_64');
  435. system_arm_darwin:
  436. { current versions of the linker require the sub-architecture type
  437. to be specified }
  438. LinkRes.Add(lower(cputypestr[current_settings.cputype]));
  439. system_aarch64_darwin:
  440. LinkRes.Add('arm64');
  441. else
  442. internalerror(2014121801);
  443. end;
  444. if MacOSXVersionMin<>'' then
  445. begin
  446. LinkRes.Add('-macosx_version_min');
  447. LinkRes.Add(MacOSXVersionMin);
  448. end
  449. else if iPhoneOSVersionMin<>'' then
  450. begin
  451. LinkRes.Add('-iphoneos_version_min');
  452. LinkRes.Add(iPhoneOSVersionMin);
  453. end;
  454. end;
  455. { Write path to search libraries }
  456. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  457. while assigned(HPath) do
  458. begin
  459. if LdSupportsNoResponseFile then
  460. LinkRes.Add('-L'+HPath.Str)
  461. else
  462. LinkRes.Add('SEARCH_DIR("'+HPath.Str+'")');
  463. HPath:=TCmdStrListItem(HPath.Next);
  464. end;
  465. HPath:=TCmdStrListItem(LibrarySearchPath.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. if (target_info.system in systems_darwin) then
  475. begin
  476. HPath:=TCmdStrListItem(current_module.localframeworksearchpath.First);
  477. while assigned(HPath) do
  478. begin
  479. LinkRes.Add('-F'+HPath.Str);
  480. HPath:=TCmdStrListItem(HPath.Next);
  481. end;
  482. HPath:=TCmdStrListItem(FrameworkSearchPath.First);
  483. while assigned(HPath) do
  484. begin
  485. LinkRes.Add('-F'+HPath.Str);
  486. HPath:=TCmdStrListItem(HPath.Next);
  487. end;
  488. end;
  489. { force local symbol resolution (i.e., inside the shared }
  490. { library itself) for all non-exorted symbols, otherwise }
  491. { several RTL symbols of FPC-compiled shared libraries }
  492. { will be bound to those of a single shared library or }
  493. { to the main program }
  494. if (isdll) and (target_info.system in systems_bsd) then
  495. begin
  496. LinkRes.add('VERSION');
  497. LinkRes.add('{');
  498. LinkRes.add(' {');
  499. if not texportlibunix(exportlib).exportedsymnames.empty then
  500. begin
  501. LinkRes.add(' global:');
  502. repeat
  503. LinkRes.add(' '+texportlibunix(exportlib).exportedsymnames.getfirst+';');
  504. until texportlibunix(exportlib).exportedsymnames.empty;
  505. end;
  506. LinkRes.add(' local:');
  507. LinkRes.add(' *;');
  508. LinkRes.add(' };');
  509. LinkRes.add('}');
  510. end;
  511. if not LdSupportsNoResponseFile then
  512. LinkRes.Add('INPUT(');
  513. { add objectfiles, start with prt0 always }
  514. if prtobj<>'' then
  515. LinkRes.AddFileName(FindObjectFile(prtobj,'',false));
  516. { try to add crti and crtbegin if linking to C }
  517. if linklibc and
  518. not IsDarwin Then
  519. begin
  520. if librarysearchpath.FindFile('crti.o',false,s) then
  521. LinkRes.AddFileName(s);
  522. if cs_create_pic in current_settings.moduleswitches then
  523. begin
  524. if librarysearchpath.FindFile('crtbeginS.o',false,s) then
  525. LinkRes.AddFileName(s);
  526. end
  527. else
  528. if (cs_link_staticflag in current_settings.globalswitches) and
  529. librarysearchpath.FindFile('crtbeginT.o',false,s) then
  530. LinkRes.AddFileName(s)
  531. else if librarysearchpath.FindFile('crtbegin.o',false,s) then
  532. LinkRes.AddFileName(s);
  533. end;
  534. { main objectfiles }
  535. while not ObjectFiles.Empty do
  536. begin
  537. s:=ObjectFiles.GetFirst;
  538. if s<>'' then
  539. if LdSupportsNoResponseFile then
  540. LinkRes.AddFileName(s)
  541. else
  542. LinkRes.AddFileName(maybequoted(s));
  543. end;
  544. if not LdSupportsNoResponseFile then
  545. LinkRes.Add(')');
  546. { Write staticlibraries }
  547. if not StaticLibFiles.Empty then
  548. begin
  549. if not LdSupportsNoResponseFile then
  550. LinkRes.Add('GROUP(');
  551. While not StaticLibFiles.Empty do
  552. begin
  553. S:=StaticLibFiles.GetFirst;
  554. if LdSupportsNoResponseFile then
  555. LinkRes.AddFileName(s)
  556. else
  557. LinkRes.AddFileName(maybequoted(s))
  558. end;
  559. if not LdSupportsNoResponseFile then
  560. LinkRes.Add(')');
  561. end;
  562. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  563. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  564. if not SharedLibFiles.Empty then
  565. begin
  566. if not LdSupportsNoResponseFile then
  567. LinkRes.Add('INPUT(');
  568. While not SharedLibFiles.Empty do
  569. begin
  570. S:=SharedLibFiles.GetFirst;
  571. if (s<>'c') or reorder then
  572. begin
  573. i:=Pos(target_info.sharedlibext,S);
  574. if i>0 then
  575. Delete(S,i,255);
  576. LinkRes.Add('-l'+s);
  577. end
  578. else
  579. begin
  580. linklibc:=true;
  581. linkdynamic:=false; { libc will include the ld-* for us }
  582. end;
  583. end;
  584. { be sure that libc is the last lib }
  585. if linklibc and not reorder then
  586. Begin
  587. If LibrarySuffix=' ' Then
  588. LinkRes.Add('-lc')
  589. else
  590. LinkRes.Add('-lc_'+LibrarySuffix);
  591. If LibrarySuffix='r' Then
  592. LinkRes.Add('-lc');
  593. end;
  594. { when we have -static for the linker the we also need libgcc }
  595. if (cs_link_staticflag in current_settings.globalswitches) then
  596. LinkRes.Add('-lgcc');
  597. if linkdynamic and (Info.DynamicLinker<>'') then
  598. LinkRes.AddFileName(Info.DynamicLinker);
  599. if not LdSupportsNoResponseFile then
  600. LinkRes.Add(')');
  601. end;
  602. { frameworks for Darwin }
  603. if IsDarwin then
  604. while not FrameworkFiles.empty do
  605. begin
  606. LinkRes.Add('-framework');
  607. LinkRes.Add(FrameworkFiles.GetFirst);
  608. end;
  609. { objects which must be at the end }
  610. if linklibc and
  611. not IsDarwin Then
  612. begin
  613. if cs_create_pic in current_settings.moduleswitches then
  614. Fl1:=librarysearchpath.FindFile('crtendS.o',false,s1)
  615. else
  616. Fl1:=librarysearchpath.FindFile('crtend.o',false,s1);
  617. Fl2:=librarysearchpath.FindFile('crtn.o',false,s2);
  618. if Fl1 or Fl2 then
  619. begin
  620. LinkRes.Add('INPUT(');
  621. If Fl1 Then
  622. LinkRes.AddFileName(s1);
  623. If Fl2 Then
  624. LinkRes.AddFileName(s2);
  625. LinkRes.Add(')');
  626. end;
  627. end;
  628. { Write and Close response }
  629. linkres.writetodisk;
  630. linkres.Free;
  631. WriteResponseFile:=True;
  632. end;
  633. function TLinkerBSD.MakeExecutable:boolean;
  634. var
  635. binstr,
  636. cmdstr,
  637. targetstr,
  638. emulstr,
  639. extdbgbinstr,
  640. extdbgcmdstr: TCmdStr;
  641. linkscript: TAsmScript;
  642. DynLinkStr : string[60];
  643. GCSectionsStr,
  644. StaticStr,
  645. StripStr : string[63];
  646. success,
  647. useshell : boolean;
  648. begin
  649. if not(cs_link_nolink in current_settings.globalswitches) then
  650. Message1(exec_i_linking,current_module.exefilename);
  651. { Create some replacements }
  652. StaticStr:='';
  653. StripStr:='';
  654. DynLinkStr:='';
  655. GCSectionsStr:='';
  656. linkscript:=nil;
  657. { i386_freebsd needs -b elf32-i386-freebsd and -m elf_i386_fbsd
  658. to avoid creation of a i386:x86_64 arch binary }
  659. if target_info.system=system_i386_freebsd then
  660. begin
  661. targetstr:='-b elf32-i386-freebsd';
  662. emulstr:='-m elf_i386_fbsd';
  663. end
  664. else
  665. begin
  666. targetstr:='';
  667. emulstr:='';
  668. end;
  669. if (cs_link_staticflag in current_settings.globalswitches) then
  670. begin
  671. if (target_info.system=system_m68k_netbsd) and
  672. ((cs_link_on_target in current_settings.globalswitches) or
  673. (target_info.system=source_info.system)) then
  674. StaticStr:='-Bstatic'
  675. else
  676. StaticStr:='-static';
  677. end;
  678. if (cs_link_strip in current_settings.globalswitches) then
  679. if (target_info.system in systems_darwin) then
  680. StripStr:='-x'
  681. else
  682. StripStr:='-s';
  683. if (cs_link_smart in current_settings.globalswitches) and
  684. (tf_smartlink_sections in target_info.flags) then
  685. if not(target_info.system in systems_darwin) then
  686. GCSectionsStr:='--gc-sections'
  687. else
  688. GCSectionsStr:='-dead_strip -no_dead_strip_inits_and_terms';
  689. if(not(target_info.system in systems_darwin) and
  690. (cs_profile in current_settings.moduleswitches)) or
  691. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty)) then
  692. DynLinkStr:='-dynamic-linker='+Info.DynamicLinker;
  693. if CShared Then
  694. begin
  695. if not(target_info.system in systems_darwin) then
  696. DynLinKStr:=DynLinkStr+' --shared'
  697. else
  698. DynLinKStr:=DynLinkStr+' -dynamic'; // one dash!
  699. end;
  700. { Use -nopie on OpenBSD }
  701. if (target_info.system in systems_openbsd) and
  702. (target_info.system <> system_x86_64_openbsd) then
  703. Info.ExtraOptions:=Info.ExtraOptions+' -nopie';
  704. { Write used files and libraries }
  705. WriteResponseFile(false);
  706. { Call linker }
  707. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  708. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename));
  709. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  710. Replace(cmdstr,'$TARGET',targetstr);
  711. Replace(cmdstr,'$EMUL',EmulStr);
  712. Replace(cmdstr,'$CATRES',CatFileContent(outputexedir+Info.ResName));
  713. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  714. Replace(cmdstr,'$STATIC',StaticStr);
  715. Replace(cmdstr,'$STRIP',StripStr);
  716. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  717. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  718. if (target_info.system in systems_darwin) then
  719. Replace(cmdstr,'$PRTOBJ',GetDarwinPrtobjName(false));
  720. BinStr:=FindUtil(utilsprefix+BinStr);
  721. { create dsym file? }
  722. extdbgbinstr:='';
  723. extdbgcmdstr:='';
  724. if (target_info.system in systems_darwin) and
  725. (target_dbg.id in [dbg_dwarf2,dbg_dwarf3]) and
  726. (cs_link_separate_dbg_file in current_settings.globalswitches) then
  727. begin
  728. extdbgbinstr:=FindUtil(utilsprefix+'dsymutil');
  729. extdbgcmdstr:=maybequoted(current_module.exefilename);
  730. end;
  731. if (LdSupportsNoResponseFile) and
  732. not(cs_link_nolink in current_settings.globalswitches) then
  733. begin
  734. { we have to use a script to use the IFS hack }
  735. linkscript:=TAsmScriptUnix.create(outputexedir+'ppaslink');
  736. linkscript.AddLinkCommand(BinStr,CmdStr,'');
  737. if (extdbgcmdstr<>'') then
  738. linkscript.AddLinkCommand(extdbgbinstr,extdbgcmdstr,'');
  739. linkscript.WriteToDisk;
  740. BinStr:=linkscript.fn;
  741. if not path_absolute(BinStr) then
  742. BinStr:='./'+BinStr;
  743. CmdStr:='';
  744. end;
  745. useshell:=not (tf_no_backquote_support in source_info.flags);
  746. success:=DoExec(BinStr,CmdStr,true,LdSupportsNoResponseFile or useshell);
  747. if (success and
  748. (extdbgbinstr<>'') and
  749. (cs_link_nolink in current_settings.globalswitches)) then
  750. success:=DoExec(extdbgbinstr,extdbgcmdstr,false,LdSupportsNoResponseFile);
  751. { Remove ReponseFile }
  752. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  753. begin
  754. DeleteFile(outputexedir+Info.ResName);
  755. if LdSupportsNoResponseFile Then
  756. begin
  757. DeleteFile(linkscript.fn);
  758. linkscript.free
  759. end;
  760. end;
  761. MakeExecutable:=success; { otherwise a recursive call to link method }
  762. end;
  763. Function TLinkerBSD.MakeSharedLibrary:boolean;
  764. var
  765. InitStr,
  766. FiniStr,
  767. SoNameStr : string[80];
  768. linkscript: TAsmScript;
  769. binstr,
  770. cmdstr,
  771. targetstr,
  772. emulstr,
  773. extdbgbinstr,
  774. extdbgcmdstr : TCmdStr;
  775. GCSectionsStr : string[63];
  776. exportedsyms: text;
  777. success : boolean;
  778. begin
  779. MakeSharedLibrary:=false;
  780. GCSectionsStr:='';
  781. linkscript:=nil;
  782. if not(cs_link_nolink in current_settings.globalswitches) then
  783. Message1(exec_i_linking,current_module.sharedlibfilename);
  784. { Write used files and libraries }
  785. WriteResponseFile(true);
  786. if (cs_link_smart in current_settings.globalswitches) and
  787. (tf_smartlink_sections in target_info.flags) then
  788. if not(target_info.system in systems_darwin) then
  789. { disabled because not tested
  790. GCSectionsStr:='--gc-sections' }
  791. else
  792. GCSectionsStr:='-dead_strip -no_dead_strip_inits_and_terms';
  793. { i386_freebsd needs -b elf32-i386-freebsd and -m elf_i386_fbsd
  794. to avoid creation of a i386:x86_64 arch binary }
  795. if target_info.system=system_i386_freebsd then
  796. begin
  797. targetstr:='-b elf32-i386-freebsd';
  798. emulstr:='-m elf_i386_fbsd';
  799. end
  800. else
  801. begin
  802. targetstr:='';
  803. emulstr:='';
  804. end;
  805. InitStr:='-init FPC_LIB_START';
  806. FiniStr:='-fini FPC_LIB_EXIT';
  807. SoNameStr:='-soname '+ExtractFileName(current_module.sharedlibfilename);
  808. { Call linker }
  809. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  810. {$ifndef darwin}
  811. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename));
  812. {$else darwin}
  813. Replace(cmdstr,'$EXE',maybequoted(ExpandFileName(current_module.sharedlibfilename)));
  814. {$endif darwin}
  815. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  816. Replace(cmdstr,'$TARGET',targetstr);
  817. Replace(cmdstr,'$EMUL',EmulStr);
  818. Replace(cmdstr,'$CATRES',CatFileContent(outputexedir+Info.ResName));
  819. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  820. Replace(cmdstr,'$INIT',InitStr);
  821. Replace(cmdstr,'$FINI',FiniStr);
  822. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  823. Replace(cmdstr,'$SONAME',SoNameStr);
  824. if (target_info.system in systems_darwin) then
  825. Replace(cmdstr,'$PRTOBJ',GetDarwinPrtobjName(true));
  826. BinStr:=FindUtil(utilsprefix+BinStr);
  827. { create dsym file? }
  828. extdbgbinstr:='';
  829. extdbgcmdstr:='';
  830. if (target_info.system in systems_darwin) and
  831. (target_dbg.id in [dbg_dwarf2,dbg_dwarf3]) and
  832. (cs_link_separate_dbg_file in current_settings.globalswitches) then
  833. begin
  834. extdbgbinstr:=FindUtil(utilsprefix+'dsymutil');
  835. extdbgcmdstr:=maybequoted(current_module.sharedlibfilename);
  836. end;
  837. if (target_info.system in systems_darwin) then
  838. begin
  839. { exported symbols for darwin }
  840. if not texportlibunix(exportlib).exportedsymnames.empty then
  841. begin
  842. assign(exportedsyms,outputexedir+'linksyms.fpc');
  843. rewrite(exportedsyms);
  844. repeat
  845. writeln(exportedsyms,texportlibunix(exportlib).exportedsymnames.getfirst);
  846. until texportlibunix(exportlib).exportedsymnames.empty;
  847. close(exportedsyms);
  848. cmdstr:=cmdstr+' -exported_symbols_list '+maybequoted(outputexedir)+'linksyms.fpc';
  849. end;
  850. end;
  851. if (LdSupportsNoResponseFile) and
  852. not(cs_link_nolink in current_settings.globalswitches) then
  853. begin
  854. { we have to use a script to use the IFS hack }
  855. linkscript:=TAsmScriptUnix.create(outputexedir+'ppaslink');
  856. linkscript.AddLinkCommand(BinStr,CmdStr,'');
  857. if (extdbgbinstr<>'') then
  858. linkscript.AddLinkCommand(extdbgbinstr,extdbgcmdstr,'');
  859. linkscript.WriteToDisk;
  860. BinStr:=linkscript.fn;
  861. if not path_absolute(BinStr) then
  862. BinStr:='./'+BinStr;
  863. CmdStr:='';
  864. end;
  865. success:=DoExec(BinStr,cmdstr,true,LdSupportsNoResponseFile);
  866. if (success and
  867. (extdbgbinstr<>'') and
  868. (cs_link_nolink in current_settings.globalswitches)) then
  869. success:=DoExec(extdbgbinstr,extdbgcmdstr,false,LdSupportsNoResponseFile);
  870. { Strip the library ? }
  871. if success and (cs_link_strip in current_settings.globalswitches) then
  872. begin
  873. SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  874. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename));
  875. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,false,false);
  876. end;
  877. { Remove ReponseFile }
  878. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  879. begin
  880. DeleteFile(outputexedir+Info.ResName);
  881. if LdSupportsNoResponseFile Then
  882. begin
  883. DeleteFile(linkscript.fn);
  884. linkscript.free
  885. end;
  886. if (target_info.system in systems_darwin) then
  887. DeleteFile(outputexedir+'linksyms.fpc');
  888. end;
  889. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  890. end;
  891. {*****************************************************************************
  892. Initialize
  893. *****************************************************************************}
  894. initialization
  895. RegisterLinker(ld_bsd,TLinkerBSD);
  896. {$ifdef x86_64}
  897. RegisterImport(system_x86_64_dragonfly,timportlibbsd);
  898. RegisterExport(system_x86_64_dragonfly,texportlibbsd);
  899. RegisterTarget(system_x86_64_dragonfly_info);
  900. RegisterImport(system_x86_64_freebsd,timportlibbsd);
  901. RegisterExport(system_x86_64_freebsd,texportlibbsd);
  902. RegisterTarget(system_x86_64_freebsd_info);
  903. RegisterImport(system_x86_64_openbsd,timportlibbsd);
  904. RegisterExport(system_x86_64_openbsd,texportlibbsd);
  905. RegisterTarget(system_x86_64_openbsd_info);
  906. RegisterImport(system_x86_64_netbsd,timportlibbsd);
  907. RegisterExport(system_x86_64_netbsd,texportlibbsd);
  908. RegisterTarget(system_x86_64_netbsd_info);
  909. RegisterImport(system_x86_64_darwin,timportlibdarwin);
  910. RegisterExport(system_x86_64_darwin,texportlibdarwin);
  911. RegisterTarget(system_x86_64_darwin_info);
  912. RegisterImport(system_x86_64_iphonesim,timportlibdarwin);
  913. RegisterExport(system_x86_64_iphonesim,texportlibdarwin);
  914. RegisterTarget(system_x86_64_iphonesim_info);
  915. {$endif}
  916. {$ifdef i386}
  917. RegisterImport(system_i386_freebsd,timportlibbsd);
  918. RegisterExport(system_i386_freebsd,texportlibbsd);
  919. RegisterTarget(system_i386_freebsd_info);
  920. RegisterImport(system_i386_netbsd,timportlibbsd);
  921. RegisterExport(system_i386_netbsd,texportlibbsd);
  922. RegisterTarget(system_i386_netbsd_info);
  923. RegisterImport(system_i386_openbsd,timportlibbsd);
  924. RegisterExport(system_i386_openbsd,texportlibbsd);
  925. RegisterTarget(system_i386_openbsd_info);
  926. RegisterImport(system_i386_darwin,timportlibdarwin);
  927. RegisterExport(system_i386_darwin,texportlibdarwin);
  928. RegisterTarget(system_i386_darwin_info);
  929. RegisterImport(system_i386_iphonesim,timportlibdarwin);
  930. RegisterExport(system_i386_iphonesim,texportlibdarwin);
  931. RegisterTarget(system_i386_iphonesim_info);
  932. {$endif i386}
  933. {$ifdef m68k}
  934. RegisterImport(system_m68k_netbsd,timportlibbsd);
  935. RegisterExport(system_m68k_netbsd,texportlibbsd);
  936. RegisterTarget(system_m68k_netbsd_info);
  937. {$endif m68k}
  938. {$ifdef powerpc}
  939. RegisterImport(system_powerpc_darwin,timportlibdarwin);
  940. RegisterExport(system_powerpc_darwin,texportlibdarwin);
  941. RegisterTarget(system_powerpc_darwin_info);
  942. RegisterImport(system_powerpc_netbsd,timportlibbsd);
  943. RegisterExport(system_powerpc_netbsd,texportlibbsd);
  944. RegisterTarget(system_powerpc_netbsd_info);
  945. {$endif powerpc}
  946. {$ifdef powerpc64}
  947. RegisterImport(system_powerpc64_darwin,timportlibdarwin);
  948. RegisterExport(system_powerpc64_darwin,texportlibdarwin);
  949. RegisterTarget(system_powerpc64_darwin_info);
  950. {$endif powerpc64}
  951. {$ifdef arm}
  952. RegisterImport(system_arm_darwin,timportlibdarwin);
  953. RegisterExport(system_arm_darwin,texportlibdarwin);
  954. RegisterTarget(system_arm_darwin_info);
  955. {$endif arm}
  956. {$ifdef aarch64}
  957. RegisterImport(system_aarch64_darwin,timportlibdarwin);
  958. RegisterExport(system_aarch64_darwin,texportlibdarwin);
  959. RegisterTarget(system_aarch64_darwin_info);
  960. {$endif aarch64}
  961. RegisterRes(res_elf_info,TWinLikeResourceFile);
  962. RegisterRes(res_macho_info,TWinLikeResourceFile);
  963. end.