15f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
25f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// found in the LICENSE file.
45f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
55f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "delta_encoder.h"
65f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
75f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include <vector>
85f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
95f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "debug.h"
105f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "elf_traits.h"
115f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
125f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)namespace relocation_packer {
135f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
145f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// Encode relative relocations with addends into a delta encoded (packed)
155f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// representation.  Represented as simple r_offset and r_addend delta pairs,
165f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// with an implicit neutral element at the start.
175f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void RelocationDeltaCodec::Encode(const std::vector<ELF::Rela>& relocations,
185f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                                  std::vector<ELF::Sxword>* packed) {
195f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // One relocation is sufficient for delta encoding.
205f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  if (relocations.size() < 1)
215f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    return;
225f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
235f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // Start with the element count, then append the delta pairs.
245f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  packed->push_back(relocations.size());
255f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
265f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  ELF::Addr offset = 0;
275f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  ELF::Sxword addend = 0;
285f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
295f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  for (size_t i = 0; i < relocations.size(); ++i) {
305f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    const ELF::Rela* relocation = &relocations[i];
315f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    CHECK(ELF_R_TYPE(relocation->r_info) == ELF::kRelativeRelocationCode);
325f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
335f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    packed->push_back(relocation->r_offset - offset);
345f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    offset = relocation->r_offset;
355f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    packed->push_back(relocation->r_addend - addend);
365f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    addend = relocation->r_addend;
375f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  }
385f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
395f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
405f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// Decode relative relocations with addends from a delta encoded (packed)
415f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// representation.
425f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void RelocationDeltaCodec::Decode(const std::vector<ELF::Sxword>& packed,
435f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                                  std::vector<ELF::Rela>* relocations) {
445f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // We need at least one packed pair after the packed pair count to be
455f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // able to unpack.
465f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  if (packed.size() < 3)
475f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    return;
485f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
495f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // Ensure that the packed data offers enough pairs.  There may be zero
505f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // padding on it that we ignore.
515f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  CHECK(static_cast<size_t>(packed[0]) <= (packed.size() - 1) >> 1);
525f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
535f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  ELF::Addr offset = 0;
545f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  ELF::Sxword addend = 0;
555f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
565f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // The first packed vector element is the pairs count.  Start uncondensing
575f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // pairs at the second, and finish at the end of the pairs data.
585f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  const size_t pairs_count = packed[0];
595f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  for (size_t i = 1; i < 1 + (pairs_count << 1); i += 2) {
605f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    offset += packed[i];
615f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    addend += packed[i + 1];
625f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
635f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    // Generate a relocation for this offset and addend pair.
645f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    ELF::Rela relocation;
655f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    relocation.r_offset = offset;
665f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    relocation.r_info = ELF_R_INFO(0, ELF::kRelativeRelocationCode);
675f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    relocation.r_addend = addend;
685f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    relocations->push_back(relocation);
695f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  }
705f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
715f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
725f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}  // namespace relocation_packer
73