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