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