t_freertos.pas 59 KB

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