t_freertos.pas 49 KB

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