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