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 { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 25 | protected: |
| 26 | virtual void SetUp() { |
| 27 | ReserveImageSpace(); |
| 28 | CommonTest::SetUp(); |
| 29 | } |
| 30 | }; |
| 31 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 32 | #define EXPECT_ELF_FILE_ADDRESS(ef, value, name, build_map) \ |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 33 | 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] | 34 | EXPECT_EQ(value, ef->FindDynamicSymbolAddress(name)); \ |
| 35 | |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 36 | TEST_F(ElfWriterTest, dlsym) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 37 | 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 Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 65 | UniquePtr<File> file(OS::OpenFileForReading(elf_filename.c_str())); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 66 | ASSERT_TRUE(file.get() != NULL); |
| 67 | { |
| 68 | UniquePtr<ElfFile> ef(ElfFile::Open(file.get(), false, false)); |
| 69 | CHECK(ef.get() != NULL); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 70 | EXPECT_ELF_FILE_ADDRESS(ef, dl_oatdata, "oatdata", false); |
| 71 | EXPECT_ELF_FILE_ADDRESS(ef, dl_oatexec, "oatexec", false); |
| 72 | EXPECT_ELF_FILE_ADDRESS(ef, dl_oatlastword, "oatlastword", false); |
| 73 | } |
| 74 | { |
| 75 | UniquePtr<ElfFile> ef(ElfFile::Open(file.get(), false, false)); |
| 76 | CHECK(ef.get() != NULL); |
| 77 | EXPECT_ELF_FILE_ADDRESS(ef, dl_oatdata, "oatdata", true); |
| 78 | EXPECT_ELF_FILE_ADDRESS(ef, dl_oatexec, "oatexec", true); |
| 79 | EXPECT_ELF_FILE_ADDRESS(ef, dl_oatlastword, "oatlastword", true); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 80 | } |
| 81 | { |
| 82 | UniquePtr<ElfFile> ef(ElfFile::Open(file.get(), false, true)); |
| 83 | CHECK(ef.get() != NULL); |
Brian Carlstrom | f1d3455 | 2013-07-12 20:22:23 -0700 | [diff] [blame] | 84 | ef->Load(false); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 85 | EXPECT_EQ(dl_oatdata, ef->FindDynamicSymbolAddress("oatdata")); |
| 86 | EXPECT_EQ(dl_oatexec, ef->FindDynamicSymbolAddress("oatexec")); |
| 87 | EXPECT_EQ(dl_oatlastword, ef->FindDynamicSymbolAddress("oatlastword")); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | } // namespace art |