1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef COURGETTE_ASSEMBLY_PROGRAM_H_
6 #define COURGETTE_ASSEMBLY_PROGRAM_H_
12 #include "base/macros.h"
13 #include "courgette/courgette.h"
14 #include "courgette/image_utils.h"
15 #include "courgette/instruction_utils.h"
16 #include "courgette/label_manager.h"
17 #include "courgette/memory_allocator.h" // For CheckBool.
23 // An AssemblyProgram stores Labels extracted from an executable file, and
24 // (optionally) Label annotations. It is initialized by a Disassembler, but
25 // stores separate state so that the Disassembler can be deleted. Typical usage:
27 // * The Disassembler calls PrecomputeLabels() and injects RVAs for abs32/rel32
28 // references. These are used to initialize labels.
29 // * The Disassembler calls DefaultAssignIndexes() to assign addresses to
30 // positions in the address tables.
32 // * The Disassembler can use Labels in AssemblyProgram to convert the
33 // executable file to an EncodedProgram, serialized to an output stream.
34 // * Later, the Disassembler can use the AssemblyProgram to can be deserialized
35 // and assembled into the original executable file via an EncodedProgram.
37 // The optional step is to adjust Labels in the AssemblyProgram. One form of
38 // adjustment is to assign indexes in such a way as to make the EncodedProgram
39 // for an executable look more like the EncodedProgram for another exectuable.
40 // The adjustment process should call UnassignIndexes(), do its own assignment,
41 // and then call AssignRemainingIndexes() to ensure all indexes are assigned.
42 class AssemblyProgram {
44 AssemblyProgram(ExecutableType kind, uint64_t image_base);
47 ExecutableType kind() const { return kind_; }
48 const std::vector<Label*>& abs32_label_annotations() const {
49 return abs32_label_annotations_;
51 const std::vector<Label*>& rel32_label_annotations() const {
52 return rel32_label_annotations_;
55 // Traverses RVAs in |abs32_visitor| and |rel32_visitor| to precompute Labels.
56 void PrecomputeLabels(RvaVisitor* abs32_visitor, RvaVisitor* rel32_visitor);
58 // Removes underused Labels. Thresholds used (0 = no trimming) is
59 // architecture-dependent.
62 void UnassignIndexes();
63 void DefaultAssignIndexes();
64 void AssignRemainingIndexes();
66 // Looks up abs32 label. Returns null if none found.
67 Label* FindAbs32Label(RVA rva);
69 // Looks up rel32 label. Returns null if none found.
70 Label* FindRel32Label(RVA rva);
72 // Uses |gen| to initializes |*_label_annotations_|.
73 CheckBool AnnotateLabels(const InstructionGenerator& gen);
75 // Initializes |encoded| by injecting basic data and Label data.
76 bool PrepareEncodedProgram(EncodedProgram* encoded) const;
79 static const int kLabelLowerLimit;
81 // Looks up a label or creates a new one. Might return NULL.
82 Label* FindLabel(RVA rva, RVAToLabel* labels);
84 const ExecutableType kind_;
85 const uint64_t image_base_; // Desired or mandated base address of image.
87 // Storage and lookup of Labels associated with target addresses. We use
88 // separate abs32 and rel32 labels.
89 LabelManager abs32_label_manager_;
90 LabelManager rel32_label_manager_;
92 // Label pointers for each abs32 and rel32 location, sorted by file offset.
93 // These are used by Label adjustment during patch generation.
94 std::vector<Label*> abs32_label_annotations_;
95 std::vector<Label*> rel32_label_annotations_;
97 DISALLOW_COPY_AND_ASSIGN(AssemblyProgram);
100 } // namespace courgette
102 #endif // COURGETTE_ASSEMBLY_PROGRAM_H_