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