blob: ef85fd16fff30fee6dfae069cf71fa001e675c48 [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
Vladimir Markoc09cd052018-08-23 16:36:36 +010017#include <sys/mman.h> // For the PROT_NONE constant.
18
Brian Carlstrom700c8d32012-11-05 10:42:02 -080019#include "elf_file.h"
20
David Sehr891a50e2017-10-27 17:01:07 -070021#include "base/file_utils.h"
Vladimir Markoc09cd052018-08-23 16:36:36 +010022#include "base/mem_map.h"
Ian Rogersd4c4d952014-10-16 20:31:53 -070023#include "base/unix_file/fd_file.h"
David Sehrc431b9d2018-03-02 12:01:51 -080024#include "base/utils.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080025#include "common_compiler_test.h"
David Srbecky2f6cdb02015-04-11 00:17:53 +010026#include "elf_file.h"
27#include "elf_file_impl.h"
28#include "elf_writer_quick.h"
Vladimir Marko74527972016-11-29 15:57:32 +000029#include "linker/elf_builder.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080030#include "oat.h"
31
Brian Carlstrom700c8d32012-11-05 10:42:02 -080032namespace art {
Vladimir Marko74527972016-11-29 15:57:32 +000033namespace linker {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080034
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080035class ElfWriterTest : public CommonCompilerTest {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080036 protected:
Andreas Gampefa6a1b02018-09-07 08:11:55 -070037 void SetUp() override {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080038 ReserveImageSpace();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080039 CommonCompilerTest::SetUp();
Brian Carlstrom700c8d32012-11-05 10:42:02 -080040 }
41};
42
Brian Carlstrom31050c62013-12-15 12:36:09 -080043#define EXPECT_ELF_FILE_ADDRESS(ef, expected_value, symbol_name, build_map) \
44 do { \
Chih-Hung Hsieh1a0de6a2016-08-26 15:06:11 -070045 void* addr = reinterpret_cast<void*>((ef)->FindSymbolAddress(SHT_DYNSYM, \
46 symbol_name, \
47 build_map)); \
Brian Carlstrom31050c62013-12-15 12:36:09 -080048 EXPECT_NE(nullptr, addr); \
Chih-Hung Hsieh1a0de6a2016-08-26 15:06:11 -070049 if ((expected_value) == nullptr) { \
50 (expected_value) = addr; \
Brian Carlstrom31050c62013-12-15 12:36:09 -080051 } \
52 EXPECT_EQ(expected_value, addr); \
Chih-Hung Hsieh1a0de6a2016-08-26 15:06:11 -070053 EXPECT_EQ(expected_value, (ef)->FindDynamicSymbolAddress(symbol_name)); \
Brian Carlstrom31050c62013-12-15 12:36:09 -080054 } while (false)
Brian Carlstrom700c8d32012-11-05 10:42:02 -080055
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -070056TEST_F(ElfWriterTest, dlsym) {
Igor Murashkin37743352014-11-13 14:38:00 -080057 std::string elf_location = GetCoreOatLocation();
Brian Carlstrom2afe4942014-05-19 10:25:33 -070058 std::string elf_filename = GetSystemImageFilename(elf_location.c_str(), kRuntimeISA);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080059 LOG(INFO) << "elf_filename=" << elf_filename;
60
61 UnreserveImageSpace();
Mathieu Chartier2cebb242015-04-21 16:50:40 -070062 void* dl_oatdata = nullptr;
63 void* dl_oatexec = nullptr;
64 void* dl_oatlastword = nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080065
Ian Rogers700a4022014-05-19 16:49:03 -070066 std::unique_ptr<File> file(OS::OpenFileForReading(elf_filename.c_str()));
Richard Uhler67e1dc52017-02-06 16:50:17 +000067 ASSERT_TRUE(file.get() != nullptr) << elf_filename;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080068 {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070069 std::string error_msg;
Mathieu Chartierbcb6a722016-03-08 16:49:58 -080070 std::unique_ptr<ElfFile> ef(ElfFile::Open(file.get(),
Vladimir Markoc09cd052018-08-23 16:36:36 +010071 /* writable */ false,
72 /* program_header_only */ false,
Mathieu Chartierbcb6a722016-03-08 16:49:58 -080073 /*low_4gb*/false,
74 &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", false);
77 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatexec, "oatexec", false);
78 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatlastword, "oatlastword", false);
79 }
80 {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070081 std::string error_msg;
Mathieu Chartierbcb6a722016-03-08 16:49:58 -080082 std::unique_ptr<ElfFile> ef(ElfFile::Open(file.get(),
Vladimir Markoc09cd052018-08-23 16:36:36 +010083 /* writable */ false,
84 /* program_header_only */ false,
85 /* low_4gb */ false,
Mathieu Chartierbcb6a722016-03-08 16:49:58 -080086 &error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -070087 CHECK(ef.get() != nullptr) << error_msg;
Brian Carlstrom265091e2013-01-30 14:08:26 -080088 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 Carlstrom700c8d32012-11-05 10:42:02 -080091 }
92 {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070093 std::string error_msg;
Mathieu Chartierbcb6a722016-03-08 16:49:58 -080094 std::unique_ptr<ElfFile> ef(ElfFile::Open(file.get(),
Vladimir Markoc09cd052018-08-23 16:36:36 +010095 /* writable */ false,
96 /* program_header_only */ true,
97 /* low_4gb */ false,
98 &error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -070099 CHECK(ef.get() != nullptr) << error_msg;
Vladimir Markoc09cd052018-08-23 16:36:36 +0100100 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 Uhler67e1dc52017-02-06 16:50:17 +0000115 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 Carlstrom700c8d32012-11-05 10:42:02 -0800121 }
122}
123
Alexey Alexandrovab40c112016-09-19 09:33:49 -0700124TEST_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 Markoc09cd052018-08-23 16:36:36 +0100134 /* writable */ false,
135 /* program_header_only */ false,
136 /* low_4gb */ false,
Alexey Alexandrovab40c112016-09-19 09:33:49 -0700137 &error_msg));
138 CHECK(ef.get() != nullptr) << error_msg;
139 EXPECT_TRUE(ef->HasSection(".note.gnu.build-id"));
140 }
141}
142
David Srbecky2f6cdb02015-04-11 00:17:53 +0100143TEST_F(ElfWriterTest, EncodeDecodeOatPatches) {
David Srbeckyf8980872015-05-22 17:04:47 +0100144 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 Srbecky2f6cdb02015-04-11 00:17:53 +0100152
David Srbeckyf8980872015-05-22 17:04:47 +0100153 // Encode patch locations.
154 std::vector<uint8_t> oat_patches;
Vladimir Marko10c13562015-11-25 14:33:36 +0000155 ElfBuilder<ElfTypes32>::EncodeOatPatches(ArrayRef<const uintptr_t>(patch_locations),
156 &oat_patches);
David Srbecky2f6cdb02015-04-11 00:17:53 +0100157
David Srbeckyf8980872015-05-22 17:04:47 +0100158 // 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 Srbecky2f6cdb02015-04-11 00:17:53 +0100178 }
David Srbecky2f6cdb02015-04-11 00:17:53 +0100179}
180
Vladimir Marko74527972016-11-29 15:57:32 +0000181} // namespace linker
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800182} // namespace art