blob: 9669c4a9d36027d39369232e38fac7c5462a4901 [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
Ian Rogersd4c4d952014-10-16 20:31:53 -070019#include "base/unix_file/fd_file.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080020#include "common_compiler_test.h"
David Srbecky2f6cdb02015-04-11 00:17:53 +010021#include "elf_file.h"
22#include "elf_file_impl.h"
David Srbecky6d8c8f02015-10-26 10:57:09 +000023#include "elf_builder.h"
David Srbecky2f6cdb02015-04-11 00:17:53 +010024#include "elf_writer_quick.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080025#include "oat.h"
Ian Rogerse63db272014-07-15 15:36:11 -070026#include "utils.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080027
Brian Carlstrom700c8d32012-11-05 10:42:02 -080028namespace art {
29
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080030class ElfWriterTest : public CommonCompilerTest {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080031 protected:
32 virtual void SetUp() {
33 ReserveImageSpace();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080034 CommonCompilerTest::SetUp();
Brian Carlstrom700c8d32012-11-05 10:42:02 -080035 }
36};
37
Brian Carlstrom31050c62013-12-15 12:36:09 -080038#define EXPECT_ELF_FILE_ADDRESS(ef, expected_value, symbol_name, build_map) \
39 do { \
Chih-Hung Hsieh1a0de6a2016-08-26 15:06:11 -070040 void* addr = reinterpret_cast<void*>((ef)->FindSymbolAddress(SHT_DYNSYM, \
41 symbol_name, \
42 build_map)); \
Brian Carlstrom31050c62013-12-15 12:36:09 -080043 EXPECT_NE(nullptr, addr); \
44 EXPECT_LT(static_cast<uintptr_t>(ART_BASE_ADDRESS), reinterpret_cast<uintptr_t>(addr)); \
Chih-Hung Hsieh1a0de6a2016-08-26 15:06:11 -070045 if ((expected_value) == nullptr) { \
46 (expected_value) = addr; \
Brian Carlstrom31050c62013-12-15 12:36:09 -080047 } \
48 EXPECT_EQ(expected_value, addr); \
Chih-Hung Hsieh1a0de6a2016-08-26 15:06:11 -070049 EXPECT_EQ(expected_value, (ef)->FindDynamicSymbolAddress(symbol_name)); \
Brian Carlstrom31050c62013-12-15 12:36:09 -080050 } while (false)
Brian Carlstrom700c8d32012-11-05 10:42:02 -080051
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -070052TEST_F(ElfWriterTest, dlsym) {
Igor Murashkin37743352014-11-13 14:38:00 -080053 std::string elf_location = GetCoreOatLocation();
Brian Carlstrom2afe4942014-05-19 10:25:33 -070054 std::string elf_filename = GetSystemImageFilename(elf_location.c_str(), kRuntimeISA);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080055 LOG(INFO) << "elf_filename=" << elf_filename;
56
57 UnreserveImageSpace();
Mathieu Chartier2cebb242015-04-21 16:50:40 -070058 void* dl_oatdata = nullptr;
59 void* dl_oatexec = nullptr;
60 void* dl_oatlastword = nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080061
Ian Rogers700a4022014-05-19 16:49:03 -070062 std::unique_ptr<File> file(OS::OpenFileForReading(elf_filename.c_str()));
Mathieu Chartier2cebb242015-04-21 16:50:40 -070063 ASSERT_TRUE(file.get() != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080064 {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070065 std::string error_msg;
Mathieu Chartierbcb6a722016-03-08 16:49:58 -080066 std::unique_ptr<ElfFile> ef(ElfFile::Open(file.get(),
67 false,
68 false,
69 /*low_4gb*/false,
70 &error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -070071 CHECK(ef.get() != nullptr) << error_msg;
Brian Carlstrom265091e2013-01-30 14:08:26 -080072 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatdata, "oatdata", false);
73 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatexec, "oatexec", false);
74 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatlastword, "oatlastword", false);
75 }
76 {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070077 std::string error_msg;
Mathieu Chartierbcb6a722016-03-08 16:49:58 -080078 std::unique_ptr<ElfFile> ef(ElfFile::Open(file.get(),
79 false,
80 false,
81 /*low_4gb*/false,
82 &error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -070083 CHECK(ef.get() != nullptr) << error_msg;
Brian Carlstrom265091e2013-01-30 14:08:26 -080084 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatdata, "oatdata", true);
85 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatexec, "oatexec", true);
86 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatlastword, "oatlastword", true);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080087 }
88 {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070089 std::string error_msg;
Mathieu Chartierbcb6a722016-03-08 16:49:58 -080090 std::unique_ptr<ElfFile> ef(ElfFile::Open(file.get(),
91 false,
92 true,
93 /*low_4gb*/false,
94 &error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -070095 CHECK(ef.get() != nullptr) << error_msg;
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -070096 CHECK(ef->Load(file.get(), false, /*low_4gb*/false, &error_msg)) << error_msg;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080097 EXPECT_EQ(dl_oatdata, ef->FindDynamicSymbolAddress("oatdata"));
98 EXPECT_EQ(dl_oatexec, ef->FindDynamicSymbolAddress("oatexec"));
99 EXPECT_EQ(dl_oatlastword, ef->FindDynamicSymbolAddress("oatlastword"));
100 }
101}
102
Alexey Alexandrovab40c112016-09-19 09:33:49 -0700103TEST_F(ElfWriterTest, CheckBuildIdPresent) {
104 std::string elf_location = GetCoreOatLocation();
105 std::string elf_filename = GetSystemImageFilename(elf_location.c_str(), kRuntimeISA);
106 LOG(INFO) << "elf_filename=" << elf_filename;
107
108 std::unique_ptr<File> file(OS::OpenFileForReading(elf_filename.c_str()));
109 ASSERT_TRUE(file.get() != nullptr);
110 {
111 std::string error_msg;
112 std::unique_ptr<ElfFile> ef(ElfFile::Open(file.get(),
113 false,
114 false,
115 /*low_4gb*/false,
116 &error_msg));
117 CHECK(ef.get() != nullptr) << error_msg;
118 EXPECT_TRUE(ef->HasSection(".note.gnu.build-id"));
119 }
120}
121
David Srbecky2f6cdb02015-04-11 00:17:53 +0100122TEST_F(ElfWriterTest, EncodeDecodeOatPatches) {
David Srbeckyf8980872015-05-22 17:04:47 +0100123 const std::vector<std::vector<uintptr_t>> test_data {
124 { 0, 4, 8, 15, 128, 200 },
125 { 8, 8 + 127 },
126 { 8, 8 + 128 },
127 { },
128 };
129 for (const auto& patch_locations : test_data) {
130 constexpr int32_t delta = 0x11235813;
David Srbecky2f6cdb02015-04-11 00:17:53 +0100131
David Srbeckyf8980872015-05-22 17:04:47 +0100132 // Encode patch locations.
133 std::vector<uint8_t> oat_patches;
Vladimir Marko10c13562015-11-25 14:33:36 +0000134 ElfBuilder<ElfTypes32>::EncodeOatPatches(ArrayRef<const uintptr_t>(patch_locations),
135 &oat_patches);
David Srbecky2f6cdb02015-04-11 00:17:53 +0100136
David Srbeckyf8980872015-05-22 17:04:47 +0100137 // Create buffer to be patched.
138 std::vector<uint8_t> initial_data(256);
139 for (size_t i = 0; i < initial_data.size(); i++) {
140 initial_data[i] = i;
141 }
142
143 // Patch manually.
144 std::vector<uint8_t> expected = initial_data;
145 for (uintptr_t location : patch_locations) {
146 typedef __attribute__((__aligned__(1))) uint32_t UnalignedAddress;
147 *reinterpret_cast<UnalignedAddress*>(expected.data() + location) += delta;
148 }
149
150 // Decode and apply patch locations.
151 std::vector<uint8_t> actual = initial_data;
152 ElfFileImpl32::ApplyOatPatches(
153 oat_patches.data(), oat_patches.data() + oat_patches.size(), delta,
154 actual.data(), actual.data() + actual.size());
155
156 EXPECT_EQ(expected, actual);
David Srbecky2f6cdb02015-04-11 00:17:53 +0100157 }
David Srbecky2f6cdb02015-04-11 00:17:53 +0100158}
159
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800160} // namespace art