t_bsd.pas 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  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<>'') then
  633. LinkRes.AddFileName(Info.DynamicLinker);
  634. if not LdSupportsNoResponseFile then
  635. LinkRes.Add(')');
  636. end;
  637. { frameworks for Darwin }
  638. if IsDarwin then
  639. while not FrameworkFiles.empty do
  640. begin
  641. LinkRes.Add('-framework');
  642. LinkRes.Add(FrameworkFiles.GetFirst);
  643. end;
  644. { objects which must be at the end }
  645. if linklibc and
  646. not IsDarwin Then
  647. begin
  648. if cs_create_pic in current_settings.moduleswitches then
  649. Fl1:=librarysearchpath.FindFile('crtendS.o',false,s1)
  650. else
  651. Fl1:=librarysearchpath.FindFile('crtend.o',false,s1);
  652. Fl2:=librarysearchpath.FindFile('crtn.o',false,s2);
  653. if Fl1 or Fl2 then
  654. begin
  655. LinkRes.Add('INPUT(');
  656. If Fl1 Then
  657. LinkRes.AddFileName(s1);
  658. If Fl2 Then
  659. LinkRes.AddFileName(s2);
  660. LinkRes.Add(')');
  661. end;
  662. end;
  663. { Write and Close response }
  664. linkres.writetodisk;
  665. linkres.Free;
  666. WriteResponseFile:=True;
  667. end;
  668. function TLinkerBSD.MakeExecutable:boolean;
  669. var
  670. binstr,
  671. cmdstr,
  672. targetstr,
  673. emulstr,
  674. extdbgbinstr,
  675. extdbgcmdstr: TCmdStr;
  676. linkscript: TAsmScript;
  677. DynLinkStr : string[60];
  678. GCSectionsStr,
  679. StaticStr,
  680. StripStr : string[63];
  681. success,
  682. useshell : boolean;
  683. begin
  684. if not(cs_link_nolink in current_settings.globalswitches) then
  685. Message1(exec_i_linking,current_module.exefilename);
  686. { Create some replacements }
  687. StaticStr:='';
  688. StripStr:='';
  689. DynLinkStr:='';
  690. GCSectionsStr:='';
  691. linkscript:=nil;
  692. { i386_freebsd needs -b elf32-i386-freebsd and -m elf_i386_fbsd
  693. to avoid creation of a i386:x86_64 arch binary }
  694. if target_info.system=system_i386_freebsd then
  695. begin
  696. targetstr:='-b elf32-i386-freebsd';
  697. emulstr:='-m elf_i386_fbsd';
  698. end
  699. else
  700. begin
  701. targetstr:='';
  702. emulstr:='';
  703. end;
  704. if (cs_link_staticflag in current_settings.globalswitches) then
  705. begin
  706. if (target_info.system=system_m68k_netbsd) and
  707. ((cs_link_on_target in current_settings.globalswitches) or
  708. (target_info.system=source_info.system)) then
  709. StaticStr:='-Bstatic'
  710. else
  711. StaticStr:='-static';
  712. end;
  713. if (cs_link_strip in current_settings.globalswitches) then
  714. if (target_info.system in systems_darwin) then
  715. StripStr:='-x'
  716. else
  717. StripStr:='-s';
  718. if (cs_link_smart in current_settings.globalswitches) and
  719. (tf_smartlink_sections in target_info.flags) then
  720. if not(target_info.system in systems_darwin) then
  721. GCSectionsStr:='--gc-sections'
  722. else
  723. GCSectionsStr:='-dead_strip -no_dead_strip_inits_and_terms';
  724. if(not(target_info.system in systems_darwin) and
  725. (cs_profile in current_settings.moduleswitches)) or
  726. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty)) then
  727. DynLinkStr:='-dynamic-linker='+Info.DynamicLinker;
  728. if CShared Then
  729. begin
  730. if not(target_info.system in systems_darwin) then
  731. DynLinKStr:=DynLinkStr+' --shared'
  732. else
  733. DynLinKStr:=DynLinkStr+' -dynamic'; // one dash!
  734. end;
  735. { Use -nopie on OpenBSD }
  736. if (target_info.system in systems_openbsd) then
  737. Info.ExtraOptions:=Info.ExtraOptions+' -nopie';
  738. { -N seems to be needed on NetBSD/earm }
  739. if (target_info.system in [system_arm_netbsd]) then
  740. Info.ExtraOptions:=Info.ExtraOptions+' -N';
  741. { Write used files and libraries }
  742. WriteResponseFile(false);
  743. { Call linker }
  744. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  745. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename));
  746. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  747. Replace(cmdstr,'$TARGET',targetstr);
  748. Replace(cmdstr,'$EMUL',EmulStr);
  749. Replace(cmdstr,'$CATRES',CatFileContent(outputexedir+Info.ResName));
  750. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  751. if (LdSupportsNoResponseFile) and (source_info.system in systems_all_windows) then
  752. Replace(cmdstr,'$FILELIST','-filelist '+maybequoted(outputexedir+'linkfiles.res'))
  753. else
  754. Replace(cmdstr,'$FILELIST','');
  755. Replace(cmdstr,'$STATIC',StaticStr);
  756. Replace(cmdstr,'$STRIP',StripStr);
  757. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  758. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  759. if (target_info.system in systems_darwin) then
  760. Replace(cmdstr,'$PRTOBJ',GetDarwinPrtobjName(false));
  761. BinStr:=FindUtil(utilsprefix+BinStr);
  762. { create dsym file? }
  763. extdbgbinstr:='';
  764. extdbgcmdstr:='';
  765. if (target_info.system in systems_darwin) and
  766. (target_dbg.id in [dbg_dwarf2,dbg_dwarf3]) and
  767. (cs_link_separate_dbg_file in current_settings.globalswitches) then
  768. begin
  769. extdbgbinstr:=FindUtil(utilsprefix+'dsymutil');
  770. extdbgcmdstr:=maybequoted(current_module.exefilename);
  771. end;
  772. if (LdSupportsNoResponseFile) and
  773. not(cs_link_nolink in current_settings.globalswitches) then
  774. begin
  775. { we have to use a script to use the IFS hack }
  776. linkscript:=GenerateScript(outputexedir+'ppaslink');
  777. linkscript.AddLinkCommand(BinStr,CmdStr,'');
  778. if (extdbgcmdstr<>'') then
  779. linkscript.AddLinkCommand(extdbgbinstr,extdbgcmdstr,'');
  780. linkscript.WriteToDisk;
  781. BinStr:=linkscript.fn;
  782. if not path_absolute(BinStr) then
  783. if cs_link_on_target in current_settings.globalswitches then
  784. BinStr:='.'+target_info.dirsep+BinStr
  785. else
  786. BinStr:='.'+source_info.dirsep+BinStr;
  787. CmdStr:='';
  788. end;
  789. useshell:=not (tf_no_backquote_support in source_info.flags);
  790. success:=DoExec(BinStr,CmdStr,true,LdSupportsNoResponseFile or useshell);
  791. if (success and
  792. (extdbgbinstr<>'') and
  793. (cs_link_nolink in current_settings.globalswitches)) then
  794. success:=DoExec(extdbgbinstr,extdbgcmdstr,false,LdSupportsNoResponseFile);
  795. { Remove ReponseFile }
  796. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  797. begin
  798. DeleteFile(outputexedir+Info.ResName);
  799. if LdSupportsNoResponseFile Then
  800. begin
  801. DeleteFile(linkscript.fn);
  802. linkscript.free
  803. end;
  804. end;
  805. { Remove linkfiles.res }
  806. if (success) and (LdSupportsNoResponseFile) and (source_info.system in systems_all_windows) then
  807. DeleteFile(outputexedir+'linkfiles.res');
  808. MakeExecutable:=success; { otherwise a recursive call to link method }
  809. end;
  810. Function TLinkerBSD.MakeSharedLibrary:boolean;
  811. var
  812. InitStr,
  813. FiniStr,
  814. SoNameStr : string[80];
  815. linkscript: TAsmScript;
  816. binstr,
  817. cmdstr,
  818. targetstr,
  819. emulstr,
  820. extdbgbinstr,
  821. extdbgcmdstr : TCmdStr;
  822. GCSectionsStr : string[63];
  823. exportedsyms: text;
  824. success : boolean;
  825. begin
  826. MakeSharedLibrary:=false;
  827. GCSectionsStr:='';
  828. linkscript:=nil;
  829. if not(cs_link_nolink in current_settings.globalswitches) then
  830. Message1(exec_i_linking,current_module.sharedlibfilename);
  831. { Write used files and libraries }
  832. WriteResponseFile(true);
  833. if (cs_link_smart in current_settings.globalswitches) and
  834. (tf_smartlink_sections in target_info.flags) then
  835. if not(target_info.system in systems_darwin) then
  836. { disabled because not tested
  837. GCSectionsStr:='--gc-sections' }
  838. else
  839. GCSectionsStr:='-dead_strip -no_dead_strip_inits_and_terms';
  840. { i386_freebsd needs -b elf32-i386-freebsd and -m elf_i386_fbsd
  841. to avoid creation of a i386:x86_64 arch binary }
  842. if target_info.system=system_i386_freebsd then
  843. begin
  844. targetstr:='-b elf32-i386-freebsd';
  845. emulstr:='-m elf_i386_fbsd';
  846. end
  847. else
  848. begin
  849. targetstr:='';
  850. emulstr:='';
  851. end;
  852. InitStr:='-init FPC_LIB_START';
  853. FiniStr:='-fini FPC_LIB_EXIT';
  854. SoNameStr:='-soname '+ExtractFileName(current_module.sharedlibfilename);
  855. { Call linker }
  856. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  857. {$ifndef darwin}
  858. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename));
  859. {$else darwin}
  860. Replace(cmdstr,'$EXE',maybequoted(ExpandFileName(current_module.sharedlibfilename)));
  861. {$endif darwin}
  862. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  863. Replace(cmdstr,'$TARGET',targetstr);
  864. Replace(cmdstr,'$EMUL',EmulStr);
  865. Replace(cmdstr,'$CATRES',CatFileContent(outputexedir+Info.ResName));
  866. if (LdSupportsNoResponseFile) and (source_info.system in systems_all_windows) then
  867. Replace(cmdstr,'$FILELIST','-filelist '+maybequoted(outputexedir+'linkfiles.res'))
  868. else
  869. Replace(cmdstr,'$FILELIST','');
  870. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  871. Replace(cmdstr,'$INIT',InitStr);
  872. Replace(cmdstr,'$FINI',FiniStr);
  873. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  874. Replace(cmdstr,'$SONAME',SoNameStr);
  875. if (target_info.system in systems_darwin) then
  876. Replace(cmdstr,'$PRTOBJ',GetDarwinPrtobjName(true));
  877. BinStr:=FindUtil(utilsprefix+BinStr);
  878. { create dsym file? }
  879. extdbgbinstr:='';
  880. extdbgcmdstr:='';
  881. if (target_info.system in systems_darwin) and
  882. (target_dbg.id in [dbg_dwarf2,dbg_dwarf3]) and
  883. (cs_link_separate_dbg_file in current_settings.globalswitches) then
  884. begin
  885. extdbgbinstr:=FindUtil(utilsprefix+'dsymutil');
  886. extdbgcmdstr:=maybequoted(current_module.sharedlibfilename);
  887. end;
  888. if (target_info.system in systems_darwin) then
  889. begin
  890. { exported symbols for darwin }
  891. if not texportlibunix(exportlib).exportedsymnames.empty then
  892. begin
  893. assign(exportedsyms,outputexedir+'linksyms.fpc');
  894. rewrite(exportedsyms);
  895. repeat
  896. writeln(exportedsyms,texportlibunix(exportlib).exportedsymnames.getfirst);
  897. until texportlibunix(exportlib).exportedsymnames.empty;
  898. close(exportedsyms);
  899. cmdstr:=cmdstr+' -exported_symbols_list '+maybequoted(outputexedir)+'linksyms.fpc';
  900. end;
  901. end;
  902. if (LdSupportsNoResponseFile) and
  903. not(cs_link_nolink in current_settings.globalswitches) then
  904. begin
  905. { we have to use a script to use the IFS hack }
  906. linkscript:=GenerateScript(outputexedir+'ppaslink');
  907. linkscript.AddLinkCommand(BinStr,CmdStr,'');
  908. if (extdbgbinstr<>'') then
  909. linkscript.AddLinkCommand(extdbgbinstr,extdbgcmdstr,'');
  910. linkscript.WriteToDisk;
  911. BinStr:=linkscript.fn;
  912. if not path_absolute(BinStr) then
  913. if cs_link_on_target in current_settings.globalswitches then
  914. BinStr:='.'+target_info.dirsep+BinStr
  915. else
  916. BinStr:='.'+source_info.dirsep+BinStr;
  917. CmdStr:='';
  918. end;
  919. success:=DoExec(BinStr,cmdstr,true,LdSupportsNoResponseFile);
  920. if (success and
  921. (extdbgbinstr<>'') and
  922. (cs_link_nolink in current_settings.globalswitches)) then
  923. success:=DoExec(extdbgbinstr,extdbgcmdstr,false,LdSupportsNoResponseFile);
  924. { Strip the library ? }
  925. if success and (cs_link_strip in current_settings.globalswitches) then
  926. begin
  927. SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  928. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename));
  929. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,false,false);
  930. end;
  931. { Remove ReponseFile }
  932. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  933. begin
  934. DeleteFile(outputexedir+Info.ResName);
  935. if LdSupportsNoResponseFile Then
  936. begin
  937. DeleteFile(linkscript.fn);
  938. linkscript.free
  939. end;
  940. if (target_info.system in systems_darwin) then
  941. DeleteFile(outputexedir+'linksyms.fpc');
  942. end;
  943. { Remove linkfiles.res }
  944. if (success) and (LdSupportsNoResponseFile) and (source_info.system in systems_all_windows) then
  945. DeleteFile(outputexedir+'linkfiles.res');
  946. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  947. end;
  948. {*****************************************************************************
  949. Initialize
  950. *****************************************************************************}
  951. initialization
  952. RegisterLinker(ld_bsd,TLinkerBSD);
  953. {$ifdef x86_64}
  954. RegisterImport(system_x86_64_dragonfly,timportlibbsd);
  955. RegisterExport(system_x86_64_dragonfly,texportlibbsd);
  956. RegisterTarget(system_x86_64_dragonfly_info);
  957. RegisterImport(system_x86_64_freebsd,timportlibbsd);
  958. RegisterExport(system_x86_64_freebsd,texportlibbsd);
  959. RegisterTarget(system_x86_64_freebsd_info);
  960. RegisterImport(system_x86_64_openbsd,timportlibbsd);
  961. RegisterExport(system_x86_64_openbsd,texportlibbsd);
  962. RegisterTarget(system_x86_64_openbsd_info);
  963. RegisterImport(system_x86_64_netbsd,timportlibbsd);
  964. RegisterExport(system_x86_64_netbsd,texportlibbsd);
  965. RegisterTarget(system_x86_64_netbsd_info);
  966. RegisterImport(system_x86_64_darwin,timportlibdarwin);
  967. RegisterExport(system_x86_64_darwin,texportlibdarwin);
  968. RegisterTarget(system_x86_64_darwin_info);
  969. RegisterImport(system_x86_64_iphonesim,timportlibdarwin);
  970. RegisterExport(system_x86_64_iphonesim,texportlibdarwin);
  971. RegisterTarget(system_x86_64_iphonesim_info);
  972. {$endif}
  973. {$ifdef i386}
  974. RegisterImport(system_i386_freebsd,timportlibbsd);
  975. RegisterExport(system_i386_freebsd,texportlibbsd);
  976. RegisterTarget(system_i386_freebsd_info);
  977. RegisterImport(system_i386_netbsd,timportlibbsd);
  978. RegisterExport(system_i386_netbsd,texportlibbsd);
  979. RegisterTarget(system_i386_netbsd_info);
  980. RegisterImport(system_i386_openbsd,timportlibbsd);
  981. RegisterExport(system_i386_openbsd,texportlibbsd);
  982. RegisterTarget(system_i386_openbsd_info);
  983. RegisterImport(system_i386_darwin,timportlibdarwin);
  984. RegisterExport(system_i386_darwin,texportlibdarwin);
  985. RegisterTarget(system_i386_darwin_info);
  986. RegisterImport(system_i386_iphonesim,timportlibdarwin);
  987. RegisterExport(system_i386_iphonesim,texportlibdarwin);
  988. RegisterTarget(system_i386_iphonesim_info);
  989. {$endif i386}
  990. {$ifdef m68k}
  991. RegisterImport(system_m68k_netbsd,timportlibbsd);
  992. RegisterExport(system_m68k_netbsd,texportlibbsd);
  993. RegisterTarget(system_m68k_netbsd_info);
  994. {$endif m68k}
  995. {$ifdef powerpc}
  996. RegisterImport(system_powerpc_darwin,timportlibdarwin);
  997. RegisterExport(system_powerpc_darwin,texportlibdarwin);
  998. RegisterTarget(system_powerpc_darwin_info);
  999. RegisterImport(system_powerpc_netbsd,timportlibbsd);
  1000. RegisterExport(system_powerpc_netbsd,texportlibbsd);
  1001. RegisterTarget(system_powerpc_netbsd_info);
  1002. {$endif powerpc}
  1003. {$ifdef powerpc64}
  1004. RegisterImport(system_powerpc64_darwin,timportlibdarwin);
  1005. RegisterExport(system_powerpc64_darwin,texportlibdarwin);
  1006. RegisterTarget(system_powerpc64_darwin_info);
  1007. {$endif powerpc64}
  1008. {$ifdef arm}
  1009. RegisterImport(system_arm_darwin,timportlibdarwin);
  1010. RegisterExport(system_arm_darwin,texportlibdarwin);
  1011. RegisterTarget(system_arm_darwin_info);
  1012. RegisterImport(system_arm_netbsd,timportlibbsd);
  1013. RegisterExport(system_arm_netbsd,texportlibbsd);
  1014. RegisterTarget(system_arm_netbsd_info);
  1015. {$endif arm}
  1016. {$ifdef aarch64}
  1017. RegisterImport(system_aarch64_darwin,timportlibdarwin);
  1018. RegisterExport(system_aarch64_darwin,texportlibdarwin);
  1019. RegisterTarget(system_aarch64_darwin_info);
  1020. {$endif aarch64}
  1021. RegisterRes(res_elf_info,TWinLikeResourceFile);
  1022. RegisterRes(res_macho_info,TWinLikeResourceFile);
  1023. end.