t_freertos.pas 61 KB

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