blob: e562d6de945ddd3fd027432cd7162d7a35ce207d [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>
Christopher Ferrisbe788d82017-11-27 14:50:38 -080023#include <mutex>
Christopher Ferris3958f802017-02-01 15:44:40 -080024#include <string>
25
Christopher Ferrisd226a512017-07-14 10:37:19 -070026#include <unwindstack/ElfInterface.h>
27#include <unwindstack/Memory.h>
Christopher Ferris3958f802017-02-01 15:44:40 -080028
29#if !defined(EM_AARCH64)
30#define EM_AARCH64 183
31#endif
32
Christopher Ferrisd226a512017-07-14 10:37:19 -070033namespace unwindstack {
34
Christopher Ferris3958f802017-02-01 15:44:40 -080035// Forward declaration.
Christopher Ferrisd226a512017-07-14 10:37:19 -070036struct MapInfo;
Christopher Ferris3958f802017-02-01 15:44:40 -080037class Regs;
38
Christopher Ferrisd06001d2017-11-30 18:56:01 -080039enum ArchEnum : uint8_t {
40 ARCH_UNKNOWN = 0,
41 ARCH_ARM,
42 ARCH_ARM64,
43 ARCH_X86,
44 ARCH_X86_64,
Douglas Leung61b1a1a2017-11-08 10:53:53 +010045 ARCH_MIPS,
46 ARCH_MIPS64,
Christopher Ferrisd06001d2017-11-30 18:56:01 -080047};
48
Christopher Ferris3958f802017-02-01 15:44:40 -080049class Elf {
50 public:
51 Elf(Memory* memory) : memory_(memory) {}
52 virtual ~Elf() = default;
53
Christopher Ferrise69f4702017-10-19 16:08:58 -070054 bool Init(bool init_gnu_debugdata);
Christopher Ferris3958f802017-02-01 15:44:40 -080055
56 void InitGnuDebugdata();
57
Christopher Ferrisd226a512017-07-14 10:37:19 -070058 bool GetSoname(std::string* name);
Christopher Ferris3958f802017-02-01 15:44:40 -080059
Christopher Ferrisd226a512017-07-14 10:37:19 -070060 bool GetFunctionName(uint64_t addr, std::string* name, uint64_t* func_offset);
Christopher Ferris3958f802017-02-01 15:44:40 -080061
Christopher Ferris150db122017-12-20 18:49:01 -080062 bool GetGlobalVariable(const std::string& name, uint64_t* memory_address);
63
Christopher Ferrisd226a512017-07-14 10:37:19 -070064 uint64_t GetRelPc(uint64_t pc, const MapInfo* map_info);
65
Christopher Ferrisc3d79f72017-11-28 19:14:54 -080066 bool Step(uint64_t rel_pc, uint64_t adjusted_rel_pc, uint64_t elf_offset, Regs* regs,
67 Memory* process_memory, bool* finished);
Christopher Ferris3958f802017-02-01 15:44:40 -080068
69 ElfInterface* CreateInterfaceFromMemory(Memory* memory);
70
Christopher Ferrise69f4702017-10-19 16:08:58 -070071 uint64_t GetLoadBias() { return load_bias_; }
Christopher Ferrisd226a512017-07-14 10:37:19 -070072
Christopher Ferris150db122017-12-20 18:49:01 -080073 bool IsValidPc(uint64_t pc);
74
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -080075 void GetLastError(ErrorData* data);
76 ErrorCode GetLastErrorCode();
77 uint64_t GetLastErrorAddress();
78
Christopher Ferris3958f802017-02-01 15:44:40 -080079 bool valid() { return valid_; }
80
81 uint32_t machine_type() { return machine_type_; }
82
83 uint8_t class_type() { return class_type_; }
84
Christopher Ferrisd06001d2017-11-30 18:56:01 -080085 ArchEnum arch() { return arch_; }
86
Christopher Ferris3958f802017-02-01 15:44:40 -080087 Memory* memory() { return memory_.get(); }
88
89 ElfInterface* interface() { return interface_.get(); }
90
Christopher Ferrisbae69f12017-06-28 14:51:54 -070091 ElfInterface* gnu_debugdata_interface() { return gnu_debugdata_interface_.get(); }
92
Christopher Ferris3958f802017-02-01 15:44:40 -080093 static bool IsValidElf(Memory* memory);
94
Christopher Ferris3f805ac2017-08-30 13:15:19 -070095 static void GetInfo(Memory* memory, bool* valid, uint64_t* size);
96
Christopher Ferrisb7de5f52017-12-01 21:37:37 -080097 static uint64_t GetLoadBias(Memory* memory);
98
Christopher Ferris3958f802017-02-01 15:44:40 -080099 protected:
100 bool valid_ = false;
Christopher Ferrise69f4702017-10-19 16:08:58 -0700101 uint64_t load_bias_ = 0;
Christopher Ferris3958f802017-02-01 15:44:40 -0800102 std::unique_ptr<ElfInterface> interface_;
103 std::unique_ptr<Memory> memory_;
104 uint32_t machine_type_;
105 uint8_t class_type_;
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800106 ArchEnum arch_;
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800107 // Protect calls that can modify internal state of the interface object.
108 std::mutex lock_;
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700109
110 std::unique_ptr<Memory> gnu_debugdata_memory_;
111 std::unique_ptr<ElfInterface> gnu_debugdata_interface_;
Christopher Ferris3958f802017-02-01 15:44:40 -0800112};
113
Christopher Ferrisd226a512017-07-14 10:37:19 -0700114} // namespace unwindstack
115
Christopher Ferris3958f802017-02-01 15:44:40 -0800116#endif // _LIBUNWINDSTACK_ELF_H