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 | |
| 33 | #define EXPECT_ELF_FILE_ADDRESS(ef, value, name) \ |
| 34 | EXPECT_EQ(value, reinterpret_cast<void*>(ef->FindSymbolAddress(llvm::ELF::SHT_SYMTAB, name))); \ |
| 35 | EXPECT_EQ(value, reinterpret_cast<void*>(ef->FindSymbolAddress(llvm::ELF::SHT_DYNSYM, name))); \ |
| 36 | EXPECT_EQ(value, ef->FindDynamicSymbolAddress(name)); \ |
| 37 | |
| 38 | TEST_F(ElfWriterTest, dlsym) { |
| 39 | std::string elf_filename; |
| 40 | if (IsHost()) { |
| 41 | const char* host_dir = getenv("ANDROID_HOST_OUT"); |
| 42 | CHECK(host_dir != NULL); |
| 43 | elf_filename = StringPrintf("%s/framework/core.oat", host_dir); |
| 44 | } else { |
| 45 | elf_filename = "/data/art-test/core.oat"; |
| 46 | } |
| 47 | LOG(INFO) << "elf_filename=" << elf_filename; |
| 48 | |
| 49 | UnreserveImageSpace(); |
| 50 | void* dl_oat_so = dlopen(elf_filename.c_str(), RTLD_NOW); |
| 51 | ASSERT_TRUE(dl_oat_so != NULL) << dlerror(); |
| 52 | void* dl_oatdata = dlsym(dl_oat_so, "oatdata"); |
| 53 | ASSERT_TRUE(dl_oatdata != NULL); |
| 54 | |
| 55 | OatHeader* dl_oat_header = reinterpret_cast<OatHeader*>(dl_oatdata); |
| 56 | ASSERT_TRUE(dl_oat_header->IsValid()); |
| 57 | void* dl_oatexec = dlsym(dl_oat_so, "oatexec"); |
| 58 | ASSERT_TRUE(dl_oatexec != NULL); |
| 59 | ASSERT_LT(dl_oatdata, dl_oatexec); |
| 60 | |
| 61 | void* dl_oatlastword = dlsym(dl_oat_so, "oatlastword"); |
| 62 | ASSERT_TRUE(dl_oatlastword != NULL); |
| 63 | ASSERT_LT(dl_oatexec, dl_oatlastword); |
| 64 | |
| 65 | ASSERT_EQ(0, dlclose(dl_oat_so)); |
| 66 | |
| 67 | UniquePtr<File> file(OS::OpenFile(elf_filename.c_str(), false)); |
| 68 | ASSERT_TRUE(file.get() != NULL); |
| 69 | { |
| 70 | UniquePtr<ElfFile> ef(ElfFile::Open(file.get(), false, false)); |
| 71 | CHECK(ef.get() != NULL); |
| 72 | EXPECT_ELF_FILE_ADDRESS(ef, dl_oatdata, "oatdata"); |
| 73 | EXPECT_ELF_FILE_ADDRESS(ef, dl_oatexec, "oatexec"); |
| 74 | EXPECT_ELF_FILE_ADDRESS(ef, dl_oatlastword, "oatlastword"); |
| 75 | } |
| 76 | { |
| 77 | UniquePtr<ElfFile> ef(ElfFile::Open(file.get(), false, true)); |
| 78 | CHECK(ef.get() != NULL); |
| 79 | ef->Load(); |
| 80 | EXPECT_EQ(dl_oatdata, ef->FindDynamicSymbolAddress("oatdata")); |
| 81 | EXPECT_EQ(dl_oatexec, ef->FindDynamicSymbolAddress("oatexec")); |
| 82 | EXPECT_EQ(dl_oatlastword, ef->FindDynamicSymbolAddress("oatlastword")); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | } // namespace art |