blob: b413a9eb7b7a384abcd8b3c8da0558aa0c7fd52a [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"
David Srbecky6d8c8f02015-10-26 10:57:09 +000024#include "elf_builder.h"
David Srbecky2f6cdb02015-04-11 00:17:53 +010025#include "elf_writer_quick.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080026#include "oat.h"
Ian Rogerse63db272014-07-15 15:36:11 -070027#include "utils.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080028
Brian Carlstrom700c8d32012-11-05 10:42:02 -080029namespace art {
30
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080031class ElfWriterTest : public CommonCompilerTest {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080032 protected:
33 virtual void SetUp() {
34 ReserveImageSpace();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080035 CommonCompilerTest::SetUp();
Brian Carlstrom700c8d32012-11-05 10:42:02 -080036 }
37};
38
Brian Carlstrom31050c62013-12-15 12:36:09 -080039#define EXPECT_ELF_FILE_ADDRESS(ef, expected_value, symbol_name, build_map) \
40 do { \
Nicolas Geoffray50cfe742014-02-19 13:27:42 +000041 void* addr = reinterpret_cast<void*>(ef->FindSymbolAddress(SHT_DYNSYM, \
Brian Carlstrom31050c62013-12-15 12:36:09 -080042 symbol_name, \
43 build_map)); \
44 EXPECT_NE(nullptr, addr); \
45 EXPECT_LT(static_cast<uintptr_t>(ART_BASE_ADDRESS), reinterpret_cast<uintptr_t>(addr)); \
46 if (expected_value == nullptr) { \
47 expected_value = addr; \
48 } \
49 EXPECT_EQ(expected_value, addr); \
50 EXPECT_EQ(expected_value, ef->FindDynamicSymbolAddress(symbol_name)); \
51 } while (false)
Brian Carlstrom700c8d32012-11-05 10:42:02 -080052
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -070053TEST_F(ElfWriterTest, dlsym) {
Igor Murashkin37743352014-11-13 14:38:00 -080054 std::string elf_location = GetCoreOatLocation();
Brian Carlstrom2afe4942014-05-19 10:25:33 -070055 std::string elf_filename = GetSystemImageFilename(elf_location.c_str(), kRuntimeISA);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080056 LOG(INFO) << "elf_filename=" << elf_filename;
57
58 UnreserveImageSpace();
Mathieu Chartier2cebb242015-04-21 16:50:40 -070059 void* dl_oatdata = nullptr;
60 void* dl_oatexec = nullptr;
61 void* dl_oatlastword = nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080062
Ian Rogers700a4022014-05-19 16:49:03 -070063 std::unique_ptr<File> file(OS::OpenFileForReading(elf_filename.c_str()));
Mathieu Chartier2cebb242015-04-21 16:50:40 -070064 ASSERT_TRUE(file.get() != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080065 {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070066 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -070067 std::unique_ptr<ElfFile> ef(ElfFile::Open(file.get(), false, false, &error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -070068 CHECK(ef.get() != nullptr) << error_msg;
Brian Carlstrom265091e2013-01-30 14:08:26 -080069 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatdata, "oatdata", false);
70 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatexec, "oatexec", false);
71 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatlastword, "oatlastword", false);
72 }
73 {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070074 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -070075 std::unique_ptr<ElfFile> ef(ElfFile::Open(file.get(), false, false, &error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -070076 CHECK(ef.get() != nullptr) << error_msg;
Brian Carlstrom265091e2013-01-30 14:08:26 -080077 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatdata, "oatdata", true);
78 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatexec, "oatexec", true);
79 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatlastword, "oatlastword", true);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080080 }
81 {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070082 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -070083 std::unique_ptr<ElfFile> ef(ElfFile::Open(file.get(), false, true, &error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -070084 CHECK(ef.get() != nullptr) << error_msg;
85 CHECK(ef->Load(false, &error_msg)) << error_msg;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080086 EXPECT_EQ(dl_oatdata, ef->FindDynamicSymbolAddress("oatdata"));
87 EXPECT_EQ(dl_oatexec, ef->FindDynamicSymbolAddress("oatexec"));
88 EXPECT_EQ(dl_oatlastword, ef->FindDynamicSymbolAddress("oatlastword"));
89 }
90}
91
David Srbecky2f6cdb02015-04-11 00:17:53 +010092TEST_F(ElfWriterTest, EncodeDecodeOatPatches) {
David Srbeckyf8980872015-05-22 17:04:47 +010093 const std::vector<std::vector<uintptr_t>> test_data {
94 { 0, 4, 8, 15, 128, 200 },
95 { 8, 8 + 127 },
96 { 8, 8 + 128 },
97 { },
98 };
99 for (const auto& patch_locations : test_data) {
100 constexpr int32_t delta = 0x11235813;
David Srbecky2f6cdb02015-04-11 00:17:53 +0100101
David Srbeckyf8980872015-05-22 17:04:47 +0100102 // Encode patch locations.
103 std::vector<uint8_t> oat_patches;
David Srbecky6d8c8f02015-10-26 10:57:09 +0000104 ElfBuilder<ElfTypes32>::EncodeOatPatches(patch_locations, &oat_patches);
David Srbecky2f6cdb02015-04-11 00:17:53 +0100105
David Srbeckyf8980872015-05-22 17:04:47 +0100106 // Create buffer to be patched.
107 std::vector<uint8_t> initial_data(256);
108 for (size_t i = 0; i < initial_data.size(); i++) {
109 initial_data[i] = i;
110 }
111
112 // Patch manually.
113 std::vector<uint8_t> expected = initial_data;
114 for (uintptr_t location : patch_locations) {
115 typedef __attribute__((__aligned__(1))) uint32_t UnalignedAddress;
116 *reinterpret_cast<UnalignedAddress*>(expected.data() + location) += delta;
117 }
118
119 // Decode and apply patch locations.
120 std::vector<uint8_t> actual = initial_data;
121 ElfFileImpl32::ApplyOatPatches(
122 oat_patches.data(), oat_patches.data() + oat_patches.size(), delta,
123 actual.data(), actual.data() + actual.size());
124
125 EXPECT_EQ(expected, actual);
David Srbecky2f6cdb02015-04-11 00:17:53 +0100126 }
David Srbecky2f6cdb02015-04-11 00:17:53 +0100127}
128
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800129} // namespace art