t_freertos.pas 47 KB

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