1 | // Licensed to the .NET Foundation under one or more agreements. |
---|---|
2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | // See the LICENSE file in the project root for more information. |
4 | |
5 | |
6 | #ifndef _NIDUMP_INL_ |
7 | #define _NIDUMP_INL_ |
8 | template<typename T> |
9 | TADDR NativeImageDumper::DPtrToPreferredAddr( T ptr ) |
10 | { |
11 | TADDR tptr = PTR_TO_TADDR(ptr); |
12 | return DataPtrToDisplay(tptr); |
13 | } |
14 | |
15 | inline TADDR NativeImageDumper::RvaToDisplay( SIZE_T rva ) |
16 | { |
17 | return DataPtrToDisplay(m_decoder.GetRvaData((RVA)rva)); |
18 | } |
19 | inline TADDR NativeImageDumper::DataPtrToDisplay(TADDR ptr) |
20 | { |
21 | if( ptr == NULL || ptr == (TADDR)-1 |
22 | || CheckOptions(CLRNATIVEIMAGE_DISABLE_REBASING) ) |
23 | return TO_TADDR(ptr); |
24 | |
25 | if( isInRange(ptr) || m_dependencies == NULL ) |
26 | { |
27 | //fast path in case the dependencies aren't loaded. |
28 | RVA rva = m_decoder.GetDataRva(ptr); |
29 | if (CheckOptions(CLRNATIVEIMAGE_FILE_OFFSET)) |
30 | return (TADDR) m_decoder.RvaToOffset(rva); |
31 | else |
32 | return rva + (INT_PTR)m_decoder.GetNativePreferredBase(); |
33 | } |
34 | if( m_mscorwksBase <= ptr && ptr < (m_mscorwksBase + m_mscorwksSize) ) |
35 | { |
36 | return ptr - m_mscorwksBase + m_mscorwksPreferred; |
37 | } |
38 | for( COUNT_T i = 0; i < m_numDependencies; ++i ) |
39 | { |
40 | const Dependency * dependency = &m_dependencies[i]; |
41 | if( dependency->pPreferredBase == NULL ) |
42 | continue; |
43 | if( dependency->pLoadedAddress <= ptr |
44 | && ((dependency->pLoadedAddress + dependency->size) > ptr) ) |
45 | { |
46 | //found the right target |
47 | return ptr - (INT_PTR)dependency->pLoadedAddress |
48 | + (INT_PTR)dependency->pPreferredBase; |
49 | } |
50 | } |
51 | return ptr; |
52 | } |
53 | inline int NativeImageDumper::CheckOptions( CLRNativeImageDumpOptions opt ) |
54 | { |
55 | //if( opt == ((CLRNativeImageDumpOptions)~0) ) |
56 | //return 1; |
57 | return (m_dumpOptions & opt) != 0; |
58 | } |
59 | |
60 | |
61 | #if 0 |
62 | PTR_CCOR_SIGNATURE |
63 | NativeImageDumper::metadataToHostDAC( PCCOR_SIGNATURE pSig, |
64 | IMetaDataImport2 * import) |
65 | { |
66 | TADDR tsig = TO_TADDR(pSig); |
67 | if( m_MetadataSize == 0 ) //assume target |
68 | return PTR_CCOR_SIGNATURE(tsig); |
69 | |
70 | //find the dependency for this import |
71 | const Dependency * dependency = NULL; |
72 | for( COUNT_T i = 0; i < m_numDependencies; ++i ) |
73 | { |
74 | if( m_dependencies[i].pImport == import ) |
75 | { |
76 | dependency = &m_dependencies[i]; |
77 | break; |
78 | } |
79 | } |
80 | if( dependency != NULL && dependency->pMetadataStartHost <= tsig |
81 | && tsig < (dependency->pMetadataStartHost |
82 | + dependency->MetadataSize) ) |
83 | { |
84 | //host metadata pointer |
85 | return PTR_CCOR_SIGNATURE((tsig |
86 | - dependency->pMetadataStartHost) |
87 | + dependency->pMetadataStartTarget ); |
88 | } |
89 | return PTR_CCOR_SIGNATURE(tsig); |
90 | } |
91 | #endif |
92 | template<typename T> |
93 | DPTR(T) |
94 | NativeImageDumper::metadataToHostDAC( T * pSig, |
95 | IMetaDataImport2 * import) |
96 | { |
97 | TADDR tsig = TO_TADDR(pSig); |
98 | if( m_MetadataSize == 0 ) //assume target |
99 | return DPTR(T)(tsig); |
100 | |
101 | //find the dependency for this import |
102 | const Dependency * dependency = NULL; |
103 | for( COUNT_T i = 0; i < m_numDependencies; ++i ) |
104 | { |
105 | if( m_dependencies[i].pImport == import ) |
106 | { |
107 | dependency = &m_dependencies[i]; |
108 | break; |
109 | } |
110 | } |
111 | if( dependency != NULL && dependency->pMetadataStartHost <= tsig |
112 | && tsig < (dependency->pMetadataStartHost |
113 | + dependency->MetadataSize) ) |
114 | { |
115 | //host metadata pointer |
116 | return DPTR(T)((tsig - dependency->pMetadataStartHost) |
117 | + dependency->pMetadataStartTarget ); |
118 | } |
119 | return DPTR(T)(tsig); |
120 | } |
121 | |
122 | inline SIZE_T NativeImageDumper::GetSectionAlignment() const { |
123 | _ASSERTE( m_sectionAlignment > 0 ); |
124 | return m_sectionAlignment; |
125 | } |
126 | #ifdef MANUAL_RELOCS |
127 | template< typename T > |
128 | inline T NativeImageDumper::RemapPointerForReloc( T ptr ) |
129 | { |
130 | return T(RemapTAddrForReloc(PTR_TO_TADDR(ptr))); |
131 | } |
132 | inline TADDR NativeImageDumper::RemapTAddrForReloc( TADDR ptr ) |
133 | { |
134 | #if 0 |
135 | if( NULL == ptr ) |
136 | return ptr; |
137 | if( isInRange(ptr) ) |
138 | return ptr; |
139 | const NativeImageDumper::Dependency * dependency = |
140 | GetDependencyForPointer(ptr); |
141 | _ASSERTE(dependency); |
142 | return RemapTAddrForReloc( dependency, ptr ); |
143 | #else |
144 | return ptr; |
145 | #endif |
146 | |
147 | } |
148 | template< typename T > |
149 | inline T |
150 | NativeImageDumper::RemapPointerForReloc(const NativeImageDumper::Dependency* dependency, |
151 | T ptr ) |
152 | { |
153 | return T(RemapTAddrForReloc( dependency, PTR_TO_TADDR(ptr) )); |
154 | } |
155 | inline TADDR |
156 | NativeImageDumper::RemapTAddrForReloc( const NativeImageDumper::Dependency * d, |
157 | TADDR ptr ) |
158 | { |
159 | #if 0 |
160 | if( d->pPreferredBase == d->pLoadedAddress ) |
161 | return ptr; |
162 | else |
163 | return (ptr - d->pPreferredBase) + d->pLoadedAddress; |
164 | #else |
165 | return ptr; |
166 | #endif |
167 | } |
168 | #endif //MANUAL_RELOCS |
169 | #endif //!_NIDUMP_INL_ |
170 |