1 : // Copyright 2013 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : // COFF file reading and support for COFF-specific features, such as
16 : // symbol and string tables and COFF relocations.
17 :
18 : #ifndef SYZYGY_PE_COFF_FILE_H_
19 : #define SYZYGY_PE_COFF_FILE_H_
20 :
21 : #include <windows.h>
22 : #include <winnt.h>
23 : #include <map>
24 : #include <set>
25 : #include <string>
26 : #include <vector>
27 :
28 : #include "base/files/file_path.h"
29 : #include "syzygy/core/address.h"
30 : #include "syzygy/core/address_space.h"
31 : #include "syzygy/core/serialization.h"
32 : #include "syzygy/pe/pe_coff_file.h"
33 : #include "syzygy/pe/pe_file.h"
34 :
35 : namespace pe {
36 :
37 : // Traits of the COFF address space.
38 : struct CoffAddressSpaceTraits {
39 : // Native addresses for COFF files: physical file offsets.
40 : typedef core::FileOffsetAddress AddressType;
41 :
42 : // Native sizes for COFF files.
43 : typedef size_t SizeType;
44 :
45 : // @returns an address different from all valid addresses for the
46 : // specified address type.
47 E : static const AddressType invalid_address() {
48 E : return AddressType::kInvalidAddress;
49 E : }
50 :
51 : // @returns the address of the header range, which is always zero
52 : // for COFF files.
53 E : static AddressType header_address() {
54 E : return AddressType(0);
55 E : }
56 :
57 : // Return the file offset of the section data on disk.
58 : //
59 : // @param header the section header.
60 : // @returns the offset of the section.
61 E : static AddressType GetSectionAddress(const IMAGE_SECTION_HEADER& header) {
62 : if ((header.Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA) != 0 &&
63 E : header.PointerToRawData == 0) {
64 : // Unmapped BSS section.
65 E : return invalid_address();
66 : }
67 E : return AddressType(header.PointerToRawData);
68 E : }
69 :
70 : // Return the number of bytes occupied by the section data on disk.
71 : //
72 : // @param header the section header.
73 : // @returns the file size of the section.
74 E : static SizeType GetSectionSize(const IMAGE_SECTION_HEADER& header) {
75 E : return SizeType(header.SizeOfRawData);
76 E : }
77 : };
78 :
79 : // A raw, sparse, representation of a COFF file. It offers a view of
80 : // the contents of the file as is present in the object file, on disk.
81 : class CoffFile : public PECoffFile<CoffAddressSpaceTraits> {
82 : public:
83 : typedef core::FileOffsetAddress FileOffsetAddress;
84 :
85 : // A map of the decoded relocation information, where each pair in
86 : // the map associates a location containing the address to relocate
87 : // with a pointer to the IMAGE_RELOCATION structure describing the
88 : // relocation.
89 : typedef std::map<FileOffsetAddress, const IMAGE_RELOCATION*> RelocMap;
90 :
91 : // Construct a CoffFile object not yet bound to any file.
92 : CoffFile();
93 :
94 : // Destroy this CoffFile object, invalidating all pointers obtained
95 : // through GetImageData(), or headers returned by corresponding
96 : // accessor methods.
97 : ~CoffFile();
98 :
99 : // Read in the image file at @p path, making its data
100 : // available. A COFF file reader may only read a single file.
101 : //
102 : // @param path the path to the file to read.
103 : // @returns true on success, false on error.
104 : bool Init(const base::FilePath& path);
105 :
106 : // Translate a file offset to a pair of section index and relative
107 : // offset.
108 : //
109 : // @param addr the file offset to translate.
110 : // @param section_index where to put the section index.
111 : // @param offset where to put the section-relative offset.
112 : // @returns true on success, false if @p addr does not refer to
113 : // section data.
114 : bool FileOffsetToSectionOffset(FileOffsetAddress addr,
115 : size_t* section_index,
116 : size_t* offset) const;
117 :
118 : // Translate a pair of section index and relative offset to a file
119 : // offset.
120 : //
121 : // @param section_index the section index.
122 : // @param offset the section-relative offset.
123 : // @param addr where to put the translated address.
124 : // @returns true on success, false if the pair does not refer to
125 : // section data.
126 : bool SectionOffsetToFileOffset(size_t section_index, size_t offset,
127 : FileOffsetAddress* addr) const;
128 :
129 : // Decode relocations for all sections, inserting the results into
130 : // @p reloc_map.
131 : //
132 : // @param reloc_map the map to which relocation--target pairs are to
133 : // be added.
134 : void DecodeRelocs(RelocMap* reloc_map) const;
135 :
136 : // Decode relocation information for the specified section,
137 : // inserting the result into @p reloc_map.
138 : //
139 : // @param section_index the index of the section to decode
140 : // relocations for.
141 : // @param reloc_map the map to which relocation--target pairs are to
142 : // be added.
143 : // @returns true on success, false on error.
144 : bool DecodeSectionRelocs(size_t section_index, RelocMap* reloc_map) const;
145 :
146 : // Test whether the specified section is mapped into the address
147 : // space of this object. BSS sections are not mapped and must be
148 : // handled specially.
149 : //
150 : // @param section_index the index of the section.
151 : // @returns true if the section is mapped, false otherwise.
152 : bool IsSectionMapped(size_t section_index) const;
153 :
154 : // Retrieve the symbol name at the specified index, handling both
155 : // short and long symbol names, reading from the string table if
156 : // necessary.
157 : //
158 : // @param symbol_index the index of the symbol.
159 : // @returns the name of the symbol, or NULL if the index is invalid.
160 : const char* GetSymbolName(size_t symbol_index) const;
161 :
162 : // @returns a pointer to the symbol table of this COFF file.
163 E : const IMAGE_SYMBOL* symbols() const {
164 E : return symbols_;
165 E : }
166 :
167 : // Retrieve a pointer to the symbol entry at index @p symbol_index.
168 : //
169 : // @param symbol_index the index of the symbol.
170 : // @returns a pointer to the symbol structure.
171 E : const IMAGE_SYMBOL* symbol(size_t symbol_index) const {
172 E : if (symbol_index >= file_header_->NumberOfSymbols) {
173 i : return NULL;
174 : }
175 E : return &symbols_[symbol_index];
176 E : }
177 :
178 : // @returns a pointer to the string table of this COFF file.
179 E : const char* strings() const {
180 E : return strings_;
181 E : }
182 :
183 : // Retrieve a pointer to the string at offset @p string_offset,
184 : // relative to the beginning of the string table.
185 : //
186 : // @param string_offset the offset of the string.
187 : // @returns a pointer to the zero-terminated string, or NULL if
188 : // @p string_offset does not refer to the beginning of
189 : // a string.
190 E : const char* string(size_t string_offset) const {
191 E : if (string_offset >= strings_size_) {
192 i : return NULL;
193 : }
194 E : return strings_ + string_offset;
195 E : }
196 :
197 : // @returns the address of the symbol table.
198 E : FileOffsetAddress symbols_address() const {
199 E : return symbols_offset_;
200 E : }
201 :
202 : // @returns the size of the symbol table.
203 E : size_t symbols_size() const {
204 E : return symbols_size_;
205 E : }
206 :
207 : // @returns the address of the string table.
208 E : FileOffsetAddress strings_address() const {
209 E : return strings_offset_;
210 E : }
211 :
212 : // @returns the size of the string table.
213 E : size_t strings_size() const {
214 E : return strings_size_;
215 E : }
216 :
217 : private:
218 : // Information on the relocation table of a given section.
219 : struct SectionRelocInfo {
220 : // A pointer to the internal relocation data.
221 : IMAGE_RELOCATION* relocs_;
222 :
223 : // The number of relocations in the table pointed to by relocs_.
224 : size_t num_relocs_;
225 : };
226 :
227 : // Add data outside of sections (relocations, symbols, strings) to
228 : // the address space.
229 : //
230 : // @returns true on success, false on error.
231 : bool ReadNonSections();
232 :
233 : // A pointer to the internal symbol table data.
234 : IMAGE_SYMBOL* symbols_;
235 :
236 : // A pointer to the internal string table data.
237 : char* strings_;
238 :
239 : // The offset to the symbol table in the file.
240 : FileOffsetAddress symbols_offset_;
241 : // The size in bytes of the symbol table.
242 : size_t symbols_size_;
243 :
244 : // The offset to the string table in the file.
245 : FileOffsetAddress strings_offset_;
246 : // The size in bytes of the string table.
247 : size_t strings_size_;
248 :
249 : // A vector containing relocation information for every sections,
250 : // indexed by the section index.
251 : std::vector<SectionRelocInfo> reloc_infos_;
252 :
253 : DISALLOW_COPY_AND_ASSIGN(CoffFile);
254 : };
255 :
256 : } // namespace pe
257 :
258 : #endif // SYZYGY_PE_COFF_FILE_H_
|