t_embed.pas 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314
  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. Add(' *(.text, .text.*)');
  499. Add(' *(.strings)');
  500. Add(' *(.rodata, .rodata.*)');
  501. Add(' *(.comment)');
  502. Add(' _etext = .;');
  503. if embedded_controllers[current_settings.controllertype].flashsize<>0 then
  504. begin
  505. Add(' } >flash');
  506. Add(' .note.gnu.build-id : { *(.note.gnu.build-id) } >flash ');
  507. end
  508. else
  509. begin
  510. Add(' } >ram');
  511. Add(' .note.gnu.build-id : { *(.note.gnu.build-id) } >ram ');
  512. end;
  513. Add(' .data :');
  514. Add(' {');
  515. Add(' _data = .;');
  516. Add(' *(.data, .data.*)');
  517. Add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  518. Add(' _edata = .;');
  519. if embedded_controllers[current_settings.controllertype].flashsize<>0 then
  520. begin
  521. Add(' } >ram AT >flash');
  522. end
  523. else
  524. begin
  525. Add(' } >ram');
  526. end;
  527. Add(' .bss :');
  528. Add(' {');
  529. Add(' _bss_start = .;');
  530. Add(' *(.bss, .bss.*)');
  531. Add(' *(COMMON)');
  532. Add(' } >ram');
  533. Add('. = ALIGN(4);');
  534. Add('_bss_end = . ;');
  535. Add('}');
  536. Add('_end = .;');
  537. end;
  538. {$endif ARM}
  539. {$ifdef i386}
  540. with linkres do
  541. begin
  542. Add('ENTRY(_START)');
  543. Add('SECTIONS');
  544. Add('{');
  545. Add(' . = 0x100000;');
  546. Add(' .text ALIGN (0x1000) :');
  547. Add(' {');
  548. Add(' _text = .;');
  549. Add(' KEEP(*(.init, .init.*))');
  550. Add(' *(.text, .text.*)');
  551. Add(' *(.strings)');
  552. Add(' *(.rodata, .rodata.*)');
  553. Add(' *(.comment)');
  554. Add(' _etext = .;');
  555. Add(' }');
  556. Add(' .data ALIGN (0x1000) :');
  557. Add(' {');
  558. Add(' _data = .;');
  559. Add(' *(.data, .data.*)');
  560. Add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  561. Add(' _edata = .;');
  562. Add(' }');
  563. Add(' . = ALIGN(4);');
  564. Add(' .bss :');
  565. Add(' {');
  566. Add(' _bss_start = .;');
  567. Add(' *(.bss, .bss.*)');
  568. Add(' *(COMMON)');
  569. Add(' }');
  570. Add('_bss_end = . ;');
  571. Add('}');
  572. Add('_end = .;');
  573. end;
  574. {$endif i386}
  575. {$ifdef AVR}
  576. with linkres do
  577. begin
  578. { linker script from ld 2.19 }
  579. Add('ENTRY(_START)');
  580. Add('OUTPUT_FORMAT("elf32-avr","elf32-avr","elf32-avr")');
  581. Add('OUTPUT_ARCH(avr:2)');
  582. Add('MEMORY');
  583. Add('{');
  584. with embedded_controllers[current_settings.controllertype] do
  585. begin
  586. Add(' text (rx) : ORIGIN = 0, LENGTH = 0x'+IntToHex(flashsize,8));
  587. Add(' data (rw!x) : ORIGIN = 0x800060, LENGTH = 0x'+IntToHex(sramsize,8));
  588. Add(' eeprom (rw!x) : ORIGIN = 0x810000, LENGTH = 0x'+IntToHex(eepromsize,8));
  589. Add(' fuse (rw!x) : ORIGIN = 0x820000, LENGTH = 1K');
  590. Add(' lock (rw!x) : ORIGIN = 0x830000, LENGTH = 1K');
  591. Add(' signature (rw!x) : ORIGIN = 0x840000, LENGTH = 1K');
  592. end;
  593. Add('}');
  594. Add('SECTIONS');
  595. Add('{');
  596. Add(' /* Read-only sections, merged into text segment: */');
  597. Add(' .hash : { *(.hash) }');
  598. Add(' .dynsym : { *(.dynsym) }');
  599. Add(' .dynstr : { *(.dynstr) }');
  600. Add(' .gnu.version : { *(.gnu.version) }');
  601. Add(' .gnu.version_d : { *(.gnu.version_d) }');
  602. Add(' .gnu.version_r : { *(.gnu.version_r) }');
  603. Add(' .rel.init : { *(.rel.init) }');
  604. Add(' .rela.init : { *(.rela.init) }');
  605. Add(' .rel.text :');
  606. Add(' {');
  607. Add(' *(.rel.text)');
  608. Add(' *(.rel.text.*)');
  609. Add(' *(.rel.gnu.linkonce.t*)');
  610. Add(' }');
  611. Add(' .rela.text :');
  612. Add(' {');
  613. Add(' *(.rela.text)');
  614. Add(' *(.rela.text.*)');
  615. Add(' *(.rela.gnu.linkonce.t*)');
  616. Add(' }');
  617. Add(' .rel.fini : { *(.rel.fini) }');
  618. Add(' .rela.fini : { *(.rela.fini) }');
  619. Add(' .rel.rodata :');
  620. Add(' {');
  621. Add(' *(.rel.rodata)');
  622. Add(' *(.rel.rodata.*)');
  623. Add(' *(.rel.gnu.linkonce.r*)');
  624. Add(' }');
  625. Add(' .rela.rodata :');
  626. Add(' {');
  627. Add(' *(.rela.rodata)');
  628. Add(' *(.rela.rodata.*)');
  629. Add(' *(.rela.gnu.linkonce.r*)');
  630. Add(' }');
  631. Add(' .rel.data :');
  632. Add(' {');
  633. Add(' *(.rel.data)');
  634. Add(' *(.rel.data.*)');
  635. Add(' *(.rel.gnu.linkonce.d*)');
  636. Add(' }');
  637. Add(' .rela.data :');
  638. Add(' {');
  639. Add(' *(.rela.data)');
  640. Add(' *(.rela.data.*)');
  641. Add(' *(.rela.gnu.linkonce.d*)');
  642. Add(' }');
  643. Add(' .rel.ctors : { *(.rel.ctors) }');
  644. Add(' .rela.ctors : { *(.rela.ctors) }');
  645. Add(' .rel.dtors : { *(.rel.dtors) }');
  646. Add(' .rela.dtors : { *(.rela.dtors) }');
  647. Add(' .rel.got : { *(.rel.got) }');
  648. Add(' .rela.got : { *(.rela.got) }');
  649. Add(' .rel.bss : { *(.rel.bss) }');
  650. Add(' .rela.bss : { *(.rela.bss) }');
  651. Add(' .rel.plt : { *(.rel.plt) }');
  652. Add(' .rela.plt : { *(.rela.plt) }');
  653. Add(' /* Internal text space or external memory. */');
  654. Add(' .text :');
  655. Add(' {');
  656. Add(' KEEP(*(.init, .init.*))');
  657. Add(' /* For data that needs to reside in the lower 64k of progmem. */');
  658. Add(' *(.progmem.gcc*)');
  659. Add(' *(.progmem*)');
  660. Add(' . = ALIGN(2);');
  661. Add(' __trampolines_start = . ;');
  662. Add(' /* The jump trampolines for the 16-bit limited relocs will reside here. */');
  663. Add(' *(.trampolines)');
  664. Add(' *(.trampolines*)');
  665. Add(' __trampolines_end = . ;');
  666. Add(' /* For future tablejump instruction arrays for 3 byte pc devices.');
  667. Add(' We don''t relax jump/call instructions within these sections. */');
  668. Add(' *(.jumptables)');
  669. Add(' *(.jumptables*)');
  670. Add(' /* For code that needs to reside in the lower 128k progmem. */');
  671. Add(' *(.lowtext)');
  672. Add(' *(.lowtext*)');
  673. Add(' __ctors_start = . ;');
  674. Add(' *(.ctors)');
  675. Add(' __ctors_end = . ;');
  676. Add(' __dtors_start = . ;');
  677. Add(' *(.dtors)');
  678. Add(' __dtors_end = . ;');
  679. Add(' KEEP(SORT(*)(.ctors))');
  680. Add(' KEEP(SORT(*)(.dtors))');
  681. Add(' /* From this point on, we don''t bother about wether the insns are');
  682. Add(' below or above the 16 bits boundary. */');
  683. Add(' *(.init0) /* Start here after reset. */');
  684. Add(' KEEP (*(.init0))');
  685. Add(' *(.init1)');
  686. Add(' KEEP (*(.init1))');
  687. Add(' *(.init2) /* Clear __zero_reg__, set up stack pointer. */');
  688. Add(' KEEP (*(.init2))');
  689. Add(' *(.init3)');
  690. Add(' KEEP (*(.init3))');
  691. Add(' *(.init4) /* Initialize data and BSS. */');
  692. Add(' KEEP (*(.init4))');
  693. Add(' *(.init5)');
  694. Add(' KEEP (*(.init5))');
  695. Add(' *(.init6) /* C++ constructors. */');
  696. Add(' KEEP (*(.init6))');
  697. Add(' *(.init7)');
  698. Add(' KEEP (*(.init7))');
  699. Add(' *(.init8)');
  700. Add(' KEEP (*(.init8))');
  701. Add(' *(.init9) /* Call main(). */');
  702. Add(' KEEP (*(.init9))');
  703. Add(' *(.text)');
  704. Add(' . = ALIGN(2);');
  705. Add(' *(.text.*)');
  706. Add(' . = ALIGN(2);');
  707. Add(' *(.fini9) /* _exit() starts here. */');
  708. Add(' KEEP (*(.fini9))');
  709. Add(' *(.fini8)');
  710. Add(' KEEP (*(.fini8))');
  711. Add(' *(.fini7)');
  712. Add(' KEEP (*(.fini7))');
  713. Add(' *(.fini6) /* C++ destructors. */');
  714. Add(' KEEP (*(.fini6))');
  715. Add(' *(.fini5)');
  716. Add(' KEEP (*(.fini5))');
  717. Add(' *(.fini4)');
  718. Add(' KEEP (*(.fini4))');
  719. Add(' *(.fini3)');
  720. Add(' KEEP (*(.fini3))');
  721. Add(' *(.fini2)');
  722. Add(' KEEP (*(.fini2))');
  723. Add(' *(.fini1)');
  724. Add(' KEEP (*(.fini1))');
  725. Add(' *(.fini0) /* Infinite loop after program termination. */');
  726. Add(' KEEP (*(.fini0))');
  727. Add(' _etext = . ;');
  728. Add(' } > text');
  729. Add(' .data : AT (ADDR (.text) + SIZEOF (.text))');
  730. Add(' {');
  731. Add(' PROVIDE (__data_start = .) ;');
  732. Add(' *(.data)');
  733. Add(' *(.data*)');
  734. Add(' *(.rodata) /* We need to include .rodata here if gcc is used */');
  735. Add(' *(.rodata*) /* with -fdata-sections. */');
  736. Add(' *(.gnu.linkonce.d*)');
  737. Add(' . = ALIGN(2);');
  738. Add(' _edata = . ;');
  739. Add(' PROVIDE (__data_end = .) ;');
  740. Add(' } > data');
  741. Add(' .bss : AT (ADDR (.bss))');
  742. Add(' {');
  743. Add(' PROVIDE (__bss_start = .) ;');
  744. Add(' *(.bss)');
  745. Add(' *(.bss*)');
  746. Add(' *(COMMON)');
  747. Add(' PROVIDE (__bss_end = .) ;');
  748. Add(' } > data');
  749. Add(' __data_load_start = LOADADDR(.data);');
  750. Add(' __data_load_end = __data_load_start + SIZEOF(.data);');
  751. Add(' /* Global data not cleared after reset. */');
  752. Add(' .noinit :');
  753. Add(' {');
  754. Add(' PROVIDE (__noinit_start = .) ;');
  755. Add(' *(.noinit*)');
  756. Add(' PROVIDE (__noinit_end = .) ;');
  757. Add(' _end = . ;');
  758. Add(' PROVIDE (__heap_start = .) ;');
  759. Add(' } > data');
  760. Add(' .eeprom :');
  761. Add(' {');
  762. Add(' *(.eeprom*)');
  763. Add(' __eeprom_end = . ;');
  764. Add(' } > eeprom');
  765. Add(' .fuse :');
  766. Add(' {');
  767. Add(' KEEP(*(.fuse))');
  768. Add(' KEEP(*(.lfuse))');
  769. Add(' KEEP(*(.hfuse))');
  770. Add(' KEEP(*(.efuse))');
  771. Add(' } > fuse');
  772. Add(' .lock :');
  773. Add(' {');
  774. Add(' KEEP(*(.lock*))');
  775. Add(' } > lock');
  776. Add(' .signature :');
  777. Add(' {');
  778. Add(' KEEP(*(.signature*))');
  779. Add(' } > signature');
  780. Add(' /* Stabs debugging sections. */');
  781. Add(' .stab 0 : { *(.stab) }');
  782. Add(' .stabstr 0 : { *(.stabstr) }');
  783. Add(' .stab.excl 0 : { *(.stab.excl) }');
  784. Add(' .stab.exclstr 0 : { *(.stab.exclstr) }');
  785. Add(' .stab.index 0 : { *(.stab.index) }');
  786. Add(' .stab.indexstr 0 : { *(.stab.indexstr) }');
  787. Add(' .comment 0 : { *(.comment) }');
  788. Add(' /* DWARF debug sections.');
  789. Add(' Symbols in the DWARF debugging sections are relative to the beginning');
  790. Add(' of the section so we begin them at 0. */');
  791. Add(' /* DWARF 1 */');
  792. Add(' .debug 0 : { *(.debug) }');
  793. Add(' .line 0 : { *(.line) }');
  794. Add(' /* GNU DWARF 1 extensions */');
  795. Add(' .debug_srcinfo 0 : { *(.debug_srcinfo) }');
  796. Add(' .debug_sfnames 0 : { *(.debug_sfnames) }');
  797. Add(' /* DWARF 1.1 and DWARF 2 */');
  798. Add(' .debug_aranges 0 : { *(.debug_aranges) }');
  799. Add(' .debug_pubnames 0 : { *(.debug_pubnames) }');
  800. Add(' /* DWARF 2 */');
  801. Add(' .debug_info 0 : { *(.debug_info) *(.gnu.linkonce.wi.*) }');
  802. Add(' .debug_abbrev 0 : { *(.debug_abbrev) }');
  803. Add(' .debug_line 0 : { *(.debug_line) }');
  804. Add(' .debug_frame 0 : { *(.debug_frame) }');
  805. Add(' .debug_str 0 : { *(.debug_str) }');
  806. Add(' .debug_loc 0 : { *(.debug_loc) }');
  807. Add(' .debug_macinfo 0 : { *(.debug_macinfo) }');
  808. Add('}');
  809. { last address of ram on an atmega128 }
  810. Add('_stack_top = 0x0fff;');
  811. end;
  812. {$endif AVR}
  813. {$ifdef MIPSEL}
  814. case current_settings.controllertype of
  815. ct_none:
  816. begin
  817. end;
  818. ct_pic32mx110f016b,
  819. ct_pic32mx110f016c,
  820. ct_pic32mx110f016d,
  821. ct_pic32mx120f032b,
  822. ct_pic32mx120f032c,
  823. ct_pic32mx120f032d,
  824. ct_pic32mx130f064b,
  825. ct_pic32mx130f064c,
  826. ct_pic32mx130f064d,
  827. ct_pic32mx150f128b,
  828. ct_pic32mx150f128c,
  829. ct_pic32mx150f128d,
  830. ct_pic32mx210f016b,
  831. ct_pic32mx210f016c,
  832. ct_pic32mx210f016d,
  833. ct_pic32mx220f032b,
  834. ct_pic32mx220f032c,
  835. ct_pic32mx220f032d,
  836. ct_pic32mx230f064b,
  837. ct_pic32mx230f064c,
  838. ct_pic32mx230f064d,
  839. ct_pic32mx250f128b,
  840. ct_pic32mx250f128c,
  841. ct_pic32mx250f128d,
  842. ct_pic32mx775f256h,
  843. ct_pic32mx775f256l,
  844. ct_pic32mx775f512h,
  845. ct_pic32mx775f512l,
  846. ct_pic32mx795f512h,
  847. ct_pic32mx795f512l:
  848. begin
  849. with embedded_controllers[current_settings.controllertype] do
  850. with linkres do
  851. begin
  852. Add('OUTPUT_FORMAT("elf32-tradlittlemips")');
  853. Add('OUTPUT_ARCH(pic32mx)');
  854. Add('ENTRY(_reset)');
  855. Add('PROVIDE(_vector_spacing = 0x00000001);');
  856. Add('_ebase_address = 0x'+IntToHex(flashbase,8)+';');
  857. Add('_RESET_ADDR = 0xBFC00000;');
  858. Add('_BEV_EXCPT_ADDR = 0xBFC00380;');
  859. Add('_DBG_EXCPT_ADDR = 0xBFC00480;');
  860. Add('_GEN_EXCPT_ADDR = _ebase_address + 0x180;');
  861. Add('MEMORY');
  862. Add('{');
  863. if flashsize<>0 then
  864. begin
  865. Add(' kseg0_program_mem : ORIGIN = 0x'+IntToHex(flashbase,8)+', LENGTH = 0x'+IntToHex(flashsize,8));
  866. //TODO This should better be placed into the controllertype records
  867. Add(' kseg1_boot_mem : ORIGIN = 0xBFC00000, LENGTH = 0xbef');
  868. Add(' config3 : ORIGIN = 0xBFC00BF0, LENGTH = 0x4');
  869. Add(' config2 : ORIGIN = 0xBFC00BF4, LENGTH = 0x4');
  870. Add(' config1 : ORIGIN = 0xBFC00BF8, LENGTH = 0x4');
  871. Add(' config0 : ORIGIN = 0xBFC00BFC, LENGTH = 0x4');
  872. end;
  873. Add(' ram : ORIGIN = 0x' + IntToHex(srambase,8)
  874. + ', LENGTH = 0x' + IntToHex(sramsize,8));
  875. Add('}');
  876. Add('_stack_top = 0x' + IntToHex(sramsize+srambase,8) + ';');
  877. end;
  878. end
  879. else
  880. if not (cs_link_nolink in current_settings.globalswitches) then
  881. internalerror(200902011);
  882. end;
  883. with linkres do
  884. begin
  885. Add('SECTIONS');
  886. Add('{');
  887. Add(' .reset _RESET_ADDR :');
  888. Add(' {');
  889. Add(' KEEP(*(.reset .reset.*))');
  890. Add(' KEEP(*(.startup .startup.*))');
  891. Add(' } > kseg1_boot_mem');
  892. Add(' .bev_excpt _BEV_EXCPT_ADDR :');
  893. Add(' {');
  894. Add(' KEEP(*(.bev_handler))');
  895. Add(' } > kseg1_boot_mem');
  896. Add(' .text :');
  897. Add(' {');
  898. Add(' _text_start = .;');
  899. Add(' . = _text_start + 0x180;');
  900. Add(' KEEP(*(.gen_handler))');
  901. Add(' . = _text_start + 0x200;');
  902. Add(' KEEP(*(.init .init.*))');
  903. Add(' *(.text .text.*)');
  904. Add(' *(.strings)');
  905. Add(' *(.rodata .rodata.*)');
  906. Add(' *(.comment)');
  907. Add(' _etext = .;');
  908. if embedded_controllers[current_settings.controllertype].flashsize<>0 then
  909. begin
  910. Add(' } >kseg0_program_mem');
  911. end
  912. else
  913. begin
  914. Add(' } >ram');
  915. end;
  916. Add(' .note.gnu.build-id : { *(.note.gnu.build-id) }');
  917. Add(' .data :');
  918. Add(' {');
  919. Add(' _data = .;');
  920. Add(' *(.data .data.*)');
  921. Add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  922. Add(' . = .;');
  923. Add(' _gp = ALIGN(16) + 0x7ff0;');
  924. Add(' _edata = .;');
  925. if embedded_controllers[current_settings.controllertype].flashsize<>0 then
  926. begin
  927. Add(' } >ram AT >kseg0_program_mem');
  928. end
  929. else
  930. begin
  931. Add(' } >ram');
  932. end;
  933. Add(' .config_BFC00BF0 : {');
  934. Add(' KEEP(*(.config_BFC00BF0))');
  935. Add(' } > config3');
  936. Add(' .config_BFC00BF4 : {');
  937. Add(' KEEP(*(.config_BFC00BF4))');
  938. Add(' } > config2');
  939. Add(' .config_BFC00BF8 : {');
  940. Add(' KEEP(*(.config_BFC00BF8))');
  941. Add(' } > config1');
  942. Add(' .config_BFC00BFC : {');
  943. Add(' KEEP(*(.config_BFC00BFC))');
  944. Add(' } > config0');
  945. Add(' .bss :');
  946. Add(' {');
  947. Add(' _bss_start = .;');
  948. Add(' *(.bss, .bss.*)');
  949. Add(' *(COMMON)');
  950. Add(' } >ram');
  951. Add('. = ALIGN(4);');
  952. Add('_bss_end = . ;');
  953. Add(' .comment 0 : { *(.comment) }');
  954. Add(' /* DWARF debug sections.');
  955. Add(' Symbols in the DWARF debugging sections are relative to the beginning');
  956. Add(' of the section so we begin them at 0. */');
  957. Add(' /* DWARF 1 */');
  958. Add(' .debug 0 : { *(.debug) }');
  959. Add(' .line 0 : { *(.line) }');
  960. Add(' /* GNU DWARF 1 extensions */');
  961. Add(' .debug_srcinfo 0 : { *(.debug_srcinfo) }');
  962. Add(' .debug_sfnames 0 : { *(.debug_sfnames) }');
  963. Add(' /* DWARF 1.1 and DWARF 2 */');
  964. Add(' .debug_aranges 0 : { *(.debug_aranges) }');
  965. Add(' .debug_pubnames 0 : { *(.debug_pubnames) }');
  966. Add(' /* DWARF 2 */');
  967. Add(' .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) }');
  968. Add(' .debug_abbrev 0 : { *(.debug_abbrev) }');
  969. Add(' /DISCARD/ : { *(.debug_line) }');
  970. Add(' .debug_frame 0 : { *(.debug_frame) }');
  971. Add(' .debug_str 0 : { *(.debug_str) }');
  972. Add(' /DISCARD/ : { *(.debug_loc) }');
  973. Add(' .debug_macinfo 0 : { *(.debug_macinfo) }');
  974. Add(' /* SGI/MIPS DWARF 2 extensions */');
  975. Add(' .debug_weaknames 0 : { *(.debug_weaknames) }');
  976. Add(' .debug_funcnames 0 : { *(.debug_funcnames) }');
  977. Add(' .debug_typenames 0 : { *(.debug_typenames) }');
  978. Add(' .debug_varnames 0 : { *(.debug_varnames) }');
  979. Add(' /* DWARF 3 */');
  980. Add(' .debug_pubtypes 0 : { *(.debug_pubtypes) }');
  981. Add(' .debug_ranges 0 : { *(.debug_ranges) }');
  982. Add(' .gnu.attributes 0 : { KEEP (*(.gnu.attributes)) }');
  983. Add(' .gptab.sdata : { *(.gptab.data) *(.gptab.sdata) }');
  984. Add(' .gptab.sbss : { *(.gptab.bss) *(.gptab.sbss) }');
  985. Add(' .mdebug.abi32 : { KEEP(*(.mdebug.abi32)) }');
  986. Add(' .mdebug.abiN32 : { KEEP(*(.mdebug.abiN32)) }');
  987. Add(' .mdebug.abi64 : { KEEP(*(.mdebug.abi64)) }');
  988. Add(' .mdebug.abiO64 : { KEEP(*(.mdebug.abiO64)) }');
  989. Add(' .mdebug.eabi32 : { KEEP(*(.mdebug.eabi32)) }');
  990. Add(' .mdebug.eabi64 : { KEEP(*(.mdebug.eabi64)) }');
  991. Add(' /DISCARD/ : { *(.rel.dyn) }');
  992. Add(' /DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) *(.gnu.lto_*) }');
  993. Add('}');
  994. Add('_end = .;');
  995. end;
  996. {$endif MIPSEL}
  997. { Write and Close response }
  998. linkres.writetodisk;
  999. linkres.free;
  1000. WriteResponseFile:=True;
  1001. end;
  1002. function TlinkerEmbedded.MakeExecutable:boolean;
  1003. var
  1004. binstr,
  1005. cmdstr : TCmdStr;
  1006. success : boolean;
  1007. StaticStr,
  1008. GCSectionsStr,
  1009. DynLinkStr,
  1010. StripStr: string;
  1011. begin
  1012. { for future use }
  1013. StaticStr:='';
  1014. StripStr:='';
  1015. DynLinkStr:='';
  1016. GCSectionsStr:='--gc-sections';
  1017. //if not(cs_link_extern in current_settings.globalswitches) then
  1018. if not(cs_link_nolink in current_settings.globalswitches) then
  1019. Message1(exec_i_linking,current_module.exefilename);
  1020. { Write used files and libraries }
  1021. WriteResponseFile();
  1022. { Call linker }
  1023. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  1024. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  1025. if not(cs_link_on_target in current_settings.globalswitches) then
  1026. begin
  1027. Replace(cmdstr,'$EXE',(maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename,'.elf')))));
  1028. Replace(cmdstr,'$RES',(maybequoted(ScriptFixFileName(outputexedir+Info.ResName))));
  1029. Replace(cmdstr,'$STATIC',StaticStr);
  1030. Replace(cmdstr,'$STRIP',StripStr);
  1031. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  1032. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  1033. end
  1034. else
  1035. begin
  1036. Replace(cmdstr,'$EXE',maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename,'.elf'))));
  1037. Replace(cmdstr,'$RES',maybequoted(ScriptFixFileName(outputexedir+Info.ResName)));
  1038. Replace(cmdstr,'$STATIC',StaticStr);
  1039. Replace(cmdstr,'$STRIP',StripStr);
  1040. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  1041. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  1042. end;
  1043. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  1044. { Remove ReponseFile }
  1045. if success and not(cs_link_nolink in current_settings.globalswitches) then
  1046. DeleteFile(outputexedir+Info.ResName);
  1047. { Post process }
  1048. if success and not(cs_link_nolink in current_settings.globalswitches) then
  1049. success:=PostProcessExecutable(current_module.exefilename+'.elf',false);
  1050. if success and (target_info.system in [system_arm_embedded,system_avr_embedded,system_mipsel_embedded]) then
  1051. begin
  1052. success:=DoExec(FindUtil(utilsprefix+'objcopy'),'-O ihex '+
  1053. ChangeFileExt(current_module.exefilename,'.elf')+' '+
  1054. ChangeFileExt(current_module.exefilename,'.hex'),true,false);
  1055. if success then
  1056. success:=DoExec(FindUtil(utilsprefix+'objcopy'),'-O binary '+
  1057. ChangeFileExt(current_module.exefilename,'.elf')+' '+
  1058. ChangeFileExt(current_module.exefilename,'.bin'),true,false);
  1059. end;
  1060. MakeExecutable:=success; { otherwise a recursive call to link method }
  1061. end;
  1062. function TLinkerEmbedded.postprocessexecutable(const fn : string;isdll:boolean):boolean;
  1063. type
  1064. TElf32header=packed record
  1065. magic0123 : longint;
  1066. file_class : byte;
  1067. data_encoding : byte;
  1068. file_version : byte;
  1069. padding : array[$07..$0f] of byte;
  1070. e_type : word;
  1071. e_machine : word;
  1072. e_version : longint;
  1073. e_entry : longint; { entrypoint }
  1074. e_phoff : longint; { program header offset }
  1075. e_shoff : longint; { sections header offset }
  1076. e_flags : longint;
  1077. e_ehsize : word; { elf header size in bytes }
  1078. e_phentsize : word; { size of an entry in the program header array }
  1079. e_phnum : word; { 0..e_phnum-1 of entrys }
  1080. e_shentsize : word; { size of an entry in sections header array }
  1081. e_shnum : word; { 0..e_shnum-1 of entrys }
  1082. e_shstrndx : word; { index of string section header }
  1083. end;
  1084. TElf32sechdr=packed record
  1085. sh_name : longint;
  1086. sh_type : longint;
  1087. sh_flags : longint;
  1088. sh_addr : longint;
  1089. sh_offset : longint;
  1090. sh_size : longint;
  1091. sh_link : longint;
  1092. sh_info : longint;
  1093. sh_addralign : longint;
  1094. sh_entsize : longint;
  1095. end;
  1096. function MayBeSwapHeader(h : telf32header) : telf32header;
  1097. begin
  1098. result:=h;
  1099. if source_info.endian<>target_info.endian then
  1100. with h do
  1101. begin
  1102. result.e_type:=swapendian(e_type);
  1103. result.e_machine:=swapendian(e_machine);
  1104. result.e_version:=swapendian(e_version);
  1105. result.e_entry:=swapendian(e_entry);
  1106. result.e_phoff:=swapendian(e_phoff);
  1107. result.e_shoff:=swapendian(e_shoff);
  1108. result.e_flags:=swapendian(e_flags);
  1109. result.e_ehsize:=swapendian(e_ehsize);
  1110. result.e_phentsize:=swapendian(e_phentsize);
  1111. result.e_phnum:=swapendian(e_phnum);
  1112. result.e_shentsize:=swapendian(e_shentsize);
  1113. result.e_shnum:=swapendian(e_shnum);
  1114. result.e_shstrndx:=swapendian(e_shstrndx);
  1115. end;
  1116. end;
  1117. function MaybeSwapSecHeader(h : telf32sechdr) : telf32sechdr;
  1118. begin
  1119. result:=h;
  1120. if source_info.endian<>target_info.endian then
  1121. with h do
  1122. begin
  1123. result.sh_name:=swapendian(sh_name);
  1124. result.sh_type:=swapendian(sh_type);
  1125. result.sh_flags:=swapendian(sh_flags);
  1126. result.sh_addr:=swapendian(sh_addr);
  1127. result.sh_offset:=swapendian(sh_offset);
  1128. result.sh_size:=swapendian(sh_size);
  1129. result.sh_link:=swapendian(sh_link);
  1130. result.sh_info:=swapendian(sh_info);
  1131. result.sh_addralign:=swapendian(sh_addralign);
  1132. result.sh_entsize:=swapendian(sh_entsize);
  1133. end;
  1134. end;
  1135. var
  1136. f : file;
  1137. function ReadSectionName(pos : longint) : String;
  1138. var
  1139. oldpos : longint;
  1140. c : char;
  1141. begin
  1142. oldpos:=filepos(f);
  1143. seek(f,pos);
  1144. Result:='';
  1145. while true do
  1146. begin
  1147. blockread(f,c,1);
  1148. if c=#0 then
  1149. break;
  1150. Result:=Result+c;
  1151. end;
  1152. seek(f,oldpos);
  1153. end;
  1154. var
  1155. elfheader : TElf32header;
  1156. secheader : TElf32sechdr;
  1157. i : longint;
  1158. stringoffset : longint;
  1159. secname : string;
  1160. begin
  1161. postprocessexecutable:=false;
  1162. { open file }
  1163. assign(f,fn);
  1164. {$push}{$I-}
  1165. reset(f,1);
  1166. if ioresult<>0 then
  1167. Message1(execinfo_f_cant_open_executable,fn);
  1168. { read header }
  1169. blockread(f,elfheader,sizeof(tElf32header));
  1170. elfheader:=MayBeSwapHeader(elfheader);
  1171. seek(f,elfheader.e_shoff);
  1172. { read string section header }
  1173. seek(f,elfheader.e_shoff+sizeof(TElf32sechdr)*elfheader.e_shstrndx);
  1174. blockread(f,secheader,sizeof(secheader));
  1175. secheader:=MaybeSwapSecHeader(secheader);
  1176. stringoffset:=secheader.sh_offset;
  1177. seek(f,elfheader.e_shoff);
  1178. status.datasize:=0;
  1179. for i:=0 to elfheader.e_shnum-1 do
  1180. begin
  1181. blockread(f,secheader,sizeof(secheader));
  1182. secheader:=MaybeSwapSecHeader(secheader);
  1183. secname:=ReadSectionName(stringoffset+secheader.sh_name);
  1184. if secname='.text' then
  1185. begin
  1186. Message1(execinfo_x_codesize,tostr(secheader.sh_size));
  1187. status.codesize:=secheader.sh_size;
  1188. end
  1189. else if secname='.data' then
  1190. begin
  1191. Message1(execinfo_x_initdatasize,tostr(secheader.sh_size));
  1192. inc(status.datasize,secheader.sh_size);
  1193. end
  1194. else if secname='.bss' then
  1195. begin
  1196. Message1(execinfo_x_uninitdatasize,tostr(secheader.sh_size));
  1197. inc(status.datasize,secheader.sh_size);
  1198. end;
  1199. end;
  1200. close(f);
  1201. {$pop}
  1202. if ioresult<>0 then
  1203. ;
  1204. postprocessexecutable:=true;
  1205. end;
  1206. {*****************************************************************************
  1207. Initialize
  1208. *****************************************************************************}
  1209. initialization
  1210. {$ifdef arm}
  1211. RegisterLinker(ld_embedded,TLinkerEmbedded);
  1212. RegisterTarget(system_arm_embedded_info);
  1213. {$endif arm}
  1214. {$ifdef avr}
  1215. RegisterLinker(ld_embedded,TLinkerEmbedded);
  1216. RegisterTarget(system_avr_embedded_info);
  1217. {$endif avr}
  1218. {$ifdef i386}
  1219. RegisterLinker(ld_embedded,TLinkerEmbedded);
  1220. RegisterTarget(system_i386_embedded_info);
  1221. {$endif i386}
  1222. {$ifdef mipsel}
  1223. RegisterLinker(ld_embedded,TLinkerEmbedded);
  1224. RegisterTarget(system_mipsel_embedded_info);
  1225. {$endif mipsel}
  1226. end.