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 : // The CoffImageLayoutBuilder is the COFF-specific class for building image
16 : // layouts for object files.
17 :
18 : #ifndef SYZYGY_PE_COFF_IMAGE_LAYOUT_BUILDER_H_
19 : #define SYZYGY_PE_COFF_IMAGE_LAYOUT_BUILDER_H_
20 :
21 : #include <windows.h>
22 : #include <map>
23 :
24 : #include "syzygy/block_graph/block_graph.h"
25 : #include "syzygy/block_graph/ordered_block_graph.h"
26 : #include "syzygy/pe/image_layout.h"
27 : #include "syzygy/pe/pe_coff_image_layout_builder.h"
28 :
29 m : namespace pe {
30 :
31 : // A CoffImageLayoutBuilder builds an image layout for a COFF file, mapping
32 : // blocks and sections to addresses, updating relocation tables, and fixing
33 : // all needed file offset pointers.
34 m : class CoffImageLayoutBuilder : public PECoffImageLayoutBuilder {
35 m : public:
36 : // Construct a new image layout builder that populates the provided image
37 : // layout. The image layout must outlive the builder.
38 : //
39 : // @param image_layout The image layout object to populate.
40 m : explicit CoffImageLayoutBuilder(ImageLayout* image_layout);
41 :
42 : // Lay out the image according to the specified ordering.
43 : //
44 : // @param ordered_graph the ordered block graph; the underlying block
45 : // graph must match that of the image layout passed to the
46 : // constructor.
47 : // @returns true on success, false on failure.
48 m : bool LayoutImage(const block_graph::OrderedBlockGraph& ordered_graph);
49 :
50 m : private:
51 : // Lay out the incomplete COFF file header and section table. This
52 : // essentially reserves space for these entities, copying over the old
53 : // data, assuming the headers block is correctly sized for the expected
54 : // number of section header entries, and all old references, in particular
55 : // to old relocation tables, have been removed. The contents will need
56 : // fixing by the other helper routines, as the information becomes
57 : // available.
58 : //
59 : // @returns true on success, false on failure.
60 m : bool LayoutHeaders();
61 :
62 : // Lay out all section blocks, section by section in the specified order,
63 : // as well as the computed relocations for each section, if any; update
64 : // the COFF section headers as appropriate.
65 : //
66 : // @param ordered_graph the ordered block graph.
67 : // @returns true on success, false on failure.
68 m : bool LayoutSectionBlocks(const OrderedBlockGraph& ordered_graph);
69 :
70 : // Lay out the symbol and string tables, and update the COFF file header.
71 : //
72 : // @param ordered_graph the ordered block graph.
73 : // @returns true on success, false on failure.
74 m : bool LayoutSymbolAndStringTables(const OrderedBlockGraph& ordered_graph);
75 :
76 : // Remove unmapped relocation blocks, and ensure that no other block is
77 : // left unmapped.
78 : //
79 : // @returns true on success, false on failure.
80 m : bool RemoveOldRelocBlocks();
81 :
82 : // The headers block that contains the file header and section table.
83 m : BlockGraph::Block* headers_block_;
84 :
85 : // The block containing the symbol table.
86 m : BlockGraph::Block* symbols_block_;
87 :
88 : // The block containing the string table.
89 m : BlockGraph::Block* strings_block_;
90 :
91 m : DISALLOW_COPY_AND_ASSIGN(CoffImageLayoutBuilder);
92 m : };
93 :
94 m : } // namespace pe
95 :
96 : #endif // SYZYGY_PE_COFF_IMAGE_LAYOUT_BUILDER_H_
|