1 /*
2  * Copyright (C) 2023 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 <stddef.h>
18 #include <stdint.h>
19 #include <string.h>
20 
21 #include <fstream>
22 #include <memory>
23 #include <optional>
24 #include <vector>
25 
26 #include "elf/relocation.h"
27 #include "libelf/elf.h"
28 
29 namespace {
30 // Read an entire ELF file into the memory
ReadFile(const std::string & file_name)31 std::vector<uint8_t> ReadFile(const std::string& file_name) {
32   std::ifstream elf_file(file_name, std::ios::binary | std::ios::ate);
33   std::streamsize file_size = elf_file.tellg();
34   elf_file.seekg(0, std::ios::beg);
35 
36   std::vector<uint8_t> ret(file_size);
37   elf_file.read(reinterpret_cast<char*>(ret.data()), file_size);
38   return ret;
39 }
40 }  // namespace
41 
42 #define ASSERT(cond)                                                                 \
43   {                                                                                  \
44     if (!(cond)) {                                                                   \
45       fprintf(stderr, "%s:%d. Assert failed \"(%s)\"\n", __FILE__, __LINE__, #cond); \
46       exit(1);                                                                       \
47     }                                                                                \
48   }
49 
50 // Simulate an ELF relocation processs using GBL relocation library. Fail if the ELF contains
51 // relocation types that we don't expect.
52 //
53 // Usage:
54 //   <executable> <ELF file> <Binary only version (ELF header stripped) of the ELF>
55 //
56 // The binary only version can be obtained by `llvm-objcopy -O binary <ELF> <output>`
57 //
58 // Return 0 if successful.
main(int argc,char * argv[])59 int main(int argc, char* argv[]) {
60   if (argc != 3) {
61     return 1;
62   }
63 
64   std::vector<uint8_t> elf_file = ReadFile(argv[1]);
65   std::vector<uint8_t> bin_file = ReadFile(argv[2]);
66 
67   Elf64_Ehdr elf_header;
68   ASSERT(elf_file.size() >= sizeof(elf_header));
69   memcpy(&elf_header, elf_file.data(), sizeof(elf_header));
70 
71   // Iterate all program headers and find out the address for dynamic section.
72   const uint8_t* program_hdr_start = elf_file.data() + elf_header.e_phoff;
73   std::optional<uint64_t> dynamic_section_offset = std::nullopt;
74   for (size_t i = 0; i < elf_header.e_phnum; i++) {
75     const uint8_t* header_start = program_hdr_start + i * sizeof(Elf64_Phdr);
76     Elf64_Phdr elf_program_header;
77     ASSERT(static_cast<size_t>(header_start + sizeof(elf_program_header) - elf_file.data()) <=
78            elf_file.size());
79     memcpy(&elf_program_header, header_start, sizeof(elf_program_header));
80     if (elf_program_header.p_type == PT_DYNAMIC) {
81       // Store the physical address
82       // For PIE ELF this is the relative offset to ELF file start.
83       dynamic_section_offset = elf_program_header.p_paddr;
84       break;
85     }
86   }
87 
88   // Dynamic section not found.
89   if (!dynamic_section_offset) {
90     return 1;
91   }
92 
93   ASSERT(bin_file.size() >= *dynamic_section_offset);
94   // Perform relocation as if we have loaded the program @ address bin_file.data().
95   return ApplyRelocation(reinterpret_cast<uintptr_t>(bin_file.data()),
96                          reinterpret_cast<uintptr_t>(bin_file.data() + *dynamic_section_offset))
97              ? 0
98              : 1;
99 }
100