t_embed.pas 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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_embed;
  19. {$i fpcdefs.inc}
  20. interface
  21. implementation
  22. uses
  23. SysUtils,
  24. cutils,cfileutl,cclasses,
  25. globtype,globals,systems,verbose,script,fmodule,i_embed,link;
  26. type
  27. TlinkerEmbedded=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. TlinkerEmbedded
  37. *****************************************************************************}
  38. Constructor TlinkerEmbedded.Create;
  39. begin
  40. Inherited Create;
  41. SharedLibFiles.doubles:=true;
  42. StaticLibFiles.doubles:=true;
  43. end;
  44. procedure TlinkerEmbedded.SetDefaultInfo;
  45. begin
  46. with Info do
  47. begin
  48. ExeCmd[1]:='ld -g $OPT $DYNLINK $STATIC $GCSECTIONS $STRIP -L. -o $EXE -T $RES';
  49. end;
  50. end;
  51. Function TlinkerEmbedded.WriteResponseFile: Boolean;
  52. Var
  53. linkres : TLinkRes;
  54. i : longint;
  55. HPath : TCmdStrListItem;
  56. s,s1,s2 : TCmdStr;
  57. prtobj,
  58. cprtobj : string[80];
  59. linklibc : boolean;
  60. found1,
  61. found2 : boolean;
  62. begin
  63. WriteResponseFile:=False;
  64. linklibc:=(SharedLibFiles.Find('c')<>nil);
  65. prtobj:='prt0';
  66. cprtobj:='cprt0';
  67. if linklibc then
  68. prtobj:=cprtobj;
  69. { Open link.res file }
  70. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName);
  71. { Write path to search libraries }
  72. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  73. while assigned(HPath) do
  74. begin
  75. s:=HPath.Str;
  76. if (cs_link_on_target in current_settings.globalswitches) then
  77. s:=ScriptFixFileName(s);
  78. LinkRes.Add('-L'+s);
  79. HPath:=TCmdStrListItem(HPath.Next);
  80. end;
  81. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  82. while assigned(HPath) do
  83. begin
  84. s:=HPath.Str;
  85. if s<>'' then
  86. LinkRes.Add('SEARCH_DIR('+(maybequoted(s))+')');
  87. HPath:=TCmdStrListItem(HPath.Next);
  88. end;
  89. LinkRes.Add('INPUT (');
  90. { add objectfiles, start with prt0 always }
  91. //s:=FindObjectFile('prt0','',false);
  92. if prtobj<>'' then
  93. s:=FindObjectFile(prtobj,'',false);
  94. LinkRes.AddFileName(s);
  95. { try to add crti and crtbegin if linking to C }
  96. if linklibc then
  97. begin
  98. if librarysearchpath.FindFile('crtbegin.o',false,s) then
  99. LinkRes.AddFileName(s);
  100. if librarysearchpath.FindFile('crti.o',false,s) then
  101. LinkRes.AddFileName(s);
  102. end;
  103. while not ObjectFiles.Empty do
  104. begin
  105. s:=ObjectFiles.GetFirst;
  106. if s<>'' then
  107. begin
  108. { vlink doesn't use SEARCH_DIR for object files }
  109. if not(cs_link_on_target in current_settings.globalswitches) then
  110. s:=FindObjectFile(s,'',false);
  111. LinkRes.AddFileName((maybequoted(s)));
  112. end;
  113. end;
  114. { Write staticlibraries }
  115. if not StaticLibFiles.Empty then
  116. begin
  117. { vlink doesn't need, and doesn't support GROUP }
  118. if (cs_link_on_target in current_settings.globalswitches) then
  119. begin
  120. LinkRes.Add(')');
  121. LinkRes.Add('GROUP(');
  122. end;
  123. while not StaticLibFiles.Empty do
  124. begin
  125. S:=StaticLibFiles.GetFirst;
  126. LinkRes.AddFileName((maybequoted(s)));
  127. end;
  128. end;
  129. if (cs_link_on_target in current_settings.globalswitches) then
  130. begin
  131. LinkRes.Add(')');
  132. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  133. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  134. linklibc:=false;
  135. while not SharedLibFiles.Empty do
  136. begin
  137. S:=SharedLibFiles.GetFirst;
  138. if s<>'c' then
  139. begin
  140. i:=Pos(target_info.sharedlibext,S);
  141. if i>0 then
  142. Delete(S,i,255);
  143. LinkRes.Add('-l'+s);
  144. end
  145. else
  146. begin
  147. LinkRes.Add('-l'+s);
  148. linklibc:=true;
  149. end;
  150. end;
  151. { be sure that libc&libgcc is the last lib }
  152. if linklibc then
  153. begin
  154. LinkRes.Add('-lc');
  155. LinkRes.Add('-lgcc');
  156. end;
  157. end
  158. else
  159. begin
  160. while not SharedLibFiles.Empty do
  161. begin
  162. S:=SharedLibFiles.GetFirst;
  163. LinkRes.Add('lib'+s+target_info.staticlibext);
  164. end;
  165. LinkRes.Add(')');
  166. end;
  167. { objects which must be at the end }
  168. if linklibc then
  169. begin
  170. found1:=librarysearchpath.FindFile('crtend.o',false,s1);
  171. found2:=librarysearchpath.FindFile('crtn.o',false,s2);
  172. if found1 or found2 then
  173. begin
  174. LinkRes.Add('INPUT(');
  175. if found1 then
  176. LinkRes.AddFileName(s1);
  177. if found2 then
  178. LinkRes.AddFileName(s2);
  179. LinkRes.Add(')');
  180. end;
  181. end;
  182. { Write and Close response }
  183. linkres.writetodisk;
  184. linkres.free;
  185. WriteResponseFile:=True;
  186. end;
  187. function TlinkerEmbedded.MakeExecutable:boolean;
  188. var
  189. binstr,
  190. cmdstr : TCmdStr;
  191. success : boolean;
  192. StaticStr,
  193. GCSectionsStr,
  194. DynLinkStr,
  195. StripStr: string;
  196. begin
  197. { for future use }
  198. StaticStr:='';
  199. StripStr:='';
  200. DynLinkStr:='';
  201. GCSectionsStr:='--gc-sections';
  202. //if not(cs_link_extern in current_settings.globalswitches) then
  203. if not(cs_link_nolink in current_settings.globalswitches) then
  204. Message1(exec_i_linking,current_module.exefilename^);
  205. { Write used files and libraries }
  206. WriteResponseFile();
  207. { Call linker }
  208. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  209. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  210. if not(cs_link_on_target in current_settings.globalswitches) then
  211. begin
  212. Replace(cmdstr,'$EXE',(maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename^,'.elf')))));
  213. Replace(cmdstr,'$RES',(maybequoted(ScriptFixFileName(outputexedir+Info.ResName))));
  214. Replace(cmdstr,'$STATIC',StaticStr);
  215. Replace(cmdstr,'$STRIP',StripStr);
  216. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  217. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  218. end
  219. else
  220. begin
  221. Replace(cmdstr,'$EXE',maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename^,'.elf'))));
  222. Replace(cmdstr,'$RES',maybequoted(ScriptFixFileName(outputexedir+Info.ResName)));
  223. Replace(cmdstr,'$STATIC',StaticStr);
  224. Replace(cmdstr,'$STRIP',StripStr);
  225. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  226. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  227. end;
  228. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  229. { Remove ReponseFile }
  230. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  231. DeleteFile(outputexedir+Info.ResName);
  232. { Post process
  233. if success then
  234. begin
  235. success:=DoExec(FindUtil(utilsprefix+'objcopy'),'-O binary '+
  236. ChangeFileExt(current_module.exefilename^,'.elf')+' '+
  237. current_module.exefilename^,true,false);
  238. end;
  239. }
  240. MakeExecutable:=success; { otherwise a recursive call to link method }
  241. end;
  242. {*****************************************************************************
  243. Initialize
  244. *****************************************************************************}
  245. initialization
  246. RegisterExternalLinker(system_arm_embedded_info,TlinkerEmbedded);
  247. RegisterTarget(system_arm_embedded_info);
  248. end.