blob: e479322238792130ce21ea1b6959cfdd3f55c294 [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"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080020#include "common_compiler_test.h"
21#include "oat.h"
Ian Rogerse63db272014-07-15 15:36:11 -070022#include "utils.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080023
Brian Carlstrom700c8d32012-11-05 10:42:02 -080024namespace art {
25
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080026class ElfWriterTest : public CommonCompilerTest {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080027 protected:
28 virtual void SetUp() {
29 ReserveImageSpace();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080030 CommonCompilerTest::SetUp();
Brian Carlstrom700c8d32012-11-05 10:42:02 -080031 }
32};
33
Brian Carlstrom31050c62013-12-15 12:36:09 -080034#define EXPECT_ELF_FILE_ADDRESS(ef, expected_value, symbol_name, build_map) \
35 do { \
Nicolas Geoffray50cfe742014-02-19 13:27:42 +000036 void* addr = reinterpret_cast<void*>(ef->FindSymbolAddress(SHT_DYNSYM, \
Brian Carlstrom31050c62013-12-15 12:36:09 -080037 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)
Brian Carlstrom700c8d32012-11-05 10:42:02 -080047
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -070048TEST_F(ElfWriterTest, dlsym) {
Brian Carlstrom2afe4942014-05-19 10:25:33 -070049 std::string elf_location;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080050 if (IsHost()) {
51 const char* host_dir = getenv("ANDROID_HOST_OUT");
52 CHECK(host_dir != NULL);
Brian Carlstrom2afe4942014-05-19 10:25:33 -070053 elf_location = StringPrintf("%s/framework/core.oat", host_dir);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080054 } else {
Brian Carlstrom2afe4942014-05-19 10:25:33 -070055 elf_location = "/data/art-test/core.oat";
Brian Carlstrom700c8d32012-11-05 10:42:02 -080056 }
Brian Carlstrom2afe4942014-05-19 10:25:33 -070057 std::string elf_filename = GetSystemImageFilename(elf_location.c_str(), kRuntimeISA);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080058 LOG(INFO) << "elf_filename=" << elf_filename;
59
60 UnreserveImageSpace();
Brian Carlstrom31050c62013-12-15 12:36:09 -080061 void* dl_oatdata = NULL;
62 void* dl_oatexec = NULL;
63 void* dl_oatlastword = NULL;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080064
Brian Carlstrom31050c62013-12-15 12:36:09 -080065#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);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080072
Brian Carlstrom31050c62013-12-15 12:36:09 -080073 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);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080078
Brian Carlstrom31050c62013-12-15 12:36:09 -080079 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
Brian Carlstrom700c8d32012-11-05 10:42:02 -080086
Ian Rogers700a4022014-05-19 16:49:03 -070087 std::unique_ptr<File> file(OS::OpenFileForReading(elf_filename.c_str()));
Brian Carlstrom700c8d32012-11-05 10:42:02 -080088 ASSERT_TRUE(file.get() != NULL);
89 {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070090 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -070091 std::unique_ptr<ElfFile> ef(ElfFile::Open(file.get(), false, false, &error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -070092 CHECK(ef.get() != nullptr) << error_msg;
Brian Carlstrom265091e2013-01-30 14:08:26 -080093 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 {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070098 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -070099 std::unique_ptr<ElfFile> ef(ElfFile::Open(file.get(), false, false, &error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700100 CHECK(ef.get() != nullptr) << error_msg;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800101 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);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800104 }
105 {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700106 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -0700107 std::unique_ptr<ElfFile> ef(ElfFile::Open(file.get(), false, true, &error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700108 CHECK(ef.get() != nullptr) << error_msg;
109 CHECK(ef->Load(false, &error_msg)) << error_msg;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800110 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