t_nds.pas 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  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_nds;
  19. {$i fpcdefs.inc}
  20. interface
  21. implementation
  22. uses
  23. SysUtils,
  24. cutils,cfileutl,cclasses,
  25. globtype,globals,systems,verbose,script,fmodule,i_nds,link;
  26. type
  27. TlinkerNDS=class(texternallinker)
  28. private
  29. Function WriteResponseFile: Boolean;
  30. public
  31. constructor Create; override;
  32. procedure SetDefaultInfo; override;
  33. function MakeExecutable:boolean; override;
  34. end;
  35. {*****************************************************************************
  36. TLINKERNDS
  37. *****************************************************************************}
  38. Constructor TLinkerNDS.Create;
  39. begin
  40. Inherited Create;
  41. SharedLibFiles.doubles:=true;
  42. StaticLibFiles.doubles:=true;
  43. // set arm9 as default apptype
  44. if (apptype <> app_arm9) or (apptype <> app_arm7) then
  45. apptype := app_arm9;
  46. end;
  47. procedure TLinkerNDS.SetDefaultInfo;
  48. begin
  49. with Info do
  50. begin
  51. ExeCmd[1]:='ld -g $OPT $DYNLINK $STATIC $GCSECTIONS $STRIP -L. -o $EXE -T $RES';
  52. end;
  53. end;
  54. Function TLinkerNDS.WriteResponseFile: Boolean;
  55. Var
  56. linkres : TLinkRes;
  57. i : longint;
  58. HPath : TCmdStrListItem;
  59. s,s1,s2 : TCmdStr;
  60. prtobj,
  61. cprtobj : string[80];
  62. linklibc,
  63. linklibgcc : boolean;
  64. found1,
  65. found2 : boolean;
  66. begin
  67. WriteResponseFile:=False;
  68. linklibc:=(SharedLibFiles.Find('c')<>nil);
  69. linklibgcc:=(SharedLibFiles.Find('gcc')<>nil);
  70. case apptype of
  71. app_arm9:
  72. begin
  73. prtobj:='prt09';
  74. cprtobj:='cprt09';
  75. end;
  76. app_arm7:
  77. begin
  78. prtobj:='prt07';
  79. cprtobj:='cprt07';
  80. end;
  81. end;
  82. if (linklibc or linklibgcc) then
  83. prtobj:=cprtobj;
  84. { Open link.res file }
  85. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName);
  86. { Write path to search libraries }
  87. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  88. while assigned(HPath) do
  89. begin
  90. s:=HPath.Str;
  91. if (cs_link_on_target in current_settings.globalswitches) then
  92. s:=ScriptFixFileName(s);
  93. LinkRes.Add('-L'+s);
  94. HPath:=TCmdStrListItem(HPath.Next);
  95. end;
  96. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  97. while assigned(HPath) do
  98. begin
  99. s:=HPath.Str;
  100. if s<>'' then
  101. LinkRes.Add('SEARCH_DIR('+(maybequoted(s))+')');
  102. HPath:=TCmdStrListItem(HPath.Next);
  103. end;
  104. LinkRes.Add('INPUT (');
  105. { add objectfiles, start with prt0 always }
  106. if prtobj<>'' then
  107. s:=FindObjectFile(prtobj,'',false);
  108. LinkRes.AddFileName(s);
  109. { try to add crti and crtbegin if linking to C }
  110. if linklibc then
  111. begin
  112. if librarysearchpath.FindFile('crti.o',false,s) then
  113. LinkRes.AddFileName(s);
  114. end;
  115. if linklibgcc then
  116. begin
  117. if librarysearchpath.FindFile('crtbegin.o',false,s) then
  118. LinkRes.AddFileName(s);
  119. end;
  120. while not ObjectFiles.Empty do
  121. begin
  122. s:=ObjectFiles.GetFirst;
  123. if s<>'' then
  124. begin
  125. { vlink doesn't use SEARCH_DIR for object files }
  126. if not(cs_link_on_target in current_settings.globalswitches) then
  127. s:=FindObjectFile(s,'',false);
  128. LinkRes.AddFileName((maybequoted(s)));
  129. end;
  130. end;
  131. { Write staticlibraries }
  132. if not StaticLibFiles.Empty then
  133. begin
  134. { vlink doesn't need, and doesn't support GROUP }
  135. if (cs_link_on_target in current_settings.globalswitches) then
  136. begin
  137. LinkRes.Add(')');
  138. LinkRes.Add('GROUP(');
  139. end;
  140. while not StaticLibFiles.Empty do
  141. begin
  142. S:=StaticLibFiles.GetFirst;
  143. LinkRes.AddFileName((maybequoted(s)));
  144. end;
  145. end;
  146. if (cs_link_on_target in current_settings.globalswitches) then
  147. begin
  148. LinkRes.Add(')');
  149. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  150. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  151. linklibc:=false;
  152. linklibgcc:=false;
  153. while not SharedLibFiles.Empty do
  154. begin
  155. S:=SharedLibFiles.GetFirst;
  156. if s<>'c' then
  157. begin
  158. i:=Pos(target_info.sharedlibext,S);
  159. if i>0 then
  160. Delete(S,i,255);
  161. LinkRes.Add('-l'+s);
  162. end
  163. else
  164. begin
  165. LinkRes.Add('-l'+s);
  166. linklibc:=true;
  167. linklibgcc:=true;
  168. end;
  169. end;
  170. { be sure that libc&libgcc is the last lib }
  171. if linklibgcc then
  172. begin
  173. LinkRes.Add('-lgcc');
  174. end;
  175. if linklibc then
  176. begin
  177. LinkRes.Add('-lc');
  178. end;
  179. end
  180. else
  181. begin
  182. while not SharedLibFiles.Empty do
  183. begin
  184. S:=SharedLibFiles.GetFirst;
  185. LinkRes.Add('lib'+s+target_info.staticlibext);
  186. end;
  187. LinkRes.Add(')');
  188. end;
  189. { objects which must be at the end }
  190. if linklibgcc then
  191. begin
  192. found1:=librarysearchpath.FindFile('crtend.o',false,s1);
  193. if found1 then
  194. begin
  195. LinkRes.Add('INPUT(');
  196. if found1 then
  197. LinkRes.AddFileName(s1);
  198. LinkRes.Add(')');
  199. end;
  200. end;
  201. if linklibc then
  202. begin
  203. found2:=librarysearchpath.FindFile('crtn.o',false,s2);
  204. if found2 then
  205. begin
  206. LinkRes.Add('INPUT(');
  207. if found2 then
  208. LinkRes.AddFileName(s2);
  209. LinkRes.Add(')');
  210. end;
  211. end;
  212. with linkres do
  213. begin
  214. if apptype=app_arm9 then //ARM9
  215. begin
  216. add('OUTPUT_FORMAT("elf32-littlearm", "elf32-bigarm", "elf32-littlearm")');
  217. add('OUTPUT_ARCH(arm)');
  218. add('ENTRY(_start)');
  219. add('');
  220. add('MEMORY {');
  221. add('');
  222. add(' rom : ORIGIN = 0x08000000, LENGTH = 32M');
  223. add(' ewram : ORIGIN = 0x02000000, LENGTH = 4M - 4k');
  224. add(' dtcm : ORIGIN = 0x0b000000, LENGTH = 16K');
  225. add(' itcm : ORIGIN = 0x01000000, LENGTH = 32K');
  226. add('}');
  227. add('');
  228. add('__itcm_start = ORIGIN(itcm);');
  229. add('__ewram_end = ORIGIN(ewram) + LENGTH(ewram);');
  230. add('__eheap_end = ORIGIN(ewram) + LENGTH(ewram);');
  231. add('__dtcm_start = ORIGIN(dtcm);');
  232. add('__dtcm_top = ORIGIN(dtcm) + LENGTH(dtcm);');
  233. add('__irq_flags = __dtcm_top - 0x08;');
  234. add('__irq_vector = __dtcm_top - 0x04;');
  235. add('');
  236. add('__sp_svc = __dtcm_top - 0x100;');
  237. add('__sp_irq = __sp_svc - 0x100;');
  238. add('__sp_usr = __sp_irq - 0x100;');
  239. add('');
  240. add('SECTIONS');
  241. add('{');
  242. add(' .init :');
  243. add(' {');
  244. add(' __text_start = . ;');
  245. add(' KEEP (*(.init))');
  246. add(' . = ALIGN(4); /* REQUIRED. LD is flaky without it. */');
  247. add(' } >ewram = 0xff');
  248. add('');
  249. add(' .plt : { *(.plt) } >ewram = 0xff');
  250. add('');
  251. add(' .text : /* ALIGN (4): */');
  252. add(' {');
  253. add(' *(EXCLUDE_FILE (*.itcm*) .text)');
  254. add('');
  255. add(' *(.text.*)');
  256. add(' *(.stub)');
  257. add(' /* .gnu.warning sections are handled specially by elf32.em. */');
  258. add(' *(.gnu.warning)');
  259. add(' *(.gnu.linkonce.t*)');
  260. add(' *(.glue_7)');
  261. add(' *(.glue_7t)');
  262. add(' . = ALIGN(4); /* REQUIRED. LD is flaky without it. */');
  263. add(' } >ewram = 0xff');
  264. add('');
  265. add(' .fini :');
  266. add(' {');
  267. add(' KEEP (*(.fini))');
  268. add(' } >ewram =0xff');
  269. add('');
  270. add(' __text_end = . ;');
  271. add('');
  272. add(' .rodata :');
  273. add(' {');
  274. add(' *(.rodata)');
  275. add(' *all.rodata*(*)');
  276. add(' *(.roda)');
  277. add(' *(.rodata.*)');
  278. add(' *(.gnu.linkonce.r*)');
  279. add(' SORT(CONSTRUCTORS)');
  280. add(' . = ALIGN(4); /* REQUIRED. LD is flaky without it. */');
  281. add(' } >ewram = 0xff');
  282. add('');
  283. add(' .ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) } >ewram');
  284. add(' __exidx_start = .;');
  285. add(' .ARM.exidx : { *(.ARM.exidx* .gnu.linkonce.armexidx.*) } >ewram');
  286. add(' __exidx_end = .;');
  287. add(' /* Ensure the __preinit_array_start label is properly aligned. We');
  288. add(' could instead move the label definition inside the section, but');
  289. add(' the linker would then create the section even if it turns out to');
  290. add(' be empty, which isn''t pretty. */');
  291. add(' . = ALIGN(32 / 8);');
  292. add(' PROVIDE (__preinit_array_start = .);');
  293. add(' .preinit_array : { KEEP (*(.preinit_array)) } >ewram = 0xff');
  294. add(' PROVIDE (__preinit_array_end = .);');
  295. add(' PROVIDE (__init_array_start = .);');
  296. add(' .init_array : { KEEP (*(.init_array)) } >ewram = 0xff');
  297. add(' PROVIDE (__init_array_end = .);');
  298. add(' PROVIDE (__fini_array_start = .);');
  299. add(' .fini_array : { KEEP (*(.fini_array)) } >ewram = 0xff');
  300. add(' PROVIDE (__fini_array_end = .);');
  301. add('');
  302. add(' .ctors :');
  303. add(' {');
  304. add(' /* gcc uses crtbegin.o to find the start of the constructors, so');
  305. add(' we make sure it is first. Because this is a wildcard, it');
  306. add(' doesn''t matter if the user does not actually link against');
  307. add(' crtbegin.o; the linker won''t look for a file to match a');
  308. add(' wildcard. The wildcard also means that it doesn''t matter which');
  309. add(' directory crtbegin.o is in. */');
  310. add(' KEEP (*crtbegin.o(.ctors))');
  311. add(' KEEP (*(EXCLUDE_FILE (*crtend.o) .ctors))');
  312. add(' KEEP (*(SORT(.ctors.*)))');
  313. add(' KEEP (*(.ctors))');
  314. add(' . = ALIGN(4); /* REQUIRED. LD is flaky without it. */');
  315. add(' } >ewram = 0xff');
  316. add('');
  317. add(' .dtors :');
  318. add(' {');
  319. add(' KEEP (*crtbegin.o(.dtors))');
  320. add(' KEEP (*(EXCLUDE_FILE (*crtend.o) .dtors))');
  321. add(' KEEP (*(SORT(.dtors.*)))');
  322. add(' KEEP (*(.dtors))');
  323. add(' . = ALIGN(4); /* REQUIRED. LD is flaky without it. */');
  324. add(' } >ewram = 0xff');
  325. add('');
  326. add(' .eh_frame :');
  327. add(' {');
  328. add(' KEEP (*(.eh_frame))');
  329. add(' . = ALIGN(4); /* REQUIRED. LD is flaky without it. */');
  330. add(' } >ewram = 0xff');
  331. add('');
  332. add(' .gcc_except_table :');
  333. add(' {');
  334. add(' *(.gcc_except_table)');
  335. add(' . = ALIGN(4); /* REQUIRED. LD is flaky without it. */');
  336. add(' } >ewram = 0xff');
  337. add(' .jcr : { KEEP (*(.jcr)) } >ewram = 0');
  338. add(' .got : { *(.got.plt) *(.got) *(.rel.got) } >ewram = 0');
  339. add('');
  340. add(' .ewram ALIGN(4) : ');
  341. add(' {');
  342. add(' __ewram_start = ABSOLUTE(.) ;');
  343. add(' *(.ewram)');
  344. add(' *ewram.*(.text)');
  345. add(' . = ALIGN(4); /* REQUIRED. LD is flaky without it. */');
  346. add(' } >ewram = 0xff');
  347. add('');
  348. add('');
  349. add(' .data ALIGN(4) :');
  350. add(' {');
  351. add(' __data_start = ABSOLUTE(.);');
  352. add(' *(.data)');
  353. add(' *(.data.*)');
  354. add(' *(.gnu.linkonce.d*)');
  355. add(' *(.fpc*)');
  356. add(' CONSTRUCTORS');
  357. add(' . = ALIGN(4);');
  358. add(' __data_end = ABSOLUTE(.) ;');
  359. add(' } >ewram = 0xff');
  360. add('');
  361. add('');
  362. add(' __dtcm_lma = . ;');
  363. add(' __bss_vma = . ;');
  364. add('');
  365. add(' .dtcm __dtcm_start : AT (__dtcm_lma)');
  366. add(' {');
  367. add(' *(.dtcm)');
  368. add(' *(.dtcm.*)');
  369. add(' . = ALIGN(4);');
  370. add(' __dtcm_end = ABSOLUTE(.);');
  371. add(' } >dtcm = 0xff');
  372. add('');
  373. add('');
  374. add(' __itcm_lma = __dtcm_lma + SIZEOF(.dtcm);');
  375. add('');
  376. add(' .itcm __itcm_start : AT (__itcm_lma)');
  377. add(' {');
  378. add(' *(.itcm)');
  379. add(' *itcm.*(.text)');
  380. add(' . = ALIGN(4);');
  381. add(' __itcm_end = ABSOLUTE(.);');
  382. add(' } >itcm = 0xff');
  383. add('');
  384. add(' .sbss __dtcm_end : ');
  385. add(' {');
  386. add(' __sbss_start = ABSOLUTE(.);');
  387. add(' __sbss_start__ = ABSOLUTE(.);');
  388. add(' *(.sbss)');
  389. add(' . = ALIGN(4); /* REQUIRED. LD is flaky without it. */');
  390. add(' __sbss_end = ABSOLUTE(.);');
  391. add(' } >dtcm');
  392. add('');
  393. add('');
  394. add('');
  395. add(' .bss __bss_vma (NOLOAD):');
  396. add(' {');
  397. add(' __bss_start = ABSOLUTE(.);');
  398. add(' __bss_start__ = ABSOLUTE(.);');
  399. add(' *(.dynbss)');
  400. add(' *(.gnu.linkonce.b*)');
  401. add(' *(.bss*)');
  402. add(' *(COMMON)');
  403. add(' . = ALIGN(4); /* REQUIRED. LD is flaky without it. */');
  404. add(' __bss_end = ABSOLUTE(.) ;');
  405. add(' __bss_end__ = __bss_end ;');
  406. add(' } AT>ewram');
  407. add('');
  408. add('');
  409. add(' _end = __bss_end__ ;');
  410. add(' __end__ = __bss_end__ ;');
  411. add('');
  412. add('');
  413. add('');
  414. add(' /* Stabs debugging sections. */');
  415. add(' .stab 0 : { *(.stab) }');
  416. add(' .stabstr 0 : { *(.stabstr) }');
  417. add(' .stab.excl 0 : { *(.stab.excl) }');
  418. add(' .stab.exclstr 0 : { *(.stab.exclstr) }');
  419. add(' .stab.index 0 : { *(.stab.index) }');
  420. add(' .stab.indexstr 0 : { *(.stab.indexstr) }');
  421. add(' .comment 0 : { *(.comment) }');
  422. add(' /* DWARF debug sections.');
  423. add(' Symbols in the DWARF debugging sections are relative to the beginning');
  424. add(' of the section so we begin them at 0. */');
  425. add(' /* DWARF 1 */');
  426. add(' .debug 0 : { *(.debug) }');
  427. add(' .line 0 : { *(.line) }');
  428. add(' /* GNU DWARF 1 extensions */');
  429. add(' .debug_srcinfo 0 : { *(.debug_srcinfo) }');
  430. add(' .debug_sfnames 0 : { *(.debug_sfnames) }');
  431. add(' /* DWARF 1.1 and DWARF 2 */');
  432. add(' .debug_aranges 0 : { *(.debug_aranges) }');
  433. add(' .debug_pubnames 0 : { *(.debug_pubnames) }');
  434. add(' /* DWARF 2 */');
  435. add(' .debug_info 0 : { *(.debug_info) }');
  436. add(' .debug_abbrev 0 : { *(.debug_abbrev) }');
  437. add(' .debug_line 0 : { *(.debug_line) }');
  438. add(' .debug_frame 0 : { *(.debug_frame) }');
  439. add(' .debug_str 0 : { *(.debug_str) }');
  440. add(' .debug_loc 0 : { *(.debug_loc) }');
  441. add(' .debug_macinfo 0 : { *(.debug_macinfo) }');
  442. add(' /* SGI/MIPS DWARF 2 extensions */');
  443. add(' .debug_weaknames 0 : { *(.debug_weaknames) }');
  444. add(' .debug_funcnames 0 : { *(.debug_funcnames) }');
  445. add(' .debug_typenames 0 : { *(.debug_typenames) }');
  446. add(' .debug_varnames 0 : { *(.debug_varnames) }');
  447. add(' .stack 0x80000 : { _stack = .; *(.stack) }');
  448. add(' /* These must appear regardless of . */');
  449. add('}');
  450. end;
  451. if apptype=app_arm7 then
  452. begin
  453. add('OUTPUT_FORMAT("elf32-littlearm", "elf32-bigarm", "elf32-littlearm")');
  454. add('OUTPUT_ARCH(arm)');
  455. add('ENTRY(_start)');
  456. add('');
  457. add('MEMORY {');
  458. add('');
  459. add(' rom : ORIGIN = 0x08000000, LENGTH = 32M');
  460. add(' iwram : ORIGIN = 0x037f8000, LENGTH = 96K');
  461. add('}');
  462. add('');
  463. add('__iwram_start = ORIGIN(iwram);');
  464. add('__iwram_top = ORIGIN(iwram)+ LENGTH(iwram);');
  465. add('__sp_irq = __iwram_top - 0x60;');
  466. add('__sp_svc = __sp_irq - 0x100;');
  467. add('__sp_usr = __sp_svc - 0x100;');
  468. add('');
  469. add('__irq_flags = __iwram_top - 8;');
  470. add('__irq_vector = __iwram_top - 4;');
  471. add('');
  472. add('SECTIONS');
  473. add('{');
  474. add(' .init :');
  475. add(' {');
  476. add(' __text_start = . ;');
  477. add(' KEEP (*(.init))');
  478. add(' . = ALIGN(4); /* REQUIRED. LD is flaky without it. */');
  479. add(' } >iwram = 0xff');
  480. add(' .plt : { *(.plt) } >iwram = 0xff');
  481. add('');
  482. add(' .text : /* ALIGN (4): */');
  483. add(' {');
  484. add('');
  485. add(' *(.text .stub .text.* .gnu.linkonce.t.*)');
  486. add(' KEEP (*(.text.*personality*))');
  487. add(' /* .gnu.warning sections are handled specially by elf32.em. */');
  488. add(' *(.gnu.warning)');
  489. add(' KEEP (*(.text.*personality*))');
  490. add(' . = ALIGN(4); /* REQUIRED. LD is flaky without it. */');
  491. add(' } >iwram = 0xff');
  492. add('');
  493. add(' .fini :');
  494. add(' {');
  495. add(' KEEP (*(.fini))');
  496. add(' } >iwram =0xff');
  497. add('');
  498. add(' __text_end = . ;');
  499. add('');
  500. add(' .rodata :');
  501. add(' {');
  502. add(' *(.rodata)');
  503. add(' *all.rodata*(*)');
  504. add(' *(.roda)');
  505. add(' *(.rodata.*)');
  506. add(' *(.gnu.linkonce.r*)');
  507. add(' SORT(CONSTRUCTORS)');
  508. add(' . = ALIGN(4); /* REQUIRED. LD is flaky without it. */');
  509. add(' } >iwram = 0xff');
  510. add('');
  511. add(' .ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) } >iwram');
  512. add(' __exidx_start = .;');
  513. add(' .ARM.exidx : { *(.ARM.exidx* .gnu.linkonce.armexidx.*) } >iwram');
  514. add(' __exidx_end = .;');
  515. add('');
  516. add('/* Ensure the __preinit_array_start label is properly aligned. We');
  517. add(' could instead move the label definition inside the section, but');
  518. add(' the linker would then create the section even if it turns out to');
  519. add(' be empty, which isn''t pretty. */');
  520. add(' . = ALIGN(32 / 8);');
  521. add(' PROVIDE (__preinit_array_start = .);');
  522. add(' .preinit_array : { KEEP (*(.preinit_array)) } >iwram = 0xff');
  523. add(' PROVIDE (__preinit_array_end = .);');
  524. add(' PROVIDE (__init_array_start = .);');
  525. add(' .init_array : { KEEP (*(.init_array)) } >iwram = 0xff');
  526. add(' PROVIDE (__init_array_end = .);');
  527. add(' PROVIDE (__fini_array_start = .);');
  528. add(' .fini_array : { KEEP (*(.fini_array)) } >iwram = 0xff');
  529. add(' PROVIDE (__fini_array_end = .);');
  530. add('');
  531. add(' .ctors :');
  532. add(' {');
  533. add(' /* gcc uses crtbegin.o to find the start of the constructors, so');
  534. add(' we make sure it is first. Because this is a wildcard, it');
  535. add(' doesn''t matter if the user does not actually link against');
  536. add(' crtbegin.o; the linker won''t look for a file to match a');
  537. add(' wildcard. The wildcard also means that it doesn''t matter which');
  538. add(' directory crtbegin.o is in. */');
  539. add(' KEEP (*crtbegin.o(.ctors))');
  540. add(' KEEP (*(EXCLUDE_FILE (*crtend.o) .ctors))');
  541. add(' KEEP (*(SORT(.ctors.*)))');
  542. add(' KEEP (*(.ctors))');
  543. add(' . = ALIGN(4); /* REQUIRED. LD is flaky without it. */');
  544. add(' } >iwram = 0xff');
  545. add('');
  546. add(' .dtors :');
  547. add(' {');
  548. add(' KEEP (*crtbegin.o(.dtors))');
  549. add(' KEEP (*(EXCLUDE_FILE (*crtend.o) .dtors))');
  550. add(' KEEP (*(SORT(.dtors.*)))');
  551. add(' KEEP (*(.dtors))');
  552. add(' . = ALIGN(4); /* REQUIRED. LD is flaky without it. */');
  553. add(' } >iwram = 0xff');
  554. add('');
  555. add(' .eh_frame :');
  556. add(' {');
  557. add(' KEEP (*(.eh_frame))');
  558. add(' . = ALIGN(4); /* REQUIRED. LD is flaky without it. */');
  559. add(' } >iwram = 0xff');
  560. add('');
  561. add(' .gcc_except_table :');
  562. add(' {');
  563. add(' *(.gcc_except_table)');
  564. add(' . = ALIGN(4); /* REQUIRED. LD is flaky without it. */');
  565. add(' } >iwram = 0xff');
  566. add(' .jcr : { KEEP (*(.jcr)) } >iwram = 0');
  567. add(' .got : { *(.got.plt) *(.got) } >iwram = 0');
  568. add('');
  569. add('');
  570. add(' .iwram ALIGN(4) :');
  571. add(' {');
  572. add(' __iwram_start = ABSOLUTE(.) ;');
  573. add(' *(.iwram)');
  574. add(' *iwram.*(.text)');
  575. add(' . = ALIGN(4); /* REQUIRED. LD is flaky without it. */');
  576. add(' __iwram_end = ABSOLUTE(.) ;');
  577. add(' } >iwram = 0xff');
  578. add('');
  579. add('');
  580. add(' .data ALIGN(4) : {');
  581. add(' __data_start = ABSOLUTE(.);');
  582. add(' *(.data)');
  583. add(' *(.data.*)');
  584. add(' *(.gnu.linkonce.d*)');
  585. add(' *(.fpc*)');
  586. add(' CONSTRUCTORS');
  587. add(' . = ALIGN(4);');
  588. add(' __data_end = ABSOLUTE(.) ;');
  589. add(' } >iwram = 0xff');
  590. add('');
  591. add('');
  592. add('');
  593. add(' .bss ALIGN(4) :');
  594. add(' {');
  595. add(' __bss_start = ABSOLUTE(.);');
  596. add(' __bss_start__ = ABSOLUTE(.);');
  597. add(' *(.dynbss)');
  598. add(' *(.gnu.linkonce.b*)');
  599. add(' *(.bss*)');
  600. add(' *(COMMON)');
  601. add(' . = ALIGN(4); /* REQUIRED. LD is flaky without it. */');
  602. add(' } >iwram');
  603. add('');
  604. add(' __bss_end = . ;');
  605. add(' __bss_end__ = . ;');
  606. add('');
  607. add(' _end = . ;');
  608. add(' __end__ = . ;');
  609. add(' PROVIDE (end = _end);');
  610. add('');
  611. add(' /* Stabs debugging sections. */');
  612. add(' .stab 0 : { *(.stab) }');
  613. add(' .stabstr 0 : { *(.stabstr) }');
  614. add(' .stab.excl 0 : { *(.stab.excl) }');
  615. add(' .stab.exclstr 0 : { *(.stab.exclstr) }');
  616. add(' .stab.index 0 : { *(.stab.index) }');
  617. add(' .stab.indexstr 0 : { *(.stab.indexstr) }');
  618. add(' .comment 0 : { *(.comment) }');
  619. add(' /* DWARF debug sections.');
  620. add(' Symbols in the DWARF debugging sections are relative to the beginning');
  621. add(' of the section so we begin them at 0. */');
  622. add(' /* DWARF 1 */');
  623. add(' .debug 0 : { *(.debug) }');
  624. add(' .line 0 : { *(.line) }');
  625. add(' /* GNU DWARF 1 extensions */');
  626. add(' .debug_srcinfo 0 : { *(.debug_srcinfo) }');
  627. add(' .debug_sfnames 0 : { *(.debug_sfnames) }');
  628. add(' /* DWARF 1.1 and DWARF 2 */');
  629. add(' .debug_aranges 0 : { *(.debug_aranges) }');
  630. add(' .debug_pubnames 0 : { *(.debug_pubnames) }');
  631. add(' /* DWARF 2 */');
  632. add(' .debug_info 0 : { *(.debug_info) }');
  633. add(' .debug_abbrev 0 : { *(.debug_abbrev) }');
  634. add(' .debug_line 0 : { *(.debug_line) }');
  635. add(' .debug_frame 0 : { *(.debug_frame) }');
  636. add(' .debug_str 0 : { *(.debug_str) }');
  637. add(' .debug_loc 0 : { *(.debug_loc) }');
  638. add(' .debug_macinfo 0 : { *(.debug_macinfo) }');
  639. add(' /* SGI/MIPS DWARF 2 extensions */');
  640. add(' .debug_weaknames 0 : { *(.debug_weaknames) }');
  641. add(' .debug_funcnames 0 : { *(.debug_funcnames) }');
  642. add(' .debug_typenames 0 : { *(.debug_typenames) }');
  643. add(' .debug_varnames 0 : { *(.debug_varnames) }');
  644. add(' .stack 0x80000 : { _stack = .; *(.stack) }');
  645. add(' /* These must appear regardless of . */');
  646. add('}');
  647. end;
  648. end;
  649. { Write and Close response }
  650. linkres.writetodisk;
  651. linkres.free;
  652. WriteResponseFile:=True;
  653. end;
  654. function TLinkerNDS.MakeExecutable:boolean;
  655. var
  656. binstr,
  657. cmdstr : TCmdStr;
  658. success : boolean;
  659. StaticStr,
  660. GCSectionsStr,
  661. DynLinkStr,
  662. StripStr: string;
  663. preName: string;
  664. begin
  665. { for future use }
  666. StaticStr:='';
  667. StripStr:='';
  668. DynLinkStr:='';
  669. case apptype of
  670. app_arm9: preName:='.nef';
  671. app_arm7: preName:='.nlf';
  672. end;
  673. GCSectionsStr:='--gc-sections';
  674. if not(cs_link_nolink in current_settings.globalswitches) then
  675. Message1(exec_i_linking,current_module.exefilename^);
  676. { Write used files and libraries }
  677. WriteResponseFile();
  678. { Call linker }
  679. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  680. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  681. Replace(cmdstr,'$EXE',(maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename^,preName)))));
  682. Replace(cmdstr,'$RES',(maybequoted(ScriptFixFileName(outputexedir+Info.ResName))));
  683. Replace(cmdstr,'$STATIC',StaticStr);
  684. Replace(cmdstr,'$STRIP',StripStr);
  685. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  686. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  687. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  688. { Remove ReponseFile }
  689. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  690. DeleteFile(outputexedir+Info.ResName);
  691. { Post process }
  692. if success then
  693. begin
  694. success:=DoExec(FindUtil(utilsprefix + 'objcopy'), '-O binary '+
  695. ChangeFileExt(current_module.exefilename^, preName) + ' ' +
  696. ChangeFileExt(current_module.exefilename^, preName+target_info.exeext),
  697. true,false);
  698. end;
  699. if success and (apptype=app_arm9) then
  700. begin
  701. success:=DoExec(FindUtil('ndstool'), '-c ' +
  702. ChangeFileExt(current_module.exefilename^, '.nds') + ' -9 ' +
  703. ChangeFileExt(current_module.exefilename^, preName+target_info.exeext),
  704. true,false);
  705. end;
  706. MakeExecutable:=success; { otherwise a recursive call to link method }
  707. end;
  708. {*****************************************************************************
  709. Initialize
  710. *****************************************************************************}
  711. initialization
  712. RegisterExternalLinker(system_arm_nds_info,TLinkerNDS);
  713. RegisterTarget(system_arm_nds_info);
  714. end.