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 #ifndef BERBERIS_LOADER_PROXY_LIBRARY_BUILDER_H_ 18 #define BERBERIS_LOADER_PROXY_LIBRARY_BUILDER_H_ 19 20 #include "berberis/guest_state/guest_addr.h" 21 #include "berberis/runtime_primitives/host_function_wrapper_impl.h" // TrampolineFunc 22 23 namespace berberis { 24 25 struct ThreadState; 26 27 struct KnownTrampoline { 28 const char* name; 29 TrampolineFunc marshal_and_call; 30 void* thunk; 31 }; 32 33 struct KnownVariable { 34 const char* name; 35 size_t size; 36 }; 37 38 // TODO(eaeltsin): these are stubs used by known trampolines, consider killing! 39 void DoBadThunk(); 40 void DoBadTrampoline(HostCode callee, ThreadState* state); 41 42 class ProxyLibraryBuilder { 43 public: 44 ProxyLibraryBuilder() = default; 45 46 void Build(const char* library_name, 47 size_t size_translation, 48 const KnownTrampoline* translations, 49 size_t size_data_symbols, 50 const KnownVariable* variables); 51 52 void InterceptSymbol(GuestAddr guest_addr, const char* name); 53 54 private: 55 const char* library_name_ = nullptr; 56 size_t num_functions_ = 0; 57 const KnownTrampoline* functions_ = nullptr; 58 size_t num_variables_ = 0; 59 const KnownVariable* variables_ = nullptr; 60 void* handle_ = nullptr; 61 }; 62 63 // Assume kKnownTrampolines and kKnownVariables defined. 64 #define DEFINE_INIT_PROXY_LIBRARY(soname) \ 65 extern "C" void InitProxyLibrary(ProxyLibraryBuilder* builder) { \ 66 builder->Build(soname, \ 67 sizeof(kKnownTrampolines) / sizeof(kKnownTrampolines[0]), \ 68 kKnownTrampolines, \ 69 sizeof(kKnownVariables) / sizeof(kKnownVariables[0]), \ 70 kKnownVariables); \ 71 } 72 73 } // namespace berberis 74 75 #endif // BERBERIS_LOADER_PROXY_LIBRARY_BUILDER_H_ 76