t_nds.pas 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  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('');
  364. add(' .dtcm __dtcm_start : AT (__dtcm_lma)');
  365. add(' {');
  366. add(' *(.dtcm)');
  367. add(' *(.dtcm.*)');
  368. add(' . = ALIGN(4);');
  369. add(' __dtcm_end = ABSOLUTE(.);');
  370. add(' } >dtcm = 0xff');
  371. add('');
  372. add('');
  373. add(' __itcm_lma = __dtcm_lma + SIZEOF(.dtcm);');
  374. add('');
  375. add(' .itcm __itcm_start : AT (__itcm_lma)');
  376. add(' {');
  377. add(' *(.itcm)');
  378. add(' *itcm.*(.text)');
  379. add(' . = ALIGN(4);');
  380. add(' __itcm_end = ABSOLUTE(.);');
  381. add(' } >itcm = 0xff');
  382. add('');
  383. add(' .sbss __dtcm_end : ');
  384. add(' {');
  385. add(' __sbss_start = ABSOLUTE(.);');
  386. add(' __sbss_start__ = ABSOLUTE(.);');
  387. add(' *(.sbss)');
  388. add(' . = ALIGN(4); /* REQUIRED. LD is flaky without it. */');
  389. add(' __sbss_end = ABSOLUTE(.);');
  390. add(' } >dtcm');
  391. add('');
  392. add('');
  393. add('');
  394. add(' __bss_lma = __itcm_lma + SIZEOF(.itcm) ;');
  395. add(' __appended_data = __itcm_lma + SIZEOF(.itcm) ;');
  396. add(' .bss __bss_lma : AT (__bss_lma)');
  397. add(' {');
  398. add(' __bss_start = ABSOLUTE(.);');
  399. add(' __bss_start__ = ABSOLUTE(.);');
  400. add(' *(.dynbss)');
  401. add(' *(.gnu.linkonce.b*)');
  402. add(' *(.bss*)');
  403. add(' *(COMMON)');
  404. add(' . = ALIGN(4); /* REQUIRED. LD is flaky without it. */');
  405. add(' __bss_end = ABSOLUTE(.) ;');
  406. add(' __bss_end__ = __bss_end ;');
  407. add(' } >ewram');
  408. add('');
  409. add('');
  410. add(' _end = . ;');
  411. add(' __end__ = . ;');
  412. add(' PROVIDE (end = _end);');
  413. add('');
  414. add('');
  415. add('');
  416. add(' /* Stabs debugging sections. */');
  417. add(' .stab 0 : { *(.stab) }');
  418. add(' .stabstr 0 : { *(.stabstr) }');
  419. add(' .stab.excl 0 : { *(.stab.excl) }');
  420. add(' .stab.exclstr 0 : { *(.stab.exclstr) }');
  421. add(' .stab.index 0 : { *(.stab.index) }');
  422. add(' .stab.indexstr 0 : { *(.stab.indexstr) }');
  423. add(' .comment 0 : { *(.comment) }');
  424. add(' /* DWARF debug sections.');
  425. add(' Symbols in the DWARF debugging sections are relative to the beginning');
  426. add(' of the section so we begin them at 0. */');
  427. add(' /* DWARF 1 */');
  428. add(' .debug 0 : { *(.debug) }');
  429. add(' .line 0 : { *(.line) }');
  430. add(' /* GNU DWARF 1 extensions */');
  431. add(' .debug_srcinfo 0 : { *(.debug_srcinfo) }');
  432. add(' .debug_sfnames 0 : { *(.debug_sfnames) }');
  433. add(' /* DWARF 1.1 and DWARF 2 */');
  434. add(' .debug_aranges 0 : { *(.debug_aranges) }');
  435. add(' .debug_pubnames 0 : { *(.debug_pubnames) }');
  436. add(' /* DWARF 2 */');
  437. add(' .debug_info 0 : { *(.debug_info) }');
  438. add(' .debug_abbrev 0 : { *(.debug_abbrev) }');
  439. add(' .debug_line 0 : { *(.debug_line) }');
  440. add(' .debug_frame 0 : { *(.debug_frame) }');
  441. add(' .debug_str 0 : { *(.debug_str) }');
  442. add(' .debug_loc 0 : { *(.debug_loc) }');
  443. add(' .debug_macinfo 0 : { *(.debug_macinfo) }');
  444. add(' /* SGI/MIPS DWARF 2 extensions */');
  445. add(' .debug_weaknames 0 : { *(.debug_weaknames) }');
  446. add(' .debug_funcnames 0 : { *(.debug_funcnames) }');
  447. add(' .debug_typenames 0 : { *(.debug_typenames) }');
  448. add(' .debug_varnames 0 : { *(.debug_varnames) }');
  449. add(' .stack 0x80000 : { _stack = .; *(.stack) }');
  450. add(' /* These must appear regardless of . */');
  451. add('}');
  452. end;
  453. if apptype=app_arm7 then
  454. begin
  455. add('OUTPUT_FORMAT("elf32-littlearm", "elf32-bigarm", "elf32-littlearm")');
  456. add('OUTPUT_ARCH(arm)');
  457. add('ENTRY(_start)');
  458. add('');
  459. add('MEMORY {');
  460. add('');
  461. add(' rom : ORIGIN = 0x08000000, LENGTH = 32M');
  462. add(' iwram : ORIGIN = 0x037f8000, LENGTH = 96K');
  463. add('}');
  464. add('');
  465. add('__iwram_start = ORIGIN(iwram);');
  466. add('__iwram_top = ORIGIN(iwram)+ LENGTH(iwram);');
  467. add('__sp_irq = __iwram_top - 0x60;');
  468. add('__sp_svc = __sp_irq - 0x100;');
  469. add('__sp_usr = __sp_svc - 0x100;');
  470. add('');
  471. add('__irq_flags = __iwram_top - 8;');
  472. add('__irq_vector = __iwram_top - 4;');
  473. add('');
  474. add('SECTIONS');
  475. add('{');
  476. add(' .init :');
  477. add(' {');
  478. add(' __text_start = . ;');
  479. add(' KEEP (*(.init))');
  480. add(' . = ALIGN(4); /* REQUIRED. LD is flaky without it. */');
  481. add(' } >iwram = 0xff');
  482. add(' .plt : { *(.plt) } >iwram = 0xff');
  483. add('');
  484. add(' .text : /* ALIGN (4): */');
  485. add(' {');
  486. add('');
  487. add(' *(.text.*)');
  488. add(' *(.stub)');
  489. add(' /* .gnu.warning sections are handled specially by elf32.em. */');
  490. add(' *(.gnu.warning)');
  491. add(' *(.gnu.linkonce.t*)');
  492. add(' *(.glue_7)');
  493. add(' *(.glue_7t)');
  494. add(' . = ALIGN(4); /* REQUIRED. LD is flaky without it. */');
  495. add(' } >iwram = 0xff');
  496. add('');
  497. add(' .fini :');
  498. add(' {');
  499. add(' KEEP (*(.fini))');
  500. add(' } >iwram =0xff');
  501. add('');
  502. add(' __text_end = . ;');
  503. add('');
  504. add(' .rodata :');
  505. add(' {');
  506. add(' *(.rodata)');
  507. add(' *all.rodata*(*)');
  508. add(' *(.roda)');
  509. add(' *(.rodata.*)');
  510. add(' *(.gnu.linkonce.r*)');
  511. add(' SORT(CONSTRUCTORS)');
  512. add(' . = ALIGN(4); /* REQUIRED. LD is flaky without it. */');
  513. add(' } >iwram = 0xff');
  514. add('');
  515. add(' .ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) } >iwram');
  516. add(' __exidx_start = .;');
  517. add(' .ARM.exidx : { *(.ARM.exidx* .gnu.linkonce.armexidx.*) } >iwram');
  518. add(' __exidx_end = .;');
  519. add('');
  520. add('/* Ensure the __preinit_array_start label is properly aligned. We');
  521. add(' could instead move the label definition inside the section, but');
  522. add(' the linker would then create the section even if it turns out to');
  523. add(' be empty, which isn''t pretty. */');
  524. add(' . = ALIGN(32 / 8);');
  525. add(' PROVIDE (__preinit_array_start = .);');
  526. add(' .preinit_array : { KEEP (*(.preinit_array)) } >iwram = 0xff');
  527. add(' PROVIDE (__preinit_array_end = .);');
  528. add(' PROVIDE (__init_array_start = .);');
  529. add(' .init_array : { KEEP (*(.init_array)) } >iwram = 0xff');
  530. add(' PROVIDE (__init_array_end = .);');
  531. add(' PROVIDE (__fini_array_start = .);');
  532. add(' .fini_array : { KEEP (*(.fini_array)) } >iwram = 0xff');
  533. add(' PROVIDE (__fini_array_end = .);');
  534. add('');
  535. add(' .ctors :');
  536. add(' {');
  537. add(' /* gcc uses crtbegin.o to find the start of the constructors, so');
  538. add(' we make sure it is first. Because this is a wildcard, it');
  539. add(' doesn''t matter if the user does not actually link against');
  540. add(' crtbegin.o; the linker won''t look for a file to match a');
  541. add(' wildcard. The wildcard also means that it doesn''t matter which');
  542. add(' directory crtbegin.o is in. */');
  543. add(' KEEP (*crtbegin.o(.ctors))');
  544. add(' KEEP (*(EXCLUDE_FILE (*crtend.o) .ctors))');
  545. add(' KEEP (*(SORT(.ctors.*)))');
  546. add(' KEEP (*(.ctors))');
  547. add(' . = ALIGN(4); /* REQUIRED. LD is flaky without it. */');
  548. add(' } >iwram = 0xff');
  549. add('');
  550. add(' .dtors :');
  551. add(' {');
  552. add(' KEEP (*crtbegin.o(.dtors))');
  553. add(' KEEP (*(EXCLUDE_FILE (*crtend.o) .dtors))');
  554. add(' KEEP (*(SORT(.dtors.*)))');
  555. add(' KEEP (*(.dtors))');
  556. add(' . = ALIGN(4); /* REQUIRED. LD is flaky without it. */');
  557. add(' } >iwram = 0xff');
  558. add('');
  559. add(' .eh_frame :');
  560. add(' {');
  561. add(' KEEP (*(.eh_frame))');
  562. add(' . = ALIGN(4); /* REQUIRED. LD is flaky without it. */');
  563. add(' } >iwram = 0xff');
  564. add('');
  565. add(' .gcc_except_table :');
  566. add(' {');
  567. add(' *(.gcc_except_table)');
  568. add(' . = ALIGN(4); /* REQUIRED. LD is flaky without it. */');
  569. add(' } >iwram = 0xff');
  570. add(' .jcr : { KEEP (*(.jcr)) } >iwram = 0');
  571. add(' .got : { *(.got.plt) *(.got) } >iwram = 0');
  572. add('');
  573. add('');
  574. add(' .iwram ALIGN(4) :');
  575. add(' {');
  576. add(' __iwram_start = ABSOLUTE(.) ;');
  577. add(' *(.iwram)');
  578. add(' *iwram.*(.text)');
  579. add(' . = ALIGN(4); /* REQUIRED. LD is flaky without it. */');
  580. add(' __iwram_end = ABSOLUTE(.) ;');
  581. add(' } >iwram = 0xff');
  582. add('');
  583. add('');
  584. add(' .data ALIGN(4) : {');
  585. add(' __data_start = ABSOLUTE(.);');
  586. add(' *(.data)');
  587. add(' *(.data.*)');
  588. add(' *(.gnu.linkonce.d*)');
  589. add(' *(.fpc*)');
  590. add(' CONSTRUCTORS');
  591. add(' . = ALIGN(4);');
  592. add(' __data_end = ABSOLUTE(.) ;');
  593. add(' } >iwram = 0xff');
  594. add('');
  595. add('');
  596. add('');
  597. add(' .bss ALIGN(4) :');
  598. add(' {');
  599. add(' __bss_start = ABSOLUTE(.);');
  600. add(' __bss_start__ = ABSOLUTE(.);');
  601. add(' *(.dynbss)');
  602. add(' *(.gnu.linkonce.b*)');
  603. add(' *(.bss*)');
  604. add(' *(COMMON)');
  605. add(' . = ALIGN(4); /* REQUIRED. LD is flaky without it. */');
  606. add(' } >iwram');
  607. add('');
  608. add(' __bss_end = . ;');
  609. add(' __bss_end__ = . ;');
  610. add('');
  611. add(' _end = . ;');
  612. add(' __end__ = . ;');
  613. add(' PROVIDE (end = _end);');
  614. add('');
  615. add(' /* Stabs debugging sections. */');
  616. add(' .stab 0 : { *(.stab) }');
  617. add(' .stabstr 0 : { *(.stabstr) }');
  618. add(' .stab.excl 0 : { *(.stab.excl) }');
  619. add(' .stab.exclstr 0 : { *(.stab.exclstr) }');
  620. add(' .stab.index 0 : { *(.stab.index) }');
  621. add(' .stab.indexstr 0 : { *(.stab.indexstr) }');
  622. add(' .comment 0 : { *(.comment) }');
  623. add(' /* DWARF debug sections.');
  624. add(' Symbols in the DWARF debugging sections are relative to the beginning');
  625. add(' of the section so we begin them at 0. */');
  626. add(' /* DWARF 1 */');
  627. add(' .debug 0 : { *(.debug) }');
  628. add(' .line 0 : { *(.line) }');
  629. add(' /* GNU DWARF 1 extensions */');
  630. add(' .debug_srcinfo 0 : { *(.debug_srcinfo) }');
  631. add(' .debug_sfnames 0 : { *(.debug_sfnames) }');
  632. add(' /* DWARF 1.1 and DWARF 2 */');
  633. add(' .debug_aranges 0 : { *(.debug_aranges) }');
  634. add(' .debug_pubnames 0 : { *(.debug_pubnames) }');
  635. add(' /* DWARF 2 */');
  636. add(' .debug_info 0 : { *(.debug_info) }');
  637. add(' .debug_abbrev 0 : { *(.debug_abbrev) }');
  638. add(' .debug_line 0 : { *(.debug_line) }');
  639. add(' .debug_frame 0 : { *(.debug_frame) }');
  640. add(' .debug_str 0 : { *(.debug_str) }');
  641. add(' .debug_loc 0 : { *(.debug_loc) }');
  642. add(' .debug_macinfo 0 : { *(.debug_macinfo) }');
  643. add(' /* SGI/MIPS DWARF 2 extensions */');
  644. add(' .debug_weaknames 0 : { *(.debug_weaknames) }');
  645. add(' .debug_funcnames 0 : { *(.debug_funcnames) }');
  646. add(' .debug_typenames 0 : { *(.debug_typenames) }');
  647. add(' .debug_varnames 0 : { *(.debug_varnames) }');
  648. add(' .stack 0x80000 : { _stack = .; *(.stack) }');
  649. add(' /* These must appear regardless of . */');
  650. add('}');
  651. end;
  652. end;
  653. { Write and Close response }
  654. linkres.writetodisk;
  655. linkres.free;
  656. WriteResponseFile:=True;
  657. end;
  658. function TLinkerNDS.MakeExecutable:boolean;
  659. var
  660. binstr,
  661. cmdstr : TCmdStr;
  662. success : boolean;
  663. StaticStr,
  664. GCSectionsStr,
  665. DynLinkStr,
  666. StripStr: string;
  667. preName: string;
  668. begin
  669. { for future use }
  670. StaticStr:='';
  671. StripStr:='';
  672. DynLinkStr:='';
  673. case apptype of
  674. app_arm9: preName:='.nef';
  675. app_arm7: preName:='.nlf';
  676. end;
  677. GCSectionsStr:='--gc-sections';
  678. if not(cs_link_nolink in current_settings.globalswitches) then
  679. Message1(exec_i_linking,current_module.exefilename^);
  680. { Write used files and libraries }
  681. WriteResponseFile();
  682. { Call linker }
  683. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  684. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  685. Replace(cmdstr,'$EXE',(maybequoted(ScriptFixFileName(ChangeFileExt(current_module.exefilename^,preName)))));
  686. Replace(cmdstr,'$RES',(maybequoted(ScriptFixFileName(outputexedir+Info.ResName))));
  687. Replace(cmdstr,'$STATIC',StaticStr);
  688. Replace(cmdstr,'$STRIP',StripStr);
  689. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  690. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  691. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  692. { Remove ReponseFile }
  693. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  694. DeleteFile(outputexedir+Info.ResName);
  695. { Post process }
  696. if success then
  697. begin
  698. success:=DoExec(FindUtil(utilsprefix + 'objcopy'), '-O binary '+
  699. ChangeFileExt(current_module.exefilename^, preName) + ' ' +
  700. ChangeFileExt(current_module.exefilename^, preName+target_info.exeext),
  701. true,false);
  702. end;
  703. if success and (apptype=app_arm9) then
  704. begin
  705. success:=DoExec(FindUtil('ndstool'), '-c ' +
  706. ChangeFileExt(current_module.exefilename^, '.nds') + ' -9 ' +
  707. ChangeFileExt(current_module.exefilename^, preName+target_info.exeext),
  708. true,false);
  709. end;
  710. MakeExecutable:=success; { otherwise a recursive call to link method }
  711. end;
  712. {*****************************************************************************
  713. Initialize
  714. *****************************************************************************}
  715. initialization
  716. RegisterExternalLinker(system_arm_nds_info,TLinkerNDS);
  717. RegisterTarget(system_arm_nds_info);
  718. end.