diff options
author | Etienne Pierre-Doray <etiennep@chromium.org> | 2018-07-25 20:16:02 +0000 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2021-07-25 20:33:17 -0700 |
commit | a88cad0485f1c73d63ba0a1bcfccc8a68bd300c6 (patch) | |
tree | a1e2e0302064526dd50280e84651979b656f0ad2 /reloc_elf.h | |
parent | ae27d8ed0116c53fb530b141e96df91e90c3671c (diff) | |
download | zucchini-a88cad0485f1c73d63ba0a1bcfccc8a68bd300c6.tar.gz |
[Zucchini] Create elf types and utils.
Creates types and utility class to manipulate ELF header format and relocation
references.
BufferView: :modify() was also added.
Change-Id: Iacec212a2fb2f8a6c85d551eed4b8e0a84926d89
Reviewed-on: https://chromium-review.googlesource.com/1136846
Commit-Queue: Etienne Pierre-Doray <etiennep@chromium.org>
Reviewed-by: Greg Thompson <grt@chromium.org>
Reviewed-by: Samuel Huang <huangs@chromium.org>
Cr-Commit-Position: refs/heads/master@{#578034}
NOKEYCHECK=True
GitOrigin-RevId: 320f7d9d2cff9702e632af296867bf6ce7b14f8b
Diffstat (limited to 'reloc_elf.h')
-rw-r--r-- | reloc_elf.h | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/reloc_elf.h b/reloc_elf.h new file mode 100644 index 0000000..1b5ba11 --- /dev/null +++ b/reloc_elf.h @@ -0,0 +1,102 @@ +// Copyright 2018 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef COMPONENTS_ZUCCHINI_RELOC_ELF_H_ +#define COMPONENTS_ZUCCHINI_RELOC_ELF_H_ + +#include <stddef.h> +#include <stdint.h> + +#include <vector> + +#include "base/optional.h" +#include "components/zucchini/address_translator.h" +#include "components/zucchini/buffer_source.h" +#include "components/zucchini/buffer_view.h" +#include "components/zucchini/image_utils.h" +#include "components/zucchini/type_elf.h" + +namespace zucchini { + +// Section dimensions for ELF files, to store relevant dimensions data from +// Elf32_Shdr and Elf64_Shdr, while reducing code duplication from templates. +struct SectionDimensionsElf { + SectionDimensionsElf() = default; + + template <class Elf_Shdr> + explicit SectionDimensionsElf(const Elf_Shdr& section) + : region(BufferRegion{base::checked_cast<size_t>(section.sh_offset), + base::checked_cast<size_t>(section.sh_size)}), + entry_size(base::checked_cast<offset_t>(section.sh_entsize)) {} + + friend bool operator<(const SectionDimensionsElf& a, + const SectionDimensionsElf& b) { + return a.region.offset < b.region.offset; + } + + friend bool operator<(offset_t offset, const SectionDimensionsElf& section) { + return offset < section.region.offset; + } + + BufferRegion region; + offset_t entry_size; // Varies across REL / RELA sections. +}; + +// A Generator to visit all reloc structs located in [|lo|, |hi|) (excluding +// truncated strct at |lo| but inlcuding truncated struct at |hi|), and emit +// valid References with |rel_type|. This implements a nested loop unrolled into +// a generator: the outer loop has |cur_section_dimensions_| visiting +// |reloc_section_dims| (sorted by |region.offset|), and the inner loop has +// |cursor_| visiting successive reloc structs within |cur_section_dimensions_|. +class RelocReaderElf : public ReferenceReader { + public: + RelocReaderElf( + ConstBufferView image, + Bitness bitness, + const std::vector<SectionDimensionsElf>& reloc_section_dimensions, + uint32_t rel_type, + offset_t lo, + offset_t hi, + const AddressTranslator& translator); + ~RelocReaderElf() override; + + // If |rel| contains |r_offset| for |rel_type_|, return the RVA. Otherwise + // return |kInvalidRva|. These also handle Elf*_Rela, by using the fact that + // Elf*_Rel is a prefix of Elf*_Rela. + rva_t GetRelocationTarget(elf::Elf32_Rel rel) const; + rva_t GetRelocationTarget(elf::Elf64_Rel rel) const; + + // ReferenceReader: + base::Optional<Reference> GetNext() override; + + private: + const ConstBufferView image_; + const Bitness bitness_; + const uint32_t rel_type_; + const std::vector<SectionDimensionsElf>& reloc_section_dimensions_; + std::vector<SectionDimensionsElf>::const_iterator cur_section_dimensions_; + offset_t hi_; + offset_t cursor_; + AddressTranslator::RvaToOffsetCache target_rva_to_offset_; +}; + +class RelocWriterElf : public ReferenceWriter { + public: + RelocWriterElf(MutableBufferView image, + Bitness bitness, + const AddressTranslator& translator); + ~RelocWriterElf() override; + + // ReferenceWriter: + void PutNext(Reference ref) override; + + private: + MutableBufferView image_; + const Bitness bitness_; + AddressTranslator::OffsetToRvaCache target_offset_to_rva_; +}; + +} // namespace zucchini + +#endif // COMPONENTS_ZUCCHINI_RELOC_ELF_H_ |