typedrva_unittest.cc revision 558790d6acca3451cf3a6b497803a5f07d0bec58
1// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "courgette/base_test_unittest.h"
6#include "courgette/disassembler_elf_32_arm.h"
7#include "courgette/disassembler_elf_32_x86.h"
8
9class TypedRVATest : public BaseTest {
10 public:
11  void TestRelativeTargetX86(courgette::RVA word, courgette::RVA expected)
12    const;
13
14  void TestRelativeTargetARM(courgette::ARM_RVA arm_rva,
15                             courgette::RVA rva,
16                             uint32 op,
17                             courgette::RVA expected) const;
18};
19
20void TypedRVATest::TestRelativeTargetX86(courgette::RVA word,
21                                         courgette::RVA expected) const {
22  courgette::DisassemblerElf32X86::TypedRVAX86* typed_rva
23    = new courgette::DisassemblerElf32X86::TypedRVAX86(0);
24  const uint8* op_pointer = reinterpret_cast<const uint8*>(&word);
25
26  EXPECT_TRUE(typed_rva->ComputeRelativeTarget(op_pointer));
27  EXPECT_EQ(typed_rva->relative_target(), expected);
28
29  delete typed_rva;
30}
31
32uint32 Read32LittleEndian(const void* address) {
33  return *reinterpret_cast<const uint32*>(address);
34}
35
36void TypedRVATest::TestRelativeTargetARM(courgette::ARM_RVA arm_rva,
37                                         courgette::RVA rva,
38                                         uint32 op,
39                                         courgette::RVA expected) const {
40  courgette::DisassemblerElf32ARM::TypedRVAARM* typed_rva
41    = new courgette::DisassemblerElf32ARM::TypedRVAARM(arm_rva, 0);
42  uint8* op_pointer = reinterpret_cast<uint8*>(&op);
43
44  EXPECT_TRUE(typed_rva->ComputeRelativeTarget(op_pointer));
45  EXPECT_EQ(rva + typed_rva->relative_target(), expected);
46
47  delete typed_rva;
48}
49
50TEST_F(TypedRVATest, TestX86) {
51  TestRelativeTargetX86(0x0, 0x4);
52}
53
54// ARM opcodes taken from and tested against the output of
55// "arm-linux-gnueabi-objdump -d daisy_3701.98.0/bin/ls"
56
57TEST_F(TypedRVATest, TestARM_OFF8_PREFETCH) {
58  TestRelativeTargetARM(courgette::ARM_OFF8, 0x0, 0x0, 0x4);
59}
60
61TEST_F(TypedRVATest, TestARM_OFF8_FORWARDS) {
62  TestRelativeTargetARM(courgette::ARM_OFF8, 0x2bcc, 0xd00e, 0x2bec);
63  TestRelativeTargetARM(courgette::ARM_OFF8, 0x3752, 0xd910, 0x3776);
64}
65
66TEST_F(TypedRVATest, TestARM_OFF8_BACKWARDS) {
67  TestRelativeTargetARM(courgette::ARM_OFF8, 0x3774, 0xd1f6, 0x3764);
68}
69
70TEST_F(TypedRVATest, TestARM_OFF11_PREFETCH) {
71  TestRelativeTargetARM(courgette::ARM_OFF11, 0x0, 0x0, 0x4);
72}
73
74TEST_F(TypedRVATest, TestARM_OFF11_FORWARDS) {
75  TestRelativeTargetARM(courgette::ARM_OFF11, 0x2bea, 0xe005, 0x2bf8);
76}
77
78TEST_F(TypedRVATest, TestARM_OFF11_BACKWARDS) {
79  TestRelativeTargetARM(courgette::ARM_OFF11, 0x2f80, 0xe6cd, 0x2d1e);
80  TestRelativeTargetARM(courgette::ARM_OFF11, 0x3610, 0xe56a, 0x30e8);
81}
82
83TEST_F(TypedRVATest, TestARM_OFF24_PREFETCH) {
84  TestRelativeTargetARM(courgette::ARM_OFF24, 0x0, 0x0, 0x8);
85}
86
87TEST_F(TypedRVATest, TestARM_OFF24_FORWARDS) {
88  TestRelativeTargetARM(courgette::ARM_OFF24, 0x2384, 0x4af3613a, 0xffcda874);
89  TestRelativeTargetARM(courgette::ARM_OFF24, 0x23bc, 0x6af961b9, 0xffe5aaa8);
90  TestRelativeTargetARM(courgette::ARM_OFF24, 0x23d4, 0x2b006823, 0x1c468);
91}
92
93TEST_F(TypedRVATest, TestARM_OFF24_BACKWARDS) {
94  // TODO(paulgazz): find a real-world example of an ARM branch op
95  // that jumps backwards.
96}
97