Home | History | Annotate | Download | only in unwindstack
      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>
     23 #include <mutex>
     24 #include <string>
     25 #include <unordered_map>
     26 #include <utility>
     27 
     28 #include <unwindstack/ElfInterface.h>
     29 #include <unwindstack/Memory.h>
     30 
     31 #if !defined(EM_AARCH64)
     32 #define EM_AARCH64 183
     33 #endif
     34 
     35 namespace unwindstack {
     36 
     37 // Forward declaration.
     38 struct MapInfo;
     39 class Regs;
     40 
     41 enum ArchEnum : uint8_t {
     42   ARCH_UNKNOWN = 0,
     43   ARCH_ARM,
     44   ARCH_ARM64,
     45   ARCH_X86,
     46   ARCH_X86_64,
     47   ARCH_MIPS,
     48   ARCH_MIPS64,
     49 };
     50 
     51 class Elf {
     52  public:
     53   Elf(Memory* memory) : memory_(memory) {}
     54   virtual ~Elf() = default;
     55 
     56   bool Init(bool init_gnu_debugdata);
     57 
     58   void InitGnuDebugdata();
     59 
     60   bool GetSoname(std::string* name);
     61 
     62   bool GetFunctionName(uint64_t addr, std::string* name, uint64_t* func_offset);
     63 
     64   bool GetGlobalVariable(const std::string& name, uint64_t* memory_address);
     65 
     66   uint64_t GetRelPc(uint64_t pc, const MapInfo* map_info);
     67 
     68   bool Step(uint64_t rel_pc, uint64_t adjusted_rel_pc, Regs* regs, Memory* process_memory,
     69             bool* finished);
     70 
     71   ElfInterface* CreateInterfaceFromMemory(Memory* memory);
     72 
     73   uint64_t GetLoadBias() { return load_bias_; }
     74 
     75   bool IsValidPc(uint64_t pc);
     76 
     77   void GetLastError(ErrorData* data);
     78   ErrorCode GetLastErrorCode();
     79   uint64_t GetLastErrorAddress();
     80 
     81   bool valid() { return valid_; }
     82 
     83   uint32_t machine_type() { return machine_type_; }
     84 
     85   uint8_t class_type() { return class_type_; }
     86 
     87   ArchEnum arch() { return arch_; }
     88 
     89   Memory* memory() { return memory_.get(); }
     90 
     91   ElfInterface* interface() { return interface_.get(); }
     92 
     93   ElfInterface* gnu_debugdata_interface() { return gnu_debugdata_interface_.get(); }
     94 
     95   static bool IsValidElf(Memory* memory);
     96 
     97   static void GetInfo(Memory* memory, bool* valid, uint64_t* size);
     98 
     99   static uint64_t GetLoadBias(Memory* memory);
    100 
    101   static void SetCachingEnabled(bool enable);
    102   static bool CachingEnabled() { return cache_enabled_; }
    103 
    104   static void CacheLock();
    105   static void CacheUnlock();
    106   static void CacheAdd(MapInfo* info);
    107   static bool CacheGet(MapInfo* info);
    108   static bool CacheAfterCreateMemory(MapInfo* info);
    109 
    110  protected:
    111   bool valid_ = false;
    112   uint64_t load_bias_ = 0;
    113   std::unique_ptr<ElfInterface> interface_;
    114   std::unique_ptr<Memory> memory_;
    115   uint32_t machine_type_;
    116   uint8_t class_type_;
    117   ArchEnum arch_;
    118   // Protect calls that can modify internal state of the interface object.
    119   std::mutex lock_;
    120 
    121   std::unique_ptr<Memory> gnu_debugdata_memory_;
    122   std::unique_ptr<ElfInterface> gnu_debugdata_interface_;
    123 
    124   static bool cache_enabled_;
    125   static std::unordered_map<std::string, std::pair<std::shared_ptr<Elf>, bool>>* cache_;
    126   static std::mutex* cache_lock_;
    127 };
    128 
    129 }  // namespace unwindstack
    130 
    131 #endif  // _LIBUNWINDSTACK_ELF_H
    132