t_embed.pas 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298
  1. {
  2. This unit implements support import,export,link routines
  3. for the (arm) GameBoy Advance target
  4. Copyright (c) 2001-2002 by Peter Vreman
  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_embed;
  19. {$i fpcdefs.inc}
  20. interface
  21. implementation
  22. uses
  23. SysUtils,
  24. cutils,cfileutl,cclasses,
  25. globtype,globals,systems,verbose,comphook,script,fmodule,i_embed,link,
  26. cpuinfo;
  27. type
  28. TlinkerEmbedded=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 TlinkerEmbedded.Create;
  41. begin
  42. Inherited Create;
  43. SharedLibFiles.doubles:=true;
  44. StaticLibFiles.doubles:=true;
  45. end;
  46. procedure TlinkerEmbedded.SetDefaultInfo;
  47. const
  48. {$ifdef mips}
  49. {$ifdef mipsel}
  50. platform_select='-EL';
  51. {$else}
  52. platform_select='-EB';
  53. {$endif}
  54. {$else}
  55. platform_select='';
  56. {$endif}
  57. begin
  58. with Info do
  59. begin
  60. ExeCmd[1]:='ld -g '+platform_select+' $OPT $DYNLINK $STATIC $GCSECTIONS $STRIP -L. -o $EXE -T $RES';
  61. end;
  62. end;
  63. Function TlinkerEmbedded.WriteResponseFile: Boolean;
  64. Var
  65. linkres : TLinkRes;
  66. i : longint;
  67. HPath : TCmdStrListItem;
  68. s,s1,s2 : TCmdStr;
  69. prtobj,
  70. cprtobj : string[80];
  71. linklibc : boolean;
  72. found1,
  73. found2 : boolean;
  74. {$if defined(ARM) or defined(MIPSEL)}
  75. LinkStr : string;
  76. {$endif}
  77. begin
  78. WriteResponseFile:=False;
  79. linklibc:=(SharedLibFiles.Find('c')<>nil);
  80. {$if defined(ARM) or defined(i386) or defined(AVR) or defined(MIPSEL)}
  81. prtobj:='';
  82. {$else}
  83. prtobj:='prt0';
  84. {$endif}
  85. cprtobj:='cprt0';
  86. if linklibc then
  87. prtobj:=cprtobj;
  88. { Open link.res file }
  89. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  90. { Write path to search libraries }
  91. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  92. while assigned(HPath) do
  93. begin
  94. s:=HPath.Str;
  95. if (cs_link_on_target in current_settings.globalswitches) then
  96. s:=ScriptFixFileName(s);
  97. LinkRes.Add('-L'+s);
  98. HPath:=TCmdStrListItem(HPath.Next);
  99. end;
  100. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  101. while assigned(HPath) do
  102. begin
  103. s:=HPath.Str;
  104. if s<>'' then
  105. LinkRes.Add('SEARCH_DIR("'+s+'")');
  106. HPath:=TCmdStrListItem(HPath.Next);
  107. end;
  108. LinkRes.Add('INPUT (');
  109. { add objectfiles, start with prt0 always }
  110. //s:=FindObjectFile('prt0','',false);
  111. if prtobj<>'' then
  112. begin
  113. s:=FindObjectFile(prtobj,'',false);
  114. LinkRes.AddFileName(s);
  115. end;
  116. { try to add crti and crtbegin if linking to C }
  117. if linklibc then
  118. begin
  119. if librarysearchpath.FindFile('crtbegin.o',false,s) then
  120. LinkRes.AddFileName(s);
  121. if librarysearchpath.FindFile('crti.o',false,s) then
  122. LinkRes.AddFileName(s);
  123. end;
  124. while not ObjectFiles.Empty do
  125. begin
  126. s:=ObjectFiles.GetFirst;
  127. if s<>'' then
  128. begin
  129. { vlink doesn't use SEARCH_DIR for object files }
  130. if not(cs_link_on_target in current_settings.globalswitches) then
  131. s:=FindObjectFile(s,'',false);
  132. LinkRes.AddFileName((maybequoted(s)));
  133. end;
  134. end;
  135. { Write staticlibraries }
  136. if not StaticLibFiles.Empty then
  137. begin
  138. { vlink doesn't need, and doesn't support GROUP }
  139. if (cs_link_on_target in current_settings.globalswitches) then
  140. begin
  141. LinkRes.Add(')');
  142. LinkRes.Add('GROUP(');
  143. end;
  144. while not StaticLibFiles.Empty do
  145. begin
  146. S:=StaticLibFiles.GetFirst;
  147. LinkRes.AddFileName((maybequoted(s)));
  148. end;
  149. end;
  150. if (cs_link_on_target in current_settings.globalswitches) then
  151. begin
  152. LinkRes.Add(')');
  153. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  154. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  155. linklibc:=false;
  156. while not SharedLibFiles.Empty do
  157. begin
  158. S:=SharedLibFiles.GetFirst;
  159. if s<>'c' then
  160. begin
  161. i:=Pos(target_info.sharedlibext,S);
  162. if i>0 then
  163. Delete(S,i,255);
  164. LinkRes.Add('-l'+s);
  165. end
  166. else
  167. begin
  168. LinkRes.Add('-l'+s);
  169. linklibc:=true;
  170. end;
  171. end;
  172. { be sure that libc&libgcc is the last lib }
  173. if linklibc then
  174. begin
  175. LinkRes.Add('-lc');
  176. LinkRes.Add('-lgcc');
  177. end;
  178. end
  179. else
  180. begin
  181. while not SharedLibFiles.Empty do
  182. begin
  183. S:=SharedLibFiles.GetFirst;
  184. LinkRes.Add('lib'+s+target_info.staticlibext);
  185. end;
  186. LinkRes.Add(')');
  187. end;
  188. { objects which must be at the end }
  189. if linklibc then
  190. begin
  191. found1:=librarysearchpath.FindFile('crtend.o',false,s1);
  192. found2:=librarysearchpath.FindFile('crtn.o',false,s2);
  193. if found1 or found2 then
  194. begin
  195. LinkRes.Add('INPUT(');
  196. if found1 then
  197. LinkRes.AddFileName(s1);
  198. if found2 then
  199. LinkRes.AddFileName(s2);
  200. LinkRes.Add(')');
  201. end;
  202. end;
  203. {$ifdef ARM}
  204. case current_settings.controllertype of
  205. ct_none:
  206. begin
  207. end;
  208. ct_lpc810m021fn8,
  209. ct_lpc811m001fdh16,
  210. ct_lpc812m101fdh16,
  211. ct_lpc812m101fd20,
  212. ct_lpc812m101fdh20,
  213. ct_lpc1110fd20,
  214. ct_lpc1111fdh20_002,
  215. ct_lpc1111fhn33_101,
  216. ct_lpc1111fhn33_102,
  217. ct_lpc1111fhn33_103,
  218. ct_lpc1111fhn33_201,
  219. ct_lpc1111fhn33_202,
  220. ct_lpc1111fhn33_203,
  221. ct_lpc1112fd20_102,
  222. ct_lpc1112fdh20_102,
  223. ct_lpc1112fdh28_102,
  224. ct_lpc1112fhn33_101,
  225. ct_lpc1112fhn33_102,
  226. ct_lpc1112fhn33_103,
  227. ct_lpc1112fhn33_201,
  228. ct_lpc1112fhn24_202,
  229. ct_lpc1112fhn33_202,
  230. ct_lpc1112fhn33_203,
  231. ct_lpc1112fhi33_202,
  232. ct_lpc1112fhi33_203,
  233. ct_lpc1113fhn33_201,
  234. ct_lpc1113fhn33_202,
  235. ct_lpc1113fhn33_203,
  236. ct_lpc1113fhn33_301,
  237. ct_lpc1113fhn33_302,
  238. ct_lpc1113fhn33_303,
  239. ct_lpc1113bfd48_301,
  240. ct_lpc1113bfd48_302,
  241. ct_lpc1113bfd48_303,
  242. ct_lpc1114fdh28_102,
  243. ct_lpc1114fn28_102,
  244. ct_lpc1114fhn33_201,
  245. ct_lpc1114fhn33_202,
  246. ct_lpc1114fhn33_203,
  247. ct_lpc1114fhn33_301,
  248. ct_lpc1114fhn33_302,
  249. ct_lpc1114fhn33_303,
  250. ct_lpc1114fhn33_333,
  251. ct_lpc1114fhi33_302,
  252. ct_lpc1114fhi33_303,
  253. ct_lpc1114bfd48_301,
  254. ct_lpc1114bfd48_302,
  255. ct_lpc1114bfd48_303,
  256. ct_lpc1114bfd48_323,
  257. ct_lpc1114bfd48_333,
  258. ct_lpc1115bfd48_303,
  259. ct_lpc11c12fd48_301,
  260. ct_lpc11c14fd48_301,
  261. ct_lpc11c22fd48_301,
  262. ct_lpc11c24fd48_301,
  263. ct_lpc11d24fd48_301,
  264. ct_lpc1224fbd48_101,
  265. ct_lpc1224fbd48_121,
  266. ct_lpc1224fbd64_101,
  267. ct_lpc1224fbd64_121,
  268. ct_lpc1225fbd48_301,
  269. ct_lpc1225fbd48_321,
  270. ct_lpc1225fbd64_301,
  271. ct_lpc1225fbd64_321,
  272. ct_lpc1226fbd48_301,
  273. ct_lpc1226fbd64_301,
  274. ct_lpc1227fbd48_301,
  275. ct_lpc1227fbd64_301,
  276. ct_lpc12d27fbd100_301,
  277. ct_lpc1311fhn33,
  278. ct_lpc1311fhn33_01,
  279. ct_lpc1313fhn33,
  280. ct_lpc1313fhn33_01,
  281. ct_lpc1313fbd48,
  282. ct_lpc1313fbd48_01,
  283. ct_lpc1315fhn33,
  284. ct_lpc1315fbd48,
  285. ct_lpc1316fhn33,
  286. ct_lpc1316fbd48,
  287. ct_lpc1317fhn33,
  288. ct_lpc1317fbd48,
  289. ct_lpc1317fbd64,
  290. ct_lpc1342fhn33,
  291. ct_lpc1342fbd48,
  292. ct_lpc1343fhn33,
  293. ct_lpc1343fbd48,
  294. ct_lpc1345fhn33,
  295. ct_lpc1345fbd48,
  296. ct_lpc1346fhn33,
  297. ct_lpc1346fbd48,
  298. ct_lpc1347fhn33,
  299. ct_lpc1347fbd48,
  300. ct_lpc1347fbd64,
  301. ct_lpc2114,
  302. ct_lpc2124,
  303. ct_lpc2194,
  304. ct_lpc1768,
  305. ct_at91sam7s256,
  306. ct_at91sam7se256,
  307. ct_at91sam7x256,
  308. ct_at91sam7xc256,
  309. ct_stm32f030c6,
  310. ct_stm32f030c8,
  311. ct_stm32f030f4,
  312. ct_stm32f030k6,
  313. ct_stm32f030r8,
  314. ct_stm32f050c4,
  315. ct_stm32f050c6,
  316. ct_stm32f050f4,
  317. ct_stm32f050f6,
  318. ct_stm32f050g4,
  319. ct_stm32f050g6,
  320. ct_stm32f050k4,
  321. ct_stm32f050k6,
  322. ct_stm32f051c4,
  323. ct_stm32f051c6,
  324. ct_stm32f051c8,
  325. ct_stm32f051k4,
  326. ct_stm32f051k6,
  327. ct_stm32f051k8,
  328. ct_stm32f051r4,
  329. ct_stm32f051r6,
  330. ct_stm32f051r8,
  331. ct_stm32f100x4,
  332. ct_stm32f100x6,
  333. ct_stm32f100x8,
  334. ct_stm32f100xB,
  335. ct_stm32f100xC,
  336. ct_stm32f100xD,
  337. ct_stm32f100xE,
  338. ct_stm32f101x4,
  339. ct_stm32f101x6,
  340. ct_stm32f101x8,
  341. ct_stm32f101xB,
  342. ct_stm32f101xC,
  343. ct_stm32f101xD,
  344. ct_stm32f101xE,
  345. ct_stm32f101xF,
  346. ct_stm32f101xG,
  347. ct_stm32f102x4,
  348. ct_stm32f102x6,
  349. ct_stm32f102x8,
  350. ct_stm32f102xB,
  351. ct_stm32f103x4,
  352. ct_stm32f103x6,
  353. ct_stm32f103x8,
  354. ct_stm32f103xB,
  355. ct_stm32f103xC,
  356. ct_stm32f103xD,
  357. ct_stm32f103xE,
  358. ct_stm32f103xF,
  359. ct_stm32f103xG,
  360. ct_stm32f107x8,
  361. ct_stm32f107xB,
  362. ct_stm32f107xC,
  363. ct_stm32f105r8,
  364. ct_stm32f105rb,
  365. ct_stm32f105rc,
  366. ct_stm32f105v8,
  367. ct_stm32f105vb,
  368. ct_stm32f105vc,
  369. ct_stm32f107rb,
  370. ct_stm32f107rc,
  371. ct_stm32f107vb,
  372. ct_stm32f107vc,
  373. { TI - 64 K Flash, 16 K SRAM Devices }
  374. ct_lm3s1110,
  375. ct_lm3s1133,
  376. ct_lm3s1138,
  377. ct_lm3s1150,
  378. ct_lm3s1162,
  379. ct_lm3s1165,
  380. ct_lm3s1166,
  381. ct_lm3s2110,
  382. ct_lm3s2139,
  383. ct_lm3s6100,
  384. ct_lm3s6110,
  385. { TI 128 K Flash, 32 K SRAM devices - Fury Class }
  386. ct_lm3s1601,
  387. ct_lm3s1608,
  388. ct_lm3s1620,
  389. ct_lm3s1635,
  390. ct_lm3s1636,
  391. ct_lm3s1637,
  392. ct_lm3s1651,
  393. ct_lm3s2601,
  394. ct_lm3s2608,
  395. ct_lm3s2620,
  396. ct_lm3s2637,
  397. ct_lm3s2651,
  398. ct_lm3s6610,
  399. ct_lm3s6611,
  400. ct_lm3s6618,
  401. ct_lm3s6633,
  402. ct_lm3s6637,
  403. ct_lm3s8630,
  404. { TI 256 K Flase, 32 K SRAM devices - Fury Class }
  405. ct_lm3s1911,
  406. ct_lm3s1918,
  407. ct_lm3s1937,
  408. ct_lm3s1958,
  409. ct_lm3s1960,
  410. ct_lm3s1968,
  411. ct_lm3s1969,
  412. ct_lm3s2911,
  413. ct_lm3s2918,
  414. ct_lm3s2919,
  415. ct_lm3s2939,
  416. ct_lm3s2948,
  417. ct_lm3s2950,
  418. ct_lm3s2965,
  419. ct_lm3s6911,
  420. ct_lm3s6918,
  421. ct_lm3s6938,
  422. ct_lm3s6950,
  423. ct_lm3s6952,
  424. ct_lm3s6965,
  425. ct_lm3s8930,
  426. ct_lm3s8933,
  427. ct_lm3s8938,
  428. ct_lm3s8962,
  429. ct_lm3s8970,
  430. ct_lm3s8971,
  431. { TI - Tempest Tempest - 256 K Flash, 64 K SRAM }
  432. ct_lm3s5951,
  433. ct_lm3s5956,
  434. ct_lm3s1b21,
  435. ct_lm3s2b93,
  436. ct_lm3s5b91,
  437. ct_lm3s9b81,
  438. ct_lm3s9b90,
  439. ct_lm3s9b92,
  440. ct_lm3s9b95,
  441. ct_lm3s9b96,
  442. ct_lm3s5d51,
  443. { TI - Stellaris something }
  444. ct_lm4f120h5,
  445. { Infineon }
  446. ct_xmc4500x1024,
  447. ct_xmc4500x768,
  448. ct_xmc4502x768,
  449. ct_xmc4504x512,
  450. ct_sc32442b,
  451. ct_thumb2bare:
  452. begin
  453. with embedded_controllers[current_settings.controllertype] do
  454. with linkres do
  455. begin
  456. Add('ENTRY(_START)');
  457. Add('MEMORY');
  458. Add('{');
  459. if flashsize<>0 then
  460. begin
  461. LinkStr := ' flash : ORIGIN = 0x' + IntToHex(flashbase,8)
  462. + ', LENGTH = 0x' + IntToHex(flashsize,8);
  463. Add(LinkStr);
  464. end;
  465. LinkStr := ' ram : ORIGIN = 0x' + IntToHex(srambase,8)
  466. + ', LENGTH = 0x' + IntToHex(sramsize,8);
  467. Add(LinkStr);
  468. Add('}');
  469. Add('_stack_top = 0x' + IntToHex(sramsize+srambase,8) + ';');
  470. // Add Checksum Calculation for LPC Controllers so that the bootloader starts the uploaded binary
  471. writeln(controllerunitstr);
  472. if (controllerunitstr = 'LPC8xx') or (controllerunitstr = 'LPC11XX') or (controllerunitstr = 'LPC122X') then
  473. Add('Startup_Checksum = 0 - (_stack_top + _START + 1 + NonMaskableInt_interrupt + 1 + Hardfault_interrupt + 1);');
  474. if (controllerunitstr = 'LPC13XX') then
  475. Add('Startup_Checksum = 0 - (_stack_top + _START + 1 + NonMaskableInt_interrupt + 1 + MemoryManagement_interrupt + 1 + BusFault_interrupt + 1 + UsageFault_interrupt + 1);');
  476. end;
  477. end
  478. else
  479. if not (cs_link_nolink in current_settings.globalswitches) then
  480. internalerror(200902011);
  481. end;
  482. with linkres do
  483. begin
  484. Add('SECTIONS');
  485. Add('{');
  486. Add(' .text :');
  487. Add(' {');
  488. Add(' _text_start = .;');
  489. Add(' KEEP(*(.init, .init.*))');
  490. Add(' *(.text, .text.*)');
  491. Add(' *(.strings)');
  492. Add(' *(.rodata, .rodata.*)');
  493. Add(' *(.comment)');
  494. Add(' _etext = .;');
  495. if embedded_controllers[current_settings.controllertype].flashsize<>0 then
  496. begin
  497. Add(' } >flash');
  498. end
  499. else
  500. begin
  501. Add(' } >ram');
  502. end;
  503. Add(' .note.gnu.build-id : { *(.note.gnu.build-id) } >flash ');
  504. Add(' .data :');
  505. Add(' {');
  506. Add(' _data = .;');
  507. Add(' *(.data, .data.*)');
  508. Add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  509. Add(' _edata = .;');
  510. if embedded_controllers[current_settings.controllertype].flashsize<>0 then
  511. begin
  512. Add(' } >ram AT >flash');
  513. end
  514. else
  515. begin
  516. Add(' } >ram');
  517. end;
  518. Add(' .bss :');
  519. Add(' {');
  520. Add(' _bss_start = .;');
  521. Add(' *(.bss, .bss.*)');
  522. Add(' *(COMMON)');
  523. Add(' } >ram');
  524. Add('. = ALIGN(4);');
  525. Add('_bss_end = . ;');
  526. Add('}');
  527. Add('_end = .;');
  528. end;
  529. {$endif ARM}
  530. {$ifdef i386}
  531. with linkres do
  532. begin
  533. Add('ENTRY(_START)');
  534. Add('SECTIONS');
  535. Add('{');
  536. Add(' . = 0x100000;');
  537. Add(' .text ALIGN (0x1000) :');
  538. Add(' {');
  539. Add(' KEEP(*(.init, .init.*))');
  540. Add(' *(.text, .text.*)');
  541. Add(' *(.strings)');
  542. Add(' *(.rodata, .rodata.*)');
  543. Add(' *(.comment)');
  544. Add(' _etext = .;');
  545. Add(' }');
  546. Add(' .data ALIGN (0x1000) :');
  547. Add(' {');
  548. Add(' _data = .;');
  549. Add(' *(.data, .data.*)');
  550. Add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  551. Add(' _edata = .;');
  552. Add(' }');
  553. Add(' . = ALIGN(4);');
  554. Add(' .bss :');
  555. Add(' {');
  556. Add(' _bss_start = .;');
  557. Add(' *(.bss, .bss.*)');
  558. Add(' *(COMMON)');
  559. Add(' }');
  560. Add('_bss_end = . ;');
  561. Add('}');
  562. Add('_end = .;');
  563. end;
  564. {$endif i386}
  565. {$ifdef AVR}
  566. with linkres do
  567. begin
  568. { linker script from ld 2.19 }
  569. Add('ENTRY(_START)');
  570. Add('OUTPUT_FORMAT("elf32-avr","elf32-avr","elf32-avr")');
  571. Add('OUTPUT_ARCH(avr:2)');
  572. Add('MEMORY');
  573. Add('{');
  574. Add(' text (rx) : ORIGIN = 0, LENGTH = 8K');
  575. Add(' data (rw!x) : ORIGIN = 0x800060, LENGTH = 0xffa0');
  576. Add(' eeprom (rw!x) : ORIGIN = 0x810000, LENGTH = 64K');
  577. Add(' fuse (rw!x) : ORIGIN = 0x820000, LENGTH = 1K');
  578. Add(' lock (rw!x) : ORIGIN = 0x830000, LENGTH = 1K');
  579. Add(' signature (rw!x) : ORIGIN = 0x840000, LENGTH = 1K');
  580. Add('}');
  581. Add('SECTIONS');
  582. Add('{');
  583. Add(' /* Read-only sections, merged into text segment: */');
  584. Add(' .hash : { *(.hash) }');
  585. Add(' .dynsym : { *(.dynsym) }');
  586. Add(' .dynstr : { *(.dynstr) }');
  587. Add(' .gnu.version : { *(.gnu.version) }');
  588. Add(' .gnu.version_d : { *(.gnu.version_d) }');
  589. Add(' .gnu.version_r : { *(.gnu.version_r) }');
  590. Add(' .rel.init : { *(.rel.init) }');
  591. Add(' .rela.init : { *(.rela.init) }');
  592. Add(' .rel.text :');
  593. Add(' {');
  594. Add(' *(.rel.text)');
  595. Add(' *(.rel.text.*)');
  596. Add(' *(.rel.gnu.linkonce.t*)');
  597. Add(' }');
  598. Add(' .rela.text :');
  599. Add(' {');
  600. Add(' *(.rela.text)');
  601. Add(' *(.rela.text.*)');
  602. Add(' *(.rela.gnu.linkonce.t*)');
  603. Add(' }');
  604. Add(' .rel.fini : { *(.rel.fini) }');
  605. Add(' .rela.fini : { *(.rela.fini) }');
  606. Add(' .rel.rodata :');
  607. Add(' {');
  608. Add(' *(.rel.rodata)');
  609. Add(' *(.rel.rodata.*)');
  610. Add(' *(.rel.gnu.linkonce.r*)');
  611. Add(' }');
  612. Add(' .rela.rodata :');
  613. Add(' {');
  614. Add(' *(.rela.rodata)');
  615. Add(' *(.rela.rodata.*)');
  616. Add(' *(.rela.gnu.linkonce.r*)');
  617. Add(' }');
  618. Add(' .rel.data :');
  619. Add(' {');
  620. Add(' *(.rel.data)');
  621. Add(' *(.rel.data.*)');
  622. Add(' *(.rel.gnu.linkonce.d*)');
  623. Add(' }');
  624. Add(' .rela.data :');
  625. Add(' {');
  626. Add(' *(.rela.data)');
  627. Add(' *(.rela.data.*)');
  628. Add(' *(.rela.gnu.linkonce.d*)');
  629. Add(' }');
  630. Add(' .rel.ctors : { *(.rel.ctors) }');
  631. Add(' .rela.ctors : { *(.rela.ctors) }');
  632. Add(' .rel.dtors : { *(.rel.dtors) }');
  633. Add(' .rela.dtors : { *(.rela.dtors) }');
  634. Add(' .rel.got : { *(.rel.got) }');
  635. Add(' .rela.got : { *(.rela.got) }');
  636. Add(' .rel.bss : { *(.rel.bss) }');
  637. Add(' .rela.bss : { *(.rela.bss) }');
  638. Add(' .rel.plt : { *(.rel.plt) }');
  639. Add(' .rela.plt : { *(.rela.plt) }');
  640. Add(' /* Internal text space or external memory. */');
  641. Add(' .text :');
  642. Add(' {');
  643. Add(' KEEP(*(.init, .init.*))');
  644. Add(' /* For data that needs to reside in the lower 64k of progmem. */');
  645. Add(' *(.progmem.gcc*)');
  646. Add(' *(.progmem*)');
  647. Add(' . = ALIGN(2);');
  648. Add(' __trampolines_start = . ;');
  649. Add(' /* The jump trampolines for the 16-bit limited relocs will reside here. */');
  650. Add(' *(.trampolines)');
  651. Add(' *(.trampolines*)');
  652. Add(' __trampolines_end = . ;');
  653. Add(' /* For future tablejump instruction arrays for 3 byte pc devices.');
  654. Add(' We don''t relax jump/call instructions within these sections. */');
  655. Add(' *(.jumptables)');
  656. Add(' *(.jumptables*)');
  657. Add(' /* For code that needs to reside in the lower 128k progmem. */');
  658. Add(' *(.lowtext)');
  659. Add(' *(.lowtext*)');
  660. Add(' __ctors_start = . ;');
  661. Add(' *(.ctors)');
  662. Add(' __ctors_end = . ;');
  663. Add(' __dtors_start = . ;');
  664. Add(' *(.dtors)');
  665. Add(' __dtors_end = . ;');
  666. Add(' KEEP(SORT(*)(.ctors))');
  667. Add(' KEEP(SORT(*)(.dtors))');
  668. Add(' /* From this point on, we don''t bother about wether the insns are');
  669. Add(' below or above the 16 bits boundary. */');
  670. Add(' *(.init0) /* Start here after reset. */');
  671. Add(' KEEP (*(.init0))');
  672. Add(' *(.init1)');
  673. Add(' KEEP (*(.init1))');
  674. Add(' *(.init2) /* Clear __zero_reg__, set up stack pointer. */');
  675. Add(' KEEP (*(.init2))');
  676. Add(' *(.init3)');
  677. Add(' KEEP (*(.init3))');
  678. Add(' *(.init4) /* Initialize data and BSS. */');
  679. Add(' KEEP (*(.init4))');
  680. Add(' *(.init5)');
  681. Add(' KEEP (*(.init5))');
  682. Add(' *(.init6) /* C++ constructors. */');
  683. Add(' KEEP (*(.init6))');
  684. Add(' *(.init7)');
  685. Add(' KEEP (*(.init7))');
  686. Add(' *(.init8)');
  687. Add(' KEEP (*(.init8))');
  688. Add(' *(.init9) /* Call main(). */');
  689. Add(' KEEP (*(.init9))');
  690. Add(' *(.text)');
  691. Add(' . = ALIGN(2);');
  692. Add(' *(.text.*)');
  693. Add(' . = ALIGN(2);');
  694. Add(' *(.fini9) /* _exit() starts here. */');
  695. Add(' KEEP (*(.fini9))');
  696. Add(' *(.fini8)');
  697. Add(' KEEP (*(.fini8))');
  698. Add(' *(.fini7)');
  699. Add(' KEEP (*(.fini7))');
  700. Add(' *(.fini6) /* C++ destructors. */');
  701. Add(' KEEP (*(.fini6))');
  702. Add(' *(.fini5)');
  703. Add(' KEEP (*(.fini5))');
  704. Add(' *(.fini4)');
  705. Add(' KEEP (*(.fini4))');
  706. Add(' *(.fini3)');
  707. Add(' KEEP (*(.fini3))');
  708. Add(' *(.fini2)');
  709. Add(' KEEP (*(.fini2))');
  710. Add(' *(.fini1)');
  711. Add(' KEEP (*(.fini1))');
  712. Add(' *(.fini0) /* Infinite loop after program termination. */');
  713. Add(' KEEP (*(.fini0))');
  714. Add(' _etext = . ;');
  715. Add(' } > text');
  716. Add(' .data : AT (ADDR (.text) + SIZEOF (.text))');
  717. Add(' {');
  718. Add(' PROVIDE (__data_start = .) ;');
  719. Add(' *(.data)');
  720. Add(' *(.data*)');
  721. Add(' *(.rodata) /* We need to include .rodata here if gcc is used */');
  722. Add(' *(.rodata*) /* with -fdata-sections. */');
  723. Add(' *(.gnu.linkonce.d*)');
  724. Add(' . = ALIGN(2);');
  725. Add(' _edata = . ;');
  726. Add(' PROVIDE (__data_end = .) ;');
  727. Add(' } > data');
  728. Add(' .bss : AT (ADDR (.bss))');
  729. Add(' {');
  730. Add(' PROVIDE (__bss_start = .) ;');
  731. Add(' *(.bss)');
  732. Add(' *(.bss*)');
  733. Add(' *(COMMON)');
  734. Add(' PROVIDE (__bss_end = .) ;');
  735. Add(' } > data');
  736. Add(' __data_load_start = LOADADDR(.data);');
  737. Add(' __data_load_end = __data_load_start + SIZEOF(.data);');
  738. Add(' /* Global data not cleared after reset. */');
  739. Add(' .noinit :');
  740. Add(' {');
  741. Add(' PROVIDE (__noinit_start = .) ;');
  742. Add(' *(.noinit*)');
  743. Add(' PROVIDE (__noinit_end = .) ;');
  744. Add(' _end = . ;');
  745. Add(' PROVIDE (__heap_start = .) ;');
  746. Add(' } > data');
  747. Add(' .eeprom :');
  748. Add(' {');
  749. Add(' *(.eeprom*)');
  750. Add(' __eeprom_end = . ;');
  751. Add(' } > eeprom');
  752. Add(' .fuse :');
  753. Add(' {');
  754. Add(' KEEP(*(.fuse))');
  755. Add(' KEEP(*(.lfuse))');
  756. Add(' KEEP(*(.hfuse))');
  757. Add(' KEEP(*(.efuse))');
  758. Add(' } > fuse');
  759. Add(' .lock :');
  760. Add(' {');
  761. Add(' KEEP(*(.lock*))');
  762. Add(' } > lock');
  763. Add(' .signature :');
  764. Add(' {');
  765. Add(' KEEP(*(.signature*))');
  766. Add(' } > signature');
  767. Add(' /* Stabs debugging sections. */');
  768. Add(' .stab 0 : { *(.stab) }');
  769. Add(' .stabstr 0 : { *(.stabstr) }');
  770. Add(' .stab.excl 0 : { *(.stab.excl) }');
  771. Add(' .stab.exclstr 0 : { *(.stab.exclstr) }');
  772. Add(' .stab.index 0 : { *(.stab.index) }');
  773. Add(' .stab.indexstr 0 : { *(.stab.indexstr) }');
  774. Add(' .comment 0 : { *(.comment) }');
  775. Add(' /* DWARF debug sections.');
  776. Add(' Symbols in the DWARF debugging sections are relative to the beginning');
  777. Add(' of the section so we begin them at 0. */');
  778. Add(' /* DWARF 1 */');
  779. Add(' .debug 0 : { *(.debug) }');
  780. Add(' .line 0 : { *(.line) }');
  781. Add(' /* GNU DWARF 1 extensions */');
  782. Add(' .debug_srcinfo 0 : { *(.debug_srcinfo) }');
  783. Add(' .debug_sfnames 0 : { *(.debug_sfnames) }');
  784. Add(' /* DWARF 1.1 and DWARF 2 */');
  785. Add(' .debug_aranges 0 : { *(.debug_aranges) }');
  786. Add(' .debug_pubnames 0 : { *(.debug_pubnames) }');
  787. Add(' /* DWARF 2 */');
  788. Add(' .debug_info 0 : { *(.debug_info) *(.gnu.linkonce.wi.*) }');
  789. Add(' .debug_abbrev 0 : { *(.debug_abbrev) }');
  790. Add(' .debug_line 0 : { *(.debug_line) }');
  791. Add(' .debug_frame 0 : { *(.debug_frame) }');
  792. Add(' .debug_str 0 : { *(.debug_str) }');
  793. Add(' .debug_loc 0 : { *(.debug_loc) }');
  794. Add(' .debug_macinfo 0 : { *(.debug_macinfo) }');
  795. Add('}');
  796. { last address of ram on an atmega128 }
  797. Add('_stack_top = 0x0fff;');
  798. end;
  799. {$endif AVR}
  800. {$ifdef MIPSEL}
  801. case current_settings.controllertype of
  802. ct_none:
  803. begin
  804. end;
  805. ct_pic32mx110f016b,
  806. ct_pic32mx110f016c,
  807. ct_pic32mx110f016d,
  808. ct_pic32mx120f032b,
  809. ct_pic32mx120f032c,
  810. ct_pic32mx120f032d,
  811. ct_pic32mx130f064b,
  812. ct_pic32mx130f064c,
  813. ct_pic32mx130f064d,
  814. ct_pic32mx150f128b,
  815. ct_pic32mx150f128c,
  816. ct_pic32mx150f128d,
  817. ct_pic32mx210f016b,
  818. ct_pic32mx210f016c,
  819. ct_pic32mx210f016d,
  820. ct_pic32mx220f032b,
  821. ct_pic32mx220f032c,
  822. ct_pic32mx220f032d,
  823. ct_pic32mx230f064b,
  824. ct_pic32mx230f064c,
  825. ct_pic32mx230f064d,
  826. ct_pic32mx250f128b,
  827. ct_pic32mx250f128c,
  828. ct_pic32mx250f128d,
  829. ct_pic32mx775f256h,
  830. ct_pic32mx775f256l,
  831. ct_pic32mx775f512h,
  832. ct_pic32mx775f512l,
  833. ct_pic32mx795f512h,
  834. ct_pic32mx795f512l:
  835. begin
  836. with embedded_controllers[current_settings.controllertype] do
  837. with linkres do
  838. begin
  839. Add('OUTPUT_FORMAT("elf32-tradlittlemips")');
  840. Add('OUTPUT_ARCH(pic32mx)');
  841. Add('ENTRY(_reset)');
  842. Add('PROVIDE(_vector_spacing = 0x00000001);');
  843. Add('_ebase_address = 0x'+IntToHex(flashbase,8)+';');
  844. Add('_RESET_ADDR = 0xBFC00000;');
  845. Add('_BEV_EXCPT_ADDR = 0xBFC00380;');
  846. Add('_DBG_EXCPT_ADDR = 0xBFC00480;');
  847. Add('_GEN_EXCPT_ADDR = _ebase_address + 0x180;');
  848. Add('MEMORY');
  849. Add('{');
  850. if flashsize<>0 then
  851. begin
  852. Add(' kseg0_program_mem : ORIGIN = 0x'+IntToHex(flashbase,8)+', LENGTH = 0x'+IntToHex(flashsize,8));
  853. //TODO This should better be placed into the controllertype records
  854. Add(' kseg1_boot_mem : ORIGIN = 0xBFC00000, LENGTH = 0xbef');
  855. Add(' config3 : ORIGIN = 0xBFC00BF0, LENGTH = 0x4');
  856. Add(' config2 : ORIGIN = 0xBFC00BF4, LENGTH = 0x4');
  857. Add(' config1 : ORIGIN = 0xBFC00BF8, LENGTH = 0x4');
  858. Add(' config0 : ORIGIN = 0xBFC00BFC, LENGTH = 0x4');
  859. end;
  860. Add(' ram : ORIGIN = 0x' + IntToHex(srambase,8)
  861. + ', LENGTH = 0x' + IntToHex(sramsize,8));
  862. Add('}');
  863. Add('_stack_top = 0x' + IntToHex(sramsize+srambase,8) + ';');
  864. end;
  865. end
  866. else
  867. if not (cs_link_nolink in current_settings.globalswitches) then
  868. internalerror(200902011);
  869. end;
  870. with linkres do
  871. begin
  872. Add('SECTIONS');
  873. Add('{');
  874. Add(' .reset _RESET_ADDR :');
  875. Add(' {');
  876. Add(' KEEP(*(.reset .reset.*))');
  877. Add(' KEEP(*(.startup .startup.*))');
  878. Add(' } > kseg1_boot_mem');
  879. Add(' .bev_excpt _BEV_EXCPT_ADDR :');
  880. Add(' {');
  881. Add(' KEEP(*(.bev_handler))');
  882. Add(' } > kseg1_boot_mem');
  883. Add(' .text :');
  884. Add(' {');
  885. Add(' _text_start = .;');
  886. Add(' . = _text_start + 0x180;');
  887. Add(' KEEP(*(.gen_handler))');
  888. Add(' . = _text_start + 0x200;');
  889. Add(' KEEP(*(.init .init.*))');
  890. Add(' *(.text .text.*)');
  891. Add(' *(.strings)');
  892. Add(' *(.rodata .rodata.*)');
  893. Add(' *(.comment)');
  894. Add(' _etext = .;');
  895. if embedded_controllers[current_settings.controllertype].flashsize<>0 then
  896. begin
  897. Add(' } >kseg0_program_mem');
  898. end
  899. else
  900. begin
  901. Add(' } >ram');
  902. end;
  903. Add(' .note.gnu.build-id : { *(.note.gnu.build-id) }');
  904. Add(' .data :');
  905. Add(' {');
  906. Add(' _data = .;');
  907. Add(' *(.data .data.*)');
  908. Add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  909. Add(' . = .;');
  910. Add(' _gp = ALIGN(16) + 0x7ff0;');
  911. Add(' _edata = .;');
  912. if embedded_controllers[current_settings.controllertype].flashsize<>0 then
  913. begin
  914. Add(' } >ram AT >kseg0_program_mem');
  915. end
  916. else
  917. begin
  918. Add(' } >ram');
  919. end;
  920. Add(' .config_BFC00BF0 : {');
  921. Add(' KEEP(*(.config_BFC00BF0))');
  922. Add(' } > config3');
  923. Add(' .config_BFC00BF4 : {');
  924. Add(' KEEP(*(.config_BFC00BF4))');
  925. Add(' } > config2');
  926. Add(' .config_BFC00BF8 : {');
  927. Add(' KEEP(*(.config_BFC00BF8))');
  928. Add(' } > config1');
  929. Add(' .config_BFC00BFC : {');
  930. Add(' KEEP(*(.config_BFC00BFC))');
  931. Add(' } > config0');
  932. Add(' .bss :');
  933. Add(' {');
  934. Add(' _bss_start = .;');
  935. Add(' *(.bss, .bss.*)');
  936. Add(' *(COMMON)');
  937. Add(' } >ram');
  938. Add('. = ALIGN(4);');
  939. Add('_bss_end = . ;');
  940. Add(' .comment 0 : { *(.comment) }');
  941. Add(' /* DWARF debug sections.');
  942. Add(' Symbols in the DWARF debugging sections are relative to the beginning');
  943. Add(' of the section so we begin them at 0. */');
  944. Add(' /* DWARF 1 */');
  945. Add(' .debug 0 : { *(.debug) }');
  946. Add(' .line 0 : { *(.line) }');
  947. Add(' /* GNU DWARF 1 extensions */');
  948. Add(' .debug_srcinfo 0 : { *(.debug_srcinfo) }');
  949. Add(' .debug_sfnames 0 : { *(.debug_sfnames) }');
  950. Add(' /* DWARF 1.1 and DWARF 2 */');
  951. Add(' .debug_aranges 0 : { *(.debug_aranges) }');
  952. Add(' .debug_pubnames 0 : { *(.debug_pubnames) }');
  953. Add(' /* DWARF 2 */');
  954. Add(' .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) }');
  955. Add(' .debug_abbrev 0 : { *(.debug_abbrev) }');
  956. Add(' /DISCARD/ : { *(.debug_line) }');
  957. Add(' .debug_frame 0 : { *(.debug_frame) }');
  958. Add(' .debug_str 0 : { *(.debug_str) }');
  959. Add(' /DISCARD/ : { *(.debug_loc) }');
  960. Add(' .debug_macinfo 0 : { *(.debug_macinfo) }');
  961. Add(' /* SGI/MIPS DWARF 2 extensions */');
  962. Add(' .debug_weaknames 0 : { *(.debug_weaknames) }');
  963. Add(' .debug_funcnames 0 : { *(.debug_funcnames) }');
  964. Add(' .debug_typenames 0 : { *(.debug_typenames) }');
  965. Add(' .debug_varnames 0 : { *(.debug_varnames) }');
  966. Add(' /* DWARF 3 */');
  967. Add(' .debug_pubtypes 0 : { *(.debug_pubtypes) }');
  968. Add(' .debug_ranges 0 : { *(.debug_ranges) }');
  969. Add(' .gnu.attributes 0 : { KEEP (*(.gnu.attributes)) }');
  970. Add(' .gptab.sdata : { *(.gptab.data) *(.gptab.sdata) }');
  971. Add(' .gptab.sbss : { *(.gptab.bss) *(.gptab.sbss) }');
  972. Add(' .mdebug.abi32 : { KEEP(*(.mdebug.abi32)) }');
  973. Add(' .mdebug.abiN32 : { KEEP(*(.mdebug.abiN32)) }');
  974. Add(' .mdebug.abi64 : { KEEP(*(.mdebug.abi64)) }');
  975. Add(' .mdebug.abiO64 : { KEEP(*(.mdebug.abiO64)) }');
  976. Add(' .mdebug.eabi32 : { KEEP(*(.mdebug.eabi32)) }');
  977. Add(' .mdebug.eabi64 : { KEEP(*(.mdebug.eabi64)) }');
  978. Add(' /DISCARD/ : { *(.rel.dyn) }');
  979. Add(' /DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) *(.gnu.lto_*) }');
  980. Add('}');
  981. Add('_end = .;');
  982. end;
  983. {$endif MIPSEL}
  984. { Write and Close response }
  985. linkres.writetodisk;
  986. linkres.free;
  987. WriteResponseFile:=True;
  988. end;
  989. function TlinkerEmbedded.MakeExecutable:boolean;
  990. var
  991. binstr,
  992. cmdstr : TCmdStr;
  993. success : boolean;
  994. StaticStr,
  995. GCSectionsStr,
  996. DynLinkStr,
  997. StripStr: string;
  998. begin
  999. { for future use }
  1000. StaticStr:='';
  1001. StripStr:='';
  1002. DynLinkStr:='';
  1003. GCSectionsStr:='--gc-sections';
  1004. //if not(cs_link_extern in current_settings.globalswitches) then
  1005. if not(cs_link_nolink in current_settings.globalswitches) then
  1006. Message1(exec_i_linking,current_module.exefilename);
  1007. { Write used files and libraries }
  1008. WriteResponseFile();
  1009. { Call linker }
  1010. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  1011. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  1012. if not(cs_link_on_target in current_settings.globalswitches) then
  1013. begin
  1014. Replace(cmdstr,'$EXE',(maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename,'.elf')))));
  1015. Replace(cmdstr,'$RES',(maybequoted(ScriptFixFileName(outputexedir+Info.ResName))));
  1016. Replace(cmdstr,'$STATIC',StaticStr);
  1017. Replace(cmdstr,'$STRIP',StripStr);
  1018. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  1019. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  1020. end
  1021. else
  1022. begin
  1023. Replace(cmdstr,'$EXE',maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename,'.elf'))));
  1024. Replace(cmdstr,'$RES',maybequoted(ScriptFixFileName(outputexedir+Info.ResName)));
  1025. Replace(cmdstr,'$STATIC',StaticStr);
  1026. Replace(cmdstr,'$STRIP',StripStr);
  1027. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  1028. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  1029. end;
  1030. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  1031. { Remove ReponseFile }
  1032. if success and not(cs_link_nolink in current_settings.globalswitches) then
  1033. DeleteFile(outputexedir+Info.ResName);
  1034. { Post process }
  1035. if success and not(cs_link_nolink in current_settings.globalswitches) then
  1036. success:=PostProcessExecutable(current_module.exefilename+'.elf',false);
  1037. if success and (target_info.system in [system_arm_embedded,system_avr_embedded,system_mipsel_embedded]) then
  1038. begin
  1039. success:=DoExec(FindUtil(utilsprefix+'objcopy'),'-O ihex '+
  1040. ChangeFileExt(current_module.exefilename,'.elf')+' '+
  1041. ChangeFileExt(current_module.exefilename,'.hex'),true,false);
  1042. if success then
  1043. success:=DoExec(FindUtil(utilsprefix+'objcopy'),'-O binary '+
  1044. ChangeFileExt(current_module.exefilename,'.elf')+' '+
  1045. ChangeFileExt(current_module.exefilename,'.bin'),true,false);
  1046. end;
  1047. MakeExecutable:=success; { otherwise a recursive call to link method }
  1048. end;
  1049. function TLinkerEmbedded.postprocessexecutable(const fn : string;isdll:boolean):boolean;
  1050. type
  1051. TElf32header=packed record
  1052. magic0123 : longint;
  1053. file_class : byte;
  1054. data_encoding : byte;
  1055. file_version : byte;
  1056. padding : array[$07..$0f] of byte;
  1057. e_type : word;
  1058. e_machine : word;
  1059. e_version : longint;
  1060. e_entry : longint; { entrypoint }
  1061. e_phoff : longint; { program header offset }
  1062. e_shoff : longint; { sections header offset }
  1063. e_flags : longint;
  1064. e_ehsize : word; { elf header size in bytes }
  1065. e_phentsize : word; { size of an entry in the program header array }
  1066. e_phnum : word; { 0..e_phnum-1 of entrys }
  1067. e_shentsize : word; { size of an entry in sections header array }
  1068. e_shnum : word; { 0..e_shnum-1 of entrys }
  1069. e_shstrndx : word; { index of string section header }
  1070. end;
  1071. TElf32sechdr=packed record
  1072. sh_name : longint;
  1073. sh_type : longint;
  1074. sh_flags : longint;
  1075. sh_addr : longint;
  1076. sh_offset : longint;
  1077. sh_size : longint;
  1078. sh_link : longint;
  1079. sh_info : longint;
  1080. sh_addralign : longint;
  1081. sh_entsize : longint;
  1082. end;
  1083. function MayBeSwapHeader(h : telf32header) : telf32header;
  1084. begin
  1085. result:=h;
  1086. if source_info.endian<>target_info.endian then
  1087. with h do
  1088. begin
  1089. result.e_type:=swapendian(e_type);
  1090. result.e_machine:=swapendian(e_machine);
  1091. result.e_version:=swapendian(e_version);
  1092. result.e_entry:=swapendian(e_entry);
  1093. result.e_phoff:=swapendian(e_phoff);
  1094. result.e_shoff:=swapendian(e_shoff);
  1095. result.e_flags:=swapendian(e_flags);
  1096. result.e_ehsize:=swapendian(e_ehsize);
  1097. result.e_phentsize:=swapendian(e_phentsize);
  1098. result.e_phnum:=swapendian(e_phnum);
  1099. result.e_shentsize:=swapendian(e_shentsize);
  1100. result.e_shnum:=swapendian(e_shnum);
  1101. result.e_shstrndx:=swapendian(e_shstrndx);
  1102. end;
  1103. end;
  1104. function MaybeSwapSecHeader(h : telf32sechdr) : telf32sechdr;
  1105. begin
  1106. result:=h;
  1107. if source_info.endian<>target_info.endian then
  1108. with h do
  1109. begin
  1110. result.sh_name:=swapendian(sh_name);
  1111. result.sh_type:=swapendian(sh_type);
  1112. result.sh_flags:=swapendian(sh_flags);
  1113. result.sh_addr:=swapendian(sh_addr);
  1114. result.sh_offset:=swapendian(sh_offset);
  1115. result.sh_size:=swapendian(sh_size);
  1116. result.sh_link:=swapendian(sh_link);
  1117. result.sh_info:=swapendian(sh_info);
  1118. result.sh_addralign:=swapendian(sh_addralign);
  1119. result.sh_entsize:=swapendian(sh_entsize);
  1120. end;
  1121. end;
  1122. var
  1123. f : file;
  1124. function ReadSectionName(pos : longint) : String;
  1125. var
  1126. oldpos : longint;
  1127. c : char;
  1128. begin
  1129. oldpos:=filepos(f);
  1130. seek(f,pos);
  1131. Result:='';
  1132. while true do
  1133. begin
  1134. blockread(f,c,1);
  1135. if c=#0 then
  1136. break;
  1137. Result:=Result+c;
  1138. end;
  1139. seek(f,oldpos);
  1140. end;
  1141. var
  1142. elfheader : TElf32header;
  1143. secheader : TElf32sechdr;
  1144. i : longint;
  1145. stringoffset : longint;
  1146. secname : string;
  1147. begin
  1148. postprocessexecutable:=false;
  1149. { open file }
  1150. assign(f,fn);
  1151. {$push}{$I-}
  1152. reset(f,1);
  1153. if ioresult<>0 then
  1154. Message1(execinfo_f_cant_open_executable,fn);
  1155. { read header }
  1156. blockread(f,elfheader,sizeof(tElf32header));
  1157. elfheader:=MayBeSwapHeader(elfheader);
  1158. seek(f,elfheader.e_shoff);
  1159. { read string section header }
  1160. seek(f,elfheader.e_shoff+sizeof(TElf32sechdr)*elfheader.e_shstrndx);
  1161. blockread(f,secheader,sizeof(secheader));
  1162. secheader:=MaybeSwapSecHeader(secheader);
  1163. stringoffset:=secheader.sh_offset;
  1164. seek(f,elfheader.e_shoff);
  1165. status.datasize:=0;
  1166. for i:=0 to elfheader.e_shnum-1 do
  1167. begin
  1168. blockread(f,secheader,sizeof(secheader));
  1169. secheader:=MaybeSwapSecHeader(secheader);
  1170. secname:=ReadSectionName(stringoffset+secheader.sh_name);
  1171. if secname='.text' then
  1172. begin
  1173. Message1(execinfo_x_codesize,tostr(secheader.sh_size));
  1174. status.codesize:=secheader.sh_size;
  1175. end
  1176. else if secname='.data' then
  1177. begin
  1178. Message1(execinfo_x_initdatasize,tostr(secheader.sh_size));
  1179. inc(status.datasize,secheader.sh_size);
  1180. end
  1181. else if secname='.bss' then
  1182. begin
  1183. Message1(execinfo_x_uninitdatasize,tostr(secheader.sh_size));
  1184. inc(status.datasize,secheader.sh_size);
  1185. end;
  1186. end;
  1187. close(f);
  1188. {$pop}
  1189. if ioresult<>0 then
  1190. ;
  1191. postprocessexecutable:=true;
  1192. end;
  1193. {*****************************************************************************
  1194. Initialize
  1195. *****************************************************************************}
  1196. initialization
  1197. {$ifdef arm}
  1198. RegisterLinker(ld_embedded,TLinkerEmbedded);
  1199. RegisterTarget(system_arm_embedded_info);
  1200. {$endif arm}
  1201. {$ifdef avr}
  1202. RegisterLinker(ld_embedded,TLinkerEmbedded);
  1203. RegisterTarget(system_avr_embedded_info);
  1204. {$endif avr}
  1205. {$ifdef i386}
  1206. RegisterLinker(ld_embedded,TLinkerEmbedded);
  1207. RegisterTarget(system_i386_embedded_info);
  1208. {$endif i386}
  1209. {$ifdef mipsel}
  1210. RegisterLinker(ld_embedded,TLinkerEmbedded);
  1211. RegisterTarget(system_mipsel_embedded_info);
  1212. {$endif mipsel}
  1213. end.