blob: dd7ef2a4829e3b9bd1ca7c650a045689be66a5f8 [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 {
53 elf_filename = "/data/art-test/core.oat";
54 }
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -070055 elf_filename = GetSystemImageFilename(elf_filename.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
Brian Carlstrom31050c62013-12-15 12:36:09 -080063#if defined(ART_USE_PORTABLE_COMPILER)
64 {
65 // We only use dlopen for loading with portable. See OatFile::Open.
66 void* dl_oat_so = dlopen(elf_filename.c_str(), RTLD_NOW);
67 ASSERT_TRUE(dl_oat_so != NULL) << dlerror();
68 dl_oatdata = dlsym(dl_oat_so, "oatdata");
69 ASSERT_TRUE(dl_oatdata != NULL);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080070
Brian Carlstrom31050c62013-12-15 12:36:09 -080071 OatHeader* dl_oat_header = reinterpret_cast<OatHeader*>(dl_oatdata);
72 ASSERT_TRUE(dl_oat_header->IsValid());
73 dl_oatexec = dlsym(dl_oat_so, "oatexec");
74 ASSERT_TRUE(dl_oatexec != NULL);
75 ASSERT_LT(dl_oatdata, dl_oatexec);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080076
Brian Carlstrom31050c62013-12-15 12:36:09 -080077 dl_oatlastword = dlsym(dl_oat_so, "oatlastword");
78 ASSERT_TRUE(dl_oatlastword != NULL);
79 ASSERT_LT(dl_oatexec, dl_oatlastword);
80
81 ASSERT_EQ(0, dlclose(dl_oat_so));
82 }
83#endif
Brian Carlstrom700c8d32012-11-05 10:42:02 -080084
Brian Carlstrom7571e8b2013-08-12 17:04:14 -070085 UniquePtr<File> file(OS::OpenFileForReading(elf_filename.c_str()));
Brian Carlstrom700c8d32012-11-05 10:42:02 -080086 ASSERT_TRUE(file.get() != NULL);
87 {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070088 std::string error_msg;
89 UniquePtr<ElfFile> ef(ElfFile::Open(file.get(), false, false, &error_msg));
90 CHECK(ef.get() != nullptr) << error_msg;
Brian Carlstrom265091e2013-01-30 14:08:26 -080091 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatdata, "oatdata", false);
92 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatexec, "oatexec", false);
93 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatlastword, "oatlastword", false);
94 }
95 {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070096 std::string error_msg;
97 UniquePtr<ElfFile> ef(ElfFile::Open(file.get(), false, false, &error_msg));
98 CHECK(ef.get() != nullptr) << error_msg;
Brian Carlstrom265091e2013-01-30 14:08:26 -080099 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatdata, "oatdata", true);
100 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatexec, "oatexec", true);
101 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatlastword, "oatlastword", true);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800102 }
103 {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700104 std::string error_msg;
105 UniquePtr<ElfFile> ef(ElfFile::Open(file.get(), false, true, &error_msg));
106 CHECK(ef.get() != nullptr) << error_msg;
107 CHECK(ef->Load(false, &error_msg)) << error_msg;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800108 EXPECT_EQ(dl_oatdata, ef->FindDynamicSymbolAddress("oatdata"));
109 EXPECT_EQ(dl_oatexec, ef->FindDynamicSymbolAddress("oatexec"));
110 EXPECT_EQ(dl_oatlastword, ef->FindDynamicSymbolAddress("oatlastword"));
111 }
112}
113
114} // namespace art