// Usage: // // dlsym $RTLD_DEFAULT "errno" // static int get_symbol_address(WORD_LIST *list) { int opt; void *handle; void *symbol; char *resultname; char retval[256]; resultname = "DLRETVAL"; reset_internal_getopt(); // $ dlcall [-n name] while ((opt = internal_getopt(list, "n:")) != -1) { switch (opt) { case 'n': resultname = list_optarg; break; default: builtin_usage(); return EX_USAGE; } } // Skip past any options. if ((list = loptend) == NULL || list->next == NULL) { builtin_usage(); return EX_USAGE; } if (check_parse_ulong(list->word->word, (void *) &handle) == 0) { builtin_warning("handle %s %p is not well-formed", list->word->word, handle); return EX_USAGE; } if (!(symbol = dlsym(handle, list->next->word->word))) { builtin_warning("failed to resolve symbol %s, %s", list->next->word->word, dlerror()); return EXECUTION_FAILURE; } snprintf(retval, sizeof retval, "pointer:%p", symbol); fprintf(stderr, "%s\n", retval); bind_variable(resultname, retval, 0); return EXECUTION_SUCCESS; }
// Return the value of the single rtld flag specified. static uint32_t rtld_flags_decode(const char *flag) { intmax_t result; // Enumerate through all flags to find the one specified, this is // suboptimal but there are only 32 possible flags. for (uint32_t i = 0; i < 31; i++) { if (strcmp(rtld_flags_encode(1 << i), flag) == 0) { return 1 << i; } } // Perhaps it was specified numerically? if (check_parse_ulong(flag, &result)) { return result; } builtin_warning("invalid or unrecognised rtld flag ignored: %s", flag); return 0; }
static int close_dynamic_library(WORD_LIST *list) { void *handle; if (!list) { builtin_usage(); return EX_USAGE; } while (list) { if (!check_parse_ulong(list->word->word, (long *) &handle)) { builtin_warning("could not parse handle identifier %s", list->word->word); } else { if (dlclose(handle) != 0) { builtin_warning("dlclose set an error for %s, %s", list->word->word, dlerror()); } } list = list->next; } return 0; }
// Usage: // // dlcall $RTLD_DEFAULT "printf" "hello %s %u %c" $USER 123 int:10 // static int call_foreign_function(WORD_LIST *list) { unsigned nargs; int opt; ffi_cif cif; ffi_type **argtypes; ffi_type *rettype; void **values; void *handle; void *func; char *prefix; char *format; char *resultname; nargs = 0; argtypes = NULL; values = NULL; format = NULL; prefix = NULL; rettype = &ffi_type_void; resultname = "DLRETVAL"; reset_internal_getopt(); // $ dlcall [-a abi] [-r type] [-n name] while ((opt = internal_getopt(list, "a:r:n:")) != -1) { switch (opt) { case 'a': builtin_warning("FIXME: only abi %u is currently supported", FFI_DEFAULT_ABI); return 1; break; case 'r': if (decode_type_prefix(prefix = list_optarg, NULL, &rettype, NULL, &format) != true) { builtin_warning("failed to parse return type"); return 1; } break; case 'n': resultname = list_optarg; break; default: builtin_usage(); return 1; } } // Skip past any options. if ((list = loptend) == NULL || list->next == NULL) { builtin_usage(); return 1; } if (check_parse_ulong(list->word->word, (void *) &handle) == 0) { builtin_warning("handle %s %p is not well-formed", list->word->word, handle); return 1; } if (!(func = dlsym(handle, list->next->word->word))) { builtin_warning("failed to resolve symbol %s, %s", list->next->word->word, dlerror()); return 1; } // Skip to optional parameters list = list->next->next; while (list) { argtypes = realloc(argtypes, (nargs + 1) * sizeof(ffi_type *)); values = realloc(values, (nargs + 1) * sizeof(void *)); if (decode_primitive_type(list->word->word, &values[nargs], &argtypes[nargs]) != true) { builtin_error("failed to decode type from parameter %s", list->word->word); goto error; } nargs++; list = list->next; } if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, nargs, rettype, argtypes) == FFI_OK) { char *retval; void *rc = alloca(rettype->size); // Do the call. ffi_call(&cif, func, rc, values); // Print the result. if (format) { switch (rettype->size) { case 1: asprintf(&retval, format, *(uint8_t *) rc); break; case 2: asprintf(&retval, format, *(uint16_t *) rc); break; case 4: asprintf(&retval, format, *(uint32_t *) rc, *(float *) rc); break; case 8: asprintf(&retval, format, *(uint64_t *) rc, *(double *) rc); break; case 16: asprintf(&retval, format, *(long double *) rc); break; default: builtin_error("cannot handle size %lu", rettype->size); abort(); } fprintf(stderr, "%s\n", retval); bind_variable(resultname, retval, 0); free(retval); } } for (unsigned i = 0; i < nargs; i++) free(values[i]); free(values); free(argtypes); return 0; error: for (unsigned i = 0; i < nargs; i++) free(values[i]); free(values); free(argtypes); return 1; }
// Usage: // // dlcall "printf" "hello %s %u %c" $USER 123 int:10 // static int call_foreign_function(WORD_LIST *list) { unsigned nargs; unsigned i; int opt; ffi_cif cif; ffi_type **argtypes; ffi_type *rettype; void **values; void *handle; void *func; char *prefix; char *format; char *resultname; nargs = 0; argtypes = NULL; values = NULL; format = NULL; prefix = NULL; rettype = &ffi_type_void; resultname = "DLRETVAL"; handle = RTLD_DEFAULT; reset_internal_getopt(); // $ dlcall [-a abi] [-r type] [-n name] [-h handle] symbol args... while ((opt = internal_getopt(list, "h:a:r:n:")) != -1) { switch (opt) { case 'a': builtin_warning("FIXME: only abi %u is currently supported", FFI_DEFAULT_ABI); return 1; break; case 'r': if (decode_type_prefix(prefix = list_optarg, NULL, &rettype, NULL, &format) != true) { builtin_warning("failed to parse return type"); return 1; } break; case 'n': resultname = list_optarg; break; case 'h': if (check_parse_ulong(list_optarg, (void *) &handle) == 0) { builtin_warning("handle %s %p is not well-formed", list_optarg, handle); return EXECUTION_FAILURE; } break; default: builtin_usage(); return EX_USAGE; } } // Skip past any options. if ((list = loptend) == NULL) { builtin_usage(); return EX_USAGE; } if (!(func = dlsym(handle, list->word->word))) { builtin_warning("failed to resolve symbol %s, %s", list->word->word, dlerror()); return 1; } // Skip to optional parameters list = list->next; while (list) { argtypes = realloc(argtypes, (nargs + 1) * sizeof(ffi_type *)); values = realloc(values, (nargs + 1) * sizeof(void *)); if (decode_primitive_type(list->word->word, &values[nargs], &argtypes[nargs]) != true) { builtin_error("failed to decode type from parameter %s", list->word->word); goto error; } nargs++; list = list->next; } if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, nargs, rettype, argtypes) == FFI_OK) { char *retval; void *rc = alloca(rettype->size); // Do the call. ffi_call(&cif, func, rc, values); // Decode the result. if (format) { retval = encode_primitive_type(format, rettype, rc); // If this is an interactive shell, print the output. if (interactive_shell) { fprintf(stderr, "%s\n", retval); } // Save the result to the requested location. bind_variable(resultname, retval, 0); // Bash maintains it's own copy of this string, so we can throw it away. free(retval); } } for (i = 0; i < nargs; i++) free(values[i]); free(values); free(argtypes); return 0; error: for (i = 0; i < nargs; i++) free(values[i]); free(values); free(argtypes); return 1; }
// Usage: // // dlsym $RTLD_DEFAULT "errno" // static int get_symbol_address(WORD_LIST *list) { int opt; void *handle; void *symbol; char *resultname; char *format; char *retval; ffi_type *rettype; handle = RTLD_DEFAULT; resultname = "DLRETVAL"; rettype = NULL; reset_internal_getopt(); // $ dlsym [-n name] [-h handle] symbol while ((opt = internal_getopt(list, "d:h:n:")) != -1) { switch (opt) { case 'd': if (decode_type_prefix(list_optarg, NULL, &rettype, NULL, &format) != true) { builtin_warning("failed to parse dereference type"); return 1; } break; case 'n': resultname = list_optarg; break; case 'h': if (check_parse_ulong(list_optarg, (void *) &handle) == 0) { builtin_warning("handle %s %p is not well-formed", list_optarg, handle); return EXECUTION_FAILURE; } break; default: builtin_usage(); return EX_USAGE; } } // Skip past any options. if ((list = loptend) == NULL) { builtin_usage(); return EX_USAGE; } if (!(symbol = dlsym(handle, list->word->word))) { builtin_warning("failed to resolve symbol %s, %s", list->word->word, dlerror()); return EXECUTION_FAILURE; } if (rettype == NULL) { asprintf(&retval, "pointer:%p", symbol); } else { retval = encode_primitive_type(format, rettype, symbol); } if (interactive_shell) { fprintf(stderr, "%s\n", retval); } bind_variable(resultname, retval, 0); free(retval); return EXECUTION_SUCCESS; }