2
0

t_embed.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. cpuinfo;
  27. type
  28. TlinkerEmbedded=class(texternallinker)
  29. private
  30. Function WriteResponseFile: Boolean;
  31. public
  32. constructor Create; override;
  33. procedure SetDefaultInfo; override;
  34. function MakeExecutable:boolean; override;
  35. end;
  36. {*****************************************************************************
  37. TlinkerEmbedded
  38. *****************************************************************************}
  39. Constructor TlinkerEmbedded.Create;
  40. begin
  41. Inherited Create;
  42. SharedLibFiles.doubles:=true;
  43. StaticLibFiles.doubles:=true;
  44. end;
  45. procedure TlinkerEmbedded.SetDefaultInfo;
  46. begin
  47. with Info do
  48. begin
  49. ExeCmd[1]:='ld -g $OPT $DYNLINK $STATIC $GCSECTIONS $STRIP -L. -o $EXE -T $RES';
  50. end;
  51. end;
  52. Function TlinkerEmbedded.WriteResponseFile: Boolean;
  53. Var
  54. linkres : TLinkRes;
  55. i : longint;
  56. HPath : TCmdStrListItem;
  57. s,s1,s2 : TCmdStr;
  58. prtobj,
  59. cprtobj : string[80];
  60. linklibc : boolean;
  61. found1,
  62. found2 : boolean;
  63. begin
  64. WriteResponseFile:=False;
  65. linklibc:=(SharedLibFiles.Find('c')<>nil);
  66. {$ifdef ARM}
  67. prtobj:='';
  68. {$else ARM}
  69. prtobj:='prt0';
  70. {$endif ARM}
  71. cprtobj:='cprt0';
  72. if linklibc then
  73. prtobj:=cprtobj;
  74. { Open link.res file }
  75. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName);
  76. { Write path to search libraries }
  77. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  78. while assigned(HPath) do
  79. begin
  80. s:=HPath.Str;
  81. if (cs_link_on_target in current_settings.globalswitches) then
  82. s:=ScriptFixFileName(s);
  83. LinkRes.Add('-L'+s);
  84. HPath:=TCmdStrListItem(HPath.Next);
  85. end;
  86. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  87. while assigned(HPath) do
  88. begin
  89. s:=HPath.Str;
  90. if s<>'' then
  91. LinkRes.Add('SEARCH_DIR('+(maybequoted(s))+')');
  92. HPath:=TCmdStrListItem(HPath.Next);
  93. end;
  94. LinkRes.Add('INPUT (');
  95. { add objectfiles, start with prt0 always }
  96. //s:=FindObjectFile('prt0','',false);
  97. if prtobj<>'' then
  98. begin
  99. s:=FindObjectFile(prtobj,'',false);
  100. LinkRes.AddFileName(s);
  101. end;
  102. { try to add crti and crtbegin if linking to C }
  103. if linklibc then
  104. begin
  105. if librarysearchpath.FindFile('crtbegin.o',false,s) then
  106. LinkRes.AddFileName(s);
  107. if librarysearchpath.FindFile('crti.o',false,s) then
  108. LinkRes.AddFileName(s);
  109. end;
  110. while not ObjectFiles.Empty do
  111. begin
  112. s:=ObjectFiles.GetFirst;
  113. if s<>'' then
  114. begin
  115. { vlink doesn't use SEARCH_DIR for object files }
  116. if not(cs_link_on_target in current_settings.globalswitches) then
  117. s:=FindObjectFile(s,'',false);
  118. LinkRes.AddFileName((maybequoted(s)));
  119. end;
  120. end;
  121. { Write staticlibraries }
  122. if not StaticLibFiles.Empty then
  123. begin
  124. { vlink doesn't need, and doesn't support GROUP }
  125. if (cs_link_on_target in current_settings.globalswitches) then
  126. begin
  127. LinkRes.Add(')');
  128. LinkRes.Add('GROUP(');
  129. end;
  130. while not StaticLibFiles.Empty do
  131. begin
  132. S:=StaticLibFiles.GetFirst;
  133. LinkRes.AddFileName((maybequoted(s)));
  134. end;
  135. end;
  136. if (cs_link_on_target in current_settings.globalswitches) then
  137. begin
  138. LinkRes.Add(')');
  139. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  140. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  141. linklibc:=false;
  142. while not SharedLibFiles.Empty do
  143. begin
  144. S:=SharedLibFiles.GetFirst;
  145. if s<>'c' then
  146. begin
  147. i:=Pos(target_info.sharedlibext,S);
  148. if i>0 then
  149. Delete(S,i,255);
  150. LinkRes.Add('-l'+s);
  151. end
  152. else
  153. begin
  154. LinkRes.Add('-l'+s);
  155. linklibc:=true;
  156. end;
  157. end;
  158. { be sure that libc&libgcc is the last lib }
  159. if linklibc then
  160. begin
  161. LinkRes.Add('-lc');
  162. LinkRes.Add('-lgcc');
  163. end;
  164. end
  165. else
  166. begin
  167. while not SharedLibFiles.Empty do
  168. begin
  169. S:=SharedLibFiles.GetFirst;
  170. LinkRes.Add('lib'+s+target_info.staticlibext);
  171. end;
  172. LinkRes.Add(')');
  173. end;
  174. { objects which must be at the end }
  175. if linklibc then
  176. begin
  177. found1:=librarysearchpath.FindFile('crtend.o',false,s1);
  178. found2:=librarysearchpath.FindFile('crtn.o',false,s2);
  179. if found1 or found2 then
  180. begin
  181. LinkRes.Add('INPUT(');
  182. if found1 then
  183. LinkRes.AddFileName(s1);
  184. if found2 then
  185. LinkRes.AddFileName(s2);
  186. LinkRes.Add(')');
  187. end;
  188. end;
  189. {$ifdef ARM}
  190. case current_settings.controllertype of
  191. ct_none:
  192. ;
  193. ct_lpc2114,
  194. ct_lpc2124,
  195. ct_lpc2194:
  196. with linkres do
  197. begin
  198. Add('ENTRY(_START)');
  199. Add('MEMORY');
  200. Add('{');
  201. Add(' flash : ORIGIN = 0, LENGTH = 256K');
  202. Add(' ram : ORIGIN = 0x40000000, LENGTH = 16K');
  203. Add('}');
  204. Add('_stack_top = 0x40003FFC;');
  205. end;
  206. ct_at91sam7s256,
  207. ct_at91sam7se256,
  208. ct_at91sam7x256,
  209. ct_at91sam7xc256:
  210. with linkres do
  211. begin
  212. Add('ENTRY(_START)');
  213. Add('MEMORY');
  214. Add('{');
  215. Add(' flash : ORIGIN = 0, LENGTH = 256K');
  216. Add(' ram : ORIGIN = 0x200000, LENGTH = 64K');
  217. Add('}');
  218. Add('_stack_top = 0x20FFFC;');
  219. end;
  220. ct_stm32f103re:
  221. with linkres do
  222. begin
  223. Add('ENTRY(_START)');
  224. Add('MEMORY');
  225. Add('{');
  226. Add(' flash : ORIGIN = 0x08000000, LENGTH = 512K');
  227. Add(' ram : ORIGIN = 0x20000000, LENGTH = 64K');
  228. Add('}');
  229. Add('_stack_top = 0x2000FFFC;');
  230. end;
  231. else
  232. internalerror(200902011);
  233. end;
  234. with linkres do
  235. begin
  236. Add('SECTIONS');
  237. Add('{');
  238. Add(' .text :');
  239. Add(' {');
  240. Add(' *(.init, .init.*)');
  241. Add(' *(.text, .text.*)');
  242. Add(' *(.strings)');
  243. Add(' *(.rodata, .rodata.*)');
  244. Add(' *(.comment)');
  245. Add(' _etext = .;');
  246. Add(' } >flash');
  247. Add(' .data :');
  248. Add(' {');
  249. Add(' _data = .;');
  250. Add(' *(.data, .data.*)');
  251. Add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  252. Add(' _edata = .;');
  253. Add(' } >ram AT >flash');
  254. Add(' .bss :');
  255. Add(' {');
  256. Add(' _bss_start = .;');
  257. Add(' *(.bss, .bss.*)');
  258. Add(' *(COMMON)');
  259. Add(' } >ram');
  260. Add('. = ALIGN(4);');
  261. Add('_bss_end = . ;');
  262. Add('}');
  263. Add('_end = .;');
  264. end;
  265. {$endif ARM}
  266. { Write and Close response }
  267. linkres.writetodisk;
  268. linkres.free;
  269. WriteResponseFile:=True;
  270. end;
  271. function TlinkerEmbedded.MakeExecutable:boolean;
  272. var
  273. binstr,
  274. cmdstr : TCmdStr;
  275. success : boolean;
  276. StaticStr,
  277. GCSectionsStr,
  278. DynLinkStr,
  279. StripStr: string;
  280. begin
  281. { for future use }
  282. StaticStr:='';
  283. StripStr:='';
  284. DynLinkStr:='';
  285. GCSectionsStr:='--gc-sections';
  286. //if not(cs_link_extern in current_settings.globalswitches) then
  287. if not(cs_link_nolink in current_settings.globalswitches) then
  288. Message1(exec_i_linking,current_module.exefilename^);
  289. { Write used files and libraries }
  290. WriteResponseFile();
  291. { Call linker }
  292. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  293. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  294. if not(cs_link_on_target in current_settings.globalswitches) then
  295. begin
  296. Replace(cmdstr,'$EXE',(maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename^,'.elf')))));
  297. Replace(cmdstr,'$RES',(maybequoted(ScriptFixFileName(outputexedir+Info.ResName))));
  298. Replace(cmdstr,'$STATIC',StaticStr);
  299. Replace(cmdstr,'$STRIP',StripStr);
  300. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  301. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  302. end
  303. else
  304. begin
  305. Replace(cmdstr,'$EXE',maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename^,'.elf'))));
  306. Replace(cmdstr,'$RES',maybequoted(ScriptFixFileName(outputexedir+Info.ResName)));
  307. Replace(cmdstr,'$STATIC',StaticStr);
  308. Replace(cmdstr,'$STRIP',StripStr);
  309. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  310. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  311. end;
  312. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  313. { Remove ReponseFile }
  314. if success and not(cs_link_nolink in current_settings.globalswitches) then
  315. DeleteFile(outputexedir+Info.ResName);
  316. { Post process }
  317. if success then
  318. begin
  319. success:=DoExec(FindUtil(utilsprefix+'objcopy'),'-O ihex '+
  320. ChangeFileExt(current_module.exefilename^,'.elf')+' '+
  321. ChangeFileExt(current_module.exefilename^,'.hex'),true,false);
  322. end;
  323. MakeExecutable:=success; { otherwise a recursive call to link method }
  324. end;
  325. {*****************************************************************************
  326. Initialize
  327. *****************************************************************************}
  328. initialization
  329. {$ifdef arm}
  330. RegisterExternalLinker(system_arm_embedded_info,TlinkerEmbedded);
  331. RegisterTarget(system_arm_embedded_info);
  332. {$endif arm}
  333. {$ifdef avr}
  334. RegisterExternalLinker(system_avr_embedded_info,TlinkerEmbedded);
  335. RegisterTarget(system_avr_embedded_info);
  336. {$endif avr}
  337. end.