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 <elf.h>
18 #include <stdio.h>
19 
20 #include <iostream>
21 #include <string>
22 
23 #include "./elf.h"
24 
25 namespace shell_as {
26 
27 namespace {
28 // The base address of a PIE binary when loaded with ASLR disabled.
29 #if defined(__arm__) || defined(__aarch64__)
30 constexpr uint64_t k32BitImageBase = 0xAAAAA000;
31 constexpr uint64_t k64BitImageBase = 0x5555555000;
32 #else
33 constexpr uint64_t k32BitImageBase = 0x56555000;
34 constexpr uint64_t k64BitImageBase = 0x555555554000;
35 #endif
36 }  // namespace
37 
GetElfEntryPoint(const pid_t process_id,uint64_t * entry_address,bool * is_arm_mode)38 bool GetElfEntryPoint(const pid_t process_id, uint64_t* entry_address,
39                       bool* is_arm_mode) {
40   uint8_t elf_header_buffer[sizeof(Elf64_Ehdr)];
41   std::string exe_path = "/proc/" + std::to_string(process_id) + "/exe";
42   FILE* exe_file = fopen(exe_path.c_str(), "rb");
43   if (exe_file == nullptr) {
44     std::cerr << "Unable to open executable of process " << process_id
45               << std::endl;
46     return false;
47   }
48 
49   int read_size =
50       fread(elf_header_buffer, sizeof(elf_header_buffer), 1, exe_file);
51   fclose(exe_file);
52   if (read_size <= 0) {
53     std::cerr << "Unable to read executable of process " << process_id
54               << std::endl;
55     return false;
56   }
57 
58   const Elf32_Ehdr* file_header_32 = (Elf32_Ehdr*)elf_header_buffer;
59   const Elf64_Ehdr* file_header_64 = (Elf64_Ehdr*)elf_header_buffer;
60   // The first handful of bytes of a header do not depend on whether the file is
61   // 32bit vs 64bit.
62   const bool is_pie_binary = file_header_32->e_type == ET_DYN;
63 
64   if (file_header_32->e_ident[EI_CLASS] == ELFCLASS32) {
65     *entry_address =
66         file_header_32->e_entry + (is_pie_binary ? k32BitImageBase : 0);
67   } else if (file_header_32->e_ident[EI_CLASS] == ELFCLASS64) {
68     *entry_address =
69         file_header_64->e_entry + (is_pie_binary ? k64BitImageBase : 0);
70   } else {
71     return false;
72   }
73 
74   *is_arm_mode = false;
75 #if defined(__arm__)
76   if ((*entry_address & 1) == 0) {
77     *is_arm_mode = true;
78   }
79   // The entry address for ARM Elf binaries is branched to using a BX
80   // instruction. The low bit of these instructions indicates the instruction
81   // set of the code that is being jumped to. A low bit of 1 indicates thumb
82   // mode while a low bit of 0 indicates ARM mode.
83   *entry_address &= ~1;
84 #endif
85 
86   return true;
87 }
88 
89 }  // namespace shell_as
90