blob: fd3a9121ae197cd208f48870a16c5d23972734e8 [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 Rogerse63db272014-07-15 15:36:11 -070019#include "base/stringprintf.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"
22#include "oat.h"
Ian Rogerse63db272014-07-15 15:36:11 -070023#include "utils.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080024
Brian Carlstrom700c8d32012-11-05 10:42:02 -080025namespace art {
26
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080027class ElfWriterTest : public CommonCompilerTest {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080028 protected:
29 virtual void SetUp() {
30 ReserveImageSpace();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080031 CommonCompilerTest::SetUp();
Brian Carlstrom700c8d32012-11-05 10:42:02 -080032 }
33};
34
Brian Carlstrom31050c62013-12-15 12:36:09 -080035#define EXPECT_ELF_FILE_ADDRESS(ef, expected_value, symbol_name, build_map) \
36 do { \
Nicolas Geoffray50cfe742014-02-19 13:27:42 +000037 void* addr = reinterpret_cast<void*>(ef->FindSymbolAddress(SHT_DYNSYM, \
Brian Carlstrom31050c62013-12-15 12:36:09 -080038 symbol_name, \
39 build_map)); \
40 EXPECT_NE(nullptr, addr); \
41 EXPECT_LT(static_cast<uintptr_t>(ART_BASE_ADDRESS), reinterpret_cast<uintptr_t>(addr)); \
42 if (expected_value == nullptr) { \
43 expected_value = addr; \
44 } \
45 EXPECT_EQ(expected_value, addr); \
46 EXPECT_EQ(expected_value, ef->FindDynamicSymbolAddress(symbol_name)); \
47 } while (false)
Brian Carlstrom700c8d32012-11-05 10:42:02 -080048
Nicolas Geoffray72c25a92014-12-04 16:44:58 +000049#if defined(ART_USE_OPTIMIZING_COMPILER)
50TEST_F(ElfWriterTest, DISABLED_dlsym) {
51#else
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -070052TEST_F(ElfWriterTest, dlsym) {
Nicolas Geoffray72c25a92014-12-04 16:44:58 +000053#endif
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();
Brian Carlstrom31050c62013-12-15 12:36:09 -080059 void* dl_oatdata = NULL;
60 void* dl_oatexec = NULL;
61 void* dl_oatlastword = NULL;
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()));
Brian Carlstrom700c8d32012-11-05 10:42:02 -080064 ASSERT_TRUE(file.get() != NULL);
65 {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070066 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -070067 std::unique_ptr<ElfFile> ef(ElfFile::Open(file.get(), false, false, &error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -070068 CHECK(ef.get() != nullptr) << error_msg;
Brian Carlstrom265091e2013-01-30 14:08:26 -080069 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatdata, "oatdata", false);
70 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatexec, "oatexec", false);
71 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatlastword, "oatlastword", false);
72 }
73 {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070074 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -070075 std::unique_ptr<ElfFile> ef(ElfFile::Open(file.get(), false, false, &error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -070076 CHECK(ef.get() != nullptr) << error_msg;
Brian Carlstrom265091e2013-01-30 14:08:26 -080077 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatdata, "oatdata", true);
78 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatexec, "oatexec", true);
79 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatlastword, "oatlastword", true);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080080 }
81 {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070082 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -070083 std::unique_ptr<ElfFile> ef(ElfFile::Open(file.get(), false, true, &error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -070084 CHECK(ef.get() != nullptr) << error_msg;
85 CHECK(ef->Load(false, &error_msg)) << error_msg;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080086 EXPECT_EQ(dl_oatdata, ef->FindDynamicSymbolAddress("oatdata"));
87 EXPECT_EQ(dl_oatexec, ef->FindDynamicSymbolAddress("oatexec"));
88 EXPECT_EQ(dl_oatlastword, ef->FindDynamicSymbolAddress("oatlastword"));
89 }
90}
91
92} // namespace art