t_embed.pas 28 KB

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