t_embed.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  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. {$if defined(ARM) or defined(i386) or defined(AVR)}
  67. prtobj:='';
  68. {$else}
  69. prtobj:='prt0';
  70. {$endif}
  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(' KEEP(*(.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. {$ifdef i386}
  267. with linkres do
  268. begin
  269. Add('ENTRY(_START)');
  270. Add('SECTIONS');
  271. Add('{');
  272. Add(' . = 0x100000;');
  273. Add(' .text ALIGN (0x1000) :');
  274. Add(' {');
  275. Add(' KEEP(*(.init, .init.*))');
  276. Add(' *(.text, .text.*)');
  277. Add(' *(.strings)');
  278. Add(' *(.rodata, .rodata.*)');
  279. Add(' *(.comment)');
  280. Add(' _etext = .;');
  281. Add(' }');
  282. Add(' .data ALIGN (0x1000) :');
  283. Add(' {');
  284. Add(' _data = .;');
  285. Add(' *(.data, .data.*)');
  286. Add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  287. Add(' _edata = .;');
  288. Add(' }');
  289. Add(' . = ALIGN(4);');
  290. Add(' .bss :');
  291. Add(' {');
  292. Add(' _bss_start = .;');
  293. Add(' *(.bss, .bss.*)');
  294. Add(' *(COMMON)');
  295. Add(' }');
  296. Add('_bss_end = . ;');
  297. Add('}');
  298. Add('_end = .;');
  299. end;
  300. {$endif I386}
  301. {$ifdef AVR}
  302. with linkres do
  303. begin
  304. { linker script from ld 2.19 }
  305. Add('ENTRY(_START)');
  306. Add('OUTPUT_FORMAT("elf32-avr","elf32-avr","elf32-avr")');
  307. Add('OUTPUT_ARCH(avr:2)');
  308. Add('MEMORY');
  309. Add('{');
  310. Add(' text (rx) : ORIGIN = 0, LENGTH = 8K');
  311. Add(' data (rw!x) : ORIGIN = 0x800060, LENGTH = 0xffa0');
  312. Add(' eeprom (rw!x) : ORIGIN = 0x810000, LENGTH = 64K');
  313. Add(' fuse (rw!x) : ORIGIN = 0x820000, LENGTH = 1K');
  314. Add(' lock (rw!x) : ORIGIN = 0x830000, LENGTH = 1K');
  315. Add(' signature (rw!x) : ORIGIN = 0x840000, LENGTH = 1K');
  316. Add('}');
  317. Add('SECTIONS');
  318. Add('{');
  319. Add(' /* Read-only sections, merged into text segment: */');
  320. Add(' .hash : { *(.hash) }');
  321. Add(' .dynsym : { *(.dynsym) }');
  322. Add(' .dynstr : { *(.dynstr) }');
  323. Add(' .gnu.version : { *(.gnu.version) }');
  324. Add(' .gnu.version_d : { *(.gnu.version_d) }');
  325. Add(' .gnu.version_r : { *(.gnu.version_r) }');
  326. Add(' .rel.init : { *(.rel.init) }');
  327. Add(' .rela.init : { *(.rela.init) }');
  328. Add(' .rel.text :');
  329. Add(' {');
  330. Add(' *(.rel.text)');
  331. Add(' *(.rel.text.*)');
  332. Add(' *(.rel.gnu.linkonce.t*)');
  333. Add(' }');
  334. Add(' .rela.text :');
  335. Add(' {');
  336. Add(' *(.rela.text)');
  337. Add(' *(.rela.text.*)');
  338. Add(' *(.rela.gnu.linkonce.t*)');
  339. Add(' }');
  340. Add(' .rel.fini : { *(.rel.fini) }');
  341. Add(' .rela.fini : { *(.rela.fini) }');
  342. Add(' .rel.rodata :');
  343. Add(' {');
  344. Add(' *(.rel.rodata)');
  345. Add(' *(.rel.rodata.*)');
  346. Add(' *(.rel.gnu.linkonce.r*)');
  347. Add(' }');
  348. Add(' .rela.rodata :');
  349. Add(' {');
  350. Add(' *(.rela.rodata)');
  351. Add(' *(.rela.rodata.*)');
  352. Add(' *(.rela.gnu.linkonce.r*)');
  353. Add(' }');
  354. Add(' .rel.data :');
  355. Add(' {');
  356. Add(' *(.rel.data)');
  357. Add(' *(.rel.data.*)');
  358. Add(' *(.rel.gnu.linkonce.d*)');
  359. Add(' }');
  360. Add(' .rela.data :');
  361. Add(' {');
  362. Add(' *(.rela.data)');
  363. Add(' *(.rela.data.*)');
  364. Add(' *(.rela.gnu.linkonce.d*)');
  365. Add(' }');
  366. Add(' .rel.ctors : { *(.rel.ctors) }');
  367. Add(' .rela.ctors : { *(.rela.ctors) }');
  368. Add(' .rel.dtors : { *(.rel.dtors) }');
  369. Add(' .rela.dtors : { *(.rela.dtors) }');
  370. Add(' .rel.got : { *(.rel.got) }');
  371. Add(' .rela.got : { *(.rela.got) }');
  372. Add(' .rel.bss : { *(.rel.bss) }');
  373. Add(' .rela.bss : { *(.rela.bss) }');
  374. Add(' .rel.plt : { *(.rel.plt) }');
  375. Add(' .rela.plt : { *(.rela.plt) }');
  376. Add(' /* Internal text space or external memory. */');
  377. Add(' .text :');
  378. Add(' {');
  379. Add(' *(.vectors)');
  380. Add(' KEEP(*(.vectors))');
  381. Add(' /* For data that needs to reside in the lower 64k of progmem. */');
  382. Add(' *(.progmem.gcc*)');
  383. Add(' *(.progmem*)');
  384. Add(' . = ALIGN(2);');
  385. Add(' __trampolines_start = . ;');
  386. Add(' /* The jump trampolines for the 16-bit limited relocs will reside here. */');
  387. Add(' *(.trampolines)');
  388. Add(' *(.trampolines*)');
  389. Add(' __trampolines_end = . ;');
  390. Add(' /* For future tablejump instruction arrays for 3 byte pc devices.');
  391. Add(' We don''t relax jump/call instructions within these sections. */');
  392. Add(' *(.jumptables)');
  393. Add(' *(.jumptables*)');
  394. Add(' /* For code that needs to reside in the lower 128k progmem. */');
  395. Add(' *(.lowtext)');
  396. Add(' *(.lowtext*)');
  397. Add(' __ctors_start = . ;');
  398. Add(' *(.ctors)');
  399. Add(' __ctors_end = . ;');
  400. Add(' __dtors_start = . ;');
  401. Add(' *(.dtors)');
  402. Add(' __dtors_end = . ;');
  403. Add(' KEEP(SORT(*)(.ctors))');
  404. Add(' KEEP(SORT(*)(.dtors))');
  405. Add(' /* From this point on, we don''t bother about wether the insns are');
  406. Add(' below or above the 16 bits boundary. */');
  407. Add(' *(.init0) /* Start here after reset. */');
  408. Add(' KEEP (*(.init0))');
  409. Add(' *(.init1)');
  410. Add(' KEEP (*(.init1))');
  411. Add(' *(.init2) /* Clear __zero_reg__, set up stack pointer. */');
  412. Add(' KEEP (*(.init2))');
  413. Add(' *(.init3)');
  414. Add(' KEEP (*(.init3))');
  415. Add(' *(.init4) /* Initialize data and BSS. */');
  416. Add(' KEEP (*(.init4))');
  417. Add(' *(.init5)');
  418. Add(' KEEP (*(.init5))');
  419. Add(' *(.init6) /* C++ constructors. */');
  420. Add(' KEEP (*(.init6))');
  421. Add(' *(.init7)');
  422. Add(' KEEP (*(.init7))');
  423. Add(' *(.init8)');
  424. Add(' KEEP (*(.init8))');
  425. Add(' *(.init9) /* Call main(). */');
  426. Add(' KEEP (*(.init9))');
  427. Add(' *(.text)');
  428. Add(' . = ALIGN(2);');
  429. Add(' *(.text.*)');
  430. Add(' . = ALIGN(2);');
  431. Add(' *(.fini9) /* _exit() starts here. */');
  432. Add(' KEEP (*(.fini9))');
  433. Add(' *(.fini8)');
  434. Add(' KEEP (*(.fini8))');
  435. Add(' *(.fini7)');
  436. Add(' KEEP (*(.fini7))');
  437. Add(' *(.fini6) /* C++ destructors. */');
  438. Add(' KEEP (*(.fini6))');
  439. Add(' *(.fini5)');
  440. Add(' KEEP (*(.fini5))');
  441. Add(' *(.fini4)');
  442. Add(' KEEP (*(.fini4))');
  443. Add(' *(.fini3)');
  444. Add(' KEEP (*(.fini3))');
  445. Add(' *(.fini2)');
  446. Add(' KEEP (*(.fini2))');
  447. Add(' *(.fini1)');
  448. Add(' KEEP (*(.fini1))');
  449. Add(' *(.fini0) /* Infinite loop after program termination. */');
  450. Add(' KEEP (*(.fini0))');
  451. Add(' _etext = . ;');
  452. Add(' } > text');
  453. Add(' .data : AT (ADDR (.text) + SIZEOF (.text))');
  454. Add(' {');
  455. Add(' PROVIDE (__data_start = .) ;');
  456. Add(' *(.data)');
  457. Add(' *(.data*)');
  458. Add(' *(.rodata) /* We need to include .rodata here if gcc is used */');
  459. Add(' *(.rodata*) /* with -fdata-sections. */');
  460. Add(' *(.gnu.linkonce.d*)');
  461. Add(' . = ALIGN(2);');
  462. Add(' _edata = . ;');
  463. Add(' PROVIDE (__data_end = .) ;');
  464. Add(' } > data');
  465. Add(' .bss : AT (ADDR (.bss))');
  466. Add(' {');
  467. Add(' PROVIDE (__bss_start = .) ;');
  468. Add(' *(.bss)');
  469. Add(' *(.bss*)');
  470. Add(' *(COMMON)');
  471. Add(' PROVIDE (__bss_end = .) ;');
  472. Add(' } > data');
  473. Add(' __data_load_start = LOADADDR(.data);');
  474. Add(' __data_load_end = __data_load_start + SIZEOF(.data);');
  475. Add(' /* Global data not cleared after reset. */');
  476. Add(' .noinit :');
  477. Add(' {');
  478. Add(' PROVIDE (__noinit_start = .) ;');
  479. Add(' *(.noinit*)');
  480. Add(' PROVIDE (__noinit_end = .) ;');
  481. Add(' _end = . ;');
  482. Add(' PROVIDE (__heap_start = .) ;');
  483. Add(' } > data');
  484. Add(' .eeprom :');
  485. Add(' {');
  486. Add(' *(.eeprom*)');
  487. Add(' __eeprom_end = . ;');
  488. Add(' } > eeprom');
  489. Add(' .fuse :');
  490. Add(' {');
  491. Add(' KEEP(*(.fuse))');
  492. Add(' KEEP(*(.lfuse))');
  493. Add(' KEEP(*(.hfuse))');
  494. Add(' KEEP(*(.efuse))');
  495. Add(' } > fuse');
  496. Add(' .lock :');
  497. Add(' {');
  498. Add(' KEEP(*(.lock*))');
  499. Add(' } > lock');
  500. Add(' .signature :');
  501. Add(' {');
  502. Add(' KEEP(*(.signature*))');
  503. Add(' } > signature');
  504. Add(' /* Stabs debugging sections. */');
  505. Add(' .stab 0 : { *(.stab) }');
  506. Add(' .stabstr 0 : { *(.stabstr) }');
  507. Add(' .stab.excl 0 : { *(.stab.excl) }');
  508. Add(' .stab.exclstr 0 : { *(.stab.exclstr) }');
  509. Add(' .stab.index 0 : { *(.stab.index) }');
  510. Add(' .stab.indexstr 0 : { *(.stab.indexstr) }');
  511. Add(' .comment 0 : { *(.comment) }');
  512. Add(' /* DWARF debug sections.');
  513. Add(' Symbols in the DWARF debugging sections are relative to the beginning');
  514. Add(' of the section so we begin them at 0. */');
  515. Add(' /* DWARF 1 */');
  516. Add(' .debug 0 : { *(.debug) }');
  517. Add(' .line 0 : { *(.line) }');
  518. Add(' /* GNU DWARF 1 extensions */');
  519. Add(' .debug_srcinfo 0 : { *(.debug_srcinfo) }');
  520. Add(' .debug_sfnames 0 : { *(.debug_sfnames) }');
  521. Add(' /* DWARF 1.1 and DWARF 2 */');
  522. Add(' .debug_aranges 0 : { *(.debug_aranges) }');
  523. Add(' .debug_pubnames 0 : { *(.debug_pubnames) }');
  524. Add(' /* DWARF 2 */');
  525. Add(' .debug_info 0 : { *(.debug_info) *(.gnu.linkonce.wi.*) }');
  526. Add(' .debug_abbrev 0 : { *(.debug_abbrev) }');
  527. Add(' .debug_line 0 : { *(.debug_line) }');
  528. Add(' .debug_frame 0 : { *(.debug_frame) }');
  529. Add(' .debug_str 0 : { *(.debug_str) }');
  530. Add(' .debug_loc 0 : { *(.debug_loc) }');
  531. Add(' .debug_macinfo 0 : { *(.debug_macinfo) }');
  532. Add('}');
  533. {$endif AVR}
  534. end;
  535. { Write and Close response }
  536. linkres.writetodisk;
  537. linkres.free;
  538. WriteResponseFile:=True;
  539. end;
  540. function TlinkerEmbedded.MakeExecutable:boolean;
  541. var
  542. binstr,
  543. cmdstr : TCmdStr;
  544. success : boolean;
  545. StaticStr,
  546. GCSectionsStr,
  547. DynLinkStr,
  548. StripStr: string;
  549. begin
  550. { for future use }
  551. StaticStr:='';
  552. StripStr:='';
  553. DynLinkStr:='';
  554. GCSectionsStr:='--gc-sections';
  555. //if not(cs_link_extern in current_settings.globalswitches) then
  556. if not(cs_link_nolink in current_settings.globalswitches) then
  557. Message1(exec_i_linking,current_module.exefilename^);
  558. { Write used files and libraries }
  559. WriteResponseFile();
  560. { Call linker }
  561. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  562. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  563. if not(cs_link_on_target in current_settings.globalswitches) then
  564. begin
  565. Replace(cmdstr,'$EXE',(maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename^,'.elf')))));
  566. Replace(cmdstr,'$RES',(maybequoted(ScriptFixFileName(outputexedir+Info.ResName))));
  567. Replace(cmdstr,'$STATIC',StaticStr);
  568. Replace(cmdstr,'$STRIP',StripStr);
  569. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  570. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  571. end
  572. else
  573. begin
  574. Replace(cmdstr,'$EXE',maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename^,'.elf'))));
  575. Replace(cmdstr,'$RES',maybequoted(ScriptFixFileName(outputexedir+Info.ResName)));
  576. Replace(cmdstr,'$STATIC',StaticStr);
  577. Replace(cmdstr,'$STRIP',StripStr);
  578. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  579. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  580. end;
  581. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  582. { Remove ReponseFile }
  583. if success and not(cs_link_nolink in current_settings.globalswitches) then
  584. DeleteFile(outputexedir+Info.ResName);
  585. { Post process }
  586. if success and (target_info.system=system_arm_embedded) then
  587. begin
  588. success:=DoExec(FindUtil(utilsprefix+'objcopy'),'-O ihex '+
  589. ChangeFileExt(current_module.exefilename^,'.elf')+' '+
  590. ChangeFileExt(current_module.exefilename^,'.hex'),true,false);
  591. end;
  592. MakeExecutable:=success; { otherwise a recursive call to link method }
  593. end;
  594. {*****************************************************************************
  595. Initialize
  596. *****************************************************************************}
  597. initialization
  598. {$ifdef arm}
  599. RegisterExternalLinker(system_arm_embedded_info,TlinkerEmbedded);
  600. RegisterTarget(system_arm_embedded_info);
  601. {$endif arm}
  602. {$ifdef avr}
  603. RegisterExternalLinker(system_avr_embedded_info,TlinkerEmbedded);
  604. RegisterTarget(system_avr_embedded_info);
  605. {$endif avr}
  606. {$ifdef i386}
  607. RegisterExternalLinker(system_i386_embedded_info,TlinkerEmbedded);
  608. RegisterTarget(system_i386_embedded_info);
  609. {$endif i386}
  610. end.