blob: da2ddc0279732065620025ff3b4b583f9c2061ba [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
39class Elf {
40 public:
41 Elf(Memory* memory) : memory_(memory) {}
42 virtual ~Elf() = default;
43
Christopher Ferrise69f4702017-10-19 16:08:58 -070044 bool Init(bool init_gnu_debugdata);
Christopher Ferris3958f802017-02-01 15:44:40 -080045
46 void InitGnuDebugdata();
47
Christopher Ferrisd226a512017-07-14 10:37:19 -070048 bool GetSoname(std::string* name);
Christopher Ferris3958f802017-02-01 15:44:40 -080049
Christopher Ferrisd226a512017-07-14 10:37:19 -070050 bool GetFunctionName(uint64_t addr, std::string* name, uint64_t* func_offset);
Christopher Ferris3958f802017-02-01 15:44:40 -080051
Christopher Ferrisd226a512017-07-14 10:37:19 -070052 uint64_t GetRelPc(uint64_t pc, const MapInfo* map_info);
53
Christopher Ferrisc3d79f72017-11-28 19:14:54 -080054 bool Step(uint64_t rel_pc, uint64_t adjusted_rel_pc, uint64_t elf_offset, Regs* regs,
55 Memory* process_memory, bool* finished);
Christopher Ferris3958f802017-02-01 15:44:40 -080056
57 ElfInterface* CreateInterfaceFromMemory(Memory* memory);
58
Christopher Ferrise69f4702017-10-19 16:08:58 -070059 uint64_t GetLoadBias() { return load_bias_; }
Christopher Ferrisd226a512017-07-14 10:37:19 -070060
Christopher Ferris3958f802017-02-01 15:44:40 -080061 bool valid() { return valid_; }
62
63 uint32_t machine_type() { return machine_type_; }
64
65 uint8_t class_type() { return class_type_; }
66
67 Memory* memory() { return memory_.get(); }
68
69 ElfInterface* interface() { return interface_.get(); }
70
Christopher Ferrisbae69f12017-06-28 14:51:54 -070071 ElfInterface* gnu_debugdata_interface() { return gnu_debugdata_interface_.get(); }
72
Christopher Ferris3958f802017-02-01 15:44:40 -080073 static bool IsValidElf(Memory* memory);
74
Christopher Ferris3f805ac2017-08-30 13:15:19 -070075 static void GetInfo(Memory* memory, bool* valid, uint64_t* size);
76
Christopher Ferris3958f802017-02-01 15:44:40 -080077 protected:
78 bool valid_ = false;
Christopher Ferrise69f4702017-10-19 16:08:58 -070079 uint64_t load_bias_ = 0;
Christopher Ferris3958f802017-02-01 15:44:40 -080080 std::unique_ptr<ElfInterface> interface_;
81 std::unique_ptr<Memory> memory_;
82 uint32_t machine_type_;
83 uint8_t class_type_;
Christopher Ferrisbe788d82017-11-27 14:50:38 -080084 // Protect calls that can modify internal state of the interface object.
85 std::mutex lock_;
Christopher Ferrisbae69f12017-06-28 14:51:54 -070086
87 std::unique_ptr<Memory> gnu_debugdata_memory_;
88 std::unique_ptr<ElfInterface> gnu_debugdata_interface_;
Christopher Ferris3958f802017-02-01 15:44:40 -080089};
90
Christopher Ferrisd226a512017-07-14 10:37:19 -070091} // namespace unwindstack
92
Christopher Ferris3958f802017-02-01 15:44:40 -080093#endif // _LIBUNWINDSTACK_ELF_H