t_embed.pas 31 KB

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