Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 1 | /* |
| 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 Ferris | be788d8 | 2017-11-27 14:50:38 -0800 | [diff] [blame] | 23 | #include <mutex> |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 24 | #include <string> |
| 25 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 26 | #include <unwindstack/ElfInterface.h> |
| 27 | #include <unwindstack/Memory.h> |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 28 | |
| 29 | #if !defined(EM_AARCH64) |
| 30 | #define EM_AARCH64 183 |
| 31 | #endif |
| 32 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 33 | namespace unwindstack { |
| 34 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 35 | // Forward declaration. |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 36 | struct MapInfo; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 37 | class Regs; |
| 38 | |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame^] | 39 | enum ArchEnum : uint8_t { |
| 40 | ARCH_UNKNOWN = 0, |
| 41 | ARCH_ARM, |
| 42 | ARCH_ARM64, |
| 43 | ARCH_X86, |
| 44 | ARCH_X86_64, |
| 45 | }; |
| 46 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 47 | class Elf { |
| 48 | public: |
| 49 | Elf(Memory* memory) : memory_(memory) {} |
| 50 | virtual ~Elf() = default; |
| 51 | |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 52 | bool Init(bool init_gnu_debugdata); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 53 | |
| 54 | void InitGnuDebugdata(); |
| 55 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 56 | bool GetSoname(std::string* name); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 57 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 58 | bool GetFunctionName(uint64_t addr, std::string* name, uint64_t* func_offset); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 59 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 60 | uint64_t GetRelPc(uint64_t pc, const MapInfo* map_info); |
| 61 | |
Christopher Ferris | c3d79f7 | 2017-11-28 19:14:54 -0800 | [diff] [blame] | 62 | bool Step(uint64_t rel_pc, uint64_t adjusted_rel_pc, uint64_t elf_offset, Regs* regs, |
| 63 | Memory* process_memory, bool* finished); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 64 | |
| 65 | ElfInterface* CreateInterfaceFromMemory(Memory* memory); |
| 66 | |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 67 | uint64_t GetLoadBias() { return load_bias_; } |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 68 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 69 | bool valid() { return valid_; } |
| 70 | |
| 71 | uint32_t machine_type() { return machine_type_; } |
| 72 | |
| 73 | uint8_t class_type() { return class_type_; } |
| 74 | |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame^] | 75 | ArchEnum arch() { return arch_; } |
| 76 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 77 | Memory* memory() { return memory_.get(); } |
| 78 | |
| 79 | ElfInterface* interface() { return interface_.get(); } |
| 80 | |
Christopher Ferris | bae69f1 | 2017-06-28 14:51:54 -0700 | [diff] [blame] | 81 | ElfInterface* gnu_debugdata_interface() { return gnu_debugdata_interface_.get(); } |
| 82 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 83 | static bool IsValidElf(Memory* memory); |
| 84 | |
Christopher Ferris | 3f805ac | 2017-08-30 13:15:19 -0700 | [diff] [blame] | 85 | static void GetInfo(Memory* memory, bool* valid, uint64_t* size); |
| 86 | |
Christopher Ferris | b7de5f5 | 2017-12-01 21:37:37 -0800 | [diff] [blame] | 87 | static uint64_t GetLoadBias(Memory* memory); |
| 88 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 89 | protected: |
| 90 | bool valid_ = false; |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 91 | uint64_t load_bias_ = 0; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 92 | std::unique_ptr<ElfInterface> interface_; |
| 93 | std::unique_ptr<Memory> memory_; |
| 94 | uint32_t machine_type_; |
| 95 | uint8_t class_type_; |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame^] | 96 | ArchEnum arch_; |
Christopher Ferris | be788d8 | 2017-11-27 14:50:38 -0800 | [diff] [blame] | 97 | // Protect calls that can modify internal state of the interface object. |
| 98 | std::mutex lock_; |
Christopher Ferris | bae69f1 | 2017-06-28 14:51:54 -0700 | [diff] [blame] | 99 | |
| 100 | std::unique_ptr<Memory> gnu_debugdata_memory_; |
| 101 | std::unique_ptr<ElfInterface> gnu_debugdata_interface_; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 102 | }; |
| 103 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 104 | } // namespace unwindstack |
| 105 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 106 | #endif // _LIBUNWINDSTACK_ELF_H |