t_android.pas 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. {
  2. Copyright (c) 1998-2008 by Peter Vreman
  3. This unit implements support import,export,link routines
  4. for the Android target
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit t_android;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. aasmdata,
  23. symsym,symdef,ppu,
  24. import,export,expunix,link;
  25. type
  26. timportlibandroid=class(timportlib)
  27. procedure generatelib;override;
  28. end;
  29. texportlibandroid=class(texportlibunix)
  30. procedure setfininame(list: TAsmList; const s: string); override;
  31. end;
  32. tlinkerandroid=class(texternallinker)
  33. private
  34. libctype:(libc5,glibc2,glibc21,uclibc);
  35. cprtobj,
  36. gprtobj,
  37. prtobj : string[80];
  38. reorder : boolean;
  39. linklibc: boolean;
  40. Function WriteResponseFile(isdll:boolean) : Boolean;
  41. public
  42. constructor Create;override;
  43. procedure SetDefaultInfo;override;
  44. procedure InitSysInitUnitName;override;
  45. function MakeExecutable:boolean;override;
  46. function MakeSharedLibrary:boolean;override;
  47. procedure LoadPredefinedLibraryOrder; override;
  48. end;
  49. implementation
  50. uses
  51. SysUtils,
  52. cutils,cfileutl,cclasses,
  53. verbose,systems,globtype,globals,
  54. symconst,script,
  55. fmodule,
  56. aasmbase,aasmtai,aasmcpu,cpubase,
  57. cgbase,cgobj,cgutils,ogbase,ncgutil,
  58. comprsrc,
  59. rescmn, i_android
  60. ;
  61. {*****************************************************************************
  62. TIMPORTLIBANDROID
  63. *****************************************************************************}
  64. procedure timportlibandroid.generatelib;
  65. var
  66. i : longint;
  67. ImportLibrary : TImportLibrary;
  68. begin
  69. for i:=0 to current_module.ImportLibraryList.Count-1 do
  70. begin
  71. ImportLibrary:=TImportLibrary(current_module.ImportLibraryList[i]);
  72. current_module.linkothersharedlibs.add(ImportLibrary.Name,link_always);
  73. end;
  74. end;
  75. {*****************************************************************************
  76. TEXPORTLIBANDROID
  77. *****************************************************************************}
  78. procedure texportlibandroid.setfininame(list: TAsmList; const s: string);
  79. begin
  80. { the problem with not having a .fini section is that a finalization
  81. routine in regular code can get "smart" linked away -> reference it
  82. just like the debug info }
  83. new_section(list,sec_fpc,'links',0);
  84. list.concat(Tai_const.Createname(s,0));
  85. inherited setfininame(list,s);
  86. end;
  87. {*****************************************************************************
  88. TLINKERANDROID
  89. *****************************************************************************}
  90. Constructor TLinkerAndroid.Create;
  91. begin
  92. Inherited Create;
  93. end;
  94. procedure TLinkerAndroid.SetDefaultInfo;
  95. {
  96. This will also detect which libc version will be used
  97. }
  98. const
  99. {$ifdef arm} platform_select='';{$endif} {unknown :( }
  100. {$ifdef i386} platform_select='';{$endif} {unknown :( }
  101. var
  102. defdynlinker: string;
  103. begin
  104. with Info do
  105. begin
  106. ExeCmd[1]:='ld '+platform_select+' $OPT $DYNLINK $STATIC $GCSECTIONS $STRIP -L. -o $EXE';
  107. { when we want to cross-link we need to override default library paths }
  108. if length(sysrootpath) > 0 then
  109. ExeCmd[1]:=ExeCmd[1]+' -T';
  110. ExeCmd[1]:=ExeCmd[1]+' $RES';
  111. DllCmd[1]:='ld '+platform_select+' $OPT $INIT $FINI $SONAME -shared -L. -o $EXE $RES';
  112. DllCmd[2]:='strip --strip-unneeded $EXE';
  113. ExtDbgCmd[1]:='objcopy --only-keep-debug $EXE $DBG';
  114. ExtDbgCmd[2]:='objcopy --add-gnu-debuglink=$DBG $EXE';
  115. ExtDbgCmd[3]:='strip --strip-unneeded $EXE';
  116. {$ifdef arm}
  117. defdynlinker:='/system/bin/linker';
  118. {$endif arm}
  119. {
  120. Search order:
  121. glibc 2.1+
  122. uclibc
  123. glibc 2.0
  124. If none is found (e.g. when cross compiling) glibc21 is assumed
  125. }
  126. if fileexists(sysrootpath+defdynlinker,false) then
  127. begin
  128. DynamicLinker:=defdynlinker;
  129. libctype:=glibc2;
  130. end
  131. else if fileexists(sysrootpath+'/lib/ld-uClibc.so.0',false) then
  132. begin
  133. dynamiclinker:='/lib/ld-uClibc.so.0';
  134. libctype:=uclibc;
  135. end
  136. else
  137. begin
  138. { when no dyn. linker is found, we are probably
  139. cross compiling, so use the default dyn. linker }
  140. DynamicLinker:=defdynlinker;
  141. {
  142. the default c startup script is gcrt0.as on all platforms
  143. except i386
  144. }
  145. libctype:=glibc2;
  146. end;
  147. end;
  148. end;
  149. procedure TLinkerAndroid.LoadPredefinedLibraryOrder;
  150. // put your linkorder/linkalias overrides here.
  151. // Note: assumes only called when reordering/aliasing is used.
  152. Begin
  153. if not (cs_link_no_default_lib_order in current_settings.globalswitches) Then
  154. Begin
  155. LinkLibraryOrder.add('gcc','',15);
  156. LinkLibraryOrder.add('c','',100);
  157. LinkLibraryOrder.add('gmon','',120);
  158. LinkLibraryOrder.add('dl','',140);
  159. LinkLibraryOrder.add('pthread','',160);
  160. end;
  161. End;
  162. Procedure TLinkerAndroid.InitSysInitUnitName;
  163. var
  164. csysinitunit,
  165. gsysinitunit : string[20];
  166. hp : tmodule;
  167. begin
  168. hp:=tmodule(loaded_units.first);
  169. while assigned(hp) do
  170. begin
  171. linklibc := hp.linkothersharedlibs.find('c');
  172. if linklibc then break;
  173. hp:=tmodule(hp.next);
  174. end;
  175. reorder := linklibc and ReOrderEntries;
  176. if current_module.islibrary then
  177. begin
  178. sysinitunit:='dll';
  179. csysinitunit:='dll';
  180. gsysinitunit:='dll';
  181. prtobj:='dllprt0';
  182. cprtobj:='dllprt0';
  183. gprtobj:='dllprt0';
  184. end
  185. else
  186. begin
  187. prtobj:='prt0';
  188. sysinitunit:='prc';
  189. case libctype of
  190. glibc21:
  191. begin
  192. cprtobj:='cprt21';
  193. gprtobj:='gprt21';
  194. csysinitunit:='c21';
  195. gsysinitunit:='c21g';
  196. end;
  197. uclibc:
  198. begin
  199. cprtobj:='ucprt0';
  200. gprtobj:='ugprt0';
  201. csysinitunit:='uc';
  202. gsysinitunit:='ucg';
  203. end
  204. else
  205. cprtobj:='cprt0';
  206. gprtobj:='gprt0';
  207. csysinitunit:='c';
  208. gsysinitunit:='g';
  209. end;
  210. end;
  211. if cs_profile in current_settings.moduleswitches then
  212. begin
  213. prtobj:=gprtobj;
  214. sysinitunit:=gsysinitunit;
  215. linklibc:=true;
  216. end
  217. else
  218. begin
  219. if linklibc then
  220. begin
  221. prtobj:=cprtobj;
  222. sysinitunit:=csysinitunit;
  223. end;
  224. end;
  225. sysinitunit:='si_'+sysinitunit;
  226. end;
  227. Function TLinkerAndroid.WriteResponseFile(isdll:boolean) : Boolean;
  228. Var
  229. linkres : TLinkRes;
  230. i : longint;
  231. HPath : TCmdStrListItem;
  232. s,s1,s2 : TCmdStr;
  233. found1,
  234. found2 : boolean;
  235. linksToSharedLibFiles : boolean;
  236. begin
  237. result:=False;
  238. { set special options for some targets }
  239. if cs_profile in current_settings.moduleswitches then
  240. begin
  241. if not(libctype in [glibc2,glibc21]) then
  242. AddSharedLibrary('gmon');
  243. AddSharedLibrary('c');
  244. end;
  245. { Open link.res file }
  246. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName);
  247. with linkres do
  248. begin
  249. { Write path to search libraries }
  250. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  251. while assigned(HPath) do
  252. begin
  253. Add('SEARCH_DIR('+maybequoted(HPath.Str)+')');
  254. HPath:=TCmdStrListItem(HPath.Next);
  255. end;
  256. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  257. while assigned(HPath) do
  258. begin
  259. Add('SEARCH_DIR('+maybequoted(HPath.Str)+')');
  260. HPath:=TCmdStrListItem(HPath.Next);
  261. end;
  262. { force local symbol resolution (i.e., inside the shared }
  263. { library itself) for all non-exorted symbols, otherwise }
  264. { several RTL symbols of FPC-compiled shared libraries }
  265. { will be bound to those of a single shared library or }
  266. { to the main program }
  267. if (isdll) then
  268. begin
  269. add('VERSION');
  270. add('{');
  271. add(' {');
  272. if not texportlibunix(exportlib).exportedsymnames.empty then
  273. begin
  274. add(' global:');
  275. repeat
  276. add(' '+texportlibunix(exportlib).exportedsymnames.getfirst+';');
  277. until texportlibunix(exportlib).exportedsymnames.empty;
  278. end;
  279. add(' local:');
  280. add(' *;');
  281. add(' };');
  282. add('}');
  283. end;
  284. StartSection('INPUT(');
  285. { add objectfiles, start with prt0 always }
  286. if not (target_info.system in systems_internal_sysinit) and (prtobj<>'') then
  287. AddFileName(maybequoted(FindObjectFile(prtobj,'',false)));
  288. { try to add crti and crtbegin if linking to C }
  289. if linklibc and (libctype<>uclibc) then
  290. begin
  291. { crti.o must come first }
  292. if librarysearchpath.FindFile('crti.o',false,s) then
  293. AddFileName(s);
  294. { then the crtbegin* }
  295. if cs_create_pic in current_settings.moduleswitches then
  296. begin
  297. if librarysearchpath.FindFile('crtbeginS.o',false,s) then
  298. AddFileName(s);
  299. end
  300. else
  301. if (cs_link_staticflag in current_settings.globalswitches) and
  302. librarysearchpath.FindFile('crtbeginT.o',false,s) then
  303. AddFileName(s)
  304. else if librarysearchpath.FindFile('crtbegin.o',false,s) then
  305. AddFileName(s);
  306. end;
  307. { main objectfiles }
  308. while not ObjectFiles.Empty do
  309. begin
  310. s:=ObjectFiles.GetFirst;
  311. if s<>'' then
  312. AddFileName(maybequoted(s));
  313. end;
  314. EndSection(')');
  315. { Write staticlibraries }
  316. if not StaticLibFiles.Empty then
  317. begin
  318. Add('GROUP(');
  319. While not StaticLibFiles.Empty do
  320. begin
  321. S:=StaticLibFiles.GetFirst;
  322. AddFileName(maybequoted(s))
  323. end;
  324. Add(')');
  325. end;
  326. // we must reorder here because the result could empty sharedlibfiles
  327. if reorder Then
  328. ExpandAndApplyOrder(SharedLibFiles);
  329. // after this point addition of shared libs not allowed.
  330. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  331. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  332. if (isdll) then
  333. begin
  334. Add('INPUT(');
  335. Add(info.DynamicLinker);
  336. Add(')');
  337. end;
  338. linksToSharedLibFiles := not SharedLibFiles.Empty;
  339. if not SharedLibFiles.Empty then
  340. begin
  341. if (SharedLibFiles.Count<>1) or
  342. (TCmdStrListItem(SharedLibFiles.First).Str<>'c') or
  343. reorder then
  344. begin
  345. Add('INPUT(');
  346. While not SharedLibFiles.Empty do
  347. begin
  348. S:=SharedLibFiles.GetFirst;
  349. if (s<>'c') or reorder then
  350. begin
  351. i:=Pos(target_info.sharedlibext,S);
  352. if i>0 then
  353. Delete(S,i,255);
  354. Add('-l'+s);
  355. end
  356. else
  357. begin
  358. linklibc:=true;
  359. end;
  360. end;
  361. Add(')');
  362. end
  363. else
  364. linklibc:=true;
  365. if (cs_link_staticflag in current_settings.globalswitches) or
  366. (linklibc and not reorder) then
  367. begin
  368. Add('GROUP(');
  369. { when we have -static for the linker the we also need libgcc }
  370. if (cs_link_staticflag in current_settings.globalswitches) then
  371. begin
  372. Add('-lgcc');
  373. if librarysearchpath.FindFile('libgcc_eh.a',false,s1) then
  374. Add('-lgcc_eh');
  375. end;
  376. { be sure that libc is the last lib }
  377. if linklibc and not reorder then
  378. Add('-lc');
  379. Add(')');
  380. end;
  381. end;
  382. { objects which must be at the end }
  383. if linklibc and (libctype<>uclibc) then
  384. begin
  385. if cs_create_pic in current_settings.moduleswitches then
  386. found1:=librarysearchpath.FindFile('crtendS.o',false,s1)
  387. else
  388. found1:=librarysearchpath.FindFile('crtend.o',false,s1);
  389. found2:=librarysearchpath.FindFile('crtn.o',false,s2);
  390. if found1 or found2 then
  391. begin
  392. Add('INPUT(');
  393. if found1 then
  394. AddFileName(s1);
  395. if found2 then
  396. AddFileName(s2);
  397. Add(')');
  398. end;
  399. end;
  400. {Entry point. Only needed for executables, set on the linker command line for
  401. shared libraries. }
  402. if (not isdll) then
  403. if (linksToSharedLibFiles and not linklibc) then
  404. add('ENTRY(_dynamic_start)')
  405. else
  406. add('ENTRY(_start)');
  407. {$ifdef ARM}
  408. if target_info.abi=abi_eabi then
  409. begin
  410. { from GNU ld (CodeSourcery Sourcery G++ Lite 2007q3-53) 2.18.50.20070820 }
  411. add('/* Script for -z combreloc: combine and sort reloc sections */');
  412. add('OUTPUT_FORMAT("elf32-littlearm", "elf32-bigarm",');
  413. add(' "elf32-littlearm")');
  414. add('OUTPUT_ARCH(arm)');
  415. add('SEARCH_DIR("=/usr/local/lib"); SEARCH_DIR("=/lib"); SEARCH_DIR("=/usr/lib");');
  416. add('SECTIONS');
  417. add('{');
  418. add(' /* Read-only sections, merged into text segment: */');
  419. add(' PROVIDE (__executable_start = 0x8000); . = 0x8000 + SIZEOF_HEADERS;');
  420. add(' .interp : { *(.interp) }');
  421. add(' .note.gnu.build-id : { *(.note.gnu.build-id) }');
  422. add(' .hash : { *(.hash) }');
  423. add(' .gnu.hash : { *(.gnu.hash) }');
  424. add(' .dynsym : { *(.dynsym) }');
  425. add(' .dynstr : { *(.dynstr) }');
  426. add(' .gnu.version : { *(.gnu.version) }');
  427. add(' .gnu.version_d : { *(.gnu.version_d) }');
  428. add(' .gnu.version_r : { *(.gnu.version_r) }');
  429. add(' .rel.dyn :');
  430. add(' {');
  431. add(' *(.rel.init)');
  432. add(' *(.rel.text .rel.text.* .rel.gnu.linkonce.t.*)');
  433. add(' *(.rel.fini)');
  434. add(' *(.rel.rodata .rel.rodata.* .rel.gnu.linkonce.r.*)');
  435. add(' *(.rel.data.rel.ro* .rel.gnu.linkonce.d.rel.ro.*)');
  436. add(' *(.rel.data .rel.data.* .rel.gnu.linkonce.d.*)');
  437. add(' *(.rel.tdata .rel.tdata.* .rel.gnu.linkonce.td.*)');
  438. add(' *(.rel.tbss .rel.tbss.* .rel.gnu.linkonce.tb.*)');
  439. add(' *(.rel.ctors)');
  440. add(' *(.rel.dtors)');
  441. add(' *(.rel.got)');
  442. add(' *(.rel.bss .rel.bss.* .rel.gnu.linkonce.b.*)');
  443. add(' }');
  444. add(' .rela.dyn :');
  445. add(' {');
  446. add(' *(.rela.init)');
  447. add(' *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*)');
  448. add(' *(.rela.fini)');
  449. add(' *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*)');
  450. add(' *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*)');
  451. add(' *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*)');
  452. add(' *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*)');
  453. add(' *(.rela.ctors)');
  454. add(' *(.rela.dtors)');
  455. add(' *(.rela.got)');
  456. add(' *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*)');
  457. add(' }');
  458. add(' .rel.plt : { *(.rel.plt) }');
  459. add(' .rela.plt : { *(.rela.plt) }');
  460. add(' .init :');
  461. add(' {');
  462. add(' KEEP (*(.init))');
  463. add(' } =0');
  464. add(' .plt : { *(.plt) }');
  465. add(' .text :');
  466. add(' {');
  467. add(' *(.text .stub .text.* .gnu.linkonce.t.*)');
  468. add(' KEEP (*(.text.*personality*))');
  469. add(' /* .gnu.warning sections are handled specially by elf32.em. */');
  470. add(' *(.gnu.warning)');
  471. add(' *(.glue_7t) *(.glue_7) *(.vfp11_veneer)');
  472. add(' } =0');
  473. add(' .fini :');
  474. add(' {');
  475. add(' KEEP (*(.fini))');
  476. add(' } =0');
  477. add(' PROVIDE (__etext = .);');
  478. add(' PROVIDE (_etext = .);');
  479. add(' PROVIDE (etext = .);');
  480. add(' .rodata : { *(.rodata .rodata.* .gnu.linkonce.r.*) }');
  481. add(' .rodata1 : { *(.rodata1) }');
  482. add(' .ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) }');
  483. add(' __exidx_start = .;');
  484. add(' .ARM.exidx : { *(.ARM.exidx* .gnu.linkonce.armexidx.*) }');
  485. add(' __exidx_end = .;');
  486. add(' .eh_frame_hdr : { *(.eh_frame_hdr) }');
  487. add(' .eh_frame : ONLY_IF_RO { KEEP (*(.eh_frame)) }');
  488. add(' .gcc_except_table : ONLY_IF_RO { *(.gcc_except_table .gcc_except_table.*) }');
  489. add(' /* Adjust the address for the data segment. We want to adjust up to');
  490. add(' the same address within the page on the next page up. */');
  491. add(' . = ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1));');
  492. add(' /* Exception handling */');
  493. add(' .eh_frame : ONLY_IF_RW { KEEP (*(.eh_frame)) }');
  494. add(' .gcc_except_table : ONLY_IF_RW { *(.gcc_except_table .gcc_except_table.*) }');
  495. add(' /* Thread Local Storage sections */');
  496. add(' .tdata : { *(.tdata .tdata.* .gnu.linkonce.td.*) }');
  497. add(' .tbss : { *(.tbss .tbss.* .gnu.linkonce.tb.*) *(.tcommon) }');
  498. add(' .preinit_array :');
  499. add(' {');
  500. add(' PROVIDE_HIDDEN (__preinit_array_start = .);');
  501. add(' KEEP (*(.preinit_array))');
  502. add(' PROVIDE_HIDDEN (__preinit_array_end = .);');
  503. add(' }');
  504. add(' .init_array :');
  505. add(' {');
  506. add(' PROVIDE_HIDDEN (__init_array_start = .);');
  507. add(' KEEP (*(SORT(.init_array.*)))');
  508. add(' KEEP (*(.init_array))');
  509. add(' PROVIDE_HIDDEN (__init_array_end = .);');
  510. add(' }');
  511. add(' .fini_array :');
  512. add(' {');
  513. add(' PROVIDE_HIDDEN (__fini_array_start = .);');
  514. add(' KEEP (*(.fini_array))');
  515. add(' KEEP (*(SORT(.fini_array.*)))');
  516. add(' PROVIDE_HIDDEN (__fini_array_end = .);');
  517. add(' }');
  518. add(' .ctors :');
  519. add(' {');
  520. add(' /* gcc uses crtbegin.o to find the start of');
  521. add(' the constructors, so we make sure it is');
  522. add(' first. Because this is a wildcard, it');
  523. add(' doesn''t matter if the user does not');
  524. add(' actually link against crtbegin.o; the');
  525. add(' linker won''t look for a file to match a');
  526. add(' wildcard. The wildcard also means that it');
  527. add(' doesn''t matter which directory crtbegin.o');
  528. add(' is in. */');
  529. add(' KEEP (*crtbegin.o(.ctors))');
  530. add(' KEEP (*crtbegin?.o(.ctors))');
  531. add(' /* We don''t want to include the .ctor section from');
  532. add(' the crtend.o file until after the sorted ctors.');
  533. add(' The .ctor section from the crtend file contains the');
  534. add(' end of ctors marker and it must be last */');
  535. add(' KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .ctors))');
  536. add(' KEEP (*(SORT(.ctors.*)))');
  537. add(' KEEP (*(.ctors))');
  538. add(' }');
  539. add(' .dtors :');
  540. add(' {');
  541. add(' KEEP (*crtbegin.o(.dtors))');
  542. add(' KEEP (*crtbegin?.o(.dtors))');
  543. add(' KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors))');
  544. add(' KEEP (*(SORT(.dtors.*)))');
  545. add(' KEEP (*(.dtors))');
  546. add(' }');
  547. add(' .jcr : { KEEP (*(.jcr)) }');
  548. add(' .data.rel.ro : { *(.data.rel.ro.local* .gnu.linkonce.d.rel.ro.local.*) *(.data.rel.ro* .gnu.linkonce.d.rel.ro.*) }');
  549. add(' .dynamic : { *(.dynamic) }');
  550. add(' .got : { *(.got.plt) *(.got) }');
  551. add(' .data :');
  552. add(' {');
  553. add(' __data_start = . ;');
  554. add(' *(.data .data.* .gnu.linkonce.d.*)');
  555. { extra by FPC }
  556. add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  557. add(' KEEP (*(.gnu.linkonce.d.*personality*))');
  558. add(' SORT(CONSTRUCTORS)');
  559. add(' }');
  560. add(' .data1 : { *(.data1) }');
  561. add(' _edata = .; PROVIDE (edata = .);');
  562. add(' __bss_start = .;');
  563. add(' __bss_start__ = .;');
  564. add(' .bss :');
  565. add(' {');
  566. add(' *(.dynbss)');
  567. add(' *(.bss .bss.* .gnu.linkonce.b.*)');
  568. add(' *(COMMON)');
  569. add(' /* Align here to ensure that the .bss section occupies space up to');
  570. add(' _end. Align after .bss to ensure correct alignment even if the');
  571. add(' .bss section disappears because there are no input sections.');
  572. add(' FIXME: Why do we need it? When there is no .bss section, we don''t');
  573. add(' pad the .data section. */');
  574. add(' . = ALIGN(. != 0 ? 32 / 8 : 1);');
  575. add(' }');
  576. add(' _bss_end__ = . ; __bss_end__ = . ;');
  577. add(' . = ALIGN(32 / 8);');
  578. add(' . = ALIGN(32 / 8);');
  579. add(' __end__ = . ;');
  580. add(' _end = .; PROVIDE (end = .);');
  581. add(' /* Stabs debugging sections. */');
  582. add(' .stab 0 : { *(.stab) }');
  583. add(' .stabstr 0 : { *(.stabstr) }');
  584. add(' .stab.excl 0 : { *(.stab.excl) }');
  585. add(' .stab.exclstr 0 : { *(.stab.exclstr) }');
  586. add(' .stab.index 0 : { *(.stab.index) }');
  587. add(' .stab.indexstr 0 : { *(.stab.indexstr) }');
  588. add(' .comment 0 : { *(.comment) }');
  589. add(' /* DWARF debug sections.');
  590. add(' Symbols in the DWARF debugging sections are relative to the beginning');
  591. add(' of the section so we begin them at 0. */');
  592. add(' /* DWARF 1 */');
  593. add(' .debug 0 : { *(.debug) }');
  594. add(' .line 0 : { *(.line) }');
  595. add(' /* GNU DWARF 1 extensions */');
  596. add(' .debug_srcinfo 0 : { *(.debug_srcinfo) }');
  597. add(' .debug_sfnames 0 : { *(.debug_sfnames) }');
  598. add(' /* DWARF 1.1 and DWARF 2 */');
  599. add(' .debug_aranges 0 : { *(.debug_aranges) }');
  600. add(' .debug_pubnames 0 : { *(.debug_pubnames) }');
  601. add(' /* DWARF 2 */');
  602. add(' .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) }');
  603. add(' .debug_abbrev 0 : { *(.debug_abbrev) }');
  604. add(' .debug_line 0 : { *(.debug_line) }');
  605. add(' .debug_frame 0 : { *(.debug_frame) }');
  606. add(' .debug_str 0 : { *(.debug_str) }');
  607. add(' .debug_loc 0 : { *(.debug_loc) }');
  608. add(' .debug_macinfo 0 : { *(.debug_macinfo) }');
  609. add(' /* SGI/MIPS DWARF 2 extensions */');
  610. add(' .debug_weaknames 0 : { *(.debug_weaknames) }');
  611. add(' .debug_funcnames 0 : { *(.debug_funcnames) }');
  612. add(' .debug_typenames 0 : { *(.debug_typenames) }');
  613. add(' .debug_varnames 0 : { *(.debug_varnames) }');
  614. add(' /* DWARF 3 */');
  615. add(' .debug_pubtypes 0 : { *(.debug_pubtypes) }');
  616. add(' .debug_ranges 0 : { *(.debug_ranges) }');
  617. add(' .stack 0x80000 :');
  618. add(' {');
  619. add(' _stack = .;');
  620. add(' *(.stack)');
  621. add(' }');
  622. add(' .ARM.attributes 0 : { KEEP (*(.ARM.attributes)) KEEP (*(.gnu.attributes)) }');
  623. add(' .note.gnu.arm.ident 0 : { KEEP (*(.note.gnu.arm.ident)) }');
  624. add(' /DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) }');
  625. add('}');
  626. end;
  627. {$endif ARM}
  628. { Write and Close response }
  629. writetodisk;
  630. Free;
  631. end;
  632. WriteResponseFile:=True;
  633. end;
  634. function TLinkerAndroid.MakeExecutable:boolean;
  635. var
  636. i : longint;
  637. binstr,
  638. cmdstr : TCmdStr;
  639. success : boolean;
  640. DynLinkStr : string;
  641. GCSectionsStr,
  642. StaticStr,
  643. StripStr : string[40];
  644. begin
  645. if not(cs_link_nolink in current_settings.globalswitches) then
  646. Message1(exec_i_linking,current_module.exefilename^);
  647. { Create some replacements }
  648. StaticStr:='';
  649. StripStr:='';
  650. GCSectionsStr:='';
  651. DynLinkStr:='';
  652. if (cs_link_staticflag in current_settings.globalswitches) then
  653. StaticStr:='-static';
  654. if (cs_link_strip in current_settings.globalswitches) and
  655. not(cs_link_separate_dbg_file in current_settings.globalswitches) then
  656. StripStr:='-s';
  657. if (cs_link_map in current_settings.globalswitches) then
  658. StripStr:='-Map '+maybequoted(ChangeFileExt(current_module.exefilename^,'.map'));
  659. if create_smartlink_sections then
  660. GCSectionsStr:='--gc-sections';
  661. If (cs_profile in current_settings.moduleswitches) or
  662. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty)) then
  663. begin
  664. DynLinkStr:='--dynamic-linker='+Info.DynamicLinker;
  665. if cshared then
  666. DynLinkStr:=DynLinkStr+' --shared ';
  667. if rlinkpath<>'' then
  668. DynLinkStr:=DynLinkStr+' --rpath-link '+rlinkpath;
  669. End;
  670. { Write used files and libraries }
  671. WriteResponseFile(false);
  672. { Call linker }
  673. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  674. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename^));
  675. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  676. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  677. Replace(cmdstr,'$STATIC',StaticStr);
  678. Replace(cmdstr,'$STRIP',StripStr);
  679. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  680. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  681. { create dynamic symbol table? }
  682. if HasExports then
  683. cmdstr:=cmdstr+' -E';
  684. success:=DoExec(FindUtil(utilsprefix+BinStr),CmdStr,true,false);
  685. { Create external .dbg file with debuginfo }
  686. if success and (cs_link_separate_dbg_file in current_settings.globalswitches) then
  687. begin
  688. for i:=1 to 3 do
  689. begin
  690. SplitBinCmd(Info.ExtDbgCmd[i],binstr,cmdstr);
  691. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename^));
  692. Replace(cmdstr,'$DBGFN',maybequoted(extractfilename(current_module.dbgfilename^)));
  693. Replace(cmdstr,'$DBG',maybequoted(current_module.dbgfilename^));
  694. success:=DoExec(FindUtil(utilsprefix+BinStr),CmdStr,true,false);
  695. if not success then
  696. break;
  697. end;
  698. end;
  699. { Remove ReponseFile }
  700. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  701. DeleteFile(outputexedir+Info.ResName);
  702. MakeExecutable:=success; { otherwise a recursive call to link method }
  703. end;
  704. Function TLinkerAndroid.MakeSharedLibrary:boolean;
  705. var
  706. InitStr,
  707. FiniStr,
  708. SoNameStr : string[80];
  709. binstr,
  710. cmdstr : TCmdStr;
  711. success : boolean;
  712. begin
  713. MakeSharedLibrary:=false;
  714. if not(cs_link_nolink in current_settings.globalswitches) then
  715. Message1(exec_i_linking,current_module.sharedlibfilename^);
  716. { Write used files and libraries }
  717. WriteResponseFile(true);
  718. { Create some replacements }
  719. { note: linux does not use exportlib.initname/fininame due to the custom startup code }
  720. InitStr:='-init FPC_SHARED_LIB_START';
  721. FiniStr:='-fini FPC_LIB_EXIT';
  722. SoNameStr:='-soname '+ExtractFileName(current_module.sharedlibfilename^);
  723. { Call linker }
  724. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  725. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename^));
  726. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  727. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  728. Replace(cmdstr,'$INIT',InitStr);
  729. Replace(cmdstr,'$FINI',FiniStr);
  730. Replace(cmdstr,'$SONAME',SoNameStr);
  731. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  732. { Strip the library ? }
  733. if success and (cs_link_strip in current_settings.globalswitches) then
  734. begin
  735. { only remove non global symbols and debugging info for a library }
  736. Info.DllCmd[2]:='strip --discard-all --strip-debug $EXE';
  737. SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  738. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename^));
  739. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  740. end;
  741. { Remove ReponseFile }
  742. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  743. DeleteFile(outputexedir+Info.ResName);
  744. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  745. end;
  746. {*****************************************************************************
  747. Initialize
  748. *****************************************************************************}
  749. initialization
  750. {$ifdef ARM}
  751. RegisterExternalLinker(system_arm_android_info,TLinkerAndroid);
  752. RegisterImport(system_arm_android,timportlibandroid);
  753. RegisterExport(system_arm_android,texportlibandroid);
  754. RegisterTarget(system_arm_android_info);
  755. {$endif ARM}
  756. {$ifdef I386}
  757. RegisterExternalLinker(system_i386_android_info,TLinkerAndroid);
  758. RegisterImport(system_i386_android,timportlibandroid);
  759. RegisterExport(system_i386_android,texportlibandroid);
  760. RegisterTarget(system_i386_android_info);
  761. {$endif I386}
  762. RegisterRes(res_elf_info,TWinLikeResourceFile);
  763. end.