t_embed.pas 9.1 KB

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