t_embed.pas 28 KB

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