t_android.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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 GetLdOptions: string;
  39. function DoLink(IsSharedLib: boolean): boolean;
  40. public
  41. constructor Create;override;
  42. procedure SetDefaultInfo;override;
  43. procedure InitSysInitUnitName;override;
  44. function MakeExecutable:boolean;override;
  45. function MakeSharedLibrary:boolean;override;
  46. procedure LoadPredefinedLibraryOrder; override;
  47. end;
  48. implementation
  49. uses
  50. SysUtils,
  51. cutils,cfileutl,cclasses,
  52. verbose,systems,globtype,globals,
  53. symconst,script,
  54. fmodule,
  55. aasmbase,aasmtai,aasmcpu,cpubase,
  56. cgbase,cgobj,cgutils,ogbase,ncgutil,
  57. comprsrc,
  58. rescmn, i_android
  59. ;
  60. {*****************************************************************************
  61. TIMPORTLIBANDROID
  62. *****************************************************************************}
  63. procedure timportlibandroid.generatelib;
  64. var
  65. i : longint;
  66. ImportLibrary : TImportLibrary;
  67. begin
  68. for i:=0 to current_module.ImportLibraryList.Count-1 do
  69. begin
  70. ImportLibrary:=TImportLibrary(current_module.ImportLibraryList[i]);
  71. current_module.linkothersharedlibs.add(ImportLibrary.Name,link_always);
  72. end;
  73. end;
  74. {*****************************************************************************
  75. TEXPORTLIBANDROID
  76. *****************************************************************************}
  77. procedure texportlibandroid.setfininame(list: TAsmList; const s: string);
  78. begin
  79. { the problem with not having a .fini section is that a finalization
  80. routine in regular code can get "smart" linked away -> reference it
  81. just like the debug info }
  82. new_section(list,sec_fpc,'links',0);
  83. list.concat(Tai_const.Createname(s,0));
  84. inherited setfininame(list,s);
  85. end;
  86. {*****************************************************************************
  87. TLINKERANDROID
  88. *****************************************************************************}
  89. Constructor TLinkerAndroid.Create;
  90. begin
  91. Inherited Create;
  92. end;
  93. procedure TLinkerAndroid.SetDefaultInfo;
  94. {
  95. This will also detect which libc version will be used
  96. }
  97. const
  98. {$ifdef arm} platform_select='';{$endif} {unknown :( }
  99. {$ifdef i386} 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 $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(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(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.GetLdOptions: string;
  282. begin
  283. Result:='';
  284. if (cs_link_strip in current_settings.globalswitches) and
  285. not(cs_link_separate_dbg_file in current_settings.globalswitches) then
  286. Result:=Result + ' -s';
  287. if (cs_link_map in current_settings.globalswitches) then
  288. Result:=Result + ' -Map '+maybequoted(ChangeFileExt(current_module.exefilename,'.map'));
  289. if create_smartlink_sections then
  290. Result:=Result + ' --gc-sections';
  291. end;
  292. function tlinkerandroid.DoLink(IsSharedLib: boolean): boolean;
  293. var
  294. i: longint;
  295. binstr, cmdstr: TCmdStr;
  296. s, opts, outname: string;
  297. success: boolean;
  298. begin
  299. Result:=False;
  300. if IsSharedLib then
  301. outname:=current_module.sharedlibfilename
  302. else
  303. outname:=current_module.exefilename;
  304. if not(cs_link_nolink in current_settings.globalswitches) then
  305. Message1(exec_i_linking, outname);
  306. opts:='';
  307. if (cs_link_strip in current_settings.globalswitches) and
  308. not (cs_link_separate_dbg_file in current_settings.globalswitches) then
  309. opts:=opts + ' -s';
  310. if (cs_link_map in current_settings.globalswitches) then
  311. opts:=opts + ' -Map '+maybequoted(ChangeFileExt(outname,'.map'));
  312. if create_smartlink_sections then
  313. opts:=opts + ' --gc-sections';
  314. if (cs_link_staticflag in current_settings.globalswitches) then
  315. opts:=opts + ' -static'
  316. else
  317. if cshared then
  318. opts:=opts + ' -call_shared';
  319. if rlinkpath<>'' then
  320. opts:=opts+' --rpath-link '+rlinkpath;
  321. if not IsSharedLib then
  322. begin
  323. opts:=opts + ' --dynamic-linker=' + Info.DynamicLinker;
  324. { create dynamic symbol table? }
  325. if HasExports then
  326. opts:=opts+' -E';
  327. end;
  328. opts:=Trim(opts + ' ' + Info.ExtraOptions);
  329. { Write used files and libraries }
  330. WriteResponseFile(IsSharedLib);
  331. { Call linker }
  332. if IsSharedLib then
  333. s:=Info.DllCmd[1]
  334. else
  335. s:=Info.ExeCmd[1];
  336. SplitBinCmd(s, binstr, cmdstr);
  337. Replace(cmdstr,'$EXE',maybequoted(outname));
  338. Replace(cmdstr,'$OPT',opts);
  339. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  340. if IsSharedLib then
  341. Replace(cmdstr,'$SONAME',ExtractFileName(outname));
  342. binstr:=FindUtil(utilsprefix+BinStr);
  343. { We should use BFD version of LD, since GOLD version does not support INSERT command in linker scripts }
  344. if binstr <> '' then begin
  345. { Checking if ld.bfd exists }
  346. s:=ChangeFileExt(binstr, '.bfd' + source_info.exeext);
  347. if FileExists(s, True) then
  348. binstr:=s;
  349. end;
  350. success:=DoExec(binstr,CmdStr,true,false);
  351. { Create external .dbg file with debuginfo }
  352. if success and (cs_link_separate_dbg_file in current_settings.globalswitches) then
  353. begin
  354. for i:=1 to 3 do
  355. begin
  356. SplitBinCmd(Info.ExtDbgCmd[i],binstr,cmdstr);
  357. Replace(cmdstr,'$EXE',maybequoted(outname));
  358. Replace(cmdstr,'$DBGFN',maybequoted(extractfilename(current_module.dbgfilename)));
  359. Replace(cmdstr,'$DBG',maybequoted(current_module.dbgfilename));
  360. success:=DoExec(FindUtil(utilsprefix+BinStr),CmdStr,true,false);
  361. if not success then
  362. break;
  363. end;
  364. end;
  365. { Remove ReponseFile }
  366. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  367. DeleteFile(outputexedir+Info.ResName);
  368. Result:=success; { otherwise a recursive call to link method }
  369. end;
  370. function TLinkerAndroid.MakeExecutable:boolean;
  371. begin
  372. Result:=DoLink(False);
  373. end;
  374. Function TLinkerAndroid.MakeSharedLibrary:boolean;
  375. begin
  376. Result:=DoLink(True);
  377. end;
  378. {*****************************************************************************
  379. Initialize
  380. *****************************************************************************}
  381. initialization
  382. {$ifdef ARM}
  383. RegisterExternalLinker(system_arm_android_info,TLinkerAndroid);
  384. RegisterImport(system_arm_android,timportlibandroid);
  385. RegisterExport(system_arm_android,texportlibandroid);
  386. RegisterTarget(system_arm_android_info);
  387. {$endif ARM}
  388. {$ifdef I386}
  389. RegisterExternalLinker(system_i386_android_info,TLinkerAndroid);
  390. RegisterImport(system_i386_android,timportlibandroid);
  391. RegisterExport(system_i386_android,texportlibandroid);
  392. RegisterTarget(system_i386_android_info);
  393. {$endif I386}
  394. RegisterRes(res_elf_info,TWinLikeResourceFile);
  395. end.