+
+ // Select bits from A and B using bits in MASK. For each n in
+ // [0..31], the n-th bit in the result is chosen from the n-th bits
+ // of A and B. A zero selects A and a one selects B.
+ static inline uint64_t
+ bit_select64(uint64_t a, uint64_t b, uint64_t mask)
+ { return (a & ~mask) | (b & mask); }
+};
+
+// Track relocations while reading a section. This lets you ask for
+// the relocation at a certain offset, and see how relocs occur
+// between points of interest.
+
+template<int size, bool big_endian>
+class Track_relocs
+{
+ public:
+ Track_relocs()
+ : prelocs_(NULL), len_(0), pos_(0), reloc_size_(0)
+ { }
+
+ // Initialize the Track_relocs object. OBJECT is the object holding
+ // the reloc section, RELOC_SHNDX is the section index of the reloc
+ // section, and RELOC_TYPE is the type of the reloc section
+ // (elfcpp::SHT_REL or elfcpp::SHT_RELA). This returns false if
+ // something went wrong.
+ bool
+ initialize(Object* object, unsigned int reloc_shndx,
+ unsigned int reloc_type);
+
+ // Return the offset in the data section to which the next reloc
+ // applies. This returns -1 if there is no next reloc.
+ off_t
+ next_offset() const;
+
+ // Return the symbol index of the next reloc. This returns -1U if
+ // there is no next reloc.
+ unsigned int
+ next_symndx() const;
+
+ // Return the addend of the next reloc. This returns 0 if there is
+ // no next reloc.
+ uint64_t
+ next_addend() const;
+
+ // Advance to OFFSET within the data section, and return the number
+ // of relocs which would be skipped.
+ int
+ advance(off_t offset);
+
+ // Checkpoint the current position in the reloc section.
+ section_size_type
+ checkpoint() const
+ { return this->pos_; }
+
+ // Reset the position to CHECKPOINT.
+ void
+ reset(section_size_type checkpoint)
+ { this->pos_ = checkpoint; }
+
+ private:
+ // The contents of the input object's reloc section.
+ const unsigned char* prelocs_;
+ // The length of the reloc section.
+ section_size_type len_;
+ // Our current position in the reloc section.
+ section_size_type pos_;
+ // The size of the relocs in the section.
+ int reloc_size_;