t_gba.pas 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. implementation
  22. uses
  23. link,
  24. cutils,cclasses,
  25. globtype,globals,systems,verbose,script,fmodule,i_gba;
  26. type
  27. TlinkerGBA=class(texternallinker)
  28. private
  29. Function WriteResponseFile: Boolean;
  30. public
  31. constructor Create; override;
  32. procedure SetDefaultInfo; override;
  33. function MakeExecutable:boolean; override;
  34. end;
  35. {*****************************************************************************
  36. TLINKERGBA
  37. *****************************************************************************}
  38. Constructor TLinkerGba.Create;
  39. begin
  40. Inherited Create;
  41. SharedLibFiles.doubles:=true;
  42. StaticLibFiles.doubles:=true;
  43. end;
  44. procedure TLinkerGba.SetDefaultInfo;
  45. begin
  46. with Info do
  47. begin
  48. ExeCmd[1]:='ld $OPT $DYNLINK $STATIC $GCSECTIONS $STRIP -L. -o $EXE $RES';
  49. end;
  50. end;
  51. Function TLinkerGba.WriteResponseFile: Boolean;
  52. Var
  53. linkres : TLinkRes;
  54. i : longint;
  55. HPath : TStringListItem;
  56. s : string;
  57. linklibc : boolean;
  58. begin
  59. WriteResponseFile:=False;
  60. { Open link.res file }
  61. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName);
  62. { Write path to search libraries }
  63. HPath:=TStringListItem(current_module.locallibrarysearchpath.First);
  64. while assigned(HPath) do
  65. begin
  66. s:=HPath.Str;
  67. if (cs_link_on_target in aktglobalswitches) then
  68. s:=ScriptFixFileName(s);
  69. LinkRes.Add('-L'+s);
  70. HPath:=TStringListItem(HPath.Next);
  71. end;
  72. HPath:=TStringListItem(LibrarySearchPath.First);
  73. while assigned(HPath) do
  74. begin
  75. s:=HPath.Str;
  76. if s<>'' then
  77. LinkRes.Add('SEARCH_DIR('+(maybequoted(s))+')');
  78. HPath:=TStringListItem(HPath.Next);
  79. end;
  80. LinkRes.Add('INPUT (');
  81. { add objectfiles, start with prt0 always }
  82. s:=FindObjectFile('prt0','',false);
  83. LinkRes.AddFileName(s);
  84. while not ObjectFiles.Empty do
  85. begin
  86. s:=ObjectFiles.GetFirst;
  87. if s<>'' then
  88. begin
  89. { vlink doesn't use SEARCH_DIR for object files }
  90. if not(cs_link_on_target in aktglobalswitches) then
  91. s:=FindObjectFile(s,'',false);
  92. LinkRes.AddFileName((maybequoted(s)));
  93. end;
  94. end;
  95. { Write staticlibraries }
  96. if not StaticLibFiles.Empty then
  97. begin
  98. { vlink doesn't need, and doesn't support GROUP }
  99. if (cs_link_on_target in aktglobalswitches) then
  100. begin
  101. LinkRes.Add(')');
  102. LinkRes.Add('GROUP(');
  103. end;
  104. while not StaticLibFiles.Empty do
  105. begin
  106. S:=StaticLibFiles.GetFirst;
  107. LinkRes.AddFileName((maybequoted(s)));
  108. end;
  109. end;
  110. if (cs_link_on_target in aktglobalswitches) then
  111. begin
  112. LinkRes.Add(')');
  113. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  114. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  115. linklibc:=false;
  116. while not SharedLibFiles.Empty do
  117. begin
  118. S:=SharedLibFiles.GetFirst;
  119. if s<>'c' then
  120. begin
  121. i:=Pos(target_info.sharedlibext,S);
  122. if i>0 then
  123. Delete(S,i,255);
  124. LinkRes.Add('-l'+s);
  125. end
  126. else
  127. begin
  128. LinkRes.Add('-l'+s);
  129. linklibc:=true;
  130. end;
  131. end;
  132. { be sure that libc&libgcc is the last lib }
  133. if linklibc then
  134. begin
  135. LinkRes.Add('-lc');
  136. LinkRes.Add('-lgcc');
  137. end;
  138. end
  139. else
  140. begin
  141. while not SharedLibFiles.Empty do
  142. begin
  143. S:=SharedLibFiles.GetFirst;
  144. LinkRes.Add('lib'+s+target_info.staticlibext);
  145. end;
  146. LinkRes.Add(')');
  147. end;
  148. { Write and Close response }
  149. linkres.writetodisk;
  150. linkres.free;
  151. WriteResponseFile:=True;
  152. end;
  153. function TLinkerGba.MakeExecutable:boolean;
  154. var
  155. binstr : string;
  156. cmdstr : TCmdStr;
  157. success : boolean;
  158. StripStr: string[40];
  159. begin
  160. //if not(cs_link_extern in aktglobalswitches) then
  161. if not(cs_link_nolink in aktglobalswitches) then
  162. Message1(exec_i_linking,current_module.exefilename^);
  163. { Write used files and libraries }
  164. WriteResponseFile();
  165. { Call linker }
  166. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  167. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  168. if not(cs_link_on_target in aktglobalswitches) then
  169. begin
  170. Replace(cmdstr,'$EXE',(maybequoted(ScriptFixFileName(current_module.exefilename^))));
  171. Replace(cmdstr,'$RES',(maybequoted(ScriptFixFileName(outputexedir+Info.ResName))));
  172. Replace(cmdstr,'$STRIP',StripStr);
  173. end
  174. else
  175. begin
  176. Replace(cmdstr,'$EXE',maybequoted(ScriptFixFileName(current_module.exefilename^)));
  177. Replace(cmdstr,'$RES',maybequoted(ScriptFixFileName(outputexedir+Info.ResName)));
  178. end;
  179. success:=DoExec(FindUtil(BinStr),cmdstr,true,false);
  180. { Remove ReponseFile }
  181. if (success) and not(cs_link_nolink in aktglobalswitches) then
  182. RemoveFile(outputexedir+Info.ResName);
  183. MakeExecutable:=success; { otherwise a recursive call to link method }
  184. end;
  185. {*****************************************************************************
  186. Initialize
  187. *****************************************************************************}
  188. initialization
  189. RegisterExternalLinker(system_arm_gba_info,TLinkerGba);
  190. RegisterTarget(system_arm_gba_info);
  191. end.