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> 24 #include <unordered_map> 25 26 #include <unwindstack/Elf.h> 27 #include <unwindstack/ElfInterface.h> 28 #include <unwindstack/Memory.h> 29 #include <unwindstack/Regs.h> 30 31 #include "ElfInterfaceArm.h" 32 33 namespace unwindstack { 34 35 struct StepData { StepDataStepData36 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 { FunctionDataFunctionData43 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: ElfFake(Memory * memory)51 ElfFake(Memory* memory) : Elf(memory) { valid_ = true; } 52 virtual ~ElfFake() = default; 53 FakeSetValid(bool valid)54 void FakeSetValid(bool valid) { valid_ = valid; } 55 FakeSetLoadBias(uint64_t load_bias)56 void FakeSetLoadBias(uint64_t load_bias) { load_bias_ = load_bias; } 57 FakeSetArch(ArchEnum arch)58 void FakeSetArch(ArchEnum arch) { arch_ = arch; } 59 FakeSetInterface(ElfInterface * interface)60 void FakeSetInterface(ElfInterface* interface) { interface_.reset(interface); } FakeSetGnuDebugdataInterface(ElfInterface * interface)61 void FakeSetGnuDebugdataInterface(ElfInterface* interface) { 62 gnu_debugdata_interface_.reset(interface); 63 } 64 }; 65 66 class ElfInterfaceFake : public ElfInterface { 67 public: ElfInterfaceFake(Memory * memory)68 ElfInterfaceFake(Memory* memory) : ElfInterface(memory) {} 69 virtual ~ElfInterfaceFake() = default; 70 Init(int64_t *)71 bool Init(int64_t*) override { return false; } InitHeaders()72 void InitHeaders() override {} GetSoname()73 std::string GetSoname() override { return fake_soname_; } 74 75 bool GetFunctionName(uint64_t, std::string*, uint64_t*) override; 76 bool GetGlobalVariable(const std::string&, uint64_t*) override; GetBuildID()77 std::string GetBuildID() override { return fake_build_id_; } 78 79 bool Step(uint64_t, Regs*, Memory*, bool*) override; 80 FakeSetGlobalVariable(const std::string & global,uint64_t offset)81 void FakeSetGlobalVariable(const std::string& global, uint64_t offset) { 82 globals_[global] = offset; 83 } 84 FakeSetBuildID(std::string & build_id)85 void FakeSetBuildID(std::string& build_id) { fake_build_id_ = build_id; } FakeSetBuildID(const char * build_id)86 void FakeSetBuildID(const char* build_id) { fake_build_id_ = build_id; } 87 FakeSetSoname(const char * soname)88 void FakeSetSoname(const char* soname) { fake_soname_ = soname; } 89 FakePushFunctionData(const FunctionData data)90 static void FakePushFunctionData(const FunctionData data) { functions_.push_back(data); } FakePushStepData(const StepData data)91 static void FakePushStepData(const StepData data) { steps_.push_back(data); } 92 FakeClear()93 static void FakeClear() { 94 functions_.clear(); 95 steps_.clear(); 96 } 97 FakeSetErrorCode(ErrorCode code)98 void FakeSetErrorCode(ErrorCode code) { last_error_.code = code; } 99 FakeSetErrorAddress(uint64_t address)100 void FakeSetErrorAddress(uint64_t address) { last_error_.address = address; } 101 FakeSetDataOffset(uint64_t offset)102 void FakeSetDataOffset(uint64_t offset) { data_offset_ = offset; } FakeSetDataVaddrStart(uint64_t vaddr)103 void FakeSetDataVaddrStart(uint64_t vaddr) { data_vaddr_start_ = vaddr; } FakeSetDataVaddrEnd(uint64_t vaddr)104 void FakeSetDataVaddrEnd(uint64_t vaddr) { data_vaddr_end_ = vaddr; } 105 FakeSetDynamicOffset(uint64_t offset)106 void FakeSetDynamicOffset(uint64_t offset) { dynamic_offset_ = offset; } FakeSetDynamicVaddrStart(uint64_t vaddr)107 void FakeSetDynamicVaddrStart(uint64_t vaddr) { dynamic_vaddr_start_ = vaddr; } FakeSetDynamicVaddrEnd(uint64_t vaddr)108 void FakeSetDynamicVaddrEnd(uint64_t vaddr) { dynamic_vaddr_end_ = vaddr; } 109 FakeSetGnuDebugdataOffset(uint64_t offset)110 void FakeSetGnuDebugdataOffset(uint64_t offset) { gnu_debugdata_offset_ = offset; } FakeSetGnuDebugdataSize(uint64_t size)111 void FakeSetGnuDebugdataSize(uint64_t size) { gnu_debugdata_size_ = size; } 112 113 private: 114 std::unordered_map<std::string, uint64_t> globals_; 115 std::string fake_build_id_; 116 std::string fake_soname_; 117 118 static std::deque<FunctionData> functions_; 119 static std::deque<StepData> steps_; 120 }; 121 122 class ElfInterface32Fake : public ElfInterface32 { 123 public: ElfInterface32Fake(Memory * memory)124 ElfInterface32Fake(Memory* memory) : ElfInterface32(memory) {} 125 virtual ~ElfInterface32Fake() = default; 126 FakeSetEhFrameOffset(uint64_t offset)127 void FakeSetEhFrameOffset(uint64_t offset) { eh_frame_offset_ = offset; } FakeSetEhFrameSize(uint64_t size)128 void FakeSetEhFrameSize(uint64_t size) { eh_frame_size_ = size; } FakeSetDebugFrameOffset(uint64_t offset)129 void FakeSetDebugFrameOffset(uint64_t offset) { debug_frame_offset_ = offset; } FakeSetDebugFrameSize(uint64_t size)130 void FakeSetDebugFrameSize(uint64_t size) { debug_frame_size_ = size; } 131 }; 132 133 class ElfInterface64Fake : public ElfInterface64 { 134 public: ElfInterface64Fake(Memory * memory)135 ElfInterface64Fake(Memory* memory) : ElfInterface64(memory) {} 136 virtual ~ElfInterface64Fake() = default; 137 FakeSetEhFrameOffset(uint64_t offset)138 void FakeSetEhFrameOffset(uint64_t offset) { eh_frame_offset_ = offset; } FakeSetEhFrameSize(uint64_t size)139 void FakeSetEhFrameSize(uint64_t size) { eh_frame_size_ = size; } FakeSetDebugFrameOffset(uint64_t offset)140 void FakeSetDebugFrameOffset(uint64_t offset) { debug_frame_offset_ = offset; } FakeSetDebugFrameSize(uint64_t size)141 void FakeSetDebugFrameSize(uint64_t size) { debug_frame_size_ = size; } 142 }; 143 144 class ElfInterfaceArmFake : public ElfInterfaceArm { 145 public: ElfInterfaceArmFake(Memory * memory)146 ElfInterfaceArmFake(Memory* memory) : ElfInterfaceArm(memory) {} 147 virtual ~ElfInterfaceArmFake() = default; 148 FakeSetStartOffset(uint64_t offset)149 void FakeSetStartOffset(uint64_t offset) { start_offset_ = offset; } FakeSetTotalEntries(size_t entries)150 void FakeSetTotalEntries(size_t entries) { total_entries_ = entries; } 151 }; 152 153 } // namespace unwindstack 154 155 #endif // _LIBUNWINDSTACK_TESTS_ELF_FAKE_H 156