t_android.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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 }
  33. tlinkerandroid=class(texternallinker)
  34. private
  35. libctype:(bionic);
  36. prtobj : string[80];
  37. reorder : boolean;
  38. Function WriteResponseFile(isdll:boolean) : Boolean;
  39. function GetLdOptions: string;
  40. function DoLink(IsSharedLib: 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. s: string;
  103. begin
  104. with Info do
  105. begin
  106. { Specify correct max-page-size and common-page-size to prevent big gaps between sections in resulting executable }
  107. s:='ld '+platform_select+'-z max-page-size=0x1000 -z common-page-size=0x1000 $OPT -L. -T $RES -o $EXE';
  108. ExeCmd[1]:=s + ' --entry=_fpc_start';
  109. DllCmd[1]:=s + ' -shared -soname $SONAME';
  110. DllCmd[2]:='strip --strip-unneeded $EXE';
  111. ExtDbgCmd[1]:='objcopy --only-keep-debug $EXE $DBG';
  112. ExtDbgCmd[2]:='objcopy --add-gnu-debuglink=$DBG $EXE';
  113. ExtDbgCmd[3]:='strip --strip-unneeded $EXE';
  114. {
  115. There is only one C library on android, the bionic libc
  116. }
  117. libctype:=bionic;
  118. DynamicLinker:='/system/bin/linker';
  119. end;
  120. end;
  121. procedure TLinkerAndroid.LoadPredefinedLibraryOrder;
  122. // put your linkorder/linkalias overrides here.
  123. // Note: assumes only called when reordering/aliasing is used.
  124. Begin
  125. if not (cs_link_no_default_lib_order in current_settings.globalswitches) Then
  126. Begin
  127. LinkLibraryOrder.add('gcc','',15);
  128. LinkLibraryOrder.add('c','',100);
  129. LinkLibraryOrder.add('gmon','',120);
  130. LinkLibraryOrder.add('dl','',140);
  131. LinkLibraryOrder.add('pthread','',160);
  132. end;
  133. End;
  134. Procedure TLinkerAndroid.InitSysInitUnitName;
  135. begin
  136. reorder := ReOrderEntries;
  137. if current_module.islibrary then
  138. prtobj:='dllprt0'
  139. else
  140. prtobj:='prt0';
  141. end;
  142. Function TLinkerAndroid.WriteResponseFile(isdll:boolean) : Boolean;
  143. Var
  144. linkres : TLinkRes;
  145. i : longint;
  146. HPath : TCmdStrListItem;
  147. s,s1 : TCmdStr;
  148. begin
  149. result:=False;
  150. { Always link to libc }
  151. AddSharedLibrary('c');
  152. { Open link.res file }
  153. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  154. with linkres do
  155. begin
  156. { Write path to search libraries }
  157. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  158. while assigned(HPath) do
  159. begin
  160. Add('SEARCH_DIR('+maybequoted(HPath.Str)+')');
  161. HPath:=TCmdStrListItem(HPath.Next);
  162. end;
  163. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  164. while assigned(HPath) do
  165. begin
  166. Add('SEARCH_DIR('+maybequoted(HPath.Str)+')');
  167. HPath:=TCmdStrListItem(HPath.Next);
  168. end;
  169. { force local symbol resolution (i.e., inside the shared }
  170. { library itself) for all non-exorted symbols, otherwise }
  171. { several RTL symbols of FPC-compiled shared libraries }
  172. { will be bound to those of a single shared library or }
  173. { to the main program }
  174. if (isdll) then
  175. begin
  176. add('VERSION');
  177. add('{');
  178. add(' {');
  179. if not texportlibunix(exportlib).exportedsymnames.empty then
  180. begin
  181. add(' global:');
  182. repeat
  183. add(' '+texportlibunix(exportlib).exportedsymnames.getfirst+';');
  184. until texportlibunix(exportlib).exportedsymnames.empty;
  185. end;
  186. add(' local:');
  187. add(' *;');
  188. add(' };');
  189. add('}');
  190. end;
  191. StartSection('INPUT(');
  192. { add objectfiles, start with prt0 always }
  193. if not (target_info.system in systems_internal_sysinit) and (prtobj<>'') then
  194. AddFileName(maybequoted(FindObjectFile(prtobj,'',false)));
  195. { Add libc startup object file }
  196. if isdll then
  197. s:='crtbegin_so.o'
  198. else
  199. if cs_link_staticflag in current_settings.globalswitches then
  200. s:='crtbegin_static.o'
  201. else
  202. s:='crtbegin_dynamic.o';
  203. librarysearchpath.FindFile(s,false,s1);
  204. AddFileName(s1);
  205. { main objectfiles }
  206. while not ObjectFiles.Empty do
  207. begin
  208. s:=ObjectFiles.GetFirst;
  209. if s<>'' then
  210. AddFileName(maybequoted(s));
  211. end;
  212. EndSection(')');
  213. { Write staticlibraries }
  214. if not StaticLibFiles.Empty then
  215. begin
  216. Add('GROUP(');
  217. While not StaticLibFiles.Empty do
  218. begin
  219. S:=StaticLibFiles.GetFirst;
  220. AddFileName(maybequoted(s))
  221. end;
  222. Add(')');
  223. end;
  224. // we must reorder here because the result could empty sharedlibfiles
  225. if reorder Then
  226. ExpandAndApplyOrder(SharedLibFiles);
  227. // after this point addition of shared libs not allowed.
  228. if not SharedLibFiles.Empty then
  229. begin
  230. if (SharedLibFiles.Count<>1) or reorder then
  231. begin
  232. Add('INPUT(');
  233. While not SharedLibFiles.Empty do
  234. begin
  235. S:=SharedLibFiles.GetFirst;
  236. i:=Pos(target_info.sharedlibext,S);
  237. if i>0 then
  238. Delete(S,i,255);
  239. Add('-l'+s);
  240. end;
  241. Add(')');
  242. end;
  243. if (cs_link_staticflag in current_settings.globalswitches) or
  244. (not reorder) then
  245. begin
  246. Add('GROUP(');
  247. { when we have -static for the linker the we also need libgcc }
  248. if (cs_link_staticflag in current_settings.globalswitches) then
  249. begin
  250. Add('-lgcc');
  251. if librarysearchpath.FindFile('libgcc_eh.a',false,s1) then
  252. Add('-lgcc_eh');
  253. end;
  254. { be sure that libc is the last lib }
  255. if not reorder then
  256. Add('-lc');
  257. Add(')');
  258. end;
  259. end;
  260. { objects which must be at the end }
  261. { Add libc finalization object file }
  262. Add('INPUT(');
  263. if isdll then
  264. s:='crtend_so.o'
  265. else
  266. s:='crtend_android.o';
  267. librarysearchpath.FindFile(s,false,s1);
  268. AddFileName(s1);
  269. Add(')');
  270. { Additions to the linker script }
  271. add('SECTIONS');
  272. add('{');
  273. add(' .data :');
  274. add(' {');
  275. { extra by FPC }
  276. add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  277. add(' }');
  278. add('}');
  279. add('INSERT BEFORE .data1');
  280. { Write and Close response }
  281. writetodisk;
  282. Free;
  283. end;
  284. WriteResponseFile:=True;
  285. end;
  286. function tlinkerandroid.GetLdOptions: string;
  287. begin
  288. Result:='';
  289. if (cs_link_strip in current_settings.globalswitches) and
  290. not(cs_link_separate_dbg_file in current_settings.globalswitches) then
  291. Result:=Result + ' -s';
  292. if (cs_link_map in current_settings.globalswitches) then
  293. Result:=Result + ' -Map '+maybequoted(ChangeFileExt(current_module.exefilename,'.map'));
  294. if create_smartlink_sections then
  295. Result:=Result + ' --gc-sections';
  296. end;
  297. function tlinkerandroid.DoLink(IsSharedLib: boolean): boolean;
  298. var
  299. i: longint;
  300. binstr, cmdstr: TCmdStr;
  301. s, opts, outname: string;
  302. success: boolean;
  303. begin
  304. Result:=False;
  305. if IsSharedLib then
  306. outname:=current_module.sharedlibfilename
  307. else
  308. outname:=current_module.exefilename;
  309. if not(cs_link_nolink in current_settings.globalswitches) then
  310. Message1(exec_i_linking, outname);
  311. opts:='';
  312. if (cs_link_strip in current_settings.globalswitches) and
  313. not (cs_link_separate_dbg_file in current_settings.globalswitches) then
  314. opts:=opts + ' -s';
  315. if (cs_link_map in current_settings.globalswitches) then
  316. opts:=opts + ' -Map '+maybequoted(ChangeFileExt(outname,'.map'));
  317. if create_smartlink_sections then
  318. opts:=opts + ' --gc-sections';
  319. if (cs_link_staticflag in current_settings.globalswitches) then
  320. opts:=opts + ' -static'
  321. else
  322. if cshared then
  323. opts:=opts + ' -call_shared';
  324. if rlinkpath<>'' then
  325. opts:=opts+' --rpath-link '+rlinkpath;
  326. if not IsSharedLib then
  327. begin
  328. if (cs_profile in current_settings.moduleswitches) or
  329. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty))
  330. then
  331. opts:=opts + ' --dynamic-linker=' + Info.DynamicLinker;
  332. { create dynamic symbol table? }
  333. if HasExports then
  334. opts:=opts+' -E';
  335. end;
  336. opts:=Trim(opts + ' ' + Info.ExtraOptions);
  337. { Write used files and libraries }
  338. WriteResponseFile(IsSharedLib);
  339. { Call linker }
  340. if IsSharedLib then
  341. s:=Info.DllCmd[1]
  342. else
  343. s:=Info.ExeCmd[1];
  344. SplitBinCmd(s, binstr, cmdstr);
  345. Replace(cmdstr,'$EXE',maybequoted(outname));
  346. Replace(cmdstr,'$OPT',opts);
  347. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  348. if IsSharedLib then
  349. Replace(cmdstr,'$SONAME',ExtractFileName(outname));
  350. binstr:=FindUtil(utilsprefix+BinStr);
  351. { We should use BFD version of LD, since GOLD version does not support INSERT command in linker scripts }
  352. if binstr <> '' then begin
  353. { Checking if ld.bfd exists }
  354. s:=ChangeFileExt(binstr, '.bfd' + source_info.exeext);
  355. if FileExists(s, True) then
  356. binstr:=s;
  357. end;
  358. success:=DoExec(binstr,CmdStr,true,false);
  359. { Create external .dbg file with debuginfo }
  360. if success and (cs_link_separate_dbg_file in current_settings.globalswitches) then
  361. begin
  362. for i:=1 to 3 do
  363. begin
  364. SplitBinCmd(Info.ExtDbgCmd[i],binstr,cmdstr);
  365. Replace(cmdstr,'$EXE',maybequoted(outname));
  366. Replace(cmdstr,'$DBGFN',maybequoted(extractfilename(current_module.dbgfilename)));
  367. Replace(cmdstr,'$DBG',maybequoted(current_module.dbgfilename));
  368. success:=DoExec(FindUtil(utilsprefix+BinStr),CmdStr,true,false);
  369. if not success then
  370. break;
  371. end;
  372. end;
  373. { Remove ReponseFile }
  374. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  375. DeleteFile(outputexedir+Info.ResName);
  376. Result:=success; { otherwise a recursive call to link method }
  377. end;
  378. function TLinkerAndroid.MakeExecutable:boolean;
  379. begin
  380. Result:=DoLink(False);
  381. end;
  382. Function TLinkerAndroid.MakeSharedLibrary:boolean;
  383. begin
  384. Result:=DoLink(True);
  385. end;
  386. {*****************************************************************************
  387. Initialize
  388. *****************************************************************************}
  389. initialization
  390. {$ifdef ARM}
  391. RegisterExternalLinker(system_arm_android_info,TLinkerAndroid);
  392. RegisterImport(system_arm_android,timportlibandroid);
  393. RegisterExport(system_arm_android,texportlibandroid);
  394. RegisterTarget(system_arm_android_info);
  395. {$endif ARM}
  396. {$ifdef I386}
  397. RegisterExternalLinker(system_i386_android_info,TLinkerAndroid);
  398. RegisterImport(system_i386_android,timportlibandroid);
  399. RegisterExport(system_i386_android,texportlibandroid);
  400. RegisterTarget(system_i386_android_info);
  401. {$endif I386}
  402. RegisterRes(res_elf_info,TWinLikeResourceFile);
  403. end.