1 /*
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 "elf_file.h"
18
19 #include "base/stringprintf.h"
20 #include "common_compiler_test.h"
21 #include "oat.h"
22 #include "utils.h"
23
24 namespace art {
25
26 class ElfWriterTest : public CommonCompilerTest {
27 protected:
SetUp()28 virtual void SetUp() {
29 ReserveImageSpace();
30 CommonCompilerTest::SetUp();
31 }
32 };
33
34 #define EXPECT_ELF_FILE_ADDRESS(ef, expected_value, symbol_name, build_map) \
35 do { \
36 void* addr = reinterpret_cast<void*>(ef->FindSymbolAddress(SHT_DYNSYM, \
37 symbol_name, \
38 build_map)); \
39 EXPECT_NE(nullptr, addr); \
40 EXPECT_LT(static_cast<uintptr_t>(ART_BASE_ADDRESS), reinterpret_cast<uintptr_t>(addr)); \
41 if (expected_value == nullptr) { \
42 expected_value = addr; \
43 } \
44 EXPECT_EQ(expected_value, addr); \
45 EXPECT_EQ(expected_value, ef->FindDynamicSymbolAddress(symbol_name)); \
46 } while (false)
47
TEST_F(ElfWriterTest,dlsym)48 TEST_F(ElfWriterTest, dlsym) {
49 std::string elf_location;
50 if (IsHost()) {
51 const char* host_dir = getenv("ANDROID_HOST_OUT");
52 CHECK(host_dir != NULL);
53 elf_location = StringPrintf("%s/framework/core.oat", host_dir);
54 } else {
55 elf_location = "/data/art-test/core.oat";
56 }
57 std::string elf_filename = GetSystemImageFilename(elf_location.c_str(), kRuntimeISA);
58 LOG(INFO) << "elf_filename=" << elf_filename;
59
60 UnreserveImageSpace();
61 void* dl_oatdata = NULL;
62 void* dl_oatexec = NULL;
63 void* dl_oatlastword = NULL;
64
65 #if defined(ART_USE_PORTABLE_COMPILER)
66 {
67 // We only use dlopen for loading with portable. See OatFile::Open.
68 void* dl_oat_so = dlopen(elf_filename.c_str(), RTLD_NOW);
69 ASSERT_TRUE(dl_oat_so != NULL) << dlerror();
70 dl_oatdata = dlsym(dl_oat_so, "oatdata");
71 ASSERT_TRUE(dl_oatdata != NULL);
72
73 OatHeader* dl_oat_header = reinterpret_cast<OatHeader*>(dl_oatdata);
74 ASSERT_TRUE(dl_oat_header->IsValid());
75 dl_oatexec = dlsym(dl_oat_so, "oatexec");
76 ASSERT_TRUE(dl_oatexec != NULL);
77 ASSERT_LT(dl_oatdata, dl_oatexec);
78
79 dl_oatlastword = dlsym(dl_oat_so, "oatlastword");
80 ASSERT_TRUE(dl_oatlastword != NULL);
81 ASSERT_LT(dl_oatexec, dl_oatlastword);
82
83 ASSERT_EQ(0, dlclose(dl_oat_so));
84 }
85 #endif
86
87 std::unique_ptr<File> file(OS::OpenFileForReading(elf_filename.c_str()));
88 ASSERT_TRUE(file.get() != NULL);
89 {
90 std::string error_msg;
91 std::unique_ptr<ElfFile> ef(ElfFile::Open(file.get(), false, false, &error_msg));
92 CHECK(ef.get() != nullptr) << error_msg;
93 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatdata, "oatdata", false);
94 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatexec, "oatexec", false);
95 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatlastword, "oatlastword", false);
96 }
97 {
98 std::string error_msg;
99 std::unique_ptr<ElfFile> ef(ElfFile::Open(file.get(), false, false, &error_msg));
100 CHECK(ef.get() != nullptr) << error_msg;
101 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatdata, "oatdata", true);
102 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatexec, "oatexec", true);
103 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatlastword, "oatlastword", true);
104 }
105 {
106 std::string error_msg;
107 std::unique_ptr<ElfFile> ef(ElfFile::Open(file.get(), false, true, &error_msg));
108 CHECK(ef.get() != nullptr) << error_msg;
109 CHECK(ef->Load(false, &error_msg)) << error_msg;
110 EXPECT_EQ(dl_oatdata, ef->FindDynamicSymbolAddress("oatdata"));
111 EXPECT_EQ(dl_oatexec, ef->FindDynamicSymbolAddress("oatexec"));
112 EXPECT_EQ(dl_oatlastword, ef->FindDynamicSymbolAddress("oatlastword"));
113 }
114 }
115
116 } // namespace art
117