t_freertos.pas 64 KB

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