Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 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 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 17 | #include "elf_file.h" |
| 18 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 19 | #include "common_compiler_test.h" |
| 20 | #include "oat.h" |
| 21 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 22 | namespace art { |
| 23 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 24 | class ElfWriterTest : public CommonCompilerTest { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 25 | protected: |
| 26 | virtual void SetUp() { |
| 27 | ReserveImageSpace(); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 28 | CommonCompilerTest::SetUp(); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 29 | } |
| 30 | }; |
| 31 | |
Brian Carlstrom | 31050c6 | 2013-12-15 12:36:09 -0800 | [diff] [blame] | 32 | #define EXPECT_ELF_FILE_ADDRESS(ef, expected_value, symbol_name, build_map) \ |
| 33 | do { \ |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 34 | void* addr = reinterpret_cast<void*>(ef->FindSymbolAddress(SHT_DYNSYM, \ |
Brian Carlstrom | 31050c6 | 2013-12-15 12:36:09 -0800 | [diff] [blame] | 35 | 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 Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 45 | |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 46 | TEST_F(ElfWriterTest, dlsym) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 47 | 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 Carlstrom | 0e12bdc | 2014-05-14 17:44:28 -0700 | [diff] [blame^] | 55 | elf_filename = GetSystemImageFilename(elf_filename.c_str(), kRuntimeISA); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 56 | LOG(INFO) << "elf_filename=" << elf_filename; |
| 57 | |
| 58 | UnreserveImageSpace(); |
Brian Carlstrom | 31050c6 | 2013-12-15 12:36:09 -0800 | [diff] [blame] | 59 | void* dl_oatdata = NULL; |
| 60 | void* dl_oatexec = NULL; |
| 61 | void* dl_oatlastword = NULL; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 62 | |
Brian Carlstrom | 31050c6 | 2013-12-15 12:36:09 -0800 | [diff] [blame] | 63 | #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 Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 70 | |
Brian Carlstrom | 31050c6 | 2013-12-15 12:36:09 -0800 | [diff] [blame] | 71 | 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 Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 76 | |
Brian Carlstrom | 31050c6 | 2013-12-15 12:36:09 -0800 | [diff] [blame] | 77 | 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 Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 84 | |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 85 | UniquePtr<File> file(OS::OpenFileForReading(elf_filename.c_str())); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 86 | ASSERT_TRUE(file.get() != NULL); |
| 87 | { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 88 | std::string error_msg; |
| 89 | UniquePtr<ElfFile> ef(ElfFile::Open(file.get(), false, false, &error_msg)); |
| 90 | CHECK(ef.get() != nullptr) << error_msg; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 91 | 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 Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 96 | std::string error_msg; |
| 97 | UniquePtr<ElfFile> ef(ElfFile::Open(file.get(), false, false, &error_msg)); |
| 98 | CHECK(ef.get() != nullptr) << error_msg; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 99 | 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 Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 102 | } |
| 103 | { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 104 | 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 Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 108 | 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 |