Refactoring: introduce reloc_iterators

 Replace rel/rela array with reloc_iterators.

Change-Id: I6165d062e0390b6bc60da2e8279aabbedf828ec9
diff --git a/linker/linker.cpp b/linker/linker.cpp
index 5d2425f..ae9df52 100644
--- a/linker/linker.cpp
+++ b/linker/linker.cpp
@@ -50,11 +50,12 @@
 #include "private/UniquePtr.h"
 
 #include "linker.h"
+#include "linker_allocator.h"
 #include "linker_debug.h"
 #include "linker_environ.h"
 #include "linker_phdr.h"
 #include "linker_relocs.h"
-#include "linker_allocator.h"
+#include "linker_reloc_iterators.h"
 
 /* >>> IMPORTANT NOTE - READ ME BEFORE MODIFYING <<<
  *
@@ -1297,9 +1298,10 @@
 }
 #endif
 
-template<typename ElfRelT>
-bool soinfo::relocate(ElfRelT* rel, unsigned count, const soinfo_list_t& global_group, const soinfo_list_t& local_group) {
-  for (size_t idx = 0; idx < count; ++idx, ++rel) {
+template<typename ElfRelIteratorT>
+bool soinfo::relocate(ElfRelIteratorT&& rel_iterator, const soinfo_list_t& global_group, const soinfo_list_t& local_group) {
+  for (size_t idx = 0; rel_iterator.has_next(); ++idx) {
+    const auto rel = rel_iterator.next();
     ElfW(Word) type = ELFW(R_TYPE)(rel->r_info);
     ElfW(Word) sym = ELFW(R_SYM)(rel->r_info);
 
@@ -2273,26 +2275,26 @@
 #if defined(USE_RELA)
   if (rela_ != nullptr) {
     DEBUG("[ relocating %s ]", name);
-    if (!relocate(rela_, rela_count_, global_group, local_group)) {
+    if (!relocate(plain_reloc_iterator(rela_, rela_count_), global_group, local_group)) {
       return false;
     }
   }
   if (plt_rela_ != nullptr) {
     DEBUG("[ relocating %s plt ]", name);
-    if (!relocate(plt_rela_, plt_rela_count_, global_group, local_group)) {
+    if (!relocate(plain_reloc_iterator(plt_rela_, plt_rela_count_), global_group, local_group)) {
       return false;
     }
   }
 #else
   if (rel_ != nullptr) {
     DEBUG("[ relocating %s ]", name);
-    if (!relocate(rel_, rel_count_, global_group, local_group)) {
+    if (!relocate(plain_reloc_iterator(rel_, rel_count_), global_group, local_group)) {
       return false;
     }
   }
   if (plt_rel_ != nullptr) {
     DEBUG("[ relocating %s plt ]", name);
-    if (!relocate(plt_rel_, plt_rel_count_, global_group, local_group)) {
+    if (!relocate(plain_reloc_iterator(plt_rel_, plt_rel_count_), global_group, local_group)) {
       return false;
     }
   }
diff --git a/linker/linker.h b/linker/linker.h
index 2afbaf6..222ddbf 100644
--- a/linker/linker.h
+++ b/linker/linker.h
@@ -286,8 +286,8 @@
 
   void call_array(const char* array_name, linker_function_t* functions, size_t count, bool reverse);
   void call_function(const char* function_name, linker_function_t function);
-  template<typename ElfRelT>
-  bool relocate(ElfRelT* rel, unsigned count, const soinfo_list_t& global_group, const soinfo_list_t& local_group);
+  template<typename ElfRelIteratorT>
+  bool relocate(ElfRelIteratorT&& rel_iterator, const soinfo_list_t& global_group, const soinfo_list_t& local_group);
 
  private:
   // This part of the structure is only available
diff --git a/linker/linker_mips.cpp b/linker/linker_mips.cpp
index 7fbde3d..d71659e 100644
--- a/linker/linker_mips.cpp
+++ b/linker/linker_mips.cpp
@@ -29,10 +29,15 @@
 #include "linker.h"
 #include "linker_debug.h"
 #include "linker_relocs.h"
+#include "linker_reloc_iterators.h"
 
-template<>
-bool soinfo::relocate(ElfW(Rel)* rel, unsigned count, const soinfo_list_t& global_group, const soinfo_list_t& local_group) {
-  for (size_t idx = 0; idx < count; ++idx, ++rel) {
+template bool soinfo::relocate<plain_reloc_iterator>(plain_reloc_iterator&& rel_iterator, const soinfo_list_t& global_group, const soinfo_list_t& local_group);
+
+template <typename ElfRelIteratorT>
+bool soinfo::relocate(ElfRelIteratorT&& rel_iterator, const soinfo_list_t& global_group, const soinfo_list_t& local_group) {
+  for (size_t idx = 0; rel_iterator.has_next(); ++idx) {
+    const auto rel = rel_iterator.next();
+
     ElfW(Word) type = ELFW(R_TYPE)(rel->r_info);
     ElfW(Word) sym = ELFW(R_SYM)(rel->r_info);
 
diff --git a/linker/linker_reloc_iterators.h b/linker/linker_reloc_iterators.h
new file mode 100644
index 0000000..6388bb0
--- /dev/null
+++ b/linker/linker_reloc_iterators.h
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __LINKER_RELOC_ITERATORS_H
+#define __LINKER_RELOC_ITERATORS_H
+
+#include "linker.h"
+
+class plain_reloc_iterator {
+#if defined(USE_RELA)
+  typedef ElfW(Rela) rel_t;
+#else
+  typedef ElfW(Rel) rel_t;
+#endif
+ public:
+  plain_reloc_iterator(rel_t* rel_array, size_t count)
+      : begin_(rel_array), end_(begin_ + count), current_(begin_) {}
+
+  bool has_next() {
+    return current_ < end_;
+  }
+
+  rel_t* next() {
+    return current_++;
+  }
+ private:
+  rel_t* const begin_;
+  rel_t* const end_;
+  rel_t* current_;
+
+  DISALLOW_COPY_AND_ASSIGN(plain_reloc_iterator);
+};
+
+#endif  // __LINKER_RELOC_ITERATORS_H