| OLD | NEW |
| 1 // Copyright 2011 Google Inc. All Rights Reserved. | 1 // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 // | 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with 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 | 5 // You may obtain a copy of the License at |
| 6 // | 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // | 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 | 35 |
| 36 // @param image_layout the image layout to write. | 36 // @param image_layout the image layout to write. |
| 37 explicit PEFileWriter(const ImageLayout& image_layout); | 37 explicit PEFileWriter(const ImageLayout& image_layout); |
| 38 | 38 |
| 39 // Writes the image to path. | 39 // Writes the image to path. |
| 40 bool WriteImage(const base::FilePath& path); | 40 bool WriteImage(const base::FilePath& path); |
| 41 | 41 |
| 42 // Updates the checksum for the image @p path. | 42 // Updates the checksum for the image @p path. |
| 43 static bool UpdateFileChecksum(const base::FilePath& path); | 43 static bool UpdateFileChecksum(const base::FilePath& path); |
| 44 | 44 |
| 45 protected: | 45 // Retrieves the base address of the image. |
| 46 // @return the base address of the image output by this function. |
| 47 AbsoluteAddress* PEFileWriter::GetImageBase(); |
| 48 |
| 49 // Returns the size of the image. |
| 50 const size_t GetImageSize(); |
| 51 |
| 52 // Closes off the writing of a section by adding any necessary padding to the |
| 53 // output buffer. |
| 54 void FlushSection(size_t section_index, |
| 55 std::vector<uint8_t>* buffer); |
| 56 |
| 57 // Writes a single block to the buffer, first writing any necessary padding |
| 58 // (the content of which depends on the section type), followed by the |
| 59 // block data (containing finalized references). It outputs the offset of |
| 60 // the block in the actual file on disk. |
| 61 bool WriteOneBlock(AbsoluteAddress image_base, |
| 62 size_t section_index, |
| 63 const BlockGraph::Block* block, |
| 64 std::vector<uint8_t>* buffer, |
| 65 FileOffsetAddress* block_offset); |
| 66 |
| 46 // Validates the DOS header and the NT headers in the image. | 67 // Validates the DOS header and the NT headers in the image. |
| 47 // On success, sets the nt_headers_ pointer. | 68 // On success, sets the nt_headers_ pointer. |
| 48 bool ValidateHeaders(); | 69 bool ValidateHeaders(); |
| 49 | 70 |
| 50 // Validates that the section info is consistent and populates | 71 // Validates that the section info is consistent and populates |
| 51 // section_file_range_map_ and section_index_space_. | 72 // section_file_range_map_ and section_index_space_. |
| 52 bool CalculateSectionRanges(); | 73 bool CalculateSectionRanges(); |
| 53 | 74 |
| 75 protected: |
| 54 // Writes the entire image to the given file. Delegates to FlushSection and | 76 // Writes the entire image to the given file. Delegates to FlushSection and |
| 55 // WriteOneBlock. | 77 // WriteOneBlock. |
| 56 bool WriteBlocks(FILE* file); | 78 bool WriteBlocks(FILE* file); |
| 57 | 79 |
| 58 // Closes off the writing of a section by adding any necessary padding to the | |
| 59 // output buffer. | |
| 60 void FlushSection(size_t section_index, std::vector<uint8_t>* buffer); | |
| 61 | |
| 62 // Writes a single block to the buffer, first writing any necessary padding | |
| 63 // (the content of which depends on the section type), followed by the | |
| 64 // block data (containing finalized references). | |
| 65 bool WriteOneBlock(AbsoluteAddress image_base, | |
| 66 size_t section_index, | |
| 67 const BlockGraph::Block* block, | |
| 68 std::vector<uint8_t>* buffer); | |
| 69 | |
| 70 // The file ranges of each section. This is populated by | 80 // The file ranges of each section. This is populated by |
| 71 // CalculateSectionRanges and is a map from section index (as ordered in | 81 // CalculateSectionRanges and is a map from section index (as ordered in |
| 72 // the image layout) to section ranges on disk. | 82 // the image layout) to section ranges on disk. |
| 73 typedef core::AddressRange<core::FileOffsetAddress, size_t> FileRange; | 83 typedef core::AddressRange<core::FileOffsetAddress, size_t> FileRange; |
| 74 typedef std::map<size_t, FileRange> SectionIndexFileRangeMap; | 84 typedef std::map<size_t, FileRange> SectionIndexFileRangeMap; |
| 75 SectionIndexFileRangeMap section_file_range_map_; | 85 SectionIndexFileRangeMap section_file_range_map_; |
| 76 | 86 |
| 77 // This stores an address-space from RVAs to section indices and is populated | 87 // This stores an address-space from RVAs to section indices and is populated |
| 78 // by CalculateSectionRanges. This can be used to map from a block's | 88 // by CalculateSectionRanges. This can be used to map from a block's |
| 79 // address to the index of its section. This is needed for finalizing | 89 // address to the index of its section. This is needed for finalizing |
| 80 // references. | 90 // references. |
| 81 typedef core::AddressSpace<core::RelativeAddress, size_t, size_t> | 91 typedef core::AddressSpace<core::RelativeAddress, size_t, size_t> |
| 82 SectionIndexSpace; | 92 SectionIndexSpace; |
| 83 SectionIndexSpace section_index_space_; | 93 SectionIndexSpace section_index_space_; |
| 84 | 94 |
| 85 // Our image layout as provided to the constructor. | 95 // Our image layout as provided to the constructor. |
| 86 const ImageLayout& image_layout_; | 96 const ImageLayout& image_layout_; |
| 87 | 97 |
| 88 // Refers to the nt headers from the image during WriteImage. | 98 // Refers to the nt headers from the image during WriteImage. |
| 89 const IMAGE_NT_HEADERS* nt_headers_; | 99 const IMAGE_NT_HEADERS* nt_headers_; |
| 90 | 100 |
| 91 private: | 101 private: |
| 92 DISALLOW_COPY_AND_ASSIGN(PEFileWriter); | 102 DISALLOW_COPY_AND_ASSIGN(PEFileWriter); |
| 93 }; | 103 }; |
| 94 | 104 |
| 95 } // namespace pe | 105 } // namespace pe |
| 96 | 106 |
| 97 #endif // SYZYGY_PE_PE_FILE_WRITER_H_ | 107 #endif // SYZYGY_PE_PE_FILE_WRITER_H_ |
| OLD | NEW |