t_bsd.pas 37 KB

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