t_android.pas 13 KB

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