t_freertos.pas 50 KB

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