t_embed.pas 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517
  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(' _etext = .;');
  621. if embedded_controllers[current_settings.controllertype].flashsize<>0 then
  622. begin
  623. Add(' } >flash');
  624. Add(' .note.gnu.build-id : { *(.note.gnu.build-id) } >flash ');
  625. end
  626. else
  627. begin
  628. Add(' } >ram');
  629. Add(' .note.gnu.build-id : { *(.note.gnu.build-id) } >ram ');
  630. end;
  631. Add(' .data :');
  632. Add(' {');
  633. Add(' _data = .;');
  634. Add(' *(.data .data.*)');
  635. Add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  636. Add(' _edata = .;');
  637. if embedded_controllers[current_settings.controllertype].flashsize<>0 then
  638. begin
  639. Add(' } >ram AT >flash');
  640. end
  641. else
  642. begin
  643. Add(' } >ram');
  644. end;
  645. Add(' .bss :');
  646. Add(' {');
  647. Add(' _bss_start = .;');
  648. Add(' *(.bss .bss.*)');
  649. Add(' *(COMMON)');
  650. Add(' } >ram');
  651. Add('. = ALIGN(4);');
  652. Add('_bss_end = . ;');
  653. Add('}');
  654. Add('_end = .;');
  655. end;
  656. {$endif ARM}
  657. {$ifdef i386}
  658. with linkres do
  659. begin
  660. Add('ENTRY(_START)');
  661. Add('SECTIONS');
  662. Add('{');
  663. Add(' . = 0x100000;');
  664. Add(' .text ALIGN (0x1000) :');
  665. Add(' {');
  666. Add(' _text = .;');
  667. Add(' KEEP(*(.init .init.*))');
  668. Add(' *(.text .text.*)');
  669. Add(' *(.strings)');
  670. Add(' *(.rodata .rodata.*)');
  671. Add(' *(.comment)');
  672. Add(' _etext = .;');
  673. Add(' }');
  674. Add(' .data ALIGN (0x1000) :');
  675. Add(' {');
  676. Add(' _data = .;');
  677. Add(' *(.data .data.*)');
  678. Add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  679. Add(' _edata = .;');
  680. Add(' }');
  681. Add(' . = ALIGN(4);');
  682. Add(' .bss :');
  683. Add(' {');
  684. Add(' _bss_start = .;');
  685. Add(' *(.bss .bss.*)');
  686. Add(' *(COMMON)');
  687. Add(' }');
  688. Add('_bss_end = . ;');
  689. Add('}');
  690. Add('_end = .;');
  691. end;
  692. {$endif i386}
  693. {$ifdef x86_64}
  694. with linkres do
  695. begin
  696. Add('ENTRY(_START)');
  697. Add('SECTIONS');
  698. Add('{');
  699. Add(' . = 0x100000;');
  700. Add(' .text ALIGN (0x1000) :');
  701. Add(' {');
  702. Add(' _text = .;');
  703. Add(' KEEP(*(.init .init.*))');
  704. Add(' *(.text .text.*)');
  705. Add(' *(.strings)');
  706. Add(' *(.rodata .rodata.*)');
  707. Add(' *(.comment)');
  708. Add(' _etext = .;');
  709. Add(' }');
  710. Add(' .data ALIGN (0x1000) :');
  711. Add(' {');
  712. Add(' _data = .;');
  713. Add(' *(.data .data.*)');
  714. Add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  715. Add(' _edata = .;');
  716. Add(' }');
  717. Add(' . = ALIGN(4);');
  718. Add(' .bss :');
  719. Add(' {');
  720. Add(' _bss_start = .;');
  721. Add(' *(.bss .bss.*)');
  722. Add(' *(COMMON)');
  723. Add(' }');
  724. Add('_bss_end = . ;');
  725. Add('}');
  726. Add('_end = .;');
  727. end;
  728. {$endif x86_64}
  729. {$ifdef AVR}
  730. with linkres do
  731. begin
  732. { linker script from ld 2.19 }
  733. Add('ENTRY(_START)');
  734. Add('OUTPUT_FORMAT("elf32-avr","elf32-avr","elf32-avr")');
  735. case current_settings.cputype of
  736. cpu_avr1:
  737. Add('OUTPUT_ARCH(avr:1)');
  738. cpu_avr2:
  739. Add('OUTPUT_ARCH(avr:2)');
  740. cpu_avr25:
  741. Add('OUTPUT_ARCH(avr:25)');
  742. cpu_avr3:
  743. Add('OUTPUT_ARCH(avr:3)');
  744. cpu_avr31:
  745. Add('OUTPUT_ARCH(avr:31)');
  746. cpu_avr35:
  747. Add('OUTPUT_ARCH(avr:35)');
  748. cpu_avr4:
  749. Add('OUTPUT_ARCH(avr:4)');
  750. cpu_avr5:
  751. Add('OUTPUT_ARCH(avr:5)');
  752. cpu_avr51:
  753. Add('OUTPUT_ARCH(avr:51)');
  754. cpu_avr6:
  755. Add('OUTPUT_ARCH(avr:6)');
  756. else
  757. Internalerror(2015072701);
  758. end;
  759. Add('MEMORY');
  760. with embedded_controllers[current_settings.controllertype] do
  761. begin
  762. Add('{');
  763. Add(' text (rx) : ORIGIN = 0, LENGTH = 0x'+IntToHex(flashsize,6));
  764. Add(' data (rw!x) : ORIGIN = 0x'+IntToHex($800000+srambase,6)+', LENGTH = 0x'+IntToHex(sramsize,6));
  765. Add(' eeprom (rw!x) : ORIGIN = 0x810000, LENGTH = 0x'+IntToHex(eepromsize,6));
  766. Add(' fuse (rw!x) : ORIGIN = 0x820000, LENGTH = 1K');
  767. Add(' lock (rw!x) : ORIGIN = 0x830000, LENGTH = 1K');
  768. Add(' signature (rw!x) : ORIGIN = 0x840000, LENGTH = 1K');
  769. Add('}');
  770. Add('_stack_top = 0x' + IntToHex(srambase+sramsize-1,4) + ';');
  771. end;
  772. Add('SECTIONS');
  773. Add('{');
  774. Add(' /* Read-only sections, merged into text segment: */');
  775. Add(' .hash : { *(.hash) }');
  776. Add(' .dynsym : { *(.dynsym) }');
  777. Add(' .dynstr : { *(.dynstr) }');
  778. Add(' .gnu.version : { *(.gnu.version) }');
  779. Add(' .gnu.version_d : { *(.gnu.version_d) }');
  780. Add(' .gnu.version_r : { *(.gnu.version_r) }');
  781. Add(' .rel.init : { *(.rel.init) }');
  782. Add(' .rela.init : { *(.rela.init) }');
  783. Add(' .rel.text :');
  784. Add(' {');
  785. Add(' *(.rel.text)');
  786. Add(' *(.rel.text.*)');
  787. Add(' *(.rel.gnu.linkonce.t*)');
  788. Add(' }');
  789. Add(' .rela.text :');
  790. Add(' {');
  791. Add(' *(.rela.text)');
  792. Add(' *(.rela.text.*)');
  793. Add(' *(.rela.gnu.linkonce.t*)');
  794. Add(' }');
  795. Add(' .rel.fini : { *(.rel.fini) }');
  796. Add(' .rela.fini : { *(.rela.fini) }');
  797. Add(' .rel.rodata :');
  798. Add(' {');
  799. Add(' *(.rel.rodata)');
  800. Add(' *(.rel.rodata.*)');
  801. Add(' *(.rel.gnu.linkonce.r*)');
  802. Add(' }');
  803. Add(' .rela.rodata :');
  804. Add(' {');
  805. Add(' *(.rela.rodata)');
  806. Add(' *(.rela.rodata.*)');
  807. Add(' *(.rela.gnu.linkonce.r*)');
  808. Add(' }');
  809. Add(' .rel.data :');
  810. Add(' {');
  811. Add(' *(.rel.data)');
  812. Add(' *(.rel.data.*)');
  813. Add(' *(.rel.gnu.linkonce.d*)');
  814. Add(' }');
  815. Add(' .rela.data :');
  816. Add(' {');
  817. Add(' *(.rela.data)');
  818. Add(' *(.rela.data.*)');
  819. Add(' *(.rela.gnu.linkonce.d*)');
  820. Add(' }');
  821. Add(' .rel.ctors : { *(.rel.ctors) }');
  822. Add(' .rela.ctors : { *(.rela.ctors) }');
  823. Add(' .rel.dtors : { *(.rel.dtors) }');
  824. Add(' .rela.dtors : { *(.rela.dtors) }');
  825. Add(' .rel.got : { *(.rel.got) }');
  826. Add(' .rela.got : { *(.rela.got) }');
  827. Add(' .rel.bss : { *(.rel.bss) }');
  828. Add(' .rela.bss : { *(.rela.bss) }');
  829. Add(' .rel.plt : { *(.rel.plt) }');
  830. Add(' .rela.plt : { *(.rela.plt) }');
  831. Add(' /* Internal text space or external memory. */');
  832. Add(' .text :');
  833. Add(' {');
  834. Add(' KEEP(*(.init .init.*))');
  835. Add(' /* For data that needs to reside in the lower 64k of progmem. */');
  836. Add(' *(.progmem.gcc*)');
  837. Add(' *(.progmem*)');
  838. Add(' . = ALIGN(2);');
  839. Add(' __trampolines_start = . ;');
  840. Add(' /* The jump trampolines for the 16-bit limited relocs will reside here. */');
  841. Add(' *(.trampolines)');
  842. Add(' *(.trampolines*)');
  843. Add(' __trampolines_end = . ;');
  844. Add(' /* For future tablejump instruction arrays for 3 byte pc devices.');
  845. Add(' We don''t relax jump/call instructions within these sections. */');
  846. Add(' *(.jumptables)');
  847. Add(' *(.jumptables*)');
  848. Add(' /* For code that needs to reside in the lower 128k progmem. */');
  849. Add(' *(.lowtext)');
  850. Add(' *(.lowtext*)');
  851. Add(' __ctors_start = . ;');
  852. Add(' *(.ctors)');
  853. Add(' __ctors_end = . ;');
  854. Add(' __dtors_start = . ;');
  855. Add(' *(.dtors)');
  856. Add(' __dtors_end = . ;');
  857. Add(' KEEP(SORT(*)(.ctors))');
  858. Add(' KEEP(SORT(*)(.dtors))');
  859. Add(' /* From this point on, we don''t bother about wether the insns are');
  860. Add(' below or above the 16 bits boundary. */');
  861. Add(' *(.init0) /* Start here after reset. */');
  862. Add(' KEEP (*(.init0))');
  863. Add(' *(.init1)');
  864. Add(' KEEP (*(.init1))');
  865. Add(' *(.init2) /* Clear __zero_reg__, set up stack pointer. */');
  866. Add(' KEEP (*(.init2))');
  867. Add(' *(.init3)');
  868. Add(' KEEP (*(.init3))');
  869. Add(' *(.init4) /* Initialize data and BSS. */');
  870. Add(' KEEP (*(.init4))');
  871. Add(' *(.init5)');
  872. Add(' KEEP (*(.init5))');
  873. Add(' *(.init6) /* C++ constructors. */');
  874. Add(' KEEP (*(.init6))');
  875. Add(' *(.init7)');
  876. Add(' KEEP (*(.init7))');
  877. Add(' *(.init8)');
  878. Add(' KEEP (*(.init8))');
  879. Add(' *(.init9) /* Call main(). */');
  880. Add(' KEEP (*(.init9))');
  881. Add(' *(.text)');
  882. Add(' . = ALIGN(2);');
  883. Add(' *(.text.*)');
  884. Add(' . = ALIGN(2);');
  885. Add(' *(.fini9) /* _exit() starts here. */');
  886. Add(' KEEP (*(.fini9))');
  887. Add(' *(.fini8)');
  888. Add(' KEEP (*(.fini8))');
  889. Add(' *(.fini7)');
  890. Add(' KEEP (*(.fini7))');
  891. Add(' *(.fini6) /* C++ destructors. */');
  892. Add(' KEEP (*(.fini6))');
  893. Add(' *(.fini5)');
  894. Add(' KEEP (*(.fini5))');
  895. Add(' *(.fini4)');
  896. Add(' KEEP (*(.fini4))');
  897. Add(' *(.fini3)');
  898. Add(' KEEP (*(.fini3))');
  899. Add(' *(.fini2)');
  900. Add(' KEEP (*(.fini2))');
  901. Add(' *(.fini1)');
  902. Add(' KEEP (*(.fini1))');
  903. Add(' *(.fini0) /* Infinite loop after program termination. */');
  904. Add(' KEEP (*(.fini0))');
  905. Add(' _etext = . ;');
  906. Add(' } > text');
  907. Add(' .data : AT (ADDR (.text) + SIZEOF (.text))');
  908. Add(' {');
  909. Add(' PROVIDE (__data_start = .) ;');
  910. Add(' *(.data)');
  911. Add(' *(.data*)');
  912. Add(' *(.rodata) /* We need to include .rodata here if gcc is used */');
  913. Add(' *(.rodata*) /* with -fdata-sections. */');
  914. Add(' *(.gnu.linkonce.d*)');
  915. Add(' . = ALIGN(2);');
  916. Add(' _edata = . ;');
  917. Add(' PROVIDE (__data_end = .) ;');
  918. Add(' } > data');
  919. Add(' .bss : AT (ADDR (.bss))');
  920. Add(' {');
  921. Add(' PROVIDE (__bss_start = .) ;');
  922. Add(' *(.bss)');
  923. Add(' *(.bss*)');
  924. Add(' *(COMMON)');
  925. Add(' PROVIDE (__bss_end = .) ;');
  926. Add(' } > data');
  927. Add(' __data_load_start = LOADADDR(.data);');
  928. Add(' __data_load_end = __data_load_start + SIZEOF(.data);');
  929. Add(' /* Global data not cleared after reset. */');
  930. Add(' .noinit :');
  931. Add(' {');
  932. Add(' PROVIDE (__noinit_start = .) ;');
  933. Add(' *(.noinit*)');
  934. Add(' PROVIDE (__noinit_end = .) ;');
  935. Add(' _end = . ;');
  936. Add(' PROVIDE (__heap_start = .) ;');
  937. Add(' } > data');
  938. Add(' .eeprom :');
  939. Add(' {');
  940. Add(' *(.eeprom*)');
  941. Add(' __eeprom_end = . ;');
  942. Add(' } > eeprom');
  943. Add(' .fuse :');
  944. Add(' {');
  945. Add(' KEEP(*(.fuse))');
  946. Add(' KEEP(*(.lfuse))');
  947. Add(' KEEP(*(.hfuse))');
  948. Add(' KEEP(*(.efuse))');
  949. Add(' } > fuse');
  950. Add(' .lock :');
  951. Add(' {');
  952. Add(' KEEP(*(.lock*))');
  953. Add(' } > lock');
  954. Add(' .signature :');
  955. Add(' {');
  956. Add(' KEEP(*(.signature*))');
  957. Add(' } > signature');
  958. Add(' /* Stabs debugging sections. */');
  959. Add(' .stab 0 : { *(.stab) }');
  960. Add(' .stabstr 0 : { *(.stabstr) }');
  961. Add(' .stab.excl 0 : { *(.stab.excl) }');
  962. Add(' .stab.exclstr 0 : { *(.stab.exclstr) }');
  963. Add(' .stab.index 0 : { *(.stab.index) }');
  964. Add(' .stab.indexstr 0 : { *(.stab.indexstr) }');
  965. Add(' .comment 0 : { *(.comment) }');
  966. Add(' /* DWARF debug sections.');
  967. Add(' Symbols in the DWARF debugging sections are relative to the beginning');
  968. Add(' of the section so we begin them at 0. */');
  969. Add(' /* DWARF 1 */');
  970. Add(' .debug 0 : { *(.debug) }');
  971. Add(' .line 0 : { *(.line) }');
  972. Add(' /* GNU DWARF 1 extensions */');
  973. Add(' .debug_srcinfo 0 : { *(.debug_srcinfo) }');
  974. Add(' .debug_sfnames 0 : { *(.debug_sfnames) }');
  975. Add(' /* DWARF 1.1 and DWARF 2 */');
  976. Add(' .debug_aranges 0 : { *(.debug_aranges) }');
  977. Add(' .debug_pubnames 0 : { *(.debug_pubnames) }');
  978. Add(' /* DWARF 2 */');
  979. Add(' .debug_info 0 : { *(.debug_info) *(.gnu.linkonce.wi.*) }');
  980. Add(' .debug_abbrev 0 : { *(.debug_abbrev) }');
  981. Add(' .debug_line 0 : { *(.debug_line) }');
  982. Add(' .debug_frame 0 : { *(.debug_frame) }');
  983. Add(' .debug_str 0 : { *(.debug_str) }');
  984. Add(' .debug_loc 0 : { *(.debug_loc) }');
  985. Add(' .debug_macinfo 0 : { *(.debug_macinfo) }');
  986. Add('}');
  987. end;
  988. {$endif AVR}
  989. {$ifdef MIPSEL}
  990. case current_settings.controllertype of
  991. ct_none:
  992. begin
  993. end;
  994. ct_pic32mx110f016b,
  995. ct_pic32mx110f016c,
  996. ct_pic32mx110f016d,
  997. ct_pic32mx120f032b,
  998. ct_pic32mx120f032c,
  999. ct_pic32mx120f032d,
  1000. ct_pic32mx130f064b,
  1001. ct_pic32mx130f064c,
  1002. ct_pic32mx130f064d,
  1003. ct_pic32mx150f128b,
  1004. ct_pic32mx150f128c,
  1005. ct_pic32mx150f128d,
  1006. ct_pic32mx210f016b,
  1007. ct_pic32mx210f016c,
  1008. ct_pic32mx210f016d,
  1009. ct_pic32mx220f032b,
  1010. ct_pic32mx220f032c,
  1011. ct_pic32mx220f032d,
  1012. ct_pic32mx230f064b,
  1013. ct_pic32mx230f064c,
  1014. ct_pic32mx230f064d,
  1015. ct_pic32mx250f128b,
  1016. ct_pic32mx250f128c,
  1017. ct_pic32mx250f128d,
  1018. ct_pic32mx775f256h,
  1019. ct_pic32mx775f256l,
  1020. ct_pic32mx775f512h,
  1021. ct_pic32mx775f512l,
  1022. ct_pic32mx795f512h,
  1023. ct_pic32mx795f512l:
  1024. begin
  1025. with embedded_controllers[current_settings.controllertype] do
  1026. with linkres do
  1027. begin
  1028. Add('OUTPUT_FORMAT("elf32-tradlittlemips")');
  1029. Add('OUTPUT_ARCH(pic32mx)');
  1030. Add('ENTRY(_reset)');
  1031. Add('PROVIDE(_vector_spacing = 0x00000001);');
  1032. Add('_ebase_address = 0x'+IntToHex(flashbase,8)+';');
  1033. Add('_RESET_ADDR = 0xBFC00000;');
  1034. Add('_BEV_EXCPT_ADDR = 0xBFC00380;');
  1035. Add('_DBG_EXCPT_ADDR = 0xBFC00480;');
  1036. Add('_GEN_EXCPT_ADDR = _ebase_address + 0x180;');
  1037. Add('MEMORY');
  1038. Add('{');
  1039. if flashsize<>0 then
  1040. begin
  1041. Add(' kseg0_program_mem : ORIGIN = 0x'+IntToHex(flashbase,8)+', LENGTH = 0x'+IntToHex(flashsize,8));
  1042. //TODO This should better be placed into the controllertype records
  1043. Add(' kseg1_boot_mem : ORIGIN = 0xBFC00000, LENGTH = 0xbef');
  1044. Add(' config3 : ORIGIN = 0xBFC00BF0, LENGTH = 0x4');
  1045. Add(' config2 : ORIGIN = 0xBFC00BF4, LENGTH = 0x4');
  1046. Add(' config1 : ORIGIN = 0xBFC00BF8, LENGTH = 0x4');
  1047. Add(' config0 : ORIGIN = 0xBFC00BFC, LENGTH = 0x4');
  1048. end;
  1049. Add(' ram : ORIGIN = 0x' + IntToHex(srambase,8)
  1050. + ', LENGTH = 0x' + IntToHex(sramsize,8));
  1051. Add('}');
  1052. Add('_stack_top = 0x' + IntToHex(sramsize+srambase,8) + ';');
  1053. end;
  1054. end
  1055. else
  1056. if not (cs_link_nolink in current_settings.globalswitches) then
  1057. internalerror(200902011);
  1058. end;
  1059. with linkres do
  1060. begin
  1061. Add('SECTIONS');
  1062. Add('{');
  1063. Add(' .reset _RESET_ADDR :');
  1064. Add(' {');
  1065. Add(' KEEP(*(.reset .reset.*))');
  1066. Add(' KEEP(*(.startup .startup.*))');
  1067. Add(' } > kseg1_boot_mem');
  1068. Add(' .bev_excpt _BEV_EXCPT_ADDR :');
  1069. Add(' {');
  1070. Add(' KEEP(*(.bev_handler))');
  1071. Add(' } > kseg1_boot_mem');
  1072. Add(' .text :');
  1073. Add(' {');
  1074. Add(' _text_start = .;');
  1075. Add(' . = _text_start + 0x180;');
  1076. Add(' KEEP(*(.gen_handler))');
  1077. Add(' . = _text_start + 0x200;');
  1078. Add(' KEEP(*(.init .init.*))');
  1079. Add(' *(.text .text.*)');
  1080. Add(' *(.strings)');
  1081. Add(' *(.rodata .rodata.*)');
  1082. Add(' *(.comment)');
  1083. Add(' _etext = .;');
  1084. if embedded_controllers[current_settings.controllertype].flashsize<>0 then
  1085. begin
  1086. Add(' } >kseg0_program_mem');
  1087. end
  1088. else
  1089. begin
  1090. Add(' } >ram');
  1091. end;
  1092. Add(' .note.gnu.build-id : { *(.note.gnu.build-id) }');
  1093. Add(' .data :');
  1094. Add(' {');
  1095. Add(' _data = .;');
  1096. Add(' *(.data .data.*)');
  1097. Add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  1098. Add(' . = .;');
  1099. Add(' _gp = ALIGN(16) + 0x7ff0;');
  1100. Add(' _edata = .;');
  1101. if embedded_controllers[current_settings.controllertype].flashsize<>0 then
  1102. begin
  1103. Add(' } >ram AT >kseg0_program_mem');
  1104. end
  1105. else
  1106. begin
  1107. Add(' } >ram');
  1108. end;
  1109. Add(' .config_BFC00BF0 : {');
  1110. Add(' KEEP(*(.config_BFC00BF0))');
  1111. Add(' } > config3');
  1112. Add(' .config_BFC00BF4 : {');
  1113. Add(' KEEP(*(.config_BFC00BF4))');
  1114. Add(' } > config2');
  1115. Add(' .config_BFC00BF8 : {');
  1116. Add(' KEEP(*(.config_BFC00BF8))');
  1117. Add(' } > config1');
  1118. Add(' .config_BFC00BFC : {');
  1119. Add(' KEEP(*(.config_BFC00BFC))');
  1120. Add(' } > config0');
  1121. Add(' .bss :');
  1122. Add(' {');
  1123. Add(' _bss_start = .;');
  1124. Add(' *(.bss .bss.*)');
  1125. Add(' *(COMMON)');
  1126. Add(' } >ram');
  1127. Add('. = ALIGN(4);');
  1128. Add('_bss_end = . ;');
  1129. Add(' .comment 0 : { *(.comment) }');
  1130. Add(' /* DWARF debug sections.');
  1131. Add(' Symbols in the DWARF debugging sections are relative to the beginning');
  1132. Add(' of the section so we begin them at 0. */');
  1133. Add(' /* DWARF 1 */');
  1134. Add(' .debug 0 : { *(.debug) }');
  1135. Add(' .line 0 : { *(.line) }');
  1136. Add(' /* GNU DWARF 1 extensions */');
  1137. Add(' .debug_srcinfo 0 : { *(.debug_srcinfo) }');
  1138. Add(' .debug_sfnames 0 : { *(.debug_sfnames) }');
  1139. Add(' /* DWARF 1.1 and DWARF 2 */');
  1140. Add(' .debug_aranges 0 : { *(.debug_aranges) }');
  1141. Add(' .debug_pubnames 0 : { *(.debug_pubnames) }');
  1142. Add(' /* DWARF 2 */');
  1143. Add(' .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) }');
  1144. Add(' .debug_abbrev 0 : { *(.debug_abbrev) }');
  1145. Add(' /DISCARD/ : { *(.debug_line) }');
  1146. Add(' .debug_frame 0 : { *(.debug_frame) }');
  1147. Add(' .debug_str 0 : { *(.debug_str) }');
  1148. Add(' /DISCARD/ : { *(.debug_loc) }');
  1149. Add(' .debug_macinfo 0 : { *(.debug_macinfo) }');
  1150. Add(' /* SGI/MIPS DWARF 2 extensions */');
  1151. Add(' .debug_weaknames 0 : { *(.debug_weaknames) }');
  1152. Add(' .debug_funcnames 0 : { *(.debug_funcnames) }');
  1153. Add(' .debug_typenames 0 : { *(.debug_typenames) }');
  1154. Add(' .debug_varnames 0 : { *(.debug_varnames) }');
  1155. Add(' /* DWARF 3 */');
  1156. Add(' .debug_pubtypes 0 : { *(.debug_pubtypes) }');
  1157. Add(' .debug_ranges 0 : { *(.debug_ranges) }');
  1158. Add(' .gnu.attributes 0 : { KEEP (*(.gnu.attributes)) }');
  1159. Add(' .gptab.sdata : { *(.gptab.data) *(.gptab.sdata) }');
  1160. Add(' .gptab.sbss : { *(.gptab.bss) *(.gptab.sbss) }');
  1161. Add(' .mdebug.abi32 : { KEEP(*(.mdebug.abi32)) }');
  1162. Add(' .mdebug.abiN32 : { KEEP(*(.mdebug.abiN32)) }');
  1163. Add(' .mdebug.abi64 : { KEEP(*(.mdebug.abi64)) }');
  1164. Add(' .mdebug.abiO64 : { KEEP(*(.mdebug.abiO64)) }');
  1165. Add(' .mdebug.eabi32 : { KEEP(*(.mdebug.eabi32)) }');
  1166. Add(' .mdebug.eabi64 : { KEEP(*(.mdebug.eabi64)) }');
  1167. Add(' /DISCARD/ : { *(.rel.dyn) }');
  1168. Add(' /DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) *(.gnu.lto_*) }');
  1169. Add('}');
  1170. Add('_end = .;');
  1171. end;
  1172. {$endif MIPSEL}
  1173. { Write and Close response }
  1174. linkres.writetodisk;
  1175. linkres.free;
  1176. WriteResponseFile:=True;
  1177. end;
  1178. function TlinkerEmbedded.MakeExecutable:boolean;
  1179. var
  1180. binstr,
  1181. cmdstr : TCmdStr;
  1182. success : boolean;
  1183. StaticStr,
  1184. GCSectionsStr,
  1185. DynLinkStr,
  1186. StripStr: string;
  1187. begin
  1188. { for future use }
  1189. StaticStr:='';
  1190. StripStr:='';
  1191. DynLinkStr:='';
  1192. GCSectionsStr:='--gc-sections';
  1193. //if not(cs_link_extern in current_settings.globalswitches) then
  1194. if not(cs_link_nolink in current_settings.globalswitches) then
  1195. Message1(exec_i_linking,current_module.exefilename);
  1196. { Write used files and libraries }
  1197. WriteResponseFile();
  1198. { Call linker }
  1199. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  1200. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  1201. if not(cs_link_on_target in current_settings.globalswitches) then
  1202. begin
  1203. Replace(cmdstr,'$EXE',(maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename,'.elf')))));
  1204. Replace(cmdstr,'$RES',(maybequoted(ScriptFixFileName(outputexedir+Info.ResName))));
  1205. Replace(cmdstr,'$STATIC',StaticStr);
  1206. Replace(cmdstr,'$STRIP',StripStr);
  1207. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  1208. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  1209. end
  1210. else
  1211. begin
  1212. Replace(cmdstr,'$EXE',maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename,'.elf'))));
  1213. Replace(cmdstr,'$RES',maybequoted(ScriptFixFileName(outputexedir+Info.ResName)));
  1214. Replace(cmdstr,'$STATIC',StaticStr);
  1215. Replace(cmdstr,'$STRIP',StripStr);
  1216. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  1217. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  1218. end;
  1219. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  1220. { Remove ReponseFile }
  1221. if success and not(cs_link_nolink in current_settings.globalswitches) then
  1222. DeleteFile(outputexedir+Info.ResName);
  1223. { Post process }
  1224. if success and not(cs_link_nolink in current_settings.globalswitches) then
  1225. success:=PostProcessExecutable(current_module.exefilename+'.elf',false);
  1226. if success and (target_info.system in [system_arm_embedded,system_avr_embedded,system_mipsel_embedded]) then
  1227. begin
  1228. success:=DoExec(FindUtil(utilsprefix+'objcopy'),'-O ihex '+
  1229. ChangeFileExt(current_module.exefilename,'.elf')+' '+
  1230. ChangeFileExt(current_module.exefilename,'.hex'),true,false);
  1231. if success then
  1232. success:=DoExec(FindUtil(utilsprefix+'objcopy'),'-O binary '+
  1233. ChangeFileExt(current_module.exefilename,'.elf')+' '+
  1234. ChangeFileExt(current_module.exefilename,'.bin'),true,false);
  1235. end;
  1236. MakeExecutable:=success; { otherwise a recursive call to link method }
  1237. end;
  1238. function TLinkerEmbedded.postprocessexecutable(const fn : string;isdll:boolean):boolean;
  1239. type
  1240. TElf32header=packed record
  1241. magic0123 : longint;
  1242. file_class : byte;
  1243. data_encoding : byte;
  1244. file_version : byte;
  1245. padding : array[$07..$0f] of byte;
  1246. e_type : word;
  1247. e_machine : word;
  1248. e_version : longint;
  1249. e_entry : longint; { entrypoint }
  1250. e_phoff : longint; { program header offset }
  1251. e_shoff : longint; { sections header offset }
  1252. e_flags : longint;
  1253. e_ehsize : word; { elf header size in bytes }
  1254. e_phentsize : word; { size of an entry in the program header array }
  1255. e_phnum : word; { 0..e_phnum-1 of entrys }
  1256. e_shentsize : word; { size of an entry in sections header array }
  1257. e_shnum : word; { 0..e_shnum-1 of entrys }
  1258. e_shstrndx : word; { index of string section header }
  1259. end;
  1260. TElf32sechdr=packed record
  1261. sh_name : longint;
  1262. sh_type : longint;
  1263. sh_flags : longint;
  1264. sh_addr : longint;
  1265. sh_offset : longint;
  1266. sh_size : longint;
  1267. sh_link : longint;
  1268. sh_info : longint;
  1269. sh_addralign : longint;
  1270. sh_entsize : longint;
  1271. end;
  1272. function MayBeSwapHeader(h : telf32header) : telf32header;
  1273. begin
  1274. result:=h;
  1275. if source_info.endian<>target_info.endian then
  1276. with h do
  1277. begin
  1278. result.e_type:=swapendian(e_type);
  1279. result.e_machine:=swapendian(e_machine);
  1280. result.e_version:=swapendian(e_version);
  1281. result.e_entry:=swapendian(e_entry);
  1282. result.e_phoff:=swapendian(e_phoff);
  1283. result.e_shoff:=swapendian(e_shoff);
  1284. result.e_flags:=swapendian(e_flags);
  1285. result.e_ehsize:=swapendian(e_ehsize);
  1286. result.e_phentsize:=swapendian(e_phentsize);
  1287. result.e_phnum:=swapendian(e_phnum);
  1288. result.e_shentsize:=swapendian(e_shentsize);
  1289. result.e_shnum:=swapendian(e_shnum);
  1290. result.e_shstrndx:=swapendian(e_shstrndx);
  1291. end;
  1292. end;
  1293. function MaybeSwapSecHeader(h : telf32sechdr) : telf32sechdr;
  1294. begin
  1295. result:=h;
  1296. if source_info.endian<>target_info.endian then
  1297. with h do
  1298. begin
  1299. result.sh_name:=swapendian(sh_name);
  1300. result.sh_type:=swapendian(sh_type);
  1301. result.sh_flags:=swapendian(sh_flags);
  1302. result.sh_addr:=swapendian(sh_addr);
  1303. result.sh_offset:=swapendian(sh_offset);
  1304. result.sh_size:=swapendian(sh_size);
  1305. result.sh_link:=swapendian(sh_link);
  1306. result.sh_info:=swapendian(sh_info);
  1307. result.sh_addralign:=swapendian(sh_addralign);
  1308. result.sh_entsize:=swapendian(sh_entsize);
  1309. end;
  1310. end;
  1311. var
  1312. f : file;
  1313. function ReadSectionName(pos : longint) : String;
  1314. var
  1315. oldpos : longint;
  1316. c : char;
  1317. begin
  1318. oldpos:=filepos(f);
  1319. seek(f,pos);
  1320. Result:='';
  1321. while true do
  1322. begin
  1323. blockread(f,c,1);
  1324. if c=#0 then
  1325. break;
  1326. Result:=Result+c;
  1327. end;
  1328. seek(f,oldpos);
  1329. end;
  1330. var
  1331. elfheader : TElf32header;
  1332. secheader : TElf32sechdr;
  1333. i : longint;
  1334. stringoffset : longint;
  1335. secname : string;
  1336. begin
  1337. postprocessexecutable:=false;
  1338. { open file }
  1339. assign(f,fn);
  1340. {$push}{$I-}
  1341. reset(f,1);
  1342. if ioresult<>0 then
  1343. Message1(execinfo_f_cant_open_executable,fn);
  1344. { read header }
  1345. blockread(f,elfheader,sizeof(tElf32header));
  1346. elfheader:=MayBeSwapHeader(elfheader);
  1347. seek(f,elfheader.e_shoff);
  1348. { read string section header }
  1349. seek(f,elfheader.e_shoff+sizeof(TElf32sechdr)*elfheader.e_shstrndx);
  1350. blockread(f,secheader,sizeof(secheader));
  1351. secheader:=MaybeSwapSecHeader(secheader);
  1352. stringoffset:=secheader.sh_offset;
  1353. seek(f,elfheader.e_shoff);
  1354. status.datasize:=0;
  1355. for i:=0 to elfheader.e_shnum-1 do
  1356. begin
  1357. blockread(f,secheader,sizeof(secheader));
  1358. secheader:=MaybeSwapSecHeader(secheader);
  1359. secname:=ReadSectionName(stringoffset+secheader.sh_name);
  1360. if secname='.text' then
  1361. begin
  1362. Message1(execinfo_x_codesize,tostr(secheader.sh_size));
  1363. status.codesize:=secheader.sh_size;
  1364. end
  1365. else if secname='.data' then
  1366. begin
  1367. Message1(execinfo_x_initdatasize,tostr(secheader.sh_size));
  1368. inc(status.datasize,secheader.sh_size);
  1369. end
  1370. else if secname='.bss' then
  1371. begin
  1372. Message1(execinfo_x_uninitdatasize,tostr(secheader.sh_size));
  1373. inc(status.datasize,secheader.sh_size);
  1374. end;
  1375. end;
  1376. close(f);
  1377. {$pop}
  1378. if ioresult<>0 then
  1379. ;
  1380. postprocessexecutable:=true;
  1381. end;
  1382. {*****************************************************************************
  1383. Initialize
  1384. *****************************************************************************}
  1385. initialization
  1386. {$ifdef arm}
  1387. RegisterLinker(ld_embedded,TLinkerEmbedded);
  1388. RegisterTarget(system_arm_embedded_info);
  1389. {$endif arm}
  1390. {$ifdef avr}
  1391. RegisterLinker(ld_embedded,TLinkerEmbedded);
  1392. RegisterTarget(system_avr_embedded_info);
  1393. {$endif avr}
  1394. {$ifdef i386}
  1395. RegisterLinker(ld_embedded,TLinkerEmbedded);
  1396. RegisterTarget(system_i386_embedded_info);
  1397. {$endif i386}
  1398. {$ifdef x86_64}
  1399. RegisterLinker(ld_embedded,TLinkerEmbedded);
  1400. RegisterTarget(system_x86_64_embedded_info);
  1401. {$endif x86_64}
  1402. {$ifdef i8086}
  1403. { no need to register linker ld_embedded, because i8086_embedded uses the
  1404. regular msdos linker. In case a flat binary, relocated for a specific
  1405. segment address is needed (e.g. for a BIOS or a real mode bootloader), it
  1406. can be produced post-compilation with exe2bin or a similar tool. }
  1407. RegisterTarget(system_i8086_embedded_info);
  1408. {$endif i8086}
  1409. {$ifdef mipsel}
  1410. RegisterLinker(ld_embedded,TLinkerEmbedded);
  1411. RegisterTarget(system_mipsel_embedded_info);
  1412. {$endif mipsel}
  1413. {$ifdef m68k}
  1414. RegisterLinker(ld_embedded,TLinkerEmbedded);
  1415. RegisterTarget(system_m68k_embedded_info);
  1416. {$endif m68k}
  1417. end.