blob: 5bad0d00eaf9e86142f8cdbf47e2434eb5c2e326 [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
17#include "common_test.h"
18
19#include "oat.h"
20#include "elf_file.h"
21
22namespace art {
23
24class ElfWriterTest : public CommonTest {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080025 protected:
26 virtual void SetUp() {
27 ReserveImageSpace();
28 CommonTest::SetUp();
29 }
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 {
53 elf_filename = "/data/art-test/core.oat";
54 }
55 LOG(INFO) << "elf_filename=" << elf_filename;
56
57 UnreserveImageSpace();
Brian Carlstrom31050c62013-12-15 12:36:09 -080058 void* dl_oatdata = NULL;
59 void* dl_oatexec = NULL;
60 void* dl_oatlastword = NULL;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080061
Brian Carlstrom31050c62013-12-15 12:36:09 -080062#if defined(ART_USE_PORTABLE_COMPILER)
63 {
64 // We only use dlopen for loading with portable. See OatFile::Open.
65 void* dl_oat_so = dlopen(elf_filename.c_str(), RTLD_NOW);
66 ASSERT_TRUE(dl_oat_so != NULL) << dlerror();
67 dl_oatdata = dlsym(dl_oat_so, "oatdata");
68 ASSERT_TRUE(dl_oatdata != NULL);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080069
Brian Carlstrom31050c62013-12-15 12:36:09 -080070 OatHeader* dl_oat_header = reinterpret_cast<OatHeader*>(dl_oatdata);
71 ASSERT_TRUE(dl_oat_header->IsValid());
72 dl_oatexec = dlsym(dl_oat_so, "oatexec");
73 ASSERT_TRUE(dl_oatexec != NULL);
74 ASSERT_LT(dl_oatdata, dl_oatexec);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080075
Brian Carlstrom31050c62013-12-15 12:36:09 -080076 dl_oatlastword = dlsym(dl_oat_so, "oatlastword");
77 ASSERT_TRUE(dl_oatlastword != NULL);
78 ASSERT_LT(dl_oatexec, dl_oatlastword);
79
80 ASSERT_EQ(0, dlclose(dl_oat_so));
81 }
82#endif
Brian Carlstrom700c8d32012-11-05 10:42:02 -080083
Brian Carlstrom7571e8b2013-08-12 17:04:14 -070084 UniquePtr<File> file(OS::OpenFileForReading(elf_filename.c_str()));
Brian Carlstrom700c8d32012-11-05 10:42:02 -080085 ASSERT_TRUE(file.get() != NULL);
86 {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070087 std::string error_msg;
88 UniquePtr<ElfFile> ef(ElfFile::Open(file.get(), false, false, &error_msg));
89 CHECK(ef.get() != nullptr) << error_msg;
Brian Carlstrom265091e2013-01-30 14:08:26 -080090 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatdata, "oatdata", false);
91 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatexec, "oatexec", false);
92 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatlastword, "oatlastword", false);
93 }
94 {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070095 std::string error_msg;
96 UniquePtr<ElfFile> ef(ElfFile::Open(file.get(), false, false, &error_msg));
97 CHECK(ef.get() != nullptr) << error_msg;
Brian Carlstrom265091e2013-01-30 14:08:26 -080098 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatdata, "oatdata", true);
99 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatexec, "oatexec", true);
100 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatlastword, "oatlastword", true);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800101 }
102 {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700103 std::string error_msg;
104 UniquePtr<ElfFile> ef(ElfFile::Open(file.get(), false, true, &error_msg));
105 CHECK(ef.get() != nullptr) << error_msg;
106 CHECK(ef->Load(false, &error_msg)) << error_msg;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800107 EXPECT_EQ(dl_oatdata, ef->FindDynamicSymbolAddress("oatdata"));
108 EXPECT_EQ(dl_oatexec, ef->FindDynamicSymbolAddress("oatexec"));
109 EXPECT_EQ(dl_oatlastword, ef->FindDynamicSymbolAddress("oatlastword"));
110 }
111}
112
113} // namespace art