t_freertos.pas 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979
  1. {
  2. Copyright (c) 2005-2017 by Free Pascal Compiler team
  3. This unit implements support import, export, link routines
  4. for the FreeRTOS 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_freertos;
  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_freertos,link,
  26. cpuinfo;
  27. type
  28. TlinkerFreeRTOS=class(texternallinker)
  29. private
  30. Function WriteResponseFile: Boolean;
  31. {$if defined(XTENSA) or defined(RISCV32)}
  32. procedure GenerateDefaultLinkerScripts(var memory_filename,sections_filename: AnsiString);
  33. {$endif (XTENSA) or defined(RISCV32)}
  34. public
  35. constructor Create; override;
  36. procedure SetDefaultInfo; override;
  37. function MakeExecutable:boolean; override;
  38. function postprocessexecutable(const fn : string;isdll:boolean):boolean;
  39. end;
  40. {*****************************************************************************
  41. TlinkerEmbedded
  42. *****************************************************************************}
  43. constructor TlinkerFreeRTOS.Create;
  44. begin
  45. Inherited Create;
  46. SharedLibFiles.doubles:=true;
  47. StaticLibFiles.doubles:=true;
  48. end;
  49. procedure TlinkerFreeRTOS.SetDefaultInfo;
  50. const
  51. {$ifdef mips}
  52. {$ifdef mipsel}
  53. platform_select='-EL';
  54. {$else}
  55. platform_select='-EB';
  56. {$endif}
  57. {$else}
  58. platform_select='';
  59. {$endif}
  60. var
  61. platformopt : string;
  62. begin
  63. {$ifdef xtensa}
  64. if target_info.endian=endian_little then
  65. platformopt:=' -b elf32-xtensa-le -m elf32xtensa'
  66. else
  67. platformopt:=' -b elf32-xtensa-be -m elf32xtensa';
  68. if target_info.abi=abi_xtensa_call0 then
  69. platformopt:=platformopt+' --abi-call0'
  70. else if target_info.abi=abi_xtensa_windowed then
  71. platformopt:=platformopt+' --abi-windowed';
  72. {$else}
  73. platformopt:='';
  74. {$endif}
  75. Info.ExeCmd[1]:='ld -g '+platform_select+platformopt+' $OPT $DYNLINK $STATIC $GCSECTIONS $STRIP $MAP -L. -o $EXE -T $RES';
  76. end;
  77. function TlinkerFreeRTOS.WriteResponseFile: Boolean;
  78. Var
  79. linkres : TLinkRes;
  80. i : longint;
  81. HPath : TCmdStrListItem;
  82. s,s1,s2 : TCmdStr;
  83. prtobj : string[80];
  84. linklibc : boolean;
  85. found1,
  86. found2 : boolean;
  87. {$if defined(ARM)}
  88. LinkStr : string;
  89. {$endif}
  90. begin
  91. WriteResponseFile:=False;
  92. linklibc:=(SharedLibFiles.Find('c')<>nil);
  93. {$if defined(ARM) or defined(i386) or defined(x86_64) or defined(AVR) or defined(MIPSEL) or defined(RISCV32) or defined(XTENSA)}
  94. prtobj:='';
  95. {$else}
  96. prtobj:='prt0';
  97. if linklibc then
  98. prtobj:='cprt0';
  99. {$endif}
  100. { Open link.res file }
  101. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  102. { Write path to search libraries }
  103. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  104. while assigned(HPath) do
  105. begin
  106. s:=HPath.Str;
  107. if (cs_link_on_target in current_settings.globalswitches) then
  108. s:=ScriptFixFileName(s);
  109. LinkRes.Add('-L'+s);
  110. HPath:=TCmdStrListItem(HPath.Next);
  111. end;
  112. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  113. while assigned(HPath) do
  114. begin
  115. s:=HPath.Str;
  116. if s<>'' then
  117. LinkRes.Add('SEARCH_DIR("'+s+'")');
  118. HPath:=TCmdStrListItem(HPath.Next);
  119. end;
  120. LinkRes.Add('INPUT (');
  121. { add objectfiles, start with prt0 always }
  122. //s:=FindObjectFile('prt0','',false);
  123. if prtobj<>'' then
  124. begin
  125. s:=FindObjectFile(prtobj,'',false);
  126. LinkRes.AddFileName(s);
  127. end;
  128. { xtensa FreeRTOS links always against libc, the runtime needs it }
  129. if not(target_info.system in [system_xtensa_freertos]) then
  130. begin
  131. { try to add crti and crtbegin if linking to C }
  132. if linklibc then
  133. begin
  134. if librarysearchpath.FindFile('crtbegin.o',false,s) then
  135. LinkRes.AddFileName(s);
  136. if librarysearchpath.FindFile('crti.o',false,s) then
  137. LinkRes.AddFileName(s);
  138. end;
  139. end;
  140. while not ObjectFiles.Empty do
  141. begin
  142. s:=ObjectFiles.GetFirst;
  143. if s<>'' then
  144. begin
  145. { vlink doesn't use SEARCH_DIR for object files }
  146. if not(cs_link_on_target in current_settings.globalswitches) then
  147. s:=FindObjectFile(s,'',false);
  148. if (idf_version>=50200) then
  149. LinkRes.AddFileName(ExtractFileName(maybequoted(s)))
  150. else
  151. LinkRes.AddFileName((maybequoted(s)));
  152. end;
  153. end;
  154. { Write staticlibraries }
  155. if not(StaticLibFiles.Empty) then
  156. begin
  157. LinkRes.Add(')');
  158. LinkRes.Add('GROUP(');
  159. while not StaticLibFiles.Empty do
  160. begin
  161. S:=StaticLibFiles.GetFirst;
  162. if (idf_version>=50200) then
  163. LinkRes.AddFileName(ExtractFileName(maybequoted(s)))
  164. else
  165. LinkRes.AddFileName((maybequoted(s)));
  166. end;
  167. end;
  168. { xtensa FreeRTOS links always against libc, the runtime needs it }
  169. if not(target_info.system in [system_xtensa_freertos]) then
  170. begin
  171. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  172. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  173. linklibc:=false;
  174. while not SharedLibFiles.Empty do
  175. begin
  176. S:=SharedLibFiles.GetFirst;
  177. if s<>'c' then
  178. begin
  179. i:=Pos(target_info.sharedlibext,S);
  180. if i>0 then
  181. Delete(S,i,255);
  182. LinkRes.Add('-l'+s);
  183. end
  184. else
  185. begin
  186. LinkRes.Add('-l'+s);
  187. linklibc:=true;
  188. end;
  189. end;
  190. { be sure that libc&libgcc is the last lib }
  191. if linklibc then
  192. begin
  193. LinkRes.Add('-lc');
  194. LinkRes.Add('-lgcc');
  195. end;
  196. end;
  197. LinkRes.Add(')');
  198. { xtensa FreeRTOS links always against libc }
  199. if not(target_info.system in [system_xtensa_freertos]) then
  200. begin
  201. { objects which must be at the end }
  202. if linklibc then
  203. begin
  204. found1:=librarysearchpath.FindFile('crtend.o',false,s1);
  205. found2:=librarysearchpath.FindFile('crtn.o',false,s2);
  206. if found1 or found2 then
  207. begin
  208. LinkRes.Add('INPUT(');
  209. if found1 then
  210. LinkRes.AddFileName(s1);
  211. if found2 then
  212. LinkRes.AddFileName(s2);
  213. LinkRes.Add(')');
  214. end;
  215. end;
  216. end;
  217. {$ifdef ARM}
  218. with embedded_controllers[current_settings.controllertype] do
  219. with linkres do
  220. begin
  221. Add('ENTRY(_START)');
  222. Add('MEMORY');
  223. Add('{');
  224. if flashsize<>0 then
  225. begin
  226. LinkStr := ' flash : ORIGIN = 0x' + IntToHex(flashbase,8)
  227. + ', LENGTH = 0x' + IntToHex(flashsize,8);
  228. Add(LinkStr);
  229. end;
  230. LinkStr := ' ram : ORIGIN = 0x' + IntToHex(srambase,8)
  231. + ', LENGTH = 0x' + IntToHex(sramsize,8);
  232. Add(LinkStr);
  233. Add('}');
  234. Add('_stack_top = 0x' + IntToHex(sramsize+srambase,8) + ';');
  235. Add('SECTIONS');
  236. Add('{');
  237. Add(' .text :');
  238. Add(' {');
  239. Add(' _text_start = .;');
  240. Add(' KEEP(*(.init .init.*))');
  241. Add(' *(.text .text.*)');
  242. Add(' *(.strings)');
  243. Add(' *(.rodata .rodata.*)');
  244. Add(' *(.comment)');
  245. Add(' . = ALIGN(4);');
  246. Add(' _etext = .;');
  247. if flashsize<>0 then
  248. begin
  249. Add(' } >flash');
  250. Add(' .note.gnu.build-id : { *(.note.gnu.build-id) } >flash ');
  251. end
  252. else
  253. begin
  254. Add(' } >ram');
  255. Add(' .note.gnu.build-id : { *(.note.gnu.build-id) } >ram ');
  256. end;
  257. Add(' .data :');
  258. Add(' {');
  259. Add(' _data = .;');
  260. Add(' *(.data .data.*)');
  261. Add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  262. Add(' _edata = .;');
  263. if flashsize<>0 then
  264. begin
  265. Add(' } >ram AT >flash');
  266. end
  267. else
  268. begin
  269. Add(' } >ram');
  270. end;
  271. Add(' .bss :');
  272. Add(' {');
  273. Add(' _bss_start = .;');
  274. Add(' *(.bss .bss.*)');
  275. Add(' *(COMMON)');
  276. Add(' } >ram');
  277. Add(' . = ALIGN(4);');
  278. Add(' _bss_end = . ;');
  279. Add('}');
  280. Add('_end = .;');
  281. end;
  282. {$endif ARM}
  283. {$ifdef i386}
  284. with linkres do
  285. begin
  286. Add('ENTRY(_START)');
  287. Add('SECTIONS');
  288. Add('{');
  289. Add(' . = 0x100000;');
  290. Add(' .text ALIGN (0x1000) :');
  291. Add(' {');
  292. Add(' _text = .;');
  293. Add(' KEEP(*(.init .init.*))');
  294. Add(' *(.text .text.*)');
  295. Add(' *(.strings)');
  296. Add(' *(.rodata .rodata.*)');
  297. Add(' *(.comment)');
  298. Add(' _etext = .;');
  299. Add(' }');
  300. Add(' .data ALIGN (0x1000) :');
  301. Add(' {');
  302. Add(' _data = .;');
  303. Add(' *(.data .data.*)');
  304. Add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  305. Add(' _edata = .;');
  306. Add(' }');
  307. Add(' . = ALIGN(4);');
  308. Add(' .bss :');
  309. Add(' {');
  310. Add(' _bss_start = .;');
  311. Add(' *(.bss .bss.*)');
  312. Add(' *(COMMON)');
  313. Add(' }');
  314. Add('_bss_end = . ;');
  315. Add('}');
  316. Add('_end = .;');
  317. end;
  318. {$endif i386}
  319. {$ifdef x86_64}
  320. with linkres do
  321. begin
  322. Add('ENTRY(_START)');
  323. Add('SECTIONS');
  324. Add('{');
  325. Add(' . = 0x100000;');
  326. Add(' .text ALIGN (0x1000) :');
  327. Add(' {');
  328. Add(' _text = .;');
  329. Add(' KEEP(*(.init .init.*))');
  330. Add(' *(.text .text.*)');
  331. Add(' *(.strings)');
  332. Add(' *(.rodata .rodata.*)');
  333. Add(' *(.comment)');
  334. Add(' _etext = .;');
  335. Add(' }');
  336. Add(' .data ALIGN (0x1000) :');
  337. Add(' {');
  338. Add(' _data = .;');
  339. Add(' *(.data .data.*)');
  340. Add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  341. Add(' _edata = .;');
  342. Add(' }');
  343. Add(' . = ALIGN(4);');
  344. Add(' .bss :');
  345. Add(' {');
  346. Add(' _bss_start = .;');
  347. Add(' *(.bss .bss.*)');
  348. Add(' *(COMMON)');
  349. Add(' }');
  350. Add('_bss_end = . ;');
  351. Add('}');
  352. Add('_end = .;');
  353. end;
  354. {$endif x86_64}
  355. {$ifdef AVR}
  356. with linkres do
  357. begin
  358. { linker script from ld 2.19 }
  359. Add('ENTRY(_START)');
  360. Add('OUTPUT_FORMAT("elf32-avr","elf32-avr","elf32-avr")');
  361. case current_settings.cputype of
  362. cpu_avr1:
  363. Add('OUTPUT_ARCH(avr:1)');
  364. cpu_avr2:
  365. Add('OUTPUT_ARCH(avr:2)');
  366. cpu_avr25:
  367. Add('OUTPUT_ARCH(avr:25)');
  368. cpu_avr3:
  369. Add('OUTPUT_ARCH(avr:3)');
  370. cpu_avr31:
  371. Add('OUTPUT_ARCH(avr:31)');
  372. cpu_avr35:
  373. Add('OUTPUT_ARCH(avr:35)');
  374. cpu_avr4:
  375. Add('OUTPUT_ARCH(avr:4)');
  376. cpu_avr5:
  377. Add('OUTPUT_ARCH(avr:5)');
  378. cpu_avr51:
  379. Add('OUTPUT_ARCH(avr:51)');
  380. cpu_avr6:
  381. Add('OUTPUT_ARCH(avr:6)');
  382. cpu_avrxmega3:
  383. Add('OUTPUT_ARCH(avr:103)');
  384. cpu_avrtiny:
  385. Add('OUTPUT_ARCH(avr:100)');
  386. else
  387. Internalerror(2015072701);
  388. end;
  389. Add('MEMORY');
  390. with embedded_controllers[current_settings.controllertype] do
  391. begin
  392. Add('{');
  393. Add(' text (rx) : ORIGIN = 0, LENGTH = 0x'+IntToHex(flashsize,6));
  394. Add(' data (rw!x) : ORIGIN = 0x'+IntToHex($800000+srambase,6)+', LENGTH = 0x'+IntToHex(sramsize,6));
  395. Add(' eeprom (rw!x) : ORIGIN = 0x810000, LENGTH = 0x'+IntToHex(eepromsize,6));
  396. Add(' fuse (rw!x) : ORIGIN = 0x820000, LENGTH = 1K');
  397. Add(' lock (rw!x) : ORIGIN = 0x830000, LENGTH = 1K');
  398. Add(' signature (rw!x) : ORIGIN = 0x840000, LENGTH = 1K');
  399. Add('}');
  400. Add('_stack_top = 0x' + IntToHex(srambase+sramsize-1,4) + ';');
  401. end;
  402. Add('SECTIONS');
  403. Add('{');
  404. Add(' /* Read-only sections, merged into text segment: */');
  405. Add(' .hash : { *(.hash) }');
  406. Add(' .dynsym : { *(.dynsym) }');
  407. Add(' .dynstr : { *(.dynstr) }');
  408. Add(' .gnu.version : { *(.gnu.version) }');
  409. Add(' .gnu.version_d : { *(.gnu.version_d) }');
  410. Add(' .gnu.version_r : { *(.gnu.version_r) }');
  411. Add(' .rel.init : { *(.rel.init) }');
  412. Add(' .rela.init : { *(.rela.init) }');
  413. Add(' .rel.text :');
  414. Add(' {');
  415. Add(' *(.rel.text)');
  416. Add(' *(.rel.text.*)');
  417. Add(' *(.rel.gnu.linkonce.t*)');
  418. Add(' }');
  419. Add(' .rela.text :');
  420. Add(' {');
  421. Add(' *(.rela.text)');
  422. Add(' *(.rela.text.*)');
  423. Add(' *(.rela.gnu.linkonce.t*)');
  424. Add(' }');
  425. Add(' .rel.fini : { *(.rel.fini) }');
  426. Add(' .rela.fini : { *(.rela.fini) }');
  427. Add(' .rel.rodata :');
  428. Add(' {');
  429. Add(' *(.rel.rodata)');
  430. Add(' *(.rel.rodata.*)');
  431. Add(' *(.rel.gnu.linkonce.r*)');
  432. Add(' }');
  433. Add(' .rela.rodata :');
  434. Add(' {');
  435. Add(' *(.rela.rodata)');
  436. Add(' *(.rela.rodata.*)');
  437. Add(' *(.rela.gnu.linkonce.r*)');
  438. Add(' }');
  439. Add(' .rel.data :');
  440. Add(' {');
  441. Add(' *(.rel.data)');
  442. Add(' *(.rel.data.*)');
  443. Add(' *(.rel.gnu.linkonce.d*)');
  444. Add(' }');
  445. Add(' .rela.data :');
  446. Add(' {');
  447. Add(' *(.rela.data)');
  448. Add(' *(.rela.data.*)');
  449. Add(' *(.rela.gnu.linkonce.d*)');
  450. Add(' }');
  451. Add(' .rel.ctors : { *(.rel.ctors) }');
  452. Add(' .rela.ctors : { *(.rela.ctors) }');
  453. Add(' .rel.dtors : { *(.rel.dtors) }');
  454. Add(' .rela.dtors : { *(.rela.dtors) }');
  455. Add(' .rel.got : { *(.rel.got) }');
  456. Add(' .rela.got : { *(.rela.got) }');
  457. Add(' .rel.bss : { *(.rel.bss) }');
  458. Add(' .rela.bss : { *(.rela.bss) }');
  459. Add(' .rel.plt : { *(.rel.plt) }');
  460. Add(' .rela.plt : { *(.rela.plt) }');
  461. Add(' /* Internal text space or external memory. */');
  462. Add(' .text :');
  463. Add(' {');
  464. Add(' KEEP(*(.init .init.*))');
  465. Add(' /* For data that needs to reside in the lower 64k of progmem. */');
  466. Add(' *(.progmem.gcc*)');
  467. Add(' *(.progmem*)');
  468. Add(' . = ALIGN(2);');
  469. Add(' __trampolines_start = . ;');
  470. Add(' /* The jump trampolines for the 16-bit limited relocs will reside here. */');
  471. Add(' *(.trampolines)');
  472. Add(' *(.trampolines*)');
  473. Add(' __trampolines_end = . ;');
  474. Add(' /* For future tablejump instruction arrays for 3 byte pc devices.');
  475. Add(' We don''t relax jump/call instructions within these sections. */');
  476. Add(' *(.jumptables)');
  477. Add(' *(.jumptables*)');
  478. Add(' /* For code that needs to reside in the lower 128k progmem. */');
  479. Add(' *(.lowtext)');
  480. Add(' *(.lowtext*)');
  481. Add(' __ctors_start = . ;');
  482. Add(' *(.ctors)');
  483. Add(' __ctors_end = . ;');
  484. Add(' __dtors_start = . ;');
  485. Add(' *(.dtors)');
  486. Add(' __dtors_end = . ;');
  487. Add(' KEEP(SORT(*)(.ctors))');
  488. Add(' KEEP(SORT(*)(.dtors))');
  489. Add(' /* From this point on, we don''t bother about wether the insns are');
  490. Add(' below or above the 16 bits boundary. */');
  491. Add(' *(.init0) /* Start here after reset. */');
  492. Add(' KEEP (*(.init0))');
  493. Add(' *(.init1)');
  494. Add(' KEEP (*(.init1))');
  495. Add(' *(.init2) /* Clear __zero_reg__, set up stack pointer. */');
  496. Add(' KEEP (*(.init2))');
  497. Add(' *(.init3)');
  498. Add(' KEEP (*(.init3))');
  499. Add(' *(.init4) /* Initialize data and BSS. */');
  500. Add(' KEEP (*(.init4))');
  501. Add(' *(.init5)');
  502. Add(' KEEP (*(.init5))');
  503. Add(' *(.init6) /* C++ constructors. */');
  504. Add(' KEEP (*(.init6))');
  505. Add(' *(.init7)');
  506. Add(' KEEP (*(.init7))');
  507. Add(' *(.init8)');
  508. Add(' KEEP (*(.init8))');
  509. Add(' *(.init9) /* Call main(). */');
  510. Add(' KEEP (*(.init9))');
  511. Add(' *(.text)');
  512. Add(' . = ALIGN(2);');
  513. Add(' *(.text.*)');
  514. Add(' . = ALIGN(2);');
  515. Add(' *(.fini9) /* _exit() starts here. */');
  516. Add(' KEEP (*(.fini9))');
  517. Add(' *(.fini8)');
  518. Add(' KEEP (*(.fini8))');
  519. Add(' *(.fini7)');
  520. Add(' KEEP (*(.fini7))');
  521. Add(' *(.fini6) /* C++ destructors. */');
  522. Add(' KEEP (*(.fini6))');
  523. Add(' *(.fini5)');
  524. Add(' KEEP (*(.fini5))');
  525. Add(' *(.fini4)');
  526. Add(' KEEP (*(.fini4))');
  527. Add(' *(.fini3)');
  528. Add(' KEEP (*(.fini3))');
  529. Add(' *(.fini2)');
  530. Add(' KEEP (*(.fini2))');
  531. Add(' *(.fini1)');
  532. Add(' KEEP (*(.fini1))');
  533. Add(' *(.fini0) /* Infinite loop after program termination. */');
  534. Add(' KEEP (*(.fini0))');
  535. Add(' _etext = . ;');
  536. Add(' } > text');
  537. Add(' .data : AT (ADDR (.text) + SIZEOF (.text))');
  538. Add(' {');
  539. Add(' PROVIDE (__data_start = .) ;');
  540. Add(' *(.data)');
  541. Add(' *(.data*)');
  542. Add(' *(.rodata) /* We need to include .rodata here if gcc is used */');
  543. Add(' *(.rodata*) /* with -fdata-sections. */');
  544. Add(' *(.gnu.linkonce.d*)');
  545. Add(' . = ALIGN(2);');
  546. Add(' _edata = . ;');
  547. Add(' PROVIDE (__data_end = .) ;');
  548. Add(' } > data');
  549. Add(' .bss : AT (ADDR (.bss))');
  550. Add(' {');
  551. Add(' PROVIDE (__bss_start = .) ;');
  552. Add(' *(.bss)');
  553. Add(' *(.bss*)');
  554. Add(' *(COMMON)');
  555. Add(' PROVIDE (__bss_end = .) ;');
  556. Add(' } > data');
  557. Add(' __data_load_start = LOADADDR(.data);');
  558. Add(' __data_load_end = __data_load_start + SIZEOF(.data);');
  559. Add(' /* Global data not cleared after reset. */');
  560. Add(' .noinit :');
  561. Add(' {');
  562. Add(' PROVIDE (__noinit_start = .) ;');
  563. Add(' *(.noinit*)');
  564. Add(' PROVIDE (__noinit_end = .) ;');
  565. Add(' _end = . ;');
  566. Add(' PROVIDE (__heap_start = .) ;');
  567. Add(' } > data');
  568. Add(' .eeprom :');
  569. Add(' {');
  570. Add(' *(.eeprom*)');
  571. Add(' __eeprom_end = . ;');
  572. Add(' } > eeprom');
  573. Add(' .fuse :');
  574. Add(' {');
  575. Add(' KEEP(*(.fuse))');
  576. Add(' KEEP(*(.lfuse))');
  577. Add(' KEEP(*(.hfuse))');
  578. Add(' KEEP(*(.efuse))');
  579. Add(' } > fuse');
  580. Add(' .lock :');
  581. Add(' {');
  582. Add(' KEEP(*(.lock*))');
  583. Add(' } > lock');
  584. Add(' .signature :');
  585. Add(' {');
  586. Add(' KEEP(*(.signature*))');
  587. Add(' } > signature');
  588. Add(' /* Stabs debugging sections. */');
  589. Add(' .stab 0 : { *(.stab) }');
  590. Add(' .stabstr 0 : { *(.stabstr) }');
  591. Add(' .stab.excl 0 : { *(.stab.excl) }');
  592. Add(' .stab.exclstr 0 : { *(.stab.exclstr) }');
  593. Add(' .stab.index 0 : { *(.stab.index) }');
  594. Add(' .stab.indexstr 0 : { *(.stab.indexstr) }');
  595. Add(' .comment 0 : { *(.comment) }');
  596. Add(' /* DWARF debug sections.');
  597. Add(' Symbols in the DWARF debugging sections are relative to the beginning');
  598. Add(' of the section so we begin them at 0. */');
  599. Add(' /* DWARF 1 */');
  600. Add(' .debug 0 : { *(.debug) }');
  601. Add(' .line 0 : { *(.line) }');
  602. Add(' /* GNU DWARF 1 extensions */');
  603. Add(' .debug_srcinfo 0 : { *(.debug_srcinfo) }');
  604. Add(' .debug_sfnames 0 : { *(.debug_sfnames) }');
  605. Add(' /* DWARF 1.1 and DWARF 2 */');
  606. Add(' .debug_aranges 0 : { *(.debug_aranges) }');
  607. Add(' .debug_pubnames 0 : { *(.debug_pubnames) }');
  608. Add(' /* DWARF 2 */');
  609. Add(' .debug_info 0 : { *(.debug_info) *(.gnu.linkonce.wi.*) }');
  610. Add(' .debug_abbrev 0 : { *(.debug_abbrev) }');
  611. Add(' .debug_line 0 : { *(.debug_line) }');
  612. Add(' .debug_frame 0 : { *(.debug_frame) }');
  613. Add(' .debug_str 0 : { *(.debug_str) }');
  614. Add(' .debug_loc 0 : { *(.debug_loc) }');
  615. Add(' .debug_macinfo 0 : { *(.debug_macinfo) }');
  616. Add('}');
  617. end;
  618. {$endif AVR}
  619. {$ifdef MIPSEL}
  620. case current_settings.controllertype of
  621. ct_none:
  622. begin
  623. end;
  624. ct_pic32mx110f016b,
  625. ct_pic32mx110f016c,
  626. ct_pic32mx110f016d,
  627. ct_pic32mx120f032b,
  628. ct_pic32mx120f032c,
  629. ct_pic32mx120f032d,
  630. ct_pic32mx130f064b,
  631. ct_pic32mx130f064c,
  632. ct_pic32mx130f064d,
  633. ct_pic32mx150f128b,
  634. ct_pic32mx150f128c,
  635. ct_pic32mx150f128d,
  636. ct_pic32mx210f016b,
  637. ct_pic32mx210f016c,
  638. ct_pic32mx210f016d,
  639. ct_pic32mx220f032b,
  640. ct_pic32mx220f032c,
  641. ct_pic32mx220f032d,
  642. ct_pic32mx230f064b,
  643. ct_pic32mx230f064c,
  644. ct_pic32mx230f064d,
  645. ct_pic32mx250f128b,
  646. ct_pic32mx250f128c,
  647. ct_pic32mx250f128d,
  648. ct_pic32mx775f256h,
  649. ct_pic32mx775f256l,
  650. ct_pic32mx775f512h,
  651. ct_pic32mx775f512l,
  652. ct_pic32mx795f512h,
  653. ct_pic32mx795f512l:
  654. begin
  655. with embedded_controllers[current_settings.controllertype] do
  656. with linkres do
  657. begin
  658. Add('OUTPUT_FORMAT("elf32-tradlittlemips")');
  659. Add('OUTPUT_ARCH(pic32mx)');
  660. Add('ENTRY(_reset)');
  661. Add('PROVIDE(_vector_spacing = 0x00000001);');
  662. Add('_ebase_address = 0x'+IntToHex(flashbase,8)+';');
  663. Add('_RESET_ADDR = 0xBFC00000;');
  664. Add('_BEV_EXCPT_ADDR = 0xBFC00380;');
  665. Add('_DBG_EXCPT_ADDR = 0xBFC00480;');
  666. Add('_GEN_EXCPT_ADDR = _ebase_address + 0x180;');
  667. Add('MEMORY');
  668. Add('{');
  669. if flashsize<>0 then
  670. begin
  671. Add(' kseg0_program_mem : ORIGIN = 0x'+IntToHex(flashbase,8)+', LENGTH = 0x'+IntToHex(flashsize,8));
  672. //TODO This should better be placed into the controllertype records
  673. Add(' kseg1_boot_mem : ORIGIN = 0xBFC00000, LENGTH = 0xbef');
  674. Add(' config3 : ORIGIN = 0xBFC00BF0, LENGTH = 0x4');
  675. Add(' config2 : ORIGIN = 0xBFC00BF4, LENGTH = 0x4');
  676. Add(' config1 : ORIGIN = 0xBFC00BF8, LENGTH = 0x4');
  677. Add(' config0 : ORIGIN = 0xBFC00BFC, LENGTH = 0x4');
  678. end;
  679. Add(' ram : ORIGIN = 0x' + IntToHex(srambase,8)
  680. + ', LENGTH = 0x' + IntToHex(sramsize,8));
  681. Add('}');
  682. Add('_stack_top = 0x' + IntToHex(sramsize+srambase,8) + ';');
  683. end;
  684. end
  685. end;
  686. with linkres do
  687. begin
  688. Add('SECTIONS');
  689. Add('{');
  690. Add(' .reset _RESET_ADDR :');
  691. Add(' {');
  692. Add(' KEEP(*(.reset .reset.*))');
  693. Add(' KEEP(*(.startup .startup.*))');
  694. Add(' } > kseg1_boot_mem');
  695. Add(' .bev_excpt _BEV_EXCPT_ADDR :');
  696. Add(' {');
  697. Add(' KEEP(*(.bev_handler))');
  698. Add(' } > kseg1_boot_mem');
  699. Add(' .text :');
  700. Add(' {');
  701. Add(' _text_start = .;');
  702. Add(' . = _text_start + 0x180;');
  703. Add(' KEEP(*(.gen_handler))');
  704. Add(' . = _text_start + 0x200;');
  705. Add(' KEEP(*(.init .init.*))');
  706. Add(' *(.text .text.*)');
  707. Add(' *(.strings)');
  708. Add(' *(.rodata .rodata.*)');
  709. Add(' *(.comment)');
  710. Add(' _etext = .;');
  711. if embedded_controllers[current_settings.controllertype].flashsize<>0 then
  712. begin
  713. Add(' } >kseg0_program_mem');
  714. end
  715. else
  716. begin
  717. Add(' } >ram');
  718. end;
  719. Add(' .note.gnu.build-id : { *(.note.gnu.build-id) }');
  720. Add(' .data :');
  721. Add(' {');
  722. Add(' _data = .;');
  723. Add(' *(.data .data.*)');
  724. Add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  725. Add(' . = .;');
  726. Add(' _gp = ALIGN(16) + 0x7ff0;');
  727. Add(' _edata = .;');
  728. if embedded_controllers[current_settings.controllertype].flashsize<>0 then
  729. begin
  730. Add(' } >ram AT >kseg0_program_mem');
  731. end
  732. else
  733. begin
  734. Add(' } >ram');
  735. end;
  736. Add(' .config_BFC00BF0 : {');
  737. Add(' KEEP(*(.config_BFC00BF0))');
  738. Add(' } > config3');
  739. Add(' .config_BFC00BF4 : {');
  740. Add(' KEEP(*(.config_BFC00BF4))');
  741. Add(' } > config2');
  742. Add(' .config_BFC00BF8 : {');
  743. Add(' KEEP(*(.config_BFC00BF8))');
  744. Add(' } > config1');
  745. Add(' .config_BFC00BFC : {');
  746. Add(' KEEP(*(.config_BFC00BFC))');
  747. Add(' } > config0');
  748. Add(' .bss :');
  749. Add(' {');
  750. Add(' _bss_start = .;');
  751. Add(' *(.bss .bss.*)');
  752. Add(' *(COMMON)');
  753. Add(' } >ram');
  754. Add('. = ALIGN(4);');
  755. Add('_bss_end = . ;');
  756. Add(' .comment 0 : { *(.comment) }');
  757. Add(' /* DWARF debug sections.');
  758. Add(' Symbols in the DWARF debugging sections are relative to the beginning');
  759. Add(' of the section so we begin them at 0. */');
  760. Add(' /* DWARF 1 */');
  761. Add(' .debug 0 : { *(.debug) }');
  762. Add(' .line 0 : { *(.line) }');
  763. Add(' /* GNU DWARF 1 extensions */');
  764. Add(' .debug_srcinfo 0 : { *(.debug_srcinfo) }');
  765. Add(' .debug_sfnames 0 : { *(.debug_sfnames) }');
  766. Add(' /* DWARF 1.1 and DWARF 2 */');
  767. Add(' .debug_aranges 0 : { *(.debug_aranges) }');
  768. Add(' .debug_pubnames 0 : { *(.debug_pubnames) }');
  769. Add(' /* DWARF 2 */');
  770. Add(' .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) }');
  771. Add(' .debug_abbrev 0 : { *(.debug_abbrev) }');
  772. Add(' /DISCARD/ : { *(.debug_line) }');
  773. Add(' .debug_frame 0 : { *(.debug_frame) }');
  774. Add(' .debug_str 0 : { *(.debug_str) }');
  775. Add(' /DISCARD/ : { *(.debug_loc) }');
  776. Add(' .debug_macinfo 0 : { *(.debug_macinfo) }');
  777. Add(' /* SGI/MIPS DWARF 2 extensions */');
  778. Add(' .debug_weaknames 0 : { *(.debug_weaknames) }');
  779. Add(' .debug_funcnames 0 : { *(.debug_funcnames) }');
  780. Add(' .debug_typenames 0 : { *(.debug_typenames) }');
  781. Add(' .debug_varnames 0 : { *(.debug_varnames) }');
  782. Add(' /* DWARF 3 */');
  783. Add(' .debug_pubtypes 0 : { *(.debug_pubtypes) }');
  784. Add(' .debug_ranges 0 : { *(.debug_ranges) }');
  785. Add(' .gnu.attributes 0 : { KEEP (*(.gnu.attributes)) }');
  786. Add(' .gptab.sdata : { *(.gptab.data) *(.gptab.sdata) }');
  787. Add(' .gptab.sbss : { *(.gptab.bss) *(.gptab.sbss) }');
  788. Add(' .mdebug.abi32 : { KEEP(*(.mdebug.abi32)) }');
  789. Add(' .mdebug.abiN32 : { KEEP(*(.mdebug.abiN32)) }');
  790. Add(' .mdebug.abi64 : { KEEP(*(.mdebug.abi64)) }');
  791. Add(' .mdebug.abiO64 : { KEEP(*(.mdebug.abiO64)) }');
  792. Add(' .mdebug.eabi32 : { KEEP(*(.mdebug.eabi32)) }');
  793. Add(' .mdebug.eabi64 : { KEEP(*(.mdebug.eabi64)) }');
  794. Add(' /DISCARD/ : { *(.rel.dyn) }');
  795. Add(' /DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) *(.gnu.lto_*) }');
  796. Add('}');
  797. Add('_end = .;');
  798. end;
  799. {$endif MIPSEL}
  800. {$ifdef RISCV32}
  801. with linkres do
  802. begin
  803. Add('MEMORY');
  804. Add('{');
  805. Add(' dummy : org = 0x0, len = 0x100');
  806. Add('}');
  807. Add('SECTIONS');
  808. Add('{');
  809. Add(' .data :');
  810. Add(' {');
  811. Add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  812. Add(' } > dummy');
  813. Add('}');
  814. end;
  815. {$endif RISCV32}
  816. {$ifdef XTENSA}
  817. with linkres do
  818. begin
  819. Add('SECTIONS');
  820. Add('{');
  821. Add(' .data :');
  822. Add(' {');
  823. Add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  824. Add(' }');
  825. Add('}');
  826. end;
  827. {$endif XTENSA}
  828. { Write and Close response }
  829. linkres.writetodisk;
  830. linkres.free;
  831. WriteResponseFile:=True;
  832. end;
  833. {$ifdef XTENSA}
  834. { If espX.project.ld or espX_out.ld scripts cannot be located, generate
  835. default scripts so that linking can proceed. Note: the generated
  836. scripts may not match the actual options chosen when the libraries
  837. were built. }
  838. procedure TlinkerFreeRTOS.GenerateDefaultLinkerScripts(var memory_filename,
  839. sections_filename: AnsiString);
  840. type
  841. Tesp_idf_index=(esp32_v4_2=0,esp32_v4_4,esp8266_v3_3);
  842. const
  843. esp_fragment_list: array[esp32_v4_2..esp8266_v3_3] of array of string=(
  844. ('xtensa/linker',
  845. 'soc/linker',
  846. 'esp_event/linker',
  847. 'spi_flash/linker',
  848. 'esp_wifi/linker',
  849. 'lwip/linker',
  850. 'heap/linker',
  851. 'esp_ringbuf/linker',
  852. 'espcoredump/linker',
  853. 'esp32/linker',
  854. 'esp32/ld/esp32_fragments',
  855. 'freertos/linker',
  856. 'newlib/newlib',
  857. 'esp_gdbstub/linker'),
  858. ('driver/linker',
  859. 'esp_pm/linker',
  860. 'spi_flash/linker',
  861. 'esp_gdbstub/linker',
  862. 'espcoredump/linker',
  863. 'esp_phy/linker',
  864. 'esp_system/linker',
  865. 'esp_system/app',
  866. 'hal/linker',
  867. 'esp_event/linker',
  868. 'esp_wifi/linker',
  869. 'lwip/linker',
  870. 'log/linker',
  871. 'heap/linker',
  872. 'soc/linker',
  873. 'esp_hw_support/linker',
  874. 'xtensa/linker',
  875. 'esp_common/common',
  876. 'esp_common/soc',
  877. 'freertos/linker',
  878. 'newlib/newlib',
  879. 'newlib/system_libs',
  880. 'app_trace/linker',
  881. 'bt/linker'),
  882. ('esp8266/ld/esp8266_fragments',
  883. 'esp8266/ld/esp8266_bss_fragments',
  884. 'esp8266/linker',
  885. 'freertos/linker',
  886. 'log/linker',
  887. 'lwip/linker',
  888. 'spi_flash/linker'));
  889. var
  890. S: Ansistring;
  891. t: Text;
  892. hp: TCmdStrListItem;
  893. filepath: TCmdStr = '';
  894. i,j: integer;
  895. idf_index: Tesp_idf_index;
  896. lib,
  897. binstr,
  898. cmdstr: AnsiString;
  899. success: boolean;
  900. begin
  901. { generate a sdkconfig.h if none is provided,
  902. only a few fields are provided to far.
  903. Assume that if linker scripts are not located,
  904. sdkconfig.h is also missing }
  905. Assign(t,outputexedir+'/sdkconfig.h');
  906. {$push}{$I-}
  907. Rewrite(t);
  908. if ioresult<>0 then
  909. exit;
  910. if (current_settings.controllertype = ct_esp32) then
  911. begin
  912. writeln(t,'#pragma once');
  913. writeln(t,'#define CONFIG_APP_BUILD_USE_FLASH_SECTIONS 1');
  914. writeln(t,'#define CONFIG_BT_RESERVE_DRAM 0x0');
  915. writeln(t,'#define CONFIG_ESP32_ULP_COPROC_RESERVE_MEM 0');
  916. writeln(t,'#define CONFIG_ESP32_TRACEMEM_RESERVE_DRAM 0x0');
  917. end
  918. else
  919. begin
  920. { TODO: APP_OFFSET & APP_SIZE depends on partition table
  921. Default for partition table: Single factory app, no OTA }
  922. writeln(t,'#define APP_OFFSET 0x10000');
  923. writeln(t,'#define APP_SIZE 0xf0000');
  924. { Include build version of sdkconfig.h for custom configuration, if available }
  925. S:=idfpath+'/libs/sdkconfig.h';
  926. if SysUtils.FileExists(S) then
  927. writeln(t,'#include "'+S+'"')
  928. else
  929. { Assume SOC_FULL_ICACHE option not selected (default) }
  930. writeln(t,'#define CONFIG_SOC_IRAM_SIZE 0xC000');
  931. end;
  932. Close(t);
  933. if ioresult<>0 then
  934. exit;
  935. {$pop}
  936. { generate an sdkconfig if none is provided,
  937. this is a dummy so far }
  938. if not(Sysutils.FileExists(outputexedir+'/sdkconfig')) then
  939. begin
  940. Assign(t,outputexedir+'/sdkconfig');
  941. {$push}{$I-}
  942. Rewrite(t);
  943. if ioresult<>0 then
  944. exit;
  945. writeln(t);
  946. Close(t);
  947. if ioresult<>0 then
  948. exit;
  949. {$pop}
  950. end;
  951. { generate an Kconfig if none is provided,
  952. this is a dummy so far }
  953. if not(Sysutils.FileExists(outputexedir+'/Kconfig')) then
  954. begin
  955. Assign(t,outputexedir+'/Kconfig');
  956. {$push}{$I-}
  957. Rewrite(t);
  958. if ioresult<>0 then
  959. exit;
  960. writeln(t);
  961. Close(t);
  962. if ioresult<>0 then
  963. exit;
  964. {$pop}
  965. end;
  966. { generate an Kconfig.projbuild if none is provided,
  967. this is a dummy so far }
  968. if not(Sysutils.FileExists(outputexedir+'/Kconfig.projbuild')) then
  969. begin
  970. Assign(t,outputexedir+'/Kconfig.projbuild');
  971. {$push}{$I-}
  972. Rewrite(t);
  973. if ioresult<>0 then
  974. exit;
  975. writeln(t);
  976. Close(t);
  977. if ioresult<>0 then
  978. exit;
  979. {$pop}
  980. end;
  981. { generate an kconfigs.in if none is provided,
  982. this is a dummy so far }
  983. if not(Sysutils.FileExists(outputexedir+'/kconfigs.in')) then
  984. begin
  985. Assign(t,outputexedir+'/kconfigs.in');
  986. {$push}{$I-}
  987. Rewrite(t);
  988. if ioresult<>0 then
  989. exit;
  990. writeln(t);
  991. Close(t);
  992. if ioresult<>0 then
  993. exit;
  994. {$pop}
  995. end;
  996. { generate an kconfigs_projbuild.in if none is provided,
  997. this is a dummy so far }
  998. if not(Sysutils.FileExists(outputexedir+'/kconfigs_projbuild.in')) then
  999. begin
  1000. Assign(t,outputexedir+'/kconfigs_projbuild.in');
  1001. {$push}{$I-}
  1002. Rewrite(t);
  1003. if ioresult<>0 then
  1004. exit;
  1005. writeln(t);
  1006. Close(t);
  1007. if ioresult<>0 then
  1008. exit;
  1009. {$pop}
  1010. end;
  1011. { generate a config.env if none is provided,
  1012. COMPONENT_KCONFIGS and COMPONENT_KCONFIGS_PROJBUILD are dummy fields and might
  1013. be needed to be filed properly }
  1014. Assign(t,outputexedir+'/config.env');
  1015. {$push}{$I-}
  1016. Rewrite(t);
  1017. if ioresult<>0 then
  1018. exit;
  1019. writeln(t,'{');
  1020. if (current_settings.controllertype = ct_esp32) then
  1021. begin
  1022. writeln(t,' "COMPONENT_KCONFIGS": "Kconfig",');
  1023. writeln(t,' "COMPONENT_KCONFIGS_PROJBUILD": "Kconfig.projbuild",');
  1024. writeln(t,' "IDF_CMAKE": "y",');
  1025. writeln(t,' "IDF_TARGET": "esp32",');
  1026. writeln(t,' "IDF_PATH": "'+TargetFixPath(idfpath,false)+'",');
  1027. writeln(t,' "COMPONENT_KCONFIGS_SOURCE_FILE": "'+outputexedir+'/kconfigs.in",');
  1028. writeln(t,' "COMPONENT_KCONFIGS_PROJBUILD_SOURCE_FILE": "'+outputexedir+'/kconfigs_projbuild.in"');
  1029. end
  1030. else
  1031. begin
  1032. writeln(t,' "IDF_PATH": "'+TargetFixPath(idfpath,false)+'",');
  1033. writeln(t,' "IDF_TARGET": "esp8266",');
  1034. writeln(t,' "IDF_CMAKE": "n"');
  1035. end;
  1036. writeln(t,'}');
  1037. Close(t);
  1038. if ioresult<>0 then
  1039. exit;
  1040. {$pop}
  1041. { generate ldgen_libraries }
  1042. Assign(t,outputexedir+'/ldgen_libraries');
  1043. {$push}{$I-}
  1044. Rewrite(t);
  1045. if ioresult<>0 then
  1046. exit;
  1047. { extract libraries from linker options and add to static libraries list }
  1048. Info.ExtraOptions:=trim(Info.ExtraOptions);
  1049. i := pos('-l', Info.ExtraOptions);
  1050. while i > 0 do
  1051. begin
  1052. j:=pos(' ',Info.ExtraOptions);
  1053. if j=0 then
  1054. j:=length(Info.ExtraOptions)+1;
  1055. lib:=copy(Info.ExtraOptions,i+2,j-i-2);
  1056. AddStaticCLibrary(lib);
  1057. delete(Info.ExtraOptions,i,j);
  1058. trim(Info.ExtraOptions);
  1059. i := pos('-l', Info.ExtraOptions);
  1060. end;
  1061. hp:=TCmdStrListItem(StaticLibFiles.First);
  1062. while assigned(hp) do
  1063. begin
  1064. FindLibraryFile(hp.Str,target_info.staticClibprefix,target_info.staticClibext,filepath);
  1065. writeln(t,filepath);
  1066. hp:=TCmdStrListItem(hp.Next);
  1067. end;
  1068. Close(t);
  1069. if ioresult<>0 then
  1070. exit;
  1071. {$pop}
  1072. memory_filename:=IncludeTrailingPathDelimiter(outputexedir)+memory_filename;
  1073. cmdstr:='-C -P -x c -E -o '+memory_filename+' -I $OUTPUT ';
  1074. binstr:='gcc';
  1075. if current_settings.controllertype = ct_none then
  1076. Message(exec_f_controllertype_expected)
  1077. else if current_settings.controllertype = ct_esp32 then
  1078. begin
  1079. if idf_version>=40400 then
  1080. cmdstr:=cmdstr+'-I $IDF_PATH/components/esp_system/ld $IDF_PATH/components/esp_system/ld/esp32/memory.ld.in'
  1081. else
  1082. cmdstr:=cmdstr+'$IDF_PATH/components/esp32/ld/esp32.ld';
  1083. end
  1084. else
  1085. cmdstr:=cmdstr+'$IDF_PATH/components/esp8266/ld/esp8266.ld';
  1086. Replace(cmdstr,'$IDF_PATH',idfpath);
  1087. Replace(cmdstr,'$OUTPUT',outputexedir);
  1088. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,true);
  1089. { generate linker maps }
  1090. {$ifdef UNIX}
  1091. binstr:=TargetFixPath(idfpath,false)+'/tools/ldgen/ldgen.py';
  1092. {$else}
  1093. binstr:='python';
  1094. {$endif UNIX}
  1095. if source_info.exeext<>'' then
  1096. binstr:=binstr+source_info.exeext;
  1097. sections_filename:=IncludeTrailingPathDelimiter(outputexedir)+sections_filename;
  1098. cmdstr:={$ifndef UNIX}'$IDF_PATH/tools/ldgen/ldgen.py '+{$endif UNIX}
  1099. '--config $OUTPUT/sdkconfig --fragments';
  1100. { Pick corresponding linker fragments list for SDK version }
  1101. if (current_settings.controllertype = ct_esp32) then
  1102. if idf_version>=40400 then
  1103. idf_index:=esp32_v4_4
  1104. else
  1105. idf_index:=esp32_v4_2
  1106. else
  1107. idf_index:=esp8266_v3_3;
  1108. for S in esp_fragment_list[idf_index] do
  1109. cmdstr:=cmdstr+' $IDF_PATH/components/'+S+'.lf';
  1110. if (current_settings.controllertype = ct_esp32) then
  1111. begin
  1112. if idf_version>=40400 then
  1113. cmdstr:=cmdstr+' --input $IDF_PATH/components/esp_system/ld/esp32/sections.ld.in'
  1114. else
  1115. cmdstr:=cmdstr+' --input $IDF_PATH/components/esp32/ld/esp32.project.ld.in';
  1116. end
  1117. else
  1118. begin
  1119. cmdstr:=cmdstr+
  1120. ' --env "COMPONENT_KCONFIGS_PROJBUILD= $IDF_PATH/components/bootloader/Kconfig.projbuild'+
  1121. ' $IDF_PATH/components/esptool_py/Kconfig.projbuild $IDF_PATH/components/partition_table/Kconfig.projbuild"'+
  1122. ' --env "COMPONENT_KCONFIGS=$IDF_PATH/components/app_update/Kconfig'+
  1123. ' $IDF_PATH/components/esp8266/Kconfig $IDF_PATH/components/freertos/Kconfig'+
  1124. ' $IDF_PATH/components/log/Kconfig $IDF_PATH/components/lwip/Kconfig"'+
  1125. ' --input $IDF_PATH/components/esp8266/ld/esp8266.project.ld.in';
  1126. end;
  1127. S:=FindUtil(utilsprefix+'objdump');
  1128. cmdstr:=cmdstr+' --output '+sections_filename+
  1129. ' --kconfig $IDF_PATH/Kconfig'+
  1130. ' --env-file $OUTPUT/config.env'+
  1131. ' --libraries-file $OUTPUT/ldgen_libraries'+
  1132. ' --objdump '+S;
  1133. Replace(cmdstr,'$IDF_PATH',idfpath);
  1134. Replace(cmdstr,'$OUTPUT',outputexedir);
  1135. if success then
  1136. success:=DoExec(binstr,cmdstr,true,false);
  1137. end;
  1138. {$endif XTENSA}
  1139. {$ifdef RISCV32}
  1140. { If espX.project.ld or espX_out.ld scripts cannot be located, generate
  1141. default scripts so that linking can proceed. Note: the generated
  1142. scripts may not match the actual options chosen when the libraries
  1143. were built. }
  1144. procedure TlinkerFreeRTOS.GenerateDefaultLinkerScripts(var memory_filename,
  1145. sections_filename: AnsiString);
  1146. type
  1147. Tesp_idf_index=(esp32c3_v5_0=0);
  1148. const
  1149. esp_fragment_list: array[esp32c3_v5_0..esp32c3_v5_0] of array of string=(
  1150. ('riscv/linker',
  1151. 'esp_ringbuf/linker',
  1152. 'driver/linker',
  1153. 'esp_pm/linker',
  1154. 'esp_mm/linker',
  1155. 'spi_flash/linker',
  1156. 'esp_system/linker',
  1157. 'esp_system/app',
  1158. 'esp_rom/linker',
  1159. 'hal/linker',
  1160. 'log/linker',
  1161. 'heap/linker',
  1162. 'soc/linker',
  1163. 'esp_hw_support/linker',
  1164. 'freertos/linker',
  1165. 'freertos/linker_common',
  1166. 'newlib/newlib',
  1167. 'newlib/system_libs',
  1168. 'esp_common/common',
  1169. 'esp_common/soc',
  1170. 'app_trace/linker',
  1171. 'esp_event/linker',
  1172. 'esp_phy/linker',
  1173. 'lwip/linker',
  1174. 'esp_netif/linker',
  1175. 'esp_wifi/linker',
  1176. 'bt/linker',
  1177. 'esp_adc/linker',
  1178. 'esp_gdbstub/linker',
  1179. 'esp_lcd/linker',
  1180. 'esp_psram/linker',
  1181. 'espcoredump/linker'));
  1182. var
  1183. S: Ansistring;
  1184. t: Text;
  1185. hp: TCmdStrListItem;
  1186. filepath: TCmdStr = '';
  1187. i,j: integer;
  1188. idf_index: Tesp_idf_index;
  1189. lib,
  1190. binstr,
  1191. cmdstr: AnsiString;
  1192. success: boolean;
  1193. begin
  1194. { generate a sdkconfig.h if none is provided,
  1195. only a few fields are provided to far.
  1196. Assume that if linker scripts are not located,
  1197. sdkconfig.h is also missing }
  1198. Assign(t,outputexedir+'/sdkconfig.h');
  1199. {$push}{$I-}
  1200. Rewrite(t);
  1201. if ioresult<>0 then
  1202. exit;
  1203. if (current_settings.controllertype = ct_esp32c3) then
  1204. begin
  1205. writeln(t,'#pragma once');
  1206. writeln(t,'#define CONFIG_APP_BUILD_USE_FLASH_SECTIONS 1');
  1207. end;
  1208. Close(t);
  1209. if ioresult<>0 then
  1210. exit;
  1211. {$pop}
  1212. { generate an sdkconfig if none is provided,
  1213. this is a dummy so far }
  1214. if not(Sysutils.FileExists(outputexedir+'/sdkconfig')) then
  1215. begin
  1216. Assign(t,outputexedir+'/sdkconfig');
  1217. {$push}{$I-}
  1218. Rewrite(t);
  1219. if ioresult<>0 then
  1220. exit;
  1221. writeln(t);
  1222. Close(t);
  1223. if ioresult<>0 then
  1224. exit;
  1225. {$pop}
  1226. end;
  1227. { generate an Kconfig if none is provided,
  1228. this is a dummy so far }
  1229. if not(Sysutils.FileExists(outputexedir+'/Kconfig')) then
  1230. begin
  1231. Assign(t,outputexedir+'/Kconfig');
  1232. {$push}{$I-}
  1233. Rewrite(t);
  1234. if ioresult<>0 then
  1235. exit;
  1236. writeln(t);
  1237. Close(t);
  1238. if ioresult<>0 then
  1239. exit;
  1240. {$pop}
  1241. end;
  1242. { generate an Kconfig.projbuild if none is provided,
  1243. this is a dummy so far }
  1244. if not(Sysutils.FileExists(outputexedir+'/Kconfig.projbuild')) then
  1245. begin
  1246. Assign(t,outputexedir+'/Kconfig.projbuild');
  1247. {$push}{$I-}
  1248. Rewrite(t);
  1249. if ioresult<>0 then
  1250. exit;
  1251. writeln(t);
  1252. Close(t);
  1253. if ioresult<>0 then
  1254. exit;
  1255. {$pop}
  1256. end;
  1257. { generate an kconfigs.in if none is provided,
  1258. this is a dummy so far }
  1259. if not(Sysutils.FileExists(outputexedir+'/kconfigs.in')) then
  1260. begin
  1261. Assign(t,outputexedir+'/kconfigs.in');
  1262. {$push}{$I-}
  1263. Rewrite(t);
  1264. if ioresult<>0 then
  1265. exit;
  1266. writeln(t);
  1267. Close(t);
  1268. if ioresult<>0 then
  1269. exit;
  1270. {$pop}
  1271. end;
  1272. { generate an kconfigs_projbuild.in if none is provided,
  1273. this is a dummy so far }
  1274. if not(Sysutils.FileExists(outputexedir+'/kconfigs_projbuild.in')) then
  1275. begin
  1276. Assign(t,outputexedir+'/kconfigs_projbuild.in');
  1277. {$push}{$I-}
  1278. Rewrite(t);
  1279. if ioresult<>0 then
  1280. exit;
  1281. writeln(t);
  1282. Close(t);
  1283. if ioresult<>0 then
  1284. exit;
  1285. {$pop}
  1286. end;
  1287. { generate a config.env if none is provided,
  1288. COMPONENT_KCONFIGS and COMPONENT_KCONFIGS_PROJBUILD are dummy fields and might
  1289. be needed to be filed properly }
  1290. Assign(t,outputexedir+'/config.env');
  1291. {$push}{$I-}
  1292. Rewrite(t);
  1293. if ioresult<>0 then
  1294. exit;
  1295. writeln(t,'{');
  1296. if (current_settings.controllertype = ct_esp32c3) then
  1297. begin
  1298. writeln(t,' "COMPONENT_KCONFIGS": "Kconfig",');
  1299. writeln(t,' "COMPONENT_KCONFIGS_PROJBUILD": "Kconfig.projbuild",');
  1300. writeln(t,' "IDF_CMAKE": "y",');
  1301. writeln(t,' "IDF_TARGET": "esp32c3",');
  1302. writeln(t,' "IDF_ENV_FPGA": "",');
  1303. writeln(t,' "IDF_PATH": "'+TargetFixPath(idfpath,false)+'",');
  1304. writeln(t,' "COMPONENT_KCONFIGS_SOURCE_FILE": "'+outputexedir+'/kconfigs.in",');
  1305. writeln(t,' "COMPONENT_KCONFIGS_PROJBUILD_SOURCE_FILE": "'+outputexedir+'/kconfigs_projbuild.in"');
  1306. end;
  1307. writeln(t,'}');
  1308. Close(t);
  1309. if ioresult<>0 then
  1310. exit;
  1311. {$pop}
  1312. { generate ldgen_libraries }
  1313. Assign(t,outputexedir+'/ldgen_libraries');
  1314. {$push}{$I-}
  1315. Rewrite(t);
  1316. if ioresult<>0 then
  1317. exit;
  1318. { extract libraries from linker options and add to static libraries list }
  1319. Info.ExtraOptions:=trim(Info.ExtraOptions);
  1320. i := pos('-l', Info.ExtraOptions);
  1321. while i > 0 do
  1322. begin
  1323. j:=pos(' ',Info.ExtraOptions);
  1324. if j=0 then
  1325. j:=length(Info.ExtraOptions)+1;
  1326. lib:=copy(Info.ExtraOptions,i+2,j-i-2);
  1327. AddStaticCLibrary(lib);
  1328. delete(Info.ExtraOptions,i,j);
  1329. trim(Info.ExtraOptions);
  1330. i := pos('-l', Info.ExtraOptions);
  1331. end;
  1332. hp:=TCmdStrListItem(StaticLibFiles.First);
  1333. while assigned(hp) do
  1334. begin
  1335. FindLibraryFile(hp.Str,target_info.staticClibprefix,target_info.staticClibext,filepath);
  1336. writeln(t,filepath);
  1337. hp:=TCmdStrListItem(hp.Next);
  1338. end;
  1339. Close(t);
  1340. if ioresult<>0 then
  1341. exit;
  1342. {$pop}
  1343. memory_filename:=IncludeTrailingPathDelimiter(outputexedir)+memory_filename;
  1344. cmdstr:='-C -P -x c -E -o '+memory_filename+' -I $OUTPUT ';
  1345. binstr:='gcc';
  1346. if current_settings.controllertype = ct_none then
  1347. Message(exec_f_controllertype_expected)
  1348. else if current_settings.controllertype = ct_esp32c3 then
  1349. begin
  1350. if idf_version>=40400 then
  1351. cmdstr:=cmdstr+'-I $IDF_PATH/components/esp_system/ld $IDF_PATH/components/esp_system/ld/esp32c3/memory.ld.in'
  1352. else
  1353. cmdstr:=cmdstr+'$IDF_PATH/components/esp32c3/ld/esp32c3.ld';
  1354. end;
  1355. Replace(cmdstr,'$IDF_PATH',idfpath);
  1356. Replace(cmdstr,'$OUTPUT',outputexedir);
  1357. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,true);
  1358. { generate linker maps }
  1359. {$ifdef UNIX}
  1360. binstr:=TargetFixPath(idfpath,false)+'/tools/ldgen/ldgen.py';
  1361. {$else}
  1362. binstr:='python';
  1363. {$endif UNIX}
  1364. if source_info.exeext<>'' then
  1365. binstr:=binstr+source_info.exeext;
  1366. sections_filename:=IncludeTrailingPathDelimiter(outputexedir)+sections_filename;
  1367. cmdstr:={$ifndef UNIX}'$IDF_PATH/tools/ldgen/ldgen.py '+{$endif UNIX}
  1368. '--config $OUTPUT/sdkconfig --fragments';
  1369. { Pick corresponding linker fragments list for SDK version }
  1370. idf_index:=esp32c3_v5_0;
  1371. for S in esp_fragment_list[idf_index] do
  1372. cmdstr:=cmdstr+' $IDF_PATH/components/'+S+'.lf';
  1373. if (current_settings.controllertype = ct_esp32c3) then
  1374. begin
  1375. if idf_version>=40400 then
  1376. cmdstr:=cmdstr+' --input $IDF_PATH/components/esp_system/ld/esp32c3/sections.ld.in'
  1377. else
  1378. cmdstr:=cmdstr+' --input $IDF_PATH/components/esp32/ld/esp32c3.project.ld.in';
  1379. end
  1380. else;
  1381. S:=FindUtil(utilsprefix+'objdump');
  1382. cmdstr:=cmdstr+' --output '+sections_filename+
  1383. ' --kconfig $IDF_PATH/Kconfig'+
  1384. ' --env-file $OUTPUT/config.env'+
  1385. ' --libraries-file $OUTPUT/ldgen_libraries'+
  1386. ' --objdump '+S;
  1387. Replace(cmdstr,'$IDF_PATH',idfpath);
  1388. Replace(cmdstr,'$OUTPUT',outputexedir);
  1389. if success then
  1390. success:=DoExec(binstr,cmdstr,true,false);
  1391. end;
  1392. {$endif RISCV32}
  1393. function TlinkerFreeRTOS.MakeExecutable:boolean;
  1394. var
  1395. StaticStr,
  1396. binstr,
  1397. cmdstr,
  1398. mapstr: Ansistring;
  1399. success : boolean;
  1400. GCSectionsStr,
  1401. DynLinkStr,
  1402. StripStr,
  1403. FixedExeFileName: string;
  1404. {$if defined(XTENSA) or defined(RISCV32) or defined(ARM)}
  1405. memory_script,
  1406. sections_script,
  1407. cntrlr,
  1408. extraopts: AnsiString;
  1409. {$endif defined(XTENSA) or defined(RISCV32) or defined(ARM)}
  1410. begin
  1411. { for future use }
  1412. StaticStr:='';
  1413. StripStr:='';
  1414. mapstr:='';
  1415. DynLinkStr:='';
  1416. success:=true;
  1417. Result:=false;
  1418. extraopts:='';
  1419. {$if defined(XTENSA) or defined(RISCV32)}
  1420. { idfpath can be set by -Ff, else default to environment value of IDF_PATH }
  1421. if idfpath='' then
  1422. idfpath := trim(GetEnvironmentVariable('IDF_PATH'));
  1423. idfpath:=ExcludeTrailingBackslash(idfpath);
  1424. {$ifdef XTENSA}
  1425. case current_settings.controllertype of
  1426. ct_esp32: cntrlr:='esp32';
  1427. ct_esp32s2: cntrlr:='esp32s2';
  1428. ct_esp32s3: cntrlr:='esp32s3';
  1429. ct_esp8266: cntrlr:='esp8266';
  1430. else
  1431. cntrlr:='';
  1432. end;
  1433. {$endif XTENSA}
  1434. {$ifdef RISCV32}
  1435. case current_settings.controllertype of
  1436. ct_esp32c2: cntrlr:='esp32c2';
  1437. ct_esp32c3: cntrlr:='esp32c3';
  1438. ct_esp32c6:
  1439. begin
  1440. cntrlr:='esp32c6';
  1441. { Extra option required to generate bin file }
  1442. extraopts:=' --flash-mmu-page-size 32KB ';
  1443. end
  1444. else
  1445. cntrlr:='';
  1446. end;
  1447. {$endif RISCV32}
  1448. { Locate linker scripts. If not found, generate defaults. }
  1449. { Cater for different script names in different esp-idf versions }
  1450. if idf_version >= 40400 then
  1451. begin
  1452. memory_script := 'memory.ld';
  1453. sections_script := 'sections.ld';
  1454. end
  1455. else
  1456. begin
  1457. memory_script := cntrlr+'_out.ld';
  1458. sections_script := cntrlr+'.project.ld';
  1459. end;
  1460. if not (FindLibraryFile(memory_script,'','',memory_script) and
  1461. FindLibraryFile(sections_script,'','',sections_script)) then
  1462. GenerateDefaultLinkerScripts(memory_script,sections_script);
  1463. {$ifdef XTENSA}
  1464. if (current_settings.controllertype=ct_esp8266) then
  1465. begin
  1466. Info.ExeCmd[1] := Info.ExeCmd[1]+' -u call_user_start -u g_esp_sys_info -u _printf_float -u _scanf_float '+
  1467. '-L $IDF_PATH/components/'+cntrlr+'/ld -T '+cntrlr+'.peripherals.ld -T '+cntrlr+'.rom.ld '+ { SDK scripts }
  1468. '-T '+memory_script+' -T '+sections_script; { Project scripts }
  1469. end
  1470. else
  1471. {$endif XTENSA}
  1472. begin
  1473. Info.ExeCmd[1]:=Info.ExeCmd[1]+' -u call_user_start_cpu0 -u ld_include_panic_highint_hdl -u esp_app_desc -u vfs_include_syscalls_impl -u pthread_include_pthread_impl -u pthread_include_pthread_cond_impl -u pthread_include_pthread_local_storage_impl -u newlib_include_locks_impl '+
  1474. '-u newlib_include_heap_impl -u newlib_include_syscalls_impl -u newlib_include_pthread_impl -u app_main -u uxTopUsedPriority '+
  1475. '-L $IDF_PATH/components/esp_rom/'+cntrlr+'/ld ';
  1476. if idf_version<40400 then
  1477. Info.ExeCmd[1]:=Info.ExeCmd[1]+' -L $IDF_PATH/components/'+cntrlr+'/ld'
  1478. else
  1479. Info.ExeCmd[1]:=Info.ExeCmd[1]+' -L $IDF_PATH/components/soc/'+cntrlr+'/ld';
  1480. Info.ExeCmd[1]:=Info.ExeCmd[1]+' -T '+memory_script+' -T '+sections_script;
  1481. Info.ExeCmd[1]:=Info.ExeCmd[1]+' -T '+cntrlr+'.rom.ld -T '+cntrlr+'.rom.api.ld';
  1482. {$ifdef XTENSA}
  1483. if current_settings.controllertype = ct_esp32 then
  1484. if idf_version>=50200 then
  1485. Info.ExeCmd[1]:=Info.ExeCmd[1]+' -T '+cntrlr+'.rom.libgcc.ld -T '+cntrlr+'.rom.newlib-data.ld -T '+cntrlr+'.rom.syscalls.ld -T '+cntrlr+'.rom.newlib-funcs.ld'
  1486. else if idf_version>=50000 then
  1487. Info.ExeCmd[1]:=Info.ExeCmd[1]+' -T '+cntrlr+'.rom.libgcc.ld -T '+cntrlr+'.rom.newlib-data.ld -T '+cntrlr+'.rom.syscalls.ld -T '+cntrlr+'.rom.newlib-funcs.ld'
  1488. else if idf_version>=40400 then
  1489. Info.ExeCmd[1]:=Info.ExeCmd[1]+' -T '+cntrlr+'.rom.libgcc.ld -T '+cntrlr+'.rom.newlib-data.ld -T '+cntrlr+'.rom.syscalls.ld -T '+cntrlr+'.rom.newlib-funcs.ld -T '+cntrlr+'.rom.newlib-time.ld'
  1490. else if idf_version>=40300 then
  1491. Info.ExeCmd[1]:=Info.ExeCmd[1]+' -T '+cntrlr+'.rom.libgcc.ld -T '+cntrlr+'.rom.newlib-data.ld -T '+cntrlr+'.rom.syscalls.ld -T '+cntrlr+'.rom.newlib-funcs.ld -T '+cntrlr+'.rom.newlib-time.ld'
  1492. else
  1493. begin
  1494. //Currently not supported
  1495. end;
  1496. if current_settings.controllertype = ct_esp32s2 then
  1497. if idf_version>=50200 then
  1498. Info.ExeCmd[1]:=Info.ExeCmd[1]+' -T '+cntrlr+'.rom.libgcc.ld -T '+cntrlr+'.rom.newlib-funcs.ld -T '+cntrlr+'.rom.newlib-data.ld -T '+cntrlr+'.rom.spiflash.ld'
  1499. else if idf_version>=50000 then
  1500. Info.ExeCmd[1]:=Info.ExeCmd[1]+' -T '+cntrlr+'.rom.libgcc.ld -T '+cntrlr+'.rom.newlib-funcs.ld -T '+cntrlr+'.rom.newlib-data.ld -T '+cntrlr+'.rom.spiflash.ld'
  1501. else if idf_version>=40400 then
  1502. Info.ExeCmd[1]:=Info.ExeCmd[1]+' -T '+cntrlr+'.rom.libgcc.ld -T '+cntrlr+'.rom.newlib-funcs.ld -T '+cntrlr+'.rom.newlib-data.ld -T '+cntrlr+'.rom.spiflash.ld -T '+cntrlr+'.rom.newlib-time.ld'
  1503. else
  1504. begin
  1505. //Currently not supported
  1506. end;
  1507. if current_settings.controllertype = ct_esp32s3 then
  1508. if idf_version>=50200 then
  1509. Info.ExeCmd[1]:=Info.ExeCmd[1]+' -T '+cntrlr+'.rom.libgcc.ld -T '+cntrlr+'.rom.newlib.ld -T '+cntrlr+'.rom.version.ld'
  1510. else if idf_version>=50000 then
  1511. Info.ExeCmd[1]:=Info.ExeCmd[1]+' -T '+cntrlr+'.rom.libgcc.ld -T '+cntrlr+'.rom.newlib.ld -T '+cntrlr+'.rom.version.ld'
  1512. else if idf_version>=40400 then
  1513. Info.ExeCmd[1]:=Info.ExeCmd[1]+' -T '+cntrlr+'.rom.libgcc.ld -T '+cntrlr+'.rom.newlib.ld -T '+cntrlr+'.rom.version.ld -T '+cntrlr+'.rom.newlib-time.ld'
  1514. else
  1515. begin
  1516. //Currently not supported
  1517. end;
  1518. {$endif XTENSA}
  1519. {$ifdef RISCV32}
  1520. if current_settings.controllertype=ct_esp32c2 then
  1521. if idf_version>=50200 then
  1522. Info.ExeCmd[1]:=Info.ExeCmd[1]+' -T '+cntrlr+'.rom.rvfp.ld -T '+cntrlr+'.rom.newlib.ld -T '+cntrlr+'.rom.version.ld -T '+cntrlr+'.rom.newlib-nano.ld -T '+cntrlr+'.rom.heap.ld'
  1523. else if idf_version>=50000 then
  1524. Info.ExeCmd[1]:=Info.ExeCmd[1]+' -T '+cntrlr+'.rom.rvfp.ld -T '+cntrlr+'.rom.newlib.ld -T '+cntrlr+'.rom.version.ld -T '+cntrlr+'.rom.newlib-time.ld -T '+cntrlr+'.rom.newlib-nano.ld -T '+cntrlr+'.rom.heap.ld'
  1525. else
  1526. begin
  1527. //Currently not supported
  1528. end;
  1529. if current_settings.controllertype=ct_esp32c3 then
  1530. if idf_version>=50200 then
  1531. Info.ExeCmd[1]:=Info.ExeCmd[1]+' -T '+cntrlr+'.rom.libgcc.ld -T '+cntrlr+'.rom.newlib.ld -T '+cntrlr+'.rom.version.ld -T '+cntrlr+'.rom.eco3.ld'
  1532. else if idf_version>=50000 then
  1533. Info.ExeCmd[1]:=Info.ExeCmd[1]+' -T '+cntrlr+'.rom.libgcc.ld -T '+cntrlr+'.rom.newlib.ld -T '+cntrlr+'.rom.version.ld -T '+cntrlr+'.rom.eco3.ld'
  1534. else if idf_version>=40400 then
  1535. Info.ExeCmd[1]:=Info.ExeCmd[1]+' -T '+cntrlr+'.rom.libgcc.ld -T '+cntrlr+'.rom.newlib.ld -T '+cntrlr+'.rom.version.ld -T '+cntrlr+'.rom.newlib-time.ld -T '+cntrlr+'.rom.eco3.ld'
  1536. else
  1537. begin
  1538. //Currently not supported
  1539. end;
  1540. if current_settings.controllertype=ct_esp32c6 then
  1541. if idf_version>=50200 then
  1542. Info.ExeCmd[1]:=Info.ExeCmd[1]+' -T '+cntrlr+'.rom.rvfp.ld -T '+cntrlr+'.rom.newlib.ld -T '+cntrlr+'.rom.version.ld -T '+cntrlr+'.rom.phy.ld -T '+cntrlr+'.rom.coexist.ld -T '+cntrlr+'.rom.net80211.ld -T '+cntrlr+'.rom.pp.ld -T '+cntrlr+'.rom.wdt.ld -T '+cntrlr+'.rom.systimer.ld -T '+cntrlr+'.rom.newlib-normal.ld -T '+cntrlr+'.rom.heap.ld'
  1543. else
  1544. begin
  1545. //Currently not supported
  1546. end;
  1547. {$endif RISCV32}
  1548. Info.ExeCmd[1]:=Info.ExeCmd[1]+' -T '+cntrlr+'.peripherals.ld'
  1549. end;
  1550. Replace(Info.ExeCmd[1],'$IDF_PATH',idfpath);
  1551. {$endif defined(XTENSA) or defined(RISCV32)}
  1552. FixedExeFileName:=maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename,'.elf')));
  1553. GCSectionsStr:='--gc-sections';
  1554. if not(cs_link_nolink in current_settings.globalswitches) then
  1555. Message1(exec_i_linking,current_module.exefilename);
  1556. if (cs_link_map in current_settings.globalswitches) then
  1557. mapstr:='-Map '+maybequoted(ChangeFileExt(current_module.exefilename,'.map'));
  1558. { Write used files and libraries }
  1559. WriteResponseFile();
  1560. { Call linker }
  1561. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  1562. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  1563. if not(cs_link_on_target in current_settings.globalswitches) then
  1564. begin
  1565. Replace(cmdstr,'$EXE',FixedExeFileName);
  1566. Replace(cmdstr,'$RES',(maybequoted(ScriptFixFileName(outputexedir+Info.ResName))));
  1567. Replace(cmdstr,'$STATIC',StaticStr);
  1568. Replace(cmdstr,'$STRIP',StripStr);
  1569. Replace(cmdstr,'$MAP',mapstr);
  1570. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  1571. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  1572. end
  1573. else
  1574. begin
  1575. Replace(cmdstr,'$EXE',FixedExeFileName);
  1576. Replace(cmdstr,'$RES',maybequoted(ScriptFixFileName(outputexedir+Info.ResName)));
  1577. Replace(cmdstr,'$STATIC',StaticStr);
  1578. Replace(cmdstr,'$STRIP',StripStr);
  1579. Replace(cmdstr,'$MAP',mapstr);
  1580. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  1581. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  1582. end;
  1583. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  1584. { Remove ReponseFile }
  1585. if success and not(cs_link_nolink in current_settings.globalswitches) then
  1586. DeleteFile(outputexedir+Info.ResName);
  1587. { Post process }
  1588. if success and not(cs_link_nolink in current_settings.globalswitches) then
  1589. success:=PostProcessExecutable(FixedExeFileName,false);
  1590. {$if defined(XTENSA) or defined(RISCV32)}
  1591. if success then
  1592. begin
  1593. {$if defined(DARWIN)}
  1594. success:=FindFileInExeLocations('python',true,binstr);
  1595. cmdstr:=idfpath+'/components/esptool_py/esptool/esptool.py ';
  1596. {$elseif defined(UNIX)}
  1597. binstr:=TargetFixPath(idfpath,false)+'/components/esptool_py/esptool/esptool.py';
  1598. cmdstr:='';
  1599. {$else}
  1600. binstr:='python';
  1601. cmdstr:=idfpath+'/components/esptool_py/esptool/esptool.py ';
  1602. {$endif UNIX}
  1603. {$ifdef XTENSA}
  1604. if (current_settings.controllertype = ct_esp8266) then
  1605. begin
  1606. success:=DoExec(binstr,cmdstr+'--chip esp8266 elf2image --flash_mode dout --flash_freq 40m '+
  1607. '--flash_size '+tostr(embedded_controllers[current_settings.controllertype].flashsize div (1024*1024))+'MB '+
  1608. '--version=3 '+
  1609. '-o '+maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename,'.bin')))+' '+
  1610. FixedExeFileName,
  1611. true,false);
  1612. end
  1613. else
  1614. {$endif XTENSA}
  1615. begin
  1616. success:=DoExec(binstr,cmdstr+'--chip '+cntrlr+' elf2image '+
  1617. '--flash_size '+tostr(embedded_controllers[current_settings.controllertype].flashsize div (1024*1024))+'MB '+
  1618. '--elf-sha256-offset 0xb0 '+extraopts+
  1619. '-o '+maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename,'.bin')))+' '+
  1620. FixedExeFileName,
  1621. true,false);
  1622. end;
  1623. end;
  1624. {$else}
  1625. if success then
  1626. success:=DoExec(FindUtil(utilsprefix+'objcopy'),'-O binary '+
  1627. FixedExeFileName+' '+
  1628. maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename,'.bin'))),true,false);
  1629. {$endif defined(XTENSA) or defined(RISCV32)}
  1630. MakeExecutable:=success; { otherwise a recursive call to link method }
  1631. end;
  1632. function TlinkerFreeRTOS.postprocessexecutable(const fn : string;isdll:boolean):boolean;
  1633. type
  1634. TElf32header=packed record
  1635. magic0123 : longint;
  1636. file_class : byte;
  1637. data_encoding : byte;
  1638. file_version : byte;
  1639. padding : array[$07..$0f] of byte;
  1640. e_type : word;
  1641. e_machine : word;
  1642. e_version : longint;
  1643. e_entry : longint; { entrypoint }
  1644. e_phoff : longint; { program header offset }
  1645. e_shoff : longint; { sections header offset }
  1646. e_flags : longint;
  1647. e_ehsize : word; { elf header size in bytes }
  1648. e_phentsize : word; { size of an entry in the program header array }
  1649. e_phnum : word; { 0..e_phnum-1 of entrys }
  1650. e_shentsize : word; { size of an entry in sections header array }
  1651. e_shnum : word; { 0..e_shnum-1 of entrys }
  1652. e_shstrndx : word; { index of string section header }
  1653. end;
  1654. TElf32sechdr=packed record
  1655. sh_name : longint;
  1656. sh_type : longint;
  1657. sh_flags : longint;
  1658. sh_addr : longint;
  1659. sh_offset : longint;
  1660. sh_size : longint;
  1661. sh_link : longint;
  1662. sh_info : longint;
  1663. sh_addralign : longint;
  1664. sh_entsize : longint;
  1665. end;
  1666. function MayBeSwapHeader(h : telf32header) : telf32header;
  1667. begin
  1668. result:=h;
  1669. if source_info.endian<>target_info.endian then
  1670. with h do
  1671. begin
  1672. result.e_type:=swapendian(e_type);
  1673. result.e_machine:=swapendian(e_machine);
  1674. result.e_version:=swapendian(e_version);
  1675. result.e_entry:=swapendian(e_entry);
  1676. result.e_phoff:=swapendian(e_phoff);
  1677. result.e_shoff:=swapendian(e_shoff);
  1678. result.e_flags:=swapendian(e_flags);
  1679. result.e_ehsize:=swapendian(e_ehsize);
  1680. result.e_phentsize:=swapendian(e_phentsize);
  1681. result.e_phnum:=swapendian(e_phnum);
  1682. result.e_shentsize:=swapendian(e_shentsize);
  1683. result.e_shnum:=swapendian(e_shnum);
  1684. result.e_shstrndx:=swapendian(e_shstrndx);
  1685. end;
  1686. end;
  1687. function MaybeSwapSecHeader(h : telf32sechdr) : telf32sechdr;
  1688. begin
  1689. result:=h;
  1690. if source_info.endian<>target_info.endian then
  1691. with h do
  1692. begin
  1693. result.sh_name:=swapendian(sh_name);
  1694. result.sh_type:=swapendian(sh_type);
  1695. result.sh_flags:=swapendian(sh_flags);
  1696. result.sh_addr:=swapendian(sh_addr);
  1697. result.sh_offset:=swapendian(sh_offset);
  1698. result.sh_size:=swapendian(sh_size);
  1699. result.sh_link:=swapendian(sh_link);
  1700. result.sh_info:=swapendian(sh_info);
  1701. result.sh_addralign:=swapendian(sh_addralign);
  1702. result.sh_entsize:=swapendian(sh_entsize);
  1703. end;
  1704. end;
  1705. var
  1706. f : file;
  1707. function ReadSectionName(pos : longint) : String;
  1708. var
  1709. oldpos : longint;
  1710. c : char;
  1711. begin
  1712. oldpos:=filepos(f);
  1713. seek(f,pos);
  1714. Result:='';
  1715. while true do
  1716. begin
  1717. blockread(f,c,1);
  1718. if c=#0 then
  1719. break;
  1720. Result:=Result+c;
  1721. end;
  1722. seek(f,oldpos);
  1723. end;
  1724. var
  1725. elfheader : TElf32header;
  1726. secheader : TElf32sechdr;
  1727. i : longint;
  1728. stringoffset : longint;
  1729. secname : string;
  1730. begin
  1731. postprocessexecutable:=false;
  1732. { open file }
  1733. assign(f,fn);
  1734. {$push}{$I-}
  1735. reset(f,1);
  1736. if ioresult<>0 then
  1737. Message1(execinfo_f_cant_open_executable,fn);
  1738. { read header }
  1739. blockread(f,elfheader,sizeof(tElf32header));
  1740. elfheader:=MayBeSwapHeader(elfheader);
  1741. seek(f,elfheader.e_shoff);
  1742. { read string section header }
  1743. seek(f,elfheader.e_shoff+sizeof(TElf32sechdr)*elfheader.e_shstrndx);
  1744. blockread(f,secheader,sizeof(secheader));
  1745. secheader:=MaybeSwapSecHeader(secheader);
  1746. stringoffset:=secheader.sh_offset;
  1747. seek(f,elfheader.e_shoff);
  1748. status.codesize:=0;
  1749. status.datasize:=0;
  1750. for i:=0 to elfheader.e_shnum-1 do
  1751. begin
  1752. blockread(f,secheader,sizeof(secheader));
  1753. secheader:=MaybeSwapSecHeader(secheader);
  1754. secname:=ReadSectionName(stringoffset+secheader.sh_name);
  1755. if pos('.text',secname)<>0 then
  1756. begin
  1757. Message1(execinfo_x_codesize,tostr(secheader.sh_size));
  1758. status.codesize:=secheader.sh_size;
  1759. end
  1760. else if secname='.data' then
  1761. begin
  1762. Message1(execinfo_x_initdatasize,tostr(secheader.sh_size));
  1763. inc(status.datasize,secheader.sh_size);
  1764. end
  1765. else if secname='.bss' then
  1766. begin
  1767. Message1(execinfo_x_uninitdatasize,tostr(secheader.sh_size));
  1768. inc(status.datasize,secheader.sh_size);
  1769. end;
  1770. end;
  1771. close(f);
  1772. {$pop}
  1773. if ioresult<>0 then
  1774. ;
  1775. postprocessexecutable:=true;
  1776. end;
  1777. {*****************************************************************************
  1778. Initialize
  1779. *****************************************************************************}
  1780. initialization
  1781. {$ifdef arm}
  1782. RegisterLinker(ld_freertos,TlinkerFreeRTOS);
  1783. RegisterTarget(system_arm_freertos_info);
  1784. {$endif arm}
  1785. {$ifdef avr}
  1786. RegisterLinker(ld_freertos,TlinkerFreeRTOS);
  1787. RegisterTarget(system_avr_embedded_info);
  1788. {$endif avr}
  1789. {$ifdef i386}
  1790. RegisterLinker(ld_freertos,TlinkerFreeRTOS);
  1791. RegisterTarget(system_i386_embedded_info);
  1792. {$endif i386}
  1793. {$ifdef x86_64}
  1794. RegisterLinker(ld_freertos,TlinkerFreeRTOS);
  1795. RegisterTarget(system_x86_64_embedded_info);
  1796. {$endif x86_64}
  1797. {$ifdef i8086}
  1798. { no need to register linker ld_embedded, because i8086_embedded uses the
  1799. regular msdos linker. In case a flat binary, relocated for a specific
  1800. segment address is needed (e.g. for a BIOS or a real mode bootloader), it
  1801. can be produced post-compilation with exe2bin or a similar tool. }
  1802. RegisterTarget(system_i8086_embedded_info);
  1803. {$endif i8086}
  1804. {$ifdef mipsel}
  1805. RegisterLinker(ld_freertos,TlinkerFreeRTOS);
  1806. RegisterTarget(system_mipsel_embedded_info);
  1807. {$endif mipsel}
  1808. {$ifdef m68k}
  1809. RegisterLinker(ld_freertos,TlinkerFreeRTOS);
  1810. RegisterTarget(system_m68k_embedded_info);
  1811. {$endif m68k}
  1812. {$ifdef riscv32}
  1813. RegisterLinker(ld_freertos,TlinkerFreeRTOS);
  1814. RegisterTarget(system_riscv32_freertos_info);
  1815. {$endif riscv32}
  1816. {$ifdef riscv64}
  1817. RegisterLinker(ld_freertos,TlinkerFreeRTOS);
  1818. RegisterTarget(system_riscv64_embedded_info);
  1819. {$endif riscv64}
  1820. {$ifdef xtensa}
  1821. RegisterLinker(ld_freertos,TlinkerFreeRTOS);
  1822. RegisterTarget(system_xtensa_freertos_info);
  1823. {$endif xtensa}
  1824. end.