| Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright (C) 2017 The Android Open Source Project | 
 | 3 |  * | 
 | 4 |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
 | 5 |  * you may not use this file except in compliance with the License. | 
 | 6 |  * You may obtain a copy of the License at | 
 | 7 |  * | 
 | 8 |  *      http://www.apache.org/licenses/LICENSE-2.0 | 
 | 9 |  * | 
 | 10 |  * Unless required by applicable law or agreed to in writing, software | 
 | 11 |  * distributed under the License is distributed on an "AS IS" BASIS, | 
 | 12 |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
 | 13 |  * See the License for the specific language governing permissions and | 
 | 14 |  * limitations under the License. | 
 | 15 |  */ | 
 | 16 |  | 
 | 17 | #ifndef _LIBUNWINDSTACK_TESTS_ELF_FAKE_H | 
 | 18 | #define _LIBUNWINDSTACK_TESTS_ELF_FAKE_H | 
 | 19 |  | 
 | 20 | #include <stdint.h> | 
 | 21 |  | 
 | 22 | #include <deque> | 
 | 23 | #include <string> | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 24 | #include <unordered_map> | 
| Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 25 |  | 
 | 26 | #include <unwindstack/Elf.h> | 
 | 27 | #include <unwindstack/ElfInterface.h> | 
 | 28 | #include <unwindstack/Memory.h> | 
 | 29 | #include <unwindstack/Regs.h> | 
 | 30 |  | 
| Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 31 | #include "ElfInterfaceArm.h" | 
 | 32 |  | 
| Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 33 | namespace unwindstack { | 
 | 34 |  | 
 | 35 | struct StepData { | 
 | 36 |   StepData(uint64_t pc, uint64_t sp, bool finished) : pc(pc), sp(sp), finished(finished) {} | 
 | 37 |   uint64_t pc; | 
 | 38 |   uint64_t sp; | 
 | 39 |   bool finished; | 
 | 40 | }; | 
 | 41 |  | 
 | 42 | struct FunctionData { | 
 | 43 |   FunctionData(std::string name, uint64_t offset) : name(name), offset(offset) {} | 
 | 44 |  | 
 | 45 |   std::string name; | 
 | 46 |   uint64_t offset; | 
 | 47 | }; | 
 | 48 |  | 
 | 49 | class ElfFake : public Elf { | 
 | 50 |  public: | 
 | 51 |   ElfFake(Memory* memory) : Elf(memory) { valid_ = true; } | 
 | 52 |   virtual ~ElfFake() = default; | 
 | 53 |  | 
| Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 54 |   void FakeSetValid(bool valid) { valid_ = valid; } | 
 | 55 |  | 
 | 56 |   void FakeSetLoadBias(uint64_t load_bias) { load_bias_ = load_bias; } | 
 | 57 |  | 
| Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 58 |   void FakeSetInterface(ElfInterface* interface) { interface_.reset(interface); } | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 59 |   void FakeSetGnuDebugdataInterface(ElfInterface* interface) { | 
 | 60 |     gnu_debugdata_interface_.reset(interface); | 
 | 61 |   } | 
| Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 62 | }; | 
 | 63 |  | 
 | 64 | class ElfInterfaceFake : public ElfInterface { | 
 | 65 |  public: | 
 | 66 |   ElfInterfaceFake(Memory* memory) : ElfInterface(memory) {} | 
 | 67 |   virtual ~ElfInterfaceFake() = default; | 
 | 68 |  | 
| Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 69 |   bool Init(int64_t*) override { return false; } | 
 | 70 |   void InitHeaders() override {} | 
| Christopher Ferris | 02a6c44 | 2019-03-11 14:43:33 -0700 | [diff] [blame] | 71 |   std::string GetSoname() override { return fake_soname_; } | 
| Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 72 |  | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 73 |   bool GetFunctionName(uint64_t, std::string*, uint64_t*) override; | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 74 |   bool GetGlobalVariable(const std::string&, uint64_t*) override; | 
| Christopher Ferris | bf373ed | 2019-01-16 17:23:39 -0800 | [diff] [blame] | 75 |   std::string GetBuildID() override { return fake_build_id_; } | 
| Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 76 |  | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 77 |   bool Step(uint64_t, Regs*, Memory*, bool*) override; | 
| Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 78 |  | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 79 |   void FakeSetGlobalVariable(const std::string& global, uint64_t offset) { | 
 | 80 |     globals_[global] = offset; | 
 | 81 |   } | 
 | 82 |  | 
| Christopher Ferris | bf373ed | 2019-01-16 17:23:39 -0800 | [diff] [blame] | 83 |   void FakeSetBuildID(std::string& build_id) { fake_build_id_ = build_id; } | 
 | 84 |   void FakeSetBuildID(const char* build_id) { fake_build_id_ = build_id; } | 
 | 85 |  | 
| Christopher Ferris | 02a6c44 | 2019-03-11 14:43:33 -0700 | [diff] [blame] | 86 |   void FakeSetSoname(const char* soname) { fake_soname_ = soname; } | 
 | 87 |  | 
| Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 88 |   static void FakePushFunctionData(const FunctionData data) { functions_.push_back(data); } | 
 | 89 |   static void FakePushStepData(const StepData data) { steps_.push_back(data); } | 
 | 90 |  | 
 | 91 |   static void FakeClear() { | 
 | 92 |     functions_.clear(); | 
 | 93 |     steps_.clear(); | 
 | 94 |   } | 
 | 95 |  | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 96 |   void FakeSetErrorCode(ErrorCode code) { last_error_.code = code; } | 
 | 97 |  | 
 | 98 |   void FakeSetErrorAddress(uint64_t address) { last_error_.address = address; } | 
 | 99 |  | 
| Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 100 |   void FakeSetDataOffset(uint64_t offset) { data_offset_ = offset; } | 
 | 101 |   void FakeSetDataVaddrStart(uint64_t vaddr) { data_vaddr_start_ = vaddr; } | 
 | 102 |   void FakeSetDataVaddrEnd(uint64_t vaddr) { data_vaddr_end_ = vaddr; } | 
 | 103 |  | 
 | 104 |   void FakeSetDynamicOffset(uint64_t offset) { dynamic_offset_ = offset; } | 
 | 105 |   void FakeSetDynamicVaddrStart(uint64_t vaddr) { dynamic_vaddr_start_ = vaddr; } | 
 | 106 |   void FakeSetDynamicVaddrEnd(uint64_t vaddr) { dynamic_vaddr_end_ = vaddr; } | 
 | 107 |  | 
| Christopher Ferris | 8726d3a | 2019-12-19 16:10:53 -0800 | [diff] [blame] | 108 |   void FakeSetGnuDebugdataOffset(uint64_t offset) { gnu_debugdata_offset_ = offset; } | 
 | 109 |   void FakeSetGnuDebugdataSize(uint64_t size) { gnu_debugdata_size_ = size; } | 
 | 110 |  | 
| Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 111 |  private: | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 112 |   std::unordered_map<std::string, uint64_t> globals_; | 
| Christopher Ferris | bf373ed | 2019-01-16 17:23:39 -0800 | [diff] [blame] | 113 |   std::string fake_build_id_; | 
| Christopher Ferris | 02a6c44 | 2019-03-11 14:43:33 -0700 | [diff] [blame] | 114 |   std::string fake_soname_; | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 115 |  | 
| Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 116 |   static std::deque<FunctionData> functions_; | 
 | 117 |   static std::deque<StepData> steps_; | 
 | 118 | }; | 
 | 119 |  | 
| Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 120 | class ElfInterface32Fake : public ElfInterface32 { | 
 | 121 |  public: | 
 | 122 |   ElfInterface32Fake(Memory* memory) : ElfInterface32(memory) {} | 
 | 123 |   virtual ~ElfInterface32Fake() = default; | 
 | 124 |  | 
 | 125 |   void FakeSetEhFrameOffset(uint64_t offset) { eh_frame_offset_ = offset; } | 
 | 126 |   void FakeSetEhFrameSize(uint64_t size) { eh_frame_size_ = size; } | 
 | 127 |   void FakeSetDebugFrameOffset(uint64_t offset) { debug_frame_offset_ = offset; } | 
 | 128 |   void FakeSetDebugFrameSize(uint64_t size) { debug_frame_size_ = size; } | 
 | 129 | }; | 
 | 130 |  | 
 | 131 | class ElfInterface64Fake : public ElfInterface64 { | 
 | 132 |  public: | 
 | 133 |   ElfInterface64Fake(Memory* memory) : ElfInterface64(memory) {} | 
 | 134 |   virtual ~ElfInterface64Fake() = default; | 
 | 135 |  | 
 | 136 |   void FakeSetEhFrameOffset(uint64_t offset) { eh_frame_offset_ = offset; } | 
 | 137 |   void FakeSetEhFrameSize(uint64_t size) { eh_frame_size_ = size; } | 
 | 138 |   void FakeSetDebugFrameOffset(uint64_t offset) { debug_frame_offset_ = offset; } | 
 | 139 |   void FakeSetDebugFrameSize(uint64_t size) { debug_frame_size_ = size; } | 
 | 140 | }; | 
 | 141 |  | 
 | 142 | class ElfInterfaceArmFake : public ElfInterfaceArm { | 
 | 143 |  public: | 
 | 144 |   ElfInterfaceArmFake(Memory* memory) : ElfInterfaceArm(memory) {} | 
 | 145 |   virtual ~ElfInterfaceArmFake() = default; | 
 | 146 |  | 
 | 147 |   void FakeSetStartOffset(uint64_t offset) { start_offset_ = offset; } | 
 | 148 |   void FakeSetTotalEntries(size_t entries) { total_entries_ = entries; } | 
 | 149 | }; | 
 | 150 |  | 
| Christopher Ferris | f6f691b | 2017-09-25 19:23:07 -0700 | [diff] [blame] | 151 | }  // namespace unwindstack | 
 | 152 |  | 
 | 153 | #endif  // _LIBUNWINDSTACK_TESTS_ELF_FAKE_H |