t_freertos.pas 50 KB

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