Jim Ingham | 5a36912 | 2010-09-28 01:25:32 +0000 | [diff] [blame] | 1 | //===-- AppleObjCTrampolineHandler.cpp ----------------------------*- C++ -*-===// |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Jim Ingham | 5a36912 | 2010-09-28 01:25:32 +0000 | [diff] [blame] | 10 | #include "AppleObjCTrampolineHandler.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 11 | |
| 12 | // C Includes |
| 13 | // C++ Includes |
| 14 | // Other libraries and framework includes |
| 15 | // Project includes |
Jim Ingham | 5a36912 | 2010-09-28 01:25:32 +0000 | [diff] [blame] | 16 | #include "AppleThreadPlanStepThroughObjCTrampoline.h" |
| 17 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 18 | #include "lldb/Core/ConstString.h" |
| 19 | #include "lldb/Core/FileSpec.h" |
Jim Ingham | 5a36912 | 2010-09-28 01:25:32 +0000 | [diff] [blame] | 20 | #include "lldb/Core/Log.h" |
| 21 | #include "lldb/Core/Module.h" |
| 22 | #include "lldb/Core/Value.h" |
| 23 | #include "lldb/Expression/ClangFunction.h" |
| 24 | #include "lldb/Symbol/ClangASTContext.h" |
| 25 | #include "lldb/Target/ObjCLanguageRuntime.h" |
| 26 | #include "lldb/Target/Process.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 27 | #include "lldb/Target/RegisterContext.h" |
| 28 | #include "lldb/Target/Target.h" |
Jim Ingham | 5a36912 | 2010-09-28 01:25:32 +0000 | [diff] [blame] | 29 | #include "lldb/Target/Thread.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 30 | #include "lldb/Target/ExecutionContext.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 31 | #include "lldb/Target/ThreadPlanRunToAddress.h" |
| 32 | |
| 33 | using namespace lldb; |
| 34 | using namespace lldb_private; |
| 35 | |
Jim Ingham | 5a36912 | 2010-09-28 01:25:32 +0000 | [diff] [blame] | 36 | const AppleObjCTrampolineHandler::DispatchFunction |
| 37 | AppleObjCTrampolineHandler::g_dispatch_functions[] = |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 38 | { |
| 39 | // NAME STRET SUPER FIXUP TYPE |
Jim Ingham | 5a36912 | 2010-09-28 01:25:32 +0000 | [diff] [blame] | 40 | {"objc_msgSend", false, false, AppleObjCTrampolineHandler::DispatchFunction::eFixUpNone }, |
| 41 | {"objc_msgSend_fixup", false, false, AppleObjCTrampolineHandler::DispatchFunction::eFixUpToFix }, |
| 42 | {"objc_msgSend_fixedup", false, false, AppleObjCTrampolineHandler::DispatchFunction::eFixUpFixed }, |
| 43 | {"objc_msgSend_stret", true, false, AppleObjCTrampolineHandler::DispatchFunction::eFixUpNone }, |
| 44 | {"objc_msgSend_stret_fixup", true, false, AppleObjCTrampolineHandler::DispatchFunction::eFixUpToFix }, |
| 45 | {"objc_msgSend_stret_fixedup", true, false, AppleObjCTrampolineHandler::DispatchFunction::eFixUpFixed }, |
| 46 | {"objc_msgSend_fpret", false, false, AppleObjCTrampolineHandler::DispatchFunction::eFixUpNone }, |
| 47 | {"objc_msgSend_fpret_fixup", false, false, AppleObjCTrampolineHandler::DispatchFunction::eFixUpToFix }, |
| 48 | {"objc_msgSend_fpret_fixedup", false, false, AppleObjCTrampolineHandler::DispatchFunction::eFixUpFixed }, |
| 49 | {"objc_msgSend_fp2ret", false, false, AppleObjCTrampolineHandler::DispatchFunction::eFixUpNone }, |
| 50 | {"objc_msgSend_fp2ret_fixup", false, false, AppleObjCTrampolineHandler::DispatchFunction::eFixUpToFix }, |
| 51 | {"objc_msgSend_fp2ret_fixedup", false, false, AppleObjCTrampolineHandler::DispatchFunction::eFixUpFixed }, |
| 52 | {"objc_msgSendSuper", false, true, AppleObjCTrampolineHandler::DispatchFunction::eFixUpNone }, |
| 53 | {"objc_msgSendSuper_stret", true, true, AppleObjCTrampolineHandler::DispatchFunction::eFixUpNone }, |
| 54 | {"objc_msgSendSuper2", false, true, AppleObjCTrampolineHandler::DispatchFunction::eFixUpNone }, |
| 55 | {"objc_msgSendSuper2_fixup", false, true, AppleObjCTrampolineHandler::DispatchFunction::eFixUpToFix }, |
| 56 | {"objc_msgSendSuper2_fixedup", false, true, AppleObjCTrampolineHandler::DispatchFunction::eFixUpFixed }, |
| 57 | {"objc_msgSendSuper2_stret", true, true, AppleObjCTrampolineHandler::DispatchFunction::eFixUpNone }, |
| 58 | {"objc_msgSendSuper2_stret_fixup", true, true, AppleObjCTrampolineHandler::DispatchFunction::eFixUpToFix }, |
| 59 | {"objc_msgSendSuper2_stret_fixedup", true, true, AppleObjCTrampolineHandler::DispatchFunction::eFixUpFixed }, |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 60 | {NULL} |
| 61 | }; |
| 62 | |
| 63 | bool |
Jim Ingham | 5a36912 | 2010-09-28 01:25:32 +0000 | [diff] [blame] | 64 | AppleObjCTrampolineHandler::ModuleIsObjCLibrary (const ModuleSP &module_sp) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 65 | { |
| 66 | const FileSpec &module_file_spec = module_sp->GetFileSpec(); |
| 67 | static ConstString ObjCName ("libobjc.A.dylib"); |
| 68 | |
| 69 | if (module_file_spec) |
| 70 | { |
| 71 | if (module_file_spec.GetFilename() == ObjCName) |
| 72 | return true; |
| 73 | } |
| 74 | |
| 75 | return false; |
| 76 | } |
| 77 | |
Jim Ingham | 5a36912 | 2010-09-28 01:25:32 +0000 | [diff] [blame] | 78 | AppleObjCTrampolineHandler::AppleObjCTrampolineHandler (ProcessSP process_sp, ModuleSP objc_module) : |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 79 | m_process_sp (process_sp), |
| 80 | m_objc_module_sp (objc_module), |
| 81 | m_impl_fn_addr (LLDB_INVALID_ADDRESS), |
| 82 | m_impl_stret_fn_addr (LLDB_INVALID_ADDRESS) |
| 83 | { |
| 84 | // Look up the known resolution functions: |
| 85 | |
| 86 | ConstString get_impl_name("class_getMethodImplementation"); |
| 87 | ConstString get_impl_stret_name("class_getMethodImplementation_stret"); |
| 88 | |
Greg Clayton | f5e56de | 2010-09-14 23:36:40 +0000 | [diff] [blame] | 89 | Target *target = m_process_sp ? &m_process_sp->GetTarget() : NULL; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 90 | const Symbol *class_getMethodImplementation = m_objc_module_sp->FindFirstSymbolWithNameAndType (get_impl_name, eSymbolTypeCode); |
| 91 | const Symbol *class_getMethodImplementation_stret = m_objc_module_sp->FindFirstSymbolWithNameAndType (get_impl_stret_name, eSymbolTypeCode); |
| 92 | |
| 93 | if (class_getMethodImplementation) |
Greg Clayton | f5e56de | 2010-09-14 23:36:40 +0000 | [diff] [blame] | 94 | m_impl_fn_addr = class_getMethodImplementation->GetValue().GetLoadAddress(target); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 95 | if (class_getMethodImplementation_stret) |
Greg Clayton | f5e56de | 2010-09-14 23:36:40 +0000 | [diff] [blame] | 96 | m_impl_stret_fn_addr = class_getMethodImplementation_stret->GetValue().GetLoadAddress(target); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 97 | |
| 98 | // FIXME: Do some kind of logging here. |
| 99 | if (m_impl_fn_addr == LLDB_INVALID_ADDRESS || m_impl_stret_fn_addr == LLDB_INVALID_ADDRESS) |
| 100 | return; |
| 101 | |
| 102 | // Look up the addresses for the objc dispatch functions and cache them. For now I'm inspecting the symbol |
| 103 | // names dynamically to figure out how to dispatch to them. If it becomes more complicated than this we can |
| 104 | // turn the g_dispatch_functions char * array into a template table, and populate the DispatchFunction map |
| 105 | // from there. |
| 106 | |
| 107 | for (int i = 0; g_dispatch_functions[i].name != NULL; i++) |
| 108 | { |
| 109 | ConstString name_const_str(g_dispatch_functions[i].name); |
| 110 | const Symbol *msgSend_symbol = m_objc_module_sp->FindFirstSymbolWithNameAndType (name_const_str, eSymbolTypeCode); |
| 111 | if (msgSend_symbol) |
| 112 | { |
| 113 | // FixMe: Make g_dispatch_functions static table of DisptachFunctions, and have the map be address->index. |
| 114 | // Problem is we also need to lookup the dispatch function. For now we could have a side table of stret & non-stret |
| 115 | // dispatch functions. If that's as complex as it gets, we're fine. |
| 116 | |
Greg Clayton | f5e56de | 2010-09-14 23:36:40 +0000 | [diff] [blame] | 117 | lldb::addr_t sym_addr = msgSend_symbol->GetValue().GetLoadAddress(target); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 118 | |
| 119 | m_msgSend_map.insert(std::pair<lldb::addr_t, int>(sym_addr, i)); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | ThreadPlanSP |
Jim Ingham | 5a36912 | 2010-09-28 01:25:32 +0000 | [diff] [blame] | 125 | AppleObjCTrampolineHandler::GetStepThroughDispatchPlan (Thread &thread, bool stop_others) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 126 | { |
| 127 | ThreadPlanSP ret_plan_sp; |
| 128 | lldb::addr_t curr_pc = thread.GetRegisterContext()->GetPC(); |
| 129 | |
| 130 | MsgsendMap::iterator pos; |
| 131 | pos = m_msgSend_map.find (curr_pc); |
| 132 | if (pos != m_msgSend_map.end()) |
| 133 | { |
| 134 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP); |
| 135 | |
| 136 | const DispatchFunction *this_dispatch = &g_dispatch_functions[(*pos).second]; |
| 137 | |
| 138 | lldb::StackFrameSP thread_cur_frame = thread.GetStackFrameAtIndex(0); |
| 139 | |
| 140 | Process *process = thread.CalculateProcess(); |
| 141 | const ABI *abi = process->GetABI(); |
| 142 | if (abi == NULL) |
| 143 | return ret_plan_sp; |
| 144 | |
| 145 | Target *target = thread.CalculateTarget(); |
| 146 | |
| 147 | // FIXME: Since neither the value nor the Clang QualType know their ASTContext, |
| 148 | // we have to make sure the type we put in our value list comes from the same ASTContext |
| 149 | // the ABI will use to get the argument values. THis is the bottom-most frame's module. |
| 150 | |
| 151 | ClangASTContext *clang_ast_context = target->GetScratchClangASTContext(); |
| 152 | ValueList argument_values; |
| 153 | Value input_value; |
| 154 | void *clang_void_ptr_type = clang_ast_context->GetVoidPtrType(false); |
| 155 | input_value.SetValueType (Value::eValueTypeScalar); |
| 156 | input_value.SetContext (Value::eContextTypeOpaqueClangQualType, clang_void_ptr_type); |
| 157 | |
| 158 | int obj_index; |
| 159 | int sel_index; |
| 160 | |
| 161 | // If this is a struct return dispatch, then the first argument is the |
| 162 | // return struct pointer, and the object is the second, and the selector is the third. |
| 163 | // Otherwise the object is the first and the selector the second. |
| 164 | if (this_dispatch->stret_return) |
| 165 | { |
| 166 | obj_index = 1; |
| 167 | sel_index = 2; |
| 168 | argument_values.PushValue(input_value); |
| 169 | argument_values.PushValue(input_value); |
| 170 | argument_values.PushValue(input_value); |
| 171 | } |
| 172 | else |
| 173 | { |
| 174 | obj_index = 0; |
| 175 | sel_index = 1; |
| 176 | argument_values.PushValue(input_value); |
| 177 | argument_values.PushValue(input_value); |
| 178 | } |
| 179 | |
| 180 | |
| 181 | bool success = abi->GetArgumentValues (thread, argument_values); |
| 182 | if (!success) |
| 183 | return ret_plan_sp; |
| 184 | |
| 185 | // Okay, the first value here is the object, we actually want the class of that object. |
| 186 | // For now we're just going with the ISA. |
| 187 | // FIXME: This should really be the return value of [object class] to properly handle KVO interposition. |
| 188 | |
| 189 | Value isa_value(*(argument_values.GetValueAtIndex(obj_index))); |
| 190 | |
| 191 | // This is a little cheesy, but since object->isa is the first field, |
| 192 | // making the object value a load address value and resolving it will get |
| 193 | // the pointer sized data pointed to by that value... |
| 194 | ExecutionContext exec_ctx; |
Greg Clayton | 0603aa9 | 2010-10-04 01:05:56 +0000 | [diff] [blame^] | 195 | thread.CalculateExecutionContext (exec_ctx); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 196 | |
| 197 | isa_value.SetValueType(Value::eValueTypeLoadAddress); |
| 198 | isa_value.ResolveValue(&exec_ctx, clang_ast_context->getASTContext()); |
| 199 | |
| 200 | if (this_dispatch->fixedup == DispatchFunction::eFixUpFixed) |
| 201 | { |
| 202 | // For the FixedUp method the Selector is actually a pointer to a |
| 203 | // structure, the second field of which is the selector number. |
| 204 | Value *sel_value = argument_values.GetValueAtIndex(sel_index); |
| 205 | sel_value->GetScalar() += process->GetAddressByteSize(); |
| 206 | sel_value->SetValueType(Value::eValueTypeLoadAddress); |
| 207 | sel_value->ResolveValue(&exec_ctx, clang_ast_context->getASTContext()); |
| 208 | } |
| 209 | else if (this_dispatch->fixedup == DispatchFunction::eFixUpToFix) |
| 210 | { |
| 211 | // FIXME: If the method dispatch is not "fixed up" then the selector is actually a |
| 212 | // pointer to the string name of the selector. We need to look that up... |
| 213 | // For now I'm going to punt on that and just return no plan. |
| 214 | if (log) |
| 215 | log->Printf ("Punting on stepping into un-fixed-up method dispatch."); |
| 216 | return ret_plan_sp; |
| 217 | } |
| 218 | |
| 219 | // FIXME: If this is a dispatch to the super-class, we need to get the super-class from |
| 220 | // the class, and disaptch to that instead. |
| 221 | // But for now I just punt and return no plan. |
| 222 | if (this_dispatch->is_super) |
| 223 | { |
| 224 | if (log) |
| 225 | log->Printf ("Punting on stepping into super method dispatch."); |
| 226 | return ret_plan_sp; |
| 227 | } |
| 228 | |
| 229 | ValueList dispatch_values; |
| 230 | dispatch_values.PushValue (isa_value); |
| 231 | dispatch_values.PushValue(*(argument_values.GetValueAtIndex(sel_index))); |
| 232 | |
| 233 | if (log) |
| 234 | { |
| 235 | log->Printf("Resolving method call for class - 0x%llx and selector - 0x%llx", |
| 236 | dispatch_values.GetValueAtIndex(0)->GetScalar().ULongLong(), |
| 237 | dispatch_values.GetValueAtIndex(1)->GetScalar().ULongLong()); |
| 238 | } |
Jim Ingham | 5a36912 | 2010-09-28 01:25:32 +0000 | [diff] [blame] | 239 | ObjCLanguageRuntime *objc_runtime = m_process_sp->GetObjCLanguageRuntime (); |
| 240 | assert(objc_runtime != NULL); |
| 241 | lldb::addr_t impl_addr = objc_runtime->LookupInMethodCache (dispatch_values.GetValueAtIndex(0)->GetScalar().ULongLong(), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 242 | dispatch_values.GetValueAtIndex(1)->GetScalar().ULongLong()); |
| 243 | |
| 244 | if (impl_addr == LLDB_INVALID_ADDRESS) |
| 245 | { |
| 246 | |
| 247 | Address resolve_address(NULL, this_dispatch->stret_return ? m_impl_stret_fn_addr : m_impl_fn_addr); |
| 248 | |
| 249 | StreamString errors; |
| 250 | { |
| 251 | // Scope for mutex locker: |
Greg Clayton | b132097 | 2010-07-14 00:18:15 +0000 | [diff] [blame] | 252 | Mutex::Locker locker(m_impl_function_mutex); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 253 | if (!m_impl_function.get()) |
| 254 | { |
| 255 | m_impl_function.reset(new ClangFunction(process->GetTargetTriple().GetCString(), |
| 256 | clang_ast_context, |
| 257 | clang_void_ptr_type, |
| 258 | resolve_address, |
| 259 | dispatch_values)); |
| 260 | |
| 261 | unsigned num_errors = m_impl_function->CompileFunction(errors); |
| 262 | if (num_errors) |
| 263 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 264 | if (log) |
| 265 | log->Printf ("Error compiling function: \"%s\".", errors.GetData()); |
| 266 | return ret_plan_sp; |
| 267 | } |
| 268 | |
| 269 | errors.Clear(); |
| 270 | if (!m_impl_function->WriteFunctionWrapper(exec_ctx, errors)) |
| 271 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 272 | if (log) |
| 273 | log->Printf ("Error Inserting function: \"%s\".", errors.GetData()); |
| 274 | return ret_plan_sp; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | } |
| 279 | |
| 280 | errors.Clear(); |
| 281 | |
| 282 | // Now write down the argument values for this call. |
| 283 | lldb::addr_t args_addr = LLDB_INVALID_ADDRESS; |
| 284 | if (!m_impl_function->WriteFunctionArguments (exec_ctx, args_addr, resolve_address, dispatch_values, errors)) |
| 285 | return ret_plan_sp; |
| 286 | |
Jim Ingham | 5a36912 | 2010-09-28 01:25:32 +0000 | [diff] [blame] | 287 | ret_plan_sp.reset (new AppleThreadPlanStepThroughObjCTrampoline (thread, this, args_addr, |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 288 | argument_values.GetValueAtIndex(0)->GetScalar().ULongLong(), |
| 289 | dispatch_values.GetValueAtIndex(0)->GetScalar().ULongLong(), |
| 290 | dispatch_values.GetValueAtIndex(1)->GetScalar().ULongLong(), |
| 291 | stop_others)); |
| 292 | } |
| 293 | else |
| 294 | { |
| 295 | if (log) |
| 296 | log->Printf ("Found implementation address in cache: 0x%llx", impl_addr); |
| 297 | |
| 298 | ret_plan_sp.reset (new ThreadPlanRunToAddress (thread, impl_addr, stop_others)); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | return ret_plan_sp; |
| 303 | } |
| 304 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 305 | ClangFunction * |
Jim Ingham | 5a36912 | 2010-09-28 01:25:32 +0000 | [diff] [blame] | 306 | AppleObjCTrampolineHandler::GetLookupImplementationWrapperFunction () |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 307 | { |
| 308 | return m_impl_function.get(); |
| 309 | } |