t_embed.pas 28 KB

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