blob: 5f343916a164ab9ce7e3cbc52b0f88950ea15591 [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 Ferris3958f802017-02-01 15:44:40 -080075 bool valid() { return valid_; }
76
77 uint32_t machine_type() { return machine_type_; }
78
79 uint8_t class_type() { return class_type_; }
80
Christopher Ferrisd06001d2017-11-30 18:56:01 -080081 ArchEnum arch() { return arch_; }
82
Christopher Ferris3958f802017-02-01 15:44:40 -080083 Memory* memory() { return memory_.get(); }
84
85 ElfInterface* interface() { return interface_.get(); }
86
Christopher Ferrisbae69f12017-06-28 14:51:54 -070087 ElfInterface* gnu_debugdata_interface() { return gnu_debugdata_interface_.get(); }
88
Christopher Ferris3958f802017-02-01 15:44:40 -080089 static bool IsValidElf(Memory* memory);
90
Christopher Ferris3f805ac2017-08-30 13:15:19 -070091 static void GetInfo(Memory* memory, bool* valid, uint64_t* size);
92
Christopher Ferrisb7de5f52017-12-01 21:37:37 -080093 static uint64_t GetLoadBias(Memory* memory);
94
Christopher Ferris3958f802017-02-01 15:44:40 -080095 protected:
96 bool valid_ = false;
Christopher Ferrise69f4702017-10-19 16:08:58 -070097 uint64_t load_bias_ = 0;
Christopher Ferris3958f802017-02-01 15:44:40 -080098 std::unique_ptr<ElfInterface> interface_;
99 std::unique_ptr<Memory> memory_;
100 uint32_t machine_type_;
101 uint8_t class_type_;
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800102 ArchEnum arch_;
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800103 // Protect calls that can modify internal state of the interface object.
104 std::mutex lock_;
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700105
106 std::unique_ptr<Memory> gnu_debugdata_memory_;
107 std::unique_ptr<ElfInterface> gnu_debugdata_interface_;
Christopher Ferris3958f802017-02-01 15:44:40 -0800108};
109
Christopher Ferrisd226a512017-07-14 10:37:19 -0700110} // namespace unwindstack
111
Christopher Ferris3958f802017-02-01 15:44:40 -0800112#endif // _LIBUNWINDSTACK_ELF_H