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 | } |
| 55 | LOG(INFO) << "elf_filename=" << elf_filename; |
| 56 | |
| 57 | UnreserveImageSpace(); |
Brian Carlstrom | 31050c6 | 2013-12-15 12:36:09 -0800 | [diff] [blame] | 58 | void* dl_oatdata = NULL; |
| 59 | void* dl_oatexec = NULL; |
| 60 | void* dl_oatlastword = NULL; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 61 | |
Brian Carlstrom | 31050c6 | 2013-12-15 12:36:09 -0800 | [diff] [blame] | 62 | #if defined(ART_USE_PORTABLE_COMPILER) |
| 63 | { |
| 64 | // We only use dlopen for loading with portable. See OatFile::Open. |
| 65 | void* dl_oat_so = dlopen(elf_filename.c_str(), RTLD_NOW); |
| 66 | ASSERT_TRUE(dl_oat_so != NULL) << dlerror(); |
| 67 | dl_oatdata = dlsym(dl_oat_so, "oatdata"); |
| 68 | ASSERT_TRUE(dl_oatdata != NULL); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 69 | |
Brian Carlstrom | 31050c6 | 2013-12-15 12:36:09 -0800 | [diff] [blame] | 70 | OatHeader* dl_oat_header = reinterpret_cast<OatHeader*>(dl_oatdata); |
| 71 | ASSERT_TRUE(dl_oat_header->IsValid()); |
| 72 | dl_oatexec = dlsym(dl_oat_so, "oatexec"); |
| 73 | ASSERT_TRUE(dl_oatexec != NULL); |
| 74 | ASSERT_LT(dl_oatdata, dl_oatexec); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 75 | |
Brian Carlstrom | 31050c6 | 2013-12-15 12:36:09 -0800 | [diff] [blame] | 76 | dl_oatlastword = dlsym(dl_oat_so, "oatlastword"); |
| 77 | ASSERT_TRUE(dl_oatlastword != NULL); |
| 78 | ASSERT_LT(dl_oatexec, dl_oatlastword); |
| 79 | |
| 80 | ASSERT_EQ(0, dlclose(dl_oat_so)); |
| 81 | } |
| 82 | #endif |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 83 | |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 84 | UniquePtr<File> file(OS::OpenFileForReading(elf_filename.c_str())); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 85 | ASSERT_TRUE(file.get() != NULL); |
| 86 | { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 87 | std::string error_msg; |
| 88 | UniquePtr<ElfFile> ef(ElfFile::Open(file.get(), false, false, &error_msg)); |
| 89 | CHECK(ef.get() != nullptr) << error_msg; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 90 | EXPECT_ELF_FILE_ADDRESS(ef, dl_oatdata, "oatdata", false); |
| 91 | EXPECT_ELF_FILE_ADDRESS(ef, dl_oatexec, "oatexec", false); |
| 92 | EXPECT_ELF_FILE_ADDRESS(ef, dl_oatlastword, "oatlastword", false); |
| 93 | } |
| 94 | { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 95 | std::string error_msg; |
| 96 | UniquePtr<ElfFile> ef(ElfFile::Open(file.get(), false, false, &error_msg)); |
| 97 | CHECK(ef.get() != nullptr) << error_msg; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 98 | EXPECT_ELF_FILE_ADDRESS(ef, dl_oatdata, "oatdata", true); |
| 99 | EXPECT_ELF_FILE_ADDRESS(ef, dl_oatexec, "oatexec", true); |
| 100 | EXPECT_ELF_FILE_ADDRESS(ef, dl_oatlastword, "oatlastword", true); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 101 | } |
| 102 | { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 103 | std::string error_msg; |
| 104 | UniquePtr<ElfFile> ef(ElfFile::Open(file.get(), false, true, &error_msg)); |
| 105 | CHECK(ef.get() != nullptr) << error_msg; |
| 106 | CHECK(ef->Load(false, &error_msg)) << error_msg; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 107 | EXPECT_EQ(dl_oatdata, ef->FindDynamicSymbolAddress("oatdata")); |
| 108 | EXPECT_EQ(dl_oatexec, ef->FindDynamicSymbolAddress("oatexec")); |
| 109 | EXPECT_EQ(dl_oatlastword, ef->FindDynamicSymbolAddress("oatlastword")); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | } // namespace art |