t_freertos.pas 51 KB

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