t_freertos.pas 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434
  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. Result:=false;
  915. if target_info.system=system_xtensa_freertos then
  916. begin
  917. { generate a sdkconfig.h if none is provided,
  918. only a few fields are provided to far }
  919. if not(Sysutils.FileExists('sdkconfig.h')) then
  920. begin
  921. Assign(t,'sdkconfig.h');
  922. {$push}{$I-}
  923. Rewrite(t);
  924. if ioresult<>0 then
  925. exit;
  926. writeln(t,'#pragma once');
  927. writeln(t,'#define CONFIG_APP_BUILD_USE_FLASH_SECTIONS 1');
  928. writeln(t,'#define CONFIG_BT_RESERVE_DRAM 0x0');
  929. writeln(t,'#define CONFIG_ESP32_ULP_COPROC_RESERVE_MEM 0');
  930. writeln(t,'#define CONFIG_ESP32_TRACEMEM_RESERVE_DRAM 0x0');
  931. Close(t);
  932. if ioresult<>0 then
  933. exit;
  934. {$pop}
  935. end;
  936. { generate an sdkconfig if none is provided,
  937. this is a dummy so far }
  938. if not(Sysutils.FileExists('sdkconfig')) then
  939. begin
  940. Assign(t,'sdkconfig');
  941. {$push}{$I-}
  942. Rewrite(t);
  943. if ioresult<>0 then
  944. exit;
  945. writeln(t);
  946. Close(t);
  947. if ioresult<>0 then
  948. exit;
  949. {$pop}
  950. end;
  951. { generate an Kconfig if none is provided,
  952. this is a dummy so far }
  953. if not(Sysutils.FileExists('Kconfig')) then
  954. begin
  955. Assign(t,'Kconfig');
  956. {$push}{$I-}
  957. Rewrite(t);
  958. if ioresult<>0 then
  959. exit;
  960. writeln(t);
  961. Close(t);
  962. if ioresult<>0 then
  963. exit;
  964. {$pop}
  965. end;
  966. { generate an Kconfig.projbuild if none is provided,
  967. this is a dummy so far }
  968. if not(Sysutils.FileExists('Kconfig.projbuild')) then
  969. begin
  970. Assign(t,'Kconfig.projbuild');
  971. {$push}{$I-}
  972. Rewrite(t);
  973. if ioresult<>0 then
  974. exit;
  975. writeln(t);
  976. Close(t);
  977. if ioresult<>0 then
  978. exit;
  979. {$pop}
  980. end;
  981. { generate an kconfigs.in if none is provided,
  982. this is a dummy so far }
  983. if not(Sysutils.FileExists('kconfigs.in')) then
  984. begin
  985. Assign(t,'kconfigs.in');
  986. {$push}{$I-}
  987. Rewrite(t);
  988. if ioresult<>0 then
  989. exit;
  990. writeln(t);
  991. Close(t);
  992. if ioresult<>0 then
  993. exit;
  994. {$pop}
  995. end;
  996. { generate an kconfigs_projbuild.in if none is provided,
  997. this is a dummy so far }
  998. if not(Sysutils.FileExists('kconfigs_projbuild.in')) then
  999. begin
  1000. Assign(t,'kconfigs_projbuild.in');
  1001. {$push}{$I-}
  1002. Rewrite(t);
  1003. if ioresult<>0 then
  1004. exit;
  1005. writeln(t);
  1006. Close(t);
  1007. if ioresult<>0 then
  1008. exit;
  1009. {$pop}
  1010. end;
  1011. { generate a config.env if none is provided,
  1012. COMPONENT_KCONFIGS and COMPONENT_KCONFIGS_PROJBUILD are dummy fields and might
  1013. be needed to be filed properly }
  1014. if not(Sysutils.FileExists('config.env')) then
  1015. begin
  1016. Assign(t,'config.env');
  1017. {$push}{$I-}
  1018. Rewrite(t);
  1019. if ioresult<>0 then
  1020. exit;
  1021. writeln(t,'{');
  1022. writeln(t,' "COMPONENT_KCONFIGS": "Kconfig",');
  1023. writeln(t,' "COMPONENT_KCONFIGS_PROJBUILD": "Kconfig.projbuild",');
  1024. writeln(t,' "IDF_CMAKE": "y",');
  1025. writeln(t,' "IDF_TARGET": "esp32",');
  1026. writeln(t,' "IDF_PATH": "'+GetEnvironmentVariable('IDF_PATH')+'",');
  1027. writeln(t,' "COMPONENT_KCONFIGS_SOURCE_FILE": "kconfigs.in",');
  1028. writeln(t,' "COMPONENT_KCONFIGS_PROJBUILD_SOURCE_FILE": "kconfigs_projbuild.in"');
  1029. writeln(t,'}');
  1030. Close(t);
  1031. if ioresult<>0 then
  1032. exit;
  1033. {$pop}
  1034. end;
  1035. { generate ldgen_libraries }
  1036. Assign(t,'ldgen_libraries');
  1037. {$push}{$I-}
  1038. Rewrite(t);
  1039. if ioresult<>0 then
  1040. exit;
  1041. hp:=TCmdStrListItem(StaticLibFiles.First);
  1042. while assigned(hp) do
  1043. begin
  1044. FindLibraryFile(hp.Str,target_info.staticClibprefix,target_info.staticClibext,filepath);
  1045. writeln(t,filepath);
  1046. hp:=TCmdStrListItem(hp.Next);
  1047. end;
  1048. Close(t);
  1049. if ioresult<>0 then
  1050. exit;
  1051. {$pop}
  1052. binstr:='xtensa-esp32-elf-gcc';
  1053. cmdstr:='-C -P -x c -E -o esp32_out.ld -I . $IDF_PATH/components/esp32/ld/esp32.ld';
  1054. Replace(binstr,'$IDF_PATH',maybequoted(GetEnvironmentVariable('IDF_PATH')));
  1055. Replace(cmdstr,'$IDF_PATH',maybequoted(GetEnvironmentVariable('IDF_PATH')));
  1056. success:=DoExec(binstr,cmdstr,true,true);
  1057. { generate linker maps for esp32 }
  1058. binstr:='$IDF_PATH/tools/ldgen/ldgen.py';
  1059. cmdstr:='--config sdkconfig --fragments $IDF_PATH/components/xtensa/linker.lf $IDF_PATH/components/soc/linker.lf $IDF_PATH/components/esp_event/linker.lf '+
  1060. '$IDF_PATH/components/spi_flash/linker.lf $IDF_PATH/components/esp_wifi/linker.lf $IDF_PATH/components/lwip/linker.lf '+
  1061. '$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 '+
  1062. '$IDF_PATH/components/esp32/ld/esp32_fragments.lf $IDF_PATH/components/freertos/linker.lf $IDF_PATH/components/newlib/newlib.lf '+
  1063. '$IDF_PATH/components/esp_gdbstub/linker.lf '+
  1064. '--input $IDF_PATH/components/esp32/ld/esp32.project.ld.in --output ./esp32.project.ld --kconfig $IDF_PATH/Kconfig --env-file config.env '+
  1065. '--libraries-file ldgen_libraries --objdump xtensa-esp32-elf-objdump';
  1066. Replace(binstr,'$IDF_PATH',maybequoted(GetEnvironmentVariable('IDF_PATH')));
  1067. Replace(cmdstr,'$IDF_PATH',maybequoted(GetEnvironmentVariable('IDF_PATH')));
  1068. if success and not(cs_link_nolink in current_settings.globalswitches) then
  1069. success:=DoExec(binstr,cmdstr,true,false);
  1070. end;
  1071. FixedExeFileName:=maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename,'.elf')));
  1072. GCSectionsStr:='--gc-sections';
  1073. //if not(cs_link_extern in current_settings.globalswitches) then
  1074. if not(cs_link_nolink in current_settings.globalswitches) then
  1075. Message1(exec_i_linking,current_module.exefilename);
  1076. if (cs_link_map in current_settings.globalswitches) then
  1077. mapstr:='-Map '+maybequoted(ChangeFileExt(current_module.exefilename,'.map'));
  1078. { Write used files and libraries }
  1079. WriteResponseFile();
  1080. { Call linker }
  1081. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  1082. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  1083. if target_info.system=system_xtensa_freertos then
  1084. Replace(cmdstr,'$IDF_PATH',maybequoted(GetEnvironmentVariable('IDF_PATH')));
  1085. if not(cs_link_on_target in current_settings.globalswitches) then
  1086. begin
  1087. Replace(cmdstr,'$EXE',FixedExeFileName);
  1088. Replace(cmdstr,'$RES',(maybequoted(ScriptFixFileName(outputexedir+Info.ResName))));
  1089. Replace(cmdstr,'$STATIC',StaticStr);
  1090. Replace(cmdstr,'$STRIP',StripStr);
  1091. Replace(cmdstr,'$MAP',mapstr);
  1092. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  1093. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  1094. end
  1095. else
  1096. begin
  1097. Replace(cmdstr,'$EXE',FixedExeFileName);
  1098. Replace(cmdstr,'$RES',maybequoted(ScriptFixFileName(outputexedir+Info.ResName)));
  1099. Replace(cmdstr,'$STATIC',StaticStr);
  1100. Replace(cmdstr,'$STRIP',StripStr);
  1101. Replace(cmdstr,'$MAP',mapstr);
  1102. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  1103. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  1104. end;
  1105. if success and not(cs_link_nolink in current_settings.globalswitches) then
  1106. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  1107. { Remove ReponseFile }
  1108. if success and not(cs_link_nolink in current_settings.globalswitches) then
  1109. DeleteFile(outputexedir+Info.ResName);
  1110. { Post process }
  1111. if success and not(cs_link_nolink in current_settings.globalswitches) then
  1112. success:=PostProcessExecutable(FixedExeFileName,false);
  1113. if success and (target_info.system=system_xtensa_freertos) then
  1114. begin
  1115. binstr:='$IDF_PATH/components/esptool_py/esptool/esptool.py';
  1116. Replace(binstr,'$IDF_PATH',maybequoted(GetEnvironmentVariable('IDF_PATH')));
  1117. success:=DoExec(binstr,'--chip esp32 elf2image --flash_mode dio --flash_freq 40m '+
  1118. '--flash_size '+tostr(embedded_controllers[current_settings.controllertype].flashsize div (1024*1024))+'MB '+
  1119. '--elf-sha256-offset 0xb0 '+
  1120. '-o '+maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename,'.bin')))+' '+
  1121. FixedExeFileName,
  1122. true,false);
  1123. end
  1124. else
  1125. if success then
  1126. success:=DoExec(FindUtil(utilsprefix+'objcopy'),'-O binary '+
  1127. FixedExeFileName+' '+
  1128. maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename,'.bin'))),true,false);
  1129. MakeExecutable:=success; { otherwise a recursive call to link method }
  1130. end;
  1131. function TlinkerFreeRTOS.postprocessexecutable(const fn : string;isdll:boolean):boolean;
  1132. type
  1133. TElf32header=packed record
  1134. magic0123 : longint;
  1135. file_class : byte;
  1136. data_encoding : byte;
  1137. file_version : byte;
  1138. padding : array[$07..$0f] of byte;
  1139. e_type : word;
  1140. e_machine : word;
  1141. e_version : longint;
  1142. e_entry : longint; { entrypoint }
  1143. e_phoff : longint; { program header offset }
  1144. e_shoff : longint; { sections header offset }
  1145. e_flags : longint;
  1146. e_ehsize : word; { elf header size in bytes }
  1147. e_phentsize : word; { size of an entry in the program header array }
  1148. e_phnum : word; { 0..e_phnum-1 of entrys }
  1149. e_shentsize : word; { size of an entry in sections header array }
  1150. e_shnum : word; { 0..e_shnum-1 of entrys }
  1151. e_shstrndx : word; { index of string section header }
  1152. end;
  1153. TElf32sechdr=packed record
  1154. sh_name : longint;
  1155. sh_type : longint;
  1156. sh_flags : longint;
  1157. sh_addr : longint;
  1158. sh_offset : longint;
  1159. sh_size : longint;
  1160. sh_link : longint;
  1161. sh_info : longint;
  1162. sh_addralign : longint;
  1163. sh_entsize : longint;
  1164. end;
  1165. function MayBeSwapHeader(h : telf32header) : telf32header;
  1166. begin
  1167. result:=h;
  1168. if source_info.endian<>target_info.endian then
  1169. with h do
  1170. begin
  1171. result.e_type:=swapendian(e_type);
  1172. result.e_machine:=swapendian(e_machine);
  1173. result.e_version:=swapendian(e_version);
  1174. result.e_entry:=swapendian(e_entry);
  1175. result.e_phoff:=swapendian(e_phoff);
  1176. result.e_shoff:=swapendian(e_shoff);
  1177. result.e_flags:=swapendian(e_flags);
  1178. result.e_ehsize:=swapendian(e_ehsize);
  1179. result.e_phentsize:=swapendian(e_phentsize);
  1180. result.e_phnum:=swapendian(e_phnum);
  1181. result.e_shentsize:=swapendian(e_shentsize);
  1182. result.e_shnum:=swapendian(e_shnum);
  1183. result.e_shstrndx:=swapendian(e_shstrndx);
  1184. end;
  1185. end;
  1186. function MaybeSwapSecHeader(h : telf32sechdr) : telf32sechdr;
  1187. begin
  1188. result:=h;
  1189. if source_info.endian<>target_info.endian then
  1190. with h do
  1191. begin
  1192. result.sh_name:=swapendian(sh_name);
  1193. result.sh_type:=swapendian(sh_type);
  1194. result.sh_flags:=swapendian(sh_flags);
  1195. result.sh_addr:=swapendian(sh_addr);
  1196. result.sh_offset:=swapendian(sh_offset);
  1197. result.sh_size:=swapendian(sh_size);
  1198. result.sh_link:=swapendian(sh_link);
  1199. result.sh_info:=swapendian(sh_info);
  1200. result.sh_addralign:=swapendian(sh_addralign);
  1201. result.sh_entsize:=swapendian(sh_entsize);
  1202. end;
  1203. end;
  1204. var
  1205. f : file;
  1206. function ReadSectionName(pos : longint) : String;
  1207. var
  1208. oldpos : longint;
  1209. c : char;
  1210. begin
  1211. oldpos:=filepos(f);
  1212. seek(f,pos);
  1213. Result:='';
  1214. while true do
  1215. begin
  1216. blockread(f,c,1);
  1217. if c=#0 then
  1218. break;
  1219. Result:=Result+c;
  1220. end;
  1221. seek(f,oldpos);
  1222. end;
  1223. var
  1224. elfheader : TElf32header;
  1225. secheader : TElf32sechdr;
  1226. i : longint;
  1227. stringoffset : longint;
  1228. secname : string;
  1229. begin
  1230. postprocessexecutable:=false;
  1231. { open file }
  1232. assign(f,fn);
  1233. {$push}{$I-}
  1234. reset(f,1);
  1235. if ioresult<>0 then
  1236. Message1(execinfo_f_cant_open_executable,fn);
  1237. { read header }
  1238. blockread(f,elfheader,sizeof(tElf32header));
  1239. elfheader:=MayBeSwapHeader(elfheader);
  1240. seek(f,elfheader.e_shoff);
  1241. { read string section header }
  1242. seek(f,elfheader.e_shoff+sizeof(TElf32sechdr)*elfheader.e_shstrndx);
  1243. blockread(f,secheader,sizeof(secheader));
  1244. secheader:=MaybeSwapSecHeader(secheader);
  1245. stringoffset:=secheader.sh_offset;
  1246. seek(f,elfheader.e_shoff);
  1247. status.datasize:=0;
  1248. for i:=0 to elfheader.e_shnum-1 do
  1249. begin
  1250. blockread(f,secheader,sizeof(secheader));
  1251. secheader:=MaybeSwapSecHeader(secheader);
  1252. secname:=ReadSectionName(stringoffset+secheader.sh_name);
  1253. if secname='.text' then
  1254. begin
  1255. Message1(execinfo_x_codesize,tostr(secheader.sh_size));
  1256. status.codesize:=secheader.sh_size;
  1257. end
  1258. else if secname='.data' then
  1259. begin
  1260. Message1(execinfo_x_initdatasize,tostr(secheader.sh_size));
  1261. inc(status.datasize,secheader.sh_size);
  1262. end
  1263. else if secname='.bss' then
  1264. begin
  1265. Message1(execinfo_x_uninitdatasize,tostr(secheader.sh_size));
  1266. inc(status.datasize,secheader.sh_size);
  1267. end;
  1268. end;
  1269. close(f);
  1270. {$pop}
  1271. if ioresult<>0 then
  1272. ;
  1273. postprocessexecutable:=true;
  1274. end;
  1275. {*****************************************************************************
  1276. Initialize
  1277. *****************************************************************************}
  1278. initialization
  1279. {$ifdef arm}
  1280. RegisterLinker(ld_freertos,TlinkerFreeRTOS);
  1281. RegisterTarget(system_arm_freertos_info);
  1282. {$endif arm}
  1283. {$ifdef avr}
  1284. RegisterLinker(ld_freertos,TLinkerEmbedded);
  1285. RegisterTarget(system_avr_embedded_info);
  1286. {$endif avr}
  1287. {$ifdef i386}
  1288. RegisterLinker(ld_freertos,TLinkerEmbedded);
  1289. RegisterTarget(system_i386_embedded_info);
  1290. {$endif i386}
  1291. {$ifdef x86_64}
  1292. RegisterLinker(ld_freertos,TLinkerEmbedded);
  1293. RegisterTarget(system_x86_64_embedded_info);
  1294. {$endif x86_64}
  1295. {$ifdef i8086}
  1296. { no need to register linker ld_embedded, because i8086_embedded uses the
  1297. regular msdos linker. In case a flat binary, relocated for a specific
  1298. segment address is needed (e.g. for a BIOS or a real mode bootloader), it
  1299. can be produced post-compilation with exe2bin or a similar tool. }
  1300. RegisterTarget(system_i8086_embedded_info);
  1301. {$endif i8086}
  1302. {$ifdef mipsel}
  1303. RegisterLinker(ld_freertos,TLinkerEmbedded);
  1304. RegisterTarget(system_mipsel_embedded_info);
  1305. {$endif mipsel}
  1306. {$ifdef m68k}
  1307. RegisterLinker(ld_freertos,TLinkerEmbedded);
  1308. RegisterTarget(system_m68k_embedded_info);
  1309. {$endif m68k}
  1310. {$ifdef riscv32}
  1311. RegisterLinker(ld_freertos,TLinkerEmbedded);
  1312. RegisterTarget(system_riscv32_embedded_info);
  1313. {$endif riscv32}
  1314. {$ifdef riscv64}
  1315. RegisterLinker(ld_freertos,TLinkerEmbedded);
  1316. RegisterTarget(system_riscv64_embedded_info);
  1317. {$endif riscv64}
  1318. {$ifdef xtensa}
  1319. RegisterLinker(ld_freertos,TlinkerFreeRTOS);
  1320. RegisterTarget(system_xtensa_freertos_info);
  1321. {$endif xtensa}
  1322. end.