t_freertos.pas 48 KB

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