blob: 3b6cb80324052538b56e90ed027ec9a95eecfb63 [file] [log] [blame]
Christopher Ferrisf6f691b2017-09-25 19:23:07 -07001/*
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 Ferris150db122017-12-20 18:49:01 -080024#include <unordered_map>
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070025
26#include <unwindstack/Elf.h>
27#include <unwindstack/ElfInterface.h>
28#include <unwindstack/Memory.h>
29#include <unwindstack/Regs.h>
30
Christopher Ferrise69f4702017-10-19 16:08:58 -070031#include "ElfInterfaceArm.h"
32
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070033namespace unwindstack {
34
35struct 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
42struct FunctionData {
43 FunctionData(std::string name, uint64_t offset) : name(name), offset(offset) {}
44
45 std::string name;
46 uint64_t offset;
47};
48
49class ElfFake : public Elf {
50 public:
51 ElfFake(Memory* memory) : Elf(memory) { valid_ = true; }
52 virtual ~ElfFake() = default;
53
Christopher Ferrise69f4702017-10-19 16:08:58 -070054 void FakeSetValid(bool valid) { valid_ = valid; }
55
56 void FakeSetLoadBias(uint64_t load_bias) { load_bias_ = load_bias; }
57
Yong Lifbca3002020-04-23 20:40:29 +000058 void FakeSetArch(ArchEnum arch) { arch_ = arch; }
59
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070060 void FakeSetInterface(ElfInterface* interface) { interface_.reset(interface); }
Christopher Ferris150db122017-12-20 18:49:01 -080061 void FakeSetGnuDebugdataInterface(ElfInterface* interface) {
62 gnu_debugdata_interface_.reset(interface);
63 }
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070064};
65
66class ElfInterfaceFake : public ElfInterface {
67 public:
68 ElfInterfaceFake(Memory* memory) : ElfInterface(memory) {}
69 virtual ~ElfInterfaceFake() = default;
70
Christopher Ferris819f1312019-10-03 13:35:48 -070071 bool Init(int64_t*) override { return false; }
72 void InitHeaders() override {}
Christopher Ferris02a6c442019-03-11 14:43:33 -070073 std::string GetSoname() override { return fake_soname_; }
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070074
Christopher Ferris4cc36d22018-06-06 14:47:31 -070075 bool GetFunctionName(uint64_t, std::string*, uint64_t*) override;
Christopher Ferris150db122017-12-20 18:49:01 -080076 bool GetGlobalVariable(const std::string&, uint64_t*) override;
Christopher Ferrisbf373ed2019-01-16 17:23:39 -080077 std::string GetBuildID() override { return fake_build_id_; }
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070078
Christopher Ferris4cc36d22018-06-06 14:47:31 -070079 bool Step(uint64_t, Regs*, Memory*, bool*) override;
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070080
Christopher Ferris150db122017-12-20 18:49:01 -080081 void FakeSetGlobalVariable(const std::string& global, uint64_t offset) {
82 globals_[global] = offset;
83 }
84
Christopher Ferrisbf373ed2019-01-16 17:23:39 -080085 void FakeSetBuildID(std::string& build_id) { fake_build_id_ = build_id; }
86 void FakeSetBuildID(const char* build_id) { fake_build_id_ = build_id; }
87
Christopher Ferris02a6c442019-03-11 14:43:33 -070088 void FakeSetSoname(const char* soname) { fake_soname_ = soname; }
89
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070090 static void FakePushFunctionData(const FunctionData data) { functions_.push_back(data); }
91 static void FakePushStepData(const StepData data) { steps_.push_back(data); }
92
93 static void FakeClear() {
94 functions_.clear();
95 steps_.clear();
96 }
97
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -080098 void FakeSetErrorCode(ErrorCode code) { last_error_.code = code; }
99
100 void FakeSetErrorAddress(uint64_t address) { last_error_.address = address; }
101
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800102 void FakeSetDataOffset(uint64_t offset) { data_offset_ = offset; }
103 void FakeSetDataVaddrStart(uint64_t vaddr) { data_vaddr_start_ = vaddr; }
104 void FakeSetDataVaddrEnd(uint64_t vaddr) { data_vaddr_end_ = vaddr; }
105
106 void FakeSetDynamicOffset(uint64_t offset) { dynamic_offset_ = offset; }
107 void FakeSetDynamicVaddrStart(uint64_t vaddr) { dynamic_vaddr_start_ = vaddr; }
108 void FakeSetDynamicVaddrEnd(uint64_t vaddr) { dynamic_vaddr_end_ = vaddr; }
109
Christopher Ferris8726d3a2019-12-19 16:10:53 -0800110 void FakeSetGnuDebugdataOffset(uint64_t offset) { gnu_debugdata_offset_ = offset; }
111 void FakeSetGnuDebugdataSize(uint64_t size) { gnu_debugdata_size_ = size; }
112
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700113 private:
Christopher Ferris150db122017-12-20 18:49:01 -0800114 std::unordered_map<std::string, uint64_t> globals_;
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800115 std::string fake_build_id_;
Christopher Ferris02a6c442019-03-11 14:43:33 -0700116 std::string fake_soname_;
Christopher Ferris150db122017-12-20 18:49:01 -0800117
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700118 static std::deque<FunctionData> functions_;
119 static std::deque<StepData> steps_;
120};
121
Christopher Ferrise69f4702017-10-19 16:08:58 -0700122class ElfInterface32Fake : public ElfInterface32 {
123 public:
124 ElfInterface32Fake(Memory* memory) : ElfInterface32(memory) {}
125 virtual ~ElfInterface32Fake() = default;
126
127 void FakeSetEhFrameOffset(uint64_t offset) { eh_frame_offset_ = offset; }
128 void FakeSetEhFrameSize(uint64_t size) { eh_frame_size_ = size; }
129 void FakeSetDebugFrameOffset(uint64_t offset) { debug_frame_offset_ = offset; }
130 void FakeSetDebugFrameSize(uint64_t size) { debug_frame_size_ = size; }
131};
132
133class ElfInterface64Fake : public ElfInterface64 {
134 public:
135 ElfInterface64Fake(Memory* memory) : ElfInterface64(memory) {}
136 virtual ~ElfInterface64Fake() = default;
137
138 void FakeSetEhFrameOffset(uint64_t offset) { eh_frame_offset_ = offset; }
139 void FakeSetEhFrameSize(uint64_t size) { eh_frame_size_ = size; }
140 void FakeSetDebugFrameOffset(uint64_t offset) { debug_frame_offset_ = offset; }
141 void FakeSetDebugFrameSize(uint64_t size) { debug_frame_size_ = size; }
142};
143
144class ElfInterfaceArmFake : public ElfInterfaceArm {
145 public:
146 ElfInterfaceArmFake(Memory* memory) : ElfInterfaceArm(memory) {}
147 virtual ~ElfInterfaceArmFake() = default;
148
149 void FakeSetStartOffset(uint64_t offset) { start_offset_ = offset; }
150 void FakeSetTotalEntries(size_t entries) { total_entries_ = entries; }
151};
152
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700153} // namespace unwindstack
154
155#endif // _LIBUNWINDSTACK_TESTS_ELF_FAKE_H