Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1 | /* |
| 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 Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 17 | #include "elf_file.h" |
| 18 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 19 | #include "base/stringprintf.h" |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 20 | #include "base/unix_file/fd_file.h" |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 21 | #include "common_compiler_test.h" |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 22 | #include "elf_file.h" |
| 23 | #include "elf_file_impl.h" |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 24 | #include "elf_builder.h" |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 25 | #include "elf_writer_quick.h" |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 26 | #include "oat.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 27 | #include "utils.h" |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 28 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 29 | namespace art { |
| 30 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 31 | class ElfWriterTest : public CommonCompilerTest { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 32 | protected: |
| 33 | virtual void SetUp() { |
| 34 | ReserveImageSpace(); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 35 | CommonCompilerTest::SetUp(); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 36 | } |
| 37 | }; |
| 38 | |
Brian Carlstrom | 31050c6 | 2013-12-15 12:36:09 -0800 | [diff] [blame] | 39 | #define EXPECT_ELF_FILE_ADDRESS(ef, expected_value, symbol_name, build_map) \ |
| 40 | do { \ |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 41 | void* addr = reinterpret_cast<void*>(ef->FindSymbolAddress(SHT_DYNSYM, \ |
Brian Carlstrom | 31050c6 | 2013-12-15 12:36:09 -0800 | [diff] [blame] | 42 | 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 Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 52 | |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 53 | TEST_F(ElfWriterTest, dlsym) { |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 54 | std::string elf_location = GetCoreOatLocation(); |
Brian Carlstrom | 2afe494 | 2014-05-19 10:25:33 -0700 | [diff] [blame] | 55 | std::string elf_filename = GetSystemImageFilename(elf_location.c_str(), kRuntimeISA); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 56 | LOG(INFO) << "elf_filename=" << elf_filename; |
| 57 | |
| 58 | UnreserveImageSpace(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 59 | void* dl_oatdata = nullptr; |
| 60 | void* dl_oatexec = nullptr; |
| 61 | void* dl_oatlastword = nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 62 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 63 | std::unique_ptr<File> file(OS::OpenFileForReading(elf_filename.c_str())); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 64 | ASSERT_TRUE(file.get() != nullptr); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 65 | { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 66 | std::string error_msg; |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 67 | std::unique_ptr<ElfFile> ef(ElfFile::Open(file.get(), |
| 68 | false, |
| 69 | false, |
| 70 | /*low_4gb*/false, |
| 71 | &error_msg)); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 72 | CHECK(ef.get() != nullptr) << error_msg; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 73 | EXPECT_ELF_FILE_ADDRESS(ef, dl_oatdata, "oatdata", false); |
| 74 | EXPECT_ELF_FILE_ADDRESS(ef, dl_oatexec, "oatexec", false); |
| 75 | EXPECT_ELF_FILE_ADDRESS(ef, dl_oatlastword, "oatlastword", false); |
| 76 | } |
| 77 | { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 78 | std::string error_msg; |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 79 | std::unique_ptr<ElfFile> ef(ElfFile::Open(file.get(), |
| 80 | false, |
| 81 | false, |
| 82 | /*low_4gb*/false, |
| 83 | &error_msg)); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 84 | CHECK(ef.get() != nullptr) << error_msg; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 85 | EXPECT_ELF_FILE_ADDRESS(ef, dl_oatdata, "oatdata", true); |
| 86 | EXPECT_ELF_FILE_ADDRESS(ef, dl_oatexec, "oatexec", true); |
| 87 | EXPECT_ELF_FILE_ADDRESS(ef, dl_oatlastword, "oatlastword", true); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 88 | } |
| 89 | { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 90 | std::string error_msg; |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 91 | std::unique_ptr<ElfFile> ef(ElfFile::Open(file.get(), |
| 92 | false, |
| 93 | true, |
| 94 | /*low_4gb*/false, |
| 95 | &error_msg)); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 96 | CHECK(ef.get() != nullptr) << error_msg; |
Brian Carlstrom | 645e477 | 2016-10-14 01:04:26 -0700 | [diff] [blame] | 97 | CHECK(ef->Load(file.get(), false, /*low_4gb*/false, &error_msg)) << error_msg; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 98 | EXPECT_EQ(dl_oatdata, ef->FindDynamicSymbolAddress("oatdata")); |
| 99 | EXPECT_EQ(dl_oatexec, ef->FindDynamicSymbolAddress("oatexec")); |
| 100 | EXPECT_EQ(dl_oatlastword, ef->FindDynamicSymbolAddress("oatlastword")); |
| 101 | } |
| 102 | } |
| 103 | |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 104 | TEST_F(ElfWriterTest, EncodeDecodeOatPatches) { |
David Srbecky | f898087 | 2015-05-22 17:04:47 +0100 | [diff] [blame] | 105 | const std::vector<std::vector<uintptr_t>> test_data { |
| 106 | { 0, 4, 8, 15, 128, 200 }, |
| 107 | { 8, 8 + 127 }, |
| 108 | { 8, 8 + 128 }, |
| 109 | { }, |
| 110 | }; |
| 111 | for (const auto& patch_locations : test_data) { |
| 112 | constexpr int32_t delta = 0x11235813; |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 113 | |
David Srbecky | f898087 | 2015-05-22 17:04:47 +0100 | [diff] [blame] | 114 | // Encode patch locations. |
| 115 | std::vector<uint8_t> oat_patches; |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 116 | ElfBuilder<ElfTypes32>::EncodeOatPatches(ArrayRef<const uintptr_t>(patch_locations), |
| 117 | &oat_patches); |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 118 | |
David Srbecky | f898087 | 2015-05-22 17:04:47 +0100 | [diff] [blame] | 119 | // Create buffer to be patched. |
| 120 | std::vector<uint8_t> initial_data(256); |
| 121 | for (size_t i = 0; i < initial_data.size(); i++) { |
| 122 | initial_data[i] = i; |
| 123 | } |
| 124 | |
| 125 | // Patch manually. |
| 126 | std::vector<uint8_t> expected = initial_data; |
| 127 | for (uintptr_t location : patch_locations) { |
| 128 | typedef __attribute__((__aligned__(1))) uint32_t UnalignedAddress; |
| 129 | *reinterpret_cast<UnalignedAddress*>(expected.data() + location) += delta; |
| 130 | } |
| 131 | |
| 132 | // Decode and apply patch locations. |
| 133 | std::vector<uint8_t> actual = initial_data; |
| 134 | ElfFileImpl32::ApplyOatPatches( |
| 135 | oat_patches.data(), oat_patches.data() + oat_patches.size(), delta, |
| 136 | actual.data(), actual.data() + actual.size()); |
| 137 | |
| 138 | EXPECT_EQ(expected, actual); |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 139 | } |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 140 | } |
| 141 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 142 | } // namespace art |