blob: 5488e2f6d048c1c2a1fac5591e2dc5587ac5462f [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
Brian Carlstrom2afe4942014-05-19 10:25:33 -070054 std::string elf_location;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080055 if (IsHost()) {
56 const char* host_dir = getenv("ANDROID_HOST_OUT");
57 CHECK(host_dir != NULL);
Brian Carlstrom2afe4942014-05-19 10:25:33 -070058 elf_location = StringPrintf("%s/framework/core.oat", host_dir);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080059 } else {
Brian Carlstrom2afe4942014-05-19 10:25:33 -070060 elf_location = "/data/art-test/core.oat";
Brian Carlstrom700c8d32012-11-05 10:42:02 -080061 }
Brian Carlstrom2afe4942014-05-19 10:25:33 -070062 std::string elf_filename = GetSystemImageFilename(elf_location.c_str(), kRuntimeISA);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080063 LOG(INFO) << "elf_filename=" << elf_filename;
64
65 UnreserveImageSpace();
Brian Carlstrom31050c62013-12-15 12:36:09 -080066 void* dl_oatdata = NULL;
67 void* dl_oatexec = NULL;
68 void* dl_oatlastword = NULL;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080069
Brian Carlstrom31050c62013-12-15 12:36:09 -080070#if defined(ART_USE_PORTABLE_COMPILER)
71 {
72 // We only use dlopen for loading with portable. See OatFile::Open.
73 void* dl_oat_so = dlopen(elf_filename.c_str(), RTLD_NOW);
74 ASSERT_TRUE(dl_oat_so != NULL) << dlerror();
75 dl_oatdata = dlsym(dl_oat_so, "oatdata");
76 ASSERT_TRUE(dl_oatdata != NULL);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080077
Brian Carlstrom31050c62013-12-15 12:36:09 -080078 OatHeader* dl_oat_header = reinterpret_cast<OatHeader*>(dl_oatdata);
79 ASSERT_TRUE(dl_oat_header->IsValid());
80 dl_oatexec = dlsym(dl_oat_so, "oatexec");
81 ASSERT_TRUE(dl_oatexec != NULL);
82 ASSERT_LT(dl_oatdata, dl_oatexec);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080083
Brian Carlstrom31050c62013-12-15 12:36:09 -080084 dl_oatlastword = dlsym(dl_oat_so, "oatlastword");
85 ASSERT_TRUE(dl_oatlastword != NULL);
86 ASSERT_LT(dl_oatexec, dl_oatlastword);
87
88 ASSERT_EQ(0, dlclose(dl_oat_so));
89 }
90#endif
Brian Carlstrom700c8d32012-11-05 10:42:02 -080091
Ian Rogers700a4022014-05-19 16:49:03 -070092 std::unique_ptr<File> file(OS::OpenFileForReading(elf_filename.c_str()));
Brian Carlstrom700c8d32012-11-05 10:42:02 -080093 ASSERT_TRUE(file.get() != NULL);
94 {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070095 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -070096 std::unique_ptr<ElfFile> ef(ElfFile::Open(file.get(), false, false, &error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -070097 CHECK(ef.get() != nullptr) << error_msg;
Brian Carlstrom265091e2013-01-30 14:08:26 -080098 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatdata, "oatdata", false);
99 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatexec, "oatexec", false);
100 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatlastword, "oatlastword", false);
101 }
102 {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700103 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -0700104 std::unique_ptr<ElfFile> ef(ElfFile::Open(file.get(), false, false, &error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700105 CHECK(ef.get() != nullptr) << error_msg;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800106 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatdata, "oatdata", true);
107 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatexec, "oatexec", true);
108 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatlastword, "oatlastword", true);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800109 }
110 {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700111 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -0700112 std::unique_ptr<ElfFile> ef(ElfFile::Open(file.get(), false, true, &error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700113 CHECK(ef.get() != nullptr) << error_msg;
114 CHECK(ef->Load(false, &error_msg)) << error_msg;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800115 EXPECT_EQ(dl_oatdata, ef->FindDynamicSymbolAddress("oatdata"));
116 EXPECT_EQ(dl_oatexec, ef->FindDynamicSymbolAddress("oatexec"));
117 EXPECT_EQ(dl_oatlastword, ef->FindDynamicSymbolAddress("oatlastword"));
118 }
119}
120
121} // namespace art