1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/zucchini/reloc_elf.h"
#include <stdint.h>
#include <algorithm>
#include <memory>
#include <utility>
#include <vector>
#include "base/numerics/safe_conversions.h"
#include "components/zucchini/address_translator.h"
#include "components/zucchini/algorithm.h"
#include "components/zucchini/disassembler_elf.h"
#include "components/zucchini/image_utils.h"
#include "components/zucchini/test_utils.h"
#include "components/zucchini/type_elf.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace zucchini {
namespace {
template <class Elf_Shdr>
SectionDimensionsElf MakeSectionDimensions(const BufferRegion& region,
offset_t entry_size) {
using sh_offset_t = decltype(Elf_Shdr::sh_offset);
using sh_size_t = decltype(Elf_Shdr::sh_size);
using sh_entsize_t = decltype(Elf_Shdr::sh_entsize);
return SectionDimensionsElf{Elf_Shdr{
0, // sh_name
0, // sh_type
0, // sh_flags
0, // sh_addr
// sh_offset
base::checked_cast<sh_offset_t>(region.offset),
// sh_size
base::checked_cast<sh_size_t>(region.size),
0, // sh_link
0, // sh_info
0, // sh_addralign
// sh_entsize
base::checked_cast<sh_entsize_t>(entry_size),
}};
}
// Helper to manipulate an image with one or more relocation tables.
template <class ELF_INTEL_TRAITS>
class FakeImageWithReloc {
public:
using ElfIntelTraits = ELF_INTEL_TRAITS;
struct RelocSpec {
offset_t start;
std::vector<uint8_t> data;
};
FakeImageWithReloc(size_t image_size,
rva_t base_rva,
const std::vector<RelocSpec>& reloc_specs)
: image_data_(image_size, 0xFF),
mutable_image_(&image_data_[0], image_data_.size()) {
translator_.Initialize({{0, static_cast<offset_t>(image_size), base_rva,
static_cast<rva_t>(image_size)}});
// Set up test image with reloc sections.
for (const RelocSpec& reloc_spec : reloc_specs) {
BufferRegion reloc_region = {reloc_spec.start, reloc_spec.data.size()};
std::ranges::copy(reloc_spec.data,
image_data_.begin() + reloc_region.lo());
section_dimensions_.emplace_back(
MakeSectionDimensions<typename ElfIntelTraits::Elf_Shdr>(
reloc_region, ElfIntelTraits::kVAWidth));
reloc_regions_.push_back(reloc_region);
}
}
std::vector<Reference> ExtractRelocReferences() {
const size_t image_size = image_data_.size();
ConstBufferView image = {image_data_.data(), image_size};
// Make RelocReaderElf.
auto reader = std::make_unique<RelocReaderElf>(
image, ElfIntelTraits::kBitness, section_dimensions_,
ElfIntelTraits::kRelType, 0, image_size, translator_);
// Read all references and check.
std::vector<Reference> refs;
for (std::optional<Reference> ref = reader->GetNext(); ref.has_value();
ref = reader->GetNext()) {
refs.push_back(ref.value());
}
return refs;
}
std::unique_ptr<RelocWriterElf> MakeRelocWriter() {
return std::move(std::make_unique<RelocWriterElf>(
mutable_image_, ElfIntelTraits::kBitness, translator_));
}
std::vector<uint8_t> GetRawRelocData(int reloc_index) {
BufferRegion reloc_region = reloc_regions_[reloc_index];
return Sub(image_data_, reloc_region.lo(), reloc_region.hi());
}
private:
std::vector<uint8_t> image_data_;
MutableBufferView mutable_image_;
std::vector<BufferRegion> reloc_regions_;
std::vector<SectionDimensionsElf> section_dimensions_;
AddressTranslator translator_;
};
} // namespace
TEST(RelocElfTest, ReadWrite32) {
// Set up mock image: Size = 0x3000, .reloc at 0x600. RVA is 0x40000 + offset.
constexpr size_t kImageSize = 0x3000;
constexpr rva_t kBaseRva = 0x40000;
constexpr offset_t kRelocStart0 = 0x600;
// "C0 10 04 00 08 00 00 00" represents
// (r_sym, r_type, r_offset) = (0x000000, 0x08, 0x000410C0).
// r_type = 0x08 = R_386_RELATIVE, and so |r_offset| is an RVA 0x000410C0.
// Zucchini does not care about |r_sym|.
std::vector<uint8_t> reloc_data0 = ParseHexString(
"C0 10 04 00 08 00 00 00 " // R_386_RELATIVE.
"F8 10 04 00 08 AB CD EF " // R_386_RELATIVE.
"00 10 04 00 00 AB CD EF " // R_386_NONE.
"00 10 04 00 07 AB CD EF"); // R_386_JMP_SLOT.
constexpr offset_t kRelocStart1 = 0x620;
std::vector<uint8_t> reloc_data1 = ParseHexString(
"BC 20 04 00 08 00 00 00 " // R_386_RELATIVE.
"A0 20 04 00 08 AB CD EF"); // R_386_RELATIVE.
FakeImageWithReloc<Elf32IntelTraits> fake_image(
kImageSize, kBaseRva,
{{kRelocStart0, reloc_data0}, {kRelocStart1, reloc_data1}});
// Only R_386_RELATIVE references are extracted. Targets are translated from
// address (e.g., 0x000420BC) to offset (e.g., 0x20BC).
std::vector<Reference> exp_refs{
{0x600, 0x10C0}, {0x608, 0x10F8}, {0x620, 0x20BC}, {0x628, 0x20A0}};
EXPECT_EQ(exp_refs, fake_image.ExtractRelocReferences());
// Write reference, extract bytes and check.
std::unique_ptr<RelocWriterElf> writer = fake_image.MakeRelocWriter();
writer->PutNext({0x608, 0x1F83});
std::vector<uint8_t> exp_reloc_data0 = ParseHexString(
"C0 10 04 00 08 00 00 00 " // R_386_RELATIVE.
"83 1F 04 00 08 AB CD EF " // R_386_RELATIVE (address modified).
"00 10 04 00 00 AB CD EF " // R_386_NONE.
"00 10 04 00 07 AB CD EF"); // R_386_JMP_SLOT.
EXPECT_EQ(exp_reloc_data0, fake_image.GetRawRelocData(0));
writer->PutNext({0x628, 0x2950});
std::vector<uint8_t> exp_reloc_data1 = ParseHexString(
"BC 20 04 00 08 00 00 00 " // R_386_RELATIVE.
"50 29 04 00 08 AB CD EF"); // R_386_RELATIVE (address modified).
EXPECT_EQ(exp_reloc_data1, fake_image.GetRawRelocData(1));
}
TEST(RelocElfTest, Limit32) {
constexpr size_t kImageSize = 0x3000;
constexpr offset_t kBaseRva = 0x40000;
constexpr offset_t kRelocStart = 0x600;
// All R_386_RELATIVE.
std::vector<uint8_t> reloc_data = ParseHexString(
// Strictly within file.
"00 00 04 00 08 00 00 00 "
"00 10 04 00 08 00 00 00 "
"F0 2F 04 00 08 00 00 00 "
"F8 2F 04 00 08 00 00 00 "
"FC 2F 04 00 08 00 00 00 "
// Straddles end of file.
"FD 2F 04 00 08 00 00 00 "
"FE 2F 04 00 08 00 00 00 "
"FF 2F 04 00 08 00 00 00 "
// Beyond end of file.
"00 30 04 00 08 00 00 00 "
"01 30 04 00 08 00 00 00 "
"FC FF FF 7F 08 00 00 00 "
"FE FF FF 7F 08 00 00 00 "
"00 00 00 80 08 00 00 00 "
"FC FF FF FF 08 00 00 00 "
"FF FF FF FF 08 00 00 00 "
// Another good reference.
"34 12 04 00 08 00 00 00");
FakeImageWithReloc<Elf32IntelTraits> fake_image(kImageSize, kBaseRva,
{{kRelocStart, reloc_data}});
std::vector<Reference> exp_refs{{0x600, 0x0000}, {0x608, 0x1000},
{0x610, 0x2FF0}, {0x618, 0x2FF8},
{0x620, 0x2FFC}, {0x678, 0x1234}};
EXPECT_EQ(exp_refs, fake_image.ExtractRelocReferences());
}
TEST(RelocElfTest, Limit64) {
constexpr size_t kImageSize = 0x3000;
constexpr offset_t kBaseRva = 0x40000;
constexpr offset_t kRelocStart = 0x600;
// All R_X86_64_RELATIVE.
std::vector<uint8_t> reloc_data = ParseHexString(
// Strictly within file.
"00 00 04 00 00 00 00 00 08 00 00 00 00 00 00 00 "
"00 10 04 00 00 00 00 00 08 00 00 00 00 00 00 00 "
"F0 2F 04 00 00 00 00 00 08 00 00 00 00 00 00 00 "
"F4 2F 04 00 00 00 00 00 08 00 00 00 00 00 00 00 "
"F8 2F 04 00 00 00 00 00 08 00 00 00 00 00 00 00 "
// Straddles end of file.
"F9 2F 04 00 00 00 00 00 08 00 00 00 00 00 00 00 "
"FC 2F 04 00 00 00 00 00 08 00 00 00 00 00 00 00 "
"FF 2F 04 00 00 00 00 00 08 00 00 00 00 00 00 00 "
// Beyond end of file.
"00 30 04 00 00 00 00 00 08 00 00 00 00 00 00 00 "
"01 30 04 00 00 00 00 00 08 00 00 00 00 00 00 00 "
"FC FF FF 7F 00 00 00 00 08 00 00 00 00 00 00 00 "
"FE FF FF 7F 00 00 00 00 08 00 00 00 00 00 00 00 "
"00 00 00 80 00 00 00 00 08 00 00 00 00 00 00 00 "
"FC FF FF FF 00 00 00 00 08 00 00 00 00 00 00 00 "
"FF FF FF FF 00 00 00 00 08 00 00 00 00 00 00 00 "
"00 00 04 00 01 00 00 00 08 00 00 00 00 00 00 00 "
"FF FF FF FF FF FF FF FF 08 00 00 00 00 00 00 00 "
"F8 FF FF FF FF FF FF FF 08 00 00 00 00 00 00 00 "
// Another good reference.
"34 12 04 00 00 00 00 00 08 00 00 00 00 00 00 00");
FakeImageWithReloc<Elf64IntelTraits> fake_image(kImageSize, kBaseRva,
{{kRelocStart, reloc_data}});
std::vector<Reference> exp_refs{{0x600, 0x0000}, {0x610, 0x1000},
{0x620, 0x2FF0}, {0x630, 0x2FF4},
{0x640, 0x2FF8}, {0x720, 0x1234}};
EXPECT_EQ(exp_refs, fake_image.ExtractRelocReferences());
}
} // namespace zucchini
|