blob: c1ff8f29881eba26019dc55040e12425d40595dd [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"
Vladimir Marko815d5e52019-03-04 10:18:31 +000025#include "common_compiler_driver_test.h"
David Srbecky2faab002019-02-12 16:35:48 +000026#include "elf/elf_builder.h"
David Srbecky2f6cdb02015-04-11 00:17:53 +010027#include "elf_file.h"
28#include "elf_file_impl.h"
29#include "elf_writer_quick.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
Vladimir Marko815d5e52019-03-04 10:18:31 +000035class ElfWriterTest : public CommonCompilerDriverTest {
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 Marko11306592018-10-26 14:22:59 +010071 /*writable=*/ false,
72 /*program_header_only=*/ false,
73 /*low_4gb=*/false,
Mathieu Chartierbcb6a722016-03-08 16:49:58 -080074 &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 Marko11306592018-10-26 14:22:59 +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 Marko11306592018-10-26 14:22:59 +010095 /*writable=*/ false,
96 /*program_header_only=*/ true,
97 /*low_4gb=*/ false,
Vladimir Markoc09cd052018-08-23 16:36:36 +010098 &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",
Vladimir Markoc09cd052018-08-23 16:36:36 +0100104 RoundUp(size, kPageSize),
105 PROT_NONE,
Vladimir Marko11306592018-10-26 14:22:59 +0100106 /*low_4gb=*/ true,
Vladimir Markoc09cd052018-08-23 16:36:36 +0100107 &error_msg);
108 CHECK(reservation.IsValid()) << error_msg;
109 uint8_t* base = reservation.Begin();
110 success =
Vladimir Marko11306592018-10-26 14:22:59 +0100111 ef->Load(file.get(), /*executable=*/ false, /*low_4gb=*/ false, &reservation, &error_msg);
Vladimir Markoc09cd052018-08-23 16:36:36 +0100112 CHECK(success) << error_msg;
113 CHECK(!reservation.IsValid());
Richard Uhler67e1dc52017-02-06 16:50:17 +0000114 EXPECT_EQ(reinterpret_cast<uintptr_t>(dl_oatdata) + reinterpret_cast<uintptr_t>(base),
115 reinterpret_cast<uintptr_t>(ef->FindDynamicSymbolAddress("oatdata")));
116 EXPECT_EQ(reinterpret_cast<uintptr_t>(dl_oatexec) + reinterpret_cast<uintptr_t>(base),
117 reinterpret_cast<uintptr_t>(ef->FindDynamicSymbolAddress("oatexec")));
118 EXPECT_EQ(reinterpret_cast<uintptr_t>(dl_oatlastword) + reinterpret_cast<uintptr_t>(base),
119 reinterpret_cast<uintptr_t>(ef->FindDynamicSymbolAddress("oatlastword")));
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800120 }
121}
122
Alexey Alexandrovab40c112016-09-19 09:33:49 -0700123TEST_F(ElfWriterTest, CheckBuildIdPresent) {
124 std::string elf_location = GetCoreOatLocation();
125 std::string elf_filename = GetSystemImageFilename(elf_location.c_str(), kRuntimeISA);
126 LOG(INFO) << "elf_filename=" << elf_filename;
127
128 std::unique_ptr<File> file(OS::OpenFileForReading(elf_filename.c_str()));
129 ASSERT_TRUE(file.get() != nullptr);
130 {
131 std::string error_msg;
132 std::unique_ptr<ElfFile> ef(ElfFile::Open(file.get(),
Vladimir Marko11306592018-10-26 14:22:59 +0100133 /*writable=*/ false,
134 /*program_header_only=*/ false,
135 /*low_4gb=*/ false,
Alexey Alexandrovab40c112016-09-19 09:33:49 -0700136 &error_msg));
137 CHECK(ef.get() != nullptr) << error_msg;
138 EXPECT_TRUE(ef->HasSection(".note.gnu.build-id"));
139 }
140}
141
Vladimir Marko74527972016-11-29 15:57:32 +0000142} // namespace linker
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800143} // namespace art