t_nds.pas 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  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,true);
  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("'+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('MEMORY {');
  218. add(' rom : ORIGIN = 0x08000000, LENGTH = 32M');
  219. add(' ewram : ORIGIN = 0x02000000, LENGTH = 4M - 4k');
  220. add(' dtcm : ORIGIN = 0x0b000000, LENGTH = 16K');
  221. add(' vectors : ORIGIN = 0x01000000, LENGTH = 256');
  222. add(' itcm : ORIGIN = 0x01000100, LENGTH = 32K - 256');
  223. add('}');
  224. add('');
  225. add('OUTPUT_ARCH(arm)');
  226. add('OUTPUT_FORMAT("elf32-littlearm", "elf32-bigarm", "elf32-littlearm")');
  227. add('ENTRY(_start)');
  228. add('');
  229. add('__vectors_start = ORIGIN(vectors);');
  230. add('__itcm_start = ORIGIN(itcm);');
  231. add('__ewram_end = ORIGIN(ewram) + LENGTH(ewram);');
  232. add('__eheap_end = ORIGIN(ewram) + LENGTH(ewram);');
  233. add('__dtcm_start = ORIGIN(dtcm);');
  234. add('__dtcm_top = ORIGIN(dtcm) + LENGTH(dtcm);');
  235. add('__irq_flags = __dtcm_top - 0x08;');
  236. add('__irq_vector = __dtcm_top - 0x04;');
  237. add('');
  238. add('__sp_svc = __dtcm_top - 0x100;');
  239. add('__sp_irq = __sp_svc - 0x100;');
  240. add('__sp_usr = __sp_irq - 0x100;');
  241. add('');
  242. add('SECTIONS');
  243. add('{');
  244. add(' .init :');
  245. add(' {');
  246. add(' __text_start = . ;');
  247. add(' KEEP (*(.init))');
  248. add(' . = ALIGN(4); /* REQUIRED. LD is flaky without it. */');
  249. add(' } >ewram = 0xff');
  250. add('');
  251. add(' .plt : { *(.plt) } >ewram = 0xff');
  252. add('');
  253. add(' .text : /* ALIGN (4): */');
  254. add(' {');
  255. add(' *(EXCLUDE_FILE (*.itcm*) .text)');
  256. add('');
  257. add(' *(.text.*)');
  258. add(' *(.stub)');
  259. add(' /* .gnu.warning sections are handled specially by elf32.em. */');
  260. add(' *(.gnu.warning)');
  261. add(' *(.gnu.linkonce.t*)');
  262. add(' *(.glue_7)');
  263. add(' *(.glue_7t)');
  264. add(' . = ALIGN(4); /* REQUIRED. LD is flaky without it. */');
  265. add(' } >ewram = 0xff');
  266. add('');
  267. add(' .fini :');
  268. add(' {');
  269. add(' KEEP (*(.fini))');
  270. add(' } >ewram =0xff');
  271. add('');
  272. add(' __text_end = . ;');
  273. add('');
  274. add(' .rodata :');
  275. add(' {');
  276. add(' *(.rodata)');
  277. add(' *all.rodata*(*)');
  278. add(' *(.roda)');
  279. add(' *(.rodata.*)');
  280. add(' *(.gnu.linkonce.r*)');
  281. add(' SORT(CONSTRUCTORS)');
  282. add(' . = ALIGN(4); /* REQUIRED. LD is flaky without it. */');
  283. add(' } >ewram = 0xff');
  284. add('');
  285. add(' .ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) } >ewram');
  286. add(' __exidx_start = .;');
  287. add(' ARM.exidx : { *(.ARM.exidx* .gnu.linkonce.armexidx.*) } >ewram');
  288. add(' __exidx_end = .;');
  289. add('');
  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('');
  295. add(' . = ALIGN(32 / 8);');
  296. add('');
  297. add(' PROVIDE (__preinit_array_start = .);');
  298. add(' .preinit_array : { KEEP (*(.preinit_array)) } >ewram = 0xff');
  299. add(' PROVIDE (__preinit_array_end = .);');
  300. add(' PROVIDE (__init_array_start = .);');
  301. add(' .init_array :');
  302. add(' {');
  303. add(' KEEP (*(SORT(.init_array.*)))');
  304. add(' KEEP (*(.init_array))');
  305. add(' } >ewram = 0xff');
  306. add(' PROVIDE (__init_array_end = .);');
  307. add(' PROVIDE (__fini_array_start = .);');
  308. add(' .fini_array :');
  309. add(' {');
  310. add(' KEEP (*(.fini_array))');
  311. add(' KEEP (*(SORT(.fini_array.*)))');
  312. add(' } >ewram = 0xff');
  313. add('');
  314. add(' PROVIDE (__fini_array_end = .);');
  315. add('');
  316. add(' .ctors :');
  317. add(' {');
  318. add(' /* gcc uses crtbegin.o to find the start of the constructors, so');
  319. add(' we make sure it is first. Because this is a wildcard, it');
  320. add(' doesn''t matter if the user does not actually link against');
  321. add(' crtbegin.o; the linker won''t look for a file to match a');
  322. add(' wildcard. The wildcard also means that it doesn''t matter which');
  323. add(' directory crtbegin.o is in. */');
  324. add(' KEEP (*crtbegin.o(.ctors))');
  325. add(' KEEP (*(EXCLUDE_FILE (*crtend.o) .ctors))');
  326. add(' KEEP (*(SORT(.ctors.*)))');
  327. add(' KEEP (*(.ctors))');
  328. add(' . = ALIGN(4); /* REQUIRED. LD is flaky without it. */');
  329. add(' } >ewram = 0xff');
  330. add('');
  331. add(' .dtors :');
  332. add(' {');
  333. add(' KEEP (*crtbegin.o(.dtors))');
  334. add(' KEEP (*(EXCLUDE_FILE (*crtend.o) .dtors))');
  335. add(' KEEP (*(SORT(.dtors.*)))');
  336. add(' KEEP (*(.dtors))');
  337. add(' . = ALIGN(4); /* REQUIRED. LD is flaky without it. */');
  338. add(' } >ewram = 0xff');
  339. add('');
  340. add(' .eh_frame :');
  341. add(' {');
  342. add(' KEEP (*(.eh_frame))');
  343. add(' . = ALIGN(4); /* REQUIRED. LD is flaky without it. */');
  344. add(' } >ewram = 0xff');
  345. add('');
  346. add(' .gcc_except_table :');
  347. add(' {');
  348. add(' *(.gcc_except_table)');
  349. add(' . = ALIGN(4); /* REQUIRED. LD is flaky without it. */');
  350. add(' } >ewram = 0xff');
  351. add(' .jcr : { KEEP (*(.jcr)) } >ewram = 0');
  352. add(' .got : { *(.got.plt) *(.got) *(.rel.got) } >ewram = 0');
  353. add('');
  354. add(' .ewram ALIGN(4) : ');
  355. add(' {');
  356. add(' __ewram_start = ABSOLUTE(.) ;');
  357. add(' *(.ewram)');
  358. add(' *ewram.*(.text)');
  359. add(' . = ALIGN(4); /* REQUIRED. LD is flaky without it. */');
  360. add(' } >ewram = 0xff');
  361. add('');
  362. add('');
  363. add(' .data ALIGN(4) :');
  364. add(' {');
  365. add(' __data_start = ABSOLUTE(.);');
  366. add(' *(.data)');
  367. add(' *(.data.*)');
  368. add(' *(.gnu.linkonce.d*)');
  369. add(' CONSTRUCTORS');
  370. add(' . = ALIGN(4);');
  371. add(' __data_end = ABSOLUTE(.) ;');
  372. add(' } >ewram = 0xff');
  373. add('');
  374. add('');
  375. add(' __dtcm_lma = . ;');
  376. add(' __bss_vma = . ;');
  377. add('');
  378. add(' .dtcm __dtcm_start : AT (__dtcm_lma)');
  379. add(' {');
  380. add(' *(.dtcm)');
  381. add(' *(.dtcm.*)');
  382. add(' . = ALIGN(4);');
  383. add(' __dtcm_end = ABSOLUTE(.);');
  384. add(' } >dtcm = 0xff');
  385. add('');
  386. add('');
  387. add(' __itcm_lma = __dtcm_lma + SIZEOF(.dtcm);');
  388. add('');
  389. add(' .itcm __itcm_start : AT (__itcm_lma)');
  390. add(' {');
  391. add(' *(.itcm)');
  392. add(' *itcm.*(.text)');
  393. add(' . = ALIGN(4);');
  394. add(' __itcm_end = ABSOLUTE(.);');
  395. add(' } >itcm = 0xff');
  396. add(' ');
  397. add(' __vectors_lma = __itcm_lma + SIZEOF(.itcm);');
  398. add('');
  399. add(' .vectors __vectors_start : AT (__vectors_lma)');
  400. add(' {');
  401. add(' *(.vectors)');
  402. add(' *vectors.*(.text)');
  403. add(' . = ALIGN(4);');
  404. add(' __vectors_end = ABSOLUTE(.);');
  405. add(' } >vectors = 0xff');
  406. add(' ');
  407. add(' .sbss __dtcm_end (NOLOAD): ');
  408. add(' {');
  409. add(' __sbss_start = ABSOLUTE(.);');
  410. add(' __sbss_start__ = ABSOLUTE(.);');
  411. add(' *(.sbss)');
  412. add(' . = ALIGN(4); /* REQUIRED. LD is flaky without it. */');
  413. add(' __sbss_end = ABSOLUTE(.);');
  414. add(' } >dtcm ');
  415. add('');
  416. add(' .bss __bss_vma (NOLOAD): ');
  417. add(' {');
  418. add(' __bss_start = ABSOLUTE(.);');
  419. add(' __bss_start__ = ABSOLUTE(.);');
  420. add(' *(.dynbss)');
  421. add(' *(.gnu.linkonce.b*)');
  422. add(' *(.bss*)');
  423. add(' *(COMMON)');
  424. add(' . = ALIGN(4); /* REQUIRED. LD is flaky without it. */');
  425. add(' __bss_end__ = ABSOLUTE(.) ;');
  426. add(' __end__ = ABSOLUTE(.) ;');
  427. add(' } AT>ewram ');
  428. add('');
  429. add('');
  430. add('');
  431. add(' /* Stabs debugging sections. */');
  432. add(' .stab 0 : { *(.stab) }');
  433. add(' .stabstr 0 : { *(.stabstr) }');
  434. add(' .stab.excl 0 : { *(.stab.excl) }');
  435. add(' .stab.exclstr 0 : { *(.stab.exclstr) }');
  436. add(' .stab.index 0 : { *(.stab.index) }');
  437. add(' .stab.indexstr 0 : { *(.stab.indexstr) }');
  438. add(' .comment 0 : { *(.comment) }');
  439. add(' /* DWARF debug sections.');
  440. add(' Symbols in the DWARF debugging sections are relative to the beginning');
  441. add(' of the section so we begin them at 0. */');
  442. add(' /* DWARF 1 */');
  443. add(' .debug 0 : { *(.debug) }');
  444. add(' .line 0 : { *(.line) }');
  445. add(' /* GNU DWARF 1 extensions */');
  446. add(' .debug_srcinfo 0 : { *(.debug_srcinfo) }');
  447. add(' .debug_sfnames 0 : { *(.debug_sfnames) }');
  448. add(' /* DWARF 1.1 and DWARF 2 */');
  449. add(' .debug_aranges 0 : { *(.debug_aranges) }');
  450. add(' .debug_pubnames 0 : { *(.debug_pubnames) }');
  451. add(' /* DWARF 2 */');
  452. add(' .debug_info 0 : { *(.debug_info) }');
  453. add(' .debug_abbrev 0 : { *(.debug_abbrev) }');
  454. add(' .debug_line 0 : { *(.debug_line) }');
  455. add(' .debug_frame 0 : { *(.debug_frame) }');
  456. add(' .debug_str 0 : { *(.debug_str) }');
  457. add(' .debug_loc 0 : { *(.debug_loc) }');
  458. add(' .debug_macinfo 0 : { *(.debug_macinfo) }');
  459. add(' /* SGI/MIPS DWARF 2 extensions */');
  460. add(' .debug_weaknames 0 : { *(.debug_weaknames) }');
  461. add(' .debug_funcnames 0 : { *(.debug_funcnames) }');
  462. add(' .debug_typenames 0 : { *(.debug_typenames) }');
  463. add(' .debug_varnames 0 : { *(.debug_varnames) }');
  464. add(' .stack 0x80000 : { _stack = .; *(.stack) }');
  465. add(' /* These must appear regardless of . */');
  466. add('}');
  467. add('');
  468. end;
  469. if apptype=app_arm7 then
  470. begin
  471. add('OUTPUT_FORMAT("elf32-littlearm", "elf32-bigarm", "elf32-littlearm")');
  472. add('OUTPUT_ARCH(arm)');
  473. add('ENTRY(_start)');
  474. add('');
  475. add('MEMORY {');
  476. add('');
  477. add(' rom : ORIGIN = 0x08000000, LENGTH = 32M');
  478. add(' iwram : ORIGIN = 0x037f8000, LENGTH = 96K ');
  479. add('}');
  480. add('');
  481. add('__iwram_start = ORIGIN(iwram);');
  482. add('__iwram_top = ORIGIN(iwram)+ LENGTH(iwram);');
  483. add('');
  484. add('__sp_irq = __iwram_top - 0x100;');
  485. add('__sp_svc = __sp_irq - 0x100;');
  486. add('__sp_usr = __sp_svc - 0x100;');
  487. add('');
  488. add('__irq_flags = 0x04000000 - 8;');
  489. add('__irq_flagsaux = 0x04000000 - 0x40;');
  490. add('__irq_vector = 0x04000000 - 4;');
  491. add('');
  492. add('SECTIONS');
  493. add('{');
  494. add(' .init :');
  495. add(' {');
  496. add(' __text_start = . ;');
  497. add(' KEEP (*(.init))');
  498. add(' . = ALIGN(4); /* REQUIRED. LD is flaky without it. */');
  499. add(' } >iwram = 0xff');
  500. add(' .plt : { *(.plt) } >iwram = 0xff');
  501. add('');
  502. add(' .text : /* ALIGN (4): */');
  503. add(' {');
  504. add(' *(.text .stub .text.* .gnu.linkonce.t.*)');
  505. add(' KEEP (*(.text.*personality*))');
  506. add(' /* .gnu.warning sections are handled specially by elf32.em. */');
  507. add(' *(.gnu.warning)');
  508. add(' *(.glue_7t) *(.glue_7) *(.vfp11_veneer)');
  509. add(' . = ALIGN(4); /* REQUIRED. LD is flaky without it. */');
  510. add(' } >iwram = 0xff');
  511. add('');
  512. add(' .fini :');
  513. add(' {');
  514. add(' KEEP (*(.fini))');
  515. add(' } >iwram =0xff');
  516. add('');
  517. add(' __text_end = . ;');
  518. add('');
  519. add(' .rodata :');
  520. add(' {');
  521. add(' *(.rodata)');
  522. add(' *all.rodata*(*)');
  523. add(' *(.roda)');
  524. add(' *(.rodata.*)');
  525. add(' *(.gnu.linkonce.r*)');
  526. add(' SORT(CONSTRUCTORS)');
  527. add(' . = ALIGN(4); /* REQUIRED. LD is flaky without it. */');
  528. add(' } >iwram = 0xff');
  529. add('');
  530. add(' .ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) } >iwram');
  531. add(' __exidx_start = .;');
  532. add(' .ARM.exidx : { *(.ARM.exidx* .gnu.linkonce.armexidx.*) } >iwram');
  533. add(' __exidx_end = .;');
  534. add('');
  535. add('/* Ensure the __preinit_array_start label is properly aligned. We');
  536. add(' could instead move the label definition inside the section, but');
  537. add(' the linker would then create the section even if it turns out to');
  538. add(' be empty, which isn''t pretty. */');
  539. add(' . = ALIGN(32 / 8);');
  540. add(' PROVIDE (__preinit_array_start = .);');
  541. add(' .preinit_array : { KEEP (*(.preinit_array)) } >iwram = 0xff');
  542. add(' PROVIDE (__preinit_array_end = .);');
  543. add(' PROVIDE (__init_array_start = .);');
  544. add(' .init_array : { KEEP (*(.init_array)) } >iwram = 0xff');
  545. add(' PROVIDE (__init_array_end = .);');
  546. add(' PROVIDE (__fini_array_start = .);');
  547. add(' .fini_array : { KEEP (*(.fini_array)) } >iwram = 0xff');
  548. add(' PROVIDE (__fini_array_end = .);');
  549. add('');
  550. add(' .ctors :');
  551. add(' {');
  552. add(' /* gcc uses crtbegin.o to find the start of the constructors, so');
  553. add(' we make sure it is first. Because this is a wildcard, it');
  554. add(' doesn''t matter if the user does not actually link against');
  555. add(' crtbegin.o; the linker won''t look for a file to match a');
  556. add(' wildcard. The wildcard also means that it doesn''t matter which');
  557. add(' directory crtbegin.o is in. */');
  558. add(' KEEP (*crtbegin.o(.ctors))');
  559. add(' KEEP (*(EXCLUDE_FILE (*crtend.o) .ctors))');
  560. add(' KEEP (*(SORT(.ctors.*)))');
  561. add(' KEEP (*(.ctors))');
  562. add(' . = ALIGN(4); /* REQUIRED. LD is flaky without it. */');
  563. add(' } >iwram = 0xff');
  564. add('');
  565. add(' .dtors :');
  566. add(' {');
  567. add(' KEEP (*crtbegin.o(.dtors))');
  568. add(' KEEP (*(EXCLUDE_FILE (*crtend.o) .dtors))');
  569. add(' KEEP (*(SORT(.dtors.*)))');
  570. add(' KEEP (*(.dtors))');
  571. add(' . = ALIGN(4); /* REQUIRED. LD is flaky without it. */');
  572. add(' } >iwram = 0xff');
  573. add('');
  574. add(' .eh_frame :');
  575. add(' {');
  576. add(' KEEP (*(.eh_frame))');
  577. add(' . = ALIGN(4); /* REQUIRED. LD is flaky without it. */');
  578. add(' } >iwram = 0xff');
  579. add('');
  580. add(' .gcc_except_table :');
  581. add(' {');
  582. add(' *(.gcc_except_table)');
  583. add(' . = ALIGN(4); /* REQUIRED. LD is flaky without it. */');
  584. add(' } >iwram = 0xff');
  585. add(' .jcr : { KEEP (*(.jcr)) } >iwram = 0');
  586. add(' .got : { *(.got.plt) *(.got) } >iwram = 0');
  587. add('');
  588. add(' .data ALIGN(4) : {');
  589. add(' __data_start = ABSOLUTE(.);');
  590. add(' *(.data)');
  591. add(' *(.data.*)');
  592. add(' *(.gnu.linkonce.d*)');
  593. add(' CONSTRUCTORS');
  594. add(' . = ALIGN(4);');
  595. add(' __data_end = ABSOLUTE(.) ;');
  596. add(' } >iwram = 0xff');
  597. add('');
  598. add(' .bss ALIGN(4) :');
  599. add(' {');
  600. add(' __bss_start = ABSOLUTE(.);');
  601. add(' __bss_start__ = ABSOLUTE(.);');
  602. add(' *(.dynbss)');
  603. add(' *(.gnu.linkonce.b*)');
  604. add(' *(.bss*)');
  605. add(' *(COMMON)');
  606. add(' . = ALIGN(4); /* REQUIRED. LD is flaky without it. */');
  607. add(' __bss_end__ = ABSOLUTE(.);');
  608. add(' __end__ = ABSOLUTE(.);');
  609. add(' } >iwram');
  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. MapStr,
  663. StripStr: string;
  664. preName: string;
  665. begin
  666. { for future use }
  667. StaticStr:='';
  668. StripStr:='';
  669. MapStr:='';
  670. DynLinkStr:='';
  671. case apptype of
  672. app_arm9: preName:='.nef';
  673. app_arm7: preName:='.nlf';
  674. end;
  675. if (cs_link_strip in current_settings.globalswitches) and
  676. not(cs_link_separate_dbg_file in current_settings.globalswitches) then
  677. StripStr:='-s';
  678. if (cs_link_map in current_settings.globalswitches) then
  679. StripStr:='-Map '+maybequoted(ChangeFileExt(current_module.exefilename,'.map'));
  680. if create_smartlink_sections then
  681. GCSectionsStr:='--gc-sections';
  682. if not(cs_link_nolink in current_settings.globalswitches) then
  683. Message1(exec_i_linking,current_module.exefilename);
  684. { Write used files and libraries }
  685. WriteResponseFile();
  686. { Call linker }
  687. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  688. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  689. Replace(cmdstr,'$EXE',(maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename,preName)))));
  690. Replace(cmdstr,'$RES',(maybequoted(ScriptFixFileName(outputexedir+Info.ResName))));
  691. Replace(cmdstr,'$STATIC',StaticStr);
  692. Replace(cmdstr,'$STRIP',StripStr);
  693. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  694. Replace(cmdstr,'$MAP',MapStr);
  695. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  696. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  697. { Remove ReponseFile }
  698. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  699. DeleteFile(outputexedir+Info.ResName);
  700. { Post process }
  701. if success then
  702. begin
  703. success:=DoExec(FindUtil(utilsprefix + 'objcopy'), '-O binary '+
  704. ChangeFileExt(current_module.exefilename, preName) + ' ' +
  705. ChangeFileExt(current_module.exefilename, preName+target_info.exeext),
  706. true,false);
  707. end;
  708. if success and (apptype=app_arm9) then
  709. begin
  710. success:=DoExec(FindUtil('ndstool'), '-c ' +
  711. ChangeFileExt(current_module.exefilename, '.nds') + ' -9 ' +
  712. ChangeFileExt(current_module.exefilename, preName+target_info.exeext),
  713. true,false);
  714. end;
  715. MakeExecutable:=success; { otherwise a recursive call to link method }
  716. end;
  717. {*****************************************************************************
  718. Initialize
  719. *****************************************************************************}
  720. initialization
  721. RegisterLinker(ld_nds,TLinkerNDS);
  722. RegisterTarget(system_arm_nds_info);
  723. end.