+2015-11-07  Kevin Buettner  <kevinb@redhat.com>
+
+       * lib/gdb (gdb_target_symbol_prefix_flags_asm): New proc.
+       (gdb_target_symbol_prefix_flags): Define in terms of _asm
+       version.
+       * gdb.arch/i386-float.exp, gdb.arch/i386-permbkpt.exp,
+       gdb.dwarf2/dw2-canonicalize-type.exp,
+       gdb.dwarf2/dw2-compdir-oldgcc.exp, gdb.dwarf2/dw2-minsym-in-cu.exp,
+       gdb.dwarf2/dw2-op-stack-value.exp, gdb.dwarf2/dw2-unresolved.exp,
+       gdb.dwarf2/fission-reread.exp, gdb.dwarf2/pr13961.exp: Use flags
+       provided by gdb_target_symbol_prefix_flags_asm.
+       * gdb.dwarf2/dw2-canonicalize-type.S, gdb.dwarf2/dw2-compdir-oldgcc.S,
+       testsuite/gdb.dwarf2/dw2-minsym-in-cu.S,
+       testsuite/gdb.dwarf2/dw2-unresolved-main.c,
+       testsuite/gdb.dwarf2/dw2-unresolved.S, gdb.dwarf2/fission-reread.S,
+       gdb.dwarf2/pr13961.S: Define and use SYMBOL macro (and supporting
+       macros where needed).  Use this macro for symbols which require
+       the prefix provided by SYMBOL_PREFIX.
+
 2015-11-05  Kevin Buettner  <kevinb@redhat.com>
 
        * lib/gdb.exp (gdb_target_symbol_prefix, gdb_target_symbol):
 
 standard_testfile .S
 
 # some targets have leading underscores on assembly symbols.
-set additional_flags [gdb_target_symbol_prefix_flags]
+set additional_flags [gdb_target_symbol_prefix_flags_asm]
 
 if { [prepare_for_testing break.exp $testfile $srcfile [list debug $additional_flags]] } {
     return -1
 
 standard_testfile .S
 
 # some targets have leading underscores on assembly symbols.
-set additional_flags [gdb_target_symbol_prefix_flags]
+set additional_flags [gdb_target_symbol_prefix_flags_asm]
 
 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable [list debug $additional_flags]] != "" } {
     untested i386-permbkpt.exp
 
    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 CONCAT1(a, b) CONCAT2(a, b)
+#define CONCAT2(a, b) a ## b
+
+#ifdef SYMBOL_PREFIX
+# define SYMBOL(str)     CONCAT1(SYMBOL_PREFIX, str)
+#else
+# define SYMBOL(str)     str
+#endif
+
        .text
-       .globl main
-main:
+       .globl SYMBOL(main)
+SYMBOL(main):
        .4byte  0
 .Lmain_end:
        .section        .debug_info
        .ascii "GNU C 4.4.3\0"  /* DW_AT_producer */
        .byte   0x4     /* DW_AT_language = DW_LANG_C_plus_plus */
        .ascii "1.c\0"  /* DW_AT_name */
-       .4byte  main            /* DW_AT_low_pc */
+       .4byte  SYMBOL(main)            /* DW_AT_low_pc */
        .4byte  .Lmain_end      /* DW_AT_high_pc */
        .uleb128 0x4    /* (DIE (0x3c) DW_TAG_subprogram) */
        .ascii "f\0"    /* DW_AT_name */
 /* Value 0 would require has_section_at_zero != 0 (which is true, though).  */
-       .4byte  main            /* DW_AT_low_pc */
+       .4byte  SYMBOL(main)            /* DW_AT_low_pc */
        .4byte  .Lmain_end      /* DW_AT_high_pc */
        .byte   0x1     /* DW_AT_prototyped */
 
 
     return 0  
 }
 
+# Some targets have leading underscores on assembly symbols.
+set additional_flags [gdb_target_symbol_prefix_flags_asm]
+
 standard_testfile .S
 set executable ${testfile}
 
-if [prepare_for_testing $testfile.exp $testfile $srcfile {nodebug}] {
+if [prepare_for_testing $testfile.exp $testfile $srcfile \
+       [list nodebug $additional_flags]] {
     return -1
 }
 
 
    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 CONCAT1(a, b) CONCAT2(a, b)
+#define CONCAT2(a, b) a ## b
+
+#ifdef SYMBOL_PREFIX
+# define SYMBOL(str)     CONCAT1(SYMBOL_PREFIX, str)
+#else
+# define SYMBOL(str)     str
+#endif
+
        .text
-main:  .globl main
+SYMBOL(main):  .globl SYMBOL(main)
 
 gcc42: .globl gcc42
 .Lgcc42_procstart:
 
     return 0  
 }
 
+# Some targets have leading underscores on assembly symbols.
+set additional_flags [gdb_target_symbol_prefix_flags_asm]
+
 standard_testfile .S
-if {[prepare_for_testing $testfile.exp $testfile $srcfile]} {
+if {[prepare_for_testing $testfile.exp $testfile $srcfile \
+       $additional_flags]} {
     return -1
 }
 
 
    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 CONCAT1(a, b) CONCAT2(a, b)
+#define CONCAT2(a, b) a ## b
+
+#ifdef SYMBOL_PREFIX
+# define SYMBOL(str)     CONCAT1(SYMBOL_PREFIX, str)
+#else
+# define SYMBOL(str)     str
+#endif
+
        .text
 .Lbegin_text1:
 
-       .globl main
-       .type main, %function
-main:
+       .globl SYMBOL(main)
+       .type SYMBOL(main), %function
+SYMBOL(main):
 .Lbegin_main:
        .int 0
 .Lend_main:
-       .size main, .-main
+       .size SYMBOL(main), .-SYMBOL(main)
 
        .globl func2
        .type func2, %function
 
 
 # This testfile has reproducibility only with cc-with-index.sh.
 
+# Some targets have leading underscores on assembly symbols.
+set additional_flags [gdb_target_symbol_prefix_flags_asm]
+
 standard_testfile .S
 
-if [prepare_for_testing ${testfile}.exp ${testfile} ${srcfile}] {
+if [prepare_for_testing ${testfile}.exp ${testfile} ${srcfile} \
+       ${additional_flags}] {
     return -1
 }
 
 
     return 0   
 }
 
+# Some targets have leading underscores on assembly symbols.
+set additional_flags [gdb_target_symbol_prefix_flags_asm]
+
 standard_testfile .S
 
 if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" $binfile \
 
 
 #include <stdlib.h>
 
+#define CONCAT1(a, b) CONCAT2(a, b)
+#define CONCAT2(a, b) a ## b
+
+#ifdef SYMBOL_PREFIX
+# define SYMBOL1(str)     CONCAT1(SYMBOL_PREFIX, str)
+#else
+# define SYMBOL1(str)     str
+#endif
+
+#define STR1(s) #s
+#define STR(s) STR1(s)
+
+#define SYMBOL(str)     STR(SYMBOL1(str))
+
 asm (".globl cu_text_start");
 asm ("cu_text_start:");
 
     extern unsigned char var;
 
     /* Do not rely on the `extern' DIE output by GCC (GCC PR debug/39563).  */
-asm (".globl extern_block_start");
-asm ("extern_block_start:");
+asm (".globl " SYMBOL(extern_block_start));
+asm (SYMBOL(extern_block_start) ":");
     if (var != 2)
       abort ();
-asm (".globl extern_block_end");
-asm ("extern_block_end:");
+asm (".globl " SYMBOL(extern_block_end));
+asm (SYMBOL(extern_block_end) ":");
   }
 
   return 0;
 
    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 CONCAT1(a, b) CONCAT2(a, b)
+#define CONCAT2(a, b) a ## b
+
+#ifdef SYMBOL_PREFIX
+# define SYMBOL(str)     CONCAT1(SYMBOL_PREFIX, str)
+#else
+# define SYMBOL(str)     str
+#endif
+
        .data
 
 /* VAR1 is wrong here, in the real inferior it is located on stack.  As both
    places are never modified and they are initialized to the same value it
    makes no difference.  Ensure the name clash for "var".  */
-var1:  .byte   1
+SYMBOL(var1):  .byte   1
 
-       .globl  var
-var:   .byte   2
+       .globl  SYMBOL(var)
+SYMBOL(var):   .byte   2
 
 /* Debug information */
 
        .byte           2f - 1f                 /* DW_AT_location */
 1:     .byte           3                       /*   DW_OP_addr */
 /* See VAR1 definition why this DIE is not correct.  */
-       .4byte          var1                    /*   <addr> */
+       .4byte          SYMBOL(var1)                    /*   <addr> */
 2:     .4byte          .Ltype_uchar-.Lcu1_begin        /* DW_AT_type */
 
        .uleb128        6                       /* Abbrev: DW_TAG_lexical_block */
-       .4byte          extern_block_start      /* DW_AT_low_pc */
-       .4byte          extern_block_end        /* DW_AT_high_pc */
+       .4byte          SYMBOL(extern_block_start)      /* DW_AT_low_pc */
+       .4byte          SYMBOL(extern_block_end)        /* DW_AT_high_pc */
 
        .uleb128        5                       /* Abbrev: DW_TAG_variable (extern) */
        .ascii          "var\0"                 /* DW_AT_name */
 
     return 0  
 }
 
-if { [prepare_for_testing dw2-unresolved.exp "dw2-unresolved" {dw2-unresolved-main.c dw2-unresolved.S} {nodebug}] } {
+# Some targets have leading underscores on assembly symbols.
+set additional_flags [gdb_target_symbol_prefix_flags_asm]
+
+if { [prepare_for_testing dw2-unresolved.exp "dw2-unresolved" \
+       {dw2-unresolved-main.c dw2-unresolved.S} \
+       [list nodebug $additional_flags]] } {
     return -1
 }
 
 
    further hand-edited to support that.
 */
 
+#define CONCAT1(a, b) CONCAT2(a, b)
+#define CONCAT2(a, b) a ## b
+
+#ifdef SYMBOL_PREFIX
+# define SYMBOL(str)     CONCAT1(SYMBOL_PREFIX, str)
+#else
+# define SYMBOL(str)     str
+#endif
+
        .file   "fission-reread.cc"
 
-       .globl  baz
+       .globl  SYMBOL(baz)
        .data   /* Previously this used .bss, but it's not portable.  */
        .align 4
-       .type   baz, %object
-       .size   baz, 4
-baz:
+       .type   SYMBOL(baz), %object
+       .size   SYMBOL(baz), 4
+SYMBOL(baz):
        .zero   4
 
        .text
 .Ltext0:
-       .globl  main
-       .type   main, %function
-main:
+       .globl  SYMBOL(main)
+       .type   SYMBOL(main), %function
+SYMBOL(main):
 .LFB0:
        .file 1 "fission-reread.cc"
        .loc 1 11 0
        .4byte 0
 .LFE0:
-       .size   main, .-main
+       .size   SYMBOL(main), .-SYMBOL(main)
 .Letext0:
 
        .section        .debug_types.dwo
        .section        .debug_addr
 .Ldebug_addr0:
        .4byte  .LFB0   /* DW_AT_low_pc */
-       .4byte  baz     /* DW_AT_location */
+       .4byte  SYMBOL(baz)     /* DW_AT_location */
 
     return 0  
 }
 
+# Some targets have leading underscores on assembly symbols.
+set additional_flags [gdb_target_symbol_prefix_flags_asm]
+
 standard_testfile .S
 
 if [build_executable_from_fission_assembler \
-       "$testfile.exp" "$binfile" "$srcfile" {nodebug}] {
+       "$testfile.exp" "$binfile" "$srcfile" \
+       [list nodebug $additional_flags]] {
     return -1
 }
 
 
    further hand-edited to support that.
 */
 
+#define CONCAT1(a, b) CONCAT2(a, b)
+#define CONCAT2(a, b) a ## b
+
+#ifdef SYMBOL_PREFIX
+# define SYMBOL(str)     CONCAT1(SYMBOL_PREFIX, str)
+#else
+# define SYMBOL(str)     str
+#endif
+
        .file   "pr13961.cc"
 
        .globl  baz
 
        .text
 .Ltext0:
-       .globl  main
-       .type   main, %function
-main:
+       .globl  SYMBOL(main)
+       .type   SYMBOL(main), %function
+SYMBOL(main):
 .LFB0:
        .4byte 0
 .LFE0:
-       .size   main, .-main
+       .size   SYMBOL(main), .-SYMBOL(main)
 .Letext0:
 
        .section        .debug_types,"",%progbits
 
     return 0
 }
 
+# Some targets have leading underscores on assembly symbols.
+set additional_flags [gdb_target_symbol_prefix_flags_asm]
+
 standard_testfile .S
 
-if { [prepare_for_testing ${testfile}.exp ${testfile} ${srcfile}] == -1 } {
+if { [prepare_for_testing ${testfile}.exp ${testfile} ${srcfile} \
+       ${additional_flags}] == -1 } {
     return -1
 }
 
 
   return "${prefix}${symbol}"
 }
 
-# gdb_target_symbol_prefix_flags returns a string that can be added
-# to gdb_compile options to define SYMBOL_PREFIX macro value
-# symbol_prefix_flags returns a string that can be added
-# for targets that use underscore as symbol prefix.
-# TODO: find out automatically if the target needs this.
+# gdb_target_symbol_prefix_flags_asm returns a string that can be
+# added to gdb_compile options to define the C-preprocessor macro
+# SYMBOL_PREFIX with a value that can be prepended to symbols
+# for targets which require a prefix, such as underscore.
+#
+# This version (_asm) defines the prefix without double quotes
+# surrounding the prefix.  It is used to define the macro
+# SYMBOL_PREFIX for assembly language files.  Another version, below,
+# is used for symbols in inline assembler in C/C++ files.
+# 
+# The lack of quotes in this version (_asm) makes it possible to
+# define supporting macros in the .S file.  (The version which
+# uses quotes for the prefix won't work for such files since it's
+# impossible to define a quote-stripping macro in C.)
+#
+# It's possible to use this version (_asm) for C/C++ source files too,
+# but a string is usually required in such files; providing a version
+# (no _asm) which encloses the prefix with double quotes makes it
+# somewhat easier to define the supporting macros in the test case.
+
+proc gdb_target_symbol_prefix_flags_asm {} {
+    set prefix [gdb_target_symbol_prefix]
+    if {$prefix ne ""} {
+       return "additional_flags=-DSYMBOL_PREFIX=$prefix"
+    } else {
+       return "";
+    }
+}
+
+# gdb_target_symbol_prefix_flags returns the same string as
+# gdb_target_symbol_prefix_flags_asm, above, but with the prefix
+# enclosed in double quotes if there is a prefix.
+#
+# See the comment for gdb_target_symbol_prefix_flags_asm for an
+# extended discussion.
 
 proc gdb_target_symbol_prefix_flags {} {
-    if { [istarget "i?86-*-cygwin*"] || [istarget "i?86-*-mingw*"]
-        || [istarget "*-*-msdosdjgpp*"] || [istarget "*-*-go32*"] } {
-       return "additional_flags=-DSYMBOL_PREFIX=\"_\""
+    set prefix [gdb_target_symbol_prefix]
+    if {$prefix ne ""} {
+       return "additional_flags=-DSYMBOL_PREFIX=\"$prefix\""
     } else {
-       return ""
+       return "";
     }
 }