t_bsd.pas 37 KB

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