1 /*---------------------------------------------------------------------------*
2   Project:  rsodemo
3   File:     static.c
4 
5   Copyright 2006 Nintendo. All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law. They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   @version $Id: static.c,v 1.2 2008/06/13 04:43:46 mitu Exp $
14 
15   $NoKeywords: $
16  *---------------------------------------------------------------------------*/
17 
18 #include <revolution.h>
19 
20 #include <revolution/rso.h>
21 
22 
23 #ifdef _DEBUG
24 #define MODULE_D     "/dD.rso"
25 #define STATIC_RSO   "/staticD.sel"
26 #else
27 #define MODULE_D     "/d.rso"
28 #define STATIC_RSO   "/static.sel"
29 #endif
30 
31 #define DLL_EXPORT
32 #define DLL_SYMBOL(s) (*s)
33 
34 #include "d.h"
35 
36 #undef DLL_EXPORT
37 #undef DLL_SYMBOL
38 
39 // File created using makeinc.exe from d.h.
40 // % makeinc.exe -o d.inc -l moduleD d.h
41 #include "d.inc"
42 
43 //
44 void _unresolved();
45 //
46 extern void sub(void);
47 
48 /*---------------------------------------------------------------------------*
49    Load
50  *---------------------------------------------------------------------------*/
51 // Load and link the specified module
RsoLoad(char * moduleName)52 static RSOObjectHeader* RsoLoad(char* moduleName)
53 {
54     DVDFileInfo     fileInfo;
55     s32             length;
56     BOOL            result;
57     RSOObjectHeader* module;
58     u8*             bss;
59 
60     result = DVDOpen(moduleName, &fileInfo);
61     if (!result)
62         return NULL;
63     length = (s32) OSRoundUp32B(DVDGetLength(&fileInfo));
64     module = OSAllocFromArenaLo((u32) length, 32);
65     result = DVDRead(&fileInfo, module, length, 0);
66     if (!result)
67         return NULL;
68     DVDClose(&fileInfo);
69 
70     if (module->bssSize > 0)
71     {
72         bss = OSAllocFromArenaLo(module->bssSize, 32); // Alloc bss area
73     }
74     RSOLinkList(module, bss);
75 
76     return module;
77 }
78 
79 // Load and link the static module
StaticRsoLoad(char * moduleName)80 static RSOObjectHeader* StaticRsoLoad(char* moduleName)
81 {
82     DVDFileInfo     fileInfo;
83     s32             length;
84     BOOL            result;
85     RSOObjectHeader* module;
86 
87     result = DVDOpen(moduleName, &fileInfo);
88     if (!result)
89         return NULL;
90     length = (s32) OSRoundUp32B(DVDGetLength(&fileInfo));
91     module = OSAllocFromArenaLo((u32) length, 32);
92     result = DVDRead(&fileInfo, module, length, 0);
93     if (!result)
94         return NULL;
95     DVDClose(&fileInfo);
96 
97     RSOListInit(module);
98 
99     return module;
100 }
101 
102 //
_unresolved()103 void _unresolved()
104 {
105     OSReport("_unresolved func.\n");
106 }
107 
108 
109 //Main
main(void)110 int main(void)
111 {
112     RSOObjectHeader* staticRso;
113     RSOObjectHeader* moduleD;
114 
115     // Generate weak symbol 'InlineFunc'
116     DVDInit();
117 
118     //Read
119     // Load static rso file
120     OSReport("Loading static rso...\n");
121     staticRso = StaticRsoLoad(STATIC_RSO);
122     if (!staticRso)
123         return 1;
124 
125     // Load and link module D
126     OSReport("Linking module D...\n");
127     moduleD = RsoLoad(MODULE_D);
128     if (!moduleD) {
129         return 1;
130     }
131 
132     //Check whether everything has been resolved
133     if (RSOIsImportSymbolResolvedAll(moduleD))
134     {
135         OSReport("moduleD's ImportSymbol is resolved all.\n");
136     }
137 
138     //
139     OSReport("\nD prolog()\n");
140     ((u32 (*)(void)) moduleD->prolog)();
141 
142     OSReport("\n-- ResolvedModule_d --\n");
143     //
144     ResolvedModule_moduleD(moduleD);
145     //
146     FuncD_1(1);
147     FuncD_2(2,3);
148     OSReport("g_intD = %d\n",*g_intD);
149     //
150     sub();
151 
152     //
153     OSReport("\nD epilog()\n");
154     ((u32 (*)(void)) moduleD->epilog)();
155     RSOUnLinkList(moduleD);
156     UnresolvedModule_moduleD();
157     //
158     OSReport("\nRSOLink finish all!\n");
159 
160     return 0;
161 }
162