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_INTERFACE_ARM_H
18 #define _LIBUNWINDSTACK_ELF_INTERFACE_ARM_H
19 
20 #include <elf.h>
21 #include <stdint.h>
22 
23 #include <iterator>
24 #include <unordered_map>
25 
26 #include "ElfInterface.h"
27 #include "Memory.h"
28 
29 class ElfInterfaceArm : public ElfInterface32 {
30  public:
ElfInterfaceArm(Memory * memory)31   ElfInterfaceArm(Memory* memory) : ElfInterface32(memory) {}
32   virtual ~ElfInterfaceArm() = default;
33 
34   class iterator : public std::iterator<std::bidirectional_iterator_tag, uint32_t> {
35    public:
iterator(ElfInterfaceArm * interface,size_t index)36     iterator(ElfInterfaceArm* interface, size_t index) : interface_(interface), index_(index) { }
37 
38     iterator& operator++() { index_++; return *this; }
39     iterator& operator++(int increment) { index_ += increment; return *this; }
40     iterator& operator--() { index_--; return *this; }
41     iterator& operator--(int decrement) { index_ -= decrement; return *this; }
42 
43     bool operator==(const iterator& rhs) { return this->index_ == rhs.index_; }
44     bool operator!=(const iterator& rhs) { return this->index_ != rhs.index_; }
45 
46     uint32_t operator*() {
47       uint32_t addr = interface_->addrs_[index_];
48       if (addr == 0) {
49         if (!interface_->GetPrel31Addr(interface_->start_offset_ + index_ * 8, &addr)) {
50           return 0;
51         }
52         interface_->addrs_[index_] = addr;
53       }
54       return addr;
55     }
56 
57    private:
58     ElfInterfaceArm* interface_ = nullptr;
59     size_t index_ = 0;
60   };
61 
begin()62   iterator begin() { return iterator(this, 0); }
end()63   iterator end() { return iterator(this, total_entries_); }
64 
65   bool GetPrel31Addr(uint32_t offset, uint32_t* addr);
66 
67   bool FindEntry(uint32_t pc, uint64_t* entry_offset);
68 
69   bool HandleType(uint64_t offset, uint32_t type) override;
70 
71   bool Step(uint64_t pc, Regs* regs, Memory* process_memory) override;
72 
73   bool StepExidx(uint64_t pc, Regs* regs, Memory* process_memory);
74 
start_offset()75   uint64_t start_offset() { return start_offset_; }
76 
set_start_offset(uint64_t start_offset)77   void set_start_offset(uint64_t start_offset) { start_offset_ = start_offset; }
78 
total_entries()79   size_t total_entries() { return total_entries_; }
80 
set_total_entries(size_t total_entries)81   void set_total_entries(size_t total_entries) { total_entries_ = total_entries; }
82 
83  private:
84   uint64_t start_offset_ = 0;
85   size_t total_entries_ = 0;
86 
87   std::unordered_map<size_t, uint32_t> addrs_;
88 };
89 
90 #endif  // _LIBUNWINDSTACK_ELF_INTERFACE_ARM_H
91