Dmitriy Ivanov | fa26eee | 2015-02-03 16:06:47 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 __LINKER_RELOC_ITERATORS_H |
| 18 | #define __LINKER_RELOC_ITERATORS_H |
| 19 | |
| 20 | #include "linker.h" |
| 21 | |
Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame^] | 22 | #include <string.h> |
| 23 | |
| 24 | #define RELOCATION_GROUPED_BY_INFO_FLAG 1 |
| 25 | #define RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG 2 |
| 26 | #define RELOCATION_GROUPED_BY_ADDEND_FLAG 4 |
| 27 | #define RELOCATION_GROUP_HAS_ADDEND_FLAG 8 |
| 28 | |
| 29 | #define RELOCATION_GROUPED_BY_INFO(flags) (((flags) & RELOCATION_GROUPED_BY_INFO_FLAG) != 0) |
| 30 | #define RELOCATION_GROUPED_BY_OFFSET_DELTA(flags) (((flags) & RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG) != 0) |
| 31 | #define RELOCATION_GROUPED_BY_ADDEND(flags) (((flags) & RELOCATION_GROUPED_BY_ADDEND_FLAG) != 0) |
| 32 | #define RELOCATION_GROUP_HAS_ADDEND(flags) (((flags) & RELOCATION_GROUP_HAS_ADDEND_FLAG) != 0) |
| 33 | |
Dmitriy Ivanov | fa26eee | 2015-02-03 16:06:47 -0800 | [diff] [blame] | 34 | class plain_reloc_iterator { |
| 35 | #if defined(USE_RELA) |
| 36 | typedef ElfW(Rela) rel_t; |
| 37 | #else |
| 38 | typedef ElfW(Rel) rel_t; |
| 39 | #endif |
| 40 | public: |
| 41 | plain_reloc_iterator(rel_t* rel_array, size_t count) |
| 42 | : begin_(rel_array), end_(begin_ + count), current_(begin_) {} |
| 43 | |
| 44 | bool has_next() { |
| 45 | return current_ < end_; |
| 46 | } |
| 47 | |
| 48 | rel_t* next() { |
| 49 | return current_++; |
| 50 | } |
| 51 | private: |
| 52 | rel_t* const begin_; |
| 53 | rel_t* const end_; |
| 54 | rel_t* current_; |
| 55 | |
| 56 | DISALLOW_COPY_AND_ASSIGN(plain_reloc_iterator); |
| 57 | }; |
| 58 | |
Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame^] | 59 | template <typename decoder_t> |
| 60 | class packed_reloc_iterator { |
| 61 | #if defined(USE_RELA) |
| 62 | typedef ElfW(Rela) rel_t; |
| 63 | #else |
| 64 | typedef ElfW(Rel) rel_t; |
| 65 | #endif |
| 66 | public: |
| 67 | explicit packed_reloc_iterator(decoder_t&& decoder) |
| 68 | : decoder_(decoder) { |
| 69 | // initialize fields |
| 70 | memset(&reloc_, 0, sizeof(reloc_)); |
| 71 | relocation_count_ = decoder_.pop_front(); |
| 72 | reloc_.r_offset = decoder_.pop_front(); |
| 73 | relocation_index_ = 0; |
| 74 | relocation_group_index_ = 0; |
| 75 | group_size_ = 0; |
| 76 | } |
| 77 | |
| 78 | bool has_next() const { |
| 79 | return relocation_index_ < relocation_count_; |
| 80 | } |
| 81 | |
| 82 | rel_t* next() { |
| 83 | if (relocation_group_index_ == group_size_) { |
| 84 | if (!read_group_fields()) { |
| 85 | // Iterator is inconsistent state; it should not be called again |
| 86 | // but in case it is let's make sure has_next() returns false. |
| 87 | relocation_index_ = relocation_count_ = 0; |
| 88 | return nullptr; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | if (RELOCATION_GROUPED_BY_OFFSET_DELTA(group_flags_)) { |
| 93 | reloc_.r_offset += group_r_offset_delta_; |
| 94 | } else { |
| 95 | reloc_.r_offset += decoder_.pop_front(); |
| 96 | } |
| 97 | |
| 98 | if (!RELOCATION_GROUPED_BY_INFO(group_flags_)) { |
| 99 | reloc_.r_info = decoder_.pop_front(); |
| 100 | } |
| 101 | |
| 102 | #if defined(USE_RELA) |
| 103 | if (RELOCATION_GROUP_HAS_ADDEND(group_flags_) && !RELOCATION_GROUPED_BY_ADDEND(group_flags_)) { |
| 104 | reloc_.r_addend += decoder_.pop_front(); |
| 105 | } |
| 106 | #endif |
| 107 | |
| 108 | relocation_index_++; |
| 109 | relocation_group_index_++; |
| 110 | |
| 111 | return &reloc_; |
| 112 | } |
| 113 | private: |
| 114 | bool read_group_fields() { |
| 115 | group_size_ = decoder_.pop_front(); |
| 116 | group_flags_ = decoder_.pop_front(); |
| 117 | |
| 118 | if (RELOCATION_GROUPED_BY_OFFSET_DELTA(group_flags_)) { |
| 119 | group_r_offset_delta_ = decoder_.pop_front(); |
| 120 | } |
| 121 | |
| 122 | if (RELOCATION_GROUPED_BY_INFO(group_flags_)) { |
| 123 | reloc_.r_info = decoder_.pop_front(); |
| 124 | } |
| 125 | |
| 126 | if (RELOCATION_GROUP_HAS_ADDEND(group_flags_) && RELOCATION_GROUPED_BY_ADDEND(group_flags_)) { |
| 127 | #if !defined(USE_RELA) |
| 128 | // This platform does not support rela, and yet we have it encoded in android_rel section. |
| 129 | DL_ERR("unexpected r_addend in android.rel section"); |
| 130 | return false; |
| 131 | #else |
| 132 | reloc_.r_addend += decoder_.pop_front(); |
| 133 | } else if (!RELOCATION_GROUP_HAS_ADDEND(group_flags_)) { |
| 134 | reloc_.r_addend = 0; |
| 135 | #endif |
| 136 | } |
| 137 | |
| 138 | relocation_group_index_ = 0; |
| 139 | return true; |
| 140 | } |
| 141 | |
| 142 | decoder_t decoder_; |
| 143 | size_t relocation_count_; |
| 144 | size_t group_size_; |
| 145 | size_t group_flags_; |
| 146 | size_t group_r_offset_delta_; |
| 147 | size_t relocation_index_; |
| 148 | size_t relocation_group_index_; |
| 149 | rel_t reloc_; |
| 150 | }; |
| 151 | |
Dmitriy Ivanov | fa26eee | 2015-02-03 16:06:47 -0800 | [diff] [blame] | 152 | #endif // __LINKER_RELOC_ITERATORS_H |