t_android.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. prtobj : string[80];
  36. reorder : boolean;
  37. Function WriteResponseFile(isdll:boolean) : Boolean;
  38. function DoLink(IsSharedLib: boolean): boolean;
  39. public
  40. constructor Create;override;
  41. procedure SetDefaultInfo;override;
  42. procedure InitSysInitUnitName;override;
  43. function MakeExecutable:boolean;override;
  44. function MakeSharedLibrary:boolean;override;
  45. procedure LoadPredefinedLibraryOrder; override;
  46. end;
  47. implementation
  48. uses
  49. SysUtils,
  50. cutils,cfileutl,cclasses,
  51. verbose,systems,globtype,globals,
  52. symconst,script,
  53. fmodule,
  54. aasmbase,aasmtai,aasmcpu,cpubase,
  55. cgbase,cgobj,cgutils,ogbase,ncgutil,
  56. comprsrc,
  57. rescmn, i_android
  58. ;
  59. {*****************************************************************************
  60. TIMPORTLIBANDROID
  61. *****************************************************************************}
  62. procedure timportlibandroid.generatelib;
  63. var
  64. i : longint;
  65. ImportLibrary : TImportLibrary;
  66. begin
  67. for i:=0 to current_module.ImportLibraryList.Count-1 do
  68. begin
  69. ImportLibrary:=TImportLibrary(current_module.ImportLibraryList[i]);
  70. current_module.linkothersharedlibs.add(ImportLibrary.Name,link_always);
  71. end;
  72. end;
  73. {*****************************************************************************
  74. TEXPORTLIBANDROID
  75. *****************************************************************************}
  76. procedure texportlibandroid.setfininame(list: TAsmList; const s: string);
  77. begin
  78. { the problem with not having a .fini section is that a finalization
  79. routine in regular code can get "smart" linked away -> reference it
  80. just like the debug info }
  81. new_section(list,sec_fpc,'links',0);
  82. list.concat(Tai_const.Createname(s,0));
  83. inherited setfininame(list,s);
  84. end;
  85. {*****************************************************************************
  86. TLINKERANDROID
  87. *****************************************************************************}
  88. Constructor TLinkerAndroid.Create;
  89. begin
  90. Inherited Create;
  91. end;
  92. procedure TLinkerAndroid.SetDefaultInfo;
  93. {
  94. This will also detect which libc version will be used
  95. }
  96. const
  97. {$ifdef arm} platform_select='';{$endif} {unknown :( }
  98. {$ifdef i386} platform_select='';{$endif} {unknown :( }
  99. {$ifdef mipsel} platform_select='';{$endif} {unknown :( }
  100. var
  101. s: string;
  102. begin
  103. with Info do
  104. begin
  105. { Specify correct max-page-size and common-page-size to prevent big gaps between sections in resulting executable }
  106. s:='ld '+platform_select+'-z max-page-size=0x1000 -z common-page-size=0x1000 -z noexecstack -z now $OPT -L. -T $RES -o $EXE';
  107. ExeCmd[1]:=s + ' --entry=_fpc_start';
  108. DllCmd[1]:=s + ' -shared -soname $SONAME';
  109. DllCmd[2]:='strip --strip-unneeded $EXE';
  110. ExtDbgCmd[1]:='objcopy --only-keep-debug $EXE $DBG';
  111. ExtDbgCmd[2]:='objcopy --add-gnu-debuglink=$DBG $EXE';
  112. ExtDbgCmd[3]:='strip --strip-unneeded $EXE';
  113. DynamicLinker:='/system/bin/linker';
  114. end;
  115. end;
  116. procedure TLinkerAndroid.LoadPredefinedLibraryOrder;
  117. // put your linkorder/linkalias overrides here.
  118. // Note: assumes only called when reordering/aliasing is used.
  119. Begin
  120. if not (cs_link_no_default_lib_order in current_settings.globalswitches) Then
  121. Begin
  122. LinkLibraryOrder.add('gcc','',15);
  123. LinkLibraryOrder.add('c','',100);
  124. LinkLibraryOrder.add('gmon','',120);
  125. LinkLibraryOrder.add('dl','',140);
  126. LinkLibraryOrder.add('pthread','',160);
  127. end;
  128. End;
  129. Procedure TLinkerAndroid.InitSysInitUnitName;
  130. begin
  131. reorder := ReOrderEntries;
  132. if current_module.islibrary then
  133. prtobj:='dllprt0'
  134. else
  135. prtobj:='prt0';
  136. end;
  137. Function TLinkerAndroid.WriteResponseFile(isdll:boolean) : Boolean;
  138. Var
  139. linkres : TLinkRes;
  140. i : longint;
  141. HPath : TCmdStrListItem;
  142. s,s1 : TCmdStr;
  143. begin
  144. result:=False;
  145. { Always link to libc }
  146. AddSharedLibrary('c');
  147. { Open link.res file }
  148. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  149. with linkres do
  150. begin
  151. { Write path to search libraries }
  152. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  153. while assigned(HPath) do
  154. begin
  155. Add('SEARCH_DIR('+maybequoted(HPath.Str)+')');
  156. HPath:=TCmdStrListItem(HPath.Next);
  157. end;
  158. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  159. while assigned(HPath) do
  160. begin
  161. Add('SEARCH_DIR('+maybequoted(HPath.Str)+')');
  162. HPath:=TCmdStrListItem(HPath.Next);
  163. end;
  164. { force local symbol resolution (i.e., inside the shared }
  165. { library itself) for all non-exorted symbols, otherwise }
  166. { several RTL symbols of FPC-compiled shared libraries }
  167. { will be bound to those of a single shared library or }
  168. { to the main program }
  169. if (isdll) then
  170. begin
  171. add('VERSION');
  172. add('{');
  173. add(' {');
  174. if not texportlibunix(exportlib).exportedsymnames.empty then
  175. begin
  176. add(' global:');
  177. repeat
  178. add(' '+texportlibunix(exportlib).exportedsymnames.getfirst+';');
  179. until texportlibunix(exportlib).exportedsymnames.empty;
  180. end;
  181. add(' local:');
  182. add(' *;');
  183. add(' };');
  184. add('}');
  185. end;
  186. StartSection('INPUT(');
  187. { add objectfiles, start with prt0 always }
  188. if not (target_info.system in systems_internal_sysinit) and (prtobj<>'') then
  189. AddFileName(maybequoted(FindObjectFile(prtobj,'',false)));
  190. { Add libc startup object file }
  191. if isdll then
  192. s:='crtbegin_so.o'
  193. else
  194. if cs_link_staticflag in current_settings.globalswitches then
  195. s:='crtbegin_static.o'
  196. else
  197. s:='crtbegin_dynamic.o';
  198. librarysearchpath.FindFile(s,false,s1);
  199. AddFileName(maybequoted(s1));
  200. { main objectfiles }
  201. while not ObjectFiles.Empty do
  202. begin
  203. s:=ObjectFiles.GetFirst;
  204. if s<>'' then
  205. AddFileName(maybequoted(s));
  206. end;
  207. EndSection(')');
  208. { Write staticlibraries }
  209. if not StaticLibFiles.Empty then
  210. begin
  211. Add('GROUP(');
  212. While not StaticLibFiles.Empty do
  213. begin
  214. S:=StaticLibFiles.GetFirst;
  215. AddFileName(maybequoted(s))
  216. end;
  217. Add(')');
  218. end;
  219. // we must reorder here because the result could empty sharedlibfiles
  220. if reorder Then
  221. ExpandAndApplyOrder(SharedLibFiles);
  222. // after this point addition of shared libs not allowed.
  223. if not SharedLibFiles.Empty then
  224. begin
  225. if (SharedLibFiles.Count<>1) or reorder then
  226. begin
  227. Add('INPUT(');
  228. While not SharedLibFiles.Empty do
  229. begin
  230. S:=SharedLibFiles.GetFirst;
  231. i:=Pos(target_info.sharedlibext,S);
  232. if i>0 then
  233. Delete(S,i,255);
  234. Add('-l'+s);
  235. end;
  236. Add(')');
  237. end;
  238. if (cs_link_staticflag in current_settings.globalswitches) or
  239. (not reorder) then
  240. begin
  241. Add('GROUP(');
  242. { when we have -static for the linker the we also need libgcc }
  243. if (cs_link_staticflag in current_settings.globalswitches) then
  244. begin
  245. Add('-lgcc');
  246. if librarysearchpath.FindFile('libgcc_eh.a',false,s1) then
  247. Add('-lgcc_eh');
  248. end;
  249. { be sure that libc is the last lib }
  250. if not reorder then
  251. Add('-lc');
  252. Add(')');
  253. end;
  254. end;
  255. { objects which must be at the end }
  256. { Add libc finalization object file }
  257. Add('INPUT(');
  258. if isdll then
  259. s:='crtend_so.o'
  260. else
  261. s:='crtend_android.o';
  262. librarysearchpath.FindFile(s,false,s1);
  263. AddFileName(maybequoted(s1));
  264. Add(')');
  265. { Additions to the linker script }
  266. add('SECTIONS');
  267. add('{');
  268. add(' .data :');
  269. add(' {');
  270. { extra by FPC }
  271. add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  272. add(' }');
  273. add('}');
  274. add('INSERT BEFORE .data1');
  275. { Write and Close response }
  276. writetodisk;
  277. Free;
  278. end;
  279. WriteResponseFile:=True;
  280. end;
  281. function tlinkerandroid.DoLink(IsSharedLib: boolean): boolean;
  282. var
  283. i: longint;
  284. binstr, cmdstr: TCmdStr;
  285. s, opts, outname: string;
  286. success: boolean;
  287. begin
  288. Result:=False;
  289. if IsSharedLib then
  290. outname:=current_module.sharedlibfilename
  291. else
  292. outname:=current_module.exefilename;
  293. if not(cs_link_nolink in current_settings.globalswitches) then
  294. Message1(exec_i_linking, outname);
  295. opts:='';
  296. if (cs_link_strip in current_settings.globalswitches) and
  297. not (cs_link_separate_dbg_file in current_settings.globalswitches) then
  298. opts:=opts + ' -s';
  299. if (cs_link_map in current_settings.globalswitches) then
  300. opts:=opts + ' -Map '+maybequoted(ChangeFileExt(outname,'.map'));
  301. if create_smartlink_sections then
  302. opts:=opts + ' --gc-sections';
  303. if (cs_link_staticflag in current_settings.globalswitches) then
  304. opts:=opts + ' -static'
  305. else
  306. if cshared then
  307. opts:=opts + ' -call_shared';
  308. if rlinkpath<>'' then
  309. opts:=opts+' --rpath-link '+rlinkpath;
  310. if not IsSharedLib then
  311. begin
  312. opts:=opts + ' --dynamic-linker ' + Info.DynamicLinker;
  313. { create dynamic symbol table? }
  314. if HasExports then
  315. opts:=opts+' -E';
  316. end;
  317. opts:=Trim(opts + ' ' + Info.ExtraOptions);
  318. { Write used files and libraries }
  319. WriteResponseFile(IsSharedLib);
  320. { Call linker }
  321. if IsSharedLib then
  322. s:=Info.DllCmd[1]
  323. else
  324. s:=Info.ExeCmd[1];
  325. SplitBinCmd(s, binstr, cmdstr);
  326. Replace(cmdstr,'$EXE',maybequoted(outname));
  327. Replace(cmdstr,'$OPT',opts);
  328. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  329. if IsSharedLib then
  330. Replace(cmdstr,'$SONAME',ExtractFileName(outname));
  331. binstr:=FindUtil(utilsprefix+BinStr);
  332. { We should use BFD version of LD, since GOLD version does not support INSERT command in linker scripts }
  333. if binstr <> '' then begin
  334. { Checking if ld.bfd exists }
  335. s:=ChangeFileExt(binstr, '.bfd' + source_info.exeext);
  336. if FileExists(s, True) then
  337. binstr:=s;
  338. end;
  339. success:=DoExec(binstr,CmdStr,true,false);
  340. { Create external .dbg file with debuginfo }
  341. if success and (cs_link_separate_dbg_file in current_settings.globalswitches) then
  342. begin
  343. for i:=1 to 3 do
  344. begin
  345. SplitBinCmd(Info.ExtDbgCmd[i],binstr,cmdstr);
  346. Replace(cmdstr,'$EXE',maybequoted(outname));
  347. Replace(cmdstr,'$DBGFN',maybequoted(extractfilename(current_module.dbgfilename)));
  348. Replace(cmdstr,'$DBG',maybequoted(current_module.dbgfilename));
  349. success:=DoExec(FindUtil(utilsprefix+BinStr),CmdStr,true,false);
  350. if not success then
  351. break;
  352. end;
  353. end;
  354. { Remove ReponseFile }
  355. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  356. DeleteFile(outputexedir+Info.ResName);
  357. Result:=success; { otherwise a recursive call to link method }
  358. end;
  359. function TLinkerAndroid.MakeExecutable:boolean;
  360. begin
  361. Result:=DoLink(False);
  362. end;
  363. Function TLinkerAndroid.MakeSharedLibrary:boolean;
  364. begin
  365. Result:=DoLink(True);
  366. end;
  367. {*****************************************************************************
  368. Initialize
  369. *****************************************************************************}
  370. initialization
  371. RegisterLinker(ld_android,TLinkerAndroid);
  372. {$ifdef ARM}
  373. RegisterImport(system_arm_android,timportlibandroid);
  374. RegisterExport(system_arm_android,texportlibandroid);
  375. RegisterTarget(system_arm_android_info);
  376. {$endif ARM}
  377. {$ifdef I386}
  378. RegisterImport(system_i386_android,timportlibandroid);
  379. RegisterExport(system_i386_android,texportlibandroid);
  380. RegisterTarget(system_i386_android_info);
  381. {$endif I386}
  382. {$ifdef MIPSEL}
  383. RegisterImport(system_mipsel_android,timportlibandroid);
  384. RegisterExport(system_mipsel_android,texportlibandroid);
  385. RegisterTarget(system_mipsel_android_info);
  386. {$endif MIPSEL}
  387. RegisterRes(res_elf_info,TWinLikeResourceFile);
  388. end.