/compiler/systems/t_embed.pas

https://github.com/slibre/freepascal · Pascal · 939 lines · 800 code · 63 blank · 76 comment · 46 complexity · f307ae7ffcc44cbf7e199f8b14219985 MD5 · raw file

  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,comphook,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. function postprocessexecutable(const fn : string;isdll:boolean):boolean;
  36. end;
  37. {*****************************************************************************
  38. TlinkerEmbedded
  39. *****************************************************************************}
  40. Constructor TlinkerEmbedded.Create;
  41. begin
  42. Inherited Create;
  43. SharedLibFiles.doubles:=true;
  44. StaticLibFiles.doubles:=true;
  45. end;
  46. procedure TlinkerEmbedded.SetDefaultInfo;
  47. begin
  48. with Info do
  49. begin
  50. ExeCmd[1]:='ld -g $OPT $DYNLINK $STATIC $GCSECTIONS $STRIP -L. -o $EXE -T $RES';
  51. end;
  52. end;
  53. Function TlinkerEmbedded.WriteResponseFile: Boolean;
  54. Var
  55. linkres : TLinkRes;
  56. i : longint;
  57. HPath : TCmdStrListItem;
  58. s,s1,s2 : TCmdStr;
  59. prtobj,
  60. cprtobj : string[80];
  61. linklibc : boolean;
  62. found1,
  63. found2 : boolean;
  64. {$ifdef ARM}
  65. LinkStr : string;
  66. {$endif}
  67. begin
  68. WriteResponseFile:=False;
  69. linklibc:=(SharedLibFiles.Find('c')<>nil);
  70. {$if defined(ARM) or defined(i386) or defined(AVR)}
  71. prtobj:='';
  72. {$else}
  73. prtobj:='prt0';
  74. {$endif}
  75. cprtobj:='cprt0';
  76. if linklibc then
  77. prtobj:=cprtobj;
  78. { Open link.res file }
  79. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  80. { Write path to search libraries }
  81. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  82. while assigned(HPath) do
  83. begin
  84. s:=HPath.Str;
  85. if (cs_link_on_target in current_settings.globalswitches) then
  86. s:=ScriptFixFileName(s);
  87. LinkRes.Add('-L'+s);
  88. HPath:=TCmdStrListItem(HPath.Next);
  89. end;
  90. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  91. while assigned(HPath) do
  92. begin
  93. s:=HPath.Str;
  94. if s<>'' then
  95. LinkRes.Add('SEARCH_DIR("'+s+'")');
  96. HPath:=TCmdStrListItem(HPath.Next);
  97. end;
  98. LinkRes.Add('INPUT (');
  99. { add objectfiles, start with prt0 always }
  100. //s:=FindObjectFile('prt0','',false);
  101. if prtobj<>'' then
  102. begin
  103. s:=FindObjectFile(prtobj,'',false);
  104. LinkRes.AddFileName(s);
  105. end;
  106. { try to add crti and crtbegin if linking to C }
  107. if linklibc then
  108. begin
  109. if librarysearchpath.FindFile('crtbegin.o',false,s) then
  110. LinkRes.AddFileName(s);
  111. if librarysearchpath.FindFile('crti.o',false,s) then
  112. LinkRes.AddFileName(s);
  113. end;
  114. while not ObjectFiles.Empty do
  115. begin
  116. s:=ObjectFiles.GetFirst;
  117. if s<>'' then
  118. begin
  119. { vlink doesn't use SEARCH_DIR for object files }
  120. if not(cs_link_on_target in current_settings.globalswitches) then
  121. s:=FindObjectFile(s,'',false);
  122. LinkRes.AddFileName((maybequoted(s)));
  123. end;
  124. end;
  125. { Write staticlibraries }
  126. if not StaticLibFiles.Empty then
  127. begin
  128. { vlink doesn't need, and doesn't support GROUP }
  129. if (cs_link_on_target in current_settings.globalswitches) then
  130. begin
  131. LinkRes.Add(')');
  132. LinkRes.Add('GROUP(');
  133. end;
  134. while not StaticLibFiles.Empty do
  135. begin
  136. S:=StaticLibFiles.GetFirst;
  137. LinkRes.AddFileName((maybequoted(s)));
  138. end;
  139. end;
  140. if (cs_link_on_target in current_settings.globalswitches) then
  141. begin
  142. LinkRes.Add(')');
  143. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  144. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  145. linklibc:=false;
  146. while not SharedLibFiles.Empty do
  147. begin
  148. S:=SharedLibFiles.GetFirst;
  149. if s<>'c' then
  150. begin
  151. i:=Pos(target_info.sharedlibext,S);
  152. if i>0 then
  153. Delete(S,i,255);
  154. LinkRes.Add('-l'+s);
  155. end
  156. else
  157. begin
  158. LinkRes.Add('-l'+s);
  159. linklibc:=true;
  160. end;
  161. end;
  162. { be sure that libc&libgcc is the last lib }
  163. if linklibc then
  164. begin
  165. LinkRes.Add('-lc');
  166. LinkRes.Add('-lgcc');
  167. end;
  168. end
  169. else
  170. begin
  171. while not SharedLibFiles.Empty do
  172. begin
  173. S:=SharedLibFiles.GetFirst;
  174. LinkRes.Add('lib'+s+target_info.staticlibext);
  175. end;
  176. LinkRes.Add(')');
  177. end;
  178. { objects which must be at the end }
  179. if linklibc then
  180. begin
  181. found1:=librarysearchpath.FindFile('crtend.o',false,s1);
  182. found2:=librarysearchpath.FindFile('crtn.o',false,s2);
  183. if found1 or found2 then
  184. begin
  185. LinkRes.Add('INPUT(');
  186. if found1 then
  187. LinkRes.AddFileName(s1);
  188. if found2 then
  189. LinkRes.AddFileName(s2);
  190. LinkRes.Add(')');
  191. end;
  192. end;
  193. {$ifdef ARM}
  194. case current_settings.controllertype of
  195. ct_none:
  196. begin
  197. end;
  198. ct_lpc2114,
  199. ct_lpc2124,
  200. ct_lpc2194,
  201. ct_lpc1768,
  202. ct_at91sam7s256,
  203. ct_at91sam7se256,
  204. ct_at91sam7x256,
  205. ct_at91sam7xc256,
  206. ct_stm32f100x4,
  207. ct_stm32f100x6,
  208. ct_stm32f100x8,
  209. ct_stm32f100xB,
  210. ct_stm32f100xC,
  211. ct_stm32f100xD,
  212. ct_stm32f100xE,
  213. ct_stm32f101x4,
  214. ct_stm32f101x6,
  215. ct_stm32f101x8,
  216. ct_stm32f101xB,
  217. ct_stm32f101xC,
  218. ct_stm32f101xD,
  219. ct_stm32f101xE,
  220. ct_stm32f101xF,
  221. ct_stm32f101xG,
  222. ct_stm32f102x4,
  223. ct_stm32f102x6,
  224. ct_stm32f102x8,
  225. ct_stm32f102xB,
  226. ct_stm32f103x4,
  227. ct_stm32f103x6,
  228. ct_stm32f103x8,
  229. ct_stm32f103xB,
  230. ct_stm32f103xC,
  231. ct_stm32f103xD,
  232. ct_stm32f103xE,
  233. ct_stm32f103xF,
  234. ct_stm32f103xG,
  235. ct_stm32f107x8,
  236. ct_stm32f107xB,
  237. ct_stm32f107xC,
  238. { TI - 64 K Flash, 16 K SRAM Devices }
  239. ct_lm3s1110,
  240. ct_lm3s1133,
  241. ct_lm3s1138,
  242. ct_lm3s1150,
  243. ct_lm3s1162,
  244. ct_lm3s1165,
  245. ct_lm3s1166,
  246. ct_lm3s2110,
  247. ct_lm3s2139,
  248. ct_lm3s6100,
  249. ct_lm3s6110,
  250. { TI 128 K Flash, 32 K SRAM devices - Fury Class }
  251. ct_lm3s1601,
  252. ct_lm3s1608,
  253. ct_lm3s1620,
  254. ct_lm3s1635,
  255. ct_lm3s1636,
  256. ct_lm3s1637,
  257. ct_lm3s1651,
  258. ct_lm3s2601,
  259. ct_lm3s2608,
  260. ct_lm3s2620,
  261. ct_lm3s2637,
  262. ct_lm3s2651,
  263. ct_lm3s6610,
  264. ct_lm3s6611,
  265. ct_lm3s6618,
  266. ct_lm3s6633,
  267. ct_lm3s6637,
  268. ct_lm3s8630,
  269. { TI 256 K Flase, 32 K SRAM devices - Fury Class }
  270. ct_lm3s1911,
  271. ct_lm3s1918,
  272. ct_lm3s1937,
  273. ct_lm3s1958,
  274. ct_lm3s1960,
  275. ct_lm3s1968,
  276. ct_lm3s1969,
  277. ct_lm3s2911,
  278. ct_lm3s2918,
  279. ct_lm3s2919,
  280. ct_lm3s2939,
  281. ct_lm3s2948,
  282. ct_lm3s2950,
  283. ct_lm3s2965,
  284. ct_lm3s6911,
  285. ct_lm3s6918,
  286. ct_lm3s6938,
  287. ct_lm3s6950,
  288. ct_lm3s6952,
  289. ct_lm3s6965,
  290. ct_lm3s8930,
  291. ct_lm3s8933,
  292. ct_lm3s8938,
  293. ct_lm3s8962,
  294. ct_lm3s8970,
  295. ct_lm3s8971,
  296. { TI - Tempest Tempest - 256 K Flash, 64 K SRAM }
  297. ct_lm3s5951,
  298. ct_lm3s5956,
  299. ct_lm3s1b21,
  300. ct_lm3s2b93,
  301. ct_lm3s5b91,
  302. ct_lm3s9b81,
  303. ct_lm3s9b90,
  304. ct_lm3s9b92,
  305. ct_lm3s9b95,
  306. ct_lm3s9b96,
  307. ct_sc32442b,
  308. ct_thumb2bare:
  309. begin
  310. with embedded_controllers[current_settings.controllertype] do
  311. with linkres do
  312. begin
  313. Add('ENTRY(_START)');
  314. Add('MEMORY');
  315. Add('{');
  316. if flashsize<>0 then
  317. begin
  318. LinkStr := ' flash : ORIGIN = 0x' + IntToHex(flashbase,8)
  319. + ', LENGTH = 0x' + IntToHex(flashsize,8);
  320. Add(LinkStr);
  321. end;
  322. LinkStr := ' ram : ORIGIN = 0x' + IntToHex(srambase,8)
  323. + ', LENGTH = 0x' + IntToHex(sramsize,8);
  324. Add(LinkStr);
  325. Add('}');
  326. Add('_stack_top = 0x' + IntToHex(sramsize+srambase,8) + ';');
  327. end;
  328. end
  329. else
  330. if not (cs_link_nolink in current_settings.globalswitches) then
  331. internalerror(200902011);
  332. end;
  333. with linkres do
  334. begin
  335. Add('SECTIONS');
  336. Add('{');
  337. Add(' .text :');
  338. Add(' {');
  339. Add(' KEEP(*(.init, .init.*))');
  340. Add(' *(.text, .text.*)');
  341. Add(' *(.strings)');
  342. Add(' *(.rodata, .rodata.*)');
  343. Add(' *(.comment)');
  344. Add(' _etext = .;');
  345. if embedded_controllers[current_settings.controllertype].flashsize<>0 then
  346. begin
  347. Add(' } >flash');
  348. end
  349. else
  350. begin
  351. Add(' } >ram');
  352. end;
  353. Add(' .data :');
  354. Add(' {');
  355. Add(' _data = .;');
  356. Add(' *(.data, .data.*)');
  357. Add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  358. Add(' _edata = .;');
  359. if embedded_controllers[current_settings.controllertype].flashsize<>0 then
  360. begin
  361. Add(' } >ram AT >flash');
  362. end
  363. else
  364. begin
  365. Add(' } >ram');
  366. end;
  367. Add(' .bss :');
  368. Add(' {');
  369. Add(' _bss_start = .;');
  370. Add(' *(.bss, .bss.*)');
  371. Add(' *(COMMON)');
  372. Add(' } >ram');
  373. Add('. = ALIGN(4);');
  374. Add('_bss_end = . ;');
  375. Add('}');
  376. Add('_end = .;');
  377. end;
  378. {$endif ARM}
  379. {$ifdef i386}
  380. with linkres do
  381. begin
  382. Add('ENTRY(_START)');
  383. Add('SECTIONS');
  384. Add('{');
  385. Add(' . = 0x100000;');
  386. Add(' .text ALIGN (0x1000) :');
  387. Add(' {');
  388. Add(' KEEP(*(.init, .init.*))');
  389. Add(' *(.text, .text.*)');
  390. Add(' *(.strings)');
  391. Add(' *(.rodata, .rodata.*)');
  392. Add(' *(.comment)');
  393. Add(' _etext = .;');
  394. Add(' }');
  395. Add(' .data ALIGN (0x1000) :');
  396. Add(' {');
  397. Add(' _data = .;');
  398. Add(' *(.data, .data.*)');
  399. Add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  400. Add(' _edata = .;');
  401. Add(' }');
  402. Add(' . = ALIGN(4);');
  403. Add(' .bss :');
  404. Add(' {');
  405. Add(' _bss_start = .;');
  406. Add(' *(.bss, .bss.*)');
  407. Add(' *(COMMON)');
  408. Add(' }');
  409. Add('_bss_end = . ;');
  410. Add('}');
  411. Add('_end = .;');
  412. end;
  413. {$endif i386}
  414. {$ifdef AVR}
  415. with linkres do
  416. begin
  417. { linker script from ld 2.19 }
  418. Add('ENTRY(_START)');
  419. Add('OUTPUT_FORMAT("elf32-avr","elf32-avr","elf32-avr")');
  420. Add('OUTPUT_ARCH(avr:2)');
  421. Add('MEMORY');
  422. Add('{');
  423. Add(' text (rx) : ORIGIN = 0, LENGTH = 8K');
  424. Add(' data (rw!x) : ORIGIN = 0x800060, LENGTH = 0xffa0');
  425. Add(' eeprom (rw!x) : ORIGIN = 0x810000, LENGTH = 64K');
  426. Add(' fuse (rw!x) : ORIGIN = 0x820000, LENGTH = 1K');
  427. Add(' lock (rw!x) : ORIGIN = 0x830000, LENGTH = 1K');
  428. Add(' signature (rw!x) : ORIGIN = 0x840000, LENGTH = 1K');
  429. Add('}');
  430. Add('SECTIONS');
  431. Add('{');
  432. Add(' /* Read-only sections, merged into text segment: */');
  433. Add(' .hash : { *(.hash) }');
  434. Add(' .dynsym : { *(.dynsym) }');
  435. Add(' .dynstr : { *(.dynstr) }');
  436. Add(' .gnu.version : { *(.gnu.version) }');
  437. Add(' .gnu.version_d : { *(.gnu.version_d) }');
  438. Add(' .gnu.version_r : { *(.gnu.version_r) }');
  439. Add(' .rel.init : { *(.rel.init) }');
  440. Add(' .rela.init : { *(.rela.init) }');
  441. Add(' .rel.text :');
  442. Add(' {');
  443. Add(' *(.rel.text)');
  444. Add(' *(.rel.text.*)');
  445. Add(' *(.rel.gnu.linkonce.t*)');
  446. Add(' }');
  447. Add(' .rela.text :');
  448. Add(' {');
  449. Add(' *(.rela.text)');
  450. Add(' *(.rela.text.*)');
  451. Add(' *(.rela.gnu.linkonce.t*)');
  452. Add(' }');
  453. Add(' .rel.fini : { *(.rel.fini) }');
  454. Add(' .rela.fini : { *(.rela.fini) }');
  455. Add(' .rel.rodata :');
  456. Add(' {');
  457. Add(' *(.rel.rodata)');
  458. Add(' *(.rel.rodata.*)');
  459. Add(' *(.rel.gnu.linkonce.r*)');
  460. Add(' }');
  461. Add(' .rela.rodata :');
  462. Add(' {');
  463. Add(' *(.rela.rodata)');
  464. Add(' *(.rela.rodata.*)');
  465. Add(' *(.rela.gnu.linkonce.r*)');
  466. Add(' }');
  467. Add(' .rel.data :');
  468. Add(' {');
  469. Add(' *(.rel.data)');
  470. Add(' *(.rel.data.*)');
  471. Add(' *(.rel.gnu.linkonce.d*)');
  472. Add(' }');
  473. Add(' .rela.data :');
  474. Add(' {');
  475. Add(' *(.rela.data)');
  476. Add(' *(.rela.data.*)');
  477. Add(' *(.rela.gnu.linkonce.d*)');
  478. Add(' }');
  479. Add(' .rel.ctors : { *(.rel.ctors) }');
  480. Add(' .rela.ctors : { *(.rela.ctors) }');
  481. Add(' .rel.dtors : { *(.rel.dtors) }');
  482. Add(' .rela.dtors : { *(.rela.dtors) }');
  483. Add(' .rel.got : { *(.rel.got) }');
  484. Add(' .rela.got : { *(.rela.got) }');
  485. Add(' .rel.bss : { *(.rel.bss) }');
  486. Add(' .rela.bss : { *(.rela.bss) }');
  487. Add(' .rel.plt : { *(.rel.plt) }');
  488. Add(' .rela.plt : { *(.rela.plt) }');
  489. Add(' /* Internal text space or external memory. */');
  490. Add(' .text :');
  491. Add(' {');
  492. Add(' *(.vectors)');
  493. Add(' KEEP(*(.vectors))');
  494. Add(' /* For data that needs to reside in the lower 64k of progmem. */');
  495. Add(' *(.progmem.gcc*)');
  496. Add(' *(.progmem*)');
  497. Add(' . = ALIGN(2);');
  498. Add(' __trampolines_start = . ;');
  499. Add(' /* The jump trampolines for the 16-bit limited relocs will reside here. */');
  500. Add(' *(.trampolines)');
  501. Add(' *(.trampolines*)');
  502. Add(' __trampolines_end = . ;');
  503. Add(' /* For future tablejump instruction arrays for 3 byte pc devices.');
  504. Add(' We don''t relax jump/call instructions within these sections. */');
  505. Add(' *(.jumptables)');
  506. Add(' *(.jumptables*)');
  507. Add(' /* For code that needs to reside in the lower 128k progmem. */');
  508. Add(' *(.lowtext)');
  509. Add(' *(.lowtext*)');
  510. Add(' __ctors_start = . ;');
  511. Add(' *(.ctors)');
  512. Add(' __ctors_end = . ;');
  513. Add(' __dtors_start = . ;');
  514. Add(' *(.dtors)');
  515. Add(' __dtors_end = . ;');
  516. Add(' KEEP(SORT(*)(.ctors))');
  517. Add(' KEEP(SORT(*)(.dtors))');
  518. Add(' /* From this point on, we don''t bother about wether the insns are');
  519. Add(' below or above the 16 bits boundary. */');
  520. Add(' *(.init0) /* Start here after reset. */');
  521. Add(' KEEP (*(.init0))');
  522. Add(' *(.init1)');
  523. Add(' KEEP (*(.init1))');
  524. Add(' *(.init2) /* Clear __zero_reg__, set up stack pointer. */');
  525. Add(' KEEP (*(.init2))');
  526. Add(' *(.init3)');
  527. Add(' KEEP (*(.init3))');
  528. Add(' *(.init4) /* Initialize data and BSS. */');
  529. Add(' KEEP (*(.init4))');
  530. Add(' *(.init5)');
  531. Add(' KEEP (*(.init5))');
  532. Add(' *(.init6) /* C++ constructors. */');
  533. Add(' KEEP (*(.init6))');
  534. Add(' *(.init7)');
  535. Add(' KEEP (*(.init7))');
  536. Add(' *(.init8)');
  537. Add(' KEEP (*(.init8))');
  538. Add(' *(.init9) /* Call main(). */');
  539. Add(' KEEP (*(.init9))');
  540. Add(' *(.text)');
  541. Add(' . = ALIGN(2);');
  542. Add(' *(.text.*)');
  543. Add(' . = ALIGN(2);');
  544. Add(' *(.fini9) /* _exit() starts here. */');
  545. Add(' KEEP (*(.fini9))');
  546. Add(' *(.fini8)');
  547. Add(' KEEP (*(.fini8))');
  548. Add(' *(.fini7)');
  549. Add(' KEEP (*(.fini7))');
  550. Add(' *(.fini6) /* C++ destructors. */');
  551. Add(' KEEP (*(.fini6))');
  552. Add(' *(.fini5)');
  553. Add(' KEEP (*(.fini5))');
  554. Add(' *(.fini4)');
  555. Add(' KEEP (*(.fini4))');
  556. Add(' *(.fini3)');
  557. Add(' KEEP (*(.fini3))');
  558. Add(' *(.fini2)');
  559. Add(' KEEP (*(.fini2))');
  560. Add(' *(.fini1)');
  561. Add(' KEEP (*(.fini1))');
  562. Add(' *(.fini0) /* Infinite loop after program termination. */');
  563. Add(' KEEP (*(.fini0))');
  564. Add(' _etext = . ;');
  565. Add(' } > text');
  566. Add(' .data : AT (ADDR (.text) + SIZEOF (.text))');
  567. Add(' {');
  568. Add(' PROVIDE (__data_start = .) ;');
  569. Add(' *(.data)');
  570. Add(' *(.data*)');
  571. Add(' *(.rodata) /* We need to include .rodata here if gcc is used */');
  572. Add(' *(.rodata*) /* with -fdata-sections. */');
  573. Add(' *(.gnu.linkonce.d*)');
  574. Add(' . = ALIGN(2);');
  575. Add(' _edata = . ;');
  576. Add(' PROVIDE (__data_end = .) ;');
  577. Add(' } > data');
  578. Add(' .bss : AT (ADDR (.bss))');
  579. Add(' {');
  580. Add(' PROVIDE (__bss_start = .) ;');
  581. Add(' *(.bss)');
  582. Add(' *(.bss*)');
  583. Add(' *(COMMON)');
  584. Add(' PROVIDE (__bss_end = .) ;');
  585. Add(' } > data');
  586. Add(' __data_load_start = LOADADDR(.data);');
  587. Add(' __data_load_end = __data_load_start + SIZEOF(.data);');
  588. Add(' /* Global data not cleared after reset. */');
  589. Add(' .noinit :');
  590. Add(' {');
  591. Add(' PROVIDE (__noinit_start = .) ;');
  592. Add(' *(.noinit*)');
  593. Add(' PROVIDE (__noinit_end = .) ;');
  594. Add(' _end = . ;');
  595. Add(' PROVIDE (__heap_start = .) ;');
  596. Add(' } > data');
  597. Add(' .eeprom :');
  598. Add(' {');
  599. Add(' *(.eeprom*)');
  600. Add(' __eeprom_end = . ;');
  601. Add(' } > eeprom');
  602. Add(' .fuse :');
  603. Add(' {');
  604. Add(' KEEP(*(.fuse))');
  605. Add(' KEEP(*(.lfuse))');
  606. Add(' KEEP(*(.hfuse))');
  607. Add(' KEEP(*(.efuse))');
  608. Add(' } > fuse');
  609. Add(' .lock :');
  610. Add(' {');
  611. Add(' KEEP(*(.lock*))');
  612. Add(' } > lock');
  613. Add(' .signature :');
  614. Add(' {');
  615. Add(' KEEP(*(.signature*))');
  616. Add(' } > signature');
  617. Add(' /* Stabs debugging sections. */');
  618. Add(' .stab 0 : { *(.stab) }');
  619. Add(' .stabstr 0 : { *(.stabstr) }');
  620. Add(' .stab.excl 0 : { *(.stab.excl) }');
  621. Add(' .stab.exclstr 0 : { *(.stab.exclstr) }');
  622. Add(' .stab.index 0 : { *(.stab.index) }');
  623. Add(' .stab.indexstr 0 : { *(.stab.indexstr) }');
  624. Add(' .comment 0 : { *(.comment) }');
  625. Add(' /* DWARF debug sections.');
  626. Add(' Symbols in the DWARF debugging sections are relative to the beginning');
  627. Add(' of the section so we begin them at 0. */');
  628. Add(' /* DWARF 1 */');
  629. Add(' .debug 0 : { *(.debug) }');
  630. Add(' .line 0 : { *(.line) }');
  631. Add(' /* GNU DWARF 1 extensions */');
  632. Add(' .debug_srcinfo 0 : { *(.debug_srcinfo) }');
  633. Add(' .debug_sfnames 0 : { *(.debug_sfnames) }');
  634. Add(' /* DWARF 1.1 and DWARF 2 */');
  635. Add(' .debug_aranges 0 : { *(.debug_aranges) }');
  636. Add(' .debug_pubnames 0 : { *(.debug_pubnames) }');
  637. Add(' /* DWARF 2 */');
  638. Add(' .debug_info 0 : { *(.debug_info) *(.gnu.linkonce.wi.*) }');
  639. Add(' .debug_abbrev 0 : { *(.debug_abbrev) }');
  640. Add(' .debug_line 0 : { *(.debug_line) }');
  641. Add(' .debug_frame 0 : { *(.debug_frame) }');
  642. Add(' .debug_str 0 : { *(.debug_str) }');
  643. Add(' .debug_loc 0 : { *(.debug_loc) }');
  644. Add(' .debug_macinfo 0 : { *(.debug_macinfo) }');
  645. Add('}');
  646. { last address of ram on an atmega128 }
  647. Add('_stack_top = 0x0fff;');
  648. end;
  649. {$endif AVR}
  650. { Write and Close response }
  651. linkres.writetodisk;
  652. linkres.free;
  653. WriteResponseFile:=True;
  654. end;
  655. function TlinkerEmbedded.MakeExecutable:boolean;
  656. var
  657. binstr,
  658. cmdstr : TCmdStr;
  659. success : boolean;
  660. StaticStr,
  661. GCSectionsStr,
  662. DynLinkStr,
  663. StripStr: string;
  664. begin
  665. { for future use }
  666. StaticStr:='';
  667. StripStr:='';
  668. DynLinkStr:='';
  669. GCSectionsStr:='--gc-sections';
  670. //if not(cs_link_extern in current_settings.globalswitches) then
  671. if not(cs_link_nolink in current_settings.globalswitches) then
  672. Message1(exec_i_linking,current_module.exefilename);
  673. { Write used files and libraries }
  674. WriteResponseFile();
  675. { Call linker }
  676. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  677. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  678. if not(cs_link_on_target in current_settings.globalswitches) then
  679. begin
  680. Replace(cmdstr,'$EXE',(maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename,'.elf')))));
  681. Replace(cmdstr,'$RES',(maybequoted(ScriptFixFileName(outputexedir+Info.ResName))));
  682. Replace(cmdstr,'$STATIC',StaticStr);
  683. Replace(cmdstr,'$STRIP',StripStr);
  684. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  685. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  686. end
  687. else
  688. begin
  689. Replace(cmdstr,'$EXE',maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename,'.elf'))));
  690. Replace(cmdstr,'$RES',maybequoted(ScriptFixFileName(outputexedir+Info.ResName)));
  691. Replace(cmdstr,'$STATIC',StaticStr);
  692. Replace(cmdstr,'$STRIP',StripStr);
  693. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  694. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  695. end;
  696. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  697. { Remove ReponseFile }
  698. if success and not(cs_link_nolink in current_settings.globalswitches) then
  699. DeleteFile(outputexedir+Info.ResName);
  700. { Post process }
  701. if success then
  702. success:=PostProcessExecutable(current_module.exefilename+'.elf',false);
  703. if success and (target_info.system in [system_arm_embedded,system_avr_embedded]) then
  704. begin
  705. success:=DoExec(FindUtil(utilsprefix+'objcopy'),'-O ihex '+
  706. ChangeFileExt(current_module.exefilename,'.elf')+' '+
  707. ChangeFileExt(current_module.exefilename,'.hex'),true,false);
  708. end;
  709. MakeExecutable:=success; { otherwise a recursive call to link method }
  710. end;
  711. function TLinkerEmbedded.postprocessexecutable(const fn : string;isdll:boolean):boolean;
  712. type
  713. TElf32header=packed record
  714. magic0123 : longint;
  715. file_class : byte;
  716. data_encoding : byte;
  717. file_version : byte;
  718. padding : array[$07..$0f] of byte;
  719. e_type : word;
  720. e_machine : word;
  721. e_version : longint;
  722. e_entry : longint; { entrypoint }
  723. e_phoff : longint; { program header offset }
  724. e_shoff : longint; { sections header offset }
  725. e_flags : longint;
  726. e_ehsize : word; { elf header size in bytes }
  727. e_phentsize : word; { size of an entry in the program header array }
  728. e_phnum : word; { 0..e_phnum-1 of entrys }
  729. e_shentsize : word; { size of an entry in sections header array }
  730. e_shnum : word; { 0..e_shnum-1 of entrys }
  731. e_shstrndx : word; { index of string section header }
  732. end;
  733. TElf32sechdr=packed record
  734. sh_name : longint;
  735. sh_type : longint;
  736. sh_flags : longint;
  737. sh_addr : longint;
  738. sh_offset : longint;
  739. sh_size : longint;
  740. sh_link : longint;
  741. sh_info : longint;
  742. sh_addralign : longint;
  743. sh_entsize : longint;
  744. end;
  745. function MayBeSwapHeader(h : telf32header) : telf32header;
  746. begin
  747. result:=h;
  748. if source_info.endian<>target_info.endian then
  749. with h do
  750. begin
  751. result.e_type:=swapendian(e_type);
  752. result.e_machine:=swapendian(e_machine);
  753. result.e_version:=swapendian(e_version);
  754. result.e_entry:=swapendian(e_entry);
  755. result.e_phoff:=swapendian(e_phoff);
  756. result.e_shoff:=swapendian(e_shoff);
  757. result.e_flags:=swapendian(e_flags);
  758. result.e_ehsize:=swapendian(e_ehsize);
  759. result.e_phentsize:=swapendian(e_phentsize);
  760. result.e_phnum:=swapendian(e_phnum);
  761. result.e_shentsize:=swapendian(e_shentsize);
  762. result.e_shnum:=swapendian(e_shnum);
  763. result.e_shstrndx:=swapendian(e_shstrndx);
  764. end;
  765. end;
  766. function MaybeSwapSecHeader(h : telf32sechdr) : telf32sechdr;
  767. begin
  768. result:=h;
  769. if source_info.endian<>target_info.endian then
  770. with h do
  771. begin
  772. result.sh_name:=swapendian(sh_name);
  773. result.sh_type:=swapendian(sh_type);
  774. result.sh_flags:=swapendian(sh_flags);
  775. result.sh_addr:=swapendian(sh_addr);
  776. result.sh_offset:=swapendian(sh_offset);
  777. result.sh_size:=swapendian(sh_size);
  778. result.sh_link:=swapendian(sh_link);
  779. result.sh_info:=swapendian(sh_info);
  780. result.sh_addralign:=swapendian(sh_addralign);
  781. result.sh_entsize:=swapendian(sh_entsize);
  782. end;
  783. end;
  784. var
  785. f : file;
  786. function ReadSectionName(pos : longint) : String;
  787. var
  788. oldpos : longint;
  789. c : char;
  790. begin
  791. oldpos:=filepos(f);
  792. seek(f,pos);
  793. Result:='';
  794. while true do
  795. begin
  796. blockread(f,c,1);
  797. if c=#0 then
  798. break;
  799. Result:=Result+c;
  800. end;
  801. seek(f,oldpos);
  802. end;
  803. var
  804. elfheader : TElf32header;
  805. secheader : TElf32sechdr;
  806. i : longint;
  807. stringoffset : longint;
  808. secname : string;
  809. begin
  810. postprocessexecutable:=false;
  811. { open file }
  812. assign(f,fn);
  813. {$push}{$I-}
  814. reset(f,1);
  815. if ioresult<>0 then
  816. Message1(execinfo_f_cant_open_executable,fn);
  817. { read header }
  818. blockread(f,elfheader,sizeof(tElf32header));
  819. elfheader:=MayBeSwapHeader(elfheader);
  820. seek(f,elfheader.e_shoff);
  821. { read string section header }
  822. seek(f,elfheader.e_shoff+sizeof(TElf32sechdr)*elfheader.e_shstrndx);
  823. blockread(f,secheader,sizeof(secheader));
  824. secheader:=MaybeSwapSecHeader(secheader);
  825. stringoffset:=secheader.sh_offset;
  826. seek(f,elfheader.e_shoff);
  827. status.datasize:=0;
  828. for i:=0 to elfheader.e_shnum-1 do
  829. begin
  830. blockread(f,secheader,sizeof(secheader));
  831. secheader:=MaybeSwapSecHeader(secheader);
  832. secname:=ReadSectionName(stringoffset+secheader.sh_name);
  833. if secname='.text' then
  834. begin
  835. Message1(execinfo_x_codesize,tostr(secheader.sh_size));
  836. status.codesize:=secheader.sh_size;
  837. end
  838. else if secname='.data' then
  839. begin
  840. Message1(execinfo_x_initdatasize,tostr(secheader.sh_size));
  841. inc(status.datasize,secheader.sh_size);
  842. end
  843. else if secname='.bss' then
  844. begin
  845. Message1(execinfo_x_uninitdatasize,tostr(secheader.sh_size));
  846. inc(status.datasize,secheader.sh_size);
  847. end;
  848. end;
  849. close(f);
  850. {$pop}
  851. if ioresult<>0 then
  852. ;
  853. postprocessexecutable:=true;
  854. end;
  855. {*****************************************************************************
  856. Initialize
  857. *****************************************************************************}
  858. initialization
  859. {$ifdef arm}
  860. RegisterExternalLinker(system_arm_embedded_info,TlinkerEmbedded);
  861. RegisterTarget(system_arm_embedded_info);
  862. {$endif arm}
  863. {$ifdef avr}
  864. RegisterExternalLinker(system_avr_embedded_info,TlinkerEmbedded);
  865. RegisterTarget(system_avr_embedded_info);
  866. {$endif avr}
  867. {$ifdef i386}
  868. RegisterExternalLinker(system_i386_embedded_info,TlinkerEmbedded);
  869. RegisterTarget(system_i386_embedded_info);
  870. {$endif i386}
  871. end.