xref: /aosp_15_r20/system/chre/platform/arm/nanoapp_loader.cc (revision 84e339476a462649f82315436d70fd732297a399)
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 "chre/platform/shared/nanoapp_loader.h"
18 
19 namespace chre {
20 
relocateTable(DynamicHeader * dyn,int tag)21 bool NanoappLoader::relocateTable(DynamicHeader *dyn, int tag) {
22   bool success = false;
23   if (dyn == nullptr) {
24     return false;
25   }
26 
27   switch (tag) {
28     case DT_REL: {
29       if (getDynEntry(dyn, tag) == 0) {
30         LOGE("ARM Elf binaries must have DT_REL dynamic entry");
31         break;
32       }
33 
34       ElfRel *reloc =
35           reinterpret_cast<ElfRel *>(mBinary + getDynEntry(dyn, DT_REL));
36       size_t relocSize = getDynEntry(dyn, DT_RELSZ);
37       size_t nRelocs = relocSize / sizeof(ElfRel);
38       LOGV("Relocation %zu entries in DT_REL table", nRelocs);
39 
40       bool resolvedAllSymbols = true;
41       size_t i;
42       for (i = 0; i < nRelocs; ++i) {
43         ElfRel *curr = &reloc[i];
44         int relocType = ELFW_R_TYPE(curr->r_info);
45         ElfAddr *addr = reinterpret_cast<ElfAddr *>(mMapping + curr->r_offset);
46 
47         switch (relocType) {
48           case R_ARM_RELATIVE:
49             LOGV("Resolving ARM_RELATIVE at offset %lx",
50                  static_cast<long unsigned int>(curr->r_offset));
51             // TODO(b/155512914): When we move to DRAM allocations, we need to
52             // check if the above address is in a Read-Only section of memory,
53             // and give it temporary write permission if that is the case.
54             *addr += reinterpret_cast<uintptr_t>(mMapping);
55             break;
56 
57           case R_ARM_ABS32: {
58             LOGV("Resolving ARM_ABS32 at offset %lx",
59                  static_cast<long unsigned int>(curr->r_offset));
60             size_t posInSymbolTable = ELFW_R_SYM(curr->r_info);
61             auto *dynamicSymbolTable =
62                 reinterpret_cast<ElfSym *>(mDynamicSymbolTablePtr);
63             ElfSym *sym = &dynamicSymbolTable[posInSymbolTable];
64             *addr = reinterpret_cast<uintptr_t>(mMapping + sym->st_value);
65             break;
66           }
67 
68           case R_ARM_GLOB_DAT: {
69             LOGV("Resolving type ARM_GLOB_DAT at offset %lx",
70                  static_cast<long unsigned int>(curr->r_offset));
71             size_t posInSymbolTable = ELFW_R_SYM(curr->r_info);
72             void *resolved = resolveData(posInSymbolTable);
73             if (resolved == nullptr) {
74               LOGV("Failed to resolve global symbol(%zu) at offset 0x%lx", i,
75                    static_cast<long unsigned int>(curr->r_offset));
76               resolvedAllSymbols = false;
77             }
78             // TODO(b/155512914): When we move to DRAM allocations, we need to
79             // check if the above address is in a Read-Only section of memory,
80             // and give it temporary write permission if that is the case.
81             *addr = reinterpret_cast<ElfAddr>(resolved);
82             break;
83           }
84 
85           case R_ARM_COPY:
86             LOGE("R_ARM_COPY is an invalid relocation for shared libraries");
87             break;
88           default:
89             LOGE("Invalid relocation type %u", relocType);
90             break;
91         }
92       }
93 
94       if (!resolvedAllSymbols) {
95         LOGE("Unable to resolve all symbols in the binary");
96       } else {
97         success = true;
98       }
99       break;
100     }
101     case DT_RELA:
102       if (getDynEntry(dyn, tag) != 0) {
103         LOGE("ARM Elf binaries with a DT_RELA dynamic entry are unsupported");
104       } else {
105         // Not required for ARM
106         success = true;
107       }
108       break;
109     default:
110       LOGE("Unsupported table tag %d", tag);
111   }
112 
113   return success;
114 }
115 
resolveGot()116 bool NanoappLoader::resolveGot() {
117   ElfAddr *addr;
118   ElfRel *reloc = reinterpret_cast<ElfRel *>(
119       mMapping + getDynEntry(getDynamicHeader(), DT_JMPREL));
120   size_t relocSize = getDynEntry(getDynamicHeader(), DT_PLTRELSZ);
121   size_t nRelocs = relocSize / sizeof(ElfRel);
122   LOGV("Resolving GOT with %zu relocations", nRelocs);
123 
124   bool success = true;
125 
126   for (size_t i = 0; i < nRelocs; ++i) {
127     ElfRel *curr = &reloc[i];
128     int relocType = ELFW_R_TYPE(curr->r_info);
129 
130     switch (relocType) {
131       case R_ARM_JUMP_SLOT: {
132         LOGV("Resolving ARM_JUMP_SLOT at offset %lx",
133              static_cast<long unsigned int>(curr->r_offset));
134         addr = reinterpret_cast<ElfAddr *>(mMapping + curr->r_offset);
135         size_t posInSymbolTable = ELFW_R_SYM(curr->r_info);
136         void *resolved = resolveData(posInSymbolTable);
137         if (resolved == nullptr) {
138           LOGE("Failed to resolve symbol(%zu) at offset 0x%x", i,
139                curr->r_offset);
140           success = false;
141         }
142         *addr = reinterpret_cast<ElfAddr>(resolved);
143         break;
144       }
145 
146       default:
147         LOGE("Unsupported relocation type: %u for symbol %s", relocType,
148              getDataName(getDynamicSymbol(ELFW_R_SYM(curr->r_info))));
149         success = false;
150     }
151   }
152   return success;
153 }
154 
155 }  // namespace chre
156