assembly_program.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #ifndef COURGETTE_ASSEMBLY_PROGRAM_H_
  5. #define COURGETTE_ASSEMBLY_PROGRAM_H_
  6. #include <stdint.h>
  7. #include <vector>
  8. #include "courgette/courgette.h"
  9. #include "courgette/image_utils.h"
  10. #include "courgette/instruction_utils.h"
  11. #include "courgette/label_manager.h"
  12. #include "courgette/memory_allocator.h" // For CheckBool.
  13. namespace courgette {
  14. class EncodedProgram;
  15. // An AssemblyProgram stores Labels extracted from an executable file, and
  16. // (optionally) Label annotations. It is initialized by a Disassembler, but
  17. // stores separate state so that the Disassembler can be deleted. Typical usage:
  18. //
  19. // * The Disassembler calls PrecomputeLabels() and injects RVAs for abs32/rel32
  20. // references. These are used to initialize labels.
  21. // * The Disassembler calls DefaultAssignIndexes() to assign addresses to
  22. // positions in the address tables.
  23. // * [Optional step]
  24. // * The Disassembler can use Labels in AssemblyProgram to convert the
  25. // executable file to an EncodedProgram, serialized to an output stream.
  26. // * Later, the Disassembler can use the AssemblyProgram to can be deserialized
  27. // and assembled into the original executable file via an EncodedProgram.
  28. //
  29. // The optional step is to adjust Labels in the AssemblyProgram. One form of
  30. // adjustment is to assign indexes in such a way as to make the EncodedProgram
  31. // for an executable look more like the EncodedProgram for another exectuable.
  32. // The adjustment process should call UnassignIndexes(), do its own assignment,
  33. // and then call AssignRemainingIndexes() to ensure all indexes are assigned.
  34. class AssemblyProgram {
  35. public:
  36. AssemblyProgram(ExecutableType kind, uint64_t image_base);
  37. AssemblyProgram(const AssemblyProgram&) = delete;
  38. AssemblyProgram& operator=(const AssemblyProgram&) = delete;
  39. ~AssemblyProgram();
  40. ExecutableType kind() const { return kind_; }
  41. const std::vector<Label*>& abs32_label_annotations() const {
  42. return abs32_label_annotations_;
  43. }
  44. const std::vector<Label*>& rel32_label_annotations() const {
  45. return rel32_label_annotations_;
  46. }
  47. // Traverses RVAs in |abs32_visitor| and |rel32_visitor| to precompute Labels.
  48. void PrecomputeLabels(RvaVisitor* abs32_visitor, RvaVisitor* rel32_visitor);
  49. void UnassignIndexes();
  50. void DefaultAssignIndexes();
  51. void AssignRemainingIndexes();
  52. // Looks up abs32 label. Returns null if none found.
  53. Label* FindAbs32Label(RVA rva);
  54. // Looks up rel32 label. Returns null if none found.
  55. Label* FindRel32Label(RVA rva);
  56. // Uses |gen| to initializes |*_label_annotations_|.
  57. CheckBool AnnotateLabels(const InstructionGenerator& gen);
  58. // Initializes |encoded| by injecting basic data and Label data.
  59. bool PrepareEncodedProgram(EncodedProgram* encoded) const;
  60. private:
  61. static const int kLabelLowerLimit;
  62. // Looks up a label or creates a new one. Might return nullptr.
  63. Label* FindLabel(RVA rva, RVAToLabel* labels);
  64. const ExecutableType kind_;
  65. const uint64_t image_base_; // Desired or mandated base address of image.
  66. // Storage and lookup of Labels associated with target addresses. We use
  67. // separate abs32 and rel32 labels.
  68. LabelManager abs32_label_manager_;
  69. LabelManager rel32_label_manager_;
  70. // Label pointers for each abs32 and rel32 location, sorted by file offset.
  71. // These are used by Label adjustment during patch generation.
  72. std::vector<Label*> abs32_label_annotations_;
  73. std::vector<Label*> rel32_label_annotations_;
  74. };
  75. } // namespace courgette
  76. #endif // COURGETTE_ASSEMBLY_PROGRAM_H_