t_embed.pas 25 KB

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