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 <sys/mman.h>  // For the PROT_NONE constant.
18 
19 #include "base/file_utils.h"
20 #include "base/mem_map.h"
21 #include "base/unix_file/fd_file.h"
22 #include "base/utils.h"
23 #include "common_compiler_driver_test.h"
24 #include "elf/elf_builder.h"
25 #include "elf_writer_quick.h"
26 #include "oat/elf_file.h"
27 #include "oat/elf_file_impl.h"
28 #include "oat/oat.h"
29 
30 namespace art {
31 namespace linker {
32 
33 class ElfWriterTest : public CommonCompilerDriverTest {
34  protected:
SetUp()35   void SetUp() override {
36     ReserveImageSpace();
37     CommonCompilerTest::SetUp();
38   }
39 };
40 
41 #define EXPECT_ELF_FILE_ADDRESS(ef, expected_value, symbol_name, build_map) \
42   do { \
43     void* addr = reinterpret_cast<void*>((ef)->FindSymbolAddress(SHT_DYNSYM, \
44                                                                  symbol_name, \
45                                                                  build_map)); \
46     EXPECT_NE(nullptr, addr); \
47     if ((expected_value) == nullptr) { \
48       (expected_value) = addr; \
49     }                        \
50     EXPECT_EQ(expected_value, addr); \
51     EXPECT_EQ(expected_value, (ef)->FindDynamicSymbolAddress(symbol_name)); \
52   } while (false)
53 
TEST_F(ElfWriterTest,dlsym)54 TEST_F(ElfWriterTest, dlsym) {
55   std::string elf_location = GetCoreOatLocation();
56   std::string elf_filename = GetSystemImageFilename(elf_location.c_str(), kRuntimeISA);
57   LOG(INFO) << "elf_filename=" << elf_filename;
58 
59   UnreserveImageSpace();
60   void* dl_oatdata = nullptr;
61   void* dl_oatexec = nullptr;
62   void* dl_oatlastword = nullptr;
63 
64   std::unique_ptr<File> file(OS::OpenFileForReading(elf_filename.c_str()));
65   ASSERT_TRUE(file.get() != nullptr) << elf_filename;
66   {
67     std::string error_msg;
68     std::unique_ptr<ElfFile> ef(ElfFile::Open(file.get(),
69                                               /*writable=*/ false,
70                                               /*program_header_only=*/ false,
71                                               /*low_4gb=*/false,
72                                               &error_msg));
73     CHECK(ef.get() != nullptr) << error_msg;
74     EXPECT_ELF_FILE_ADDRESS(ef, dl_oatdata, "oatdata", false);
75     EXPECT_ELF_FILE_ADDRESS(ef, dl_oatexec, "oatexec", false);
76     EXPECT_ELF_FILE_ADDRESS(ef, dl_oatlastword, "oatlastword", false);
77   }
78   {
79     std::string error_msg;
80     std::unique_ptr<ElfFile> ef(ElfFile::Open(file.get(),
81                                               /*writable=*/ false,
82                                               /*program_header_only=*/ false,
83                                               /*low_4gb=*/ false,
84                                               &error_msg));
85     CHECK(ef.get() != nullptr) << error_msg;
86     EXPECT_ELF_FILE_ADDRESS(ef, dl_oatdata, "oatdata", true);
87     EXPECT_ELF_FILE_ADDRESS(ef, dl_oatexec, "oatexec", true);
88     EXPECT_ELF_FILE_ADDRESS(ef, dl_oatlastword, "oatlastword", true);
89   }
90   {
91     std::string error_msg;
92     std::unique_ptr<ElfFile> ef(ElfFile::Open(file.get(),
93                                               /*writable=*/ false,
94                                               /*program_header_only=*/ true,
95                                               /*low_4gb=*/ false,
96                                               &error_msg));
97     CHECK(ef.get() != nullptr) << error_msg;
98     size_t size;
99     bool success = ef->GetLoadedSize(&size, &error_msg);
100     CHECK(success) << error_msg;
101     MemMap reservation = MemMap::MapAnonymous("ElfWriterTest#dlsym reservation",
102                                               RoundUp(size, MemMap::GetPageSize()),
103                                               PROT_NONE,
104                                               /*low_4gb=*/ true,
105                                               &error_msg);
106     CHECK(reservation.IsValid()) << error_msg;
107     uint8_t* base = reservation.Begin();
108     success =
109         ef->Load(file.get(), /*executable=*/ false, /*low_4gb=*/ false, &reservation, &error_msg);
110     CHECK(success) << error_msg;
111     CHECK(!reservation.IsValid());
112     EXPECT_EQ(reinterpret_cast<uintptr_t>(dl_oatdata) + reinterpret_cast<uintptr_t>(base),
113         reinterpret_cast<uintptr_t>(ef->FindDynamicSymbolAddress("oatdata")));
114     EXPECT_EQ(reinterpret_cast<uintptr_t>(dl_oatexec) + reinterpret_cast<uintptr_t>(base),
115         reinterpret_cast<uintptr_t>(ef->FindDynamicSymbolAddress("oatexec")));
116     EXPECT_EQ(reinterpret_cast<uintptr_t>(dl_oatlastword) + reinterpret_cast<uintptr_t>(base),
117         reinterpret_cast<uintptr_t>(ef->FindDynamicSymbolAddress("oatlastword")));
118   }
119 }
120 
TEST_F(ElfWriterTest,CheckBuildIdPresent)121 TEST_F(ElfWriterTest, CheckBuildIdPresent) {
122   std::string elf_location = GetCoreOatLocation();
123   std::string elf_filename = GetSystemImageFilename(elf_location.c_str(), kRuntimeISA);
124   LOG(INFO) << "elf_filename=" << elf_filename;
125 
126   std::unique_ptr<File> file(OS::OpenFileForReading(elf_filename.c_str()));
127   ASSERT_TRUE(file.get() != nullptr);
128   {
129     std::string error_msg;
130     std::unique_ptr<ElfFile> ef(ElfFile::Open(file.get(),
131                                               /*writable=*/ false,
132                                               /*program_header_only=*/ false,
133                                               /*low_4gb=*/ false,
134                                               &error_msg));
135     CHECK(ef.get() != nullptr) << error_msg;
136     EXPECT_TRUE(ef->HasSection(".note.gnu.build-id"));
137   }
138 }
139 
140 }  // namespace linker
141 }  // namespace art
142