t_nds.pas 31 KB

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