t_embed.pas 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258
  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_stm32f100x4,
  310. ct_stm32f100x6,
  311. ct_stm32f100x8,
  312. ct_stm32f100xB,
  313. ct_stm32f100xC,
  314. ct_stm32f100xD,
  315. ct_stm32f100xE,
  316. ct_stm32f101x4,
  317. ct_stm32f101x6,
  318. ct_stm32f101x8,
  319. ct_stm32f101xB,
  320. ct_stm32f101xC,
  321. ct_stm32f101xD,
  322. ct_stm32f101xE,
  323. ct_stm32f101xF,
  324. ct_stm32f101xG,
  325. ct_stm32f102x4,
  326. ct_stm32f102x6,
  327. ct_stm32f102x8,
  328. ct_stm32f102xB,
  329. ct_stm32f103x4,
  330. ct_stm32f103x6,
  331. ct_stm32f103x8,
  332. ct_stm32f103xB,
  333. ct_stm32f103xC,
  334. ct_stm32f103xD,
  335. ct_stm32f103xE,
  336. ct_stm32f103xF,
  337. ct_stm32f103xG,
  338. ct_stm32f107x8,
  339. ct_stm32f107xB,
  340. ct_stm32f107xC,
  341. { TI - 64 K Flash, 16 K SRAM Devices }
  342. ct_lm3s1110,
  343. ct_lm3s1133,
  344. ct_lm3s1138,
  345. ct_lm3s1150,
  346. ct_lm3s1162,
  347. ct_lm3s1165,
  348. ct_lm3s1166,
  349. ct_lm3s2110,
  350. ct_lm3s2139,
  351. ct_lm3s6100,
  352. ct_lm3s6110,
  353. { TI 128 K Flash, 32 K SRAM devices - Fury Class }
  354. ct_lm3s1601,
  355. ct_lm3s1608,
  356. ct_lm3s1620,
  357. ct_lm3s1635,
  358. ct_lm3s1636,
  359. ct_lm3s1637,
  360. ct_lm3s1651,
  361. ct_lm3s2601,
  362. ct_lm3s2608,
  363. ct_lm3s2620,
  364. ct_lm3s2637,
  365. ct_lm3s2651,
  366. ct_lm3s6610,
  367. ct_lm3s6611,
  368. ct_lm3s6618,
  369. ct_lm3s6633,
  370. ct_lm3s6637,
  371. ct_lm3s8630,
  372. { TI 256 K Flase, 32 K SRAM devices - Fury Class }
  373. ct_lm3s1911,
  374. ct_lm3s1918,
  375. ct_lm3s1937,
  376. ct_lm3s1958,
  377. ct_lm3s1960,
  378. ct_lm3s1968,
  379. ct_lm3s1969,
  380. ct_lm3s2911,
  381. ct_lm3s2918,
  382. ct_lm3s2919,
  383. ct_lm3s2939,
  384. ct_lm3s2948,
  385. ct_lm3s2950,
  386. ct_lm3s2965,
  387. ct_lm3s6911,
  388. ct_lm3s6918,
  389. ct_lm3s6938,
  390. ct_lm3s6950,
  391. ct_lm3s6952,
  392. ct_lm3s6965,
  393. ct_lm3s8930,
  394. ct_lm3s8933,
  395. ct_lm3s8938,
  396. ct_lm3s8962,
  397. ct_lm3s8970,
  398. ct_lm3s8971,
  399. { TI - Tempest Tempest - 256 K Flash, 64 K SRAM }
  400. ct_lm3s5951,
  401. ct_lm3s5956,
  402. ct_lm3s1b21,
  403. ct_lm3s2b93,
  404. ct_lm3s5b91,
  405. ct_lm3s9b81,
  406. ct_lm3s9b90,
  407. ct_lm3s9b92,
  408. ct_lm3s9b95,
  409. ct_lm3s9b96,
  410. ct_lm3s5d51,
  411. { TI - Stellaris something }
  412. ct_lm4f120h5,
  413. { Infineon }
  414. ct_xmc4500x1024,
  415. ct_xmc4500x768,
  416. ct_xmc4502x768,
  417. ct_xmc4504x512,
  418. ct_sc32442b,
  419. ct_thumb2bare:
  420. begin
  421. with embedded_controllers[current_settings.controllertype] do
  422. with linkres do
  423. begin
  424. Add('ENTRY(_START)');
  425. Add('MEMORY');
  426. Add('{');
  427. if flashsize<>0 then
  428. begin
  429. LinkStr := ' flash : ORIGIN = 0x' + IntToHex(flashbase,8)
  430. + ', LENGTH = 0x' + IntToHex(flashsize,8);
  431. Add(LinkStr);
  432. end;
  433. LinkStr := ' ram : ORIGIN = 0x' + IntToHex(srambase,8)
  434. + ', LENGTH = 0x' + IntToHex(sramsize,8);
  435. Add(LinkStr);
  436. Add('}');
  437. Add('_stack_top = 0x' + IntToHex(sramsize+srambase,8) + ';');
  438. end;
  439. end
  440. else
  441. if not (cs_link_nolink in current_settings.globalswitches) then
  442. internalerror(200902011);
  443. end;
  444. with linkres do
  445. begin
  446. Add('SECTIONS');
  447. Add('{');
  448. Add(' .text :');
  449. Add(' {');
  450. Add(' _text_start = .;');
  451. Add(' KEEP(*(.init, .init.*))');
  452. Add(' *(.text, .text.*)');
  453. Add(' *(.strings)');
  454. Add(' *(.rodata, .rodata.*)');
  455. Add(' *(.comment)');
  456. Add(' _etext = .;');
  457. if embedded_controllers[current_settings.controllertype].flashsize<>0 then
  458. begin
  459. Add(' } >flash');
  460. end
  461. else
  462. begin
  463. Add(' } >ram');
  464. end;
  465. Add(' .note.gnu.build-id : { *(.note.gnu.build-id) } >flash ');
  466. Add(' .data :');
  467. Add(' {');
  468. Add(' _data = .;');
  469. Add(' *(.data, .data.*)');
  470. Add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  471. Add(' _edata = .;');
  472. if embedded_controllers[current_settings.controllertype].flashsize<>0 then
  473. begin
  474. Add(' } >ram AT >flash');
  475. end
  476. else
  477. begin
  478. Add(' } >ram');
  479. end;
  480. Add(' .bss :');
  481. Add(' {');
  482. Add(' _bss_start = .;');
  483. Add(' *(.bss, .bss.*)');
  484. Add(' *(COMMON)');
  485. Add(' } >ram');
  486. Add('. = ALIGN(4);');
  487. Add('_bss_end = . ;');
  488. Add('}');
  489. Add('_end = .;');
  490. end;
  491. {$endif ARM}
  492. {$ifdef i386}
  493. with linkres do
  494. begin
  495. Add('ENTRY(_START)');
  496. Add('SECTIONS');
  497. Add('{');
  498. Add(' . = 0x100000;');
  499. Add(' .text ALIGN (0x1000) :');
  500. Add(' {');
  501. Add(' KEEP(*(.init, .init.*))');
  502. Add(' *(.text, .text.*)');
  503. Add(' *(.strings)');
  504. Add(' *(.rodata, .rodata.*)');
  505. Add(' *(.comment)');
  506. Add(' _etext = .;');
  507. Add(' }');
  508. Add(' .data ALIGN (0x1000) :');
  509. Add(' {');
  510. Add(' _data = .;');
  511. Add(' *(.data, .data.*)');
  512. Add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  513. Add(' _edata = .;');
  514. Add(' }');
  515. Add(' . = ALIGN(4);');
  516. Add(' .bss :');
  517. Add(' {');
  518. Add(' _bss_start = .;');
  519. Add(' *(.bss, .bss.*)');
  520. Add(' *(COMMON)');
  521. Add(' }');
  522. Add('_bss_end = . ;');
  523. Add('}');
  524. Add('_end = .;');
  525. end;
  526. {$endif i386}
  527. {$ifdef AVR}
  528. with linkres do
  529. begin
  530. { linker script from ld 2.19 }
  531. Add('ENTRY(_START)');
  532. Add('OUTPUT_FORMAT("elf32-avr","elf32-avr","elf32-avr")');
  533. Add('OUTPUT_ARCH(avr:2)');
  534. Add('MEMORY');
  535. Add('{');
  536. Add(' text (rx) : ORIGIN = 0, LENGTH = 8K');
  537. Add(' data (rw!x) : ORIGIN = 0x800060, LENGTH = 0xffa0');
  538. Add(' eeprom (rw!x) : ORIGIN = 0x810000, LENGTH = 64K');
  539. Add(' fuse (rw!x) : ORIGIN = 0x820000, LENGTH = 1K');
  540. Add(' lock (rw!x) : ORIGIN = 0x830000, LENGTH = 1K');
  541. Add(' signature (rw!x) : ORIGIN = 0x840000, LENGTH = 1K');
  542. Add('}');
  543. Add('SECTIONS');
  544. Add('{');
  545. Add(' /* Read-only sections, merged into text segment: */');
  546. Add(' .hash : { *(.hash) }');
  547. Add(' .dynsym : { *(.dynsym) }');
  548. Add(' .dynstr : { *(.dynstr) }');
  549. Add(' .gnu.version : { *(.gnu.version) }');
  550. Add(' .gnu.version_d : { *(.gnu.version_d) }');
  551. Add(' .gnu.version_r : { *(.gnu.version_r) }');
  552. Add(' .rel.init : { *(.rel.init) }');
  553. Add(' .rela.init : { *(.rela.init) }');
  554. Add(' .rel.text :');
  555. Add(' {');
  556. Add(' *(.rel.text)');
  557. Add(' *(.rel.text.*)');
  558. Add(' *(.rel.gnu.linkonce.t*)');
  559. Add(' }');
  560. Add(' .rela.text :');
  561. Add(' {');
  562. Add(' *(.rela.text)');
  563. Add(' *(.rela.text.*)');
  564. Add(' *(.rela.gnu.linkonce.t*)');
  565. Add(' }');
  566. Add(' .rel.fini : { *(.rel.fini) }');
  567. Add(' .rela.fini : { *(.rela.fini) }');
  568. Add(' .rel.rodata :');
  569. Add(' {');
  570. Add(' *(.rel.rodata)');
  571. Add(' *(.rel.rodata.*)');
  572. Add(' *(.rel.gnu.linkonce.r*)');
  573. Add(' }');
  574. Add(' .rela.rodata :');
  575. Add(' {');
  576. Add(' *(.rela.rodata)');
  577. Add(' *(.rela.rodata.*)');
  578. Add(' *(.rela.gnu.linkonce.r*)');
  579. Add(' }');
  580. Add(' .rel.data :');
  581. Add(' {');
  582. Add(' *(.rel.data)');
  583. Add(' *(.rel.data.*)');
  584. Add(' *(.rel.gnu.linkonce.d*)');
  585. Add(' }');
  586. Add(' .rela.data :');
  587. Add(' {');
  588. Add(' *(.rela.data)');
  589. Add(' *(.rela.data.*)');
  590. Add(' *(.rela.gnu.linkonce.d*)');
  591. Add(' }');
  592. Add(' .rel.ctors : { *(.rel.ctors) }');
  593. Add(' .rela.ctors : { *(.rela.ctors) }');
  594. Add(' .rel.dtors : { *(.rel.dtors) }');
  595. Add(' .rela.dtors : { *(.rela.dtors) }');
  596. Add(' .rel.got : { *(.rel.got) }');
  597. Add(' .rela.got : { *(.rela.got) }');
  598. Add(' .rel.bss : { *(.rel.bss) }');
  599. Add(' .rela.bss : { *(.rela.bss) }');
  600. Add(' .rel.plt : { *(.rel.plt) }');
  601. Add(' .rela.plt : { *(.rela.plt) }');
  602. Add(' /* Internal text space or external memory. */');
  603. Add(' .text :');
  604. Add(' {');
  605. Add(' KEEP(*(.init, .init.*))');
  606. Add(' /* For data that needs to reside in the lower 64k of progmem. */');
  607. Add(' *(.progmem.gcc*)');
  608. Add(' *(.progmem*)');
  609. Add(' . = ALIGN(2);');
  610. Add(' __trampolines_start = . ;');
  611. Add(' /* The jump trampolines for the 16-bit limited relocs will reside here. */');
  612. Add(' *(.trampolines)');
  613. Add(' *(.trampolines*)');
  614. Add(' __trampolines_end = . ;');
  615. Add(' /* For future tablejump instruction arrays for 3 byte pc devices.');
  616. Add(' We don''t relax jump/call instructions within these sections. */');
  617. Add(' *(.jumptables)');
  618. Add(' *(.jumptables*)');
  619. Add(' /* For code that needs to reside in the lower 128k progmem. */');
  620. Add(' *(.lowtext)');
  621. Add(' *(.lowtext*)');
  622. Add(' __ctors_start = . ;');
  623. Add(' *(.ctors)');
  624. Add(' __ctors_end = . ;');
  625. Add(' __dtors_start = . ;');
  626. Add(' *(.dtors)');
  627. Add(' __dtors_end = . ;');
  628. Add(' KEEP(SORT(*)(.ctors))');
  629. Add(' KEEP(SORT(*)(.dtors))');
  630. Add(' /* From this point on, we don''t bother about wether the insns are');
  631. Add(' below or above the 16 bits boundary. */');
  632. Add(' *(.init0) /* Start here after reset. */');
  633. Add(' KEEP (*(.init0))');
  634. Add(' *(.init1)');
  635. Add(' KEEP (*(.init1))');
  636. Add(' *(.init2) /* Clear __zero_reg__, set up stack pointer. */');
  637. Add(' KEEP (*(.init2))');
  638. Add(' *(.init3)');
  639. Add(' KEEP (*(.init3))');
  640. Add(' *(.init4) /* Initialize data and BSS. */');
  641. Add(' KEEP (*(.init4))');
  642. Add(' *(.init5)');
  643. Add(' KEEP (*(.init5))');
  644. Add(' *(.init6) /* C++ constructors. */');
  645. Add(' KEEP (*(.init6))');
  646. Add(' *(.init7)');
  647. Add(' KEEP (*(.init7))');
  648. Add(' *(.init8)');
  649. Add(' KEEP (*(.init8))');
  650. Add(' *(.init9) /* Call main(). */');
  651. Add(' KEEP (*(.init9))');
  652. Add(' *(.text)');
  653. Add(' . = ALIGN(2);');
  654. Add(' *(.text.*)');
  655. Add(' . = ALIGN(2);');
  656. Add(' *(.fini9) /* _exit() starts here. */');
  657. Add(' KEEP (*(.fini9))');
  658. Add(' *(.fini8)');
  659. Add(' KEEP (*(.fini8))');
  660. Add(' *(.fini7)');
  661. Add(' KEEP (*(.fini7))');
  662. Add(' *(.fini6) /* C++ destructors. */');
  663. Add(' KEEP (*(.fini6))');
  664. Add(' *(.fini5)');
  665. Add(' KEEP (*(.fini5))');
  666. Add(' *(.fini4)');
  667. Add(' KEEP (*(.fini4))');
  668. Add(' *(.fini3)');
  669. Add(' KEEP (*(.fini3))');
  670. Add(' *(.fini2)');
  671. Add(' KEEP (*(.fini2))');
  672. Add(' *(.fini1)');
  673. Add(' KEEP (*(.fini1))');
  674. Add(' *(.fini0) /* Infinite loop after program termination. */');
  675. Add(' KEEP (*(.fini0))');
  676. Add(' _etext = . ;');
  677. Add(' } > text');
  678. Add(' .data : AT (ADDR (.text) + SIZEOF (.text))');
  679. Add(' {');
  680. Add(' PROVIDE (__data_start = .) ;');
  681. Add(' *(.data)');
  682. Add(' *(.data*)');
  683. Add(' *(.rodata) /* We need to include .rodata here if gcc is used */');
  684. Add(' *(.rodata*) /* with -fdata-sections. */');
  685. Add(' *(.gnu.linkonce.d*)');
  686. Add(' . = ALIGN(2);');
  687. Add(' _edata = . ;');
  688. Add(' PROVIDE (__data_end = .) ;');
  689. Add(' } > data');
  690. Add(' .bss : AT (ADDR (.bss))');
  691. Add(' {');
  692. Add(' PROVIDE (__bss_start = .) ;');
  693. Add(' *(.bss)');
  694. Add(' *(.bss*)');
  695. Add(' *(COMMON)');
  696. Add(' PROVIDE (__bss_end = .) ;');
  697. Add(' } > data');
  698. Add(' __data_load_start = LOADADDR(.data);');
  699. Add(' __data_load_end = __data_load_start + SIZEOF(.data);');
  700. Add(' /* Global data not cleared after reset. */');
  701. Add(' .noinit :');
  702. Add(' {');
  703. Add(' PROVIDE (__noinit_start = .) ;');
  704. Add(' *(.noinit*)');
  705. Add(' PROVIDE (__noinit_end = .) ;');
  706. Add(' _end = . ;');
  707. Add(' PROVIDE (__heap_start = .) ;');
  708. Add(' } > data');
  709. Add(' .eeprom :');
  710. Add(' {');
  711. Add(' *(.eeprom*)');
  712. Add(' __eeprom_end = . ;');
  713. Add(' } > eeprom');
  714. Add(' .fuse :');
  715. Add(' {');
  716. Add(' KEEP(*(.fuse))');
  717. Add(' KEEP(*(.lfuse))');
  718. Add(' KEEP(*(.hfuse))');
  719. Add(' KEEP(*(.efuse))');
  720. Add(' } > fuse');
  721. Add(' .lock :');
  722. Add(' {');
  723. Add(' KEEP(*(.lock*))');
  724. Add(' } > lock');
  725. Add(' .signature :');
  726. Add(' {');
  727. Add(' KEEP(*(.signature*))');
  728. Add(' } > signature');
  729. Add(' /* Stabs debugging sections. */');
  730. Add(' .stab 0 : { *(.stab) }');
  731. Add(' .stabstr 0 : { *(.stabstr) }');
  732. Add(' .stab.excl 0 : { *(.stab.excl) }');
  733. Add(' .stab.exclstr 0 : { *(.stab.exclstr) }');
  734. Add(' .stab.index 0 : { *(.stab.index) }');
  735. Add(' .stab.indexstr 0 : { *(.stab.indexstr) }');
  736. Add(' .comment 0 : { *(.comment) }');
  737. Add(' /* DWARF debug sections.');
  738. Add(' Symbols in the DWARF debugging sections are relative to the beginning');
  739. Add(' of the section so we begin them at 0. */');
  740. Add(' /* DWARF 1 */');
  741. Add(' .debug 0 : { *(.debug) }');
  742. Add(' .line 0 : { *(.line) }');
  743. Add(' /* GNU DWARF 1 extensions */');
  744. Add(' .debug_srcinfo 0 : { *(.debug_srcinfo) }');
  745. Add(' .debug_sfnames 0 : { *(.debug_sfnames) }');
  746. Add(' /* DWARF 1.1 and DWARF 2 */');
  747. Add(' .debug_aranges 0 : { *(.debug_aranges) }');
  748. Add(' .debug_pubnames 0 : { *(.debug_pubnames) }');
  749. Add(' /* DWARF 2 */');
  750. Add(' .debug_info 0 : { *(.debug_info) *(.gnu.linkonce.wi.*) }');
  751. Add(' .debug_abbrev 0 : { *(.debug_abbrev) }');
  752. Add(' .debug_line 0 : { *(.debug_line) }');
  753. Add(' .debug_frame 0 : { *(.debug_frame) }');
  754. Add(' .debug_str 0 : { *(.debug_str) }');
  755. Add(' .debug_loc 0 : { *(.debug_loc) }');
  756. Add(' .debug_macinfo 0 : { *(.debug_macinfo) }');
  757. Add('}');
  758. { last address of ram on an atmega128 }
  759. Add('_stack_top = 0x0fff;');
  760. end;
  761. {$endif AVR}
  762. {$ifdef MIPSEL}
  763. case current_settings.controllertype of
  764. ct_none:
  765. begin
  766. end;
  767. ct_pic32mx110f016b,
  768. ct_pic32mx110f016c,
  769. ct_pic32mx110f016d,
  770. ct_pic32mx120f032b,
  771. ct_pic32mx120f032c,
  772. ct_pic32mx120f032d,
  773. ct_pic32mx130f064b,
  774. ct_pic32mx130f064c,
  775. ct_pic32mx130f064d,
  776. ct_pic32mx150f128b,
  777. ct_pic32mx150f128c,
  778. ct_pic32mx150f128d,
  779. ct_pic32mx210f016b,
  780. ct_pic32mx210f016c,
  781. ct_pic32mx210f016d,
  782. ct_pic32mx220f032b,
  783. ct_pic32mx220f032c,
  784. ct_pic32mx220f032d,
  785. ct_pic32mx230f064b,
  786. ct_pic32mx230f064c,
  787. ct_pic32mx230f064d,
  788. ct_pic32mx250f128b,
  789. ct_pic32mx250f128c,
  790. ct_pic32mx250f128d,
  791. ct_pic32mx775f256h,
  792. ct_pic32mx775f256l,
  793. ct_pic32mx775f512h,
  794. ct_pic32mx775f512l,
  795. ct_pic32mx795f512h,
  796. ct_pic32mx795f512l:
  797. begin
  798. with embedded_controllers[current_settings.controllertype] do
  799. with linkres do
  800. begin
  801. Add('OUTPUT_FORMAT("elf32-tradlittlemips")');
  802. Add('OUTPUT_ARCH(pic32mx)');
  803. Add('ENTRY(_reset)');
  804. Add('PROVIDE(_vector_spacing = 0x00000001);');
  805. Add('_ebase_address = 0x'+IntToHex(flashbase,8)+';');
  806. Add('_RESET_ADDR = 0xBFC00000;');
  807. Add('_BEV_EXCPT_ADDR = 0xBFC00380;');
  808. Add('_DBG_EXCPT_ADDR = 0xBFC00480;');
  809. Add('_GEN_EXCPT_ADDR = _ebase_address + 0x180;');
  810. Add('MEMORY');
  811. Add('{');
  812. if flashsize<>0 then
  813. begin
  814. Add(' kseg0_program_mem : ORIGIN = 0x'+IntToHex(flashbase,8)+', LENGTH = 0x'+IntToHex(flashsize,8));
  815. //TODO This should better be placed into the controllertype records
  816. Add(' kseg1_boot_mem : ORIGIN = 0xBFC00000, LENGTH = 0xbef');
  817. Add(' config3 : ORIGIN = 0xBFC00BF0, LENGTH = 0x4');
  818. Add(' config2 : ORIGIN = 0xBFC00BF4, LENGTH = 0x4');
  819. Add(' config1 : ORIGIN = 0xBFC00BF8, LENGTH = 0x4');
  820. Add(' config0 : ORIGIN = 0xBFC00BFC, LENGTH = 0x4');
  821. end;
  822. Add(' ram : ORIGIN = 0x' + IntToHex(srambase,8)
  823. + ', LENGTH = 0x' + IntToHex(sramsize,8));
  824. Add('}');
  825. Add('_stack_top = 0x' + IntToHex(sramsize+srambase,8) + ';');
  826. end;
  827. end
  828. else
  829. if not (cs_link_nolink in current_settings.globalswitches) then
  830. internalerror(200902011);
  831. end;
  832. with linkres do
  833. begin
  834. Add('SECTIONS');
  835. Add('{');
  836. Add(' .reset _RESET_ADDR :');
  837. Add(' {');
  838. Add(' KEEP(*(.reset .reset.*))');
  839. Add(' KEEP(*(.startup .startup.*))');
  840. Add(' } > kseg1_boot_mem');
  841. Add(' .bev_excpt _BEV_EXCPT_ADDR :');
  842. Add(' {');
  843. Add(' KEEP(*(.bev_handler))');
  844. Add(' } > kseg1_boot_mem');
  845. Add(' .text :');
  846. Add(' {');
  847. Add(' _text_start = .;');
  848. Add(' . = _text_start + 0x180;');
  849. Add(' KEEP(*(.gen_handler))');
  850. Add(' . = _text_start + 0x200;');
  851. Add(' KEEP(*(.init .init.*))');
  852. Add(' *(.text .text.*)');
  853. Add(' *(.strings)');
  854. Add(' *(.rodata .rodata.*)');
  855. Add(' *(.comment)');
  856. Add(' _etext = .;');
  857. if embedded_controllers[current_settings.controllertype].flashsize<>0 then
  858. begin
  859. Add(' } >kseg0_program_mem');
  860. end
  861. else
  862. begin
  863. Add(' } >ram');
  864. end;
  865. Add(' .note.gnu.build-id : { *(.note.gnu.build-id) }');
  866. Add(' .data :');
  867. Add(' {');
  868. Add(' _data = .;');
  869. Add(' *(.data .data.*)');
  870. Add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  871. Add(' . = .;');
  872. Add(' _gp = ALIGN(16) + 0x7ff0;');
  873. Add(' _edata = .;');
  874. if embedded_controllers[current_settings.controllertype].flashsize<>0 then
  875. begin
  876. Add(' } >ram AT >kseg0_program_mem');
  877. end
  878. else
  879. begin
  880. Add(' } >ram');
  881. end;
  882. Add(' .config_BFC00BF0 : {');
  883. Add(' KEEP(*(.config_BFC00BF0))');
  884. Add(' } > config3');
  885. Add(' .config_BFC00BF4 : {');
  886. Add(' KEEP(*(.config_BFC00BF4))');
  887. Add(' } > config2');
  888. Add(' .config_BFC00BF8 : {');
  889. Add(' KEEP(*(.config_BFC00BF8))');
  890. Add(' } > config1');
  891. Add(' .config_BFC00BFC : {');
  892. Add(' KEEP(*(.config_BFC00BFC))');
  893. Add(' } > config0');
  894. Add(' .bss :');
  895. Add(' {');
  896. Add(' _bss_start = .;');
  897. Add(' *(.bss, .bss.*)');
  898. Add(' *(COMMON)');
  899. Add(' } >ram');
  900. Add('. = ALIGN(4);');
  901. Add('_bss_end = . ;');
  902. Add(' .comment 0 : { *(.comment) }');
  903. Add(' /* DWARF debug sections.');
  904. Add(' Symbols in the DWARF debugging sections are relative to the beginning');
  905. Add(' of the section so we begin them at 0. */');
  906. Add(' /* DWARF 1 */');
  907. Add(' .debug 0 : { *(.debug) }');
  908. Add(' .line 0 : { *(.line) }');
  909. Add(' /* GNU DWARF 1 extensions */');
  910. Add(' .debug_srcinfo 0 : { *(.debug_srcinfo) }');
  911. Add(' .debug_sfnames 0 : { *(.debug_sfnames) }');
  912. Add(' /* DWARF 1.1 and DWARF 2 */');
  913. Add(' .debug_aranges 0 : { *(.debug_aranges) }');
  914. Add(' .debug_pubnames 0 : { *(.debug_pubnames) }');
  915. Add(' /* DWARF 2 */');
  916. Add(' .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) }');
  917. Add(' .debug_abbrev 0 : { *(.debug_abbrev) }');
  918. Add(' /DISCARD/ : { *(.debug_line) }');
  919. Add(' .debug_frame 0 : { *(.debug_frame) }');
  920. Add(' .debug_str 0 : { *(.debug_str) }');
  921. Add(' /DISCARD/ : { *(.debug_loc) }');
  922. Add(' .debug_macinfo 0 : { *(.debug_macinfo) }');
  923. Add(' /* SGI/MIPS DWARF 2 extensions */');
  924. Add(' .debug_weaknames 0 : { *(.debug_weaknames) }');
  925. Add(' .debug_funcnames 0 : { *(.debug_funcnames) }');
  926. Add(' .debug_typenames 0 : { *(.debug_typenames) }');
  927. Add(' .debug_varnames 0 : { *(.debug_varnames) }');
  928. Add(' /* DWARF 3 */');
  929. Add(' .debug_pubtypes 0 : { *(.debug_pubtypes) }');
  930. Add(' .debug_ranges 0 : { *(.debug_ranges) }');
  931. Add(' .gnu.attributes 0 : { KEEP (*(.gnu.attributes)) }');
  932. Add(' .gptab.sdata : { *(.gptab.data) *(.gptab.sdata) }');
  933. Add(' .gptab.sbss : { *(.gptab.bss) *(.gptab.sbss) }');
  934. Add(' .mdebug.abi32 : { KEEP(*(.mdebug.abi32)) }');
  935. Add(' .mdebug.abiN32 : { KEEP(*(.mdebug.abiN32)) }');
  936. Add(' .mdebug.abi64 : { KEEP(*(.mdebug.abi64)) }');
  937. Add(' .mdebug.abiO64 : { KEEP(*(.mdebug.abiO64)) }');
  938. Add(' .mdebug.eabi32 : { KEEP(*(.mdebug.eabi32)) }');
  939. Add(' .mdebug.eabi64 : { KEEP(*(.mdebug.eabi64)) }');
  940. Add(' /DISCARD/ : { *(.rel.dyn) }');
  941. Add(' /DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) *(.gnu.lto_*) }');
  942. Add('}');
  943. Add('_end = .;');
  944. end;
  945. {$endif MIPSEL}
  946. { Write and Close response }
  947. linkres.writetodisk;
  948. linkres.free;
  949. WriteResponseFile:=True;
  950. end;
  951. function TlinkerEmbedded.MakeExecutable:boolean;
  952. var
  953. binstr,
  954. cmdstr : TCmdStr;
  955. success : boolean;
  956. StaticStr,
  957. GCSectionsStr,
  958. DynLinkStr,
  959. StripStr: string;
  960. begin
  961. { for future use }
  962. StaticStr:='';
  963. StripStr:='';
  964. DynLinkStr:='';
  965. GCSectionsStr:='--gc-sections';
  966. //if not(cs_link_extern in current_settings.globalswitches) then
  967. if not(cs_link_nolink in current_settings.globalswitches) then
  968. Message1(exec_i_linking,current_module.exefilename);
  969. { Write used files and libraries }
  970. WriteResponseFile();
  971. { Call linker }
  972. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  973. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  974. if not(cs_link_on_target in current_settings.globalswitches) then
  975. begin
  976. Replace(cmdstr,'$EXE',(maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename,'.elf')))));
  977. Replace(cmdstr,'$RES',(maybequoted(ScriptFixFileName(outputexedir+Info.ResName))));
  978. Replace(cmdstr,'$STATIC',StaticStr);
  979. Replace(cmdstr,'$STRIP',StripStr);
  980. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  981. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  982. end
  983. else
  984. begin
  985. Replace(cmdstr,'$EXE',maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename,'.elf'))));
  986. Replace(cmdstr,'$RES',maybequoted(ScriptFixFileName(outputexedir+Info.ResName)));
  987. Replace(cmdstr,'$STATIC',StaticStr);
  988. Replace(cmdstr,'$STRIP',StripStr);
  989. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  990. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  991. end;
  992. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  993. { Remove ReponseFile }
  994. if success and not(cs_link_nolink in current_settings.globalswitches) then
  995. DeleteFile(outputexedir+Info.ResName);
  996. { Post process }
  997. if success and not(cs_link_nolink in current_settings.globalswitches) then
  998. success:=PostProcessExecutable(current_module.exefilename+'.elf',false);
  999. if success and (target_info.system in [system_arm_embedded,system_avr_embedded,system_mipsel_embedded]) then
  1000. begin
  1001. success:=DoExec(FindUtil(utilsprefix+'objcopy'),'-O ihex '+
  1002. ChangeFileExt(current_module.exefilename,'.elf')+' '+
  1003. ChangeFileExt(current_module.exefilename,'.hex'),true,false);
  1004. if success then
  1005. success:=DoExec(FindUtil(utilsprefix+'objcopy'),'-O binary '+
  1006. ChangeFileExt(current_module.exefilename,'.elf')+' '+
  1007. ChangeFileExt(current_module.exefilename,'.bin'),true,false);
  1008. end;
  1009. MakeExecutable:=success; { otherwise a recursive call to link method }
  1010. end;
  1011. function TLinkerEmbedded.postprocessexecutable(const fn : string;isdll:boolean):boolean;
  1012. type
  1013. TElf32header=packed record
  1014. magic0123 : longint;
  1015. file_class : byte;
  1016. data_encoding : byte;
  1017. file_version : byte;
  1018. padding : array[$07..$0f] of byte;
  1019. e_type : word;
  1020. e_machine : word;
  1021. e_version : longint;
  1022. e_entry : longint; { entrypoint }
  1023. e_phoff : longint; { program header offset }
  1024. e_shoff : longint; { sections header offset }
  1025. e_flags : longint;
  1026. e_ehsize : word; { elf header size in bytes }
  1027. e_phentsize : word; { size of an entry in the program header array }
  1028. e_phnum : word; { 0..e_phnum-1 of entrys }
  1029. e_shentsize : word; { size of an entry in sections header array }
  1030. e_shnum : word; { 0..e_shnum-1 of entrys }
  1031. e_shstrndx : word; { index of string section header }
  1032. end;
  1033. TElf32sechdr=packed record
  1034. sh_name : longint;
  1035. sh_type : longint;
  1036. sh_flags : longint;
  1037. sh_addr : longint;
  1038. sh_offset : longint;
  1039. sh_size : longint;
  1040. sh_link : longint;
  1041. sh_info : longint;
  1042. sh_addralign : longint;
  1043. sh_entsize : longint;
  1044. end;
  1045. function MayBeSwapHeader(h : telf32header) : telf32header;
  1046. begin
  1047. result:=h;
  1048. if source_info.endian<>target_info.endian then
  1049. with h do
  1050. begin
  1051. result.e_type:=swapendian(e_type);
  1052. result.e_machine:=swapendian(e_machine);
  1053. result.e_version:=swapendian(e_version);
  1054. result.e_entry:=swapendian(e_entry);
  1055. result.e_phoff:=swapendian(e_phoff);
  1056. result.e_shoff:=swapendian(e_shoff);
  1057. result.e_flags:=swapendian(e_flags);
  1058. result.e_ehsize:=swapendian(e_ehsize);
  1059. result.e_phentsize:=swapendian(e_phentsize);
  1060. result.e_phnum:=swapendian(e_phnum);
  1061. result.e_shentsize:=swapendian(e_shentsize);
  1062. result.e_shnum:=swapendian(e_shnum);
  1063. result.e_shstrndx:=swapendian(e_shstrndx);
  1064. end;
  1065. end;
  1066. function MaybeSwapSecHeader(h : telf32sechdr) : telf32sechdr;
  1067. begin
  1068. result:=h;
  1069. if source_info.endian<>target_info.endian then
  1070. with h do
  1071. begin
  1072. result.sh_name:=swapendian(sh_name);
  1073. result.sh_type:=swapendian(sh_type);
  1074. result.sh_flags:=swapendian(sh_flags);
  1075. result.sh_addr:=swapendian(sh_addr);
  1076. result.sh_offset:=swapendian(sh_offset);
  1077. result.sh_size:=swapendian(sh_size);
  1078. result.sh_link:=swapendian(sh_link);
  1079. result.sh_info:=swapendian(sh_info);
  1080. result.sh_addralign:=swapendian(sh_addralign);
  1081. result.sh_entsize:=swapendian(sh_entsize);
  1082. end;
  1083. end;
  1084. var
  1085. f : file;
  1086. function ReadSectionName(pos : longint) : String;
  1087. var
  1088. oldpos : longint;
  1089. c : char;
  1090. begin
  1091. oldpos:=filepos(f);
  1092. seek(f,pos);
  1093. Result:='';
  1094. while true do
  1095. begin
  1096. blockread(f,c,1);
  1097. if c=#0 then
  1098. break;
  1099. Result:=Result+c;
  1100. end;
  1101. seek(f,oldpos);
  1102. end;
  1103. var
  1104. elfheader : TElf32header;
  1105. secheader : TElf32sechdr;
  1106. i : longint;
  1107. stringoffset : longint;
  1108. secname : string;
  1109. begin
  1110. postprocessexecutable:=false;
  1111. { open file }
  1112. assign(f,fn);
  1113. {$push}{$I-}
  1114. reset(f,1);
  1115. if ioresult<>0 then
  1116. Message1(execinfo_f_cant_open_executable,fn);
  1117. { read header }
  1118. blockread(f,elfheader,sizeof(tElf32header));
  1119. elfheader:=MayBeSwapHeader(elfheader);
  1120. seek(f,elfheader.e_shoff);
  1121. { read string section header }
  1122. seek(f,elfheader.e_shoff+sizeof(TElf32sechdr)*elfheader.e_shstrndx);
  1123. blockread(f,secheader,sizeof(secheader));
  1124. secheader:=MaybeSwapSecHeader(secheader);
  1125. stringoffset:=secheader.sh_offset;
  1126. seek(f,elfheader.e_shoff);
  1127. status.datasize:=0;
  1128. for i:=0 to elfheader.e_shnum-1 do
  1129. begin
  1130. blockread(f,secheader,sizeof(secheader));
  1131. secheader:=MaybeSwapSecHeader(secheader);
  1132. secname:=ReadSectionName(stringoffset+secheader.sh_name);
  1133. if secname='.text' then
  1134. begin
  1135. Message1(execinfo_x_codesize,tostr(secheader.sh_size));
  1136. status.codesize:=secheader.sh_size;
  1137. end
  1138. else if secname='.data' then
  1139. begin
  1140. Message1(execinfo_x_initdatasize,tostr(secheader.sh_size));
  1141. inc(status.datasize,secheader.sh_size);
  1142. end
  1143. else if secname='.bss' then
  1144. begin
  1145. Message1(execinfo_x_uninitdatasize,tostr(secheader.sh_size));
  1146. inc(status.datasize,secheader.sh_size);
  1147. end;
  1148. end;
  1149. close(f);
  1150. {$pop}
  1151. if ioresult<>0 then
  1152. ;
  1153. postprocessexecutable:=true;
  1154. end;
  1155. {*****************************************************************************
  1156. Initialize
  1157. *****************************************************************************}
  1158. initialization
  1159. {$ifdef arm}
  1160. RegisterLinker(ld_embedded,TLinkerEmbedded);
  1161. RegisterTarget(system_arm_embedded_info);
  1162. {$endif arm}
  1163. {$ifdef avr}
  1164. RegisterLinker(ld_embedded,TLinkerEmbedded);
  1165. RegisterTarget(system_avr_embedded_info);
  1166. {$endif avr}
  1167. {$ifdef i386}
  1168. RegisterLinker(ld_embedded,TLinkerEmbedded);
  1169. RegisterTarget(system_i386_embedded_info);
  1170. {$endif i386}
  1171. {$ifdef mipsel}
  1172. RegisterLinker(ld_embedded,TLinkerEmbedded);
  1173. RegisterTarget(system_mipsel_embedded_info);
  1174. {$endif mipsel}
  1175. end.