blob: 8427e7b8ce6f1b90ec09a9ba913b058f53386e59 [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
David Sehr891a50e2017-10-27 17:01:07 -070019#include "base/file_utils.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"
24#include "elf_writer_quick.h"
Vladimir Marko74527972016-11-29 15:57:32 +000025#include "linker/elf_builder.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 {
Vladimir Marko74527972016-11-29 15:57:32 +000030namespace linker {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080031
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080032class ElfWriterTest : public CommonCompilerTest {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080033 protected:
34 virtual void SetUp() {
35 ReserveImageSpace();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080036 CommonCompilerTest::SetUp();
Brian Carlstrom700c8d32012-11-05 10:42:02 -080037 }
38};
39
Brian Carlstrom31050c62013-12-15 12:36:09 -080040#define EXPECT_ELF_FILE_ADDRESS(ef, expected_value, symbol_name, build_map) \
41 do { \
Chih-Hung Hsieh1a0de6a2016-08-26 15:06:11 -070042 void* addr = reinterpret_cast<void*>((ef)->FindSymbolAddress(SHT_DYNSYM, \
43 symbol_name, \
44 build_map)); \
Brian Carlstrom31050c62013-12-15 12:36:09 -080045 EXPECT_NE(nullptr, addr); \
Chih-Hung Hsieh1a0de6a2016-08-26 15:06:11 -070046 if ((expected_value) == nullptr) { \
47 (expected_value) = addr; \
Brian Carlstrom31050c62013-12-15 12:36:09 -080048 } \
49 EXPECT_EQ(expected_value, addr); \
Chih-Hung Hsieh1a0de6a2016-08-26 15:06:11 -070050 EXPECT_EQ(expected_value, (ef)->FindDynamicSymbolAddress(symbol_name)); \
Brian Carlstrom31050c62013-12-15 12:36:09 -080051 } 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()));
Richard Uhler67e1dc52017-02-06 16:50:17 +000064 ASSERT_TRUE(file.get() != nullptr) << elf_filename;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080065 {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070066 std::string error_msg;
Mathieu Chartierbcb6a722016-03-08 16:49:58 -080067 std::unique_ptr<ElfFile> ef(ElfFile::Open(file.get(),
68 false,
69 false,
70 /*low_4gb*/false,
71 &error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -070072 CHECK(ef.get() != nullptr) << error_msg;
Brian Carlstrom265091e2013-01-30 14:08:26 -080073 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 Rogers8d31bbd2013-10-13 10:44:14 -070078 std::string error_msg;
Mathieu Chartierbcb6a722016-03-08 16:49:58 -080079 std::unique_ptr<ElfFile> ef(ElfFile::Open(file.get(),
80 false,
81 false,
82 /*low_4gb*/false,
83 &error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -070084 CHECK(ef.get() != nullptr) << error_msg;
Brian Carlstrom265091e2013-01-30 14:08:26 -080085 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 Carlstrom700c8d32012-11-05 10:42:02 -080088 }
89 {
Richard Uhler67e1dc52017-02-06 16:50:17 +000090 uint8_t* base = reinterpret_cast<uint8_t*>(ART_BASE_ADDRESS);
Ian Rogers8d31bbd2013-10-13 10:44:14 -070091 std::string error_msg;
Mathieu Chartierbcb6a722016-03-08 16:49:58 -080092 std::unique_ptr<ElfFile> ef(ElfFile::Open(file.get(),
93 false,
94 true,
95 /*low_4gb*/false,
Richard Uhler67e1dc52017-02-06 16:50:17 +000096 &error_msg,
97 base));
Ian Rogers8d31bbd2013-10-13 10:44:14 -070098 CHECK(ef.get() != nullptr) << error_msg;
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -070099 CHECK(ef->Load(file.get(), false, /*low_4gb*/false, &error_msg)) << error_msg;
Richard Uhler67e1dc52017-02-06 16:50:17 +0000100 EXPECT_EQ(reinterpret_cast<uintptr_t>(dl_oatdata) + reinterpret_cast<uintptr_t>(base),
101 reinterpret_cast<uintptr_t>(ef->FindDynamicSymbolAddress("oatdata")));
102 EXPECT_EQ(reinterpret_cast<uintptr_t>(dl_oatexec) + reinterpret_cast<uintptr_t>(base),
103 reinterpret_cast<uintptr_t>(ef->FindDynamicSymbolAddress("oatexec")));
104 EXPECT_EQ(reinterpret_cast<uintptr_t>(dl_oatlastword) + reinterpret_cast<uintptr_t>(base),
105 reinterpret_cast<uintptr_t>(ef->FindDynamicSymbolAddress("oatlastword")));
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800106 }
107}
108
Alexey Alexandrovab40c112016-09-19 09:33:49 -0700109TEST_F(ElfWriterTest, CheckBuildIdPresent) {
110 std::string elf_location = GetCoreOatLocation();
111 std::string elf_filename = GetSystemImageFilename(elf_location.c_str(), kRuntimeISA);
112 LOG(INFO) << "elf_filename=" << elf_filename;
113
114 std::unique_ptr<File> file(OS::OpenFileForReading(elf_filename.c_str()));
115 ASSERT_TRUE(file.get() != nullptr);
116 {
117 std::string error_msg;
118 std::unique_ptr<ElfFile> ef(ElfFile::Open(file.get(),
119 false,
120 false,
121 /*low_4gb*/false,
122 &error_msg));
123 CHECK(ef.get() != nullptr) << error_msg;
124 EXPECT_TRUE(ef->HasSection(".note.gnu.build-id"));
125 }
126}
127
David Srbecky2f6cdb02015-04-11 00:17:53 +0100128TEST_F(ElfWriterTest, EncodeDecodeOatPatches) {
David Srbeckyf8980872015-05-22 17:04:47 +0100129 const std::vector<std::vector<uintptr_t>> test_data {
130 { 0, 4, 8, 15, 128, 200 },
131 { 8, 8 + 127 },
132 { 8, 8 + 128 },
133 { },
134 };
135 for (const auto& patch_locations : test_data) {
136 constexpr int32_t delta = 0x11235813;
David Srbecky2f6cdb02015-04-11 00:17:53 +0100137
David Srbeckyf8980872015-05-22 17:04:47 +0100138 // Encode patch locations.
139 std::vector<uint8_t> oat_patches;
Vladimir Marko10c13562015-11-25 14:33:36 +0000140 ElfBuilder<ElfTypes32>::EncodeOatPatches(ArrayRef<const uintptr_t>(patch_locations),
141 &oat_patches);
David Srbecky2f6cdb02015-04-11 00:17:53 +0100142
David Srbeckyf8980872015-05-22 17:04:47 +0100143 // Create buffer to be patched.
144 std::vector<uint8_t> initial_data(256);
145 for (size_t i = 0; i < initial_data.size(); i++) {
146 initial_data[i] = i;
147 }
148
149 // Patch manually.
150 std::vector<uint8_t> expected = initial_data;
151 for (uintptr_t location : patch_locations) {
152 typedef __attribute__((__aligned__(1))) uint32_t UnalignedAddress;
153 *reinterpret_cast<UnalignedAddress*>(expected.data() + location) += delta;
154 }
155
156 // Decode and apply patch locations.
157 std::vector<uint8_t> actual = initial_data;
158 ElfFileImpl32::ApplyOatPatches(
159 oat_patches.data(), oat_patches.data() + oat_patches.size(), delta,
160 actual.data(), actual.data() + actual.size());
161
162 EXPECT_EQ(expected, actual);
David Srbecky2f6cdb02015-04-11 00:17:53 +0100163 }
David Srbecky2f6cdb02015-04-11 00:17:53 +0100164}
165
Vladimir Marko74527972016-11-29 15:57:32 +0000166} // namespace linker
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800167} // namespace art