Upload upstream chromium 73.0.3683.0
[platform/framework/web/chromium-efl.git] / courgette / assembly_program.h
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.
4
5 #ifndef COURGETTE_ASSEMBLY_PROGRAM_H_
6 #define COURGETTE_ASSEMBLY_PROGRAM_H_
7
8 #include <stdint.h>
9
10 #include <vector>
11
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.
18
19 namespace courgette {
20
21 class EncodedProgram;
22
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:
26 //
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.
31 // * [Optional step]
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.
36 //
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 {
43  public:
44   AssemblyProgram(ExecutableType kind, uint64_t image_base);
45   ~AssemblyProgram();
46
47   ExecutableType kind() const { return kind_; }
48   const std::vector<Label*>& abs32_label_annotations() const {
49     return abs32_label_annotations_;
50   }
51   const std::vector<Label*>& rel32_label_annotations() const {
52     return rel32_label_annotations_;
53   }
54
55   // Traverses RVAs in |abs32_visitor| and |rel32_visitor| to precompute Labels.
56   void PrecomputeLabels(RvaVisitor* abs32_visitor, RvaVisitor* rel32_visitor);
57
58   // Removes underused Labels. Thresholds used (0 = no trimming) is
59   // architecture-dependent.
60   void TrimLabels();
61
62   void UnassignIndexes();
63   void DefaultAssignIndexes();
64   void AssignRemainingIndexes();
65
66   // Looks up abs32 label. Returns null if none found.
67   Label* FindAbs32Label(RVA rva);
68
69   // Looks up rel32 label. Returns null if none found.
70   Label* FindRel32Label(RVA rva);
71
72   // Uses |gen| to initializes |*_label_annotations_|.
73   CheckBool AnnotateLabels(const InstructionGenerator& gen);
74
75   // Initializes |encoded| by injecting basic data and Label data.
76   bool PrepareEncodedProgram(EncodedProgram* encoded) const;
77
78  private:
79   static const int kLabelLowerLimit;
80
81   // Looks up a label or creates a new one.  Might return NULL.
82   Label* FindLabel(RVA rva, RVAToLabel* labels);
83
84   const ExecutableType kind_;
85   const uint64_t image_base_;  // Desired or mandated base address of image.
86
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_;
91
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_;
96
97   DISALLOW_COPY_AND_ASSIGN(AssemblyProgram);
98 };
99
100 }  // namespace courgette
101
102 #endif  // COURGETTE_ASSEMBLY_PROGRAM_H_