blob: 3e5ad7b9f891c77989386f667f6e2d9e58addad8 [file] [log] [blame]
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Brian Carlstrom700c8d32012-11-05 10:42:02 -080017#include "elf_file.h"
18
Ian Rogerse63db272014-07-15 15:36:11 -070019#include "base/stringprintf.h"
Ian Rogersd4c4d952014-10-16 20:31:53 -070020#include "base/unix_file/fd_file.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080021#include "common_compiler_test.h"
David Srbecky2f6cdb02015-04-11 00:17:53 +010022#include "elf_file.h"
23#include "elf_file_impl.h"
24#include "elf_writer_quick.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080025#include "oat.h"
Ian Rogerse63db272014-07-15 15:36:11 -070026#include "utils.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080027
Brian Carlstrom700c8d32012-11-05 10:42:02 -080028namespace art {
29
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080030class ElfWriterTest : public CommonCompilerTest {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080031 protected:
32 virtual void SetUp() {
33 ReserveImageSpace();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080034 CommonCompilerTest::SetUp();
Brian Carlstrom700c8d32012-11-05 10:42:02 -080035 }
36};
37
Brian Carlstrom31050c62013-12-15 12:36:09 -080038#define EXPECT_ELF_FILE_ADDRESS(ef, expected_value, symbol_name, build_map) \
39 do { \
Nicolas Geoffray50cfe742014-02-19 13:27:42 +000040 void* addr = reinterpret_cast<void*>(ef->FindSymbolAddress(SHT_DYNSYM, \
Brian Carlstrom31050c62013-12-15 12:36:09 -080041 symbol_name, \
42 build_map)); \
43 EXPECT_NE(nullptr, addr); \
44 EXPECT_LT(static_cast<uintptr_t>(ART_BASE_ADDRESS), reinterpret_cast<uintptr_t>(addr)); \
45 if (expected_value == nullptr) { \
46 expected_value = addr; \
47 } \
48 EXPECT_EQ(expected_value, addr); \
49 EXPECT_EQ(expected_value, ef->FindDynamicSymbolAddress(symbol_name)); \
50 } while (false)
Brian Carlstrom700c8d32012-11-05 10:42:02 -080051
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -070052TEST_F(ElfWriterTest, dlsym) {
Igor Murashkin37743352014-11-13 14:38:00 -080053 std::string elf_location = GetCoreOatLocation();
Brian Carlstrom2afe4942014-05-19 10:25:33 -070054 std::string elf_filename = GetSystemImageFilename(elf_location.c_str(), kRuntimeISA);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080055 LOG(INFO) << "elf_filename=" << elf_filename;
56
57 UnreserveImageSpace();
Brian Carlstrom31050c62013-12-15 12:36:09 -080058 void* dl_oatdata = NULL;
59 void* dl_oatexec = NULL;
60 void* dl_oatlastword = NULL;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080061
Ian Rogers700a4022014-05-19 16:49:03 -070062 std::unique_ptr<File> file(OS::OpenFileForReading(elf_filename.c_str()));
Brian Carlstrom700c8d32012-11-05 10:42:02 -080063 ASSERT_TRUE(file.get() != NULL);
64 {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070065 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -070066 std::unique_ptr<ElfFile> ef(ElfFile::Open(file.get(), false, false, &error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -070067 CHECK(ef.get() != nullptr) << error_msg;
Brian Carlstrom265091e2013-01-30 14:08:26 -080068 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatdata, "oatdata", false);
69 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatexec, "oatexec", false);
70 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatlastword, "oatlastword", false);
71 }
72 {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070073 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -070074 std::unique_ptr<ElfFile> ef(ElfFile::Open(file.get(), false, false, &error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -070075 CHECK(ef.get() != nullptr) << error_msg;
Brian Carlstrom265091e2013-01-30 14:08:26 -080076 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatdata, "oatdata", true);
77 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatexec, "oatexec", true);
78 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatlastword, "oatlastword", true);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080079 }
80 {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070081 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -070082 std::unique_ptr<ElfFile> ef(ElfFile::Open(file.get(), false, true, &error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -070083 CHECK(ef.get() != nullptr) << error_msg;
84 CHECK(ef->Load(false, &error_msg)) << error_msg;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080085 EXPECT_EQ(dl_oatdata, ef->FindDynamicSymbolAddress("oatdata"));
86 EXPECT_EQ(dl_oatexec, ef->FindDynamicSymbolAddress("oatexec"));
87 EXPECT_EQ(dl_oatlastword, ef->FindDynamicSymbolAddress("oatlastword"));
88 }
89}
90
David Srbecky2f6cdb02015-04-11 00:17:53 +010091// Run only on host since we do unaligned memory accesses.
92#ifndef HAVE_ANDROID_OS
93
94static void PatchSection(const std::vector<uintptr_t>& patch_locations,
95 std::vector<uint8_t>* section, int32_t delta) {
96 for (uintptr_t location : patch_locations) {
97 *reinterpret_cast<int32_t*>(section->data() + location) += delta;
98 }
99}
100
101TEST_F(ElfWriterTest, EncodeDecodeOatPatches) {
102 std::vector<uint8_t> oat_patches; // Encoded patches.
103
104 // Encode patch locations for a few sections.
105 OatWriter::PatchLocationsMap sections;
106 std::vector<uintptr_t> patches0 { 0, 4, 8, 15, 128, 200 }; // NOLINT
107 sections.emplace(".section0", std::unique_ptr<std::vector<uintptr_t>>(
108 new std::vector<uintptr_t> { patches0 }));
109 std::vector<uintptr_t> patches1 { 8, 127 }; // NOLINT
110 sections.emplace(".section1", std::unique_ptr<std::vector<uintptr_t>>(
111 new std::vector<uintptr_t> { patches1 }));
112 std::vector<uintptr_t> patches2 { }; // NOLINT
113 sections.emplace(".section2", std::unique_ptr<std::vector<uintptr_t>>(
114 new std::vector<uintptr_t> { patches2 }));
115 ElfWriterQuick32::EncodeOatPatches(sections, &oat_patches);
116
117 // Create buffers to be patched.
118 std::vector<uint8_t> initial_data(256);
119 for (size_t i = 0; i < initial_data.size(); i++) {
120 initial_data[i] = i;
121 }
122 std::vector<uint8_t> section0_expected = initial_data;
123 std::vector<uint8_t> section1_expected = initial_data;
124 std::vector<uint8_t> section2_expected = initial_data;
125 std::vector<uint8_t> section0_actual = initial_data;
126 std::vector<uint8_t> section1_actual = initial_data;
127 std::vector<uint8_t> section2_actual = initial_data;
128
129 // Patch manually.
130 constexpr int32_t delta = 0x11235813;
131 PatchSection(patches0, &section0_expected, delta);
132 PatchSection(patches1, &section1_expected, delta);
133 PatchSection(patches2, &section2_expected, delta);
134
135 // Decode and apply patch locations.
136 bool section0_successful = ElfFileImpl32::ApplyOatPatches(
137 oat_patches.data(), oat_patches.data() + oat_patches.size(),
138 ".section0", delta,
139 section0_actual.data(), section0_actual.data() + section0_actual.size());
140 EXPECT_TRUE(section0_successful);
141 EXPECT_EQ(section0_expected, section0_actual);
142
143 bool section1_successful = ElfFileImpl32::ApplyOatPatches(
144 oat_patches.data(), oat_patches.data() + oat_patches.size(),
145 ".section1", delta,
146 section1_actual.data(), section1_actual.data() + section1_actual.size());
147 EXPECT_TRUE(section1_successful);
148 EXPECT_EQ(section1_expected, section1_actual);
149
150 bool section2_successful = ElfFileImpl32::ApplyOatPatches(
151 oat_patches.data(), oat_patches.data() + oat_patches.size(),
152 ".section2", delta,
153 section2_actual.data(), section2_actual.data() + section2_actual.size());
154 EXPECT_TRUE(section2_successful);
155 EXPECT_EQ(section2_expected, section2_actual);
156}
157
158#endif
159
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800160} // namespace art