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 | |
| 17 | #include "common_test.h" |
| 18 | |
| 19 | #include "oat.h" |
| 20 | #include "elf_file.h" |
| 21 | |
| 22 | namespace art { |
| 23 | |
| 24 | class ElfWriterTest : public CommonTest { |
| 25 | |
| 26 | protected: |
| 27 | virtual void SetUp() { |
| 28 | ReserveImageSpace(); |
| 29 | CommonTest::SetUp(); |
| 30 | } |
| 31 | }; |
| 32 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame^] | 33 | #define EXPECT_ELF_FILE_ADDRESS(ef, value, name, build_map) \ |
| 34 | EXPECT_EQ(value, reinterpret_cast<void*>(ef->FindSymbolAddress(::llvm::ELF::SHT_SYMTAB, name, build_map))); \ |
| 35 | EXPECT_EQ(value, reinterpret_cast<void*>(ef->FindSymbolAddress(::llvm::ELF::SHT_DYNSYM, name, build_map))); \ |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 36 | EXPECT_EQ(value, ef->FindDynamicSymbolAddress(name)); \ |
| 37 | |
Jeff Hao | f9e609f | 2013-02-15 16:36:29 -0800 | [diff] [blame] | 38 | /* |
| 39 | * TODO: Reenable dlopen when it works again on MIPS. It may have broken from this change: |
| 40 | * commit 818d98eb563ad5d7293b8b5c40f3dabf745e611f |
| 41 | * Author: Brian Carlstrom <bdc@google.com> |
| 42 | * Date: Sun Feb 10 21:38:12 2013 -0800 |
| 43 | * |
| 44 | * Fix MIPS to use standard kPageSize=0x1000 section alignment for ELF sections |
| 45 | * |
| 46 | * Change-Id: I905f0c5f75921a65bd7426a54d6258c780d85d0e |
| 47 | */ |
| 48 | TEST_F(ElfWriterTest, DISABLED_dlsym) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 49 | std::string elf_filename; |
| 50 | if (IsHost()) { |
| 51 | const char* host_dir = getenv("ANDROID_HOST_OUT"); |
| 52 | CHECK(host_dir != NULL); |
| 53 | elf_filename = StringPrintf("%s/framework/core.oat", host_dir); |
| 54 | } else { |
| 55 | elf_filename = "/data/art-test/core.oat"; |
| 56 | } |
| 57 | LOG(INFO) << "elf_filename=" << elf_filename; |
| 58 | |
| 59 | UnreserveImageSpace(); |
| 60 | void* dl_oat_so = dlopen(elf_filename.c_str(), RTLD_NOW); |
| 61 | ASSERT_TRUE(dl_oat_so != NULL) << dlerror(); |
| 62 | void* dl_oatdata = dlsym(dl_oat_so, "oatdata"); |
| 63 | ASSERT_TRUE(dl_oatdata != NULL); |
| 64 | |
| 65 | OatHeader* dl_oat_header = reinterpret_cast<OatHeader*>(dl_oatdata); |
| 66 | ASSERT_TRUE(dl_oat_header->IsValid()); |
| 67 | void* dl_oatexec = dlsym(dl_oat_so, "oatexec"); |
| 68 | ASSERT_TRUE(dl_oatexec != NULL); |
| 69 | ASSERT_LT(dl_oatdata, dl_oatexec); |
| 70 | |
| 71 | void* dl_oatlastword = dlsym(dl_oat_so, "oatlastword"); |
| 72 | ASSERT_TRUE(dl_oatlastword != NULL); |
| 73 | ASSERT_LT(dl_oatexec, dl_oatlastword); |
| 74 | |
| 75 | ASSERT_EQ(0, dlclose(dl_oat_so)); |
| 76 | |
| 77 | UniquePtr<File> file(OS::OpenFile(elf_filename.c_str(), false)); |
| 78 | ASSERT_TRUE(file.get() != NULL); |
| 79 | { |
| 80 | UniquePtr<ElfFile> ef(ElfFile::Open(file.get(), false, false)); |
| 81 | CHECK(ef.get() != NULL); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame^] | 82 | EXPECT_ELF_FILE_ADDRESS(ef, dl_oatdata, "oatdata", false); |
| 83 | EXPECT_ELF_FILE_ADDRESS(ef, dl_oatexec, "oatexec", false); |
| 84 | EXPECT_ELF_FILE_ADDRESS(ef, dl_oatlastword, "oatlastword", false); |
| 85 | } |
| 86 | { |
| 87 | UniquePtr<ElfFile> ef(ElfFile::Open(file.get(), false, false)); |
| 88 | CHECK(ef.get() != NULL); |
| 89 | EXPECT_ELF_FILE_ADDRESS(ef, dl_oatdata, "oatdata", true); |
| 90 | EXPECT_ELF_FILE_ADDRESS(ef, dl_oatexec, "oatexec", true); |
| 91 | EXPECT_ELF_FILE_ADDRESS(ef, dl_oatlastword, "oatlastword", true); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 92 | } |
| 93 | { |
| 94 | UniquePtr<ElfFile> ef(ElfFile::Open(file.get(), false, true)); |
| 95 | CHECK(ef.get() != NULL); |
| 96 | ef->Load(); |
| 97 | EXPECT_EQ(dl_oatdata, ef->FindDynamicSymbolAddress("oatdata")); |
| 98 | EXPECT_EQ(dl_oatexec, ef->FindDynamicSymbolAddress("oatexec")); |
| 99 | EXPECT_EQ(dl_oatlastword, ef->FindDynamicSymbolAddress("oatlastword")); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | } // namespace art |