t_nds.pas 26 KB

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