1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     ro_SectionTable.h
4 
5   Copyright (C)2009-2012 Nintendo Co., Ltd.  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   $Rev: 46347 $
14  *---------------------------------------------------------------------------*/
15 
16 #ifndef NN_RO_RO_SECTIONTABLE_H_
17 #define NN_RO_RO_SECTIONTABLE_H_
18 
19 #include <nn/types.h>
20 #include <nn/util/util_SizedEnum.h>
21 #include <nn/drivers/ro/ro_Types.h>
22 #include <nn/ro/ro_Types.h>
23 
24 namespace nn { namespace ro { namespace detail {
25 
26 
27 
28     //---------------------------------------------------------------------------
29     //
30     //
31     //---------------------------------------------------------------------------
32     class SectionTable
33     {
34     private:
35         const nn::drivers::ro::SectionInfo*  m_pSectionTable;
36         s32                 m_NumSections;
37 
38     public:
39         //---------------------------------------------------------------------------
40         //
41         //
42         //
43         //---------------------------------------------------------------------------
SectionTable(const nn::drivers::ro::ObjectHeader * pModule)44         SectionTable(const nn::drivers::ro::ObjectHeader* pModule)
45             : m_pSectionTable(pModule->sectionInfo)
46             , m_NumSections(pModule->numSections)
47         {
48         }
49 
50         //---------------------------------------------------------------------------
51         //
52         //
53         //
54         //
55         //
56         //
57         //
58         //
59         //
60         //---------------------------------------------------------------------------
GetSection(int index)61         const nn::drivers::ro::SectionInfo* GetSection(int index) const
62         {
63             if( (0 <= index) && (index < m_NumSections) )
64             {
65                 return &m_pSectionTable[index];
66             }
67             else
68             {
69                 return NULL;
70             }
71         }
72 
73         //---------------------------------------------------------------------------
74         //
75         //
76         //
77         //
78         //
79         //
80         //
81         //
82         //
83         //
84         //
85         //
86         //
87         //---------------------------------------------------------------------------
GetAddress(int index,u32 offset)88         uptr GetAddress(int index, u32 offset) const
89         {
90             if( ! ((0 <= index) && (index < m_NumSections)) )
91             {
92                 return NULL;
93             }
94 
95             const nn::drivers::ro::SectionInfo& section = m_pSectionTable[index];
96 
97             if( ! (offset < section.size) )
98             {
99                 return NULL;
100             }
101 
102             return section.offset + offset;
103         }
104 
105         //---------------------------------------------------------------------------
106         //
107         //
108         //
109         //
110         //
111         //
112         //
113         //
114         //
115         //
116         //
117         //
118         //
119         //
120         //
121         //---------------------------------------------------------------------------
GetReferedAddress(int index,u32 offset)122         uptr GetReferedAddress(int index, u32 offset) const
123         {
124             if( ! ((0 <= index) && (index < m_NumSections)) )
125             {
126                 return NULL;
127             }
128 
129             const nn::drivers::ro::SectionInfo& section = m_pSectionTable[index];
130 
131             if( ! (offset <= section.size) )
132             {
133                 return NULL;
134             }
135 
136             return section.offset + offset;
137         }
138 
139         //---------------------------------------------------------------------------
140         //
141         //
142         //
143         //
144         //
145         //
146         //
147         //
148         //
149         //
150         //
151         //
152         //
153         //---------------------------------------------------------------------------
154         template<typename T>
GetPointer(int index,u32 offset)155         T GetPointer(int index, u32 offset) const
156         {
157             return reinterpret_cast<T>(GetAddress(index, offset));
158         }
159 
GetAddress(nn::drivers::ro::SectionAndOffset sao)160         uptr GetAddress(nn::drivers::ro::SectionAndOffset sao) const
161         {
162             return GetAddress(sao.GetFirst(), sao.GetSecond());
163         }
164 
165         template<typename T>
GetPointer(nn::drivers::ro::SectionAndOffset sao)166         T GetPointer(nn::drivers::ro::SectionAndOffset sao) const
167         {
168             return reinterpret_cast<T>(GetAddress(sao));
169         }
170     };
171 
172 
173 
174 }}}
175 
176 
177 
178 #endif  // ifndef NN_RO_RO_SECTIONTABLE_H_
179