t_embed.pas 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341
  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. const
  48. {$ifdef mips}
  49. {$ifdef mipsel}
  50. platform_select='-EL';
  51. {$else}
  52. platform_select='-EB';
  53. {$endif}
  54. {$else}
  55. platform_select='';
  56. {$endif}
  57. begin
  58. with Info do
  59. begin
  60. ExeCmd[1]:='ld -g '+platform_select+' $OPT $DYNLINK $STATIC $GCSECTIONS $STRIP -L. -o $EXE -T $RES';
  61. end;
  62. end;
  63. Function TlinkerEmbedded.WriteResponseFile: Boolean;
  64. Var
  65. linkres : TLinkRes;
  66. i : longint;
  67. HPath : TCmdStrListItem;
  68. s,s1,s2 : TCmdStr;
  69. prtobj,
  70. cprtobj : string[80];
  71. linklibc : boolean;
  72. found1,
  73. found2 : boolean;
  74. {$if defined(ARM) or defined(MIPSEL)}
  75. LinkStr : string;
  76. {$endif}
  77. begin
  78. WriteResponseFile:=False;
  79. linklibc:=(SharedLibFiles.Find('c')<>nil);
  80. {$if defined(ARM) or defined(i386) or defined(AVR) or defined(MIPSEL)}
  81. prtobj:='';
  82. {$else}
  83. prtobj:='prt0';
  84. {$endif}
  85. cprtobj:='cprt0';
  86. if linklibc then
  87. prtobj:=cprtobj;
  88. { Open link.res file }
  89. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  90. { Write path to search libraries }
  91. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  92. while assigned(HPath) do
  93. begin
  94. s:=HPath.Str;
  95. if (cs_link_on_target in current_settings.globalswitches) then
  96. s:=ScriptFixFileName(s);
  97. LinkRes.Add('-L'+s);
  98. HPath:=TCmdStrListItem(HPath.Next);
  99. end;
  100. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  101. while assigned(HPath) do
  102. begin
  103. s:=HPath.Str;
  104. if s<>'' then
  105. LinkRes.Add('SEARCH_DIR("'+s+'")');
  106. HPath:=TCmdStrListItem(HPath.Next);
  107. end;
  108. LinkRes.Add('INPUT (');
  109. { add objectfiles, start with prt0 always }
  110. //s:=FindObjectFile('prt0','',false);
  111. if prtobj<>'' then
  112. begin
  113. s:=FindObjectFile(prtobj,'',false);
  114. LinkRes.AddFileName(s);
  115. end;
  116. { try to add crti and crtbegin if linking to C }
  117. if linklibc then
  118. begin
  119. if librarysearchpath.FindFile('crtbegin.o',false,s) then
  120. LinkRes.AddFileName(s);
  121. if librarysearchpath.FindFile('crti.o',false,s) then
  122. LinkRes.AddFileName(s);
  123. end;
  124. while not ObjectFiles.Empty do
  125. begin
  126. s:=ObjectFiles.GetFirst;
  127. if s<>'' then
  128. begin
  129. { vlink doesn't use SEARCH_DIR for object files }
  130. if not(cs_link_on_target in current_settings.globalswitches) then
  131. s:=FindObjectFile(s,'',false);
  132. LinkRes.AddFileName((maybequoted(s)));
  133. end;
  134. end;
  135. { Write staticlibraries }
  136. if not StaticLibFiles.Empty then
  137. begin
  138. { vlink doesn't need, and doesn't support GROUP }
  139. if (cs_link_on_target in current_settings.globalswitches) then
  140. begin
  141. LinkRes.Add(')');
  142. LinkRes.Add('GROUP(');
  143. end;
  144. while not StaticLibFiles.Empty do
  145. begin
  146. S:=StaticLibFiles.GetFirst;
  147. LinkRes.AddFileName((maybequoted(s)));
  148. end;
  149. end;
  150. if (cs_link_on_target in current_settings.globalswitches) then
  151. begin
  152. LinkRes.Add(')');
  153. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  154. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  155. linklibc:=false;
  156. while not SharedLibFiles.Empty do
  157. begin
  158. S:=SharedLibFiles.GetFirst;
  159. if s<>'c' then
  160. begin
  161. i:=Pos(target_info.sharedlibext,S);
  162. if i>0 then
  163. Delete(S,i,255);
  164. LinkRes.Add('-l'+s);
  165. end
  166. else
  167. begin
  168. LinkRes.Add('-l'+s);
  169. linklibc:=true;
  170. end;
  171. end;
  172. { be sure that libc&libgcc is the last lib }
  173. if linklibc then
  174. begin
  175. LinkRes.Add('-lc');
  176. LinkRes.Add('-lgcc');
  177. end;
  178. end
  179. else
  180. begin
  181. while not SharedLibFiles.Empty do
  182. begin
  183. S:=SharedLibFiles.GetFirst;
  184. LinkRes.Add('lib'+s+target_info.staticlibext);
  185. end;
  186. LinkRes.Add(')');
  187. end;
  188. { objects which must be at the end }
  189. if linklibc then
  190. begin
  191. found1:=librarysearchpath.FindFile('crtend.o',false,s1);
  192. found2:=librarysearchpath.FindFile('crtn.o',false,s2);
  193. if found1 or found2 then
  194. begin
  195. LinkRes.Add('INPUT(');
  196. if found1 then
  197. LinkRes.AddFileName(s1);
  198. if found2 then
  199. LinkRes.AddFileName(s2);
  200. LinkRes.Add(')');
  201. end;
  202. end;
  203. {$ifdef ARM}
  204. case current_settings.controllertype of
  205. ct_none:
  206. begin
  207. end;
  208. ct_lpc810m021fn8,
  209. ct_lpc811m001fdh16,
  210. ct_lpc812m101fdh16,
  211. ct_lpc812m101fd20,
  212. ct_lpc812m101fdh20,
  213. ct_lpc1110fd20,
  214. ct_lpc1111fdh20_002,
  215. ct_lpc1111fhn33_101,
  216. ct_lpc1111fhn33_102,
  217. ct_lpc1111fhn33_103,
  218. ct_lpc1111fhn33_201,
  219. ct_lpc1111fhn33_202,
  220. ct_lpc1111fhn33_203,
  221. ct_lpc1112fd20_102,
  222. ct_lpc1112fdh20_102,
  223. ct_lpc1112fdh28_102,
  224. ct_lpc1112fhn33_101,
  225. ct_lpc1112fhn33_102,
  226. ct_lpc1112fhn33_103,
  227. ct_lpc1112fhn33_201,
  228. ct_lpc1112fhn24_202,
  229. ct_lpc1112fhn33_202,
  230. ct_lpc1112fhn33_203,
  231. ct_lpc1112fhi33_202,
  232. ct_lpc1112fhi33_203,
  233. ct_lpc1113fhn33_201,
  234. ct_lpc1113fhn33_202,
  235. ct_lpc1113fhn33_203,
  236. ct_lpc1113fhn33_301,
  237. ct_lpc1113fhn33_302,
  238. ct_lpc1113fhn33_303,
  239. ct_lpc1113bfd48_301,
  240. ct_lpc1113bfd48_302,
  241. ct_lpc1113bfd48_303,
  242. ct_lpc1114fdh28_102,
  243. ct_lpc1114fn28_102,
  244. ct_lpc1114fhn33_201,
  245. ct_lpc1114fhn33_202,
  246. ct_lpc1114fhn33_203,
  247. ct_lpc1114fhn33_301,
  248. ct_lpc1114fhn33_302,
  249. ct_lpc1114fhn33_303,
  250. ct_lpc1114fhn33_333,
  251. ct_lpc1114fhi33_302,
  252. ct_lpc1114fhi33_303,
  253. ct_lpc1114bfd48_301,
  254. ct_lpc1114bfd48_302,
  255. ct_lpc1114bfd48_303,
  256. ct_lpc1114bfd48_323,
  257. ct_lpc1114bfd48_333,
  258. ct_lpc1115bfd48_303,
  259. ct_lpc11c12fd48_301,
  260. ct_lpc11c14fd48_301,
  261. ct_lpc11c22fd48_301,
  262. ct_lpc11c24fd48_301,
  263. ct_lpc11d24fd48_301,
  264. ct_lpc1224fbd48_101,
  265. ct_lpc1224fbd48_121,
  266. ct_lpc1224fbd64_101,
  267. ct_lpc1224fbd64_121,
  268. ct_lpc1225fbd48_301,
  269. ct_lpc1225fbd48_321,
  270. ct_lpc1225fbd64_301,
  271. ct_lpc1225fbd64_321,
  272. ct_lpc1226fbd48_301,
  273. ct_lpc1226fbd64_301,
  274. ct_lpc1227fbd48_301,
  275. ct_lpc1227fbd64_301,
  276. ct_lpc12d27fbd100_301,
  277. ct_lpc1311fhn33,
  278. ct_lpc1311fhn33_01,
  279. ct_lpc1313fhn33,
  280. ct_lpc1313fhn33_01,
  281. ct_lpc1313fbd48,
  282. ct_lpc1313fbd48_01,
  283. ct_lpc1315fhn33,
  284. ct_lpc1315fbd48,
  285. ct_lpc1316fhn33,
  286. ct_lpc1316fbd48,
  287. ct_lpc1317fhn33,
  288. ct_lpc1317fbd48,
  289. ct_lpc1317fbd64,
  290. ct_lpc1342fhn33,
  291. ct_lpc1342fbd48,
  292. ct_lpc1343fhn33,
  293. ct_lpc1343fbd48,
  294. ct_lpc1345fhn33,
  295. ct_lpc1345fbd48,
  296. ct_lpc1346fhn33,
  297. ct_lpc1346fbd48,
  298. ct_lpc1347fhn33,
  299. ct_lpc1347fbd48,
  300. ct_lpc1347fbd64,
  301. ct_lpc2114,
  302. ct_lpc2124,
  303. ct_lpc2194,
  304. ct_lpc1768,
  305. ct_at91sam7s256,
  306. ct_at91sam7se256,
  307. ct_at91sam7x256,
  308. ct_at91sam7xc256,
  309. ct_stm32f030c6,
  310. ct_stm32f030c8,
  311. ct_stm32f030f4,
  312. ct_stm32f030k6,
  313. ct_stm32f030r8,
  314. ct_stm32f050c4,
  315. ct_stm32f050c6,
  316. ct_stm32f050f4,
  317. ct_stm32f050f6,
  318. ct_stm32f050g4,
  319. ct_stm32f050g6,
  320. ct_stm32f050k4,
  321. ct_stm32f050k6,
  322. ct_stm32f051c4,
  323. ct_stm32f051c6,
  324. ct_stm32f051c8,
  325. ct_stm32f051k4,
  326. ct_stm32f051k6,
  327. ct_stm32f051k8,
  328. ct_stm32f051r4,
  329. ct_stm32f051r6,
  330. ct_stm32f051r8,
  331. ct_stm32f100x4,
  332. ct_stm32f100x6,
  333. ct_stm32f100x8,
  334. ct_stm32f100xB,
  335. ct_stm32f100xC,
  336. ct_stm32f100xD,
  337. ct_stm32f100xE,
  338. ct_stm32f101x4,
  339. ct_stm32f101x6,
  340. ct_stm32f101x8,
  341. ct_stm32f101xB,
  342. ct_stm32f101xC,
  343. ct_stm32f101xD,
  344. ct_stm32f101xE,
  345. ct_stm32f101xF,
  346. ct_stm32f101xG,
  347. ct_stm32f102x4,
  348. ct_stm32f102x6,
  349. ct_stm32f102x8,
  350. ct_stm32f102xB,
  351. ct_stm32f103x4,
  352. ct_stm32f103x6,
  353. ct_stm32f103x8,
  354. ct_stm32f103xB,
  355. ct_stm32f103xC,
  356. ct_stm32f103xD,
  357. ct_stm32f103xE,
  358. ct_stm32f103xF,
  359. ct_stm32f103xG,
  360. ct_stm32f107x8,
  361. ct_stm32f107xB,
  362. ct_stm32f107xC,
  363. ct_stm32f105r8,
  364. ct_stm32f105rb,
  365. ct_stm32f105rc,
  366. ct_stm32f105v8,
  367. ct_stm32f105vb,
  368. ct_stm32f105vc,
  369. ct_stm32f107rb,
  370. ct_stm32f107rc,
  371. ct_stm32f107vb,
  372. ct_stm32f107vc,
  373. ct_stm32f429xe,
  374. ct_stm32f429xg,
  375. ct_stm32f429xi,
  376. { TI - 64 K Flash, 16 K SRAM Devices }
  377. ct_lm3s1110,
  378. ct_lm3s1133,
  379. ct_lm3s1138,
  380. ct_lm3s1150,
  381. ct_lm3s1162,
  382. ct_lm3s1165,
  383. ct_lm3s1166,
  384. ct_lm3s2110,
  385. ct_lm3s2139,
  386. ct_lm3s6100,
  387. ct_lm3s6110,
  388. { TI 128 K Flash, 32 K SRAM devices - Fury Class }
  389. ct_lm3s1601,
  390. ct_lm3s1608,
  391. ct_lm3s1620,
  392. ct_lm3s1635,
  393. ct_lm3s1636,
  394. ct_lm3s1637,
  395. ct_lm3s1651,
  396. ct_lm3s2601,
  397. ct_lm3s2608,
  398. ct_lm3s2620,
  399. ct_lm3s2637,
  400. ct_lm3s2651,
  401. ct_lm3s6610,
  402. ct_lm3s6611,
  403. ct_lm3s6618,
  404. ct_lm3s6633,
  405. ct_lm3s6637,
  406. ct_lm3s8630,
  407. { TI 256 K Flase, 32 K SRAM devices - Fury Class }
  408. ct_lm3s1911,
  409. ct_lm3s1918,
  410. ct_lm3s1937,
  411. ct_lm3s1958,
  412. ct_lm3s1960,
  413. ct_lm3s1968,
  414. ct_lm3s1969,
  415. ct_lm3s2911,
  416. ct_lm3s2918,
  417. ct_lm3s2919,
  418. ct_lm3s2939,
  419. ct_lm3s2948,
  420. ct_lm3s2950,
  421. ct_lm3s2965,
  422. ct_lm3s6911,
  423. ct_lm3s6918,
  424. ct_lm3s6938,
  425. ct_lm3s6950,
  426. ct_lm3s6952,
  427. ct_lm3s6965,
  428. ct_lm3s8930,
  429. ct_lm3s8933,
  430. ct_lm3s8938,
  431. ct_lm3s8962,
  432. ct_lm3s8970,
  433. ct_lm3s8971,
  434. { TI - Tempest Tempest - 256 K Flash, 64 K SRAM }
  435. ct_lm3s5951,
  436. ct_lm3s5956,
  437. ct_lm3s1b21,
  438. ct_lm3s2b93,
  439. ct_lm3s5b91,
  440. ct_lm3s9b81,
  441. ct_lm3s9b90,
  442. ct_lm3s9b92,
  443. ct_lm3s9b95,
  444. ct_lm3s9b96,
  445. ct_lm3s5d51,
  446. { TI - Stellaris something }
  447. ct_lm4f120h5,
  448. { Infineon }
  449. ct_xmc4500x1024,
  450. ct_xmc4500x768,
  451. ct_xmc4502x768,
  452. ct_xmc4504x512,
  453. { Allwinner }
  454. ct_allwinner_a20,
  455. ct_mk20dx128xxx7,
  456. ct_mk20dx256xxx7,
  457. ct_mk20dx64xxx7,
  458. ct_sc32442b,
  459. ct_thumb2bare:
  460. begin
  461. with embedded_controllers[current_settings.controllertype] do
  462. with linkres do
  463. begin
  464. Add('ENTRY(_START)');
  465. Add('MEMORY');
  466. Add('{');
  467. if flashsize<>0 then
  468. begin
  469. LinkStr := ' flash : ORIGIN = 0x' + IntToHex(flashbase,8)
  470. + ', LENGTH = 0x' + IntToHex(flashsize,8);
  471. Add(LinkStr);
  472. end;
  473. LinkStr := ' ram : ORIGIN = 0x' + IntToHex(srambase,8)
  474. + ', LENGTH = 0x' + IntToHex(sramsize,8);
  475. Add(LinkStr);
  476. Add('}');
  477. Add('_stack_top = 0x' + IntToHex(sramsize+srambase,8) + ';');
  478. // Add Checksum Calculation for LPC Controllers so that the bootloader starts the uploaded binary
  479. writeln(controllerunitstr);
  480. if (controllerunitstr = 'LPC8xx') or (controllerunitstr = 'LPC11XX') or (controllerunitstr = 'LPC122X') then
  481. Add('Startup_Checksum = 0 - (_stack_top + _START + 1 + NonMaskableInt_interrupt + 1 + Hardfault_interrupt + 1);');
  482. if (controllerunitstr = 'LPC13XX') then
  483. Add('Startup_Checksum = 0 - (_stack_top + _START + 1 + NonMaskableInt_interrupt + 1 + MemoryManagement_interrupt + 1 + BusFault_interrupt + 1 + UsageFault_interrupt + 1);');
  484. end;
  485. end
  486. else
  487. if not (cs_link_nolink in current_settings.globalswitches) then
  488. internalerror(200902011);
  489. end;
  490. with linkres do
  491. begin
  492. Add('SECTIONS');
  493. Add('{');
  494. Add(' .text :');
  495. Add(' {');
  496. Add(' _text_start = .;');
  497. Add(' KEEP(*(.init, .init.*))');
  498. if embedded_controllers[current_settings.controllertype].controllerunitstr='MK20D7' then
  499. begin
  500. Add(' . = 0x400;');
  501. Add(' KEEP(*(.flash_config, *.flash_config.*))');
  502. end;
  503. Add(' *(.text, .text.*)');
  504. Add(' *(.strings)');
  505. Add(' *(.rodata, .rodata.*)');
  506. Add(' *(.comment)');
  507. Add(' _etext = .;');
  508. if embedded_controllers[current_settings.controllertype].flashsize<>0 then
  509. begin
  510. Add(' } >flash');
  511. Add(' .note.gnu.build-id : { *(.note.gnu.build-id) } >flash ');
  512. end
  513. else
  514. begin
  515. Add(' } >ram');
  516. Add(' .note.gnu.build-id : { *(.note.gnu.build-id) } >ram ');
  517. end;
  518. Add(' .data :');
  519. Add(' {');
  520. Add(' _data = .;');
  521. Add(' *(.data, .data.*)');
  522. Add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  523. Add(' _edata = .;');
  524. if embedded_controllers[current_settings.controllertype].flashsize<>0 then
  525. begin
  526. Add(' } >ram AT >flash');
  527. end
  528. else
  529. begin
  530. Add(' } >ram');
  531. end;
  532. Add(' .bss :');
  533. Add(' {');
  534. Add(' _bss_start = .;');
  535. Add(' *(.bss, .bss.*)');
  536. Add(' *(COMMON)');
  537. Add(' } >ram');
  538. Add('. = ALIGN(4);');
  539. Add('_bss_end = . ;');
  540. Add('}');
  541. Add('_end = .;');
  542. end;
  543. {$endif ARM}
  544. {$ifdef i386}
  545. with linkres do
  546. begin
  547. Add('ENTRY(_START)');
  548. Add('SECTIONS');
  549. Add('{');
  550. Add(' . = 0x100000;');
  551. Add(' .text ALIGN (0x1000) :');
  552. Add(' {');
  553. Add(' _text = .;');
  554. Add(' KEEP(*(.init, .init.*))');
  555. Add(' *(.text, .text.*)');
  556. Add(' *(.strings)');
  557. Add(' *(.rodata, .rodata.*)');
  558. Add(' *(.comment)');
  559. Add(' _etext = .;');
  560. Add(' }');
  561. Add(' .data ALIGN (0x1000) :');
  562. Add(' {');
  563. Add(' _data = .;');
  564. Add(' *(.data, .data.*)');
  565. Add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  566. Add(' _edata = .;');
  567. Add(' }');
  568. Add(' . = ALIGN(4);');
  569. Add(' .bss :');
  570. Add(' {');
  571. Add(' _bss_start = .;');
  572. Add(' *(.bss, .bss.*)');
  573. Add(' *(COMMON)');
  574. Add(' }');
  575. Add('_bss_end = . ;');
  576. Add('}');
  577. Add('_end = .;');
  578. end;
  579. {$endif i386}
  580. {$ifdef AVR}
  581. with linkres do
  582. begin
  583. { linker script from ld 2.19 }
  584. Add('ENTRY(_START)');
  585. Add('OUTPUT_FORMAT("elf32-avr","elf32-avr","elf32-avr")');
  586. case current_settings.cputype of
  587. cpu_avr1:
  588. Add('OUTPUT_ARCH(avr:1)');
  589. cpu_avr2:
  590. Add('OUTPUT_ARCH(avr:2)');
  591. cpu_avr25:
  592. Add('OUTPUT_ARCH(avr:25)');
  593. cpu_avr3:
  594. Add('OUTPUT_ARCH(avr:3)');
  595. cpu_avr31:
  596. Add('OUTPUT_ARCH(avr:31)');
  597. cpu_avr35:
  598. Add('OUTPUT_ARCH(avr:35)');
  599. cpu_avr4:
  600. Add('OUTPUT_ARCH(avr:4)');
  601. cpu_avr5:
  602. Add('OUTPUT_ARCH(avr:5)');
  603. cpu_avr51:
  604. Add('OUTPUT_ARCH(avr:51)');
  605. cpu_avr6:
  606. Add('OUTPUT_ARCH(avr:6)');
  607. else
  608. Internalerror(2015072701);
  609. end;
  610. Add('MEMORY');
  611. with embedded_controllers[current_settings.controllertype] do
  612. begin
  613. Add('{');
  614. Add(' text (rx) : ORIGIN = 0, LENGTH = 0x'+IntToHex(flashsize,6));
  615. Add(' data (rw!x) : ORIGIN = 0x'+IntToHex($800000+srambase,6)+', LENGTH = 0x'+IntToHex(sramsize,6));
  616. Add(' eeprom (rw!x) : ORIGIN = 0x810000, LENGTH = 0x'+IntToHex(eepromsize,6));
  617. Add(' fuse (rw!x) : ORIGIN = 0x820000, LENGTH = 1K');
  618. Add(' lock (rw!x) : ORIGIN = 0x830000, LENGTH = 1K');
  619. Add(' signature (rw!x) : ORIGIN = 0x840000, LENGTH = 1K');
  620. Add('}');
  621. Add('_stack_top = 0x' + IntToHex(sramsize-1,4) + ';');
  622. end;
  623. Add('SECTIONS');
  624. Add('{');
  625. Add(' /* Read-only sections, merged into text segment: */');
  626. Add(' .hash : { *(.hash) }');
  627. Add(' .dynsym : { *(.dynsym) }');
  628. Add(' .dynstr : { *(.dynstr) }');
  629. Add(' .gnu.version : { *(.gnu.version) }');
  630. Add(' .gnu.version_d : { *(.gnu.version_d) }');
  631. Add(' .gnu.version_r : { *(.gnu.version_r) }');
  632. Add(' .rel.init : { *(.rel.init) }');
  633. Add(' .rela.init : { *(.rela.init) }');
  634. Add(' .rel.text :');
  635. Add(' {');
  636. Add(' *(.rel.text)');
  637. Add(' *(.rel.text.*)');
  638. Add(' *(.rel.gnu.linkonce.t*)');
  639. Add(' }');
  640. Add(' .rela.text :');
  641. Add(' {');
  642. Add(' *(.rela.text)');
  643. Add(' *(.rela.text.*)');
  644. Add(' *(.rela.gnu.linkonce.t*)');
  645. Add(' }');
  646. Add(' .rel.fini : { *(.rel.fini) }');
  647. Add(' .rela.fini : { *(.rela.fini) }');
  648. Add(' .rel.rodata :');
  649. Add(' {');
  650. Add(' *(.rel.rodata)');
  651. Add(' *(.rel.rodata.*)');
  652. Add(' *(.rel.gnu.linkonce.r*)');
  653. Add(' }');
  654. Add(' .rela.rodata :');
  655. Add(' {');
  656. Add(' *(.rela.rodata)');
  657. Add(' *(.rela.rodata.*)');
  658. Add(' *(.rela.gnu.linkonce.r*)');
  659. Add(' }');
  660. Add(' .rel.data :');
  661. Add(' {');
  662. Add(' *(.rel.data)');
  663. Add(' *(.rel.data.*)');
  664. Add(' *(.rel.gnu.linkonce.d*)');
  665. Add(' }');
  666. Add(' .rela.data :');
  667. Add(' {');
  668. Add(' *(.rela.data)');
  669. Add(' *(.rela.data.*)');
  670. Add(' *(.rela.gnu.linkonce.d*)');
  671. Add(' }');
  672. Add(' .rel.ctors : { *(.rel.ctors) }');
  673. Add(' .rela.ctors : { *(.rela.ctors) }');
  674. Add(' .rel.dtors : { *(.rel.dtors) }');
  675. Add(' .rela.dtors : { *(.rela.dtors) }');
  676. Add(' .rel.got : { *(.rel.got) }');
  677. Add(' .rela.got : { *(.rela.got) }');
  678. Add(' .rel.bss : { *(.rel.bss) }');
  679. Add(' .rela.bss : { *(.rela.bss) }');
  680. Add(' .rel.plt : { *(.rel.plt) }');
  681. Add(' .rela.plt : { *(.rela.plt) }');
  682. Add(' /* Internal text space or external memory. */');
  683. Add(' .text :');
  684. Add(' {');
  685. Add(' KEEP(*(.init, .init.*))');
  686. Add(' /* For data that needs to reside in the lower 64k of progmem. */');
  687. Add(' *(.progmem.gcc*)');
  688. Add(' *(.progmem*)');
  689. Add(' . = ALIGN(2);');
  690. Add(' __trampolines_start = . ;');
  691. Add(' /* The jump trampolines for the 16-bit limited relocs will reside here. */');
  692. Add(' *(.trampolines)');
  693. Add(' *(.trampolines*)');
  694. Add(' __trampolines_end = . ;');
  695. Add(' /* For future tablejump instruction arrays for 3 byte pc devices.');
  696. Add(' We don''t relax jump/call instructions within these sections. */');
  697. Add(' *(.jumptables)');
  698. Add(' *(.jumptables*)');
  699. Add(' /* For code that needs to reside in the lower 128k progmem. */');
  700. Add(' *(.lowtext)');
  701. Add(' *(.lowtext*)');
  702. Add(' __ctors_start = . ;');
  703. Add(' *(.ctors)');
  704. Add(' __ctors_end = . ;');
  705. Add(' __dtors_start = . ;');
  706. Add(' *(.dtors)');
  707. Add(' __dtors_end = . ;');
  708. Add(' KEEP(SORT(*)(.ctors))');
  709. Add(' KEEP(SORT(*)(.dtors))');
  710. Add(' /* From this point on, we don''t bother about wether the insns are');
  711. Add(' below or above the 16 bits boundary. */');
  712. Add(' *(.init0) /* Start here after reset. */');
  713. Add(' KEEP (*(.init0))');
  714. Add(' *(.init1)');
  715. Add(' KEEP (*(.init1))');
  716. Add(' *(.init2) /* Clear __zero_reg__, set up stack pointer. */');
  717. Add(' KEEP (*(.init2))');
  718. Add(' *(.init3)');
  719. Add(' KEEP (*(.init3))');
  720. Add(' *(.init4) /* Initialize data and BSS. */');
  721. Add(' KEEP (*(.init4))');
  722. Add(' *(.init5)');
  723. Add(' KEEP (*(.init5))');
  724. Add(' *(.init6) /* C++ constructors. */');
  725. Add(' KEEP (*(.init6))');
  726. Add(' *(.init7)');
  727. Add(' KEEP (*(.init7))');
  728. Add(' *(.init8)');
  729. Add(' KEEP (*(.init8))');
  730. Add(' *(.init9) /* Call main(). */');
  731. Add(' KEEP (*(.init9))');
  732. Add(' *(.text)');
  733. Add(' . = ALIGN(2);');
  734. Add(' *(.text.*)');
  735. Add(' . = ALIGN(2);');
  736. Add(' *(.fini9) /* _exit() starts here. */');
  737. Add(' KEEP (*(.fini9))');
  738. Add(' *(.fini8)');
  739. Add(' KEEP (*(.fini8))');
  740. Add(' *(.fini7)');
  741. Add(' KEEP (*(.fini7))');
  742. Add(' *(.fini6) /* C++ destructors. */');
  743. Add(' KEEP (*(.fini6))');
  744. Add(' *(.fini5)');
  745. Add(' KEEP (*(.fini5))');
  746. Add(' *(.fini4)');
  747. Add(' KEEP (*(.fini4))');
  748. Add(' *(.fini3)');
  749. Add(' KEEP (*(.fini3))');
  750. Add(' *(.fini2)');
  751. Add(' KEEP (*(.fini2))');
  752. Add(' *(.fini1)');
  753. Add(' KEEP (*(.fini1))');
  754. Add(' *(.fini0) /* Infinite loop after program termination. */');
  755. Add(' KEEP (*(.fini0))');
  756. Add(' _etext = . ;');
  757. Add(' } > text');
  758. Add(' .data : AT (ADDR (.text) + SIZEOF (.text))');
  759. Add(' {');
  760. Add(' PROVIDE (__data_start = .) ;');
  761. Add(' *(.data)');
  762. Add(' *(.data*)');
  763. Add(' *(.rodata) /* We need to include .rodata here if gcc is used */');
  764. Add(' *(.rodata*) /* with -fdata-sections. */');
  765. Add(' *(.gnu.linkonce.d*)');
  766. Add(' . = ALIGN(2);');
  767. Add(' _edata = . ;');
  768. Add(' PROVIDE (__data_end = .) ;');
  769. Add(' } > data');
  770. Add(' .bss : AT (ADDR (.bss))');
  771. Add(' {');
  772. Add(' PROVIDE (__bss_start = .) ;');
  773. Add(' *(.bss)');
  774. Add(' *(.bss*)');
  775. Add(' *(COMMON)');
  776. Add(' PROVIDE (__bss_end = .) ;');
  777. Add(' } > data');
  778. Add(' __data_load_start = LOADADDR(.data);');
  779. Add(' __data_load_end = __data_load_start + SIZEOF(.data);');
  780. Add(' /* Global data not cleared after reset. */');
  781. Add(' .noinit :');
  782. Add(' {');
  783. Add(' PROVIDE (__noinit_start = .) ;');
  784. Add(' *(.noinit*)');
  785. Add(' PROVIDE (__noinit_end = .) ;');
  786. Add(' _end = . ;');
  787. Add(' PROVIDE (__heap_start = .) ;');
  788. Add(' } > data');
  789. Add(' .eeprom :');
  790. Add(' {');
  791. Add(' *(.eeprom*)');
  792. Add(' __eeprom_end = . ;');
  793. Add(' } > eeprom');
  794. Add(' .fuse :');
  795. Add(' {');
  796. Add(' KEEP(*(.fuse))');
  797. Add(' KEEP(*(.lfuse))');
  798. Add(' KEEP(*(.hfuse))');
  799. Add(' KEEP(*(.efuse))');
  800. Add(' } > fuse');
  801. Add(' .lock :');
  802. Add(' {');
  803. Add(' KEEP(*(.lock*))');
  804. Add(' } > lock');
  805. Add(' .signature :');
  806. Add(' {');
  807. Add(' KEEP(*(.signature*))');
  808. Add(' } > signature');
  809. Add(' /* Stabs debugging sections. */');
  810. Add(' .stab 0 : { *(.stab) }');
  811. Add(' .stabstr 0 : { *(.stabstr) }');
  812. Add(' .stab.excl 0 : { *(.stab.excl) }');
  813. Add(' .stab.exclstr 0 : { *(.stab.exclstr) }');
  814. Add(' .stab.index 0 : { *(.stab.index) }');
  815. Add(' .stab.indexstr 0 : { *(.stab.indexstr) }');
  816. Add(' .comment 0 : { *(.comment) }');
  817. Add(' /* DWARF debug sections.');
  818. Add(' Symbols in the DWARF debugging sections are relative to the beginning');
  819. Add(' of the section so we begin them at 0. */');
  820. Add(' /* DWARF 1 */');
  821. Add(' .debug 0 : { *(.debug) }');
  822. Add(' .line 0 : { *(.line) }');
  823. Add(' /* GNU DWARF 1 extensions */');
  824. Add(' .debug_srcinfo 0 : { *(.debug_srcinfo) }');
  825. Add(' .debug_sfnames 0 : { *(.debug_sfnames) }');
  826. Add(' /* DWARF 1.1 and DWARF 2 */');
  827. Add(' .debug_aranges 0 : { *(.debug_aranges) }');
  828. Add(' .debug_pubnames 0 : { *(.debug_pubnames) }');
  829. Add(' /* DWARF 2 */');
  830. Add(' .debug_info 0 : { *(.debug_info) *(.gnu.linkonce.wi.*) }');
  831. Add(' .debug_abbrev 0 : { *(.debug_abbrev) }');
  832. Add(' .debug_line 0 : { *(.debug_line) }');
  833. Add(' .debug_frame 0 : { *(.debug_frame) }');
  834. Add(' .debug_str 0 : { *(.debug_str) }');
  835. Add(' .debug_loc 0 : { *(.debug_loc) }');
  836. Add(' .debug_macinfo 0 : { *(.debug_macinfo) }');
  837. Add('}');
  838. end;
  839. {$endif AVR}
  840. {$ifdef MIPSEL}
  841. case current_settings.controllertype of
  842. ct_none:
  843. begin
  844. end;
  845. ct_pic32mx110f016b,
  846. ct_pic32mx110f016c,
  847. ct_pic32mx110f016d,
  848. ct_pic32mx120f032b,
  849. ct_pic32mx120f032c,
  850. ct_pic32mx120f032d,
  851. ct_pic32mx130f064b,
  852. ct_pic32mx130f064c,
  853. ct_pic32mx130f064d,
  854. ct_pic32mx150f128b,
  855. ct_pic32mx150f128c,
  856. ct_pic32mx150f128d,
  857. ct_pic32mx210f016b,
  858. ct_pic32mx210f016c,
  859. ct_pic32mx210f016d,
  860. ct_pic32mx220f032b,
  861. ct_pic32mx220f032c,
  862. ct_pic32mx220f032d,
  863. ct_pic32mx230f064b,
  864. ct_pic32mx230f064c,
  865. ct_pic32mx230f064d,
  866. ct_pic32mx250f128b,
  867. ct_pic32mx250f128c,
  868. ct_pic32mx250f128d,
  869. ct_pic32mx775f256h,
  870. ct_pic32mx775f256l,
  871. ct_pic32mx775f512h,
  872. ct_pic32mx775f512l,
  873. ct_pic32mx795f512h,
  874. ct_pic32mx795f512l:
  875. begin
  876. with embedded_controllers[current_settings.controllertype] do
  877. with linkres do
  878. begin
  879. Add('OUTPUT_FORMAT("elf32-tradlittlemips")');
  880. Add('OUTPUT_ARCH(pic32mx)');
  881. Add('ENTRY(_reset)');
  882. Add('PROVIDE(_vector_spacing = 0x00000001);');
  883. Add('_ebase_address = 0x'+IntToHex(flashbase,8)+';');
  884. Add('_RESET_ADDR = 0xBFC00000;');
  885. Add('_BEV_EXCPT_ADDR = 0xBFC00380;');
  886. Add('_DBG_EXCPT_ADDR = 0xBFC00480;');
  887. Add('_GEN_EXCPT_ADDR = _ebase_address + 0x180;');
  888. Add('MEMORY');
  889. Add('{');
  890. if flashsize<>0 then
  891. begin
  892. Add(' kseg0_program_mem : ORIGIN = 0x'+IntToHex(flashbase,8)+', LENGTH = 0x'+IntToHex(flashsize,8));
  893. //TODO This should better be placed into the controllertype records
  894. Add(' kseg1_boot_mem : ORIGIN = 0xBFC00000, LENGTH = 0xbef');
  895. Add(' config3 : ORIGIN = 0xBFC00BF0, LENGTH = 0x4');
  896. Add(' config2 : ORIGIN = 0xBFC00BF4, LENGTH = 0x4');
  897. Add(' config1 : ORIGIN = 0xBFC00BF8, LENGTH = 0x4');
  898. Add(' config0 : ORIGIN = 0xBFC00BFC, LENGTH = 0x4');
  899. end;
  900. Add(' ram : ORIGIN = 0x' + IntToHex(srambase,8)
  901. + ', LENGTH = 0x' + IntToHex(sramsize,8));
  902. Add('}');
  903. Add('_stack_top = 0x' + IntToHex(sramsize+srambase,8) + ';');
  904. end;
  905. end
  906. else
  907. if not (cs_link_nolink in current_settings.globalswitches) then
  908. internalerror(200902011);
  909. end;
  910. with linkres do
  911. begin
  912. Add('SECTIONS');
  913. Add('{');
  914. Add(' .reset _RESET_ADDR :');
  915. Add(' {');
  916. Add(' KEEP(*(.reset .reset.*))');
  917. Add(' KEEP(*(.startup .startup.*))');
  918. Add(' } > kseg1_boot_mem');
  919. Add(' .bev_excpt _BEV_EXCPT_ADDR :');
  920. Add(' {');
  921. Add(' KEEP(*(.bev_handler))');
  922. Add(' } > kseg1_boot_mem');
  923. Add(' .text :');
  924. Add(' {');
  925. Add(' _text_start = .;');
  926. Add(' . = _text_start + 0x180;');
  927. Add(' KEEP(*(.gen_handler))');
  928. Add(' . = _text_start + 0x200;');
  929. Add(' KEEP(*(.init .init.*))');
  930. Add(' *(.text .text.*)');
  931. Add(' *(.strings)');
  932. Add(' *(.rodata .rodata.*)');
  933. Add(' *(.comment)');
  934. Add(' _etext = .;');
  935. if embedded_controllers[current_settings.controllertype].flashsize<>0 then
  936. begin
  937. Add(' } >kseg0_program_mem');
  938. end
  939. else
  940. begin
  941. Add(' } >ram');
  942. end;
  943. Add(' .note.gnu.build-id : { *(.note.gnu.build-id) }');
  944. Add(' .data :');
  945. Add(' {');
  946. Add(' _data = .;');
  947. Add(' *(.data .data.*)');
  948. Add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  949. Add(' . = .;');
  950. Add(' _gp = ALIGN(16) + 0x7ff0;');
  951. Add(' _edata = .;');
  952. if embedded_controllers[current_settings.controllertype].flashsize<>0 then
  953. begin
  954. Add(' } >ram AT >kseg0_program_mem');
  955. end
  956. else
  957. begin
  958. Add(' } >ram');
  959. end;
  960. Add(' .config_BFC00BF0 : {');
  961. Add(' KEEP(*(.config_BFC00BF0))');
  962. Add(' } > config3');
  963. Add(' .config_BFC00BF4 : {');
  964. Add(' KEEP(*(.config_BFC00BF4))');
  965. Add(' } > config2');
  966. Add(' .config_BFC00BF8 : {');
  967. Add(' KEEP(*(.config_BFC00BF8))');
  968. Add(' } > config1');
  969. Add(' .config_BFC00BFC : {');
  970. Add(' KEEP(*(.config_BFC00BFC))');
  971. Add(' } > config0');
  972. Add(' .bss :');
  973. Add(' {');
  974. Add(' _bss_start = .;');
  975. Add(' *(.bss, .bss.*)');
  976. Add(' *(COMMON)');
  977. Add(' } >ram');
  978. Add('. = ALIGN(4);');
  979. Add('_bss_end = . ;');
  980. Add(' .comment 0 : { *(.comment) }');
  981. Add(' /* DWARF debug sections.');
  982. Add(' Symbols in the DWARF debugging sections are relative to the beginning');
  983. Add(' of the section so we begin them at 0. */');
  984. Add(' /* DWARF 1 */');
  985. Add(' .debug 0 : { *(.debug) }');
  986. Add(' .line 0 : { *(.line) }');
  987. Add(' /* GNU DWARF 1 extensions */');
  988. Add(' .debug_srcinfo 0 : { *(.debug_srcinfo) }');
  989. Add(' .debug_sfnames 0 : { *(.debug_sfnames) }');
  990. Add(' /* DWARF 1.1 and DWARF 2 */');
  991. Add(' .debug_aranges 0 : { *(.debug_aranges) }');
  992. Add(' .debug_pubnames 0 : { *(.debug_pubnames) }');
  993. Add(' /* DWARF 2 */');
  994. Add(' .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) }');
  995. Add(' .debug_abbrev 0 : { *(.debug_abbrev) }');
  996. Add(' /DISCARD/ : { *(.debug_line) }');
  997. Add(' .debug_frame 0 : { *(.debug_frame) }');
  998. Add(' .debug_str 0 : { *(.debug_str) }');
  999. Add(' /DISCARD/ : { *(.debug_loc) }');
  1000. Add(' .debug_macinfo 0 : { *(.debug_macinfo) }');
  1001. Add(' /* SGI/MIPS DWARF 2 extensions */');
  1002. Add(' .debug_weaknames 0 : { *(.debug_weaknames) }');
  1003. Add(' .debug_funcnames 0 : { *(.debug_funcnames) }');
  1004. Add(' .debug_typenames 0 : { *(.debug_typenames) }');
  1005. Add(' .debug_varnames 0 : { *(.debug_varnames) }');
  1006. Add(' /* DWARF 3 */');
  1007. Add(' .debug_pubtypes 0 : { *(.debug_pubtypes) }');
  1008. Add(' .debug_ranges 0 : { *(.debug_ranges) }');
  1009. Add(' .gnu.attributes 0 : { KEEP (*(.gnu.attributes)) }');
  1010. Add(' .gptab.sdata : { *(.gptab.data) *(.gptab.sdata) }');
  1011. Add(' .gptab.sbss : { *(.gptab.bss) *(.gptab.sbss) }');
  1012. Add(' .mdebug.abi32 : { KEEP(*(.mdebug.abi32)) }');
  1013. Add(' .mdebug.abiN32 : { KEEP(*(.mdebug.abiN32)) }');
  1014. Add(' .mdebug.abi64 : { KEEP(*(.mdebug.abi64)) }');
  1015. Add(' .mdebug.abiO64 : { KEEP(*(.mdebug.abiO64)) }');
  1016. Add(' .mdebug.eabi32 : { KEEP(*(.mdebug.eabi32)) }');
  1017. Add(' .mdebug.eabi64 : { KEEP(*(.mdebug.eabi64)) }');
  1018. Add(' /DISCARD/ : { *(.rel.dyn) }');
  1019. Add(' /DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) *(.gnu.lto_*) }');
  1020. Add('}');
  1021. Add('_end = .;');
  1022. end;
  1023. {$endif MIPSEL}
  1024. { Write and Close response }
  1025. linkres.writetodisk;
  1026. linkres.free;
  1027. WriteResponseFile:=True;
  1028. end;
  1029. function TlinkerEmbedded.MakeExecutable:boolean;
  1030. var
  1031. binstr,
  1032. cmdstr : TCmdStr;
  1033. success : boolean;
  1034. StaticStr,
  1035. GCSectionsStr,
  1036. DynLinkStr,
  1037. StripStr: string;
  1038. begin
  1039. { for future use }
  1040. StaticStr:='';
  1041. StripStr:='';
  1042. DynLinkStr:='';
  1043. GCSectionsStr:='--gc-sections';
  1044. //if not(cs_link_extern in current_settings.globalswitches) then
  1045. if not(cs_link_nolink in current_settings.globalswitches) then
  1046. Message1(exec_i_linking,current_module.exefilename);
  1047. { Write used files and libraries }
  1048. WriteResponseFile();
  1049. { Call linker }
  1050. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  1051. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  1052. if not(cs_link_on_target in current_settings.globalswitches) then
  1053. begin
  1054. Replace(cmdstr,'$EXE',(maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename,'.elf')))));
  1055. Replace(cmdstr,'$RES',(maybequoted(ScriptFixFileName(outputexedir+Info.ResName))));
  1056. Replace(cmdstr,'$STATIC',StaticStr);
  1057. Replace(cmdstr,'$STRIP',StripStr);
  1058. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  1059. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  1060. end
  1061. else
  1062. begin
  1063. Replace(cmdstr,'$EXE',maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename,'.elf'))));
  1064. Replace(cmdstr,'$RES',maybequoted(ScriptFixFileName(outputexedir+Info.ResName)));
  1065. Replace(cmdstr,'$STATIC',StaticStr);
  1066. Replace(cmdstr,'$STRIP',StripStr);
  1067. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  1068. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  1069. end;
  1070. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  1071. { Remove ReponseFile }
  1072. if success and not(cs_link_nolink in current_settings.globalswitches) then
  1073. DeleteFile(outputexedir+Info.ResName);
  1074. { Post process }
  1075. if success and not(cs_link_nolink in current_settings.globalswitches) then
  1076. success:=PostProcessExecutable(current_module.exefilename+'.elf',false);
  1077. if success and (target_info.system in [system_arm_embedded,system_avr_embedded,system_mipsel_embedded]) then
  1078. begin
  1079. success:=DoExec(FindUtil(utilsprefix+'objcopy'),'-O ihex '+
  1080. ChangeFileExt(current_module.exefilename,'.elf')+' '+
  1081. ChangeFileExt(current_module.exefilename,'.hex'),true,false);
  1082. if success then
  1083. success:=DoExec(FindUtil(utilsprefix+'objcopy'),'-O binary '+
  1084. ChangeFileExt(current_module.exefilename,'.elf')+' '+
  1085. ChangeFileExt(current_module.exefilename,'.bin'),true,false);
  1086. end;
  1087. MakeExecutable:=success; { otherwise a recursive call to link method }
  1088. end;
  1089. function TLinkerEmbedded.postprocessexecutable(const fn : string;isdll:boolean):boolean;
  1090. type
  1091. TElf32header=packed record
  1092. magic0123 : longint;
  1093. file_class : byte;
  1094. data_encoding : byte;
  1095. file_version : byte;
  1096. padding : array[$07..$0f] of byte;
  1097. e_type : word;
  1098. e_machine : word;
  1099. e_version : longint;
  1100. e_entry : longint; { entrypoint }
  1101. e_phoff : longint; { program header offset }
  1102. e_shoff : longint; { sections header offset }
  1103. e_flags : longint;
  1104. e_ehsize : word; { elf header size in bytes }
  1105. e_phentsize : word; { size of an entry in the program header array }
  1106. e_phnum : word; { 0..e_phnum-1 of entrys }
  1107. e_shentsize : word; { size of an entry in sections header array }
  1108. e_shnum : word; { 0..e_shnum-1 of entrys }
  1109. e_shstrndx : word; { index of string section header }
  1110. end;
  1111. TElf32sechdr=packed record
  1112. sh_name : longint;
  1113. sh_type : longint;
  1114. sh_flags : longint;
  1115. sh_addr : longint;
  1116. sh_offset : longint;
  1117. sh_size : longint;
  1118. sh_link : longint;
  1119. sh_info : longint;
  1120. sh_addralign : longint;
  1121. sh_entsize : longint;
  1122. end;
  1123. function MayBeSwapHeader(h : telf32header) : telf32header;
  1124. begin
  1125. result:=h;
  1126. if source_info.endian<>target_info.endian then
  1127. with h do
  1128. begin
  1129. result.e_type:=swapendian(e_type);
  1130. result.e_machine:=swapendian(e_machine);
  1131. result.e_version:=swapendian(e_version);
  1132. result.e_entry:=swapendian(e_entry);
  1133. result.e_phoff:=swapendian(e_phoff);
  1134. result.e_shoff:=swapendian(e_shoff);
  1135. result.e_flags:=swapendian(e_flags);
  1136. result.e_ehsize:=swapendian(e_ehsize);
  1137. result.e_phentsize:=swapendian(e_phentsize);
  1138. result.e_phnum:=swapendian(e_phnum);
  1139. result.e_shentsize:=swapendian(e_shentsize);
  1140. result.e_shnum:=swapendian(e_shnum);
  1141. result.e_shstrndx:=swapendian(e_shstrndx);
  1142. end;
  1143. end;
  1144. function MaybeSwapSecHeader(h : telf32sechdr) : telf32sechdr;
  1145. begin
  1146. result:=h;
  1147. if source_info.endian<>target_info.endian then
  1148. with h do
  1149. begin
  1150. result.sh_name:=swapendian(sh_name);
  1151. result.sh_type:=swapendian(sh_type);
  1152. result.sh_flags:=swapendian(sh_flags);
  1153. result.sh_addr:=swapendian(sh_addr);
  1154. result.sh_offset:=swapendian(sh_offset);
  1155. result.sh_size:=swapendian(sh_size);
  1156. result.sh_link:=swapendian(sh_link);
  1157. result.sh_info:=swapendian(sh_info);
  1158. result.sh_addralign:=swapendian(sh_addralign);
  1159. result.sh_entsize:=swapendian(sh_entsize);
  1160. end;
  1161. end;
  1162. var
  1163. f : file;
  1164. function ReadSectionName(pos : longint) : String;
  1165. var
  1166. oldpos : longint;
  1167. c : char;
  1168. begin
  1169. oldpos:=filepos(f);
  1170. seek(f,pos);
  1171. Result:='';
  1172. while true do
  1173. begin
  1174. blockread(f,c,1);
  1175. if c=#0 then
  1176. break;
  1177. Result:=Result+c;
  1178. end;
  1179. seek(f,oldpos);
  1180. end;
  1181. var
  1182. elfheader : TElf32header;
  1183. secheader : TElf32sechdr;
  1184. i : longint;
  1185. stringoffset : longint;
  1186. secname : string;
  1187. begin
  1188. postprocessexecutable:=false;
  1189. { open file }
  1190. assign(f,fn);
  1191. {$push}{$I-}
  1192. reset(f,1);
  1193. if ioresult<>0 then
  1194. Message1(execinfo_f_cant_open_executable,fn);
  1195. { read header }
  1196. blockread(f,elfheader,sizeof(tElf32header));
  1197. elfheader:=MayBeSwapHeader(elfheader);
  1198. seek(f,elfheader.e_shoff);
  1199. { read string section header }
  1200. seek(f,elfheader.e_shoff+sizeof(TElf32sechdr)*elfheader.e_shstrndx);
  1201. blockread(f,secheader,sizeof(secheader));
  1202. secheader:=MaybeSwapSecHeader(secheader);
  1203. stringoffset:=secheader.sh_offset;
  1204. seek(f,elfheader.e_shoff);
  1205. status.datasize:=0;
  1206. for i:=0 to elfheader.e_shnum-1 do
  1207. begin
  1208. blockread(f,secheader,sizeof(secheader));
  1209. secheader:=MaybeSwapSecHeader(secheader);
  1210. secname:=ReadSectionName(stringoffset+secheader.sh_name);
  1211. if secname='.text' then
  1212. begin
  1213. Message1(execinfo_x_codesize,tostr(secheader.sh_size));
  1214. status.codesize:=secheader.sh_size;
  1215. end
  1216. else if secname='.data' then
  1217. begin
  1218. Message1(execinfo_x_initdatasize,tostr(secheader.sh_size));
  1219. inc(status.datasize,secheader.sh_size);
  1220. end
  1221. else if secname='.bss' then
  1222. begin
  1223. Message1(execinfo_x_uninitdatasize,tostr(secheader.sh_size));
  1224. inc(status.datasize,secheader.sh_size);
  1225. end;
  1226. end;
  1227. close(f);
  1228. {$pop}
  1229. if ioresult<>0 then
  1230. ;
  1231. postprocessexecutable:=true;
  1232. end;
  1233. {*****************************************************************************
  1234. Initialize
  1235. *****************************************************************************}
  1236. initialization
  1237. {$ifdef arm}
  1238. RegisterLinker(ld_embedded,TLinkerEmbedded);
  1239. RegisterTarget(system_arm_embedded_info);
  1240. {$endif arm}
  1241. {$ifdef avr}
  1242. RegisterLinker(ld_embedded,TLinkerEmbedded);
  1243. RegisterTarget(system_avr_embedded_info);
  1244. {$endif avr}
  1245. {$ifdef i386}
  1246. RegisterLinker(ld_embedded,TLinkerEmbedded);
  1247. RegisterTarget(system_i386_embedded_info);
  1248. {$endif i386}
  1249. {$ifdef mipsel}
  1250. RegisterLinker(ld_embedded,TLinkerEmbedded);
  1251. RegisterTarget(system_mipsel_embedded_info);
  1252. {$endif mipsel}
  1253. end.