blob: cff9dc4f999d7a7272e283911ff7e2f0669f9d86 [file] [log] [blame]
Christopher Ferris3958f802017-02-01 15:44:40 -08001/*
2 * Copyright (C) 2016 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_ELF_H
18#define _LIBUNWINDSTACK_ELF_H
19
20#include <stddef.h>
21
22#include <memory>
23#include <string>
24
25#include "ElfInterface.h"
26#include "Memory.h"
27
28#if !defined(EM_AARCH64)
29#define EM_AARCH64 183
30#endif
31
32// Forward declaration.
33class Regs;
34
35class Elf {
36 public:
37 Elf(Memory* memory) : memory_(memory) {}
38 virtual ~Elf() = default;
39
40 bool Init();
41
42 void InitGnuDebugdata();
43
44 bool GetSoname(std::string* name) {
45 return valid_ && interface_->GetSoname(name);
46 }
47
Christopher Ferris8098b1c2017-06-20 13:54:08 -070048 bool GetFunctionName(uint64_t addr, std::string* name, uint64_t* func_offset) {
Christopher Ferrisbae69f12017-06-28 14:51:54 -070049 return valid_ && (interface_->GetFunctionName(addr, name, func_offset) ||
50 (gnu_debugdata_interface_ &&
51 gnu_debugdata_interface_->GetFunctionName(addr, name, func_offset)));
Christopher Ferris3958f802017-02-01 15:44:40 -080052 }
53
54 bool Step(uint64_t rel_pc, Regs* regs, Memory* process_memory) {
Christopher Ferrisbae69f12017-06-28 14:51:54 -070055 return valid_ && (interface_->Step(rel_pc, regs, process_memory) ||
56 (gnu_debugdata_interface_ &&
57 gnu_debugdata_interface_->Step(rel_pc, regs, process_memory)));
Christopher Ferris3958f802017-02-01 15:44:40 -080058 }
59
60 ElfInterface* CreateInterfaceFromMemory(Memory* memory);
61
62 bool valid() { return valid_; }
63
64 uint32_t machine_type() { return machine_type_; }
65
66 uint8_t class_type() { return class_type_; }
67
68 Memory* memory() { return memory_.get(); }
69
70 ElfInterface* interface() { return interface_.get(); }
71
Christopher Ferrisbae69f12017-06-28 14:51:54 -070072 ElfInterface* gnu_debugdata_interface() { return gnu_debugdata_interface_.get(); }
73
Christopher Ferris3958f802017-02-01 15:44:40 -080074 static bool IsValidElf(Memory* memory);
75
76 protected:
77 bool valid_ = false;
78 std::unique_ptr<ElfInterface> interface_;
79 std::unique_ptr<Memory> memory_;
80 uint32_t machine_type_;
81 uint8_t class_type_;
Christopher Ferrisbae69f12017-06-28 14:51:54 -070082
83 std::unique_ptr<Memory> gnu_debugdata_memory_;
84 std::unique_ptr<ElfInterface> gnu_debugdata_interface_;
Christopher Ferris3958f802017-02-01 15:44:40 -080085};
86
87#endif // _LIBUNWINDSTACK_ELF_H