blob: eca67a8a6e69f54c64e366db40a74a69690e897c [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 Carlstrom265091e2013-01-30 14:08:26 -080032#define EXPECT_ELF_FILE_ADDRESS(ef, value, name, build_map) \
Brian Carlstrom265091e2013-01-30 14:08:26 -080033 EXPECT_EQ(value, reinterpret_cast<void*>(ef->FindSymbolAddress(::llvm::ELF::SHT_DYNSYM, name, build_map))); \
Brian Carlstrom700c8d32012-11-05 10:42:02 -080034 EXPECT_EQ(value, ef->FindDynamicSymbolAddress(name)); \
35
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -070036TEST_F(ElfWriterTest, dlsym) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080037 std::string elf_filename;
38 if (IsHost()) {
39 const char* host_dir = getenv("ANDROID_HOST_OUT");
40 CHECK(host_dir != NULL);
41 elf_filename = StringPrintf("%s/framework/core.oat", host_dir);
42 } else {
43 elf_filename = "/data/art-test/core.oat";
44 }
45 LOG(INFO) << "elf_filename=" << elf_filename;
46
47 UnreserveImageSpace();
48 void* dl_oat_so = dlopen(elf_filename.c_str(), RTLD_NOW);
49 ASSERT_TRUE(dl_oat_so != NULL) << dlerror();
50 void* dl_oatdata = dlsym(dl_oat_so, "oatdata");
51 ASSERT_TRUE(dl_oatdata != NULL);
52
53 OatHeader* dl_oat_header = reinterpret_cast<OatHeader*>(dl_oatdata);
54 ASSERT_TRUE(dl_oat_header->IsValid());
55 void* dl_oatexec = dlsym(dl_oat_so, "oatexec");
56 ASSERT_TRUE(dl_oatexec != NULL);
57 ASSERT_LT(dl_oatdata, dl_oatexec);
58
59 void* dl_oatlastword = dlsym(dl_oat_so, "oatlastword");
60 ASSERT_TRUE(dl_oatlastword != NULL);
61 ASSERT_LT(dl_oatexec, dl_oatlastword);
62
63 ASSERT_EQ(0, dlclose(dl_oat_so));
64
Brian Carlstrom7571e8b2013-08-12 17:04:14 -070065 UniquePtr<File> file(OS::OpenFileForReading(elf_filename.c_str()));
Brian Carlstrom700c8d32012-11-05 10:42:02 -080066 ASSERT_TRUE(file.get() != NULL);
67 {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070068 std::string error_msg;
69 UniquePtr<ElfFile> ef(ElfFile::Open(file.get(), false, false, &error_msg));
70 CHECK(ef.get() != nullptr) << error_msg;
Brian Carlstrom265091e2013-01-30 14:08:26 -080071 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatdata, "oatdata", false);
72 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatexec, "oatexec", false);
73 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatlastword, "oatlastword", false);
74 }
75 {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070076 std::string error_msg;
77 UniquePtr<ElfFile> ef(ElfFile::Open(file.get(), false, false, &error_msg));
78 CHECK(ef.get() != nullptr) << error_msg;
Brian Carlstrom265091e2013-01-30 14:08:26 -080079 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatdata, "oatdata", true);
80 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatexec, "oatexec", true);
81 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatlastword, "oatlastword", true);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080082 }
83 {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070084 std::string error_msg;
85 UniquePtr<ElfFile> ef(ElfFile::Open(file.get(), false, true, &error_msg));
86 CHECK(ef.get() != nullptr) << error_msg;
87 CHECK(ef->Load(false, &error_msg)) << error_msg;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080088 EXPECT_EQ(dl_oatdata, ef->FindDynamicSymbolAddress("oatdata"));
89 EXPECT_EQ(dl_oatexec, ef->FindDynamicSymbolAddress("oatexec"));
90 EXPECT_EQ(dl_oatlastword, ef->FindDynamicSymbolAddress("oatlastword"));
91 }
92}
93
94} // namespace art