t_embed.pas 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  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);
  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('+(maybequoted(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_stm32f103rb,
  207. ct_stm32f103re,
  208. { TI - 64 K Flash, 16 K SRAM Devices }
  209. ct_lm3s1110,
  210. ct_lm3s1133,
  211. ct_lm3s1138,
  212. ct_lm3s1150,
  213. ct_lm3s1162,
  214. ct_lm3s1165,
  215. ct_lm3s1166,
  216. ct_lm3s2110,
  217. ct_lm3s2139,
  218. ct_lm3s6100,
  219. ct_lm3s6110,
  220. { TI 128 K Flash, 32 K SRAM devices - Fury Class }
  221. ct_lm3s1601,
  222. ct_lm3s1608,
  223. ct_lm3s1620,
  224. ct_lm3s1635,
  225. ct_lm3s1636,
  226. ct_lm3s1637,
  227. ct_lm3s1651,
  228. ct_lm3s2601,
  229. ct_lm3s2608,
  230. ct_lm3s2620,
  231. ct_lm3s2637,
  232. ct_lm3s2651,
  233. ct_lm3s6610,
  234. ct_lm3s6611,
  235. ct_lm3s6618,
  236. ct_lm3s6633,
  237. ct_lm3s6637,
  238. ct_lm3s8630,
  239. { TI 256 K Flase, 32 K SRAM devices - Fury Class }
  240. ct_lm3s1911,
  241. ct_lm3s1918,
  242. ct_lm3s1937,
  243. ct_lm3s1958,
  244. ct_lm3s1960,
  245. ct_lm3s1968,
  246. ct_lm3s1969,
  247. ct_lm3s2911,
  248. ct_lm3s2918,
  249. ct_lm3s2919,
  250. ct_lm3s2939,
  251. ct_lm3s2948,
  252. ct_lm3s2950,
  253. ct_lm3s2965,
  254. ct_lm3s6911,
  255. ct_lm3s6918,
  256. ct_lm3s6938,
  257. ct_lm3s6950,
  258. ct_lm3s6952,
  259. ct_lm3s6965,
  260. ct_lm3s8930,
  261. ct_lm3s8933,
  262. ct_lm3s8938,
  263. ct_lm3s8962,
  264. ct_lm3s8970,
  265. ct_lm3s8971,
  266. { TI - Tempest Tempest - 256 K Flash, 64 K SRAM }
  267. ct_lm3s5951,
  268. ct_lm3s5956,
  269. ct_lm3s1b21,
  270. ct_lm3s2b93,
  271. ct_lm3s5b91,
  272. ct_lm3s9b81,
  273. ct_lm3s9b90,
  274. ct_lm3s9b92,
  275. ct_lm3s9b95,
  276. ct_lm3s9b96,
  277. ct_thumb2bare:
  278. begin
  279. with embedded_controllers[current_settings.controllertype] do
  280. with linkres do
  281. begin
  282. Add('ENTRY(_START)');
  283. Add('MEMORY');
  284. Add('{');
  285. if flashsize<>0 then
  286. begin
  287. LinkStr := ' flash : ORIGIN = 0x' + IntToHex(flashbase,8)
  288. + ', LENGTH = 0x' + IntToHex(flashsize,8);
  289. Add(LinkStr);
  290. end;
  291. LinkStr := ' ram : ORIGIN = 0x' + IntToHex(srambase,8)
  292. + ', LENGTH = 0x' + IntToHex(sramsize,8);
  293. Add(LinkStr);
  294. Add('}');
  295. Add('_stack_top = 0x' + IntToHex(sramsize+srambase,8) + ';');
  296. end;
  297. end
  298. else
  299. if not (cs_link_nolink in current_settings.globalswitches) then
  300. internalerror(200902011);
  301. end;
  302. with linkres do
  303. begin
  304. Add('SECTIONS');
  305. Add('{');
  306. Add(' .text :');
  307. Add(' {');
  308. Add(' KEEP(*(.init, .init.*))');
  309. Add(' *(.text, .text.*)');
  310. Add(' *(.strings)');
  311. Add(' *(.rodata, .rodata.*)');
  312. Add(' *(.comment)');
  313. Add(' _etext = .;');
  314. if embedded_controllers[current_settings.controllertype].flashsize<>0 then
  315. begin
  316. Add(' } >flash');
  317. end
  318. else
  319. begin
  320. Add(' } >ram');
  321. end;
  322. Add(' .data :');
  323. Add(' {');
  324. Add(' _data = .;');
  325. Add(' *(.data, .data.*)');
  326. Add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  327. Add(' _edata = .;');
  328. if embedded_controllers[current_settings.controllertype].flashsize<>0 then
  329. begin
  330. Add(' } >ram AT >flash');
  331. end
  332. else
  333. begin
  334. Add(' } >ram');
  335. end;
  336. Add(' .bss :');
  337. Add(' {');
  338. Add(' _bss_start = .;');
  339. Add(' *(.bss, .bss.*)');
  340. Add(' *(COMMON)');
  341. Add(' } >ram');
  342. Add('. = ALIGN(4);');
  343. Add('_bss_end = . ;');
  344. Add('}');
  345. Add('_end = .;');
  346. end;
  347. {$endif ARM}
  348. {$ifdef i386}
  349. with linkres do
  350. begin
  351. Add('ENTRY(_START)');
  352. Add('SECTIONS');
  353. Add('{');
  354. Add(' . = 0x100000;');
  355. Add(' .text ALIGN (0x1000) :');
  356. Add(' {');
  357. Add(' KEEP(*(.init, .init.*))');
  358. Add(' *(.text, .text.*)');
  359. Add(' *(.strings)');
  360. Add(' *(.rodata, .rodata.*)');
  361. Add(' *(.comment)');
  362. Add(' _etext = .;');
  363. Add(' }');
  364. Add(' .data ALIGN (0x1000) :');
  365. Add(' {');
  366. Add(' _data = .;');
  367. Add(' *(.data, .data.*)');
  368. Add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  369. Add(' _edata = .;');
  370. Add(' }');
  371. Add(' . = ALIGN(4);');
  372. Add(' .bss :');
  373. Add(' {');
  374. Add(' _bss_start = .;');
  375. Add(' *(.bss, .bss.*)');
  376. Add(' *(COMMON)');
  377. Add(' }');
  378. Add('_bss_end = . ;');
  379. Add('}');
  380. Add('_end = .;');
  381. end;
  382. {$endif i386}
  383. {$ifdef AVR}
  384. with linkres do
  385. begin
  386. { linker script from ld 2.19 }
  387. Add('ENTRY(_START)');
  388. Add('OUTPUT_FORMAT("elf32-avr","elf32-avr","elf32-avr")');
  389. Add('OUTPUT_ARCH(avr:2)');
  390. Add('MEMORY');
  391. Add('{');
  392. Add(' text (rx) : ORIGIN = 0, LENGTH = 8K');
  393. Add(' data (rw!x) : ORIGIN = 0x800060, LENGTH = 0xffa0');
  394. Add(' eeprom (rw!x) : ORIGIN = 0x810000, LENGTH = 64K');
  395. Add(' fuse (rw!x) : ORIGIN = 0x820000, LENGTH = 1K');
  396. Add(' lock (rw!x) : ORIGIN = 0x830000, LENGTH = 1K');
  397. Add(' signature (rw!x) : ORIGIN = 0x840000, LENGTH = 1K');
  398. Add('}');
  399. Add('SECTIONS');
  400. Add('{');
  401. Add(' /* Read-only sections, merged into text segment: */');
  402. Add(' .hash : { *(.hash) }');
  403. Add(' .dynsym : { *(.dynsym) }');
  404. Add(' .dynstr : { *(.dynstr) }');
  405. Add(' .gnu.version : { *(.gnu.version) }');
  406. Add(' .gnu.version_d : { *(.gnu.version_d) }');
  407. Add(' .gnu.version_r : { *(.gnu.version_r) }');
  408. Add(' .rel.init : { *(.rel.init) }');
  409. Add(' .rela.init : { *(.rela.init) }');
  410. Add(' .rel.text :');
  411. Add(' {');
  412. Add(' *(.rel.text)');
  413. Add(' *(.rel.text.*)');
  414. Add(' *(.rel.gnu.linkonce.t*)');
  415. Add(' }');
  416. Add(' .rela.text :');
  417. Add(' {');
  418. Add(' *(.rela.text)');
  419. Add(' *(.rela.text.*)');
  420. Add(' *(.rela.gnu.linkonce.t*)');
  421. Add(' }');
  422. Add(' .rel.fini : { *(.rel.fini) }');
  423. Add(' .rela.fini : { *(.rela.fini) }');
  424. Add(' .rel.rodata :');
  425. Add(' {');
  426. Add(' *(.rel.rodata)');
  427. Add(' *(.rel.rodata.*)');
  428. Add(' *(.rel.gnu.linkonce.r*)');
  429. Add(' }');
  430. Add(' .rela.rodata :');
  431. Add(' {');
  432. Add(' *(.rela.rodata)');
  433. Add(' *(.rela.rodata.*)');
  434. Add(' *(.rela.gnu.linkonce.r*)');
  435. Add(' }');
  436. Add(' .rel.data :');
  437. Add(' {');
  438. Add(' *(.rel.data)');
  439. Add(' *(.rel.data.*)');
  440. Add(' *(.rel.gnu.linkonce.d*)');
  441. Add(' }');
  442. Add(' .rela.data :');
  443. Add(' {');
  444. Add(' *(.rela.data)');
  445. Add(' *(.rela.data.*)');
  446. Add(' *(.rela.gnu.linkonce.d*)');
  447. Add(' }');
  448. Add(' .rel.ctors : { *(.rel.ctors) }');
  449. Add(' .rela.ctors : { *(.rela.ctors) }');
  450. Add(' .rel.dtors : { *(.rel.dtors) }');
  451. Add(' .rela.dtors : { *(.rela.dtors) }');
  452. Add(' .rel.got : { *(.rel.got) }');
  453. Add(' .rela.got : { *(.rela.got) }');
  454. Add(' .rel.bss : { *(.rel.bss) }');
  455. Add(' .rela.bss : { *(.rela.bss) }');
  456. Add(' .rel.plt : { *(.rel.plt) }');
  457. Add(' .rela.plt : { *(.rela.plt) }');
  458. Add(' /* Internal text space or external memory. */');
  459. Add(' .text :');
  460. Add(' {');
  461. Add(' *(.vectors)');
  462. Add(' KEEP(*(.vectors))');
  463. Add(' /* For data that needs to reside in the lower 64k of progmem. */');
  464. Add(' *(.progmem.gcc*)');
  465. Add(' *(.progmem*)');
  466. Add(' . = ALIGN(2);');
  467. Add(' __trampolines_start = . ;');
  468. Add(' /* The jump trampolines for the 16-bit limited relocs will reside here. */');
  469. Add(' *(.trampolines)');
  470. Add(' *(.trampolines*)');
  471. Add(' __trampolines_end = . ;');
  472. Add(' /* For future tablejump instruction arrays for 3 byte pc devices.');
  473. Add(' We don''t relax jump/call instructions within these sections. */');
  474. Add(' *(.jumptables)');
  475. Add(' *(.jumptables*)');
  476. Add(' /* For code that needs to reside in the lower 128k progmem. */');
  477. Add(' *(.lowtext)');
  478. Add(' *(.lowtext*)');
  479. Add(' __ctors_start = . ;');
  480. Add(' *(.ctors)');
  481. Add(' __ctors_end = . ;');
  482. Add(' __dtors_start = . ;');
  483. Add(' *(.dtors)');
  484. Add(' __dtors_end = . ;');
  485. Add(' KEEP(SORT(*)(.ctors))');
  486. Add(' KEEP(SORT(*)(.dtors))');
  487. Add(' /* From this point on, we don''t bother about wether the insns are');
  488. Add(' below or above the 16 bits boundary. */');
  489. Add(' *(.init0) /* Start here after reset. */');
  490. Add(' KEEP (*(.init0))');
  491. Add(' *(.init1)');
  492. Add(' KEEP (*(.init1))');
  493. Add(' *(.init2) /* Clear __zero_reg__, set up stack pointer. */');
  494. Add(' KEEP (*(.init2))');
  495. Add(' *(.init3)');
  496. Add(' KEEP (*(.init3))');
  497. Add(' *(.init4) /* Initialize data and BSS. */');
  498. Add(' KEEP (*(.init4))');
  499. Add(' *(.init5)');
  500. Add(' KEEP (*(.init5))');
  501. Add(' *(.init6) /* C++ constructors. */');
  502. Add(' KEEP (*(.init6))');
  503. Add(' *(.init7)');
  504. Add(' KEEP (*(.init7))');
  505. Add(' *(.init8)');
  506. Add(' KEEP (*(.init8))');
  507. Add(' *(.init9) /* Call main(). */');
  508. Add(' KEEP (*(.init9))');
  509. Add(' *(.text)');
  510. Add(' . = ALIGN(2);');
  511. Add(' *(.text.*)');
  512. Add(' . = ALIGN(2);');
  513. Add(' *(.fini9) /* _exit() starts here. */');
  514. Add(' KEEP (*(.fini9))');
  515. Add(' *(.fini8)');
  516. Add(' KEEP (*(.fini8))');
  517. Add(' *(.fini7)');
  518. Add(' KEEP (*(.fini7))');
  519. Add(' *(.fini6) /* C++ destructors. */');
  520. Add(' KEEP (*(.fini6))');
  521. Add(' *(.fini5)');
  522. Add(' KEEP (*(.fini5))');
  523. Add(' *(.fini4)');
  524. Add(' KEEP (*(.fini4))');
  525. Add(' *(.fini3)');
  526. Add(' KEEP (*(.fini3))');
  527. Add(' *(.fini2)');
  528. Add(' KEEP (*(.fini2))');
  529. Add(' *(.fini1)');
  530. Add(' KEEP (*(.fini1))');
  531. Add(' *(.fini0) /* Infinite loop after program termination. */');
  532. Add(' KEEP (*(.fini0))');
  533. Add(' _etext = . ;');
  534. Add(' } > text');
  535. Add(' .data : AT (ADDR (.text) + SIZEOF (.text))');
  536. Add(' {');
  537. Add(' PROVIDE (__data_start = .) ;');
  538. Add(' *(.data)');
  539. Add(' *(.data*)');
  540. Add(' *(.rodata) /* We need to include .rodata here if gcc is used */');
  541. Add(' *(.rodata*) /* with -fdata-sections. */');
  542. Add(' *(.gnu.linkonce.d*)');
  543. Add(' . = ALIGN(2);');
  544. Add(' _edata = . ;');
  545. Add(' PROVIDE (__data_end = .) ;');
  546. Add(' } > data');
  547. Add(' .bss : AT (ADDR (.bss))');
  548. Add(' {');
  549. Add(' PROVIDE (__bss_start = .) ;');
  550. Add(' *(.bss)');
  551. Add(' *(.bss*)');
  552. Add(' *(COMMON)');
  553. Add(' PROVIDE (__bss_end = .) ;');
  554. Add(' } > data');
  555. Add(' __data_load_start = LOADADDR(.data);');
  556. Add(' __data_load_end = __data_load_start + SIZEOF(.data);');
  557. Add(' /* Global data not cleared after reset. */');
  558. Add(' .noinit :');
  559. Add(' {');
  560. Add(' PROVIDE (__noinit_start = .) ;');
  561. Add(' *(.noinit*)');
  562. Add(' PROVIDE (__noinit_end = .) ;');
  563. Add(' _end = . ;');
  564. Add(' PROVIDE (__heap_start = .) ;');
  565. Add(' } > data');
  566. Add(' .eeprom :');
  567. Add(' {');
  568. Add(' *(.eeprom*)');
  569. Add(' __eeprom_end = . ;');
  570. Add(' } > eeprom');
  571. Add(' .fuse :');
  572. Add(' {');
  573. Add(' KEEP(*(.fuse))');
  574. Add(' KEEP(*(.lfuse))');
  575. Add(' KEEP(*(.hfuse))');
  576. Add(' KEEP(*(.efuse))');
  577. Add(' } > fuse');
  578. Add(' .lock :');
  579. Add(' {');
  580. Add(' KEEP(*(.lock*))');
  581. Add(' } > lock');
  582. Add(' .signature :');
  583. Add(' {');
  584. Add(' KEEP(*(.signature*))');
  585. Add(' } > signature');
  586. Add(' /* Stabs debugging sections. */');
  587. Add(' .stab 0 : { *(.stab) }');
  588. Add(' .stabstr 0 : { *(.stabstr) }');
  589. Add(' .stab.excl 0 : { *(.stab.excl) }');
  590. Add(' .stab.exclstr 0 : { *(.stab.exclstr) }');
  591. Add(' .stab.index 0 : { *(.stab.index) }');
  592. Add(' .stab.indexstr 0 : { *(.stab.indexstr) }');
  593. Add(' .comment 0 : { *(.comment) }');
  594. Add(' /* DWARF debug sections.');
  595. Add(' Symbols in the DWARF debugging sections are relative to the beginning');
  596. Add(' of the section so we begin them at 0. */');
  597. Add(' /* DWARF 1 */');
  598. Add(' .debug 0 : { *(.debug) }');
  599. Add(' .line 0 : { *(.line) }');
  600. Add(' /* GNU DWARF 1 extensions */');
  601. Add(' .debug_srcinfo 0 : { *(.debug_srcinfo) }');
  602. Add(' .debug_sfnames 0 : { *(.debug_sfnames) }');
  603. Add(' /* DWARF 1.1 and DWARF 2 */');
  604. Add(' .debug_aranges 0 : { *(.debug_aranges) }');
  605. Add(' .debug_pubnames 0 : { *(.debug_pubnames) }');
  606. Add(' /* DWARF 2 */');
  607. Add(' .debug_info 0 : { *(.debug_info) *(.gnu.linkonce.wi.*) }');
  608. Add(' .debug_abbrev 0 : { *(.debug_abbrev) }');
  609. Add(' .debug_line 0 : { *(.debug_line) }');
  610. Add(' .debug_frame 0 : { *(.debug_frame) }');
  611. Add(' .debug_str 0 : { *(.debug_str) }');
  612. Add(' .debug_loc 0 : { *(.debug_loc) }');
  613. Add(' .debug_macinfo 0 : { *(.debug_macinfo) }');
  614. Add('}');
  615. { last address of ram on an atmega128 }
  616. Add('_stack_top = 0x0fff;');
  617. end;
  618. {$endif AVR}
  619. { Write and Close response }
  620. linkres.writetodisk;
  621. linkres.free;
  622. WriteResponseFile:=True;
  623. end;
  624. function TlinkerEmbedded.MakeExecutable:boolean;
  625. var
  626. binstr,
  627. cmdstr : TCmdStr;
  628. success : boolean;
  629. StaticStr,
  630. GCSectionsStr,
  631. DynLinkStr,
  632. StripStr: string;
  633. begin
  634. { for future use }
  635. StaticStr:='';
  636. StripStr:='';
  637. DynLinkStr:='';
  638. GCSectionsStr:='--gc-sections';
  639. //if not(cs_link_extern in current_settings.globalswitches) then
  640. if not(cs_link_nolink in current_settings.globalswitches) then
  641. Message1(exec_i_linking,current_module.exefilename^);
  642. { Write used files and libraries }
  643. WriteResponseFile();
  644. { Call linker }
  645. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  646. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  647. if not(cs_link_on_target in current_settings.globalswitches) then
  648. begin
  649. Replace(cmdstr,'$EXE',(maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename^,'.elf')))));
  650. Replace(cmdstr,'$RES',(maybequoted(ScriptFixFileName(outputexedir+Info.ResName))));
  651. Replace(cmdstr,'$STATIC',StaticStr);
  652. Replace(cmdstr,'$STRIP',StripStr);
  653. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  654. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  655. end
  656. else
  657. begin
  658. Replace(cmdstr,'$EXE',maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename^,'.elf'))));
  659. Replace(cmdstr,'$RES',maybequoted(ScriptFixFileName(outputexedir+Info.ResName)));
  660. Replace(cmdstr,'$STATIC',StaticStr);
  661. Replace(cmdstr,'$STRIP',StripStr);
  662. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  663. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  664. end;
  665. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  666. { Remove ReponseFile }
  667. if success and not(cs_link_nolink in current_settings.globalswitches) then
  668. DeleteFile(outputexedir+Info.ResName);
  669. { Post process }
  670. if success then
  671. success:=PostProcessExecutable(current_module.exefilename^+'.elf',false);
  672. if success and (target_info.system in [system_arm_embedded,system_avr_embedded]) then
  673. begin
  674. success:=DoExec(FindUtil(utilsprefix+'objcopy'),'-O ihex '+
  675. ChangeFileExt(current_module.exefilename^,'.elf')+' '+
  676. ChangeFileExt(current_module.exefilename^,'.hex'),true,false);
  677. end;
  678. MakeExecutable:=success; { otherwise a recursive call to link method }
  679. end;
  680. function TLinkerEmbedded.postprocessexecutable(const fn : string;isdll:boolean):boolean;
  681. type
  682. TElf32header=packed record
  683. magic0123 : longint;
  684. file_class : byte;
  685. data_encoding : byte;
  686. file_version : byte;
  687. padding : array[$07..$0f] of byte;
  688. e_type : word;
  689. e_machine : word;
  690. e_version : longint;
  691. e_entry : longint; { entrypoint }
  692. e_phoff : longint; { program header offset }
  693. e_shoff : longint; { sections header offset }
  694. e_flags : longint;
  695. e_ehsize : word; { elf header size in bytes }
  696. e_phentsize : word; { size of an entry in the program header array }
  697. e_phnum : word; { 0..e_phnum-1 of entrys }
  698. e_shentsize : word; { size of an entry in sections header array }
  699. e_shnum : word; { 0..e_shnum-1 of entrys }
  700. e_shstrndx : word; { index of string section header }
  701. end;
  702. TElf32sechdr=packed record
  703. sh_name : longint;
  704. sh_type : longint;
  705. sh_flags : longint;
  706. sh_addr : longint;
  707. sh_offset : longint;
  708. sh_size : longint;
  709. sh_link : longint;
  710. sh_info : longint;
  711. sh_addralign : longint;
  712. sh_entsize : longint;
  713. end;
  714. var
  715. f : file;
  716. function ReadSectionName(pos : longint) : String;
  717. var
  718. oldpos : longint;
  719. c : char;
  720. begin
  721. oldpos:=filepos(f);
  722. seek(f,pos);
  723. Result:='';
  724. while true do
  725. begin
  726. blockread(f,c,1);
  727. if c=#0 then
  728. break;
  729. Result:=Result+c;
  730. end;
  731. seek(f,oldpos);
  732. end;
  733. var
  734. elfheader : TElf32header;
  735. secheader : TElf32sechdr;
  736. i : longint;
  737. stringoffset : longint;
  738. secname : string;
  739. begin
  740. postprocessexecutable:=false;
  741. { open file }
  742. assign(f,fn);
  743. {$push}{$I-}
  744. reset(f,1);
  745. if ioresult<>0 then
  746. Message1(execinfo_f_cant_open_executable,fn);
  747. { read header }
  748. blockread(f,elfheader,sizeof(tElf32header));
  749. seek(f,elfheader.e_shoff);
  750. { read string section header }
  751. seek(f,elfheader.e_shoff+sizeof(TElf32sechdr)*elfheader.e_shstrndx);
  752. blockread(f,secheader,sizeof(secheader));
  753. stringoffset:=secheader.sh_offset;
  754. seek(f,elfheader.e_shoff);
  755. status.datasize:=0;
  756. for i:=0 to elfheader.e_shnum-1 do
  757. begin
  758. blockread(f,secheader,sizeof(secheader));
  759. secname:=ReadSectionName(stringoffset+secheader.sh_name);
  760. if secname='.text' then
  761. begin
  762. Message1(execinfo_x_codesize,tostr(secheader.sh_size));
  763. status.codesize:=secheader.sh_size;
  764. end
  765. else if secname='.data' then
  766. begin
  767. Message1(execinfo_x_initdatasize,tostr(secheader.sh_size));
  768. inc(status.datasize,secheader.sh_size);
  769. end
  770. else if secname='.bss' then
  771. begin
  772. Message1(execinfo_x_uninitdatasize,tostr(secheader.sh_size));
  773. inc(status.datasize,secheader.sh_size);
  774. end;
  775. end;
  776. close(f);
  777. {$pop}
  778. if ioresult<>0 then
  779. ;
  780. postprocessexecutable:=true;
  781. end;
  782. {*****************************************************************************
  783. Initialize
  784. *****************************************************************************}
  785. initialization
  786. {$ifdef arm}
  787. RegisterExternalLinker(system_arm_embedded_info,TlinkerEmbedded);
  788. RegisterTarget(system_arm_embedded_info);
  789. {$endif arm}
  790. {$ifdef avr}
  791. RegisterExternalLinker(system_avr_embedded_info,TlinkerEmbedded);
  792. RegisterTarget(system_avr_embedded_info);
  793. {$endif avr}
  794. {$ifdef i386}
  795. RegisterExternalLinker(system_i386_embedded_info,TlinkerEmbedded);
  796. RegisterTarget(system_i386_embedded_info);
  797. {$endif i386}
  798. end.