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.17 12/18/2006 06:23:39 srd_nisiwaki 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 RSOObjectHeader   RSOObjectHeader;
29 
30 typedef u32    RSOHash;
31 typedef struct RSOObjectList		RSOObjectList;
32 typedef struct RSOObjectLink		RSOObjectLink;
33 typedef struct RSOObjectInfo		RSOObjectInfo;
34 typedef struct RSOSymbolHeader		RSOSymbolHeader;
35 typedef struct RSOSectionInfo		RSOSectionInfo;
36 typedef struct RSOExportTable		RSOExportTable;
37 typedef struct RSOImportTable		RSOImportTable;
38 typedef struct RSORel			RSORel;
39 typedef struct RSOExportFuncTable       RSOExportFuncTable;
40 
41 #define RSO_NAME_ADR(moduleP)    (((RSOObjectHeader *)moduleP)->info.nameOffset)
42 #define RSO_NAME_SIZE(moduleP)   (((RSOObjectHeader *)moduleP)->info.nameSize)
43 
44 struct RSOExportFuncTable
45 {
46     const char*  symbol_name;
47     u32*  symbol_ptr;
48 };
49 
50 struct RSOObjectList
51 {
52     RSOObjectInfo*   head;
53     RSOObjectInfo*   tail;
54 };
55 
56 struct RSOObjectLink
57 {
58     RSOObjectInfo*   next;
59     RSOObjectInfo*   prev;
60 };
61 
62 struct RSOObjectInfo
63 {
64     RSOObjectLink   link;
65     u32             numSections;        // # of sections
66     u32             sectionInfoOffset;  // offset to section info table
67     u32             nameOffset;         // offset to module name
68     u32             nameSize;           // size of module name
69     u32             version;            // version number
70 };
71 
72 struct RSOSymbolHeader
73 {
74 	u32				tableOffset;		// offset to symbol table
75 	u32				tableSize;			// symbol table size
76 	u32				stringOffset;		// offset to symbol name table
77 };
78 
79 struct RSOObjectHeader
80 {
81     // CAUTION: info must be the 1st member
82     RSOObjectInfo    info;
83 
84     u32 bssSize;            // total size of bss sections in bytes
85     u8  prologSection;
86     u8  epilogSection;
87     u8  unresolvedSection;
88     u8  bssSection;
89     u32 prolog;
90     u32 epilog;
91     u32 unresolved;
92     // Data ordering (Be careful when changing)
93     // (Deletes the latter half data after linking module)
94     // Information necessary for referencing from outside
95     // expHeader.tableOffset -> expHeader.stringOffset ->
96     // Information necessary for referencing outside
97     // externalRelOffset�� impHeader.tableOffset -> impHeader.stringOffset ->
98     // Information necessary for internal referencing
99     // internalRelOffset
100 
101     // Internal referencing (self-contained)
102     u32 internalRelOffset;
103     u32 internalRelSize;
104 
105     // External referencing (reference other modules)
106     u32 externalRelOffset;
107     u32 externalRelSize;
108     // Information to be referenced from outside
109     RSOSymbolHeader     expHeader;
110     // Information referencing outside
111     RSOSymbolHeader     impHeader;
112 };
113 
114 #define RSOGetSectionInfo(module)    \
115         ((RSOSectionInfo*) (((RSOObjectInfo*) (module))->sectionInfoOffset))
116 
117 // fixed level
118 #define RSO_FL_NON              0
119 #define RSO_FL_INTERNAL         1       // Up to the internal reference information
120 #define RSO_FL_EXTERNAL         2       // Up to the information necessary for referencing outside
121 //#define RSO_FL_EXPORT           3       // Up to the information necessary for referencing from outside
122 
123 // RSO_FL_EXPORT includes RSO_FL_EXTERNAL and RSO_FL_INTERNAL
124 // RSO_FL_EXTERNAL includes RSO_FL_INTERNAL
125 
126 // Size of a single intermediate code export.
127 #define RSO_FAR_JUMP_SIZE     24
128 
129 /*---------------------------------------------------------------------------*
130   Name:         RSOSectionInfo
131 
132   Description :  Section information that corresponds to Elf32_Shdr.
133 
134   Members:      offset      Byte offset from the beginning of the file.
135 
136                             The OS_SECTIONINFO_EXEC bit is set for a
137                             section that contains executable machine
138                             instructions.
139 
140                             The offset can be zero for unused sections and
141                             also for BSS sections. BSS sections have non-
142                             zero size but unused sections do not.
143 
144                 size        Section size in bytes. For BSS sections, the
145                             sections occupy no space in the file.
146 
147   Note:         Unused sections are required to use same section indices
148                 as the original ELF file. (for debugging purpose)
149 
150  *---------------------------------------------------------------------------*/
151 struct RSOSectionInfo
152 {
153     u32             offset;
154     u32             size;
155 };
156 
157 //Use the following numbers for a static rso RSOExportTable.section
158 #define RSO_EXPSEC_INIT		1
159 #define RSO_EXPSEC_TEXT		2
160 #define RSO_EXPSEC_CTORS	3
161 #define RSO_EXPSEC_DTORS	4
162 #define RSO_EXPSEC_RODATA	5
163 #define RSO_EXPSEC_DATA		6
164 #define RSO_EXPSEC_BSS		7
165 #define RSO_EXPSEC_SDATA	8
166 #define RSO_EXPSEC_SDATA2	9
167 #define RSO_EXPSEC_SDATA0	10
168 #define RSO_EXPSEC_SBSS		11
169 #define RSO_EXPSEC_SBSS2	12
170 #define RSO_EXPSEC_SBSS0	13
171 #define RSO_EXPSEC_UNKNOWN  0xf1
172 
173 struct RSOExportTable
174 {
175     u32         strOffset;
176     u32         value;
177     u32         section;
178     RSOHash     hash;
179 };
180 
181 struct RSOImportTable
182 {
183     u32         strOffset;
184     u32         value;
185     u32         relOffset;
186 };
187 
188 struct RSORel
189 {
190     u32 offset;
191     u32 info;
192     u32 addend;
193 };
194 
195 //The lower 8 bit is 'type' (relocation type) is RSORel.info
196 //The upper 24 bit is 'section' (section number of referenced symbol) when internally referenced, and
197 //'importIndex' (index number of import symbol table) when externally referenced
198 //Use the below macro to translate. The operation of RSO_R_SECTION and RSO_R_IMPIDX is exactly the same.
199 #define RSO_R_SECTION(i)	((i)>>8)
200 #define RSO_R_IMPIDX(i)		((i)>>8)
201 #define RSO_R_TYPE(i)		((unsigned char)(i))
202 #define RSO_R_INFO(s,t)		(((s)<<8)+(unsigned char)(t))
203 
204 #define R_REVOLUTION_NOP			201     //  C9h current offset += OSRel.offset
205 #define R_REVOLUTION_SECTION		202     //  CAh current section = OSRel.section
206 #define R_REVOLUTION_END			203     //  CBh
207 #define R_REVOLUTION_MRKREF			204     //  CCh
208 
209 #ifdef __cplusplus
210 }
211 #endif
212 
213 #endif  // __OSMODULE_H__
214