1 /*---------------------------------------------------------------------------*
2   Project:  RSO
3   File:     rso.h
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: rso.h,v 1.2 2008/06/13 04:48:28 mitu Exp $
14 
15  *---------------------------------------------------------------------------*/
16 
17 #ifndef __RSO_H__
18 #define __RSO_H__
19 
20 #include <revolution/types.h>
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 #define RSO_VERSION       1
27 //version 0.10
28 typedef struct RSOExportFuncTable       RSOExportFuncTable;
29 typedef struct RSOExportTable		RSOExportTable;
30 typedef struct RSOImportTable		RSOImportTable;
31 typedef struct RSOSymbolHeader		RSOSymbolHeader;
32 typedef struct RSOObjectLink            RSOObjectLink;
33 typedef struct RSOObjectInfo            RSOObjectInfo;
34 typedef struct RSOObjectHeader          RSOObjectHeader;
35 typedef u32    RSOHash;
36 
37 struct RSOExportFuncTable
38 {
39     const char*  symbol_name;
40     u32*  symbol_ptr;
41 };
42 
43 struct RSOExportTable
44 {
45     u32         strOffset;
46     u32         value;
47     u32         section;
48     RSOHash     hash;
49 };
50 
51 struct RSOImportTable
52 {
53     u32         strOffset;
54     u32         value;
55     u32         relOffset;
56 };
57 
58 struct RSOSymbolHeader
59 {
60 	u32				tableOffset;		// Offset to symbol table
61 	u32				tableSize;			// Symbol table size
62 	u32				stringOffset;		// Offset to symbol name table
63 };
64 
65 struct RSOObjectLink
66 {
67     RSOObjectInfo*   next;
68     RSOObjectInfo*   prev;
69 };
70 
71 struct RSOObjectInfo
72 {
73     RSOObjectLink   link;
74     u32             numSections;        // # of sections
75     u32             sectionInfoOffset;  // Offset to section info table
76     u32             nameOffset;         // Offset to module name
77     u32             nameSize;           // Size of module name
78     u32             version;            // Version number
79 };
80 
81 struct RSOObjectHeader
82 {
83     // CAUTION: Info must be the 1st member
84     RSOObjectInfo    info;
85 
86     u32 bssSize;            // Total size of bss sections in bytes
87     u8  prologSection;
88     u8  epilogSection;
89     u8  unresolvedSection;
90     u8  bssSection;
91     u32 prolog;
92     u32 epilog;
93     u32 unresolved;
94     // Data ordering (Be careful when changing)
95     // (Deletes the latter half data after linking module)
96     // Information necessary for referencing from outside
97     // expHeader.tableOffset -> expHeader.stringOffset ->
98     // Information necessary for referencing outside
99     // externalRelOffset -> impHeader.tableOffset -> impHeader.stringOffset ->
100     // Information necessary for internal referencing
101     // internalRelOffset
102 
103     // Internal referencing (self-contained)
104     u32 internalRelOffset;
105     u32 internalRelSize;
106 
107     // External referencing (reference other modules)
108     u32 externalRelOffset;
109     u32 externalRelSize;
110     // Information to be referenced from outside
111     RSOSymbolHeader     expHeader;
112     // Information referencing outside
113     RSOSymbolHeader     impHeader;
114 };
115 
116 // Fixed level
117 typedef enum {
118     RSO_FL_NON = 0,
119     RSO_FL_INTERNAL,    // Up to the internal reference information
120     RSO_FL_EXTERNAL     // Up to the information necessary for referencing outside
121 } RSOFixedLevel;
122 
123 // RSO_FL_EXTERNAL includes RSO_FL_INTERNAL
124 
125 // Size of a single intermediate code export.
126 #define RSO_FAR_JUMP_SIZE     24
127 
128 /*---------------------------------------------------------------------------*
129    RSOObject
130  *---------------------------------------------------------------------------*/
131 RSOImportTable*         RSOGetImport                    ( const RSOSymbolHeader* imp );
132 const RSOExportTable*   RSOGetExport                    ( const RSOSymbolHeader* exp );
133 BOOL                    RSOLocateObject                 ( void* newModule, void* bss );
134 BOOL                    RSOLocateObjectFixed            (void* newModule,void* bss);
135 BOOL                    RSOStaticLocateObject           ( void* newModule );
136 BOOL                    RSOUnLocateObject               ( void* oldModule );
137 int                     RSOLink                         ( RSOObjectHeader* rsoImp, const RSOObjectHeader* rsoExp );
138 void                    RSOUnLink                      ( RSOObjectHeader* rsoImp, const RSOObjectHeader* rsoExp );
139 RSOHash                 RSOGetHash                      ( const char * symbolname );
140 /*---------------------------------------------------------------------------*
141    RSOImport
142  *---------------------------------------------------------------------------*/
143 BOOL                RSOIsImportSymbolResolved       ( const RSOObjectHeader* rso, int index );
144 BOOL                RSOIsImportSymbolResolvedAll    ( const RSOObjectHeader* rso );
145 int                 RSOGetNumImportSymbols          ( const RSOSymbolHeader* imp );
146 const char*         RSOGetImportSymbolName          ( const RSOSymbolHeader* imp, int index );
147 
148 int                 RSOGetNumImportSymbolsUnresolved( const RSOObjectHeader* rso );
149 
150 /*---------------------------------------------------------------------------*
151    RSOExport
152  *---------------------------------------------------------------------------*/
153 int                 RSOGetNumExportSymbols          ( const RSOSymbolHeader* exp );
154 const void*         RSOFindExportSymbolAddr         ( const RSOObjectHeader* rso, const char* name );
155 RSOExportTable*     RSOFindExportSymbol             ( const RSOObjectHeader* rso, const char* name );
156 const char*         RSOGetExportSymbolName          ( const RSOSymbolHeader* exp, int index );
157 const void*         RSOGetExportSymbolAddr          ( const RSOObjectHeader* rso, int index );
158 
159 /*---------------------------------------------------------------------------*
160    RSOLinkList
161  *---------------------------------------------------------------------------*/
162 BOOL                RSOListInit                    (void* i_staticRso);
163 BOOL                RSOLinkList                    (void* i_newRso, void* i_bss);
164 BOOL                RSOLinkListFixed               (void* i_newRso, void* i_bss,RSOFixedLevel i_fixed);
165 BOOL                RSOUnLinkList                  (void* i_oldRso);
166 
167 u32                 RSOGetFixedSize                 (void *i_rso,RSOFixedLevel i_fixed_level);
168 /*---------------------------------------------------------------------------*
169    RSOLinkFar
170  *---------------------------------------------------------------------------*/
171 int RSOGetFarCodeSize(const RSOObjectHeader* rsoImp, const RSOObjectHeader* rsoExp);
172 int RSOLinkFar(RSOObjectHeader* rsoImp, const RSOObjectHeader* rsoExp,void *buff);
173 /*---------------------------------------------------------------------------*
174    RSOLinkJump
175  *---------------------------------------------------------------------------*/
176 int RSOGetJumpCodeSize(const RSOObjectHeader *i_rsoRso);
177 void RSOMakeJumpCode(const RSOObjectHeader *i_rsoExp,void *i_buff);
178 int RSOLinkJump(RSOObjectHeader* i_rsoImp,const RSOObjectHeader* i_rsoExp,void *i_buff);
179 
180 #ifdef __cplusplus
181 }
182 #endif
183 
184 #endif  // __OSMODULE_H__
185