blob: 099026ce059d13a35c2cd56b380b87e56b756423 [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
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070058 void FakeSetInterface(ElfInterface* interface) { interface_.reset(interface); }
Christopher Ferris150db122017-12-20 18:49:01 -080059 void FakeSetGnuDebugdataInterface(ElfInterface* interface) {
60 gnu_debugdata_interface_.reset(interface);
61 }
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070062};
63
64class ElfInterfaceFake : public ElfInterface {
65 public:
66 ElfInterfaceFake(Memory* memory) : ElfInterface(memory) {}
67 virtual ~ElfInterfaceFake() = default;
68
Christopher Ferrise69f4702017-10-19 16:08:58 -070069 bool Init(uint64_t*) override { return false; }
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070070 void InitHeaders() override {}
71 bool GetSoname(std::string*) override { return false; }
72
Christopher Ferrise69f4702017-10-19 16:08:58 -070073 bool GetFunctionName(uint64_t, uint64_t, std::string*, uint64_t*) override;
Christopher Ferris150db122017-12-20 18:49:01 -080074 bool GetGlobalVariable(const std::string&, uint64_t*) override;
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070075
Christopher Ferrise7b66242017-12-15 11:17:45 -080076 bool Step(uint64_t, uint64_t, Regs*, Memory*, bool*) override;
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070077
Christopher Ferris150db122017-12-20 18:49:01 -080078 void FakeSetGlobalVariable(const std::string& global, uint64_t offset) {
79 globals_[global] = offset;
80 }
81
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070082 static void FakePushFunctionData(const FunctionData data) { functions_.push_back(data); }
83 static void FakePushStepData(const StepData data) { steps_.push_back(data); }
84
85 static void FakeClear() {
86 functions_.clear();
87 steps_.clear();
88 }
89
90 private:
Christopher Ferris150db122017-12-20 18:49:01 -080091 std::unordered_map<std::string, uint64_t> globals_;
92
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070093 static std::deque<FunctionData> functions_;
94 static std::deque<StepData> steps_;
95};
96
Christopher Ferrise69f4702017-10-19 16:08:58 -070097class ElfInterface32Fake : public ElfInterface32 {
98 public:
99 ElfInterface32Fake(Memory* memory) : ElfInterface32(memory) {}
100 virtual ~ElfInterface32Fake() = default;
101
102 void FakeSetEhFrameOffset(uint64_t offset) { eh_frame_offset_ = offset; }
103 void FakeSetEhFrameSize(uint64_t size) { eh_frame_size_ = size; }
104 void FakeSetDebugFrameOffset(uint64_t offset) { debug_frame_offset_ = offset; }
105 void FakeSetDebugFrameSize(uint64_t size) { debug_frame_size_ = size; }
106};
107
108class ElfInterface64Fake : public ElfInterface64 {
109 public:
110 ElfInterface64Fake(Memory* memory) : ElfInterface64(memory) {}
111 virtual ~ElfInterface64Fake() = default;
112
113 void FakeSetEhFrameOffset(uint64_t offset) { eh_frame_offset_ = offset; }
114 void FakeSetEhFrameSize(uint64_t size) { eh_frame_size_ = size; }
115 void FakeSetDebugFrameOffset(uint64_t offset) { debug_frame_offset_ = offset; }
116 void FakeSetDebugFrameSize(uint64_t size) { debug_frame_size_ = size; }
117};
118
119class ElfInterfaceArmFake : public ElfInterfaceArm {
120 public:
121 ElfInterfaceArmFake(Memory* memory) : ElfInterfaceArm(memory) {}
122 virtual ~ElfInterfaceArmFake() = default;
123
124 void FakeSetStartOffset(uint64_t offset) { start_offset_ = offset; }
125 void FakeSetTotalEntries(size_t entries) { total_entries_ = entries; }
126};
127
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700128} // namespace unwindstack
129
130#endif // _LIBUNWINDSTACK_TESTS_ELF_FAKE_H