+static void
+dwarf_require_integral (struct type *type)
+{
+ if (TYPE_CODE (type) != TYPE_CODE_INT
+ && TYPE_CODE (type) != TYPE_CODE_CHAR
+ && TYPE_CODE (type) != TYPE_CODE_BOOL)
+ error (_("integral type expected in DWARF expression"));
+}
+
+/* Return the unsigned form of TYPE. TYPE is necessarily an integral
+ type. */
+
+static struct type *
+get_unsigned_type (struct gdbarch *gdbarch, struct type *type)
+{
+ switch (TYPE_LENGTH (type))
+ {
+ case 1:
+ return builtin_type (gdbarch)->builtin_uint8;
+ case 2:
+ return builtin_type (gdbarch)->builtin_uint16;
+ case 4:
+ return builtin_type (gdbarch)->builtin_uint32;
+ case 8:
+ return builtin_type (gdbarch)->builtin_uint64;
+ default:
+ error (_("no unsigned variant found for type, while evaluating "
+ "DWARF expression"));
+ }
+}
+
+/* Return the signed form of TYPE. TYPE is necessarily an integral
+ type. */
+
+static struct type *
+get_signed_type (struct gdbarch *gdbarch, struct type *type)
+{
+ switch (TYPE_LENGTH (type))
+ {
+ case 1:
+ return builtin_type (gdbarch)->builtin_int8;
+ case 2:
+ return builtin_type (gdbarch)->builtin_int16;
+ case 4:
+ return builtin_type (gdbarch)->builtin_int32;
+ case 8:
+ return builtin_type (gdbarch)->builtin_int64;
+ default:
+ error (_("no signed variant found for type, while evaluating "
+ "DWARF expression"));
+ }
+}
+
+/* Retrieve the N'th item on CTX's stack, converted to an address. */
+
+CORE_ADDR
+dwarf_expr_fetch_address (struct dwarf_expr_context *ctx, int n)
+{
+ struct value *result_val = dwarf_expr_fetch (ctx, n);
+ enum bfd_endian byte_order = gdbarch_byte_order (ctx->gdbarch);
+ ULONGEST result;
+
+ dwarf_require_integral (value_type (result_val));
+ result = extract_unsigned_integer (value_contents (result_val),
+ TYPE_LENGTH (value_type (result_val)),
+ byte_order);
+
+ /* For most architectures, calling extract_unsigned_integer() alone
+ is sufficient for extracting an address. However, some
+ architectures (e.g. MIPS) use signed addresses and using
+ extract_unsigned_integer() will not produce a correct
+ result. Make sure we invoke gdbarch_integer_to_address()
+ for those architectures which require it. */
+ if (gdbarch_integer_to_address_p (ctx->gdbarch))
+ {
+ gdb_byte *buf = alloca (ctx->addr_size);
+ struct type *int_type = get_unsigned_type (ctx->gdbarch,
+ value_type (result_val));
+
+ store_unsigned_integer (buf, ctx->addr_size, byte_order, result);
+ return gdbarch_integer_to_address (ctx->gdbarch, int_type, buf);
+ }
+
+ return (CORE_ADDR) result;