t_embed.pas 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. {$ifdef ARM}
  183. with linkres do
  184. begin
  185. Add('SECTIONS');
  186. Add('{');
  187. Add(' /* interrupt vectors */');
  188. Add(' . = 0x0; /* start of flash */');
  189. Add(' .init :');
  190. Add(' {');
  191. Add(' *(.init)');
  192. Add(' }');
  193. Add(' /* code and constants */');
  194. Add(' .text :');
  195. Add(' {');
  196. Add(' *(.text)');
  197. Add(' *(.strings)');
  198. Add(' *(.rodata.*)');
  199. Add(' *(.comment)');
  200. Add(' }');
  201. Add(' /* uninitialized data */');
  202. Add(' . = 0x40000000; /* start of ram */');
  203. Add(' .bss :');
  204. Add(' {');
  205. Add(' *(.bss)');
  206. Add(' *(COMMON)');
  207. Add(' *(.data)');
  208. Add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  209. Add(' }');
  210. Add('}');
  211. end;
  212. {$endif ARM}
  213. { Write and Close response }
  214. linkres.writetodisk;
  215. linkres.free;
  216. WriteResponseFile:=True;
  217. end;
  218. function TlinkerEmbedded.MakeExecutable:boolean;
  219. var
  220. binstr,
  221. cmdstr : TCmdStr;
  222. success : boolean;
  223. StaticStr,
  224. GCSectionsStr,
  225. DynLinkStr,
  226. StripStr: string;
  227. begin
  228. { for future use }
  229. StaticStr:='';
  230. StripStr:='';
  231. DynLinkStr:='';
  232. GCSectionsStr:='--gc-sections';
  233. //if not(cs_link_extern in current_settings.globalswitches) then
  234. if not(cs_link_nolink in current_settings.globalswitches) then
  235. Message1(exec_i_linking,current_module.exefilename^);
  236. { Write used files and libraries }
  237. WriteResponseFile();
  238. { Call linker }
  239. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  240. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  241. if not(cs_link_on_target in current_settings.globalswitches) then
  242. begin
  243. Replace(cmdstr,'$EXE',(maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename^,'.elf')))));
  244. Replace(cmdstr,'$RES',(maybequoted(ScriptFixFileName(outputexedir+Info.ResName))));
  245. Replace(cmdstr,'$STATIC',StaticStr);
  246. Replace(cmdstr,'$STRIP',StripStr);
  247. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  248. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  249. end
  250. else
  251. begin
  252. Replace(cmdstr,'$EXE',maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename^,'.elf'))));
  253. Replace(cmdstr,'$RES',maybequoted(ScriptFixFileName(outputexedir+Info.ResName)));
  254. Replace(cmdstr,'$STATIC',StaticStr);
  255. Replace(cmdstr,'$STRIP',StripStr);
  256. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  257. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  258. end;
  259. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  260. { Remove ReponseFile }
  261. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  262. DeleteFile(outputexedir+Info.ResName);
  263. { Post process
  264. if success then
  265. begin
  266. success:=DoExec(FindUtil(utilsprefix+'objcopy'),'-O binary '+
  267. ChangeFileExt(current_module.exefilename^,'.elf')+' '+
  268. current_module.exefilename^,true,false);
  269. end;
  270. }
  271. MakeExecutable:=success; { otherwise a recursive call to link method }
  272. end;
  273. {*****************************************************************************
  274. Initialize
  275. *****************************************************************************}
  276. initialization
  277. {$ifdef arm}
  278. RegisterExternalLinker(system_arm_embedded_info,TlinkerEmbedded);
  279. RegisterTarget(system_arm_embedded_info);
  280. {$endif arm}
  281. {$ifdef avr}
  282. RegisterExternalLinker(system_avr_embedded_info,TlinkerEmbedded);
  283. RegisterTarget(system_avr_embedded_info);
  284. {$endif avr}
  285. end.