blob: 864dadc96317d9fe633de6c77b71dadb288937d0 [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
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080019#include "common_compiler_test.h"
20#include "oat.h"
21
Brian Carlstrom700c8d32012-11-05 10:42:02 -080022namespace art {
23
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080024class ElfWriterTest : public CommonCompilerTest {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080025 protected:
26 virtual void SetUp() {
27 ReserveImageSpace();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080028 CommonCompilerTest::SetUp();
Brian Carlstrom700c8d32012-11-05 10:42:02 -080029 }
30};
31
Brian Carlstrom31050c62013-12-15 12:36:09 -080032#define EXPECT_ELF_FILE_ADDRESS(ef, expected_value, symbol_name, build_map) \
33 do { \
Nicolas Geoffray50cfe742014-02-19 13:27:42 +000034 void* addr = reinterpret_cast<void*>(ef->FindSymbolAddress(SHT_DYNSYM, \
Brian Carlstrom31050c62013-12-15 12:36:09 -080035 symbol_name, \
36 build_map)); \
37 EXPECT_NE(nullptr, addr); \
38 EXPECT_LT(static_cast<uintptr_t>(ART_BASE_ADDRESS), reinterpret_cast<uintptr_t>(addr)); \
39 if (expected_value == nullptr) { \
40 expected_value = addr; \
41 } \
42 EXPECT_EQ(expected_value, addr); \
43 EXPECT_EQ(expected_value, ef->FindDynamicSymbolAddress(symbol_name)); \
44 } while (false)
Brian Carlstrom700c8d32012-11-05 10:42:02 -080045
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -070046TEST_F(ElfWriterTest, dlsym) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080047 std::string elf_filename;
48 if (IsHost()) {
49 const char* host_dir = getenv("ANDROID_HOST_OUT");
50 CHECK(host_dir != NULL);
51 elf_filename = StringPrintf("%s/framework/core.oat", host_dir);
52 } else {
Andreas Gampee400aa22014-04-18 10:00:40 -070053#ifdef __LP64__
54 elf_filename = "/data/art-test64/core.oat";
55#else
Brian Carlstrom700c8d32012-11-05 10:42:02 -080056 elf_filename = "/data/art-test/core.oat";
Andreas Gampee400aa22014-04-18 10:00:40 -070057#endif
Brian Carlstrom700c8d32012-11-05 10:42:02 -080058 }
59 LOG(INFO) << "elf_filename=" << elf_filename;
60
61 UnreserveImageSpace();
Brian Carlstrom31050c62013-12-15 12:36:09 -080062 void* dl_oatdata = NULL;
63 void* dl_oatexec = NULL;
64 void* dl_oatlastword = NULL;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080065
Brian Carlstrom31050c62013-12-15 12:36:09 -080066#if defined(ART_USE_PORTABLE_COMPILER)
67 {
68 // We only use dlopen for loading with portable. See OatFile::Open.
69 void* dl_oat_so = dlopen(elf_filename.c_str(), RTLD_NOW);
70 ASSERT_TRUE(dl_oat_so != NULL) << dlerror();
71 dl_oatdata = dlsym(dl_oat_so, "oatdata");
72 ASSERT_TRUE(dl_oatdata != NULL);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080073
Brian Carlstrom31050c62013-12-15 12:36:09 -080074 OatHeader* dl_oat_header = reinterpret_cast<OatHeader*>(dl_oatdata);
75 ASSERT_TRUE(dl_oat_header->IsValid());
76 dl_oatexec = dlsym(dl_oat_so, "oatexec");
77 ASSERT_TRUE(dl_oatexec != NULL);
78 ASSERT_LT(dl_oatdata, dl_oatexec);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080079
Brian Carlstrom31050c62013-12-15 12:36:09 -080080 dl_oatlastword = dlsym(dl_oat_so, "oatlastword");
81 ASSERT_TRUE(dl_oatlastword != NULL);
82 ASSERT_LT(dl_oatexec, dl_oatlastword);
83
84 ASSERT_EQ(0, dlclose(dl_oat_so));
85 }
86#endif
Brian Carlstrom700c8d32012-11-05 10:42:02 -080087
Brian Carlstrom7571e8b2013-08-12 17:04:14 -070088 UniquePtr<File> file(OS::OpenFileForReading(elf_filename.c_str()));
Brian Carlstrom700c8d32012-11-05 10:42:02 -080089 ASSERT_TRUE(file.get() != NULL);
90 {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070091 std::string error_msg;
92 UniquePtr<ElfFile> ef(ElfFile::Open(file.get(), false, false, &error_msg));
93 CHECK(ef.get() != nullptr) << error_msg;
Brian Carlstrom265091e2013-01-30 14:08:26 -080094 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatdata, "oatdata", false);
95 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatexec, "oatexec", false);
96 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatlastword, "oatlastword", false);
97 }
98 {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070099 std::string error_msg;
100 UniquePtr<ElfFile> ef(ElfFile::Open(file.get(), false, false, &error_msg));
101 CHECK(ef.get() != nullptr) << error_msg;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800102 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatdata, "oatdata", true);
103 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatexec, "oatexec", true);
104 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatlastword, "oatlastword", true);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800105 }
106 {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700107 std::string error_msg;
108 UniquePtr<ElfFile> ef(ElfFile::Open(file.get(), false, true, &error_msg));
109 CHECK(ef.get() != nullptr) << error_msg;
110 CHECK(ef->Load(false, &error_msg)) << error_msg;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800111 EXPECT_EQ(dl_oatdata, ef->FindDynamicSymbolAddress("oatdata"));
112 EXPECT_EQ(dl_oatexec, ef->FindDynamicSymbolAddress("oatexec"));
113 EXPECT_EQ(dl_oatlastword, ef->FindDynamicSymbolAddress("oatlastword"));
114 }
115}
116
117} // namespace art