t_freertos.pas 59 KB

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