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 #pragma once
18 
19 #include <stdint.h>
20 
21 #include <memory>
22 
23 namespace unwindstack {
24 
25 // Forward declarations.
26 class Memory;
27 
28 class DwarfMemory {
29  public:
DwarfMemory(std::shared_ptr<Memory> & memory)30   DwarfMemory(std::shared_ptr<Memory>& memory) : memory_(memory) {}
31   virtual ~DwarfMemory() = default;
32 
33   bool ReadBytes(void* dst, size_t num_bytes);
34 
35   template <typename SignedType>
36   bool ReadSigned(uint64_t* value);
37 
38   bool ReadULEB128(uint64_t* value);
39 
40   bool ReadSLEB128(int64_t* value);
41 
42   template <typename AddressType>
43   size_t GetEncodedSize(uint8_t encoding);
44 
45   bool AdjustEncodedValue(uint8_t encoding, uint64_t* value);
46 
47   template <typename AddressType>
48   bool ReadEncodedValue(uint8_t encoding, uint64_t* value);
49 
cur_offset()50   uint64_t cur_offset() { return cur_offset_; }
set_cur_offset(uint64_t cur_offset)51   void set_cur_offset(uint64_t cur_offset) { cur_offset_ = cur_offset; }
52 
set_pc_offset(int64_t offset)53   void set_pc_offset(int64_t offset) { pc_offset_ = offset; }
clear_pc_offset()54   void clear_pc_offset() { pc_offset_ = INT64_MAX; }
55 
set_data_offset(uint64_t offset)56   void set_data_offset(uint64_t offset) { data_offset_ = offset; }
clear_data_offset()57   void clear_data_offset() { data_offset_ = static_cast<uint64_t>(-1); }
58 
set_func_offset(uint64_t offset)59   void set_func_offset(uint64_t offset) { func_offset_ = offset; }
clear_func_offset()60   void clear_func_offset() { func_offset_ = static_cast<uint64_t>(-1); }
61 
set_text_offset(uint64_t offset)62   void set_text_offset(uint64_t offset) { text_offset_ = offset; }
clear_text_offset()63   void clear_text_offset() { text_offset_ = static_cast<uint64_t>(-1); }
64 
65  private:
66   std::shared_ptr<Memory> memory_;
67   uint64_t cur_offset_ = 0;
68 
69   int64_t pc_offset_ = INT64_MAX;
70   uint64_t data_offset_ = static_cast<uint64_t>(-1);
71   uint64_t func_offset_ = static_cast<uint64_t>(-1);
72   uint64_t text_offset_ = static_cast<uint64_t>(-1);
73 };
74 
75 }  // namespace unwindstack
76