t_bsd.pas 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  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. { Write used files and libraries }
  739. WriteResponseFile(false);
  740. { Call linker }
  741. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  742. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename));
  743. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  744. Replace(cmdstr,'$TARGET',targetstr);
  745. Replace(cmdstr,'$EMUL',EmulStr);
  746. Replace(cmdstr,'$CATRES',CatFileContent(outputexedir+Info.ResName));
  747. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  748. if (LdSupportsNoResponseFile) and (source_info.system in systems_all_windows) then
  749. Replace(cmdstr,'$FILELIST','-filelist '+maybequoted(outputexedir+'linkfiles.res'))
  750. else
  751. Replace(cmdstr,'$FILELIST','');
  752. Replace(cmdstr,'$STATIC',StaticStr);
  753. Replace(cmdstr,'$STRIP',StripStr);
  754. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  755. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  756. if (target_info.system in systems_darwin) then
  757. Replace(cmdstr,'$PRTOBJ',GetDarwinPrtobjName(false));
  758. BinStr:=FindUtil(utilsprefix+BinStr);
  759. { create dsym file? }
  760. extdbgbinstr:='';
  761. extdbgcmdstr:='';
  762. if (target_info.system in systems_darwin) and
  763. (target_dbg.id in [dbg_dwarf2,dbg_dwarf3]) and
  764. (cs_link_separate_dbg_file in current_settings.globalswitches) then
  765. begin
  766. extdbgbinstr:=FindUtil(utilsprefix+'dsymutil');
  767. extdbgcmdstr:=maybequoted(current_module.exefilename);
  768. end;
  769. if (LdSupportsNoResponseFile) and
  770. not(cs_link_nolink in current_settings.globalswitches) then
  771. begin
  772. { we have to use a script to use the IFS hack }
  773. linkscript:=GenerateScript(outputexedir+'ppaslink');
  774. linkscript.AddLinkCommand(BinStr,CmdStr,'');
  775. if (extdbgcmdstr<>'') then
  776. linkscript.AddLinkCommand(extdbgbinstr,extdbgcmdstr,'');
  777. linkscript.WriteToDisk;
  778. BinStr:=linkscript.fn;
  779. if not path_absolute(BinStr) then
  780. if cs_link_on_target in current_settings.globalswitches then
  781. BinStr:='.'+target_info.dirsep+BinStr
  782. else
  783. BinStr:='.'+source_info.dirsep+BinStr;
  784. CmdStr:='';
  785. end;
  786. useshell:=not (tf_no_backquote_support in source_info.flags);
  787. success:=DoExec(BinStr,CmdStr,true,LdSupportsNoResponseFile or useshell);
  788. if (success and
  789. (extdbgbinstr<>'') and
  790. (cs_link_nolink in current_settings.globalswitches)) then
  791. success:=DoExec(extdbgbinstr,extdbgcmdstr,false,LdSupportsNoResponseFile);
  792. { Remove ReponseFile }
  793. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  794. begin
  795. DeleteFile(outputexedir+Info.ResName);
  796. if LdSupportsNoResponseFile Then
  797. begin
  798. DeleteFile(linkscript.fn);
  799. linkscript.free
  800. end;
  801. end;
  802. { Remove linkfiles.res }
  803. if (success) and (LdSupportsNoResponseFile) and (source_info.system in systems_all_windows) then
  804. DeleteFile(outputexedir+'linkfiles.res');
  805. MakeExecutable:=success; { otherwise a recursive call to link method }
  806. end;
  807. Function TLinkerBSD.MakeSharedLibrary:boolean;
  808. var
  809. InitStr,
  810. FiniStr,
  811. SoNameStr : string[80];
  812. linkscript: TAsmScript;
  813. binstr,
  814. cmdstr,
  815. targetstr,
  816. emulstr,
  817. extdbgbinstr,
  818. extdbgcmdstr : TCmdStr;
  819. GCSectionsStr : string[63];
  820. exportedsyms: text;
  821. success : boolean;
  822. begin
  823. MakeSharedLibrary:=false;
  824. GCSectionsStr:='';
  825. linkscript:=nil;
  826. if not(cs_link_nolink in current_settings.globalswitches) then
  827. Message1(exec_i_linking,current_module.sharedlibfilename);
  828. { Write used files and libraries }
  829. WriteResponseFile(true);
  830. if (cs_link_smart in current_settings.globalswitches) and
  831. (tf_smartlink_sections in target_info.flags) then
  832. if not(target_info.system in systems_darwin) then
  833. { disabled because not tested
  834. GCSectionsStr:='--gc-sections' }
  835. else
  836. GCSectionsStr:='-dead_strip -no_dead_strip_inits_and_terms';
  837. { i386_freebsd needs -b elf32-i386-freebsd and -m elf_i386_fbsd
  838. to avoid creation of a i386:x86_64 arch binary }
  839. if target_info.system=system_i386_freebsd then
  840. begin
  841. targetstr:='-b elf32-i386-freebsd';
  842. emulstr:='-m elf_i386_fbsd';
  843. end
  844. else
  845. begin
  846. targetstr:='';
  847. emulstr:='';
  848. end;
  849. InitStr:='-init FPC_LIB_START';
  850. FiniStr:='-fini FPC_LIB_EXIT';
  851. SoNameStr:='-soname '+ExtractFileName(current_module.sharedlibfilename);
  852. { Call linker }
  853. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  854. {$ifndef darwin}
  855. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename));
  856. {$else darwin}
  857. Replace(cmdstr,'$EXE',maybequoted(ExpandFileName(current_module.sharedlibfilename)));
  858. {$endif darwin}
  859. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  860. Replace(cmdstr,'$TARGET',targetstr);
  861. Replace(cmdstr,'$EMUL',EmulStr);
  862. Replace(cmdstr,'$CATRES',CatFileContent(outputexedir+Info.ResName));
  863. if (LdSupportsNoResponseFile) and (source_info.system in systems_all_windows) then
  864. Replace(cmdstr,'$FILELIST','-filelist '+maybequoted(outputexedir+'linkfiles.res'))
  865. else
  866. Replace(cmdstr,'$FILELIST','');
  867. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  868. Replace(cmdstr,'$INIT',InitStr);
  869. Replace(cmdstr,'$FINI',FiniStr);
  870. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  871. Replace(cmdstr,'$SONAME',SoNameStr);
  872. if (target_info.system in systems_darwin) then
  873. Replace(cmdstr,'$PRTOBJ',GetDarwinPrtobjName(true));
  874. BinStr:=FindUtil(utilsprefix+BinStr);
  875. { create dsym file? }
  876. extdbgbinstr:='';
  877. extdbgcmdstr:='';
  878. if (target_info.system in systems_darwin) and
  879. (target_dbg.id in [dbg_dwarf2,dbg_dwarf3]) and
  880. (cs_link_separate_dbg_file in current_settings.globalswitches) then
  881. begin
  882. extdbgbinstr:=FindUtil(utilsprefix+'dsymutil');
  883. extdbgcmdstr:=maybequoted(current_module.sharedlibfilename);
  884. end;
  885. if (target_info.system in systems_darwin) then
  886. begin
  887. { exported symbols for darwin }
  888. if not texportlibunix(exportlib).exportedsymnames.empty then
  889. begin
  890. assign(exportedsyms,outputexedir+'linksyms.fpc');
  891. rewrite(exportedsyms);
  892. repeat
  893. writeln(exportedsyms,texportlibunix(exportlib).exportedsymnames.getfirst);
  894. until texportlibunix(exportlib).exportedsymnames.empty;
  895. close(exportedsyms);
  896. cmdstr:=cmdstr+' -exported_symbols_list '+maybequoted(outputexedir)+'linksyms.fpc';
  897. end;
  898. end;
  899. if (LdSupportsNoResponseFile) and
  900. not(cs_link_nolink in current_settings.globalswitches) then
  901. begin
  902. { we have to use a script to use the IFS hack }
  903. linkscript:=GenerateScript(outputexedir+'ppaslink');
  904. linkscript.AddLinkCommand(BinStr,CmdStr,'');
  905. if (extdbgbinstr<>'') then
  906. linkscript.AddLinkCommand(extdbgbinstr,extdbgcmdstr,'');
  907. linkscript.WriteToDisk;
  908. BinStr:=linkscript.fn;
  909. if not path_absolute(BinStr) then
  910. if cs_link_on_target in current_settings.globalswitches then
  911. BinStr:='.'+target_info.dirsep+BinStr
  912. else
  913. BinStr:='.'+source_info.dirsep+BinStr;
  914. CmdStr:='';
  915. end;
  916. success:=DoExec(BinStr,cmdstr,true,LdSupportsNoResponseFile);
  917. if (success and
  918. (extdbgbinstr<>'') and
  919. (cs_link_nolink in current_settings.globalswitches)) then
  920. success:=DoExec(extdbgbinstr,extdbgcmdstr,false,LdSupportsNoResponseFile);
  921. { Strip the library ? }
  922. if success and (cs_link_strip in current_settings.globalswitches) then
  923. begin
  924. SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  925. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename));
  926. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,false,false);
  927. end;
  928. { Remove ReponseFile }
  929. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  930. begin
  931. DeleteFile(outputexedir+Info.ResName);
  932. if LdSupportsNoResponseFile Then
  933. begin
  934. DeleteFile(linkscript.fn);
  935. linkscript.free
  936. end;
  937. if (target_info.system in systems_darwin) then
  938. DeleteFile(outputexedir+'linksyms.fpc');
  939. end;
  940. { Remove linkfiles.res }
  941. if (success) and (LdSupportsNoResponseFile) and (source_info.system in systems_all_windows) then
  942. DeleteFile(outputexedir+'linkfiles.res');
  943. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  944. end;
  945. {*****************************************************************************
  946. Initialize
  947. *****************************************************************************}
  948. initialization
  949. RegisterLinker(ld_bsd,TLinkerBSD);
  950. {$ifdef x86_64}
  951. RegisterImport(system_x86_64_dragonfly,timportlibbsd);
  952. RegisterExport(system_x86_64_dragonfly,texportlibbsd);
  953. RegisterTarget(system_x86_64_dragonfly_info);
  954. RegisterImport(system_x86_64_freebsd,timportlibbsd);
  955. RegisterExport(system_x86_64_freebsd,texportlibbsd);
  956. RegisterTarget(system_x86_64_freebsd_info);
  957. RegisterImport(system_x86_64_openbsd,timportlibbsd);
  958. RegisterExport(system_x86_64_openbsd,texportlibbsd);
  959. RegisterTarget(system_x86_64_openbsd_info);
  960. RegisterImport(system_x86_64_netbsd,timportlibbsd);
  961. RegisterExport(system_x86_64_netbsd,texportlibbsd);
  962. RegisterTarget(system_x86_64_netbsd_info);
  963. RegisterImport(system_x86_64_darwin,timportlibdarwin);
  964. RegisterExport(system_x86_64_darwin,texportlibdarwin);
  965. RegisterTarget(system_x86_64_darwin_info);
  966. RegisterImport(system_x86_64_iphonesim,timportlibdarwin);
  967. RegisterExport(system_x86_64_iphonesim,texportlibdarwin);
  968. RegisterTarget(system_x86_64_iphonesim_info);
  969. {$endif}
  970. {$ifdef i386}
  971. RegisterImport(system_i386_freebsd,timportlibbsd);
  972. RegisterExport(system_i386_freebsd,texportlibbsd);
  973. RegisterTarget(system_i386_freebsd_info);
  974. RegisterImport(system_i386_netbsd,timportlibbsd);
  975. RegisterExport(system_i386_netbsd,texportlibbsd);
  976. RegisterTarget(system_i386_netbsd_info);
  977. RegisterImport(system_i386_openbsd,timportlibbsd);
  978. RegisterExport(system_i386_openbsd,texportlibbsd);
  979. RegisterTarget(system_i386_openbsd_info);
  980. RegisterImport(system_i386_darwin,timportlibdarwin);
  981. RegisterExport(system_i386_darwin,texportlibdarwin);
  982. RegisterTarget(system_i386_darwin_info);
  983. RegisterImport(system_i386_iphonesim,timportlibdarwin);
  984. RegisterExport(system_i386_iphonesim,texportlibdarwin);
  985. RegisterTarget(system_i386_iphonesim_info);
  986. {$endif i386}
  987. {$ifdef m68k}
  988. RegisterImport(system_m68k_netbsd,timportlibbsd);
  989. RegisterExport(system_m68k_netbsd,texportlibbsd);
  990. RegisterTarget(system_m68k_netbsd_info);
  991. {$endif m68k}
  992. {$ifdef powerpc}
  993. RegisterImport(system_powerpc_darwin,timportlibdarwin);
  994. RegisterExport(system_powerpc_darwin,texportlibdarwin);
  995. RegisterTarget(system_powerpc_darwin_info);
  996. RegisterImport(system_powerpc_netbsd,timportlibbsd);
  997. RegisterExport(system_powerpc_netbsd,texportlibbsd);
  998. RegisterTarget(system_powerpc_netbsd_info);
  999. {$endif powerpc}
  1000. {$ifdef powerpc64}
  1001. RegisterImport(system_powerpc64_darwin,timportlibdarwin);
  1002. RegisterExport(system_powerpc64_darwin,texportlibdarwin);
  1003. RegisterTarget(system_powerpc64_darwin_info);
  1004. {$endif powerpc64}
  1005. {$ifdef arm}
  1006. RegisterImport(system_arm_darwin,timportlibdarwin);
  1007. RegisterExport(system_arm_darwin,texportlibdarwin);
  1008. RegisterTarget(system_arm_darwin_info);
  1009. RegisterImport(system_arm_netbsd,timportlibbsd);
  1010. RegisterExport(system_arm_netbsd,texportlibbsd);
  1011. RegisterTarget(system_arm_netbsd_info);
  1012. {$endif arm}
  1013. {$ifdef aarch64}
  1014. RegisterImport(system_aarch64_darwin,timportlibdarwin);
  1015. RegisterExport(system_aarch64_darwin,texportlibdarwin);
  1016. RegisterTarget(system_aarch64_darwin_info);
  1017. {$endif aarch64}
  1018. RegisterRes(res_elf_info,TWinLikeResourceFile);
  1019. RegisterRes(res_macho_info,TWinLikeResourceFile);
  1020. end.