t_android.pas 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  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:(bionic);
  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. defdynlinker:='/system/bin/linker';
  117. {
  118. There is only one C library on android, the bionic libc
  119. }
  120. libctype:=bionic;
  121. if fileexists(sysrootpath+defdynlinker,false) then
  122. begin
  123. DynamicLinker:=defdynlinker;
  124. end
  125. else
  126. begin
  127. { when no dyn. linker is found, we are probably
  128. cross compiling, so use the default dyn. linker }
  129. DynamicLinker:=defdynlinker;
  130. end;
  131. end;
  132. end;
  133. procedure TLinkerAndroid.LoadPredefinedLibraryOrder;
  134. // put your linkorder/linkalias overrides here.
  135. // Note: assumes only called when reordering/aliasing is used.
  136. Begin
  137. if not (cs_link_no_default_lib_order in current_settings.globalswitches) Then
  138. Begin
  139. LinkLibraryOrder.add('gcc','',15);
  140. LinkLibraryOrder.add('c','',100);
  141. LinkLibraryOrder.add('gmon','',120);
  142. LinkLibraryOrder.add('dl','',140);
  143. LinkLibraryOrder.add('pthread','',160);
  144. end;
  145. End;
  146. Procedure TLinkerAndroid.InitSysInitUnitName;
  147. var
  148. csysinitunit,
  149. gsysinitunit : string[20];
  150. hp : tmodule;
  151. begin
  152. hp:=tmodule(loaded_units.first);
  153. while assigned(hp) do
  154. begin
  155. linklibc := hp.linkothersharedlibs.find('c');
  156. if linklibc then break;
  157. hp:=tmodule(hp.next);
  158. end;
  159. reorder := linklibc and ReOrderEntries;
  160. if current_module.islibrary then
  161. begin
  162. sysinitunit:='dll';
  163. csysinitunit:='dll';
  164. gsysinitunit:='dll';
  165. prtobj:='dllprt0';
  166. cprtobj:='dllprt0';
  167. gprtobj:='dllprt0';
  168. end
  169. else
  170. begin
  171. prtobj:='prt0';
  172. sysinitunit:='prc';
  173. case libctype of
  174. bionic:
  175. begin
  176. cprtobj:='cprt0';
  177. gprtobj:='gprt0';
  178. csysinitunit:='c';
  179. gsysinitunit:='g';
  180. end
  181. else
  182. runerror(2012080901);
  183. end;
  184. end;
  185. if cs_profile in current_settings.moduleswitches then
  186. begin
  187. prtobj:=gprtobj;
  188. sysinitunit:=gsysinitunit;
  189. linklibc:=true;
  190. end
  191. else
  192. begin
  193. if linklibc then
  194. begin
  195. prtobj:=cprtobj;
  196. sysinitunit:=csysinitunit;
  197. end;
  198. end;
  199. sysinitunit:='si_'+sysinitunit;
  200. end;
  201. Function TLinkerAndroid.WriteResponseFile(isdll:boolean) : Boolean;
  202. Var
  203. linkres : TLinkRes;
  204. i : longint;
  205. HPath : TCmdStrListItem;
  206. s,s1,s2 : TCmdStr;
  207. found1,
  208. found2 : boolean;
  209. linksToSharedLibFiles : boolean;
  210. begin
  211. result:=False;
  212. { set special options for some targets }
  213. if cs_profile in current_settings.moduleswitches then
  214. begin
  215. AddSharedLibrary('c');
  216. end;
  217. { Open link.res file }
  218. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  219. with linkres do
  220. begin
  221. { Write path to search libraries }
  222. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  223. while assigned(HPath) do
  224. begin
  225. Add('SEARCH_DIR('+maybequoted(HPath.Str)+')');
  226. HPath:=TCmdStrListItem(HPath.Next);
  227. end;
  228. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  229. while assigned(HPath) do
  230. begin
  231. Add('SEARCH_DIR('+maybequoted(HPath.Str)+')');
  232. HPath:=TCmdStrListItem(HPath.Next);
  233. end;
  234. { force local symbol resolution (i.e., inside the shared }
  235. { library itself) for all non-exorted symbols, otherwise }
  236. { several RTL symbols of FPC-compiled shared libraries }
  237. { will be bound to those of a single shared library or }
  238. { to the main program }
  239. if (isdll) then
  240. begin
  241. add('VERSION');
  242. add('{');
  243. add(' {');
  244. if not texportlibunix(exportlib).exportedsymnames.empty then
  245. begin
  246. add(' global:');
  247. repeat
  248. add(' '+texportlibunix(exportlib).exportedsymnames.getfirst+';');
  249. until texportlibunix(exportlib).exportedsymnames.empty;
  250. end;
  251. add(' local:');
  252. add(' *;');
  253. add(' };');
  254. add('}');
  255. end;
  256. StartSection('INPUT(');
  257. { add objectfiles, start with prt0 always }
  258. if not (target_info.system in systems_internal_sysinit) and (prtobj<>'') then
  259. AddFileName(maybequoted(FindObjectFile(prtobj,'',false)));
  260. { try to add crti and crtbegin if linking to C }
  261. if linklibc then
  262. begin
  263. { crti.o must come first }
  264. if librarysearchpath.FindFile('crti.o',false,s) then
  265. AddFileName(s);
  266. { then the crtbegin* }
  267. if cs_create_pic in current_settings.moduleswitches then
  268. begin
  269. if librarysearchpath.FindFile('crtbeginS.o',false,s) then
  270. AddFileName(s);
  271. end
  272. else
  273. if (cs_link_staticflag in current_settings.globalswitches) and
  274. librarysearchpath.FindFile('crtbeginT.o',false,s) then
  275. AddFileName(s)
  276. else if librarysearchpath.FindFile('crtbegin.o',false,s) then
  277. AddFileName(s);
  278. end;
  279. { main objectfiles }
  280. while not ObjectFiles.Empty do
  281. begin
  282. s:=ObjectFiles.GetFirst;
  283. if s<>'' then
  284. AddFileName(maybequoted(s));
  285. end;
  286. EndSection(')');
  287. { Write staticlibraries }
  288. if not StaticLibFiles.Empty then
  289. begin
  290. Add('GROUP(');
  291. While not StaticLibFiles.Empty do
  292. begin
  293. S:=StaticLibFiles.GetFirst;
  294. AddFileName(maybequoted(s))
  295. end;
  296. Add(')');
  297. end;
  298. // we must reorder here because the result could empty sharedlibfiles
  299. if reorder Then
  300. ExpandAndApplyOrder(SharedLibFiles);
  301. // after this point addition of shared libs not allowed.
  302. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  303. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  304. if (isdll) then
  305. begin
  306. Add('INPUT(');
  307. Add(info.DynamicLinker);
  308. Add(')');
  309. end;
  310. linksToSharedLibFiles := not SharedLibFiles.Empty;
  311. if not SharedLibFiles.Empty then
  312. begin
  313. if (SharedLibFiles.Count<>1) or
  314. (TCmdStrListItem(SharedLibFiles.First).Str<>'c') or
  315. reorder then
  316. begin
  317. Add('INPUT(');
  318. While not SharedLibFiles.Empty do
  319. begin
  320. S:=SharedLibFiles.GetFirst;
  321. if (s<>'c') or reorder then
  322. begin
  323. i:=Pos(target_info.sharedlibext,S);
  324. if i>0 then
  325. Delete(S,i,255);
  326. Add('-l'+s);
  327. end
  328. else
  329. begin
  330. linklibc:=true;
  331. end;
  332. end;
  333. Add(')');
  334. end
  335. else
  336. linklibc:=true;
  337. if (cs_link_staticflag in current_settings.globalswitches) or
  338. (linklibc and not reorder) then
  339. begin
  340. Add('GROUP(');
  341. { when we have -static for the linker the we also need libgcc }
  342. if (cs_link_staticflag in current_settings.globalswitches) then
  343. begin
  344. Add('-lgcc');
  345. if librarysearchpath.FindFile('libgcc_eh.a',false,s1) then
  346. Add('-lgcc_eh');
  347. end;
  348. { be sure that libc is the last lib }
  349. if linklibc and not reorder then
  350. Add('-lc');
  351. Add(')');
  352. end;
  353. end;
  354. { objects which must be at the end }
  355. if linklibc then
  356. begin
  357. if cs_create_pic in current_settings.moduleswitches then
  358. found1:=librarysearchpath.FindFile('crtendS.o',false,s1)
  359. else
  360. found1:=librarysearchpath.FindFile('crtend.o',false,s1);
  361. found2:=librarysearchpath.FindFile('crtn.o',false,s2);
  362. if found1 or found2 then
  363. begin
  364. Add('INPUT(');
  365. if found1 then
  366. AddFileName(s1);
  367. if found2 then
  368. AddFileName(s2);
  369. Add(')');
  370. end;
  371. end;
  372. {Entry point. Only needed for executables, set on the linker command line for
  373. shared libraries. }
  374. if (not isdll) then
  375. if (linksToSharedLibFiles and not linklibc) then
  376. add('ENTRY(_dynamic_start)')
  377. else
  378. add('ENTRY(_start)');
  379. {$ifdef ARM}
  380. if target_info.abi=abi_eabi then
  381. begin
  382. { from GNU ld (CodeSourcery Sourcery G++ Lite 2007q3-53) 2.18.50.20070820 }
  383. add('/* Script for -z combreloc: combine and sort reloc sections */');
  384. add('OUTPUT_FORMAT("elf32-littlearm", "elf32-bigarm",');
  385. add(' "elf32-littlearm")');
  386. add('OUTPUT_ARCH(arm)');
  387. add('SEARCH_DIR("=/usr/local/lib"); SEARCH_DIR("=/lib"); SEARCH_DIR("=/usr/lib");');
  388. add('SECTIONS');
  389. add('{');
  390. add(' /* Read-only sections, merged into text segment: */');
  391. add(' PROVIDE (__executable_start = 0x8000); . = 0x8000 + SIZEOF_HEADERS;');
  392. add(' .interp : { *(.interp) }');
  393. add(' .note.gnu.build-id : { *(.note.gnu.build-id) }');
  394. add(' .hash : { *(.hash) }');
  395. add(' .gnu.hash : { *(.gnu.hash) }');
  396. add(' .dynsym : { *(.dynsym) }');
  397. add(' .dynstr : { *(.dynstr) }');
  398. add(' .gnu.version : { *(.gnu.version) }');
  399. add(' .gnu.version_d : { *(.gnu.version_d) }');
  400. add(' .gnu.version_r : { *(.gnu.version_r) }');
  401. add(' .rel.dyn :');
  402. add(' {');
  403. add(' *(.rel.init)');
  404. add(' *(.rel.text .rel.text.* .rel.gnu.linkonce.t.*)');
  405. add(' *(.rel.fini)');
  406. add(' *(.rel.rodata .rel.rodata.* .rel.gnu.linkonce.r.*)');
  407. add(' *(.rel.data.rel.ro* .rel.gnu.linkonce.d.rel.ro.*)');
  408. add(' *(.rel.data .rel.data.* .rel.gnu.linkonce.d.*)');
  409. add(' *(.rel.tdata .rel.tdata.* .rel.gnu.linkonce.td.*)');
  410. add(' *(.rel.tbss .rel.tbss.* .rel.gnu.linkonce.tb.*)');
  411. add(' *(.rel.ctors)');
  412. add(' *(.rel.dtors)');
  413. add(' *(.rel.got)');
  414. add(' *(.rel.bss .rel.bss.* .rel.gnu.linkonce.b.*)');
  415. add(' }');
  416. add(' .rela.dyn :');
  417. add(' {');
  418. add(' *(.rela.init)');
  419. add(' *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*)');
  420. add(' *(.rela.fini)');
  421. add(' *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*)');
  422. add(' *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*)');
  423. add(' *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*)');
  424. add(' *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*)');
  425. add(' *(.rela.ctors)');
  426. add(' *(.rela.dtors)');
  427. add(' *(.rela.got)');
  428. add(' *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*)');
  429. add(' }');
  430. add(' .rel.plt : { *(.rel.plt) }');
  431. add(' .rela.plt : { *(.rela.plt) }');
  432. add(' .init :');
  433. add(' {');
  434. add(' KEEP (*(.init))');
  435. add(' } =0');
  436. add(' .plt : { *(.plt) }');
  437. add(' .text :');
  438. add(' {');
  439. add(' *(.text .stub .text.* .gnu.linkonce.t.*)');
  440. add(' KEEP (*(.text.*personality*))');
  441. add(' /* .gnu.warning sections are handled specially by elf32.em. */');
  442. add(' *(.gnu.warning)');
  443. add(' *(.glue_7t) *(.glue_7) *(.vfp11_veneer)');
  444. add(' } =0');
  445. add(' .fini :');
  446. add(' {');
  447. add(' KEEP (*(.fini))');
  448. add(' } =0');
  449. add(' PROVIDE (__etext = .);');
  450. add(' PROVIDE (_etext = .);');
  451. add(' PROVIDE (etext = .);');
  452. add(' .rodata : { *(.rodata .rodata.* .gnu.linkonce.r.*) }');
  453. add(' .rodata1 : { *(.rodata1) }');
  454. add(' .ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) }');
  455. add(' __exidx_start = .;');
  456. add(' .ARM.exidx : { *(.ARM.exidx* .gnu.linkonce.armexidx.*) }');
  457. add(' __exidx_end = .;');
  458. add(' .eh_frame_hdr : { *(.eh_frame_hdr) }');
  459. add(' .eh_frame : ONLY_IF_RO { KEEP (*(.eh_frame)) }');
  460. add(' .gcc_except_table : ONLY_IF_RO { *(.gcc_except_table .gcc_except_table.*) }');
  461. add(' /* Adjust the address for the data segment. We want to adjust up to');
  462. add(' the same address within the page on the next page up. */');
  463. add(' . = ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1));');
  464. add(' /* Exception handling */');
  465. add(' .eh_frame : ONLY_IF_RW { KEEP (*(.eh_frame)) }');
  466. add(' .gcc_except_table : ONLY_IF_RW { *(.gcc_except_table .gcc_except_table.*) }');
  467. add(' /* Thread Local Storage sections */');
  468. add(' .tdata : { *(.tdata .tdata.* .gnu.linkonce.td.*) }');
  469. add(' .tbss : { *(.tbss .tbss.* .gnu.linkonce.tb.*) *(.tcommon) }');
  470. add(' .preinit_array :');
  471. add(' {');
  472. add(' PROVIDE_HIDDEN (__preinit_array_start = .);');
  473. add(' KEEP (*(.preinit_array))');
  474. add(' PROVIDE_HIDDEN (__preinit_array_end = .);');
  475. add(' }');
  476. add(' .init_array :');
  477. add(' {');
  478. add(' PROVIDE_HIDDEN (__init_array_start = .);');
  479. add(' KEEP (*(SORT(.init_array.*)))');
  480. add(' KEEP (*(.init_array))');
  481. add(' PROVIDE_HIDDEN (__init_array_end = .);');
  482. add(' }');
  483. add(' .fini_array :');
  484. add(' {');
  485. add(' PROVIDE_HIDDEN (__fini_array_start = .);');
  486. add(' KEEP (*(.fini_array))');
  487. add(' KEEP (*(SORT(.fini_array.*)))');
  488. add(' PROVIDE_HIDDEN (__fini_array_end = .);');
  489. add(' }');
  490. add(' .ctors :');
  491. add(' {');
  492. add(' /* gcc uses crtbegin.o to find the start of');
  493. add(' the constructors, so we make sure it is');
  494. add(' first. Because this is a wildcard, it');
  495. add(' doesn''t matter if the user does not');
  496. add(' actually link against crtbegin.o; the');
  497. add(' linker won''t look for a file to match a');
  498. add(' wildcard. The wildcard also means that it');
  499. add(' doesn''t matter which directory crtbegin.o');
  500. add(' is in. */');
  501. add(' KEEP (*crtbegin.o(.ctors))');
  502. add(' KEEP (*crtbegin?.o(.ctors))');
  503. add(' /* We don''t want to include the .ctor section from');
  504. add(' the crtend.o file until after the sorted ctors.');
  505. add(' The .ctor section from the crtend file contains the');
  506. add(' end of ctors marker and it must be last */');
  507. add(' KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .ctors))');
  508. add(' KEEP (*(SORT(.ctors.*)))');
  509. add(' KEEP (*(.ctors))');
  510. add(' }');
  511. add(' .dtors :');
  512. add(' {');
  513. add(' KEEP (*crtbegin.o(.dtors))');
  514. add(' KEEP (*crtbegin?.o(.dtors))');
  515. add(' KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors))');
  516. add(' KEEP (*(SORT(.dtors.*)))');
  517. add(' KEEP (*(.dtors))');
  518. add(' }');
  519. add(' .jcr : { KEEP (*(.jcr)) }');
  520. add(' .data.rel.ro : { *(.data.rel.ro.local* .gnu.linkonce.d.rel.ro.local.*) *(.data.rel.ro* .gnu.linkonce.d.rel.ro.*) }');
  521. add(' .dynamic : { *(.dynamic) }');
  522. add(' .got : { *(.got.plt) *(.got) }');
  523. add(' .data :');
  524. add(' {');
  525. add(' __data_start = . ;');
  526. add(' *(.data .data.* .gnu.linkonce.d.*)');
  527. { extra by FPC }
  528. add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  529. add(' KEEP (*(.gnu.linkonce.d.*personality*))');
  530. add(' SORT(CONSTRUCTORS)');
  531. add(' }');
  532. add(' .data1 : { *(.data1) }');
  533. add(' _edata = .; PROVIDE (edata = .);');
  534. add(' __bss_start = .;');
  535. add(' __bss_start__ = .;');
  536. add(' .bss :');
  537. add(' {');
  538. add(' *(.dynbss)');
  539. add(' *(.bss .bss.* .gnu.linkonce.b.*)');
  540. add(' *(COMMON)');
  541. add(' /* Align here to ensure that the .bss section occupies space up to');
  542. add(' _end. Align after .bss to ensure correct alignment even if the');
  543. add(' .bss section disappears because there are no input sections.');
  544. add(' FIXME: Why do we need it? When there is no .bss section, we don''t');
  545. add(' pad the .data section. */');
  546. add(' . = ALIGN(. != 0 ? 32 / 8 : 1);');
  547. add(' }');
  548. add(' _bss_end__ = . ; __bss_end__ = . ;');
  549. add(' . = ALIGN(32 / 8);');
  550. add(' . = ALIGN(32 / 8);');
  551. add(' __end__ = . ;');
  552. add(' _end = .; PROVIDE (end = .);');
  553. add(' /* Stabs debugging sections. */');
  554. add(' .stab 0 : { *(.stab) }');
  555. add(' .stabstr 0 : { *(.stabstr) }');
  556. add(' .stab.excl 0 : { *(.stab.excl) }');
  557. add(' .stab.exclstr 0 : { *(.stab.exclstr) }');
  558. add(' .stab.index 0 : { *(.stab.index) }');
  559. add(' .stab.indexstr 0 : { *(.stab.indexstr) }');
  560. add(' .comment 0 : { *(.comment) }');
  561. add(' /* DWARF debug sections.');
  562. add(' Symbols in the DWARF debugging sections are relative to the beginning');
  563. add(' of the section so we begin them at 0. */');
  564. add(' /* DWARF 1 */');
  565. add(' .debug 0 : { *(.debug) }');
  566. add(' .line 0 : { *(.line) }');
  567. add(' /* GNU DWARF 1 extensions */');
  568. add(' .debug_srcinfo 0 : { *(.debug_srcinfo) }');
  569. add(' .debug_sfnames 0 : { *(.debug_sfnames) }');
  570. add(' /* DWARF 1.1 and DWARF 2 */');
  571. add(' .debug_aranges 0 : { *(.debug_aranges) }');
  572. add(' .debug_pubnames 0 : { *(.debug_pubnames) }');
  573. add(' /* DWARF 2 */');
  574. add(' .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) }');
  575. add(' .debug_abbrev 0 : { *(.debug_abbrev) }');
  576. add(' .debug_line 0 : { *(.debug_line) }');
  577. add(' .debug_frame 0 : { *(.debug_frame) }');
  578. add(' .debug_str 0 : { *(.debug_str) }');
  579. add(' .debug_loc 0 : { *(.debug_loc) }');
  580. add(' .debug_macinfo 0 : { *(.debug_macinfo) }');
  581. add(' /* SGI/MIPS DWARF 2 extensions */');
  582. add(' .debug_weaknames 0 : { *(.debug_weaknames) }');
  583. add(' .debug_funcnames 0 : { *(.debug_funcnames) }');
  584. add(' .debug_typenames 0 : { *(.debug_typenames) }');
  585. add(' .debug_varnames 0 : { *(.debug_varnames) }');
  586. add(' /* DWARF 3 */');
  587. add(' .debug_pubtypes 0 : { *(.debug_pubtypes) }');
  588. add(' .debug_ranges 0 : { *(.debug_ranges) }');
  589. add(' .stack 0x80000 :');
  590. add(' {');
  591. add(' _stack = .;');
  592. add(' *(.stack)');
  593. add(' }');
  594. add(' .ARM.attributes 0 : { KEEP (*(.ARM.attributes)) KEEP (*(.gnu.attributes)) }');
  595. add(' .note.gnu.arm.ident 0 : { KEEP (*(.note.gnu.arm.ident)) }');
  596. add(' /DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) }');
  597. add('}');
  598. end;
  599. {$endif ARM}
  600. { Write and Close response }
  601. writetodisk;
  602. Free;
  603. end;
  604. WriteResponseFile:=True;
  605. end;
  606. function TLinkerAndroid.MakeExecutable:boolean;
  607. var
  608. i : longint;
  609. binstr,
  610. cmdstr : TCmdStr;
  611. success : boolean;
  612. DynLinkStr : string;
  613. GCSectionsStr,
  614. StaticStr,
  615. StripStr : string[40];
  616. begin
  617. if not(cs_link_nolink in current_settings.globalswitches) then
  618. Message1(exec_i_linking,current_module.exefilename);
  619. { Create some replacements }
  620. StaticStr:='';
  621. StripStr:='';
  622. GCSectionsStr:='';
  623. DynLinkStr:='';
  624. if (cs_link_staticflag in current_settings.globalswitches) then
  625. StaticStr:='-static';
  626. if (cs_link_strip in current_settings.globalswitches) and
  627. not(cs_link_separate_dbg_file in current_settings.globalswitches) then
  628. StripStr:='-s';
  629. if (cs_link_map in current_settings.globalswitches) then
  630. StripStr:='-Map '+maybequoted(ChangeFileExt(current_module.exefilename,'.map'));
  631. if create_smartlink_sections then
  632. GCSectionsStr:='--gc-sections';
  633. If (cs_profile in current_settings.moduleswitches) or
  634. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty)) then
  635. begin
  636. DynLinkStr:='--dynamic-linker='+Info.DynamicLinker;
  637. if cshared then
  638. DynLinkStr:=DynLinkStr+' --shared ';
  639. if rlinkpath<>'' then
  640. DynLinkStr:=DynLinkStr+' --rpath-link '+rlinkpath;
  641. End;
  642. { Write used files and libraries }
  643. WriteResponseFile(false);
  644. { Call linker }
  645. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  646. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename));
  647. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  648. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  649. Replace(cmdstr,'$STATIC',StaticStr);
  650. Replace(cmdstr,'$STRIP',StripStr);
  651. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  652. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  653. { create dynamic symbol table? }
  654. if HasExports then
  655. cmdstr:=cmdstr+' -E';
  656. success:=DoExec(FindUtil(utilsprefix+BinStr),CmdStr,true,false);
  657. { Create external .dbg file with debuginfo }
  658. if success and (cs_link_separate_dbg_file in current_settings.globalswitches) then
  659. begin
  660. for i:=1 to 3 do
  661. begin
  662. SplitBinCmd(Info.ExtDbgCmd[i],binstr,cmdstr);
  663. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename));
  664. Replace(cmdstr,'$DBGFN',maybequoted(extractfilename(current_module.dbgfilename)));
  665. Replace(cmdstr,'$DBG',maybequoted(current_module.dbgfilename));
  666. success:=DoExec(FindUtil(utilsprefix+BinStr),CmdStr,true,false);
  667. if not success then
  668. break;
  669. end;
  670. end;
  671. { Remove ReponseFile }
  672. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  673. DeleteFile(outputexedir+Info.ResName);
  674. MakeExecutable:=success; { otherwise a recursive call to link method }
  675. end;
  676. Function TLinkerAndroid.MakeSharedLibrary:boolean;
  677. var
  678. InitStr,
  679. FiniStr,
  680. SoNameStr : string[80];
  681. binstr,
  682. cmdstr : TCmdStr;
  683. success : boolean;
  684. begin
  685. MakeSharedLibrary:=false;
  686. if not(cs_link_nolink in current_settings.globalswitches) then
  687. Message1(exec_i_linking,current_module.sharedlibfilename);
  688. { Write used files and libraries }
  689. WriteResponseFile(true);
  690. { Create some replacements }
  691. { note: linux does not use exportlib.initname/fininame due to the custom startup code }
  692. InitStr:='-init FPC_SHARED_LIB_START';
  693. FiniStr:='-fini FPC_LIB_EXIT';
  694. SoNameStr:='-soname '+ExtractFileName(current_module.sharedlibfilename);
  695. { Call linker }
  696. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  697. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename));
  698. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  699. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  700. Replace(cmdstr,'$INIT',InitStr);
  701. Replace(cmdstr,'$FINI',FiniStr);
  702. Replace(cmdstr,'$SONAME',SoNameStr);
  703. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  704. { Strip the library ? }
  705. if success and (cs_link_strip in current_settings.globalswitches) then
  706. begin
  707. { only remove non global symbols and debugging info for a library }
  708. Info.DllCmd[2]:='strip --discard-all --strip-debug $EXE';
  709. SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  710. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename));
  711. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  712. end;
  713. { Remove ReponseFile }
  714. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  715. DeleteFile(outputexedir+Info.ResName);
  716. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  717. end;
  718. {*****************************************************************************
  719. Initialize
  720. *****************************************************************************}
  721. initialization
  722. {$ifdef ARM}
  723. RegisterExternalLinker(system_arm_android_info,TLinkerAndroid);
  724. RegisterImport(system_arm_android,timportlibandroid);
  725. RegisterExport(system_arm_android,texportlibandroid);
  726. RegisterTarget(system_arm_android_info);
  727. {$endif ARM}
  728. {$ifdef I386}
  729. RegisterExternalLinker(system_i386_android_info,TLinkerAndroid);
  730. RegisterImport(system_i386_android,timportlibandroid);
  731. RegisterExport(system_i386_android,texportlibandroid);
  732. RegisterTarget(system_i386_android_info);
  733. {$endif I386}
  734. RegisterRes(res_elf_info,TWinLikeResourceFile);
  735. end.