t_freertos.pas 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975
  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('SECTIONS');
  804. Add('{');
  805. Add(' .data :');
  806. Add(' {');
  807. Add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  808. Add(' }');
  809. Add('}');
  810. end;
  811. {$endif RISCV32}
  812. {$ifdef XTENSA}
  813. with linkres do
  814. begin
  815. Add('SECTIONS');
  816. Add('{');
  817. Add(' .data :');
  818. Add(' {');
  819. Add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  820. Add(' }');
  821. Add('}');
  822. end;
  823. {$endif XTENSA}
  824. { Write and Close response }
  825. linkres.writetodisk;
  826. linkres.free;
  827. WriteResponseFile:=True;
  828. end;
  829. {$ifdef XTENSA}
  830. { If espX.project.ld or espX_out.ld scripts cannot be located, generate
  831. default scripts so that linking can proceed. Note: the generated
  832. scripts may not match the actual options chosen when the libraries
  833. were built. }
  834. procedure TlinkerFreeRTOS.GenerateDefaultLinkerScripts(var memory_filename,
  835. sections_filename: AnsiString);
  836. type
  837. Tesp_idf_index=(esp32_v4_2=0,esp32_v4_4,esp8266_v3_3);
  838. const
  839. esp_fragment_list: array[esp32_v4_2..esp8266_v3_3] of array of string=(
  840. ('xtensa/linker',
  841. 'soc/linker',
  842. 'esp_event/linker',
  843. 'spi_flash/linker',
  844. 'esp_wifi/linker',
  845. 'lwip/linker',
  846. 'heap/linker',
  847. 'esp_ringbuf/linker',
  848. 'espcoredump/linker',
  849. 'esp32/linker',
  850. 'esp32/ld/esp32_fragments',
  851. 'freertos/linker',
  852. 'newlib/newlib',
  853. 'esp_gdbstub/linker'),
  854. ('driver/linker',
  855. 'esp_pm/linker',
  856. 'spi_flash/linker',
  857. 'esp_gdbstub/linker',
  858. 'espcoredump/linker',
  859. 'esp_phy/linker',
  860. 'esp_system/linker',
  861. 'esp_system/app',
  862. 'hal/linker',
  863. 'esp_event/linker',
  864. 'esp_wifi/linker',
  865. 'lwip/linker',
  866. 'log/linker',
  867. 'heap/linker',
  868. 'soc/linker',
  869. 'esp_hw_support/linker',
  870. 'xtensa/linker',
  871. 'esp_common/common',
  872. 'esp_common/soc',
  873. 'freertos/linker',
  874. 'newlib/newlib',
  875. 'newlib/system_libs',
  876. 'app_trace/linker',
  877. 'bt/linker'),
  878. ('esp8266/ld/esp8266_fragments',
  879. 'esp8266/ld/esp8266_bss_fragments',
  880. 'esp8266/linker',
  881. 'freertos/linker',
  882. 'log/linker',
  883. 'lwip/linker',
  884. 'spi_flash/linker'));
  885. var
  886. S: Ansistring;
  887. t: Text;
  888. hp: TCmdStrListItem;
  889. filepath: TCmdStr = '';
  890. i,j: integer;
  891. idf_index: Tesp_idf_index;
  892. lib,
  893. binstr,
  894. cmdstr: AnsiString;
  895. success: boolean;
  896. begin
  897. { generate a sdkconfig.h if none is provided,
  898. only a few fields are provided to far.
  899. Assume that if linker scripts are not located,
  900. sdkconfig.h is also missing }
  901. Assign(t,outputexedir+'/sdkconfig.h');
  902. {$push}{$I-}
  903. Rewrite(t);
  904. if ioresult<>0 then
  905. exit;
  906. if (current_settings.controllertype = ct_esp32) then
  907. begin
  908. writeln(t,'#pragma once');
  909. writeln(t,'#define CONFIG_APP_BUILD_USE_FLASH_SECTIONS 1');
  910. writeln(t,'#define CONFIG_BT_RESERVE_DRAM 0x0');
  911. writeln(t,'#define CONFIG_ESP32_ULP_COPROC_RESERVE_MEM 0');
  912. writeln(t,'#define CONFIG_ESP32_TRACEMEM_RESERVE_DRAM 0x0');
  913. end
  914. else
  915. begin
  916. { TODO: APP_OFFSET & APP_SIZE depends on partition table
  917. Default for partition table: Single factory app, no OTA }
  918. writeln(t,'#define APP_OFFSET 0x10000');
  919. writeln(t,'#define APP_SIZE 0xf0000');
  920. { Include build version of sdkconfig.h for custom configuration, if available }
  921. S:=idfpath+'/libs/sdkconfig.h';
  922. if SysUtils.FileExists(S) then
  923. writeln(t,'#include "'+S+'"')
  924. else
  925. { Assume SOC_FULL_ICACHE option not selected (default) }
  926. writeln(t,'#define CONFIG_SOC_IRAM_SIZE 0xC000');
  927. end;
  928. Close(t);
  929. if ioresult<>0 then
  930. exit;
  931. {$pop}
  932. { generate an sdkconfig if none is provided,
  933. this is a dummy so far }
  934. if not(Sysutils.FileExists(outputexedir+'/sdkconfig')) then
  935. begin
  936. Assign(t,outputexedir+'/sdkconfig');
  937. {$push}{$I-}
  938. Rewrite(t);
  939. if ioresult<>0 then
  940. exit;
  941. writeln(t);
  942. Close(t);
  943. if ioresult<>0 then
  944. exit;
  945. {$pop}
  946. end;
  947. { generate an Kconfig if none is provided,
  948. this is a dummy so far }
  949. if not(Sysutils.FileExists(outputexedir+'/Kconfig')) then
  950. begin
  951. Assign(t,outputexedir+'/Kconfig');
  952. {$push}{$I-}
  953. Rewrite(t);
  954. if ioresult<>0 then
  955. exit;
  956. writeln(t);
  957. Close(t);
  958. if ioresult<>0 then
  959. exit;
  960. {$pop}
  961. end;
  962. { generate an Kconfig.projbuild if none is provided,
  963. this is a dummy so far }
  964. if not(Sysutils.FileExists(outputexedir+'/Kconfig.projbuild')) then
  965. begin
  966. Assign(t,outputexedir+'/Kconfig.projbuild');
  967. {$push}{$I-}
  968. Rewrite(t);
  969. if ioresult<>0 then
  970. exit;
  971. writeln(t);
  972. Close(t);
  973. if ioresult<>0 then
  974. exit;
  975. {$pop}
  976. end;
  977. { generate an kconfigs.in if none is provided,
  978. this is a dummy so far }
  979. if not(Sysutils.FileExists(outputexedir+'/kconfigs.in')) then
  980. begin
  981. Assign(t,outputexedir+'/kconfigs.in');
  982. {$push}{$I-}
  983. Rewrite(t);
  984. if ioresult<>0 then
  985. exit;
  986. writeln(t);
  987. Close(t);
  988. if ioresult<>0 then
  989. exit;
  990. {$pop}
  991. end;
  992. { generate an kconfigs_projbuild.in if none is provided,
  993. this is a dummy so far }
  994. if not(Sysutils.FileExists(outputexedir+'/kconfigs_projbuild.in')) then
  995. begin
  996. Assign(t,outputexedir+'/kconfigs_projbuild.in');
  997. {$push}{$I-}
  998. Rewrite(t);
  999. if ioresult<>0 then
  1000. exit;
  1001. writeln(t);
  1002. Close(t);
  1003. if ioresult<>0 then
  1004. exit;
  1005. {$pop}
  1006. end;
  1007. { generate a config.env if none is provided,
  1008. COMPONENT_KCONFIGS and COMPONENT_KCONFIGS_PROJBUILD are dummy fields and might
  1009. be needed to be filed properly }
  1010. Assign(t,outputexedir+'/config.env');
  1011. {$push}{$I-}
  1012. Rewrite(t);
  1013. if ioresult<>0 then
  1014. exit;
  1015. writeln(t,'{');
  1016. if (current_settings.controllertype = ct_esp32) then
  1017. begin
  1018. writeln(t,' "COMPONENT_KCONFIGS": "Kconfig",');
  1019. writeln(t,' "COMPONENT_KCONFIGS_PROJBUILD": "Kconfig.projbuild",');
  1020. writeln(t,' "IDF_CMAKE": "y",');
  1021. writeln(t,' "IDF_TARGET": "esp32",');
  1022. writeln(t,' "IDF_PATH": "'+TargetFixPath(idfpath,false)+'",');
  1023. writeln(t,' "COMPONENT_KCONFIGS_SOURCE_FILE": "'+outputexedir+'/kconfigs.in",');
  1024. writeln(t,' "COMPONENT_KCONFIGS_PROJBUILD_SOURCE_FILE": "'+outputexedir+'/kconfigs_projbuild.in"');
  1025. end
  1026. else
  1027. begin
  1028. writeln(t,' "IDF_PATH": "'+TargetFixPath(idfpath,false)+'",');
  1029. writeln(t,' "IDF_TARGET": "esp8266",');
  1030. writeln(t,' "IDF_CMAKE": "n"');
  1031. end;
  1032. writeln(t,'}');
  1033. Close(t);
  1034. if ioresult<>0 then
  1035. exit;
  1036. {$pop}
  1037. { generate ldgen_libraries }
  1038. Assign(t,outputexedir+'/ldgen_libraries');
  1039. {$push}{$I-}
  1040. Rewrite(t);
  1041. if ioresult<>0 then
  1042. exit;
  1043. { extract libraries from linker options and add to static libraries list }
  1044. Info.ExtraOptions:=trim(Info.ExtraOptions);
  1045. i := pos('-l', Info.ExtraOptions);
  1046. while i > 0 do
  1047. begin
  1048. j:=pos(' ',Info.ExtraOptions);
  1049. if j=0 then
  1050. j:=length(Info.ExtraOptions)+1;
  1051. lib:=copy(Info.ExtraOptions,i+2,j-i-2);
  1052. AddStaticCLibrary(lib);
  1053. delete(Info.ExtraOptions,i,j);
  1054. trim(Info.ExtraOptions);
  1055. i := pos('-l', Info.ExtraOptions);
  1056. end;
  1057. hp:=TCmdStrListItem(StaticLibFiles.First);
  1058. while assigned(hp) do
  1059. begin
  1060. FindLibraryFile(hp.Str,target_info.staticClibprefix,target_info.staticClibext,filepath);
  1061. writeln(t,filepath);
  1062. hp:=TCmdStrListItem(hp.Next);
  1063. end;
  1064. Close(t);
  1065. if ioresult<>0 then
  1066. exit;
  1067. {$pop}
  1068. memory_filename:=IncludeTrailingPathDelimiter(outputexedir)+memory_filename;
  1069. cmdstr:='-C -P -x c -E -o '+memory_filename+' -I $OUTPUT ';
  1070. binstr:='gcc';
  1071. if current_settings.controllertype = ct_none then
  1072. Message(exec_f_controllertype_expected)
  1073. else if current_settings.controllertype = ct_esp32 then
  1074. begin
  1075. if idf_version>=40400 then
  1076. cmdstr:=cmdstr+'-I $IDF_PATH/components/esp_system/ld $IDF_PATH/components/esp_system/ld/esp32/memory.ld.in'
  1077. else
  1078. cmdstr:=cmdstr+'$IDF_PATH/components/esp32/ld/esp32.ld';
  1079. end
  1080. else
  1081. cmdstr:=cmdstr+'$IDF_PATH/components/esp8266/ld/esp8266.ld';
  1082. Replace(cmdstr,'$IDF_PATH',idfpath);
  1083. Replace(cmdstr,'$OUTPUT',outputexedir);
  1084. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,true);
  1085. { generate linker maps }
  1086. {$ifdef UNIX}
  1087. binstr:=TargetFixPath(idfpath,false)+'/tools/ldgen/ldgen.py';
  1088. {$else}
  1089. binstr:='python';
  1090. {$endif UNIX}
  1091. if source_info.exeext<>'' then
  1092. binstr:=binstr+source_info.exeext;
  1093. sections_filename:=IncludeTrailingPathDelimiter(outputexedir)+sections_filename;
  1094. cmdstr:={$ifndef UNIX}'$IDF_PATH/tools/ldgen/ldgen.py '+{$endif UNIX}
  1095. '--config $OUTPUT/sdkconfig --fragments';
  1096. { Pick corresponding linker fragments list for SDK version }
  1097. if (current_settings.controllertype = ct_esp32) then
  1098. if idf_version>=40400 then
  1099. idf_index:=esp32_v4_4
  1100. else
  1101. idf_index:=esp32_v4_2
  1102. else
  1103. idf_index:=esp8266_v3_3;
  1104. for S in esp_fragment_list[idf_index] do
  1105. cmdstr:=cmdstr+' $IDF_PATH/components/'+S+'.lf';
  1106. if (current_settings.controllertype = ct_esp32) then
  1107. begin
  1108. if idf_version>=40400 then
  1109. cmdstr:=cmdstr+' --input $IDF_PATH/components/esp_system/ld/esp32/sections.ld.in'
  1110. else
  1111. cmdstr:=cmdstr+' --input $IDF_PATH/components/esp32/ld/esp32.project.ld.in';
  1112. end
  1113. else
  1114. begin
  1115. cmdstr:=cmdstr+
  1116. ' --env "COMPONENT_KCONFIGS_PROJBUILD= $IDF_PATH/components/bootloader/Kconfig.projbuild'+
  1117. ' $IDF_PATH/components/esptool_py/Kconfig.projbuild $IDF_PATH/components/partition_table/Kconfig.projbuild"'+
  1118. ' --env "COMPONENT_KCONFIGS=$IDF_PATH/components/app_update/Kconfig'+
  1119. ' $IDF_PATH/components/esp8266/Kconfig $IDF_PATH/components/freertos/Kconfig'+
  1120. ' $IDF_PATH/components/log/Kconfig $IDF_PATH/components/lwip/Kconfig"'+
  1121. ' --input $IDF_PATH/components/esp8266/ld/esp8266.project.ld.in';
  1122. end;
  1123. S:=FindUtil(utilsprefix+'objdump');
  1124. cmdstr:=cmdstr+' --output '+sections_filename+
  1125. ' --kconfig $IDF_PATH/Kconfig'+
  1126. ' --env-file $OUTPUT/config.env'+
  1127. ' --libraries-file $OUTPUT/ldgen_libraries'+
  1128. ' --objdump '+S;
  1129. Replace(cmdstr,'$IDF_PATH',idfpath);
  1130. Replace(cmdstr,'$OUTPUT',outputexedir);
  1131. if success then
  1132. success:=DoExec(binstr,cmdstr,true,false);
  1133. end;
  1134. {$endif XTENSA}
  1135. {$ifdef RISCV32}
  1136. { If espX.project.ld or espX_out.ld scripts cannot be located, generate
  1137. default scripts so that linking can proceed. Note: the generated
  1138. scripts may not match the actual options chosen when the libraries
  1139. were built. }
  1140. procedure TlinkerFreeRTOS.GenerateDefaultLinkerScripts(var memory_filename,
  1141. sections_filename: AnsiString);
  1142. type
  1143. Tesp_idf_index=(esp32c3_v5_0=0);
  1144. const
  1145. esp_fragment_list: array[esp32c3_v5_0..esp32c3_v5_0] of array of string=(
  1146. ('riscv/linker',
  1147. 'esp_ringbuf/linker',
  1148. 'driver/linker',
  1149. 'esp_pm/linker',
  1150. 'esp_mm/linker',
  1151. 'spi_flash/linker',
  1152. 'esp_system/linker',
  1153. 'esp_system/app',
  1154. 'esp_rom/linker',
  1155. 'hal/linker',
  1156. 'log/linker',
  1157. 'heap/linker',
  1158. 'soc/linker',
  1159. 'esp_hw_support/linker',
  1160. 'freertos/linker',
  1161. 'freertos/linker_common',
  1162. 'newlib/newlib',
  1163. 'newlib/system_libs',
  1164. 'esp_common/common',
  1165. 'esp_common/soc',
  1166. 'app_trace/linker',
  1167. 'esp_event/linker',
  1168. 'esp_phy/linker',
  1169. 'lwip/linker',
  1170. 'esp_netif/linker',
  1171. 'esp_wifi/linker',
  1172. 'bt/linker',
  1173. 'esp_adc/linker',
  1174. 'esp_gdbstub/linker',
  1175. 'esp_lcd/linker',
  1176. 'esp_psram/linker',
  1177. 'espcoredump/linker'));
  1178. var
  1179. S: Ansistring;
  1180. t: Text;
  1181. hp: TCmdStrListItem;
  1182. filepath: TCmdStr = '';
  1183. i,j: integer;
  1184. idf_index: Tesp_idf_index;
  1185. lib,
  1186. binstr,
  1187. cmdstr: AnsiString;
  1188. success: boolean;
  1189. begin
  1190. { generate a sdkconfig.h if none is provided,
  1191. only a few fields are provided to far.
  1192. Assume that if linker scripts are not located,
  1193. sdkconfig.h is also missing }
  1194. Assign(t,outputexedir+'/sdkconfig.h');
  1195. {$push}{$I-}
  1196. Rewrite(t);
  1197. if ioresult<>0 then
  1198. exit;
  1199. if (current_settings.controllertype = ct_esp32c3) then
  1200. begin
  1201. writeln(t,'#pragma once');
  1202. writeln(t,'#define CONFIG_APP_BUILD_USE_FLASH_SECTIONS 1');
  1203. end;
  1204. Close(t);
  1205. if ioresult<>0 then
  1206. exit;
  1207. {$pop}
  1208. { generate an sdkconfig if none is provided,
  1209. this is a dummy so far }
  1210. if not(Sysutils.FileExists(outputexedir+'/sdkconfig')) then
  1211. begin
  1212. Assign(t,outputexedir+'/sdkconfig');
  1213. {$push}{$I-}
  1214. Rewrite(t);
  1215. if ioresult<>0 then
  1216. exit;
  1217. writeln(t);
  1218. Close(t);
  1219. if ioresult<>0 then
  1220. exit;
  1221. {$pop}
  1222. end;
  1223. { generate an Kconfig if none is provided,
  1224. this is a dummy so far }
  1225. if not(Sysutils.FileExists(outputexedir+'/Kconfig')) then
  1226. begin
  1227. Assign(t,outputexedir+'/Kconfig');
  1228. {$push}{$I-}
  1229. Rewrite(t);
  1230. if ioresult<>0 then
  1231. exit;
  1232. writeln(t);
  1233. Close(t);
  1234. if ioresult<>0 then
  1235. exit;
  1236. {$pop}
  1237. end;
  1238. { generate an Kconfig.projbuild if none is provided,
  1239. this is a dummy so far }
  1240. if not(Sysutils.FileExists(outputexedir+'/Kconfig.projbuild')) then
  1241. begin
  1242. Assign(t,outputexedir+'/Kconfig.projbuild');
  1243. {$push}{$I-}
  1244. Rewrite(t);
  1245. if ioresult<>0 then
  1246. exit;
  1247. writeln(t);
  1248. Close(t);
  1249. if ioresult<>0 then
  1250. exit;
  1251. {$pop}
  1252. end;
  1253. { generate an kconfigs.in if none is provided,
  1254. this is a dummy so far }
  1255. if not(Sysutils.FileExists(outputexedir+'/kconfigs.in')) then
  1256. begin
  1257. Assign(t,outputexedir+'/kconfigs.in');
  1258. {$push}{$I-}
  1259. Rewrite(t);
  1260. if ioresult<>0 then
  1261. exit;
  1262. writeln(t);
  1263. Close(t);
  1264. if ioresult<>0 then
  1265. exit;
  1266. {$pop}
  1267. end;
  1268. { generate an kconfigs_projbuild.in if none is provided,
  1269. this is a dummy so far }
  1270. if not(Sysutils.FileExists(outputexedir+'/kconfigs_projbuild.in')) then
  1271. begin
  1272. Assign(t,outputexedir+'/kconfigs_projbuild.in');
  1273. {$push}{$I-}
  1274. Rewrite(t);
  1275. if ioresult<>0 then
  1276. exit;
  1277. writeln(t);
  1278. Close(t);
  1279. if ioresult<>0 then
  1280. exit;
  1281. {$pop}
  1282. end;
  1283. { generate a config.env if none is provided,
  1284. COMPONENT_KCONFIGS and COMPONENT_KCONFIGS_PROJBUILD are dummy fields and might
  1285. be needed to be filed properly }
  1286. Assign(t,outputexedir+'/config.env');
  1287. {$push}{$I-}
  1288. Rewrite(t);
  1289. if ioresult<>0 then
  1290. exit;
  1291. writeln(t,'{');
  1292. if (current_settings.controllertype = ct_esp32c3) then
  1293. begin
  1294. writeln(t,' "COMPONENT_KCONFIGS": "Kconfig",');
  1295. writeln(t,' "COMPONENT_KCONFIGS_PROJBUILD": "Kconfig.projbuild",');
  1296. writeln(t,' "IDF_CMAKE": "y",');
  1297. writeln(t,' "IDF_TARGET": "esp32c3",');
  1298. writeln(t,' "IDF_ENV_FPGA": "",');
  1299. writeln(t,' "IDF_PATH": "'+TargetFixPath(idfpath,false)+'",');
  1300. writeln(t,' "COMPONENT_KCONFIGS_SOURCE_FILE": "'+outputexedir+'/kconfigs.in",');
  1301. writeln(t,' "COMPONENT_KCONFIGS_PROJBUILD_SOURCE_FILE": "'+outputexedir+'/kconfigs_projbuild.in"');
  1302. end;
  1303. writeln(t,'}');
  1304. Close(t);
  1305. if ioresult<>0 then
  1306. exit;
  1307. {$pop}
  1308. { generate ldgen_libraries }
  1309. Assign(t,outputexedir+'/ldgen_libraries');
  1310. {$push}{$I-}
  1311. Rewrite(t);
  1312. if ioresult<>0 then
  1313. exit;
  1314. { extract libraries from linker options and add to static libraries list }
  1315. Info.ExtraOptions:=trim(Info.ExtraOptions);
  1316. i := pos('-l', Info.ExtraOptions);
  1317. while i > 0 do
  1318. begin
  1319. j:=pos(' ',Info.ExtraOptions);
  1320. if j=0 then
  1321. j:=length(Info.ExtraOptions)+1;
  1322. lib:=copy(Info.ExtraOptions,i+2,j-i-2);
  1323. AddStaticCLibrary(lib);
  1324. delete(Info.ExtraOptions,i,j);
  1325. trim(Info.ExtraOptions);
  1326. i := pos('-l', Info.ExtraOptions);
  1327. end;
  1328. hp:=TCmdStrListItem(StaticLibFiles.First);
  1329. while assigned(hp) do
  1330. begin
  1331. FindLibraryFile(hp.Str,target_info.staticClibprefix,target_info.staticClibext,filepath);
  1332. writeln(t,filepath);
  1333. hp:=TCmdStrListItem(hp.Next);
  1334. end;
  1335. Close(t);
  1336. if ioresult<>0 then
  1337. exit;
  1338. {$pop}
  1339. memory_filename:=IncludeTrailingPathDelimiter(outputexedir)+memory_filename;
  1340. cmdstr:='-C -P -x c -E -o '+memory_filename+' -I $OUTPUT ';
  1341. binstr:='gcc';
  1342. if current_settings.controllertype = ct_none then
  1343. Message(exec_f_controllertype_expected)
  1344. else if current_settings.controllertype = ct_esp32c3 then
  1345. begin
  1346. if idf_version>=40400 then
  1347. cmdstr:=cmdstr+'-I $IDF_PATH/components/esp_system/ld $IDF_PATH/components/esp_system/ld/esp32c3/memory.ld.in'
  1348. else
  1349. cmdstr:=cmdstr+'$IDF_PATH/components/esp32c3/ld/esp32c3.ld';
  1350. end;
  1351. Replace(cmdstr,'$IDF_PATH',idfpath);
  1352. Replace(cmdstr,'$OUTPUT',outputexedir);
  1353. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,true);
  1354. { generate linker maps }
  1355. {$ifdef UNIX}
  1356. binstr:=TargetFixPath(idfpath,false)+'/tools/ldgen/ldgen.py';
  1357. {$else}
  1358. binstr:='python';
  1359. {$endif UNIX}
  1360. if source_info.exeext<>'' then
  1361. binstr:=binstr+source_info.exeext;
  1362. sections_filename:=IncludeTrailingPathDelimiter(outputexedir)+sections_filename;
  1363. cmdstr:={$ifndef UNIX}'$IDF_PATH/tools/ldgen/ldgen.py '+{$endif UNIX}
  1364. '--config $OUTPUT/sdkconfig --fragments';
  1365. { Pick corresponding linker fragments list for SDK version }
  1366. idf_index:=esp32c3_v5_0;
  1367. for S in esp_fragment_list[idf_index] do
  1368. cmdstr:=cmdstr+' $IDF_PATH/components/'+S+'.lf';
  1369. if (current_settings.controllertype = ct_esp32c3) then
  1370. begin
  1371. if idf_version>=40400 then
  1372. cmdstr:=cmdstr+' --input $IDF_PATH/components/esp_system/ld/esp32c3/sections.ld.in'
  1373. else
  1374. cmdstr:=cmdstr+' --input $IDF_PATH/components/esp32/ld/esp32c3.project.ld.in';
  1375. end
  1376. else;
  1377. S:=FindUtil(utilsprefix+'objdump');
  1378. cmdstr:=cmdstr+' --output '+sections_filename+
  1379. ' --kconfig $IDF_PATH/Kconfig'+
  1380. ' --env-file $OUTPUT/config.env'+
  1381. ' --libraries-file $OUTPUT/ldgen_libraries'+
  1382. ' --objdump '+S;
  1383. Replace(cmdstr,'$IDF_PATH',idfpath);
  1384. Replace(cmdstr,'$OUTPUT',outputexedir);
  1385. if success then
  1386. success:=DoExec(binstr,cmdstr,true,false);
  1387. end;
  1388. {$endif RISCV32}
  1389. function TlinkerFreeRTOS.MakeExecutable:boolean;
  1390. var
  1391. StaticStr,
  1392. binstr,
  1393. cmdstr,
  1394. mapstr: Ansistring;
  1395. success : boolean;
  1396. GCSectionsStr,
  1397. DynLinkStr,
  1398. StripStr,
  1399. FixedExeFileName: string;
  1400. {$if defined(XTENSA) or defined(RISCV32) or defined(ARM)}
  1401. memory_script,
  1402. sections_script,
  1403. cntrlr,
  1404. extraopts: AnsiString;
  1405. {$endif defined(XTENSA) or defined(RISCV32) or defined(ARM)}
  1406. begin
  1407. { for future use }
  1408. StaticStr:='';
  1409. StripStr:='';
  1410. mapstr:='';
  1411. DynLinkStr:='';
  1412. success:=true;
  1413. Result:=false;
  1414. extraopts:='';
  1415. {$if defined(XTENSA) or defined(RISCV32)}
  1416. { idfpath can be set by -Ff, else default to environment value of IDF_PATH }
  1417. if idfpath='' then
  1418. idfpath := trim(GetEnvironmentVariable('IDF_PATH'));
  1419. idfpath:=ExcludeTrailingBackslash(idfpath);
  1420. {$ifdef XTENSA}
  1421. case current_settings.controllertype of
  1422. ct_esp32: cntrlr:='esp32';
  1423. ct_esp32s2: cntrlr:='esp32s2';
  1424. ct_esp32s3: cntrlr:='esp32s3';
  1425. ct_esp8266: cntrlr:='esp8266';
  1426. else
  1427. cntrlr:='';
  1428. end;
  1429. {$endif XTENSA}
  1430. {$ifdef RISCV32}
  1431. case current_settings.controllertype of
  1432. ct_esp32c2: cntrlr:='esp32c2';
  1433. ct_esp32c3: cntrlr:='esp32c3';
  1434. ct_esp32c6:
  1435. begin
  1436. cntrlr:='esp32c6';
  1437. { Extra option required to generate bin file }
  1438. extraopts:=' --flash-mmu-page-size 32KB ';
  1439. end
  1440. else
  1441. cntrlr:='';
  1442. end;
  1443. {$endif RISCV32}
  1444. { Locate linker scripts. If not found, generate defaults. }
  1445. { Cater for different script names in different esp-idf versions }
  1446. if idf_version >= 40400 then
  1447. begin
  1448. memory_script := 'memory.ld';
  1449. sections_script := 'sections.ld';
  1450. end
  1451. else
  1452. begin
  1453. memory_script := cntrlr+'_out.ld';
  1454. sections_script := cntrlr+'.project.ld';
  1455. end;
  1456. if not (FindLibraryFile(memory_script,'','',memory_script) and
  1457. FindLibraryFile(sections_script,'','',sections_script)) then
  1458. GenerateDefaultLinkerScripts(memory_script,sections_script);
  1459. {$ifdef XTENSA}
  1460. if (current_settings.controllertype=ct_esp8266) then
  1461. begin
  1462. Info.ExeCmd[1] := Info.ExeCmd[1]+' -u call_user_start -u g_esp_sys_info -u _printf_float -u _scanf_float '+
  1463. '-L $IDF_PATH/components/'+cntrlr+'/ld -T '+cntrlr+'.peripherals.ld -T '+cntrlr+'.rom.ld '+ { SDK scripts }
  1464. '-T '+memory_script+' -T '+sections_script; { Project scripts }
  1465. end
  1466. else
  1467. {$endif XTENSA}
  1468. begin
  1469. 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 '+
  1470. '-u newlib_include_heap_impl -u newlib_include_syscalls_impl -u newlib_include_pthread_impl -u app_main -u uxTopUsedPriority '+
  1471. '-L $IDF_PATH/components/esp_rom/'+cntrlr+'/ld ';
  1472. if idf_version<40400 then
  1473. Info.ExeCmd[1]:=Info.ExeCmd[1]+' -L $IDF_PATH/components/'+cntrlr+'/ld'
  1474. else
  1475. Info.ExeCmd[1]:=Info.ExeCmd[1]+' -L $IDF_PATH/components/soc/'+cntrlr+'/ld';
  1476. Info.ExeCmd[1]:=Info.ExeCmd[1]+' -T '+memory_script+' -T '+sections_script;
  1477. Info.ExeCmd[1]:=Info.ExeCmd[1]+' -T '+cntrlr+'.rom.ld -T '+cntrlr+'.rom.api.ld';
  1478. {$ifdef XTENSA}
  1479. if current_settings.controllertype = ct_esp32 then
  1480. if idf_version>=50200 then
  1481. 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'
  1482. else if idf_version>=50000 then
  1483. 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'
  1484. else if idf_version>=40400 then
  1485. 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'
  1486. else if idf_version>=40300 then
  1487. 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'
  1488. else
  1489. begin
  1490. //Currently not supported
  1491. end;
  1492. if current_settings.controllertype = ct_esp32s2 then
  1493. if idf_version>=50200 then
  1494. 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'
  1495. else if idf_version>=50000 then
  1496. 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'
  1497. else if idf_version>=40400 then
  1498. 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'
  1499. else
  1500. begin
  1501. //Currently not supported
  1502. end;
  1503. if current_settings.controllertype = ct_esp32s3 then
  1504. if idf_version>=50200 then
  1505. Info.ExeCmd[1]:=Info.ExeCmd[1]+' -T '+cntrlr+'.rom.libgcc.ld -T '+cntrlr+'.rom.newlib.ld -T '+cntrlr+'.rom.version.ld'
  1506. else if idf_version>=50000 then
  1507. Info.ExeCmd[1]:=Info.ExeCmd[1]+' -T '+cntrlr+'.rom.libgcc.ld -T '+cntrlr+'.rom.newlib.ld -T '+cntrlr+'.rom.version.ld'
  1508. else if idf_version>=40400 then
  1509. 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'
  1510. else
  1511. begin
  1512. //Currently not supported
  1513. end;
  1514. {$endif XTENSA}
  1515. {$ifdef RISCV32}
  1516. if current_settings.controllertype=ct_esp32c2 then
  1517. if idf_version>=50200 then
  1518. 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'
  1519. else if idf_version>=50000 then
  1520. 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'
  1521. else
  1522. begin
  1523. //Currently not supported
  1524. end;
  1525. if current_settings.controllertype=ct_esp32c3 then
  1526. if idf_version>=50200 then
  1527. 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'
  1528. else if idf_version>=50000 then
  1529. 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'
  1530. else if idf_version>=40400 then
  1531. 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'
  1532. else
  1533. begin
  1534. //Currently not supported
  1535. end;
  1536. if current_settings.controllertype=ct_esp32c6 then
  1537. if idf_version>=50200 then
  1538. 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'
  1539. else
  1540. begin
  1541. //Currently not supported
  1542. end;
  1543. {$endif RISCV32}
  1544. Info.ExeCmd[1]:=Info.ExeCmd[1]+' -T '+cntrlr+'.peripherals.ld'
  1545. end;
  1546. Replace(Info.ExeCmd[1],'$IDF_PATH',idfpath);
  1547. {$endif defined(XTENSA) or defined(RISCV32)}
  1548. FixedExeFileName:=maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename,'.elf')));
  1549. GCSectionsStr:='--gc-sections';
  1550. if not(cs_link_nolink in current_settings.globalswitches) then
  1551. Message1(exec_i_linking,current_module.exefilename);
  1552. if (cs_link_map in current_settings.globalswitches) then
  1553. mapstr:='-Map '+maybequoted(ChangeFileExt(current_module.exefilename,'.map'));
  1554. { Write used files and libraries }
  1555. WriteResponseFile();
  1556. { Call linker }
  1557. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  1558. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  1559. if not(cs_link_on_target in current_settings.globalswitches) then
  1560. begin
  1561. Replace(cmdstr,'$EXE',FixedExeFileName);
  1562. Replace(cmdstr,'$RES',(maybequoted(ScriptFixFileName(outputexedir+Info.ResName))));
  1563. Replace(cmdstr,'$STATIC',StaticStr);
  1564. Replace(cmdstr,'$STRIP',StripStr);
  1565. Replace(cmdstr,'$MAP',mapstr);
  1566. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  1567. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  1568. end
  1569. else
  1570. begin
  1571. Replace(cmdstr,'$EXE',FixedExeFileName);
  1572. Replace(cmdstr,'$RES',maybequoted(ScriptFixFileName(outputexedir+Info.ResName)));
  1573. Replace(cmdstr,'$STATIC',StaticStr);
  1574. Replace(cmdstr,'$STRIP',StripStr);
  1575. Replace(cmdstr,'$MAP',mapstr);
  1576. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  1577. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  1578. end;
  1579. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  1580. { Remove ReponseFile }
  1581. if success and not(cs_link_nolink in current_settings.globalswitches) then
  1582. DeleteFile(outputexedir+Info.ResName);
  1583. { Post process }
  1584. if success and not(cs_link_nolink in current_settings.globalswitches) then
  1585. success:=PostProcessExecutable(FixedExeFileName,false);
  1586. {$if defined(XTENSA) or defined(RISCV32)}
  1587. if success then
  1588. begin
  1589. {$if defined(DARWIN)}
  1590. success:=FindFileInExeLocations('python',true,binstr);
  1591. cmdstr:=idfpath+'/components/esptool_py/esptool/esptool.py ';
  1592. {$elseif defined(UNIX)}
  1593. binstr:=TargetFixPath(idfpath,false)+'/components/esptool_py/esptool/esptool.py';
  1594. cmdstr:='';
  1595. {$else}
  1596. binstr:='python';
  1597. cmdstr:=idfpath+'/components/esptool_py/esptool/esptool.py ';
  1598. {$endif UNIX}
  1599. {$ifdef XTENSA}
  1600. if (current_settings.controllertype = ct_esp8266) then
  1601. begin
  1602. success:=DoExec(binstr,cmdstr+'--chip esp8266 elf2image --flash_mode dout --flash_freq 40m '+
  1603. '--flash_size '+tostr(embedded_controllers[current_settings.controllertype].flashsize div (1024*1024))+'MB '+
  1604. '--version=3 '+
  1605. '-o '+maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename,'.bin')))+' '+
  1606. FixedExeFileName,
  1607. true,false);
  1608. end
  1609. else
  1610. {$endif XTENSA}
  1611. begin
  1612. success:=DoExec(binstr,cmdstr+'--chip '+cntrlr+' elf2image '+
  1613. '--flash_size '+tostr(embedded_controllers[current_settings.controllertype].flashsize div (1024*1024))+'MB '+
  1614. '--elf-sha256-offset 0xb0 '+extraopts+
  1615. '-o '+maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename,'.bin')))+' '+
  1616. FixedExeFileName,
  1617. true,false);
  1618. end;
  1619. end;
  1620. {$else}
  1621. if success then
  1622. success:=DoExec(FindUtil(utilsprefix+'objcopy'),'-O binary '+
  1623. FixedExeFileName+' '+
  1624. maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename,'.bin'))),true,false);
  1625. {$endif defined(XTENSA) or defined(RISCV32)}
  1626. MakeExecutable:=success; { otherwise a recursive call to link method }
  1627. end;
  1628. function TlinkerFreeRTOS.postprocessexecutable(const fn : string;isdll:boolean):boolean;
  1629. type
  1630. TElf32header=packed record
  1631. magic0123 : longint;
  1632. file_class : byte;
  1633. data_encoding : byte;
  1634. file_version : byte;
  1635. padding : array[$07..$0f] of byte;
  1636. e_type : word;
  1637. e_machine : word;
  1638. e_version : longint;
  1639. e_entry : longint; { entrypoint }
  1640. e_phoff : longint; { program header offset }
  1641. e_shoff : longint; { sections header offset }
  1642. e_flags : longint;
  1643. e_ehsize : word; { elf header size in bytes }
  1644. e_phentsize : word; { size of an entry in the program header array }
  1645. e_phnum : word; { 0..e_phnum-1 of entrys }
  1646. e_shentsize : word; { size of an entry in sections header array }
  1647. e_shnum : word; { 0..e_shnum-1 of entrys }
  1648. e_shstrndx : word; { index of string section header }
  1649. end;
  1650. TElf32sechdr=packed record
  1651. sh_name : longint;
  1652. sh_type : longint;
  1653. sh_flags : longint;
  1654. sh_addr : longint;
  1655. sh_offset : longint;
  1656. sh_size : longint;
  1657. sh_link : longint;
  1658. sh_info : longint;
  1659. sh_addralign : longint;
  1660. sh_entsize : longint;
  1661. end;
  1662. function MayBeSwapHeader(h : telf32header) : telf32header;
  1663. begin
  1664. result:=h;
  1665. if source_info.endian<>target_info.endian then
  1666. with h do
  1667. begin
  1668. result.e_type:=swapendian(e_type);
  1669. result.e_machine:=swapendian(e_machine);
  1670. result.e_version:=swapendian(e_version);
  1671. result.e_entry:=swapendian(e_entry);
  1672. result.e_phoff:=swapendian(e_phoff);
  1673. result.e_shoff:=swapendian(e_shoff);
  1674. result.e_flags:=swapendian(e_flags);
  1675. result.e_ehsize:=swapendian(e_ehsize);
  1676. result.e_phentsize:=swapendian(e_phentsize);
  1677. result.e_phnum:=swapendian(e_phnum);
  1678. result.e_shentsize:=swapendian(e_shentsize);
  1679. result.e_shnum:=swapendian(e_shnum);
  1680. result.e_shstrndx:=swapendian(e_shstrndx);
  1681. end;
  1682. end;
  1683. function MaybeSwapSecHeader(h : telf32sechdr) : telf32sechdr;
  1684. begin
  1685. result:=h;
  1686. if source_info.endian<>target_info.endian then
  1687. with h do
  1688. begin
  1689. result.sh_name:=swapendian(sh_name);
  1690. result.sh_type:=swapendian(sh_type);
  1691. result.sh_flags:=swapendian(sh_flags);
  1692. result.sh_addr:=swapendian(sh_addr);
  1693. result.sh_offset:=swapendian(sh_offset);
  1694. result.sh_size:=swapendian(sh_size);
  1695. result.sh_link:=swapendian(sh_link);
  1696. result.sh_info:=swapendian(sh_info);
  1697. result.sh_addralign:=swapendian(sh_addralign);
  1698. result.sh_entsize:=swapendian(sh_entsize);
  1699. end;
  1700. end;
  1701. var
  1702. f : file;
  1703. function ReadSectionName(pos : longint) : String;
  1704. var
  1705. oldpos : longint;
  1706. c : char;
  1707. begin
  1708. oldpos:=filepos(f);
  1709. seek(f,pos);
  1710. Result:='';
  1711. while true do
  1712. begin
  1713. blockread(f,c,1);
  1714. if c=#0 then
  1715. break;
  1716. Result:=Result+c;
  1717. end;
  1718. seek(f,oldpos);
  1719. end;
  1720. var
  1721. elfheader : TElf32header;
  1722. secheader : TElf32sechdr;
  1723. i : longint;
  1724. stringoffset : longint;
  1725. secname : string;
  1726. begin
  1727. postprocessexecutable:=false;
  1728. { open file }
  1729. assign(f,fn);
  1730. {$push}{$I-}
  1731. reset(f,1);
  1732. if ioresult<>0 then
  1733. Message1(execinfo_f_cant_open_executable,fn);
  1734. { read header }
  1735. blockread(f,elfheader,sizeof(tElf32header));
  1736. elfheader:=MayBeSwapHeader(elfheader);
  1737. seek(f,elfheader.e_shoff);
  1738. { read string section header }
  1739. seek(f,elfheader.e_shoff+sizeof(TElf32sechdr)*elfheader.e_shstrndx);
  1740. blockread(f,secheader,sizeof(secheader));
  1741. secheader:=MaybeSwapSecHeader(secheader);
  1742. stringoffset:=secheader.sh_offset;
  1743. seek(f,elfheader.e_shoff);
  1744. status.codesize:=0;
  1745. status.datasize:=0;
  1746. for i:=0 to elfheader.e_shnum-1 do
  1747. begin
  1748. blockread(f,secheader,sizeof(secheader));
  1749. secheader:=MaybeSwapSecHeader(secheader);
  1750. secname:=ReadSectionName(stringoffset+secheader.sh_name);
  1751. if pos('.text',secname)<>0 then
  1752. begin
  1753. Message1(execinfo_x_codesize,tostr(secheader.sh_size));
  1754. status.codesize:=secheader.sh_size;
  1755. end
  1756. else if secname='.data' then
  1757. begin
  1758. Message1(execinfo_x_initdatasize,tostr(secheader.sh_size));
  1759. inc(status.datasize,secheader.sh_size);
  1760. end
  1761. else if secname='.bss' then
  1762. begin
  1763. Message1(execinfo_x_uninitdatasize,tostr(secheader.sh_size));
  1764. inc(status.datasize,secheader.sh_size);
  1765. end;
  1766. end;
  1767. close(f);
  1768. {$pop}
  1769. if ioresult<>0 then
  1770. ;
  1771. postprocessexecutable:=true;
  1772. end;
  1773. {*****************************************************************************
  1774. Initialize
  1775. *****************************************************************************}
  1776. initialization
  1777. {$ifdef arm}
  1778. RegisterLinker(ld_freertos,TlinkerFreeRTOS);
  1779. RegisterTarget(system_arm_freertos_info);
  1780. {$endif arm}
  1781. {$ifdef avr}
  1782. RegisterLinker(ld_freertos,TlinkerFreeRTOS);
  1783. RegisterTarget(system_avr_embedded_info);
  1784. {$endif avr}
  1785. {$ifdef i386}
  1786. RegisterLinker(ld_freertos,TlinkerFreeRTOS);
  1787. RegisterTarget(system_i386_embedded_info);
  1788. {$endif i386}
  1789. {$ifdef x86_64}
  1790. RegisterLinker(ld_freertos,TlinkerFreeRTOS);
  1791. RegisterTarget(system_x86_64_embedded_info);
  1792. {$endif x86_64}
  1793. {$ifdef i8086}
  1794. { no need to register linker ld_embedded, because i8086_embedded uses the
  1795. regular msdos linker. In case a flat binary, relocated for a specific
  1796. segment address is needed (e.g. for a BIOS or a real mode bootloader), it
  1797. can be produced post-compilation with exe2bin or a similar tool. }
  1798. RegisterTarget(system_i8086_embedded_info);
  1799. {$endif i8086}
  1800. {$ifdef mipsel}
  1801. RegisterLinker(ld_freertos,TlinkerFreeRTOS);
  1802. RegisterTarget(system_mipsel_embedded_info);
  1803. {$endif mipsel}
  1804. {$ifdef m68k}
  1805. RegisterLinker(ld_freertos,TlinkerFreeRTOS);
  1806. RegisterTarget(system_m68k_embedded_info);
  1807. {$endif m68k}
  1808. {$ifdef riscv32}
  1809. RegisterLinker(ld_freertos,TlinkerFreeRTOS);
  1810. RegisterTarget(system_riscv32_freertos_info);
  1811. {$endif riscv32}
  1812. {$ifdef riscv64}
  1813. RegisterLinker(ld_freertos,TlinkerFreeRTOS);
  1814. RegisterTarget(system_riscv64_embedded_info);
  1815. {$endif riscv64}
  1816. {$ifdef xtensa}
  1817. RegisterLinker(ld_freertos,TlinkerFreeRTOS);
  1818. RegisterTarget(system_xtensa_freertos_info);
  1819. {$endif xtensa}
  1820. end.