t_gba.pas 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. {
  2. This unit implements support import,export,link routines
  3. for the (arm) GameBoy Advance target
  4. Copyright (c) 2001-2002 by Peter Vreman
  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_gba;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. symsym,symdef,
  23. import,export,link;
  24. type
  25. tlinkergba=class(texternallinker)
  26. private
  27. libctype:(libc5,glibc2,glibc21,uclibc);
  28. Function WriteResponseFile : Boolean;
  29. public
  30. constructor Create;override;
  31. procedure SetDefaultInfo;override;
  32. function MakeExecutable:boolean;override;
  33. end;
  34. implementation
  35. uses
  36. cutils,cclasses,verbose,systems,globtype,globals,
  37. symconst,script,fmodule,dos,aasmbase,aasmtai,aasmdata,aasmcpu,
  38. cpubase,cgobj,i_gba;
  39. {*****************************************************************************
  40. TLINKERLINUX
  41. *****************************************************************************}
  42. Constructor TLinkerGba.Create;
  43. begin
  44. Inherited Create;
  45. if not Dontlinkstdlibpath Then
  46. LibrarySearchPath.AddPath('/lib;/usr/lib;/usr/X11R6/lib',true);
  47. end;
  48. procedure TLinkerGba.SetDefaultInfo;
  49. {
  50. This will also detect which libc version will be used
  51. }
  52. begin
  53. with Info do
  54. begin
  55. //ExeCmd[1]:='ld $OPT $DYNLINK $STATIC $GCSECTIONS $STRIP -L. -o $EXE $RES';
  56. // Here we call ld with right options for GBA
  57. ExeCmd[1]:='ld $OPT $DYNLINK $STATIC $GCSECTIONS $STRIP -Ttext 0x08000000 -Tbss 0x03000000 -L. -o $EXE $RES';
  58. DllCmd[1]:='ld $OPT $INIT $FINI $SONAME -shared -L. -o $EXE $RES';
  59. DllCmd[2]:='strip --strip-unneeded $EXE';
  60. DynamicLinker:='/lib/ld-linux.so.2';
  61. libctype:=glibc2;
  62. end;
  63. end;
  64. Function TLinkerGba.WriteResponseFile: Boolean;
  65. Var
  66. linkres : TLinkRes;
  67. i : longint;
  68. cprtobj,
  69. gprtobj,
  70. prtobj : string[80];
  71. HPath : TStringListItem;
  72. s,s1,s2 : string;
  73. found1,
  74. found2,
  75. linklibc : boolean;
  76. begin
  77. WriteResponseFile:=False;
  78. { set special options for some targets }
  79. linklibc:=(SharedLibFiles.Find('c')<>nil);
  80. prtobj:='prt0';
  81. case libctype of
  82. glibc21:
  83. begin
  84. cprtobj:='cprt21';
  85. gprtobj:='gprt21';
  86. end;
  87. uclibc:
  88. begin
  89. cprtobj:='ucprt0';
  90. gprtobj:='ugprt0';
  91. end
  92. else
  93. cprtobj:='cprt0';
  94. gprtobj:='gprt0';
  95. end;
  96. if cs_profile in aktmoduleswitches then
  97. begin
  98. prtobj:=gprtobj;
  99. if not(libctype in [glibc2,glibc21]) then
  100. AddSharedLibrary('gmon');
  101. AddSharedLibrary('c');
  102. linklibc:=true;
  103. end
  104. else
  105. begin
  106. if linklibc then
  107. prtobj:=cprtobj;
  108. end;
  109. { Open link.res file }
  110. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName);
  111. { Write path to search libraries }
  112. HPath:=TStringListItem(current_module.locallibrarysearchpath.First);
  113. while assigned(HPath) do
  114. begin
  115. LinkRes.Add('SEARCH_DIR('+maybequoted(HPath.Str)+')');
  116. HPath:=TStringListItem(HPath.Next);
  117. end;
  118. HPath:=TStringListItem(LibrarySearchPath.First);
  119. while assigned(HPath) do
  120. begin
  121. LinkRes.Add('SEARCH_DIR('+maybequoted(HPath.Str)+')');
  122. HPath:=TStringListItem(HPath.Next);
  123. end;
  124. LinkRes.Add('INPUT(');
  125. { add objectfiles, start with prt0 always }
  126. if prtobj<>'' then
  127. LinkRes.AddFileName(maybequoted(FindObjectFile(prtobj,'',false)));
  128. { try to add crti and crtbegin if linking to C }
  129. if linklibc then
  130. begin
  131. if librarysearchpath.FindFile('crtbegin.o',s) then
  132. LinkRes.AddFileName(s);
  133. if librarysearchpath.FindFile('crti.o',s) then
  134. LinkRes.AddFileName(s);
  135. end;
  136. { main objectfiles }
  137. while not ObjectFiles.Empty do
  138. begin
  139. s:=ObjectFiles.GetFirst;
  140. if s<>'' then
  141. LinkRes.AddFileName(maybequoted(s));
  142. end;
  143. LinkRes.Add(')');
  144. { Write staticlibraries }
  145. if not StaticLibFiles.Empty then
  146. begin
  147. LinkRes.Add('GROUP(');
  148. While not StaticLibFiles.Empty do
  149. begin
  150. S:=StaticLibFiles.GetFirst;
  151. LinkRes.AddFileName(maybequoted(s))
  152. end;
  153. LinkRes.Add(')');
  154. end;
  155. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  156. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  157. if not SharedLibFiles.Empty then
  158. begin
  159. LinkRes.Add('INPUT(');
  160. While not SharedLibFiles.Empty do
  161. begin
  162. S:=SharedLibFiles.GetFirst;
  163. if s<>'c' then
  164. begin
  165. i:=Pos(target_info.sharedlibext,S);
  166. if i>0 then
  167. Delete(S,i,255);
  168. LinkRes.Add('-l'+s);
  169. end
  170. else
  171. begin
  172. linklibc:=true;
  173. end;
  174. end;
  175. { be sure that libc is the last lib }
  176. if linklibc then
  177. LinkRes.Add('-lc');
  178. { when we have -static for the linker the we also need libgcc }
  179. if (cs_link_staticflag in aktglobalswitches) then
  180. LinkRes.Add('-lgcc');
  181. LinkRes.Add(')');
  182. end;
  183. { objects which must be at the end }
  184. if linklibc and (libctype<>uclibc) then
  185. begin
  186. found1:=librarysearchpath.FindFile('crtend.o',s1);
  187. found2:=librarysearchpath.FindFile('crtn.o',s2);
  188. if found1 or found2 then
  189. begin
  190. LinkRes.Add('INPUT(');
  191. if found1 then
  192. LinkRes.AddFileName(s1);
  193. if found2 then
  194. LinkRes.AddFileName(s2);
  195. LinkRes.Add(')');
  196. end;
  197. end;
  198. { Write and Close response }
  199. linkres.writetodisk;
  200. linkres.Free;
  201. WriteResponseFile:=True;
  202. end;
  203. function TLinkerGba.MakeExecutable:boolean;
  204. var
  205. binstr : String;
  206. cmdstr : TCmdStr;
  207. success : boolean;
  208. DynLinkStr : string[60];
  209. GCSectionsStr,
  210. StaticStr,
  211. StripStr : string[40];
  212. begin
  213. if not(cs_link_extern in aktglobalswitches) then
  214. Message1(exec_i_linking,current_module.exefilename^);
  215. { Create some replacements }
  216. StaticStr:='';
  217. StripStr:='';
  218. GCSectionsStr:='';
  219. DynLinkStr:='';
  220. if (cs_link_staticflag in aktglobalswitches) then
  221. StaticStr:='-static';
  222. if (cs_link_strip in aktglobalswitches) then
  223. StripStr:='-s';
  224. if (cs_link_smart in aktglobalswitches) and
  225. (tf_smartlink_sections in target_info.flags) then
  226. GCSectionsStr:='--gc-sections';
  227. If (cs_profile in aktmoduleswitches) or
  228. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty)) then
  229. begin
  230. DynLinkStr:='-dynamic-linker='+Info.DynamicLinker;
  231. if cshared Then
  232. DynLinkStr:='--shared ' + DynLinkStr;
  233. if rlinkpath<>'' Then
  234. DynLinkStr:='--rpath-link '+rlinkpath + ' '+ DynLinkStr;
  235. End;
  236. { Write used files and libraries }
  237. WriteResponseFile;
  238. { Call linker }
  239. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  240. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename^));
  241. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  242. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  243. Replace(cmdstr,'$STATIC',StaticStr);
  244. Replace(cmdstr,'$STRIP',StripStr);
  245. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  246. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  247. success:=DoExec(FindUtil(utilsprefix+BinStr),CmdStr,true,false);
  248. { Remove ReponseFile }
  249. if (success) and not(cs_link_extern in aktglobalswitches) then
  250. RemoveFile(outputexedir+Info.ResName);
  251. MakeExecutable:=success; { otherwise a recursive call to link method }
  252. end;
  253. {*****************************************************************************
  254. Initialize
  255. *****************************************************************************}
  256. initialization
  257. RegisterExternalLinker(system_arm_gba_info,TLinkerGba);
  258. RegisterTarget(system_arm_gba_info);
  259. end.