t_nds.pas 27 KB

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