xref: /aosp_15_r20/external/cronet/base/win/pe_image.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1*6777b538SAndroid Build Coastguard Worker // Copyright 2010 The Chromium Authors
2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file.
4*6777b538SAndroid Build Coastguard Worker 
5*6777b538SAndroid Build Coastguard Worker // This file was adapted from GreenBorder's Code.
6*6777b538SAndroid Build Coastguard Worker // To understand what this class is about (for other than well known functions
7*6777b538SAndroid Build Coastguard Worker // as GetProcAddress), a good starting point is "An In-Depth Look into the
8*6777b538SAndroid Build Coastguard Worker // Win32 Portable Executable File Format" by Matt Pietrek:
9*6777b538SAndroid Build Coastguard Worker // http://msdn.microsoft.com/msdnmag/issues/02/02/PE/default.aspx
10*6777b538SAndroid Build Coastguard Worker 
11*6777b538SAndroid Build Coastguard Worker #ifndef BASE_WIN_PE_IMAGE_H_
12*6777b538SAndroid Build Coastguard Worker #define BASE_WIN_PE_IMAGE_H_
13*6777b538SAndroid Build Coastguard Worker 
14*6777b538SAndroid Build Coastguard Worker #include <windows.h>
15*6777b538SAndroid Build Coastguard Worker 
16*6777b538SAndroid Build Coastguard Worker #include <delayimp.h>
17*6777b538SAndroid Build Coastguard Worker #include <stdint.h>
18*6777b538SAndroid Build Coastguard Worker 
19*6777b538SAndroid Build Coastguard Worker namespace base {
20*6777b538SAndroid Build Coastguard Worker namespace win {
21*6777b538SAndroid Build Coastguard Worker 
22*6777b538SAndroid Build Coastguard Worker // This class is a wrapper for the Portable Executable File Format (PE).
23*6777b538SAndroid Build Coastguard Worker // Its main purpose is to provide an easy way to work with imports and exports
24*6777b538SAndroid Build Coastguard Worker // from a file, mapped in memory as image. A PEImage object is constructed from
25*6777b538SAndroid Build Coastguard Worker // a loaded PE file by passing the HMODULE to the constructor. Loading a PE file
26*6777b538SAndroid Build Coastguard Worker // as an image will execute code and should only be done with trusted images.
27*6777b538SAndroid Build Coastguard Worker // Parsing of untrusted PE files is better done with PeImageReader.
28*6777b538SAndroid Build Coastguard Worker // PEImage can only parse PE files that match the bitness of the process.
29*6777b538SAndroid Build Coastguard Worker // See also PEImageAsData.
30*6777b538SAndroid Build Coastguard Worker class PEImage {
31*6777b538SAndroid Build Coastguard Worker  public:
32*6777b538SAndroid Build Coastguard Worker   // Callback to enumerate sections.
33*6777b538SAndroid Build Coastguard Worker   // cookie is the value passed to the enumerate method.
34*6777b538SAndroid Build Coastguard Worker   // Returns true to continue the enumeration.
35*6777b538SAndroid Build Coastguard Worker   using EnumSectionsFunction =
36*6777b538SAndroid Build Coastguard Worker       bool (*)(const PEImage&, PIMAGE_SECTION_HEADER, PVOID, DWORD, PVOID);
37*6777b538SAndroid Build Coastguard Worker 
38*6777b538SAndroid Build Coastguard Worker   // Callback to enumerate exports.
39*6777b538SAndroid Build Coastguard Worker   // function is the actual address of the symbol. If forward is not null, it
40*6777b538SAndroid Build Coastguard Worker   // contains the dll and symbol to forward this export to. cookie is the value
41*6777b538SAndroid Build Coastguard Worker   // passed to the enumerate method.
42*6777b538SAndroid Build Coastguard Worker   // Returns true to continue the enumeration.
43*6777b538SAndroid Build Coastguard Worker   using EnumExportsFunction =
44*6777b538SAndroid Build Coastguard Worker       bool (*)(const PEImage&, DWORD, DWORD, LPCSTR, PVOID, LPCSTR, PVOID);
45*6777b538SAndroid Build Coastguard Worker 
46*6777b538SAndroid Build Coastguard Worker   // Callback to enumerate import blocks.
47*6777b538SAndroid Build Coastguard Worker   // name_table and iat point to the imports name table and address table for
48*6777b538SAndroid Build Coastguard Worker   // this block. cookie is the value passed to the enumerate method.
49*6777b538SAndroid Build Coastguard Worker   // Returns true to continue the enumeration.
50*6777b538SAndroid Build Coastguard Worker   using EnumImportChunksFunction = bool (*)(const PEImage&,
51*6777b538SAndroid Build Coastguard Worker                                             LPCSTR,
52*6777b538SAndroid Build Coastguard Worker                                             PIMAGE_THUNK_DATA,
53*6777b538SAndroid Build Coastguard Worker                                             PIMAGE_THUNK_DATA,
54*6777b538SAndroid Build Coastguard Worker                                             PVOID);
55*6777b538SAndroid Build Coastguard Worker 
56*6777b538SAndroid Build Coastguard Worker   // Callback to enumerate imports.
57*6777b538SAndroid Build Coastguard Worker   // module is the dll that exports this symbol. cookie is the value passed to
58*6777b538SAndroid Build Coastguard Worker   // the enumerate method.
59*6777b538SAndroid Build Coastguard Worker   // Returns true to continue the enumeration.
60*6777b538SAndroid Build Coastguard Worker   using EnumImportsFunction = bool (*)(const PEImage&,
61*6777b538SAndroid Build Coastguard Worker                                        LPCSTR,
62*6777b538SAndroid Build Coastguard Worker                                        DWORD,
63*6777b538SAndroid Build Coastguard Worker                                        LPCSTR,
64*6777b538SAndroid Build Coastguard Worker                                        DWORD,
65*6777b538SAndroid Build Coastguard Worker                                        PIMAGE_THUNK_DATA,
66*6777b538SAndroid Build Coastguard Worker                                        PVOID);
67*6777b538SAndroid Build Coastguard Worker 
68*6777b538SAndroid Build Coastguard Worker   // Callback to enumerate delayed import blocks.
69*6777b538SAndroid Build Coastguard Worker   // module is the dll that exports this block of symbols. cookie is the value
70*6777b538SAndroid Build Coastguard Worker   // passed to the enumerate method.
71*6777b538SAndroid Build Coastguard Worker   // Returns true to continue the enumeration.
72*6777b538SAndroid Build Coastguard Worker   using EnumDelayImportChunksFunction = bool (*)(const PEImage&,
73*6777b538SAndroid Build Coastguard Worker                                                  PImgDelayDescr,
74*6777b538SAndroid Build Coastguard Worker                                                  LPCSTR,
75*6777b538SAndroid Build Coastguard Worker                                                  PIMAGE_THUNK_DATA,
76*6777b538SAndroid Build Coastguard Worker                                                  PIMAGE_THUNK_DATA,
77*6777b538SAndroid Build Coastguard Worker                                                  PVOID);
78*6777b538SAndroid Build Coastguard Worker 
79*6777b538SAndroid Build Coastguard Worker   // Callback to enumerate relocations.
80*6777b538SAndroid Build Coastguard Worker   // cookie is the value passed to the enumerate method.
81*6777b538SAndroid Build Coastguard Worker   // Returns true to continue the enumeration.
82*6777b538SAndroid Build Coastguard Worker   using EnumRelocsFunction = bool (*)(const PEImage&, WORD, PVOID, PVOID);
83*6777b538SAndroid Build Coastguard Worker 
PEImage(HMODULE module)84*6777b538SAndroid Build Coastguard Worker   explicit PEImage(HMODULE module) : module_(module) {}
PEImage(const void * module)85*6777b538SAndroid Build Coastguard Worker   explicit PEImage(const void* module) {
86*6777b538SAndroid Build Coastguard Worker     module_ = reinterpret_cast<HMODULE>(const_cast<void*>(module));
87*6777b538SAndroid Build Coastguard Worker   }
88*6777b538SAndroid Build Coastguard Worker 
89*6777b538SAndroid Build Coastguard Worker   virtual ~PEImage() = default;
90*6777b538SAndroid Build Coastguard Worker 
91*6777b538SAndroid Build Coastguard Worker   // Gets the HMODULE for this object.
92*6777b538SAndroid Build Coastguard Worker   HMODULE module() const;
93*6777b538SAndroid Build Coastguard Worker 
94*6777b538SAndroid Build Coastguard Worker   // Sets this object's HMODULE.
95*6777b538SAndroid Build Coastguard Worker   void set_module(HMODULE module);
96*6777b538SAndroid Build Coastguard Worker 
97*6777b538SAndroid Build Coastguard Worker   // Checks if this symbol is actually an ordinal.
98*6777b538SAndroid Build Coastguard Worker   static bool IsOrdinal(LPCSTR name);
99*6777b538SAndroid Build Coastguard Worker 
100*6777b538SAndroid Build Coastguard Worker   // Converts a named symbol to the corresponding ordinal.
101*6777b538SAndroid Build Coastguard Worker   static WORD ToOrdinal(LPCSTR name);
102*6777b538SAndroid Build Coastguard Worker 
103*6777b538SAndroid Build Coastguard Worker   // Returns the DOS_HEADER for this PE.
104*6777b538SAndroid Build Coastguard Worker   PIMAGE_DOS_HEADER GetDosHeader() const;
105*6777b538SAndroid Build Coastguard Worker 
106*6777b538SAndroid Build Coastguard Worker   // Returns the NT_HEADER for this PE.
107*6777b538SAndroid Build Coastguard Worker   PIMAGE_NT_HEADERS GetNTHeaders() const;
108*6777b538SAndroid Build Coastguard Worker 
109*6777b538SAndroid Build Coastguard Worker   // Returns number of sections of this PE.
110*6777b538SAndroid Build Coastguard Worker   WORD GetNumSections() const;
111*6777b538SAndroid Build Coastguard Worker 
112*6777b538SAndroid Build Coastguard Worker   // Returns the header for a given section.
113*6777b538SAndroid Build Coastguard Worker   // returns NULL if there is no such section.
114*6777b538SAndroid Build Coastguard Worker   PIMAGE_SECTION_HEADER GetSectionHeader(WORD section) const;
115*6777b538SAndroid Build Coastguard Worker 
116*6777b538SAndroid Build Coastguard Worker   // Returns the size of a given directory entry or 0 if |directory| is out of
117*6777b538SAndroid Build Coastguard Worker   // bounds.
118*6777b538SAndroid Build Coastguard Worker   DWORD GetImageDirectoryEntrySize(UINT directory) const;
119*6777b538SAndroid Build Coastguard Worker 
120*6777b538SAndroid Build Coastguard Worker   // Returns the address of a given directory entry or NULL if |directory| is
121*6777b538SAndroid Build Coastguard Worker   // out of bounds.
122*6777b538SAndroid Build Coastguard Worker   PVOID GetImageDirectoryEntryAddr(UINT directory) const;
123*6777b538SAndroid Build Coastguard Worker 
124*6777b538SAndroid Build Coastguard Worker   // Returns the section header for a given address.
125*6777b538SAndroid Build Coastguard Worker   // Use: s = image.GetImageSectionFromAddr(a);
126*6777b538SAndroid Build Coastguard Worker   // Post: 's' is the section header of the section that contains 'a'
127*6777b538SAndroid Build Coastguard Worker   //       or NULL if there is no such section.
128*6777b538SAndroid Build Coastguard Worker   PIMAGE_SECTION_HEADER GetImageSectionFromAddr(PVOID address) const;
129*6777b538SAndroid Build Coastguard Worker 
130*6777b538SAndroid Build Coastguard Worker   // Returns the section header for a given section.
131*6777b538SAndroid Build Coastguard Worker   PIMAGE_SECTION_HEADER GetImageSectionHeaderByName(LPCSTR section_name) const;
132*6777b538SAndroid Build Coastguard Worker 
133*6777b538SAndroid Build Coastguard Worker   // Returns the first block of imports.
134*6777b538SAndroid Build Coastguard Worker   PIMAGE_IMPORT_DESCRIPTOR GetFirstImportChunk() const;
135*6777b538SAndroid Build Coastguard Worker 
136*6777b538SAndroid Build Coastguard Worker   // Returns the exports directory.
137*6777b538SAndroid Build Coastguard Worker   PIMAGE_EXPORT_DIRECTORY GetExportDirectory() const;
138*6777b538SAndroid Build Coastguard Worker 
139*6777b538SAndroid Build Coastguard Worker   // Retrieves the contents of the image's CodeView debug entry, returning true
140*6777b538SAndroid Build Coastguard Worker   // if such an entry is found and is within a section mapped into the current
141*6777b538SAndroid Build Coastguard Worker   // process's memory. |guid|, |age|, and |pdb_filename| are each optional and
142*6777b538SAndroid Build Coastguard Worker   // may be NULL. |pdb_filename_length| is mandatory if |pdb_filename| is not
143*6777b538SAndroid Build Coastguard Worker   // NULL, as the latter is populated with a direct reference to a string in the
144*6777b538SAndroid Build Coastguard Worker   // image that is is not guaranteed to be terminated (note: informal
145*6777b538SAndroid Build Coastguard Worker   // documentation indicates that it should be terminated, but the data is
146*6777b538SAndroid Build Coastguard Worker   // untrusted). Furthermore, owing to its nature of being a string in the
147*6777b538SAndroid Build Coastguard Worker   // image, it is only valid while the image is mapped into the process, and the
148*6777b538SAndroid Build Coastguard Worker   // caller is not responsible for freeing it. |pdb_filename_length| is
149*6777b538SAndroid Build Coastguard Worker   // populated with the string length of |pdb_filename| (not including a
150*6777b538SAndroid Build Coastguard Worker   // terminator) and must be used rather than relying on |pdb_filename| being
151*6777b538SAndroid Build Coastguard Worker   // properly terminated.
152*6777b538SAndroid Build Coastguard Worker   bool GetDebugId(LPGUID guid,
153*6777b538SAndroid Build Coastguard Worker                   LPDWORD age,
154*6777b538SAndroid Build Coastguard Worker                   LPCSTR* pdb_filename,
155*6777b538SAndroid Build Coastguard Worker                   size_t* pdb_filename_length) const;
156*6777b538SAndroid Build Coastguard Worker 
157*6777b538SAndroid Build Coastguard Worker   // Returns a given export entry.
158*6777b538SAndroid Build Coastguard Worker   // Use: e = image.GetExportEntry(f);
159*6777b538SAndroid Build Coastguard Worker   // Pre: 'f' is either a zero terminated string or ordinal
160*6777b538SAndroid Build Coastguard Worker   // Post: 'e' is a pointer to the export directory entry
161*6777b538SAndroid Build Coastguard Worker   //       that contains 'f's export RVA, or NULL if 'f'
162*6777b538SAndroid Build Coastguard Worker   //       is not exported from this image
163*6777b538SAndroid Build Coastguard Worker   PDWORD GetExportEntry(LPCSTR name) const;
164*6777b538SAndroid Build Coastguard Worker 
165*6777b538SAndroid Build Coastguard Worker   // Returns the address for a given exported symbol.
166*6777b538SAndroid Build Coastguard Worker   // Use: p = image.GetProcAddress(f);
167*6777b538SAndroid Build Coastguard Worker   // Pre: 'f' is either a zero terminated string or ordinal.
168*6777b538SAndroid Build Coastguard Worker   // Post: if 'f' is a non-forwarded export from image, 'p' is
169*6777b538SAndroid Build Coastguard Worker   //       the exported function. If 'f' is a forwarded export
170*6777b538SAndroid Build Coastguard Worker   //       then p is the special value -1. In this case
171*6777b538SAndroid Build Coastguard Worker   //       RVAToAddr(*GetExportEntry) can be used to resolve
172*6777b538SAndroid Build Coastguard Worker   //       the string that describes the forward.
173*6777b538SAndroid Build Coastguard Worker   FARPROC GetProcAddress(LPCSTR function_name) const;
174*6777b538SAndroid Build Coastguard Worker 
175*6777b538SAndroid Build Coastguard Worker   // Retrieves the ordinal for a given exported symbol.
176*6777b538SAndroid Build Coastguard Worker   // Returns true if the symbol was found.
177*6777b538SAndroid Build Coastguard Worker   bool GetProcOrdinal(LPCSTR function_name, WORD* ordinal) const;
178*6777b538SAndroid Build Coastguard Worker 
179*6777b538SAndroid Build Coastguard Worker   // Enumerates PE sections.
180*6777b538SAndroid Build Coastguard Worker   // cookie is a generic cookie to pass to the callback.
181*6777b538SAndroid Build Coastguard Worker   // Returns true on success.
182*6777b538SAndroid Build Coastguard Worker   bool EnumSections(EnumSectionsFunction callback, PVOID cookie) const;
183*6777b538SAndroid Build Coastguard Worker 
184*6777b538SAndroid Build Coastguard Worker   // Enumerates PE exports.
185*6777b538SAndroid Build Coastguard Worker   // cookie is a generic cookie to pass to the callback.
186*6777b538SAndroid Build Coastguard Worker   // Returns true on success.
187*6777b538SAndroid Build Coastguard Worker   bool EnumExports(EnumExportsFunction callback, PVOID cookie) const;
188*6777b538SAndroid Build Coastguard Worker 
189*6777b538SAndroid Build Coastguard Worker   // Enumerates PE imports.
190*6777b538SAndroid Build Coastguard Worker   // cookie is a generic cookie to pass to the callback.
191*6777b538SAndroid Build Coastguard Worker   // Returns true on success.
192*6777b538SAndroid Build Coastguard Worker   // Use |target_module_name| to ensure the callback is only invoked for the
193*6777b538SAndroid Build Coastguard Worker   // specified module.
194*6777b538SAndroid Build Coastguard Worker   bool EnumAllImports(EnumImportsFunction callback,
195*6777b538SAndroid Build Coastguard Worker                       PVOID cookie,
196*6777b538SAndroid Build Coastguard Worker                       LPCSTR target_module_name) const;
197*6777b538SAndroid Build Coastguard Worker 
198*6777b538SAndroid Build Coastguard Worker   // Enumerates PE import blocks.
199*6777b538SAndroid Build Coastguard Worker   // cookie is a generic cookie to pass to the callback.
200*6777b538SAndroid Build Coastguard Worker   // Returns true on success.
201*6777b538SAndroid Build Coastguard Worker   // Use |target_module_name| to ensure the callback is only invoked for the
202*6777b538SAndroid Build Coastguard Worker   // specified module.
203*6777b538SAndroid Build Coastguard Worker   bool EnumImportChunks(EnumImportChunksFunction callback,
204*6777b538SAndroid Build Coastguard Worker                         PVOID cookie,
205*6777b538SAndroid Build Coastguard Worker                         LPCSTR target_module_name) const;
206*6777b538SAndroid Build Coastguard Worker 
207*6777b538SAndroid Build Coastguard Worker   // Enumerates the imports from a single PE import block.
208*6777b538SAndroid Build Coastguard Worker   // cookie is a generic cookie to pass to the callback.
209*6777b538SAndroid Build Coastguard Worker   // Returns true on success.
210*6777b538SAndroid Build Coastguard Worker   bool EnumOneImportChunk(EnumImportsFunction callback,
211*6777b538SAndroid Build Coastguard Worker                           LPCSTR module_name,
212*6777b538SAndroid Build Coastguard Worker                           PIMAGE_THUNK_DATA name_table,
213*6777b538SAndroid Build Coastguard Worker                           PIMAGE_THUNK_DATA iat,
214*6777b538SAndroid Build Coastguard Worker                           PVOID cookie) const;
215*6777b538SAndroid Build Coastguard Worker 
216*6777b538SAndroid Build Coastguard Worker   // Enumerates PE delay imports.
217*6777b538SAndroid Build Coastguard Worker   // cookie is a generic cookie to pass to the callback.
218*6777b538SAndroid Build Coastguard Worker   // Returns true on success.
219*6777b538SAndroid Build Coastguard Worker   // Use |target_module_name| to ensure the callback is only invoked for the
220*6777b538SAndroid Build Coastguard Worker   // specified module. If this parameter is non-null then all delayloaded
221*6777b538SAndroid Build Coastguard Worker   // imports are resolved when the target module is found.
222*6777b538SAndroid Build Coastguard Worker   bool EnumAllDelayImports(EnumImportsFunction callback,
223*6777b538SAndroid Build Coastguard Worker                            PVOID cookie,
224*6777b538SAndroid Build Coastguard Worker                            LPCSTR target_module_name) const;
225*6777b538SAndroid Build Coastguard Worker 
226*6777b538SAndroid Build Coastguard Worker   // Enumerates PE delay import blocks.
227*6777b538SAndroid Build Coastguard Worker   // cookie is a generic cookie to pass to the callback.
228*6777b538SAndroid Build Coastguard Worker   // Returns true on success.
229*6777b538SAndroid Build Coastguard Worker   // Use |target_module_name| to ensure the callback is only invoked for the
230*6777b538SAndroid Build Coastguard Worker   // specified module. If this parameter is non-null then all delayloaded
231*6777b538SAndroid Build Coastguard Worker   // imports are resolved when the target module is found.
232*6777b538SAndroid Build Coastguard Worker   bool EnumDelayImportChunks(EnumDelayImportChunksFunction callback,
233*6777b538SAndroid Build Coastguard Worker                              PVOID cookie,
234*6777b538SAndroid Build Coastguard Worker                              LPCSTR target_module_name) const;
235*6777b538SAndroid Build Coastguard Worker 
236*6777b538SAndroid Build Coastguard Worker   // Enumerates imports from a single PE delay import block.
237*6777b538SAndroid Build Coastguard Worker   // cookie is a generic cookie to pass to the callback.
238*6777b538SAndroid Build Coastguard Worker   // Returns true on success.
239*6777b538SAndroid Build Coastguard Worker   bool EnumOneDelayImportChunk(EnumImportsFunction callback,
240*6777b538SAndroid Build Coastguard Worker                                PImgDelayDescr delay_descriptor,
241*6777b538SAndroid Build Coastguard Worker                                LPCSTR module_name,
242*6777b538SAndroid Build Coastguard Worker                                PIMAGE_THUNK_DATA name_table,
243*6777b538SAndroid Build Coastguard Worker                                PIMAGE_THUNK_DATA iat,
244*6777b538SAndroid Build Coastguard Worker                                PVOID cookie) const;
245*6777b538SAndroid Build Coastguard Worker 
246*6777b538SAndroid Build Coastguard Worker   // Enumerates PE relocation entries.
247*6777b538SAndroid Build Coastguard Worker   // cookie is a generic cookie to pass to the callback.
248*6777b538SAndroid Build Coastguard Worker   // Returns true on success.
249*6777b538SAndroid Build Coastguard Worker   bool EnumRelocs(EnumRelocsFunction callback, PVOID cookie) const;
250*6777b538SAndroid Build Coastguard Worker 
251*6777b538SAndroid Build Coastguard Worker   // Verifies the magic values on the PE file.
252*6777b538SAndroid Build Coastguard Worker   // Returns true if all values are correct.
253*6777b538SAndroid Build Coastguard Worker   bool VerifyMagic() const;
254*6777b538SAndroid Build Coastguard Worker 
255*6777b538SAndroid Build Coastguard Worker   // Converts an rva value to the appropriate address.
256*6777b538SAndroid Build Coastguard Worker   virtual PVOID RVAToAddr(uintptr_t rva) const;
257*6777b538SAndroid Build Coastguard Worker 
258*6777b538SAndroid Build Coastguard Worker   // Converts an rva value to an offset on disk.
259*6777b538SAndroid Build Coastguard Worker   // Returns true on success.
260*6777b538SAndroid Build Coastguard Worker   bool ImageRVAToOnDiskOffset(uintptr_t rva, DWORD* on_disk_offset) const;
261*6777b538SAndroid Build Coastguard Worker 
262*6777b538SAndroid Build Coastguard Worker   // Converts an address to an offset on disk.
263*6777b538SAndroid Build Coastguard Worker   // Returns true on success.
264*6777b538SAndroid Build Coastguard Worker   bool ImageAddrToOnDiskOffset(LPVOID address, DWORD* on_disk_offset) const;
265*6777b538SAndroid Build Coastguard Worker 
266*6777b538SAndroid Build Coastguard Worker  private:
267*6777b538SAndroid Build Coastguard Worker   // Returns a pointer to a data directory, or NULL if |directory| is out of
268*6777b538SAndroid Build Coastguard Worker   // range.
269*6777b538SAndroid Build Coastguard Worker   const IMAGE_DATA_DIRECTORY* GetDataDirectory(UINT directory) const;
270*6777b538SAndroid Build Coastguard Worker 
271*6777b538SAndroid Build Coastguard Worker   HMODULE module_;
272*6777b538SAndroid Build Coastguard Worker };
273*6777b538SAndroid Build Coastguard Worker 
274*6777b538SAndroid Build Coastguard Worker // This class is an extension to the PEImage class that allows working with PE
275*6777b538SAndroid Build Coastguard Worker // files mapped as data instead of as image file.
276*6777b538SAndroid Build Coastguard Worker class PEImageAsData : public PEImage {
277*6777b538SAndroid Build Coastguard Worker  public:
PEImageAsData(HMODULE hModule)278*6777b538SAndroid Build Coastguard Worker   explicit PEImageAsData(HMODULE hModule) : PEImage(hModule) {}
279*6777b538SAndroid Build Coastguard Worker 
280*6777b538SAndroid Build Coastguard Worker   PVOID RVAToAddr(uintptr_t rva) const override;
281*6777b538SAndroid Build Coastguard Worker };
282*6777b538SAndroid Build Coastguard Worker 
IsOrdinal(LPCSTR name)283*6777b538SAndroid Build Coastguard Worker inline bool PEImage::IsOrdinal(LPCSTR name) {
284*6777b538SAndroid Build Coastguard Worker   return reinterpret_cast<uintptr_t>(name) <= 0xFFFF;
285*6777b538SAndroid Build Coastguard Worker }
286*6777b538SAndroid Build Coastguard Worker 
ToOrdinal(LPCSTR name)287*6777b538SAndroid Build Coastguard Worker inline WORD PEImage::ToOrdinal(LPCSTR name) {
288*6777b538SAndroid Build Coastguard Worker   return static_cast<WORD>(reinterpret_cast<intptr_t>(name));
289*6777b538SAndroid Build Coastguard Worker }
290*6777b538SAndroid Build Coastguard Worker 
module()291*6777b538SAndroid Build Coastguard Worker inline HMODULE PEImage::module() const {
292*6777b538SAndroid Build Coastguard Worker   return module_;
293*6777b538SAndroid Build Coastguard Worker }
294*6777b538SAndroid Build Coastguard Worker 
GetFirstImportChunk()295*6777b538SAndroid Build Coastguard Worker inline PIMAGE_IMPORT_DESCRIPTOR PEImage::GetFirstImportChunk() const {
296*6777b538SAndroid Build Coastguard Worker   return reinterpret_cast<PIMAGE_IMPORT_DESCRIPTOR>(
297*6777b538SAndroid Build Coastguard Worker       GetImageDirectoryEntryAddr(IMAGE_DIRECTORY_ENTRY_IMPORT));
298*6777b538SAndroid Build Coastguard Worker }
299*6777b538SAndroid Build Coastguard Worker 
GetExportDirectory()300*6777b538SAndroid Build Coastguard Worker inline PIMAGE_EXPORT_DIRECTORY PEImage::GetExportDirectory() const {
301*6777b538SAndroid Build Coastguard Worker   return reinterpret_cast<PIMAGE_EXPORT_DIRECTORY>(
302*6777b538SAndroid Build Coastguard Worker       GetImageDirectoryEntryAddr(IMAGE_DIRECTORY_ENTRY_EXPORT));
303*6777b538SAndroid Build Coastguard Worker }
304*6777b538SAndroid Build Coastguard Worker 
305*6777b538SAndroid Build Coastguard Worker }  // namespace win
306*6777b538SAndroid Build Coastguard Worker }  // namespace base
307*6777b538SAndroid Build Coastguard Worker 
308*6777b538SAndroid Build Coastguard Worker #endif  // BASE_WIN_PE_IMAGE_H_
309