blob: 7fabc30b671ba17d1cd324ad64364965cdaba9b4 [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"
Ian Rogersd4c4d952014-10-16 20:31:53 -070020#include "base/unix_file/fd_file.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080021#include "common_compiler_test.h"
22#include "oat.h"
Ian Rogerse63db272014-07-15 15:36:11 -070023#include "utils.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080024
Brian Carlstrom700c8d32012-11-05 10:42:02 -080025namespace art {
26
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080027class ElfWriterTest : public CommonCompilerTest {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080028 protected:
29 virtual void SetUp() {
30 ReserveImageSpace();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080031 CommonCompilerTest::SetUp();
Brian Carlstrom700c8d32012-11-05 10:42:02 -080032 }
33};
34
Brian Carlstrom31050c62013-12-15 12:36:09 -080035#define EXPECT_ELF_FILE_ADDRESS(ef, expected_value, symbol_name, build_map) \
36 do { \
Nicolas Geoffray50cfe742014-02-19 13:27:42 +000037 void* addr = reinterpret_cast<void*>(ef->FindSymbolAddress(SHT_DYNSYM, \
Brian Carlstrom31050c62013-12-15 12:36:09 -080038 symbol_name, \
39 build_map)); \
40 EXPECT_NE(nullptr, addr); \
41 EXPECT_LT(static_cast<uintptr_t>(ART_BASE_ADDRESS), reinterpret_cast<uintptr_t>(addr)); \
42 if (expected_value == nullptr) { \
43 expected_value = addr; \
44 } \
45 EXPECT_EQ(expected_value, addr); \
46 EXPECT_EQ(expected_value, ef->FindDynamicSymbolAddress(symbol_name)); \
47 } while (false)
Brian Carlstrom700c8d32012-11-05 10:42:02 -080048
Nicolas Geoffray72c25a92014-12-04 16:44:58 +000049#if defined(ART_USE_OPTIMIZING_COMPILER)
50TEST_F(ElfWriterTest, DISABLED_dlsym) {
51#else
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -070052TEST_F(ElfWriterTest, dlsym) {
Nicolas Geoffray72c25a92014-12-04 16:44:58 +000053#endif
Brian Carlstrom2afe4942014-05-19 10:25:33 -070054 std::string elf_location;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080055 if (IsHost()) {
56 const char* host_dir = getenv("ANDROID_HOST_OUT");
57 CHECK(host_dir != NULL);
Brian Carlstrom2afe4942014-05-19 10:25:33 -070058 elf_location = StringPrintf("%s/framework/core.oat", host_dir);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080059 } else {
Brian Carlstrom2afe4942014-05-19 10:25:33 -070060 elf_location = "/data/art-test/core.oat";
Brian Carlstrom700c8d32012-11-05 10:42:02 -080061 }
Brian Carlstrom2afe4942014-05-19 10:25:33 -070062 std::string elf_filename = GetSystemImageFilename(elf_location.c_str(), kRuntimeISA);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080063 LOG(INFO) << "elf_filename=" << elf_filename;
64
65 UnreserveImageSpace();
Brian Carlstrom31050c62013-12-15 12:36:09 -080066 void* dl_oatdata = NULL;
67 void* dl_oatexec = NULL;
68 void* dl_oatlastword = NULL;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080069
Ian Rogers700a4022014-05-19 16:49:03 -070070 std::unique_ptr<File> file(OS::OpenFileForReading(elf_filename.c_str()));
Brian Carlstrom700c8d32012-11-05 10:42:02 -080071 ASSERT_TRUE(file.get() != NULL);
72 {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070073 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -070074 std::unique_ptr<ElfFile> ef(ElfFile::Open(file.get(), false, false, &error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -070075 CHECK(ef.get() != nullptr) << error_msg;
Brian Carlstrom265091e2013-01-30 14:08:26 -080076 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatdata, "oatdata", false);
77 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatexec, "oatexec", false);
78 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatlastword, "oatlastword", false);
79 }
80 {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070081 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -070082 std::unique_ptr<ElfFile> ef(ElfFile::Open(file.get(), false, false, &error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -070083 CHECK(ef.get() != nullptr) << error_msg;
Brian Carlstrom265091e2013-01-30 14:08:26 -080084 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatdata, "oatdata", true);
85 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatexec, "oatexec", true);
86 EXPECT_ELF_FILE_ADDRESS(ef, dl_oatlastword, "oatlastword", true);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080087 }
88 {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070089 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -070090 std::unique_ptr<ElfFile> ef(ElfFile::Open(file.get(), false, true, &error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -070091 CHECK(ef.get() != nullptr) << error_msg;
92 CHECK(ef->Load(false, &error_msg)) << error_msg;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080093 EXPECT_EQ(dl_oatdata, ef->FindDynamicSymbolAddress("oatdata"));
94 EXPECT_EQ(dl_oatexec, ef->FindDynamicSymbolAddress("oatexec"));
95 EXPECT_EQ(dl_oatlastword, ef->FindDynamicSymbolAddress("oatlastword"));
96 }
97}
98
99} // namespace art