public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH v3 00/14] let gdb reuse gcc's C compiler
@ 2014-11-01 21:45 Jan Kratochvil
  2014-11-01 21:46 ` [PATCH v3 05/14] add dummy frame destructor Jan Kratochvil
                   ` (16 more replies)
  0 siblings, 17 replies; 35+ messages in thread
From: Jan Kratochvil @ 2014-11-01 21:45 UTC (permalink / raw)
  To: gdb-patches

Hi,

https://github.com/tromey/gdb.git
submit/compile

This is version 3 of the patch to let gdb reuse gcc's C compiler.

Version 2 is here:

    https://sourceware.org/ml/gdb-patches/2014-06/msg00758.html

New in version 3:
 * x86_64 in -m32 mode testsuite run was failing:
     (gdb) compile code -- ;
     Could not find a compiler matching "^i.86-[^-]*-linux(-gnu)?-gcc$"
   Therefore applied the patch included below.
 * Rename dwarf_expr_frame_base_1 -> func_get_frame_base_dwarf_block:
   https://sourceware.org/ml/gdb-patches/2014-10/msg00158.html
 * doc updates:
   https://sourceware.org/ml/gdb-patches/2014-10/msg00164.html

Built and regtested on x86-64, x86_64-m32 and i686 Fedora 21pre in linux-nat
and gdbserver modes.

Going to check it in in a week as v2 has been posted by global maintainer Tom
Tromey and there has already been enough time for its reviews.


Thanks,
Jan


diff --git a/gdb/amd64-tdep.c b/gdb/amd64-tdep.c
index ca61b1b..ed41f88 100644
--- a/gdb/amd64-tdep.c
+++ b/gdb/amd64-tdep.c
@@ -2914,14 +2914,6 @@ static const int amd64_record_regmap[] =
   AMD64_DS_REGNUM, AMD64_ES_REGNUM, AMD64_FS_REGNUM, AMD64_GS_REGNUM
 };
 
-/* gdbarch gnu_triplet_regexp method.  */
-
-static const char *
-amd64_gnu_triplet_regexp (struct gdbarch *gdbarch)
-{
-  return "x86_64";
-}
-
 void
 amd64_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
@@ -3071,8 +3063,6 @@ amd64_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
   set_gdbarch_insn_is_call (gdbarch, amd64_insn_is_call);
   set_gdbarch_insn_is_ret (gdbarch, amd64_insn_is_ret);
   set_gdbarch_insn_is_jump (gdbarch, amd64_insn_is_jump);
-
-  set_gdbarch_gnu_triplet_regexp (gdbarch, amd64_gnu_triplet_regexp);
 }
 \f
 
diff --git a/gdb/i386-tdep.c b/gdb/i386-tdep.c
index 4a59560..56bfd49 100644
--- a/gdb/i386-tdep.c
+++ b/gdb/i386-tdep.c
@@ -4305,12 +4305,13 @@ i386_stap_parse_special_token (struct gdbarch *gdbarch,
 
 \f
 
-/* gdbarch gnu_triplet_regexp method.  */
+/* gdbarch gnu_triplet_regexp method.  Both arches are acceptable as GDB always
+   also supplies -m64 or -m32 by gdbarch_gcc_target_options.  */
 
 static const char *
 i386_gnu_triplet_regexp (struct gdbarch *gdbarch)
 {
-  return "i.86";
+  return "(x86_64|i.86)";
 }
 
 \f

^ permalink raw reply	[flat|nested] 35+ messages in thread

* [PATCH v3 04/14] add make_unqualified_type
  2014-11-01 21:45 [PATCH v3 00/14] let gdb reuse gcc's C compiler Jan Kratochvil
  2014-11-01 21:46 ` [PATCH v3 05/14] add dummy frame destructor Jan Kratochvil
  2014-11-01 21:46 ` [PATCH v3 01/14] introduce ui_file_write_for_put Jan Kratochvil
@ 2014-11-01 21:46 ` Jan Kratochvil
  2014-11-01 21:46 ` [PATCH v3 03/14] add some missing ops to DWARF assembler Jan Kratochvil
                   ` (13 subsequent siblings)
  16 siblings, 0 replies; 35+ messages in thread
From: Jan Kratochvil @ 2014-11-01 21:46 UTC (permalink / raw)
  To: gdb-patches

From: Tom Tromey <tromey@redhat.com>

There's seemingly no function to get the unqualified variant of a
type, so this patch adds one.  This new function will be used in the
final patch.

2014-10-07  Tom Tromey  <tromey@redhat.com>

	* gdbtypes.h (make_unqualified_type): Declare.
	* gdbtypes.c (make_unqualified_type): New function.
---
 gdb/ChangeLog  |    5 +++++
 gdb/gdbtypes.c |   13 +++++++++++++
 gdb/gdbtypes.h |    2 ++
 3 files changed, 20 insertions(+)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 9993a2b..15513ae 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,10 @@
 2014-10-07  Tom Tromey  <tromey@redhat.com>
 
+	* gdbtypes.h (make_unqualified_type): Declare.
+	* gdbtypes.c (make_unqualified_type): New function.
+
+2014-10-07  Tom Tromey  <tromey@redhat.com>
+
 	* ui-file.h (ui_file_write_for_put): Declare.
 	* ui-file.c (ui_file_write_for_put): New function.
 	* mi/mi-out.c (do_write): Remove.
diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c
index ee33d77..017cd28 100644
--- a/gdb/gdbtypes.c
+++ b/gdb/gdbtypes.c
@@ -703,6 +703,19 @@ make_restrict_type (struct type *type)
 			      NULL);
 }
 
+/* Make a type without const, volatile, or restrict.  */
+
+struct type *
+make_unqualified_type (struct type *type)
+{
+  return make_qualified_type (type,
+			      (TYPE_INSTANCE_FLAGS (type)
+			       & ~(TYPE_INSTANCE_FLAG_CONST
+				   | TYPE_INSTANCE_FLAG_VOLATILE
+				   | TYPE_INSTANCE_FLAG_RESTRICT)),
+			      NULL);
+}
+
 /* Replace the contents of ntype with the type *type.  This changes the
    contents, rather than the pointer for TYPE_MAIN_TYPE (ntype); thus
    the changes are propogated to all types in the TYPE_CHAIN.
diff --git a/gdb/gdbtypes.h b/gdb/gdbtypes.h
index bd1a0ab..879c650 100644
--- a/gdb/gdbtypes.h
+++ b/gdb/gdbtypes.h
@@ -1641,6 +1641,8 @@ extern struct type *make_cv_type (int, int, struct type *, struct type **);
 
 extern struct type *make_restrict_type (struct type *);
 
+extern struct type *make_unqualified_type (struct type *);
+
 extern void replace_type (struct type *, struct type *);
 
 extern int address_space_name_to_int (struct gdbarch *, char *);

^ permalink raw reply	[flat|nested] 35+ messages in thread

* [PATCH v3 01/14] introduce ui_file_write_for_put
  2014-11-01 21:45 [PATCH v3 00/14] let gdb reuse gcc's C compiler Jan Kratochvil
  2014-11-01 21:46 ` [PATCH v3 05/14] add dummy frame destructor Jan Kratochvil
@ 2014-11-01 21:46 ` Jan Kratochvil
  2014-11-01 21:46 ` [PATCH v3 04/14] add make_unqualified_type Jan Kratochvil
                   ` (14 subsequent siblings)
  16 siblings, 0 replies; 35+ messages in thread
From: Jan Kratochvil @ 2014-11-01 21:46 UTC (permalink / raw)
  To: gdb-patches

From: Tom Tromey <tromey@redhat.com>

This introduces a small helper function, ui_file_write_for_put.  It is
a wrapper for ui_write that is suitable for passing directly to
ui_file_put.

This patch also updates one existing spot to use this new function.

2014-10-07  Tom Tromey  <tromey@redhat.com>

	* ui-file.h (ui_file_write_for_put): Declare.
	* ui-file.c (ui_file_write_for_put): New function.
	* mi/mi-out.c (do_write): Remove.
	(mi_out_put): Use ui_file_write_for_put.
---
 gdb/ChangeLog   |    7 +++++++
 gdb/mi/mi-out.c |    8 +-------
 gdb/ui-file.c   |    6 ++++++
 gdb/ui-file.h   |    6 ++++++
 4 files changed, 20 insertions(+), 7 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index a629660..9993a2b 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,10 @@
+2014-10-07  Tom Tromey  <tromey@redhat.com>
+
+	* ui-file.h (ui_file_write_for_put): Declare.
+	* ui-file.c (ui_file_write_for_put): New function.
+	* mi/mi-out.c (do_write): Remove.
+	(mi_out_put): Use ui_file_write_for_put.
+
 2014-10-31  Doug Evans  <xdje42@gmail.com>
 
 	* objfiles.h (ALL_PSPACE_OBJFILES_SAFE): Delete, unused.
diff --git a/gdb/mi/mi-out.c b/gdb/mi/mi-out.c
index 6ec41e6..9f5d1c0 100644
--- a/gdb/mi/mi-out.c
+++ b/gdb/mi/mi-out.c
@@ -376,18 +376,12 @@ mi_out_rewind (struct ui_out *uiout)
 
 /* Dump the buffer onto the specified stream.  */
 
-static void
-do_write (void *data, const char *buffer, long length_buffer)
-{
-  ui_file_write (data, buffer, length_buffer);
-}
-
 void
 mi_out_put (struct ui_out *uiout, struct ui_file *stream)
 {
   mi_out_data *data = ui_out_data (uiout);
 
-  ui_file_put (data->buffer, do_write, stream);
+  ui_file_put (data->buffer, ui_file_write_for_put, stream);
   ui_file_rewind (data->buffer);
 }
 
diff --git a/gdb/ui-file.c b/gdb/ui-file.c
index 49607dc..e3c7ba2 100644
--- a/gdb/ui-file.c
+++ b/gdb/ui-file.c
@@ -223,6 +223,12 @@ ui_file_write (struct ui_file *file,
 }
 
 void
+ui_file_write_for_put (void *data, const char *buffer, long length_buffer)
+{
+  ui_file_write (data, buffer, length_buffer);
+}
+
+void
 ui_file_write_async_safe (struct ui_file *file,
 			  const char *buf,
 			  long length_buf)
diff --git a/gdb/ui-file.h b/gdb/ui-file.h
index 50c1333..29ce5e0 100644
--- a/gdb/ui-file.h
+++ b/gdb/ui-file.h
@@ -98,6 +98,12 @@ extern int ui_file_isatty (struct ui_file *);
 extern void ui_file_write (struct ui_file *file, const char *buf,
 			   long length_buf);
 
+/* A wrapper for ui_file_write that is suitable for use by
+   ui_file_put.  */
+
+extern void ui_file_write_for_put (void *data, const char *buffer,
+				   long length_buffer);
+
 extern void ui_file_write_async_safe (struct ui_file *file, const char *buf,
 				      long length_buf);
 

^ permalink raw reply	[flat|nested] 35+ messages in thread

* [PATCH v3 06/14] add infcall_mmap and gcc_target_options gdbarch methods
  2014-11-01 21:45 [PATCH v3 00/14] let gdb reuse gcc's C compiler Jan Kratochvil
                   ` (4 preceding siblings ...)
  2014-11-01 21:46 ` [PATCH v3 08/14] introduce call_function_by_hand_dummy Jan Kratochvil
@ 2014-11-01 21:46 ` Jan Kratochvil
  2014-11-01 21:46 ` [PATCH v3 07/14] add gnu_triplet_regexp gdbarch method Jan Kratochvil
                   ` (10 subsequent siblings)
  16 siblings, 0 replies; 35+ messages in thread
From: Jan Kratochvil @ 2014-11-01 21:46 UTC (permalink / raw)
  To: gdb-patches

The compiler needed two new gdbarch methods.

The infcall_mmap method allocates memory in the inferior.
This is used when inserting the object code.

The gcc_target_options method computes some arch-specific gcc options
to pass to the compiler.  This is used to ensure that gcc generates
object code for the correct architecture.

2014-10-07  Jan Kratochvil  <jan.kratochvil@redhat.com>

	* arch-utils.c (default_infcall_mmap)
	(default_gcc_target_options): New functions.
	* arch-utils.h (default_infcall_mmap, default_gcc_target_options):
	Declare.
	* gdbarch.h: Rebuild.
	* gdbarch.c: Rebuild.
	* gdbarch.sh (infcall_mmap, gcc_target_options): New methods.
---
 gdb/ChangeLog    |   10 ++++++++++
 gdb/arch-utils.c |   13 +++++++++++++
 gdb/arch-utils.h |    4 ++++
 gdb/gdbarch.c    |   46 ++++++++++++++++++++++++++++++++++++++++++++++
 gdb/gdbarch.h    |   18 ++++++++++++++++++
 gdb/gdbarch.sh   |   12 ++++++++++++
 6 files changed, 103 insertions(+)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index ede97a2..910fcc2 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,15 @@
 2014-10-07  Jan Kratochvil  <jan.kratochvil@redhat.com>
 
+	* arch-utils.c (default_infcall_mmap)
+	(default_gcc_target_options): New functions.
+	* arch-utils.h (default_infcall_mmap, default_gcc_target_options):
+	Declare.
+	* gdbarch.h: Rebuild.
+	* gdbarch.c: Rebuild.
+	* gdbarch.sh (infcall_mmap, gcc_target_options): New methods.
+
+2014-10-07  Jan Kratochvil  <jan.kratochvil@redhat.com>
+
 	* dummy-frame.c (struct dummy_frame) <dtor, dtor_data>: New
 	fields.
 	(pop_dummy_frame): Call the destructor if it exists.
diff --git a/gdb/arch-utils.c b/gdb/arch-utils.c
index 41cf828..5771484 100644
--- a/gdb/arch-utils.c
+++ b/gdb/arch-utils.c
@@ -826,6 +826,19 @@ int default_insn_is_jump (struct gdbarch *gdbarch, CORE_ADDR addr)
   return 0;
 }
 
+CORE_ADDR
+default_infcall_mmap (CORE_ADDR size, unsigned prot)
+{
+  error (_("This target does not support inferior memory allocation by mmap."));
+}
+
+char *
+default_gcc_target_options (struct gdbarch *gdbarch)
+{
+  return xstrprintf ("-m%d%s", gdbarch_ptr_bit (gdbarch),
+		     gdbarch_ptr_bit (gdbarch) == 64 ? " -mcmodel=large" : "");
+}
+
 /* */
 
 /* -Wmissing-prototypes */
diff --git a/gdb/arch-utils.h b/gdb/arch-utils.h
index a609662..53358ed 100644
--- a/gdb/arch-utils.h
+++ b/gdb/arch-utils.h
@@ -178,4 +178,8 @@ extern int default_insn_is_jump (struct gdbarch *, CORE_ADDR);
 /* Do-nothing version of vsyscall_range.  Returns false.  */
 
 extern int default_vsyscall_range (struct gdbarch *gdbarch, struct mem_range *range);
+
+extern CORE_ADDR default_infcall_mmap (CORE_ADDR size, unsigned prot);
+extern char *default_gcc_target_options (struct gdbarch *gdbarch);
+
 #endif
diff --git a/gdb/gdbarch.c b/gdb/gdbarch.c
index 2984358..8d882bc 100644
--- a/gdb/gdbarch.c
+++ b/gdb/gdbarch.c
@@ -316,6 +316,8 @@ struct gdbarch
   gdbarch_insn_is_jump_ftype *insn_is_jump;
   gdbarch_auxv_parse_ftype *auxv_parse;
   gdbarch_vsyscall_range_ftype *vsyscall_range;
+  gdbarch_infcall_mmap_ftype *infcall_mmap;
+  gdbarch_gcc_target_options_ftype *gcc_target_options;
 };
 
 /* Create a new ``struct gdbarch'' based on information provided by
@@ -409,6 +411,8 @@ gdbarch_alloc (const struct gdbarch_info *info,
   gdbarch->insn_is_ret = default_insn_is_ret;
   gdbarch->insn_is_jump = default_insn_is_jump;
   gdbarch->vsyscall_range = default_vsyscall_range;
+  gdbarch->infcall_mmap = default_infcall_mmap;
+  gdbarch->gcc_target_options = default_gcc_target_options;
   /* gdbarch_alloc() */
 
   return gdbarch;
@@ -629,6 +633,8 @@ verify_gdbarch (struct gdbarch *gdbarch)
   /* Skip verify of insn_is_jump, invalid_p == 0 */
   /* Skip verify of auxv_parse, has predicate.  */
   /* Skip verify of vsyscall_range, invalid_p == 0 */
+  /* Skip verify of infcall_mmap, invalid_p == 0 */
+  /* Skip verify of gcc_target_options, invalid_p == 0 */
   buf = ui_file_xstrdup (log, &length);
   make_cleanup (xfree, buf);
   if (length > 0)
@@ -890,6 +896,9 @@ gdbarch_dump (struct gdbarch *gdbarch, struct ui_file *file)
                       "gdbarch_dump: frame_red_zone_size = %s\n",
                       plongest (gdbarch->frame_red_zone_size));
   fprintf_unfiltered (file,
+                      "gdbarch_dump: gcc_target_options = <%s>\n",
+                      host_address_to_string (gdbarch->gcc_target_options));
+  fprintf_unfiltered (file,
                       "gdbarch_dump: gdbarch_gcore_bfd_target_p() = %d\n",
                       gdbarch_gcore_bfd_target_p (gdbarch));
   fprintf_unfiltered (file,
@@ -956,6 +965,9 @@ gdbarch_dump (struct gdbarch *gdbarch, struct ui_file *file)
                       "gdbarch_dump: in_solib_return_trampoline = <%s>\n",
                       host_address_to_string (gdbarch->in_solib_return_trampoline));
   fprintf_unfiltered (file,
+                      "gdbarch_dump: infcall_mmap = <%s>\n",
+                      host_address_to_string (gdbarch->infcall_mmap));
+  fprintf_unfiltered (file,
                       "gdbarch_dump: gdbarch_info_proc_p() = %d\n",
                       gdbarch_info_proc_p (gdbarch));
   fprintf_unfiltered (file,
@@ -4406,6 +4418,40 @@ set_gdbarch_vsyscall_range (struct gdbarch *gdbarch,
   gdbarch->vsyscall_range = vsyscall_range;
 }
 
+CORE_ADDR
+gdbarch_infcall_mmap (struct gdbarch *gdbarch, CORE_ADDR size, unsigned prot)
+{
+  gdb_assert (gdbarch != NULL);
+  gdb_assert (gdbarch->infcall_mmap != NULL);
+  if (gdbarch_debug >= 2)
+    fprintf_unfiltered (gdb_stdlog, "gdbarch_infcall_mmap called\n");
+  return gdbarch->infcall_mmap (size, prot);
+}
+
+void
+set_gdbarch_infcall_mmap (struct gdbarch *gdbarch,
+                          gdbarch_infcall_mmap_ftype infcall_mmap)
+{
+  gdbarch->infcall_mmap = infcall_mmap;
+}
+
+char *
+gdbarch_gcc_target_options (struct gdbarch *gdbarch)
+{
+  gdb_assert (gdbarch != NULL);
+  gdb_assert (gdbarch->gcc_target_options != NULL);
+  if (gdbarch_debug >= 2)
+    fprintf_unfiltered (gdb_stdlog, "gdbarch_gcc_target_options called\n");
+  return gdbarch->gcc_target_options (gdbarch);
+}
+
+void
+set_gdbarch_gcc_target_options (struct gdbarch *gdbarch,
+                                gdbarch_gcc_target_options_ftype gcc_target_options)
+{
+  gdbarch->gcc_target_options = gcc_target_options;
+}
+
 
 /* Keep a registry of per-architecture data-pointers required by GDB
    modules.  */
diff --git a/gdb/gdbarch.h b/gdb/gdbarch.h
index f5330c2..0b2292b 100644
--- a/gdb/gdbarch.h
+++ b/gdb/gdbarch.h
@@ -1330,6 +1330,24 @@ typedef int (gdbarch_vsyscall_range_ftype) (struct gdbarch *gdbarch, struct mem_
 extern int gdbarch_vsyscall_range (struct gdbarch *gdbarch, struct mem_range *range);
 extern void set_gdbarch_vsyscall_range (struct gdbarch *gdbarch, gdbarch_vsyscall_range_ftype *vsyscall_range);
 
+/* Allocate SIZE bytes of PROT protected page aligned memory in inferior.
+   PROT has rwx bitmask format - bit 2 (value 4) is for readable memory, bit 1
+   (value 2) is for writable memory and bit 0 (value 1) is for executable memory.
+   Throw an error if it is not possible.  Returned address is always valid. */
+
+typedef CORE_ADDR (gdbarch_infcall_mmap_ftype) (CORE_ADDR size, unsigned prot);
+extern CORE_ADDR gdbarch_infcall_mmap (struct gdbarch *gdbarch, CORE_ADDR size, unsigned prot);
+extern void set_gdbarch_infcall_mmap (struct gdbarch *gdbarch, gdbarch_infcall_mmap_ftype *infcall_mmap);
+
+/* Return string (caller has to use xfree for it) with options for GCC
+   to produce code for this target, typically "-m64", "-m32" or "-m31".
+   These options are put before CU's DW_AT_producer compilation options so that
+   they can override it.  Method may also return NULL. */
+
+typedef char * (gdbarch_gcc_target_options_ftype) (struct gdbarch *gdbarch);
+extern char * gdbarch_gcc_target_options (struct gdbarch *gdbarch);
+extern void set_gdbarch_gcc_target_options (struct gdbarch *gdbarch, gdbarch_gcc_target_options_ftype *gcc_target_options);
+
 /* Definition for an unknown syscall, used basically in error-cases.  */
 #define UNKNOWN_SYSCALL (-1)
 
diff --git a/gdb/gdbarch.sh b/gdb/gdbarch.sh
index 5442799..ac63a8b 100755
--- a/gdb/gdbarch.sh
+++ b/gdb/gdbarch.sh
@@ -1036,6 +1036,18 @@ M:int:auxv_parse:gdb_byte **readptr, gdb_byte *endptr, CORE_ADDR *typep, CORE_AD
 # range with zero length is returned.  Returns true if the vsyscall is
 # found, false otherwise.
 m:int:vsyscall_range:struct mem_range *range:range::default_vsyscall_range::0
+
+# Allocate SIZE bytes of PROT protected page aligned memory in inferior.
+# PROT has rwx bitmask format - bit 2 (value 4) is for readable memory, bit 1
+# (value 2) is for writable memory and bit 0 (value 1) is for executable memory.
+# Throw an error if it is not possible.  Returned address is always valid.
+f:CORE_ADDR:infcall_mmap:CORE_ADDR size, unsigned prot:size, prot::default_infcall_mmap::0
+
+# Return string (caller has to use xfree for it) with options for GCC
+# to produce code for this target, typically "-m64", "-m32" or "-m31".
+# These options are put before CU's DW_AT_producer compilation options so that
+# they can override it.  Method may also return NULL.
+m:char *:gcc_target_options:void:::default_gcc_target_options::0
 EOF
 }
 

^ permalink raw reply	[flat|nested] 35+ messages in thread

* [PATCH v3 03/14] add some missing ops to DWARF assembler
  2014-11-01 21:45 [PATCH v3 00/14] let gdb reuse gcc's C compiler Jan Kratochvil
                   ` (2 preceding siblings ...)
  2014-11-01 21:46 ` [PATCH v3 04/14] add make_unqualified_type Jan Kratochvil
@ 2014-11-01 21:46 ` Jan Kratochvil
  2014-11-01 21:46 ` [PATCH v3 08/14] introduce call_function_by_hand_dummy Jan Kratochvil
                   ` (12 subsequent siblings)
  16 siblings, 0 replies; 35+ messages in thread
From: Jan Kratochvil @ 2014-11-01 21:46 UTC (permalink / raw)
  To: gdb-patches

From: Tom Tromey <tromey@redhat.com>

This changes the DWARF assembler to allow comments in a location
expression, and also adds support for a few new opcodes I needed.

2014-10-07  Tom Tromey  <tromey@redhat.com>

	* lib/dwarf.exp (_location): Ignore blank lines.  Allow comments.
	Handle DW_OP_pick, DW_OP_skip, DW_OP_bra.
---
 gdb/testsuite/ChangeLog     |    5 +++++
 gdb/testsuite/lib/dwarf.exp |    9 ++++++++-
 2 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog
index 4f2f208..8fa75d1 100644
--- a/gdb/testsuite/ChangeLog
+++ b/gdb/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2014-10-07  Tom Tromey  <tromey@redhat.com>
+
+	* lib/dwarf.exp (_location): Ignore blank lines.  Allow comments.
+	Handle DW_OP_pick, DW_OP_skip, DW_OP_bra.
+
 2014-10-30  Doug Evans  <dje@google.com>
 
 	* gdb.python/py-objfile.exp: Add tests for setting random attributes
diff --git a/gdb/testsuite/lib/dwarf.exp b/gdb/testsuite/lib/dwarf.exp
index 1483271..eac2c80 100644
--- a/gdb/testsuite/lib/dwarf.exp
+++ b/gdb/testsuite/lib/dwarf.exp
@@ -678,7 +678,8 @@ namespace eval Dwarf {
 	variable _cu_offset_size
 
 	foreach line [split $body \n] {
-	    if {[lindex $line 0] == ""} {
+	    # Ignore blank lines, and allow embedded comments.
+	    if {[lindex $line 0] == "" || [regexp -- {^[ \t]*#} $line]} {
 		continue
 	    }
 	    set opcode [_map_name [lindex $line 0] _OP]
@@ -689,6 +690,7 @@ namespace eval Dwarf {
 		    _op .${_cu_addr_size}byte [lindex $line 1]
 		}
 
+		DW_OP_pick -
 		DW_OP_const1u -
 		DW_OP_const1s {
 		    _op .byte [lindex $line 1]
@@ -729,6 +731,11 @@ namespace eval Dwarf {
 		    _op .uleb128 [lindex $line 2]
 		}
 
+		DW_OP_skip -
+		DW_OP_bra {
+		    _op .2byte [lindex $line 1]
+		}
+
 		DW_OP_GNU_implicit_pointer {
 		    if {[llength $line] != 3} {
 			error "usage: DW_OP_GNU_implicit_pointer LABEL OFFSET"

^ permalink raw reply	[flat|nested] 35+ messages in thread

* [PATCH v3 05/14] add dummy frame destructor
  2014-11-01 21:45 [PATCH v3 00/14] let gdb reuse gcc's C compiler Jan Kratochvil
@ 2014-11-01 21:46 ` Jan Kratochvil
  2014-11-01 21:46 ` [PATCH v3 01/14] introduce ui_file_write_for_put Jan Kratochvil
                   ` (15 subsequent siblings)
  16 siblings, 0 replies; 35+ messages in thread
From: Jan Kratochvil @ 2014-11-01 21:46 UTC (permalink / raw)
  To: gdb-patches

The compiler code needed a hook into dummy frame destruction, so that
some state could be kept while the inferior call is made and then
destroyed when the inferior call finishes.

This patch adds an optional destructor to dummy frames and a new API
to access it.

2014-10-07  Jan Kratochvil  <jan.kratochvil@redhat.com>

	* dummy-frame.c (struct dummy_frame) <dtor, dtor_data>: New
	fields.
	(pop_dummy_frame): Call the destructor if it exists.
	(register_dummy_frame_dtor, find_dummy_frame_dtor): New
	functions.
	* dummy-frame.h (dummy_frame_dtor_ftype): New typedef.
	(register_dummy_frame_dtor, find_dummy_frame_dtor): Declare.
---
 gdb/ChangeLog     |   10 ++++++++++
 gdb/dummy-frame.c |   41 +++++++++++++++++++++++++++++++++++++++++
 gdb/dummy-frame.h |   13 +++++++++++++
 3 files changed, 64 insertions(+)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 15513ae..ede97a2 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,13 @@
+2014-10-07  Jan Kratochvil  <jan.kratochvil@redhat.com>
+
+	* dummy-frame.c (struct dummy_frame) <dtor, dtor_data>: New
+	fields.
+	(pop_dummy_frame): Call the destructor if it exists.
+	(register_dummy_frame_dtor, find_dummy_frame_dtor): New
+	functions.
+	* dummy-frame.h (dummy_frame_dtor_ftype): New typedef.
+	(register_dummy_frame_dtor, find_dummy_frame_dtor): Declare.
+
 2014-10-07  Tom Tromey  <tromey@redhat.com>
 
 	* gdbtypes.h (make_unqualified_type): Declare.
diff --git a/gdb/dummy-frame.c b/gdb/dummy-frame.c
index a13601b..6ca8b19 100644
--- a/gdb/dummy-frame.c
+++ b/gdb/dummy-frame.c
@@ -61,6 +61,13 @@ struct dummy_frame
 
   /* The caller's state prior to the call.  */
   struct infcall_suspend_state *caller_state;
+
+  /* If non-NULL, a destructor that is run when this dummy frame is
+     popped.  */
+  void (*dtor) (void *data);
+
+  /* Arbitrary data that is passed to DTOR.  */
+  void *dtor_data;
 };
 
 static struct dummy_frame *dummy_frame_stack = NULL;
@@ -127,6 +134,10 @@ pop_dummy_frame (struct dummy_frame **dummy_ptr)
   struct dummy_frame *dummy = *dummy_ptr;
 
   gdb_assert (ptid_equal (dummy->id.ptid, inferior_ptid));
+
+  if (dummy->dtor != NULL)
+    dummy->dtor (dummy->dtor_data);
+
   restore_infcall_suspend_state (dummy->caller_state);
 
   iterate_over_breakpoints (pop_dummy_frame_bpt, dummy);
@@ -190,6 +201,36 @@ dummy_frame_discard (struct frame_id dummy_id, ptid_t ptid)
     remove_dummy_frame (dp);
 }
 
+/* See dummy-frame.h.  */
+
+void
+register_dummy_frame_dtor (struct frame_id dummy_id, ptid_t ptid,
+			   dummy_frame_dtor_ftype *dtor, void *dtor_data)
+{
+  struct dummy_frame_id id = { dummy_id, ptid };
+  struct dummy_frame **dp, *d;
+
+  dp = lookup_dummy_frame (&id);
+  gdb_assert (dp != NULL);
+  d = *dp;
+  gdb_assert (d->dtor == NULL);
+  d->dtor = dtor;
+  d->dtor_data = dtor_data;
+}
+
+/* See dummy-frame.h.  */
+
+int
+find_dummy_frame_dtor (dummy_frame_dtor_ftype *dtor, void *dtor_data)
+{
+  struct dummy_frame *d;
+
+  for (d = dummy_frame_stack; d != NULL; d = d->next)
+    if (d->dtor == dtor && d->dtor_data == dtor_data)
+      return 1;
+  return 0;
+}
+
 /* There may be stale dummy frames, perhaps left over from when an uncaught
    longjmp took us out of a function that was called by the debugger.  Clean
    them up at least once whenever we start a new inferior.  */
diff --git a/gdb/dummy-frame.h b/gdb/dummy-frame.h
index bac1aac..8c1a1d0 100644
--- a/gdb/dummy-frame.h
+++ b/gdb/dummy-frame.h
@@ -54,4 +54,17 @@ extern void dummy_frame_discard (struct frame_id dummy_id, ptid_t ptid);
 
 extern const struct frame_unwind dummy_frame_unwind;
 
+/* Call DTOR with DTOR_DATA when DUMMY_ID frame of thread PTID gets discarded.
+   Dummy frame with DUMMY_ID must exist.  There must be no other call of
+   register_dummy_frame_dtor for that dummy frame.  */
+typedef void (dummy_frame_dtor_ftype) (void *data);
+extern void register_dummy_frame_dtor (struct frame_id dummy_id, ptid_t ptid,
+				       dummy_frame_dtor_ftype *dtor,
+				       void *dtor_data);
+
+/* Return 1 if there exists dummy frame with registered DTOR and DTOR_DATA.
+   Return 0 otherwise.  */
+extern int find_dummy_frame_dtor (dummy_frame_dtor_ftype *dtor,
+				  void *dtor_data);
+
 #endif /* !defined (DUMMY_FRAME_H)  */

^ permalink raw reply	[flat|nested] 35+ messages in thread

* [PATCH v3 07/14] add gnu_triplet_regexp gdbarch method
  2014-11-01 21:45 [PATCH v3 00/14] let gdb reuse gcc's C compiler Jan Kratochvil
                   ` (5 preceding siblings ...)
  2014-11-01 21:46 ` [PATCH v3 06/14] add infcall_mmap and gcc_target_options gdbarch methods Jan Kratochvil
@ 2014-11-01 21:46 ` Jan Kratochvil
  2014-11-01 21:46 ` [PATCH v3 02/14] add gcc/gdb interface files Jan Kratochvil
                   ` (9 subsequent siblings)
  16 siblings, 0 replies; 35+ messages in thread
From: Jan Kratochvil @ 2014-11-01 21:46 UTC (permalink / raw)
  To: gdb-patches

From: Tom Tromey <tromey@redhat.com>

gdb has to inform libcc1.so of the target being used, so that the
correct compiler can be invoked.  The compiler is invoked using the
GNU configury triplet prefix, e.g., "x86_64-unknown-linux-gnu-gcc".

In order for this to work we need to map the gdbarch to the GNU
configury triplet arch.  In most cases these are identical; however,
the x86 family poses some problems, as the BFD arch names are quite
different from the GNU triplet names.  So, we introduce a new gdbarch
method for this.  A regular expression is used because there are
various valid values for the arch prefix in the triplet.

This patch also updates the osabi code to associate a regular
expression with the OS ABI.  I have only added a concrete value for
Linux.  Note that the "-gnu" part is optional, at least on Fedora it
is omitted from the installed GCC executable's name.

2014-10-07  Tom Tromey  <tromey@redhat.com>
	    Jan Kratochvil  <jan.kratochvil@redhat.com>

	* osabi.h (osabi_triplet_regexp): Declare.
	* osabi.c (struct osabi_names): New.
	(gdb_osabi_names): Change type to struct osabi_names.  Update
	values.
	(gdbarch_osabi_name): Update.
	(osabi_triplet_regexp): New function.
	(osabi_from_tdesc_string, _initialize_gdb_osabi): Update.
	* i386-tdep.c (i386_gnu_triplet_regexp): New method.
	(i386_elf_init_abi, i386_go32_init_abi, i386_gdbarch_init): Call
	set_gdbarch_gnu_triplet_regexp.
	* gdbarch.sh (gnu_triplet_regexp): New method.
	* gdbarch.c, gdbarch.h: Rebuild.
	* arch-utils.h (default_gnu_triplet_regexp): Declare.
	* arch-utils.c (default_gnu_triplet_regexp): New function.
---
 gdb/ChangeLog    |   18 +++++++++++
 gdb/arch-utils.c |    8 +++++
 gdb/arch-utils.h |    1 +
 gdb/gdbarch.c    |   23 ++++++++++++++
 gdb/gdbarch.h    |   10 ++++++
 gdb/gdbarch.sh   |    7 ++++
 gdb/i386-tdep.c  |   17 ++++++++++
 gdb/osabi.c      |   88 ++++++++++++++++++++++++++++++++++--------------------
 gdb/osabi.h      |    4 ++
 9 files changed, 144 insertions(+), 32 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 910fcc2..f66a995 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,21 @@
+2014-10-07  Tom Tromey  <tromey@redhat.com>
+	    Jan Kratochvil  <jan.kratochvil@redhat.com>
+
+	* osabi.h (osabi_triplet_regexp): Declare.
+	* osabi.c (struct osabi_names): New.
+	(gdb_osabi_names): Change type to struct osabi_names.  Update
+	values.
+	(gdbarch_osabi_name): Update.
+	(osabi_triplet_regexp): New function.
+	(osabi_from_tdesc_string, _initialize_gdb_osabi): Update.
+	* i386-tdep.c (i386_gnu_triplet_regexp): New method.
+	(i386_elf_init_abi, i386_go32_init_abi, i386_gdbarch_init): Call
+	set_gdbarch_gnu_triplet_regexp.
+	* gdbarch.sh (gnu_triplet_regexp): New method.
+	* gdbarch.c, gdbarch.h: Rebuild.
+	* arch-utils.h (default_gnu_triplet_regexp): Declare.
+	* arch-utils.c (default_gnu_triplet_regexp): New function.
+
 2014-10-07  Jan Kratochvil  <jan.kratochvil@redhat.com>
 
 	* arch-utils.c (default_infcall_mmap)
diff --git a/gdb/arch-utils.c b/gdb/arch-utils.c
index 5771484..79968f7 100644
--- a/gdb/arch-utils.c
+++ b/gdb/arch-utils.c
@@ -839,6 +839,14 @@ default_gcc_target_options (struct gdbarch *gdbarch)
 		     gdbarch_ptr_bit (gdbarch) == 64 ? " -mcmodel=large" : "");
 }
 
+/* gdbarch gnu_triplet_regexp method.  */
+
+const char *
+default_gnu_triplet_regexp (struct gdbarch *gdbarch)
+{
+  return gdbarch_bfd_arch_info (gdbarch)->arch_name;
+}
+
 /* */
 
 /* -Wmissing-prototypes */
diff --git a/gdb/arch-utils.h b/gdb/arch-utils.h
index 53358ed..00116da 100644
--- a/gdb/arch-utils.h
+++ b/gdb/arch-utils.h
@@ -181,5 +181,6 @@ extern int default_vsyscall_range (struct gdbarch *gdbarch, struct mem_range *ra
 
 extern CORE_ADDR default_infcall_mmap (CORE_ADDR size, unsigned prot);
 extern char *default_gcc_target_options (struct gdbarch *gdbarch);
+extern const char *default_gnu_triplet_regexp (struct gdbarch *gdbarch);
 
 #endif
diff --git a/gdb/gdbarch.c b/gdb/gdbarch.c
index 8d882bc..789f422 100644
--- a/gdb/gdbarch.c
+++ b/gdb/gdbarch.c
@@ -318,6 +318,7 @@ struct gdbarch
   gdbarch_vsyscall_range_ftype *vsyscall_range;
   gdbarch_infcall_mmap_ftype *infcall_mmap;
   gdbarch_gcc_target_options_ftype *gcc_target_options;
+  gdbarch_gnu_triplet_regexp_ftype *gnu_triplet_regexp;
 };
 
 /* Create a new ``struct gdbarch'' based on information provided by
@@ -413,6 +414,7 @@ gdbarch_alloc (const struct gdbarch_info *info,
   gdbarch->vsyscall_range = default_vsyscall_range;
   gdbarch->infcall_mmap = default_infcall_mmap;
   gdbarch->gcc_target_options = default_gcc_target_options;
+  gdbarch->gnu_triplet_regexp = default_gnu_triplet_regexp;
   /* gdbarch_alloc() */
 
   return gdbarch;
@@ -635,6 +637,7 @@ verify_gdbarch (struct gdbarch *gdbarch)
   /* Skip verify of vsyscall_range, invalid_p == 0 */
   /* Skip verify of infcall_mmap, invalid_p == 0 */
   /* Skip verify of gcc_target_options, invalid_p == 0 */
+  /* Skip verify of gnu_triplet_regexp, invalid_p == 0 */
   buf = ui_file_xstrdup (log, &length);
   make_cleanup (xfree, buf);
   if (length > 0)
@@ -938,6 +941,9 @@ gdbarch_dump (struct gdbarch *gdbarch, struct ui_file *file)
                       "gdbarch_dump: get_syscall_number = <%s>\n",
                       host_address_to_string (gdbarch->get_syscall_number));
   fprintf_unfiltered (file,
+                      "gdbarch_dump: gnu_triplet_regexp = <%s>\n",
+                      host_address_to_string (gdbarch->gnu_triplet_regexp));
+  fprintf_unfiltered (file,
                       "gdbarch_dump: half_bit = %s\n",
                       plongest (gdbarch->half_bit));
   fprintf_unfiltered (file,
@@ -4452,6 +4458,23 @@ set_gdbarch_gcc_target_options (struct gdbarch *gdbarch,
   gdbarch->gcc_target_options = gcc_target_options;
 }
 
+const char *
+gdbarch_gnu_triplet_regexp (struct gdbarch *gdbarch)
+{
+  gdb_assert (gdbarch != NULL);
+  gdb_assert (gdbarch->gnu_triplet_regexp != NULL);
+  if (gdbarch_debug >= 2)
+    fprintf_unfiltered (gdb_stdlog, "gdbarch_gnu_triplet_regexp called\n");
+  return gdbarch->gnu_triplet_regexp (gdbarch);
+}
+
+void
+set_gdbarch_gnu_triplet_regexp (struct gdbarch *gdbarch,
+                                gdbarch_gnu_triplet_regexp_ftype gnu_triplet_regexp)
+{
+  gdbarch->gnu_triplet_regexp = gnu_triplet_regexp;
+}
+
 
 /* Keep a registry of per-architecture data-pointers required by GDB
    modules.  */
diff --git a/gdb/gdbarch.h b/gdb/gdbarch.h
index 0b2292b..2dd8708 100644
--- a/gdb/gdbarch.h
+++ b/gdb/gdbarch.h
@@ -1348,6 +1348,16 @@ typedef char * (gdbarch_gcc_target_options_ftype) (struct gdbarch *gdbarch);
 extern char * gdbarch_gcc_target_options (struct gdbarch *gdbarch);
 extern void set_gdbarch_gcc_target_options (struct gdbarch *gdbarch, gdbarch_gcc_target_options_ftype *gcc_target_options);
 
+/* Return a regular expression that matches names used by this
+   architecture in GNU configury triplets.  The result is statically
+   allocated and must not be freed.  The default implementation simply
+   returns the BFD architecture name, which is correct in nearly every
+   case. */
+
+typedef const char * (gdbarch_gnu_triplet_regexp_ftype) (struct gdbarch *gdbarch);
+extern const char * gdbarch_gnu_triplet_regexp (struct gdbarch *gdbarch);
+extern void set_gdbarch_gnu_triplet_regexp (struct gdbarch *gdbarch, gdbarch_gnu_triplet_regexp_ftype *gnu_triplet_regexp);
+
 /* Definition for an unknown syscall, used basically in error-cases.  */
 #define UNKNOWN_SYSCALL (-1)
 
diff --git a/gdb/gdbarch.sh b/gdb/gdbarch.sh
index ac63a8b..ee4b342 100755
--- a/gdb/gdbarch.sh
+++ b/gdb/gdbarch.sh
@@ -1048,6 +1048,13 @@ f:CORE_ADDR:infcall_mmap:CORE_ADDR size, unsigned prot:size, prot::default_infca
 # These options are put before CU's DW_AT_producer compilation options so that
 # they can override it.  Method may also return NULL.
 m:char *:gcc_target_options:void:::default_gcc_target_options::0
+
+# Return a regular expression that matches names used by this
+# architecture in GNU configury triplets.  The result is statically
+# allocated and must not be freed.  The default implementation simply
+# returns the BFD architecture name, which is correct in nearly every
+# case.
+m:const char *:gnu_triplet_regexp:void:::default_gnu_triplet_regexp::0
 EOF
 }
 
diff --git a/gdb/i386-tdep.c b/gdb/i386-tdep.c
index 8387c72..56bfd49 100644
--- a/gdb/i386-tdep.c
+++ b/gdb/i386-tdep.c
@@ -4305,6 +4305,17 @@ i386_stap_parse_special_token (struct gdbarch *gdbarch,
 
 \f
 
+/* gdbarch gnu_triplet_regexp method.  Both arches are acceptable as GDB always
+   also supplies -m64 or -m32 by gdbarch_gcc_target_options.  */
+
+static const char *
+i386_gnu_triplet_regexp (struct gdbarch *gdbarch)
+{
+  return "(x86_64|i.86)";
+}
+
+\f
+
 /* Generic ELF.  */
 
 void
@@ -4331,6 +4342,8 @@ i386_elf_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 				      i386_stap_is_single_operand);
   set_gdbarch_stap_parse_special_token (gdbarch,
 					i386_stap_parse_special_token);
+
+  set_gdbarch_gnu_triplet_regexp (gdbarch, i386_gnu_triplet_regexp);
 }
 
 /* System V Release 4 (SVR4).  */
@@ -4378,6 +4391,8 @@ i386_go32_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
   set_gdbarch_sdb_reg_to_regnum (gdbarch, i386_svr4_reg_to_regnum);
 
   set_gdbarch_has_dos_based_file_system (gdbarch, 1);
+
+  set_gdbarch_gnu_triplet_regexp (gdbarch, i386_gnu_triplet_regexp);
 }
 \f
 
@@ -8413,6 +8428,8 @@ i386_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
      gap for the upper AVX, MPX and AVX512 registers.  */
   set_gdbarch_num_regs (gdbarch, I386_AVX512_NUM_REGS);
 
+  set_gdbarch_gnu_triplet_regexp (gdbarch, i386_gnu_triplet_regexp);
+
   /* Get the x86 target description from INFO.  */
   tdesc = info.target_desc;
   if (! tdesc_has_registers (tdesc))
diff --git a/gdb/osabi.c b/gdb/osabi.c
index d33ef9c..50d391a 100644
--- a/gdb/osabi.c
+++ b/gdb/osabi.c
@@ -41,46 +41,70 @@ static const char *gdb_osabi_available_names[GDB_OSABI_INVALID + 3] = {
 };
 static const char *set_osabi_string;
 
+/* Names associated with each osabi.  */
+
+struct osabi_names
+{
+  /* The "pretty" name.  */
+
+  const char *pretty;
+
+  /* The triplet regexp, or NULL if not known.  */
+
+  const char *regexp;
+};
+
 /* This table matches the indices assigned to enum gdb_osabi.  Keep
    them in sync.  */
-static const char * const gdb_osabi_names[] =
+static const struct osabi_names gdb_osabi_names[] =
 {
-  "none",
-
-  "SVR4",
-  "GNU/Hurd",
-  "Solaris",
-  "GNU/Linux",
-  "FreeBSD a.out",
-  "FreeBSD ELF",
-  "NetBSD a.out",
-  "NetBSD ELF",
-  "OpenBSD ELF",
-  "Windows CE",
-  "DJGPP",
-  "Irix",
-  "HP/UX ELF",
-  "HP/UX SOM",
-  "QNX Neutrino",
-  "Cygwin",
-  "AIX",
-  "DICOS",
-  "Darwin",
-  "Symbian",
-  "OpenVMS",
-  "LynxOS178",
-  "Newlib",
-
-  "<invalid>"
+  { "none", NULL },
+
+  { "SVR4", NULL },
+  { "GNU/Hurd", NULL },
+  { "Solaris", NULL },
+  { "GNU/Linux", "linux(-gnu)?" },
+  { "FreeBSD a.out", NULL },
+  { "FreeBSD ELF", NULL },
+  { "NetBSD a.out", NULL },
+  { "NetBSD ELF", NULL },
+  { "OpenBSD ELF", NULL },
+  { "Windows CE", NULL },
+  { "DJGPP", NULL },
+  { "Irix", NULL },
+  { "HP/UX ELF", NULL },
+  { "HP/UX SOM", NULL },
+  { "QNX Neutrino", NULL },
+  { "Cygwin", NULL },
+  { "AIX", NULL },
+  { "DICOS", NULL },
+  { "Darwin", NULL },
+  { "Symbian", NULL },
+  { "OpenVMS", NULL },
+  { "LynxOS178", NULL },
+  { "Newlib", NULL },
+
+  { "<invalid>", NULL }
 };
 
 const char *
 gdbarch_osabi_name (enum gdb_osabi osabi)
 {
   if (osabi >= GDB_OSABI_UNKNOWN && osabi < GDB_OSABI_INVALID)
-    return gdb_osabi_names[osabi];
+    return gdb_osabi_names[osabi].pretty;
+
+  return gdb_osabi_names[GDB_OSABI_INVALID].pretty;
+}
+
+/* See osabi.h.  */
+
+const char *
+osabi_triplet_regexp (enum gdb_osabi osabi)
+{
+  if (osabi >= GDB_OSABI_UNKNOWN && osabi < GDB_OSABI_INVALID)
+    return gdb_osabi_names[osabi].regexp;
 
-  return gdb_osabi_names[GDB_OSABI_INVALID];
+  return gdb_osabi_names[GDB_OSABI_INVALID].regexp;
 }
 
 /* Lookup the OS ABI corresponding to the specified target description
@@ -92,7 +116,7 @@ osabi_from_tdesc_string (const char *name)
   int i;
 
   for (i = 0; i < ARRAY_SIZE (gdb_osabi_names); i++)
-    if (strcmp (name, gdb_osabi_names[i]) == 0)
+    if (strcmp (name, gdb_osabi_names[i].pretty) == 0)
       {
 	/* See note above: the name table matches the indices assigned
 	   to enum gdb_osabi.  */
@@ -645,7 +669,7 @@ extern initialize_file_ftype _initialize_gdb_osabi; /* -Wmissing-prototype */
 void
 _initialize_gdb_osabi (void)
 {
-  if (strcmp (gdb_osabi_names[GDB_OSABI_INVALID], "<invalid>") != 0)
+  if (strcmp (gdb_osabi_names[GDB_OSABI_INVALID].pretty, "<invalid>") != 0)
     internal_error
       (__FILE__, __LINE__,
        _("_initialize_gdb_osabi: gdb_osabi_names[] is inconsistent"));
diff --git a/gdb/osabi.h b/gdb/osabi.h
index 4c03790..8408f0a 100644
--- a/gdb/osabi.h
+++ b/gdb/osabi.h
@@ -49,6 +49,10 @@ void gdbarch_init_osabi (struct gdbarch_info, struct gdbarch *);
 /* Return the name of the specified OS ABI.  */
 const char *gdbarch_osabi_name (enum gdb_osabi);
 
+/* Return a regular expression that matches the OS part of a GNU
+   configury triplet for the given OSABI.  */
+const char *osabi_triplet_regexp (enum gdb_osabi osabi);
+
 /* Helper routine for ELF file sniffers.  This looks at ABI tag note
    sections to determine the OS ABI from the note.  It should be called
    via bfd_map_over_sections.  */

^ permalink raw reply	[flat|nested] 35+ messages in thread

* [PATCH v3 02/14] add gcc/gdb interface files
  2014-11-01 21:45 [PATCH v3 00/14] let gdb reuse gcc's C compiler Jan Kratochvil
                   ` (6 preceding siblings ...)
  2014-11-01 21:46 ` [PATCH v3 07/14] add gnu_triplet_regexp gdbarch method Jan Kratochvil
@ 2014-11-01 21:46 ` Jan Kratochvil
  2014-11-03 12:51   ` Yao Qi
  2014-11-01 21:47 ` [PATCH v3 12/14] add linux_infcall_mmap Jan Kratochvil
                   ` (8 subsequent siblings)
  16 siblings, 1 reply; 35+ messages in thread
From: Jan Kratochvil @ 2014-11-01 21:46 UTC (permalink / raw)
  To: gdb-patches

From: Tom Tromey <tromey@redhat.com>

The gcc plugin is split into two parts.  One part is an ordinary gcc
plugin.  The other part is a shared library that is loaded by gdb.

This patch adds some files that define the interface exported by this
shared library to gdb.  These files also define the internal API by
which the gdb- and gcc-sides communicate.

These files will be kept in sync between gcc and gdb like much of
include/.

The exported API has been intentionally kept very simple.  In
particular only a single function is exported from the gdb-side
library; symbol visibility is used to hide everything else.  This
exported symbol is a function which is called to return a structure
holding function pointers that gdb then uses.  This structure is
versioned so that changes can be made without necessarily requiring a
simultaneous gdb upgrade.

Note that the C compiler API is broken out separately.  This lets us
extend it to other GCC front ends as desired.  We plan to investigate
C++ in the future.

2014-10-07  Phil Muldoon  <pmuldoon@redhat.com>
	    Jan Kratochvil  <jan.kratochvil@redhat.com>
	    Tom Tromey  <tromey@redhat.com>

	* gcc-c-fe.def: New file.
	* gcc-c-interface.h: New file.
	* gcc-interface.h: New file.
---
 include/ChangeLog         |    8 ++
 include/gcc-c-fe.def      |  197 ++++++++++++++++++++++++++++++++++++++++
 include/gcc-c-interface.h |  220 +++++++++++++++++++++++++++++++++++++++++++++
 include/gcc-interface.h   |  127 ++++++++++++++++++++++++++
 4 files changed, 552 insertions(+)
 create mode 100644 include/gcc-c-fe.def
 create mode 100644 include/gcc-c-interface.h
 create mode 100644 include/gcc-interface.h

diff --git a/include/ChangeLog b/include/ChangeLog
index 0204432..84b3dfc 100644
--- a/include/ChangeLog
+++ b/include/ChangeLog
@@ -1,3 +1,11 @@
+2014-10-07  Phil Muldoon  <pmuldoon@redhat.com>
+	    Jan Kratochvil  <jan.kratochvil@redhat.com>
+	    Tom Tromey  <tromey@redhat.com>
+
+	* gcc-c-fe.def: New file.
+	* gcc-c-interface.h: New file.
+	* gcc-interface.h: New file.
+
 2014-10-30  Andrew Pinski  <apinski@cavium.com>
 
 	* elf/mips.h (AFL_EXT_OCTEON3): Define.
diff --git a/include/gcc-c-fe.def b/include/gcc-c-fe.def
new file mode 100644
index 0000000..19cb867
--- /dev/null
+++ b/include/gcc-c-fe.def
@@ -0,0 +1,197 @@
+/* Interface between GCC C FE and GDB  -*- c -*-
+
+   Copyright (C) 2014 Free Software Foundation, Inc.
+
+   This file is part of GCC.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+
+
+/* Create a new "decl" in GCC.  A decl is a declaration, basically a
+   kind of symbol.
+
+   NAME is the name of the new symbol.  SYM_KIND is the kind of
+   symbol being requested.  SYM_TYPE is the new symbol's C type;
+   except for labels, where this is not meaningful and should be
+   zero.  If SUBSTITUTION_NAME is not NULL, then a reference to this
+   decl in the source will later be substituted with a dereference
+   of a variable of the given name.  Otherwise, for symbols having
+   an address (e.g., functions), ADDRESS is the address.  FILENAME
+   and LINE_NUMBER refer to the symbol's source location.  If this
+   is not known, FILENAME can be NULL and LINE_NUMBER can be 0.
+   This function returns the new decl.  */
+
+GCC_METHOD7 (gcc_decl, build_decl,
+	     const char *,	      /* Argument NAME.  */
+	     enum gcc_c_symbol_kind,  /* Argument SYM_KIND.  */
+	     gcc_type,		      /* Argument SYM_TYPE.  */
+	     const char *,	      /* Argument SUBSTITUTION_NAME.  */
+	     gcc_address,	      /* Argument ADDRESS.  */
+	     const char *,	      /* Argument FILENAME.  */
+	     unsigned int)	      /* Argument LINE_NUMBER.  */
+
+/* Insert a GCC decl into the symbol table.  DECL is the decl to
+   insert.  IS_GLOBAL is true if this is an outermost binding, and
+   false if it is a possibly-shadowing binding.  */
+
+GCC_METHOD2 (int /* bool */, bind,
+	     gcc_decl,		   /* Argument DECL.  */
+	     int /* bool */)       /* Argument IS_GLOBAL.  */
+
+/* Insert a tagged type into the symbol table.  NAME is the tag name
+   of the type and TAGGED_TYPE is the type itself.  TAGGED_TYPE must
+   be either a struct, union, or enum type, as these are the only
+   types that have tags.  FILENAME and LINE_NUMBER refer to the type's
+   source location.  If this is not known, FILENAME can be NULL and
+   LINE_NUMBER can be 0.  */
+
+GCC_METHOD4 (int /* bool */, tagbind,
+	     const char *,	      /* Argument NAME.  */
+	     gcc_type,		      /* Argument TAGGED_TYPE.  */
+	     const char *,	      /* Argument FILENAME.  */
+	     unsigned int)	      /* Argument LINE_NUMBER.  */
+
+/* Return the type of a pointer to a given base type.  */
+
+GCC_METHOD1 (gcc_type, build_pointer_type,
+	     gcc_type)			/* Argument BASE_TYPE.  */
+
+/* Create a new 'struct' type.  Initially it has no fields.  */
+
+GCC_METHOD0 (gcc_type, build_record_type)
+
+/* Create a new 'union' type.  Initially it has no fields.  */
+
+GCC_METHOD0 (gcc_type, build_union_type)
+
+/* Add a field to a struct or union type.  FIELD_NAME is the field's
+   name.  FIELD_TYPE is the type of the field.  BITSIZE and BITPOS
+   indicate where in the struct the field occurs.  */
+
+GCC_METHOD5 (int /* bool */, build_add_field,
+	     gcc_type,			   /* Argument RECORD_OR_UNION_TYPE. */
+	     const char *,		   /* Argument FIELD_NAME.  */
+	     gcc_type,			   /* Argument FIELD_TYPE.  */
+	     unsigned long,		   /* Argument BITSIZE.  */
+	     unsigned long)		   /* Argument BITPOS.  */
+
+/* After all the fields have been added to a struct or union, the
+   struct or union type must be "finished".  This does some final
+   cleanups in GCC.  */
+
+GCC_METHOD2 (int /* bool */, finish_record_or_union,
+	     gcc_type,			   /* Argument RECORD_OR_UNION_TYPE. */
+	     unsigned long)		   /* Argument SIZE_IN_BYTES.  */
+
+/* Create a new 'enum' type.  The new type initially has no
+   associated constants.  */
+
+GCC_METHOD1 (gcc_type, build_enum_type,
+	     gcc_type)			    /* Argument UNDERLYING_INT_TYPE. */
+
+/* Add a new constant to an enum type.  NAME is the constant's
+   name and VALUE is its value.  */
+
+GCC_METHOD3 (int /* bool */, build_add_enum_constant,
+	     gcc_type,		       /* Argument ENUM_TYPE.  */
+	     const char *,	       /* Argument NAME.  */
+	     unsigned long)	       /* Argument VALUE.  */
+
+/* After all the constants have been added to an enum, the type must
+   be "finished".  This does some final cleanups in GCC.  */
+
+GCC_METHOD1 (int /* bool */, finish_enum_type,
+	     gcc_type)		       /* Argument ENUM_TYPE.  */
+
+/* Create a new function type.  RETURN_TYPE is the type returned by
+   the function, and ARGUMENT_TYPES is a vector, of length NARGS, of
+   the argument types.  IS_VARARGS is true if the function is
+   varargs.  */
+
+GCC_METHOD3 (gcc_type, build_function_type,
+	     gcc_type,			   /* Argument RETURN_TYPE.  */
+	     const struct gcc_type_array *, /* Argument ARGUMENT_TYPES.  */
+	     int /* bool */)               /* Argument IS_VARARGS.  */
+
+/* Return an integer type with the given properties.  */
+
+GCC_METHOD2 (gcc_type, int_type,
+	     int /* bool */,               /* Argument IS_UNSIGNED.  */
+	     unsigned long)                /* Argument SIZE_IN_BYTES.  */
+
+/* Return a floating point type with the given properties.  */
+
+GCC_METHOD1 (gcc_type, float_type,
+	     unsigned long)			/* Argument SIZE_IN_BYTES.  */
+
+/* Return the 'void' type.  */
+
+GCC_METHOD0 (gcc_type, void_type)
+
+/* Return the 'bool' type.  */
+
+GCC_METHOD0 (gcc_type, bool_type)
+
+/* Create a new array type.  If NUM_ELEMENTS is -1, then the array
+   is assumed to have an unknown length.  */
+
+GCC_METHOD2 (gcc_type, build_array_type,
+	     gcc_type,			  /* Argument ELEMENT_TYPE.  */
+	     int)			  /* Argument NUM_ELEMENTS.  */
+
+/* Create a new variably-sized array type.  UPPER_BOUND_NAME is the
+   name of a local variable that holds the upper bound of the array;
+   it is one less than the array size.  */
+
+GCC_METHOD2 (gcc_type, build_vla_array_type,
+	     gcc_type,			  /* Argument ELEMENT_TYPE.  */
+	     const char *)		  /* Argument UPPER_BOUND_NAME.  */
+
+/* Return a qualified variant of a given base type.  QUALIFIERS says
+   which qualifiers to use; it is composed of or'd together
+   constants from 'enum gcc_qualifiers'.  */
+
+GCC_METHOD2 (gcc_type, build_qualified_type,
+	     gcc_type,			      /* Argument UNQUALIFIED_TYPE.  */
+	     enum gcc_qualifiers)	      /* Argument QUALIFIERS.  */
+
+/* Build a complex type given its element type.  */
+
+GCC_METHOD1 (gcc_type, build_complex_type,
+	     gcc_type)			  /* Argument ELEMENT_TYPE.  */
+
+/* Build a vector type given its element type and number of
+   elements.  */
+
+GCC_METHOD2 (gcc_type, build_vector_type,
+	     gcc_type,			  /* Argument ELEMENT_TYPE.  */
+	     int)			  /* Argument NUM_ELEMENTS.  */
+
+/* Build a constant.  NAME is the constant's name and VALUE is its
+   value.  FILENAME and LINE_NUMBER refer to the type's source
+   location.  If this is not known, FILENAME can be NULL and
+   LINE_NUMBER can be 0.  */
+
+GCC_METHOD5 (int /* bool */, build_constant,
+	     gcc_type,		  /* Argument TYPE.  */
+	     const char *,	  /* Argument NAME.  */
+	     unsigned long,	  /* Argument VALUE.  */
+	     const char *,	  /* Argument FILENAME.  */
+	     unsigned int)	  /* Argument LINE_NUMBER.  */
+
+/* Emit an error and return an error type object.  */
+
+GCC_METHOD1 (gcc_type, error,
+	     const char *)		 /* Argument MESSAGE.  */
diff --git a/include/gcc-c-interface.h b/include/gcc-c-interface.h
new file mode 100644
index 0000000..25ef62f
--- /dev/null
+++ b/include/gcc-c-interface.h
@@ -0,0 +1,220 @@
+/* Interface between GCC C FE and GDB
+
+   Copyright (C) 2014 Free Software Foundation, Inc.
+
+   This file is part of GCC.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#ifndef GCC_C_INTERFACE_H
+#define GCC_C_INTERFACE_H
+
+#include "gcc-interface.h"
+
+/* This header defines the interface to the GCC API.  It must be both
+   valid C and valid C++, because it is included by both programs.  */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Forward declaration.  */
+
+struct gcc_c_context;
+
+/*
+ * Definitions and declarations for the C front end.
+ */
+
+/* Defined versions of the C front-end API.  */
+
+enum gcc_c_api_version
+{
+  GCC_C_FE_VERSION_0 = 0
+};
+
+/* Qualifiers.  */
+
+enum gcc_qualifiers
+{
+  GCC_QUALIFIER_CONST = 1,
+  GCC_QUALIFIER_VOLATILE = 2,
+  GCC_QUALIFIER_RESTRICT = 4
+};
+
+/* This enumerates the kinds of decls that GDB can create.  */
+
+enum gcc_c_symbol_kind
+{
+  /* A function.  */
+
+  GCC_C_SYMBOL_FUNCTION,
+
+  /* A variable.  */
+
+  GCC_C_SYMBOL_VARIABLE,
+
+  /* A typedef.  */
+
+  GCC_C_SYMBOL_TYPEDEF,
+
+  /* A label.  */
+
+  GCC_C_SYMBOL_LABEL
+};
+
+/* This enumerates the types of symbols that GCC might request from
+   GDB.  */
+
+enum gcc_c_oracle_request
+{
+  /* An ordinary symbol -- a variable, function, typedef, or enum
+     constant.  */
+
+  GCC_C_ORACLE_SYMBOL,
+
+  /* A struct, union, or enum tag.  */
+
+  GCC_C_ORACLE_TAG,
+
+  /* A label.  */
+
+  GCC_C_ORACLE_LABEL
+};
+
+/* The type of the function called by GCC to ask GDB for a symbol's
+   definition.  DATUM is an arbitrary value supplied when the oracle
+   function is registered.  CONTEXT is the GCC context in which the
+   request is being made.  REQUEST specifies what sort of symbol is
+   being requested, and IDENTIFIER is the name of the symbol.  */
+
+typedef void gcc_c_oracle_function (void *datum,
+				    struct gcc_c_context *context,
+				    enum gcc_c_oracle_request request,
+				    const char *identifier);
+
+/* The type of the function called by GCC to ask GDB for a symbol's
+   address.  This should return 0 if the address is not known.  */
+
+typedef gcc_address gcc_c_symbol_address_function (void *datum,
+						   struct gcc_c_context *ctxt,
+						   const char *identifier);
+
+/* An array of types used for creating a function type.  */
+
+struct gcc_type_array
+{
+  /* Number of elements.  */
+
+  int n_elements;
+
+  /* The elements.  */
+
+  gcc_type *elements;
+};
+
+/* The vtable used by the C front end.  */
+
+struct gcc_c_fe_vtable
+{
+  /* The version of the C interface.  The value is one of the
+     gcc_c_api_version constants.  */
+
+  unsigned int c_version;
+
+  /* Set the callbacks for this context.
+
+     The binding oracle is called whenever the C parser needs to look
+     up a symbol.  This gives the caller a chance to lazily
+     instantiate symbols using other parts of the gcc_c_fe_interface
+     API.
+
+     The address oracle is called whenever the C parser needs to look
+     up a symbol.  This is only called for symbols not provided by the
+     symbol oracle -- that is, just built-in functions where GCC
+     provides the declaration.
+
+     DATUM is an arbitrary piece of data that is passed back verbatim
+     to the callbakcs in requests.  */
+
+  void (*set_callbacks) (struct gcc_c_context *self,
+			 gcc_c_oracle_function *binding_oracle,
+			 gcc_c_symbol_address_function *address_oracle,
+			 void *datum);
+
+#define GCC_METHOD0(R, N) \
+  R (*N) (struct gcc_c_context *);
+#define GCC_METHOD1(R, N, A) \
+  R (*N) (struct gcc_c_context *, A);
+#define GCC_METHOD2(R, N, A, B) \
+  R (*N) (struct gcc_c_context *, A, B);
+#define GCC_METHOD3(R, N, A, B, C) \
+  R (*N) (struct gcc_c_context *, A, B, C);
+#define GCC_METHOD4(R, N, A, B, C, D) \
+  R (*N) (struct gcc_c_context *, A, B, C, D);
+#define GCC_METHOD5(R, N, A, B, C, D, E) \
+  R (*N) (struct gcc_c_context *, A, B, C, D, E);
+#define GCC_METHOD7(R, N, A, B, C, D, E, F, G) \
+  R (*N) (struct gcc_c_context *, A, B, C, D, E, F, G);
+
+#include "gcc-c-fe.def"
+
+#undef GCC_METHOD0
+#undef GCC_METHOD1
+#undef GCC_METHOD2
+#undef GCC_METHOD3
+#undef GCC_METHOD4
+#undef GCC_METHOD5
+#undef GCC_METHOD7
+
+};
+
+/* The C front end object.  */
+
+struct gcc_c_context
+{
+  /* Base class.  */
+
+  struct gcc_base_context base;
+
+  /* Our vtable.  This is a separate field because this is simpler
+     than implementing a vtable inheritance scheme in C.  */
+
+  const struct gcc_c_fe_vtable *c_ops;
+};
+
+/* The name of the .so that the compiler builds.  We dlopen this
+   later.  */
+
+#define GCC_C_FE_LIBCC libcc1.so
+
+/* The compiler exports a single initialization function.  This macro
+   holds its name as a symbol.  */
+
+#define GCC_C_FE_CONTEXT gcc_c_fe_context
+
+/* The type of the initialization function.  The caller passes in the
+   desired base version and desired C-specific version.  If the
+   request can be satisfied, a compatible gcc_context object will be
+   returned.  Otherwise, the function returns NULL.  */
+
+typedef struct gcc_c_context *gcc_c_fe_context_function
+    (enum gcc_base_api_version,
+     enum gcc_c_api_version);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* GCC_C_INTERFACE_H */
diff --git a/include/gcc-interface.h b/include/gcc-interface.h
new file mode 100644
index 0000000..34010f2
--- /dev/null
+++ b/include/gcc-interface.h
@@ -0,0 +1,127 @@
+/* Generic interface between GCC and GDB
+
+   Copyright (C) 2014 Free Software Foundation, Inc.
+
+   This file is part of GCC.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#ifndef GCC_INTERFACE_H
+#define GCC_INTERFACE_H
+
+/* This header defines the interface to the GCC API.  It must be both
+   valid C and valid C++, because it is included by both programs.  */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Opaque typedefs for objects passed through the interface.  */
+
+typedef unsigned long long gcc_type;
+typedef unsigned long long gcc_decl;
+
+/* An address in the inferior.  */
+
+typedef unsigned long long gcc_address;
+
+/* Forward declaration.  */
+
+struct gcc_base_context;
+
+/* Defined versions of the generic API.  */
+
+enum gcc_base_api_version
+{
+  GCC_FE_VERSION_0 = 0
+};
+
+/* The operations defined by the GCC base API.  This is the vtable for
+   the real context structure which is passed around.
+
+   The "base" API is concerned with basics shared by all compiler
+   front ends: setting command-line arguments, the file names, etc.
+
+   Front-end-specific interfaces inherit from this one.  */
+
+struct gcc_base_vtable
+{
+  /* The actual version implemented in this interface.  This field can
+     be relied on not to move, so users can always check it if they
+     desire.  The value is one of the gcc_base_api_version constants.
+  */
+
+  unsigned int version;
+
+  /* Set the compiler's command-line options for the next compilation.
+     TRIPLET_REGEXP is a regular expression that is used to match the
+     configury triplet prefix to the compiler.
+     The arguments are copied by GCC.  ARGV need not be
+     NULL-terminated.  The arguments must be set separately for each
+     compilation; that is, after a compile is requested, the
+     previously-set arguments cannot be reused.
+
+     This returns NULL on success.  On failure, returns a malloc()d
+     error message.  The caller is responsible for freeing it.  */
+
+  char *(*set_arguments) (struct gcc_base_context *self,
+			  const char *triplet_regexp,
+			  int argc, char **argv);
+
+  /* Set the file name of the program to compile.  The string is
+     copied by the method implementation, but the caller must
+     guarantee that the file exists through the compilation.  */
+
+  void (*set_source_file) (struct gcc_base_context *self, const char *file);
+
+  /* Set a callback to use for printing error messages.  DATUM is
+     passed through to the callback unchanged.  */
+
+  void (*set_print_callback) (struct gcc_base_context *self,
+			      void (*print_function) (void *datum,
+						      const char *message),
+			      void *datum);
+
+  /* Perform the compilation.  FILENAME is the name of the resulting
+     object file.  VERBOSE can be set to cause GCC to print some
+     information as it works.  Returns true on success, false on
+     error.  */
+
+  int /* bool */ (*compile) (struct gcc_base_context *self,
+			     const char *filename,
+			     int /* bool */ verbose);
+
+  /* Destroy this object.  */
+
+  void (*destroy) (struct gcc_base_context *self);
+};
+
+/* The GCC object.  */
+
+struct gcc_base_context
+{
+  /* The virtual table.  */
+
+  const struct gcc_base_vtable *ops;
+};
+
+/* The name of the dummy wrapper function generated by gdb.  */
+
+#define GCC_FE_WRAPPER_FUNCTION "_gdb_expr"
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* GCC_INTERFACE_H */

^ permalink raw reply	[flat|nested] 35+ messages in thread

* [PATCH v3 08/14] introduce call_function_by_hand_dummy
  2014-11-01 21:45 [PATCH v3 00/14] let gdb reuse gcc's C compiler Jan Kratochvil
                   ` (3 preceding siblings ...)
  2014-11-01 21:46 ` [PATCH v3 03/14] add some missing ops to DWARF assembler Jan Kratochvil
@ 2014-11-01 21:46 ` Jan Kratochvil
  2014-11-01 21:46 ` [PATCH v3 06/14] add infcall_mmap and gcc_target_options gdbarch methods Jan Kratochvil
                   ` (11 subsequent siblings)
  16 siblings, 0 replies; 35+ messages in thread
From: Jan Kratochvil @ 2014-11-01 21:46 UTC (permalink / raw)
  To: gdb-patches

This provides a variant of call_function_by_hand that allows the dummy
frame destructor to be set.  This is used by the compiler code to
manage some resources when calling the gdb-generated inferior
function.

2014-10-07  Jan Kratochvil  <jan.kratochvil@redhat.com>

	* infcall.h (call_function_by_hand_dummy): Declare.
	* infcall.c (call_function_by_hand): Use
	call_function_by_hand_dummy.
	(call_function_by_hand_dummy): Rename from call_function_by_hand.
	Add arguments.  Register a destructor.
---
 gdb/ChangeLog |    8 ++++++++
 gdb/infcall.c |   16 +++++++++++++++-
 gdb/infcall.h |   11 +++++++++++
 3 files changed, 34 insertions(+), 1 deletion(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index f66a995..abdf3f2 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,11 @@
+2014-10-07  Jan Kratochvil  <jan.kratochvil@redhat.com>
+
+	* infcall.h (call_function_by_hand_dummy): Declare.
+	* infcall.c (call_function_by_hand): Use
+	call_function_by_hand_dummy.
+	(call_function_by_hand_dummy): Rename from call_function_by_hand.
+	Add arguments.  Register a destructor.
+
 2014-10-07  Tom Tromey  <tromey@redhat.com>
 	    Jan Kratochvil  <jan.kratochvil@redhat.com>
 
diff --git a/gdb/infcall.c b/gdb/infcall.c
index bbac693..74e90d4 100644
--- a/gdb/infcall.c
+++ b/gdb/infcall.c
@@ -455,6 +455,14 @@ cleanup_delete_std_terminate_breakpoint (void *ignore)
   delete_std_terminate_breakpoint ();
 }
 
+/* See infcall.h.  */
+
+struct value *
+call_function_by_hand (struct value *function, int nargs, struct value **args)
+{
+  return call_function_by_hand_dummy (function, nargs, args, NULL, NULL);
+}
+
 /* All this stuff with a dummy frame may seem unnecessarily complicated
    (why not just save registers in GDB?).  The purpose of pushing a dummy
    frame which looks just like a real frame is so that if you call a
@@ -474,7 +482,10 @@ cleanup_delete_std_terminate_breakpoint (void *ignore)
    ARGS is modified to contain coerced values.  */
 
 struct value *
-call_function_by_hand (struct value *function, int nargs, struct value **args)
+call_function_by_hand_dummy (struct value *function,
+			     int nargs, struct value **args,
+			     call_function_by_hand_dummy_dtor_ftype *dummy_dtor,
+			     void *dummy_dtor_data)
 {
   CORE_ADDR sp;
   struct type *values_type, *target_values_type;
@@ -831,6 +842,9 @@ call_function_by_hand (struct value *function, int nargs, struct value **args)
      caller (and identify the dummy-frame) onto the dummy-frame
      stack.  */
   dummy_frame_push (caller_state, &dummy_id, inferior_ptid);
+  if (dummy_dtor != NULL)
+    register_dummy_frame_dtor (dummy_id, inferior_ptid,
+			       dummy_dtor, dummy_dtor_data);
 
   /* Discard both inf_status and caller_state cleanups.
      From this point on we explicitly restore the associated state
diff --git a/gdb/infcall.h b/gdb/infcall.h
index c6dcdc3..f895e33 100644
--- a/gdb/infcall.h
+++ b/gdb/infcall.h
@@ -38,4 +38,15 @@ extern CORE_ADDR find_function_addr (struct value *function,
 extern struct value *call_function_by_hand (struct value *function, int nargs,
 					    struct value **args);
 
+/* Similar to call_function_by_hand and additional call
+   register_dummy_frame_dtor with DUMMY_DTOR and DUMMY_DTOR_DATA for the
+   created inferior call dummy frame.  */
+
+typedef void (call_function_by_hand_dummy_dtor_ftype) (void *data);
+extern struct value *
+  call_function_by_hand_dummy (struct value *function, int nargs,
+			       struct value **args,
+			     call_function_by_hand_dummy_dtor_ftype *dummy_dtor,
+			       void *dummy_dtor_data);
+
 #endif

^ permalink raw reply	[flat|nested] 35+ messages in thread

* [PATCH v3 12/14] add linux_infcall_mmap
  2014-11-01 21:45 [PATCH v3 00/14] let gdb reuse gcc's C compiler Jan Kratochvil
                   ` (7 preceding siblings ...)
  2014-11-01 21:46 ` [PATCH v3 02/14] add gcc/gdb interface files Jan Kratochvil
@ 2014-11-01 21:47 ` Jan Kratochvil
  2014-11-11 16:43   ` Pedro Alves
  2014-11-01 21:47 ` [PATCH v3 10/14] make dwarf_expr_frame_base_1 public Jan Kratochvil
                   ` (7 subsequent siblings)
  16 siblings, 1 reply; 35+ messages in thread
From: Jan Kratochvil @ 2014-11-01 21:47 UTC (permalink / raw)
  To: gdb-patches

This implements the new gdbarch "infcall_mmap" method for Linux.

2014-10-07  Jan Kratochvil  <jan.kratochvil@redhat.com>

	* linux-tdep.c (linux_infcall_mmap): New function.
	(linux_init_abi): Add it to gdbarch.
---
 gdb/ChangeLog    |    5 +++++
 gdb/linux-tdep.c |   46 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 51 insertions(+)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 329e42b..52b048e 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,10 @@
 2014-10-07  Jan Kratochvil  <jan.kratochvil@redhat.com>
 
+	* linux-tdep.c (linux_infcall_mmap): New function.
+	(linux_init_abi): Add it to gdbarch.
+
+2014-10-07  Jan Kratochvil  <jan.kratochvil@redhat.com>
+
 	* dwarf2loc.h (dwarf2_reg_to_regnum_or_error): Declare.
 	* dwarf2loc.c (dwarf2_reg_to_regnum_or_error): Rename from
 	translate_register.  Now public.
diff --git a/gdb/linux-tdep.c b/gdb/linux-tdep.c
index ffc3e87..e93ba61 100644
--- a/gdb/linux-tdep.c
+++ b/gdb/linux-tdep.c
@@ -33,6 +33,9 @@
 #include "arch-utils.h"
 #include "gdb_obstack.h"
 #include "observer.h"
+#include "objfiles.h"
+#include "infcall.h"
+#include <sys/mman.h>
 
 #include <ctype.h>
 
@@ -1921,6 +1924,48 @@ linux_vsyscall_range (struct gdbarch *gdbarch, struct mem_range *range)
   return 1;
 }
 
+/* See gdbarch.sh 'infcall_mmap'.  */
+
+static CORE_ADDR
+linux_infcall_mmap (CORE_ADDR size, unsigned prot)
+{
+  struct objfile *objf;
+  /* Do there still exist any Linux systems without "mmap64"?
+     "mmap" uses 64-bit off_t on x86_64 and 32-bit off_t on i386 and x32.  */
+  struct value *mmap_val = find_function_in_inferior ("mmap64", &objf);
+  struct value *addr_val;
+  struct gdbarch *gdbarch = get_objfile_arch (objf);
+  CORE_ADDR retval;
+  enum
+    {
+      ARG_ADDR, ARG_LENGTH, ARG_PROT, ARG_FLAGS, ARG_FD, ARG_OFFSET, ARG_MAX
+    };
+  struct value *arg[ARG_MAX];
+
+  arg[ARG_ADDR] = value_from_pointer (builtin_type (gdbarch)->builtin_data_ptr,
+				      0);
+  /* Assuming sizeof (unsigned long) == sizeof (size_t).  */
+  arg[ARG_LENGTH] = value_from_ulongest
+		    (builtin_type (gdbarch)->builtin_unsigned_long, size);
+  gdb_assert ((prot & ~7) == 0);
+  arg[ARG_PROT] = value_from_longest (builtin_type (gdbarch)->builtin_int,
+				      0
+				      | ((prot & 4) != 0 ? PROT_READ : 0)
+				      | ((prot & 2) != 0 ? PROT_WRITE : 0)
+				      | ((prot & 1) != 0 ? PROT_EXEC : 0));
+  arg[ARG_FLAGS] = value_from_longest (builtin_type (gdbarch)->builtin_int,
+				       MAP_PRIVATE | MAP_ANONYMOUS);
+  arg[ARG_FD] = value_from_longest (builtin_type (gdbarch)->builtin_int, -1);
+  arg[ARG_OFFSET] = value_from_longest (builtin_type (gdbarch)->builtin_int64,
+					0);
+  addr_val = call_function_by_hand (mmap_val, ARG_MAX, arg);
+  retval = value_as_address (addr_val);
+  if (retval == (CORE_ADDR) -1)
+    error (_("Failed inferior mmap call for %s bytes, errno is changed."),
+	   pulongest (size));
+  return retval;
+}
+
 /* To be called from the various GDB_OSABI_LINUX handlers for the
    various GNU/Linux architectures and machine types.  */
 
@@ -1939,6 +1984,7 @@ linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
   set_gdbarch_gdb_signal_to_target (gdbarch,
 				    linux_gdb_signal_to_target);
   set_gdbarch_vsyscall_range (gdbarch, linux_vsyscall_range);
+  set_gdbarch_infcall_mmap (gdbarch, linux_infcall_mmap);
 }
 
 /* Provide a prototype to silence -Wmissing-prototypes.  */

^ permalink raw reply	[flat|nested] 35+ messages in thread

* [PATCH v3 13/14] add s390_gcc_target_options
  2014-11-01 21:45 [PATCH v3 00/14] let gdb reuse gcc's C compiler Jan Kratochvil
                   ` (11 preceding siblings ...)
  2014-11-01 21:47 ` [PATCH v3 11/14] export dwarf2_reg_to_regnum_or_error Jan Kratochvil
@ 2014-11-01 21:47 ` Jan Kratochvil
  2014-11-01 21:48 ` [PATCH v3 14/14] the "compile" command Jan Kratochvil
                   ` (3 subsequent siblings)
  16 siblings, 0 replies; 35+ messages in thread
From: Jan Kratochvil @ 2014-11-01 21:47 UTC (permalink / raw)
  To: gdb-patches

This adds s390_gcc_target_options, an implementation of the new
"gcc_target_options" gdbarch method.  This was needed because the
default implementation of the method doesn't work properly for S390,
as this architecture needs "-m31" rather than "-m32".

2014-10-07  Jan Kratochvil  <jan.kratochvil@redhat.com>

	* s390-linux-tdep.c (s390_gcc_target_options): New function.
	(s390_gdbarch_init): Add it to gdbarch.
---
 gdb/ChangeLog         |    5 +++++
 gdb/s390-linux-tdep.c |    9 +++++++++
 2 files changed, 14 insertions(+)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 52b048e..62ae948 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,10 @@
 2014-10-07  Jan Kratochvil  <jan.kratochvil@redhat.com>
 
+	* s390-linux-tdep.c (s390_gcc_target_options): New function.
+	(s390_gdbarch_init): Add it to gdbarch.
+
+2014-10-07  Jan Kratochvil  <jan.kratochvil@redhat.com>
+
 	* linux-tdep.c (linux_infcall_mmap): New function.
 	(linux_init_abi): Add it to gdbarch.
 
diff --git a/gdb/s390-linux-tdep.c b/gdb/s390-linux-tdep.c
index abd2e40..ea22579 100644
--- a/gdb/s390-linux-tdep.c
+++ b/gdb/s390-linux-tdep.c
@@ -2808,6 +2808,14 @@ s390_address_class_name_to_type_flags (struct gdbarch *gdbarch,
     return 0;
 }
 
+/* Implement gdbarch_gcc_target_options.  GCC does not know "-m32".  */
+
+static char *
+s390_gcc_target_options (struct gdbarch *gdbarch)
+{
+  return xstrdup ("-m31");
+}
+
 /* Implementation of `gdbarch_stap_is_single_operand', as defined in
    gdbarch.h.  */
 
@@ -3104,6 +3112,7 @@ s390_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
     {
     case ABI_LINUX_S390:
       set_gdbarch_addr_bits_remove (gdbarch, s390_addr_bits_remove);
+      set_gdbarch_gcc_target_options (gdbarch, s390_gcc_target_options);
       set_solib_svr4_fetch_link_map_offsets
 	(gdbarch, svr4_ilp32_fetch_link_map_offsets);
 

^ permalink raw reply	[flat|nested] 35+ messages in thread

* [PATCH v3 11/14] export dwarf2_reg_to_regnum_or_error
  2014-11-01 21:45 [PATCH v3 00/14] let gdb reuse gcc's C compiler Jan Kratochvil
                   ` (10 preceding siblings ...)
  2014-11-01 21:47 ` [PATCH v3 09/14] split dwarf2_fetch_cfa_info from dwarf2_compile_expr_to_ax Jan Kratochvil
@ 2014-11-01 21:47 ` Jan Kratochvil
  2014-11-01 21:47 ` [PATCH v3 13/14] add s390_gcc_target_options Jan Kratochvil
                   ` (4 subsequent siblings)
  16 siblings, 0 replies; 35+ messages in thread
From: Jan Kratochvil @ 2014-11-01 21:47 UTC (permalink / raw)
  To: gdb-patches

This exports a utility function, dwarf2_reg_to_regnum_or_error, that
was previously private to dwarf2loc.c.

2014-10-07  Jan Kratochvil  <jan.kratochvil@redhat.com>

	* dwarf2loc.h (dwarf2_reg_to_regnum_or_error): Declare.
	* dwarf2loc.c (dwarf2_reg_to_regnum_or_error): Rename from
	translate_register.  Now public.
	(dwarf2_compile_expr_to_ax): Update.
---
 gdb/ChangeLog   |    7 +++++++
 gdb/dwarf2loc.c |   18 +++++++-----------
 gdb/dwarf2loc.h |    8 ++++++++
 3 files changed, 22 insertions(+), 11 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 187f20c..329e42b 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,10 @@
+2014-10-07  Jan Kratochvil  <jan.kratochvil@redhat.com>
+
+	* dwarf2loc.h (dwarf2_reg_to_regnum_or_error): Declare.
+	* dwarf2loc.c (dwarf2_reg_to_regnum_or_error): Rename from
+	translate_register.  Now public.
+	(dwarf2_compile_expr_to_ax): Update.
+
 2014-10-07  Tom Tromey  <tromey@redhat.com>
 	    Jan Kratochvil  <jan.kratochvil@redhat.com>
 
diff --git a/gdb/dwarf2loc.c b/gdb/dwarf2loc.c
index 35ebd9b..3a0c5ce 100644
--- a/gdb/dwarf2loc.c
+++ b/gdb/dwarf2loc.c
@@ -2704,14 +2704,10 @@ unimplemented (unsigned int op)
 	   op);
 }
 
-/* A helper function to convert a DWARF register to an arch register.
-   ARCH is the architecture.
-   DWARF_REG is the register.
-   This will throw an exception if the DWARF register cannot be
-   translated to an architecture register.  */
+/* See dwarf2loc.h.  */
 
-static int
-translate_register (struct gdbarch *arch, int dwarf_reg)
+int
+dwarf2_reg_to_regnum_or_error (struct gdbarch *arch, int dwarf_reg)
 {
   int reg = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_reg);
   if (reg == -1)
@@ -2962,14 +2958,14 @@ dwarf2_compile_expr_to_ax (struct agent_expr *expr, struct axs_value *loc,
 	case DW_OP_reg30:
 	case DW_OP_reg31:
 	  dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
-	  loc->u.reg = translate_register (arch, op - DW_OP_reg0);
+	  loc->u.reg = dwarf2_reg_to_regnum_or_error (arch, op - DW_OP_reg0);
 	  loc->kind = axs_lvalue_register;
 	  break;
 
 	case DW_OP_regx:
 	  op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
 	  dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
-	  loc->u.reg = translate_register (arch, reg);
+	  loc->u.reg = dwarf2_reg_to_regnum_or_error (arch, reg);
 	  loc->kind = axs_lvalue_register;
 	  break;
 
@@ -3032,7 +3028,7 @@ dwarf2_compile_expr_to_ax (struct agent_expr *expr, struct axs_value *loc,
 	case DW_OP_breg30:
 	case DW_OP_breg31:
 	  op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
-	  i = translate_register (arch, op - DW_OP_breg0);
+	  i = dwarf2_reg_to_regnum_or_error (arch, op - DW_OP_breg0);
 	  ax_reg (expr, i);
 	  if (offset != 0)
 	    {
@@ -3044,7 +3040,7 @@ dwarf2_compile_expr_to_ax (struct agent_expr *expr, struct axs_value *loc,
 	  {
 	    op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
 	    op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
-	    i = translate_register (arch, reg);
+	    i = dwarf2_reg_to_regnum_or_error (arch, reg);
 	    ax_reg (expr, i);
 	    if (offset != 0)
 	      {
diff --git a/gdb/dwarf2loc.h b/gdb/dwarf2loc.h
index 082ccfa..76fb45b4 100644
--- a/gdb/dwarf2loc.h
+++ b/gdb/dwarf2loc.h
@@ -222,4 +222,12 @@ extern struct call_site_chain *call_site_find_chain (struct gdbarch *gdbarch,
 						     CORE_ADDR caller_pc,
 						     CORE_ADDR callee_pc);
 
+/* A helper function to convert a DWARF register to an arch register.
+   ARCH is the architecture.
+   DWARF_REG is the register.
+   This will throw an exception if the DWARF register cannot be
+   translated to an architecture register.  */
+
+extern int dwarf2_reg_to_regnum_or_error (struct gdbarch *arch, int dwarf_reg);
+
 #endif /* dwarf2loc.h */

^ permalink raw reply	[flat|nested] 35+ messages in thread

* [PATCH v3 10/14] make dwarf_expr_frame_base_1 public
  2014-11-01 21:45 [PATCH v3 00/14] let gdb reuse gcc's C compiler Jan Kratochvil
                   ` (8 preceding siblings ...)
  2014-11-01 21:47 ` [PATCH v3 12/14] add linux_infcall_mmap Jan Kratochvil
@ 2014-11-01 21:47 ` Jan Kratochvil
  2014-11-01 21:47 ` [PATCH v3 09/14] split dwarf2_fetch_cfa_info from dwarf2_compile_expr_to_ax Jan Kratochvil
                   ` (6 subsequent siblings)
  16 siblings, 0 replies; 35+ messages in thread
From: Jan Kratochvil @ 2014-11-01 21:47 UTC (permalink / raw)
  To: gdb-patches

From: Tom Tromey <tromey@redhat.com>

This exports dwarf_expr_frame_base_1 so that other code can use it.

2014-10-07  Tom Tromey  <tromey@redhat.com>
	    Jan Kratochvil  <jan.kratochvil@redhat.com>

	* dwarf2loc.c (dwarf_expr_frame_base_1): Remove declaration.
	(dwarf_expr_frame_base): Update caller.
	(dwarf_expr_frame_base_1): Rename to ...
	(func_get_frame_base_dwarf_block): ... this and make it public.
	(dwarf2_compile_expr_to_ax, locexpr_describe_location_piece): Update
	callers.
	* dwarf2loc.h (func_get_frame_base_dwarf_block): New declaration.
---
 gdb/ChangeLog   |   11 +++++++++++
 gdb/dwarf2loc.c |   23 +++++++++++------------
 gdb/dwarf2loc.h |   12 ++++++++++++
 3 files changed, 34 insertions(+), 12 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index d0498bf..187f20c 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,4 +1,15 @@
 2014-10-07  Tom Tromey  <tromey@redhat.com>
+	    Jan Kratochvil  <jan.kratochvil@redhat.com>
+
+	* dwarf2loc.c (dwarf_expr_frame_base_1): Remove declaration.
+	(dwarf_expr_frame_base): Update caller.
+	(dwarf_expr_frame_base_1): Rename to ...
+	(func_get_frame_base_dwarf_block): ... this and make it public.
+	(dwarf2_compile_expr_to_ax, locexpr_describe_location_piece): Update
+	callers.
+	* dwarf2loc.h (func_get_frame_base_dwarf_block): New declaration.
+
+2014-10-07  Tom Tromey  <tromey@redhat.com>
 
 	* dwarf2loc.c (dwarf2_compile_expr_to_ax) <DW_OP_call_frame_cfa>:
 	Update.
diff --git a/gdb/dwarf2loc.c b/gdb/dwarf2loc.c
index 006ee3d..35ebd9b 100644
--- a/gdb/dwarf2loc.c
+++ b/gdb/dwarf2loc.c
@@ -40,9 +40,6 @@
 
 extern int dwarf2_always_disassemble;
 
-static void dwarf_expr_frame_base_1 (struct symbol *framefunc, CORE_ADDR pc,
-				     const gdb_byte **start, size_t *length);
-
 static const struct dwarf_expr_context_funcs dwarf_expr_ctx_funcs;
 
 static struct value *dwarf2_evaluate_loc_desc_full (struct type *type,
@@ -365,9 +362,9 @@ dwarf_expr_frame_base (void *baton, const gdb_byte **start, size_t * length)
      something has gone wrong.  */
   gdb_assert (framefunc != NULL);
 
-  dwarf_expr_frame_base_1 (framefunc,
-			   get_frame_address_in_block (debaton->frame),
-			   start, length);
+  func_get_frame_base_dwarf_block (framefunc,
+				   get_frame_address_in_block (debaton->frame),
+				   start, length);
 }
 
 /* Implement find_frame_base_location method for LOC_BLOCK functions using
@@ -411,9 +408,11 @@ const struct symbol_block_ops dwarf2_block_frame_base_loclist_funcs =
   loclist_find_frame_base_location
 };
 
-static void
-dwarf_expr_frame_base_1 (struct symbol *framefunc, CORE_ADDR pc,
-			 const gdb_byte **start, size_t *length)
+/* See dwarf2loc.h.  */
+
+void
+func_get_frame_base_dwarf_block (struct symbol *framefunc, CORE_ADDR pc,
+				 const gdb_byte **start, size_t *length)
 {
   if (SYMBOL_BLOCK_OPS (framefunc) != NULL)
     {
@@ -3071,8 +3070,8 @@ dwarf2_compile_expr_to_ax (struct agent_expr *expr, struct axs_value *loc,
 	    if (!framefunc)
 	      error (_("No function found for block"));
 
-	    dwarf_expr_frame_base_1 (framefunc, expr->scope,
-				     &datastart, &datalen);
+	    func_get_frame_base_dwarf_block (framefunc, expr->scope,
+					     &datastart, &datalen);
 
 	    op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
 	    dwarf2_compile_expr_to_ax (expr, loc, arch, addr_size, datastart,
@@ -3568,7 +3567,7 @@ locexpr_describe_location_piece (struct symbol *symbol, struct ui_file *stream,
 	error (_("No function found for block for symbol \"%s\"."),
 	       SYMBOL_PRINT_NAME (symbol));
 
-      dwarf_expr_frame_base_1 (framefunc, addr, &base_data, &base_size);
+      func_get_frame_base_dwarf_block (framefunc, addr, &base_data, &base_size);
 
       if (base_data[0] >= DW_OP_breg0 && base_data[0] <= DW_OP_breg31)
 	{
diff --git a/gdb/dwarf2loc.h b/gdb/dwarf2loc.h
index 96925e4..082ccfa 100644
--- a/gdb/dwarf2loc.h
+++ b/gdb/dwarf2loc.h
@@ -80,6 +80,18 @@ extern const gdb_byte *dwarf2_fetch_constant_bytes (sect_offset,
 struct type *dwarf2_get_die_type (cu_offset die_offset,
 				  struct dwarf2_per_cu_data *per_cu);
 
+/* Find the frame base information for FRAMEFUNC at PC.  START is an
+   out parameter which is set to point to the DWARF expression to
+   compute.  LENGTH is an out parameter which is set to the length of
+   the DWARF expression.  This throws an exception on error or if an
+   expression is not found; the returned length will never be
+   zero.  */
+
+extern void func_get_frame_base_dwarf_block (struct symbol *framefunc,
+					     CORE_ADDR pc,
+					     const gdb_byte **start,
+					     size_t *length);
+
 /* Evaluate a location description, starting at DATA and with length
    SIZE, to find the current location of variable of TYPE in the context
    of FRAME.  */

^ permalink raw reply	[flat|nested] 35+ messages in thread

* [PATCH v3 09/14] split dwarf2_fetch_cfa_info from dwarf2_compile_expr_to_ax
  2014-11-01 21:45 [PATCH v3 00/14] let gdb reuse gcc's C compiler Jan Kratochvil
                   ` (9 preceding siblings ...)
  2014-11-01 21:47 ` [PATCH v3 10/14] make dwarf_expr_frame_base_1 public Jan Kratochvil
@ 2014-11-01 21:47 ` Jan Kratochvil
  2014-11-01 21:47 ` [PATCH v3 11/14] export dwarf2_reg_to_regnum_or_error Jan Kratochvil
                   ` (5 subsequent siblings)
  16 siblings, 0 replies; 35+ messages in thread
From: Jan Kratochvil @ 2014-11-01 21:47 UTC (permalink / raw)
  To: gdb-patches

From: Tom Tromey <tromey@redhat.com>

This removes dwarf2_compile_expr_to_ax, replacing it with a utility
function that fetches the CFA data and adding the code to actually
compile to an agent expression directly into
dwarf2_compile_expr_to_ax.  This refactoring lets a later patch reuse
the new dwarf2_fetch_cfa_info.

2014-10-07  Tom Tromey  <tromey@redhat.com>

	* dwarf2loc.c (dwarf2_compile_expr_to_ax) <DW_OP_call_frame_cfa>:
	Update.
	* dwarf2-frame.c (dwarf2_fetch_cfa_info): New function, based on
	dwarf2_compile_cfa_to_ax.
	(dwarf2_compile_cfa_to_ax): Remove.
	* dwarf2-frame.h (dwarf2_fetch_cfa_info): Declare.
	(dwarf2_compile_cfa_to_ax): Remove.
---
 gdb/ChangeLog      |   10 ++++++++++
 gdb/dwarf2-frame.c |   40 +++++++++++++++++++---------------------
 gdb/dwarf2-frame.h |   31 +++++++++++++++++++++----------
 gdb/dwarf2loc.c    |   30 ++++++++++++++++++++++++++++--
 4 files changed, 78 insertions(+), 33 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index abdf3f2..d0498bf 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,13 @@
+2014-10-07  Tom Tromey  <tromey@redhat.com>
+
+	* dwarf2loc.c (dwarf2_compile_expr_to_ax) <DW_OP_call_frame_cfa>:
+	Update.
+	* dwarf2-frame.c (dwarf2_fetch_cfa_info): New function, based on
+	dwarf2_compile_cfa_to_ax.
+	(dwarf2_compile_cfa_to_ax): Remove.
+	* dwarf2-frame.h (dwarf2_fetch_cfa_info): Declare.
+	(dwarf2_compile_cfa_to_ax): Remove.
+
 2014-10-07  Jan Kratochvil  <jan.kratochvil@redhat.com>
 
 	* infcall.h (call_function_by_hand_dummy): Declare.
diff --git a/gdb/dwarf2-frame.c b/gdb/dwarf2-frame.c
index 80e5903..3172d3a 100644
--- a/gdb/dwarf2-frame.c
+++ b/gdb/dwarf2-frame.c
@@ -882,11 +882,15 @@ dwarf2_frame_find_quirks (struct dwarf2_frame_state *fs,
 }
 \f
 
-void
-dwarf2_compile_cfa_to_ax (struct agent_expr *expr, struct axs_value *loc,
-			  struct gdbarch *gdbarch,
-			  CORE_ADDR pc,
-			  struct dwarf2_per_cu_data *data)
+/* See dwarf2-frame.h.  */
+
+int
+dwarf2_fetch_cfa_info (struct gdbarch *gdbarch, CORE_ADDR pc,
+		       struct dwarf2_per_cu_data *data,
+		       int *regnum_out, LONGEST *offset_out,
+		       CORE_ADDR *text_offset_out,
+		       const gdb_byte **cfa_start_out,
+		       const gdb_byte **cfa_end_out)
 {
   struct dwarf2_fde *fde;
   CORE_ADDR text_offset;
@@ -932,26 +936,20 @@ dwarf2_compile_cfa_to_ax (struct agent_expr *expr, struct axs_value *loc,
 	if (regnum == -1)
 	  error (_("Unable to access DWARF register number %d"),
 		 (int) fs.regs.cfa_reg); /* FIXME */
-	ax_reg (expr, regnum);
 
-	if (fs.regs.cfa_offset != 0)
-	  {
-	    if (fs.armcc_cfa_offsets_reversed)
-	      ax_const_l (expr, -fs.regs.cfa_offset);
-	    else
-	      ax_const_l (expr, fs.regs.cfa_offset);
-	    ax_simple (expr, aop_add);
-	  }
+	*regnum_out = regnum;
+	if (fs.armcc_cfa_offsets_reversed)
+	  *offset_out = -fs.regs.cfa_offset;
+	else
+	  *offset_out = fs.regs.cfa_offset;
+	return 1;
       }
-      break;
 
     case CFA_EXP:
-      ax_const_l (expr, text_offset);
-      dwarf2_compile_expr_to_ax (expr, loc, gdbarch, addr_size,
-				 fs.regs.cfa_exp,
-				 fs.regs.cfa_exp + fs.regs.cfa_exp_len,
-				 data);
-      break;
+      *text_offset_out = text_offset;
+      *cfa_start_out = fs.regs.cfa_exp;
+      *cfa_end_out = fs.regs.cfa_exp + fs.regs.cfa_exp_len;
+      return 0;
 
     default:
       internal_error (__FILE__, __LINE__, _("Unknown CFA rule."));
diff --git a/gdb/dwarf2-frame.h b/gdb/dwarf2-frame.h
index 0470cac..5ccf0c8 100644
--- a/gdb/dwarf2-frame.h
+++ b/gdb/dwarf2-frame.h
@@ -120,15 +120,26 @@ extern const struct frame_base *
 
 CORE_ADDR dwarf2_frame_cfa (struct frame_info *this_frame);
 
-/* Update the agent expression EXPR with code to compute the CFA for a
-   frame at PC.  GDBARCH is the architecture of the function at PC.
-   This function may call dwarf2_compile_expr_to_ax; DATA is passed
-   through to that function if needed.  */
-
-extern void dwarf2_compile_cfa_to_ax (struct agent_expr *expr,
-				      struct axs_value *loc,
-				      struct gdbarch *gdbarch,
-				      CORE_ADDR pc,
-				      struct dwarf2_per_cu_data *data);
+/* Find the CFA information for PC.
+
+   Return 1 if a register is used for the CFA, or 0 if another
+   expression is used.  Throw an exception on error.
+
+   GDBARCH is the architecture to use.
+   DATA is the per-CU data.
+
+   REGNUM_OUT is an out parameter that is set to the register number.
+   OFFSET_OUT is the offset to use from this register.
+   These are only filled in when 1 is returned.
+
+   TEXT_OFFSET_OUT, CFA_START_OUT, and CFA_END_OUT describe the CFA
+   in other cases.  These are only used when 0 is returned.  */
+
+extern int dwarf2_fetch_cfa_info (struct gdbarch *gdbarch, CORE_ADDR pc,
+				  struct dwarf2_per_cu_data *data,
+				  int *regnum_out, LONGEST *offset_out,
+				  CORE_ADDR *text_offset_out,
+				  const gdb_byte **cfa_start_out,
+				  const gdb_byte **cfa_end_out);
 
 #endif /* dwarf2-frame.h */
diff --git a/gdb/dwarf2loc.c b/gdb/dwarf2loc.c
index 71adc89..006ee3d 100644
--- a/gdb/dwarf2loc.c
+++ b/gdb/dwarf2loc.c
@@ -3279,8 +3279,34 @@ dwarf2_compile_expr_to_ax (struct agent_expr *expr, struct axs_value *loc,
 	  break;
 
 	case DW_OP_call_frame_cfa:
-	  dwarf2_compile_cfa_to_ax (expr, loc, arch, expr->scope, per_cu);
-	  loc->kind = axs_lvalue_memory;
+	  {
+	    int regnum;
+	    CORE_ADDR text_offset;
+	    LONGEST off;
+	    const gdb_byte *cfa_start, *cfa_end;
+
+	    if (dwarf2_fetch_cfa_info (arch, expr->scope, per_cu,
+				       &regnum, &off,
+				       &text_offset, &cfa_start, &cfa_end))
+	      {
+		/* Register.  */
+		ax_reg (expr, regnum);
+		if (off != 0)
+		  {
+		    ax_const_l (expr, off);
+		    ax_simple (expr, aop_add);
+		  }
+	      }
+	    else
+	      {
+		/* Another expression.  */
+		ax_const_l (expr, text_offset);
+		dwarf2_compile_expr_to_ax (expr, loc, arch, addr_size,
+					   cfa_start, cfa_end, per_cu);
+	      }
+
+	    loc->kind = axs_lvalue_memory;
+	  }
 	  break;
 
 	case DW_OP_GNU_push_tls_address:

^ permalink raw reply	[flat|nested] 35+ messages in thread

* [PATCH v3 14/14] the "compile" command
  2014-11-01 21:45 [PATCH v3 00/14] let gdb reuse gcc's C compiler Jan Kratochvil
                   ` (12 preceding siblings ...)
  2014-11-01 21:47 ` [PATCH v3 13/14] add s390_gcc_target_options Jan Kratochvil
@ 2014-11-01 21:48 ` Jan Kratochvil
  2014-11-02 16:03   ` Eli Zaretskii
                     ` (2 more replies)
  2014-11-01 21:52 ` [PATCH v3 00/14] let gdb reuse gcc's C compiler Jan Kratochvil
                   ` (2 subsequent siblings)
  16 siblings, 3 replies; 35+ messages in thread
From: Jan Kratochvil @ 2014-11-01 21:48 UTC (permalink / raw)
  To: gdb-patches

From: Tom Tromey <tromey@redhat.com>

This final patch adds the new "compile" command and subcommands, and
all the machinery needed to make it work.

A shared library supplied by gcc is used for all communications with
gcc.  Types and most aspects of symbols are provided directly by gdb
to the compiler using this library.

gdb provides some information about the user's code using plain text.
Macros are emitted this way, and DWARF location expressions (and
bounds for VLA) are compiled to C code.

This hybrid approach was taken because, on the one hand, it is better
to provide global declarations and such on demand; but on the other
hand, for local variables, translating DWARF location expressions to C
was much simpler than exporting a full compiler API to gdb -- the same
result, only easier to implement, understand, and debug.

In the ordinary mode, the user's expression is wrapped in a dummy
function.  After compilation, gdb inserts the resulting object code
into the inferior, then calls this function.

Access to local variables is provided by noting which registers are
used by location expressions, and passing a structure of register
values into the function.  Writes to registers are supported by
copying out these values after the function returns.

This approach was taken so that we could eventually implement other
more interesting features based on this same infrastructure; for
example, we're planning to investigate inferior-side breakpoint
conditions.

2014-10-07  Phil Muldoon  <pmuldoon@redhat.com>
	    Jan Kratochvil  <jan.kratochvil@redhat.com>
	    Tom Tromey  <tromey@redhat.com>

	* NEWS: Update.
	* symtab.h (struct symbol_computed_ops) <generate_c_location>: New
	field.
	* p-lang.c (pascal_language_defn): Update.
	* opencl-lang.c (opencl_language_defn): Update.
	* objc-lang.c (objc_language_defn): Update.
	* m2-lang.c (m2_language_defn): Update.
	* language.h (struct language_defn) <la_get_compile_instance,
	la_compute_program>: New fields.
	* language.c (unknown_language_defn, auto_language_defn)
	(local_language_defn): Update.
	* jv-lang.c (java_language_defn): Update.
	* go-lang.c (go_language_defn): Update.
	* f-lang.c (f_language_defn): Update.
	* dwarf2loc.h (dwarf2_compile_property_to_c): Declare.
	* dwarf2loc.c (dwarf2_compile_property_to_c)
	(locexpr_generate_c_location, loclist_generate_c_location): New
	functions.
	(dwarf2_locexpr_funcs, dwarf2_loclist_funcs): Update.
	* defs.h (enum compile_i_scope_types): New.
	(enum command_control_type) <compile_control>: New constant.
	(struct command_line) <control_u>: New field.
	* d-lang.c (d_language_defn): Update.
	* compile/compile.c: New file.
	* compile/compile-c-support.c: New file.
	* compile/compile-c-symbols.c: New file.
	* compile/compile-c-types.c: New file.
	* compile/compile.h: New file.
	* compile/compile-internal.h: New file.
	* compile/compile-loc2c.c: New file.
	* compile/compile-object-load.c: New file.
	* compile/compile-object-load.h: New file.
	* compile/compile-object-run.c: New file.
	* compile/compile-object-run.h: New file.
	* cli/cli-script.c (multi_line_command_p, print_command_lines)
	(execute_control_command, process_next_line)
	(recurse_read_control_structure): Handle compile_control.
	* c-lang.h (c_get_compile_context, c_compute_program): Declare.
	* c-lang.c (c_language_defn, cplus_language_defn)
	(asm_language_defn, minimal_language_defn): Update.
	* ada-lang.c (ada_language_defn): Update.
	* Makefile.in (SUBDIR_GCC_COMPILE_OBS, SUBDIR_GCC_COMPILE_SRCS):
	New variables.
	(SFILES): Add SUBDIR_GCC_COMPILE_SRCS.
	(HFILES_NO_SRCDIR): Add compile.h.
	(COMMON_OBS): Add SUBDIR_GCC_COMPILE_OBS.
	(INIT_FILES): Add SUBDIR_GCC_COMPILE_SRCS.
	(compile.o, compile-c-types.o, compile-c-symbols.o)
	(compile-object-load.o, compile-object-run.o, compile-loc2c.o)
	(compile-c-support.o): New targets.

2014-10-07  Phil Muldoon  <pmuldoon@redhat.com>

	* gdb.texinfo (Altering): Update.
	(Compiling and Injecting Code): New node.

2014-10-07  Phil Muldoon  <pmuldoon@redhat.com>
	    Jan Kratochvil  <jan.kratochvil@redhat.com>
	    Tom Tromey  <tromey@redhat.com>

	* gdb.dwarf2/compile-ops.exp: New file.
	* gdb.threads/compile-tls.c: New file.
	* gdb.threads/compile-tls.exp: New file.
	* gdb.base/compile-constvar.S: New file.
	* gdb.base/compile-constvar.c: New file.
	* gdb.base/compile-mod.c: New file.
	* gdb.base/compile-nodebug.c: New file.
	* gdb.base/compile-setjmp-mod.c: New file.
	* gdb.base/compile-setjmp.c: New file.
	* gdb.base/compile-setjmp.exp: New file.
	* gdb.base/compile-shlib.c: New file.
	* gdb.base/compile.c: New file.
	* gdb.base/compile.exp: New file.
	* lib/gdb.exp (skip_compile_feature_tests): New proc.
---
 gdb/ChangeLog                               |   55 +
 gdb/Makefile.in                             |   61 +
 gdb/NEWS                                    |   19 
 gdb/ada-lang.c                              |    2 
 gdb/c-lang.c                                |    8 
 gdb/c-lang.h                                |   19 
 gdb/cli/cli-script.c                        |   31 +
 gdb/compile/compile-c-support.c             |  396 +++++++++
 gdb/compile/compile-c-symbols.c             |  759 ++++++++++++++++++
 gdb/compile/compile-c-types.c               |  438 ++++++++++
 gdb/compile/compile-internal.h              |  147 +++
 gdb/compile/compile-loc2c.c                 | 1148 +++++++++++++++++++++++++++
 gdb/compile/compile-object-load.c           |  586 ++++++++++++++
 gdb/compile/compile-object-load.h           |   39 +
 gdb/compile/compile-object-run.c            |  138 +++
 gdb/compile/compile-object-run.h            |   24 +
 gdb/compile/compile.c                       |  642 +++++++++++++++
 gdb/compile/compile.h                       |  102 ++
 gdb/d-lang.c                                |    2 
 gdb/defs.h                                  |   28 +
 gdb/doc/ChangeLog                           |    5 
 gdb/doc/gdb.texinfo                         |  225 +++++
 gdb/dwarf2loc.c                             |   86 ++
 gdb/dwarf2loc.h                             |   21 
 gdb/f-lang.c                                |    2 
 gdb/go-lang.c                               |    2 
 gdb/jv-lang.c                               |    2 
 gdb/language.c                              |    6 
 gdb/language.h                              |   31 +
 gdb/m2-lang.c                               |    2 
 gdb/objc-lang.c                             |    2 
 gdb/opencl-lang.c                           |    2 
 gdb/p-lang.c                                |    2 
 gdb/symtab.h                                |   15 
 gdb/testsuite/ChangeLog                     |   19 
 gdb/testsuite/gdb.base/compile-constvar.S   |   95 ++
 gdb/testsuite/gdb.base/compile-constvar.c   |   18 
 gdb/testsuite/gdb.base/compile-mod.c        |   26 +
 gdb/testsuite/gdb.base/compile-nodebug.c    |   24 +
 gdb/testsuite/gdb.base/compile-setjmp-mod.c |   46 +
 gdb/testsuite/gdb.base/compile-setjmp.c     |   24 +
 gdb/testsuite/gdb.base/compile-setjmp.exp   |   34 +
 gdb/testsuite/gdb.base/compile-shlib.c      |   26 +
 gdb/testsuite/gdb.base/compile.c            |  130 +++
 gdb/testsuite/gdb.base/compile.exp          |  357 ++++++++
 gdb/testsuite/gdb.dwarf2/compile-ops.exp    |  424 ++++++++++
 gdb/testsuite/gdb.threads/compile-tls.c     |   40 +
 gdb/testsuite/gdb.threads/compile-tls.exp   |   42 +
 gdb/testsuite/lib/gdb.exp                   |   17 
 49 files changed, 6362 insertions(+), 7 deletions(-)
 create mode 100644 gdb/compile/compile-c-support.c
 create mode 100644 gdb/compile/compile-c-symbols.c
 create mode 100644 gdb/compile/compile-c-types.c
 create mode 100644 gdb/compile/compile-internal.h
 create mode 100644 gdb/compile/compile-loc2c.c
 create mode 100644 gdb/compile/compile-object-load.c
 create mode 100644 gdb/compile/compile-object-load.h
 create mode 100644 gdb/compile/compile-object-run.c
 create mode 100644 gdb/compile/compile-object-run.h
 create mode 100644 gdb/compile/compile.c
 create mode 100644 gdb/compile/compile.h
 create mode 100644 gdb/testsuite/gdb.base/compile-constvar.S
 create mode 100644 gdb/testsuite/gdb.base/compile-constvar.c
 create mode 100644 gdb/testsuite/gdb.base/compile-mod.c
 create mode 100644 gdb/testsuite/gdb.base/compile-nodebug.c
 create mode 100644 gdb/testsuite/gdb.base/compile-setjmp-mod.c
 create mode 100644 gdb/testsuite/gdb.base/compile-setjmp.c
 create mode 100644 gdb/testsuite/gdb.base/compile-setjmp.exp
 create mode 100644 gdb/testsuite/gdb.base/compile-shlib.c
 create mode 100644 gdb/testsuite/gdb.base/compile.c
 create mode 100644 gdb/testsuite/gdb.base/compile.exp
 create mode 100644 gdb/testsuite/gdb.dwarf2/compile-ops.exp
 create mode 100644 gdb/testsuite/gdb.threads/compile-tls.c
 create mode 100644 gdb/testsuite/gdb.threads/compile-tls.exp

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 62ae948..0edffe8 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,58 @@
+2014-10-07  Phil Muldoon  <pmuldoon@redhat.com>
+	    Jan Kratochvil  <jan.kratochvil@redhat.com>
+	    Tom Tromey  <tromey@redhat.com>
+
+	* NEWS: Update.
+	* symtab.h (struct symbol_computed_ops) <generate_c_location>: New
+	field.
+	* p-lang.c (pascal_language_defn): Update.
+	* opencl-lang.c (opencl_language_defn): Update.
+	* objc-lang.c (objc_language_defn): Update.
+	* m2-lang.c (m2_language_defn): Update.
+	* language.h (struct language_defn) <la_get_compile_instance,
+	la_compute_program>: New fields.
+	* language.c (unknown_language_defn, auto_language_defn)
+	(local_language_defn): Update.
+	* jv-lang.c (java_language_defn): Update.
+	* go-lang.c (go_language_defn): Update.
+	* f-lang.c (f_language_defn): Update.
+	* dwarf2loc.h (dwarf2_compile_property_to_c): Declare.
+	* dwarf2loc.c (dwarf2_compile_property_to_c)
+	(locexpr_generate_c_location, loclist_generate_c_location): New
+	functions.
+	(dwarf2_locexpr_funcs, dwarf2_loclist_funcs): Update.
+	* defs.h (enum compile_i_scope_types): New.
+	(enum command_control_type) <compile_control>: New constant.
+	(struct command_line) <control_u>: New field.
+	* d-lang.c (d_language_defn): Update.
+	* compile/compile.c: New file.
+	* compile/compile-c-support.c: New file.
+	* compile/compile-c-symbols.c: New file.
+	* compile/compile-c-types.c: New file.
+	* compile/compile.h: New file.
+	* compile/compile-internal.h: New file.
+	* compile/compile-loc2c.c: New file.
+	* compile/compile-object-load.c: New file.
+	* compile/compile-object-load.h: New file.
+	* compile/compile-object-run.c: New file.
+	* compile/compile-object-run.h: New file.
+	* cli/cli-script.c (multi_line_command_p, print_command_lines)
+	(execute_control_command, process_next_line)
+	(recurse_read_control_structure): Handle compile_control.
+	* c-lang.h (c_get_compile_context, c_compute_program): Declare.
+	* c-lang.c (c_language_defn, cplus_language_defn)
+	(asm_language_defn, minimal_language_defn): Update.
+	* ada-lang.c (ada_language_defn): Update.
+	* Makefile.in (SUBDIR_GCC_COMPILE_OBS, SUBDIR_GCC_COMPILE_SRCS):
+	New variables.
+	(SFILES): Add SUBDIR_GCC_COMPILE_SRCS.
+	(HFILES_NO_SRCDIR): Add compile.h.
+	(COMMON_OBS): Add SUBDIR_GCC_COMPILE_OBS.
+	(INIT_FILES): Add SUBDIR_GCC_COMPILE_SRCS.
+	(compile.o, compile-c-types.o, compile-c-symbols.o)
+	(compile-object-load.o, compile-object-run.o, compile-loc2c.o)
+	(compile-c-support.o): New targets.
+
 2014-10-07  Jan Kratochvil  <jan.kratochvil@redhat.com>
 
 	* s390-linux-tdep.c (s390_gcc_target_options): New function.
diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index 1da8af6..7d1b809 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -280,6 +280,24 @@ SUBDIR_TUI_LDFLAGS=
 SUBDIR_TUI_CFLAGS= \
 	-DTUI=1
 
+#
+# GCC Compile support sub-directory definitions
+#
+SUBDIR_GCC_COMPILE_OBS = \
+	compile.o compile-c-symbols.o compile-c-types.o \
+	compile-object-load.o compile-object-run.o \
+	compile-loc2c.o compile-c-support.o
+SUBDIR_GCC_COMPILE_SRCS = \
+	compile/compile.c \
+	compile/compile-c-symbols.c \
+	compile/compile-c-types.c \
+	compile/compile-object-load.c \
+	compile/compile-object-load.h \
+	compile/compile-object-run.c \
+	compile/compile-object-run.h \
+	compile/compile-loc2c.c \
+	compile/compile-c-support.c
+
 # Guile sub directory definitons for guile support.
 
 SUBDIR_GUILE_OBS = \
@@ -852,7 +870,8 @@ SFILES = ada-exp.y ada-lang.c ada-typeprint.c ada-valprint.c ada-tasks.c \
 	common/ptid.c common/buffer.c gdb-dlfcn.c common/agent.c \
 	common/format.c common/filestuff.c btrace.c record-btrace.c ctf.c \
 	target/waitstatus.c common/print-utils.c common/rsp-low.c \
-	common/errors.c common/common-debug.c common/common-exceptions.c
+	common/errors.c common/common-debug.c common/common-exceptions.c \
+	$(SUBDIR_GCC_COMPILE_SRCS)
 
 LINTFILES = $(SFILES) $(YYFILES) $(CONFIG_SRCS) init.c
 
@@ -874,7 +893,7 @@ nto-tdep.h serial.h \
 c-lang.h d-lang.h go-lang.h frame.h event-loop.h block.h cli/cli-setshow.h \
 cli/cli-decode.h cli/cli-cmds.h cli/cli-utils.h \
 cli/cli-script.h macrotab.h symtab.h common/version.h \
-gnulib/import/string.in.h gnulib/import/str-two-way.h \
+compile/compile.h gnulib/import/string.in.h gnulib/import/str-two-way.h \
 gnulib/import/stdint.in.h remote.h remote-notif.h gdb.h sparc-nat.h \
 gdbthread.h dwarf2-frame.h dwarf2-frame-tailcall.h nbsd-nat.h dcache.h \
 amd64-nat.h s390-linux-tdep.h arm-linux-tdep.h exceptions.h macroscope.h \
@@ -1039,7 +1058,8 @@ COMMON_OBS = $(DEPFILES) $(CONFIG_OBS) $(YYOBJ) \
 	common-utils.o buffer.o ptid.o gdb-dlfcn.o common-agent.o \
 	format.o registry.o btrace.o record-btrace.o waitstatus.o \
 	print-utils.o rsp-low.o errors.o common-debug.o debug.o \
-	common-exceptions.o
+	common-exceptions.o \
+	$(SUBDIR_GCC_COMPILE_OBS)
 
 TSOBS = inflow.o
 
@@ -1282,7 +1302,7 @@ test-cp-name-parser$(EXEEXT): test-cp-name-parser.o $(LIBIBERTY)
 # duplicates.  Files in the gdb/ directory can end up appearing in
 # COMMON_OBS (as a .o file) and CONFIG_SRCS (as a .c file).
 
-INIT_FILES = $(COMMON_OBS) $(TSOBS) $(CONFIG_SRCS)
+INIT_FILES = $(COMMON_OBS) $(TSOBS) $(CONFIG_SRCS) $(SUBDIR_GCC_COMPILE_SRCS)
 init.c: $(INIT_FILES)
 	@echo Making init.c
 	@rm -f init.c-tmp init.l-tmp
@@ -1914,6 +1934,39 @@ cli-utils.o: $(srcdir)/cli/cli-utils.c
 	$(COMPILE) $(srcdir)/cli/cli-utils.c
 	$(POSTCOMPILE)
 
+# GCC Compile support dependencies
+#
+# Need to explicitly specify the compile rule as make will do nothing
+# or try to compile the object file into the sub-directory.
+
+compile.o: $(srcdir)/compile/compile.c
+	$(COMPILE) $(srcdir)/compile/compile.c
+	$(POSTCOMPILE)
+
+compile-c-types.o: $(srcdir)/compile/compile-c-types.c
+	$(COMPILE) $(srcdir)/compile/compile-c-types.c
+	$(POSTCOMPILE)
+
+compile-c-symbols.o: $(srcdir)/compile/compile-c-symbols.c
+	$(COMPILE) $(srcdir)/compile/compile-c-symbols.c
+	$(POSTCOMPILE)
+
+compile-object-load.o: $(srcdir)/compile/compile-object-load.c
+	$(COMPILE) $(srcdir)/compile/compile-object-load.c
+	$(POSTCOMPILE)
+
+compile-object-run.o: $(srcdir)/compile/compile-object-run.c
+	$(COMPILE) $(srcdir)/compile/compile-object-run.c
+	$(POSTCOMPILE)
+
+compile-loc2c.o: $(srcdir)/compile/compile-loc2c.c
+	$(COMPILE) $(srcdir)/compile/compile-loc2c.c
+	$(POSTCOMPILE)
+
+compile-c-support.o: $(srcdir)/compile/compile-c-support.c
+	$(COMPILE) $(srcdir)/compile/compile-c-support.c
+	$(POSTCOMPILE)
+
 
 #
 # GDBTK sub-directory
diff --git a/gdb/NEWS b/gdb/NEWS
index 649c29e..c4a6c6a 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -22,11 +22,30 @@
   ** $_any_caller_is(name [, number_of_frames])
   ** $_any_caller_matches(regexp [, number_of_frames])
 
+* GDB now supports the compilation and injection of source code into
+  the inferior.  GDB will use GCC 5.0 or higher built with libcc1.so
+  to compile the source code to object code, and if successful, inject
+  and execute that code within the current context of the inferior.
+  Currently the C language is supported.  The commands used to
+  interface with this new feature are:
+
+     compile code [-raw|-r] [--] [source code]
+     compile file [-raw|-r] filename
+
 * New commands
 
 queue-signal signal-name-or-number
   Queue a signal to be delivered to the thread when it is resumed.
 
+compile code [-r|-raw] [--] [source code]
+  Compile, inject, and execute in the inferior the executable object
+  code produced by compiling the provided source code.
+
+compile file [-r|-raw] filename
+  Compile and inject into the inferior the executable object code
+  produced by compiling the source code stored in the filename
+  provided.
+
 * On resume, GDB now always passes the signal the program had stopped
   for to the thread the signal was sent to, even if the user changed
   threads before resuming.  Previously GDB would often (but not
diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c
index 5793cd2..1575fce 100644
--- a/gdb/ada-lang.c
+++ b/gdb/ada-lang.c
@@ -13526,6 +13526,8 @@ const struct language_defn ada_language_defn = {
   ada_get_symbol_name_cmp,	/* la_get_symbol_name_cmp */
   ada_iterate_over_symbols,
   &ada_varobj_ops,
+  NULL,
+  NULL,
   LANG_MAGIC
 };
 
diff --git a/gdb/c-lang.c b/gdb/c-lang.c
index 1353eea..e8283e3 100644
--- a/gdb/c-lang.c
+++ b/gdb/c-lang.c
@@ -865,6 +865,8 @@ const struct language_defn c_language_defn =
   NULL,				/* la_get_symbol_name_cmp */
   iterate_over_symbols,
   &c_varobj_ops,
+  c_get_compile_context,
+  c_compute_program,
   LANG_MAGIC
 };
 
@@ -990,6 +992,8 @@ const struct language_defn cplus_language_defn =
   NULL,				/* la_get_symbol_name_cmp */
   iterate_over_symbols,
   &cplus_varobj_ops,
+  NULL,
+  NULL,
   LANG_MAGIC
 };
 
@@ -1033,6 +1037,8 @@ const struct language_defn asm_language_defn =
   NULL,				/* la_get_symbol_name_cmp */
   iterate_over_symbols,
   &default_varobj_ops,
+  NULL,
+  NULL,
   LANG_MAGIC
 };
 
@@ -1081,6 +1087,8 @@ const struct language_defn minimal_language_defn =
   NULL,				/* la_get_symbol_name_cmp */
   iterate_over_symbols,
   &default_varobj_ops,
+  NULL,
+  NULL,
   LANG_MAGIC
 };
 
diff --git a/gdb/c-lang.h b/gdb/c-lang.h
index 76bd426..5a2f878 100644
--- a/gdb/c-lang.h
+++ b/gdb/c-lang.h
@@ -141,5 +141,24 @@ extern int cp_is_vtbl_member (struct type *);
 
 extern int c_textual_element_type (struct type *, char);
 
+/* Create a new instance of the C compiler and return it.  The new
+   compiler is owned by the caller and must be freed using the destroy
+   method.  This function never returns NULL, but rather throws an
+   exception on failure.  This is suitable for use as the
+   la_get_compile_instance language method.  */
+
+extern struct compile_instance *c_get_compile_context (void);
+
+/* This takes the user-supplied text and returns a newly malloc'd bit
+   of code to compile.
+
+   This is used as the la_compute_program language method; see that
+   for a description of the arguments.  */
+
+extern char *c_compute_program (struct compile_instance *inst,
+				const char *input,
+				struct gdbarch *gdbarch,
+				const struct block *expr_block,
+				CORE_ADDR expr_pc);
 
 #endif /* !defined (C_LANG_H) */
diff --git a/gdb/cli/cli-script.c b/gdb/cli/cli-script.c
index f8ff58d..779c8dc 100644
--- a/gdb/cli/cli-script.c
+++ b/gdb/cli/cli-script.c
@@ -31,6 +31,7 @@
 
 #include "extension.h"
 #include "interps.h"
+#include "compile/compile.h"
 
 /* Prototypes for local functions.  */
 
@@ -87,6 +88,7 @@ multi_line_command_p (enum command_control_type type)
     case while_control:
     case while_stepping_control:
     case commands_control:
+    case compile_control:
     case python_control:
     case guile_control:
       return 1;
@@ -272,6 +274,19 @@ print_command_lines (struct ui_out *uiout, struct command_line *cmd,
 	  continue;
 	}
 
+      if (list->control_type == compile_control)
+	{
+	  ui_out_field_string (uiout, NULL, "compile expression");
+	  ui_out_text (uiout, "\n");
+	  print_command_lines (uiout, *list->body_list, 0);
+	  if (depth)
+	    ui_out_spaces (uiout, 2 * depth);
+	  ui_out_field_string (uiout, NULL, "end");
+	  ui_out_text (uiout, "\n");
+	  list = list->next;
+	  continue;
+	}
+
       if (list->control_type == guile_control)
 	{
 	  ui_out_field_string (uiout, NULL, "guile");
@@ -599,6 +614,11 @@ execute_control_command (struct command_line *cmd)
 	break;
       }
 
+    case compile_control:
+      eval_compile_command (cmd, NULL, cmd->control_u.compile.scope);
+      ret = simple_control;
+      break;
+
     case python_control:
     case guile_control:
       {
@@ -1040,6 +1060,14 @@ process_next_line (char *p, struct command_line **command, int parse_commands,
 	     here.  */
 	  *command = build_command_line (python_control, "");
 	}
+      else if (p_end - p == 6 && !strncmp (p, "compile", 7))
+	{
+	  /* Note that we ignore the inline "compile command" form
+	     here.  */
+	  *command = build_command_line (compile_control, "");
+	  (*command)->control_u.compile.scope = COMPILE_I_INVALID_SCOPE;
+	}
+
       else if (p_end - p == 5 && !strncmp (p, "guile", 5))
 	{
 	  /* Note that we ignore the inline "guile command" form here.  */
@@ -1133,7 +1161,8 @@ recurse_read_control_structure (char * (*read_next_line_func) (void),
       next = NULL;
       val = process_next_line (read_next_line_func (), &next, 
 			       current_cmd->control_type != python_control
-			       && current_cmd->control_type != guile_control,
+			       && current_cmd->control_type != guile_control
+			       && current_cmd->control_type != compile_control,
 			       validator, closure);
 
       /* Just skip blanks and comments.  */
diff --git a/gdb/compile/compile-c-support.c b/gdb/compile/compile-c-support.c
new file mode 100644
index 0000000..f0fa999
--- /dev/null
+++ b/gdb/compile/compile-c-support.c
@@ -0,0 +1,396 @@
+/* C language support for compilation.
+
+   Copyright (C) 2014 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include "defs.h"
+#include "compile-internal.h"
+#include "compile.h"
+#include "gdb-dlfcn.h"
+#include "c-lang.h"
+#include "macrotab.h"
+#include "macroscope.h"
+#include "regcache.h"
+
+/* See compile-internal.h.  */
+
+const char *
+c_get_mode_for_size (int size)
+{
+  const char *mode = NULL;
+
+  switch (size)
+    {
+    case 1:
+      mode = "QI";
+      break;
+    case 2:
+      mode = "HI";
+      break;
+    case 4:
+      mode = "SI";
+      break;
+    case 8:
+      mode = "DI";
+      break;
+    }
+
+  return mode;
+}
+
+/* See compile-internal.h.  */
+
+char *
+c_get_range_decl_name (const struct dynamic_prop *prop)
+{
+  return xstrprintf ("__gdb_prop_%s", host_address_to_string (prop));
+}
+
+\f
+
+#define STR(x) #x
+#define STRINGIFY(x) STR(x)
+
+/* Helper function for c_get_compile_context.  Open the GCC front-end
+   shared library and return the symbol specified by the current
+   GCC_C_FE_CONTEXT.  */
+
+static gcc_c_fe_context_function *
+load_libcc (void)
+{
+  void *handle;
+  gcc_c_fe_context_function *func;
+
+   /* gdb_dlopen will call error () on an error, so no need to check
+      value.  */
+  handle = gdb_dlopen (STRINGIFY (GCC_C_FE_LIBCC));
+  func = (gcc_c_fe_context_function *) gdb_dlsym (handle,
+						  STRINGIFY (GCC_C_FE_CONTEXT));
+
+  if (func == NULL)
+    error (_("could not find symbol %s in library %s"),
+	   STRINGIFY (GCC_C_FE_CONTEXT),
+	   STRINGIFY (GCC_C_FE_LIBCC));
+  return func;
+}
+
+/* Return the compile instance associated with the current context.
+   This function calls the symbol returned from the load_libcc
+   function.  This will provide the gcc_c_context.  */
+
+struct compile_instance *
+c_get_compile_context (void)
+{
+  static gcc_c_fe_context_function *func;
+
+  struct gcc_c_context *context;
+
+  if (func == NULL)
+    {
+      func = load_libcc ();
+      gdb_assert (func != NULL);
+    }
+
+  context = (*func) (GCC_FE_VERSION_0, GCC_C_FE_VERSION_0);
+  if (context == NULL)
+    error (_("The loaded version of GCC does not support the required version "
+	     "of the API."));
+
+  return new_compile_instance (context);
+}
+
+\f
+
+/* Write one macro definition.  */
+
+static void
+print_one_macro (const char *name, const struct macro_definition *macro,
+		 struct macro_source_file *source, int line,
+		 void *user_data)
+{
+  struct ui_file *file = user_data;
+
+  /* Don't print command-line defines.  They will be supplied another
+     way.  */
+  if (line == 0)
+    return;
+
+  fprintf_filtered (file, "#define %s", name);
+
+  if (macro->kind == macro_function_like)
+    {
+      int i;
+
+      fputs_filtered ("(", file);
+      for (i = 0; i < macro->argc; i++)
+	{
+	  fputs_filtered (macro->argv[i], file);
+	  if (i + 1 < macro->argc)
+	    fputs_filtered (", ", file);
+	}
+      fputs_filtered (")", file);
+    }
+
+  fprintf_filtered (file, " %s\n", macro->replacement);
+}
+
+/* Write macro definitions at PC to FILE.  */
+
+static void
+write_macro_definitions (const struct block *block, CORE_ADDR pc,
+			 struct ui_file *file)
+{
+  struct macro_scope *scope;
+
+  if (block != NULL)
+    scope = sal_macro_scope (find_pc_line (pc, 0));
+  else
+    scope = default_macro_scope ();
+  if (scope == NULL)
+    scope = user_macro_scope ();
+
+  if (scope != NULL && scope->file != NULL && scope->file->table != NULL)
+    macro_for_each_in_scope (scope->file, scope->line, print_one_macro, file);
+}
+
+/* Helper function to construct a header scope for a block of code.
+   Takes a scope argument which selects the correct header to
+   insert into BUF.  */
+
+static void
+add_code_header (enum compile_i_scope_types type, struct ui_file *buf)
+{
+  switch (type)
+    {
+    case COMPILE_I_SIMPLE_SCOPE:
+      fputs_unfiltered ("void "
+			GCC_FE_WRAPPER_FUNCTION
+			" (struct "
+			COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG
+			" *"
+			COMPILE_I_SIMPLE_REGISTER_ARG_NAME
+			") {\n",
+			buf);
+      break;
+    case COMPILE_I_RAW_SCOPE:
+      break;
+    default:
+      gdb_assert_not_reached (_("Unknown compiler scope reached."));
+    }
+}
+
+/* Helper function to construct a footer scope for a block of code.
+   Takes a scope argument which selects the correct footer to
+   insert into BUF.  */
+
+static void
+add_code_footer (enum compile_i_scope_types type, struct ui_file *buf)
+{
+  switch (type)
+    {
+    case COMPILE_I_SIMPLE_SCOPE:
+      fputs_unfiltered ("}\n", buf);
+      break;
+    case COMPILE_I_RAW_SCOPE:
+      break;
+    default:
+      gdb_assert_not_reached (_("Unknown compiler scope reached."));
+    }
+}
+
+/* Generate a structure holding all the registers used by the function
+   we're generating.  */
+
+static void
+generate_register_struct (struct ui_file *stream, struct gdbarch *gdbarch,
+			  const unsigned char *registers_used)
+{
+  int i;
+  int seen = 0;
+
+  fputs_unfiltered ("struct " COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG " {\n",
+		    stream);
+
+  if (registers_used != NULL)
+    for (i = 0; i < gdbarch_num_regs (gdbarch); ++i)
+      {
+	if (registers_used[i])
+	  {
+	    struct type *regtype = check_typedef (register_type (gdbarch, i));
+	    char *regname = compile_register_name_mangled (gdbarch, i);
+	    struct cleanup *cleanups = make_cleanup (xfree, regname);
+
+	    seen = 1;
+
+	    /* You might think we could use type_print here.  However,
+	       target descriptions often use types with names like
+	       "int64_t", which may not be defined in the inferior
+	       (and in any case would not be looked up due to the
+	       #pragma business).  So, we take a much simpler
+	       approach: for pointer- or integer-typed registers, emit
+	       the field in the most direct way; and for other
+	       register types (typically flags or vectors), emit a
+	       maximally-aligned array of the correct size.  */
+
+	    fputs_unfiltered ("  ", stream);
+	    switch (TYPE_CODE (regtype))
+	      {
+	      case TYPE_CODE_PTR:
+		fprintf_filtered (stream, "void *%s", regname);
+		break;
+
+	      case TYPE_CODE_INT:
+		{
+		  const char *mode
+		    = c_get_mode_for_size (TYPE_LENGTH (regtype));
+
+		  if (mode != NULL)
+		    {
+		      if (TYPE_UNSIGNED (regtype))
+			fputs_unfiltered ("unsigned ", stream);
+		      fprintf_unfiltered (stream,
+					  "int %s"
+					  " __attribute__ ((__mode__(__%s__)))",
+					  regname,
+					  mode);
+		      break;
+		    }
+		}
+
+		/* Fall through.  */
+
+	      default:
+		fprintf_unfiltered (stream,
+				    "  unsigned char %s[%d]"
+				    " __attribute__((__aligned__("
+				    "__BIGGEST_ALIGNMENT__)))",
+				    regname,
+				    TYPE_LENGTH (regtype));
+	      }
+	    fputs_unfiltered (";\n", stream);
+
+	    do_cleanups (cleanups);
+	  }
+      }
+
+  if (!seen)
+    fputs_unfiltered ("  char " COMPILE_I_SIMPLE_REGISTER_DUMMY ";\n",
+		      stream);
+
+  fputs_unfiltered ("};\n\n", stream);
+}
+
+/* Take the source code provided by the user with the 'compile'
+   command, and compute the additional wrapping, macro, variable and
+   register operations needed.  INPUT is the source code derived from
+   the 'compile' command, GDBARCH is the architecture to use when
+   computing above, EXPR_BLOCK denotes the block relevant contextually
+   to the inferior when the expression was created, and EXPR_PC
+   indicates the value of $PC.  */
+
+char *
+c_compute_program (struct compile_instance *inst,
+		   const char *input,
+		   struct gdbarch *gdbarch,
+		   const struct block *expr_block,
+		   CORE_ADDR expr_pc)
+{
+  struct ui_file *buf, *var_stream = NULL;
+  char *code;
+  struct cleanup *cleanup;
+  struct compile_c_instance *context = (struct compile_c_instance *) inst;
+
+  buf = mem_fileopen ();
+  cleanup = make_cleanup_ui_file_delete (buf);
+
+  write_macro_definitions (expr_block, expr_pc, buf);
+
+  /* Do not generate local variable information for "raw"
+     compilations.  In this case we aren't emitting our own function
+     and the user's code may only refer to globals.  */
+  if (inst->scope != COMPILE_I_RAW_SCOPE)
+    {
+      unsigned char *registers_used;
+      int i;
+
+      /* Generate the code to compute variable locations, but do it
+	 before generating the function header, so we can define the
+	 register struct before the function body.  This requires a
+	 temporary stream.  */
+      var_stream = mem_fileopen ();
+      make_cleanup_ui_file_delete (var_stream);
+      registers_used = generate_c_for_variable_locations (context,
+							  var_stream, gdbarch,
+							  expr_block, expr_pc);
+      make_cleanup (xfree, registers_used);
+
+      generate_register_struct (buf, gdbarch, registers_used);
+
+      fputs_unfiltered ("typedef unsigned int"
+			" __attribute__ ((__mode__(__pointer__)))"
+			" __gdb_uintptr;\n",
+			buf);
+      fputs_unfiltered ("typedef int"
+			" __attribute__ ((__mode__(__pointer__)))"
+			" __gdb_intptr;\n",
+			buf);
+
+      for (i = 0; i < 4; ++i)
+	{
+	  const char *mode = c_get_mode_for_size (1 << i);
+
+	  gdb_assert (mode != NULL);
+	  fprintf_unfiltered (buf,
+			      "typedef int"
+			      " __attribute__ ((__mode__(__%s__)))"
+			      " __gdb_int_%s;\n",
+			      mode, mode);
+	}
+    }
+
+  add_code_header (inst->scope, buf);
+
+  if (inst->scope == COMPILE_I_SIMPLE_SCOPE)
+    {
+      ui_file_put (var_stream, ui_file_write_for_put, buf);
+      fputs_unfiltered ("#pragma GCC user_expression\n", buf);
+    }
+
+  /* The user expression has to be in its own scope, so that "extern"
+     works properly.  Otherwise gcc thinks that the "extern"
+     declaration is in the same scope as the declaration provided by
+     gdb.  */
+  if (inst->scope != COMPILE_I_RAW_SCOPE)
+    fputs_unfiltered ("{\n", buf);
+
+  fputs_unfiltered ("#line 1 \"gdb command line\"\n", buf);
+  fputs_unfiltered (input, buf);
+  fputs_unfiltered ("\n", buf);
+
+  /* For larger user expressions the automatic semicolons may be
+     confusing.  */
+  if (strchr (input, '\n') == NULL)
+    fputs_unfiltered (";\n", buf);
+
+  if (inst->scope != COMPILE_I_RAW_SCOPE)
+    fputs_unfiltered ("}\n", buf);
+
+  add_code_footer (inst->scope, buf);
+  code = ui_file_xstrdup (buf, NULL);
+  do_cleanups (cleanup);
+  return code;
+}
diff --git a/gdb/compile/compile-c-symbols.c b/gdb/compile/compile-c-symbols.c
new file mode 100644
index 0000000..a9e381f
--- /dev/null
+++ b/gdb/compile/compile-c-symbols.c
@@ -0,0 +1,759 @@
+/* Convert symbols from GDB to GCC
+
+   Copyright (C) 2014 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+
+#include "defs.h"
+#include "compile-internal.h"
+#include "gdb_assert.h"
+#include "symtab.h"
+#include "parser-defs.h"
+#include "block.h"
+#include "objfiles.h"
+#include "compile.h"
+#include "value.h"
+#include "exceptions.h"
+#include "gdbtypes.h"
+#include "dwarf2loc.h"
+
+\f
+
+/* Object of this type are stored in the compiler's symbol_err_map.  */
+
+struct symbol_error
+{
+  /* The symbol.  */
+
+  const struct symbol *sym;
+
+  /* The error message to emit.  This is malloc'd and owned by the
+     hash table.  */
+
+  char *message;
+};
+
+/* Hash function for struct symbol_error.  */
+
+static hashval_t
+hash_symbol_error (const void *a)
+{
+  const struct symbol_error *se = a;
+
+  return htab_hash_pointer (se->sym);
+}
+
+/* Equality function for struct symbol_error.  */
+
+static int
+eq_symbol_error (const void *a, const void *b)
+{
+  const struct symbol_error *sea = a;
+  const struct symbol_error *seb = b;
+
+  return sea->sym == seb->sym;
+}
+
+/* Deletion function for struct symbol_error.  */
+
+static void
+del_symbol_error (void *a)
+{
+  struct symbol_error *se = a;
+
+  xfree (se->message);
+  xfree (se);
+}
+
+/* Associate SYMBOL with some error text.  */
+
+static void
+insert_symbol_error (htab_t hash, const struct symbol *sym, const char *text)
+{
+  struct symbol_error e;
+  void **slot;
+
+  e.sym = sym;
+  slot = htab_find_slot (hash, &e, INSERT);
+  if (*slot == NULL)
+    {
+      struct symbol_error *e = XNEW (struct symbol_error);
+
+      e->sym = sym;
+      e->message = xstrdup (text);
+      *slot = e;
+    }
+}
+
+/* Emit the error message corresponding to SYM, if one exists, and
+   arrange for it not to be emitted again.  */
+
+static void
+error_symbol_once (struct compile_c_instance *context,
+		   const struct symbol *sym)
+{
+  struct symbol_error search;
+  struct symbol_error *err;
+  char *message;
+
+  if (context->symbol_err_map == NULL)
+    return;
+
+  search.sym = sym;
+  err = htab_find (context->symbol_err_map, &search);
+  if (err == NULL || err->message == NULL)
+    return;
+
+  message = err->message;
+  err->message = NULL;
+  make_cleanup (xfree, message);
+  error (_("%s"), message);
+}
+
+\f
+
+/* Compute the name of the pointer representing a local symbol's
+   address.  */
+
+static char *
+symbol_substitution_name (struct symbol *sym)
+{
+  return concat ("__", SYMBOL_NATURAL_NAME (sym), "_ptr", (char *) NULL);
+}
+
+/* Convert a given symbol, SYM, to the compiler's representation.
+   CONTEXT is the compiler instance.  IS_GLOBAL is true if the
+   symbol came from the global scope.  IS_LOCAL is true if the symbol
+   came from a local scope.  (Note that the two are not strictly
+   inverses because the symbol might have come from the static
+   scope.)  */
+
+static void
+convert_one_symbol (struct compile_c_instance *context,
+		    struct symbol *sym,
+		    int is_global,
+		    int is_local)
+{
+  gcc_type sym_type;
+  const char *filename = SYMBOL_SYMTAB (sym)->filename;
+  unsigned short line = SYMBOL_LINE (sym);
+
+  error_symbol_once (context, sym);
+
+  if (SYMBOL_CLASS (sym) == LOC_LABEL)
+    sym_type = 0;
+  else
+    sym_type = convert_type (context, SYMBOL_TYPE (sym));
+
+  if (SYMBOL_DOMAIN (sym) == STRUCT_DOMAIN)
+    {
+      /* Binding a tag, so we don't need to build a decl.  */
+      C_CTX (context)->c_ops->tagbind (C_CTX (context),
+				       SYMBOL_NATURAL_NAME (sym),
+				       sym_type, filename, line);
+    }
+  else
+    {
+      gcc_decl decl;
+      enum gcc_c_symbol_kind kind;
+      CORE_ADDR addr = 0;
+      char *symbol_name = NULL;
+
+      switch (SYMBOL_CLASS (sym))
+	{
+	case LOC_TYPEDEF:
+	  kind = GCC_C_SYMBOL_TYPEDEF;
+	  break;
+
+	case LOC_LABEL:
+	  kind = GCC_C_SYMBOL_LABEL;
+	  addr = SYMBOL_VALUE_ADDRESS (sym);
+	  break;
+
+	case LOC_BLOCK:
+	  kind = GCC_C_SYMBOL_FUNCTION;
+	  addr = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
+	  break;
+
+	case LOC_CONST:
+	  if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_ENUM)
+	    {
+	      /* Already handled by convert_enum.  */
+	      return;
+	    }
+	  C_CTX (context)->c_ops->build_constant (C_CTX (context), sym_type,
+						  SYMBOL_NATURAL_NAME (sym),
+						  SYMBOL_VALUE (sym),
+						  filename, line);
+	  return;
+
+	case LOC_CONST_BYTES:
+	  error (_("Unsupported LOC_CONST_BYTES for symbol \"%s\"."),
+		 SYMBOL_PRINT_NAME (sym));
+
+	case LOC_UNDEF:
+	  internal_error (__FILE__, __LINE__, _("LOC_UNDEF found for \"%s\"."),
+			  SYMBOL_PRINT_NAME (sym));
+
+	case LOC_COMMON_BLOCK:
+	  error (_("Fortran common block is unsupported for compilation "
+		   "evaluaton of symbol \"%s\"."),
+		 SYMBOL_PRINT_NAME (sym));
+
+	case LOC_OPTIMIZED_OUT:
+	  error (_("Symbol \"%s\" cannot be used for compilation evaluation "
+		   "as it is optimized out."),
+		 SYMBOL_PRINT_NAME (sym));
+
+	case LOC_COMPUTED:
+	  if (is_local)
+	    goto substitution;
+	  /* Probably TLS here.  */
+	  warning (_("Symbol \"%s\" is thread-local and currently can only "
+		     "be referenced from the current thread in "
+		     "compiled code."),
+		   SYMBOL_PRINT_NAME (sym));
+	  /* FALLTHROUGH */
+	case LOC_UNRESOLVED:
+	  /* 'symbol_name' cannot be used here as that one is used only for
+	     local variables from compile_dwarf_expr_to_c.
+	     Global variables can be accessed by GCC only by their address, not
+	     by their name.  */
+	  {
+	    struct value *val;
+	    struct frame_info *frame = NULL;
+
+	    if (symbol_read_needs_frame (sym))
+	      {
+		frame = get_selected_frame (NULL);
+		if (frame == NULL)
+		  error (_("Symbol \"%s\" cannot be used because "
+			   "there is no selected frame"),
+			 SYMBOL_PRINT_NAME (sym));
+	      }
+
+	    val = read_var_value (sym, frame);
+	    if (VALUE_LVAL (val) != lval_memory)
+	      error (_("Symbol \"%s\" cannot be used for compilation "
+		       "evaluation as its address has not been found."),
+		     SYMBOL_PRINT_NAME (sym));
+
+	    kind = GCC_C_SYMBOL_VARIABLE;
+	    addr = value_address (val);
+	  }
+	  break;
+
+
+	case LOC_REGISTER:
+	case LOC_ARG:
+	case LOC_REF_ARG:
+	case LOC_REGPARM_ADDR:
+	case LOC_LOCAL:
+	substitution:
+	  kind = GCC_C_SYMBOL_VARIABLE;
+	  symbol_name = symbol_substitution_name (sym);
+	  break;
+
+	case LOC_STATIC:
+	  kind = GCC_C_SYMBOL_VARIABLE;
+	  addr = SYMBOL_VALUE_ADDRESS (sym);
+	  break;
+
+	case LOC_FINAL_VALUE:
+	default:
+	  gdb_assert_not_reached ("Unreachable case in convert_one_symbol.");
+
+	}
+
+      /* Don't emit local variable decls for a raw expression.  */
+      if (context->base.scope != COMPILE_I_RAW_SCOPE
+	  || symbol_name == NULL)
+	{
+	  decl = C_CTX (context)->c_ops->build_decl (C_CTX (context),
+						     SYMBOL_NATURAL_NAME (sym),
+						     kind,
+						     sym_type,
+						     symbol_name, addr,
+						     filename, line);
+
+	  C_CTX (context)->c_ops->bind (C_CTX (context), decl, is_global);
+	}
+
+      xfree (symbol_name);
+    }
+}
+
+/* Convert a full symbol to its gcc form.  CONTEXT is the compiler to
+   use, IDENTIFIER is the name of the symbol, SYM is the symbol
+   itself, and DOMAIN is the domain which was searched.  */
+
+static void
+convert_symbol_sym (struct compile_c_instance *context, const char *identifier,
+		    struct symbol *sym, domain_enum domain)
+{
+  const struct block *static_block, *found_block;
+  int is_local_symbol;
+
+  found_block = block_found;
+
+  /* If we found a symbol and it is not in the  static or global
+     scope, then we should first convert any static or global scope
+     symbol of the same name.  This lets this unusual case work:
+
+     int x; // Global.
+     int func(void)
+     {
+     int x;
+     // At this spot, evaluate "extern int x; x"
+     }
+  */
+
+  static_block = block_static_block (found_block);
+  /* STATIC_BLOCK is NULL if FOUND_BLOCK is the global block.  */
+  is_local_symbol = (found_block != static_block && static_block != NULL);
+  if (is_local_symbol)
+    {
+      struct symbol *global_sym;
+
+      global_sym = lookup_symbol (identifier, NULL, domain, NULL);
+      /* If the outer symbol is in the static block, we ignore it, as
+	 it cannot be referenced.  */
+      if (global_sym != NULL
+	  && block_found != block_static_block (block_found))
+	{
+	  if (compile_debug)
+	    fprintf_unfiltered (gdb_stdout,
+				"gcc_convert_symbol \"%s\": global symbol\n",
+				identifier);
+	  convert_one_symbol (context, global_sym, 1, 0);
+	}
+    }
+
+  if (compile_debug)
+    fprintf_unfiltered (gdb_stdout,
+			"gcc_convert_symbol \"%s\": local symbol\n",
+			identifier);
+  convert_one_symbol (context, sym, 0, is_local_symbol);
+}
+
+/* Convert a minimal symbol to its gcc form.  CONTEXT is the compiler
+   to use and BMSYM is the minimal symbol to convert.  */
+
+static void
+convert_symbol_bmsym (struct compile_c_instance *context,
+		      struct bound_minimal_symbol bmsym)
+{
+  struct minimal_symbol *msym = bmsym.minsym;
+  struct objfile *objfile = bmsym.objfile;
+  struct type *type;
+  enum gcc_c_symbol_kind kind;
+  gcc_type sym_type;
+  gcc_decl decl;
+  CORE_ADDR addr;
+
+  /* Conversion copied from write_exp_msymbol.  */
+  switch (MSYMBOL_TYPE (msym))
+    {
+    case mst_text:
+    case mst_file_text:
+    case mst_solib_trampoline:
+      type = objfile_type (objfile)->nodebug_text_symbol;
+      kind = GCC_C_SYMBOL_FUNCTION;
+      break;
+
+    case mst_text_gnu_ifunc:
+      type = objfile_type (objfile)->nodebug_text_gnu_ifunc_symbol;
+      kind = GCC_C_SYMBOL_FUNCTION;
+      break;
+
+    case mst_data:
+    case mst_file_data:
+    case mst_bss:
+    case mst_file_bss:
+      type = objfile_type (objfile)->nodebug_data_symbol;
+      kind = GCC_C_SYMBOL_VARIABLE;
+      break;
+
+    case mst_slot_got_plt:
+      type = objfile_type (objfile)->nodebug_got_plt_symbol;
+      kind = GCC_C_SYMBOL_FUNCTION;
+      break;
+
+    default:
+      type = objfile_type (objfile)->nodebug_unknown_symbol;
+      kind = GCC_C_SYMBOL_VARIABLE;
+      break;
+    }
+
+  sym_type = convert_type (context, type);
+  addr = MSYMBOL_VALUE_ADDRESS (objfile, msym);
+  decl = C_CTX (context)->c_ops->build_decl (C_CTX (context),
+					     MSYMBOL_NATURAL_NAME (msym),
+					     kind, sym_type, NULL, addr,
+					     NULL, 0);
+  C_CTX (context)->c_ops->bind (C_CTX (context), decl, 1 /* is_global */);
+}
+
+/* See compile-internal.h.  */
+
+void
+gcc_convert_symbol (void *datum,
+		    struct gcc_c_context *gcc_context,
+		    enum gcc_c_oracle_request request,
+		    const char *identifier)
+{
+  struct compile_c_instance *context = datum;
+  domain_enum domain;
+  volatile struct gdb_exception e;
+  int found = 0;
+
+  switch (request)
+    {
+    case GCC_C_ORACLE_SYMBOL:
+      domain = VAR_DOMAIN;
+      break;
+    case GCC_C_ORACLE_TAG:
+      domain = STRUCT_DOMAIN;
+      break;
+    case GCC_C_ORACLE_LABEL:
+      domain = LABEL_DOMAIN;
+      break;
+    default:
+      gdb_assert_not_reached ("Unrecognized oracle request.");
+    }
+
+  /* We can't allow exceptions to escape out of this callback.  Safest
+     is to simply emit a gcc error.  */
+  TRY_CATCH (e, RETURN_MASK_ERROR)
+    {
+      struct symbol *sym;
+
+      sym = lookup_symbol (identifier, context->base.block, domain, NULL);
+      if (sym != NULL)
+	{
+	  convert_symbol_sym (context, identifier, sym, domain);
+	  found = 1;
+	}
+      else if (domain == VAR_DOMAIN)
+	{
+	  struct bound_minimal_symbol bmsym;
+
+	  bmsym = lookup_minimal_symbol (identifier, NULL, NULL);
+	  if (bmsym.minsym != NULL)
+	    {
+	      convert_symbol_bmsym (context, bmsym);
+	      found = 1;
+	    }
+	}
+    }
+
+  if (e.reason < 0)
+    C_CTX (context)->c_ops->error (C_CTX (context), e.message);
+
+  if (compile_debug && !found)
+    fprintf_unfiltered (gdb_stdout,
+			"gcc_convert_symbol \"%s\": lookup_symbol failed\n",
+			identifier);
+  return;
+}
+
+/* See compile-internal.h.  */
+
+gcc_address
+gcc_symbol_address (void *datum, struct gcc_c_context *gcc_context,
+		    const char *identifier)
+{
+  struct compile_c_instance *context = datum;
+  volatile struct gdb_exception e;
+  gcc_address result = 0;
+  int found = 0;
+
+  /* We can't allow exceptions to escape out of this callback.  Safest
+     is to simply emit a gcc error.  */
+  TRY_CATCH (e, RETURN_MASK_ERROR)
+    {
+      struct symbol *sym;
+
+      /* We only need global functions here.  */
+      sym = lookup_symbol (identifier, NULL, VAR_DOMAIN, NULL);
+      if (sym != NULL && SYMBOL_CLASS (sym) == LOC_BLOCK)
+	{
+	  if (compile_debug)
+	    fprintf_unfiltered (gdb_stdout,
+				"gcc_symbol_address \"%s\": full symbol\n",
+				identifier);
+	  result = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
+	  found = 1;
+	}
+      else
+	{
+	  struct bound_minimal_symbol msym;
+
+	  msym = lookup_bound_minimal_symbol (identifier);
+	  if (msym.minsym != NULL)
+	    {
+	      if (compile_debug)
+		fprintf_unfiltered (gdb_stdout,
+				    "gcc_symbol_address \"%s\": minimal "
+				    "symbol\n",
+				    identifier);
+	      result = BMSYMBOL_VALUE_ADDRESS (msym);
+	      found = 1;
+	    }
+	}
+    }
+
+  if (e.reason < 0)
+    C_CTX (context)->c_ops->error (C_CTX (context), e.message);
+
+  if (compile_debug && !found)
+    fprintf_unfiltered (gdb_stdout,
+			"gcc_symbol_address \"%s\": failed\n",
+			identifier);
+  return result;
+}
+
+\f
+
+/* A hash function for symbol names.  */
+
+static hashval_t
+hash_symname (const void *a)
+{
+  const struct symbol *sym = a;
+
+  return htab_hash_string (SYMBOL_NATURAL_NAME (sym));
+}
+
+/* A comparison function for hash tables that just looks at symbol
+   names.  */
+
+static int
+eq_symname (const void *a, const void *b)
+{
+  const struct symbol *syma = a;
+  const struct symbol *symb = b;
+
+  return strcmp (SYMBOL_NATURAL_NAME (syma), SYMBOL_NATURAL_NAME (symb)) == 0;
+}
+
+/* If a symbol with the same name as SYM is already in HASHTAB, return
+   1.  Otherwise, add SYM to HASHTAB and return 0.  */
+
+static int
+symbol_seen (htab_t hashtab, struct symbol *sym)
+{
+  void **slot;
+
+  slot = htab_find_slot (hashtab, sym, INSERT);
+  if (*slot != NULL)
+    return 1;
+
+  *slot = sym;
+  return 0;
+}
+
+/* Generate C code to compute the length of a VLA.  */
+
+static void
+generate_vla_size (struct compile_c_instance *compiler,
+		   struct ui_file *stream,
+		   struct gdbarch *gdbarch,
+		   unsigned char *registers_used,
+		   CORE_ADDR pc,
+		   struct type *type,
+		   struct symbol *sym)
+{
+  type = check_typedef (type);
+
+  if (TYPE_CODE (type) == TYPE_CODE_REF)
+    type = check_typedef (TYPE_TARGET_TYPE (type));
+
+  switch (TYPE_CODE (type))
+    {
+    case TYPE_CODE_RANGE:
+      {
+	if (TYPE_HIGH_BOUND_KIND (type) == PROP_LOCEXPR
+	    || TYPE_HIGH_BOUND_KIND (type) == PROP_LOCLIST)
+	  {
+	    const struct dynamic_prop *prop = &TYPE_RANGE_DATA (type)->high;
+	    char *name = c_get_range_decl_name (prop);
+	    struct cleanup *cleanup = make_cleanup (xfree, name);
+
+	    dwarf2_compile_property_to_c (stream, name,
+					  gdbarch, registers_used,
+					  prop, pc, sym);
+	    do_cleanups (cleanup);
+	  }
+      }
+      break;
+
+    case TYPE_CODE_ARRAY:
+      generate_vla_size (compiler, stream, gdbarch, registers_used, pc,
+			 TYPE_INDEX_TYPE (type), sym);
+      generate_vla_size (compiler, stream, gdbarch, registers_used, pc,
+			 TYPE_TARGET_TYPE (type), sym);
+      break;
+
+    case TYPE_CODE_UNION:
+    case TYPE_CODE_STRUCT:
+      {
+	int i;
+
+	for (i = 0; i < TYPE_NFIELDS (type); ++i)
+	  if (!field_is_static (&TYPE_FIELD (type, i)))
+	    generate_vla_size (compiler, stream, gdbarch, registers_used, pc,
+			       TYPE_FIELD_TYPE (type, i), sym);
+      }
+      break;
+    }
+}
+
+/* Generate C code to compute the address of SYM.  */
+
+static void
+generate_c_for_for_one_variable (struct compile_c_instance *compiler,
+				 struct ui_file *stream,
+				 struct gdbarch *gdbarch,
+				 unsigned char *registers_used,
+				 CORE_ADDR pc,
+				 struct symbol *sym)
+{
+  volatile struct gdb_exception e;
+
+  TRY_CATCH (e, RETURN_MASK_ERROR)
+    {
+      if (is_dynamic_type (SYMBOL_TYPE (sym)))
+	{
+	  struct ui_file *size_file = mem_fileopen ();
+	  struct cleanup *cleanup = make_cleanup_ui_file_delete (size_file);
+
+	  generate_vla_size (compiler, size_file, gdbarch, registers_used, pc,
+			     SYMBOL_TYPE (sym), sym);
+	  ui_file_put (size_file, ui_file_write_for_put, stream);
+
+	  do_cleanups (cleanup);
+	}
+
+      if (SYMBOL_COMPUTED_OPS (sym) != NULL)
+	{
+	  char *generated_name = symbol_substitution_name (sym);
+	  struct cleanup *cleanup = make_cleanup (xfree, generated_name);
+	  /* We need to emit to a temporary buffer in case an error
+	     occurs in the middle.  */
+	  struct ui_file *local_file = mem_fileopen ();
+
+	  make_cleanup_ui_file_delete (local_file);
+	  SYMBOL_COMPUTED_OPS (sym)->generate_c_location (sym, local_file,
+							  gdbarch,
+							  registers_used,
+							  pc, generated_name);
+	  ui_file_put (local_file, ui_file_write_for_put, stream);
+
+	  do_cleanups (cleanup);
+	}
+      else
+	{
+	  switch (SYMBOL_CLASS (sym))
+	    {
+	    case LOC_REGISTER:
+	    case LOC_ARG:
+	    case LOC_REF_ARG:
+	    case LOC_REGPARM_ADDR:
+	    case LOC_LOCAL:
+	      error (_("Local symbol unhandled when generating C code."));
+
+	    case LOC_COMPUTED:
+	      gdb_assert_not_reached (_("LOC_COMPUTED variable "
+					"missing a method."));
+
+	    default:
+	      /* Nothing to do for all other cases, as they don't represent
+		 local variables.  */
+	      break;
+	    }
+	}
+    }
+
+  if (e.reason >= 0)
+    return;
+
+  if (compiler->symbol_err_map == NULL)
+    compiler->symbol_err_map = htab_create_alloc (10,
+						  hash_symbol_error,
+						  eq_symbol_error,
+						  del_symbol_error,
+						  xcalloc,
+						  xfree);
+  insert_symbol_error (compiler->symbol_err_map, sym, e.message);
+}
+
+/* See compile-internal.h.  */
+
+unsigned char *
+generate_c_for_variable_locations (struct compile_c_instance *compiler,
+				   struct ui_file *stream,
+				   struct gdbarch *gdbarch,
+				   const struct block *block,
+				   CORE_ADDR pc)
+{
+  struct cleanup *cleanup, *outer;
+  htab_t symhash;
+  const struct block *static_block = block_static_block (block);
+  unsigned char *registers_used;
+
+  /* If we're already in the static or global block, there is nothing
+     to write.  */
+  if (static_block == NULL || block == static_block)
+    return NULL;
+
+  registers_used = XCNEWVEC (unsigned char, gdbarch_num_regs (gdbarch));
+  outer = make_cleanup (xfree, registers_used);
+
+  /* Ensure that a given name is only entered once.  This reflects the
+     reality of shadowing.  */
+  symhash = htab_create_alloc (1, hash_symname, eq_symname, NULL,
+			       xcalloc, xfree);
+  cleanup = make_cleanup_htab_delete (symhash);
+
+  while (1)
+    {
+      struct symbol *sym;
+      struct block_iterator iter;
+
+      /* Iterate over symbols in this block, generating code to
+	 compute the location of each local variable.  */
+      for (sym = block_iterator_first (block, &iter);
+	   sym != NULL;
+	   sym = block_iterator_next (&iter))
+	{
+	  if (!symbol_seen (symhash, sym))
+	    generate_c_for_for_one_variable (compiler, stream, gdbarch,
+					     registers_used, pc, sym);
+	}
+
+      /* If we just finished the outermost block of a function, we're
+	 done.  */
+      if (BLOCK_FUNCTION (block) != NULL)
+	break;
+      block = BLOCK_SUPERBLOCK (block);
+    }
+
+  do_cleanups (cleanup);
+  discard_cleanups (outer);
+  return registers_used;
+}
diff --git a/gdb/compile/compile-c-types.c b/gdb/compile/compile-c-types.c
new file mode 100644
index 0000000..78ae9a8
--- /dev/null
+++ b/gdb/compile/compile-c-types.c
@@ -0,0 +1,438 @@
+/* Convert types from GDB to GCC
+
+   Copyright (C) 2014 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+
+#include "defs.h"
+#include "gdbtypes.h"
+#include "compile-internal.h"
+#include "gdb_assert.h"
+
+/* An object that maps a gdb type to a gcc type.  */
+
+struct type_map_instance
+{
+  /* The gdb type.  */
+
+  struct type *type;
+
+  /* The corresponding gcc type handle.  */
+
+  gcc_type gcc_type;
+};
+
+/* Hash a type_map_instance.  */
+
+static hashval_t
+hash_type_map_instance (const void *p)
+{
+  const struct type_map_instance *inst = p;
+
+  return htab_hash_pointer (inst->type);
+}
+
+/* Check two type_map_instance objects for equality.  */
+
+static int
+eq_type_map_instance (const void *a, const void *b)
+{
+  const struct type_map_instance *insta = a;
+  const struct type_map_instance *instb = b;
+
+  return insta->type == instb->type;
+}
+
+\f
+
+/* Insert an entry into the type map associated with CONTEXT that maps
+   from the gdb type TYPE to the gcc type GCC_TYPE.  It is ok for a
+   given type to be inserted more than once, provided that the exact
+   same association is made each time.  This simplifies how type
+   caching works elsewhere in this file -- see how struct type caching
+   is handled.  */
+
+static void
+insert_type (struct compile_c_instance *context, struct type *type,
+	     gcc_type gcc_type)
+{
+  struct type_map_instance inst, *add;
+  void **slot;
+
+  inst.type = type;
+  inst.gcc_type = gcc_type;
+  slot = htab_find_slot (context->type_map, &inst, INSERT);
+
+  add = *slot;
+  /* The type might have already been inserted in order to handle
+     recursive types.  */
+  gdb_assert (add == NULL || add->gcc_type == gcc_type);
+
+  if (add == NULL)
+    {
+      add = XNEW (struct type_map_instance);
+      *add = inst;
+      *slot = add;
+    }
+}
+
+/* Convert a pointer type to its gcc representation.  */
+
+static gcc_type
+convert_pointer (struct compile_c_instance *context, struct type *type)
+{
+  gcc_type target = convert_type (context, TYPE_TARGET_TYPE (type));
+
+  return C_CTX (context)->c_ops->build_pointer_type (C_CTX (context),
+						     target);
+}
+
+/* Convert an array type to its gcc representation.  */
+
+static gcc_type
+convert_array (struct compile_c_instance *context, struct type *type)
+{
+  gcc_type element_type;
+  struct type *range = TYPE_INDEX_TYPE (type);
+
+  element_type = convert_type (context, TYPE_TARGET_TYPE (type));
+
+  if (TYPE_LOW_BOUND_KIND (range) != PROP_CONST)
+    return C_CTX (context)->c_ops->error (C_CTX (context),
+					  _("array type with non-constant"
+					    " lower bound is not supported"));
+  if (TYPE_LOW_BOUND (range) != 0)
+    return C_CTX (context)->c_ops->error (C_CTX (context),
+					  _("cannot convert array type with "
+					    "non-zero lower bound to C"));
+
+  if (TYPE_HIGH_BOUND_KIND (range) == PROP_LOCEXPR
+      || TYPE_HIGH_BOUND_KIND (range) == PROP_LOCLIST)
+    {
+      gcc_type result;
+      char *upper_bound;
+
+      if (TYPE_VECTOR (type))
+	return C_CTX (context)->c_ops->error (C_CTX (context),
+					      _("variably-sized vector type"
+						" is not supported"));
+
+      upper_bound = c_get_range_decl_name (&TYPE_RANGE_DATA (range)->high);
+      result = C_CTX (context)->c_ops->build_vla_array_type (C_CTX (context),
+							     element_type,
+							     upper_bound);
+      xfree (upper_bound);
+      return result;
+    }
+  else
+    {
+      LONGEST low_bound, high_bound, count;
+
+      if (get_array_bounds (type, &low_bound, &high_bound) == 0)
+	count = -1;
+      else
+	{
+	  gdb_assert (low_bound == 0); /* Ensured above.  */
+	  count = high_bound + 1;
+	}
+
+      if (TYPE_VECTOR (type))
+	return C_CTX (context)->c_ops->build_vector_type (C_CTX (context),
+							  element_type,
+							  count);
+      return C_CTX (context)->c_ops->build_array_type (C_CTX (context),
+						       element_type, count);
+    }
+}
+
+/* Convert a struct or union type to its gcc representation.  */
+
+static gcc_type
+convert_struct_or_union (struct compile_c_instance *context, struct type *type)
+{
+  int i;
+  gcc_type result;
+
+  /* First we create the resulting type and enter it into our hash
+     table.  This lets recursive types work.  */
+  if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
+    result = C_CTX (context)->c_ops->build_record_type (C_CTX (context));
+  else
+    {
+      gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
+      result = C_CTX (context)->c_ops->build_union_type (C_CTX (context));
+    }
+  insert_type (context, type, result);
+
+  for (i = 0; i < TYPE_NFIELDS (type); ++i)
+    {
+      gcc_type field_type;
+      unsigned long bitsize = TYPE_FIELD_BITSIZE (type, i);
+
+      field_type = convert_type (context, TYPE_FIELD_TYPE (type, i));
+      if (bitsize == 0)
+	bitsize = 8 * TYPE_LENGTH (TYPE_FIELD_TYPE (type, i));
+      C_CTX (context)->c_ops->build_add_field (C_CTX (context), result,
+					       TYPE_FIELD_NAME (type, i),
+					       field_type,
+					       bitsize,
+					       TYPE_FIELD_BITPOS (type, i));
+    }
+
+  C_CTX (context)->c_ops->finish_record_or_union (C_CTX (context), result,
+						  TYPE_LENGTH (type));
+  return result;
+}
+
+/* Convert an enum type to its gcc representation.  */
+
+static gcc_type
+convert_enum (struct compile_c_instance *context, struct type *type)
+{
+  gcc_type int_type, result;
+  int i;
+  struct gcc_c_context *ctx = C_CTX (context);
+
+  int_type = ctx->c_ops->int_type (ctx,
+				   TYPE_UNSIGNED (type),
+				   TYPE_LENGTH (type));
+
+  result = ctx->c_ops->build_enum_type (ctx, int_type);
+  for (i = 0; i < TYPE_NFIELDS (type); ++i)
+    {
+      ctx->c_ops->build_add_enum_constant (ctx,
+					   result,
+					   TYPE_FIELD_NAME (type, i),
+					   TYPE_FIELD_ENUMVAL (type, i));
+    }
+
+  ctx->c_ops->finish_enum_type (ctx, result);
+
+  return result;
+}
+
+/* Convert a function type to its gcc representation.  */
+
+static gcc_type
+convert_func (struct compile_c_instance *context, struct type *type)
+{
+  int i;
+  gcc_type result, return_type;
+  struct gcc_type_array array;
+  int is_varargs = TYPE_VARARGS (type) || !TYPE_PROTOTYPED (type);
+
+  /* This approach means we can't make self-referential function
+     types.  Those are impossible in C, though.  */
+  return_type = convert_type (context, TYPE_TARGET_TYPE (type));
+
+  array.n_elements = TYPE_NFIELDS (type);
+  array.elements = XNEWVEC (gcc_type, TYPE_NFIELDS (type));
+  for (i = 0; i < TYPE_NFIELDS (type); ++i)
+    array.elements[i] = convert_type (context, TYPE_FIELD_TYPE (type, i));
+
+  result = C_CTX (context)->c_ops->build_function_type (C_CTX (context),
+							return_type,
+							&array, is_varargs);
+  xfree (array.elements);
+
+  return result;
+}
+
+/* Convert an integer type to its gcc representation.  */
+
+static gcc_type
+convert_int (struct compile_c_instance *context, struct type *type)
+{
+  return C_CTX (context)->c_ops->int_type (C_CTX (context),
+					   TYPE_UNSIGNED (type),
+					   TYPE_LENGTH (type));
+}
+
+/* Convert a floating-point type to its gcc representation.  */
+
+static gcc_type
+convert_float (struct compile_c_instance *context, struct type *type)
+{
+  return C_CTX (context)->c_ops->float_type (C_CTX (context),
+					     TYPE_LENGTH (type));
+}
+
+/* Convert the 'void' type to its gcc representation.  */
+
+static gcc_type
+convert_void (struct compile_c_instance *context, struct type *type)
+{
+  return C_CTX (context)->c_ops->void_type (C_CTX (context));
+}
+
+/* Convert a boolean type to its gcc representation.  */
+
+static gcc_type
+convert_bool (struct compile_c_instance *context, struct type *type)
+{
+  return C_CTX (context)->c_ops->bool_type (C_CTX (context));
+}
+
+/* Convert a qualified type to its gcc representation.  */
+
+static gcc_type
+convert_qualified (struct compile_c_instance *context, struct type *type)
+{
+  struct type *unqual = make_unqualified_type (type);
+  gcc_type unqual_converted;
+  int quals = 0;
+
+  unqual_converted = convert_type (context, unqual);
+
+  if (TYPE_CONST (type))
+    quals |= GCC_QUALIFIER_CONST;
+  if (TYPE_VOLATILE (type))
+    quals |= GCC_QUALIFIER_VOLATILE;
+  if (TYPE_RESTRICT (type))
+    quals |= GCC_QUALIFIER_RESTRICT;
+
+  return C_CTX (context)->c_ops->build_qualified_type (C_CTX (context),
+						       unqual_converted,
+						       quals);
+}
+
+/* Convert a complex type to its gcc representation.  */
+
+static gcc_type
+convert_complex (struct compile_c_instance *context, struct type *type)
+{
+  gcc_type base = convert_type (context, TYPE_TARGET_TYPE (type));
+
+  return C_CTX (context)->c_ops->build_complex_type (C_CTX (context), base);
+}
+
+/* A helper function which knows how to convert most types from their
+   gdb representation to the corresponding gcc form.  This examines
+   the TYPE and dispatches to the appropriate conversion function.  It
+   returns the gcc type.  */
+
+static gcc_type
+convert_type_basic (struct compile_c_instance *context, struct type *type)
+{
+  /* If we are converting a qualified type, first convert the
+     unqualified type and then apply the qualifiers.  */
+  if ((TYPE_INSTANCE_FLAGS (type) & (TYPE_INSTANCE_FLAG_CONST
+				     | TYPE_INSTANCE_FLAG_VOLATILE
+				     | TYPE_INSTANCE_FLAG_RESTRICT)) != 0)
+    return convert_qualified (context, type);
+
+  switch (TYPE_CODE (type))
+    {
+    case TYPE_CODE_PTR:
+      return convert_pointer (context, type);
+
+    case TYPE_CODE_ARRAY:
+      return convert_array (context, type);
+
+    case TYPE_CODE_STRUCT:
+    case TYPE_CODE_UNION:
+      return convert_struct_or_union (context, type);
+
+    case TYPE_CODE_ENUM:
+      return convert_enum (context, type);
+
+    case TYPE_CODE_FUNC:
+      return convert_func (context, type);
+
+    case TYPE_CODE_INT:
+      return convert_int (context, type);
+
+    case TYPE_CODE_FLT:
+      return convert_float (context, type);
+
+    case TYPE_CODE_VOID:
+      return convert_void (context, type);
+
+    case TYPE_CODE_BOOL:
+      return convert_bool (context, type);
+
+    case TYPE_CODE_COMPLEX:
+      return convert_complex (context, type);
+    }
+
+  return C_CTX (context)->c_ops->error (C_CTX (context),
+					_("cannot convert gdb type "
+					  "to gcc type"));
+}
+
+/* See compile-internal.h.  */
+
+gcc_type
+convert_type (struct compile_c_instance *context, struct type *type)
+{
+  struct type_map_instance inst, *found;
+  gcc_type result;
+
+  /* We don't ever have to deal with typedefs in this code, because
+     those are only needed as symbols by the C compiler.  */
+  CHECK_TYPEDEF (type);
+
+  inst.type = type;
+  found = htab_find (context->type_map, &inst);
+  if (found != NULL)
+    return found->gcc_type;
+
+  result = convert_type_basic (context, type);
+  insert_type (context, type, result);
+  return result;
+}
+
+\f
+
+/* Delete the compiler instance C.  */
+
+static void
+delete_instance (struct compile_instance *c)
+{
+  struct compile_c_instance *context = (struct compile_c_instance *) c;
+
+  context->base.fe->ops->destroy (context->base.fe);
+  htab_delete (context->type_map);
+  if (context->symbol_err_map != NULL)
+    htab_delete (context->symbol_err_map);
+  xfree (context);
+}
+
+/* See compile-internal.h.  */
+
+struct compile_instance *
+new_compile_instance (struct gcc_c_context *fe)
+{
+  struct compile_c_instance *result = XCNEW (struct compile_c_instance);
+
+  result->base.fe = &fe->base;
+  result->base.destroy = delete_instance;
+  result->base.gcc_target_options = ("-std=gnu11"
+				     /* Otherwise the .o file may need
+					"_Unwind_Resume" and
+					"__gcc_personality_v0".  */
+				     " -fno-exceptions");
+
+  result->type_map = htab_create_alloc (10, hash_type_map_instance,
+					eq_type_map_instance,
+					xfree, xcalloc, xfree);
+
+  fe->c_ops->set_callbacks (fe, gcc_convert_symbol,
+			    gcc_symbol_address, result);
+
+  return &result->base;
+}
diff --git a/gdb/compile/compile-internal.h b/gdb/compile/compile-internal.h
new file mode 100644
index 0000000..cb8d3d1
--- /dev/null
+++ b/gdb/compile/compile-internal.h
@@ -0,0 +1,147 @@
+/* Header file for GDB compile command and supporting functions.
+   Copyright (C) 2014 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#ifndef GDB_COMPILE_INTERNAL_H
+#define GDB_COMPILE_INTERNAL_H
+
+#include "hashtab.h"
+#include "gcc-c-interface.h"
+
+/* Debugging flag for the "compile" family of commands.  */
+
+extern int compile_debug;
+
+struct block;
+
+/* An object of this type holds state associated with a given
+   compilation job.  */
+
+struct compile_instance
+{
+  /* The GCC front end.  */
+
+  struct gcc_base_context *fe;
+
+  /* The "scope" of this compilation.  */
+
+  enum compile_i_scope_types scope;
+
+  /* The block in which an expression is being parsed.  */
+
+  const struct block *block;
+
+  /* Specify "-std=gnu11", "-std=gnu++11" or similar.  These options are put
+     after CU's DW_AT_producer compilation options to override them.  */
+
+  const char *gcc_target_options;
+
+  /* How to destroy this object.  */
+
+  void (*destroy) (struct compile_instance *);
+};
+
+/* A subclass of compile_instance that is specific to the C front
+   end.  */
+struct compile_c_instance
+{
+  /* Base class.  Note that the base class vtable actually points to a
+     gcc_c_fe_vtable.  */
+
+  struct compile_instance base;
+
+  /* Map from gdb types to gcc types.  */
+
+  htab_t type_map;
+
+  /* Map from gdb symbols to gcc error messages to emit.  */
+
+  htab_t symbol_err_map;
+};
+
+/* A helper macro that takes a compile_c_instance and returns its
+   corresponding gcc_c_context.  */
+
+#define C_CTX(I) ((struct gcc_c_context *) ((I)->base.fe))
+
+/* Define header and footers for different scopes.  */
+
+/* A simple scope just declares a function named "_gdb_expr", takes no
+   arguments and returns no value.  */
+
+#define COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG "__gdb_regs"
+#define COMPILE_I_SIMPLE_REGISTER_ARG_NAME "__regs"
+#define COMPILE_I_SIMPLE_REGISTER_DUMMY "_dummy"
+
+/* Call gdbarch_register_name (GDBARCH, REGNUM) and convert its result
+   to a form suitable for the compiler source.  The register names
+   should not clash with inferior defined macros.  Returned pointer is
+   never NULL.  Returned pointer needs to be deallocated by xfree.  */
+
+extern char *compile_register_name_mangled (struct gdbarch *gdbarch,
+					    int regnum);
+
+/* Convert compiler source register name to register number of
+   GDBARCH.  Returned value is always >= 0, function throws an error
+   for non-matching REG_NAME.  */
+
+extern int compile_register_name_demangle (struct gdbarch *gdbarch,
+					   const char *reg_name);
+
+/* Convert a gdb type, TYPE, to a GCC type.  CONTEXT is used to do the
+   actual conversion.  The new GCC type is returned.  */
+
+struct type;
+extern gcc_type convert_type (struct compile_c_instance *context,
+			      struct type *type);
+
+/* A callback suitable for use as the GCC C symbol oracle.  */
+
+extern gcc_c_oracle_function gcc_convert_symbol;
+
+/* A callback suitable for use as the GCC C address oracle.  */
+
+extern gcc_c_symbol_address_function gcc_symbol_address;
+
+/* Instantiate a GDB object holding state for the GCC context FE.  The
+   new object is returned.  */
+
+extern struct compile_instance *new_compile_instance (struct gcc_c_context *fe);
+
+/* Emit code to compute the address for all the local variables in
+   scope at PC in BLOCK.  Returns a malloc'd vector, indexed by gdb
+   register number, where each element indicates if the corresponding
+   register is needed to compute a local variable.  */
+
+extern unsigned char *generate_c_for_variable_locations
+     (struct compile_c_instance *compiler,
+      struct ui_file *stream,
+      struct gdbarch *gdbarch,
+      const struct block *block,
+      CORE_ADDR pc);
+
+/* Get the GCC mode attribute value for a given type size.  */
+
+extern const char *c_get_mode_for_size (int size);
+
+/* Given a dynamic property, return an xmallocd name that is used to
+   represent its size.  The result must be freed by the caller.  The
+   contents of the resulting string will be the same each time for
+   each call with the same argument.  */
+
+struct dynamic_prop;
+extern char *c_get_range_decl_name (const struct dynamic_prop *prop);
+
+#endif /* GDB_COMPILE_INTERNAL_H */
diff --git a/gdb/compile/compile-loc2c.c b/gdb/compile/compile-loc2c.c
new file mode 100644
index 0000000..e31d44d
--- /dev/null
+++ b/gdb/compile/compile-loc2c.c
@@ -0,0 +1,1148 @@
+/* Convert a DWARF location expression to C
+
+   Copyright (C) 2014 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include "defs.h"
+#include "dwarf2.h"
+#include "dwarf2expr.h"
+#include "dwarf2loc.h"
+#include "ui-file.h"
+#include "utils.h"
+#include "compile-internal.h"
+#include "compile.h"
+#include "block.h"
+#include "dwarf2-frame.h"
+#include "gdb_vecs.h"
+#include "value.h"
+
+\f
+
+/* Information about a given instruction.  */
+
+struct insn_info
+{
+  /* Stack depth at entry.  */
+
+  unsigned int depth;
+
+  /* Whether this instruction has been visited.  */
+
+  unsigned int visited : 1;
+
+  /* Whether this instruction needs a label.  */
+
+  unsigned int label : 1;
+
+  /* Whether this instruction is DW_OP_GNU_push_tls_address.  This is
+     a hack until we can add a feature to glibc to let us properly
+     generate code for TLS.  */
+
+  unsigned int is_tls : 1;
+};
+
+/* A helper function for compute_stack_depth that does the work.  This
+   examines the DWARF expression starting from START and computes
+   stack effects.
+
+   NEED_TEMPVAR is an out parameter which is set if this expression
+   needs a special temporary variable to be emitted (see the code
+   generator).
+   INFO is an array of insn_info objects, indexed by offset from the
+   start of the DWARF expression.
+   TO_DO is a list of bytecodes which must be examined; it may be
+   added to by this function.
+   BYTE_ORDER and ADDR_SIZE describe this bytecode in the obvious way.
+   OP_PTR and OP_END are the bounds of the DWARF expression.  */
+
+static void
+compute_stack_depth_worker (int start, int *need_tempvar,
+			    struct insn_info *info,
+			    VEC (int) **to_do,
+			    enum bfd_endian byte_order, unsigned int addr_size,
+			    const gdb_byte *op_ptr, const gdb_byte *op_end)
+{
+  const gdb_byte * const base = op_ptr;
+  int stack_depth;
+
+  op_ptr += start;
+  gdb_assert (info[start].visited);
+  stack_depth = info[start].depth;
+
+  while (op_ptr < op_end)
+    {
+      enum dwarf_location_atom op = *op_ptr;
+      uint64_t reg;
+      int64_t offset;
+      int ndx = op_ptr - base;
+
+#define SET_CHECK_DEPTH(WHERE)				\
+      if (info[WHERE].visited)				\
+	{						\
+	  if (info[WHERE].depth != stack_depth)		\
+	    error (_("inconsistent stack depths"));	\
+	}						\
+      else						\
+	{						\
+	  /* Stack depth not set, so set it.  */	\
+	  info[WHERE].visited = 1;			\
+	  info[WHERE].depth = stack_depth;		\
+	}
+
+      SET_CHECK_DEPTH (ndx);
+
+      ++op_ptr;
+
+      switch (op)
+	{
+	case DW_OP_lit0:
+	case DW_OP_lit1:
+	case DW_OP_lit2:
+	case DW_OP_lit3:
+	case DW_OP_lit4:
+	case DW_OP_lit5:
+	case DW_OP_lit6:
+	case DW_OP_lit7:
+	case DW_OP_lit8:
+	case DW_OP_lit9:
+	case DW_OP_lit10:
+	case DW_OP_lit11:
+	case DW_OP_lit12:
+	case DW_OP_lit13:
+	case DW_OP_lit14:
+	case DW_OP_lit15:
+	case DW_OP_lit16:
+	case DW_OP_lit17:
+	case DW_OP_lit18:
+	case DW_OP_lit19:
+	case DW_OP_lit20:
+	case DW_OP_lit21:
+	case DW_OP_lit22:
+	case DW_OP_lit23:
+	case DW_OP_lit24:
+	case DW_OP_lit25:
+	case DW_OP_lit26:
+	case DW_OP_lit27:
+	case DW_OP_lit28:
+	case DW_OP_lit29:
+	case DW_OP_lit30:
+	case DW_OP_lit31:
+	  ++stack_depth;
+	  break;
+
+	case DW_OP_addr:
+	  op_ptr += addr_size;
+	  ++stack_depth;
+	  break;
+
+	case DW_OP_const1u:
+	case DW_OP_const1s:
+	  op_ptr += 1;
+	  ++stack_depth;
+	  break;
+	case DW_OP_const2u:
+	case DW_OP_const2s:
+	  op_ptr += 2;
+	  ++stack_depth;
+	  break;
+	case DW_OP_const4u:
+	case DW_OP_const4s:
+	  op_ptr += 4;
+	  ++stack_depth;
+	  break;
+	case DW_OP_const8u:
+	case DW_OP_const8s:
+	  op_ptr += 8;
+	  ++stack_depth;
+	  break;
+	case DW_OP_constu:
+	case DW_OP_consts:
+	  op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
+	  ++stack_depth;
+	  break;
+
+	case DW_OP_reg0:
+	case DW_OP_reg1:
+	case DW_OP_reg2:
+	case DW_OP_reg3:
+	case DW_OP_reg4:
+	case DW_OP_reg5:
+	case DW_OP_reg6:
+	case DW_OP_reg7:
+	case DW_OP_reg8:
+	case DW_OP_reg9:
+	case DW_OP_reg10:
+	case DW_OP_reg11:
+	case DW_OP_reg12:
+	case DW_OP_reg13:
+	case DW_OP_reg14:
+	case DW_OP_reg15:
+	case DW_OP_reg16:
+	case DW_OP_reg17:
+	case DW_OP_reg18:
+	case DW_OP_reg19:
+	case DW_OP_reg20:
+	case DW_OP_reg21:
+	case DW_OP_reg22:
+	case DW_OP_reg23:
+	case DW_OP_reg24:
+	case DW_OP_reg25:
+	case DW_OP_reg26:
+	case DW_OP_reg27:
+	case DW_OP_reg28:
+	case DW_OP_reg29:
+	case DW_OP_reg30:
+	case DW_OP_reg31:
+	  ++stack_depth;
+	  break;
+
+	case DW_OP_regx:
+	  op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
+	  ++stack_depth;
+	  break;
+
+	case DW_OP_breg0:
+	case DW_OP_breg1:
+	case DW_OP_breg2:
+	case DW_OP_breg3:
+	case DW_OP_breg4:
+	case DW_OP_breg5:
+	case DW_OP_breg6:
+	case DW_OP_breg7:
+	case DW_OP_breg8:
+	case DW_OP_breg9:
+	case DW_OP_breg10:
+	case DW_OP_breg11:
+	case DW_OP_breg12:
+	case DW_OP_breg13:
+	case DW_OP_breg14:
+	case DW_OP_breg15:
+	case DW_OP_breg16:
+	case DW_OP_breg17:
+	case DW_OP_breg18:
+	case DW_OP_breg19:
+	case DW_OP_breg20:
+	case DW_OP_breg21:
+	case DW_OP_breg22:
+	case DW_OP_breg23:
+	case DW_OP_breg24:
+	case DW_OP_breg25:
+	case DW_OP_breg26:
+	case DW_OP_breg27:
+	case DW_OP_breg28:
+	case DW_OP_breg29:
+	case DW_OP_breg30:
+	case DW_OP_breg31:
+	  op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
+	  ++stack_depth;
+	  break;
+	case DW_OP_bregx:
+	  {
+	    op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
+	    op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
+	    ++stack_depth;
+	  }
+	  break;
+	case DW_OP_fbreg:
+	  op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
+	  ++stack_depth;
+	  break;
+
+	case DW_OP_dup:
+	  ++stack_depth;
+	  break;
+
+	case DW_OP_drop:
+	  --stack_depth;
+	  break;
+
+	case DW_OP_pick:
+	  ++op_ptr;
+	  ++stack_depth;
+	  break;
+
+	case DW_OP_rot:
+	case DW_OP_swap:
+	  *need_tempvar = 1;
+	  break;
+
+	case DW_OP_over:
+	  ++stack_depth;
+	  break;
+
+	case DW_OP_abs:
+	case DW_OP_neg:
+	case DW_OP_not:
+	case DW_OP_deref:
+	  break;
+
+	case DW_OP_deref_size:
+	  ++op_ptr;
+	  break;
+
+	case DW_OP_plus_uconst:
+	  op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
+	  break;
+
+	case DW_OP_div:
+	case DW_OP_shra:
+	case DW_OP_and:
+	case DW_OP_minus:
+	case DW_OP_mod:
+	case DW_OP_mul:
+	case DW_OP_or:
+	case DW_OP_plus:
+	case DW_OP_shl:
+	case DW_OP_shr:
+	case DW_OP_xor:
+	case DW_OP_le:
+	case DW_OP_ge:
+	case DW_OP_eq:
+	case DW_OP_lt:
+	case DW_OP_gt:
+	case DW_OP_ne:
+	  --stack_depth;
+	  break;
+
+	case DW_OP_call_frame_cfa:
+	  ++stack_depth;
+	  break;
+
+	case DW_OP_GNU_push_tls_address:
+	  info[ndx].is_tls = 1;
+	  break;
+
+	case DW_OP_skip:
+	  offset = extract_signed_integer (op_ptr, 2, byte_order);
+	  op_ptr += 2;
+	  offset = op_ptr + offset - base;
+	  /* If the destination has not been seen yet, add it to the
+	     to-do list.  */
+	  if (!info[offset].visited)
+	    VEC_safe_push (int, *to_do, offset);
+	  SET_CHECK_DEPTH (offset);
+	  info[offset].label = 1;
+	  /* We're done with this line of code.  */
+	  return;
+
+	case DW_OP_bra:
+	  offset = extract_signed_integer (op_ptr, 2, byte_order);
+	  op_ptr += 2;
+	  offset = op_ptr + offset - base;
+	  --stack_depth;
+	  /* If the destination has not been seen yet, add it to the
+	     to-do list.  */
+	  if (!info[offset].visited)
+	    VEC_safe_push (int, *to_do, offset);
+	  SET_CHECK_DEPTH (offset);
+	  info[offset].label = 1;
+	  break;
+
+	case DW_OP_nop:
+	  break;
+
+	default:
+	  error (_("unhandled DWARF op: %s"), get_DW_OP_name (op));
+	}
+    }
+
+  gdb_assert (op_ptr == op_end);
+
+#undef SET_CHECK_DEPTH
+}
+
+/* Compute the maximum needed stack depth of a DWARF expression, and
+   some other information as well.
+
+   BYTE_ORDER and ADDR_SIZE describe this bytecode in the obvious way.
+   NEED_TEMPVAR is an out parameter which is set if this expression
+   needs a special temporary variable to be emitted (see the code
+   generator).
+   IS_TLS is an out parameter which is set if this expression refers
+   to a TLS variable.
+   OP_PTR and OP_END are the bounds of the DWARF expression.
+   INITIAL_DEPTH is the initial depth of the DWARF expression stack.
+   INFO is an array of insn_info objects, indexed by offset from the
+   start of the DWARF expression.
+
+   This returns the maximum stack depth.  */
+
+static int
+compute_stack_depth (enum bfd_endian byte_order, unsigned int addr_size,
+		     int *need_tempvar, int *is_tls,
+		     const gdb_byte *op_ptr, const gdb_byte *op_end,
+		     int initial_depth,
+		     struct insn_info **info)
+{
+  unsigned char *set;
+  struct cleanup *outer_cleanup, *cleanup;
+  VEC (int) *to_do = NULL;
+  int stack_depth, i;
+
+  *info = XCNEWVEC (struct insn_info, op_end - op_ptr);
+  outer_cleanup = make_cleanup (xfree, *info);
+
+  cleanup = make_cleanup (VEC_cleanup (int), &to_do);
+
+  VEC_safe_push (int, to_do, 0);
+  (*info)[0].depth = initial_depth;
+  (*info)[0].visited = 1;
+
+  while (!VEC_empty (int, to_do))
+    {
+      int ndx = VEC_pop (int, to_do);
+
+      compute_stack_depth_worker (ndx, need_tempvar, *info, &to_do,
+				  byte_order, addr_size,
+				  op_ptr, op_end);
+    }
+
+  stack_depth = 0;
+  *is_tls = 0;
+  for (i = 0; i < op_end - op_ptr; ++i)
+    {
+      if ((*info)[i].depth > stack_depth)
+	stack_depth = (*info)[i].depth;
+      if ((*info)[i].is_tls)
+	*is_tls = 1;
+    }
+
+  do_cleanups (cleanup);
+  discard_cleanups (outer_cleanup);
+  return stack_depth + 1;
+}
+
+\f
+
+#define GCC_UINTPTR "__gdb_uintptr"
+#define GCC_INTPTR "__gdb_intptr"
+
+/* Emit code to push a constant.  */
+
+static void
+push (int indent, struct ui_file *stream, ULONGEST l)
+{
+  fprintfi_filtered (indent, stream, "__gdb_stack[++__gdb_tos] = %s;\n",
+		     hex_string (l));
+}
+
+/* Emit code to push an arbitrary expression.  This works like
+   printf.  */
+
+static void
+pushf (int indent, struct ui_file *stream, const char *format, ...)
+{
+  va_list args;
+
+  fprintfi_filtered (indent, stream, "__gdb_stack[__gdb_tos + 1] = ");
+  va_start (args, format);
+  vfprintf_filtered (stream, format, args);
+  va_end (args);
+  fprintf_filtered (stream, ";\n");
+
+  fprintfi_filtered (indent, stream, "++__gdb_tos;\n");
+}
+
+/* Emit code for a unary expression -- one which operates in-place on
+   the top-of-stack.  This works like printf.  */
+
+static void
+unary (int indent, struct ui_file *stream, const char *format, ...)
+{
+  va_list args;
+
+  fprintfi_filtered (indent, stream, "__gdb_stack[__gdb_tos] = ");
+  va_start (args, format);
+  vfprintf_filtered (stream, format, args);
+  va_end (args);
+  fprintf_filtered (stream, ";\n");
+}
+
+/* Emit code for a unary expression -- one which uses the top two
+   stack items, popping the topmost one.  This works like printf.  */
+
+static void
+binary (int indent, struct ui_file *stream, const char *format, ...)
+{
+  va_list args;
+
+  fprintfi_filtered (indent, stream, "__gdb_stack[__gdb_tos - 1] = ");
+  va_start (args, format);
+  vfprintf_filtered (stream, format, args);
+  va_end (args);
+  fprintf_filtered (stream, ";\n");
+  fprintfi_filtered (indent, stream, "--__gdb_tos;\n");
+}
+
+/* Print the name of a label given its "SCOPE", an arbitrary integer
+   used for uniqueness, and its TARGET, the bytecode offset
+   corresponding to the label's point of definition.  */
+
+static void
+print_label (struct ui_file *stream, unsigned int scope, int target)
+{
+  fprintf_filtered (stream, "__label_%u_%s",
+		    scope, pulongest (target));
+}
+
+/* Emit code that pushes a register's address on the stack.
+   REGISTERS_USED is an out parameter which is updated to note which
+   register was needed by this expression.  */
+
+static void
+pushf_register_address (int indent, struct ui_file *stream,
+			unsigned char *registers_used,
+			struct gdbarch *gdbarch, int regnum)
+{
+  char *regname = compile_register_name_mangled (gdbarch, regnum);
+  struct cleanup *cleanups = make_cleanup (xfree, regname);
+
+  registers_used[regnum] = 1;
+  pushf (indent, stream, "&" COMPILE_I_SIMPLE_REGISTER_ARG_NAME	 "->%s",
+	 regname);
+
+  do_cleanups (cleanups);
+}
+
+/* Emit code that pushes a register's value on the stack.
+   REGISTERS_USED is an out parameter which is updated to note which
+   register was needed by this expression.  OFFSET is added to the
+   register's value before it is pushed.  */
+
+static void
+pushf_register (int indent, struct ui_file *stream,
+		unsigned char *registers_used,
+		struct gdbarch *gdbarch, int regnum, uint64_t offset)
+{
+  char *regname = compile_register_name_mangled (gdbarch, regnum);
+  struct cleanup *cleanups = make_cleanup (xfree, regname);
+
+  registers_used[regnum] = 1;
+  if (offset == 0)
+    pushf (indent, stream, COMPILE_I_SIMPLE_REGISTER_ARG_NAME "->%s",
+	   regname);
+  else
+    pushf (indent, stream, COMPILE_I_SIMPLE_REGISTER_ARG_NAME "->%s + %s",
+	   regname, hex_string (offset));
+
+  do_cleanups (cleanups);
+}
+
+/* Compile a DWARF expression to C code.
+
+   INDENT is the indentation level to use.
+   STREAM is the stream where the code should be written.
+
+   TYPE_NAME names the type of the result of the DWARF expression.
+   For locations this is "void *" but for array bounds it will be an
+   integer type.
+
+   RESULT_NAME is the name of a variable in the resulting C code.  The
+   result of the expression will be assigned to this variable.
+
+   SYM is the symbol corresponding to this expression.
+   PC is the location at which the expression is being evaluated.
+   ARCH is the architecture to use.
+
+   REGISTERS_USED is an out parameter which is updated to note which
+   registers were needed by this expression.
+
+   ADDR_SIZE is the DWARF address size to use.
+
+   OPT_PTR and OP_END are the bounds of the DWARF expression.
+
+   If non-NULL, INITIAL points to an initial value to write to the
+   stack.  If NULL, no initial value is written.
+
+   PER_CU is the per-CU object used for looking up various other
+   things.  */
+
+static void
+do_compile_dwarf_expr_to_c (int indent, struct ui_file *stream,
+			    const char *type_name,
+			    const char *result_name,
+			    struct symbol *sym, CORE_ADDR pc,
+			    struct gdbarch *arch,
+			    unsigned char *registers_used,
+			    unsigned int addr_size,
+			    const gdb_byte *op_ptr, const gdb_byte *op_end,
+			    CORE_ADDR *initial,
+			    struct dwarf2_per_cu_data *per_cu)
+{
+  /* We keep a counter so that labels and other objects we create have
+     unique names.  */
+  static unsigned int scope;
+
+  enum bfd_endian byte_order = gdbarch_byte_order (arch);
+  const gdb_byte * const base = op_ptr;
+  int need_tempvar = 0;
+  int is_tls = 0;
+  struct cleanup *cleanup;
+  struct insn_info *info;
+  int stack_depth;
+
+  ++scope;
+
+  fprintfi_filtered (indent, stream, "%s%s;\n", type_name, result_name);
+  fprintfi_filtered (indent, stream, "{\n");
+  indent += 2;
+
+  stack_depth = compute_stack_depth (byte_order, addr_size,
+				     &need_tempvar, &is_tls,
+				     op_ptr, op_end, initial != NULL,
+				     &info);
+  cleanup = make_cleanup (xfree, info);
+
+  /* This is a hack until we can add a feature to glibc to let us
+     properly generate code for TLS.  You might think we could emit
+     the address in the ordinary course of translating
+     DW_OP_GNU_push_tls_address, but since the operand appears on the
+     stack, it is relatively hard to find, and the idea of calling
+     target_translate_tls_address with OFFSET==0 and then adding the
+     offset by hand seemed too hackish.  */
+  if (is_tls)
+    {
+      struct frame_info *frame = get_selected_frame (NULL);
+      struct value *val;
+
+      if (frame == NULL)
+	error (_("Symbol \"%s\" cannot be used because "
+		 "there is no selected frame"),
+	       SYMBOL_PRINT_NAME (sym));
+
+      val = read_var_value (sym, frame);
+      if (VALUE_LVAL (val) != lval_memory)
+	error (_("Symbol \"%s\" cannot be used for compilation evaluation "
+		 "as its address has not been found."),
+	       SYMBOL_PRINT_NAME (sym));
+
+      warning (_("Symbol \"%s\" is thread-local and currently can only "
+		 "be referenced from the current thread in "
+		 "compiled code."),
+	       SYMBOL_PRINT_NAME (sym));
+
+      fprintfi_filtered (indent, stream, "%s = %s;\n",
+			 result_name,
+			 core_addr_to_string (value_address (val)));
+      fprintfi_filtered (indent - 2, stream, "}\n");
+      do_cleanups (cleanup);
+      return;
+    }
+
+  fprintfi_filtered (indent, stream, GCC_UINTPTR " __gdb_stack[%d];\n",
+		     stack_depth);
+
+  if (need_tempvar)
+    fprintfi_filtered (indent, stream, GCC_UINTPTR " __gdb_tmp;\n");
+  fprintfi_filtered (indent, stream, "int __gdb_tos = -1;\n");
+
+  if (initial != NULL)
+    pushf (indent, stream, core_addr_to_string (*initial));
+
+  while (op_ptr < op_end)
+    {
+      enum dwarf_location_atom op = *op_ptr;
+      uint64_t uoffset, reg;
+      int64_t offset;
+
+      print_spaces (indent - 2, stream);
+      if (info[op_ptr - base].label)
+	{
+	  print_label (stream, scope, op_ptr - base);
+	  fprintf_filtered (stream, ":;");
+	}
+      fprintf_filtered (stream, "/* %s */\n", get_DW_OP_name (op));
+
+      /* This is handy for debugging the generated code:
+      fprintf_filtered (stream, "if (__gdb_tos != %d) abort ();\n",
+			(int) info[op_ptr - base].depth - 1);
+      */
+
+      ++op_ptr;
+
+      switch (op)
+	{
+	case DW_OP_lit0:
+	case DW_OP_lit1:
+	case DW_OP_lit2:
+	case DW_OP_lit3:
+	case DW_OP_lit4:
+	case DW_OP_lit5:
+	case DW_OP_lit6:
+	case DW_OP_lit7:
+	case DW_OP_lit8:
+	case DW_OP_lit9:
+	case DW_OP_lit10:
+	case DW_OP_lit11:
+	case DW_OP_lit12:
+	case DW_OP_lit13:
+	case DW_OP_lit14:
+	case DW_OP_lit15:
+	case DW_OP_lit16:
+	case DW_OP_lit17:
+	case DW_OP_lit18:
+	case DW_OP_lit19:
+	case DW_OP_lit20:
+	case DW_OP_lit21:
+	case DW_OP_lit22:
+	case DW_OP_lit23:
+	case DW_OP_lit24:
+	case DW_OP_lit25:
+	case DW_OP_lit26:
+	case DW_OP_lit27:
+	case DW_OP_lit28:
+	case DW_OP_lit29:
+	case DW_OP_lit30:
+	case DW_OP_lit31:
+	  push (indent, stream, op - DW_OP_lit0);
+	  break;
+
+	case DW_OP_addr:
+	  op_ptr += addr_size;
+	  /* Some versions of GCC emit DW_OP_addr before
+	     DW_OP_GNU_push_tls_address.  In this case the value is an
+	     index, not an address.  We don't support things like
+	     branching between the address and the TLS op.  */
+	  if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
+	    uoffset += dwarf2_per_cu_text_offset (per_cu);
+	  push (indent, stream, uoffset);
+	  break;
+
+	case DW_OP_const1u:
+	  push (indent, stream,
+		extract_unsigned_integer (op_ptr, 1, byte_order));
+	  op_ptr += 1;
+	  break;
+	case DW_OP_const1s:
+	  push (indent, stream,
+		extract_signed_integer (op_ptr, 1, byte_order));
+	  op_ptr += 1;
+	  break;
+	case DW_OP_const2u:
+	  push (indent, stream,
+		extract_unsigned_integer (op_ptr, 2, byte_order));
+	  op_ptr += 2;
+	  break;
+	case DW_OP_const2s:
+	  push (indent, stream,
+		extract_signed_integer (op_ptr, 2, byte_order));
+	  op_ptr += 2;
+	  break;
+	case DW_OP_const4u:
+	  push (indent, stream,
+		extract_unsigned_integer (op_ptr, 4, byte_order));
+	  op_ptr += 4;
+	  break;
+	case DW_OP_const4s:
+	  push (indent, stream,
+		extract_signed_integer (op_ptr, 4, byte_order));
+	  op_ptr += 4;
+	  break;
+	case DW_OP_const8u:
+	  push (indent, stream,
+		extract_unsigned_integer (op_ptr, 8, byte_order));
+	  op_ptr += 8;
+	  break;
+	case DW_OP_const8s:
+	  push (indent, stream,
+		extract_signed_integer (op_ptr, 8, byte_order));
+	  op_ptr += 8;
+	  break;
+	case DW_OP_constu:
+	  op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
+	  push (indent, stream, uoffset);
+	  break;
+	case DW_OP_consts:
+	  op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
+	  push (indent, stream, offset);
+	  break;
+
+	case DW_OP_reg0:
+	case DW_OP_reg1:
+	case DW_OP_reg2:
+	case DW_OP_reg3:
+	case DW_OP_reg4:
+	case DW_OP_reg5:
+	case DW_OP_reg6:
+	case DW_OP_reg7:
+	case DW_OP_reg8:
+	case DW_OP_reg9:
+	case DW_OP_reg10:
+	case DW_OP_reg11:
+	case DW_OP_reg12:
+	case DW_OP_reg13:
+	case DW_OP_reg14:
+	case DW_OP_reg15:
+	case DW_OP_reg16:
+	case DW_OP_reg17:
+	case DW_OP_reg18:
+	case DW_OP_reg19:
+	case DW_OP_reg20:
+	case DW_OP_reg21:
+	case DW_OP_reg22:
+	case DW_OP_reg23:
+	case DW_OP_reg24:
+	case DW_OP_reg25:
+	case DW_OP_reg26:
+	case DW_OP_reg27:
+	case DW_OP_reg28:
+	case DW_OP_reg29:
+	case DW_OP_reg30:
+	case DW_OP_reg31:
+	  dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
+	  pushf_register_address (indent, stream, registers_used, arch,
+				  dwarf2_reg_to_regnum_or_error (arch,
+							      op - DW_OP_reg0));
+	  break;
+
+	case DW_OP_regx:
+	  op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
+	  dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
+	  pushf_register_address (indent, stream, registers_used, arch,
+				  dwarf2_reg_to_regnum_or_error (arch, reg));
+	  break;
+
+	case DW_OP_breg0:
+	case DW_OP_breg1:
+	case DW_OP_breg2:
+	case DW_OP_breg3:
+	case DW_OP_breg4:
+	case DW_OP_breg5:
+	case DW_OP_breg6:
+	case DW_OP_breg7:
+	case DW_OP_breg8:
+	case DW_OP_breg9:
+	case DW_OP_breg10:
+	case DW_OP_breg11:
+	case DW_OP_breg12:
+	case DW_OP_breg13:
+	case DW_OP_breg14:
+	case DW_OP_breg15:
+	case DW_OP_breg16:
+	case DW_OP_breg17:
+	case DW_OP_breg18:
+	case DW_OP_breg19:
+	case DW_OP_breg20:
+	case DW_OP_breg21:
+	case DW_OP_breg22:
+	case DW_OP_breg23:
+	case DW_OP_breg24:
+	case DW_OP_breg25:
+	case DW_OP_breg26:
+	case DW_OP_breg27:
+	case DW_OP_breg28:
+	case DW_OP_breg29:
+	case DW_OP_breg30:
+	case DW_OP_breg31:
+	  op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
+	  pushf_register (indent, stream, registers_used, arch,
+			  dwarf2_reg_to_regnum_or_error (arch,
+							 op - DW_OP_breg0),
+			  offset);
+	  break;
+	case DW_OP_bregx:
+	  {
+	    op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
+	    op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
+	    pushf_register (indent, stream, registers_used, arch,
+			    dwarf2_reg_to_regnum_or_error (arch, reg), offset);
+	  }
+	  break;
+	case DW_OP_fbreg:
+	  {
+	    const gdb_byte *datastart;
+	    size_t datalen;
+	    const struct block *b;
+	    struct symbol *framefunc;
+	    char fb_name[50];
+
+	    b = block_for_pc (pc);
+
+	    if (!b)
+	      error (_("No block found for address"));
+
+	    framefunc = block_linkage_function (b);
+
+	    if (!framefunc)
+	      error (_("No function found for block"));
+
+	    func_get_frame_base_dwarf_block (framefunc, pc,
+					     &datastart, &datalen);
+
+	    op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
+
+	    /* Generate a unique-enough name, in case the frame base
+	       is computed multiple times in this expression.  */
+	    xsnprintf (fb_name, sizeof (fb_name), "__frame_base_%ld",
+		       (long) (op_ptr - base));
+
+	    do_compile_dwarf_expr_to_c (indent, stream,
+					"void *", fb_name,
+					sym, pc,
+					arch, registers_used, addr_size,
+					datastart, datastart + datalen,
+					NULL, per_cu);
+
+	    pushf (indent, stream, "%s + %s", fb_name, hex_string (offset));
+	  }
+	  break;
+
+	case DW_OP_dup:
+	  pushf (indent, stream, "__gdb_stack[__gdb_tos]");
+	  break;
+
+	case DW_OP_drop:
+	  fprintfi_filtered (indent, stream, "--__gdb_tos;\n");
+	  break;
+
+	case DW_OP_pick:
+	  offset = *op_ptr++;
+	  pushf (indent, stream, "__gdb_stack[__gdb_tos - %d]", offset);
+	  break;
+
+	case DW_OP_swap:
+	  fprintfi_filtered (indent, stream,
+			     "__gdb_tmp = __gdb_stack[__gdb_tos - 1];\n");
+	  fprintfi_filtered (indent, stream,
+			     "__gdb_stack[__gdb_tos - 1] = "
+			     "__gdb_stack[__gdb_tos];\n");
+	  fprintfi_filtered (indent, stream, ("__gdb_stack[__gdb_tos] = "
+					      "__gdb_tmp;\n"));
+	  break;
+
+	case DW_OP_over:
+	  pushf (indent, stream, "__gdb_stack[__gdb_tos - 1]");
+	  break;
+
+	case DW_OP_rot:
+	  fprintfi_filtered (indent, stream, ("__gdb_tmp = "
+					      "__gdb_stack[__gdb_tos];\n"));
+	  fprintfi_filtered (indent, stream,
+			     "__gdb_stack[__gdb_tos] = "
+			     "__gdb_stack[__gdb_tos - 1];\n");
+	  fprintfi_filtered (indent, stream,
+			     "__gdb_stack[__gdb_tos - 1] = "
+			     "__gdb_stack[__gdb_tos -2];\n");
+	  fprintfi_filtered (indent, stream, "__gdb_stack[__gdb_tos - 2] = "
+			     "__gdb_tmp;\n");
+	  break;
+
+	case DW_OP_deref:
+	case DW_OP_deref_size:
+	  {
+	    int size;
+	    const char *mode;
+
+	    if (op == DW_OP_deref_size)
+	      size = *op_ptr++;
+	    else
+	      size = addr_size;
+
+	    mode = c_get_mode_for_size (size);
+	    if (mode == NULL)
+	      error (_("Unsupported size %d in %s"),
+		     size, get_DW_OP_name (op));
+
+	    /* Cast to a pointer of the desired type, then
+	       dereference.  */
+	    fprintfi_filtered (indent, stream,
+			       "__gdb_stack[__gdb_tos] = "
+			       "*((__gdb_int_%s *) "
+			       "__gdb_stack[__gdb_tos]);\n",
+			       mode);
+	  }
+	  break;
+
+	case DW_OP_abs:
+	  unary (indent, stream,
+		 "((" GCC_INTPTR ") __gdb_stack[__gdb_tos]) < 0 ? "
+		 "-__gdb_stack[__gdb_tos] : __gdb_stack[__gdb_tos]");
+	  break;
+
+	case DW_OP_neg:
+	  unary (indent, stream, "-__gdb_stack[__gdb_tos]");
+	  break;
+
+	case DW_OP_not:
+	  unary (indent, stream, "~__gdb_stack[__gdb_tos]");
+	  break;
+
+	case DW_OP_plus_uconst:
+	  op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
+	  unary (indent, stream, "__gdb_stack[__gdb_tos] + %s",
+		 hex_string (reg));
+	  break;
+
+	case DW_OP_div:
+	  binary (indent, stream, ("((" GCC_INTPTR
+				   ") __gdb_stack[__gdb_tos-1]) / (("
+				   GCC_INTPTR ") __gdb_stack[__gdb_tos])"));
+	  break;
+
+	case DW_OP_shra:
+	  binary (indent, stream,
+		  "((" GCC_INTPTR ") __gdb_stack[__gdb_tos-1]) >> "
+		  "__gdb_stack[__gdb_tos]");
+	  break;
+
+#define BINARY(OP)							\
+	  binary (indent, stream, ("__gdb_stack[__gdb_tos-1] " #OP	\
+				   " __gdb_stack[__gdb_tos]"));	\
+	  break
+
+	case DW_OP_and:
+	  BINARY (&);
+	case DW_OP_minus:
+	  BINARY (-);
+	case DW_OP_mod:
+	  BINARY (%);
+	case DW_OP_mul:
+	  BINARY (*);
+	case DW_OP_or:
+	  BINARY (|);
+	case DW_OP_plus:
+	  BINARY (+);
+	case DW_OP_shl:
+	  BINARY (<<);
+	case DW_OP_shr:
+	  BINARY (>>);
+	case DW_OP_xor:
+	  BINARY (^);
+#undef BINARY
+
+#define COMPARE(OP)							\
+	  binary (indent, stream,					\
+		  "(((" GCC_INTPTR ") __gdb_stack[__gdb_tos-1]) " #OP	\
+		  " ((" GCC_INTPTR					\
+		  ") __gdb_stack[__gdb_tos]))");			\
+	  break
+
+	case DW_OP_le:
+	  COMPARE (<=);
+	case DW_OP_ge:
+	  COMPARE (>=);
+	case DW_OP_eq:
+	  COMPARE (==);
+	case DW_OP_lt:
+	  COMPARE (<);
+	case DW_OP_gt:
+	  COMPARE (>);
+	case DW_OP_ne:
+	  COMPARE (!=);
+#undef COMPARE
+
+	case DW_OP_call_frame_cfa:
+	  {
+	    int regnum;
+	    CORE_ADDR text_offset;
+	    LONGEST off;
+	    const gdb_byte *cfa_start, *cfa_end;
+
+	    if (dwarf2_fetch_cfa_info (arch, pc, per_cu,
+				       &regnum, &off,
+				       &text_offset, &cfa_start, &cfa_end))
+	      {
+		/* Register.  */
+		pushf_register (indent, stream, registers_used, arch, regnum,
+				off);
+	      }
+	    else
+	      {
+		/* Another expression.  */
+		char cfa_name[50];
+
+		/* Generate a unique-enough name, in case the CFA is
+		   computed multiple times in this expression.  */
+		xsnprintf (cfa_name, sizeof (cfa_name),
+			   "__cfa_%ld", (long) (op_ptr - base));
+
+		do_compile_dwarf_expr_to_c (indent, stream,
+					    "void *", cfa_name,
+					    sym, pc, arch, registers_used,
+					    addr_size,
+					    cfa_start, cfa_end,
+					    &text_offset, per_cu);
+		pushf (indent, stream, cfa_name);
+	      }
+	  }
+
+	  break;
+
+	case DW_OP_skip:
+	  offset = extract_signed_integer (op_ptr, 2, byte_order);
+	  op_ptr += 2;
+	  fprintfi_filtered (indent, stream, "goto ");
+	  print_label (stream, scope, op_ptr + offset - base);
+	  fprintf_filtered (stream, ";\n");
+	  break;
+
+	case DW_OP_bra:
+	  offset = extract_signed_integer (op_ptr, 2, byte_order);
+	  op_ptr += 2;
+	  fprintfi_filtered (indent, stream,
+			     "if ((( " GCC_INTPTR
+			     ") __gdb_stack[__gdb_tos--]) != 0) goto ");
+	  print_label (stream, scope, op_ptr + offset - base);
+	  fprintf_filtered (stream, ";\n");
+	  break;
+
+	case DW_OP_nop:
+	  break;
+
+	default:
+	  error (_("unhandled DWARF op: %s"), get_DW_OP_name (op));
+	}
+    }
+
+  fprintfi_filtered (indent, stream, "%s = (%s) __gdb_stack[__gdb_tos];\n",
+		     result_name, type_name);
+  fprintfi_filtered (indent - 2, stream, "}\n");
+
+  do_cleanups (cleanup);
+}
+
+/* See compile.h.  */
+
+void
+compile_dwarf_expr_to_c (struct ui_file *stream, const char *result_name,
+			 struct symbol *sym, CORE_ADDR pc,
+			 struct gdbarch *arch, unsigned char *registers_used,
+			 unsigned int addr_size,
+			 const gdb_byte *op_ptr, const gdb_byte *op_end,
+			 struct dwarf2_per_cu_data *per_cu)
+{
+  do_compile_dwarf_expr_to_c (2, stream, "void *", result_name, sym, pc,
+			      arch, registers_used, addr_size, op_ptr, op_end,
+			      NULL, per_cu);
+}
+
+/* See compile.h.  */
+
+void
+compile_dwarf_bounds_to_c (struct ui_file *stream,
+			   const char *result_name,
+			   const struct dynamic_prop *prop,
+			   struct symbol *sym, CORE_ADDR pc,
+			   struct gdbarch *arch, unsigned char *registers_used,
+			   unsigned int addr_size,
+			   const gdb_byte *op_ptr, const gdb_byte *op_end,
+			   struct dwarf2_per_cu_data *per_cu)
+{
+  do_compile_dwarf_expr_to_c (2, stream, "unsigned long ", result_name,
+			      sym, pc, arch, registers_used,
+			      addr_size, op_ptr, op_end, NULL, per_cu);
+}
diff --git a/gdb/compile/compile-object-load.c b/gdb/compile/compile-object-load.c
new file mode 100644
index 0000000..1df5201
--- /dev/null
+++ b/gdb/compile/compile-object-load.c
@@ -0,0 +1,586 @@
+/* Load module for 'compile' command.
+
+   Copyright (C) 2014 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include "defs.h"
+#include "compile-object-load.h"
+#include "compile-internal.h"
+#include "command.h"
+#include "objfiles.h"
+#include "gdbcore.h"
+#include "readline/tilde.h"
+#include "bfdlink.h"
+#include "gdbcmd.h"
+#include "regcache.h"
+#include "inferior.h"
+#include "compile.h"
+
+/* Helper data for setup_sections.  */
+
+struct setup_sections_data
+{
+  /* Size of all recent sections with matching LAST_PROT.  */
+  CORE_ADDR last_size;
+
+  /* First section matching LAST_PROT.  */
+  asection *last_section_first;
+
+  /* Memory protection like the prot parameter of gdbarch_infcall_mmap. */
+  unsigned last_prot;
+
+  /* Maximum of alignments of all sections matching LAST_PROT.
+     This value is always at least 1.  This value is always a power of 2.  */
+  CORE_ADDR last_max_alignment;
+};
+
+/* Place all ABFD sections next to each other obeying all constraints.  */
+
+static void
+setup_sections (bfd *abfd, asection *sect, void *data_voidp)
+{
+  struct setup_sections_data *data = data_voidp;
+  CORE_ADDR alignment;
+  unsigned prot;
+
+  if (sect != NULL)
+    {
+      /* It is required by later bfd_get_relocated_section_contents.  */
+      if (sect->output_section == NULL)
+	sect->output_section = sect;
+
+      if ((bfd_get_section_flags (abfd, sect) & SEC_ALLOC) == 0)
+	return;
+
+      // Make the memory always readable.
+      prot = 4;
+      if ((bfd_get_section_flags (abfd, sect) & SEC_READONLY) == 0)
+	prot |= 2;
+      if ((bfd_get_section_flags (abfd, sect) & SEC_CODE) != 0)
+	prot |= 1;
+
+      if (compile_debug)
+	fprintf_unfiltered (gdb_stdout,
+			    "module \"%s\" section \"%s\" size %s prot %u\n",
+			    bfd_get_filename (abfd),
+			    bfd_get_section_name (abfd, sect),
+			    paddress (target_gdbarch (),
+				      bfd_get_section_size (sect)),
+			    prot);
+    }
+  else
+    prot = -1;
+
+  if (sect == NULL
+      || (data->last_prot != prot && bfd_get_section_size (sect) != 0))
+    {
+      CORE_ADDR addr;
+      asection *sect_iter;
+
+      if (data->last_size != 0)
+	{
+	  addr = gdbarch_infcall_mmap (target_gdbarch (), data->last_size,
+				       data->last_prot);
+	  if (compile_debug)
+	    fprintf_unfiltered (gdb_stdout,
+				"allocated %s bytes at %s prot %u\n",
+				paddress (target_gdbarch (), data->last_size),
+				paddress (target_gdbarch (), addr),
+				data->last_prot);
+	}
+      else
+	addr = 0;
+
+      if ((addr & (data->last_max_alignment - 1)) != 0)
+	error (_("Inferior compiled module address %s "
+		 "is not aligned to BFD required %s."),
+	       paddress (target_gdbarch (), addr),
+	       paddress (target_gdbarch (), data->last_max_alignment));
+
+      for (sect_iter = data->last_section_first; sect_iter != sect;
+	   sect_iter = sect_iter->next)
+	if ((bfd_get_section_flags (abfd, sect_iter) & SEC_ALLOC) != 0)
+	  bfd_set_section_vma (abfd, sect_iter,
+			       addr + bfd_get_section_vma (abfd, sect_iter));
+
+      data->last_size = 0;
+      data->last_section_first = sect;
+      data->last_prot = prot;
+      data->last_max_alignment = 1;
+    }
+
+  if (sect == NULL)
+    return;
+
+  alignment = ((CORE_ADDR) 1) << bfd_get_section_alignment (abfd, sect);
+  data->last_max_alignment = max (data->last_max_alignment, alignment);
+
+  data->last_size = (data->last_size + alignment - 1) & -alignment;
+
+  bfd_set_section_vma (abfd, sect, data->last_size);
+
+  data->last_size += bfd_get_section_size (sect);
+  data->last_size = (data->last_size + alignment - 1) & -alignment;
+}
+
+/* Helper for link_callbacks callbacks vector.  */
+
+static bfd_boolean
+link_callbacks_multiple_definition (struct bfd_link_info *link_info,
+				    struct bfd_link_hash_entry *h, bfd *nbfd,
+				    asection *nsec, bfd_vma nval)
+{
+  bfd *abfd = link_info->input_bfds;
+
+  if (link_info->allow_multiple_definition)
+    return TRUE;
+  warning (_("Compiled module \"%s\": multiple symbol definitions: %s\n"),
+	   bfd_get_filename (abfd), h->root.string);
+  return FALSE;
+}
+
+/* Helper for link_callbacks callbacks vector.  */
+
+static bfd_boolean
+link_callbacks_warning (struct bfd_link_info *link_info, const char *xwarning,
+                        const char *symbol, bfd *abfd, asection *section,
+			bfd_vma address)
+{
+  warning (_("Compiled module \"%s\" section \"%s\": warning: %s\n"),
+	   bfd_get_filename (abfd), bfd_get_section_name (abfd, section),
+	   xwarning);
+  /* Maybe permit running as a module?  */
+  return FALSE;
+}
+
+/* Helper for link_callbacks callbacks vector.  */
+
+static bfd_boolean
+link_callbacks_undefined_symbol (struct bfd_link_info *link_info,
+				 const char *name, bfd *abfd, asection *section,
+				 bfd_vma address, bfd_boolean is_fatal)
+{
+  warning (_("Cannot resolve relocation to \"%s\" "
+	     "from compiled module \"%s\" section \"%s\"."),
+	   name, bfd_get_filename (abfd), bfd_get_section_name (abfd, section));
+  return FALSE;
+}
+
+/* Helper for link_callbacks callbacks vector.  */
+
+static bfd_boolean
+link_callbacks_reloc_overflow (struct bfd_link_info *link_info,
+			       struct bfd_link_hash_entry *entry,
+			       const char *name, const char *reloc_name,
+			       bfd_vma addend, bfd *abfd, asection *section,
+			       bfd_vma address)
+{
+  /* TRUE is required for intra-module relocations.  */
+  return TRUE;
+}
+
+/* Helper for link_callbacks callbacks vector.  */
+
+static bfd_boolean
+link_callbacks_reloc_dangerous (struct bfd_link_info *link_info,
+				const char *message, bfd *abfd,
+				asection *section, bfd_vma address)
+{
+  warning (_("Compiled module \"%s\" section \"%s\": dangerous "
+	     "relocation: %s\n"),
+	   bfd_get_filename (abfd), bfd_get_section_name (abfd, section),
+	   message);
+  return FALSE;
+}
+
+/* Helper for link_callbacks callbacks vector.  */
+
+static bfd_boolean
+link_callbacks_unattached_reloc (struct bfd_link_info *link_info,
+				 const char *name, bfd *abfd, asection *section,
+				 bfd_vma address)
+{
+  warning (_("Compiled module \"%s\" section \"%s\": unattached "
+	     "relocation: %s\n"),
+	   bfd_get_filename (abfd), bfd_get_section_name (abfd, section),
+	   name);
+  return FALSE;
+}
+
+/* Helper for link_callbacks callbacks vector.  */
+
+static void
+link_callbacks_einfo (const char *fmt, ...)
+{
+  struct cleanup *cleanups;
+  va_list ap;
+  char *str;
+
+  va_start (ap, fmt);
+  str = xstrvprintf (fmt, ap);
+  va_end (ap);
+  cleanups = make_cleanup (xfree, str);
+
+  warning (_("Compile module: warning: %s\n"), str);
+
+  do_cleanups (cleanups);
+}
+
+/* Helper for bfd_get_relocated_section_contents.
+   Only these symbols are set by bfd_simple_get_relocated_section_contents
+   but bfd/ seems to use even the NULL ones without checking them first.  */
+
+static const struct bfd_link_callbacks link_callbacks =
+{
+  NULL, /* add_archive_element */
+  link_callbacks_multiple_definition, /* multiple_definition */
+  NULL, /* multiple_common */
+  NULL, /* add_to_set */
+  NULL, /* constructor */
+  link_callbacks_warning, /* warning */
+  link_callbacks_undefined_symbol, /* undefined_symbol */
+  link_callbacks_reloc_overflow, /* reloc_overflow */
+  link_callbacks_reloc_dangerous, /* reloc_dangerous */
+  link_callbacks_unattached_reloc, /* unattached_reloc */
+  NULL, /* notice */
+  link_callbacks_einfo, /* einfo */
+  NULL, /* info */
+  NULL, /* minfo */
+  NULL, /* override_segment_assignment */
+};
+
+struct link_hash_table_cleanup_data
+{
+  bfd *abfd;
+  bfd *link_next;
+};
+
+/* Cleanup callback for struct bfd_link_info.  */
+
+static void
+link_hash_table_free (void *d)
+{
+  struct link_hash_table_cleanup_data *data = d;
+
+  if (data->abfd->is_linker_output)
+    (*data->abfd->link.hash->hash_table_free) (data->abfd);
+  data->abfd->link.next = data->link_next;
+}
+
+/* Relocate and store into inferior memory each section SECT of ABFD.  */
+
+static void
+copy_sections (bfd *abfd, asection *sect, void *data)
+{
+  asymbol **symbol_table = data;
+  bfd_byte *sect_data, *sect_data_got;
+  struct cleanup *cleanups;
+  struct bfd_link_info link_info;
+  struct bfd_link_order link_order;
+  CORE_ADDR inferior_addr;
+  struct link_hash_table_cleanup_data cleanup_data;
+
+  if ((bfd_get_section_flags (abfd, sect) & (SEC_ALLOC | SEC_LOAD))
+      != (SEC_ALLOC | SEC_LOAD))
+    return;
+
+  if (bfd_get_section_size (sect) == 0)
+    return;
+
+  /* Mostly a copy of bfd_simple_get_relocated_section_contents which GDB
+     cannot use as it does not report relocations to undefined symbols.  */
+  memset (&link_info, 0, sizeof (link_info));
+  link_info.output_bfd = abfd;
+  link_info.input_bfds = abfd;
+  link_info.input_bfds_tail = &abfd->link.next;
+
+  cleanup_data.abfd = abfd;
+  cleanup_data.link_next = abfd->link.next;
+
+  abfd->link.next = NULL;
+  link_info.hash = bfd_link_hash_table_create (abfd);
+
+  cleanups = make_cleanup (link_hash_table_free, &cleanup_data);
+  link_info.callbacks = &link_callbacks;
+
+  memset (&link_order, 0, sizeof (link_order));
+  link_order.next = NULL;
+  link_order.type = bfd_indirect_link_order;
+  link_order.offset = 0;
+  link_order.size = bfd_get_section_size (sect);
+  link_order.u.indirect.section = sect;
+
+  sect_data = xmalloc (bfd_get_section_size (sect));
+  make_cleanup (xfree, sect_data);
+
+  sect_data_got = bfd_get_relocated_section_contents (abfd, &link_info,
+						      &link_order, sect_data,
+						      FALSE, symbol_table);
+
+  if (sect_data_got == NULL)
+    error (_("Cannot map compiled module \"%s\" section \"%s\": %s"),
+	   bfd_get_filename (abfd), bfd_get_section_name (abfd, sect),
+	   bfd_errmsg (bfd_get_error ()));
+  gdb_assert (sect_data_got == sect_data);
+
+  inferior_addr = bfd_get_section_vma (abfd, sect);
+  if (0 != target_write_memory (inferior_addr, sect_data,
+				bfd_get_section_size (sect)))
+    error (_("Cannot write compiled module \"%s\" section \"%s\" "
+	     "to inferior memory range %s-%s."),
+	   bfd_get_filename (abfd), bfd_get_section_name (abfd, sect),
+	   paddress (target_gdbarch (), inferior_addr),
+	   paddress (target_gdbarch (),
+		     inferior_addr + bfd_get_section_size (sect)));
+
+  do_cleanups (cleanups);
+}
+
+/* Fetch the type of first parameter of GCC_FE_WRAPPER_FUNCTION.
+   Return NULL if GCC_FE_WRAPPER_FUNCTION has no parameters.
+   Throw an error otherwise.  */
+
+static struct type *
+get_regs_type (struct objfile *objfile)
+{
+  struct symbol *func_sym;
+  struct type *func_type, *regsp_type, *regs_type;
+
+  func_sym = lookup_global_symbol_from_objfile (objfile,
+						GCC_FE_WRAPPER_FUNCTION,
+						VAR_DOMAIN);
+  if (func_sym == NULL)
+    error (_("Cannot find function \"%s\" in compiled module \"%s\"."),
+	   GCC_FE_WRAPPER_FUNCTION, objfile_name (objfile));
+
+  func_type = SYMBOL_TYPE (func_sym);
+  if (TYPE_CODE (func_type) != TYPE_CODE_FUNC)
+    error (_("Invalid type code %d of function \"%s\" in compiled "
+	     "module \"%s\"."),
+	   TYPE_CODE (func_type), GCC_FE_WRAPPER_FUNCTION,
+	   objfile_name (objfile));
+
+  /* No register parameter present.  */
+  if (TYPE_NFIELDS (func_type) == 0)
+    return NULL;
+
+  if (TYPE_NFIELDS (func_type) != 1)
+    error (_("Invalid %d parameters of function \"%s\" in compiled "
+	     "module \"%s\"."),
+	   TYPE_NFIELDS (func_type), GCC_FE_WRAPPER_FUNCTION,
+	   objfile_name (objfile));
+
+  regsp_type = check_typedef (TYPE_FIELD_TYPE (func_type, 0));
+  if (TYPE_CODE (regsp_type) != TYPE_CODE_PTR)
+    error (_("Invalid type code %d of first parameter of function \"%s\" "
+	     "in compiled module \"%s\"."),
+	   TYPE_CODE (regsp_type), GCC_FE_WRAPPER_FUNCTION,
+	   objfile_name (objfile));
+
+  regs_type = check_typedef (TYPE_TARGET_TYPE (regsp_type));
+  if (TYPE_CODE (regs_type) != TYPE_CODE_STRUCT)
+    error (_("Invalid type code %d of dereferenced first parameter "
+	     "of function \"%s\" in compiled module \"%s\"."),
+	   TYPE_CODE (regs_type), GCC_FE_WRAPPER_FUNCTION,
+	   objfile_name (objfile));
+
+  return regs_type;
+}
+
+/* Store all inferior registers required by REGS_TYPE to inferior memory
+   starting at inferior address REGS_BASE.  */
+
+static void
+store_regs (struct type *regs_type, CORE_ADDR regs_base)
+{
+  struct gdbarch *gdbarch = target_gdbarch ();
+  struct regcache *regcache = get_thread_regcache (inferior_ptid);
+  int fieldno;
+
+  for (fieldno = 0; fieldno < TYPE_NFIELDS (regs_type); fieldno++)
+    {
+      const char *reg_name = TYPE_FIELD_NAME (regs_type, fieldno);
+      ULONGEST reg_bitpos = TYPE_FIELD_BITPOS (regs_type, fieldno);
+      ULONGEST reg_bitsize = TYPE_FIELD_BITSIZE (regs_type, fieldno);
+      ULONGEST reg_offset;
+      struct type *reg_type = check_typedef (TYPE_FIELD_TYPE (regs_type,
+							      fieldno));
+      ULONGEST reg_size = TYPE_LENGTH (reg_type);
+      int regnum;
+      struct value *regval;
+      CORE_ADDR inferior_addr;
+
+      if (strcmp (reg_name, COMPILE_I_SIMPLE_REGISTER_DUMMY) == 0)
+	continue;
+
+      if ((reg_bitpos % 8) != 0 || reg_bitsize != 0)
+	error (_("Invalid register \"%s\" position %s bits or size %s bits"),
+	       reg_name, pulongest (reg_bitpos), pulongest (reg_bitsize));
+      reg_offset = reg_bitpos / 8;
+
+      if (TYPE_CODE (reg_type) != TYPE_CODE_INT
+	  && TYPE_CODE (reg_type) != TYPE_CODE_PTR)
+	error (_("Invalid register \"%s\" type code %d"), reg_name,
+	       TYPE_CODE (reg_type));
+
+      regnum = compile_register_name_demangle (gdbarch, reg_name);
+
+      regval = value_from_register (reg_type, regnum, get_current_frame ());
+      if (value_optimized_out (regval))
+	error (_("Register \"%s\" is optimized out."), reg_name);
+      if (!value_entirely_available (regval))
+	error (_("Register \"%s\" is not available."), reg_name);
+
+      inferior_addr = regs_base + reg_offset;
+      if (0 != target_write_memory (inferior_addr, value_contents (regval),
+				    reg_size))
+	error (_("Cannot write register \"%s\" to inferior memory at %s."),
+	       reg_name, paddress (gdbarch, inferior_addr));
+    }
+}
+
+/* Load OBJECT_FILE into inferior memory.  Throw an error otherwise.
+   Caller must fully dispose the return value by calling compile_object_run.
+   SOURCE_FILE's copy is stored into the returned object.
+   Caller should free both OBJECT_FILE and SOURCE_FILE immediatelly after this
+   function returns.  */
+
+struct compile_module *
+compile_object_load (const char *object_file, const char *source_file)
+{
+  struct cleanup *cleanups, *cleanups_free_objfile;
+  bfd *abfd;
+  struct setup_sections_data setup_sections_data;
+  CORE_ADDR addr, func_addr, regs_addr;
+  struct bound_minimal_symbol bmsym;
+  long storage_needed;
+  asymbol **symbol_table, **symp;
+  long number_of_symbols, missing_symbols;
+  struct type *dptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
+  unsigned dptr_type_len = TYPE_LENGTH (dptr_type);
+  struct compile_module *retval;
+  struct type *regs_type;
+  char *filename, **matching;
+  struct objfile *objfile;
+
+  filename = tilde_expand (object_file);
+  cleanups = make_cleanup (xfree, filename);
+
+  abfd = gdb_bfd_open (filename, gnutarget, -1);
+  if (abfd == NULL)
+    error (_("\"%s\": could not open as compiled module: %s"),
+          filename, bfd_errmsg (bfd_get_error ()));
+  make_cleanup_bfd_unref (abfd);
+
+  if (!bfd_check_format_matches (abfd, bfd_object, &matching))
+    error (_("\"%s\": not in loadable format: %s"),
+          filename, gdb_bfd_errmsg (bfd_get_error (), matching));
+
+  if ((bfd_get_file_flags (abfd) & (EXEC_P | DYNAMIC)) != 0)
+    error (_("\"%s\": not in object format."), filename);
+
+  setup_sections_data.last_size = 0;
+  setup_sections_data.last_section_first = abfd->sections;
+  setup_sections_data.last_prot = -1;
+  setup_sections_data.last_max_alignment = 1;
+  bfd_map_over_sections (abfd, setup_sections, &setup_sections_data);
+  setup_sections (abfd, NULL, &setup_sections_data);
+
+  storage_needed = bfd_get_symtab_upper_bound (abfd);
+  if (storage_needed < 0)
+    error (_("Cannot read symbols of compiled module \"%s\": %s"),
+          filename, bfd_errmsg (bfd_get_error ()));
+
+  /* SYMFILE_VERBOSE is not passed even if FROM_TTY, user is not interested in
+     "Reading symbols from ..." message for automatically generated file.  */
+  objfile = symbol_file_add_from_bfd (abfd, filename, 0, NULL, 0, NULL);
+  cleanups_free_objfile = make_cleanup_free_objfile (objfile);
+
+  bmsym = lookup_minimal_symbol_text (GCC_FE_WRAPPER_FUNCTION, objfile);
+  if (bmsym.minsym == NULL || MSYMBOL_TYPE (bmsym.minsym) == mst_file_text)
+    error (_("Could not find symbol \"%s\" of compiled module \"%s\"."),
+	   GCC_FE_WRAPPER_FUNCTION, filename);
+  func_addr = BMSYMBOL_VALUE_ADDRESS (bmsym);
+
+  /* The memory may be later needed
+     by bfd_generic_get_relocated_section_contents
+     called from default_symfile_relocate.  */
+  symbol_table = obstack_alloc (&objfile->objfile_obstack, storage_needed);
+  number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
+  if (number_of_symbols < 0)
+    error (_("Cannot parse symbols of compiled module \"%s\": %s"),
+          filename, bfd_errmsg (bfd_get_error ()));
+
+  missing_symbols = 0;
+  for (symp = symbol_table; symp < symbol_table + number_of_symbols; symp++)
+    {
+      asymbol *sym = *symp;
+
+      if (sym->flags != 0)
+	continue;
+      if (compile_debug)
+	fprintf_unfiltered (gdb_stdout,
+			    "lookup undefined ELF symbol \"%s\"\n",
+			    sym->name);
+      sym->flags = BSF_GLOBAL;
+      sym->section = bfd_abs_section_ptr;
+      if (strcmp (sym->name, "_GLOBAL_OFFSET_TABLE_") == 0)
+	{
+	  sym->value = 0;
+	  continue;
+	}
+      bmsym = lookup_minimal_symbol (sym->name, NULL, NULL);
+      switch (bmsym.minsym == NULL
+	      ? mst_unknown : MSYMBOL_TYPE (bmsym.minsym))
+	{
+	case mst_text:
+	  sym->value = BMSYMBOL_VALUE_ADDRESS (bmsym);
+	  break;
+	default:
+	  warning (_("Could not find symbol \"%s\" "
+		     "for compiled module \"%s\"."),
+		   sym->name, filename);
+	  missing_symbols++;
+	}
+    }
+  if (missing_symbols)
+    error (_("%ld symbols were missing, cannot continue."), missing_symbols);
+
+  bfd_map_over_sections (abfd, copy_sections, symbol_table);
+
+  regs_type = get_regs_type (objfile);
+  if (regs_type == NULL)
+    regs_addr = 0;
+  else
+    {
+      /* Use read-only non-executable memory protection.  */
+      regs_addr = gdbarch_infcall_mmap (target_gdbarch (),
+					TYPE_LENGTH (regs_type), 4);
+      gdb_assert (regs_addr != 0);
+      store_regs (regs_type, regs_addr);
+    }
+
+  discard_cleanups (cleanups_free_objfile);
+  do_cleanups (cleanups);
+
+  retval = xmalloc (sizeof (*retval));
+  retval->objfile = objfile;
+  retval->source_file = xstrdup (source_file);
+  retval->func_addr = func_addr;
+  retval->regs_addr = regs_addr;
+  return retval;
+}
diff --git a/gdb/compile/compile-object-load.h b/gdb/compile/compile-object-load.h
new file mode 100644
index 0000000..850111e
--- /dev/null
+++ b/gdb/compile/compile-object-load.h
@@ -0,0 +1,39 @@
+/* Header file to load module for 'compile' command.
+   Copyright (C) 2014 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#ifndef GDB_COMPILE_OBJECT_LOAD_H
+#define GDB_COMPILE_OBJECT_LOAD_H
+
+struct compile_module
+{
+  /* objfile for the compiled module.  */
+  struct objfile *objfile;
+
+  /* .c file OBJFILE was built from.  It needs to be xfree-d.  */
+  char *source_file;
+
+  /* Inferior function address.  */
+  CORE_ADDR func_addr;
+
+  /* Inferior registers address or NULL if the inferior function does not
+     require any.  */
+  CORE_ADDR regs_addr;
+};
+
+extern struct compile_module *compile_object_load (const char *object_file,
+						   const char *source_file);
+
+#endif /* GDB_COMPILE_OBJECT_LOAD_H */
diff --git a/gdb/compile/compile-object-run.c b/gdb/compile/compile-object-run.c
new file mode 100644
index 0000000..b7c4c4d
--- /dev/null
+++ b/gdb/compile/compile-object-run.c
@@ -0,0 +1,138 @@
+/* Call module for 'compile' command.
+
+   Copyright (C) 2014 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include "defs.h"
+#include "compile-object-run.h"
+#include "value.h"
+#include "infcall.h"
+#include "objfiles.h"
+#include "compile-internal.h"
+#include "dummy-frame.h"
+
+/* Helper for do_module_cleanup.  */
+
+struct do_module_cleanup
+{
+  /* Boolean to set true upon a call of do_module_cleanup.
+     The pointer may be NULL.  */
+  int *executedp;
+
+  /* .c file OBJFILE was built from.  It needs to be xfree-d.  */
+  char *source_file;
+
+  /* objfile_name of our objfile.  */
+  char objfile_name_string[1];
+};
+
+/* Cleanup everything after the inferior function dummy frame gets
+   discarded.  */
+
+static dummy_frame_dtor_ftype do_module_cleanup;
+static void
+do_module_cleanup (void *arg)
+{
+  struct do_module_cleanup *data = arg;
+  struct objfile *objfile;
+
+  if (data->executedp != NULL)
+    *data->executedp = 1;
+
+  ALL_OBJFILES (objfile)
+    if ((objfile->flags & OBJF_USERLOADED) == 0
+        && (strcmp (objfile_name (objfile), data->objfile_name_string) == 0))
+      {
+	free_objfile (objfile);
+
+	/* It may be a bit too pervasive in this dummy_frame dtor callback.  */
+	clear_symtab_users (0);
+
+	break;
+      }
+
+  /* Delete the .c file.  */
+  unlink (data->source_file);
+  xfree (data->source_file);
+
+  /* Delete the .o file.  */
+  unlink (data->objfile_name_string);
+  xfree (data);
+}
+
+/* Perform inferior call of MODULE.  This function may throw an error.
+   This function may leave files referenced by MODULE on disk until
+   the inferior call dummy frame is discarded.  This function may throw errors.
+   Thrown errors and left MODULE files are unrelated events.  Caller must no
+   longer touch MODULE's memory after this function has been called.  */
+
+void
+compile_object_run (struct compile_module *module)
+{
+  struct value *func_val;
+  struct frame_id dummy_id;
+  struct cleanup *cleanups;
+  struct do_module_cleanup *data;
+  volatile struct gdb_exception ex;
+  const char *objfile_name_s = objfile_name (module->objfile);
+  int dtor_found, executed = 0;
+  CORE_ADDR func_addr = module->func_addr;
+  CORE_ADDR regs_addr = module->regs_addr;
+
+  data = xmalloc (sizeof (*data) + strlen (objfile_name_s));
+  data->executedp = &executed;
+  data->source_file = xstrdup (module->source_file);
+  strcpy (data->objfile_name_string, objfile_name_s);
+
+  xfree (module->source_file);
+  xfree (module);
+
+  TRY_CATCH (ex, RETURN_MASK_ERROR)
+    {
+      func_val = value_from_pointer
+		 (builtin_type (target_gdbarch ())->builtin_func_ptr,
+		  func_addr);
+
+      if (regs_addr == 0)
+	call_function_by_hand_dummy (func_val, 0, NULL,
+				     do_module_cleanup, data);
+      else
+	{
+	  struct value *arg_val;
+
+	  arg_val = value_from_pointer
+		    (builtin_type (target_gdbarch ())->builtin_func_ptr,
+		     regs_addr);
+	  call_function_by_hand_dummy (func_val, 1, &arg_val,
+				       do_module_cleanup, data);
+	}
+    }
+  dtor_found = find_dummy_frame_dtor (do_module_cleanup, data);
+  if (!executed)
+    data->executedp = NULL;
+  if (ex.reason >= 0)
+    gdb_assert (!dtor_found && executed);
+  else
+    {
+      /* In the case od DTOR_FOUND or in the case of EXECUTED nothing
+	 needs to be done.  */
+      gdb_assert (!(dtor_found && executed));
+      if (!dtor_found && !executed)
+	do_module_cleanup (data);
+      throw_exception (ex);
+    }
+}
diff --git a/gdb/compile/compile-object-run.h b/gdb/compile/compile-object-run.h
new file mode 100644
index 0000000..71ba077
--- /dev/null
+++ b/gdb/compile/compile-object-run.h
@@ -0,0 +1,24 @@
+/* Header file to call module for 'compile' command.
+   Copyright (C) 2014 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#ifndef GDB_COMPILE_OBJECT_RUN_H
+#define GDB_COMPILE_OBJECT_RUN_H
+
+#include "compile-object-load.h"
+
+extern void compile_object_run (struct compile_module *module);
+
+#endif /* GDB_COMPILE_OBJECT_RUN_H */
diff --git a/gdb/compile/compile.c b/gdb/compile/compile.c
new file mode 100644
index 0000000..93a4c0a
--- /dev/null
+++ b/gdb/compile/compile.c
@@ -0,0 +1,642 @@
+/* General Compile and inject code
+
+   Copyright (C) 2014 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include "defs.h"
+#include "interps.h"
+#include "ui-out.h"
+#include "command.h"
+#include "cli/cli-script.h"
+#include "cli/cli-utils.h"
+#include "completer.h"
+#include "gdbcmd.h"
+#include "compile.h"
+#include "compile-internal.h"
+#include "compile-object-load.h"
+#include "compile-object-run.h"
+#include "language.h"
+#include "frame.h"
+#include "source.h"
+#include "block.h"
+#include "arch-utils.h"
+#include "filestuff.h"
+#include "target.h"
+#include "osabi.h"
+
+\f
+
+/* Hold "compile" commands.  */
+
+static struct cmd_list_element *compile_command_list;
+
+/* Debug flag for "compile" commands.  */
+
+int compile_debug;
+
+/* Implement "show debug compile".  */
+
+static void
+show_compile_debug (struct ui_file *file, int from_tty,
+		    struct cmd_list_element *c, const char *value)
+{
+  fprintf_filtered (file, _("Compile debugging is %s.\n"), value);
+}
+
+\f
+
+/* Check *ARG for a "-raw" or "-r" argument.  Return 0 if not seen.
+   Return 1 if seen and update *ARG.  */
+
+static int
+check_raw_argument (char **arg)
+{
+  *arg = skip_spaces (*arg);
+
+  if (arg != NULL
+      && (check_for_argument (arg, "-raw", sizeof ("-raw") - 1)
+	  || check_for_argument (arg, "-r", sizeof ("-r") - 1)))
+      return 1;
+  return 0;
+}
+
+/* Handle the input from the 'compile file' command.  The "compile
+   file" command is used to evaluate an expression contained in a file
+   that may contain calls to the GCC compiler.  */
+
+static void
+compile_file_command (char *arg, int from_tty)
+{
+  enum compile_i_scope_types scope = COMPILE_I_SIMPLE_SCOPE;
+  char *buffer;
+  struct cleanup *cleanup;
+
+  cleanup = make_cleanup_restore_integer (&interpreter_async);
+  interpreter_async = 0;
+
+  /* Check the user did not just <enter> after command.  */
+  if (arg == NULL)
+    error (_("You must provide a filename for this command."));
+
+  /* Check if a raw (-r|-raw) argument is provided.  */
+  if (arg != NULL && check_raw_argument (&arg))
+    {
+      scope = COMPILE_I_RAW_SCOPE;
+      arg = skip_spaces (arg);
+    }
+
+  /* After processing arguments, check there is a filename at the end
+     of the command.  */
+  if (arg[0] == '\0')
+    error (_("You must provide a filename with the raw option set."));
+
+  if (arg[0] == '-')
+    error (_("Unknown argument specified."));
+
+  arg = skip_spaces (arg);
+  arg = gdb_abspath (arg);
+  make_cleanup (xfree, arg);
+  buffer = xstrprintf ("#include \"%s\"\n", arg);
+  make_cleanup (xfree, buffer);
+  eval_compile_command (NULL, buffer, scope);
+  do_cleanups (cleanup);
+}
+
+/* Handle the input from the 'compile code' command.  The
+   "compile code" command is used to evaluate an expression that may
+   contain calls to the GCC compiler.  The language expected in this
+   compile command is the language currently set in GDB.  */
+
+static void
+compile_code_command (char *arg, int from_tty)
+{
+  struct cleanup *cleanup;
+  enum compile_i_scope_types scope = COMPILE_I_SIMPLE_SCOPE;
+
+  cleanup = make_cleanup_restore_integer (&interpreter_async);
+  interpreter_async = 0;
+
+  if (arg != NULL && check_raw_argument (&arg))
+    {
+      scope = COMPILE_I_RAW_SCOPE;
+      arg = skip_spaces (arg);
+    }
+
+  arg = skip_spaces (arg);
+
+  if (arg != NULL && !check_for_argument (&arg, "--", sizeof ("--") - 1))
+    {
+      if (arg[0] == '-')
+	error (_("Unknown argument specified."));
+    }
+
+  if (arg && *arg)
+      eval_compile_command (NULL, arg, scope);
+  else
+    {
+      struct command_line *l = get_command_line (compile_control, "");
+
+      make_cleanup_free_command_lines (&l);
+      l->control_u.compile.scope = scope;
+      execute_control_command_untraced (l);
+    }
+
+  do_cleanups (cleanup);
+}
+
+/* A cleanup function to remove a directory and all its contents.  */
+
+static void
+do_rmdir (void *arg)
+{
+  char *zap = concat ("rm -rf ", arg, (char *) NULL);
+
+  system (zap);
+}
+
+/* Return the name of the temporary directory to use for .o files, and
+   arrange for the directory to be removed at shutdown.  */
+
+static const char *
+get_compile_file_tempdir (void)
+{
+  static char *tempdir_name;
+
+#define TEMPLATE "/tmp/gdbobj-XXXXXX"
+  char tname[sizeof (TEMPLATE)];
+
+  if (tempdir_name != NULL)
+    return tempdir_name;
+
+  strcpy (tname, TEMPLATE);
+#undef TEMPLATE
+  tempdir_name = mkdtemp (tname);
+  if (tempdir_name == NULL)
+    perror_with_name (_("Could not make temporary directory"));
+
+  tempdir_name = xstrdup (tempdir_name);
+  make_final_cleanup (do_rmdir, tempdir_name);
+  return tempdir_name;
+}
+
+/* Compute the names of source and object files to use.  The names are
+   allocated by malloc and should be freed by the caller.  */
+
+static void
+get_new_file_names (char **source_file, char **object_file)
+{
+  static int seq;
+  const char *dir = get_compile_file_tempdir ();
+
+  ++seq;
+  *source_file = xstrprintf ("%s%sout%d.c", dir, SLASH_STRING, seq);
+  *object_file = xstrprintf ("%s%sout%d.o", dir, SLASH_STRING, seq);
+}
+
+/* Get the block and PC at which to evaluate an expression.  */
+
+static const struct block *
+get_expr_block_and_pc (CORE_ADDR *pc)
+{
+  const struct block *block = get_selected_block (pc);
+
+  if (block == NULL)
+    {
+      struct symtab_and_line cursal = get_current_source_symtab_and_line ();
+
+      if (cursal.symtab)
+	block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (cursal.symtab), STATIC_BLOCK);
+      if (block != NULL)
+	*pc = BLOCK_START (block);
+    }
+  else
+    *pc = BLOCK_START (block);
+
+  return block;
+}
+
+/* Call gdb_buildargv, set its result for S into *ARGVP but calculate also the
+   number of parsed arguments into *ARGCP.  If gdb_buildargv has returned NULL
+   then *ARGCP is set to zero.  */
+
+static void
+build_argc_argv (const char *s, int *argcp, char ***argvp)
+{
+  *argvp = gdb_buildargv (s);
+  *argcp = countargv (*argvp);
+}
+
+/* String for 'set compile-args' and 'show compile-args'.  */
+static char *compile_args;
+
+/* Parsed form of COMPILE_ARGS.  COMPILE_ARGS_ARGV is NULL terminated.  */
+static int compile_args_argc;
+static char **compile_args_argv;
+
+/* Implement 'set compile-args'.  */
+
+static void
+set_compile_args (char *args, int from_tty, struct cmd_list_element *c)
+{
+  freeargv (compile_args_argv);
+  build_argc_argv (compile_args, &compile_args_argc, &compile_args_argv);
+}
+
+/* Implement 'show compile-args'.  */
+
+static void
+show_compile_args (struct ui_file *file, int from_tty,
+		   struct cmd_list_element *c, const char *value)
+{
+  fprintf_filtered (file, _("Compile command command-line arguments "
+			    "are \"%s\".\n"),
+		    value);
+}
+
+/* Append ARGC and ARGV (as parsed by build_argc_argv) to *ARGCP and *ARGVP.
+   ARGCP+ARGVP can be zero+NULL and also ARGC+ARGV can be zero+NULL.  */
+
+static void
+append_args (int *argcp, char ***argvp, int argc, char **argv)
+{
+  int argi;
+
+  *argvp = xrealloc (*argvp, (*argcp + argc + 1) * sizeof (**argvp));
+
+  for (argi = 0; argi < argc; argi++)
+    (*argvp)[(*argcp)++] = xstrdup (argv[argi]);
+  (*argvp)[(*argcp)] = NULL;
+}
+
+/* Return DW_AT_producer parsed for get_selected_frame () (if any).
+   Return NULL otherwise.
+
+   GCC already filters its command-line arguments only for the suitable ones to
+   put into DW_AT_producer - see GCC function gen_producer_string.  */
+
+static const char *
+get_selected_pc_producer_options (void)
+{
+  CORE_ADDR pc = get_frame_pc (get_selected_frame (NULL));
+  struct symtab *symtab = find_pc_symtab (pc);
+  const char *cs;
+
+  if (symtab == NULL || symtab->producer == NULL
+      || strncmp (symtab->producer, "GNU ", strlen ("GNU ")) != 0)
+    return NULL;
+
+  cs = symtab->producer;
+  while (*cs != 0 && *cs != '-')
+    cs = skip_spaces_const (skip_to_space_const (cs));
+  if (*cs != '-')
+    return NULL;
+  return cs;
+}
+
+/* Produce final vector of GCC compilation options.  First element is target
+   size ("-m64", "-m32" etc.), optionally followed by DW_AT_producer options
+   and then compile-args string GDB variable.  */
+
+static void
+get_args (const struct compile_instance *compiler, struct gdbarch *gdbarch,
+	  int *argcp, char ***argvp)
+{
+  const char *cs_producer_options;
+  int argc_compiler;
+  char **argv_compiler;
+
+  build_argc_argv (gdbarch_gcc_target_options (gdbarch),
+		   argcp, argvp);
+
+  cs_producer_options = get_selected_pc_producer_options ();
+  if (cs_producer_options != NULL)
+    {
+      int argc_producer;
+      char **argv_producer;
+
+      build_argc_argv (cs_producer_options, &argc_producer, &argv_producer);
+      append_args (argcp, argvp, argc_producer, argv_producer);
+      freeargv (argv_producer);
+    }
+
+  build_argc_argv (compiler->gcc_target_options,
+		   &argc_compiler, &argv_compiler);
+  append_args (argcp, argvp, argc_compiler, argv_compiler);
+  freeargv (argv_compiler);
+
+  append_args (argcp, argvp, compile_args_argc, compile_args_argv);
+}
+
+/* A cleanup function to destroy a gdb_gcc_instance.  */
+
+static void
+cleanup_compile_instance (void *arg)
+{
+  struct compile_instance *inst = arg;
+
+  inst->destroy (inst);
+}
+
+/* A cleanup function to unlink a file.  */
+
+static void
+cleanup_unlink_file (void *arg)
+{
+  const char *filename = arg;
+
+  unlink (filename);
+}
+
+/* A helper function suitable for use as the "print_callback" in the
+   compiler object.  */
+
+static void
+print_callback (void *ignore, const char *message)
+{
+  fputs_filtered (message, gdb_stderr);
+}
+
+/* Process the compilation request.  On success it returns the object
+   file name and *SOURCE_FILEP is set to source file name.  On an
+   error condition, error () is called.  The caller is responsible for
+   freeing both strings.  */
+
+static char *
+compile_to_object (struct command_line *cmd, char *cmd_string,
+		   enum compile_i_scope_types scope,
+		   char **source_filep)
+{
+  char *code;
+  char *source_file, *object_file;
+  struct compile_instance *compiler;
+  struct cleanup *cleanup, *inner_cleanup;
+  const struct block *expr_block;
+  CORE_ADDR trash_pc, expr_pc;
+  int argc;
+  char **argv;
+  int ok;
+  FILE *src;
+  struct gdbarch *gdbarch = get_current_arch ();
+  const char *os_rx;
+  const char *arch_rx;
+  char *triplet_rx;
+  char *error_message;
+
+  if (!target_has_execution)
+    error (_("The program must be running for the compile command to "\
+	     "work."));
+
+  expr_block = get_expr_block_and_pc (&trash_pc);
+  expr_pc = get_frame_address_in_block (get_selected_frame (NULL));
+
+  /* Set up instance and context for the compiler.  */
+  if (current_language->la_get_compile_instance == NULL)
+    error (_("No compiler support for this language."));
+  compiler = current_language->la_get_compile_instance ();
+  cleanup = make_cleanup (cleanup_compile_instance, compiler);
+
+  compiler->fe->ops->set_print_callback (compiler->fe, print_callback, NULL);
+
+  compiler->scope = scope;
+  compiler->block = expr_block;
+
+  /* From the provided expression, build a scope to pass to the
+     compiler.  */
+  if (cmd != NULL)
+    {
+      struct ui_file *stream = mem_fileopen ();
+      struct command_line *iter;
+
+      make_cleanup_ui_file_delete (stream);
+      for (iter = cmd->body_list[0]; iter; iter = iter->next)
+	{
+	  fputs_unfiltered (iter->line, stream);
+	  fputs_unfiltered ("\n", stream);
+	}
+
+      code = ui_file_xstrdup (stream, NULL);
+      make_cleanup (xfree, code);
+    }
+  else if (cmd_string != NULL)
+    code = cmd_string;
+  else
+    error (_("Neither a simple expression, or a multi-line specified."));
+
+  code = current_language->la_compute_program (compiler, code, gdbarch,
+					       expr_block, expr_pc);
+  make_cleanup (xfree, code);
+  if (compile_debug)
+    fprintf_unfiltered (gdb_stdout, "debug output:\n\n%s", code);
+
+  os_rx = osabi_triplet_regexp (gdbarch_osabi (gdbarch));
+  arch_rx = gdbarch_gnu_triplet_regexp (gdbarch);
+  triplet_rx = concat (arch_rx, "-[^-]*-", os_rx, (char *) NULL);
+  make_cleanup (xfree, triplet_rx);
+
+  /* Set compiler command-line arguments.  */
+  get_args (compiler, gdbarch, &argc, &argv);
+  make_cleanup_freeargv (argv);
+
+  error_message = compiler->fe->ops->set_arguments (compiler->fe, triplet_rx,
+						    argc, argv);
+  if (error_message != NULL)
+    {
+      make_cleanup (xfree, error_message);
+      error ("%s", error_message);
+    }
+
+  if (compile_debug)
+    {
+      int argi;
+
+      fprintf_unfiltered (gdb_stdout, "Passing %d compiler options:\n", argc);
+      for (argi = 0; argi < argc; argi++)
+	fprintf_unfiltered (gdb_stdout, "Compiler option %d: <%s>\n",
+			    argi, argv[argi]);
+    }
+
+  get_new_file_names (&source_file, &object_file);
+  inner_cleanup = make_cleanup (xfree, source_file);
+  make_cleanup (xfree, object_file);
+
+  src = gdb_fopen_cloexec (source_file, "w");
+  if (src == NULL)
+    perror_with_name (_("Could not open source file for writing"));
+  make_cleanup (cleanup_unlink_file, source_file);
+  if (fputs (code, src) == EOF)
+    perror_with_name (_("Could not write to source file"));
+  fclose (src);
+
+  if (compile_debug)
+    fprintf_unfiltered (gdb_stdout, "source file produced: %s\n\n",
+			source_file);
+
+  /* Call the compiler and start the compilation process.  */
+  compiler->fe->ops->set_source_file (compiler->fe, source_file);
+
+  if (!compiler->fe->ops->compile (compiler->fe, object_file,
+				   compile_debug))
+    error (_("Compilation failed."));
+
+  if (compile_debug)
+    fprintf_unfiltered (gdb_stdout, "object file produced: %s\n\n",
+			object_file);
+
+  discard_cleanups (inner_cleanup);
+  do_cleanups (cleanup);
+  *source_filep = source_file;
+  return object_file;
+}
+
+/* The "compile" prefix command.  */
+
+static void
+compile_command (char *args, int from_tty)
+{
+  /* If a sub-command is not specified to the compile prefix command,
+     assume it is a direct code compilation.  */
+  compile_code_command (args, from_tty);
+}
+
+/* See compile.h.  */
+
+void
+eval_compile_command (struct command_line *cmd, char *cmd_string,
+		      enum compile_i_scope_types scope)
+{
+  char *object_file, *source_file;
+
+  object_file = compile_to_object (cmd, cmd_string, scope, &source_file);
+  if (object_file != NULL)
+    {
+      struct cleanup *cleanup_xfree, *cleanup_unlink;
+      struct compile_module *compile_module;
+
+      cleanup_xfree = make_cleanup (xfree, object_file);
+      make_cleanup (xfree, source_file);
+      cleanup_unlink = make_cleanup (cleanup_unlink_file, object_file);
+      make_cleanup (cleanup_unlink_file, source_file);
+      compile_module = compile_object_load (object_file, source_file);
+      discard_cleanups (cleanup_unlink);
+      do_cleanups (cleanup_xfree);
+      compile_object_run (compile_module);
+    }
+}
+
+/* See compile/compile-internal.h.  */
+
+char *
+compile_register_name_mangled (struct gdbarch *gdbarch, int regnum)
+{
+  const char *regname = gdbarch_register_name (gdbarch, regnum);
+
+  return xstrprintf ("__%s", regname);
+}
+
+/* See compile/compile-internal.h.  */
+
+int
+compile_register_name_demangle (struct gdbarch *gdbarch,
+				 const char *regname)
+{
+  int regnum;
+
+  if (regname[0] != '_' || regname[1] != '_')
+    error (_("Invalid register name \"%s\"."), regname);
+  regname += 2;
+
+  for (regnum = 0; regnum < gdbarch_num_regs (gdbarch); regnum++)
+    if (strcmp (regname, gdbarch_register_name (gdbarch, regnum)) == 0)
+      return regnum;
+
+  error (_("Cannot find gdbarch register \"%s\"."), regname);
+}
+
+extern initialize_file_ftype _initialize_compile;
+
+void
+_initialize_compile (void)
+{
+  struct cmd_list_element *c = NULL;
+
+  add_prefix_cmd ("compile", class_obscure, compile_command,
+		  _("\
+Command to compile source code and inject it into the inferior."),
+		  &compile_command_list, "compile ", 1, &cmdlist);
+  add_com_alias ("expression", "compile", class_obscure, 0);
+
+  add_cmd ("code", class_obscure, compile_code_command,
+	   _("\
+Compile, inject, and execute code.\n\
+\n\
+Usage: compile code [-r|-raw] [--] [CODE]\n\
+-r|-raw: Suppress automatic 'void _gdb_expr () { CODE }' wrapping.\n\
+--: Do not parse any options beyond this delimiter.  All text to the\n\
+    right will be treated as source code.\n\
+\n\
+The source code may be specified as a simple one line expression, e.g.:\n\
+\n\
+    compile code printf(\"Hello world\\n\");\n\
+\n\
+Alternatively, you can type the source code interactively.\n\
+You can invoke this mode when no argument is given to the command\n\
+(i.e.,\"compile code\" is typed with nothing after it).  An\n\
+interactive prompt will be shown allowing you to enter multiple\n\
+lines of source code.  Type a line containing \"end\" to indicate\n\
+the end of the source code."),
+	   &compile_command_list);
+
+  c = add_cmd ("file", class_obscure, compile_file_command,
+	       _("\
+Evaluate a file containing source code.\n\
+\n\
+Usage: compile file [-r|-raw] [filename]\n\
+-r|-raw: Suppress automatic 'void _gdb_expr () { CODE }' wrapping."),
+	       &compile_command_list);
+  set_cmd_completer (c, filename_completer);
+
+  add_setshow_boolean_cmd ("compile", class_maintenance, &compile_debug, _("\
+Set compile command debugging."), _("\
+Show compile command debugging."), _("\
+When on, compile command debugging is enabled."),
+			   NULL, show_compile_debug,
+			   &setdebuglist, &showdebuglist);
+
+  add_setshow_string_cmd ("compile-args", class_support,
+			  &compile_args,
+			  _("Set compile command GCC command-line arguments"),
+			  _("Show compile command GCC command-line arguments"),
+			  _("\
+Use options like -I (include file directory) or ABI settings.\n\
+String quoting is parsed like in shell, for example:\n\
+  -mno-align-double \"-I/dir with a space/include\""),
+			  set_compile_args, show_compile_args, &setlist, &showlist);
+
+  /* Override flags possibly coming from DW_AT_producer.  */
+  compile_args = xstrdup ("-O0 -gdwarf-4"
+  /* We use -fPIC to ensure that we can reference properly.  Otherwise
+     on x86-64 a string constant's address might be truncated when gdb
+     loads the object; another approach would be -mcmodel=large, but
+     -fPIC seems more portable across back ends.  */
+			 " -fPIC"
+  /* We don't want warnings.  */
+			 " -w"
+  /* Override CU's possible -fstack-protector-strong.  */
+			 " -fno-stack-protector"
+  );
+  set_compile_args (compile_args, 0, NULL);
+}
diff --git a/gdb/compile/compile.h b/gdb/compile/compile.h
new file mode 100644
index 0000000..486361f
--- /dev/null
+++ b/gdb/compile/compile.h
@@ -0,0 +1,102 @@
+/* Header file for Compile and inject module.
+
+   Copyright (C) 2014 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#ifndef GDB_COMPILE_H
+#define GDB_COMPILE_H
+
+struct ui_file;
+struct gdbarch;
+struct dwarf2_per_cu_data;
+struct symbol;
+struct dynamic_prop;
+
+/* Public function that is called from compile_control case in the
+   expression command.  GDB returns either a CMD, or a CMD_STRING, but
+   never both.  */
+
+extern void eval_compile_command (struct command_line *cmd, char *cmd_string,
+				  enum compile_i_scope_types scope);
+
+/* Compile a DWARF location expression to C, suitable for use by the
+   compiler.
+
+   STREAM is the stream where the code should be written.
+
+   RESULT_NAME is the name of a variable in the resulting C code.  The
+   result of the expression will be assigned to this variable.
+
+   SYM is the symbol corresponding to this expression.
+   PC is the location at which the expression is being evaluated.
+   ARCH is the architecture to use.
+
+   REGISTERS_USED is an out parameter which is updated to note which
+   registers were needed by this expression.
+
+   ADDR_SIZE is the DWARF address size to use.
+
+   OPT_PTR and OP_END are the bounds of the DWARF expression.
+
+   PER_CU is the per-CU object used for looking up various other
+   things.  */
+
+extern void compile_dwarf_expr_to_c (struct ui_file *stream,
+				     const char *result_name,
+				     struct symbol *sym,
+				     CORE_ADDR pc,
+				     struct gdbarch *arch,
+				     unsigned char *registers_used,
+				     unsigned int addr_size,
+				     const gdb_byte *op_ptr,
+				     const gdb_byte *op_end,
+				     struct dwarf2_per_cu_data *per_cu);
+
+/* Compile a DWARF bounds expression to C, suitable for use by the
+   compiler.
+
+   STREAM is the stream where the code should be written.
+
+   RESULT_NAME is the name of a variable in the resulting C code.  The
+   result of the expression will be assigned to this variable.
+
+   PROP is the dynamic property for which we're compiling.
+
+   SYM is the symbol corresponding to this expression.
+   PC is the location at which the expression is being evaluated.
+   ARCH is the architecture to use.
+
+   REGISTERS_USED is an out parameter which is updated to note which
+   registers were needed by this expression.
+
+   ADDR_SIZE is the DWARF address size to use.
+
+   OPT_PTR and OP_END are the bounds of the DWARF expression.
+
+   PER_CU is the per-CU object used for looking up various other
+   things.  */
+
+extern void compile_dwarf_bounds_to_c (struct ui_file *stream,
+				       const char *result_name,
+				       const struct dynamic_prop *prop,
+				       struct symbol *sym, CORE_ADDR pc,
+				       struct gdbarch *arch,
+				       unsigned char *registers_used,
+				       unsigned int addr_size,
+				       const gdb_byte *op_ptr,
+				       const gdb_byte *op_end,
+				       struct dwarf2_per_cu_data *per_cu);
+
+#endif /* GDB_COMPILE_H */
diff --git a/gdb/d-lang.c b/gdb/d-lang.c
index e98138f..926ee77 100644
--- a/gdb/d-lang.c
+++ b/gdb/d-lang.c
@@ -231,6 +231,8 @@ static const struct language_defn d_language_defn =
   NULL,				/* la_get_symbol_name_cmp */
   iterate_over_symbols,
   &default_varobj_ops,
+  NULL,
+  NULL,
   LANG_MAGIC
 };
 
diff --git a/gdb/defs.h b/gdb/defs.h
index 1eb43eb..888af9b 100644
--- a/gdb/defs.h
+++ b/gdb/defs.h
@@ -55,6 +55,24 @@
 
 #include "host-defs.h"
 
+/* Scope types enumerator.  List the types of scopes the compiler will
+   accept.  */
+
+enum compile_i_scope_types
+  {
+    COMPILE_I_INVALID_SCOPE,
+
+    /* A simple scope.  Wrap an expression into a simple scope that
+       takes no arguments, returns no value, and uses the generic
+       function name "_gdb_expr". */
+
+    COMPILE_I_SIMPLE_SCOPE,
+
+    /* Do not wrap the expression,
+       it has to provide function "_gdb_expr" on its own.  */
+    COMPILE_I_RAW_SCOPE,
+  };
+
 /* Just in case they're not defined in stdio.h.  */
 
 #ifndef SEEK_SET
@@ -364,6 +382,7 @@ enum command_control_type
     if_control,
     commands_control,
     python_control,
+    compile_control,
     guile_control,
     while_stepping_control,
     invalid_control
@@ -377,6 +396,15 @@ struct command_line
     struct command_line *next;
     char *line;
     enum command_control_type control_type;
+    union
+      {
+	struct
+	  {
+	    enum compile_i_scope_types scope;
+	  }
+	compile;
+      }
+    control_u;
     /* * The number of elements in body_list.  */
     int body_count;
     /* * For composite commands, the nested lists of commands.  For
diff --git a/gdb/doc/ChangeLog b/gdb/doc/ChangeLog
index 2e619da..bcdd693 100644
--- a/gdb/doc/ChangeLog
+++ b/gdb/doc/ChangeLog
@@ -1,3 +1,8 @@
+2014-10-07  Phil Muldoon  <pmuldoon@redhat.com>
+
+	* gdb.texinfo (Altering): Update.
+	(Compiling and Injecting Code): New node.
+
 2014-10-30  Doug Evans  <dje@google.com>
 
 	* python.texi (Progspaces In Python): Document ability to add
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 15c2908..da0eacb 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -16493,6 +16493,7 @@ address, or even return prematurely from a function.
 * Returning::                   Returning from a function
 * Calling::                     Calling your program's functions
 * Patching::                    Patching your program
+* Compiling and Injecting Code:: Compiling and injecting code in @value{GDBN}
 @end menu
 
 @node Assignment
@@ -16915,6 +16916,230 @@ Display whether executable files and core files are opened for writing
 as well as reading.
 @end table
 
+@node Compiling and Injecting Code
+@section Compiling and injecting code in @value{GDBN}
+@cindex injecting code
+@cindex writing into executables
+@cindex compiling code
+
+@value{GDBN} supports on-demand compilation and code injection into
+programs running under @value{GDBN}.  GCC 5.0 or higher built with
+@code{libcc1.so} must be installed for this functionality to be enabled.
+This functionality is implemented with the following commands.
+
+@table @code
+@kindex compile code
+@item compile code @var{source}
+@itemx compile code -raw @var{--} @var{source}
+Compile @var{source} with the compiler language set as the current
+language in @value{GDBN} (@pxref{Languages}).  If compilation and
+injection is not supported with the current language specified in
+@value{GDBN}, or the compiler does not support this feature, an error
+message will be printed.  If @var{source} compiles and links
+successfully, @value{GDBN} will load the object-code emitted,
+and execute it within the context of the currently selected inferior.
+It is important to note that the compiled code is executed immediately.
+After execution, the compiled code is removed from @value{GDBN} and any
+new types or variables you have defined will be deleted.
+
+The command allows you to specify source code in two ways.  The simplest
+method is to provide a single line of code to the command.  E.g.:
+
+@smallexample
+compile code printf ("hello world\n");
+@end smallexample
+
+If you specify options on the command line as well as source code, they
+may conflict.  The @samp{@var{--}} delimiter can be used to separate options
+from actual source code.  E.g.:
+
+@smallexample
+compile code -r -- printf ("hello world\n");
+@end smallexample
+
+Alternatively you can enter source code as multiple lines of text.  To
+enter this mode, invoke the @samp{compile code} command without any text
+following the command.  This will start the multiple-line editor and
+allow you to type as many lines of source code as required.  When you
+have completed typing, enter @samp{end} on its own line to exit the
+editor.
+
+@smallexample
+compile code
+>printf ("hello\n");
+>printf ("world\n");
+>end
+@end smallexample
+
+Specifying @samp{-raw}, prohibits @value{GDBN} from wrapping the
+provided @var{source} in a callable scope.  In this case, you must
+specify the entry point of the code by defining a function named
+@code{_gdb_expr_}.  The @samp{-raw} code does not automatically
+access variables of the inferior, for their import you must use the
+@samp{#pragma} line first:
+
+@smallexample
+#pragma GCC user_expression
+@end smallexample
+
+@samp{#include} lines are better to be placed before the @samp{#pragma}
+line.  The use of @samp{-raw} is considered to be expert usage, and care
+should be taken when using it.
+
+@kindex compile file
+@item compile file @var{filename}
+@itemx compile file -raw @var{filename}
+Like @code{compile code}, but take the source code from @var{filename}.
+
+@smallexample
+compile file /home/user/example.c
+@end smallexample
+
+If the filename/path contains whitespace it must be enclosed in
+quotes.
+@end table
+
+There are a few caveats to keep in mind when using the @code{compile}
+command.  As the caveats are different per language, the table below
+highlights specific issues on a per language basis.
+
+@table @asis
+@item C code examples and caveats
+When the language in @value{GDBN} is set to @samp{C}, the compiler will
+attempt to compile the source code with a @samp{C} compiler.  The source
+code provided to the @code{compile} command will have much the same
+access to variables and types as it normally would if it were part of
+the program currently being debugged in @value{GDBN}.
+
+Below is a sample program that forms the basis of the examples that
+follow.  This program has been compiled and loaded into @value{GDBN},
+much like any other normal debugging session.
+
+@smallexample
+void function1 (void)
+@{
+   int i = 42;
+   printf ("function 1\n");
+@}
+
+void function2 (void)
+@{
+   int j = 12;
+   function1 ();
+@}
+
+int main(void)
+@{
+   int k = 6;
+   int *p;
+   function2 ();
+   return 0;
+@}
+@end smallexample
+
+For the purposes of the examples in this section, the program above has
+been compiled, loaded into @value{GDBN}, stopped at the function
+@code{main}, and @value{GDBN} is awaiting input from the user.
+
+To access variables and types for any program in @value{GDBN}, the
+program must be compiled and packaged with debug information.  The
+@code{compile} command is not an exception to this rule.  Without debug
+information, you can still use the @code{compile} command, but you will
+be very limited in what variables and types you can access.
+
+So with that in mind, the example above has been compiled with debug
+information enabled.  The @code{compile} command will have access to
+all variables and types (except those that may have been optimized
+out).  Currently, as @value{GDBN} has stopped the program in the
+@code{main} function, the @code{compile} command would have access to
+the variable @code{k}.  You could invoke the @code{compile} command
+and type some source code to set the value of @code{k}.  You can also
+read it, or do anything with that variable you would normally do in
+@code{C}.  Be aware that changes to inferior variables in the
+@code{compile} command are persistent.  In the following example:
+
+@smallexample
+compile code k = 3;
+@end smallexample
+
+The variable @code{k} is now 3.  It will remain that value until
+something else in the example program changes it, or another
+@code{compile} command changes it.
+
+Normal scope and access rules apply to source code compiled and injected
+by the @code{compile} command.  In the example, the variables @code{j}
+and @code{k} are not accessible; subsequent execution will bring these
+variables into scope, and later, code written and compiled with the
+@code{compile} command will be able to access them.  At this point the
+program is stopped in the @code{main} function so the following example:
+
+@smallexample
+compile code j = 3;
+@end smallexample
+
+would result in a compilation error, and @value{GDBN} would print that
+error to the console.
+
+You can create variables and types with the @code{compile} command as
+part of your source code.  Variables and types that are created as part
+of the @code{compile} command are not persistent, and only exist as
+long as the injected object code exists.  This example is valid:
+
+@smallexample
+compile code int ff = 5; printf ("ff is %d\n", ff);
+@end smallexample
+
+However, if you were to type the following into @value{GDBN} after that
+command has completed:
+
+@smallexample
+compile code printf ("ff is %d\n'', ff);
+@end smallexample
+
+A compiler error would be raised as the variable @code{ff} no longer
+exists.  Object code generated and injected by the @code{compile}
+command is removed on completion of the command.  Caution is advised
+when assigning variables belonging to the program with variables created
+in the @code{compile} command.  This example is valid:
+
+@smallexample
+compile code int ff = 5; k = ff;
+@end smallexample
+
+The @code{k} variable is assigned to the value of @code{ff}.  The variable
+@code{k} does not require the existence of @code{ff} to maintain the value
+it has been assigned.  Pointers and other types of references require
+particular care in assignment.  If the source code compiled with the
+@code{compile} command changed the address of a pointer in the example
+program, perhaps to a variable created in the @code{compile} command,
+that pointer would point to an invalid location when the command exits.
+The following example would likely cause issues with your debugged
+program:
+
+@smallexample
+compile code int ff = 5; p = &ff;
+@end smallexample
+
+In this example, @code{p} would point to @code{ff} when the
+@code{compile} command is executing the source code provided to it.
+However, as variables in the (example) program persist with their
+assigned values, the variable @code{p} would point to an invalid
+location when the command exists.  A general rule should be followed
+in that you should either assign @code{NULL} to any assigned pointers,
+or restore a valid location to the pointer before the command exits.
+
+Similar caution must be exercised with any types defined in
+@code{compile} command.  Types defined in the @code{compile} are also
+deleted when the command exits.  Therefore, if you cast a variable to a
+type defined in the @code{compile} command, care must be taken to ensure
+that any future need to resolve the type can be achieved.
+
+Variables that have been optimized away by the compiler are not
+accessible to the @code{compile} command.  Access to those variables
+will generate a compiler error which @value{GDBN} will print to the
+console.
+@end table
+
 @node GDB Files
 @chapter @value{GDBN} Files
 
diff --git a/gdb/dwarf2loc.c b/gdb/dwarf2loc.c
index 3a0c5ce..315eac9 100644
--- a/gdb/dwarf2loc.c
+++ b/gdb/dwarf2loc.c
@@ -37,6 +37,7 @@
 #include "dwarf2expr.h"
 #include "dwarf2loc.h"
 #include "dwarf2-frame.h"
+#include "compile/compile.h"
 
 extern int dwarf2_always_disassemble;
 
@@ -2506,6 +2507,42 @@ dwarf2_evaluate_property (const struct dynamic_prop *prop,
   return 0;
 }
 
+/* See dwarf2loc.h.  */
+
+void
+dwarf2_compile_property_to_c (struct ui_file *stream,
+			      const char *result_name,
+			      struct gdbarch *gdbarch,
+			      unsigned char *registers_used,
+			      const struct dynamic_prop *prop,
+			      CORE_ADDR pc,
+			      struct symbol *sym)
+{
+  struct dwarf2_property_baton *baton = prop->data.baton;
+  const gdb_byte *data;
+  size_t size;
+  struct dwarf2_per_cu_data *per_cu;
+
+  if (prop->kind == PROP_LOCEXPR)
+    {
+      data = baton->locexpr.data;
+      size = baton->locexpr.size;
+      per_cu = baton->locexpr.per_cu;
+    }
+  else
+    {
+      gdb_assert (prop->kind == PROP_LOCLIST);
+
+      data = dwarf2_find_location_expression (&baton->loclist, &size, pc);
+      per_cu = baton->loclist.per_cu;
+    }
+
+  compile_dwarf_bounds_to_c (stream, result_name, prop, sym, pc,
+			     gdbarch, registers_used,
+			     dwarf2_per_cu_addr_size (per_cu),
+			     data, data + size, per_cu);
+}
+
 \f
 /* Helper functions and baton for dwarf2_loc_desc_needs_frame.  */
 
@@ -4173,6 +4210,26 @@ locexpr_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
 			       dlbaton->per_cu);
 }
 
+/* symbol_computed_ops 'generate_c_location' method.  */
+
+static void
+locexpr_generate_c_location (struct symbol *sym, struct ui_file *stream,
+			     struct gdbarch *gdbarch,
+			     unsigned char *registers_used,
+			     CORE_ADDR pc, const char *result_name)
+{
+  struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (sym);
+  unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
+
+  if (dlbaton->size == 0)
+    error (_("symbol \"%s\" is optimized out"), SYMBOL_NATURAL_NAME (sym));
+
+  compile_dwarf_expr_to_c (stream, result_name,
+			   sym, pc, gdbarch, registers_used, addr_size,
+			   dlbaton->data, dlbaton->data + dlbaton->size,
+			   dlbaton->per_cu);
+}
+
 /* The set of location functions used with the DWARF-2 expression
    evaluator.  */
 const struct symbol_computed_ops dwarf2_locexpr_funcs = {
@@ -4181,7 +4238,8 @@ const struct symbol_computed_ops dwarf2_locexpr_funcs = {
   locexpr_read_needs_frame,
   locexpr_describe_location,
   0,	/* location_has_loclist */
-  locexpr_tracepoint_var_ref
+  locexpr_tracepoint_var_ref,
+  locexpr_generate_c_location
 };
 
 
@@ -4352,6 +4410,29 @@ loclist_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
 			       dlbaton->per_cu);
 }
 
+/* symbol_computed_ops 'generate_c_location' method.  */
+
+static void
+loclist_generate_c_location (struct symbol *sym, struct ui_file *stream,
+			     struct gdbarch *gdbarch,
+			     unsigned char *registers_used,
+			     CORE_ADDR pc, const char *result_name)
+{
+  struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (sym);
+  unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
+  const gdb_byte *data;
+  size_t size;
+
+  data = dwarf2_find_location_expression (dlbaton, &size, pc);
+  if (size == 0)
+    error (_("symbol \"%s\" is optimized out"), SYMBOL_NATURAL_NAME (sym));
+
+  compile_dwarf_expr_to_c (stream, result_name,
+			   sym, pc, gdbarch, registers_used, addr_size,
+			   data, data + size,
+			   dlbaton->per_cu);
+}
+
 /* The set of location functions used with the DWARF-2 expression
    evaluator and location lists.  */
 const struct symbol_computed_ops dwarf2_loclist_funcs = {
@@ -4360,7 +4441,8 @@ const struct symbol_computed_ops dwarf2_loclist_funcs = {
   loclist_read_needs_frame,
   loclist_describe_location,
   1,	/* location_has_loclist */
-  loclist_tracepoint_var_ref
+  loclist_tracepoint_var_ref,
+  loclist_generate_c_location
 };
 
 /* Provide a prototype to silence -Wmissing-prototypes.  */
diff --git a/gdb/dwarf2loc.h b/gdb/dwarf2loc.h
index 76fb45b4..e63b35b 100644
--- a/gdb/dwarf2loc.h
+++ b/gdb/dwarf2loc.h
@@ -111,6 +111,27 @@ int dwarf2_evaluate_property (const struct dynamic_prop *prop,
 			      CORE_ADDR address,
 			      CORE_ADDR *value);
 
+/* A helper for the compiler interface that compiles a single dynamic
+   property to C code.
+
+   STREAM is where the C code is to be written.
+   RESULT_NAME is the name of the generated variable.
+   GDBARCH is the architecture to use.
+   REGISTERS_USED is a bit-vector that is filled to note which
+   registers are required by the generated expression.
+   PROP is the property for which code is generated.
+   ADDRESS is the address at which the property is considered to be
+   evaluated.
+   SYM the originating symbol, used for error reporting.  */
+
+void dwarf2_compile_property_to_c (struct ui_file *stream,
+				   const char *result_name,
+				   struct gdbarch *gdbarch,
+				   unsigned char *registers_used,
+				   const struct dynamic_prop *prop,
+				   CORE_ADDR address,
+				   struct symbol *sym);
+
 CORE_ADDR dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
 				  unsigned int addr_index);
 
diff --git a/gdb/f-lang.c b/gdb/f-lang.c
index 52da04e..dd70798 100644
--- a/gdb/f-lang.c
+++ b/gdb/f-lang.c
@@ -275,6 +275,8 @@ const struct language_defn f_language_defn =
   NULL,				/* la_get_symbol_name_cmp */
   iterate_over_symbols,
   &default_varobj_ops,
+  NULL,
+  NULL,
   LANG_MAGIC
 };
 
diff --git a/gdb/go-lang.c b/gdb/go-lang.c
index 8cbc234..7b23fc6 100644
--- a/gdb/go-lang.c
+++ b/gdb/go-lang.c
@@ -597,6 +597,8 @@ static const struct language_defn go_language_defn =
   NULL,
   iterate_over_symbols,
   &default_varobj_ops,
+  NULL,
+  NULL,
   LANG_MAGIC
 };
 
diff --git a/gdb/jv-lang.c b/gdb/jv-lang.c
index 421e665..ed4aa80 100644
--- a/gdb/jv-lang.c
+++ b/gdb/jv-lang.c
@@ -1195,6 +1195,8 @@ const struct language_defn java_language_defn =
   NULL,				/* la_get_symbol_name_cmp */
   iterate_over_symbols,
   &java_varobj_ops,
+  NULL,
+  NULL,
   LANG_MAGIC
 };
 
diff --git a/gdb/language.c b/gdb/language.c
index 034086d..83dd4d5 100644
--- a/gdb/language.c
+++ b/gdb/language.c
@@ -827,6 +827,8 @@ const struct language_defn unknown_language_defn =
   NULL,				/* la_get_symbol_name_cmp */
   iterate_over_symbols,
   &default_varobj_ops,
+  NULL,
+  NULL,
   LANG_MAGIC
 };
 
@@ -872,6 +874,8 @@ const struct language_defn auto_language_defn =
   NULL,				/* la_get_symbol_name_cmp */
   iterate_over_symbols,
   &default_varobj_ops,
+  NULL,
+  NULL,
   LANG_MAGIC
 };
 
@@ -915,6 +919,8 @@ const struct language_defn local_language_defn =
   NULL,				/* la_get_symbol_name_cmp */
   iterate_over_symbols,
   &default_varobj_ops,
+  NULL,
+  NULL,
   LANG_MAGIC
 };
 \f
diff --git a/gdb/language.h b/gdb/language.h
index 73619ca..9ed7e22 100644
--- a/gdb/language.h
+++ b/gdb/language.h
@@ -35,6 +35,7 @@ struct value_print_options;
 struct type_print_options;
 struct lang_varobj_ops;
 struct parser_state;
+struct compile_instance;
 
 #define MAX_FORTRAN_DIMS  7	/* Maximum number of F77 array dims.  */
 
@@ -355,6 +356,36 @@ struct language_defn
     /* Various operations on varobj.  */
     const struct lang_varobj_ops *la_varobj_ops;
 
+    /* If this language allows compilation from the gdb command line,
+       this method should be non-NULL.  When called it should return
+       an instance of struct gcc_context appropriate to the language.
+       When defined this method must never return NULL; instead it
+       should throw an exception on failure.  The returned compiler
+       instance is owned by its caller and must be deallocated by
+       calling its 'destroy' method.  */
+
+    struct compile_instance *(*la_get_compile_instance) (void);
+
+    /* This method must be defined if 'la_get_gcc_context' is defined.
+       If 'la_get_gcc_context' is not defined, then this method is
+       ignored.
+
+       This takes the user-supplied text and returns a newly malloc'd
+       bit of code to compile.  The caller owns the result.
+
+       INST is the compiler instance being used.
+       INPUT is the user's input text.
+       GDBARCH is the architecture to use.
+       EXPR_BLOCK is the block in which the expression is being
+       parsed.
+       EXPR_PC is the PC at which the expression is being parsed.  */
+
+    char *(*la_compute_program) (struct compile_instance *inst,
+				 const char *input,
+				 struct gdbarch *gdbarch,
+				 const struct block *expr_block,
+				 CORE_ADDR expr_pc);
+
     /* Add fields above this point, so the magic number is always last.  */
     /* Magic number for compat checking.  */
 
diff --git a/gdb/m2-lang.c b/gdb/m2-lang.c
index 940cbcf..6a68520 100644
--- a/gdb/m2-lang.c
+++ b/gdb/m2-lang.c
@@ -394,6 +394,8 @@ const struct language_defn m2_language_defn =
   NULL,				/* la_get_symbol_name_cmp */
   iterate_over_symbols,
   &default_varobj_ops,
+  NULL,
+  NULL,
   LANG_MAGIC
 };
 
diff --git a/gdb/objc-lang.c b/gdb/objc-lang.c
index 8a3d21e..6c4e3ef 100644
--- a/gdb/objc-lang.c
+++ b/gdb/objc-lang.c
@@ -389,6 +389,8 @@ const struct language_defn objc_language_defn = {
   NULL,				/* la_get_symbol_name_cmp */
   iterate_over_symbols,
   &default_varobj_ops,
+  NULL,
+  NULL,
   LANG_MAGIC
 };
 
diff --git a/gdb/opencl-lang.c b/gdb/opencl-lang.c
index 2006cd8..7af3632 100644
--- a/gdb/opencl-lang.c
+++ b/gdb/opencl-lang.c
@@ -1083,6 +1083,8 @@ const struct language_defn opencl_language_defn =
   NULL,				/* la_get_symbol_name_cmp */
   iterate_over_symbols,
   &default_varobj_ops,
+  NULL,
+  NULL,
   LANG_MAGIC
 };
 
diff --git a/gdb/p-lang.c b/gdb/p-lang.c
index 98fcc3b..53669ad 100644
--- a/gdb/p-lang.c
+++ b/gdb/p-lang.c
@@ -451,6 +451,8 @@ const struct language_defn pascal_language_defn =
   NULL,				/* la_get_symbol_name_cmp */
   iterate_over_symbols,
   &default_varobj_ops,
+  NULL,
+  NULL,
   LANG_MAGIC
 };
 
diff --git a/gdb/symtab.h b/gdb/symtab.h
index 73108f0..33ad1d5 100644
--- a/gdb/symtab.h
+++ b/gdb/symtab.h
@@ -641,6 +641,21 @@ struct symbol_computed_ops
 
   void (*tracepoint_var_ref) (struct symbol *symbol, struct gdbarch *gdbarch,
 			      struct agent_expr *ax, struct axs_value *value);
+
+  /* Generate C code to compute the location of SYMBOL.  The C code is
+     emitted to STREAM.  GDBARCH is the current architecture and PC is
+     the PC at which SYMBOL's location should be evaluated.
+     REGISTERS_USED is a vector indexed by register number; the
+     generator function should set an element in this vector if the
+     corresponding register is needed by the location computation.
+     The generated C code must assign the location to a local
+     variable; this variable's name is RESULT_NAME.  */
+
+  void (*generate_c_location) (struct symbol *symbol, struct ui_file *stream,
+			       struct gdbarch *gdbarch,
+			       unsigned char *registers_used,
+			       CORE_ADDR pc, const char *result_name);
+
 };
 
 /* The methods needed to implement LOC_BLOCK for inferior functions.
diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog
index 8fa75d1..0483f46 100644
--- a/gdb/testsuite/ChangeLog
+++ b/gdb/testsuite/ChangeLog
@@ -1,3 +1,22 @@
+2014-10-07  Phil Muldoon  <pmuldoon@redhat.com>
+	    Jan Kratochvil  <jan.kratochvil@redhat.com>
+	    Tom Tromey  <tromey@redhat.com>
+
+	* gdb.dwarf2/compile-ops.exp: New file.
+	* gdb.threads/compile-tls.c: New file.
+	* gdb.threads/compile-tls.exp: New file.
+	* gdb.base/compile-constvar.S: New file.
+	* gdb.base/compile-constvar.c: New file.
+	* gdb.base/compile-mod.c: New file.
+	* gdb.base/compile-nodebug.c: New file.
+	* gdb.base/compile-setjmp-mod.c: New file.
+	* gdb.base/compile-setjmp.c: New file.
+	* gdb.base/compile-setjmp.exp: New file.
+	* gdb.base/compile-shlib.c: New file.
+	* gdb.base/compile.c: New file.
+	* gdb.base/compile.exp: New file.
+	* lib/gdb.exp (skip_compile_feature_tests): New proc.
+
 2014-10-07  Tom Tromey  <tromey@redhat.com>
 
 	* lib/dwarf.exp (_location): Ignore blank lines.  Allow comments.
diff --git a/gdb/testsuite/gdb.base/compile-constvar.S b/gdb/testsuite/gdb.base/compile-constvar.S
new file mode 100644
index 0000000..55d81bc
--- /dev/null
+++ b/gdb/testsuite/gdb.base/compile-constvar.S
@@ -0,0 +1,95 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2014 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+// gcc -o gdb.base/compile-constvar.S -dA -S -g gdb.base/compile-constvar.c
+
+	.file	"compile-constvar.c"
+	.file 1 "gdb.base/compile-constvar.c"
+	.section	.debug_info,"",@progbits
+.Ldebug_info0:
+	.long	.Lend-.Lstart	# Length of Compilation Unit Info
+.Lstart:
+	.value	0x4	# DWARF version number
+	.long	.Ldebug_abbrev0	# Offset Into Abbrev. Section
+	.byte	0x8	# Pointer Size (in bytes)
+	.uleb128 0x1	# (DIE (0xb) DW_TAG_compile_unit)
+	.long	.LASF0	# DW_AT_producer: "GNU C 4.8.2 20131212 (Red Hat 4.8.2-7) -mtune=generic -march=x86-64 -g"
+	.byte	0x1	# DW_AT_language
+	.long	.LASF1	# DW_AT_name: "gdb.base/compile-constvar.c"
+	.long	.LASF2	# DW_AT_comp_dir: "/home/jkratoch/redhat/gdb-gdbjit/gdb/testsuite"
+	.uleb128 0x2	# (DIE (0x1d) DW_TAG_variable)
+	.long	.LASF3	# DW_AT_name: "constvar"
+	.long	.Linttype-.Ldebug_info0	# DW_AT_type
+			# DW_AT_external
+	.byte	0x3	# DW_AT_const_value
+.Linttype:
+	.uleb128 0x3	# (DIE (0x32) DW_TAG_base_type)
+	.byte	0x4	# DW_AT_byte_size
+	.byte	0x5	# DW_AT_encoding
+	.ascii "int\0"	# DW_AT_name
+	.byte	0	# end of children of DIE 0xb
+.Lend:
+	.section	.debug_abbrev,"",@progbits
+.Ldebug_abbrev0:
+	.uleb128 0x1	# (abbrev code)
+	.uleb128 0x11	# (TAG: DW_TAG_compile_unit)
+	.byte	0x1	# DW_children_yes
+	.uleb128 0x25	# (DW_AT_producer)
+	.uleb128 0xe	# (DW_FORM_strp)
+	.uleb128 0x13	# (DW_AT_language)
+	.uleb128 0xb	# (DW_FORM_data1)
+	.uleb128 0x3	# (DW_AT_name)
+	.uleb128 0xe	# (DW_FORM_strp)
+	.uleb128 0x1b	# (DW_AT_comp_dir)
+	.uleb128 0xe	# (DW_FORM_strp)
+	.byte	0
+	.byte	0
+	.uleb128 0x2	# (abbrev code)
+	.uleb128 0x34	# (TAG: DW_TAG_variable)
+	.byte	0	# DW_children_no
+	.uleb128 0x3	# (DW_AT_name)
+	.uleb128 0xe	# (DW_FORM_strp)
+	.uleb128 0x49	# (DW_AT_type)
+	.uleb128 0x13	# (DW_FORM_ref4)
+	.uleb128 0x3f	# (DW_AT_external)
+	.uleb128 0x19	# (DW_FORM_flag_present)
+	.uleb128 0x1c	# (DW_AT_const_value)
+	.uleb128 0xb	# (DW_FORM_data1)
+	.byte	0
+	.byte	0
+	.uleb128 0x3	# (abbrev code)
+	.uleb128 0x24	# (TAG: DW_TAG_base_type)
+	.byte	0	# DW_children_no
+	.uleb128 0xb	# (DW_AT_byte_size)
+	.uleb128 0xb	# (DW_FORM_data1)
+	.uleb128 0x3e	# (DW_AT_encoding)
+	.uleb128 0xb	# (DW_FORM_data1)
+	.uleb128 0x3	# (DW_AT_name)
+	.uleb128 0x8	# (DW_FORM_string)
+	.byte	0
+	.byte	0
+	.byte	0
+	.section	.debug_str,"MS",@progbits,1
+.LASF1:
+	.string	"gdb.base/compile-constvar.c"
+.LASF3:
+	.string	"constvar"
+.LASF2:
+	.string	""
+.LASF0:
+	.string	"GNU C 4.8.2 20131212 (Red Hat 4.8.2-7) -mtune=generic -march=x86-64 -g"
+	.ident	"GCC: (GNU) 4.8.2 20131212 (Red Hat 4.8.2-7)"
diff --git a/gdb/testsuite/gdb.base/compile-constvar.c b/gdb/testsuite/gdb.base/compile-constvar.c
new file mode 100644
index 0000000..3820b43
--- /dev/null
+++ b/gdb/testsuite/gdb.base/compile-constvar.c
@@ -0,0 +1,18 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2014 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+int constvar = 5;
diff --git a/gdb/testsuite/gdb.base/compile-mod.c b/gdb/testsuite/gdb.base/compile-mod.c
new file mode 100644
index 0000000..a6dbc32
--- /dev/null
+++ b/gdb/testsuite/gdb.base/compile-mod.c
@@ -0,0 +1,26 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2014 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+// Make 'globalvar' lookup working.
+#pragma GCC user_expression
+
+void
+_gdb_expr (void)
+{
+  globalvar = 3;
+  globalvar += 4;
+}
diff --git a/gdb/testsuite/gdb.base/compile-nodebug.c b/gdb/testsuite/gdb.base/compile-nodebug.c
new file mode 100644
index 0000000..4cc8bb3
--- /dev/null
+++ b/gdb/testsuite/gdb.base/compile-nodebug.c
@@ -0,0 +1,24 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2014 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+int
+func_nodebug (int neg)
+{
+  return -neg;
+}
+
+int unresolved = 20;
diff --git a/gdb/testsuite/gdb.base/compile-setjmp-mod.c b/gdb/testsuite/gdb.base/compile-setjmp-mod.c
new file mode 100644
index 0000000..8a1c413
--- /dev/null
+++ b/gdb/testsuite/gdb.base/compile-setjmp-mod.c
@@ -0,0 +1,46 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2014 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include <setjmp.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+// Make 'done' lookup working.
+#pragma GCC user_expression
+
+static void
+foo ()
+{
+  jmp_buf env;
+
+  switch (setjmp (env))
+    {
+    case 2:
+      done = 1;
+      return;
+    case 0:
+      longjmp (env, 2);
+      break;
+    }
+  abort ();
+}
+
+void
+_gdb_expr (void)
+{
+  foo ();
+}
diff --git a/gdb/testsuite/gdb.base/compile-setjmp.c b/gdb/testsuite/gdb.base/compile-setjmp.c
new file mode 100644
index 0000000..c462f48
--- /dev/null
+++ b/gdb/testsuite/gdb.base/compile-setjmp.c
@@ -0,0 +1,24 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2014 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+static int done;
+
+int
+main (void)
+{
+  return 0;
+}
diff --git a/gdb/testsuite/gdb.base/compile-setjmp.exp b/gdb/testsuite/gdb.base/compile-setjmp.exp
new file mode 100644
index 0000000..557c1f0
--- /dev/null
+++ b/gdb/testsuite/gdb.base/compile-setjmp.exp
@@ -0,0 +1,34 @@
+# Copyright 2014 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+standard_testfile .c compile-setjmp-mod.c
+
+if { [prepare_for_testing ${testfile}.exp $testfile] } {
+    return -1
+}
+
+if ![runto_main] {
+    return -1
+}
+
+if {[skip_compile_feature_tests]} {
+    untested "could not find libcc1 shared library"
+    return -1
+}
+
+gdb_test_no_output "compile file -r ${srcdir}/${subdir}/$srcfile2" \
+    "compile file -r"
+
+gdb_test "p done" " = 1"
diff --git a/gdb/testsuite/gdb.base/compile-shlib.c b/gdb/testsuite/gdb.base/compile-shlib.c
new file mode 100644
index 0000000..6181af2
--- /dev/null
+++ b/gdb/testsuite/gdb.base/compile-shlib.c
@@ -0,0 +1,26 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2014 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+extern int globalvar;
+
+int shlibvar = 10;
+
+void
+shlib_func (void)
+{
+  globalvar = 1;
+}
diff --git a/gdb/testsuite/gdb.base/compile.c b/gdb/testsuite/gdb.base/compile.c
new file mode 100644
index 0000000..b48acab
--- /dev/null
+++ b/gdb/testsuite/gdb.base/compile.c
@@ -0,0 +1,130 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2014 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#define SOME_MACRO 23
+#define ARG_MACRO(X, Y) ((X) + (Y) - 1)
+
+
+enum enum_type {
+  ONE = 1,
+  TWO = 2
+};
+
+typedef int v4 __attribute__ ((vector_size (16)));
+
+union union_type;
+
+struct struct_type {
+  char charfield;
+  unsigned char ucharfield;
+  short shortfield;
+  unsigned short ushortfield;
+  int intfield;
+  unsigned int uintfield;
+  unsigned int bitfield : 3;
+  long longfield;
+  unsigned long ulongfield;
+  enum enum_type enumfield;
+  float floatfield;
+  double doublefield;
+  const union union_type *ptrfield;
+  struct struct_type *selffield;
+  int arrayfield[5];
+  _Complex double complexfield;
+  _Bool boolfield;
+  v4 vectorfield;
+};
+
+typedef int inttypedef;
+
+union union_type {
+  int intfield;
+  inttypedef typedeffield;
+};
+
+/* volatile provides some coverage of the conversion code.  */
+volatile struct struct_type struct_object;
+
+union union_type union_object;
+
+
+enum ulonger_enum_type {
+  REALLY_MINUS_1 = -1UL,
+};
+
+enum ulonger_enum_type ulonger;
+
+enum longer_enum_type {
+  MINUS_1 = -1,
+  FORCE_TO_LONG = 1L << ((8 * sizeof (long)) - 2)
+};
+
+enum longer_enum_type longer;
+
+int globalvar = 10;
+
+static void
+func_static (int addend)
+{
+  globalvar += addend;
+}
+
+void
+func_global (int subtrahend)
+{
+  globalvar -= subtrahend;
+}
+
+void
+no_args_or_locals (void)
+{
+  /* no_args_or_locals breakpoint */
+}
+
+int *intptr;
+int globalshadow = 10;
+static int staticshadow = 20;
+int externed = 7;
+
+int
+main (void)
+{
+  int localvar = 50;
+  int shadowed = 51;
+  int bound = 3;
+  int unresolved = 10;
+  int globalshadow = 100;
+  int staticshadow = 200;
+  int externed = 9;
+  int f = 0;
+
+  static int static_local = 77000;
+
+  {
+    int another_local = 7;
+    int shadowed = 52;
+    extern int unresolved;
+    extern int externed;
+
+    int vla[bound];
+
+    func_static (0); /* break-here */
+    no_args_or_locals ();
+  }
+
+ return 0;
+}
diff --git a/gdb/testsuite/gdb.base/compile.exp b/gdb/testsuite/gdb.base/compile.exp
new file mode 100644
index 0000000..d0dd791
--- /dev/null
+++ b/gdb/testsuite/gdb.base/compile.exp
@@ -0,0 +1,357 @@
+# Copyright 2014 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+standard_testfile .c compile-shlib.c compile-constvar.S compile-nodebug.c
+
+get_compiler_info
+set options {}
+if [test_compiler_info gcc*] {
+    lappend options additional_flags=-g3
+}
+
+if { ![istarget x86_64-*-* ] || ![is_lp64_target] } {
+    verbose "Skipping x86_64 LOC_CONST test."
+    set srcfile3 ""
+}
+
+set srcfilesoptions [list ${srcfile} ${options}]
+if { $srcfile3 != "" } {
+    lappend srcfilesoptions $srcfile3 ${options}
+}
+lappend srcfilesoptions $srcfile4 "nodebug"
+if { [eval build_executable_from_specs ${testfile}.exp $testfile {$options} ${srcfilesoptions}] } {
+    return -1
+}
+
+clean_restart ${testfile}
+
+#
+# Test command without an running inferior.
+#
+gdb_test "compile code int i=2;" \
+    "The program must be running for the compile command to work.*" \
+    "Test compile code command without running inferior"
+
+gdb_test "compile int i=2;" \
+    "The program must be running for the compile command to work.*" \
+    "Test compile command without running inferior"
+
+gdb_test "compile file -r ${srcdir}/${subdir}/${testfile}-mod.c" \
+    "The program must be running for the compile command to work.*" \
+    "Test compile file command without running inferior"
+
+if ![runto_main] {
+    return -1
+}
+
+if {[skip_compile_feature_tests]} {
+    untested "could not find libcc1 shared library"
+    return -1
+}
+
+#
+# Test delimiter for code, and arguments.
+#
+
+gdb_test_no_output "compile -- f = 10" \
+    "Test abbreviations and code delimiter"
+
+gdb_test "compile f = 10;" ".*= 10;: No such file.*" \
+    "Test abbreviations and code collision"
+
+gdb_test_no_output "compile -r -- _gdb_expr(){int i = 5;}" \
+    "Test delimiter with -r"
+
+gdb_test_no_output "compile -raw -- _gdb_expr(){int i = 5;}" \
+    "Test delimiter with -raw"
+
+gdb_test "compile -- -r  _gdb_expr(){int i = 5;}" \
+    ".* error: 'r' undeclared \\(first use in this function\\).*" \
+    "Test delimiter with -r after it"
+
+gdb_test "p globalvar" " = 10" "expect 10"
+
+gdb_test_no_output "compile code globalvar = 11" \
+    "set variable without trailing semicolon"
+gdb_test "p globalvar" " = 11" "check variable without trailing semicolon"
+
+gdb_test_no_output "compile code globalvar = SOME_MACRO;" \
+    "set variable from macro"
+gdb_test "p globalvar" " = 23" "expect 23"
+
+gdb_test_no_output "compile code globalvar = ARG_MACRO(0, 0);" \
+    "set variable from function-like macro"
+gdb_test "p globalvar" " = -1" "expect -1"
+
+gdb_test_no_output "compile code globalvar = 42;" "set variable"
+gdb_test "p globalvar" " = 42" "expect 42"
+
+gdb_test_no_output "compile code globalvar *= 2;" "modify variable"
+gdb_test "p globalvar" " = 84" "expect 84"
+
+gdb_test_multiple "compile code" "compile code multiline 1" { -re "\r\n>$" {} }
+gdb_test_multiple "globalvar = 10;" "compile code multiline 2" { -re "\r\n>$" {} }
+gdb_test_multiple "globalvar *= 2;" "compile code multiline 3" { -re "\r\n>$" {} }
+gdb_test_no_output "end" "compile code multiline 4"
+gdb_test "p globalvar" " = 20" "expect 20"
+
+gdb_test_no_output "compile file -r ${srcdir}/${subdir}/${testfile}-mod.c" \
+    "use external source file"
+gdb_test "p globalvar" " = 7" "expect 7"
+
+gdb_test_no_output "compile code func_static (2);" "call static function"
+gdb_test "p globalvar" " = 9" "expect 9"
+gdb_test_no_output "compile code func_global (1);" "call global function"
+gdb_test "p globalvar" " = 8" "expect 8"
+
+gdb_test_no_output \
+    "compile code globalvar = (sizeof (ulonger) == sizeof (long))" \
+    "compute size of ulonger"
+gdb_test "p globalvar" " = 1" "check size of ulonger"
+gdb_test_no_output \
+    "compile code globalvar = (sizeof (longer) == sizeof (long))" \
+    "compute size of longer"
+gdb_test "p globalvar" " = 1" "check size of longer"
+gdb_test_no_output "compile code globalvar = MINUS_1"
+gdb_test "p globalvar" " = -1" "check MINUS_1"
+
+gdb_test_no_output "compile code globalvar = static_local"
+gdb_test "p globalvar" " = 77000" "check static_local"
+
+gdb_test_no_output "compile code static int staticvar = 5; intptr = &staticvar" \
+    "keep jit in memory"
+gdb_test "p *intptr" " = 5" "expect 5"
+
+gdb_test "compile code func_doesnotexist ();" "warning: Could not find symbol \"func_doesnotexist\" for .*"
+
+gdb_test "compile code *(volatile int *) 0 = 0;" \
+    "The program being debugged was signaled while in a function called from GDB\\.\r\nGDB remains in the frame where the signal was received\\.\r\n.*" \
+    "compile code segfault first"
+gdb_test "bt" \
+    "\r\n#0  \[^\r\n\]* in _gdb_expr \[^\r\n\]*\r\n#1  <function called from gdb>\r\n.*"
+
+set test "p/x \$pc"
+set infcall_pc 0
+gdb_test_multiple $test $test {
+    -re " = (0x\[0-9a-f\]+)\r\n$gdb_prompt $" {
+	set infcall_pc $expect_out(1,string)
+	pass $test
+    }
+}
+
+gdb_test "info sym $infcall_pc" "\r\n_gdb_expr .*" "info sym found"
+gdb_test "return" "\r\n#0  main .*" "return" \
+	 "Make _gdb_expr return now\\? \\(y or n\\) " "y"
+gdb_test "info sym $infcall_pc" "\r\nNo symbol matches .*" "info sym not found"
+
+gdb_test_no_output "set unwindonsignal on"
+gdb_test "compile code *(volatile int *) 0 = 0;" \
+    "The program being debugged was signaled while in a function called from GDB\\.\r\nGDB has restored the context to what it was before the call\\.\r\n.*" \
+    "compile code segfault second"
+
+gdb_breakpoint [gdb_get_line_number "break-here"]
+gdb_continue_to_breakpoint "break-here" ".* break-here .*"
+
+gdb_test "p localvar" " = 50" "expect localvar 50"
+
+gdb_test_no_output "compile code localvar = 12;" "set localvar"
+gdb_test "p localvar" " = 12" "expect 12"
+
+gdb_test_no_output "compile code localvar *= 2;" "modify localvar"
+gdb_test "p localvar" " = 24" "expect 24"
+
+gdb_test_no_output "compile code localvar = shadowed" \
+    "test shadowing"
+gdb_test "p localvar" " = 52" "expect 52"
+
+gdb_test_no_output "compile code localvar = externed"
+gdb_test "p localvar" " = 7" "test extern in inner scope"
+
+gdb_test_no_output "compile code vla\[2\] = 7"
+gdb_test "p vla\[2\]" " = 7"
+gdb_test_no_output \
+    "compile code localvar = (sizeof (vla) == bound * sizeof (vla\[0\]))"
+gdb_test "p localvar" " = 1"
+
+#
+# Test setting fields and also many different types.
+#
+
+gdb_test_no_output "compile code struct_object.selffield = &struct_object"
+gdb_test "print struct_object.selffield == &struct_object" " = 1"
+
+gdb_test_no_output "compile code struct_object.charfield = 1"
+gdb_test "print struct_object.charfield" " = 1 '\\\\001'"
+gdb_test_no_output "compile code struct_object.ucharfield = 1"
+gdb_test "print struct_object.ucharfield" " = 1 '\\\\001'"
+
+foreach {field value} {
+    shortfield -5
+    ushortfield 5
+    intfield -7
+    uintfield 7
+    bitfield 2
+    longfield -9
+    ulongfield 9
+    enumfield ONE
+    floatfield 1
+    doublefield 2
+} {
+    gdb_test_no_output "compile code struct_object.$field = $value"
+    gdb_test "print struct_object.$field" " = $value"
+}
+
+gdb_test_no_output "compile code struct_object.arrayfield\[2\] = 7"
+gdb_test "print struct_object.arrayfield" \
+    " = \\{0, 0, 7, 0, 0\\}"
+
+gdb_test_no_output "compile code struct_object.complexfield = 7 + 5i"
+gdb_test "print struct_object.complexfield" " = 7 \\+ 5 \\* I"
+
+gdb_test_no_output "compile code struct_object.boolfield = 1"
+gdb_test "print struct_object.boolfield" " = true"
+
+gdb_test_no_output "compile code struct_object.vectorfield\[2\] = 7"
+gdb_test "print struct_object.vectorfield" \
+    " = \\{0, 0, 7, 0\\}"
+
+gdb_test_no_output "compile code union_object.typedeffield = 7"
+gdb_test "print union_object.typedeffield" " = 7"
+gdb_test "print union_object.intfield" " = 7"
+
+
+# LOC_UNRESOLVED tests.
+
+gdb_test "print unresolved" " = 20"
+gdb_test "compile code globalvar = unresolved;"
+gdb_test "print globalvar" " = 20" "print unresolved value"
+
+# Test shadowing with global and static variables.
+
+gdb_test_no_output "compile code globalshadow += 1;"
+gdb_test "print globalshadow" " = 101"
+gdb_test_no_output "compile code extern int globalshadow; globalshadow += 5;"
+gdb_test "print 'compile.c'::globalshadow" " = 15"
+gdb_test "print globalshadow" " = 101" "print globalshadow second time"
+gdb_test_no_output "compile code staticshadow += 2;"
+gdb_test "print staticshadow" " = 202"
+# "extern int staticshadow;" cannot access static variable.
+
+# Raw code cannot refer to locals.
+# As it references global variable we need the #pragma.
+# For #pragma we need multiline input.
+gdb_test_multiple "compile code -r" "compile code -r multiline 1" { -re "\r\n>$" {} }
+gdb_test_multiple "#pragma GCC user_expression" "compile code -r multiline 2" { -re "\r\n>$" {} }
+gdb_test_multiple "void _gdb_expr(void) { globalshadow = 77000; }" "compile code -r multiline 3" { -re "\r\n>$" {} }
+gdb_test_no_output "end" "compile code -r multiline 4"
+gdb_test "print 'compile.c'::globalshadow" " = 77000" \
+    "check globalshadow with -r"
+
+#
+# Test the case where the registers structure would not normally have
+# any fields.
+#
+
+gdb_breakpoint [gdb_get_line_number "no_args_or_locals breakpoint"]
+gdb_continue_to_breakpoint "no_args_or_locals"
+
+gdb_test_no_output "compile code globalvar = 77;" "set variable to 77"
+gdb_test "p globalvar" " = 77" "expect 77"
+
+
+# Test reference to minimal_symbol, not (full) symbol.
+
+gdb_test_no_output "compile code globalvar = func_nodebug (75);" \
+    "call func_nodebug"
+gdb_test "p globalvar" " = -75" "expect -75"
+gdb_test_no_output \
+  "compile code int (*funcp) (int) = func_nodebug; globalvar = funcp (76);" \
+  "call func_nodebug indirectly"
+gdb_test "p globalvar" " = -76" "expect -76"
+
+
+# Test compiled module memory protection.
+
+gdb_test_no_output "set debug compile on"
+gdb_test "compile code static const int readonly = 1; *(int *) &readonly = 2;" \
+    "The program being debugged was signaled while in a function called from GDB\\.\r\nGDB has restored the context to what it was before the call\\.\r\n.*"
+gdb_test_no_output "set debug compile off"
+
+
+#
+# Some simple coverage tests.
+#
+
+gdb_test "show debug compile" "Compile debugging is .*"
+gdb_test "show compile-args" \
+    "Compile command command-line arguments are .*"
+gdb_test "compile code -z" "Unknown argument.*"
+
+gdb_test "set lang java" \
+    "Warning: the current language does not match this frame."
+gdb_test "compile code globalvar" "No compiler support for this language\."
+gdb_test_no_output "set lang auto"
+
+gdb_test_no_output "compile code union union_type newdecl_u"
+gdb_test_no_output "compile code struct struct_type newdecl_s"
+gdb_test_no_output "compile code inttypedef newdecl_i"
+
+gdb_test "compile file" \
+    "You must provide a filename for this command.*" \
+    "Test compile file without a filename"
+gdb_test "compile file -r" \
+    "You must provide a filename with the raw option set.*" \
+    "Test compile file and raw option without a filename"
+gdb_test "compile file -z" \
+    "Unknown argument.*" \
+    "Test compile file with unknown argument"
+
+
+# LOC_CONST tests.
+
+if { $srcfile3 != "" } {
+    gdb_test "p constvar" " = 3"
+    gdb_test "info addr constvar" {Symbol "constvar" is constant\.}
+
+    gdb_test "compile code globalvar = constvar;"
+    gdb_test "print globalvar" " = 3" "print constvar value"
+} else {
+    untested "print constvar value"
+}
+
+# Shared library tests.
+
+if {[skip_shlib_tests]} {
+    untested "skipping shlib tests"
+    return;
+}
+
+set libbin [standard_output_file ${testfile}-shlib.so]
+if { [gdb_compile_shlib ${srcdir}/${subdir}/${srcfile2} $libbin {debug}] != ""
+     || [build_executable $testfile ${testfile}-shlib $srcfile \
+	     [list debug ldflags=$libbin]] == -1 } {
+    return -1
+}
+
+clean_restart ${testfile}-shlib
+if ![runto_main] {
+    return -1
+}
+
+gdb_test_no_output "compile code shlib_func ();" "call shared library function"
+gdb_test "p globalvar" " = 1" "expect 1"
+
+gdb_test_no_output "compile code shlibvar += 5;" "modify shared library variable"
+gdb_test "p shlibvar" " = 15" "expect 15"
diff --git a/gdb/testsuite/gdb.dwarf2/compile-ops.exp b/gdb/testsuite/gdb.dwarf2/compile-ops.exp
new file mode 100644
index 0000000..dd28083
--- /dev/null
+++ b/gdb/testsuite/gdb.dwarf2/compile-ops.exp
@@ -0,0 +1,424 @@
+# Copyright 2014 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# Some coverage testing of DWARF operators for the compiler
+# integration.
+
+load_lib dwarf.exp
+
+# This test can only be run on targets which support DWARF-2 and use gas.
+if {![dwarf2_support]} {
+    return 0
+}
+
+standard_testfile dw2-ifort-parameter.c gdbjit-ops.S
+
+#
+# A port of the pr10770.c test code to the DWARF assembler format.
+#
+
+set assert_tos_non0 {
+    bra 3
+    skip -3
+}
+
+set assert_tos_0 [subst {
+    lit0
+    eq
+    $assert_tos_non0
+}]
+
+set program [subst {
+    lit0
+    nop
+    $assert_tos_0
+    lit1
+    const1u 1
+    eq
+    $assert_tos_non0
+    lit16
+    const2u 16
+    eq
+    $assert_tos_non0
+    lit31
+    const4u 31
+    ne
+    $assert_tos_0
+    lit1
+    neg
+    const1s -1
+    eq
+    $assert_tos_non0
+    lit16
+    neg
+    const2s -16
+    ne
+    $assert_tos_0
+    lit31
+    const4s -31
+    neg
+    ne
+    $assert_tos_0
+    lit7
+    dup
+    plus_uconst 2
+    lit9
+    eq
+    $assert_tos_non0
+    lit7
+    eq
+    $assert_tos_non0
+    lit20
+    lit1
+    drop
+    lit20
+    eq
+    $assert_tos_non0
+    lit17
+    lit19
+    over
+    lit17
+    eq
+    $assert_tos_non0
+    lit19
+    eq
+    $assert_tos_non0
+    lit17
+    eq
+    $assert_tos_non0
+    lit1
+    lit2
+    lit3
+    lit4
+    pick 2
+    lit2
+    eq
+    $assert_tos_non0
+    lit4
+    eq
+    $assert_tos_non0
+    lit3
+    eq
+    $assert_tos_non0
+    pick 0
+    lit2
+    eq
+    $assert_tos_non0
+    lit2
+    eq
+    $assert_tos_non0
+    lit1
+    eq
+    $assert_tos_non0
+    lit6
+    lit12
+    swap
+    lit6
+    eq
+    $assert_tos_non0
+    lit12
+    eq
+    $assert_tos_non0
+    lit7
+    lit8
+    lit9
+    rot
+    lit8
+    eq
+    $assert_tos_non0
+    lit7
+    eq
+    $assert_tos_non0
+    lit9
+    eq
+    $assert_tos_non0
+    lit7
+    abs
+    lit7
+    eq
+    $assert_tos_non0
+    const1s -123
+    abs
+    const1u 123
+    eq
+    $assert_tos_non0
+    lit3
+    lit6
+    and
+    lit2
+    eq
+    $assert_tos_non0
+    lit3
+    lit6
+    or
+    lit7
+    eq
+    $assert_tos_non0
+    lit17
+    lit2
+    minus
+    lit15
+    eq
+    $assert_tos_non0
+    # Divide is signed truncating toward zero.
+    const1s -6
+    const1s -2
+    div
+    lit3
+    eq
+    $assert_tos_non0
+    const1s -7
+    const1s 3
+    div
+    const1s -2
+    eq
+    $assert_tos_non0
+    # Modulo is unsigned.
+    const1s -6
+    const1s -4
+    mod
+    const1s -6
+    eq
+    $assert_tos_non0
+    const1s -6
+    lit4
+    mod
+    lit2
+    eq
+    $assert_tos_non0
+    lit6
+    const1s -4
+    mod
+    lit6
+    eq
+    $assert_tos_non0
+    # Signed modulo can be implemented using 'over over div mul minus'.
+    const1s -6
+    const1s -4
+    over
+    over
+    div
+    mul
+    minus
+    const1s -2
+    eq
+    $assert_tos_non0
+    const1s -7
+    lit3
+    over
+    over
+    div
+    mul
+    minus
+    const1s -1
+    eq
+    $assert_tos_non0
+    lit7
+    const1s -3
+    over
+    over
+    div
+    mul
+    minus
+    lit1
+    eq
+    $assert_tos_non0
+    lit16
+    lit31
+    plus_uconst 1
+    mul
+    const2u 512
+    eq
+    $assert_tos_non0
+    lit5
+    not
+    lit31
+    and
+    lit26
+    eq
+    $assert_tos_non0
+    lit12
+    lit31
+    plus
+    const1u 43
+    eq
+    $assert_tos_non0
+    const1s -6
+    lit2
+    plus
+    const1s -4
+    eq
+    $assert_tos_non0
+    const1s -6
+    plus_uconst 3
+    const1s -3
+    eq
+    $assert_tos_non0
+    lit16
+    lit4
+    shl
+    const2u 256
+    eq
+    $assert_tos_non0
+    lit16
+    lit3
+    shr
+    lit2
+    eq
+    $assert_tos_non0
+    const1s -16
+    lit3
+    shra
+    const1s -2
+    eq
+    $assert_tos_non0
+    lit3
+    lit6
+    xor
+    lit5
+    eq
+    $assert_tos_non0
+    lit3
+    lit6
+    le
+    $assert_tos_non0
+    lit3
+    lit3
+    le
+    $assert_tos_non0
+    lit6
+    lit3
+    le
+    $assert_tos_0
+    lit3
+    lit6
+    lt
+    $assert_tos_non0
+    lit3
+    lit3
+    lt
+    $assert_tos_0
+    lit6
+    lit3
+    lt
+    $assert_tos_0
+    lit3
+    lit6
+    ge
+    $assert_tos_0
+    lit3
+    lit3
+    ge
+    $assert_tos_non0
+    lit6
+    lit3
+    ge
+    $assert_tos_non0
+    lit3
+    lit6
+    gt
+    $assert_tos_0
+    lit3
+    lit3
+    gt
+    $assert_tos_0
+    lit6
+    lit3
+    gt
+    $assert_tos_non0
+    const1s -6
+    lit1
+    shr
+    lit0
+    gt
+    $assert_tos_non0
+    const1s -6
+    lit1
+    shra
+    lit0
+    lt
+    $assert_tos_non0
+    # Finally some result.
+    addr ptr
+}]
+
+# Make some DWARF for the test.
+set asm_file [standard_output_file $srcfile2]
+Dwarf::assemble $asm_file {
+    # Creating a CU with 4-byte addresses lets this test link on both
+    # 32- and 64-bit machines.
+    cu { addr_size 4 } {
+
+	declare_labels int_label
+	extern func_start func_end ptr
+
+	compile_unit {
+	    {name file1.txt}
+	    {language @DW_LANG_C}
+	    {low_pc func_start addr}
+	    {high_pc func_end addr}
+	} {
+	    global program
+
+	    int_label: base_type {
+		{name int}
+		{byte_size 4 sdata}
+		{encoding @DW_ATE_signed}
+	    }
+
+	    subprogram {
+		{external 1 flag}
+		{name func}
+		{low_pc func_start addr}
+		{high_pc func_end addr}
+	    } {
+		formal_parameter {
+		    {name param}
+		    {variable_parameter 1 flag}
+		    {type :$int_label}
+		    {location $program SPECIAL_expr}
+		}
+
+		formal_parameter {
+		    {name optimized_out}
+		    {variable_parameter 1 flag}
+		    {type :$int_label}
+		}
+	    }
+	}
+    }
+}
+
+if { [prepare_for_testing ${testfile}.exp ${testfile} \
+	  [list $srcfile $asm_file] {nodebug}] } {
+    return -1
+}
+
+if ![runto func] {
+    return -1
+}
+
+if {[skip_compile_feature_tests]} {
+    untested "could not find libcc1 shared library"
+    return -1
+}
+
+# If we have a bug, this will hang.
+gdb_test_no_output "compile code param"
+
+# We can't access optimized-out variables, but their presence should
+# not affect compilations that don't refer to them.
+gdb_test "compile code optimized_out" \
+    ".*optimized out.*Compilation failed."
diff --git a/gdb/testsuite/gdb.threads/compile-tls.c b/gdb/testsuite/gdb.threads/compile-tls.c
new file mode 100644
index 0000000..6e08a30
--- /dev/null
+++ b/gdb/testsuite/gdb.threads/compile-tls.c
@@ -0,0 +1,40 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2014 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include <pthread.h>
+
+__thread int global_scope;
+
+static __thread int static_scope;
+
+int foo ()
+{
+  /* Ensure we link against pthreads even with --as-needed.  */
+  pthread_testcancel();
+  return 27;
+}
+
+int
+main (void)
+{
+  static __thread int local_scope;
+
+  /* break-here */
+  foo ();
+
+  return 0;
+}
diff --git a/gdb/testsuite/gdb.threads/compile-tls.exp b/gdb/testsuite/gdb.threads/compile-tls.exp
new file mode 100644
index 0000000..e9613f5
--- /dev/null
+++ b/gdb/testsuite/gdb.threads/compile-tls.exp
@@ -0,0 +1,42 @@
+# Copyright 2014 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+standard_testfile .c
+
+if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" \
+	 executable {debug}] != "" } {
+    return -1
+}
+
+clean_restart ${binfile}
+if ![runto_main] then {
+   fail "Can't run to main"
+   return 0
+}
+
+if {[skip_compile_feature_tests]} {
+    untested "could not find libcc1 shared library"
+    return -1
+}
+
+gdb_test "compile code local_scope = 1" \
+    "warning: .* compiled code."
+gdb_test "print local_scope" " = 1"
+gdb_test "compile code static_scope = 2" \
+    "warning: .* compiled code."
+gdb_test "print static_scope" " = 2"
+gdb_test "compile code global_scope = 3" \
+    "warning: .* compiled code."
+gdb_test "print global_scope" " = 3"
diff --git a/gdb/testsuite/lib/gdb.exp b/gdb/testsuite/lib/gdb.exp
index 2c79bc1..560573d 100644
--- a/gdb/testsuite/lib/gdb.exp
+++ b/gdb/testsuite/lib/gdb.exp
@@ -2518,6 +2518,23 @@ proc skip_libstdcxx_probe_tests {} {
     return $ok
 }
 
+# Return 1 if we should skip tests of the "compile" feature.
+# This must be invoked after the inferior has been started.
+
+proc skip_compile_feature_tests {} {
+    global gdb_prompt
+
+    set result 0
+    gdb_test_multiple "compile code -- ;" "check for working compile command" {
+	"Could not load libcc1.*\r\n$gdb_prompt $" {
+	    set result 1
+	}
+	-re "\r\n$gdb_prompt $" {
+	}
+    }
+    return $result
+}
+
 # Check whether we're testing with the remote or extended-remote
 # targets.
 

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v3 00/14] let gdb reuse gcc's C compiler
  2014-11-01 21:45 [PATCH v3 00/14] let gdb reuse gcc's C compiler Jan Kratochvil
                   ` (13 preceding siblings ...)
  2014-11-01 21:48 ` [PATCH v3 14/14] the "compile" command Jan Kratochvil
@ 2014-11-01 21:52 ` Jan Kratochvil
  2014-11-01 21:56 ` Jan Kratochvil
  2014-11-03 12:47 ` Yao Qi
  16 siblings, 0 replies; 35+ messages in thread
From: Jan Kratochvil @ 2014-11-01 21:52 UTC (permalink / raw)
  To: gdb-patches

Hi,

I have forgotten the most important:

Current GCC trunk (future 5.0) has now the libcc1.so support checked in by
Phil, therefore this gdb/NEWS addition is now really true:

* GDB now supports the compilation and injection of source code into
  the inferior.  GDB will use GCC 5.0 or higher built with libcc1.so
  to compile the source code to object code, and if successful, inject
  and execute that code within the current context of the inferior.

This makes it relatively easy to test this patchset.


Jan

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v3 00/14] let gdb reuse gcc's C compiler
  2014-11-01 21:45 [PATCH v3 00/14] let gdb reuse gcc's C compiler Jan Kratochvil
                   ` (14 preceding siblings ...)
  2014-11-01 21:52 ` [PATCH v3 00/14] let gdb reuse gcc's C compiler Jan Kratochvil
@ 2014-11-01 21:56 ` Jan Kratochvil
  2014-11-03 12:47 ` Yao Qi
  16 siblings, 0 replies; 35+ messages in thread
From: Jan Kratochvil @ 2014-11-01 21:56 UTC (permalink / raw)
  To: gdb-patches

Hi,

FYI there is now also a description of this project at:
	https://sourceware.org/gdb/wiki/GCCCompileAndExecute


Jan

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v3 14/14] the "compile" command
  2014-11-01 21:48 ` [PATCH v3 14/14] the "compile" command Jan Kratochvil
@ 2014-11-02 16:03   ` Eli Zaretskii
  2014-11-20 21:24     ` Jan Kratochvil
  2014-11-03 13:08   ` Yao Qi
  2014-11-11 18:53   ` Pedro Alves
  2 siblings, 1 reply; 35+ messages in thread
From: Eli Zaretskii @ 2014-11-02 16:03 UTC (permalink / raw)
  To: Jan Kratochvil; +Cc: gdb-patches

> From: Jan Kratochvil <jan.kratochvil@redhat.com>
> Date: Sat, 01 Nov 2014 22:47:33 +0100
> 
> diff --git a/gdb/NEWS b/gdb/NEWS
> index 649c29e..c4a6c6a 100644
> --- a/gdb/NEWS
> +++ b/gdb/NEWS

This part is OK.

> +@value{GDBN} supports on-demand compilation and code injection into
> +programs running under @value{GDBN}.  GCC 5.0 or higher built with
> +@code{libcc1.so} must be installed for this functionality to be enabled.

libcc1.so should be in @file, not @code.

> +Compile @var{source} with the compiler language set as the current
> +language in @value{GDBN} (@pxref{Languages}).

Does the user need to set the language, or will the language from the
debug info be used, if available?  If the latter, the wording here
needs to be modified, as it currently implies one needs to set the
language "in GDB".

> If compilation and
> +injection is not supported with the current language specified in
> +@value{GDBN}, or the compiler does not support this feature,

Which "this feature" needs to be supported by the compiler?

> +message will be printed.  If @var{source} compiles and links
> +successfully, @value{GDBN} will load the object-code emitted,

Judging by the example, I would suggest to use @var{source-code}
instead of @var{code}.

> +The command allows you to specify source code in two ways.  The simplest

@var{code}, so that the reader realizes you are talking about the
argument mentioned above.

> +Specifying @samp{-raw}, prohibits @value{GDBN} from wrapping the
> +provided @var{source} in a callable scope.  In this case, you must
> +specify the entry point of the code by defining a function named
> +@code{_gdb_expr_}.  The @samp{-raw} code does not automatically
> +access variables of the inferior, for their import you must use the
> +@samp{#pragma} line first:
> +
> +@smallexample
> +#pragma GCC user_expression
> +@end smallexample

I think an example of using 'raw' is in order; the description above
is not very clear.

> +@samp{#include} lines are better to be placed before the @samp{#pragma}
> +line.

Why? what happens if one doesn't?

>        The use of @samp{-raw} is considered to be expert usage, and care
> +should be taken when using it.

Like what?

This kind of remarks in documentation are "considered harmful": either
explain in detail what you mean, or don't say that at all.  Leaving
mysterious comments about "here be dragons" doesn't help the reader
understand how to use the feature correctly.

> +If the filename/path contains whitespace it must be enclosed in
> +quotes.

Which quotes? single? double?

> +There are a few caveats to keep in mind when using the @code{compile}
> +command.  As the caveats are different per language, the table below
> +highlights specific issues on a per language basis.

This and the following stuff is better put in its own subsection.

> +@code{C}.  Be aware that changes to inferior variables in the
> +@code{compile} command are persistent.  In the following example:
> +
> +@smallexample
> +compile code k = 3;
> +@end smallexample
> +
> +The variable @code{k} is now 3.

You want this line be continuation of the one before the example.  So:

 @noindent
 the variable @code{k} is now 3.

>                                   It will remain that value until
                                            ^^^^^^
"retain", not "remain".

> +something else in the example program changes it, or another
> +@code{compile} command changes it.

So why did you say the changes are persistent?  Persistent might mean
the value will be the same if the program is re-run.

> +Normal scope and access rules apply to source code compiled and injected
> +by the @code{compile} command.  In the example, the variables @code{j}
> +and @code{k} are not accessible; subsequent execution will bring these
> +variables into scope, and later, code written and compiled with the
> +@code{compile} command will be able to access them.  At this point the
> +program is stopped in the @code{main} function so the following example:
> +
> +@smallexample
> +compile code j = 3;
> +@end smallexample
> +
> +would result in a compilation error, and @value{GDBN} would print that
> +error to the console.

I needed to read this twice to understand what you mean.  Suggest to
reword as follows:

  Normal scope and access rules apply to source code compiled and
  injected by the @code{compile} command.  In the example, the variables
  @code{j} and @code{k} are not accessible yet, because the program is
  currently stopped in the @code{main} function, where these variables
  are not in scope.  Therefore, the following command

  @smallexample
  compile code j = 3;
  @end smallexample

  @noindent
  will result in a compilation error message.

  Once the program is continued, execution will bring these variables in
  scope, and they will become accessible; then the code you specify via
  the @code{compile} command will be able to access them.

> +However, if you were to type the following into @value{GDBN} after that
> +command has completed:
> +
> +@smallexample
> +compile code printf ("ff is %d\n'', ff);
> +@end smallexample
> +
> +A compiler error would be raised as the variable @code{ff} no longer

Again, @noindend after the @example, and start "a compiler error" with
a lower-case letter, as this is a continuation of the sentence before
the example.

> +exists.  Object code generated and injected by the @code{compile}
> +command is removed on completion of the command.

"... is removed when its execution ends" is better.  ("Completion of
the command" is not well-defined.)

>                                                        Caution is advised
> +when assigning variables belonging to the program with variables created
> +in the @code{compile} command.

"assigning to program variables values of variables created by the
code submitted to the @code{compile} command."

> +The @code{k} variable is assigned to the value of @code{ff}.  The variable
   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
"The value of the variable @code{ff} is assigned to @code{k}."

> +@code{k} does not require the existence of @code{ff} to maintain the value
> +it has been assigned.  Pointers and other types of references require

"However, pointers ..."

What "other types"?  How about one or two examples?

> +location when the command exists.  A general rule should be followed
> +in that you should either assign @code{NULL} to any assigned pointers,
> +or restore a valid location to the pointer before the command exits.

Why NULL?  Any constant address will do, right?

> +Similar caution must be exercised with any types defined in
> +@code{compile} command.

I would explain what you mean by "defined types" here.  I'm guessing
you mean structs, unions, and typedefs, but let's say this explicitly.

>                             Types defined in the @code{compile} are also
> +deleted when the command exits.

I think types are not deleted, they simply don't exist in compiled
code.  Right?

>                                    Therefore, if you cast a variable to a
> +type defined in the @code{compile} command, care must be taken to ensure
> +that any future need to resolve the type can be achieved.

I don't understand the meaning of this.  Defining a type and casting
to a type are two different things, so what does this text warn about?

> +Variables that have been optimized away by the compiler are not
> +accessible to the @code{compile} command.

"... not accessible to the code submitted to the @code{compile} command"

Thanks.

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v3 00/14] let gdb reuse gcc's C compiler
  2014-11-01 21:45 [PATCH v3 00/14] let gdb reuse gcc's C compiler Jan Kratochvil
                   ` (15 preceding siblings ...)
  2014-11-01 21:56 ` Jan Kratochvil
@ 2014-11-03 12:47 ` Yao Qi
  2014-11-03 12:49   ` Jan Kratochvil
  16 siblings, 1 reply; 35+ messages in thread
From: Yao Qi @ 2014-11-03 12:47 UTC (permalink / raw)
  To: Jan Kratochvil; +Cc: gdb-patches

Jan Kratochvil <jan.kratochvil@redhat.com> writes:

> This is version 3 of the patch to let gdb reuse gcc's C compiler.

IIRC, this patch series requires related changes in gcc.  Is that part
in gcc now?

-- 
Yao (齐尧)

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v3 00/14] let gdb reuse gcc's C compiler
  2014-11-03 12:47 ` Yao Qi
@ 2014-11-03 12:49   ` Jan Kratochvil
  0 siblings, 0 replies; 35+ messages in thread
From: Jan Kratochvil @ 2014-11-03 12:49 UTC (permalink / raw)
  To: Yao Qi; +Cc: gdb-patches

On Mon, 03 Nov 2014 13:42:55 +0100, Yao Qi wrote:
> IIRC, this patch series requires related changes in gcc.  Is that part
> in gcc now?

Yes, I forgot to update the cover mail but it was immediately replied to it:
	https://sourceware.org/ml/gdb-patches/2014-11/msg00020.html


Jan

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v3 02/14] add gcc/gdb interface files
  2014-11-01 21:46 ` [PATCH v3 02/14] add gcc/gdb interface files Jan Kratochvil
@ 2014-11-03 12:51   ` Yao Qi
  2014-11-03 12:56     ` Jan Kratochvil
  0 siblings, 1 reply; 35+ messages in thread
From: Yao Qi @ 2014-11-03 12:51 UTC (permalink / raw)
  To: Jan Kratochvil; +Cc: gdb-patches

Jan Kratochvil <jan.kratochvil@redhat.com> writes:

> diff --git a/include/gcc-c-fe.def b/include/gcc-c-fe.def
> new file mode 100644
> index 0000000..19cb867
> --- /dev/null
> +++ b/include/gcc-c-fe.def
> @@ -0,0 +1,197 @@
> +/* Interface between GCC C FE and GDB  -*- c -*-
> +
> +   Copyright (C) 2014 Free Software Foundation, Inc.
> +
> +   This file is part of GCC.

This file is not part of GCC.

-- 
Yao (齐尧)

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v3 02/14] add gcc/gdb interface files
  2014-11-03 12:51   ` Yao Qi
@ 2014-11-03 12:56     ` Jan Kratochvil
  2014-11-03 13:24       ` Yao Qi
  0 siblings, 1 reply; 35+ messages in thread
From: Jan Kratochvil @ 2014-11-03 12:56 UTC (permalink / raw)
  To: Yao Qi; +Cc: gdb-patches

On Mon, 03 Nov 2014 13:46:29 +0100, Yao Qi wrote:
> Jan Kratochvil <jan.kratochvil@redhat.com> writes:
> 
> > diff --git a/include/gcc-c-fe.def b/include/gcc-c-fe.def
> > new file mode 100644
> > index 0000000..19cb867
> > --- /dev/null
> > +++ b/include/gcc-c-fe.def
> > @@ -0,0 +1,197 @@
> > +/* Interface between GCC C FE and GDB  -*- c -*-
> > +
> > +   Copyright (C) 2014 Free Software Foundation, Inc.
> > +
> > +   This file is part of GCC.
> 
> This file is not part of GCC.

It is, it is already checked in the GCC repository and GDB repo shares some
files in include/ with GCC repo.  In binutils-gdb.git include/ there are
occasional commits
	Sync libiberty with upstream GCC.

I do not know if there is a better way how to import a file from GCC repo than
this way.


Jan

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v3 14/14] the "compile" command
  2014-11-01 21:48 ` [PATCH v3 14/14] the "compile" command Jan Kratochvil
  2014-11-02 16:03   ` Eli Zaretskii
@ 2014-11-03 13:08   ` Yao Qi
  2014-11-14 18:43     ` Jan Kratochvil
  2014-11-11 18:53   ` Pedro Alves
  2 siblings, 1 reply; 35+ messages in thread
From: Yao Qi @ 2014-11-03 13:08 UTC (permalink / raw)
  To: Jan Kratochvil; +Cc: gdb-patches

Jan Kratochvil <jan.kratochvil@redhat.com> writes:

> 2014-10-07  Phil Muldoon  <pmuldoon@redhat.com>
> 	    Jan Kratochvil  <jan.kratochvil@redhat.com>
> 	    Tom Tromey  <tromey@redhat.com>
>
> 	* gdb.dwarf2/compile-ops.exp: New file.
> 	* gdb.threads/compile-tls.c: New file.
> 	* gdb.threads/compile-tls.exp: New file.
> 	* gdb.base/compile-constvar.S: New file.
> 	* gdb.base/compile-constvar.c: New file.
> 	* gdb.base/compile-mod.c: New file.
> 	* gdb.base/compile-nodebug.c: New file.
> 	* gdb.base/compile-setjmp-mod.c: New file.
> 	* gdb.base/compile-setjmp.c: New file.
> 	* gdb.base/compile-setjmp.exp: New file.
> 	* gdb.base/compile-shlib.c: New file.
> 	* gdb.base/compile.c: New file.
> 	* gdb.base/compile.exp: New file.

Probably we can put these tests into gdb.compile.

-- 
Yao (齐尧)

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v3 02/14] add gcc/gdb interface files
  2014-11-03 12:56     ` Jan Kratochvil
@ 2014-11-03 13:24       ` Yao Qi
  0 siblings, 0 replies; 35+ messages in thread
From: Yao Qi @ 2014-11-03 13:24 UTC (permalink / raw)
  To: Jan Kratochvil; +Cc: gdb-patches

Jan Kratochvil <jan.kratochvil@redhat.com> writes:

> It is, it is already checked in the GCC repository and GDB repo shares some
> files in include/ with GCC repo.  In binutils-gdb.git include/ there are
> occasional commits
> 	Sync libiberty with upstream GCC.

Ur, you are right.  I forget about it.  Sorry about the noise.

-- 
Yao (齐尧)

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v3 12/14] add linux_infcall_mmap
  2014-11-01 21:47 ` [PATCH v3 12/14] add linux_infcall_mmap Jan Kratochvil
@ 2014-11-11 16:43   ` Pedro Alves
  2014-11-23 19:11     ` Jan Kratochvil
  0 siblings, 1 reply; 35+ messages in thread
From: Pedro Alves @ 2014-11-11 16:43 UTC (permalink / raw)
  To: Jan Kratochvil, gdb-patches

On 11/01/2014 09:47 PM, Jan Kratochvil wrote:
> +/* See gdbarch.sh 'infcall_mmap'.  */
> +
> +static CORE_ADDR
> +linux_infcall_mmap (CORE_ADDR size, unsigned prot)
> +{
> +  struct objfile *objf;
> +  /* Do there still exist any Linux systems without "mmap64"?
> +     "mmap" uses 64-bit off_t on x86_64 and 32-bit off_t on i386 and x32.  */
> +  struct value *mmap_val = find_function_in_inferior ("mmap64", &objf);
> +  struct value *addr_val;
> +  struct gdbarch *gdbarch = get_objfile_arch (objf);
> +  CORE_ADDR retval;
> +  enum
> +    {
> +      ARG_ADDR, ARG_LENGTH, ARG_PROT, ARG_FLAGS, ARG_FD, ARG_OFFSET, ARG_MAX
> +    };
> +  struct value *arg[ARG_MAX];
> +
> +  arg[ARG_ADDR] = value_from_pointer (builtin_type (gdbarch)->builtin_data_ptr,
> +				      0);
> +  /* Assuming sizeof (unsigned long) == sizeof (size_t).  */
> +  arg[ARG_LENGTH] = value_from_ulongest
> +		    (builtin_type (gdbarch)->builtin_unsigned_long, size);
> +  gdb_assert ((prot & ~7) == 0);
> +  arg[ARG_PROT] = value_from_longest (builtin_type (gdbarch)->builtin_int,
> +				      0
> +				      | ((prot & 4) != 0 ? PROT_READ : 0)
> +				      | ((prot & 2) != 0 ? PROT_WRITE : 0)
> +				      | ((prot & 1) != 0 ? PROT_EXEC : 0));
> +  arg[ARG_FLAGS] = value_from_longest (builtin_type (gdbarch)->builtin_int,
> +				       MAP_PRIVATE | MAP_ANONYMOUS);

PROT_READ, PROT_WRITE, PROT_EXEC, MAP_PRIVATE, MAP_ANONYMOUS
are host values/macros.

This needs to be made host independent, otherwise cross debugging to
Linux passes the wrong values to mmap.

It also likely breaks --enable-targets=all builds on hosts
that don't have mmap at all.  E.g., this comes out empty on F20:

 $ grep -rn PROT_READ /usr/i686-w64-mingw32/sys-root/mingw/include/

OTOH, the 4, 2 and 1 bits checked against the 'prot' argument
are magical constants; they'd best be put behind macros, like e.g.,
GDB_MMAP_PROT_READ, etc., here and at the callers
of gdbarch_infcall_mmap.

Thanks,
Pedro Alves

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v3 14/14] the "compile" command
  2014-11-01 21:48 ` [PATCH v3 14/14] the "compile" command Jan Kratochvil
  2014-11-02 16:03   ` Eli Zaretskii
  2014-11-03 13:08   ` Yao Qi
@ 2014-11-11 18:53   ` Pedro Alves
  2014-11-23 18:36     ` Jan Kratochvil
  2 siblings, 1 reply; 35+ messages in thread
From: Pedro Alves @ 2014-11-11 18:53 UTC (permalink / raw)
  To: Jan Kratochvil, gdb-patches

Hi Jan,

FWIW, this looks good to me, and I'm looking forward to
have this pushed in.  Just a few comments below.

> +/* See compile-internal.h.  */
> +
> +void
> +gcc_convert_symbol (void *datum,
> +		    struct gcc_c_context *gcc_context,
> +		    enum gcc_c_oracle_request request,
> +		    const char *identifier)

...

> +
> +  /* We can't allow exceptions to escape out of this callback.  Safest
> +     is to simply emit a gcc error.  */
> +  TRY_CATCH (e, RETURN_MASK_ERROR)
> +    {
> +      struct symbol *sym;

Shouldn't this catch ctrl-c too then?  Likewise the other hooks.





> +      for (i = 0; i < 4; ++i)

It'd be good to give this magic constant a name, or at
a least a comment.

> +	{
> +	  const char *mode = c_get_mode_for_size (1 << i);
> +
> +	  gdb_assert (mode != NULL);
> +	  fprintf_unfiltered (buf,
> +			      "typedef int"
> +			      " __attribute__ ((__mode__(__%s__)))"
> +			      " __gdb_int_%s;\n",
> +			      mode, mode);
> +	}





> +
> +/* A cleanup function to remove a directory and all its contents.  */
> +
> +static void
> +do_rmdir (void *arg)
> +{
> +  char *zap = concat ("rm -rf ", arg, (char *) NULL);
> +
> +  system (zap);
> +}

This is quite scary...  Could we please add an assert here
that tempdir_name starts with "/tmp/gdbobj-", just in case something
goes really wrong here?

> +
> +/* Return the name of the temporary directory to use for .o files, and
> +   arrange for the directory to be removed at shutdown.  */
> +
> +static const char *
> +get_compile_file_tempdir (void)
> +{
> +  static char *tempdir_name;
> +
> +#define TEMPLATE "/tmp/gdbobj-XXXXXX"
> +  char tname[sizeof (TEMPLATE)];
> +
> +  if (tempdir_name != NULL)
> +    return tempdir_name;
> +
> +  strcpy (tname, TEMPLATE);
> +#undef TEMPLATE
> +  tempdir_name = mkdtemp (tname);
> +  if (tempdir_name == NULL)
> +    perror_with_name (_("Could not make temporary directory"));
> +
> +  tempdir_name = xstrdup (tempdir_name);
> +  make_final_cleanup (do_rmdir, tempdir_name);
> +  return tempdir_name;
> +}




> +  /* Override flags possibly coming from DW_AT_producer.  */
> +  compile_args = xstrdup ("-O0 -gdwarf-4"
> +  /* We use -fPIC to ensure that we can reference properly.  Otherwise
> +     on x86-64 a string constant's address might be truncated when gdb
> +     loads the object; another approach would be -mcmodel=large, but
> +     -fPIC seems more portable across back ends.  */

This comment ends up being a bit unexpected/odd, given that ...

> +			 " -fPIC"
> +  /* We don't want warnings.  */
> +			 " -w"

... patch #6 has:

> +char *
> +default_gcc_target_options (struct gdbarch *gdbarch)
> +{
> +  return xstrprintf ("-m%d%s", gdbarch_ptr_bit (gdbarch),
> +		     gdbarch_ptr_bit (gdbarch) == 64 ? " -mcmodel=large" : "");
> +}
> +

IOW, is the comment stale?

Thanks,
Pedro Alves

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v3 14/14] the "compile" command
  2014-11-03 13:08   ` Yao Qi
@ 2014-11-14 18:43     ` Jan Kratochvil
  0 siblings, 0 replies; 35+ messages in thread
From: Jan Kratochvil @ 2014-11-14 18:43 UTC (permalink / raw)
  To: Yao Qi; +Cc: gdb-patches

On Mon, 03 Nov 2014 14:03:37 +0100, Yao Qi wrote:
> Jan Kratochvil <jan.kratochvil@redhat.com> writes:
> 
> > 2014-10-07  Phil Muldoon  <pmuldoon@redhat.com>
> > 	    Jan Kratochvil  <jan.kratochvil@redhat.com>
> > 	    Tom Tromey  <tromey@redhat.com>
> >
> > 	* gdb.dwarf2/compile-ops.exp: New file.
> > 	* gdb.threads/compile-tls.c: New file.
> > 	* gdb.threads/compile-tls.exp: New file.
> > 	* gdb.base/compile-constvar.S: New file.
> > 	* gdb.base/compile-constvar.c: New file.
> > 	* gdb.base/compile-mod.c: New file.
> > 	* gdb.base/compile-nodebug.c: New file.
> > 	* gdb.base/compile-setjmp-mod.c: New file.
> > 	* gdb.base/compile-setjmp.c: New file.
> > 	* gdb.base/compile-setjmp.exp: New file.
> > 	* gdb.base/compile-shlib.c: New file.
> > 	* gdb.base/compile.c: New file.
> > 	* gdb.base/compile.exp: New file.
> 
> Probably we can put these tests into gdb.compile.

Done, to be in the next patch series.


Thanks,
Jan

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v3 14/14] the "compile" command
  2014-11-02 16:03   ` Eli Zaretskii
@ 2014-11-20 21:24     ` Jan Kratochvil
  2014-11-21  7:58       ` Eli Zaretskii
  0 siblings, 1 reply; 35+ messages in thread
From: Jan Kratochvil @ 2014-11-20 21:24 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: gdb-patches

[-- Attachment #1: Type: text/plain, Size: 10015 bytes --]

On Sun, 02 Nov 2014 17:02:48 +0100, Eli Zaretskii wrote:
> > +Compile @var{source} with the compiler language set as the current
> > +language in @value{GDBN} (@pxref{Languages}).
> 
> Does the user need to set the language, or will the language from the
> debug info be used, if available?

The latter.

> If the latter, the wording here needs to be modified, as it currently
> implies one needs to set the language "in GDB".

Used s/set/found/.
	Compile @var{source} with the compiler language found as the current
	language in @value{GDBN} (@pxref{Languages}).


> > If compilation and
> > +injection is not supported with the current language specified in
> > +@value{GDBN}, or the compiler does not support this feature,
> 
> Which "this feature" needs to be supported by the compiler?

The "compilation and injection" - the libcc1.so module and its
gcc_c_fe_context() interface for GDB.


> > +message will be printed.  If @var{source} compiles and links
> > +successfully, @value{GDBN} will load the object-code emitted,
> 
> Judging by the example, I would suggest to use @var{source-code}
> instead of @var{code}.

You wanted to say "instead of @var{source}.".

Changed it in these lines:
	@item compile code @var{source-code}
	@itemx compile code -raw @var{--} @var{source-code}
	Compile @var{source-code} with the compiler language found as the current
	[...]
	message will be printed.  If @var{source-code} compiles and links
	[...]
	provided @var{source-code} in a callable scope.  In this case, you must


> > +The command allows you to specify source code in two ways.  The simplest
> 
> @var{code}, so that the reader realizes you are talking about the
> argument mentioned above.

I think it should be @var{source-code} now, therefore used:
	The command allows you to specify @var{source-code} in two ways.  


> > +Specifying @samp{-raw}, prohibits @value{GDBN} from wrapping the
> > +provided @var{source} in a callable scope.  In this case, you must
> > +specify the entry point of the code by defining a function named
> > +@code{_gdb_expr_}.  The @samp{-raw} code does not automatically
> > +access variables of the inferior, for their import you must use the
> > +@samp{#pragma} line first:
> > +
> > +@smallexample
> > +#pragma GCC user_expression
> > +@end smallexample
> 
> I think an example of using 'raw' is in order; the description above
> is not very clear.
> 
> > +@samp{#include} lines are better to be placed before the @samp{#pragma}
> > +line.
> 
> Why? what happens if one doesn't?

While debugging it I have found that for -raw file #pragma GCC user_expression
does not make sense as it does not work - it is explicitly disabled by:
gdb/compile/compile-c-symbols.c
       /* Don't emit local variable decls for a raw expression.  */
       if (context->base.scope != COMPILE_I_RAW_SCOPE
          || symbol_name == NULL)

Therefore I have reworked the paragraph to:
	Specifying @samp{-raw}, prohibits @value{GDBN} from wrapping the
	provided @var{source-code} in a callable scope.  In this case, you must
	specify the entry point of the code by defining a function named
	@code{_gdb_expr_}.  The @samp{-raw} code cannot access variables of the
	inferior.  Using @samp{-raw} option may be needed for example when
	@var{source-code} requires @samp{#include} lines which may conflict with
	inferior symbols otherwise.

In the future we may extend -raw to make the inferior variables accessible
somehow but as that requires GDB-generated code it mostly conflicts with the
goal of -raw.


> >        The use of @samp{-raw} is considered to be expert usage, and care
> > +should be taken when using it.
> 
> Like what?
> 
> This kind of remarks in documentation are "considered harmful": either
> explain in detail what you mean, or don't say that at all.  Leaving
> mysterious comments about "here be dragons" doesn't help the reader
> understand how to use the feature correctly.

Removed it.


> > +If the filename/path contains whitespace it must be enclosed in
> > +quotes.
> 
> Which quotes? single? double?

I have found if the filename/path contains whitespace it must be left as is,
without any quotes of any kind.  I have removed the sentence; or do you
suggested something else?


> > +There are a few caveats to keep in mind when using the @code{compile}
> > +command.  As the caveats are different per language, the table below
> > +highlights specific issues on a per language basis.
> 
> This and the following stuff is better put in its own subsection.

Done:
	@subsection Caveats when using the @code{compile} command


> > +something else in the example program changes it, or another
> > +@code{compile} command changes it.
> 
> So why did you say the changes are persistent?  Persistent might mean
> the value will be the same if the program is re-run.

The command "compile code k = 3;" makes the change persistent.  Contrary to it
command "compile code int k = 3;" would not be persistent which is described
about 3 paragraphs beneath:
	Variables and types that are created as part
	of the @code{compile} command are not persistent, and only exist as
	long as the injected object code exists.  This example is valid:

	@smallexample
	compile code int ff = 5; printf ("ff is %d\n", ff);
	@end smallexample


> > +Normal scope and access rules apply to source code compiled and injected
> > +by the @code{compile} command.  In the example, the variables @code{j}
> > +and @code{k} are not accessible; subsequent execution will bring these
> > +variables into scope, and later, code written and compiled with the
> > +@code{compile} command will be able to access them.  At this point the
> > +program is stopped in the @code{main} function so the following example:
> > +
> > +@smallexample
> > +compile code j = 3;
> > +@end smallexample
> > +
> > +would result in a compilation error, and @value{GDBN} would print that
> > +error to the console.
> 
> I needed to read this twice to understand what you mean.  Suggest to
> reword as follows:
> 
>   Normal scope and access rules apply to source code compiled and
>   injected by the @code{compile} command.  In the example, the variables
>   @code{j} and @code{k} are not accessible yet, because the program is
>   currently stopped in the @code{main} function, where these variables
>   are not in scope.  Therefore, the following command
> 
>   @smallexample
>   compile code j = 3;
>   @end smallexample
> 
>   @noindent
>   will result in a compilation error message.
> 
>   Once the program is continued, execution will bring these variables in
>   scope, and they will become accessible; then the code you specify via
>   the @code{compile} command will be able to access them.

I do not see there a difference but I used your text.


> > +However, if you were to type the following into @value{GDBN} after that
> > +command has completed:
> > +
> > +@smallexample
> > +compile code printf ("ff is %d\n'', ff);
> > +@end smallexample
> > +
> > +A compiler error would be raised as the variable @code{ff} no longer
> 
> Again, @noindend after the @example, and start "a compiler error" with
> a lower-case letter, as this is a continuation of the sentence before
> the example.

TBH I do not agree with this formatting as the previous sentence has been
terminated by its colon (':').  I see the difference as:
	See the expression: 1+2=3  Now we have to start a new sentence.
	See the expression 1+2=3 while here we continue the sentence.
But sure following your directions.


> > +@code{k} does not require the existence of @code{ff} to maintain the value
> > +it has been assigned.  Pointers and other types of references require
> 
> "However, pointers ..."
> 
> What "other types"?  How about one or two examples?

I would say just "pointers" myself.  "references" would be possible in C++ but
currently this patchset supports only plain C.  Currently one can pass also
a function name but that gets converted to a function pointer.  Reference to
a nest function via trampoline would be in fact also a function pointer.

I have removed "and other types of references" for now.


> > +location when the command exists.  A general rule should be followed
> > +in that you should either assign @code{NULL} to any assigned pointers,
> > +or restore a valid location to the pointer before the command exits.
> 
> Why NULL?  Any constant address will do, right?

NULL is the only valid constant address.  Otherwise a valid address points to
some object.

I find that sentence correct.


> > +Similar caution must be exercised with any types defined in
> > +@code{compile} command.
> 
> I would explain what you mean by "defined types" here.  I'm guessing
> you mean structs, unions, and typedefs, but let's say this explicitly.

Therefore used:
	Similar caution must be exercised with any structs, unions, and typedefs 
	defined in @code{compile} command.


> >                             Types defined in the @code{compile} are also
> > +deleted when the command exits.
> 
> I think types are not deleted, they simply don't exist in compiled
> code.  Right?

The meaning is that for example in a second "compile" command the types
defined in previous "compile" command are no longer available.


> >                                    Therefore, if you cast a variable to a
> > +type defined in the @code{compile} command, care must be taken to ensure
> > +that any future need to resolve the type can be achieved.
> 
> I don't understand the meaning of this.  Defining a type and casting
> to a type are two different things, so what does this text warn about?

I have put there:
	@smallexample
	(gdb) compile code static struct a { int a; } v = { 42 }; argv = &v;
	(gdb) compile code printf ("%d\n", ((struct a *) argv)->a);
	gdb command line:1:36: error: dereferencing pointer to incomplete type ‘struct a’
	Compilation failed.
	(gdb) compile code struct a { int a; }; printf ("%d\n", ((struct a *) argv)->a);
	42
	@end smallexample


Attaching only the new gdb.texinfo diff.


Thanks,
Jan

[-- Attachment #2: 1 --]
[-- Type: text/plain, Size: 10579 bytes --]

    gdb/doc/ChangeLog
    2014-10-07  Phil Muldoon  <pmuldoon@redhat.com>
    	    Jan Kratochvil  <jan.kratochvil@redhat.com>
    
    	* gdb.texinfo (Altering): Update.
    	(Compiling and Injecting Code): New node.
    
diff --git a/gdb/doc/ChangeLog b/gdb/doc/ChangeLog
index 2e619da..f314c89 100644
--- a/gdb/doc/ChangeLog
+++ b/gdb/doc/ChangeLog
@@ -1,3 +1,9 @@
+2014-10-07  Phil Muldoon  <pmuldoon@redhat.com>
+	    Jan Kratochvil  <jan.kratochvil@redhat.com>
+
+	* gdb.texinfo (Altering): Update.
+	(Compiling and Injecting Code): New node.
+
 2014-10-30  Doug Evans  <dje@google.com>
 
 	* python.texi (Progspaces In Python): Document ability to add
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 15c2908..a0b6ea7 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -16493,6 +16493,7 @@ address, or even return prematurely from a function.
 * Returning::                   Returning from a function
 * Calling::                     Calling your program's functions
 * Patching::                    Patching your program
+* Compiling and Injecting Code:: Compiling and injecting code in @value{GDBN}
 @end menu
 
 @node Assignment
@@ -16915,6 +16916,237 @@ Display whether executable files and core files are opened for writing
 as well as reading.
 @end table
 
+@node Compiling and Injecting Code
+@section Compiling and injecting code in @value{GDBN}
+@cindex injecting code
+@cindex writing into executables
+@cindex compiling code
+
+@value{GDBN} supports on-demand compilation and code injection into
+programs running under @value{GDBN}.  GCC 5.0 or higher built with
+@file{libcc1.so} must be installed for this functionality to be enabled.
+This functionality is implemented with the following commands.
+
+@table @code
+@kindex compile code
+@item compile code @var{source-code}
+@itemx compile code -raw @var{--} @var{source-code}
+Compile @var{source-code} with the compiler language found as the current
+language in @value{GDBN} (@pxref{Languages}).  If compilation and
+injection is not supported with the current language specified in
+@value{GDBN}, or the compiler does not support this feature, an error
+message will be printed.  If @var{source-code} compiles and links
+successfully, @value{GDBN} will load the object-code emitted,
+and execute it within the context of the currently selected inferior.
+It is important to note that the compiled code is executed immediately.
+After execution, the compiled code is removed from @value{GDBN} and any
+new types or variables you have defined will be deleted.
+
+The command allows you to specify @var{source-code} in two ways.
+The simplest method is to provide a single line of code to the command.
+E.g.:
+
+@smallexample
+compile code printf ("hello world\n");
+@end smallexample
+
+If you specify options on the command line as well as source code, they
+may conflict.  The @samp{@var{--}} delimiter can be used to separate options
+from actual source code.  E.g.:
+
+@smallexample
+compile code -r -- printf ("hello world\n");
+@end smallexample
+
+Alternatively you can enter source code as multiple lines of text.  To
+enter this mode, invoke the @samp{compile code} command without any text
+following the command.  This will start the multiple-line editor and
+allow you to type as many lines of source code as required.  When you
+have completed typing, enter @samp{end} on its own line to exit the
+editor.
+
+@smallexample
+compile code
+>printf ("hello\n");
+>printf ("world\n");
+>end
+@end smallexample
+
+Specifying @samp{-raw}, prohibits @value{GDBN} from wrapping the
+provided @var{source-code} in a callable scope.  In this case, you must
+specify the entry point of the code by defining a function named
+@code{_gdb_expr_}.  The @samp{-raw} code cannot access variables of the
+inferior.  Using @samp{-raw} option may be needed for example when
+@var{source-code} requires @samp{#include} lines which may conflict with
+inferior symbols otherwise.
+
+@kindex compile file
+@item compile file @var{filename}
+@itemx compile file -raw @var{filename}
+Like @code{compile code}, but take the source code from @var{filename}.
+
+@smallexample
+compile file /home/user/example.c
+@end smallexample
+@end table
+
+@subsection Caveats when using the @code{compile} command
+
+There are a few caveats to keep in mind when using the @code{compile}
+command.  As the caveats are different per language, the table below
+highlights specific issues on a per language basis.
+
+@table @asis
+@item C code examples and caveats
+When the language in @value{GDBN} is set to @samp{C}, the compiler will
+attempt to compile the source code with a @samp{C} compiler.  The source
+code provided to the @code{compile} command will have much the same
+access to variables and types as it normally would if it were part of
+the program currently being debugged in @value{GDBN}.
+
+Below is a sample program that forms the basis of the examples that
+follow.  This program has been compiled and loaded into @value{GDBN},
+much like any other normal debugging session.
+
+@smallexample
+void function1 (void)
+@{
+   int i = 42;
+   printf ("function 1\n");
+@}
+
+void function2 (void)
+@{
+   int j = 12;
+   function1 ();
+@}
+
+int main(void)
+@{
+   int k = 6;
+   int *p;
+   function2 ();
+   return 0;
+@}
+@end smallexample
+
+For the purposes of the examples in this section, the program above has
+been compiled, loaded into @value{GDBN}, stopped at the function
+@code{main}, and @value{GDBN} is awaiting input from the user.
+
+To access variables and types for any program in @value{GDBN}, the
+program must be compiled and packaged with debug information.  The
+@code{compile} command is not an exception to this rule.  Without debug
+information, you can still use the @code{compile} command, but you will
+be very limited in what variables and types you can access.
+
+So with that in mind, the example above has been compiled with debug
+information enabled.  The @code{compile} command will have access to
+all variables and types (except those that may have been optimized
+out).  Currently, as @value{GDBN} has stopped the program in the
+@code{main} function, the @code{compile} command would have access to
+the variable @code{k}.  You could invoke the @code{compile} command
+and type some source code to set the value of @code{k}.  You can also
+read it, or do anything with that variable you would normally do in
+@code{C}.  Be aware that changes to inferior variables in the
+@code{compile} command are persistent.  In the following example:
+
+@smallexample
+compile code k = 3;
+@end smallexample
+
+@noindent
+the variable @code{k} is now 3.  It will retain that value until
+something else in the example program changes it, or another
+@code{compile} command changes it.
+
+Normal scope and access rules apply to source code compiled and
+injected by the @code{compile} command.  In the example, the variables
+@code{j} and @code{k} are not accessible yet, because the program is
+currently stopped in the @code{main} function, where these variables
+are not in scope.  Therefore, the following command
+
+@smallexample
+compile code j = 3;
+@end smallexample
+
+@noindent
+will result in a compilation error message.
+
+Once the program is continued, execution will bring these variables in
+scope, and they will become accessible; then the code you specify via
+the @code{compile} command will be able to access them.
+
+You can create variables and types with the @code{compile} command as
+part of your source code.  Variables and types that are created as part
+of the @code{compile} command are not persistent, and only exist as
+long as the injected object code exists.  This example is valid:
+
+@smallexample
+compile code int ff = 5; printf ("ff is %d\n", ff);
+@end smallexample
+
+However, if you were to type the following into @value{GDBN} after that
+command has completed:
+
+@smallexample
+compile code printf ("ff is %d\n'', ff);
+@end smallexample
+
+@noindent
+a compiler error would be raised as the variable @code{ff} no longer
+exists.  Object code generated and injected by the @code{compile}
+command is removed when its execution ends.  Caution is advised
+when assigning to program variables values of variables created by the
+code submitted to the @code{compile} command.  This example is valid:
+
+@smallexample
+compile code int ff = 5; k = ff;
+@end smallexample
+
+The value of the variable @code{ff} is assigned to @code{k}.  The variable
+@code{k} does not require the existence of @code{ff} to maintain the value
+it has been assigned.  However, pointers require particular care in
+assignment.  If the source code compiled with the @code{compile} command
+changed the address of a pointer in the example program, perhaps to a
+variable created in the @code{compile} command, that pointer would point
+to an invalid location when the command exits.  The following example
+would likely cause issues with your debugged program:
+
+@smallexample
+compile code int ff = 5; p = &ff;
+@end smallexample
+
+In this example, @code{p} would point to @code{ff} when the
+@code{compile} command is executing the source code provided to it.
+However, as variables in the (example) program persist with their
+assigned values, the variable @code{p} would point to an invalid
+location when the command exists.  A general rule should be followed
+in that you should either assign @code{NULL} to any assigned pointers,
+or restore a valid location to the pointer before the command exits.
+
+Similar caution must be exercised with any structs, unions, and typedefs
+defined in @code{compile} command.  Types defined in the @code{compile}
+are also deleted when the command exits.  Therefore, if you cast
+a variable to a type defined in the @code{compile} command, care must be
+taken to ensure that any future need to resolve the type can be
+achieved.
+
+@smallexample
+(gdb) compile code static struct a @{ int a; @} v = @{ 42 @}; argv = &v;
+(gdb) compile code printf ("%d\n", ((struct a *) argv)->a);
+gdb command line:1:36: error: dereferencing pointer to incomplete type ‘struct a’
+Compilation failed.
+(gdb) compile code struct a @{ int a; @}; printf ("%d\n", ((struct a *) argv)->a);
+42
+@end smallexample
+
+Variables that have been optimized away by the compiler are not
+accessible to the code submitted to the @code{compile} command.
+Access to those variables will generate a compiler error which @value{GDBN}
+will print to the console.
+@end table
+
 @node GDB Files
 @chapter @value{GDBN} Files
 

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v3 14/14] the "compile" command
  2014-11-20 21:24     ` Jan Kratochvil
@ 2014-11-21  7:58       ` Eli Zaretskii
  2014-11-21 18:41         ` Jan Kratochvil
  0 siblings, 1 reply; 35+ messages in thread
From: Eli Zaretskii @ 2014-11-21  7:58 UTC (permalink / raw)
  To: Jan Kratochvil; +Cc: gdb-patches

> Date: Thu, 20 Nov 2014 22:24:06 +0100
> From: Jan Kratochvil <jan.kratochvil@redhat.com>
> Cc: gdb-patches@sourceware.org
> 
> > > +something else in the example program changes it, or another
> > > +@code{compile} command changes it.
> > 
> > So why did you say the changes are persistent?  Persistent might mean
> > the value will be the same if the program is re-run.
> 
> The command "compile code k = 3;" makes the change persistent.  Contrary to it
> command "compile code int k = 3;" would not be persistent which is described
> about 3 paragraphs beneath:
> 	Variables and types that are created as part
> 	of the @code{compile} command are not persistent, and only exist as
> 	long as the injected object code exists.  This example is valid:
> 
> 	@smallexample
> 	compile code int ff = 5; printf ("ff is %d\n", ff);
> 	@end smallexample

Then maybe we need to tell what "persistent" means in this context.
Something like "visible to the rest of the program for the duration of
this run".

> > > +However, if you were to type the following into @value{GDBN} after that
> > > +command has completed:
> > > +
> > > +@smallexample
> > > +compile code printf ("ff is %d\n'', ff);
> > > +@end smallexample
> > > +
> > > +A compiler error would be raised as the variable @code{ff} no longer
> > 
> > Again, @noindend after the @example, and start "a compiler error" with
> > a lower-case letter, as this is a continuation of the sentence before
> > the example.
> 
> TBH I do not agree with this formatting as the previous sentence has been
> terminated by its colon (':').

But then that previous sentence is incomplete, because it in effect
goes like "If you were to type the following: SOMETHING."  A sentence
with an "if" but no "then" part is incomplete.  I thought what follows
the @example is the "then" part, which is why I suggested those
changes.

> I see the difference as:
> 	See the expression: 1+2=3  Now we have to start a new sentence.
> 	See the expression 1+2=3 while here we continue the sentence.

These 2 examples are both fine, but they lack the "if" part, which is
what triggered my comment.

> > >                             Types defined in the @code{compile} are also
> > > +deleted when the command exits.
> > 
> > I think types are not deleted, they simply don't exist in compiled
> > code.  Right?
> 
> The meaning is that for example in a second "compile" command the types
> defined in previous "compile" command are no longer available.

OK, then let's say so explicitly.  Something like

  Types defined in the @code{compile} will no longer be available in
  the next @code{compile} command.

> +If you specify options on the command line as well as source code, they
> +may conflict.  The @samp{@var{--}} delimiter can be used to separate options

No need for @var here, as "--" is a literal string.

Otherwise, your new text is OK.

Thanks.

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v3 14/14] the "compile" command
  2014-11-21  7:58       ` Eli Zaretskii
@ 2014-11-21 18:41         ` Jan Kratochvil
  2014-11-21 19:47           ` Eli Zaretskii
  0 siblings, 1 reply; 35+ messages in thread
From: Jan Kratochvil @ 2014-11-21 18:41 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: gdb-patches

[-- Attachment #1: Type: text/plain, Size: 2542 bytes --]

On Fri, 21 Nov 2014 08:58:30 +0100, Eli Zaretskii wrote:
> > The command "compile code k = 3;" makes the change persistent.  Contrary to it
> > command "compile code int k = 3;" would not be persistent which is described
> > about 3 paragraphs beneath:
> > 	Variables and types that are created as part
> > 	of the @code{compile} command are not persistent, and only exist as
> > 	long as the injected object code exists.  This example is valid:
> > 
> > 	@smallexample
> > 	compile code int ff = 5; printf ("ff is %d\n", ff);
> > 	@end smallexample
> 
> Then maybe we need to tell what "persistent" means in this context.
> Something like "visible to the rest of the program for the duration of
> this run".

I find this was said by "and only exist as long as the injected object code
exists.".  But I have therefore changed the sentence to:
	Variables and types that are created as part of the @code{compile}
	command are not visible to the rest of the program for the duration of
	this run.

BTW I would say s/this run/its run/.

There is also later this sentence with "persistence" but I have left it as is:
	However, as variables in the (example) program persist with their
	assigned values, the variable @code{p} would point to an invalid
	location when the command exists.


> > > > +However, if you were to type the following into @value{GDBN} after that
> > > > +command has completed:
> > > > +
> > > > +@smallexample
> > > > +compile code printf ("ff is %d\n'', ff);
> > > > +@end smallexample
> > > > +
> > > > +A compiler error would be raised as the variable @code{ff} no longer
> > > 
> > > Again, @noindend after the @example, and start "a compiler error" with
> > > a lower-case letter, as this is a continuation of the sentence before
> > > the example.
> > 
> > TBH I do not agree with this formatting as the previous sentence has been
> > terminated by its colon (':').
> 
> But then that previous sentence is incomplete, because it in effect
> goes like "If you were to type the following: SOMETHING."  A sentence
> with an "if" but no "then" part is incomplete.  I thought what follows
> the @example is the "then" part, which is why I suggested those
> changes.

OK, I agree now.


> OK, then let's say so explicitly.  Something like
> 
>   Types defined in the @code{compile} will no longer be available in
>   the next @code{compile} command.

I used it as you wrote.  Although I would write:
	Types defined in the @code{compile} command will no longer be
	available in the next @code{compile} command.


Thanks,
Jan

[-- Attachment #2: 1 --]
[-- Type: text/plain, Size: 9930 bytes --]

diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 15c2908..7bd38da 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -16493,6 +16493,7 @@ address, or even return prematurely from a function.
 * Returning::                   Returning from a function
 * Calling::                     Calling your program's functions
 * Patching::                    Patching your program
+* Compiling and Injecting Code:: Compiling and injecting code in @value{GDBN}
 @end menu
 
 @node Assignment
@@ -16915,6 +16916,237 @@ Display whether executable files and core files are opened for writing
 as well as reading.
 @end table
 
+@node Compiling and Injecting Code
+@section Compiling and injecting code in @value{GDBN}
+@cindex injecting code
+@cindex writing into executables
+@cindex compiling code
+
+@value{GDBN} supports on-demand compilation and code injection into
+programs running under @value{GDBN}.  GCC 5.0 or higher built with
+@file{libcc1.so} must be installed for this functionality to be enabled.
+This functionality is implemented with the following commands.
+
+@table @code
+@kindex compile code
+@item compile code @var{source-code}
+@itemx compile code -raw @var{--} @var{source-code}
+Compile @var{source-code} with the compiler language found as the current
+language in @value{GDBN} (@pxref{Languages}).  If compilation and
+injection is not supported with the current language specified in
+@value{GDBN}, or the compiler does not support this feature, an error
+message will be printed.  If @var{source-code} compiles and links
+successfully, @value{GDBN} will load the object-code emitted,
+and execute it within the context of the currently selected inferior.
+It is important to note that the compiled code is executed immediately.
+After execution, the compiled code is removed from @value{GDBN} and any
+new types or variables you have defined will be deleted.
+
+The command allows you to specify @var{source-code} in two ways.
+The simplest method is to provide a single line of code to the command.
+E.g.:
+
+@smallexample
+compile code printf ("hello world\n");
+@end smallexample
+
+If you specify options on the command line as well as source code, they
+may conflict.  The @samp{--} delimiter can be used to separate options
+from actual source code.  E.g.:
+
+@smallexample
+compile code -r -- printf ("hello world\n");
+@end smallexample
+
+Alternatively you can enter source code as multiple lines of text.  To
+enter this mode, invoke the @samp{compile code} command without any text
+following the command.  This will start the multiple-line editor and
+allow you to type as many lines of source code as required.  When you
+have completed typing, enter @samp{end} on its own line to exit the
+editor.
+
+@smallexample
+compile code
+>printf ("hello\n");
+>printf ("world\n");
+>end
+@end smallexample
+
+Specifying @samp{-raw}, prohibits @value{GDBN} from wrapping the
+provided @var{source-code} in a callable scope.  In this case, you must
+specify the entry point of the code by defining a function named
+@code{_gdb_expr_}.  The @samp{-raw} code cannot access variables of the
+inferior.  Using @samp{-raw} option may be needed for example when
+@var{source-code} requires @samp{#include} lines which may conflict with
+inferior symbols otherwise.
+
+@kindex compile file
+@item compile file @var{filename}
+@itemx compile file -raw @var{filename}
+Like @code{compile code}, but take the source code from @var{filename}.
+
+@smallexample
+compile file /home/user/example.c
+@end smallexample
+@end table
+
+@subsection Caveats when using the @code{compile} command
+
+There are a few caveats to keep in mind when using the @code{compile}
+command.  As the caveats are different per language, the table below
+highlights specific issues on a per language basis.
+
+@table @asis
+@item C code examples and caveats
+When the language in @value{GDBN} is set to @samp{C}, the compiler will
+attempt to compile the source code with a @samp{C} compiler.  The source
+code provided to the @code{compile} command will have much the same
+access to variables and types as it normally would if it were part of
+the program currently being debugged in @value{GDBN}.
+
+Below is a sample program that forms the basis of the examples that
+follow.  This program has been compiled and loaded into @value{GDBN},
+much like any other normal debugging session.
+
+@smallexample
+void function1 (void)
+@{
+   int i = 42;
+   printf ("function 1\n");
+@}
+
+void function2 (void)
+@{
+   int j = 12;
+   function1 ();
+@}
+
+int main(void)
+@{
+   int k = 6;
+   int *p;
+   function2 ();
+   return 0;
+@}
+@end smallexample
+
+For the purposes of the examples in this section, the program above has
+been compiled, loaded into @value{GDBN}, stopped at the function
+@code{main}, and @value{GDBN} is awaiting input from the user.
+
+To access variables and types for any program in @value{GDBN}, the
+program must be compiled and packaged with debug information.  The
+@code{compile} command is not an exception to this rule.  Without debug
+information, you can still use the @code{compile} command, but you will
+be very limited in what variables and types you can access.
+
+So with that in mind, the example above has been compiled with debug
+information enabled.  The @code{compile} command will have access to
+all variables and types (except those that may have been optimized
+out).  Currently, as @value{GDBN} has stopped the program in the
+@code{main} function, the @code{compile} command would have access to
+the variable @code{k}.  You could invoke the @code{compile} command
+and type some source code to set the value of @code{k}.  You can also
+read it, or do anything with that variable you would normally do in
+@code{C}.  Be aware that changes to inferior variables in the
+@code{compile} command are persistent.  In the following example:
+
+@smallexample
+compile code k = 3;
+@end smallexample
+
+@noindent
+the variable @code{k} is now 3.  It will retain that value until
+something else in the example program changes it, or another
+@code{compile} command changes it.
+
+Normal scope and access rules apply to source code compiled and
+injected by the @code{compile} command.  In the example, the variables
+@code{j} and @code{k} are not accessible yet, because the program is
+currently stopped in the @code{main} function, where these variables
+are not in scope.  Therefore, the following command
+
+@smallexample
+compile code j = 3;
+@end smallexample
+
+@noindent
+will result in a compilation error message.
+
+Once the program is continued, execution will bring these variables in
+scope, and they will become accessible; then the code you specify via
+the @code{compile} command will be able to access them.
+
+You can create variables and types with the @code{compile} command as
+part of your source code.  Variables and types that are created as part
+of the @code{compile} command are not visible to the rest of the program for
+the duration of this run.  This example is valid:
+
+@smallexample
+compile code int ff = 5; printf ("ff is %d\n", ff);
+@end smallexample
+
+However, if you were to type the following into @value{GDBN} after that
+command has completed:
+
+@smallexample
+compile code printf ("ff is %d\n'', ff);
+@end smallexample
+
+@noindent
+a compiler error would be raised as the variable @code{ff} no longer
+exists.  Object code generated and injected by the @code{compile}
+command is removed when its execution ends.  Caution is advised
+when assigning to program variables values of variables created by the
+code submitted to the @code{compile} command.  This example is valid:
+
+@smallexample
+compile code int ff = 5; k = ff;
+@end smallexample
+
+The value of the variable @code{ff} is assigned to @code{k}.  The variable
+@code{k} does not require the existence of @code{ff} to maintain the value
+it has been assigned.  However, pointers require particular care in
+assignment.  If the source code compiled with the @code{compile} command
+changed the address of a pointer in the example program, perhaps to a
+variable created in the @code{compile} command, that pointer would point
+to an invalid location when the command exits.  The following example
+would likely cause issues with your debugged program:
+
+@smallexample
+compile code int ff = 5; p = &ff;
+@end smallexample
+
+In this example, @code{p} would point to @code{ff} when the
+@code{compile} command is executing the source code provided to it.
+However, as variables in the (example) program persist with their
+assigned values, the variable @code{p} would point to an invalid
+location when the command exists.  A general rule should be followed
+in that you should either assign @code{NULL} to any assigned pointers,
+or restore a valid location to the pointer before the command exits.
+
+Similar caution must be exercised with any structs, unions, and typedefs
+defined in @code{compile} command.  Types defined in the @code{compile}
+will no longer be available in the next @code{compile} command.
+Therefore, if you cast a variable to a type defined in the
+@code{compile} command, care must be taken to ensure that any future
+need to resolve the type can be achieved.
+
+@smallexample
+(gdb) compile code static struct a @{ int a; @} v = @{ 42 @}; argv = &v;
+(gdb) compile code printf ("%d\n", ((struct a *) argv)->a);
+gdb command line:1:36: error: dereferencing pointer to incomplete type ‘struct a’
+Compilation failed.
+(gdb) compile code struct a @{ int a; @}; printf ("%d\n", ((struct a *) argv)->a);
+42
+@end smallexample
+
+Variables that have been optimized away by the compiler are not
+accessible to the code submitted to the @code{compile} command.
+Access to those variables will generate a compiler error which @value{GDBN}
+will print to the console.
+@end table
+
 @node GDB Files
 @chapter @value{GDBN} Files
 

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v3 14/14] the "compile" command
  2014-11-21 18:41         ` Jan Kratochvil
@ 2014-11-21 19:47           ` Eli Zaretskii
  0 siblings, 0 replies; 35+ messages in thread
From: Eli Zaretskii @ 2014-11-21 19:47 UTC (permalink / raw)
  To: Jan Kratochvil; +Cc: gdb-patches

> Date: Fri, 21 Nov 2014 19:41:32 +0100
> From: Jan Kratochvil <jan.kratochvil@redhat.com>
> Cc: gdb-patches@sourceware.org
> 
> > Then maybe we need to tell what "persistent" means in this context.
> > Something like "visible to the rest of the program for the duration of
> > this run".
> 
> I find this was said by "and only exist as long as the injected object code
> exists.".  But I have therefore changed the sentence to:
> 	Variables and types that are created as part of the @code{compile}
> 	command are not visible to the rest of the program for the duration of
> 	this run.
> 
> BTW I would say s/this run/its run/.

That's also fine.

> > OK, then let's say so explicitly.  Something like
> > 
> >   Types defined in the @code{compile} will no longer be available in
> >   the next @code{compile} command.
> 
> I used it as you wrote.  Although I would write:
> 	Types defined in the @code{compile} command will no longer be
> 	available in the next @code{compile} command.

That'd be fine as well.

I think the patch can go in now.  Thanks.

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v3 14/14] the "compile" command
  2014-11-11 18:53   ` Pedro Alves
@ 2014-11-23 18:36     ` Jan Kratochvil
  2014-12-12 14:38       ` Pedro Alves
  0 siblings, 1 reply; 35+ messages in thread
From: Jan Kratochvil @ 2014-11-23 18:36 UTC (permalink / raw)
  To: Pedro Alves; +Cc: gdb-patches

On Tue, 11 Nov 2014 19:53:26 +0100, Pedro Alves wrote:
> > +/* See compile-internal.h.  */
> > +
> > +void
> > +gcc_convert_symbol (void *datum,
> > +		    struct gcc_c_context *gcc_context,
> > +		    enum gcc_c_oracle_request request,
> > +		    const char *identifier)
> 
> ...
> 
> > +
> > +  /* We can't allow exceptions to escape out of this callback.  Safest
> > +     is to simply emit a gcc error.  */
> > +  TRY_CATCH (e, RETURN_MASK_ERROR)
> > +    {
> > +      struct symbol *sym;
> 
> Shouldn't this catch ctrl-c too then?  Likewise the other hooks.

Yes, changed.


> > +      for (i = 0; i < 4; ++i)
> 
> It'd be good to give this magic constant a name, or at
> a least a comment.

Added there:
	// Iterate all log2 sizes in bytes supported by c_get_mode_for_size.
Added to c_get_mode_for_size:
	default:
	  internal_error (__FILE__, __LINE__, _("Invalid GCC mode size %d."), size);


> > +	{
> > +	  const char *mode = c_get_mode_for_size (1 << i);
> > +
> > +	  gdb_assert (mode != NULL);
> > +	  fprintf_unfiltered (buf,
> > +			      "typedef int"
> > +			      " __attribute__ ((__mode__(__%s__)))"
> > +			      " __gdb_int_%s;\n",
> > +			      mode, mode);
> > +	}
> 
> 
> 
> > +
> > +/* A cleanup function to remove a directory and all its contents.  */
> > +
> > +static void
> > +do_rmdir (void *arg)
> > +{
> > +  char *zap = concat ("rm -rf ", arg, (char *) NULL);
> > +
> > +  system (zap);
> > +}
> 
> This is quite scary...  Could we please add an assert here
> that tempdir_name starts with "/tmp/gdbobj-", just in case something
> goes really wrong here?

Added:
	/* Initial filename for temporary files.  */

	#define TMP_PREFIX "/tmp/gdbobj-"
+
	#define TEMPLATE TMP_PREFIX "XXXXXX"
+
	gdb_assert (strncmp (dir, TMP_PREFIX, strlen (TMP_PREFIX)) == 0);


> > +  /* Override flags possibly coming from DW_AT_producer.  */
> > +  compile_args = xstrdup ("-O0 -gdwarf-4"
> > +  /* We use -fPIC to ensure that we can reference properly.  Otherwise
> > +     on x86-64 a string constant's address might be truncated when gdb
> > +     loads the object; another approach would be -mcmodel=large, but
> > +     -fPIC seems more portable across back ends.  */
> 
> This comment ends up being a bit unexpected/odd, given that ...
> 
> > +			 " -fPIC"
> > +  /* We don't want warnings.  */
> > +			 " -w"
> 
> ... patch #6 has:
> 
> > +char *
> > +default_gcc_target_options (struct gdbarch *gdbarch)
> > +{
> > +  return xstrprintf ("-m%d%s", gdbarch_ptr_bit (gdbarch),
> > +		     gdbarch_ptr_bit (gdbarch) == 64 ? " -mcmodel=large" : "");
> > +}
> > +
> 
> IOW, is the comment stale?

You are right the comment is stale although the code is not stale.  Up to date
reason for these options I have written in:
	https://sourceware.org/gdb/wiki/GCCCompileAndExecute#Relocating_the_object_file
Therefore put there:
	  /* We use -fPIC Otherwise GDB would need to reserve space large enough for
	     any object file in the inferior in advance to get the final address when
	     to link the object file to and additionally the default system linker
	     script would need to be modified so that one can specify there the
	     absolute target address.  */
				" -fPIC"

And also:
	/* -mcmodel=large is used so that no GOT (Global Offset Table) is needed to be
	   created in inferior memory by GDB (normally it is set by ld.so).  */

	char *
	default_gcc_target_options (struct gdbarch *gdbarch)
	{
	  return xstrprintf ("-m%d%s", gdbarch_ptr_bit (gdbarch),
			     gdbarch_ptr_bit (gdbarch) == 64 ? " -mcmodel=large" : "");
	}


Thanks,
Jan

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v3 12/14] add linux_infcall_mmap
  2014-11-11 16:43   ` Pedro Alves
@ 2014-11-23 19:11     ` Jan Kratochvil
  2014-12-12 14:38       ` Pedro Alves
  0 siblings, 1 reply; 35+ messages in thread
From: Jan Kratochvil @ 2014-11-23 19:11 UTC (permalink / raw)
  To: Pedro Alves; +Cc: gdb-patches

On Tue, 11 Nov 2014 17:43:19 +0100, Pedro Alves wrote:
> On 11/01/2014 09:47 PM, Jan Kratochvil wrote:
> > +/* See gdbarch.sh 'infcall_mmap'.  */
> > +
> > +static CORE_ADDR
> > +linux_infcall_mmap (CORE_ADDR size, unsigned prot)
> > +{
> > +  struct objfile *objf;
> > +  /* Do there still exist any Linux systems without "mmap64"?
> > +     "mmap" uses 64-bit off_t on x86_64 and 32-bit off_t on i386 and x32.  */
> > +  struct value *mmap_val = find_function_in_inferior ("mmap64", &objf);
> > +  struct value *addr_val;
> > +  struct gdbarch *gdbarch = get_objfile_arch (objf);
> > +  CORE_ADDR retval;
> > +  enum
> > +    {
> > +      ARG_ADDR, ARG_LENGTH, ARG_PROT, ARG_FLAGS, ARG_FD, ARG_OFFSET, ARG_MAX
> > +    };
> > +  struct value *arg[ARG_MAX];
> > +
> > +  arg[ARG_ADDR] = value_from_pointer (builtin_type (gdbarch)->builtin_data_ptr,
> > +				      0);
> > +  /* Assuming sizeof (unsigned long) == sizeof (size_t).  */
> > +  arg[ARG_LENGTH] = value_from_ulongest
> > +		    (builtin_type (gdbarch)->builtin_unsigned_long, size);
> > +  gdb_assert ((prot & ~7) == 0);
> > +  arg[ARG_PROT] = value_from_longest (builtin_type (gdbarch)->builtin_int,
> > +				      0
> > +				      | ((prot & 4) != 0 ? PROT_READ : 0)
> > +				      | ((prot & 2) != 0 ? PROT_WRITE : 0)
> > +				      | ((prot & 1) != 0 ? PROT_EXEC : 0));
> > +  arg[ARG_FLAGS] = value_from_longest (builtin_type (gdbarch)->builtin_int,
> > +				       MAP_PRIVATE | MAP_ANONYMOUS);
> 
> PROT_READ, PROT_WRITE, PROT_EXEC, MAP_PRIVATE, MAP_ANONYMOUS
> are host values/macros.

Added to arch-utils.h:
+/* Symbols for gdbarch_infcall_mmap; their Linux PROT_* system
+   definitions would be dependent on compilation host.  */
+#define GDB_MMAP_PROT_READ     0x1     /* Page can be read.  */
+#define GDB_MMAP_PROT_WRITE    0x2     /* Page can be written.  */
+#define GDB_MMAP_PROT_EXEC     0x4     /* Page can be executed.  */

Added to linux-tdep.c:
+/* Symbols for linux_infcall_mmap's ARG_FLAGS; their Linux MAP_* system
+   definitions would be dependent on compilation host.  */
+#define GDB_MMAP_MAP_PRIVATE   0x02            /* Changes are private.  */
+#define GDB_MMAP_MAP_ANONYMOUS 0x20            /* Don't use a file.  */
and
-  gdb_assert ((prot & ~7) == 0);
+  gdb_assert ((prot & ~(GDB_MMAP_PROT_READ | GDB_MMAP_PROT_WRITE
+                       | GDB_MMAP_PROT_EXEC))
+             == 0);

Changed gdbarch.sh comment:
-# PROT has rwx bitmask format - bit 2 (value 4) is for readable memory, bit 1
-# (value 2) is for writable memory and bit 0 (value 1) is for executable memory.
+# PROT has GDB_MMAP_PROT_* bitmask format.

Change changed the magic numbers to GDB_MMAP_PROT_* accordingly:
       // Make the memory always readable.
-      prot = 4;
+      prot = GDB_MMAP_PROT_READ;
       if ((bfd_get_section_flags (abfd, sect) & SEC_READONLY) == 0)
-       prot |= 2;
+       prot |= GDB_MMAP_PROT_WRITE;
       if ((bfd_get_section_flags (abfd, sect) & SEC_CODE) != 0)
-       prot |= 1;
+       prot |= GDB_MMAP_PROT_EXEC;
[...]
-                                       TYPE_LENGTH (regs_type), 4);
+                                       TYPE_LENGTH (regs_type),
+                                       GDB_MMAP_PROT_READ);


> It also likely breaks --enable-targets=all builds on hosts
> that don't have mmap at all.  E.g., this comes out empty on F20:
> 
>  $ grep -rn PROT_READ /usr/i686-w64-mingw32/sys-root/mingw/include/

This could be caught automatically by Gerrit+Jenkins like other projects do
instead of wasting engineering time to do by hand all the parts of reviews
which machines can do.  Moreover such regressions would be caught before the
commit, compared to the GDB regressions happening post-commit as there is no
automatic pre-commit checking.  My Gerrit proposal was denied and replaced by
Patchwork instead.  This also makes regression results comparison difficult to
do as the regressions stack on top of each others.


Thanks,
Jan

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v3 14/14] the "compile" command
  2014-11-23 18:36     ` Jan Kratochvil
@ 2014-12-12 14:38       ` Pedro Alves
  0 siblings, 0 replies; 35+ messages in thread
From: Pedro Alves @ 2014-12-12 14:38 UTC (permalink / raw)
  To: Jan Kratochvil; +Cc: gdb-patches

On 11/23/2014 06:36 PM, Jan Kratochvil wrote:
> On Tue, 11 Nov 2014 19:53:26 +0100, Pedro Alves wrote:
>>> +/* See compile-internal.h.  */
>>> +
>>> +void
>>> +gcc_convert_symbol (void *datum,
>>> +		    struct gcc_c_context *gcc_context,
>>> +		    enum gcc_c_oracle_request request,
>>> +		    const char *identifier)
>>
>> ...
>>
>>> +
>>> +  /* We can't allow exceptions to escape out of this callback.  Safest
>>> +     is to simply emit a gcc error.  */
>>> +  TRY_CATCH (e, RETURN_MASK_ERROR)
>>> +    {
>>> +      struct symbol *sym;
>>
>> Shouldn't this catch ctrl-c too then?  Likewise the other hooks.
> 
> Yes, changed.
> 
> 
>>> +      for (i = 0; i < 4; ++i)
>>
>> It'd be good to give this magic constant a name, or at
>> a least a comment.
> 
> Added there:
> 	// Iterate all log2 sizes in bytes supported by c_get_mode_for_size.
> Added to c_get_mode_for_size:
> 	default:
> 	  internal_error (__FILE__, __LINE__, _("Invalid GCC mode size %d."), size);
> 
> 
>>> +	{
>>> +	  const char *mode = c_get_mode_for_size (1 << i);
>>> +
>>> +	  gdb_assert (mode != NULL);
>>> +	  fprintf_unfiltered (buf,
>>> +			      "typedef int"
>>> +			      " __attribute__ ((__mode__(__%s__)))"
>>> +			      " __gdb_int_%s;\n",
>>> +			      mode, mode);
>>> +	}
>>
>>
>>
>>> +
>>> +/* A cleanup function to remove a directory and all its contents.  */
>>> +
>>> +static void
>>> +do_rmdir (void *arg)
>>> +{
>>> +  char *zap = concat ("rm -rf ", arg, (char *) NULL);
>>> +
>>> +  system (zap);
>>> +}
>>
>> This is quite scary...  Could we please add an assert here
>> that tempdir_name starts with "/tmp/gdbobj-", just in case something
>> goes really wrong here?
> 
> Added:
> 	/* Initial filename for temporary files.  */
> 
> 	#define TMP_PREFIX "/tmp/gdbobj-"
> +
> 	#define TEMPLATE TMP_PREFIX "XXXXXX"
> +
> 	gdb_assert (strncmp (dir, TMP_PREFIX, strlen (TMP_PREFIX)) == 0);
> 
> 
>>> +  /* Override flags possibly coming from DW_AT_producer.  */
>>> +  compile_args = xstrdup ("-O0 -gdwarf-4"
>>> +  /* We use -fPIC to ensure that we can reference properly.  Otherwise
>>> +     on x86-64 a string constant's address might be truncated when gdb
>>> +     loads the object; another approach would be -mcmodel=large, but
>>> +     -fPIC seems more portable across back ends.  */
>>
>> This comment ends up being a bit unexpected/odd, given that ...
>>
>>> +			 " -fPIC"
>>> +  /* We don't want warnings.  */
>>> +			 " -w"
>>
>> ... patch #6 has:
>>
>>> +char *
>>> +default_gcc_target_options (struct gdbarch *gdbarch)
>>> +{
>>> +  return xstrprintf ("-m%d%s", gdbarch_ptr_bit (gdbarch),
>>> +		     gdbarch_ptr_bit (gdbarch) == 64 ? " -mcmodel=large" : "");
>>> +}
>>> +
>>
>> IOW, is the comment stale?
> 
> You are right the comment is stale although the code is not stale.  Up to date
> reason for these options I have written in:
> 	https://sourceware.org/gdb/wiki/GCCCompileAndExecute#Relocating_the_object_file
> Therefore put there:
> 	  /* We use -fPIC Otherwise GDB would need to reserve space large enough for
> 	     any object file in the inferior in advance to get the final address when
> 	     to link the object file to and additionally the default system linker
> 	     script would need to be modified so that one can specify there the
> 	     absolute target address.  */
> 				" -fPIC"
> And also:
> 	/* -mcmodel=large is used so that no GOT (Global Offset Table) is needed to be
> 	   created in inferior memory by GDB (normally it is set by ld.so).  */
> 
> 	char *
> 	default_gcc_target_options (struct gdbarch *gdbarch)
> 	{
> 	  return xstrprintf ("-m%d%s", gdbarch_ptr_bit (gdbarch),
> 			     gdbarch_ptr_bit (gdbarch) == 64 ? " -mcmodel=large" : "");
> 	}
> 

Thanks Jan.  I'm happy with these changes.

Thanks,
Pedro Alves

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v3 12/14] add linux_infcall_mmap
  2014-11-23 19:11     ` Jan Kratochvil
@ 2014-12-12 14:38       ` Pedro Alves
  0 siblings, 0 replies; 35+ messages in thread
From: Pedro Alves @ 2014-12-12 14:38 UTC (permalink / raw)
  To: Jan Kratochvil; +Cc: gdb-patches

On 11/23/2014 07:11 PM, Jan Kratochvil wrote:
> On Tue, 11 Nov 2014 17:43:19 +0100, Pedro Alves wrote:
>> On 11/01/2014 09:47 PM, Jan Kratochvil wrote:
>>> +/* See gdbarch.sh 'infcall_mmap'.  */
>>> +
>>> +static CORE_ADDR
>>> +linux_infcall_mmap (CORE_ADDR size, unsigned prot)
>>> +{
>>> +  struct objfile *objf;
>>> +  /* Do there still exist any Linux systems without "mmap64"?
>>> +     "mmap" uses 64-bit off_t on x86_64 and 32-bit off_t on i386 and x32.  */
>>> +  struct value *mmap_val = find_function_in_inferior ("mmap64", &objf);
>>> +  struct value *addr_val;
>>> +  struct gdbarch *gdbarch = get_objfile_arch (objf);
>>> +  CORE_ADDR retval;
>>> +  enum
>>> +    {
>>> +      ARG_ADDR, ARG_LENGTH, ARG_PROT, ARG_FLAGS, ARG_FD, ARG_OFFSET, ARG_MAX
>>> +    };
>>> +  struct value *arg[ARG_MAX];
>>> +
>>> +  arg[ARG_ADDR] = value_from_pointer (builtin_type (gdbarch)->builtin_data_ptr,
>>> +				      0);
>>> +  /* Assuming sizeof (unsigned long) == sizeof (size_t).  */
>>> +  arg[ARG_LENGTH] = value_from_ulongest
>>> +		    (builtin_type (gdbarch)->builtin_unsigned_long, size);
>>> +  gdb_assert ((prot & ~7) == 0);
>>> +  arg[ARG_PROT] = value_from_longest (builtin_type (gdbarch)->builtin_int,
>>> +				      0
>>> +				      | ((prot & 4) != 0 ? PROT_READ : 0)
>>> +				      | ((prot & 2) != 0 ? PROT_WRITE : 0)
>>> +				      | ((prot & 1) != 0 ? PROT_EXEC : 0));
>>> +  arg[ARG_FLAGS] = value_from_longest (builtin_type (gdbarch)->builtin_int,
>>> +				       MAP_PRIVATE | MAP_ANONYMOUS);
>>
>> PROT_READ, PROT_WRITE, PROT_EXEC, MAP_PRIVATE, MAP_ANONYMOUS
>> are host values/macros.
> 
> Added to arch-utils.h:
> +/* Symbols for gdbarch_infcall_mmap; their Linux PROT_* system
> +   definitions would be dependent on compilation host.  */
> +#define GDB_MMAP_PROT_READ     0x1     /* Page can be read.  */
> +#define GDB_MMAP_PROT_WRITE    0x2     /* Page can be written.  */
> +#define GDB_MMAP_PROT_EXEC     0x4     /* Page can be executed.  */
> 
> Added to linux-tdep.c:
> +/* Symbols for linux_infcall_mmap's ARG_FLAGS; their Linux MAP_* system
> +   definitions would be dependent on compilation host.  */
> +#define GDB_MMAP_MAP_PRIVATE   0x02            /* Changes are private.  */
> +#define GDB_MMAP_MAP_ANONYMOUS 0x20            /* Don't use a file.  */
> and
> -  gdb_assert ((prot & ~7) == 0);
> +  gdb_assert ((prot & ~(GDB_MMAP_PROT_READ | GDB_MMAP_PROT_WRITE
> +                       | GDB_MMAP_PROT_EXEC))
> +             == 0);
> 
> Changed gdbarch.sh comment:
> -# PROT has rwx bitmask format - bit 2 (value 4) is for readable memory, bit 1
> -# (value 2) is for writable memory and bit 0 (value 1) is for executable memory.
> +# PROT has GDB_MMAP_PROT_* bitmask format.
> 
> Change changed the magic numbers to GDB_MMAP_PROT_* accordingly:
>        // Make the memory always readable.
> -      prot = 4;
> +      prot = GDB_MMAP_PROT_READ;
>        if ((bfd_get_section_flags (abfd, sect) & SEC_READONLY) == 0)
> -       prot |= 2;
> +       prot |= GDB_MMAP_PROT_WRITE;
>        if ((bfd_get_section_flags (abfd, sect) & SEC_CODE) != 0)
> -       prot |= 1;
> +       prot |= GDB_MMAP_PROT_EXEC;
> [...]
> -                                       TYPE_LENGTH (regs_type), 4);
> +                                       TYPE_LENGTH (regs_type),
> +                                       GDB_MMAP_PROT_READ);
> 


Thanks Jan.  This sounds good.

Thanks,
Pedro Alves

^ permalink raw reply	[flat|nested] 35+ messages in thread

end of thread, other threads:[~2014-12-12 14:38 UTC | newest]

Thread overview: 35+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-11-01 21:45 [PATCH v3 00/14] let gdb reuse gcc's C compiler Jan Kratochvil
2014-11-01 21:46 ` [PATCH v3 05/14] add dummy frame destructor Jan Kratochvil
2014-11-01 21:46 ` [PATCH v3 01/14] introduce ui_file_write_for_put Jan Kratochvil
2014-11-01 21:46 ` [PATCH v3 04/14] add make_unqualified_type Jan Kratochvil
2014-11-01 21:46 ` [PATCH v3 03/14] add some missing ops to DWARF assembler Jan Kratochvil
2014-11-01 21:46 ` [PATCH v3 08/14] introduce call_function_by_hand_dummy Jan Kratochvil
2014-11-01 21:46 ` [PATCH v3 06/14] add infcall_mmap and gcc_target_options gdbarch methods Jan Kratochvil
2014-11-01 21:46 ` [PATCH v3 07/14] add gnu_triplet_regexp gdbarch method Jan Kratochvil
2014-11-01 21:46 ` [PATCH v3 02/14] add gcc/gdb interface files Jan Kratochvil
2014-11-03 12:51   ` Yao Qi
2014-11-03 12:56     ` Jan Kratochvil
2014-11-03 13:24       ` Yao Qi
2014-11-01 21:47 ` [PATCH v3 12/14] add linux_infcall_mmap Jan Kratochvil
2014-11-11 16:43   ` Pedro Alves
2014-11-23 19:11     ` Jan Kratochvil
2014-12-12 14:38       ` Pedro Alves
2014-11-01 21:47 ` [PATCH v3 10/14] make dwarf_expr_frame_base_1 public Jan Kratochvil
2014-11-01 21:47 ` [PATCH v3 09/14] split dwarf2_fetch_cfa_info from dwarf2_compile_expr_to_ax Jan Kratochvil
2014-11-01 21:47 ` [PATCH v3 11/14] export dwarf2_reg_to_regnum_or_error Jan Kratochvil
2014-11-01 21:47 ` [PATCH v3 13/14] add s390_gcc_target_options Jan Kratochvil
2014-11-01 21:48 ` [PATCH v3 14/14] the "compile" command Jan Kratochvil
2014-11-02 16:03   ` Eli Zaretskii
2014-11-20 21:24     ` Jan Kratochvil
2014-11-21  7:58       ` Eli Zaretskii
2014-11-21 18:41         ` Jan Kratochvil
2014-11-21 19:47           ` Eli Zaretskii
2014-11-03 13:08   ` Yao Qi
2014-11-14 18:43     ` Jan Kratochvil
2014-11-11 18:53   ` Pedro Alves
2014-11-23 18:36     ` Jan Kratochvil
2014-12-12 14:38       ` Pedro Alves
2014-11-01 21:52 ` [PATCH v3 00/14] let gdb reuse gcc's C compiler Jan Kratochvil
2014-11-01 21:56 ` Jan Kratochvil
2014-11-03 12:47 ` Yao Qi
2014-11-03 12:49   ` Jan Kratochvil

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).