t_embed.pas 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. {$ifdef ARM}
  66. prtobj:='';
  67. {$else ARM}
  68. prtobj:='prt0';
  69. {$endif ARM}
  70. cprtobj:='cprt0';
  71. if linklibc then
  72. prtobj:=cprtobj;
  73. { Open link.res file }
  74. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName);
  75. { Write path to search libraries }
  76. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  77. while assigned(HPath) do
  78. begin
  79. s:=HPath.Str;
  80. if (cs_link_on_target in current_settings.globalswitches) then
  81. s:=ScriptFixFileName(s);
  82. LinkRes.Add('-L'+s);
  83. HPath:=TCmdStrListItem(HPath.Next);
  84. end;
  85. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  86. while assigned(HPath) do
  87. begin
  88. s:=HPath.Str;
  89. if s<>'' then
  90. LinkRes.Add('SEARCH_DIR('+(maybequoted(s))+')');
  91. HPath:=TCmdStrListItem(HPath.Next);
  92. end;
  93. LinkRes.Add('INPUT (');
  94. { add objectfiles, start with prt0 always }
  95. //s:=FindObjectFile('prt0','',false);
  96. if prtobj<>'' then
  97. begin
  98. s:=FindObjectFile(prtobj,'',false);
  99. LinkRes.AddFileName(s);
  100. end;
  101. { try to add crti and crtbegin if linking to C }
  102. if linklibc then
  103. begin
  104. if librarysearchpath.FindFile('crtbegin.o',false,s) then
  105. LinkRes.AddFileName(s);
  106. if librarysearchpath.FindFile('crti.o',false,s) then
  107. LinkRes.AddFileName(s);
  108. end;
  109. while not ObjectFiles.Empty do
  110. begin
  111. s:=ObjectFiles.GetFirst;
  112. if s<>'' then
  113. begin
  114. { vlink doesn't use SEARCH_DIR for object files }
  115. if not(cs_link_on_target in current_settings.globalswitches) then
  116. s:=FindObjectFile(s,'',false);
  117. LinkRes.AddFileName((maybequoted(s)));
  118. end;
  119. end;
  120. { Write staticlibraries }
  121. if not StaticLibFiles.Empty then
  122. begin
  123. { vlink doesn't need, and doesn't support GROUP }
  124. if (cs_link_on_target in current_settings.globalswitches) then
  125. begin
  126. LinkRes.Add(')');
  127. LinkRes.Add('GROUP(');
  128. end;
  129. while not StaticLibFiles.Empty do
  130. begin
  131. S:=StaticLibFiles.GetFirst;
  132. LinkRes.AddFileName((maybequoted(s)));
  133. end;
  134. end;
  135. if (cs_link_on_target in current_settings.globalswitches) then
  136. begin
  137. LinkRes.Add(')');
  138. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  139. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  140. linklibc:=false;
  141. while not SharedLibFiles.Empty do
  142. begin
  143. S:=SharedLibFiles.GetFirst;
  144. if s<>'c' then
  145. begin
  146. i:=Pos(target_info.sharedlibext,S);
  147. if i>0 then
  148. Delete(S,i,255);
  149. LinkRes.Add('-l'+s);
  150. end
  151. else
  152. begin
  153. LinkRes.Add('-l'+s);
  154. linklibc:=true;
  155. end;
  156. end;
  157. { be sure that libc&libgcc is the last lib }
  158. if linklibc then
  159. begin
  160. LinkRes.Add('-lc');
  161. LinkRes.Add('-lgcc');
  162. end;
  163. end
  164. else
  165. begin
  166. while not SharedLibFiles.Empty do
  167. begin
  168. S:=SharedLibFiles.GetFirst;
  169. LinkRes.Add('lib'+s+target_info.staticlibext);
  170. end;
  171. LinkRes.Add(')');
  172. end;
  173. { objects which must be at the end }
  174. if linklibc then
  175. begin
  176. found1:=librarysearchpath.FindFile('crtend.o',false,s1);
  177. found2:=librarysearchpath.FindFile('crtn.o',false,s2);
  178. if found1 or found2 then
  179. begin
  180. LinkRes.Add('INPUT(');
  181. if found1 then
  182. LinkRes.AddFileName(s1);
  183. if found2 then
  184. LinkRes.AddFileName(s2);
  185. LinkRes.Add(')');
  186. end;
  187. end;
  188. {$ifdef ARM}
  189. with linkres do
  190. begin
  191. add('ENTRY(_START)');
  192. Add('SECTIONS');
  193. Add('{');
  194. Add(' . = 0x0; /* start of flash */');
  195. Add(' /* code and constants */');
  196. Add(' .text :');
  197. Add(' {');
  198. Add(' *(.init, .init.*)');
  199. Add(' *(.text, .text.*)');
  200. Add(' *(.strings)');
  201. Add(' *(.rodata.*)');
  202. Add(' *(.comment)');
  203. Add(' }');
  204. Add(' /* uninitialized data */');
  205. Add(' . = 0x40000000; /* start of ram */');
  206. Add(' .bss :');
  207. Add(' {');
  208. Add(' *(.bss, .bss.*)');
  209. Add(' *(COMMON)');
  210. Add(' *(.data, .data.*)');
  211. Add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  212. Add(' }');
  213. Add('}');
  214. end;
  215. {$endif ARM}
  216. { Write and Close response }
  217. linkres.writetodisk;
  218. linkres.free;
  219. WriteResponseFile:=True;
  220. end;
  221. function TlinkerEmbedded.MakeExecutable:boolean;
  222. var
  223. binstr,
  224. cmdstr : TCmdStr;
  225. success : boolean;
  226. StaticStr,
  227. GCSectionsStr,
  228. DynLinkStr,
  229. StripStr: string;
  230. begin
  231. { for future use }
  232. StaticStr:='';
  233. StripStr:='';
  234. DynLinkStr:='';
  235. GCSectionsStr:='--gc-sections';
  236. //if not(cs_link_extern in current_settings.globalswitches) then
  237. if not(cs_link_nolink in current_settings.globalswitches) then
  238. Message1(exec_i_linking,current_module.exefilename^);
  239. { Write used files and libraries }
  240. WriteResponseFile();
  241. { Call linker }
  242. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  243. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  244. if not(cs_link_on_target in current_settings.globalswitches) then
  245. begin
  246. Replace(cmdstr,'$EXE',(maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename^,'.elf')))));
  247. Replace(cmdstr,'$RES',(maybequoted(ScriptFixFileName(outputexedir+Info.ResName))));
  248. Replace(cmdstr,'$STATIC',StaticStr);
  249. Replace(cmdstr,'$STRIP',StripStr);
  250. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  251. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  252. end
  253. else
  254. begin
  255. Replace(cmdstr,'$EXE',maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename^,'.elf'))));
  256. Replace(cmdstr,'$RES',maybequoted(ScriptFixFileName(outputexedir+Info.ResName)));
  257. Replace(cmdstr,'$STATIC',StaticStr);
  258. Replace(cmdstr,'$STRIP',StripStr);
  259. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  260. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  261. end;
  262. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  263. { Remove ReponseFile }
  264. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  265. DeleteFile(outputexedir+Info.ResName);
  266. { Post process
  267. if success then
  268. begin
  269. success:=DoExec(FindUtil(utilsprefix+'objcopy'),'-O binary '+
  270. ChangeFileExt(current_module.exefilename^,'.elf')+' '+
  271. current_module.exefilename^,true,false);
  272. end;
  273. }
  274. MakeExecutable:=success; { otherwise a recursive call to link method }
  275. end;
  276. {*****************************************************************************
  277. Initialize
  278. *****************************************************************************}
  279. initialization
  280. {$ifdef arm}
  281. RegisterExternalLinker(system_arm_embedded_info,TlinkerEmbedded);
  282. RegisterTarget(system_arm_embedded_info);
  283. {$endif arm}
  284. {$ifdef avr}
  285. RegisterExternalLinker(system_avr_embedded_info,TlinkerEmbedded);
  286. RegisterTarget(system_avr_embedded_info);
  287. {$endif avr}
  288. end.