t_embed.pas 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518
  1. {
  2. Copyright (c) 2005-2017 by Free Pascal Compiler team
  3. This unit implements support import, export, link routines
  4. for the Embedded Target
  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,cscript,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)}
  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(x86_64) 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_stm32f401cb,
  374. ct_stm32f401rb,
  375. ct_stm32f401vb,
  376. ct_stm32f401cc,
  377. ct_stm32f401rc,
  378. ct_stm32f401vc,
  379. ct_discoveryf401vc,
  380. ct_stm32f401cd,
  381. ct_stm32f401rd,
  382. ct_stm32f401vd,
  383. ct_stm32f401ce,
  384. ct_stm32f401re,
  385. ct_nucleof401re,
  386. ct_stm32f401ve,
  387. ct_stm32f407vg,
  388. ct_discoveryf407vg,
  389. ct_stm32f407ig,
  390. ct_stm32f407zg,
  391. ct_stm32f407ve,
  392. ct_stm32f407ze,
  393. ct_stm32f407ie,
  394. ct_stm32f411cc,
  395. ct_stm32f411rc,
  396. ct_stm32f411vc,
  397. ct_stm32f411ce,
  398. ct_stm32f411re,
  399. ct_nucleof411re,
  400. ct_stm32f411ve,
  401. ct_discoveryf411ve,
  402. ct_stm32f429vg,
  403. ct_stm32f429zg,
  404. ct_stm32f429ig,
  405. ct_stm32f429vi,
  406. ct_stm32f429zi,
  407. ct_discoveryf429zi,
  408. ct_stm32f429ii,
  409. ct_stm32f429ve,
  410. ct_stm32f429ze,
  411. ct_stm32f429ie,
  412. ct_stm32f429bg,
  413. ct_stm32f429bi,
  414. ct_stm32f429be,
  415. ct_stm32f429ng,
  416. ct_stm32f429ni,
  417. ct_stm32f429ne,
  418. ct_stm32f446mc,
  419. ct_stm32f446rc,
  420. ct_stm32f446vc,
  421. ct_stm32f446zc,
  422. ct_stm32f446me,
  423. ct_stm32f446re,
  424. ct_nucleof446re,
  425. ct_stm32f446ve,
  426. ct_stm32f446ze,
  427. ct_stm32f745xe,
  428. ct_stm32f745xg,
  429. ct_stm32f746xe,
  430. ct_stm32f746xg,
  431. ct_stm32f756xe,
  432. ct_stm32f756xg,
  433. { TI - 64 K Flash, 16 K SRAM Devices }
  434. ct_lm3s1110,
  435. ct_lm3s1133,
  436. ct_lm3s1138,
  437. ct_lm3s1150,
  438. ct_lm3s1162,
  439. ct_lm3s1165,
  440. ct_lm3s1166,
  441. ct_lm3s2110,
  442. ct_lm3s2139,
  443. ct_lm3s6100,
  444. ct_lm3s6110,
  445. { TI 128 K Flash, 32 K SRAM devices - Fury Class }
  446. ct_lm3s1601,
  447. ct_lm3s1608,
  448. ct_lm3s1620,
  449. ct_lm3s1635,
  450. ct_lm3s1636,
  451. ct_lm3s1637,
  452. ct_lm3s1651,
  453. ct_lm3s2601,
  454. ct_lm3s2608,
  455. ct_lm3s2620,
  456. ct_lm3s2637,
  457. ct_lm3s2651,
  458. ct_lm3s6610,
  459. ct_lm3s6611,
  460. ct_lm3s6618,
  461. ct_lm3s6633,
  462. ct_lm3s6637,
  463. ct_lm3s8630,
  464. { TI 256 K Flase, 32 K SRAM devices - Fury Class }
  465. ct_lm3s1911,
  466. ct_lm3s1918,
  467. ct_lm3s1937,
  468. ct_lm3s1958,
  469. ct_lm3s1960,
  470. ct_lm3s1968,
  471. ct_lm3s1969,
  472. ct_lm3s2911,
  473. ct_lm3s2918,
  474. ct_lm3s2919,
  475. ct_lm3s2939,
  476. ct_lm3s2948,
  477. ct_lm3s2950,
  478. ct_lm3s2965,
  479. ct_lm3s6911,
  480. ct_lm3s6918,
  481. ct_lm3s6938,
  482. ct_lm3s6950,
  483. ct_lm3s6952,
  484. ct_lm3s6965,
  485. ct_lm3s8930,
  486. ct_lm3s8933,
  487. ct_lm3s8938,
  488. ct_lm3s8962,
  489. ct_lm3s8970,
  490. ct_lm3s8971,
  491. { TI - Tempest Tempest - 256 K Flash, 64 K SRAM }
  492. ct_lm3s5951,
  493. ct_lm3s5956,
  494. ct_lm3s1b21,
  495. ct_lm3s2b93,
  496. ct_lm3s5b91,
  497. ct_lm3s9b81,
  498. ct_lm3s9b90,
  499. ct_lm3s9b92,
  500. ct_lm3s9b95,
  501. ct_lm3s9b96,
  502. ct_lm3s5d51,
  503. { TI - Stellaris something }
  504. ct_lm4f120h5,
  505. { Infineon }
  506. ct_xmc4500x1024,
  507. ct_xmc4500x768,
  508. ct_xmc4502x768,
  509. ct_xmc4504x512,
  510. { Allwinner }
  511. ct_allwinner_a20,
  512. { Freescale }
  513. ct_mk20dx128vfm5,
  514. ct_mk20dx128vft5,
  515. ct_mk20dx128vlf5,
  516. ct_mk20dx128vlh5,
  517. ct_teensy30,
  518. ct_mk20dx128vmp5,
  519. ct_mk20dx32vfm5,
  520. ct_mk20dx32vft5,
  521. ct_mk20dx32vlf5,
  522. ct_mk20dx32vlh5,
  523. ct_mk20dx32vmp5,
  524. ct_mk20dx64vfm5,
  525. ct_mk20dx64vft5,
  526. ct_mk20dx64vlf5,
  527. ct_mk20dx64vlh5,
  528. ct_mk20dx64vmp5,
  529. ct_mk20dx128vlh7,
  530. ct_mk20dx128vlk7,
  531. ct_mk20dx128vll7,
  532. ct_mk20dx128vmc7,
  533. ct_mk20dx256vlh7,
  534. ct_mk20dx256vlk7,
  535. ct_mk20dx256vll7,
  536. ct_mk20dx256vmc7,
  537. ct_teensy31,
  538. ct_teensy32,
  539. ct_mk20dx64vlh7,
  540. ct_mk20dx64vlk7,
  541. ct_mk20dx64vmc7,
  542. ct_mk22fn512cap12,
  543. ct_mk22fn512cbp12,
  544. ct_mk22fn512vdc12,
  545. ct_mk22fn512vlh12,
  546. ct_mk22fn512vll12,
  547. ct_mk22fn512vmp12,
  548. ct_freedom_k22f,
  549. { Atmel }
  550. ct_sam3x8e,
  551. ct_arduino_due,
  552. ct_flip_n_click,
  553. { Nordic Semiconductor }
  554. ct_nrf51422_xxaa,
  555. ct_nrf51422_xxab,
  556. ct_nrf51422_xxac,
  557. ct_nrf51822_xxaa,
  558. ct_nrf51822_xxab,
  559. ct_nrf51822_xxac,
  560. ct_nrf52832_xxaa,
  561. ct_nrf52840_xxaa,
  562. ct_sc32442b,
  563. ct_thumb2bare:
  564. begin
  565. with embedded_controllers[current_settings.controllertype] do
  566. with linkres do
  567. begin
  568. if (embedded_controllers[current_settings.controllertype].controllerunitstr='MK20D5')
  569. or (embedded_controllers[current_settings.controllertype].controllerunitstr='MK20D7')
  570. or (embedded_controllers[current_settings.controllertype].controllerunitstr='MK22F51212')
  571. or (embedded_controllers[current_settings.controllertype].controllerunitstr='MK64F12') then
  572. Add('ENTRY(_LOWLEVELSTART)')
  573. else
  574. Add('ENTRY(_START)');
  575. Add('MEMORY');
  576. Add('{');
  577. if flashsize<>0 then
  578. begin
  579. LinkStr := ' flash : ORIGIN = 0x' + IntToHex(flashbase,8)
  580. + ', LENGTH = 0x' + IntToHex(flashsize,8);
  581. Add(LinkStr);
  582. end;
  583. LinkStr := ' ram : ORIGIN = 0x' + IntToHex(srambase,8)
  584. + ', LENGTH = 0x' + IntToHex(sramsize,8);
  585. Add(LinkStr);
  586. Add('}');
  587. Add('_stack_top = 0x' + IntToHex(sramsize+srambase,8) + ';');
  588. // Add Checksum Calculation for LPC Controllers so that the bootloader starts the uploaded binary
  589. writeln(controllerunitstr);
  590. if (controllerunitstr = 'LPC8xx') or (controllerunitstr = 'LPC11XX') or (controllerunitstr = 'LPC122X') then
  591. Add('Startup_Checksum = 0 - (_stack_top + _START + 1 + NonMaskableInt_interrupt + 1 + Hardfault_interrupt + 1);');
  592. if (controllerunitstr = 'LPC13XX') then
  593. Add('Startup_Checksum = 0 - (_stack_top + _START + 1 + NonMaskableInt_interrupt + 1 + MemoryManagement_interrupt + 1 + BusFault_interrupt + 1 + UsageFault_interrupt + 1);');
  594. end;
  595. end
  596. else
  597. if not (cs_link_nolink in current_settings.globalswitches) then
  598. internalerror(200902011);
  599. end;
  600. with linkres do
  601. begin
  602. Add('SECTIONS');
  603. Add('{');
  604. Add(' .text :');
  605. Add(' {');
  606. Add(' _text_start = .;');
  607. Add(' KEEP(*(.init .init.*))');
  608. if (embedded_controllers[current_settings.controllertype].controllerunitstr='MK20D5')
  609. or (embedded_controllers[current_settings.controllertype].controllerunitstr='MK20D7')
  610. or (embedded_controllers[current_settings.controllertype].controllerunitstr='MK22F51212')
  611. or (embedded_controllers[current_settings.controllertype].controllerunitstr='MK64F12') then
  612. begin
  613. Add(' . = 0x400;');
  614. Add(' KEEP(*(.flash_config *.flash_config.*))');
  615. end;
  616. Add(' *(.text .text.*)');
  617. Add(' *(.strings)');
  618. Add(' *(.rodata .rodata.*)');
  619. Add(' *(.comment)');
  620. Add(' . = ALIGN(4);');
  621. Add(' _etext = .;');
  622. if embedded_controllers[current_settings.controllertype].flashsize<>0 then
  623. begin
  624. Add(' } >flash');
  625. Add(' .note.gnu.build-id : { *(.note.gnu.build-id) } >flash ');
  626. end
  627. else
  628. begin
  629. Add(' } >ram');
  630. Add(' .note.gnu.build-id : { *(.note.gnu.build-id) } >ram ');
  631. end;
  632. Add(' .data :');
  633. Add(' {');
  634. Add(' _data = .;');
  635. Add(' *(.data .data.*)');
  636. Add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  637. Add(' _edata = .;');
  638. if embedded_controllers[current_settings.controllertype].flashsize<>0 then
  639. begin
  640. Add(' } >ram AT >flash');
  641. end
  642. else
  643. begin
  644. Add(' } >ram');
  645. end;
  646. Add(' .bss :');
  647. Add(' {');
  648. Add(' _bss_start = .;');
  649. Add(' *(.bss .bss.*)');
  650. Add(' *(COMMON)');
  651. Add(' } >ram');
  652. Add('. = ALIGN(4);');
  653. Add('_bss_end = . ;');
  654. Add('}');
  655. Add('_end = .;');
  656. end;
  657. {$endif ARM}
  658. {$ifdef i386}
  659. with linkres do
  660. begin
  661. Add('ENTRY(_START)');
  662. Add('SECTIONS');
  663. Add('{');
  664. Add(' . = 0x100000;');
  665. Add(' .text ALIGN (0x1000) :');
  666. Add(' {');
  667. Add(' _text = .;');
  668. Add(' KEEP(*(.init .init.*))');
  669. Add(' *(.text .text.*)');
  670. Add(' *(.strings)');
  671. Add(' *(.rodata .rodata.*)');
  672. Add(' *(.comment)');
  673. Add(' _etext = .;');
  674. Add(' }');
  675. Add(' .data ALIGN (0x1000) :');
  676. Add(' {');
  677. Add(' _data = .;');
  678. Add(' *(.data .data.*)');
  679. Add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  680. Add(' _edata = .;');
  681. Add(' }');
  682. Add(' . = ALIGN(4);');
  683. Add(' .bss :');
  684. Add(' {');
  685. Add(' _bss_start = .;');
  686. Add(' *(.bss .bss.*)');
  687. Add(' *(COMMON)');
  688. Add(' }');
  689. Add('_bss_end = . ;');
  690. Add('}');
  691. Add('_end = .;');
  692. end;
  693. {$endif i386}
  694. {$ifdef x86_64}
  695. with linkres do
  696. begin
  697. Add('ENTRY(_START)');
  698. Add('SECTIONS');
  699. Add('{');
  700. Add(' . = 0x100000;');
  701. Add(' .text ALIGN (0x1000) :');
  702. Add(' {');
  703. Add(' _text = .;');
  704. Add(' KEEP(*(.init .init.*))');
  705. Add(' *(.text .text.*)');
  706. Add(' *(.strings)');
  707. Add(' *(.rodata .rodata.*)');
  708. Add(' *(.comment)');
  709. Add(' _etext = .;');
  710. Add(' }');
  711. Add(' .data ALIGN (0x1000) :');
  712. Add(' {');
  713. Add(' _data = .;');
  714. Add(' *(.data .data.*)');
  715. Add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  716. Add(' _edata = .;');
  717. Add(' }');
  718. Add(' . = ALIGN(4);');
  719. Add(' .bss :');
  720. Add(' {');
  721. Add(' _bss_start = .;');
  722. Add(' *(.bss .bss.*)');
  723. Add(' *(COMMON)');
  724. Add(' }');
  725. Add('_bss_end = . ;');
  726. Add('}');
  727. Add('_end = .;');
  728. end;
  729. {$endif x86_64}
  730. {$ifdef AVR}
  731. with linkres do
  732. begin
  733. { linker script from ld 2.19 }
  734. Add('ENTRY(_START)');
  735. Add('OUTPUT_FORMAT("elf32-avr","elf32-avr","elf32-avr")');
  736. case current_settings.cputype of
  737. cpu_avr1:
  738. Add('OUTPUT_ARCH(avr:1)');
  739. cpu_avr2:
  740. Add('OUTPUT_ARCH(avr:2)');
  741. cpu_avr25:
  742. Add('OUTPUT_ARCH(avr:25)');
  743. cpu_avr3:
  744. Add('OUTPUT_ARCH(avr:3)');
  745. cpu_avr31:
  746. Add('OUTPUT_ARCH(avr:31)');
  747. cpu_avr35:
  748. Add('OUTPUT_ARCH(avr:35)');
  749. cpu_avr4:
  750. Add('OUTPUT_ARCH(avr:4)');
  751. cpu_avr5:
  752. Add('OUTPUT_ARCH(avr:5)');
  753. cpu_avr51:
  754. Add('OUTPUT_ARCH(avr:51)');
  755. cpu_avr6:
  756. Add('OUTPUT_ARCH(avr:6)');
  757. else
  758. Internalerror(2015072701);
  759. end;
  760. Add('MEMORY');
  761. with embedded_controllers[current_settings.controllertype] do
  762. begin
  763. Add('{');
  764. Add(' text (rx) : ORIGIN = 0, LENGTH = 0x'+IntToHex(flashsize,6));
  765. Add(' data (rw!x) : ORIGIN = 0x'+IntToHex($800000+srambase,6)+', LENGTH = 0x'+IntToHex(sramsize,6));
  766. Add(' eeprom (rw!x) : ORIGIN = 0x810000, LENGTH = 0x'+IntToHex(eepromsize,6));
  767. Add(' fuse (rw!x) : ORIGIN = 0x820000, LENGTH = 1K');
  768. Add(' lock (rw!x) : ORIGIN = 0x830000, LENGTH = 1K');
  769. Add(' signature (rw!x) : ORIGIN = 0x840000, LENGTH = 1K');
  770. Add('}');
  771. Add('_stack_top = 0x' + IntToHex(srambase+sramsize-1,4) + ';');
  772. end;
  773. Add('SECTIONS');
  774. Add('{');
  775. Add(' /* Read-only sections, merged into text segment: */');
  776. Add(' .hash : { *(.hash) }');
  777. Add(' .dynsym : { *(.dynsym) }');
  778. Add(' .dynstr : { *(.dynstr) }');
  779. Add(' .gnu.version : { *(.gnu.version) }');
  780. Add(' .gnu.version_d : { *(.gnu.version_d) }');
  781. Add(' .gnu.version_r : { *(.gnu.version_r) }');
  782. Add(' .rel.init : { *(.rel.init) }');
  783. Add(' .rela.init : { *(.rela.init) }');
  784. Add(' .rel.text :');
  785. Add(' {');
  786. Add(' *(.rel.text)');
  787. Add(' *(.rel.text.*)');
  788. Add(' *(.rel.gnu.linkonce.t*)');
  789. Add(' }');
  790. Add(' .rela.text :');
  791. Add(' {');
  792. Add(' *(.rela.text)');
  793. Add(' *(.rela.text.*)');
  794. Add(' *(.rela.gnu.linkonce.t*)');
  795. Add(' }');
  796. Add(' .rel.fini : { *(.rel.fini) }');
  797. Add(' .rela.fini : { *(.rela.fini) }');
  798. Add(' .rel.rodata :');
  799. Add(' {');
  800. Add(' *(.rel.rodata)');
  801. Add(' *(.rel.rodata.*)');
  802. Add(' *(.rel.gnu.linkonce.r*)');
  803. Add(' }');
  804. Add(' .rela.rodata :');
  805. Add(' {');
  806. Add(' *(.rela.rodata)');
  807. Add(' *(.rela.rodata.*)');
  808. Add(' *(.rela.gnu.linkonce.r*)');
  809. Add(' }');
  810. Add(' .rel.data :');
  811. Add(' {');
  812. Add(' *(.rel.data)');
  813. Add(' *(.rel.data.*)');
  814. Add(' *(.rel.gnu.linkonce.d*)');
  815. Add(' }');
  816. Add(' .rela.data :');
  817. Add(' {');
  818. Add(' *(.rela.data)');
  819. Add(' *(.rela.data.*)');
  820. Add(' *(.rela.gnu.linkonce.d*)');
  821. Add(' }');
  822. Add(' .rel.ctors : { *(.rel.ctors) }');
  823. Add(' .rela.ctors : { *(.rela.ctors) }');
  824. Add(' .rel.dtors : { *(.rel.dtors) }');
  825. Add(' .rela.dtors : { *(.rela.dtors) }');
  826. Add(' .rel.got : { *(.rel.got) }');
  827. Add(' .rela.got : { *(.rela.got) }');
  828. Add(' .rel.bss : { *(.rel.bss) }');
  829. Add(' .rela.bss : { *(.rela.bss) }');
  830. Add(' .rel.plt : { *(.rel.plt) }');
  831. Add(' .rela.plt : { *(.rela.plt) }');
  832. Add(' /* Internal text space or external memory. */');
  833. Add(' .text :');
  834. Add(' {');
  835. Add(' KEEP(*(.init .init.*))');
  836. Add(' /* For data that needs to reside in the lower 64k of progmem. */');
  837. Add(' *(.progmem.gcc*)');
  838. Add(' *(.progmem*)');
  839. Add(' . = ALIGN(2);');
  840. Add(' __trampolines_start = . ;');
  841. Add(' /* The jump trampolines for the 16-bit limited relocs will reside here. */');
  842. Add(' *(.trampolines)');
  843. Add(' *(.trampolines*)');
  844. Add(' __trampolines_end = . ;');
  845. Add(' /* For future tablejump instruction arrays for 3 byte pc devices.');
  846. Add(' We don''t relax jump/call instructions within these sections. */');
  847. Add(' *(.jumptables)');
  848. Add(' *(.jumptables*)');
  849. Add(' /* For code that needs to reside in the lower 128k progmem. */');
  850. Add(' *(.lowtext)');
  851. Add(' *(.lowtext*)');
  852. Add(' __ctors_start = . ;');
  853. Add(' *(.ctors)');
  854. Add(' __ctors_end = . ;');
  855. Add(' __dtors_start = . ;');
  856. Add(' *(.dtors)');
  857. Add(' __dtors_end = . ;');
  858. Add(' KEEP(SORT(*)(.ctors))');
  859. Add(' KEEP(SORT(*)(.dtors))');
  860. Add(' /* From this point on, we don''t bother about wether the insns are');
  861. Add(' below or above the 16 bits boundary. */');
  862. Add(' *(.init0) /* Start here after reset. */');
  863. Add(' KEEP (*(.init0))');
  864. Add(' *(.init1)');
  865. Add(' KEEP (*(.init1))');
  866. Add(' *(.init2) /* Clear __zero_reg__, set up stack pointer. */');
  867. Add(' KEEP (*(.init2))');
  868. Add(' *(.init3)');
  869. Add(' KEEP (*(.init3))');
  870. Add(' *(.init4) /* Initialize data and BSS. */');
  871. Add(' KEEP (*(.init4))');
  872. Add(' *(.init5)');
  873. Add(' KEEP (*(.init5))');
  874. Add(' *(.init6) /* C++ constructors. */');
  875. Add(' KEEP (*(.init6))');
  876. Add(' *(.init7)');
  877. Add(' KEEP (*(.init7))');
  878. Add(' *(.init8)');
  879. Add(' KEEP (*(.init8))');
  880. Add(' *(.init9) /* Call main(). */');
  881. Add(' KEEP (*(.init9))');
  882. Add(' *(.text)');
  883. Add(' . = ALIGN(2);');
  884. Add(' *(.text.*)');
  885. Add(' . = ALIGN(2);');
  886. Add(' *(.fini9) /* _exit() starts here. */');
  887. Add(' KEEP (*(.fini9))');
  888. Add(' *(.fini8)');
  889. Add(' KEEP (*(.fini8))');
  890. Add(' *(.fini7)');
  891. Add(' KEEP (*(.fini7))');
  892. Add(' *(.fini6) /* C++ destructors. */');
  893. Add(' KEEP (*(.fini6))');
  894. Add(' *(.fini5)');
  895. Add(' KEEP (*(.fini5))');
  896. Add(' *(.fini4)');
  897. Add(' KEEP (*(.fini4))');
  898. Add(' *(.fini3)');
  899. Add(' KEEP (*(.fini3))');
  900. Add(' *(.fini2)');
  901. Add(' KEEP (*(.fini2))');
  902. Add(' *(.fini1)');
  903. Add(' KEEP (*(.fini1))');
  904. Add(' *(.fini0) /* Infinite loop after program termination. */');
  905. Add(' KEEP (*(.fini0))');
  906. Add(' _etext = . ;');
  907. Add(' } > text');
  908. Add(' .data : AT (ADDR (.text) + SIZEOF (.text))');
  909. Add(' {');
  910. Add(' PROVIDE (__data_start = .) ;');
  911. Add(' *(.data)');
  912. Add(' *(.data*)');
  913. Add(' *(.rodata) /* We need to include .rodata here if gcc is used */');
  914. Add(' *(.rodata*) /* with -fdata-sections. */');
  915. Add(' *(.gnu.linkonce.d*)');
  916. Add(' . = ALIGN(2);');
  917. Add(' _edata = . ;');
  918. Add(' PROVIDE (__data_end = .) ;');
  919. Add(' } > data');
  920. Add(' .bss : AT (ADDR (.bss))');
  921. Add(' {');
  922. Add(' PROVIDE (__bss_start = .) ;');
  923. Add(' *(.bss)');
  924. Add(' *(.bss*)');
  925. Add(' *(COMMON)');
  926. Add(' PROVIDE (__bss_end = .) ;');
  927. Add(' } > data');
  928. Add(' __data_load_start = LOADADDR(.data);');
  929. Add(' __data_load_end = __data_load_start + SIZEOF(.data);');
  930. Add(' /* Global data not cleared after reset. */');
  931. Add(' .noinit :');
  932. Add(' {');
  933. Add(' PROVIDE (__noinit_start = .) ;');
  934. Add(' *(.noinit*)');
  935. Add(' PROVIDE (__noinit_end = .) ;');
  936. Add(' _end = . ;');
  937. Add(' PROVIDE (__heap_start = .) ;');
  938. Add(' } > data');
  939. Add(' .eeprom :');
  940. Add(' {');
  941. Add(' *(.eeprom*)');
  942. Add(' __eeprom_end = . ;');
  943. Add(' } > eeprom');
  944. Add(' .fuse :');
  945. Add(' {');
  946. Add(' KEEP(*(.fuse))');
  947. Add(' KEEP(*(.lfuse))');
  948. Add(' KEEP(*(.hfuse))');
  949. Add(' KEEP(*(.efuse))');
  950. Add(' } > fuse');
  951. Add(' .lock :');
  952. Add(' {');
  953. Add(' KEEP(*(.lock*))');
  954. Add(' } > lock');
  955. Add(' .signature :');
  956. Add(' {');
  957. Add(' KEEP(*(.signature*))');
  958. Add(' } > signature');
  959. Add(' /* Stabs debugging sections. */');
  960. Add(' .stab 0 : { *(.stab) }');
  961. Add(' .stabstr 0 : { *(.stabstr) }');
  962. Add(' .stab.excl 0 : { *(.stab.excl) }');
  963. Add(' .stab.exclstr 0 : { *(.stab.exclstr) }');
  964. Add(' .stab.index 0 : { *(.stab.index) }');
  965. Add(' .stab.indexstr 0 : { *(.stab.indexstr) }');
  966. Add(' .comment 0 : { *(.comment) }');
  967. Add(' /* DWARF debug sections.');
  968. Add(' Symbols in the DWARF debugging sections are relative to the beginning');
  969. Add(' of the section so we begin them at 0. */');
  970. Add(' /* DWARF 1 */');
  971. Add(' .debug 0 : { *(.debug) }');
  972. Add(' .line 0 : { *(.line) }');
  973. Add(' /* GNU DWARF 1 extensions */');
  974. Add(' .debug_srcinfo 0 : { *(.debug_srcinfo) }');
  975. Add(' .debug_sfnames 0 : { *(.debug_sfnames) }');
  976. Add(' /* DWARF 1.1 and DWARF 2 */');
  977. Add(' .debug_aranges 0 : { *(.debug_aranges) }');
  978. Add(' .debug_pubnames 0 : { *(.debug_pubnames) }');
  979. Add(' /* DWARF 2 */');
  980. Add(' .debug_info 0 : { *(.debug_info) *(.gnu.linkonce.wi.*) }');
  981. Add(' .debug_abbrev 0 : { *(.debug_abbrev) }');
  982. Add(' .debug_line 0 : { *(.debug_line) }');
  983. Add(' .debug_frame 0 : { *(.debug_frame) }');
  984. Add(' .debug_str 0 : { *(.debug_str) }');
  985. Add(' .debug_loc 0 : { *(.debug_loc) }');
  986. Add(' .debug_macinfo 0 : { *(.debug_macinfo) }');
  987. Add('}');
  988. end;
  989. {$endif AVR}
  990. {$ifdef MIPSEL}
  991. case current_settings.controllertype of
  992. ct_none:
  993. begin
  994. end;
  995. ct_pic32mx110f016b,
  996. ct_pic32mx110f016c,
  997. ct_pic32mx110f016d,
  998. ct_pic32mx120f032b,
  999. ct_pic32mx120f032c,
  1000. ct_pic32mx120f032d,
  1001. ct_pic32mx130f064b,
  1002. ct_pic32mx130f064c,
  1003. ct_pic32mx130f064d,
  1004. ct_pic32mx150f128b,
  1005. ct_pic32mx150f128c,
  1006. ct_pic32mx150f128d,
  1007. ct_pic32mx210f016b,
  1008. ct_pic32mx210f016c,
  1009. ct_pic32mx210f016d,
  1010. ct_pic32mx220f032b,
  1011. ct_pic32mx220f032c,
  1012. ct_pic32mx220f032d,
  1013. ct_pic32mx230f064b,
  1014. ct_pic32mx230f064c,
  1015. ct_pic32mx230f064d,
  1016. ct_pic32mx250f128b,
  1017. ct_pic32mx250f128c,
  1018. ct_pic32mx250f128d,
  1019. ct_pic32mx775f256h,
  1020. ct_pic32mx775f256l,
  1021. ct_pic32mx775f512h,
  1022. ct_pic32mx775f512l,
  1023. ct_pic32mx795f512h,
  1024. ct_pic32mx795f512l:
  1025. begin
  1026. with embedded_controllers[current_settings.controllertype] do
  1027. with linkres do
  1028. begin
  1029. Add('OUTPUT_FORMAT("elf32-tradlittlemips")');
  1030. Add('OUTPUT_ARCH(pic32mx)');
  1031. Add('ENTRY(_reset)');
  1032. Add('PROVIDE(_vector_spacing = 0x00000001);');
  1033. Add('_ebase_address = 0x'+IntToHex(flashbase,8)+';');
  1034. Add('_RESET_ADDR = 0xBFC00000;');
  1035. Add('_BEV_EXCPT_ADDR = 0xBFC00380;');
  1036. Add('_DBG_EXCPT_ADDR = 0xBFC00480;');
  1037. Add('_GEN_EXCPT_ADDR = _ebase_address + 0x180;');
  1038. Add('MEMORY');
  1039. Add('{');
  1040. if flashsize<>0 then
  1041. begin
  1042. Add(' kseg0_program_mem : ORIGIN = 0x'+IntToHex(flashbase,8)+', LENGTH = 0x'+IntToHex(flashsize,8));
  1043. //TODO This should better be placed into the controllertype records
  1044. Add(' kseg1_boot_mem : ORIGIN = 0xBFC00000, LENGTH = 0xbef');
  1045. Add(' config3 : ORIGIN = 0xBFC00BF0, LENGTH = 0x4');
  1046. Add(' config2 : ORIGIN = 0xBFC00BF4, LENGTH = 0x4');
  1047. Add(' config1 : ORIGIN = 0xBFC00BF8, LENGTH = 0x4');
  1048. Add(' config0 : ORIGIN = 0xBFC00BFC, LENGTH = 0x4');
  1049. end;
  1050. Add(' ram : ORIGIN = 0x' + IntToHex(srambase,8)
  1051. + ', LENGTH = 0x' + IntToHex(sramsize,8));
  1052. Add('}');
  1053. Add('_stack_top = 0x' + IntToHex(sramsize+srambase,8) + ';');
  1054. end;
  1055. end
  1056. else
  1057. if not (cs_link_nolink in current_settings.globalswitches) then
  1058. internalerror(200902011);
  1059. end;
  1060. with linkres do
  1061. begin
  1062. Add('SECTIONS');
  1063. Add('{');
  1064. Add(' .reset _RESET_ADDR :');
  1065. Add(' {');
  1066. Add(' KEEP(*(.reset .reset.*))');
  1067. Add(' KEEP(*(.startup .startup.*))');
  1068. Add(' } > kseg1_boot_mem');
  1069. Add(' .bev_excpt _BEV_EXCPT_ADDR :');
  1070. Add(' {');
  1071. Add(' KEEP(*(.bev_handler))');
  1072. Add(' } > kseg1_boot_mem');
  1073. Add(' .text :');
  1074. Add(' {');
  1075. Add(' _text_start = .;');
  1076. Add(' . = _text_start + 0x180;');
  1077. Add(' KEEP(*(.gen_handler))');
  1078. Add(' . = _text_start + 0x200;');
  1079. Add(' KEEP(*(.init .init.*))');
  1080. Add(' *(.text .text.*)');
  1081. Add(' *(.strings)');
  1082. Add(' *(.rodata .rodata.*)');
  1083. Add(' *(.comment)');
  1084. Add(' _etext = .;');
  1085. if embedded_controllers[current_settings.controllertype].flashsize<>0 then
  1086. begin
  1087. Add(' } >kseg0_program_mem');
  1088. end
  1089. else
  1090. begin
  1091. Add(' } >ram');
  1092. end;
  1093. Add(' .note.gnu.build-id : { *(.note.gnu.build-id) }');
  1094. Add(' .data :');
  1095. Add(' {');
  1096. Add(' _data = .;');
  1097. Add(' *(.data .data.*)');
  1098. Add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  1099. Add(' . = .;');
  1100. Add(' _gp = ALIGN(16) + 0x7ff0;');
  1101. Add(' _edata = .;');
  1102. if embedded_controllers[current_settings.controllertype].flashsize<>0 then
  1103. begin
  1104. Add(' } >ram AT >kseg0_program_mem');
  1105. end
  1106. else
  1107. begin
  1108. Add(' } >ram');
  1109. end;
  1110. Add(' .config_BFC00BF0 : {');
  1111. Add(' KEEP(*(.config_BFC00BF0))');
  1112. Add(' } > config3');
  1113. Add(' .config_BFC00BF4 : {');
  1114. Add(' KEEP(*(.config_BFC00BF4))');
  1115. Add(' } > config2');
  1116. Add(' .config_BFC00BF8 : {');
  1117. Add(' KEEP(*(.config_BFC00BF8))');
  1118. Add(' } > config1');
  1119. Add(' .config_BFC00BFC : {');
  1120. Add(' KEEP(*(.config_BFC00BFC))');
  1121. Add(' } > config0');
  1122. Add(' .bss :');
  1123. Add(' {');
  1124. Add(' _bss_start = .;');
  1125. Add(' *(.bss .bss.*)');
  1126. Add(' *(COMMON)');
  1127. Add(' } >ram');
  1128. Add('. = ALIGN(4);');
  1129. Add('_bss_end = . ;');
  1130. Add(' .comment 0 : { *(.comment) }');
  1131. Add(' /* DWARF debug sections.');
  1132. Add(' Symbols in the DWARF debugging sections are relative to the beginning');
  1133. Add(' of the section so we begin them at 0. */');
  1134. Add(' /* DWARF 1 */');
  1135. Add(' .debug 0 : { *(.debug) }');
  1136. Add(' .line 0 : { *(.line) }');
  1137. Add(' /* GNU DWARF 1 extensions */');
  1138. Add(' .debug_srcinfo 0 : { *(.debug_srcinfo) }');
  1139. Add(' .debug_sfnames 0 : { *(.debug_sfnames) }');
  1140. Add(' /* DWARF 1.1 and DWARF 2 */');
  1141. Add(' .debug_aranges 0 : { *(.debug_aranges) }');
  1142. Add(' .debug_pubnames 0 : { *(.debug_pubnames) }');
  1143. Add(' /* DWARF 2 */');
  1144. Add(' .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) }');
  1145. Add(' .debug_abbrev 0 : { *(.debug_abbrev) }');
  1146. Add(' /DISCARD/ : { *(.debug_line) }');
  1147. Add(' .debug_frame 0 : { *(.debug_frame) }');
  1148. Add(' .debug_str 0 : { *(.debug_str) }');
  1149. Add(' /DISCARD/ : { *(.debug_loc) }');
  1150. Add(' .debug_macinfo 0 : { *(.debug_macinfo) }');
  1151. Add(' /* SGI/MIPS DWARF 2 extensions */');
  1152. Add(' .debug_weaknames 0 : { *(.debug_weaknames) }');
  1153. Add(' .debug_funcnames 0 : { *(.debug_funcnames) }');
  1154. Add(' .debug_typenames 0 : { *(.debug_typenames) }');
  1155. Add(' .debug_varnames 0 : { *(.debug_varnames) }');
  1156. Add(' /* DWARF 3 */');
  1157. Add(' .debug_pubtypes 0 : { *(.debug_pubtypes) }');
  1158. Add(' .debug_ranges 0 : { *(.debug_ranges) }');
  1159. Add(' .gnu.attributes 0 : { KEEP (*(.gnu.attributes)) }');
  1160. Add(' .gptab.sdata : { *(.gptab.data) *(.gptab.sdata) }');
  1161. Add(' .gptab.sbss : { *(.gptab.bss) *(.gptab.sbss) }');
  1162. Add(' .mdebug.abi32 : { KEEP(*(.mdebug.abi32)) }');
  1163. Add(' .mdebug.abiN32 : { KEEP(*(.mdebug.abiN32)) }');
  1164. Add(' .mdebug.abi64 : { KEEP(*(.mdebug.abi64)) }');
  1165. Add(' .mdebug.abiO64 : { KEEP(*(.mdebug.abiO64)) }');
  1166. Add(' .mdebug.eabi32 : { KEEP(*(.mdebug.eabi32)) }');
  1167. Add(' .mdebug.eabi64 : { KEEP(*(.mdebug.eabi64)) }');
  1168. Add(' /DISCARD/ : { *(.rel.dyn) }');
  1169. Add(' /DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) *(.gnu.lto_*) }');
  1170. Add('}');
  1171. Add('_end = .;');
  1172. end;
  1173. {$endif MIPSEL}
  1174. { Write and Close response }
  1175. linkres.writetodisk;
  1176. linkres.free;
  1177. WriteResponseFile:=True;
  1178. end;
  1179. function TlinkerEmbedded.MakeExecutable:boolean;
  1180. var
  1181. binstr,
  1182. cmdstr : TCmdStr;
  1183. success : boolean;
  1184. StaticStr,
  1185. GCSectionsStr,
  1186. DynLinkStr,
  1187. StripStr: string;
  1188. begin
  1189. { for future use }
  1190. StaticStr:='';
  1191. StripStr:='';
  1192. DynLinkStr:='';
  1193. GCSectionsStr:='--gc-sections';
  1194. //if not(cs_link_extern in current_settings.globalswitches) then
  1195. if not(cs_link_nolink in current_settings.globalswitches) then
  1196. Message1(exec_i_linking,current_module.exefilename);
  1197. { Write used files and libraries }
  1198. WriteResponseFile();
  1199. { Call linker }
  1200. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  1201. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  1202. if not(cs_link_on_target in current_settings.globalswitches) then
  1203. begin
  1204. Replace(cmdstr,'$EXE',(maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename,'.elf')))));
  1205. Replace(cmdstr,'$RES',(maybequoted(ScriptFixFileName(outputexedir+Info.ResName))));
  1206. Replace(cmdstr,'$STATIC',StaticStr);
  1207. Replace(cmdstr,'$STRIP',StripStr);
  1208. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  1209. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  1210. end
  1211. else
  1212. begin
  1213. Replace(cmdstr,'$EXE',maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename,'.elf'))));
  1214. Replace(cmdstr,'$RES',maybequoted(ScriptFixFileName(outputexedir+Info.ResName)));
  1215. Replace(cmdstr,'$STATIC',StaticStr);
  1216. Replace(cmdstr,'$STRIP',StripStr);
  1217. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  1218. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  1219. end;
  1220. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  1221. { Remove ReponseFile }
  1222. if success and not(cs_link_nolink in current_settings.globalswitches) then
  1223. DeleteFile(outputexedir+Info.ResName);
  1224. { Post process }
  1225. if success and not(cs_link_nolink in current_settings.globalswitches) then
  1226. success:=PostProcessExecutable(current_module.exefilename+'.elf',false);
  1227. if success and (target_info.system in [system_arm_embedded,system_avr_embedded,system_mipsel_embedded]) then
  1228. begin
  1229. success:=DoExec(FindUtil(utilsprefix+'objcopy'),'-O ihex '+
  1230. ChangeFileExt(current_module.exefilename,'.elf')+' '+
  1231. ChangeFileExt(current_module.exefilename,'.hex'),true,false);
  1232. if success then
  1233. success:=DoExec(FindUtil(utilsprefix+'objcopy'),'-O binary '+
  1234. ChangeFileExt(current_module.exefilename,'.elf')+' '+
  1235. ChangeFileExt(current_module.exefilename,'.bin'),true,false);
  1236. end;
  1237. MakeExecutable:=success; { otherwise a recursive call to link method }
  1238. end;
  1239. function TLinkerEmbedded.postprocessexecutable(const fn : string;isdll:boolean):boolean;
  1240. type
  1241. TElf32header=packed record
  1242. magic0123 : longint;
  1243. file_class : byte;
  1244. data_encoding : byte;
  1245. file_version : byte;
  1246. padding : array[$07..$0f] of byte;
  1247. e_type : word;
  1248. e_machine : word;
  1249. e_version : longint;
  1250. e_entry : longint; { entrypoint }
  1251. e_phoff : longint; { program header offset }
  1252. e_shoff : longint; { sections header offset }
  1253. e_flags : longint;
  1254. e_ehsize : word; { elf header size in bytes }
  1255. e_phentsize : word; { size of an entry in the program header array }
  1256. e_phnum : word; { 0..e_phnum-1 of entrys }
  1257. e_shentsize : word; { size of an entry in sections header array }
  1258. e_shnum : word; { 0..e_shnum-1 of entrys }
  1259. e_shstrndx : word; { index of string section header }
  1260. end;
  1261. TElf32sechdr=packed record
  1262. sh_name : longint;
  1263. sh_type : longint;
  1264. sh_flags : longint;
  1265. sh_addr : longint;
  1266. sh_offset : longint;
  1267. sh_size : longint;
  1268. sh_link : longint;
  1269. sh_info : longint;
  1270. sh_addralign : longint;
  1271. sh_entsize : longint;
  1272. end;
  1273. function MayBeSwapHeader(h : telf32header) : telf32header;
  1274. begin
  1275. result:=h;
  1276. if source_info.endian<>target_info.endian then
  1277. with h do
  1278. begin
  1279. result.e_type:=swapendian(e_type);
  1280. result.e_machine:=swapendian(e_machine);
  1281. result.e_version:=swapendian(e_version);
  1282. result.e_entry:=swapendian(e_entry);
  1283. result.e_phoff:=swapendian(e_phoff);
  1284. result.e_shoff:=swapendian(e_shoff);
  1285. result.e_flags:=swapendian(e_flags);
  1286. result.e_ehsize:=swapendian(e_ehsize);
  1287. result.e_phentsize:=swapendian(e_phentsize);
  1288. result.e_phnum:=swapendian(e_phnum);
  1289. result.e_shentsize:=swapendian(e_shentsize);
  1290. result.e_shnum:=swapendian(e_shnum);
  1291. result.e_shstrndx:=swapendian(e_shstrndx);
  1292. end;
  1293. end;
  1294. function MaybeSwapSecHeader(h : telf32sechdr) : telf32sechdr;
  1295. begin
  1296. result:=h;
  1297. if source_info.endian<>target_info.endian then
  1298. with h do
  1299. begin
  1300. result.sh_name:=swapendian(sh_name);
  1301. result.sh_type:=swapendian(sh_type);
  1302. result.sh_flags:=swapendian(sh_flags);
  1303. result.sh_addr:=swapendian(sh_addr);
  1304. result.sh_offset:=swapendian(sh_offset);
  1305. result.sh_size:=swapendian(sh_size);
  1306. result.sh_link:=swapendian(sh_link);
  1307. result.sh_info:=swapendian(sh_info);
  1308. result.sh_addralign:=swapendian(sh_addralign);
  1309. result.sh_entsize:=swapendian(sh_entsize);
  1310. end;
  1311. end;
  1312. var
  1313. f : file;
  1314. function ReadSectionName(pos : longint) : String;
  1315. var
  1316. oldpos : longint;
  1317. c : char;
  1318. begin
  1319. oldpos:=filepos(f);
  1320. seek(f,pos);
  1321. Result:='';
  1322. while true do
  1323. begin
  1324. blockread(f,c,1);
  1325. if c=#0 then
  1326. break;
  1327. Result:=Result+c;
  1328. end;
  1329. seek(f,oldpos);
  1330. end;
  1331. var
  1332. elfheader : TElf32header;
  1333. secheader : TElf32sechdr;
  1334. i : longint;
  1335. stringoffset : longint;
  1336. secname : string;
  1337. begin
  1338. postprocessexecutable:=false;
  1339. { open file }
  1340. assign(f,fn);
  1341. {$push}{$I-}
  1342. reset(f,1);
  1343. if ioresult<>0 then
  1344. Message1(execinfo_f_cant_open_executable,fn);
  1345. { read header }
  1346. blockread(f,elfheader,sizeof(tElf32header));
  1347. elfheader:=MayBeSwapHeader(elfheader);
  1348. seek(f,elfheader.e_shoff);
  1349. { read string section header }
  1350. seek(f,elfheader.e_shoff+sizeof(TElf32sechdr)*elfheader.e_shstrndx);
  1351. blockread(f,secheader,sizeof(secheader));
  1352. secheader:=MaybeSwapSecHeader(secheader);
  1353. stringoffset:=secheader.sh_offset;
  1354. seek(f,elfheader.e_shoff);
  1355. status.datasize:=0;
  1356. for i:=0 to elfheader.e_shnum-1 do
  1357. begin
  1358. blockread(f,secheader,sizeof(secheader));
  1359. secheader:=MaybeSwapSecHeader(secheader);
  1360. secname:=ReadSectionName(stringoffset+secheader.sh_name);
  1361. if secname='.text' then
  1362. begin
  1363. Message1(execinfo_x_codesize,tostr(secheader.sh_size));
  1364. status.codesize:=secheader.sh_size;
  1365. end
  1366. else if secname='.data' then
  1367. begin
  1368. Message1(execinfo_x_initdatasize,tostr(secheader.sh_size));
  1369. inc(status.datasize,secheader.sh_size);
  1370. end
  1371. else if secname='.bss' then
  1372. begin
  1373. Message1(execinfo_x_uninitdatasize,tostr(secheader.sh_size));
  1374. inc(status.datasize,secheader.sh_size);
  1375. end;
  1376. end;
  1377. close(f);
  1378. {$pop}
  1379. if ioresult<>0 then
  1380. ;
  1381. postprocessexecutable:=true;
  1382. end;
  1383. {*****************************************************************************
  1384. Initialize
  1385. *****************************************************************************}
  1386. initialization
  1387. {$ifdef arm}
  1388. RegisterLinker(ld_embedded,TLinkerEmbedded);
  1389. RegisterTarget(system_arm_embedded_info);
  1390. {$endif arm}
  1391. {$ifdef avr}
  1392. RegisterLinker(ld_embedded,TLinkerEmbedded);
  1393. RegisterTarget(system_avr_embedded_info);
  1394. {$endif avr}
  1395. {$ifdef i386}
  1396. RegisterLinker(ld_embedded,TLinkerEmbedded);
  1397. RegisterTarget(system_i386_embedded_info);
  1398. {$endif i386}
  1399. {$ifdef x86_64}
  1400. RegisterLinker(ld_embedded,TLinkerEmbedded);
  1401. RegisterTarget(system_x86_64_embedded_info);
  1402. {$endif x86_64}
  1403. {$ifdef i8086}
  1404. { no need to register linker ld_embedded, because i8086_embedded uses the
  1405. regular msdos linker. In case a flat binary, relocated for a specific
  1406. segment address is needed (e.g. for a BIOS or a real mode bootloader), it
  1407. can be produced post-compilation with exe2bin or a similar tool. }
  1408. RegisterTarget(system_i8086_embedded_info);
  1409. {$endif i8086}
  1410. {$ifdef mipsel}
  1411. RegisterLinker(ld_embedded,TLinkerEmbedded);
  1412. RegisterTarget(system_mipsel_embedded_info);
  1413. {$endif mipsel}
  1414. {$ifdef m68k}
  1415. RegisterLinker(ld_embedded,TLinkerEmbedded);
  1416. RegisterTarget(system_m68k_embedded_info);
  1417. {$endif m68k}
  1418. end.