]> git.pld-linux.org Git - packages/gdb.git/blob - gdb-archer.patch
80e3cf283bfeb63ec7444d6ddf79da36bba5273f
[packages/gdb.git] / gdb-archer.patch
1 http://sourceware.org/gdb/wiki/ProjectArcher
2 http://sourceware.org/gdb/wiki/ArcherBranchManagement
3
4 GIT snapshot:
5 commit 897686c0aedb7a6da59b823f22b1d24ec5a1d2ba
6
7 branch `archer' - the merge of branches:
8 archer-jankratochvil-vla
9 archer-tromey-python
10 archer-tromey-dwz-multifile-rebase (but from post-7.5 FSF GDB commits)
11
12
13 diff --git a/gdb/Makefile.in b/gdb/Makefile.in
14 index a41cff9..d7786a6 100644
15 --- a/gdb/Makefile.in
16 +++ b/gdb/Makefile.in
17 @@ -704,7 +704,7 @@ SFILES = ada-exp.y ada-lang.c ada-typeprint.c ada-valprint.c ada-tasks.c \
18         exceptions.c expprint.c \
19         f-exp.y f-lang.c f-typeprint.c f-valprint.c filesystem.c \
20         findcmd.c findvar.c frame.c frame-base.c frame-unwind.c \
21 -       gdbarch.c arch-utils.c gdbtypes.c gnu-v2-abi.c gnu-v3-abi.c \
22 +       gdbarch.c arch-utils.c gdb_bfd.c gdbtypes.c gnu-v2-abi.c gnu-v3-abi.c \
23         go-exp.y go-lang.c go-typeprint.c go-valprint.c \
24         inf-loop.c \
25         infcall.c \
26 @@ -829,7 +829,7 @@ gnulib/import/extra/snippet/warn-on-use.h \
27  gnulib/import/stddef.in.h gnulib/import/inttypes.in.h inline-frame.h skip.h \
28  common/common-utils.h common/xml-utils.h common/buffer.h common/ptid.h \
29  common/format.h \
30 -common/linux-osdata.h gdb-dlfcn.h auto-load.h probe.h stap-probe.h
31 +common/linux-osdata.h gdb-dlfcn.h auto-load.h probe.h stap-probe.h gdb_bfd.h
32  
33  # Header files that already have srcdir in them, or which are in objdir.
34  
35 @@ -879,7 +879,7 @@ COMMON_OBS = $(DEPFILES) $(CONFIG_OBS) $(YYOBJ) \
36         macrotab.o macrocmd.o macroexp.o macroscope.o \
37         mi-common.o \
38         event-loop.o event-top.o inf-loop.o completer.o \
39 -       gdbarch.o arch-utils.o gdbtypes.o osabi.o copying.o \
40 +       gdbarch.o arch-utils.o gdbtypes.o gdb_bfd.o osabi.o copying.o \
41         memattr.o mem-break.o target.o parse.o language.o buildsym.o \
42         findcmd.o \
43         std-regs.o \
44 @@ -1307,6 +1307,12 @@ stamp-h: $(srcdir)/config.in config.status
45           CONFIG_LINKS= \
46           $(SHELL) config.status
47  
48 +.gdbinit: $(srcdir)/gdbinit.in config.status
49 +       CONFIG_FILES=".gdbinit:gdbinit.in" \
50 +         CONFIG_COMMANDS= \
51 +         CONFIG_HEADERS= \
52 +         $(SHELL) config.status
53 +
54  config.status: $(srcdir)/configure configure.tgt configure.host
55         $(SHELL) config.status --recheck
56  
57 diff --git a/gdb/NEWS b/gdb/NEWS
58 index b281824..4da886c 100644
59 --- a/gdb/NEWS
60 +++ b/gdb/NEWS
61 @@ -1,6 +1,13 @@
62                 What has changed in GDB?
63              (Organized release by release)
64  
65 +*** Changes since GDB 7.5
66 +
67 +* New commands (for set/show, see "New options" below)
68 +
69 +maint info bfds
70 +  List the BFDs known to GDB.
71 +
72  *** Changes in GDB 7.5
73  
74  * GDB now supports x32 ABI.  Visit <http://sites.google.com/site/x32abi/>
75 diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c
76 index aa090af..e672731 100644
77 --- a/gdb/ada-lang.c
78 +++ b/gdb/ada-lang.c
79 @@ -12037,6 +12037,7 @@ ada_operator_length (const struct expression *exp, int pc, int *oplenp,
80  
81  static int
82  ada_operator_check (struct expression *exp, int pos,
83 +                   int (*type_func) (struct type *type, void *data),
84                     int (*objfile_func) (struct objfile *objfile, void *data),
85                     void *data)
86  {
87 @@ -12051,12 +12052,15 @@ ada_operator_check (struct expression *exp, int pos,
88         break;
89  
90        default:
91 -       return operator_check_standard (exp, pos, objfile_func, data);
92 +       return operator_check_standard (exp, pos, type_func, objfile_func,
93 +                                       data);
94      }
95  
96    /* Invoke callbacks for TYPE and OBJFILE if they were set as non-NULL.  */
97  
98 -  if (type && TYPE_OBJFILE (type)
99 +  if (type && type_func && (*type_func) (type, data))
100 +    return 1;
101 +  if (type && TYPE_OBJFILE (type) && objfile_func
102        && (*objfile_func) (TYPE_OBJFILE (type), data))
103      return 1;
104  
105 diff --git a/gdb/bfd-target.c b/gdb/bfd-target.c
106 index 6728800..455d3e6 100644
107 --- a/gdb/bfd-target.c
108 +++ b/gdb/bfd-target.c
109 @@ -21,6 +21,7 @@
110  #include "target.h"
111  #include "bfd-target.h"
112  #include "exec.h"
113 +#include "gdb_bfd.h"
114  
115  /* The object that is stored in the target_ops->to_data field has this
116     type.  */
117 @@ -70,7 +71,7 @@ target_bfd_xclose (struct target_ops *t, int quitting)
118  {
119    struct target_bfd_data *data = t->to_data;
120  
121 -  bfd_close (data->bfd);
122 +  gdb_bfd_unref (data->bfd);
123    xfree (data->table.sections);
124    xfree (data);
125    xfree (t);
126 @@ -84,6 +85,7 @@ target_bfd_reopen (struct bfd *abfd)
127  
128    data = XZALLOC (struct target_bfd_data);
129    data->bfd = abfd;
130 +  gdb_bfd_ref (abfd);
131    build_section_table (abfd, &data->table.sections, &data->table.sections_end);
132  
133    t = XZALLOC (struct target_ops);
134 diff --git a/gdb/bfd-target.h b/gdb/bfd-target.h
135 index 71001c5..7f4e628 100644
136 --- a/gdb/bfd-target.h
137 +++ b/gdb/bfd-target.h
138 @@ -23,9 +23,9 @@
139  struct bfd;
140  struct target_ops;
141  
142 -/* Given an existing BFD, re-open it as a "struct target_ops".  On
143 -   close, it will also close the corresponding BFD (which is like
144 -   freopen and fdopen).  */
145 +/* Given an existing BFD, re-open it as a "struct target_ops".  This
146 +   acquires a new reference to the BFD.  This reference will be
147 +   released when the target is closed.  */
148  struct target_ops *target_bfd_reopen (struct bfd *bfd);
149  
150  #endif
151 diff --git a/gdb/block.c b/gdb/block.c
152 index a0f82ec..097dbf6 100644
153 --- a/gdb/block.c
154 +++ b/gdb/block.c
155 @@ -692,3 +692,21 @@ block_iter_match_next (const char *name,
156  
157    return block_iter_match_step (iterator, name, compare, 0);
158  }
159 +
160 +/* Return OBJFILE in which BLOCK is located or NULL if we cannot find it for
161 +   whatever reason.  */
162 +
163 +struct objfile *
164 +block_objfile (const struct block *block)
165 +{
166 +  struct symbol *func;
167 +
168 +  if (block == NULL)
169 +    return NULL;
170 +
171 +  func = block_linkage_function (block);
172 +  if (func == NULL)
173 +    return NULL;
174 +
175 +  return SYMBOL_SYMTAB (func)->objfile;
176 +}
177 diff --git a/gdb/block.h b/gdb/block.h
178 index 99c4788..6ceb704 100644
179 --- a/gdb/block.h
180 +++ b/gdb/block.h
181 @@ -279,4 +279,6 @@ extern struct symbol *block_iter_match_next (const char *name,
182         (sym);                                          \
183         (sym) = block_iterator_next (&(iter)))
184  
185 +extern struct objfile *block_objfile (const struct block *block);
186 +
187  #endif /* BLOCK_H */
188 diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c
189 index 78fffd3..224ba49 100644
190 --- a/gdb/breakpoint.c
191 +++ b/gdb/breakpoint.c
192 @@ -15519,6 +15519,24 @@ all_tracepoints (void)
193    return tp_vec;
194  }
195  
196 +#if 0
197 +/* Call type_mark_used for any TYPEs referenced from this GDB source file.  */
198 +
199 +static void
200 +breakpoint_types_mark_used (void)
201 +{
202 +  struct breakpoint *b;
203 +
204 +  ALL_BREAKPOINTS (b)
205 +    {
206 +      if (b->exp)
207 +       exp_types_mark_used (b->exp);
208 +      if (b->val)
209 +       type_mark_used (value_type (b->val));
210 +    }
211 +}
212 +#endif
213 +
214  \f
215  /* This help string is used for the break, hbreak, tbreak and thbreak
216     commands.  It is defined as a macro to prevent duplication.
217 @@ -16484,4 +16502,7 @@ agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
218    automatic_hardware_breakpoints = 1;
219  
220    observer_attach_about_to_proceed (breakpoint_about_to_proceed);
221 +#if 0
222 +  observer_attach_mark_used (breakpoint_types_mark_used);
223 +#endif
224  }
225 diff --git a/gdb/buildsym.c b/gdb/buildsym.c
226 index d547012..e4882fb 100644
227 --- a/gdb/buildsym.c
228 +++ b/gdb/buildsym.c
229 @@ -966,11 +966,14 @@ reset_symtab_globals (void)
230     file's text.
231  
232     If EXPANDABLE is non-zero the STATIC_BLOCK dictionary is made
233 -   expandable.  */
234 +   expandable.
235 +
236 +   If REQUIRED is non-zero, then a symtab is created even if it does
237 +   not contain any symbols.  */
238  
239  struct block *
240  end_symtab_get_static_block (CORE_ADDR end_addr, struct objfile *objfile,
241 -                            int expandable)
242 +                            int expandable, int required)
243  {
244    /* Finish the lexical context of the last function in the file; pop
245       the context stack.  */
246 @@ -1038,7 +1041,8 @@ end_symtab_get_static_block (CORE_ADDR end_addr, struct objfile *objfile,
247    cleanup_undefined_stabs_types (objfile);
248    finish_global_stabs (objfile);
249  
250 -  if (pending_blocks == NULL
251 +  if (!required
252 +      && pending_blocks == NULL
253        && file_symbols == NULL
254        && global_symbols == NULL
255        && have_line_numbers == 0
256 @@ -1296,7 +1300,7 @@ end_symtab (CORE_ADDR end_addr, struct objfile *objfile, int section)
257  {
258    struct block *static_block;
259  
260 -  static_block = end_symtab_get_static_block (end_addr, objfile, 0);
261 +  static_block = end_symtab_get_static_block (end_addr, objfile, 0, 0);
262    return end_symtab_from_static_block (static_block, objfile, section, 0);
263  }
264  
265 @@ -1308,7 +1312,7 @@ end_expandable_symtab (CORE_ADDR end_addr, struct objfile *objfile,
266  {
267    struct block *static_block;
268  
269 -  static_block = end_symtab_get_static_block (end_addr, objfile, 1);
270 +  static_block = end_symtab_get_static_block (end_addr, objfile, 1, 0);
271    return end_symtab_from_static_block (static_block, objfile, section, 1);
272  }
273  
274 diff --git a/gdb/buildsym.h b/gdb/buildsym.h
275 index 162ee8c..33b34c8 100644
276 --- a/gdb/buildsym.h
277 +++ b/gdb/buildsym.h
278 @@ -260,7 +260,8 @@ extern char *pop_subfile (void);
279  
280  extern struct block *end_symtab_get_static_block (CORE_ADDR end_addr,
281                                                   struct objfile *objfile,
282 -                                                 int expandable);
283 +                                                 int expandable,
284 +                                                 int required);
285  
286  extern struct symtab *end_symtab_from_static_block (struct block *static_block,
287                                                     struct objfile *objfile,
288 diff --git a/gdb/c-typeprint.c b/gdb/c-typeprint.c
289 index a5892b5..2944c2d 100644
290 --- a/gdb/c-typeprint.c
291 +++ b/gdb/c-typeprint.c
292 @@ -624,9 +624,14 @@ c_type_print_varspec_suffix (struct type *type,
293           fprintf_filtered (stream, ")");
294  
295         fprintf_filtered (stream, "[");
296 -       if (get_array_bounds (type, &low_bound, &high_bound))
297 -         fprintf_filtered (stream, "%d", 
298 -                           (int) (high_bound - low_bound + 1));
299 +       if (TYPE_RANGE_DATA (TYPE_INDEX_TYPE (type))->high.kind
300 +           != RANGE_BOUND_KIND_CONSTANT)
301 +         {
302 +           /* No _() - printed sources should not be locale dependent.  */
303 +           fprintf_filtered (stream, "variable");
304 +         }
305 +       else if (get_array_bounds (type, &low_bound, &high_bound))
306 +         fprintf_filtered (stream, "%d", (int) (high_bound - low_bound + 1));
307         fprintf_filtered (stream, "]");
308  
309         c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream,
310 diff --git a/gdb/cc-with-dwz.sh b/gdb/cc-with-dwz.sh
311 deleted file mode 100755
312 index f66deb1..0000000
313 --- a/gdb/cc-with-dwz.sh
314 +++ /dev/null
315 @@ -1,80 +0,0 @@
316 -#! /bin/sh
317 -# Wrapper around gcc to run 'dwz' when running the testsuite.
318 -
319 -# Copyright (C) 2010-2012 Free Software Foundation, Inc.
320 -# This program is free software; you can redistribute it and/or modify
321 -# it under the terms of the GNU General Public License as published by
322 -# the Free Software Foundation; either version 3 of the License, or
323 -# (at your option) any later version.
324 -#
325 -# This program is distributed in the hope that it will be useful,
326 -# but WITHOUT ANY WARRANTY; without even the implied warranty of
327 -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
328 -# GNU General Public License for more details.
329 -#
330 -# You should have received a copy of the GNU General Public License
331 -# along with this program.  If not, see <http://www.gnu.org/licenses/>.
332 -
333 -# This program requires dwz in addition to gcc.
334 -#
335 -# Example usage:
336 -#
337 -# bash$ cd $objdir/gdb/testsuite
338 -# bash$ runtest \
339 -#   CC_FOR_TARGET="/bin/sh $srcdir/cc-with-dwz.sh gcc" \
340 -#   CXX_FOR_TARGET="/bin/sh $srcdir/cc-with-dwz.sh g++"
341 -#
342 -
343 -myname=cc-with-dwz.sh
344 -
345 -DWZ=${DWZ:-dwz}
346 -
347 -have_link=unknown
348 -next_is_output_file=no
349 -output_file=a.out
350 -
351 -for arg in "$@"
352 -do
353 -    if [ "$next_is_output_file" = "yes" ]
354 -    then
355 -       output_file="$arg"
356 -       next_is_output_file=no
357 -       continue
358 -    fi
359 -
360 -    # Poor man's gcc argument parser.
361 -    # We don't need to handle all arguments, we just need to know if we're
362 -    # doing a link and what the output file is.
363 -    # It's not perfect, but it seems to work well enough for the task at hand.
364 -    case "$arg" in
365 -    "-c") have_link=no ;;
366 -    "-E") have_link=no ;;
367 -    "-S") have_link=no ;;
368 -    "-o") next_is_output_file=yes ;;
369 -    esac
370 -done
371 -
372 -if [ "$next_is_output_file" = "yes" ]
373 -then
374 -    echo "$myname: Unable to find output file" >&2
375 -    exit 1
376 -fi
377 -
378 -if [ "$have_link" = "no" ]
379 -then
380 -    "$@"
381 -    exit $?
382 -fi
383 -
384 -"$@"
385 -rc=$?
386 -[ $rc != 0 ] && exit $rc
387 -if [ ! -f "$output_file" ]
388 -then
389 -    echo "$myname: Internal error: $output_file missing." >&2
390 -    exit 1
391 -fi
392 -
393 -$DWZ "$output_file" > /dev/null 2>&1
394 -
395 -exit 0
396 diff --git a/gdb/cc-with-index.sh b/gdb/cc-with-index.sh
397 deleted file mode 100644
398 index 644ba34..0000000
399 --- a/gdb/cc-with-index.sh
400 +++ /dev/null
401 @@ -1,126 +0,0 @@
402 -#! /bin/sh
403 -# Wrapper around gcc to add the .gdb_index section when running the testsuite.
404 -
405 -# Copyright (C) 2010-2012 Free Software Foundation, Inc.
406 -# This program is free software; you can redistribute it and/or modify
407 -# it under the terms of the GNU General Public License as published by
408 -# the Free Software Foundation; either version 3 of the License, or
409 -# (at your option) any later version.
410 -#
411 -# This program is distributed in the hope that it will be useful,
412 -# but WITHOUT ANY WARRANTY; without even the implied warranty of
413 -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
414 -# GNU General Public License for more details.
415 -#
416 -# You should have received a copy of the GNU General Public License
417 -# along with this program.  If not, see <http://www.gnu.org/licenses/>.
418 -
419 -# This program requires gdb and objcopy in addition to gcc.
420 -# The default values are gdb from the build tree and objcopy from $PATH.
421 -# They may be overridden by setting environment variables GDB and OBJCOPY
422 -# respectively.
423 -# We assume the current directory is either $obj/gdb or $obj/gdb/testsuite.
424 -#
425 -# Example usage:
426 -#
427 -# bash$ cd $objdir/gdb/testsuite
428 -# bash$ runtest \
429 -#   CC_FOR_TARGET="/bin/sh $srcdir/cc-with-index.sh gcc" \
430 -#   CXX_FOR_TARGET="/bin/sh $srcdir/cc-with-index.sh g++"
431 -#
432 -# For documentation on index files: info -f gdb.info -n "Index Files"
433 -
434 -myname=cc-with-index.sh
435 -
436 -if [ -z "$GDB" ]
437 -then
438 -    if [ -f ./gdb ]
439 -    then
440 -       GDB="./gdb"
441 -    elif [ -f ../gdb ]
442 -    then
443 -       GDB="../gdb"
444 -    elif [ -f ../../gdb ]
445 -    then
446 -       GDB="../../gdb"
447 -    else
448 -       echo "$myname: unable to find usable gdb" >&2
449 -       exit 1
450 -    fi
451 -fi
452 -
453 -OBJCOPY=${OBJCOPY:-objcopy}
454 -
455 -have_link=unknown
456 -next_is_output_file=no
457 -output_file=a.out
458 -
459 -for arg in "$@"
460 -do
461 -    if [ "$next_is_output_file" = "yes" ]
462 -    then
463 -       output_file="$arg"
464 -       next_is_output_file=no
465 -       continue
466 -    fi
467 -
468 -    # Poor man's gcc argument parser.
469 -    # We don't need to handle all arguments, we just need to know if we're
470 -    # doing a link and what the output file is.
471 -    # It's not perfect, but it seems to work well enough for the task at hand.
472 -    case "$arg" in
473 -    "-c") have_link=no ;;
474 -    "-E") have_link=no ;;
475 -    "-S") have_link=no ;;
476 -    "-o") next_is_output_file=yes ;;
477 -    esac
478 -done
479 -
480 -if [ "$next_is_output_file" = "yes" ]
481 -then
482 -    echo "$myname: Unable to find output file" >&2
483 -    exit 1
484 -fi
485 -
486 -if [ "$have_link" = "no" ]
487 -then
488 -    "$@"
489 -    exit $?
490 -fi
491 -
492 -index_file="${output_file}.gdb-index"
493 -if [ -f "$index_file" ]
494 -then
495 -    echo "$myname: Index file $index_file exists, won't clobber." >&2
496 -    exit 1
497 -fi
498 -
499 -output_dir="${output_file%/*}"
500 -[ "$output_dir" = "$output_file" ] && output_dir="."
501 -
502 -"$@"
503 -rc=$?
504 -[ $rc != 0 ] && exit $rc
505 -if [ ! -f "$output_file" ]
506 -then
507 -    echo "$myname: Internal error: $output_file missing." >&2
508 -    exit 1
509 -fi
510 -
511 -$GDB --batch-silent -nx -ex "set auto-load no" -ex "file $output_file" -ex "save gdb-index $output_dir"
512 -rc=$?
513 -[ $rc != 0 ] && exit $rc
514 -
515 -# GDB might not always create an index.  Cope.
516 -if [ -f "$index_file" ]
517 -then
518 -    $OBJCOPY --add-section .gdb_index="$index_file" \
519 -       --set-section-flags .gdb_index=readonly \
520 -       "$output_file" "$output_file"
521 -    rc=$?
522 -else
523 -    rc=0
524 -fi
525 -
526 -rm -f "$index_file"
527 -exit $rc
528 diff --git a/gdb/cli/cli-dump.c b/gdb/cli/cli-dump.c
529 index 4a8b5d1..7341f00 100644
530 --- a/gdb/cli/cli-dump.c
531 +++ b/gdb/cli/cli-dump.c
532 @@ -32,6 +32,7 @@
533  #include "readline/readline.h"
534  #include "gdbcore.h"
535  #include "cli/cli-utils.h"
536 +#include "gdb_bfd.h"
537  
538  #define XMALLOC(TYPE) ((TYPE*) xmalloc (sizeof (TYPE)))
539  
540 @@ -111,12 +112,12 @@ bfd_openr_with_cleanup (const char *filename, const char *target)
541  {
542    bfd *ibfd;
543  
544 -  ibfd = bfd_openr (filename, target);
545 +  ibfd = gdb_bfd_openr (filename, target);
546    if (ibfd == NULL)
547      error (_("Failed to open %s: %s."), filename, 
548            bfd_errmsg (bfd_get_error ()));
549  
550 -  make_cleanup_bfd_close (ibfd);
551 +  make_cleanup_bfd_unref (ibfd);
552    if (!bfd_check_format (ibfd, bfd_object))
553      error (_("'%s' is not a recognized file format."), filename);
554  
555 @@ -131,11 +132,11 @@ bfd_openw_with_cleanup (const char *filename, const char *target,
556  
557    if (*mode == 'w')    /* Write: create new file */
558      {
559 -      obfd = bfd_openw (filename, target);
560 +      obfd = gdb_bfd_openw (filename, target);
561        if (obfd == NULL)
562         error (_("Failed to open %s: %s."), filename, 
563                bfd_errmsg (bfd_get_error ()));
564 -      make_cleanup_bfd_close (obfd);
565 +      make_cleanup_bfd_unref (obfd);
566        if (!bfd_set_format (obfd, bfd_object))
567         error (_("bfd_openw_with_cleanup: %s."), bfd_errmsg (bfd_get_error ()));
568      }
569 diff --git a/gdb/coffread.c b/gdb/coffread.c
570 index b0a8b82..0c7e6d9 100644
571 --- a/gdb/coffread.c
572 +++ b/gdb/coffread.c
573 @@ -653,13 +653,14 @@ coff_symfile_read (struct objfile *objfile, int symfile_flags)
574        char *debugfile;
575  
576        debugfile = find_separate_debug_file_by_debuglink (objfile);
577 +      make_cleanup (xfree, debugfile);
578  
579        if (debugfile)
580         {
581           bfd *abfd = symfile_bfd_open (debugfile);
582  
583 +         make_cleanup_bfd_unref (abfd);
584           symbol_file_add_separate (abfd, symfile_flags, objfile);
585 -         xfree (debugfile);
586         }
587      }
588  
589 diff --git a/gdb/contrib/cc-with-tweaks.sh b/gdb/contrib/cc-with-tweaks.sh
590 new file mode 100755
591 index 0000000..7d7932c
592 --- /dev/null
593 +++ b/gdb/contrib/cc-with-tweaks.sh
594 @@ -0,0 +1,162 @@
595 +#! /bin/sh
596 +# Wrapper around gcc to tweak the output in various ways when running
597 +# the testsuite.
598 +
599 +# Copyright (C) 2010-2012 Free Software Foundation, Inc.
600 +# This program is free software; you can redistribute it and/or modify
601 +# it under the terms of the GNU General Public License as published by
602 +# the Free Software Foundation; either version 3 of the License, or
603 +# (at your option) any later version.
604 +#
605 +# This program is distributed in the hope that it will be useful,
606 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
607 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
608 +# GNU General Public License for more details.
609 +#
610 +# You should have received a copy of the GNU General Public License
611 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
612 +
613 +# This program requires gdb and objcopy in addition to gcc.
614 +# The default values are gdb from the build tree and objcopy from $PATH.
615 +# They may be overridden by setting environment variables GDB and OBJCOPY
616 +# respectively.
617 +# We assume the current directory is either $obj/gdb or $obj/gdb/testsuite.
618 +#
619 +# Example usage:
620 +#
621 +# bash$ cd $objdir/gdb/testsuite
622 +# bash$ runtest \
623 +#   CC_FOR_TARGET="/bin/sh $srcdir/gdb/contrib/cc-with-tweaks.sh ARGS gcc" \
624 +#   CXX_FOR_TARGET="/bin/sh $srcdir/gdb/contrib/cc-with-tweaks.sh ARGS g++"
625 +#
626 +# For documentation on index files: info -f gdb.info -n "Index Files"
627 +# For information about 'dwz', see the announcement:
628 +#     http://gcc.gnu.org/ml/gcc/2012-04/msg00686.html
629 +# (More documentation is to come.)
630 +
631 +# ARGS determine what is done.  They can be:
632 +# -z compress using dwz
633 +# -m compress using dwz -m
634 +# -i make an index
635 +# If nothing is given, no changes are made
636 +
637 +myname=cc-with-tweaks.sh
638 +
639 +if [ -z "$GDB" ]
640 +then
641 +    if [ -f ./gdb ]
642 +    then
643 +       GDB="./gdb"
644 +    elif [ -f ../gdb ]
645 +    then
646 +       GDB="../gdb"
647 +    elif [ -f ../../gdb ]
648 +    then
649 +       GDB="../../gdb"
650 +    else
651 +       echo "$myname: unable to find usable gdb" >&2
652 +       exit 1
653 +    fi
654 +fi
655 +
656 +OBJCOPY=${OBJCOPY:-objcopy}
657 +
658 +DWZ=${DWZ:-dwz}
659 +
660 +have_link=unknown
661 +next_is_output_file=no
662 +output_file=a.out
663 +
664 +want_index=false
665 +want_dwz=false
666 +want_multi=false
667 +
668 +while [ $# -gt 0 ]; do
669 +    case "$1" in
670 +       -z) want_dwz=true ;;
671 +       -i) want_index=true ;;
672 +       -m) want_multi=true ;;
673 +       *) break ;;
674 +    esac
675 +    shift
676 +done
677 +
678 +for arg in "$@"
679 +do
680 +    if [ "$next_is_output_file" = "yes" ]
681 +    then
682 +       output_file="$arg"
683 +       next_is_output_file=no
684 +       continue
685 +    fi
686 +
687 +    # Poor man's gcc argument parser.
688 +    # We don't need to handle all arguments, we just need to know if we're
689 +    # doing a link and what the output file is.
690 +    # It's not perfect, but it seems to work well enough for the task at hand.
691 +    case "$arg" in
692 +    "-c") have_link=no ;;
693 +    "-E") have_link=no ;;
694 +    "-S") have_link=no ;;
695 +    "-o") next_is_output_file=yes ;;
696 +    esac
697 +done
698 +
699 +if [ "$next_is_output_file" = "yes" ]
700 +then
701 +    echo "$myname: Unable to find output file" >&2
702 +    exit 1
703 +fi
704 +
705 +if [ "$have_link" = "no" ]
706 +then
707 +    "$@"
708 +    exit $?
709 +fi
710 +
711 +index_file="${output_file}.gdb-index"
712 +if [ "$want_index" = true ] && [ -f "$index_file" ]
713 +then
714 +    echo "$myname: Index file $index_file exists, won't clobber." >&2
715 +    exit 1
716 +fi
717 +
718 +output_dir="${output_file%/*}"
719 +[ "$output_dir" = "$output_file" ] && output_dir="."
720 +
721 +"$@"
722 +rc=$?
723 +[ $rc != 0 ] && exit $rc
724 +if [ ! -f "$output_file" ]
725 +then
726 +    echo "$myname: Internal error: $output_file missing." >&2
727 +    exit 1
728 +fi
729 +
730 +if [ "$want_index" = true ]; then
731 +    $GDB --batch-silent -nx -ex "set auto-load no" -ex "file $output_file" -ex "save gdb-index $output_dir"
732 +    rc=$?
733 +    [ $rc != 0 ] && exit $rc
734 +
735 +    # GDB might not always create an index.  Cope.
736 +    if [ -f "$index_file" ]
737 +    then
738 +       $OBJCOPY --add-section .gdb_index="$index_file" \
739 +           --set-section-flags .gdb_index=readonly \
740 +           "$output_file" "$output_file"
741 +       rc=$?
742 +    else
743 +       rc=0
744 +    fi
745 +    [ $rc != 0 ] && exit $rc
746 +fi
747 +
748 +if [ "$want_dwz" = true ]; then
749 +    $DWZ "$output_file" > /dev/null 2>&1
750 +elif [ "$want_multi" = true ]; then
751 +    cp $output_file ${output_file}.alt
752 +    $DWZ -m ${output_file}.dwz "$output_file" ${output_file}.alt > /dev/null 2>&1
753 +fi
754 +
755 +rm -f "$index_file"
756 +exit $rc
757 diff --git a/gdb/corelow.c b/gdb/corelow.c
758 index dd62560..340b149 100644
759 --- a/gdb/corelow.c
760 +++ b/gdb/corelow.c
761 @@ -46,6 +46,7 @@
762  #include "filenames.h"
763  #include "progspace.h"
764  #include "objfiles.h"
765 +#include "gdb_bfd.h"
766  
767  #ifndef O_LARGEFILE
768  #define O_LARGEFILE 0
769 @@ -215,9 +216,7 @@ core_close (int quitting)
770           core_data = NULL;
771         }
772  
773 -      name = bfd_get_filename (core_bfd);
774 -      gdb_bfd_close_or_warn (core_bfd);
775 -      xfree (name);
776 +      gdb_bfd_unref (core_bfd);
777        core_bfd = NULL;
778      }
779    core_vec = NULL;
780 @@ -319,9 +318,9 @@ core_open (char *filename, int from_tty)
781    if (scratch_chan < 0)
782      perror_with_name (filename);
783  
784 -  temp_bfd = bfd_fopen (filename, gnutarget, 
785 -                       write_files ? FOPEN_RUB : FOPEN_RB,
786 -                       scratch_chan);
787 +  temp_bfd = gdb_bfd_fopen (filename, gnutarget, 
788 +                           write_files ? FOPEN_RUB : FOPEN_RB,
789 +                           scratch_chan);
790    if (temp_bfd == NULL)
791      perror_with_name (filename);
792  
793 @@ -332,7 +331,7 @@ core_open (char *filename, int from_tty)
794        /* FIXME: should be checking for errors from bfd_close (for one
795           thing, on error it does not free all the storage associated
796           with the bfd).  */
797 -      make_cleanup_bfd_close (temp_bfd);
798 +      make_cleanup_bfd_unref (temp_bfd);
799        error (_("\"%s\" is not a core dump: %s"),
800              filename, bfd_errmsg (bfd_get_error ()));
801      }
802 @@ -340,7 +339,7 @@ core_open (char *filename, int from_tty)
803    /* Looks semi-reasonable.  Toss the old core file and work on the
804       new.  */
805  
806 -  discard_cleanups (old_chain);        /* Don't free filename any more */
807 +  do_cleanups (old_chain);
808    unpush_target (&core_ops);
809    core_bfd = temp_bfd;
810    old_chain = make_cleanup (core_close_cleanup, 0 /*ignore*/);
811 diff --git a/gdb/data-directory/Makefile.in b/gdb/data-directory/Makefile.in
812 index 87c6dd4..1c26652 100644
813 --- a/gdb/data-directory/Makefile.in
814 +++ b/gdb/data-directory/Makefile.in
815 @@ -52,14 +52,25 @@ SYSCALLS_FILES = \
816  PYTHON_DIR = python
817  PYTHON_INSTALL_DIR = $(DESTDIR)$(GDB_DATADIR)/$(PYTHON_DIR)
818  PYTHON_FILES = \
819 +       gdb/FrameIterator.py \
820 +       gdb/FrameWrapper.py \
821         gdb/__init__.py \
822 -       gdb/types.py \
823 -       gdb/printing.py \
824 -       gdb/prompt.py \
825 +       gdb/backtrace.py \
826         gdb/command/__init__.py \
827 +       gdb/command/backtrace.py \
828 +       gdb/command/ignore_errors.py \
829 +       gdb/command/pahole.py \
830         gdb/command/pretty_printers.py \
831         gdb/command/prompt.py \
832 -       gdb/command/explore.py
833 +       gdb/command/explore.py \
834 +       gdb/command/require.py \
835 +       gdb/command/upto.py \
836 +       gdb/function/__init__.py \
837 +       gdb/function/caller_is.py \
838 +       gdb/function/in_scope.py \
839 +       gdb/printing.py \
840 +       gdb/prompt.py \
841 +       gdb/types.py
842  
843  FLAGS_TO_PASS = \
844         "prefix=$(prefix)" \
845 diff --git a/gdb/defs.h b/gdb/defs.h
846 index 1c6fa79..ec08348 100644
847 --- a/gdb/defs.h
848 +++ b/gdb/defs.h
849 @@ -315,7 +315,7 @@ extern struct cleanup *make_cleanup_close (int fd);
850  
851  extern struct cleanup *make_cleanup_fclose (FILE *file);
852  
853 -extern struct cleanup *make_cleanup_bfd_close (bfd *abfd);
854 +extern struct cleanup *make_cleanup_bfd_unref (bfd *abfd);
855  
856  struct obstack;
857  extern struct cleanup *make_cleanup_obstack_free (struct obstack *obstack);
858 @@ -353,6 +353,8 @@ extern struct cleanup *make_cleanup_restore_page_info (void);
859  extern struct cleanup *
860    set_batch_flag_and_make_cleanup_restore_page_info (void);
861  
862 +extern struct cleanup *make_cleanup_restore_selected_frame (void);
863 +
864  extern char *gdb_realpath (const char *);
865  extern char *xfullpath (const char *);
866  
867 diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
868 index 68ea817..5945bac 100644
869 --- a/gdb/doc/gdb.texinfo
870 +++ b/gdb/doc/gdb.texinfo
871 @@ -1200,6 +1200,16 @@ for remote debugging.
872  Run using @var{device} for your program's standard input and output.
873  @c FIXME: kingdon thinks there is more to -tty.  Investigate.
874  
875 +@item -P
876 +@cindex @code{-P}
877 +@itemx --python
878 +@cindex @code{--python}
879 +Change interpretation of command line so that the argument immediately
880 +following this switch is taken to be the name of a Python script file.
881 +This option stops option processing; subsequent options are passed to
882 +Python as @code{sys.argv}.  This option is only available if Python
883 +scripting support was enabled when @value{GDBN} was configured.
884 +
885  @c resolve the situation of these eventually
886  @item -tui
887  @cindex @code{--tui}
888 @@ -22563,8 +22573,6 @@ containing @code{end}.  For example:
889  
890  @smallexample
891  (@value{GDBP}) python
892 -Type python script
893 -End with a line saying just "end".
894  >print 23
895  >end
896  23
897 @@ -22578,6 +22586,14 @@ controlled using @code{set python print-stack}: if @code{full}, then
898  full Python stack printing is enabled; if @code{none}, then Python stack
899  and message printing is disabled; if @code{message}, the default, only
900  the message component of the error is printed.
901 +
902 +@kindex maint set python auto-load
903 +@item maint set python auto-load
904 +By default, @value{GDBN} will attempt to automatically load Python
905 +code when an object file is opened.  This can be controlled using
906 +@code{maint set python auto-load}: if @code{on}, the default, then
907 +Python auto-loading is enabled; if @code{off}, then Python
908 +auto-loading is disabled.
909  @end table
910  
911  It is also possible to execute a Python script from the @value{GDBN}
912 @@ -22599,6 +22615,14 @@ and thus is always available.
913  @cindex python api
914  @cindex programming in python
915  
916 +You can get quick online help for @value{GDBN}'s Python API by issuing
917 +the command @w{@kbd{python help (gdb)}}.
918 +
919 +Functions and methods which have two or more optional arguments allow
920 +them to be specified using keyword syntax.  This allows passing some
921 +optional arguments while skipping others.  Example:
922 +@w{@code{gdb.some_function ('foo', bar = 1, baz = 2)}}.
923 +
924  @cindex python stdout
925  @cindex python pagination
926  At startup, @value{GDBN} overrides Python's @code{sys.stdout} and
927 @@ -34536,6 +34560,11 @@ Shared library events.
928  
929  @end table
930  
931 +@kindex maint info bfds
932 +@item maint info bfds
933 +This prints information about each @code{bfd} object that is known to
934 +@value{GDBN}.  @xref{Top, , BFD, bfd, The Binary File Descriptor Library}.
935 +
936  @kindex set displaced-stepping
937  @kindex show displaced-stepping
938  @cindex displaced stepping support
939 diff --git a/gdb/doc/gdbint.texinfo b/gdb/doc/gdbint.texinfo
940 index 5e00f1f..c5c432b 100644
941 --- a/gdb/doc/gdbint.texinfo
942 +++ b/gdb/doc/gdbint.texinfo
943 @@ -2102,6 +2102,18 @@ time, and so we attempt to handle symbols incrementally.  For instance,
944  we create @dfn{partial symbol tables} consisting of only selected
945  symbols, and only expand them to full symbol tables when necessary.
946  
947 +@menu
948 +* Symbol Reading::
949 +* Partial Symbol Tables::
950 +* Types::
951 +* Object File Formats::
952 +* Debugging File Formats::
953 +* Adding a New Symbol Reader to GDB::
954 +* Memory Management for Symbol Files::
955 +* Memory Management for Types::
956 +@end menu
957 +
958 +@node Symbol Reading
959  @section Symbol Reading
960  
961  @cindex symbol reading
962 @@ -2194,6 +2206,7 @@ symtab.  Upon return, @code{pst->readin} should have been set to 1, and
963  zero if there were no symbols in that part of the symbol file.
964  @end table
965  
966 +@node Partial Symbol Tables
967  @section Partial Symbol Tables
968  
969  @value{GDBN} has three types of symbol tables:
970 @@ -2295,6 +2308,7 @@ and partial symbol tables behind a set of function pointers known as
971  the @dfn{quick symbol functions}.  These are documented in
972  @file{symfile.h}.
973  
974 +@node Types
975  @section Types
976  
977  @unnumberedsubsec Fundamental Types (e.g., @code{FT_VOID}, @code{FT_BOOLEAN}).
978 @@ -2317,6 +2331,7 @@ types map to one @code{TYPE_CODE_*} type, and are distinguished by
979  other members of the type struct, such as whether the type is signed
980  or unsigned, and how many bits it uses.
981  
982 +@anchor{Builtin Types}
983  @unnumberedsubsec Builtin Types (e.g., @code{builtin_type_void}, @code{builtin_type_char}).
984  
985  These are instances of type structs that roughly correspond to
986 @@ -2331,6 +2346,7 @@ only one instance exists, while @file{c-lang.c} builds as many
987  @code{TYPE_CODE_INT} types as needed, with each one associated with
988  some particular objfile.
989  
990 +@node Object File Formats
991  @section Object File Formats
992  @cindex object file formats
993  
994 @@ -2416,6 +2432,7 @@ SOM, which is a cross-language ABI).
995  
996  The SOM reader is in @file{somread.c}.
997  
998 +@node Debugging File Formats
999  @section Debugging File Formats
1000  
1001  This section describes characteristics of debugging information that
1002 @@ -2487,6 +2504,7 @@ DWARF 3 is an improved version of DWARF 2.
1003  @cindex SOM debugging info
1004  Like COFF, the SOM definition includes debugging information.
1005  
1006 +@node Adding a New Symbol Reader to GDB
1007  @section Adding a New Symbol Reader to @value{GDBN}
1008  
1009  @cindex adding debugging info reader
1010 @@ -2509,6 +2527,7 @@ will only ever be implemented by one object file format may be called
1011  directly.  This interface should be described in a file
1012  @file{bfd/lib@var{xyz}.h}, which is included by @value{GDBN}.
1013  
1014 +@node Memory Management for Symbol Files
1015  @section Memory Management for Symbol Files
1016  
1017  Most memory associated with a loaded symbol file is stored on
1018 @@ -2520,10 +2539,45 @@ released when the objfile is unloaded or reloaded.  Therefore one
1019  objfile must not reference symbol or type data from another objfile;
1020  they could be unloaded at different times.
1021  
1022 -User convenience variables, et cetera, have associated types.  Normally
1023 -these types live in the associated objfile.  However, when the objfile
1024 -is unloaded, those types are deep copied to global memory, so that
1025 -the values of the user variables and history items are not lost.
1026 +@node Memory Management for Types
1027 +@section Memory Management for Types
1028 +@cindex memory management for types
1029 +
1030 +@findex TYPE_OBJFILE
1031 +@code{TYPE_OBJFILE} macro indicates the current memory owner of the type.
1032 +Non-@code{NULL} value indicates it is owned by an objfile (specifically by its
1033 +obstack) and in such case the type remains valid till the objfile is unloaded
1034 +or reloaded.  For such types with an associated objfile no reference counting
1035 +is being made.
1036 +
1037 +User convenience variables, et cetera, have associated types.  Normally these
1038 +types live in the associated objfile.  However, when the objfile is unloaded,
1039 +those types are deep copied to global memory, so that the values of the user
1040 +variables and history items are not lost.  During the copy they will get their
1041 +@code{TYPE_OBJFILE} set to @code{NULL} and become so-called @dfn{reclaimable}
1042 +types.
1043 +
1044 +Types with null @code{TYPE_OBJFILE} can be either permanent types
1045 +(@pxref{Builtin Types}) or reclaimable types which will be deallocated at the
1046 +first idle @value{GDBN} moment if the last object referencing them is removed.
1047 +Permanent types are allocated by the function @code{alloc_type} (and its
1048 +derivations like @code{init_type}) specifying objfile as @code{NULL}.  The
1049 +reclaimable types are created the same way but moreover they need to have
1050 +@code{type_init_group} called to start their tracking as being possibly
1051 +deallocatable.
1052 +
1053 +@findex free_all_types
1054 +When @value{GDBN} gets idle it always calls the @code{free_all_types} function
1055 +which deallocates any unused types.  All types currently not owned by an
1056 +objfile must be marked as used on each @code{free_all_types} call as they would
1057 +get deallocated as unused otherwise.
1058 +
1059 +@code{free_all_types} automatically checks for any cross-type references such
1060 +as through @code{TYPE_TARGET_TYPE}, @code{TYPE_POINTER_TYPE} etc.@: and
1061 +prevents early deallocation for any such existing references.  Reclaimable
1062 +types may reference any other reclaimable types or even permanent types.  But
1063 +permanent types must not reference reclaimable types (nor an objfile associated
1064 +type).
1065  
1066  
1067  @node Language Support
1068 diff --git a/gdb/doc/observer.texi b/gdb/doc/observer.texi
1069 index 6827ed8..c63b901 100644
1070 --- a/gdb/doc/observer.texi
1071 +++ b/gdb/doc/observer.texi
1072 @@ -230,6 +230,11 @@ the current top-level prompt.
1073  Variable gdb_datadir has been set.  The value may not necessarily change.
1074  @end deftypefun
1075  
1076 +@c @deftypefun void mark_used (void)
1077 +@c Mark any possibly reclaimable objects as used during a mark-and-sweep garbage
1078 +@c collector pass.  Currently only @code{type_mark_used} marker is supported.
1079 +@c @end deftypefun
1080 +
1081  @deftypefun void test_notification (int @var{somearg})
1082  This observer is used for internal testing.  Do not use.  
1083  See testsuite/gdb.gdb/observer.exp.
1084 diff --git a/gdb/dsrec.c b/gdb/dsrec.c
1085 index d2c99b2..f39d0ed 100644
1086 --- a/gdb/dsrec.c
1087 +++ b/gdb/dsrec.c
1088 @@ -23,6 +23,7 @@
1089  #include <time.h>
1090  #include "gdb_assert.h"
1091  #include "gdb_string.h"
1092 +#include "gdb_bfd.h"
1093  
1094  extern void report_transfer_performance (unsigned long, time_t, time_t);
1095  
1096 @@ -56,19 +57,22 @@ load_srec (struct serial *desc, const char *file, bfd_vma load_offset,
1097    int reclen;
1098    time_t start_time, end_time;
1099    unsigned long data_count = 0;
1100 +  struct cleanup *cleanup;
1101  
1102    srec = (char *) alloca (maxrecsize + 1);
1103  
1104 -  abfd = bfd_openr (file, 0);
1105 +  abfd = gdb_bfd_openr (file, 0);
1106    if (!abfd)
1107      {
1108        printf_filtered (_("Unable to open file %s\n"), file);
1109        return;
1110      }
1111  
1112 +  cleanup = make_cleanup_bfd_unref (abfd);
1113    if (bfd_check_format (abfd, bfd_object) == 0)
1114      {
1115        printf_filtered (_("File is not an object file\n"));
1116 +      do_cleanups (cleanup);
1117        return;
1118      }
1119  
1120 @@ -170,6 +174,7 @@ load_srec (struct serial *desc, const char *file, bfd_vma load_offset,
1121    serial_flush_input (desc);
1122  
1123    report_transfer_performance (data_count, start_time, end_time);
1124 +  do_cleanups (cleanup);
1125  }
1126  
1127  /*
1128 diff --git a/gdb/dwarf2expr.c b/gdb/dwarf2expr.c
1129 index 214b371..d9b3751 100644
1130 --- a/gdb/dwarf2expr.c
1131 +++ b/gdb/dwarf2expr.c
1132 @@ -1480,6 +1480,14 @@ execute_stack_op (struct dwarf_expr_context *ctx,
1133           }
1134           break;
1135  
1136 +       case DW_OP_push_object_address:
1137 +         if (ctx->funcs->get_object_address == NULL)
1138 +           error (_("DWARF-2 expression error: DW_OP_push_object_address must "
1139 +                  "have a value to push."));
1140 +         result = (ctx->funcs->get_object_address) (ctx->baton);
1141 +         result_val = value_from_ulongest (address_type, result);
1142 +         break;
1143 +
1144         default:
1145           error (_("Unhandled dwarf expression opcode 0x%x"), op);
1146         }
1147 diff --git a/gdb/dwarf2expr.h b/gdb/dwarf2expr.h
1148 index 19efbfd..0e0e498 100644
1149 --- a/gdb/dwarf2expr.h
1150 +++ b/gdb/dwarf2expr.h
1151 @@ -78,12 +78,8 @@ struct dwarf_expr_context_funcs
1152       This can throw an exception if the index is out of range.  */
1153    CORE_ADDR (*get_addr_index) (void *baton, unsigned int index);
1154  
1155 -#if 0
1156 -  /* Not yet implemented.  */
1157 -
1158    /* Return the `object address' for DW_OP_push_object_address.  */
1159    CORE_ADDR (*get_object_address) (void *baton);
1160 -#endif
1161  };
1162  
1163  /* The location of a value.  */
1164 diff --git a/gdb/dwarf2loc.c b/gdb/dwarf2loc.c
1165 index 38e4814..46a033d 100644
1166 --- a/gdb/dwarf2loc.c
1167 +++ b/gdb/dwarf2loc.c
1168 @@ -293,6 +293,9 @@ struct dwarf_expr_baton
1169  {
1170    struct frame_info *frame;
1171    struct dwarf2_per_cu_data *per_cu;
1172 +  /* From DW_TAG_variable's DW_AT_location (not DW_TAG_type's
1173 +     DW_AT_data_location) for DW_OP_push_object_address.  */
1174 +  CORE_ADDR object_address;
1175  };
1176  
1177  /* Helper functions for dwarf2_evaluate_loc_desc.  */
1178 @@ -352,16 +355,14 @@ static void
1179  dwarf_expr_frame_base_1 (struct symbol *framefunc, CORE_ADDR pc,
1180                          const gdb_byte **start, size_t *length)
1181  {
1182 -  if (SYMBOL_LOCATION_BATON (framefunc) == NULL)
1183 -    *length = 0;
1184 -  else if (SYMBOL_COMPUTED_OPS (framefunc) == &dwarf2_loclist_funcs)
1185 +  if (SYMBOL_COMPUTED_OPS (framefunc) == &dwarf2_loclist_funcs)
1186      {
1187        struct dwarf2_loclist_baton *symbaton;
1188  
1189        symbaton = SYMBOL_LOCATION_BATON (framefunc);
1190        *start = dwarf2_find_location_expression (symbaton, length, pc);
1191      }
1192 -  else
1193 +  else if (SYMBOL_COMPUTED_OPS (framefunc) == &dwarf2_locexpr_funcs)
1194      {
1195        struct dwarf2_locexpr_baton *symbaton;
1196  
1197 @@ -374,10 +375,23 @@ dwarf_expr_frame_base_1 (struct symbol *framefunc, CORE_ADDR pc,
1198        else
1199         *length = 0;
1200      }
1201 +  else if (SYMBOL_COMPUTED_OPS (framefunc) == &dwarf2_missing_funcs)
1202 +    {
1203 +      struct dwarf2_locexpr_baton *symbaton;
1204 +
1205 +      symbaton = SYMBOL_LOCATION_BATON (framefunc);
1206 +      gdb_assert (symbaton == NULL);
1207 +      *length = 0;
1208 +    }
1209 +  else
1210 +    internal_error (__FILE__, __LINE__,
1211 +                   _("Unsupported SYMBOL_COMPUTED_OPS %p for \"%s\""),
1212 +                   SYMBOL_COMPUTED_OPS (framefunc),
1213 +                   SYMBOL_PRINT_NAME (framefunc));
1214  
1215    if (*length == 0)
1216      error (_("Could not find the frame base for \"%s\"."),
1217 -          SYMBOL_NATURAL_NAME (framefunc));
1218 +          SYMBOL_PRINT_NAME (framefunc));
1219  }
1220  
1221  /* Helper function for dwarf2_evaluate_loc_desc.  Computes the CFA for
1222 @@ -445,6 +459,85 @@ dwarf_expr_dwarf_call (struct dwarf_expr_context *ctx, cu_offset die_offset)
1223                      ctx->funcs->get_frame_pc, ctx->baton);
1224  }
1225  
1226 +static CORE_ADDR
1227 +dwarf_expr_object_address (void *baton)
1228 +{
1229 +  struct dwarf_expr_baton *debaton = baton;
1230 +
1231 +  /* The message is suppressed in DWARF_BLOCK_EXEC.  */
1232 +  if (debaton->object_address == 0)
1233 +    error (_("Cannot resolve DW_OP_push_object_address for a missing object"));
1234 +
1235 +  return debaton->object_address;
1236 +}
1237 +
1238 +/* Address of the variable we are currently referring to.  It is set from
1239 +   DW_TAG_variable's DW_AT_location (not DW_TAG_type's DW_AT_data_location) for
1240 +   DW_OP_push_object_address.  */
1241 +
1242 +static CORE_ADDR object_address;
1243 +
1244 +/* Callers use object_address_set while their callers use the result set so we
1245 +   cannot run the cleanup at the local block of our direct caller.  Still we
1246 +   should reset OBJECT_ADDRESS at least for the next GDB command.  */
1247 +
1248 +static void
1249 +object_address_cleanup (void *prev_save_voidp)
1250 +{
1251 +  CORE_ADDR *prev_save = prev_save_voidp;
1252 +
1253 +  object_address = *prev_save;
1254 +  xfree (prev_save);
1255 +}
1256 +
1257 +/* Set the base address - DW_AT_location - of a variable.  It is being later
1258 +   used to derive other object addresses by DW_OP_push_object_address.
1259 +
1260 +   It would be useful to sanity check ADDRESS - such as for some objects with
1261 +   unset value_raw_address - but some valid addresses may be zero (such as first
1262 +   objects in relocatable .o files).  */
1263 +
1264 +void
1265 +object_address_set (CORE_ADDR address)
1266 +{
1267 +  CORE_ADDR *prev_save;
1268 +
1269 +  prev_save = xmalloc (sizeof *prev_save);
1270 +  *prev_save = object_address;
1271 +  make_cleanup (object_address_cleanup, prev_save);
1272 +
1273 +  object_address = address;
1274 +}
1275 +
1276 +/* Evaluate DWARF location list at DLLBATON expecting it produces exactly one
1277 +   CORE_ADDR result stored to *ADDRP on the DWARF stack stack.  If the result
1278 +   could not be found return zero and keep *ADDRP unchanged.  */
1279 +
1280 +int
1281 +dwarf_loclist_baton_eval (struct dwarf2_loclist_baton *dllbaton,
1282 +                         struct type *type, CORE_ADDR *addrp)
1283 +{
1284 +  struct frame_info *frame = get_selected_frame (NULL);
1285 +  const gdb_byte *data;
1286 +  size_t size;
1287 +  struct value *val;
1288 +
1289 +  if (!dllbaton)
1290 +    return 0;
1291 +
1292 +  data = dwarf2_find_location_expression (dllbaton, &size,
1293 +                                         get_frame_address_in_block (frame));
1294 +  if (data == NULL)
1295 +    return 0;
1296 +
1297 +  val = dwarf2_evaluate_loc_desc (type, frame, data, size, dllbaton->per_cu);
1298 +  if (value_optimized_out (val))
1299 +    return 0;
1300 +
1301 +  *addrp = value_as_address (val);
1302 +  return 1;
1303 +}
1304 +
1305  /* Callback function for dwarf2_evaluate_loc_desc.  */
1306  
1307  static struct type *
1308 @@ -1139,10 +1232,12 @@ dwarf_expr_push_dwarf_reg_entry_value (struct dwarf_expr_context *ctx,
1309  
1310    saved_ctx.gdbarch = ctx->gdbarch;
1311    saved_ctx.addr_size = ctx->addr_size;
1312 +  saved_ctx.ref_addr_size = ctx->ref_addr_size;
1313    saved_ctx.offset = ctx->offset;
1314    saved_ctx.baton = ctx->baton;
1315    ctx->gdbarch = get_objfile_arch (dwarf2_per_cu_objfile (baton_local.per_cu));
1316    ctx->addr_size = dwarf2_per_cu_addr_size (baton_local.per_cu);
1317 +  ctx->ref_addr_size = dwarf2_per_cu_ref_addr_size (baton_local.per_cu);
1318    ctx->offset = dwarf2_per_cu_text_offset (baton_local.per_cu);
1319    ctx->baton = &baton_local;
1320  
1321 @@ -1150,10 +1245,95 @@ dwarf_expr_push_dwarf_reg_entry_value (struct dwarf_expr_context *ctx,
1322  
1323    ctx->gdbarch = saved_ctx.gdbarch;
1324    ctx->addr_size = saved_ctx.addr_size;
1325 +  ctx->ref_addr_size = saved_ctx.ref_addr_size;
1326    ctx->offset = saved_ctx.offset;
1327    ctx->baton = saved_ctx.baton;
1328  }
1329  
1330 +static CORE_ADDR dwarf_expr_get_addr_index (void *baton, unsigned int index);
1331 +
1332 +/* Virtual method table for dwarf2_evaluate_loc_desc_full below.  */
1333 +
1334 +static const struct dwarf_expr_context_funcs dwarf_expr_ctx_funcs =
1335 +{
1336 +  dwarf_expr_read_reg,
1337 +  dwarf_expr_read_mem,
1338 +  dwarf_expr_frame_base,
1339 +  dwarf_expr_frame_cfa,
1340 +  dwarf_expr_frame_pc,
1341 +  dwarf_expr_tls_address,
1342 +  dwarf_expr_dwarf_call,
1343 +  dwarf_expr_get_base_type,
1344 +  dwarf_expr_push_dwarf_reg_entry_value,
1345 +  dwarf_expr_get_addr_index,
1346 +  dwarf_expr_object_address
1347 +};
1348 +
1349 +/* Evaluate DWARF expression at DATA ... DATA + SIZE with its result readable
1350 +   by dwarf_expr_fetch (RETVAL, 0).  FRAME parameter can be NULL to call
1351 +   get_selected_frame to find it.  Returned dwarf_expr_context freeing is
1352 +   pushed on the cleanup chain.  */
1353 +
1354 +static void
1355 +dwarf_expr_prep_ctx (struct dwarf_expr_context *ctx, struct frame_info *frame,
1356 +                    const gdb_byte *data, size_t size,
1357 +                    struct dwarf2_per_cu_data *per_cu)
1358 +{
1359 +  struct dwarf_expr_baton baton;
1360 +  struct objfile *objfile = dwarf2_per_cu_objfile (per_cu);
1361 +  volatile struct gdb_exception ex;
1362 +
1363 +  baton.frame = frame;
1364 +  baton.per_cu = per_cu;
1365 +  baton.object_address = object_address;
1366 +
1367 +  ctx->gdbarch = get_objfile_arch (objfile);
1368 +  ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
1369 +  ctx->ref_addr_size = dwarf2_per_cu_ref_addr_size (per_cu);
1370 +  ctx->offset = dwarf2_per_cu_text_offset (per_cu);
1371 +  ctx->baton = &baton;
1372 +  ctx->funcs = &dwarf_expr_ctx_funcs;
1373 +
1374 +  dwarf_expr_eval (ctx, data, size);
1375 +}
1376 +
1377 +/* Evaluate DWARF expression at DLBATON expecting it produces exactly one
1378 +   CORE_ADDR result on the DWARF stack stack.  */
1379 +
1380 +CORE_ADDR
1381 +dwarf_locexpr_baton_eval (struct dwarf2_locexpr_baton *dlbaton)
1382 +{
1383 +  struct dwarf_expr_context *ctx;
1384 +  CORE_ADDR retval;
1385 +  struct cleanup *back_to;
1386 +
1387 +  ctx = new_dwarf_expr_context ();
1388 +  back_to = make_cleanup_free_dwarf_expr_context (ctx);
1389 +
1390 +  dwarf_expr_prep_ctx (ctx, get_selected_frame (NULL), dlbaton->data,
1391 +                      dlbaton->size, dlbaton->per_cu);
1392 +
1393 +  if (ctx->num_pieces > 0)
1394 +    error (_("DW_OP_*piece is unsupported for DW_FORM_block"));
1395 +
1396 +  retval = dwarf_expr_fetch_address (ctx, 0);
1397 +
1398 +  if (ctx->location == DWARF_VALUE_REGISTER)
1399 +    {
1400 +      /* Inlined dwarf_expr_read_reg as we no longer have the baton. */
1401 +
1402 +      int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (ctx->gdbarch, retval);
1403 +      struct type *type = builtin_type (ctx->gdbarch)->builtin_data_ptr;
1404 +      struct frame_info *frame = get_selected_frame (NULL);
1405 +
1406 +      retval = address_from_register (type, gdb_regnum, frame);
1407 +    }
1408 +
1409 +  do_cleanups (back_to);
1410 +
1411 +  return retval;
1412 +}
1413 +
1414  /* Callback function for dwarf2_evaluate_loc_desc.
1415     Fetch the address indexed by DW_OP_GNU_addr_index.  */
1416  
1417 @@ -2088,22 +2268,6 @@ invalid_synthetic_pointer (void)
1418            "referenced via synthetic pointer"));
1419  }
1420  
1421 -/* Virtual method table for dwarf2_evaluate_loc_desc_full below.  */
1422 -
1423 -static const struct dwarf_expr_context_funcs dwarf_expr_ctx_funcs =
1424 -{
1425 -  dwarf_expr_read_reg,
1426 -  dwarf_expr_read_mem,
1427 -  dwarf_expr_frame_base,
1428 -  dwarf_expr_frame_cfa,
1429 -  dwarf_expr_frame_pc,
1430 -  dwarf_expr_tls_address,
1431 -  dwarf_expr_dwarf_call,
1432 -  dwarf_expr_get_base_type,
1433 -  dwarf_expr_push_dwarf_reg_entry_value,
1434 -  dwarf_expr_get_addr_index
1435 -};
1436 -
1437  /* Evaluate a location description, starting at DATA and with length
1438     SIZE, to find the current location of variable of TYPE in the
1439     context of FRAME.  BYTE_OFFSET is applied after the contents are
1440 @@ -2116,7 +2280,6 @@ dwarf2_evaluate_loc_desc_full (struct type *type, struct frame_info *frame,
1441                                LONGEST byte_offset)
1442  {
1443    struct value *retval;
1444 -  struct dwarf_expr_baton baton;
1445    struct dwarf_expr_context *ctx;
1446    struct cleanup *old_chain, *value_chain;
1447    struct objfile *objfile = dwarf2_per_cu_objfile (per_cu);
1448 @@ -2128,29 +2291,18 @@ dwarf2_evaluate_loc_desc_full (struct type *type, struct frame_info *frame,
1449    if (size == 0)
1450      return allocate_optimized_out_value (type);
1451  
1452 -  baton.frame = frame;
1453 -  baton.per_cu = per_cu;
1454 -
1455    ctx = new_dwarf_expr_context ();
1456    old_chain = make_cleanup_free_dwarf_expr_context (ctx);
1457    value_chain = make_cleanup_value_free_to_mark (value_mark ());
1458  
1459 -  ctx->gdbarch = get_objfile_arch (objfile);
1460 -  ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
1461 -  ctx->ref_addr_size = dwarf2_per_cu_ref_addr_size (per_cu);
1462 -  ctx->offset = dwarf2_per_cu_text_offset (per_cu);
1463 -  ctx->baton = &baton;
1464 -  ctx->funcs = &dwarf_expr_ctx_funcs;
1465 -
1466    TRY_CATCH (ex, RETURN_MASK_ERROR)
1467      {
1468 -      dwarf_expr_eval (ctx, data, size);
1469 +      dwarf_expr_prep_ctx (ctx, frame, data, size, per_cu);
1470      }
1471    if (ex.reason < 0)
1472      {
1473        if (ex.error == NOT_AVAILABLE_ERROR)
1474         {
1475 -         do_cleanups (old_chain);
1476           retval = allocate_value (type);
1477           mark_value_bytes_unavailable (retval, 0, TYPE_LENGTH (type));
1478           return retval;
1479 @@ -2214,6 +2366,16 @@ dwarf2_evaluate_loc_desc_full (struct type *type, struct frame_info *frame,
1480             int in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 0);
1481  
1482             do_cleanups (value_chain);
1483 +
1484 +           /* Frame may be needed for check_typedef of TYPE_DYNAMIC.  */
1485 +           make_cleanup_restore_selected_frame ();
1486 +           select_frame (frame);
1487 +
1488 +           /* object_address_set called here is required in ALLOCATE_VALUE's
1489 +              CHECK_TYPEDEF for the object's possible
1490 +              DW_OP_push_object_address.  */
1491 +           object_address_set (address);
1492 +
1493             retval = allocate_value_lazy (type);
1494             VALUE_LVAL (retval) = lval_memory;
1495             if (in_stack_memory)
1496 @@ -4130,8 +4292,7 @@ loclist_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
1497                                dlbaton->per_cu);
1498  }
1499  
1500 -/* The set of location functions used with the DWARF-2 expression
1501 -   evaluator and location lists.  */
1502 +/* The set of location functions used with the DWARF-2 location lists.  */
1503  const struct symbol_computed_ops dwarf2_loclist_funcs = {
1504    loclist_read_variable,
1505    loclist_read_variable_at_entry,
1506 @@ -4140,6 +4301,48 @@ const struct symbol_computed_ops dwarf2_loclist_funcs = {
1507    loclist_tracepoint_var_ref
1508  };
1509  
1510 +static struct value *
1511 +missing_read_variable (struct symbol *symbol, struct frame_info *frame)
1512 +{
1513 +  struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1514 +
1515 +  gdb_assert (dlbaton == NULL);
1516 +  error (_("Unable to resolve variable \"%s\""), SYMBOL_PRINT_NAME (symbol));
1517 +}
1518 +
1519 +static int
1520 +missing_read_needs_frame (struct symbol *symbol)
1521 +{
1522 +  return 0;
1523 +}
1524 +
1525 +static void
1526 +missing_describe_location (struct symbol *symbol, CORE_ADDR addr,
1527 +                          struct ui_file *stream)
1528 +{
1529 +  fprintf_filtered (stream, _("a variable we are unable to resolve"));
1530 +}
1531 +
1532 +static void
1533 +missing_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
1534 +                           struct agent_expr *ax, struct axs_value *value)
1535 +{
1536 +  struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1537 +
1538 +  gdb_assert (dlbaton == NULL);
1539 +  error (_("Unable to resolve variable \"%s\""), SYMBOL_PRINT_NAME (symbol));
1540 +}
1541 +
1542 +/* The set of location functions used with the DWARF-2 evaluator when we are
1543 +   unable to resolve the symbols.  */
1544 +const struct symbol_computed_ops dwarf2_missing_funcs = {
1545 +  missing_read_variable,
1546 +  missing_read_variable,       /* read_variable_at_entry */
1547 +  missing_read_needs_frame,
1548 +  missing_describe_location,
1549 +  missing_tracepoint_var_ref
1550 +};
1551 +
1552  /* Provide a prototype to silence -Wmissing-prototypes.  */
1553  extern initialize_file_ftype _initialize_dwarf2loc;
1554  
1555 diff --git a/gdb/dwarf2loc.h b/gdb/dwarf2loc.h
1556 index e9d06a3..bfa6776 100644
1557 --- a/gdb/dwarf2loc.h
1558 +++ b/gdb/dwarf2loc.h
1559 @@ -127,6 +127,15 @@ struct dwarf2_loclist_baton
1560  
1561  extern const struct symbol_computed_ops dwarf2_locexpr_funcs;
1562  extern const struct symbol_computed_ops dwarf2_loclist_funcs;
1563 +extern const struct symbol_computed_ops dwarf2_missing_funcs;
1564 +
1565 +extern void object_address_set (CORE_ADDR address);
1566 +
1567 +extern CORE_ADDR dwarf_locexpr_baton_eval
1568 +  (struct dwarf2_locexpr_baton *dlbaton);
1569 +
1570 +extern int dwarf_loclist_baton_eval (struct dwarf2_loclist_baton *dllbaton,
1571 +                                    struct type *type, CORE_ADDR *addrp);
1572  
1573  /* Compile a DWARF location expression to an agent expression.
1574     
1575 diff --git a/gdb/dwarf2read.c b/gdb/dwarf2read.c
1576 index deee5a2..b2f052b 100644
1577 --- a/gdb/dwarf2read.c
1578 +++ b/gdb/dwarf2read.c
1579 @@ -64,20 +64,12 @@
1580  #include "gdbcore.h" /* for gnutarget */
1581  #include "gdb/gdb-index.h"
1582  #include <ctype.h>
1583 +#include "gdb_bfd.h"
1584  
1585  #include <fcntl.h>
1586  #include "gdb_string.h"
1587  #include "gdb_assert.h"
1588  #include <sys/types.h>
1589 -#ifdef HAVE_ZLIB_H
1590 -#include <zlib.h>
1591 -#endif
1592 -#ifdef HAVE_MMAP
1593 -#include <sys/mman.h>
1594 -#ifndef MAP_FAILED
1595 -#define MAP_FAILED ((void *) -1)
1596 -#endif
1597 -#endif
1598  
1599  typedef struct symbol *symbolp;
1600  DEF_VEC_P (symbolp);
1601 @@ -95,8 +87,6 @@ static int check_physname = 0;
1602  /* When non-zero, do not reject deprecated .gdb_index sections.  */
1603  static int use_deprecated_index_sections = 0;
1604  
1605 -static int pagesize;
1606 -
1607  /* When set, the file that we're processing is known to have debugging
1608     info for C++ namespaces.  GCC 3.3.x did not produce this information,
1609     but later versions do.  */
1610 @@ -110,10 +100,6 @@ struct dwarf2_section_info
1611    asection *asection;
1612    gdb_byte *buffer;
1613    bfd_size_type size;
1614 -  /* Not NULL if the section was actually mmapped.  */
1615 -  void *map_addr;
1616 -  /* Page aligned size of mmapped area.  */
1617 -  bfd_size_type map_len;
1618    /* True if we have tried to read this section.  */
1619    int readin;
1620  };
1621 @@ -247,6 +233,10 @@ struct dwarf2_per_objfile
1622       This is NULL if the table hasn't been allocated yet.  */
1623    htab_t dwo_files;
1624  
1625 +  /* The shared '.dwz' file, if one exists.  This is used when the
1626 +     original data was compressed using 'dwz -m'.  */
1627 +  struct dwz_file *dwz_file;
1628 +
1629    /* A flag indicating wether this objfile has a section loaded at a
1630       VMA of 0.  */
1631    int has_section_at_zero;
1632 @@ -505,15 +495,13 @@ struct dwarf2_cu
1633  
1634  struct dwarf2_per_cu_data
1635  {
1636 -  /* The start offset and length of this compilation unit.  2**29-1
1637 -     bytes should suffice to store the length of any compilation unit
1638 -     - if it doesn't, GDB will fall over anyway.
1639 +  /* The start offset and length of this compilation unit.
1640       NOTE: Unlike comp_unit_head.length, this length includes
1641       initial_length_size.
1642       If the DIE refers to a DWO file, this is always of the original die,
1643       not the DWO file.  */
1644    sect_offset offset;
1645 -  unsigned int length : 29;
1646 +  unsigned int length;
1647  
1648    /* Flag indicating this compilation unit will be read in before
1649       any of the current compilation units are processed.  */
1650 @@ -528,6 +516,9 @@ struct dwarf2_per_cu_data
1651    /* Non-zero if this CU is from .debug_types.  */
1652    unsigned int is_debug_types : 1;
1653  
1654 +  /* Non-zero if this CU is from the .dwz file.  */
1655 +  unsigned int is_dwz : 1;
1656 +
1657    /* The section this CU/TU lives in.
1658       If the DIE refers to a DWO file, this is always the original die,
1659       not the DWO file.  */
1660 @@ -715,6 +706,22 @@ struct dwo_file
1661    htab_t tus;
1662  };
1663  
1664 +/* This represents a '.dwz' file.  */
1665 +
1666 +struct dwz_file
1667 +{
1668 +  /* A dwz file can only contain a few sections.  */
1669 +  struct dwarf2_section_info abbrev;
1670 +  struct dwarf2_section_info info;
1671 +  struct dwarf2_section_info str;
1672 +  struct dwarf2_section_info line;
1673 +  struct dwarf2_section_info macro;
1674 +  struct dwarf2_section_info gdb_index;
1675 +
1676 +  /* The dwz's BFD.  */
1677 +  bfd *dwz_bfd;
1678 +};
1679 +
1680  /* Struct used to pass misc. parameters to read_die_and_children, et
1681     al.  which are used for both .debug_info and .debug_types dies.
1682     All parameters here are unchanging for the life of the call.  This
1683 @@ -828,6 +835,12 @@ struct partial_die_info
1684      /* Flag set if fixup_partial_die has been called on this die.  */
1685      unsigned int fixup_called : 1;
1686  
1687 +    /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt.  */
1688 +    unsigned int is_dwz : 1;
1689 +
1690 +    /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt.  */
1691 +    unsigned int spec_is_dwz : 1;
1692 +
1693      /* The name of this DIE.  Normally the value of DW_AT_name, but
1694         sometimes a default name for unnamed DIEs.  */
1695      char *name;
1696 @@ -1213,7 +1226,7 @@ static gdb_byte *read_partial_die (const struct die_reader_specs *,
1697                                    unsigned int,
1698                                    gdb_byte *);
1699  
1700 -static struct partial_die_info *find_partial_die (sect_offset,
1701 +static struct partial_die_info *find_partial_die (sect_offset, int,
1702                                                   struct dwarf2_cu *);
1703  
1704  static void fixup_partial_die (struct partial_die_info *,
1705 @@ -1258,6 +1271,8 @@ static char *read_indirect_string (bfd *, gdb_byte *,
1706                                     const struct comp_unit_head *,
1707                                     unsigned int *);
1708  
1709 +static char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1710 +
1711  static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
1712  
1713  static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
1714 @@ -1509,6 +1524,9 @@ static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1715                                    struct dwarf2_loclist_baton *baton,
1716                                    struct attribute *attr);
1717  
1718 +static struct dwarf2_loclist_baton *dwarf2_attr_to_loclist_baton
1719 +  (struct attribute *attr, struct dwarf2_cu *cu);
1720 +
1721  static void dwarf2_symbol_mark_computed (struct attribute *attr,
1722                                          struct symbol *sym,
1723                                          struct dwarf2_cu *cu);
1724 @@ -1524,7 +1542,7 @@ static hashval_t partial_die_hash (const void *item);
1725  static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1726  
1727  static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1728 -  (sect_offset offset, struct objfile *objfile);
1729 +  (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
1730  
1731  static void init_one_comp_unit (struct dwarf2_cu *cu,
1732                                 struct dwarf2_per_cu_data *per_cu);
1733 @@ -1541,6 +1559,9 @@ static void age_cached_comp_units (void);
1734  
1735  static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1736  
1737 +static void fetch_die_type_attrs (struct die_info *die, struct type *type,
1738 +                                 struct dwarf2_cu *cu);
1739 +
1740  static struct type *set_die_type (struct die_info *, struct type *,
1741                                   struct dwarf2_cu *);
1742  
1743 @@ -1569,6 +1590,9 @@ static struct type *get_die_type_at_offset (sect_offset,
1744  
1745  static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1746  
1747 +static struct dwarf2_locexpr_baton *dwarf2_attr_to_locexpr_baton
1748 +  (struct attribute *attr, struct dwarf2_cu *cu);
1749 +
1750  static void dwarf2_release_queue (void *dummy);
1751  
1752  static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1753 @@ -1587,6 +1611,12 @@ static void find_file_and_directory (struct die_info *die,
1754  static char *file_full_name (int file, struct line_header *lh,
1755                              const char *comp_dir);
1756  
1757 +static gdb_byte *read_and_check_comp_unit_head
1758 +  (struct comp_unit_head *header,
1759 +   struct dwarf2_section_info *section,
1760 +   struct dwarf2_section_info *abbrev_section, gdb_byte *info_ptr,
1761 +   int is_debug_types_section);
1762 +
1763  static void init_cutu_and_read_dies
1764    (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1765     int use_existing_cu, int keep,
1766 @@ -1608,8 +1638,6 @@ static struct dwo_unit *lookup_dwo_type_unit
1767  
1768  static void free_dwo_file_cleanup (void *);
1769  
1770 -static void munmap_section_buffer (struct dwarf2_section_info *);
1771 -
1772  static void process_cu_includes (void);
1773  
1774  #if WORDS_BIGENDIAN
1775 @@ -1779,85 +1807,6 @@ dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1776      dwarf2_per_objfile->has_section_at_zero = 1;
1777  }
1778  
1779 -/* Decompress a section that was compressed using zlib.  Store the
1780 -   decompressed buffer, and its size, in OUTBUF and OUTSIZE.  */
1781 -
1782 -static void
1783 -zlib_decompress_section (struct objfile *objfile, asection *sectp,
1784 -                         gdb_byte **outbuf, bfd_size_type *outsize)
1785 -{
1786 -  bfd *abfd = sectp->owner;
1787 -#ifndef HAVE_ZLIB_H
1788 -  error (_("Support for zlib-compressed DWARF data (from '%s') "
1789 -           "is disabled in this copy of GDB"),
1790 -         bfd_get_filename (abfd));
1791 -#else
1792 -  bfd_size_type compressed_size = bfd_get_section_size (sectp);
1793 -  gdb_byte *compressed_buffer = xmalloc (compressed_size);
1794 -  struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1795 -  bfd_size_type uncompressed_size;
1796 -  gdb_byte *uncompressed_buffer;
1797 -  z_stream strm;
1798 -  int rc;
1799 -  int header_size = 12;
1800 -
1801 -  if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1802 -      || bfd_bread (compressed_buffer,
1803 -                   compressed_size, abfd) != compressed_size)
1804 -    error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1805 -           bfd_get_filename (abfd));
1806 -
1807 -  /* Read the zlib header.  In this case, it should be "ZLIB" followed
1808 -     by the uncompressed section size, 8 bytes in big-endian order.  */
1809 -  if (compressed_size < header_size
1810 -      || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1811 -    error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1812 -           bfd_get_filename (abfd));
1813 -  uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1814 -  uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1815 -  uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1816 -  uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1817 -  uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1818 -  uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1819 -  uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1820 -  uncompressed_size += compressed_buffer[11];
1821 -
1822 -  /* It is possible the section consists of several compressed
1823 -     buffers concatenated together, so we uncompress in a loop.  */
1824 -  strm.zalloc = NULL;
1825 -  strm.zfree = NULL;
1826 -  strm.opaque = NULL;
1827 -  strm.avail_in = compressed_size - header_size;
1828 -  strm.next_in = (Bytef*) compressed_buffer + header_size;
1829 -  strm.avail_out = uncompressed_size;
1830 -  uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1831 -                                       uncompressed_size);
1832 -  rc = inflateInit (&strm);
1833 -  while (strm.avail_in > 0)
1834 -    {
1835 -      if (rc != Z_OK)
1836 -        error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1837 -               bfd_get_filename (abfd), rc);
1838 -      strm.next_out = ((Bytef*) uncompressed_buffer
1839 -                       + (uncompressed_size - strm.avail_out));
1840 -      rc = inflate (&strm, Z_FINISH);
1841 -      if (rc != Z_STREAM_END)
1842 -        error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1843 -               bfd_get_filename (abfd), rc);
1844 -      rc = inflateReset (&strm);
1845 -    }
1846 -  rc = inflateEnd (&strm);
1847 -  if (rc != Z_OK
1848 -      || strm.avail_out != 0)
1849 -    error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1850 -           bfd_get_filename (abfd), rc);
1851 -
1852 -  do_cleanups (cleanup);
1853 -  *outbuf = uncompressed_buffer;
1854 -  *outsize = uncompressed_size;
1855 -#endif
1856 -}
1857 -
1858  /* A helper function that decides whether a section is empty,
1859     or not present.  */
1860  
1861 @@ -1884,56 +1833,27 @@ dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1862    if (info->readin)
1863      return;
1864    info->buffer = NULL;
1865 -  info->map_addr = NULL;
1866    info->readin = 1;
1867  
1868    if (dwarf2_section_empty_p (info))
1869      return;
1870  
1871 -  /* Note that ABFD may not be from OBJFILE, e.g. a DWO section.  */
1872    abfd = sectp->owner;
1873  
1874 -  /* Check if the file has a 4-byte header indicating compression.  */
1875 -  if (info->size > sizeof (header)
1876 -      && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1877 -      && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1878 -    {
1879 -      /* Upon decompression, update the buffer and its size.  */
1880 -      if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1881 -        {
1882 -          zlib_decompress_section (objfile, sectp, &info->buffer,
1883 -                                  &info->size);
1884 -          return;
1885 -        }
1886 -    }
1887 -
1888 -#ifdef HAVE_MMAP
1889 -  if (pagesize == 0)
1890 -    pagesize = getpagesize ();
1891 -
1892 -  /* Only try to mmap sections which are large enough: we don't want to
1893 -     waste space due to fragmentation.  Also, only try mmap for sections
1894 -     without relocations.  */
1895 -
1896 -  if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1897 +  /* If the section has relocations, we must read it ourselves.
1898 +     Otherwise we attach it to the BFD.  */
1899 +  if ((sectp->flags & SEC_RELOC) == 0)
1900      {
1901 -      info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1902 -                         MAP_PRIVATE, sectp->filepos,
1903 -                         &info->map_addr, &info->map_len);
1904 +      const gdb_byte *bytes = gdb_bfd_map_section (sectp, &info->size);
1905  
1906 -      if ((caddr_t)info->buffer != MAP_FAILED)
1907 -       {
1908 -#if HAVE_POSIX_MADVISE
1909 -         posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
1910 -#endif
1911 -         return;
1912 -       }
1913 +      /* We have to cast away const here for historical reasons.
1914 +        Fixing dwarf2read to be const-correct would be quite nice.  */
1915 +      info->buffer = (gdb_byte *) bytes;
1916 +      return;
1917      }
1918 -#endif
1919  
1920 -  /* If we get here, we are a normal, not-compressed section.  */
1921 -  info->buffer = buf
1922 -    = obstack_alloc (&objfile->objfile_obstack, info->size);
1923 +  buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1924 +  info->buffer = buf;
1925  
1926    /* When debugging .o files, we may need to apply relocations; see
1927       http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1928 @@ -2009,6 +1929,111 @@ dwarf2_get_section_info (struct objfile *objfile,
1929    *sizep = info->size;
1930  }
1931  
1932 +/* A helper function to find the sections for a .dwz file.  */
1933 +
1934 +static void
1935 +locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
1936 +{
1937 +  struct dwz_file *dwz_file = arg;
1938 +
1939 +  /* Note that we only support the standard ELF names, because .dwz
1940 +     is ELF-only (at the time of writing).  */
1941 +  if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
1942 +    {
1943 +      dwz_file->abbrev.asection = sectp;
1944 +      dwz_file->abbrev.size = bfd_get_section_size (sectp);
1945 +    }
1946 +  else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
1947 +    {
1948 +      dwz_file->info.asection = sectp;
1949 +      dwz_file->info.size = bfd_get_section_size (sectp);
1950 +    }
1951 +  else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
1952 +    {
1953 +      dwz_file->str.asection = sectp;
1954 +      dwz_file->str.size = bfd_get_section_size (sectp);
1955 +    }
1956 +  else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
1957 +    {
1958 +      dwz_file->line.asection = sectp;
1959 +      dwz_file->line.size = bfd_get_section_size (sectp);
1960 +    }
1961 +  else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
1962 +    {
1963 +      dwz_file->macro.asection = sectp;
1964 +      dwz_file->macro.size = bfd_get_section_size (sectp);
1965 +    }
1966 +  else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
1967 +    {
1968 +      dwz_file->gdb_index.asection = sectp;
1969 +      dwz_file->gdb_index.size = bfd_get_section_size (sectp);
1970 +    }
1971 +}
1972 +
1973 +/* Open the separate '.dwz' debug file, if needed.  Error if the file
1974 +   cannot be found.  */
1975 +
1976 +static struct dwz_file *
1977 +dwarf2_get_dwz_file (void)
1978 +{
1979 +  bfd *abfd, *dwz_bfd;
1980 +  asection *section;
1981 +  gdb_byte *data;
1982 +  struct cleanup *cleanup;
1983 +  const char *filename;
1984 +  struct dwz_file *result;
1985 +
1986 +  if (dwarf2_per_objfile->dwz_file != NULL)
1987 +    return dwarf2_per_objfile->dwz_file;
1988 +
1989 +  abfd = dwarf2_per_objfile->objfile->obfd;
1990 +  section = bfd_get_section_by_name (abfd, ".gnu_debugaltlink");
1991 +  if (section == NULL)
1992 +    error (_("could not find '.gnu_debugaltlink' section"));
1993 +  if (!bfd_malloc_and_get_section (abfd, section, &data))
1994 +    error (_("could not read '.gnu_debugaltlink' section: %s"),
1995 +          bfd_errmsg (bfd_get_error ()));
1996 +  cleanup = make_cleanup (xfree, data);
1997 +
1998 +  filename = data;
1999 +  if (!IS_ABSOLUTE_PATH (filename))
2000 +    {
2001 +      char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
2002 +      char *rel;
2003 +
2004 +      make_cleanup (xfree, abs);
2005 +      abs = ldirname (abs);
2006 +      make_cleanup (xfree, abs);
2007 +
2008 +      rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2009 +      make_cleanup (xfree, rel);
2010 +      filename = rel;
2011 +    }
2012 +
2013 +  /* The format is just a NUL-terminated file name, followed by the
2014 +     build-id.  For now, though, we ignore the build-id.  */
2015 +  dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2016 +  if (dwz_bfd == NULL)
2017 +    error (_("could not read '%s': %s"), filename,
2018 +          bfd_errmsg (bfd_get_error ()));
2019 +
2020 +  if (!bfd_check_format (dwz_bfd, bfd_object))
2021 +    {
2022 +      gdb_bfd_unref (dwz_bfd);
2023 +      error (_("file '%s' was not usable: %s"), filename,
2024 +            bfd_errmsg (bfd_get_error ()));
2025 +    }
2026 +
2027 +  result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2028 +                          struct dwz_file);
2029 +  result->dwz_bfd = dwz_bfd;
2030 +
2031 +  bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2032 +
2033 +  do_cleanups (cleanup);
2034 +
2035 +  return result;
2036 +}
2037  \f
2038  /* DWARF quick_symbols_functions support.  */
2039  
2040 @@ -2279,23 +2304,19 @@ extract_cu_value (const char *bytes, ULONGEST *result)
2041    return 1;
2042  }
2043  
2044 -/* Read the CU list from the mapped index, and use it to create all
2045 -   the CU objects for this objfile.  Return 0 if something went wrong,
2046 -   1 if everything went ok.  */
2047 +/* A helper for create_cus_from_index that handles a given list of
2048 +   CUs.  */
2049  
2050  static int
2051 -create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
2052 -                      offset_type cu_list_elements)
2053 +create_cus_from_index_list (struct objfile *objfile,
2054 +                           const gdb_byte *cu_list, offset_type n_elements,
2055 +                           struct dwarf2_section_info *section,
2056 +                           int is_dwz,
2057 +                           int base_offset)
2058  {
2059    offset_type i;
2060  
2061 -  dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
2062 -  dwarf2_per_objfile->all_comp_units
2063 -    = obstack_alloc (&objfile->objfile_obstack,
2064 -                    dwarf2_per_objfile->n_comp_units
2065 -                    * sizeof (struct dwarf2_per_cu_data *));
2066 -
2067 -  for (i = 0; i < cu_list_elements; i += 2)
2068 +  for (i = 0; i < n_elements; i += 2)
2069      {
2070        struct dwarf2_per_cu_data *the_cu;
2071        ULONGEST offset, length;
2072 @@ -2310,15 +2331,45 @@ create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
2073        the_cu->offset.sect_off = offset;
2074        the_cu->length = length;
2075        the_cu->objfile = objfile;
2076 -      the_cu->info_or_types_section = &dwarf2_per_objfile->info;
2077 +      the_cu->info_or_types_section = section;
2078        the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2079                                         struct dwarf2_per_cu_quick_data);
2080 -      dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
2081 +      the_cu->is_dwz = is_dwz;
2082 +      dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
2083      }
2084  
2085    return 1;
2086  }
2087  
2088 +/* Read the CU list from the mapped index, and use it to create all
2089 +   the CU objects for this objfile.  Return 0 if something went wrong,
2090 +   1 if everything went ok.  */
2091 +
2092 +static int
2093 +create_cus_from_index (struct objfile *objfile,
2094 +                      const gdb_byte *cu_list, offset_type cu_list_elements,
2095 +                      const gdb_byte *dwz_list, offset_type dwz_elements)
2096 +{
2097 +  struct dwz_file *dwz;
2098 +
2099 +  dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2100 +  dwarf2_per_objfile->all_comp_units
2101 +    = obstack_alloc (&objfile->objfile_obstack,
2102 +                    dwarf2_per_objfile->n_comp_units
2103 +                    * sizeof (struct dwarf2_per_cu_data *));
2104 +
2105 +  if (!create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2106 +                                  &dwarf2_per_objfile->info, 0, 0))
2107 +    return 0;
2108 +
2109 +  if (dwz_elements == 0)
2110 +    return 1;
2111 +
2112 +  dwz = dwarf2_get_dwz_file ();
2113 +  return create_cus_from_index_list (objfile, dwz_list, dwz_elements,
2114 +                                    &dwz->info, 1, cu_list_elements / 2);
2115 +}
2116 +
2117  /* Create the signatured type hash table from the index.  */
2118  
2119  static int
2120 @@ -2508,33 +2559,44 @@ find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2121      }
2122  }
2123  
2124 -/* Read the index file.  If everything went ok, initialize the "quick"
2125 -   elements of all the CUs and return 1.  Otherwise, return 0.  */
2126 +/* A helper function that reads the .gdb_index from SECTION and fills
2127 +   in MAP.  FILENAME is the name of the file containing the section;
2128 +   it is used for error reporting.  DEPRECATED_OK is nonzero if it is
2129 +   ok to use deprecated sections.
2130 +
2131 +   CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2132 +   out parameters that are filled in with information about the CU and
2133 +   TU lists in the section.
2134 +
2135 +   Returns 1 if all went well, 0 otherwise.  */
2136  
2137  static int
2138 -dwarf2_read_index (struct objfile *objfile)
2139 +read_index_from_section (struct objfile *objfile,
2140 +                        const char *filename,
2141 +                        int deprecated_ok,
2142 +                        struct dwarf2_section_info *section,
2143 +                        struct mapped_index *map,
2144 +                        const gdb_byte **cu_list,
2145 +                        offset_type *cu_list_elements,
2146 +                        const gdb_byte **types_list,
2147 +                        offset_type *types_list_elements)
2148  {
2149    char *addr;
2150 -  struct mapped_index *map;
2151 +  offset_type version;
2152    offset_type *metadata;
2153 -  const gdb_byte *cu_list;
2154 -  const gdb_byte *types_list = NULL;
2155 -  offset_type version, cu_list_elements;
2156 -  offset_type types_list_elements = 0;
2157    int i;
2158  
2159 -  if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
2160 +  if (dwarf2_section_empty_p (section))
2161      return 0;
2162  
2163    /* Older elfutils strip versions could keep the section in the main
2164       executable while splitting it for the separate debug info file.  */
2165 -  if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2166 -       & SEC_HAS_CONTENTS) == 0)
2167 +  if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
2168      return 0;
2169  
2170 -  dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2171 +  dwarf2_read_section (objfile, section);
2172  
2173 -  addr = dwarf2_per_objfile->gdb_index.buffer;
2174 +  addr = section->buffer;
2175    /* Version check.  */
2176    version = MAYBE_SWAP (*(offset_type *) addr);
2177    /* Versions earlier than 3 emitted every copy of a psymbol.  This
2178 @@ -2547,7 +2609,7 @@ dwarf2_read_index (struct objfile *objfile)
2179        if (!warning_printed)
2180         {
2181           warning (_("Skipping obsolete .gdb_index section in %s."),
2182 -                  objfile->name);
2183 +                  filename);
2184           warning_printed = 1;
2185         }
2186        return 0;
2187 @@ -2560,7 +2622,7 @@ dwarf2_read_index (struct objfile *objfile)
2188       set breakpoints on inlined functions by name, so we ignore these
2189       indices unless the user has done
2190       "set use-deprecated-index-sections on".  */
2191 -  if (version < 6 && !use_deprecated_index_sections)
2192 +  if (version < 6 && !deprecated_ok)
2193      {
2194        static int warning_printed = 0;
2195        if (!warning_printed)
2196 @@ -2569,7 +2631,7 @@ dwarf2_read_index (struct objfile *objfile)
2197  Skipping deprecated .gdb_index section in %s.\n\
2198  Do \"set use-deprecated-index-sections on\" before the file is read\n\
2199  to use the section anyway."),
2200 -                  objfile->name);
2201 +                  filename);
2202           warning_printed = 1;
2203         }
2204        return 0;
2205 @@ -2579,22 +2641,21 @@ to use the section anyway."),
2206    if (version > 7)
2207      return 0;
2208  
2209 -  map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
2210    map->version = version;
2211 -  map->total_size = dwarf2_per_objfile->gdb_index.size;
2212 +  map->total_size = section->size;
2213  
2214    metadata = (offset_type *) (addr + sizeof (offset_type));
2215  
2216    i = 0;
2217 -  cu_list = addr + MAYBE_SWAP (metadata[i]);
2218 -  cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2219 -                     / 8);
2220 +  *cu_list = addr + MAYBE_SWAP (metadata[i]);
2221 +  *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2222 +                      / 8);
2223    ++i;
2224  
2225 -  types_list = addr + MAYBE_SWAP (metadata[i]);
2226 -  types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2227 -                         - MAYBE_SWAP (metadata[i]))
2228 -                        / 8);
2229 +  *types_list = addr + MAYBE_SWAP (metadata[i]);
2230 +  *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2231 +                          - MAYBE_SWAP (metadata[i]))
2232 +                         / 8);
2233    ++i;
2234  
2235    map->address_table = addr + MAYBE_SWAP (metadata[i]);
2236 @@ -2610,11 +2671,55 @@ to use the section anyway."),
2237  
2238    map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2239  
2240 +  return 1;
2241 +}
2242 +
2243 +
2244 +/* Read the index file.  If everything went ok, initialize the "quick"
2245 +   elements of all the CUs and return 1.  Otherwise, return 0.  */
2246 +
2247 +static int
2248 +dwarf2_read_index (struct objfile *objfile)
2249 +{
2250 +  struct mapped_index local_map, *map;
2251 +  const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2252 +  offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2253 +
2254 +  if (!read_index_from_section (objfile, objfile->name,
2255 +                               use_deprecated_index_sections,
2256 +                               &dwarf2_per_objfile->gdb_index, &local_map,
2257 +                               &cu_list, &cu_list_elements,
2258 +                               &types_list, &types_list_elements))
2259 +    return 0;
2260 +
2261    /* Don't use the index if it's empty.  */
2262 -  if (map->symbol_table_slots == 0)
2263 +  if (local_map.symbol_table_slots == 0)
2264      return 0;
2265  
2266 -  if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2267 +  /* If there is a .dwz file, read it so we can get its CU list as
2268 +     well.  */
2269 +  if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
2270 +    {
2271 +      struct dwz_file *dwz = dwarf2_get_dwz_file ();
2272 +      struct mapped_index dwz_map;
2273 +      const gdb_byte *dwz_types_ignore;
2274 +      offset_type dwz_types_elements_ignore;
2275 +
2276 +      if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2277 +                                   1,
2278 +                                   &dwz->gdb_index, &dwz_map,
2279 +                                   &dwz_list, &dwz_list_elements,
2280 +                                   &dwz_types_ignore,
2281 +                                   &dwz_types_elements_ignore))
2282 +       {
2283 +         warning (_("could not read '.gdb_index' section from %s; skipping"),
2284 +                  bfd_get_filename (dwz->dwz_bfd));
2285 +         return 0;
2286 +       }
2287 +    }
2288 +
2289 +  if (!create_cus_from_index (objfile, cu_list, cu_list_elements,
2290 +                             dwz_list, dwz_list_elements))
2291      return 0;
2292  
2293    if (types_list_elements)
2294 @@ -2635,7 +2740,10 @@ to use the section anyway."),
2295         return 0;
2296      }
2297  
2298 -  create_addrmap_from_index (objfile, map);
2299 +  create_addrmap_from_index (objfile, &local_map);
2300 +
2301 +  map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2302 +  *map = local_map;
2303  
2304    dwarf2_per_objfile->index_table = map;
2305    dwarf2_per_objfile->using_index = 1;
2306 @@ -3686,6 +3794,22 @@ read_comp_unit_head (struct comp_unit_head *cu_header,
2307    return info_ptr;
2308  }
2309  
2310 +/* Helper function that returns the proper abbrev section for
2311 +   THIS_CU.  */
2312 +
2313 +static struct dwarf2_section_info *
2314 +get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
2315 +{
2316 +  struct dwarf2_section_info *abbrev;
2317 +
2318 +  if (this_cu->is_dwz)
2319 +    abbrev = &dwarf2_get_dwz_file ()->abbrev;
2320 +  else
2321 +    abbrev = &dwarf2_per_objfile->abbrev;
2322 +
2323 +  return abbrev;
2324 +}
2325 +
2326  /* Subroutine of read_and_check_comp_unit_head and
2327     read_and_check_type_unit_head to simplify them.
2328     Perform various error checking on the header.  */
2329 @@ -3704,8 +3828,7 @@ error_check_comp_unit_head (struct comp_unit_head *header,
2330            filename);
2331  
2332    if (header->abbrev_offset.sect_off
2333 -      >= dwarf2_section_size (dwarf2_per_objfile->objfile,
2334 -                             &dwarf2_per_objfile->abbrev))
2335 +      >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
2336      error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2337            "(offset 0x%lx + 6) [in module %s]"),
2338            (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
2339 @@ -3942,6 +4065,7 @@ create_debug_types_hash_table (struct dwo_file *dwo_file,
2340      {
2341        bfd *abfd;
2342        gdb_byte *info_ptr, *end_ptr;
2343 +      struct dwarf2_section_info *abbrev_section;
2344  
2345        dwarf2_read_section (objfile, section);
2346        info_ptr = section->buffer;
2347 @@ -3953,6 +4077,11 @@ create_debug_types_hash_table (struct dwo_file *dwo_file,
2348          not present, in which case section->asection will be NULL.  */
2349        abfd = section->asection->owner;
2350  
2351 +      if (dwo_file)
2352 +       abbrev_section = &dwo_file->sections.abbrev;
2353 +      else
2354 +       abbrev_section = &dwarf2_per_objfile->abbrev;
2355 +
2356        if (types_htab == NULL)
2357         {
2358           if (dwo_file)
2359 @@ -4192,7 +4321,8 @@ init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
2360    dwarf2_read_section (objfile, section);
2361  
2362    begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
2363 -  abbrev_section = &dwarf2_per_objfile->abbrev;
2364 +
2365 +  abbrev_section = get_abbrev_section_for_cu (this_cu);
2366  
2367    if (use_existing_cu && this_cu->cu != NULL)
2368      {
2369 @@ -4574,7 +4704,7 @@ init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
2370                                 void *data)
2371  {
2372    init_cutu_and_read_dies_no_follow (this_cu,
2373 -                                    &dwarf2_per_objfile->abbrev,
2374 +                                    get_abbrev_section_for_cu (this_cu),
2375                                      NULL,
2376                                      die_reader_func, data);
2377  }
2378 @@ -5202,6 +5332,9 @@ set_partial_user (struct objfile *objfile)
2379        struct partial_symtab *pst = per_cu->v.psymtab;
2380        int j;
2381  
2382 +      if (pst == NULL)
2383 +       continue;
2384 +
2385        for (j = 0; j < pst->number_of_dependencies; ++j)
2386         {
2387           /* Set the 'user' field only if it is not already set.  */
2388 @@ -5296,38 +5429,32 @@ load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
2389                            load_partial_comp_unit_reader, NULL);
2390  }
2391  
2392 -/* Create a list of all compilation units in OBJFILE.
2393 -   This is only done for -readnow and building partial symtabs.  */
2394 -
2395  static void
2396 -create_all_comp_units (struct objfile *objfile)
2397 +read_comp_units_from_section (struct objfile *objfile,
2398 +                             struct dwarf2_section_info *section,
2399 +                             unsigned int is_dwz,
2400 +                             int *n_allocated,
2401 +                             int *n_comp_units,
2402 +                             struct dwarf2_per_cu_data ***all_comp_units)
2403  {
2404 -  int n_allocated;
2405 -  int n_comp_units;
2406 -  struct dwarf2_per_cu_data **all_comp_units;
2407    gdb_byte *info_ptr;
2408 +  bfd *abfd = section->asection->owner;
2409  
2410 -  dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
2411 -  info_ptr = dwarf2_per_objfile->info.buffer;
2412 +  dwarf2_read_section (objfile, section);
2413  
2414 -  n_comp_units = 0;
2415 -  n_allocated = 10;
2416 -  all_comp_units = xmalloc (n_allocated
2417 -                           * sizeof (struct dwarf2_per_cu_data *));
2418 +  info_ptr = section->buffer;
2419  
2420 -  while (info_ptr < dwarf2_per_objfile->info.buffer
2421 -        + dwarf2_per_objfile->info.size)
2422 +  while (info_ptr < section->buffer + section->size)
2423      {
2424        unsigned int length, initial_length_size;
2425        struct dwarf2_per_cu_data *this_cu;
2426        sect_offset offset;
2427  
2428 -      offset.sect_off = info_ptr - dwarf2_per_objfile->info.buffer;
2429 +      offset.sect_off = info_ptr - section->buffer;
2430  
2431        /* Read just enough information to find out where the next
2432          compilation unit is.  */
2433 -      length = read_initial_length (objfile->obfd, info_ptr,
2434 -                                   &initial_length_size);
2435 +      length = read_initial_length (abfd, info_ptr, &initial_length_size);
2436  
2437        /* Save the compilation unit for later lookup.  */
2438        this_cu = obstack_alloc (&objfile->objfile_obstack,
2439 @@ -5335,20 +5462,50 @@ create_all_comp_units (struct objfile *objfile)
2440        memset (this_cu, 0, sizeof (*this_cu));
2441        this_cu->offset = offset;
2442        this_cu->length = length + initial_length_size;
2443 +      this_cu->is_dwz = is_dwz;
2444        this_cu->objfile = objfile;
2445 -      this_cu->info_or_types_section = &dwarf2_per_objfile->info;
2446 +      this_cu->info_or_types_section = section;
2447  
2448 -      if (n_comp_units == n_allocated)
2449 +      if (*n_comp_units == *n_allocated)
2450         {
2451 -         n_allocated *= 2;
2452 -         all_comp_units = xrealloc (all_comp_units,
2453 -                                    n_allocated
2454 -                                    * sizeof (struct dwarf2_per_cu_data *));
2455 +         *n_allocated *= 2;
2456 +         *all_comp_units = xrealloc (*all_comp_units,
2457 +                                     *n_allocated
2458 +                                     * sizeof (struct dwarf2_per_cu_data *));
2459         }
2460 -      all_comp_units[n_comp_units++] = this_cu;
2461 +      (*all_comp_units)[*n_comp_units] = this_cu;
2462 +      ++*n_comp_units;
2463  
2464        info_ptr = info_ptr + this_cu->length;
2465      }
2466 +}
2467 +
2468 +/* Create a list of all compilation units in OBJFILE.
2469 +   This is only done for -readnow and building partial symtabs.  */
2470 +
2471 +static void
2472 +create_all_comp_units (struct objfile *objfile)
2473 +{
2474 +  int n_allocated;
2475 +  int n_comp_units;
2476 +  struct dwarf2_per_cu_data **all_comp_units;
2477 +
2478 +  n_comp_units = 0;
2479 +  n_allocated = 10;
2480 +  all_comp_units = xmalloc (n_allocated
2481 +                           * sizeof (struct dwarf2_per_cu_data *));
2482 +
2483 +  read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
2484 +                               &n_allocated, &n_comp_units, &all_comp_units);
2485 +
2486 +  if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
2487 +    {
2488 +      struct dwz_file *dwz = dwarf2_get_dwz_file ();
2489 +
2490 +      read_comp_units_from_section (objfile, &dwz->info, 1,
2491 +                                   &n_allocated, &n_comp_units,
2492 +                                   &all_comp_units);
2493 +    }
2494  
2495    dwarf2_per_objfile->all_comp_units
2496      = obstack_alloc (&objfile->objfile_obstack,
2497 @@ -5441,6 +5598,7 @@ scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
2498                   }
2499  
2500                 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
2501 +                                                          pdi->is_dwz,
2502                                                            cu->objfile);
2503  
2504                 /* Go read the partial unit, if needed.  */
2505 @@ -5498,7 +5656,8 @@ partial_die_parent_scope (struct partial_die_info *pdi,
2506  
2507    real_pdi = pdi;
2508    while (real_pdi->has_specification)
2509 -    real_pdi = find_partial_die (real_pdi->spec_offset, cu);
2510 +    real_pdi = find_partial_die (real_pdi->spec_offset,
2511 +                                real_pdi->spec_is_dwz, cu);
2512  
2513    parent = real_pdi->die_parent;
2514    if (parent == NULL)
2515 @@ -5998,6 +6157,9 @@ skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
2516           else
2517             info_ptr += cu->header.offset_size;
2518           break;
2519 +       case DW_FORM_GNU_ref_alt:
2520 +         info_ptr += cu->header.offset_size;
2521 +         break;
2522         case DW_FORM_addr:
2523           info_ptr += cu->header.addr_size;
2524           break;
2525 @@ -6027,6 +6189,7 @@ skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
2526           break;
2527         case DW_FORM_sec_offset:
2528         case DW_FORM_strp:
2529 +       case DW_FORM_GNU_strp_alt:
2530           info_ptr += cu->header.offset_size;
2531           break;
2532         case DW_FORM_exprloc:
2533 @@ -6694,7 +6857,9 @@ process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
2534       it, by scanning the DIE's below the compilation unit.  */
2535    get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
2536  
2537 -  static_block = end_symtab_get_static_block (highpc + baseaddr, objfile, 0);
2538 +  static_block
2539 +    = end_symtab_get_static_block (highpc + baseaddr, objfile, 0,
2540 +                                  per_cu->s.imported_symtabs != NULL);
2541  
2542    /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
2543       Also, DW_AT_ranges may record ranges not belonging to any child DIEs
2544 @@ -6845,9 +7010,11 @@ process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
2545        struct dwarf2_per_cu_data *per_cu;
2546        struct symtab *imported_symtab;
2547        sect_offset offset;
2548 +      int is_dwz;
2549  
2550        offset = dwarf2_get_ref_die_offset (attr);
2551 -      per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
2552 +      is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
2553 +      per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
2554  
2555        /* Queue the unit, if needed.  */
2556        if (maybe_queue_comp_unit (cu, per_cu, cu->language))
2557 @@ -8116,24 +8283,21 @@ try_open_dwo_file (const char *file_name)
2558    if (desc < 0)
2559      return NULL;
2560  
2561 -  sym_bfd = bfd_fopen (absolute_name, gnutarget, FOPEN_RB, desc);
2562 +  sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
2563    if (!sym_bfd)
2564      {
2565        xfree (absolute_name);
2566        return NULL;
2567      }
2568 +  xfree (absolute_name);
2569    bfd_set_cacheable (sym_bfd, 1);
2570  
2571    if (!bfd_check_format (sym_bfd, bfd_object))
2572      {
2573 -      bfd_close (sym_bfd); /* This also closes desc.  */
2574 -      xfree (absolute_name);
2575 +      gdb_bfd_unref (sym_bfd); /* This also closes desc.  */
2576        return NULL;
2577      }
2578  
2579 -  /* bfd_usrdata exists for applications and libbfd must not touch it.  */
2580 -  gdb_assert (bfd_usrdata (sym_bfd) == NULL);
2581 -
2582    return sym_bfd;
2583  }
2584  
2585 @@ -8325,20 +8489,7 @@ free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
2586    struct dwarf2_section_info *section;
2587  
2588    gdb_assert (dwo_file->dwo_bfd != objfile->obfd);
2589 -  bfd_close (dwo_file->dwo_bfd);
2590 -
2591 -  munmap_section_buffer (&dwo_file->sections.abbrev);
2592 -  munmap_section_buffer (&dwo_file->sections.info);
2593 -  munmap_section_buffer (&dwo_file->sections.line);
2594 -  munmap_section_buffer (&dwo_file->sections.loc);
2595 -  munmap_section_buffer (&dwo_file->sections.str);
2596 -  munmap_section_buffer (&dwo_file->sections.str_offsets);
2597 -
2598 -  for (ix = 0;
2599 -       VEC_iterate (dwarf2_section_info_def, dwo_file->sections.types,
2600 -                   ix, section);
2601 -       ++ix)
2602 -    munmap_section_buffer (section);
2603 +  gdb_bfd_unref (dwo_file->dwo_bfd);
2604  
2605    VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
2606  }
2607 @@ -10721,6 +10872,29 @@ process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
2608    new_symbol (die, this_type, cu);
2609  }
2610  
2611 +/* Create a new array dimension referencing its target type TYPE.
2612 +
2613 +   Multidimensional arrays are internally represented as a stack of
2614 +   singledimensional arrays being referenced by their TYPE_TARGET_TYPE.  */
2615 +
2616 +static struct type *
2617 +create_single_array_dimension (struct type *type, struct type *range_type,
2618 +                              struct die_info *die, struct dwarf2_cu *cu)
2619 +{
2620 +  type = create_array_type (NULL, type, range_type);
2621 +
2622 +  /* These generic type attributes need to be fetched by
2623 +     evaluate_subexp_standard <multi_f77_subscript>'s call of
2624 +     value_subscripted_rvalue only for the innermost array type.  */
2625 +  fetch_die_type_attrs (die, type, cu);
2626 +
2627 +  /* These generic type attributes are checked for allocated/associated
2628 +     validity while accessing FIELD_LOC_KIND_DWARF_BLOCK.  */
2629 +  fetch_die_type_attrs (die, range_type, cu);
2630 +
2631 +  return type;
2632 +}
2633 +
2634  /* Extract all information from a DW_TAG_array_type DIE and put it in
2635     the DIE's type field.  For now, this only handles one dimensional
2636     arrays.  */
2637 @@ -10734,7 +10908,7 @@ read_array_type (struct die_info *die, struct dwarf2_cu *cu)
2638    struct type *element_type, *range_type, *index_type;
2639    struct type **range_types = NULL;
2640    struct attribute *attr;
2641 -  int ndim = 0;
2642 +  int ndim = 0, i;
2643    struct cleanup *back_to;
2644    char *name;
2645  
2646 @@ -10787,17 +10961,19 @@ read_array_type (struct die_info *die, struct dwarf2_cu *cu)
2647    type = element_type;
2648  
2649    if (read_array_order (die, cu) == DW_ORD_col_major)
2650 -    {
2651 -      int i = 0;
2652 -
2653 -      while (i < ndim)
2654 -       type = create_array_type (NULL, type, range_types[i++]);
2655 -    }
2656 -  else
2657 -    {
2658 -      while (ndim-- > 0)
2659 -       type = create_array_type (NULL, type, range_types[ndim]);
2660 -    }
2661 +    for (i = 0; i < ndim; i++)
2662 +      type = create_single_array_dimension (type, range_types[i], die, cu);
2663 +  else /* (read_array_order (die, cu) == DW_ORD_row_major) */
2664 +    for (i = ndim - 1; i >= 0; i--)
2665 +      type = create_single_array_dimension (type, range_types[i], die, cu);
2666 +
2667 +  /* Data locations should be set only for the outermost dimension as they
2668 +     would be confusing for the dereferenced offset on the inner ones.  */
2669 +  attr = dwarf2_attr (die, DW_AT_data_location, cu);
2670 +  if (attr_form_is_block (attr))
2671 +    TYPE_DATA_LOCATION_DWARF_BLOCK (type)
2672 +      = dwarf2_attr_to_locexpr_baton (attr, cu);
2673 +  gdb_assert (!TYPE_DATA_LOCATION_IS_ADDR (type));
2674  
2675    /* Understand Dwarf2 support for vector types (like they occur on
2676       the PowerPC w/ AltiVec).  Gcc just adds another attribute to the
2677 @@ -11281,29 +11457,114 @@ read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
2678    struct gdbarch *gdbarch = get_objfile_arch (objfile);
2679    struct type *type, *range_type, *index_type, *char_type;
2680    struct attribute *attr;
2681 -  unsigned int length;
2682 +  int length;
2683 +
2684 +  index_type = objfile_type (objfile)->builtin_int;
2685 +  /* RANGE_TYPE is allocated from OBJFILE, not as a permanent type.  */
2686 +  range_type = alloc_type (objfile);
2687 +  /* LOW_BOUND and HIGH_BOUND are set for real below.  */
2688 +  range_type = create_range_type (range_type, index_type, 0, -1);
2689 +
2690 +  /* C/C++ should probably have the low bound 0 but C/C++ does not use
2691 +     DW_TAG_string_type.  */
2692 +  TYPE_LOW_BOUND (range_type) = 1;
2693  
2694    attr = dwarf2_attr (die, DW_AT_string_length, cu);
2695 -  if (attr)
2696 +  if (attr && attr_form_is_block (attr))
2697      {
2698 -      length = DW_UNSND (attr);
2699 +      /* Security check for a size overflow.  */
2700 +      if (DW_BLOCK (attr)->size + 2 < DW_BLOCK (attr)->size)
2701 +       TYPE_HIGH_BOUND (range_type) = 1;
2702 +      /* Extend the DWARF block by a new DW_OP_deref/DW_OP_deref_size
2703 +        instruction as DW_AT_string_length specifies the length location, not
2704 +        its value.  */
2705 +      else
2706 +       {
2707 +         struct dwarf2_locexpr_baton *length_baton = NULL;
2708 +         struct dwarf_block *blk = DW_BLOCK (attr);
2709 +
2710 +         /* Turn any single DW_OP_reg* into DW_OP_breg*(0) but clearing
2711 +            DW_OP_deref* in such case.  */
2712 +
2713 +         if (blk->size == 1 && blk->data[0] >= DW_OP_reg0
2714 +             && blk->data[0] <= DW_OP_reg31)
2715 +           length_baton = dwarf2_attr_to_locexpr_baton (attr, cu);
2716 +         else if (blk->size > 1 && blk->data[0] == DW_OP_regx)
2717 +           {
2718 +             ULONGEST ulongest;
2719 +             const gdb_byte *end;
2720 +
2721 +             end = safe_read_uleb128 (&blk->data[1], &blk->data[blk->size],
2722 +                                      &ulongest);
2723 +             if (end == &blk->data[blk->size])
2724 +               length_baton = dwarf2_attr_to_locexpr_baton (attr, cu);
2725 +           }
2726 +
2727 +         if (length_baton == NULL)
2728 +           {
2729 +             struct attribute *size_attr;
2730 +             gdb_byte *data;
2731 +
2732 +             length_baton = obstack_alloc (&cu->comp_unit_obstack,
2733 +                                           sizeof (*length_baton));
2734 +             length_baton->per_cu = cu->per_cu;
2735 +             length_baton->size = DW_BLOCK (attr)->size + 2;
2736 +             data = obstack_alloc (&cu->comp_unit_obstack,
2737 +                                   length_baton->size);
2738 +             length_baton->data = data;
2739 +             memcpy (data, DW_BLOCK (attr)->data, DW_BLOCK (attr)->size);
2740 +
2741 +             /* DW_AT_BYTE_SIZE existing together with DW_AT_STRING_LENGTH
2742 +                specifies the size of an integer to fetch.  */
2743 +             size_attr = dwarf2_attr (die, DW_AT_byte_size, cu);
2744 +             if (size_attr)
2745 +               {
2746 +                 data[DW_BLOCK (attr)->size] = DW_OP_deref_size;
2747 +                 data[DW_BLOCK (attr)->size + 1] = DW_UNSND (size_attr);
2748 +                 if (data[DW_BLOCK (attr)->size + 1] != DW_UNSND (size_attr))
2749 +                   complaint (&symfile_complaints,
2750 +                              _("DW_AT_string_length's DW_AT_byte_size "
2751 +                                "integer exceeds the byte size storage"));
2752 +               }
2753 +             else
2754 +               {
2755 +                 data[DW_BLOCK (attr)->size] = DW_OP_deref;
2756 +                 data[DW_BLOCK (attr)->size + 1] = DW_OP_nop;
2757 +               }
2758 +           }
2759 +
2760 +         TYPE_RANGE_DATA (range_type)->high.kind
2761 +           = RANGE_BOUND_KIND_DWARF_BLOCK;
2762 +         TYPE_RANGE_DATA (range_type)->high.u.dwarf_block = length_baton;
2763 +         TYPE_DYNAMIC (range_type) = 1;
2764 +       }
2765      }
2766    else
2767      {
2768 -      /* Check for the DW_AT_byte_size attribute.  */
2769 +      if (attr && attr_form_is_constant (attr))
2770 +       {
2771 +         /* We currently do not support a constant address where the location
2772 +            should be read from - attr_form_is_block is expected instead.  See
2773 +            DWARF for the DW_AT_STRING_LENGTH vs. DW_AT_BYTE_SIZE difference.
2774 +            */
2775 +         /* PASSTHRU */
2776 +       }
2777 +
2778        attr = dwarf2_attr (die, DW_AT_byte_size, cu);
2779 -      if (attr)
2780 -        {
2781 -          length = DW_UNSND (attr);
2782 -        }
2783 +      if (attr && attr_form_is_block (attr))
2784 +       {
2785 +         TYPE_RANGE_DATA (range_type)->high.kind
2786 +           = RANGE_BOUND_KIND_DWARF_BLOCK;
2787 +         TYPE_RANGE_DATA (range_type)->high.u.dwarf_block =
2788 +                                       dwarf2_attr_to_locexpr_baton (attr, cu);
2789 +         TYPE_DYNAMIC (range_type) = 1;
2790 +       }
2791 +      else if (attr && attr_form_is_constant (attr))
2792 +       TYPE_HIGH_BOUND (range_type) = dwarf2_get_attr_constant_value (attr, 0);
2793        else
2794 -        {
2795 -          length = 1;
2796 -        }
2797 +       TYPE_HIGH_BOUND (range_type) = 1;
2798      }
2799  
2800 -  index_type = objfile_type (objfile)->builtin_int;
2801 -  range_type = create_range_type (NULL, index_type, 1, length);
2802    char_type = language_string_char_type (cu->language_defn, gdbarch);
2803    type = create_string_type (NULL, char_type, range_type);
2804  
2805 @@ -11607,7 +11868,7 @@ read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
2806    struct type *base_type;
2807    struct type *range_type;
2808    struct attribute *attr;
2809 -  LONGEST low, high;
2810 +  LONGEST low;
2811    int low_default_is_valid;
2812    char *name;
2813    LONGEST negative_mask;
2814 @@ -11663,42 +11924,6 @@ read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
2815                                       "- DIE at 0x%x [in module %s]"),
2816                die->offset.sect_off, cu->objfile->name);
2817  
2818 -  attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
2819 -  if (attr)
2820 -    {
2821 -      if (attr_form_is_block (attr) || is_ref_attr (attr))
2822 -        {
2823 -          /* GCC encodes arrays with unspecified or dynamic length
2824 -             with a DW_FORM_block1 attribute or a reference attribute.
2825 -             FIXME: GDB does not yet know how to handle dynamic
2826 -             arrays properly, treat them as arrays with unspecified
2827 -             length for now.
2828 -
2829 -             FIXME: jimb/2003-09-22: GDB does not really know
2830 -             how to handle arrays of unspecified length
2831 -             either; we just represent them as zero-length
2832 -             arrays.  Choose an appropriate upper bound given
2833 -             the lower bound we've computed above.  */
2834 -          high = low - 1;
2835 -        }
2836 -      else
2837 -        high = dwarf2_get_attr_constant_value (attr, 1);
2838 -    }
2839 -  else
2840 -    {
2841 -      attr = dwarf2_attr (die, DW_AT_count, cu);
2842 -      if (attr)
2843 -       {
2844 -         int count = dwarf2_get_attr_constant_value (attr, 1);
2845 -         high = low + count - 1;
2846 -       }
2847 -      else
2848 -       {
2849 -         /* Unspecified array length.  */
2850 -         high = low - 1;
2851 -       }
2852 -    }
2853 -
2854    /* Dwarf-2 specifications explicitly allows to create subrange types
2855       without specifying a base type.
2856       In that case, the base type must be set to the type of
2857 @@ -11737,24 +11962,163 @@ read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
2858         }
2859      }
2860  
2861 -  negative_mask =
2862 +  /* LOW_BOUND and HIGH_BOUND are set for real below.  */
2863 +  range_type = create_range_type (NULL, base_type, 0, -1);
2864 +  TYPE_UNSIGNED (range_type) = 0;
2865 +
2866 +  negative_mask = 
2867      (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
2868 -  if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
2869 -    low |= negative_mask;
2870 -  if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
2871 -    high |= negative_mask;
2872  
2873 -  range_type = create_range_type (NULL, base_type, low, high);
2874 +  /* Exclude language_ada from any TYPE_DYNAMIC constructs below.  GDB Ada
2875 +     supports implements the dynamic bounds in a non-DWARF way and the
2876 +     existing DWARF dynamic bounds are invalid, leading to memory access
2877 +     errors.  */
2878  
2879 -  /* Mark arrays with dynamic length at least as an array of unspecified
2880 -     length.  GDB could check the boundary but before it gets implemented at
2881 -     least allow accessing the array elements.  */
2882 -  if (attr && attr_form_is_block (attr))
2883 -    TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
2884 +  attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
2885 +  if (attr && attr_form_is_block (attr) && cu->language != language_ada)
2886 +    {
2887 +      TYPE_RANGE_DATA (range_type)->low.kind = RANGE_BOUND_KIND_DWARF_BLOCK;
2888 +      TYPE_RANGE_DATA (range_type)->low.u.dwarf_block =
2889 +                                       dwarf2_attr_to_locexpr_baton (attr, cu);
2890 +      TYPE_DYNAMIC (range_type) = 1;
2891 +      /* For setting a default if DW_AT_UPPER_BOUND would be missing.  */
2892 +      low = 0;
2893 +    }
2894 +  else if (attr && is_ref_attr (attr) && cu->language != language_ada)
2895 +    {
2896 +      struct die_info *target_die;
2897 +      struct dwarf2_cu *target_cu = cu;
2898 +      struct attribute *target_loc_attr;
2899  
2900 -  /* Ada expects an empty array on no boundary attributes.  */
2901 -  if (attr == NULL && cu->language != language_ada)
2902 -    TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
2903 +      target_die = follow_die_ref_or_sig (die, attr, &target_cu);
2904 +      gdb_assert (target_cu->objfile == cu->objfile);
2905 +      target_loc_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
2906 +
2907 +      TYPE_RANGE_DATA (range_type)->low.kind = RANGE_BOUND_KIND_DWARF_LOCLIST;
2908 +      TYPE_RANGE_DATA (range_type)->low.u.dwarf_loclist.loclist
2909 +        = dwarf2_attr_to_loclist_baton (target_loc_attr, target_cu);
2910 +      TYPE_RANGE_DATA (range_type)->low.u.dwarf_loclist.type
2911 +        = die_type (target_die, target_cu);
2912 +      TYPE_DYNAMIC (range_type) = 1;
2913 +      /* For setting a default if DW_AT_UPPER_BOUND would be missing.  */
2914 +      low = 0;
2915 +    }
2916 +  else
2917 +    {
2918 +      if (attr && attr_form_is_constant (attr))
2919 +       low = dwarf2_get_attr_constant_value (attr, 0);
2920 +      else
2921 +       {
2922 +         if (cu->language == language_fortran)
2923 +           {
2924 +             /* FORTRAN implies a lower bound of 1, if not given.  */
2925 +             low = 1;
2926 +           }
2927 +         else
2928 +           {
2929 +             /* According to DWARF we should assume the value 0 only for
2930 +                LANGUAGE_C and LANGUAGE_CPLUS.  */
2931 +             low = 0;
2932 +           }
2933 +       }
2934 +      if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
2935 +       low |= negative_mask;
2936 +      TYPE_LOW_BOUND (range_type) = low;
2937 +      if (low >= 0)
2938 +       TYPE_UNSIGNED (range_type) = 1;
2939 +    }
2940 +
2941 +  attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
2942 +  if (!attr || (!attr_form_is_block (attr) && !attr_form_is_constant (attr)
2943 +               && !is_ref_attr (attr)))
2944 +    {
2945 +      attr = dwarf2_attr (die, DW_AT_count, cu);
2946 +      /* It does not hurt but it is needlessly ineffective in check_typedef.  */
2947 +      if (attr && (attr_form_is_block (attr) || attr_form_is_constant (attr)))
2948 +       {
2949 +         TYPE_RANGE_HIGH_BOUND_IS_COUNT (range_type) = 1;
2950 +         TYPE_DYNAMIC (range_type) = 1;
2951 +       }
2952 +      /* Pass it now as the regular DW_AT_upper_bound.  */
2953 +    }
2954 +
2955 +  if (attr && attr_form_is_block (attr) && cu->language != language_ada)
2956 +    {
2957 +      TYPE_RANGE_DATA (range_type)->high.kind = RANGE_BOUND_KIND_DWARF_BLOCK;
2958 +      TYPE_RANGE_DATA (range_type)->high.u.dwarf_block =
2959 +                                       dwarf2_attr_to_locexpr_baton (attr, cu);
2960 +      TYPE_DYNAMIC (range_type) = 1;
2961 +    }
2962 +  else if (attr && is_ref_attr (attr) && cu->language != language_ada)
2963 +    {
2964 +      struct die_info *target_die;
2965 +      struct dwarf2_cu *target_cu = cu;
2966 +      struct attribute *target_loc_attr;
2967 +
2968 +      target_die = follow_die_ref_or_sig (die, attr, &target_cu);
2969 +      gdb_assert (target_cu->objfile == cu->objfile);
2970 +      target_loc_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
2971 +
2972 +      TYPE_RANGE_DATA (range_type)->high.kind = RANGE_BOUND_KIND_DWARF_LOCLIST;
2973 +      TYPE_RANGE_DATA (range_type)->high.u.dwarf_loclist.loclist
2974 +        = dwarf2_attr_to_loclist_baton (target_loc_attr, target_cu);
2975 +      TYPE_RANGE_DATA (range_type)->high.u.dwarf_loclist.type
2976 +        = die_type (target_die, target_cu);
2977 +      TYPE_DYNAMIC (range_type) = 1;
2978 +    }
2979 +  else
2980 +    {
2981 +      LONGEST high;
2982 +
2983 +      if (attr && attr_form_is_constant (attr))
2984 +       high = dwarf2_get_attr_constant_value (attr, 0);
2985 +      else
2986 +       {
2987 +         /* Ada expects an empty array on no boundary attributes.  */
2988 +         if (cu->language != language_ada)
2989 +           TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
2990 +         high = low - 1;
2991 +       }
2992 +      if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
2993 +       high |= negative_mask;
2994 +      TYPE_HIGH_BOUND (range_type) = high;
2995 +    }
2996 +
2997 +  /* DW_AT_bit_stride is currently unsupported as we count in bytes.  */
2998 +  attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
2999 +  if (attr && attr_form_is_block (attr) && cu->language != language_ada)
3000 +    {
3001 +      TYPE_RANGE_DATA (range_type)->byte_stride.kind
3002 +        = RANGE_BOUND_KIND_DWARF_BLOCK;
3003 +      TYPE_RANGE_DATA (range_type)->byte_stride.u.dwarf_block =
3004 +                                       dwarf2_attr_to_locexpr_baton (attr, cu);
3005 +      TYPE_DYNAMIC (range_type) = 1;
3006 +    }
3007 +  else if (attr && is_ref_attr (attr) && cu->language != language_ada)
3008 +    {
3009 +      struct die_info *target_die;
3010 +      struct dwarf2_cu *target_cu = cu;
3011 +      struct attribute *target_loc_attr;
3012 +
3013 +      target_die = follow_die_ref_or_sig (die, attr, &target_cu);
3014 +      gdb_assert (target_cu->objfile == cu->objfile);
3015 +      target_loc_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
3016 +
3017 +      TYPE_RANGE_DATA (range_type)->byte_stride.kind
3018 +        = RANGE_BOUND_KIND_DWARF_LOCLIST;
3019 +      TYPE_RANGE_DATA (range_type)->byte_stride.u.dwarf_loclist.loclist
3020 +        = dwarf2_attr_to_loclist_baton (target_loc_attr, target_cu);
3021 +      TYPE_RANGE_DATA (range_type)->byte_stride.u.dwarf_loclist.type
3022 +        = die_type (target_die, target_cu);
3023 +      TYPE_DYNAMIC (range_type) = 1;
3024 +    }
3025 +  else if (attr && attr_form_is_constant (attr))
3026 +    {
3027 +      TYPE_BYTE_STRIDE (range_type) = dwarf2_get_attr_constant_value (attr, 0);
3028 +      if (TYPE_BYTE_STRIDE (range_type) == 0)
3029 +       complaint (&symfile_complaints,
3030 +                  _("Found DW_AT_byte_stride with unsupported value 0"));
3031 +    }
3032  
3033    name = dwarf2_name (die, cu);
3034    if (name)
3035 @@ -12544,6 +12908,8 @@ read_partial_die (const struct die_reader_specs *reader,
3036         case DW_AT_extension:
3037           part_die->has_specification = 1;
3038           part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
3039 +         part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
3040 +                                  || cu->per_cu->is_dwz);
3041           break;
3042         case DW_AT_sibling:
3043           /* Ignore absolute siblings, they might point outside of
3044 @@ -12590,7 +12956,11 @@ read_partial_die (const struct die_reader_specs *reader,
3045  
3046         case DW_AT_import:
3047           if (part_die->tag == DW_TAG_imported_unit)
3048 -           part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
3049 +           {
3050 +             part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
3051 +             part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
3052 +                                 || cu->per_cu->is_dwz);
3053 +           }
3054           break;
3055  
3056         default:
3057 @@ -12661,13 +13031,14 @@ find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
3058     DW_FORM_ref_sig8).  */
3059  
3060  static struct partial_die_info *
3061 -find_partial_die (sect_offset offset, struct dwarf2_cu *cu)
3062 +find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
3063  {
3064    struct objfile *objfile = cu->objfile;
3065    struct dwarf2_per_cu_data *per_cu = NULL;
3066    struct partial_die_info *pd = NULL;
3067  
3068 -  if (offset_in_cu_p (&cu->header, offset))
3069 +  if (offset_in_dwz == cu->per_cu->is_dwz
3070 +      && offset_in_cu_p (&cu->header, offset))
3071      {
3072        pd = find_partial_die_in_comp_unit (offset, cu);
3073        if (pd != NULL)
3074 @@ -12686,7 +13057,8 @@ find_partial_die (sect_offset offset, struct dwarf2_cu *cu)
3075                  (long) cu->header.offset.sect_off, (long) offset.sect_off,
3076                  bfd_get_filename (objfile->obfd));
3077         }
3078 -      per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
3079 +      per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
3080 +                                                objfile);
3081  
3082        if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
3083         load_partial_comp_unit (per_cu);
3084 @@ -12744,7 +13116,8 @@ guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
3085  
3086    real_pdi = struct_pdi;
3087    while (real_pdi->has_specification)
3088 -    real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3089 +    real_pdi = find_partial_die (real_pdi->spec_offset,
3090 +                                real_pdi->spec_is_dwz, cu);
3091  
3092    if (real_pdi->die_parent != NULL)
3093      return;
3094 @@ -12792,7 +13165,8 @@ fixup_partial_die (struct partial_die_info *part_die,
3095      {
3096        struct partial_die_info *spec_die;
3097  
3098 -      spec_die = find_partial_die (part_die->spec_offset, cu);
3099 +      spec_die = find_partial_die (part_die->spec_offset,
3100 +                                  part_die->spec_is_dwz, cu);
3101  
3102        fixup_partial_die (spec_die, cu);
3103  
3104 @@ -12880,6 +13254,10 @@ read_attribute_value (const struct die_reader_specs *reader,
3105                                        &cu->header, &bytes_read);
3106        info_ptr += bytes_read;
3107        break;
3108 +    case DW_FORM_GNU_ref_alt:
3109 +      DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
3110 +      info_ptr += bytes_read;
3111 +      break;
3112      case DW_FORM_addr:
3113        DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3114        info_ptr += bytes_read;
3115 @@ -12922,10 +13300,25 @@ read_attribute_value (const struct die_reader_specs *reader,
3116        info_ptr += bytes_read;
3117        break;
3118      case DW_FORM_strp:
3119 -      DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
3120 -                                              &bytes_read);
3121 -      DW_STRING_IS_CANONICAL (attr) = 0;
3122 -      info_ptr += bytes_read;
3123 +      if (!cu->per_cu->is_dwz)
3124 +       {
3125 +         DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
3126 +                                                  &bytes_read);
3127 +         DW_STRING_IS_CANONICAL (attr) = 0;
3128 +         info_ptr += bytes_read;
3129 +         break;
3130 +       }
3131 +      /* FALLTHROUGH */
3132 +    case DW_FORM_GNU_strp_alt:
3133 +      {
3134 +       struct dwz_file *dwz = dwarf2_get_dwz_file ();
3135 +       LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
3136 +                                         &bytes_read);
3137 +
3138 +       DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
3139 +       DW_STRING_IS_CANONICAL (attr) = 0;
3140 +       info_ptr += bytes_read;
3141 +      }
3142        break;
3143      case DW_FORM_exprloc:
3144      case DW_FORM_block:
3145 @@ -13037,6 +13430,10 @@ read_attribute_value (const struct die_reader_specs *reader,
3146              bfd_get_filename (abfd));
3147      }
3148  
3149 +  /* Super hack.  */
3150 +  if (cu->per_cu->is_dwz && is_ref_attr (attr))
3151 +    attr->form = DW_FORM_GNU_ref_alt;
3152 +
3153    /* We have seen instances where the compiler tried to emit a byte
3154       size attribute of -1 which ended up being encoded as an unsigned
3155       0xffffffff.  Although 0xffffffff is technically a valid size value,
3156 @@ -13333,6 +13730,30 @@ read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
3157    return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
3158  }
3159  
3160 +/* Read a string at offset STR_OFFSET in the .debug_str section from
3161 +   the .dwz file DWZ.  Throw an error if the offset is too large.  If
3162 +   the string consists of a single NUL byte, return NULL; otherwise
3163 +   return a pointer to the string.  */
3164 +
3165 +static char *
3166 +read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
3167 +{
3168 +  dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
3169 +
3170 +  if (dwz->str.buffer == NULL)
3171 +    error (_("DW_FORM_GNU_strp_alt used without .debug_str "
3172 +            "section [in module %s]"),
3173 +          bfd_get_filename (dwz->dwz_bfd));
3174 +  if (str_offset >= dwz->str.size)
3175 +    error (_("DW_FORM_GNU_strp_alt pointing outside of "
3176 +            ".debug_str section [in module %s]"),
3177 +          bfd_get_filename (dwz->dwz_bfd));
3178 +  gdb_assert (HOST_CHAR_BIT == 8);
3179 +  if (dwz->str.buffer[str_offset] == '\0')
3180 +    return NULL;
3181 +  return (char *) (dwz->str.buffer + str_offset);
3182 +}
3183 +
3184  static char *
3185  read_indirect_string (bfd *abfd, gdb_byte *buf,
3186                       const struct comp_unit_head *cu_header,
3187 @@ -13804,6 +14225,30 @@ add_file_name (struct line_header *lh,
3188    fe->symtab = NULL;
3189  }
3190  
3191 +/* A convenience function to find the proper .debug_line section for a
3192 +   CU.  */
3193 +
3194 +static struct dwarf2_section_info *
3195 +get_debug_line_section (struct dwarf2_cu *cu)
3196 +{
3197 +  struct dwarf2_section_info *section;
3198 +
3199 +  /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
3200 +     DWO file.  */
3201 +  if (cu->dwo_unit && cu->per_cu->is_debug_types)
3202 +    section = &cu->dwo_unit->dwo_file->sections.line;
3203 +  else if (cu->per_cu->is_dwz)
3204 +    {
3205 +      struct dwz_file *dwz = dwarf2_get_dwz_file ();
3206 +
3207 +      section = &dwz->line;
3208 +    }
3209 +  else
3210 +    section = &dwarf2_per_objfile->line;
3211 +
3212 +  return section;
3213 +}
3214 +
3215  /* Read the statement program header starting at OFFSET in
3216     .debug_line, or .debug_line.dwo.  Return a pointer
3217     to a struct line_header, allocated using xmalloc.
3218 @@ -13824,13 +14269,7 @@ dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
3219    struct dwarf2_section_info *section;
3220    bfd *abfd;
3221  
3222 -  /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
3223 -     DWO file.  */
3224 -  if (cu->dwo_unit && cu->per_cu->is_debug_types)
3225 -    section = &cu->dwo_unit->dwo_file->sections.line;
3226 -  else
3227 -    section = &dwarf2_per_objfile->line;
3228 -
3229 +  section = get_debug_line_section (cu);
3230    dwarf2_read_section (dwarf2_per_objfile->objfile, section);
3231    if (section->buffer == NULL)
3232      {
3233 @@ -14155,7 +14594,7 @@ dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
3234                          GCd by the linker.  Ignore it.  PR gdb/12528 */
3235  
3236                       long line_offset
3237 -                       = line_ptr - dwarf2_per_objfile->line.buffer;
3238 +                       = line_ptr - get_debug_line_section (cu)->buffer;
3239  
3240                       complaint (&symfile_complaints,
3241                                  _(".debug_line address at offset 0x%lx is 0 "
3242 @@ -14534,10 +14973,12 @@ var_decode_location (struct attribute *attr, struct symbol *sym,
3243       (i.e. when the value of a register or memory location is
3244       referenced, or a thread-local block, etc.).  Then again, it might
3245       not be worthwhile.  I'm assuming that it isn't unless performance
3246 -     or memory numbers show me otherwise.  */
3247 +     or memory numbers show me otherwise.
3248 +     
3249 +     SYMBOL_CLASS may get overriden by dwarf2_symbol_mark_computed.  */
3250  
3251 -  dwarf2_symbol_mark_computed (attr, sym, cu);
3252    SYMBOL_CLASS (sym) = LOC_COMPUTED;
3253 +  dwarf2_symbol_mark_computed (attr, sym, cu);
3254  
3255    if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
3256      cu->has_loclist = 1;
3257 @@ -14578,6 +15019,8 @@ new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
3258        else
3259         sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
3260        OBJSTAT (objfile, n_syms++);
3261 +      /* Some methods are called w/o checking SYMBOL_COMPUTED_OPS validity.  */
3262 +      SYMBOL_COMPUTED_OPS (sym) = &dwarf2_missing_funcs;
3263  
3264        /* Cache this symbol's name and the name's demangled form (if any).  */
3265        SYMBOL_SET_LANGUAGE (sym, cu->language);
3266 @@ -15001,6 +15444,7 @@ dwarf2_const_value_attr (struct attribute *attr, struct type *type,
3267      case DW_FORM_string:
3268      case DW_FORM_strp:
3269      case DW_FORM_GNU_str_index:
3270 +    case DW_FORM_GNU_strp_alt:
3271        /* DW_STRING is already allocated on the objfile obstack, point
3272          directly to it.  */
3273        *bytes = (gdb_byte *) DW_STRING (attr);
3274 @@ -15187,7 +15631,15 @@ lookup_die_type (struct die_info *die, struct attribute *attr,
3275  
3276    /* First see if we have it cached.  */
3277  
3278 -  if (is_ref_attr (attr))
3279 +  if (attr->form == DW_FORM_GNU_ref_alt)
3280 +    {
3281 +      struct dwarf2_per_cu_data *per_cu;
3282 +      sect_offset offset = dwarf2_get_ref_die_offset (attr);
3283 +
3284 +      per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
3285 +      this_type = get_die_type_at_offset (offset, per_cu);
3286 +    }
3287 +  else if (is_ref_attr (attr))
3288      {
3289        sect_offset offset = dwarf2_get_ref_die_offset (attr);
3290  
3291 @@ -15352,6 +15804,9 @@ read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
3292        break;
3293      }
3294  
3295 +  if (this_type)
3296 +    finalize_type (this_type);
3297 +
3298    return this_type;
3299  }
3300  
3301 @@ -15960,6 +16415,10 @@ dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
3302           fprintf_unfiltered (f, "ref address: ");
3303           fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
3304           break;
3305 +       case DW_FORM_GNU_ref_alt:
3306 +         fprintf_unfiltered (f, "alt ref address: ");
3307 +         fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
3308 +         break;
3309         case DW_FORM_ref1:
3310         case DW_FORM_ref2:
3311         case DW_FORM_ref4:
3312 @@ -15991,6 +16450,7 @@ dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
3313         case DW_FORM_string:
3314         case DW_FORM_strp:
3315         case DW_FORM_GNU_str_index:
3316 +       case DW_FORM_GNU_strp_alt:
3317           fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
3318                    DW_STRING (&die->attrs[i])
3319                    ? DW_STRING (&die->attrs[i]) : "",
3320 @@ -16094,6 +16554,7 @@ is_ref_attr (struct attribute *attr)
3321      case DW_FORM_ref4:
3322      case DW_FORM_ref8:
3323      case DW_FORM_ref_udata:
3324 +    case DW_FORM_GNU_ref_alt:
3325        return 1;
3326      default:
3327        return 0;
3328 @@ -16171,7 +16632,8 @@ follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
3329     Returns NULL if OFFSET is invalid.  */
3330  
3331  static struct die_info *
3332 -follow_die_offset (sect_offset offset, struct dwarf2_cu **ref_cu)
3333 +follow_die_offset (sect_offset offset, int offset_in_dwz,
3334 +                  struct dwarf2_cu **ref_cu)
3335  {
3336    struct die_info temp_die;
3337    struct dwarf2_cu *target_cu, *cu = *ref_cu;
3338 @@ -16188,11 +16650,13 @@ follow_die_offset (sect_offset offset, struct dwarf2_cu **ref_cu)
3339        if (! offset_in_cu_p (&cu->header, offset))
3340         return NULL;
3341      }
3342 -  else if (! offset_in_cu_p (&cu->header, offset))
3343 +  else if (offset_in_dwz != cu->per_cu->is_dwz
3344 +          || ! offset_in_cu_p (&cu->header, offset))
3345      {
3346        struct dwarf2_per_cu_data *per_cu;
3347  
3348 -      per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
3349 +      per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
3350 +                                                cu->objfile);
3351  
3352        /* If necessary, add it to the queue and load its DIEs.  */
3353        if (maybe_queue_comp_unit (cu, per_cu, cu->language))
3354 @@ -16224,7 +16688,10 @@ follow_die_ref (struct die_info *src_die, struct attribute *attr,
3355    struct dwarf2_cu *cu = *ref_cu;
3356    struct die_info *die;
3357  
3358 -  die = follow_die_offset (offset, ref_cu);
3359 +  die = follow_die_offset (offset,
3360 +                          (attr->form == DW_FORM_GNU_ref_alt
3361 +                           || cu->per_cu->is_dwz),
3362 +                          ref_cu);
3363    if (!die)
3364      error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
3365            "at 0x%x [in module %s]"),
3366 @@ -16255,7 +16722,7 @@ dwarf2_fetch_die_location_block (cu_offset offset_in_cu,
3367      load_cu (per_cu);
3368    cu = per_cu->cu;
3369  
3370 -  die = follow_die_offset (offset, &cu);
3371 +  die = follow_die_offset (offset, per_cu->is_dwz, &cu);
3372    if (!die)
3373      error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
3374            offset.sect_off, per_cu->objfile->name);
3375 @@ -17049,6 +17516,7 @@ skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
3376  
3377      case DW_FORM_sec_offset:
3378      case DW_FORM_strp:
3379 +    case DW_FORM_GNU_strp_alt:
3380        bytes += offset_size;
3381        break;
3382  
3383 @@ -17204,7 +17672,7 @@ dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
3384                           struct macro_source_file *current_file,
3385                           struct line_header *lh, char *comp_dir,
3386                           struct dwarf2_section_info *section,
3387 -                         int section_is_gnu,
3388 +                         int section_is_gnu, int section_is_dwz,
3389                           unsigned int offset_size,
3390                           struct objfile *objfile,
3391                           htab_t include_hash)
3392 @@ -17255,6 +17723,8 @@ dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
3393          case DW_MACRO_GNU_undef:
3394         case DW_MACRO_GNU_define_indirect:
3395         case DW_MACRO_GNU_undef_indirect:
3396 +       case DW_MACRO_GNU_define_indirect_alt:
3397 +       case DW_MACRO_GNU_undef_indirect_alt:
3398            {
3399              unsigned int bytes_read;
3400              int line;
3401 @@ -17277,11 +17747,21 @@ dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
3402                 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
3403                 mac_ptr += offset_size;
3404  
3405 -               body = read_indirect_string_at_offset (abfd, str_offset);
3406 +               if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
3407 +                   || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
3408 +                   || section_is_dwz)
3409 +                 {
3410 +                   struct dwz_file *dwz = dwarf2_get_dwz_file ();
3411 +
3412 +                   body = read_indirect_string_from_dwz (dwz, str_offset);
3413 +                 }
3414 +               else
3415 +                 body = read_indirect_string_at_offset (abfd, str_offset);
3416               }
3417  
3418             is_define = (macinfo_type == DW_MACRO_GNU_define
3419 -                        || macinfo_type == DW_MACRO_GNU_define_indirect);
3420 +                        || macinfo_type == DW_MACRO_GNU_define_indirect
3421 +                        || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
3422              if (! current_file)
3423               {
3424                 /* DWARF violation as no main source is present.  */
3425 @@ -17305,7 +17785,8 @@ dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
3426             else
3427               {
3428                 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
3429 -                           || macinfo_type == DW_MACRO_GNU_undef_indirect);
3430 +                           || macinfo_type == DW_MACRO_GNU_undef_indirect
3431 +                           || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
3432                 macro_undef (current_file, line, body);
3433               }
3434            }
3435 @@ -17380,6 +17861,7 @@ dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
3436            break;
3437  
3438         case DW_MACRO_GNU_transparent_include:
3439 +       case DW_MACRO_GNU_transparent_include_alt:
3440           {
3441             LONGEST offset;
3442             void **slot;
3443 @@ -17398,13 +17880,32 @@ dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
3444               }
3445             else
3446               {
3447 +               bfd *include_bfd = abfd;
3448 +               struct dwarf2_section_info *include_section = section;
3449 +               struct dwarf2_section_info alt_section;
3450 +               gdb_byte *include_mac_end = mac_end;
3451 +               int is_dwz = section_is_dwz;
3452 +
3453                 *slot = mac_ptr;
3454  
3455 -               dwarf_decode_macro_bytes (abfd,
3456 -                                         section->buffer + offset,
3457 -                                         mac_end, current_file,
3458 +               if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
3459 +                 {
3460 +                   struct dwz_file *dwz = dwarf2_get_dwz_file ();
3461 +
3462 +                   dwarf2_read_section (dwarf2_per_objfile->objfile,
3463 +                                        &dwz->macro);
3464 +
3465 +                   include_bfd = dwz->macro.asection->owner;
3466 +                   include_section = &dwz->macro;
3467 +                   include_mac_end = dwz->macro.buffer + dwz->macro.size;
3468 +                   is_dwz = 1;
3469 +                 }
3470 +
3471 +               dwarf_decode_macro_bytes (include_bfd,
3472 +                                         include_section->buffer + offset,
3473 +                                         include_mac_end, current_file,
3474                                           lh, comp_dir,
3475 -                                         section, section_is_gnu,
3476 +                                         section, section_is_gnu, is_dwz,
3477                                           offset_size, objfile, include_hash);
3478  
3479                 htab_remove_elt (include_hash, mac_ptr);
3480 @@ -17571,6 +18072,8 @@ dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
3481  
3482         case DW_MACRO_GNU_define_indirect:
3483         case DW_MACRO_GNU_undef_indirect:
3484 +       case DW_MACRO_GNU_define_indirect_alt:
3485 +       case DW_MACRO_GNU_undef_indirect_alt:
3486           {
3487             unsigned int bytes_read;
3488  
3489 @@ -17581,6 +18084,7 @@ dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
3490           break;
3491  
3492         case DW_MACRO_GNU_transparent_include:
3493 +       case DW_MACRO_GNU_transparent_include_alt:
3494           /* Note that, according to the spec, a transparent include
3495              chain cannot call DW_MACRO_GNU_start_file.  So, we can just
3496              skip this opcode.  */
3497 @@ -17623,7 +18127,8 @@ dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
3498    slot = htab_find_slot (include_hash, mac_ptr, INSERT);
3499    *slot = mac_ptr;
3500    dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
3501 -                           current_file, lh, comp_dir, section, section_is_gnu,
3502 +                           current_file, lh, comp_dir, section,
3503 +                           section_is_gnu, 0,
3504                             offset_size, objfile, include_hash);
3505    do_cleanups (cleanup);
3506  }
3507 @@ -17721,62 +18226,100 @@ fill_in_loclist_baton (struct dwarf2_cu *cu,
3508    baton->from_dwo = cu->dwo_unit != NULL;
3509  }
3510  
3511 -static void
3512 -dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
3513 -                            struct dwarf2_cu *cu)
3514 +/* Convert DW_BLOCK into struct dwarf2_locexpr_baton.  ATTR must be a DW_BLOCK
3515 +   attribute type.  */
3516 +
3517 +static struct dwarf2_locexpr_baton *
3518 +dwarf2_attr_to_locexpr_baton (struct attribute *attr, struct dwarf2_cu *cu)
3519 +{
3520 +  struct objfile *objfile = dwarf2_per_objfile->objfile;
3521 +  struct dwarf2_locexpr_baton *baton;
3522 +
3523 +  gdb_assert (attr_form_is_block (attr));
3524 +
3525 +  baton = obstack_alloc (&objfile->objfile_obstack, sizeof (*baton));
3526 +  baton->per_cu = cu->per_cu;
3527 +  gdb_assert (baton->per_cu);
3528 +
3529 +  /* Note that we're just copying the block's data pointer
3530 +     here, not the actual data.  We're still pointing into the
3531 +     info_buffer for SYM's objfile; right now we never release
3532 +     that buffer, but when we do clean up properly this may
3533 +     need to change.  */
3534 +  baton->size = DW_BLOCK (attr)->size;
3535 +  baton->data = DW_BLOCK (attr)->data;
3536 +  gdb_assert (baton->size == 0 || baton->data != NULL);
3537 +
3538 +  return baton;
3539 +}
3540 +
3541 +static struct dwarf2_loclist_baton *
3542 +dwarf2_attr_to_loclist_baton (struct attribute *attr, struct dwarf2_cu *cu)
3543  {
3544    struct objfile *objfile = dwarf2_per_objfile->objfile;
3545    struct dwarf2_section_info *section = cu_debug_loc_section (cu);
3546 +  struct dwarf2_loclist_baton *baton;
3547 +
3548 +  /* DW_AT_location of the referenced DIE may be missing if the referenced
3549 +     variable has been optimized out.  */
3550 +  if (!attr)
3551 +    return NULL;
3552 +
3553 +  dwarf2_read_section (dwarf2_per_objfile->objfile, section);
3554  
3555 -  if (attr_form_is_section_offset (attr)
3556 +  if (!(attr_form_is_section_offset (attr)
3557        /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
3558          the section.  If so, fall through to the complaint in the
3559          other branch.  */
3560 -      && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
3561 -    {
3562 -      struct dwarf2_loclist_baton *baton;
3563 +      && DW_UNSND (attr) < dwarf2_section_size (objfile, section)))
3564 +    return NULL;
3565  
3566 -      baton = obstack_alloc (&objfile->objfile_obstack,
3567 -                            sizeof (struct dwarf2_loclist_baton));
3568 +  baton = obstack_alloc (&objfile->objfile_obstack,
3569 +                        sizeof (struct dwarf2_loclist_baton));
3570  
3571 -      fill_in_loclist_baton (cu, baton, attr);
3572 +  fill_in_loclist_baton (cu, baton, attr);
3573  
3574 -      if (cu->base_known == 0)
3575 -       complaint (&symfile_complaints,
3576 -                  _("Location list used without "
3577 -                    "specifying the CU base address."));
3578 +  if (cu->base_known == 0)
3579 +    complaint (&symfile_complaints,
3580 +              _("Location list used without "
3581 +                "specifying the CU base address."));
3582 +
3583 +  return baton;
3584 +}
3585 +
3586 +/* SYM may get its SYMBOL_CLASS overriden on invalid ATTR content.  */
3587 +
3588 +static void
3589 +dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
3590 +                            struct dwarf2_cu *cu)
3591 +{
3592 +  struct dwarf2_loclist_baton *loclist_baton;
3593  
3594 +  loclist_baton = dwarf2_attr_to_loclist_baton (attr, cu);
3595 +  if (loclist_baton)
3596 +    {
3597        SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
3598 -      SYMBOL_LOCATION_BATON (sym) = baton;
3599 +      SYMBOL_LOCATION_BATON (sym) = loclist_baton;
3600 +    }
3601 +  else if (attr_form_is_block (attr))
3602 +    {
3603 +      SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
3604 +      SYMBOL_LOCATION_BATON (sym) = dwarf2_attr_to_locexpr_baton (attr, cu);
3605      }
3606    else
3607      {
3608 -      struct dwarf2_locexpr_baton *baton;
3609 +      dwarf2_invalid_attrib_class_complaint ("location description",
3610 +                                            SYMBOL_NATURAL_NAME (sym));
3611  
3612 -      baton = obstack_alloc (&objfile->objfile_obstack,
3613 -                            sizeof (struct dwarf2_locexpr_baton));
3614 -      baton->per_cu = cu->per_cu;
3615 -      gdb_assert (baton->per_cu);
3616 +      /* Some methods are called w/o checking SYMBOL_COMPUTED_OPS validity.  */
3617  
3618 -      if (attr_form_is_block (attr))
3619 -       {
3620 -         /* Note that we're just copying the block's data pointer
3621 -            here, not the actual data.  We're still pointing into the
3622 -            info_buffer for SYM's objfile; right now we never release
3623 -            that buffer, but when we do clean up properly this may
3624 -            need to change.  */
3625 -         baton->size = DW_BLOCK (attr)->size;
3626 -         baton->data = DW_BLOCK (attr)->data;
3627 -       }
3628 -      else
3629 -       {
3630 -         dwarf2_invalid_attrib_class_complaint ("location description",
3631 -                                                SYMBOL_NATURAL_NAME (sym));
3632 -         baton->size = 0;
3633 -       }
3634 +      SYMBOL_COMPUTED_OPS (sym) = &dwarf2_missing_funcs;
3635 +      SYMBOL_LOCATION_BATON (sym) = NULL;
3636  
3637 -      SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
3638 -      SYMBOL_LOCATION_BATON (sym) = baton;
3639 +      /* For functions a missing DW_AT_frame_base does not optimize out the
3640 +        whole function definition, only its frame base resolving.  */
3641 +      if (attr->name == DW_AT_location)
3642 +       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
3643      }
3644  }
3645  
3646 @@ -17878,28 +18421,35 @@ dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
3647  
3648  static struct dwarf2_per_cu_data *
3649  dwarf2_find_containing_comp_unit (sect_offset offset,
3650 +                                 unsigned int offset_in_dwz,
3651                                   struct objfile *objfile)
3652  {
3653    struct dwarf2_per_cu_data *this_cu;
3654    int low, high;
3655 +  const sect_offset *cu_off;
3656  
3657    low = 0;
3658    high = dwarf2_per_objfile->n_comp_units - 1;
3659    while (high > low)
3660      {
3661 +      struct dwarf2_per_cu_data *mid_cu;
3662        int mid = low + (high - low) / 2;
3663  
3664 -      if (dwarf2_per_objfile->all_comp_units[mid]->offset.sect_off
3665 -         >= offset.sect_off)
3666 +      mid_cu = dwarf2_per_objfile->all_comp_units[mid];
3667 +      cu_off = &mid_cu->offset;
3668 +      if (mid_cu->is_dwz > offset_in_dwz
3669 +         || (mid_cu->is_dwz == offset_in_dwz
3670 +             && cu_off->sect_off >= offset.sect_off))
3671         high = mid;
3672        else
3673         low = mid + 1;
3674      }
3675    gdb_assert (low == high);
3676 -  if (dwarf2_per_objfile->all_comp_units[low]->offset.sect_off
3677 -      > offset.sect_off)
3678 +  this_cu = dwarf2_per_objfile->all_comp_units[low];
3679 +  cu_off = &this_cu->offset;
3680 +  if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
3681      {
3682 -      if (low == 0)
3683 +      if (low == 0 || this_cu->is_dwz != offset_in_dwz)
3684         error (_("Dwarf Error: could not find partial DIE containing "
3685                "offset 0x%lx [in module %s]"),
3686                (long) offset.sect_off, bfd_get_filename (objfile->obfd));
3687 @@ -18140,6 +18690,25 @@ per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
3688           && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
3689  }
3690  
3691 +/* Fill in generic attributes applicable for type DIEs.  */
3692 +
3693 +static void
3694 +fetch_die_type_attrs (struct die_info *die, struct type *type,
3695 +                     struct dwarf2_cu *cu)
3696 +{
3697 +  struct attribute *attr;
3698 +
3699 +  attr = dwarf2_attr (die, DW_AT_allocated, cu);
3700 +  if (attr_form_is_block (attr))
3701 +    TYPE_ALLOCATED (type) = dwarf2_attr_to_locexpr_baton (attr, cu);
3702 +  gdb_assert (!TYPE_NOT_ALLOCATED (type));
3703 +
3704 +  attr = dwarf2_attr (die, DW_AT_associated, cu);
3705 +  if (attr_form_is_block (attr))
3706 +    TYPE_ASSOCIATED (type) = dwarf2_attr_to_locexpr_baton (attr, cu);
3707 +  gdb_assert (!TYPE_NOT_ASSOCIATED (type));
3708 +}
3709 +
3710  /* Set the type associated with DIE to TYPE.  Save it in CU's hash
3711     table if necessary.  For convenience, return TYPE.
3712  
3713 @@ -18164,6 +18733,8 @@ set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
3714    struct dwarf2_per_cu_offset_and_type **slot, ofs;
3715    struct objfile *objfile = cu->objfile;
3716  
3717 +  fetch_die_type_attrs (die, type, cu);
3718 +
3719    /* For Ada types, make sure that the gnat-specific data is always
3720       initialized (if not already set).  There are a few types where
3721       we should not be doing so, because the type-specific area is
3722 @@ -18341,53 +18912,13 @@ show_dwarf2_cmd (char *args, int from_tty)
3723    cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
3724  }
3725  
3726 -/* If section described by INFO was mmapped, munmap it now.  */
3727 -
3728 -static void
3729 -munmap_section_buffer (struct dwarf2_section_info *info)
3730 -{
3731 -  if (info->map_addr != NULL)
3732 -    {
3733 -#ifdef HAVE_MMAP
3734 -      int res;
3735 -
3736 -      res = munmap (info->map_addr, info->map_len);
3737 -      gdb_assert (res == 0);
3738 -#else
3739 -      /* Without HAVE_MMAP, we should never be here to begin with.  */
3740 -      gdb_assert_not_reached ("no mmap support");
3741 -#endif
3742 -    }
3743 -}
3744 -
3745 -/* munmap debug sections for OBJFILE, if necessary.  */
3746 +/* Free data associated with OBJFILE, if necessary.  */
3747  
3748  static void
3749  dwarf2_per_objfile_free (struct objfile *objfile, void *d)
3750  {
3751    struct dwarf2_per_objfile *data = d;
3752    int ix;
3753 -  struct dwarf2_section_info *section;
3754 -
3755 -  /* This is sorted according to the order they're defined in to make it easier
3756 -     to keep in sync.  */
3757 -  munmap_section_buffer (&data->info);
3758 -  munmap_section_buffer (&data->abbrev);
3759 -  munmap_section_buffer (&data->line);
3760 -  munmap_section_buffer (&data->loc);
3761 -  munmap_section_buffer (&data->macinfo);
3762 -  munmap_section_buffer (&data->macro);
3763 -  munmap_section_buffer (&data->str);
3764 -  munmap_section_buffer (&data->ranges);
3765 -  munmap_section_buffer (&data->addr);
3766 -  munmap_section_buffer (&data->frame);
3767 -  munmap_section_buffer (&data->eh_frame);
3768 -  munmap_section_buffer (&data->gdb_index);
3769 -
3770 -  for (ix = 0;
3771 -       VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
3772 -       ++ix)
3773 -    munmap_section_buffer (section);
3774  
3775    for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
3776      VEC_free (dwarf2_per_cu_ptr,
3777 @@ -18397,6 +18928,9 @@ dwarf2_per_objfile_free (struct objfile *objfile, void *d)
3778  
3779    if (data->dwo_files)
3780      free_dwo_files (data->dwo_files, objfile);
3781 +
3782 +  if (data->dwz_file && data->dwz_file->dwz_bfd)
3783 +    gdb_bfd_unref (data->dwz_file->dwz_bfd);
3784  }
3785  
3786  \f
3787 diff --git a/gdb/elfread.c b/gdb/elfread.c
3788 index 1edfb27..0b54b8a 100644
3789 --- a/gdb/elfread.c
3790 +++ b/gdb/elfread.c
3791 @@ -44,6 +44,7 @@
3792  #include "gdbthread.h"
3793  #include "regcache.h"
3794  #include "bcache.h"
3795 +#include "gdb_bfd.h"
3796  
3797  extern void _initialize_elfread (void);
3798  
3799 @@ -1108,7 +1109,7 @@ build_id_verify (const char *filename, struct build_id *check)
3800    int retval = 0;
3801  
3802    /* We expect to be silent on the non-existing files.  */
3803 -  abfd = bfd_open_maybe_remote (filename);
3804 +  abfd = gdb_bfd_open_maybe_remote (filename);
3805    if (abfd == NULL)
3806      return 0;
3807  
3808 @@ -1123,7 +1124,7 @@ build_id_verify (const char *filename, struct build_id *check)
3809    else
3810      retval = 1;
3811  
3812 -  gdb_bfd_close_or_warn (abfd);
3813 +  gdb_bfd_unref (abfd);
3814  
3815    xfree (found);
3816  
3817 @@ -1444,10 +1445,12 @@ elf_symfile_read (struct objfile *objfile, int symfile_flags)
3818  
3819        if (debugfile)
3820         {
3821 +         struct cleanup *cleanup = make_cleanup (xfree, debugfile);
3822           bfd *abfd = symfile_bfd_open (debugfile);
3823  
3824 +         make_cleanup_bfd_unref (abfd);
3825           symbol_file_add_separate (abfd, symfile_flags, objfile);
3826 -         xfree (debugfile);
3827 +         do_cleanups (cleanup);
3828         }
3829      }
3830  
3831 diff --git a/gdb/eval.c b/gdb/eval.c
3832 index 7d3a8b9..f69dff0 100644
3833 --- a/gdb/eval.c
3834 +++ b/gdb/eval.c
3835 @@ -41,6 +41,7 @@
3836  #include "gdb_obstack.h"
3837  #include "objfiles.h"
3838  #include "python/python.h"
3839 +#include "dwarf2loc.h"
3840  
3841  #include "gdb_assert.h"
3842  
3843 @@ -500,27 +501,217 @@ init_array_element (struct value *array, struct value *element,
3844  }
3845  
3846  static struct value *
3847 -value_f90_subarray (struct value *array,
3848 -                   struct expression *exp, int *pos, enum noside noside)
3849 +value_f90_subarray (struct value *array, struct expression *exp, int *pos,
3850 +                   int nargs, enum noside noside)
3851  {
3852 -  int pc = (*pos) + 1;
3853 -  LONGEST low_bound, high_bound;
3854 -  struct type *range = check_typedef (TYPE_INDEX_TYPE (value_type (array)));
3855 -  enum f90_range_type range_type = longest_to_int (exp->elts[pc].longconst);
3856
3857 -  *pos += 3;
3858 -
3859 -  if (range_type == LOW_BOUND_DEFAULT || range_type == BOTH_BOUND_DEFAULT)
3860 -    low_bound = TYPE_LOW_BOUND (range);
3861 +  /* Type to use for the newly allocated value ARRAY.  */
3862 +  struct type *new_array_type;
3863 +
3864 +  /* Type being iterated for each dimension.  */
3865 +  struct type *type, *type_last_target;
3866 +
3867 +  /* Pointer in the last holder to the type of current dimension.  */
3868 +  struct type **typep = &new_array_type;
3869 +
3870 +  struct subscript_index
3871 +    {
3872 +      enum { SUBSCRIPT_RANGE, SUBSCRIPT_NUMBER } kind;
3873 +      union
3874 +       {
3875 +         struct subscript_range
3876 +           {
3877 +             enum f90_range_type f90_range_type;
3878 +             LONGEST low_bound, high_bound;
3879 +           }
3880 +         range;
3881 +         LONGEST number;
3882 +       };
3883 +    }
3884 +  *subscript_array;
3885 +  struct type **type_array;
3886 +  int i;
3887 +  struct cleanup *old_chain;
3888 +  CORE_ADDR value_byte_address, value_byte_offset = 0;
3889 +  htab_t copied_types;
3890 +  struct value *saved_array;
3891 +
3892 +  old_chain = make_cleanup (null_cleanup, 0);
3893 +  object_address_set (value_raw_address (array));
3894 +
3895 +  if (value_optimized_out (array)
3896 +      || (VALUE_LVAL (array) != not_lval
3897 +          && VALUE_LVAL (array) != lval_memory
3898 +         && VALUE_LVAL (array) != lval_internalvar_component
3899 +         && VALUE_LVAL (array) != lval_internalvar))
3900 +    error (_("value being subranged must be in memory"));
3901 +  type = check_typedef (value_type (array));
3902 +  f_object_address_data_valid_or_error (type);
3903 +
3904 +  copied_types = create_copied_types_hash (NULL);
3905 +  type = copy_type_recursive (type, copied_types);
3906 +  htab_delete (copied_types);
3907 +
3908 +  if (nargs != calc_f77_array_dims (type))
3909 +    error (_("Wrong number of subscripts"));
3910 +
3911 +  if (TYPE_DATA_LOCATION_IS_ADDR (type))
3912 +    {
3913 +      value_byte_address = (TYPE_DATA_LOCATION_ADDR (type)
3914 +                           + value_offset (array));
3915 +      TYPE_DATA_LOCATION_IS_ADDR (type) = 0;
3916 +      TYPE_DATA_LOCATION_DWARF_BLOCK (type) = NULL;
3917 +    }
3918    else
3919 -    low_bound = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside));
3920 +    {
3921 +      gdb_assert (TYPE_DATA_LOCATION_DWARF_BLOCK (type) == NULL);
3922 +      value_byte_address = value_address (array);
3923 +    }
3924 +
3925 +  new_array_type = type;
3926 +
3927 +  subscript_array = alloca (sizeof (*subscript_array) * nargs);
3928 +
3929 +  gdb_assert (nargs > 0);
3930 +
3931 +  /* Now that we know we have a legal array subscript expression 
3932 +     let us actually find out where this element exists in the array.  */
3933 +
3934 +  /* Take array indices left to right.  */
3935 +  for (i = 0; i < nargs; i++)
3936 +    {
3937 +      struct subscript_index *index = &subscript_array[i];
3938 +
3939 +      if (exp->elts[*pos].opcode == OP_F90_RANGE)
3940 +       {
3941 +         int pc = (*pos) + 1;
3942 +         struct subscript_range *range;
3943 +
3944 +         index->kind = SUBSCRIPT_RANGE;
3945 +         range = &index->range;
3946 +
3947 +         *pos += 3;
3948 +         range->f90_range_type = longest_to_int (exp->elts[pc].longconst);
3949 +
3950 +         if (range->f90_range_type == HIGH_BOUND_DEFAULT
3951 +             || range->f90_range_type == NONE_BOUND_DEFAULT)
3952 +           range->low_bound = value_as_long (evaluate_subexp (NULL_TYPE, exp,
3953 +                                                              pos, noside));
3954 +
3955 +         if (range->f90_range_type == LOW_BOUND_DEFAULT
3956 +             || range->f90_range_type == NONE_BOUND_DEFAULT)
3957 +           range->high_bound = value_as_long (evaluate_subexp (NULL_TYPE, exp,
3958 +                                                               pos, noside));
3959 +       }
3960 +      else
3961 +       {
3962 +         struct value *val;
3963 +
3964 +         index->kind = SUBSCRIPT_NUMBER;
3965  
3966 -  if (range_type == HIGH_BOUND_DEFAULT || range_type == BOTH_BOUND_DEFAULT)
3967 -    high_bound = TYPE_HIGH_BOUND (range);
3968 +         /* Evaluate each subscript; it must be a legal integer in F77.  */
3969 +         val = evaluate_subexp_with_coercion (exp, pos, noside);
3970 +         index->number = value_as_long (val);
3971 +       }
3972 +    }
3973 +
3974 +  /* Internal type of array is arranged right to left.  */
3975 +  for (i = nargs - 1; i >= 0; i--)
3976 +    {
3977 +      struct subscript_index *index = &subscript_array[i];
3978 +      struct type *range_type = TYPE_INDEX_TYPE (type);
3979 +
3980 +      switch (index->kind)
3981 +       {
3982 +       case SUBSCRIPT_RANGE:
3983 +         {
3984 +           struct subscript_range *range = &index->range;
3985 +           CORE_ADDR byte_offset;
3986 +
3987 +           if (range->f90_range_type == LOW_BOUND_DEFAULT
3988 +               || range->f90_range_type == BOTH_BOUND_DEFAULT)
3989 +             range->low_bound = TYPE_LOW_BOUND (range_type);
3990 +
3991 +           if (range->f90_range_type == HIGH_BOUND_DEFAULT
3992 +               || range->f90_range_type == BOTH_BOUND_DEFAULT)
3993 +             range->high_bound = TYPE_HIGH_BOUND (range_type);
3994 +
3995 +           if (range->low_bound < TYPE_LOW_BOUND (range_type)
3996 +               || (!TYPE_HIGH_BOUND_UNDEFINED (range_type)
3997 +                   && range->high_bound > TYPE_HIGH_BOUND (range_type)))
3998 +             error (_("slice out of range"));
3999 +
4000 +           byte_offset = ((range->low_bound - TYPE_LOW_BOUND (range_type))
4001 +                          * TYPE_ARRAY_BYTE_STRIDE_VALUE (type));
4002 +           TYPE_LOW_BOUND (range_type) = range->low_bound;
4003 +           TYPE_HIGH_BOUND (range_type) = range->high_bound;
4004 +           if (range->f90_range_type == LOW_BOUND_DEFAULT
4005 +               || range->f90_range_type == NONE_BOUND_DEFAULT)
4006 +             TYPE_HIGH_BOUND_UNDEFINED (range_type) = 0;
4007 +
4008 +           typep = &TYPE_TARGET_TYPE (type);
4009 +           value_byte_offset += byte_offset;
4010 +           type = TYPE_TARGET_TYPE (type);
4011 +         }
4012 +         break;
4013 +
4014 +       case SUBSCRIPT_NUMBER:
4015 +         {
4016 +           CORE_ADDR byte_offset;
4017 +
4018 +           if (index->number < TYPE_LOW_BOUND (range_type)
4019 +               || (!TYPE_HIGH_BOUND_UNDEFINED (range_type)
4020 +                   && index->number > TYPE_HIGH_BOUND (range_type)))
4021 +             error (_("no such vector element"));
4022 +
4023 +           byte_offset = ((index->number - TYPE_LOW_BOUND (range_type))
4024 +                          * TYPE_ARRAY_BYTE_STRIDE_VALUE (type));
4025 +
4026 +           type = TYPE_TARGET_TYPE (type);
4027 +           *typep = type;
4028 +           value_byte_offset += byte_offset;
4029 +         }
4030 +         break;
4031 +       }
4032 +    }
4033 +
4034 +  type_last_target = type;
4035 +  type_array = alloca (sizeof (*type_array) * nargs);
4036 +  i = 0;
4037 +  for (type = new_array_type; type != type_last_target;
4038 +       type = TYPE_TARGET_TYPE (type))
4039 +    type_array[i++] = type;
4040 +  while (i > 0)
4041 +    {
4042 +      struct type *type = type_array[--i];
4043 +
4044 +      /* Force TYPE_LENGTH (type) recalculation.  */
4045 +      TYPE_TARGET_STUB (type) = 1;
4046 +      check_typedef (type);
4047 +    }
4048 +
4049 +  saved_array = array;
4050 +  array = allocate_value_lazy (new_array_type);
4051 +  VALUE_LVAL (array) = VALUE_LVAL (saved_array);
4052 +  if (VALUE_LVAL (saved_array) == lval_internalvar_component)
4053 +    VALUE_LVAL (array) = lval_internalvar;
4054    else
4055 -    high_bound = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside));
4056 +    VALUE_LVAL (array) = VALUE_LVAL (saved_array);
4057 +  VALUE_FRAME_ID (array) = VALUE_FRAME_ID (saved_array);
4058 +  if (VALUE_LVAL (array) != lval_internalvar)
4059 +    set_value_address (array, value_byte_address + value_byte_offset);
4060 +
4061 +  if (!value_lazy (saved_array))
4062 +    {
4063 +      allocate_value_contents (array);
4064 +      set_value_lazy (array, 0);
4065  
4066 -  return value_slice (array, low_bound, high_bound - low_bound + 1);
4067 +      memcpy (value_contents_writeable (array),
4068 +             value_contents (saved_array) + value_byte_offset,
4069 +             TYPE_LENGTH (new_array_type));
4070 +    }
4071 +
4072 +  do_cleanups (old_chain);
4073 +  return array;
4074  }
4075  
4076  
4077 @@ -818,6 +1009,7 @@ evaluate_subexp_standard (struct type *expect_type,
4078    int save_pos1;
4079    struct symbol *function = NULL;
4080    char *function_name = NULL;
4081 +  struct cleanup *old_chain;
4082  
4083    pc = (*pos)++;
4084    op = exp->elts[pc].opcode;
4085 @@ -1892,6 +2084,8 @@ evaluate_subexp_standard (struct type *expect_type,
4086  
4087        /* First determine the type code we are dealing with.  */
4088        arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
4089 +      old_chain = make_cleanup (null_cleanup, 0);
4090 +      object_address_set (value_raw_address (arg1));
4091        type = check_typedef (value_type (arg1));
4092        code = TYPE_CODE (type);
4093  
4094 @@ -1912,23 +2106,13 @@ evaluate_subexp_standard (struct type *expect_type,
4095               code = TYPE_CODE (type);
4096             }
4097         } 
4098 +      do_cleanups (old_chain);
4099  
4100        switch (code)
4101         {
4102         case TYPE_CODE_ARRAY:
4103 -         if (exp->elts[*pos].opcode == OP_F90_RANGE)
4104 -           return value_f90_subarray (arg1, exp, pos, noside);
4105 -         else
4106 -           goto multi_f77_subscript;
4107 -
4108         case TYPE_CODE_STRING:
4109 -         if (exp->elts[*pos].opcode == OP_F90_RANGE)
4110 -           return value_f90_subarray (arg1, exp, pos, noside);
4111 -         else
4112 -           {
4113 -             arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
4114 -             return value_subscript (arg1, value_as_long (arg2));
4115 -           }
4116 +         return value_f90_subarray (arg1, exp, pos, nargs, noside);
4117  
4118         case TYPE_CODE_PTR:
4119         case TYPE_CODE_FUNC:
4120 @@ -2361,49 +2545,6 @@ evaluate_subexp_standard (struct type *expect_type,
4121         }
4122        return (arg1);
4123  
4124 -    multi_f77_subscript:
4125 -      {
4126 -       LONGEST subscript_array[MAX_FORTRAN_DIMS];
4127 -       int ndimensions = 1, i;
4128 -       struct value *array = arg1;
4129 -
4130 -       if (nargs > MAX_FORTRAN_DIMS)
4131 -         error (_("Too many subscripts for F77 (%d Max)"), MAX_FORTRAN_DIMS);
4132 -
4133 -       ndimensions = calc_f77_array_dims (type);
4134 -
4135 -       if (nargs != ndimensions)
4136 -         error (_("Wrong number of subscripts"));
4137 -
4138 -       gdb_assert (nargs > 0);
4139 -
4140 -       /* Now that we know we have a legal array subscript expression 
4141 -          let us actually find out where this element exists in the array.  */
4142 -
4143 -       /* Take array indices left to right.  */
4144 -       for (i = 0; i < nargs; i++)
4145 -         {
4146 -           /* Evaluate each subscript; it must be a legal integer in F77.  */
4147 -           arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
4148 -
4149 -           /* Fill in the subscript array.  */
4150 -
4151 -           subscript_array[i] = value_as_long (arg2);
4152 -         }
4153 -
4154 -       /* Internal type of array is arranged right to left.  */
4155 -       for (i = nargs; i > 0; i--)
4156 -         {
4157 -           struct type *array_type = check_typedef (value_type (array));
4158 -           LONGEST index = subscript_array[i - 1];
4159 -
4160 -           lower = f77_get_lowerbound (array_type);
4161 -           array = value_subscripted_rvalue (array, index, lower);
4162 -         }
4163 -
4164 -       return array;
4165 -      }
4166 -
4167      case BINOP_LOGICAL_AND:
4168        arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
4169        if (noside == EVAL_SKIP)
4170 @@ -2635,15 +2776,23 @@ evaluate_subexp_standard (struct type *expect_type,
4171        if (expect_type && TYPE_CODE (expect_type) == TYPE_CODE_PTR)
4172         expect_type = TYPE_TARGET_TYPE (check_typedef (expect_type));
4173        arg1 = evaluate_subexp (expect_type, exp, pos, noside);
4174 +      old_chain = make_cleanup (null_cleanup, 0);
4175 +      object_address_set (value_raw_address (arg1));
4176        type = check_typedef (value_type (arg1));
4177        if (TYPE_CODE (type) == TYPE_CODE_METHODPTR
4178           || TYPE_CODE (type) == TYPE_CODE_MEMBERPTR)
4179         error (_("Attempt to dereference pointer "
4180                  "to member without an object"));
4181        if (noside == EVAL_SKIP)
4182 -       goto nosideret;
4183 +       {
4184 +         do_cleanups (old_chain);
4185 +         goto nosideret;
4186 +       }
4187        if (unop_user_defined_p (op, arg1))
4188 -       return value_x_unop (arg1, op, noside);
4189 +       {
4190 +         do_cleanups (old_chain);
4191 +         return value_x_unop (arg1, op, noside);
4192 +       }
4193        else if (noside == EVAL_AVOID_SIDE_EFFECTS)
4194         {
4195           type = check_typedef (value_type (arg1));
4196 @@ -2652,12 +2801,18 @@ evaluate_subexp_standard (struct type *expect_type,
4197           /* In C you can dereference an array to get the 1st elt.  */
4198               || TYPE_CODE (type) == TYPE_CODE_ARRAY
4199             )
4200 -           return value_zero (TYPE_TARGET_TYPE (type),
4201 -                              lval_memory);
4202 +           {
4203 +             do_cleanups (old_chain);
4204 +             return value_zero (TYPE_TARGET_TYPE (type),
4205 +                                lval_memory);
4206 +           }
4207           else if (TYPE_CODE (type) == TYPE_CODE_INT)
4208 -           /* GDB allows dereferencing an int.  */
4209 -           return value_zero (builtin_type (exp->gdbarch)->builtin_int,
4210 -                              lval_memory);
4211 +           {
4212 +             do_cleanups (old_chain);
4213 +             /* GDB allows dereferencing an int.  */
4214 +             return value_zero (builtin_type (exp->gdbarch)->builtin_int,
4215 +                                lval_memory);
4216 +           }
4217           else
4218             error (_("Attempt to take contents of a non-pointer value."));
4219         }
4220 @@ -2667,9 +2822,14 @@ evaluate_subexp_standard (struct type *expect_type,
4221          do.  "long long" variables are rare enough that
4222          BUILTIN_TYPE_LONGEST would seem to be a mistake.  */
4223        if (TYPE_CODE (type) == TYPE_CODE_INT)
4224 -       return value_at_lazy (builtin_type (exp->gdbarch)->builtin_int,
4225 -                             (CORE_ADDR) value_as_address (arg1));
4226 -      return value_ind (arg1);
4227 +       {
4228 +         do_cleanups (old_chain);
4229 +         return value_at_lazy (builtin_type (exp->gdbarch)->builtin_int,
4230 +                               (CORE_ADDR) value_as_address (arg1));
4231 +       }
4232 +      arg1 = value_ind (arg1);
4233 +      do_cleanups (old_chain);
4234 +      return arg1;
4235  
4236      case UNOP_ADDR:
4237        /* C++: check for and handle pointer to members.  */
4238 @@ -3011,7 +3171,7 @@ evaluate_subexp_with_coercion (struct expression *exp,
4239  {
4240    enum exp_opcode op;
4241    int pc;
4242 -  struct value *val;
4243 +  struct value *val = NULL;
4244    struct symbol *var;
4245    struct type *type;
4246  
4247 @@ -3022,13 +3182,18 @@ evaluate_subexp_with_coercion (struct expression *exp,
4248      {
4249      case OP_VAR_VALUE:
4250        var = exp->elts[pc + 2].symbol;
4251 +      /* address_of_variable will call object_address_set for check_typedef.
4252 +        Call it only if required as it can error-out on VAR in register.  */
4253 +      if (TYPE_DYNAMIC (SYMBOL_TYPE (var)))
4254 +       val = address_of_variable (var, exp->elts[pc + 1].block);
4255        type = check_typedef (SYMBOL_TYPE (var));
4256        if (TYPE_CODE (type) == TYPE_CODE_ARRAY
4257           && !TYPE_VECTOR (type)
4258           && CAST_IS_CONVERSION (exp->language_defn))
4259         {
4260           (*pos) += 4;
4261 -         val = address_of_variable (var, exp->elts[pc + 1].block);
4262 +         if (!val)
4263 +           val = address_of_variable (var, exp->elts[pc + 1].block);
4264           return value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
4265                              val);
4266         }
4267 @@ -3080,9 +3245,13 @@ evaluate_subexp_for_sizeof (struct expression *exp, int *pos)
4268  
4269      case OP_VAR_VALUE:
4270        (*pos) += 4;
4271 -      type = check_typedef (SYMBOL_TYPE (exp->elts[pc + 2].symbol));
4272 -      return
4273 -       value_from_longest (size_type, (LONGEST) TYPE_LENGTH (type));
4274 +      /* We do not need to call read_var_value but the object evaluation may
4275 +        need to have executed object_address_set which needs valid
4276 +        SYMBOL_VALUE_ADDRESS of the symbol.  Still VALUE returned by
4277 +        read_var_value we left as lazy.  */
4278 +      type = value_type (read_var_value (exp->elts[pc + 2].symbol,
4279 +                                       deprecated_safe_get_selected_frame ()));
4280 +      return value_from_longest (size_type, (LONGEST) TYPE_LENGTH (type));
4281  
4282      default:
4283        val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
4284 @@ -3113,18 +3282,25 @@ parse_and_eval_type (char *p, int length)
4285  int
4286  calc_f77_array_dims (struct type *array_type)
4287  {
4288 -  int ndimen = 1;
4289 -  struct type *tmp_type;
4290 +  switch (TYPE_CODE (array_type))
4291 +    {
4292 +    case TYPE_CODE_STRING:
4293 +      return 1;
4294  
4295 -  if ((TYPE_CODE (array_type) != TYPE_CODE_ARRAY))
4296 -    error (_("Can't get dimensions for a non-array type"));
4297 +    case TYPE_CODE_ARRAY:
4298 +      {
4299 +       int ndimen = 1;
4300  
4301 -  tmp_type = array_type;
4302 +       while ((array_type = TYPE_TARGET_TYPE (array_type)))
4303 +         {
4304 +           if (TYPE_CODE (array_type) == TYPE_CODE_ARRAY)
4305 +             ++ndimen;
4306 +         }
4307 +       return ndimen;
4308 +      }
4309  
4310 -  while ((tmp_type = TYPE_TARGET_TYPE (tmp_type)))
4311 -    {
4312 -      if (TYPE_CODE (tmp_type) == TYPE_CODE_ARRAY)
4313 -       ++ndimen;
4314 +    default:
4315 +      error (_("Can't get dimensions for a non-array/non-string type"));
4316      }
4317 -  return ndimen;
4318 +
4319  }
4320 diff --git a/gdb/exec.c b/gdb/exec.c
4321 index 6ba1986..e076609 100644
4322 --- a/gdb/exec.c
4323 +++ b/gdb/exec.c
4324 @@ -33,6 +33,7 @@
4325  #include "arch-utils.h"
4326  #include "gdbthread.h"
4327  #include "progspace.h"
4328 +#include "gdb_bfd.h"
4329  
4330  #include <fcntl.h>
4331  #include "readline/readline.h"
4332 @@ -98,10 +99,8 @@ exec_close (void)
4333    if (exec_bfd)
4334      {
4335        bfd *abfd = exec_bfd;
4336 -      char *name = bfd_get_filename (abfd);
4337  
4338 -      gdb_bfd_close_or_warn (abfd);
4339 -      xfree (name);
4340 +      gdb_bfd_unref (abfd);
4341  
4342        /* Removing target sections may close the exec_ops target.
4343          Clear exec_bfd before doing so to prevent recursion.  */
4344 @@ -128,17 +127,13 @@ exec_close_1 (int quitting)
4345        vp = nxt;
4346        nxt = vp->nxt;
4347  
4348 -      /* if there is an objfile associated with this bfd,
4349 -         free_objfile() will do proper cleanup of objfile *and* bfd.  */
4350 -
4351        if (vp->objfile)
4352         {
4353           free_objfile (vp->objfile);
4354           need_symtab_cleanup = 1;
4355         }
4356 -      else if (vp->bfd != exec_bfd)
4357 -       /* FIXME-leak: We should be freeing vp->name too, I think.  */
4358 -       gdb_bfd_close_or_warn (vp->bfd);
4359 +
4360 +      gdb_bfd_unref (vp->bfd);
4361  
4362        xfree (vp);
4363      }
4364 @@ -230,11 +225,14 @@ exec_file_attach (char *filename, int from_tty)
4365              &scratch_pathname);
4366         }
4367  #endif
4368 +
4369 +      cleanups = make_cleanup (xfree, scratch_pathname);
4370 +
4371        if (scratch_chan < 0)
4372         perror_with_name (filename);
4373 -      exec_bfd = bfd_fopen (scratch_pathname, gnutarget,
4374 -                           write_files ? FOPEN_RUB : FOPEN_RB,
4375 -                           scratch_chan);
4376 +      exec_bfd = gdb_bfd_fopen (scratch_pathname, gnutarget,
4377 +                               write_files ? FOPEN_RUB : FOPEN_RB,
4378 +                               scratch_chan);
4379  
4380        if (!exec_bfd)
4381         {
4382 @@ -242,13 +240,6 @@ exec_file_attach (char *filename, int from_tty)
4383                  scratch_pathname, bfd_errmsg (bfd_get_error ()));
4384         }
4385  
4386 -      /* At this point, scratch_pathname and exec_bfd->name both point to the
4387 -         same malloc'd string.  However exec_close() will attempt to free it
4388 -         via the exec_bfd->name pointer, so we need to make another copy and
4389 -         leave exec_bfd as the new owner of the original copy.  */
4390 -      scratch_pathname = xstrdup (scratch_pathname);
4391 -      cleanups = make_cleanup (xfree, scratch_pathname);
4392 -
4393        if (!bfd_check_format_matches (exec_bfd, bfd_object, &matching))
4394         {
4395           /* Make sure to close exec_bfd, or else "run" might try to use
4396 @@ -554,6 +545,7 @@ map_vmap (bfd *abfd, bfd *arch)
4397    memset ((char *) vp, '\0', sizeof (*vp));
4398    vp->nxt = 0;
4399    vp->bfd = abfd;
4400 +  gdb_bfd_ref (abfd);
4401    vp->name = bfd_get_filename (arch ? arch : abfd);
4402    vp->member = arch ? bfd_get_filename (abfd) : "";
4403  
4404 diff --git a/gdb/f-exp.y b/gdb/f-exp.y
4405 index 33c7418..4db1bfa 100644
4406 --- a/gdb/f-exp.y
4407 +++ b/gdb/f-exp.y
4408 @@ -298,7 +298,9 @@ arglist :   subrange
4409                         { arglist_len = 1; }
4410         ;
4411     
4412 -arglist        :       arglist ',' exp   %prec ABOVE_COMMA
4413 +arglist        :       arglist ',' exp       %prec ABOVE_COMMA
4414 +                       { arglist_len++; }
4415 +       |       arglist ',' subrange  %prec ABOVE_COMMA
4416                         { arglist_len++; }
4417         ;
4418  
4419 diff --git a/gdb/f-lang.h b/gdb/f-lang.h
4420 index 4aae3c5..51a4e1e 100644
4421 --- a/gdb/f-lang.h
4422 +++ b/gdb/f-lang.h
4423 @@ -28,6 +28,10 @@ extern void f_error (char *);        /* Defined in f-exp.y */
4424  extern void f_print_type (struct type *, const char *, struct ui_file *, int,
4425                           int);
4426  
4427 +extern const char *f_object_address_data_valid_print_to_stream
4428 +  (struct type *type, struct ui_file *stream);
4429 +extern void f_object_address_data_valid_or_error (struct type *type);
4430 +
4431  extern void f_val_print (struct type *, const gdb_byte *, int, CORE_ADDR,
4432                          struct ui_file *, int,
4433                          const struct value *,
4434 diff --git a/gdb/f-typeprint.c b/gdb/f-typeprint.c
4435 index a95ef84..830917d 100644
4436 --- a/gdb/f-typeprint.c
4437 +++ b/gdb/f-typeprint.c
4438 @@ -31,7 +31,7 @@
4439  #include "gdbcore.h"
4440  #include "target.h"
4441  #include "f-lang.h"
4442 -
4443 +#include "dwarf2loc.h"
4444  #include "gdb_string.h"
4445  #include <errno.h>
4446  
4447 @@ -48,6 +48,34 @@ void f_type_print_varspec_prefix (struct type *, struct ui_file *,
4448  void f_type_print_base (struct type *, struct ui_file *, int, int);
4449  \f
4450  
4451 +const char *
4452 +f_object_address_data_valid_print_to_stream (struct type *type,
4453 +                                            struct ui_file *stream)
4454 +{
4455 +  const char *msg;
4456 +
4457 +  msg = object_address_data_not_valid (type);
4458 +  if (msg != NULL)
4459 +    {
4460 +      /* Assuming the content printed to STREAM should not be localized.  */
4461 +      fprintf_filtered (stream, "<%s>", msg);
4462 +    }
4463 +
4464 +  return msg;
4465 +}
4466 +
4467 +void
4468 +f_object_address_data_valid_or_error (struct type *type)
4469 +{
4470 +  const char *msg;
4471 +
4472 +  msg = object_address_data_not_valid (type);
4473 +  if (msg != NULL)
4474 +    {
4475 +      error (_("Cannot access it because the %s."), _(msg));
4476 +    }
4477 +}
4478 +
4479  /* LEVEL is the depth to indent lines by.  */
4480  
4481  void
4482 @@ -57,6 +85,9 @@ f_print_type (struct type *type, const char *varstring, struct ui_file *stream,
4483    enum type_code code;
4484    int demangled_args;
4485  
4486 +  if (f_object_address_data_valid_print_to_stream (type, stream) != NULL)
4487 +    return;
4488 +
4489    f_type_print_base (type, stream, show, level);
4490    code = TYPE_CODE (type);
4491    if ((varstring != NULL && *varstring != '\0')
4492 @@ -164,6 +195,9 @@ f_type_print_varspec_suffix (struct type *type, struct ui_file *stream,
4493  
4494    QUIT;
4495  
4496 +  if (TYPE_CODE (type) != TYPE_CODE_TYPEDEF)
4497 +    CHECK_TYPEDEF (type);
4498 +
4499    switch (TYPE_CODE (type))
4500      {
4501      case TYPE_CODE_ARRAY:
4502 diff --git a/gdb/f-valprint.c b/gdb/f-valprint.c
4503 index 4359f6f..8ff834b 100644
4504 --- a/gdb/f-valprint.c
4505 +++ b/gdb/f-valprint.c
4506 @@ -54,15 +54,17 @@ int f77_array_offset_tbl[MAX_FORTRAN_DIMS + 1][2];
4507  /* The following macro gives us the size of the nth dimension, Where 
4508     n is 1 based.  */
4509  
4510 -#define F77_DIM_SIZE(n) (f77_array_offset_tbl[n][1])
4511 +#define F77_DIM_COUNT(n) (f77_array_offset_tbl[n][1])
4512  
4513 -/* The following gives us the offset for row n where n is 1-based.  */
4514 +/* The following gives us the element size for row n where n is 1-based.  */
4515  
4516 -#define F77_DIM_OFFSET(n) (f77_array_offset_tbl[n][0])
4517 +#define F77_DIM_BYTE_STRIDE(n) (f77_array_offset_tbl[n][0])
4518  
4519  int
4520  f77_get_lowerbound (struct type *type)
4521  {
4522 +  f_object_address_data_valid_or_error (type);
4523 +
4524    if (TYPE_ARRAY_LOWER_BOUND_IS_UNDEFINED (type))
4525      error (_("Lower bound may not be '*' in F77"));
4526  
4527 @@ -72,14 +74,17 @@ f77_get_lowerbound (struct type *type)
4528  int
4529  f77_get_upperbound (struct type *type)
4530  {
4531 +  f_object_address_data_valid_or_error (type);
4532 +
4533    if (TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))
4534      {
4535 -      /* We have an assumed size array on our hands.  Assume that
4536 -        upper_bound == lower_bound so that we show at least 1 element.
4537 -        If the user wants to see more elements, let him manually ask for 'em
4538 -        and we'll subscript the array and show him.  */
4539 +      /* We have an assumed size array on our hands.  As type_length_get
4540 +        already assumes a length zero of arrays with underfined bounds VALADDR
4541 +        passed to the Fortran functions does not contained the real inferior
4542 +        memory content.  User should request printing of specific array
4543 +        elements instead.  */
4544  
4545 -      return f77_get_lowerbound (type);
4546 +      return f77_get_lowerbound (type) - 1;
4547      }
4548  
4549    return TYPE_ARRAY_UPPER_BOUND_VALUE (type);
4550 @@ -135,24 +140,29 @@ f77_create_arrayprint_offset_tbl (struct type *type, struct ui_file *stream)
4551        upper = f77_get_upperbound (tmp_type);
4552        lower = f77_get_lowerbound (tmp_type);
4553  
4554 -      F77_DIM_SIZE (ndimen) = upper - lower + 1;
4555 +      F77_DIM_COUNT (ndimen) = upper - lower + 1;
4556 +
4557 +      F77_DIM_BYTE_STRIDE (ndimen) =
4558 +        TYPE_ARRAY_BYTE_STRIDE_VALUE (tmp_type);
4559  
4560        tmp_type = TYPE_TARGET_TYPE (tmp_type);
4561        ndimen++;
4562      }
4563  
4564 -  /* Now we multiply eltlen by all the offsets, so that later we 
4565 +  /* Now we multiply eltlen by all the BYTE_STRIDEs, so that later we
4566       can print out array elements correctly.  Up till now we 
4567 -     know an offset to apply to get the item but we also 
4568 +     know an eltlen to apply to get the item but we also 
4569       have to know how much to add to get to the next item.  */
4570  
4571    ndimen--;
4572    eltlen = TYPE_LENGTH (tmp_type);
4573 -  F77_DIM_OFFSET (ndimen) = eltlen;
4574 +  if (F77_DIM_BYTE_STRIDE (ndimen) == 0)
4575 +    F77_DIM_BYTE_STRIDE (ndimen) = eltlen;
4576    while (--ndimen > 0)
4577      {
4578 -      eltlen *= F77_DIM_SIZE (ndimen + 1);
4579 -      F77_DIM_OFFSET (ndimen) = eltlen;
4580 +      eltlen *= F77_DIM_COUNT (ndimen + 1);
4581 +      if (F77_DIM_BYTE_STRIDE (ndimen) == 0)
4582 +       F77_DIM_BYTE_STRIDE (ndimen) = eltlen;
4583      }
4584  }
4585  
4586 @@ -174,37 +184,35 @@ f77_print_array_1 (int nss, int ndimensions, struct type *type,
4587  
4588    if (nss != ndimensions)
4589      {
4590 -      for (i = 0;
4591 -          (i < F77_DIM_SIZE (nss) && (*elts) < options->print_max);
4592 -          i++)
4593 +      for (i = 0; (i < F77_DIM_COUNT (nss) && (*elts) < options->print_max); i++)
4594         {
4595           fprintf_filtered (stream, "( ");
4596           f77_print_array_1 (nss + 1, ndimensions, TYPE_TARGET_TYPE (type),
4597                              valaddr,
4598 -                            embedded_offset + i * F77_DIM_OFFSET (nss),
4599 +                            embedded_offset + i * F77_DIM_BYTE_STRIDE (nss),
4600                              address,
4601                              stream, recurse, val, options, elts);
4602           fprintf_filtered (stream, ") ");
4603         }
4604 -      if (*elts >= options->print_max && i < F77_DIM_SIZE (nss)) 
4605 +      if (*elts >= options->print_max && i < F77_DIM_COUNT (nss))
4606         fprintf_filtered (stream, "...");
4607      }
4608    else
4609      {
4610 -      for (i = 0; i < F77_DIM_SIZE (nss) && (*elts) < options->print_max;
4611 +      for (i = 0; i < F77_DIM_COUNT (nss) && (*elts) < options->print_max;
4612            i++, (*elts)++)
4613         {
4614           val_print (TYPE_TARGET_TYPE (type),
4615                      valaddr,
4616 -                    embedded_offset + i * F77_DIM_OFFSET (ndimensions),
4617 +                    embedded_offset + i * F77_DIM_BYTE_STRIDE (ndimensions),
4618                      address, stream, recurse,
4619                      val, options, current_language);
4620  
4621 -         if (i != (F77_DIM_SIZE (nss) - 1))
4622 +         if (i != (F77_DIM_COUNT (nss) - 1))
4623             fprintf_filtered (stream, ", ");
4624  
4625           if ((*elts == options->print_max - 1)
4626 -             && (i != (F77_DIM_SIZE (nss) - 1)))
4627 +             && (i != (F77_DIM_COUNT (nss) - 1)))
4628             fprintf_filtered (stream, "...");
4629         }
4630      }
4631 @@ -270,6 +278,9 @@ f_val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset,
4632    CORE_ADDR addr;
4633    int index;
4634  
4635 +  if (f_object_address_data_valid_print_to_stream (type, stream) != NULL)
4636 +    return;
4637 +
4638    CHECK_TYPEDEF (type);
4639    switch (TYPE_CODE (type))
4640      {
4641 diff --git a/gdb/findvar.c b/gdb/findvar.c
4642 index 66bcebe..e59e5f2 100644
4643 --- a/gdb/findvar.c
4644 +++ b/gdb/findvar.c
4645 @@ -35,6 +35,7 @@
4646  #include "block.h"
4647  #include "objfiles.h"
4648  #include "language.h"
4649 +#include "dwarf2loc.h"
4650  
4651  /* Basic byte-swapping routines.  All 'extract' functions return a
4652     host-format integer from a target-format integer at ADDR which is
4653 @@ -438,7 +439,10 @@ minsym_lookup_iterator_cb (struct objfile *objfile, void *cb_data)
4654  }
4655  
4656  /* A default implementation for the "la_read_var_value" hook in
4657 -   the language vector which should work in most situations.  */
4658 +   the language vector which should work in most situations.
4659 +   We have to first find the address of the variable before allocating struct
4660 +   value to return as its size may depend on DW_OP_PUSH_OBJECT_ADDRESS possibly 
4661 +   used by its type.  */
4662  
4663  struct value *
4664  default_read_var_value (struct symbol *var, struct frame_info *frame)
4665 @@ -446,16 +450,6 @@ default_read_var_value (struct symbol *var, struct frame_info *frame)
4666    struct value *v;
4667    struct type *type = SYMBOL_TYPE (var);
4668    CORE_ADDR addr;
4669 -  int len;
4670 -
4671 -  /* Call check_typedef on our type to make sure that, if TYPE is
4672 -     a TYPE_CODE_TYPEDEF, its length is set to the length of the target type
4673 -     instead of zero.  However, we do not replace the typedef type by the
4674 -     target type, because we want to keep the typedef in order to be able to
4675 -     set the returned value type description correctly.  */
4676 -  check_typedef (type);
4677 -
4678 -  len = TYPE_LENGTH (type);
4679  
4680    if (symbol_read_needs_frame (var))
4681      gdb_assert (frame);
4682 @@ -465,7 +459,7 @@ default_read_var_value (struct symbol *var, struct frame_info *frame)
4683      case LOC_CONST:
4684        /* Put the constant back in target format.  */
4685        v = allocate_value (type);
4686 -      store_signed_integer (value_contents_raw (v), len,
4687 +      store_signed_integer (value_contents_raw (v), TYPE_LENGTH (type),
4688                             gdbarch_byte_order (get_type_arch (type)),
4689                             (LONGEST) SYMBOL_VALUE (var));
4690        VALUE_LVAL (v) = not_lval;
4691 @@ -490,12 +484,12 @@ default_read_var_value (struct symbol *var, struct frame_info *frame)
4692  
4693      case LOC_CONST_BYTES:
4694        v = allocate_value (type);
4695 -      memcpy (value_contents_raw (v), SYMBOL_VALUE_BYTES (var), len);
4696 +      memcpy (value_contents_raw (v), SYMBOL_VALUE_BYTES (var),
4697 +             TYPE_LENGTH (type));
4698        VALUE_LVAL (v) = not_lval;
4699        return v;
4700  
4701      case LOC_STATIC:
4702 -      v = allocate_value_lazy (type);
4703        if (overlay_debugging)
4704         addr = symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (var),
4705                                          SYMBOL_OBJ_SECTION (var));
4706 @@ -509,7 +503,6 @@ default_read_var_value (struct symbol *var, struct frame_info *frame)
4707         error (_("Unknown argument list address for `%s'."),
4708                SYMBOL_PRINT_NAME (var));
4709        addr += SYMBOL_VALUE (var);
4710 -      v = allocate_value_lazy (type);
4711        break;
4712  
4713      case LOC_REF_ARG:
4714 @@ -524,14 +517,12 @@ default_read_var_value (struct symbol *var, struct frame_info *frame)
4715         argref += SYMBOL_VALUE (var);
4716         ref = value_at (lookup_pointer_type (type), argref);
4717         addr = value_as_address (ref);
4718 -       v = allocate_value_lazy (type);
4719         break;
4720        }
4721  
4722      case LOC_LOCAL:
4723        addr = get_frame_locals_address (frame);
4724        addr += SYMBOL_VALUE (var);
4725 -      v = allocate_value_lazy (type);
4726        break;
4727  
4728      case LOC_TYPEDEF:
4729 @@ -540,7 +531,6 @@ default_read_var_value (struct symbol *var, struct frame_info *frame)
4730        break;
4731  
4732      case LOC_BLOCK:
4733 -      v = allocate_value_lazy (type);
4734        if (overlay_debugging)
4735         addr = symbol_overlayed_address
4736           (BLOCK_START (SYMBOL_BLOCK_VALUE (var)), SYMBOL_OBJ_SECTION (var));
4737 @@ -566,7 +556,6 @@ default_read_var_value (struct symbol *var, struct frame_info *frame)
4738                      SYMBOL_PRINT_NAME (var));
4739  
4740             addr = value_as_address (regval);
4741 -           v = allocate_value_lazy (type);
4742           }
4743         else
4744           {
4745 @@ -615,7 +604,6 @@ default_read_var_value (struct symbol *var, struct frame_info *frame)
4746         if (obj_section
4747             && (obj_section->the_bfd_section->flags & SEC_THREAD_LOCAL) != 0)
4748           addr = target_translate_tls_address (obj_section->objfile, addr);
4749 -       v = allocate_value_lazy (type);
4750        }
4751        break;
4752  
4753 @@ -628,6 +616,10 @@ default_read_var_value (struct symbol *var, struct frame_info *frame)
4754        break;
4755      }
4756  
4757 +  /* ADDR is set here for ALLOCATE_VALUE's CHECK_TYPEDEF for
4758 +     DW_OP_PUSH_OBJECT_ADDRESS.  */
4759 +  object_address_set (addr);
4760 +  v = allocate_value_lazy (type);
4761    VALUE_LVAL (v) = lval_memory;
4762    set_value_address (v, addr);
4763    return v;
4764 @@ -723,10 +715,11 @@ struct value *
4765  value_from_register (struct type *type, int regnum, struct frame_info *frame)
4766  {
4767    struct gdbarch *gdbarch = get_frame_arch (frame);
4768 -  struct type *type1 = check_typedef (type);
4769    struct value *v;
4770  
4771 -  if (gdbarch_convert_register_p (gdbarch, regnum, type1))
4772 +  type = check_typedef (type);
4773 +
4774 +  if (gdbarch_convert_register_p (gdbarch, regnum, type))
4775      {
4776        int optim, unavail, ok;
4777  
4778 @@ -741,7 +734,7 @@ value_from_register (struct type *type, int regnum, struct frame_info *frame)
4779        VALUE_LVAL (v) = lval_register;
4780        VALUE_FRAME_ID (v) = get_frame_id (frame);
4781        VALUE_REGNUM (v) = regnum;
4782 -      ok = gdbarch_register_to_value (gdbarch, frame, regnum, type1,
4783 +      ok = gdbarch_register_to_value (gdbarch, frame, regnum, type,
4784                                       value_contents_raw (v), &optim,
4785                                       &unavail);
4786  
4787 diff --git a/gdb/gcore.c b/gdb/gcore.c
4788 index aedda41..a45e787 100644
4789 --- a/gdb/gcore.c
4790 +++ b/gdb/gcore.c
4791 @@ -33,6 +33,7 @@
4792  #include <fcntl.h>
4793  #include "regcache.h"
4794  #include "regset.h"
4795 +#include "gdb_bfd.h"
4796  
4797  /* The largest amount of memory to read from the target at once.  We
4798     must throttle it to limit the amount of memory used by GDB during
4799 @@ -50,7 +51,7 @@ static int gcore_memory_sections (bfd *);
4800  bfd *
4801  create_gcore_bfd (char *filename)
4802  {
4803 -  bfd *obfd = bfd_openw (filename, default_gcore_target ());
4804 +  bfd *obfd = gdb_bfd_openw (filename, default_gcore_target ());
4805  
4806    if (!obfd)
4807      error (_("Failed to open '%s' for output."), filename);
4808 @@ -110,7 +111,7 @@ do_bfd_delete_cleanup (void *arg)
4809    bfd *obfd = arg;
4810    const char *filename = obfd->filename;
4811  
4812 -  bfd_close (arg);
4813 +  gdb_bfd_unref (arg);
4814    unlink (filename);
4815  }
4816  
4817 @@ -154,7 +155,7 @@ gcore_command (char *args, int from_tty)
4818    fprintf_filtered (gdb_stdout, "Saved corefile %s\n", corefilename);
4819  
4820    discard_cleanups (old_chain);
4821 -  bfd_close (obfd);
4822 +  gdb_bfd_unref (obfd);
4823  }
4824  
4825  static unsigned long
4826 diff --git a/gdb/gdb-gdb.gdb.in b/gdb/gdb-gdb.gdb.in
4827 index ffb7f53..a2e7e94 100644
4828 --- a/gdb/gdb-gdb.gdb.in
4829 +++ b/gdb/gdb-gdb.gdb.in
4830 @@ -1,5 +1,15 @@
4831  echo Setting up the environment for debugging gdb.\n
4832  
4833 +# Set up the Python library and "require" command.
4834 +python
4835 +from os.path import abspath
4836 +gdb.datadir = abspath ('@srcdir@/python/lib')
4837 +gdb.pythonlibdir = gdb.datadir
4838 +gdb.__path__ = [gdb.datadir + '/gdb']
4839 +sys.path.insert(0, gdb.datadir)
4840 +end
4841 +source @srcdir@/python/lib/gdb/__init__.py
4842 +
4843  set complaints 1
4844  
4845  b internal_error
4846 diff --git a/gdb/gdb_bfd.c b/gdb/gdb_bfd.c
4847 new file mode 100644
4848 index 0000000..fac8776
4849 --- /dev/null
4850 +++ b/gdb/gdb_bfd.c
4851 @@ -0,0 +1,670 @@
4852 +/* Definitions for BFD wrappers used by GDB.
4853 +
4854 +   Copyright (C) 2011, 2012
4855 +   Free Software Foundation, Inc.
4856 +
4857 +   This file is part of GDB.
4858 +
4859 +   This program is free software; you can redistribute it and/or modify
4860 +   it under the terms of the GNU General Public License as published by
4861 +   the Free Software Foundation; either version 3 of the License, or
4862 +   (at your option) any later version.
4863 +
4864 +   This program is distributed in the hope that it will be useful,
4865 +   but WITHOUT ANY WARRANTY; without even the implied warranty of
4866 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4867 +   GNU General Public License for more details.
4868 +
4869 +   You should have received a copy of the GNU General Public License
4870 +   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
4871 +
4872 +#include "defs.h"
4873 +#include "gdb_bfd.h"
4874 +#include "gdb_assert.h"
4875 +#include "gdb_string.h"
4876 +#include "ui-out.h"
4877 +#include "gdbcmd.h"
4878 +#include "hashtab.h"
4879 +#ifdef HAVE_ZLIB_H
4880 +#include <zlib.h>
4881 +#endif
4882 +#ifdef HAVE_MMAP
4883 +#include <sys/mman.h>
4884 +#ifndef MAP_FAILED
4885 +#define MAP_FAILED ((void *) -1)
4886 +#endif
4887 +#endif
4888 +
4889 +/* An object of this type is stored in the section's user data when
4890 +   mapping a section.  */
4891 +
4892 +struct gdb_bfd_section_data
4893 +{
4894 +  /* Size of the data.  */
4895 +  bfd_size_type size;
4896 +  /* If the data was mmapped, this is the length of the map.  */
4897 +  bfd_size_type map_len;
4898 +  /* The data.  If NULL, the section data has not been read.  */
4899 +  void *data;
4900 +  /* If the data was mmapped, this is the map address.  */
4901 +  void *map_addr;
4902 +};
4903 +
4904 +/* A hash table holding every BFD that gdb knows about.  This is not
4905 +   to be confused with 'gdb_bfd_cache', which is used for sharing
4906 +   BFDs; in contrast, this hash is used just to implement
4907 +   "maint info bfd".  */
4908 +
4909 +static htab_t all_bfds;
4910 +
4911 +/* See gdb_bfd.h.  */
4912 +
4913 +void
4914 +gdb_bfd_stash_filename (struct bfd *abfd)
4915 +{
4916 +  char *name = bfd_get_filename (abfd);
4917 +  char *data;
4918 +
4919 +  data = bfd_alloc (abfd, strlen (name) + 1);
4920 +  strcpy (data, name);
4921 +
4922 +  /* Unwarranted chumminess with BFD.  */
4923 +  abfd->filename = data;
4924 +}
4925 +
4926 +/* An object of this type is stored in each BFD's user data.  */
4927 +
4928 +struct gdb_bfd_data
4929 +{
4930 +  /* The reference count.  */
4931 +  int refc;
4932 +
4933 +  /* The mtime of the BFD at the point the cache entry was made.  */
4934 +  time_t mtime;
4935 +};
4936 +
4937 +/* A hash table storing all the BFDs maintained in the cache.  */
4938 +
4939 +static htab_t gdb_bfd_cache;
4940 +
4941 +/* The type of an object being looked up in gdb_bfd_cache.  We use
4942 +   htab's capability of storing one kind of object (BFD in this case)
4943 +   and using a different sort of object for searching.  */
4944 +
4945 +struct gdb_bfd_cache_search
4946 +{
4947 +  /* The filename.  */
4948 +  const char *filename;
4949 +  /* The mtime.  */
4950 +  time_t mtime;
4951 +};
4952 +
4953 +/* A hash function for BFDs.  */
4954 +
4955 +static hashval_t
4956 +hash_bfd (const void *b)
4957 +{
4958 +  const bfd *abfd = b;
4959 +
4960 +  /* It is simplest to just hash the filename.  */
4961 +  return htab_hash_string (bfd_get_filename (abfd));
4962 +}
4963 +
4964 +/* An equality function for BFDs.  Note that this expects the caller
4965 +   to search using struct gdb_bfd_cache_search only, not BFDs.  */
4966 +
4967 +static int
4968 +eq_bfd (const void *a, const void *b)
4969 +{
4970 +  const bfd *abfd = a;
4971 +  const struct gdb_bfd_cache_search *s = b;
4972 +  struct gdb_bfd_data *gdata = bfd_usrdata (abfd);
4973 +
4974 +  return (gdata->mtime == s->mtime
4975 +         && strcmp (bfd_get_filename (abfd), s->filename) == 0);
4976 +}
4977 +
4978 +/* See gdb_bfd.h.  */
4979 +
4980 +struct bfd *
4981 +gdb_bfd_open (const char *name, const char *target, int fd)
4982 +{
4983 +  hashval_t hash;
4984 +  void **slot;
4985 +  bfd *abfd;
4986 +  struct gdb_bfd_cache_search search;
4987 +  struct stat st;
4988 +
4989 +  if (gdb_bfd_cache == NULL)
4990 +    gdb_bfd_cache = htab_create_alloc (1, hash_bfd, eq_bfd, NULL,
4991 +                                      xcalloc, xfree);
4992 +
4993 +  if (fd == -1)
4994 +    {
4995 +      fd = open (name, O_RDONLY | O_BINARY);
4996 +      if (fd == -1)
4997 +       {
4998 +         bfd_set_error (bfd_error_system_call);
4999 +         return NULL;
5000 +       }
5001 +    }
5002 +
5003 +  search.filename = name;
5004 +  if (fstat (fd, &st) < 0)
5005 +    {
5006 +      /* Weird situation here.  */
5007 +      search.mtime = 0;
5008 +    }
5009 +  else
5010 +    search.mtime = st.st_mtime;
5011 +
5012 +  /* Note that this must compute the same result as hash_bfd.  */
5013 +  hash = htab_hash_string (name);
5014 +  /* Note that we cannot use htab_find_slot_with_hash here, because
5015 +     opening the BFD may fail; and this would violate hashtab
5016 +     invariants.  */
5017 +  abfd = htab_find_with_hash (gdb_bfd_cache, &search, hash);
5018 +  if (abfd != NULL)
5019 +    {
5020 +      close (fd);
5021 +      gdb_bfd_ref (abfd);
5022 +      return abfd;
5023 +    }
5024 +
5025 +  abfd = bfd_fopen (name, target, FOPEN_RB, fd);
5026 +  if (abfd == NULL)
5027 +    return NULL;
5028 +
5029 +  slot = htab_find_slot_with_hash (gdb_bfd_cache, &search, hash, INSERT);
5030 +  gdb_assert (!*slot);
5031 +  *slot = abfd;
5032 +
5033 +  gdb_bfd_stash_filename (abfd);
5034 +  gdb_bfd_ref (abfd);
5035 +  return abfd;
5036 +}
5037 +
5038 +/* A helper function that releases any section data attached to the
5039 +   BFD.  */
5040 +
5041 +static void
5042 +free_one_bfd_section (bfd *abfd, asection *sectp, void *ignore)
5043 +{
5044 +  struct gdb_bfd_section_data *sect = bfd_get_section_userdata (abfd, sectp);
5045 +
5046 +  if (sect != NULL && sect->data != NULL)
5047 +    {
5048 +#ifdef HAVE_MMAP
5049 +      if (sect->map_addr != NULL)
5050 +       {
5051 +         int res;
5052 +
5053 +         res = munmap (sect->map_addr, sect->map_len);
5054 +         gdb_assert (res == 0);
5055 +       }
5056 +      else
5057 +#endif
5058 +       xfree (sect->data);
5059 +    }
5060 +}
5061 +
5062 +/* Close ABFD, and warn if that fails.  */
5063 +
5064 +static int
5065 +gdb_bfd_close_or_warn (struct bfd *abfd)
5066 +{
5067 +  int ret;
5068 +  char *name = bfd_get_filename (abfd);
5069 +
5070 +  bfd_map_over_sections (abfd, free_one_bfd_section, NULL);
5071 +
5072 +  ret = bfd_close (abfd);
5073 +
5074 +  if (!ret)
5075 +    warning (_("cannot close \"%s\": %s"),
5076 +            name, bfd_errmsg (bfd_get_error ()));
5077 +
5078 +  return ret;
5079 +}
5080 +
5081 +/* See gdb_bfd.h.  */
5082 +
5083 +void
5084 +gdb_bfd_ref (struct bfd *abfd)
5085 +{
5086 +  struct gdb_bfd_data *gdata;
5087 +  void **slot;
5088 +
5089 +  if (abfd == NULL)
5090 +    return;
5091 +
5092 +  gdata = bfd_usrdata (abfd);
5093 +
5094 +  if (gdata != NULL)
5095 +    {
5096 +      gdata->refc += 1;
5097 +      return;
5098 +    }
5099 +
5100 +  gdata = bfd_zalloc (abfd, sizeof (struct gdb_bfd_data));
5101 +  gdata->refc = 1;
5102 +  gdata->mtime = bfd_get_mtime (abfd);
5103 +  bfd_usrdata (abfd) = gdata;
5104 +
5105 +  /* This is the first we've seen it, so add it to the hash table.  */
5106 +  slot = htab_find_slot (all_bfds, abfd, INSERT);
5107 +  gdb_assert (slot && !*slot);
5108 +  *slot = abfd;
5109 +}
5110 +
5111 +/* See gdb_bfd.h.  */
5112 +
5113 +void
5114 +gdb_bfd_unref (struct bfd *abfd)
5115 +{
5116 +  struct gdb_bfd_data *gdata;
5117 +  struct gdb_bfd_cache_search search;
5118 +  void **slot;
5119 +
5120 +  if (abfd == NULL)
5121 +    return;
5122 +
5123 +  gdata = bfd_usrdata (abfd);
5124 +  gdb_assert (gdata->refc >= 1);
5125 +
5126 +  gdata->refc -= 1;
5127 +  if (gdata->refc > 0)
5128 +    return;
5129 +
5130 +  search.filename = bfd_get_filename (abfd);
5131 +
5132 +  if (gdb_bfd_cache && search.filename)
5133 +    {
5134 +      hashval_t hash = htab_hash_string (search.filename);
5135 +      void **slot;
5136 +
5137 +      search.mtime = gdata->mtime;
5138 +      slot = htab_find_slot_with_hash (gdb_bfd_cache, &search, hash,
5139 +                                      NO_INSERT);
5140 +
5141 +      if (slot && *slot)
5142 +       htab_clear_slot (gdb_bfd_cache, slot);
5143 +    }
5144 +
5145 +  bfd_usrdata (abfd) = NULL;  /* Paranoia.  */
5146 +
5147 +  htab_remove_elt (all_bfds, abfd);
5148 +
5149 +  gdb_bfd_close_or_warn (abfd);
5150 +}
5151 +
5152 +/* A helper function that returns the section data descriptor
5153 +   associated with SECTION.  If no such descriptor exists, a new one
5154 +   is allocated and cleared.  */
5155 +
5156 +static struct gdb_bfd_section_data *
5157 +get_section_descriptor (asection *section)
5158 +{
5159 +  struct gdb_bfd_section_data *result;
5160 +
5161 +  result = bfd_get_section_userdata (section->owner, section);
5162 +
5163 +  if (result == NULL)
5164 +    {
5165 +      result = bfd_zalloc (section->owner, sizeof (*result));
5166 +      bfd_set_section_userdata (section->owner, section, result);
5167 +    }
5168 +
5169 +  return result;
5170 +}
5171 +
5172 +/* Decompress a section that was compressed using zlib.  Store the
5173 +   decompressed buffer, and its size, in DESCRIPTOR.  */
5174 +
5175 +static void
5176 +zlib_decompress_section (asection *sectp,
5177 +                        struct gdb_bfd_section_data *descriptor)
5178 +{
5179 +  bfd *abfd = sectp->owner;
5180 +#ifndef HAVE_ZLIB_H
5181 +  error (_("Support for zlib-compressed data (from '%s', section '%s') "
5182 +           "is disabled in this copy of GDB"),
5183 +         bfd_get_filename (abfd),
5184 +        bfd_get_section_name (sectp));
5185 +#else
5186 +  bfd_size_type compressed_size = bfd_get_section_size (sectp);
5187 +  gdb_byte *compressed_buffer = xmalloc (compressed_size);
5188 +  struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
5189 +  struct cleanup *inner_cleanup;
5190 +  bfd_size_type uncompressed_size;
5191 +  gdb_byte *uncompressed_buffer;
5192 +  z_stream strm;
5193 +  int rc;
5194 +  int header_size = 12;
5195 +  struct dwarf2_per_bfd_section *section_data;
5196 +
5197 +  if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
5198 +      || bfd_bread (compressed_buffer,
5199 +                   compressed_size, abfd) != compressed_size)
5200 +    error (_("can't read data from '%s', section '%s'"),
5201 +           bfd_get_filename (abfd),
5202 +          bfd_get_section_name (abfd, sectp));
5203 +
5204 +  /* Read the zlib header.  In this case, it should be "ZLIB" followed
5205 +     by the uncompressed section size, 8 bytes in big-endian order.  */
5206 +  if (compressed_size < header_size
5207 +      || strncmp (compressed_buffer, "ZLIB", 4) != 0)
5208 +    error (_("corrupt ZLIB header from '%s', section '%s'"),
5209 +           bfd_get_filename (abfd),
5210 +          bfd_get_section_name (abfd, sectp));
5211 +  uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
5212 +  uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
5213 +  uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
5214 +  uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
5215 +  uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
5216 +  uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
5217 +  uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
5218 +  uncompressed_size += compressed_buffer[11];
5219 +
5220 +  /* It is possible the section consists of several compressed
5221 +     buffers concatenated together, so we uncompress in a loop.  */
5222 +  strm.zalloc = NULL;
5223 +  strm.zfree = NULL;
5224 +  strm.opaque = NULL;
5225 +  strm.avail_in = compressed_size - header_size;
5226 +  strm.next_in = (Bytef*) compressed_buffer + header_size;
5227 +  strm.avail_out = uncompressed_size;
5228 +  uncompressed_buffer = xmalloc (uncompressed_size);
5229 +  inner_cleanup = make_cleanup (xfree, uncompressed_buffer);
5230 +  rc = inflateInit (&strm);
5231 +  while (strm.avail_in > 0)
5232 +    {
5233 +      if (rc != Z_OK)
5234 +        error (_("setting up uncompression in '%s', section '%s': %d"),
5235 +               bfd_get_filename (abfd),
5236 +              bfd_get_section_name (abfd, sectp),
5237 +              rc);
5238 +      strm.next_out = ((Bytef*) uncompressed_buffer
5239 +                       + (uncompressed_size - strm.avail_out));
5240 +      rc = inflate (&strm, Z_FINISH);
5241 +      if (rc != Z_STREAM_END)
5242 +        error (_("zlib error uncompressing from '%s', section '%s': %d"),
5243 +               bfd_get_filename (abfd),
5244 +              bfd_get_section_name (abfd, sectp),
5245 +              rc);
5246 +      rc = inflateReset (&strm);
5247 +    }
5248 +  rc = inflateEnd (&strm);
5249 +  if (rc != Z_OK
5250 +      || strm.avail_out != 0)
5251 +    error (_("concluding uncompression in '%s', section '%s': %d"),
5252 +           bfd_get_filename (abfd),
5253 +          bfd_get_section_name (abfd, sectp),
5254 +          rc);
5255 +
5256 +  discard_cleanups (inner_cleanup);
5257 +  do_cleanups (cleanup);
5258 +
5259 +  /* Attach the data to the BFD section.  */
5260 +  descriptor->data = uncompressed_buffer;
5261 +  descriptor->size = uncompressed_size;
5262 +#endif
5263 +}
5264 +
5265 +/* See gdb_bfd.h.  */
5266 +
5267 +const gdb_byte *
5268 +gdb_bfd_map_section (asection *sectp, bfd_size_type *size)
5269 +{
5270 +  bfd *abfd;
5271 +  gdb_byte *buf, *retbuf;
5272 +  unsigned char header[4];
5273 +  struct gdb_bfd_section_data *descriptor;
5274 +
5275 +  gdb_assert ((sectp->flags & SEC_RELOC) == 0);
5276 +  gdb_assert (size != NULL);
5277 +
5278 +  abfd = sectp->owner;
5279 +
5280 +  descriptor = get_section_descriptor (sectp);
5281 +
5282 +  /* If the data was already read for this BFD, just reuse it.  */
5283 +  if (descriptor->data != NULL)
5284 +    goto done;
5285 +
5286 +  /* Check if the file has a 4-byte header indicating compression.  */
5287 +  if (bfd_get_section_size (sectp) > sizeof (header)
5288 +      && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
5289 +      && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
5290 +    {
5291 +      /* Upon decompression, update the buffer and its size.  */
5292 +      if (strncmp (header, "ZLIB", sizeof (header)) == 0)
5293 +        {
5294 +          zlib_decompress_section (sectp, descriptor);
5295 +         goto done;
5296 +        }
5297 +    }
5298 +
5299 +#ifdef HAVE_MMAP
5300 +  {
5301 +    /* The page size, used when mmapping.  */
5302 +    static int pagesize;
5303 +
5304 +    if (pagesize == 0)
5305 +      pagesize = getpagesize ();
5306 +
5307 +    /* Only try to mmap sections which are large enough: we don't want
5308 +       to waste space due to fragmentation.  */
5309 +
5310 +    if (bfd_get_section_size (sectp) > 4 * pagesize)
5311 +      {
5312 +       descriptor->size = bfd_get_section_size (sectp);
5313 +       descriptor->data = bfd_mmap (abfd, 0, descriptor->size, PROT_READ,
5314 +                                    MAP_PRIVATE, sectp->filepos,
5315 +                                    &descriptor->map_addr,
5316 +                                    &descriptor->map_len);
5317 +
5318 +       if ((caddr_t)descriptor->data != MAP_FAILED)
5319 +         {
5320 +#if HAVE_POSIX_MADVISE
5321 +           posix_madvise (descriptor->map_addr, descriptor->map_len,
5322 +                          POSIX_MADV_WILLNEED);
5323 +#endif
5324 +           goto done;
5325 +         }
5326 +
5327 +       /* On failure, clear out the section data and try again.  */
5328 +       memset (descriptor, 0, sizeof (*descriptor));
5329 +      }
5330 +  }
5331 +#endif /* HAVE_MMAP */
5332 +
5333 +  /* If we get here, we are a normal, not-compressed section.  */
5334 +
5335 +  descriptor->size = bfd_get_section_size (sectp);
5336 +  descriptor->data = xmalloc (descriptor->size);
5337 +
5338 +  if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
5339 +      || bfd_bread (descriptor->data, bfd_get_section_size (sectp),
5340 +                   abfd) != bfd_get_section_size (sectp))
5341 +    {
5342 +      xfree (descriptor->data);
5343 +      descriptor->data = NULL;
5344 +      error (_("Can't read data for section '%s'"),
5345 +            bfd_get_filename (abfd));
5346 +    }
5347 +
5348 + done:
5349 +  gdb_assert (descriptor->data != NULL);
5350 +  *size = descriptor->size;
5351 +  return descriptor->data;
5352 +}
5353 +
5354 +\f
5355 +
5356 +/* See gdb_bfd.h.  */
5357 +
5358 +bfd *
5359 +gdb_bfd_fopen (const char *filename, const char *target, const char *mode,
5360 +              int fd)
5361 +{
5362 +  bfd *result = bfd_fopen (filename, target, mode, fd);
5363 +
5364 +  if (result)
5365 +    {
5366 +      gdb_bfd_stash_filename (result);
5367 +      gdb_bfd_ref (result);
5368 +    }
5369 +
5370 +  return result;
5371 +}
5372 +
5373 +/* See gdb_bfd.h.  */
5374 +
5375 +bfd *
5376 +gdb_bfd_openr (const char *filename, const char *target)
5377 +{
5378 +  bfd *result = bfd_openr (filename, target);
5379 +
5380 +  if (result)
5381 +    {
5382 +      gdb_bfd_stash_filename (result);
5383 +      gdb_bfd_ref (result);
5384 +    }
5385 +
5386 +  return result;
5387 +}
5388 +
5389 +/* See gdb_bfd.h.  */
5390 +
5391 +bfd *
5392 +gdb_bfd_openw (const char *filename, const char *target)
5393 +{
5394 +  bfd *result = bfd_openw (filename, target);
5395 +
5396 +  if (result)
5397 +    {
5398 +      gdb_bfd_stash_filename (result);
5399 +      gdb_bfd_ref (result);
5400 +    }
5401 +
5402 +  return result;
5403 +}
5404 +
5405 +/* See gdb_bfd.h.  */
5406 +
5407 +bfd *
5408 +gdb_bfd_openr_iovec (const char *filename, const char *target,
5409 +                    void *(*open_func) (struct bfd *nbfd,
5410 +                                        void *open_closure),
5411 +                    void *open_closure,
5412 +                    file_ptr (*pread_func) (struct bfd *nbfd,
5413 +                                            void *stream,
5414 +                                            void *buf,
5415 +                                            file_ptr nbytes,
5416 +                                            file_ptr offset),
5417 +                    int (*close_func) (struct bfd *nbfd,
5418 +                                       void *stream),
5419 +                    int (*stat_func) (struct bfd *abfd,
5420 +                                      void *stream,
5421 +                                      struct stat *sb))
5422 +{
5423 +  bfd *result = bfd_openr_iovec (filename, target,
5424 +                                open_func, open_closure,
5425 +                                pread_func, close_func, stat_func);
5426 +
5427 +  if (result)
5428 +    {
5429 +      gdb_bfd_ref (result);
5430 +      gdb_bfd_stash_filename (result);
5431 +    }
5432 +
5433 +  return result;
5434 +}
5435 +
5436 +/* See gdb_bfd.h.  */
5437 +
5438 +bfd *
5439 +gdb_bfd_openr_next_archived_file (bfd *archive, bfd *previous)
5440 +{
5441 +  bfd *result = bfd_openr_next_archived_file (archive, previous);
5442 +
5443 +  if (result)
5444 +    {
5445 +      gdb_bfd_ref (result);
5446 +      /* No need to stash the filename here.  */
5447 +    }
5448 +
5449 +  return result;
5450 +}
5451 +
5452 +/* See gdb_bfd.h.  */
5453 +
5454 +bfd *
5455 +gdb_bfd_fdopenr (const char *filename, const char *target, int fd)
5456 +{
5457 +  bfd *result = bfd_fdopenr (filename, target, fd);
5458 +
5459 +  if (result)
5460 +    {
5461 +      gdb_bfd_ref (result);
5462 +      gdb_bfd_stash_filename (result);
5463 +    }
5464 +
5465 +  return result;
5466 +}
5467 +
5468 +\f
5469 +
5470 +/* A callback for htab_traverse that prints a single BFD.  */
5471 +
5472 +static int
5473 +print_one_bfd (void **slot, void *data)
5474 +{
5475 +  bfd *abfd = *slot;
5476 +  struct gdb_bfd_data *gdata = bfd_usrdata (abfd);
5477 +  struct ui_out *uiout = data;
5478 +  struct cleanup *inner;
5479 +
5480 +  inner = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
5481 +  ui_out_field_int (uiout, "refcount", gdata->refc);
5482 +  ui_out_field_string (uiout, "addr", host_address_to_string (abfd));
5483 +  ui_out_field_string (uiout, "filename", bfd_get_filename (abfd));
5484 +  ui_out_text (uiout, "\n");
5485 +  do_cleanups (inner);
5486 +
5487 +  return 1;
5488 +}
5489 +
5490 +/* Implement the 'maint info bfd' command.  */
5491 +
5492 +static void
5493 +maintenance_info_bfds (char *arg, int from_tty)
5494 +{
5495 +  struct cleanup *cleanup;
5496 +  struct ui_out *uiout = current_uiout;
5497 +
5498 +  cleanup = make_cleanup_ui_out_table_begin_end (uiout, 3, -1, "bfds");
5499 +  ui_out_table_header (uiout, 10, ui_left, "refcount", "Refcount");
5500 +  ui_out_table_header (uiout, 18, ui_left, "addr", "Address");
5501 +  ui_out_table_header (uiout, 40, ui_left, "filename", "Filename");
5502 +
5503 +  ui_out_table_body (uiout);
5504 +  htab_traverse (all_bfds, print_one_bfd, uiout);
5505 +
5506 +  do_cleanups (cleanup);
5507 +}
5508 +
5509 +/* -Wmissing-prototypes */
5510 +extern initialize_file_ftype _initialize_gdb_bfd;
5511 +
5512 +void
5513 +_initialize_gdb_bfd (void)
5514 +{
5515 +  all_bfds = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
5516 +                               NULL, xcalloc, xfree);
5517 +
5518 +  add_cmd ("bfds", class_maintenance, maintenance_info_bfds, _("\
5519 +List the BFDs that are currently open."),
5520 +          &maintenanceinfolist);
5521 +}
5522 diff --git a/gdb/gdb_bfd.h b/gdb/gdb_bfd.h
5523 new file mode 100644
5524 index 0000000..f131ba7
5525 --- /dev/null
5526 +++ b/gdb/gdb_bfd.h
5527 @@ -0,0 +1,106 @@
5528 +/* Definitions for BFD wrappers used by GDB.
5529 +
5530 +   Copyright (C) 2011, 2012
5531 +   Free Software Foundation, Inc.
5532 +
5533 +   This file is part of GDB.
5534 +
5535 +   This program is free software; you can redistribute it and/or modify
5536 +   it under the terms of the GNU General Public License as published by
5537 +   the Free Software Foundation; either version 3 of the License, or
5538 +   (at your option) any later version.
5539 +
5540 +   This program is distributed in the hope that it will be useful,
5541 +   but WITHOUT ANY WARRANTY; without even the implied warranty of
5542 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5543 +   GNU General Public License for more details.
5544 +
5545 +   You should have received a copy of the GNU General Public License
5546 +   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
5547 +
5548 +#ifndef GDB_BFD_H
5549 +#define GDB_BFD_H
5550 +
5551 +/* Make a copy ABFD's filename using bfd_alloc, and reassign it to the
5552 +   BFD.  This ensures that the BFD's filename has the same lifetime as
5553 +   the BFD itself.  */
5554 +
5555 +void gdb_bfd_stash_filename (struct bfd *abfd);
5556 +
5557 +/* Open a read-only (FOPEN_RB) BFD given arguments like bfd_fopen.
5558 +   Returns NULL on error.  On success, returns a new reference to the
5559 +   BFD, which must be freed with gdb_bfd_unref.  BFDs returned by this
5560 +   call are shared among all callers opening the same file.  If FD is
5561 +   not -1, then after this call it is owned by BFD.  */
5562 +
5563 +struct bfd *gdb_bfd_open (const char *name, const char *target, int fd);
5564 +
5565 +/* Increment the reference count of ABFD.  It is fine for ABFD to be
5566 +   NULL; in this case the function does nothing.  */
5567 +
5568 +void gdb_bfd_ref (struct bfd *abfd);
5569 +
5570 +/* Decrement the reference count of ABFD.  If this is the last
5571 +   reference, ABFD will be freed.  If ABFD is NULL, this function does
5572 +   nothing.  */
5573 +
5574 +void gdb_bfd_unref (struct bfd *abfd);
5575 +
5576 +/* Try to read or map the contents of the section SECT.  If
5577 +   successful, the section data is returned and *SIZE is set to the
5578 +   size of the section data; this may not be the same as the size
5579 +   according to bfd_get_section_size if the section was compressed.
5580 +   The returned section data is associated with the BFD and will be
5581 +   destroyed when the BFD is destroyed.  There is no other way to free
5582 +   it; for temporary uses of section data, see
5583 +   bfd_malloc_and_get_section.  SECT may not have relocations.  This
5584 +   function will throw on error.  */
5585 +
5586 +const gdb_byte *gdb_bfd_map_section (asection *section, bfd_size_type *size);
5587 +
5588 +\f
5589 +
5590 +/* A wrapper for bfd_fopen that initializes the gdb-specific reference
5591 +   count and calls gdb_bfd_stash_filename.  */
5592 +
5593 +bfd *gdb_bfd_fopen (const char *, const char *, const char *, int);
5594 +
5595 +/* A wrapper for bfd_openr that initializes the gdb-specific reference
5596 +   count and calls gdb_bfd_stash_filename.  */
5597 +
5598 +bfd *gdb_bfd_openr (const char *, const char *);
5599 +
5600 +/* A wrapper for bfd_openw that initializes the gdb-specific reference
5601 +   count and calls gdb_bfd_stash_filename.  */
5602 +
5603 +bfd *gdb_bfd_openw (const char *, const char *);
5604 +
5605 +/* A wrapper for bfd_openr_iovec that initializes the gdb-specific
5606 +   reference count and calls gdb_bfd_stash_filename.  */
5607 +
5608 +bfd *gdb_bfd_openr_iovec (const char *filename, const char *target,
5609 +                         void *(*open_func) (struct bfd *nbfd,
5610 +                                             void *open_closure),
5611 +                         void *open_closure,
5612 +                         file_ptr (*pread_func) (struct bfd *nbfd,
5613 +                                                 void *stream,
5614 +                                                 void *buf,
5615 +                                                 file_ptr nbytes,
5616 +                                                 file_ptr offset),
5617 +                         int (*close_func) (struct bfd *nbfd,
5618 +                                            void *stream),
5619 +                         int (*stat_func) (struct bfd *abfd,
5620 +                                           void *stream,
5621 +                                           struct stat *sb));
5622 +
5623 +/* A wrapper for bfd_openr_next_archived_file that initializes the
5624 +   gdb-specific reference count and calls gdb_bfd_stash_filename.  */
5625 +
5626 +bfd *gdb_bfd_openr_next_archived_file (bfd *archive, bfd *previous);
5627 +
5628 +/* A wrapper for bfd_fdopenr that initializes the gdb-specific
5629 +   reference count and calls gdb_bfd_stash_filename.  */
5630 +
5631 +bfd *gdb_bfd_fdopenr (const char *filename, const char *target, int fd);
5632 +
5633 +#endif /* GDB_BFD_H */
5634 diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c
5635 index 8142ab9..a2953cc 100644
5636 --- a/gdb/gdbtypes.c
5637 +++ b/gdb/gdbtypes.c
5638 @@ -37,6 +37,9 @@
5639  #include "gdb_assert.h"
5640  #include "hashtab.h"
5641  #include "exceptions.h"
5642 +#include "observer.h"
5643 +#include "dwarf2expr.h"
5644 +#include "dwarf2loc.h"
5645  
5646  /* Initialize BADNESS constants.  */
5647  
5648 @@ -141,7 +144,16 @@ static void print_bit_vector (B_TYPE *, int);
5649  static void print_arg_types (struct field *, int, int);
5650  static void dump_fn_fieldlists (struct type *, int);
5651  static void print_cplus_stuff (struct type *, int);
5652 +static LONGEST type_length_get (struct type *type, struct type *target_type,
5653 +                               int full_span);
5654  
5655 +#if 0
5656 +/* The hash table holding all discardable `struct type *' references.  */
5657 +static htab_t type_discardable_table;
5658 +
5659 +/* Current type_discardable_check pass used for TYPE_DISCARDABLE_AGE.  */
5660 +static int type_discardable_age_current;
5661 +#endif
5662  
5663  /* Allocate a new OBJFILE-associated type structure and fill it
5664     with some defaults.  Space for the type structure is allocated
5665 @@ -172,6 +184,43 @@ alloc_type (struct objfile *objfile)
5666    return type;
5667  }
5668  
5669 +#if 0
5670 +/* Declare TYPE as discardable on next garbage collection by free_all_types.
5671 +   You must call type_mark_used during each free_all_types to protect TYPE from
5672 +   being deallocated.  */
5673 +
5674 +static void
5675 +set_type_as_discardable (struct type *type)
5676 +{
5677 +  void **slot;
5678 +
5679 +  gdb_assert (!TYPE_DISCARDABLE (type));
5680 +
5681 +  TYPE_DISCARDABLE (type) = 1;
5682 +  TYPE_DISCARDABLE_AGE (type) = type_discardable_age_current;
5683 +
5684 +  slot = htab_find_slot (type_discardable_table, type, INSERT);
5685 +  gdb_assert (!*slot);
5686 +  *slot = type;
5687 +}
5688 +#endif
5689 +
5690 +/* Allocate a new type like alloc_type but preserve for it the discardability
5691 +   state of PARENT_TYPE.  */
5692 +
5693 +static struct type *
5694 +alloc_type_as_parent (struct type *parent_type)
5695 +{
5696 +  struct type *new_type = alloc_type_copy (parent_type);
5697 +
5698 +#if 0
5699 +  if (TYPE_DISCARDABLE (parent_type))
5700 +    set_type_as_discardable (new_type);
5701 +#endif
5702 +
5703 +  return new_type;
5704 +}
5705 +
5706  /* Allocate a new GDBARCH-associated type structure and fill it
5707     with some defaults.  Space for the type structure is allocated
5708     on the heap.  */
5709 @@ -297,7 +346,7 @@ make_pointer_type (struct type *type, struct type **typeptr)
5710  
5711    if (typeptr == 0 || *typeptr == 0)   /* We'll need to allocate one.  */
5712      {
5713 -      ntype = alloc_type_copy (type);
5714 +      ntype = alloc_type_as_parent (type);
5715        if (typeptr)
5716         *typeptr = ntype;
5717      }
5718 @@ -374,7 +423,7 @@ make_reference_type (struct type *type, struct type **typeptr)
5719  
5720    if (typeptr == 0 || *typeptr == 0)   /* We'll need to allocate one.  */
5721      {
5722 -      ntype = alloc_type_copy (type);
5723 +      ntype = alloc_type_as_parent (type);
5724        if (typeptr)
5725         *typeptr = ntype;
5726      }
5727 @@ -784,6 +833,7 @@ create_range_type (struct type *result_type, struct type *index_type,
5728      TYPE_ZALLOC (result_type, sizeof (struct range_bounds));
5729    TYPE_LOW_BOUND (result_type) = low_bound;
5730    TYPE_HIGH_BOUND (result_type) = high_bound;
5731 +  TYPE_BYTE_STRIDE (result_type) = 0;
5732  
5733    if (low_bound >= 0)
5734      TYPE_UNSIGNED (result_type) = 1;
5735 @@ -927,26 +977,31 @@ create_array_type (struct type *result_type,
5736  
5737    TYPE_CODE (result_type) = TYPE_CODE_ARRAY;
5738    TYPE_TARGET_TYPE (result_type) = element_type;
5739 -  if (get_discrete_bounds (range_type, &low_bound, &high_bound) < 0)
5740 -    low_bound = high_bound = 0;
5741 -  CHECK_TYPEDEF (element_type);
5742 -  /* Be careful when setting the array length.  Ada arrays can be
5743 -     empty arrays with the high_bound being smaller than the low_bound.
5744 -     In such cases, the array length should be zero.  */
5745 -  if (high_bound < low_bound)
5746 -    TYPE_LENGTH (result_type) = 0;
5747 -  else
5748 -    TYPE_LENGTH (result_type) =
5749 -      TYPE_LENGTH (element_type) * (high_bound - low_bound + 1);
5750    TYPE_NFIELDS (result_type) = 1;
5751    TYPE_FIELDS (result_type) =
5752      (struct field *) TYPE_ZALLOC (result_type, sizeof (struct field));
5753    TYPE_INDEX_TYPE (result_type) = range_type;
5754    TYPE_VPTR_FIELDNO (result_type) = -1;
5755  
5756 -  /* TYPE_FLAG_TARGET_STUB will take care of zero length arrays.  */
5757 +  /* DWARF blocks may depend on runtime information like
5758 +     DW_OP_PUSH_OBJECT_ADDRESS not being available during the
5759 +     CREATE_ARRAY_TYPE time.  */
5760 +  if (TYPE_RANGE_DATA (range_type)->low.kind != RANGE_BOUND_KIND_CONSTANT
5761 +      || TYPE_RANGE_DATA (range_type)->high.kind != RANGE_BOUND_KIND_CONSTANT
5762 +      || TYPE_DYNAMIC (element_type))
5763 +    TYPE_LENGTH (result_type) = 0;
5764 +  else
5765 +    {
5766 +      CHECK_TYPEDEF (element_type);
5767 +      TYPE_LENGTH (result_type) = type_length_get (result_type, element_type,
5768 +                                                  0);
5769 +    }
5770    if (TYPE_LENGTH (result_type) == 0)
5771 -    TYPE_TARGET_STUB (result_type) = 1;
5772 +    {
5773 +      /* The real size will be computed for specific instances by
5774 +        CHECK_TYPEDEF.  */
5775 +      TYPE_TARGET_STUB (result_type) = 1;
5776 +    }
5777  
5778    return result_type;
5779  }
5780 @@ -1468,6 +1523,105 @@ stub_noname_complaint (void)
5781    complaint (&symfile_complaints, _("stub type has NULL name"));
5782  }
5783  
5784 +/* Calculate the memory length of array TYPE.
5785 +
5786 +   TARGET_TYPE should be set to `check_typedef (TYPE_TARGET_TYPE (type))' as
5787 +   a performance hint.  Feel free to pass NULL.  Set FULL_SPAN to return the
5788 +   size incl. the possible padding of the last element - it may differ from the
5789 +   cleared FULL_SPAN return value (the expected SIZEOF) for non-zero
5790 +   TYPE_BYTE_STRIDE values.  */
5791 +
5792 +static LONGEST
5793 +type_length_get (struct type *type, struct type *target_type, int full_span)
5794 +{
5795 +  struct type *range_type;
5796 +  LONGEST byte_stride = 0;     /* `= 0' for a false GCC warning.  */
5797 +  LONGEST count, element_size, retval;
5798 +
5799 +  if (TYPE_CODE (type) != TYPE_CODE_ARRAY
5800 +      && TYPE_CODE (type) != TYPE_CODE_STRING)
5801 +    return TYPE_LENGTH (type);
5802 +
5803 +  /* Avoid executing TYPE_HIGH_BOUND for invalid (unallocated/unassociated)
5804 +     Fortran arrays.  The allocated data will never be used so they can be
5805 +     zero-length.  */
5806 +  if (object_address_data_not_valid (type))
5807 +    return 0;
5808 +
5809 +  range_type = TYPE_INDEX_TYPE (type);
5810 +  if (TYPE_LOW_BOUND_UNDEFINED (range_type)
5811 +      || TYPE_HIGH_BOUND_UNDEFINED (range_type))
5812 +    return 0;
5813 +  count = TYPE_HIGH_BOUND (range_type) - TYPE_LOW_BOUND (range_type) + 1;
5814 +  /* It may happen for wrong DWARF annotations returning garbage data.  */
5815 +  if (count < 0)
5816 +    warning (_("Range for type %s has invalid bounds %s..%s"),
5817 +            TYPE_ERROR_NAME (type), plongest (TYPE_LOW_BOUND (range_type)),
5818 +            plongest (TYPE_HIGH_BOUND (range_type)));
5819 +  /* The code below does not handle count == 0 right.  */
5820 +  if (count <= 0)
5821 +    return 0;
5822 +  if (full_span || count > 1)
5823 +    {
5824 +      /* We do not use TYPE_ARRAY_BYTE_STRIDE_VALUE (type) here as we want to
5825 +         force FULL_SPAN to 1.  */
5826 +      byte_stride = TYPE_BYTE_STRIDE (range_type);
5827 +      if (byte_stride == 0)
5828 +        {
5829 +         if (target_type == NULL)
5830 +           target_type = check_typedef (TYPE_TARGET_TYPE (type));
5831 +         byte_stride = type_length_get (target_type, NULL, 1);
5832 +       }
5833 +    }
5834 +
5835 +  /* For now, we conservatively take the array length to be 0 if its length
5836 +     exceeds UINT_MAX.  The code below assumes that for x < 0,
5837 +     (ULONGEST) x == -x + ULONGEST_MAX + 1, which is technically not guaranteed
5838 +     by C, but is usually true (because it would be true if x were unsigned
5839 +     with its high-order bit on). It uses the fact that high_bound-low_bound is
5840 +     always representable in ULONGEST and that if high_bound-low_bound+1
5841 +     overflows, it overflows to 0.  We must change these tests if we decide to
5842 +     increase the representation of TYPE_LENGTH from unsigned int to ULONGEST.
5843 +     */
5844 +
5845 +  if (full_span)
5846 +    {
5847 +      retval = count * byte_stride;
5848 +      if (count == 0 || retval / count != byte_stride || retval > UINT_MAX)
5849 +       retval = 0;
5850 +      return retval;
5851 +    }
5852 +  if (target_type == NULL)
5853 +    target_type = check_typedef (TYPE_TARGET_TYPE (type));
5854 +  element_size = type_length_get (target_type, NULL, 1);
5855 +  retval = (count - 1) * byte_stride + element_size;
5856 +  if (retval < element_size
5857 +      || (byte_stride != 0
5858 +          && (retval - element_size) / byte_stride != count - 1)
5859 +      || retval > UINT_MAX)
5860 +    retval = 0;
5861 +  return retval;
5862 +}
5863 +
5864 +/* Prepare TYPE after being read in by the backend.  Currently this function
5865 +   only propagates the TYPE_DYNAMIC flag.  */
5866 +
5867 +void
5868 +finalize_type (struct type *type)
5869 +{
5870 +  int i;
5871 +
5872 +  for (i = 0; i < TYPE_NFIELDS (type); ++i)
5873 +    if (TYPE_FIELD_TYPE (type, i) && TYPE_DYNAMIC (TYPE_FIELD_TYPE (type, i)))
5874 +      break;
5875 +
5876 +  /* FIXME: cplus_stuff is ignored here.  */
5877 +  if (i < TYPE_NFIELDS (type)
5878 +      || (TYPE_VPTR_BASETYPE (type) && TYPE_DYNAMIC (TYPE_VPTR_BASETYPE (type)))
5879 +      || (TYPE_TARGET_TYPE (type) && TYPE_DYNAMIC (TYPE_TARGET_TYPE (type))))
5880 +    TYPE_DYNAMIC (type) = 1;
5881 +}
5882 +
5883  /* Find the real type of TYPE.  This function returns the real type,
5884     after removing all layers of typedefs, and completing opaque or stub
5885     types.  Completion changes the TYPE argument, but stripping of
5886 @@ -1634,52 +1788,37 @@ check_typedef (struct type *type)
5887          }
5888      }
5889  
5890 -  if (TYPE_TARGET_STUB (type))
5891 +  /* copy_type_recursive automatically makes the resulting type containing only
5892 +     constant values expected by the callers of this function.  */
5893 +  if (TYPE_DYNAMIC (type))
5894 +    {
5895 +      htab_t copied_types;
5896 +
5897 +      copied_types = create_copied_types_hash (NULL);
5898 +      type = copy_type_recursive (type, copied_types);
5899 +      htab_delete (copied_types);
5900 +
5901 +      gdb_assert (TYPE_DYNAMIC (type) == 0);
5902 +      /* Force TYPE_LENGTH (type) recalculation.  */
5903 +      TYPE_DYNAMIC (type) = 1;
5904 +    }
5905 +
5906 +  if (TYPE_TARGET_STUB (type) || TYPE_DYNAMIC (type))
5907      {
5908 -      struct type *range_type;
5909        struct type *target_type = check_typedef (TYPE_TARGET_TYPE (type));
5910  
5911 +      if (TYPE_DYNAMIC (type))
5912 +       TYPE_TARGET_TYPE (type) = target_type;
5913        if (TYPE_STUB (target_type) || TYPE_TARGET_STUB (target_type))
5914         {
5915           /* Nothing we can do.  */
5916         }
5917        else if (TYPE_CODE (type) == TYPE_CODE_ARRAY
5918 -              && TYPE_NFIELDS (type) == 1
5919 -              && (TYPE_CODE (range_type = TYPE_INDEX_TYPE (type))
5920 -                  == TYPE_CODE_RANGE))
5921 +              || TYPE_CODE (type) == TYPE_CODE_STRING)
5922         {
5923           /* Now recompute the length of the array type, based on its
5924 -            number of elements and the target type's length.
5925 -            Watch out for Ada null Ada arrays where the high bound
5926 -            is smaller than the low bound.  */
5927 -         const LONGEST low_bound = TYPE_LOW_BOUND (range_type);
5928 -         const LONGEST high_bound = TYPE_HIGH_BOUND (range_type);
5929 -         ULONGEST len;
5930 -
5931 -         if (high_bound < low_bound)
5932 -           len = 0;
5933 -         else
5934 -           {
5935 -             /* For now, we conservatively take the array length to be 0
5936 -                if its length exceeds UINT_MAX.  The code below assumes
5937 -                that for x < 0, (ULONGEST) x == -x + ULONGEST_MAX + 1,
5938 -                which is technically not guaranteed by C, but is usually true
5939 -                (because it would be true if x were unsigned with its
5940 -                high-order bit on).  It uses the fact that
5941 -                high_bound-low_bound is always representable in
5942 -                ULONGEST and that if high_bound-low_bound+1 overflows,
5943 -                it overflows to 0.  We must change these tests if we 
5944 -                decide to increase the representation of TYPE_LENGTH
5945 -                from unsigned int to ULONGEST.  */
5946 -             ULONGEST ulow = low_bound, uhigh = high_bound;
5947 -             ULONGEST tlen = TYPE_LENGTH (target_type);
5948 -
5949 -             len = tlen * (uhigh - ulow + 1);
5950 -             if (tlen == 0 || (len / tlen - 1 + ulow) != uhigh 
5951 -                 || len > UINT_MAX)
5952 -               len = 0;
5953 -           }
5954 -         TYPE_LENGTH (type) = len;
5955 +            number of elements and the target type's length.  */
5956 +         TYPE_LENGTH (type) = type_length_get (type, target_type, 0);
5957           TYPE_TARGET_STUB (type) = 0;
5958         }
5959        else if (TYPE_CODE (type) == TYPE_CODE_RANGE)
5960 @@ -1687,6 +1826,7 @@ check_typedef (struct type *type)
5961           TYPE_LENGTH (type) = TYPE_LENGTH (target_type);
5962           TYPE_TARGET_STUB (type) = 0;
5963         }
5964 +      TYPE_DYNAMIC (type) = 0;
5965      }
5966  
5967    type = make_qualified_type (type, instance_flags, NULL);
5968 @@ -3345,33 +3485,42 @@ type_pair_eq (const void *item_lhs, const void *item_rhs)
5969  }
5970  
5971  /* Allocate the hash table used by copy_type_recursive to walk
5972 -   types without duplicates.  We use OBJFILE's obstack, because
5973 -   OBJFILE is about to be deleted.  */
5974 +   types without duplicates.   */
5975  
5976  htab_t
5977  create_copied_types_hash (struct objfile *objfile)
5978  {
5979 -  return htab_create_alloc_ex (1, type_pair_hash, type_pair_eq,
5980 -                              NULL, &objfile->objfile_obstack,
5981 -                              hashtab_obstack_allocate,
5982 -                              dummy_obstack_deallocate);
5983 +  if (objfile == NULL)
5984 +    {
5985 +      /* NULL OBJFILE is for TYPE_DYNAMIC types already contained in
5986 +        OBJFILE_MALLOC memory, such as those from VALUE_HISTORY_CHAIN.  Table
5987 +        element entries get allocated by xmalloc - so use xfree.  */
5988 +      return htab_create (1, type_pair_hash, type_pair_eq, xfree);
5989 +    }
5990 +  else
5991 +    {
5992 +      /* Use OBJFILE's obstack, because OBJFILE is about to be deleted.  Table
5993 +        element entries get allocated by xmalloc - so use xfree.  */
5994 +      return htab_create_alloc_ex (1, type_pair_hash, type_pair_eq,
5995 +                                  xfree, &objfile->objfile_obstack,
5996 +                                  hashtab_obstack_allocate,
5997 +                                  dummy_obstack_deallocate);
5998 +    }
5999  }
6000  
6001 -/* Recursively copy (deep copy) TYPE, if it is associated with
6002 -   OBJFILE.  Return a new type allocated using malloc, a saved type if
6003 -   we have already visited TYPE (using COPIED_TYPES), or TYPE if it is
6004 -   not associated with OBJFILE.  */
6005 +/* A helper for copy_type_recursive.  This does all the work.  OBJFILE is used
6006 +   only for an assertion checking.  */
6007  
6008 -struct type *
6009 -copy_type_recursive (struct objfile *objfile, 
6010 -                    struct type *type,
6011 -                    htab_t copied_types)
6012 +static struct type *
6013 +copy_type_recursive_1 (struct objfile *objfile, 
6014 +                      struct type *type,
6015 +                      htab_t copied_types)
6016  {
6017    struct type_pair *stored, pair;
6018    void **slot;
6019    struct type *new_type;
6020  
6021 -  if (! TYPE_OBJFILE_OWNED (type))
6022 +  if (! TYPE_OBJFILE_OWNED (type) && !TYPE_DYNAMIC (type))
6023      return type;
6024  
6025    /* This type shouldn't be pointing to any types in other objfiles;
6026 @@ -3386,9 +3535,10 @@ copy_type_recursive (struct objfile *objfile,
6027    new_type = alloc_type_arch (get_type_arch (type));
6028  
6029    /* We must add the new type to the hash table immediately, in case
6030 -     we encounter this type again during a recursive call below.  */
6031 -  stored
6032 -    = obstack_alloc (&objfile->objfile_obstack, sizeof (struct type_pair));
6033 +     we encounter this type again during a recursive call below.  Memory could
6034 +     be allocated from OBJFILE in the case we will be removing OBJFILE, this
6035 +     optimization is missed and xfree is called for it from COPIED_TYPES.  */
6036 +  stored = xmalloc (sizeof (*stored));
6037    stored->old = type;
6038    stored->new = new_type;
6039    *slot = stored;
6040 @@ -3399,6 +3549,21 @@ copy_type_recursive (struct objfile *objfile,
6041    TYPE_OBJFILE_OWNED (new_type) = 0;
6042    TYPE_OWNER (new_type).gdbarch = get_type_arch (type);
6043  
6044 +#if 0
6045 +  /* TYPE_MAIN_TYPE memory copy above rewrote the TYPE_DISCARDABLE flag so we
6046 +     need to initialize it again.  And even if TYPE was already discardable
6047 +     NEW_TYPE so far is not registered in TYPE_DISCARDABLE_TABLE.  */
6048 +  TYPE_DISCARDABLE (new_type) = 0;
6049 +  set_type_as_discardable (new_type);
6050 +#endif
6051 +
6052 +  /* Pre-clear the fields processed by delete_main_type.  If DWARF block
6053 +     evaluations below call error we would leave an unfreeable TYPE.  */
6054 +  TYPE_TARGET_TYPE (new_type) = NULL;
6055 +  TYPE_VPTR_BASETYPE (new_type) = NULL;
6056 +  TYPE_NFIELDS (new_type) = 0;
6057 +  TYPE_FIELDS (new_type) = NULL;
6058 +
6059    if (TYPE_NAME (type))
6060      TYPE_NAME (new_type) = xstrdup (TYPE_NAME (type));
6061    if (TYPE_TAG_NAME (type))
6062 @@ -3407,12 +3572,48 @@ copy_type_recursive (struct objfile *objfile,
6063    TYPE_INSTANCE_FLAGS (new_type) = TYPE_INSTANCE_FLAGS (type);
6064    TYPE_LENGTH (new_type) = TYPE_LENGTH (type);
6065  
6066 +  if (TYPE_ALLOCATED (new_type))
6067 +    {
6068 +      gdb_assert (!TYPE_NOT_ALLOCATED (new_type));
6069 +
6070 +      if (!dwarf_locexpr_baton_eval (TYPE_ALLOCATED (new_type)))
6071 +        TYPE_NOT_ALLOCATED (new_type) = 1;
6072 +      TYPE_ALLOCATED (new_type) = NULL;
6073 +    }
6074 +
6075 +  if (TYPE_ASSOCIATED (new_type))
6076 +    {
6077 +      gdb_assert (!TYPE_NOT_ASSOCIATED (new_type));
6078 +
6079 +      if (!dwarf_locexpr_baton_eval (TYPE_ASSOCIATED (new_type)))
6080 +        TYPE_NOT_ASSOCIATED (new_type) = 1;
6081 +      TYPE_ASSOCIATED (new_type) = NULL;
6082 +    }
6083 +
6084 +  if (!TYPE_DATA_LOCATION_IS_ADDR (new_type)
6085 +      && TYPE_DATA_LOCATION_DWARF_BLOCK (new_type))
6086 +    {
6087 +      if (TYPE_NOT_ALLOCATED (new_type)
6088 +          || TYPE_NOT_ASSOCIATED (new_type))
6089 +       TYPE_DATA_LOCATION_DWARF_BLOCK (new_type) = NULL;
6090 +      else
6091 +       {
6092 +         TYPE_DATA_LOCATION_IS_ADDR (new_type) = 1;
6093 +         TYPE_DATA_LOCATION_ADDR (new_type) = dwarf_locexpr_baton_eval
6094 +                                   (TYPE_DATA_LOCATION_DWARF_BLOCK (new_type));
6095 +       }
6096 +    }
6097 +
6098    /* Copy the fields.  */
6099    if (TYPE_NFIELDS (type))
6100      {
6101        int i, nfields;
6102  
6103 +      /* TYPE_CODE_RANGE uses TYPE_RANGE_DATA of the union with TYPE_FIELDS.  */
6104 +      gdb_assert (TYPE_CODE (type) != TYPE_CODE_RANGE);
6105 +
6106        nfields = TYPE_NFIELDS (type);
6107 +      TYPE_NFIELDS (new_type) = nfields;
6108        TYPE_FIELDS (new_type) = XCALLOC (nfields, struct field);
6109        for (i = 0; i < nfields; i++)
6110         {
6111 @@ -3421,8 +3622,8 @@ copy_type_recursive (struct objfile *objfile,
6112           TYPE_FIELD_BITSIZE (new_type, i) = TYPE_FIELD_BITSIZE (type, i);
6113           if (TYPE_FIELD_TYPE (type, i))
6114             TYPE_FIELD_TYPE (new_type, i)
6115 -             = copy_type_recursive (objfile, TYPE_FIELD_TYPE (type, i),
6116 -                                    copied_types);
6117 +             = copy_type_recursive_1 (objfile, TYPE_FIELD_TYPE (type, i),
6118 +                                      copied_types);
6119           if (TYPE_FIELD_NAME (type, i))
6120             TYPE_FIELD_NAME (new_type, i) = 
6121               xstrdup (TYPE_FIELD_NAME (type, i));
6122 @@ -3453,24 +3654,184 @@ copy_type_recursive (struct objfile *objfile,
6123         }
6124      }
6125  
6126 +  /* Both FIELD_LOC_KIND_DWARF_BLOCK and TYPE_RANGE_HIGH_BOUND_IS_COUNT were
6127 +     possibly converted.  */
6128 +  TYPE_DYNAMIC (new_type) = 0;
6129 +
6130    /* For range types, copy the bounds information.  */
6131 -  if (TYPE_CODE (type) == TYPE_CODE_RANGE)
6132 +  if (TYPE_CODE (new_type) == TYPE_CODE_RANGE)
6133      {
6134        TYPE_RANGE_DATA (new_type) = xmalloc (sizeof (struct range_bounds));
6135        *TYPE_RANGE_DATA (new_type) = *TYPE_RANGE_DATA (type);
6136 +
6137 +      switch (TYPE_RANGE_DATA (new_type)->low.kind)
6138 +       {
6139 +       case RANGE_BOUND_KIND_CONSTANT:
6140 +         break;
6141 +       case RANGE_BOUND_KIND_DWARF_BLOCK:
6142 +         /* `struct dwarf2_locexpr_baton' is too bound to its objfile so
6143 +            it is expected to be made constant by CHECK_TYPEDEF.
6144 +            TYPE_NOT_ALLOCATED and TYPE_NOT_ASSOCIATED are not valid for TYPE.
6145 +            */
6146 +         if (TYPE_NOT_ALLOCATED (new_type) || TYPE_NOT_ASSOCIATED (new_type)
6147 +             || ! has_stack_frames ())
6148 +           {
6149 +             /* We should set 1 for Fortran but how to find the language?  */
6150 +             TYPE_LOW_BOUND (new_type) = 0;
6151 +             TYPE_LOW_BOUND_UNDEFINED (new_type) = 1;
6152 +           }
6153 +         else
6154 +           {
6155 +             TYPE_LOW_BOUND (new_type) = dwarf_locexpr_baton_eval
6156 +                               (TYPE_RANGE_DATA (new_type)->low.u.dwarf_block);
6157 +             if (TYPE_LOW_BOUND (new_type) >= 0)
6158 +               TYPE_UNSIGNED (new_type) = 1;
6159 +           }
6160 +         TYPE_RANGE_DATA (new_type)->low.kind = RANGE_BOUND_KIND_CONSTANT;
6161 +         break;
6162 +       case RANGE_BOUND_KIND_DWARF_LOCLIST:
6163 +         {
6164 +           CORE_ADDR addr;
6165 +
6166 +           /* `struct dwarf2_loclist_baton' is too bound to its objfile so
6167 +              it is expected to be made constant by CHECK_TYPEDEF.
6168 +              TYPE_NOT_ALLOCATED and TYPE_NOT_ASSOCIATED are not valid for TYPE.
6169 +              */
6170 +           if (! TYPE_NOT_ALLOCATED (new_type)
6171 +               && ! TYPE_NOT_ASSOCIATED (new_type) && has_stack_frames ()
6172 +               && dwarf_loclist_baton_eval
6173 +                 (TYPE_RANGE_DATA (new_type)->low.u.dwarf_loclist.loclist,
6174 +                  TYPE_RANGE_DATA (new_type)->low.u.dwarf_loclist.type, &addr))
6175 +             {
6176 +               TYPE_LOW_BOUND (new_type) = addr;
6177 +               if (TYPE_LOW_BOUND (new_type) >= 0)
6178 +                 TYPE_UNSIGNED (new_type) = 1;
6179 +             }
6180 +           else
6181 +             {
6182 +               /* We should set 1 for Fortran but how to find the language?  */
6183 +               TYPE_LOW_BOUND (new_type) = 0;
6184 +               TYPE_LOW_BOUND_UNDEFINED (new_type) = 1;
6185 +             }
6186 +           TYPE_RANGE_DATA (new_type)->low.kind = RANGE_BOUND_KIND_CONSTANT;
6187 +         }
6188 +         break;
6189 +       }
6190 +
6191 +      switch (TYPE_RANGE_DATA (new_type)->high.kind)
6192 +       {
6193 +       case RANGE_BOUND_KIND_CONSTANT:
6194 +         break;
6195 +       case RANGE_BOUND_KIND_DWARF_BLOCK:
6196 +         /* `struct dwarf2_locexpr_baton' is too bound to its objfile so
6197 +            it is expected to be made constant by CHECK_TYPEDEF.
6198 +            TYPE_NOT_ALLOCATED and TYPE_NOT_ASSOCIATED are not valid for TYPE.
6199 +            */
6200 +         if (TYPE_NOT_ALLOCATED (new_type) || TYPE_NOT_ASSOCIATED (new_type)
6201 +             || ! has_stack_frames ())
6202 +           {
6203 +             TYPE_HIGH_BOUND (new_type) = TYPE_LOW_BOUND (new_type) - 1;
6204 +             TYPE_HIGH_BOUND_UNDEFINED (new_type) = 1;
6205 +           }
6206 +         else
6207 +           TYPE_HIGH_BOUND (new_type) = dwarf_locexpr_baton_eval
6208 +                              (TYPE_RANGE_DATA (new_type)->high.u.dwarf_block);
6209 +         TYPE_RANGE_DATA (new_type)->high.kind = RANGE_BOUND_KIND_CONSTANT;
6210 +         break;
6211 +       case RANGE_BOUND_KIND_DWARF_LOCLIST:
6212 +         {
6213 +           CORE_ADDR addr;
6214 +
6215 +           /* `struct dwarf2_loclist_baton' is too bound to its objfile so
6216 +              it is expected to be made constant by CHECK_TYPEDEF.
6217 +              TYPE_NOT_ALLOCATED and TYPE_NOT_ASSOCIATED are not valid for TYPE.
6218 +              */
6219 +           if (! TYPE_NOT_ALLOCATED (new_type)
6220 +               && ! TYPE_NOT_ASSOCIATED (new_type) && has_stack_frames ()
6221 +               && dwarf_loclist_baton_eval
6222 +                     (TYPE_RANGE_DATA (new_type)->high.u.dwarf_loclist.loclist,
6223 +                      TYPE_RANGE_DATA (new_type)->high.u.dwarf_loclist.type,
6224 +                      &addr))
6225 +             TYPE_HIGH_BOUND (new_type) = addr;
6226 +           else
6227 +             {
6228 +               TYPE_HIGH_BOUND (new_type) = TYPE_LOW_BOUND (new_type) - 1;
6229 +               TYPE_HIGH_BOUND_UNDEFINED (new_type) = 1;
6230 +             }
6231 +           TYPE_RANGE_DATA (new_type)->high.kind = RANGE_BOUND_KIND_CONSTANT;
6232 +         }
6233 +         break;
6234 +       }
6235 +
6236 +      switch (TYPE_RANGE_DATA (new_type)->byte_stride.kind)
6237 +       {
6238 +       case RANGE_BOUND_KIND_CONSTANT:
6239 +         break;
6240 +       case RANGE_BOUND_KIND_DWARF_BLOCK:
6241 +         /* `struct dwarf2_locexpr_baton' is too bound to its objfile so
6242 +            it is expected to be made constant by CHECK_TYPEDEF.
6243 +            TYPE_NOT_ALLOCATED and TYPE_NOT_ASSOCIATED are not valid for TYPE.
6244 +            */
6245 +         if (TYPE_NOT_ALLOCATED (new_type) || TYPE_NOT_ASSOCIATED (new_type)
6246 +             || ! has_stack_frames ())
6247 +           TYPE_BYTE_STRIDE (new_type) = 0;
6248 +         else
6249 +           TYPE_BYTE_STRIDE (new_type) = dwarf_locexpr_baton_eval
6250 +                       (TYPE_RANGE_DATA (new_type)->byte_stride.u.dwarf_block);
6251 +         TYPE_RANGE_DATA (new_type)->byte_stride.kind
6252 +           = RANGE_BOUND_KIND_CONSTANT;
6253 +         break;
6254 +       case RANGE_BOUND_KIND_DWARF_LOCLIST:
6255 +         {
6256 +           CORE_ADDR addr = 0;
6257 +
6258 +           /* `struct dwarf2_loclist_baton' is too bound to its objfile so
6259 +              it is expected to be made constant by CHECK_TYPEDEF.
6260 +              TYPE_NOT_ALLOCATED and TYPE_NOT_ASSOCIATED are not valid for TYPE.
6261 +              */
6262 +           if (! TYPE_NOT_ALLOCATED (new_type)
6263 +               && ! TYPE_NOT_ASSOCIATED (new_type) && has_stack_frames ())
6264 +             dwarf_loclist_baton_eval
6265 +              (TYPE_RANGE_DATA (new_type)->byte_stride.u.dwarf_loclist.loclist,
6266 +               TYPE_RANGE_DATA (new_type)->byte_stride.u.dwarf_loclist.type,
6267 +               &addr);
6268 +           TYPE_BYTE_STRIDE (new_type) = addr;
6269 +           TYPE_RANGE_DATA (new_type)->byte_stride.kind
6270 +             = RANGE_BOUND_KIND_CONSTANT;
6271 +         }
6272 +         break;
6273 +       }
6274 +
6275 +      /* Convert TYPE_RANGE_HIGH_BOUND_IS_COUNT into a regular bound.  */
6276 +      if (TYPE_RANGE_HIGH_BOUND_IS_COUNT (new_type))
6277 +       {
6278 +         TYPE_HIGH_BOUND (new_type) = TYPE_LOW_BOUND (new_type)
6279 +                                      + TYPE_HIGH_BOUND (new_type) - 1;
6280 +         TYPE_RANGE_HIGH_BOUND_IS_COUNT (new_type) = 0;
6281 +       }
6282      }
6283  
6284    /* Copy pointers to other types.  */
6285    if (TYPE_TARGET_TYPE (type))
6286      TYPE_TARGET_TYPE (new_type) = 
6287 -      copy_type_recursive (objfile, 
6288 -                          TYPE_TARGET_TYPE (type),
6289 -                          copied_types);
6290 +      copy_type_recursive_1 (objfile, 
6291 +                            TYPE_TARGET_TYPE (type),
6292 +                            copied_types);
6293    if (TYPE_VPTR_BASETYPE (type))
6294      TYPE_VPTR_BASETYPE (new_type) = 
6295 -      copy_type_recursive (objfile,
6296 -                          TYPE_VPTR_BASETYPE (type),
6297 -                          copied_types);
6298 +      copy_type_recursive_1 (objfile,
6299 +                            TYPE_VPTR_BASETYPE (type),
6300 +                            copied_types);
6301 +
6302 +  if (TYPE_CODE (new_type) == TYPE_CODE_ARRAY)
6303 +    {
6304 +      struct type *new_index_type = TYPE_INDEX_TYPE (new_type);
6305 +
6306 +      if (TYPE_BYTE_STRIDE (new_index_type) == 0)
6307 +       TYPE_BYTE_STRIDE (new_index_type)
6308 +         = TYPE_LENGTH (TYPE_TARGET_TYPE (new_type));
6309 +    }
6310 +
6311    /* Maybe copy the type_specific bits.
6312  
6313       NOTE drow/2005-12-09: We do not copy the C++-specific bits like
6314 @@ -3487,6 +3848,17 @@ copy_type_recursive (struct objfile *objfile,
6315    return new_type;
6316  }
6317  
6318 +/* Recursively copy (deep copy) TYPE.  Return a new type allocated using
6319 +   malloc, a saved type if we have already visited TYPE (using COPIED_TYPES),
6320 +   or TYPE if it is not associated with OBJFILE.  */
6321 +
6322 +struct type *
6323 +copy_type_recursive (struct type *type,
6324 +                    htab_t copied_types)
6325 +{
6326 +  return copy_type_recursive_1 (TYPE_OBJFILE (type), type, copied_types);
6327 +}
6328 +
6329  /* Make a copy of the given TYPE, except that the pointer & reference
6330     types are not preserved.
6331     
6332 @@ -3509,6 +3881,201 @@ copy_type (const struct type *type)
6333    return new_type;
6334  }
6335  
6336 +#if 0
6337 +/* Callback type for main_type_crawl.  */
6338 +typedef int (*main_type_crawl_iter) (struct type *type, void *data);
6339 +
6340 +/* Iterate all main_type structures reachable through any `struct type *' from
6341 +   TYPE.  ITER will be called only for one type of each main_type, use
6342 +   TYPE_CHAIN traversal to find all the type instances.  ITER is being called
6343 +   for each main_type found.  ITER returns non-zero if main_type_crawl should
6344 +   depth-first enter the specific type.  ITER must provide some detection for
6345 +   reentering the same main_type as this function would otherwise endlessly
6346 +   loop.  */
6347 +
6348 +static void
6349 +main_type_crawl (struct type *type, main_type_crawl_iter iter, void *data)
6350 +{
6351 +  struct type *type_iter;
6352 +  int i;
6353 +
6354 +  if (!type)
6355 +    return;
6356 +
6357 +  gdb_assert (TYPE_OBJFILE (type) == NULL);
6358 +
6359 +  /* `struct cplus_struct_type' handling is unsupported by this function.  */
6360 +  gdb_assert ((TYPE_CODE (type) != TYPE_CODE_STRUCT
6361 +              && TYPE_CODE (type) != TYPE_CODE_UNION)
6362 +             || !HAVE_CPLUS_STRUCT (type));
6363 +
6364 +  if (!(*iter) (type, data))
6365 +    return;
6366 +
6367 +  /* Iterate all the type instances of this main_type.  */
6368 +  type_iter = type;
6369 +  do
6370 +    {
6371 +      gdb_assert (TYPE_MAIN_TYPE (type_iter) == TYPE_MAIN_TYPE (type));
6372 +
6373 +      main_type_crawl (TYPE_POINTER_TYPE (type), iter, data);
6374 +      main_type_crawl (TYPE_REFERENCE_TYPE (type), iter, data);
6375 +
6376 +      type_iter = TYPE_CHAIN (type_iter);
6377 +    }
6378 +  while (type_iter != type);
6379 +
6380 +  for (i = 0; i < TYPE_NFIELDS (type); i++)
6381 +    main_type_crawl (TYPE_FIELD_TYPE (type, i), iter, data);
6382 +
6383 +  main_type_crawl (TYPE_TARGET_TYPE (type), iter, data);
6384 +  main_type_crawl (TYPE_VPTR_BASETYPE (type), iter, data);
6385 +}
6386 +
6387 +/* A helper for delete_type which deletes a main_type and the things to which
6388 +   it refers.  TYPE is a type whose main_type we wish to destroy.  */
6389 +
6390 +static void
6391 +delete_main_type (struct type *type)
6392 +{
6393 +  int i;
6394 +
6395 +  gdb_assert (TYPE_DISCARDABLE (type));
6396 +  gdb_assert (TYPE_OBJFILE (type) == NULL);
6397 +
6398 +  xfree (TYPE_NAME (type));
6399 +  xfree (TYPE_TAG_NAME (type));
6400 +
6401 +  for (i = 0; i < TYPE_NFIELDS (type); ++i)
6402 +    {
6403 +      xfree (TYPE_FIELD_NAME (type, i));
6404 +
6405 +      if (TYPE_FIELD_LOC_KIND (type, i) == FIELD_LOC_KIND_PHYSNAME)
6406 +       xfree (TYPE_FIELD_STATIC_PHYSNAME (type, i));
6407 +    }
6408 +  xfree (TYPE_FIELDS (type));
6409 +
6410 +  gdb_assert (!HAVE_CPLUS_STRUCT (type));
6411 +
6412 +  xfree (TYPE_MAIN_TYPE (type));
6413 +}
6414 +
6415 +/* Delete all the instances on TYPE_CHAIN of TYPE, including their referenced
6416 +   main_type.  TYPE must be a reclaimable type - neither permanent nor objfile
6417 +   associated.  */
6418 +
6419 +static void
6420 +delete_type_chain (struct type *type)
6421 +{
6422 +  struct type *type_iter, *type_iter_to_free;
6423 +
6424 +  gdb_assert (TYPE_DISCARDABLE (type));
6425 +  gdb_assert (TYPE_OBJFILE (type) == NULL);
6426 +
6427 +  delete_main_type (type);
6428 +
6429 +  type_iter = type;
6430 +  do
6431 +    {
6432 +      type_iter_to_free = type_iter;
6433 +      type_iter = TYPE_CHAIN (type_iter);
6434 +      xfree (type_iter_to_free);
6435 +    }
6436 +  while (type_iter != type);
6437 +}
6438 +
6439 +/* Hash function for type_discardable_table.  */
6440 +
6441 +static hashval_t
6442 +type_discardable_hash (const void *p)
6443 +{
6444 +  const struct type *type = p;
6445 +
6446 +  return htab_hash_pointer (TYPE_MAIN_TYPE (type));
6447 +}
6448 +
6449 +/* Equality function for type_discardable_table.  */
6450 +
6451 +static int
6452 +type_discardable_equal (const void *a, const void *b)
6453 +{
6454 +  const struct type *left = a;
6455 +  const struct type *right = b;
6456 +
6457 +  return TYPE_MAIN_TYPE (left) == TYPE_MAIN_TYPE (right);
6458 +}
6459 +
6460 +/* A helper for type_mark_used.  */
6461 +
6462 +static int
6463 +type_mark_used_crawl (struct type *type, void *unused)
6464 +{
6465 +  if (!TYPE_DISCARDABLE (type))
6466 +    return 0;
6467 +
6468 +  if (TYPE_DISCARDABLE_AGE (type) == type_discardable_age_current)
6469 +    return 0;
6470 +
6471 +  TYPE_DISCARDABLE_AGE (type) = type_discardable_age_current;
6472 +
6473 +  /* Continue the traversal.  */
6474 +  return 1;
6475 +}
6476 +
6477 +/* Mark TYPE and its connected types as used in this free_all_types pass.  */
6478 +
6479 +void
6480 +type_mark_used (struct type *type)
6481 +{
6482 +  if (type == NULL)
6483 +    return;
6484 +
6485 +  if (!TYPE_DISCARDABLE (type))
6486 +    return;
6487 +
6488 +  main_type_crawl (type, type_mark_used_crawl, NULL);
6489 +}
6490 +
6491 +/* A traverse callback for type_discardable_table which removes any
6492 +   type_discardable whose reference count is now zero (unused link).  */
6493 +
6494 +static int
6495 +type_discardable_remove (void **slot, void *unused)
6496 +{
6497 +  struct type *type = *slot;
6498 +
6499 +  gdb_assert (TYPE_DISCARDABLE (type));
6500 +
6501 +  if (TYPE_DISCARDABLE_AGE (type) != type_discardable_age_current)
6502 +    {
6503 +      delete_type_chain (type);
6504 +
6505 +      htab_clear_slot (type_discardable_table, slot);
6506 +    }
6507 +
6508 +  return 1;
6509 +}
6510 +
6511 +/* Free all the reclaimable types that have been allocated and that have
6512 +   currently zero reference counter.
6513 +
6514 +   This function is called after each command, successful or not.  Use this
6515 +   cleanup only in the GDB idle state as GDB only marks those types used by
6516 +   globally tracked objects (with no autovariable references tracking).  */
6517 +
6518 +void
6519 +free_all_types (void)
6520 +{
6521 +  /* Mark a new pass.  As GDB checks all the entries were visited after each
6522 +     pass there cannot be any stale entries already containing the changed
6523 +     value.  */
6524 +  type_discardable_age_current ^= 1;
6525 +
6526 +  observer_notify_mark_used ();
6527 +
6528 +  htab_traverse (type_discardable_table, type_discardable_remove, NULL);
6529 +}
6530 +#endif
6531  
6532  /* Helper functions to initialize architecture-specific types.  */
6533  
6534 @@ -4042,6 +4609,13 @@ void
6535  _initialize_gdbtypes (void)
6536  {
6537    gdbtypes_data = gdbarch_data_register_post_init (gdbtypes_post_init);
6538 +
6539 +#if 0
6540 +  type_discardable_table = htab_create_alloc (20, type_discardable_hash,
6541 +                                            type_discardable_equal, NULL,
6542 +                                            xcalloc, xfree);
6543 +#endif
6544 +
6545    objfile_type_data = register_objfile_data ();
6546  
6547    add_setshow_zinteger_cmd ("overload", no_class, &overload_debug,
6548 diff --git a/gdb/gdbtypes.h b/gdb/gdbtypes.h
6549 index 17bfbc5..72e9cc5 100644
6550 --- a/gdb/gdbtypes.h
6551 +++ b/gdb/gdbtypes.h
6552 @@ -228,6 +228,11 @@ enum type_instance_flag_value
6553  
6554  #define TYPE_TARGET_STUB(t)    (TYPE_MAIN_TYPE (t)->flag_target_stub)
6555  
6556 +/* Type needs to be evaluated on each CHECK_TYPEDEF and its results must not be
6557 +   sticky.  */
6558 +
6559 +#define TYPE_DYNAMIC(t)                (TYPE_MAIN_TYPE (t)->flag_dynamic)
6560 +
6561  /* Static type.  If this is set, the corresponding type had 
6562     a static modifier.
6563     Note: This may be unnecessary, since static data members
6564 @@ -311,6 +316,50 @@ enum type_instance_flag_value
6565  
6566  #define TYPE_FLAG_ENUM(t) (TYPE_MAIN_TYPE (t)->flag_flag_enum)
6567  
6568 +#if 0
6569 +/* Define this type as being reclaimable during free_all_types.  Type is
6570 +   required to be have TYPE_OBJFILE set to NULL.  Setting this flag requires
6571 +   initializing TYPE_DISCARDABLE_AGE, see alloc_type_discardable.  */
6572 +
6573 +#define TYPE_DISCARDABLE(t)    (TYPE_MAIN_TYPE (t)->flag_discardable)
6574 +
6575 +/* Marker this type has been visited by the type_mark_used by this
6576 +   mark-and-sweep types garbage collecting pass.  Current pass is represented
6577 +   by TYPE_DISCARDABLE_AGE_CURRENT.  */
6578 +
6579 +#define TYPE_DISCARDABLE_AGE(t)        (TYPE_MAIN_TYPE (t)->flag_discardable_age)
6580 +#endif
6581 +
6582 +/* Is HIGH_BOUND a low-bound relative count (1) or the high bound itself (0)?  */
6583 +
6584 +#define TYPE_RANGE_HIGH_BOUND_IS_COUNT(range_type) \
6585 +  (TYPE_MAIN_TYPE (range_type)->flag_range_high_bound_is_count)
6586 +
6587 +/* Not allocated.  TYPE_ALLOCATED(t) must be NULL in such case.  If this flag
6588 +   is unset and TYPE_ALLOCATED(t) is NULL then the type is allocated.  If this
6589 +   flag is unset and TYPE_ALLOCATED(t) is not NULL then its DWARF block
6590 +   determines the actual allocation state.  */
6591 +
6592 +#define TYPE_NOT_ALLOCATED(t)  (TYPE_MAIN_TYPE (t)->flag_not_allocated)
6593 +
6594 +/* Not associated.  TYPE_ASSOCIATED(t) must be NULL in such case.  If this flag
6595 +   is unset and TYPE_ASSOCIATED(t) is NULL then the type is associated.  If
6596 +   this flag is unset and TYPE_ASSOCIATED(t) is not NULL then its DWARF block
6597 +   determines the actual association state.  */
6598 +
6599 +#define TYPE_NOT_ASSOCIATED(t) (TYPE_MAIN_TYPE (t)->flag_not_associated)
6600 +
6601 +/* Address of the actual data as for DW_AT_data_location.  Its dwarf block must
6602 +   not be evaluated unless both TYPE_NOT_ALLOCATED and TYPE_NOT_ASSOCIATED are
6603 +   false.  If TYPE_DATA_LOCATION_IS_ADDR set then TYPE_DATA_LOCATION_ADDR value
6604 +   is the actual data address value.  If unset and
6605 +   TYPE_DATA_LOCATION_DWARF_BLOCK is NULL then the value is the normal
6606 +   value_raw_address.  If unset and TYPE_DATA_LOCATION_DWARF_BLOCK is not NULL
6607 +   then its DWARF block determines the actual data address.  */
6608 +
6609 +#define TYPE_DATA_LOCATION_IS_ADDR(t) \
6610 +  (TYPE_MAIN_TYPE (t)->flag_data_location_is_addr)
6611 +
6612  /* Constant type.  If this is set, the corresponding type has a
6613     const modifier.  */
6614  
6615 @@ -421,6 +470,15 @@ struct main_type
6616    /* True if this type was declared with "class" rather than
6617       "struct".  */
6618    unsigned int flag_declared_class : 1;
6619 +#if 0
6620 +  unsigned int flag_discardable : 1;
6621 +  unsigned int flag_discardable_age : 1;
6622 +#endif
6623 +  unsigned int flag_dynamic : 1;
6624 +  unsigned int flag_range_high_bound_is_count : 1;
6625 +  unsigned int flag_not_allocated : 1;
6626 +  unsigned int flag_not_associated : 1;
6627 +  unsigned int flag_data_location_is_addr : 1;
6628  
6629    /* True if this is an enum type with disjoint values.  This affects
6630       how the enum is printed.  */
6631 @@ -501,6 +559,20 @@ struct main_type
6632  
6633    struct type *target_type;
6634  
6635 +  /* For DW_AT_data_location.  */
6636 +  union
6637 +    {
6638 +      struct dwarf2_locexpr_baton *dwarf_block;
6639 +      CORE_ADDR addr;
6640 +    }
6641 +  data_location;
6642 +
6643 +  /* For DW_AT_allocated.  */
6644 +  struct dwarf2_locexpr_baton *allocated;
6645 +
6646 +  /* For DW_AT_associated.  */
6647 +  struct dwarf2_locexpr_baton *associated;
6648 +
6649    /* For structure and union types, a description of each field.
6650       For set and pascal array types, there is one "field",
6651       whose type is the domain type of the set or array.
6652 @@ -583,13 +655,34 @@ struct main_type
6653  
6654      struct range_bounds
6655      {
6656 +      struct
6657 +       {
6658 +         union
6659 +           {
6660 +             LONGEST constant;
6661 +             struct dwarf2_locexpr_baton *dwarf_block;
6662 +             struct
6663 +               {
6664 +                 struct dwarf2_loclist_baton *loclist;
6665 +                 struct type *type;
6666 +               }
6667 +             dwarf_loclist;
6668 +           }
6669 +         u;
6670 +         enum range_bound_kind
6671 +           {
6672 +             RANGE_BOUND_KIND_CONSTANT,
6673 +             RANGE_BOUND_KIND_DWARF_BLOCK,
6674 +             RANGE_BOUND_KIND_DWARF_LOCLIST
6675 +           }
6676 +         kind;
6677 +       }
6678        /* Low bound of range.  */
6679 -
6680 -      LONGEST low;
6681 -
6682 +      low,
6683        /* High bound of range.  */
6684 -
6685 -      LONGEST high;
6686 +      high,
6687 +      /* Byte stride of range.  */
6688 +      byte_stride;
6689  
6690        /* Flags indicating whether the values of low and high are
6691           valid.  When true, the respective range value is
6692 @@ -1054,9 +1147,9 @@ extern void allocate_gnat_aux_type (struct type *);
6693  #define TYPE_POINTER_TYPE(thistype) (thistype)->pointer_type
6694  #define TYPE_REFERENCE_TYPE(thistype) (thistype)->reference_type
6695  #define TYPE_CHAIN(thistype) (thistype)->chain
6696 -/* Note that if thistype is a TYPEDEF type, you have to call check_typedef.
6697 -   But check_typedef does set the TYPE_LENGTH of the TYPEDEF type,
6698 -   so you only have to call check_typedef once.  Since allocate_value
6699 +/* Note that if thistype is a TYPEDEF, ARRAY or STRING type, you have to call
6700 +   check_typedef.  But check_typedef does set the TYPE_LENGTH of the TYPEDEF
6701 +   type, so you only have to call check_typedef once.  Since allocate_value
6702     calls check_typedef, TYPE_LENGTH (VALUE_TYPE (X)) is safe.  */
6703  #define TYPE_LENGTH(thistype) (thistype)->length
6704  /* Note that TYPE_CODE can be TYPE_CODE_TYPEDEF, so if you want the real
6705 @@ -1064,11 +1157,16 @@ extern void allocate_gnat_aux_type (struct type *);
6706  #define TYPE_CODE(thistype) TYPE_MAIN_TYPE(thistype)->code
6707  #define TYPE_NFIELDS(thistype) TYPE_MAIN_TYPE(thistype)->nfields
6708  #define TYPE_FIELDS(thistype) TYPE_MAIN_TYPE(thistype)->flds_bnds.fields
6709 +#define TYPE_DATA_LOCATION_DWARF_BLOCK(thistype) TYPE_MAIN_TYPE (thistype)->data_location.dwarf_block
6710 +#define TYPE_DATA_LOCATION_ADDR(thistype) TYPE_MAIN_TYPE (thistype)->data_location.addr
6711 +#define TYPE_ALLOCATED(thistype) TYPE_MAIN_TYPE (thistype)->allocated
6712 +#define TYPE_ASSOCIATED(thistype) TYPE_MAIN_TYPE (thistype)->associated
6713  
6714  #define TYPE_INDEX_TYPE(type) TYPE_FIELD_TYPE (type, 0)
6715  #define TYPE_RANGE_DATA(thistype) TYPE_MAIN_TYPE(thistype)->flds_bnds.bounds
6716 -#define TYPE_LOW_BOUND(range_type) TYPE_RANGE_DATA(range_type)->low
6717 -#define TYPE_HIGH_BOUND(range_type) TYPE_RANGE_DATA(range_type)->high
6718 +#define TYPE_LOW_BOUND(range_type) TYPE_RANGE_DATA(range_type)->low.u.constant
6719 +#define TYPE_HIGH_BOUND(range_type) TYPE_RANGE_DATA(range_type)->high.u.constant
6720 +#define TYPE_BYTE_STRIDE(range_type) TYPE_RANGE_DATA(range_type)->byte_stride.u.constant
6721  #define TYPE_LOW_BOUND_UNDEFINED(range_type) \
6722     TYPE_RANGE_DATA(range_type)->low_undefined
6723  #define TYPE_HIGH_BOUND_UNDEFINED(range_type) \
6724 @@ -1085,7 +1183,14 @@ extern void allocate_gnat_aux_type (struct type *);
6725     (TYPE_HIGH_BOUND(TYPE_INDEX_TYPE((arraytype))))
6726  
6727  #define TYPE_ARRAY_LOWER_BOUND_VALUE(arraytype) \
6728 -   (TYPE_LOW_BOUND(TYPE_INDEX_TYPE((arraytype))))
6729 +  TYPE_LOW_BOUND (TYPE_INDEX_TYPE (arraytype))
6730 +
6731 +/* TYPE_BYTE_STRIDE (TYPE_INDEX_TYPE (arraytype)) with a fallback to the
6732 +   element size if no specific stride value is known.  */
6733 +#define TYPE_ARRAY_BYTE_STRIDE_VALUE(arraytype)                \
6734 +  (TYPE_BYTE_STRIDE (TYPE_INDEX_TYPE (arraytype)) == 0 \
6735 +   ? TYPE_LENGTH (TYPE_TARGET_TYPE (arraytype))                \
6736 +   : TYPE_BYTE_STRIDE (TYPE_INDEX_TYPE (arraytype)))
6737  
6738  /* C++ */
6739  
6740 @@ -1531,6 +1636,18 @@ extern struct type *create_array_type (struct type *, struct type *,
6741                                        struct type *);
6742  extern struct type *lookup_array_range_type (struct type *, int, int);
6743  
6744 +extern CORE_ADDR type_range_any_field_internal (struct type *range_type,
6745 +                                               int fieldno);
6746 +
6747 +extern int type_range_high_bound_internal (struct type *range_type);
6748 +
6749 +extern int type_range_count_bound_internal (struct type *range_type);
6750 +
6751 +extern CORE_ADDR type_range_byte_stride_internal (struct type *range_type,
6752 +                                                 struct type *element_type);
6753 +
6754 +extern void finalize_type (struct type *type);
6755 +
6756  extern struct type *create_string_type (struct type *, struct type *,
6757                                         struct type *);
6758  extern struct type *lookup_string_range_type (struct type *, int, int);
6759 @@ -1576,6 +1693,10 @@ extern int is_public_ancestor (struct type *, struct type *);
6760  
6761  extern int is_unique_ancestor (struct type *, struct value *);
6762  
6763 +#if 0
6764 +extern void type_mark_used (struct type *type);
6765 +#endif
6766 +
6767  /* Overload resolution */
6768  
6769  #define LENGTH_MATCH(bv) ((bv)->rank[0])
6770 @@ -1651,10 +1772,13 @@ extern void maintenance_print_type (char *, int);
6771  
6772  extern htab_t create_copied_types_hash (struct objfile *objfile);
6773  
6774 -extern struct type *copy_type_recursive (struct objfile *objfile,
6775 -                                        struct type *type,
6776 +extern struct type *copy_type_recursive (struct type *type,
6777                                          htab_t copied_types);
6778  
6779  extern struct type *copy_type (const struct type *type);
6780  
6781 +#if 0
6782 +extern void free_all_types (void);
6783 +#endif
6784 +
6785  #endif /* GDBTYPES_H */
6786 diff --git a/gdb/jit.c b/gdb/jit.c
6787 index 568d17b..cdd9f49 100644
6788 --- a/gdb/jit.c
6789 +++ b/gdb/jit.c
6790 @@ -38,6 +38,7 @@
6791  #include "gdb-dlfcn.h"
6792  #include "gdb_stat.h"
6793  #include "exceptions.h"
6794 +#include "gdb_bfd.h"
6795  
6796  static const char *jit_reader_dir = NULL;
6797  
6798 @@ -132,17 +133,16 @@ mem_bfd_iovec_stat (struct bfd *abfd, void *stream, struct stat *sb)
6799  static struct bfd *
6800  bfd_open_from_target_memory (CORE_ADDR addr, ULONGEST size, char *target)
6801  {
6802 -  const char *filename = xstrdup ("<in-memory>");
6803    struct target_buffer *buffer = xmalloc (sizeof (struct target_buffer));
6804  
6805    buffer->base = addr;
6806    buffer->size = size;
6807 -  return bfd_openr_iovec (filename, target,
6808 -                          mem_bfd_iovec_open,
6809 -                          buffer,
6810 -                          mem_bfd_iovec_pread,
6811 -                          mem_bfd_iovec_close,
6812 -                          mem_bfd_iovec_stat);
6813 +  return gdb_bfd_openr_iovec ("<in-memory>", target,
6814 +                             mem_bfd_iovec_open,
6815 +                             buffer,
6816 +                             mem_bfd_iovec_pread,
6817 +                             mem_bfd_iovec_close,
6818 +                             mem_bfd_iovec_stat);
6819  }
6820  
6821  /* One reader that has been loaded successfully, and can potentially be used to
6822 @@ -868,7 +868,7 @@ jit_bfd_try_read_symtab (struct jit_code_entry *code_entry,
6823      {
6824        printf_unfiltered (_("\
6825  JITed symbol file is not an object file, ignoring it.\n"));
6826 -      bfd_close (nbfd);
6827 +      gdb_bfd_unref (nbfd);
6828        return;
6829      }
6830  
6831 @@ -896,7 +896,8 @@ JITed symbol file is not an object file, ignoring it.\n"));
6832          ++i;
6833        }
6834  
6835 -  /* This call takes ownership of NBFD.  It does not take ownership of SAI.  */
6836 +  /* This call does not take ownership of SAI.  */
6837 +  make_cleanup_bfd_unref (nbfd);
6838    objfile = symbol_file_add_from_bfd (nbfd, 0, sai, OBJF_SHARED, NULL);
6839  
6840    do_cleanups (old_cleanups);
6841 diff --git a/gdb/m32r-rom.c b/gdb/m32r-rom.c
6842 index 76e4bf1..9a2f5aa 100644
6843 --- a/gdb/m32r-rom.c
6844 +++ b/gdb/m32r-rom.c
6845 @@ -40,6 +40,7 @@
6846  #include "inferior.h"
6847  #include <ctype.h>
6848  #include "regcache.h"
6849 +#include "gdb_bfd.h"
6850  
6851  /*
6852   * All this stuff just to get my host computer's IP address!
6853 @@ -124,13 +125,15 @@ m32r_load (char *filename, int from_tty)
6854    bfd *abfd;
6855    unsigned int data_count = 0;
6856    struct timeval start_time, end_time;
6857 +  struct cleanup *cleanup;
6858  
6859    if (filename == NULL || filename[0] == 0)
6860      filename = get_exec_file (1);
6861  
6862 -  abfd = bfd_openr (filename, 0);
6863 +  abfd = gdb_bfd_openr (filename, 0);
6864    if (!abfd)
6865      error (_("Unable to open file %s."), filename);
6866 +  cleanup = make_cleanup_bfd_unref (abfd);
6867    if (bfd_check_format (abfd, bfd_object) == 0)
6868      error (_("File is not an object file."));
6869    gettimeofday (&start_time, NULL);
6870 @@ -188,6 +191,7 @@ m32r_load (char *filename, int from_tty)
6871       confused...  */
6872  
6873    clear_symtab_users (0);
6874 +  do_cleanups (cleanup);
6875  }
6876  
6877  static void
6878 @@ -434,6 +438,7 @@ m32r_upload_command (char *args, int from_tty)
6879    char buf[1024];
6880    struct hostent *hostent;
6881    struct in_addr inet_addr;
6882 +  struct cleanup *cleanup;
6883  
6884    /* First check to see if there's an ethernet port!  */
6885    monitor_printf ("ust\r");
6886 @@ -524,7 +529,8 @@ m32r_upload_command (char *args, int from_tty)
6887      printf_filtered (" -- Ethernet load complete.\n");
6888  
6889    gettimeofday (&end_time, NULL);
6890 -  abfd = bfd_openr (args, 0);
6891 +  abfd = gdb_bfd_openr (args, 0);
6892 +  cleanup = make_cleanup_bfd_unref (abfd);
6893    if (abfd != NULL)
6894      {          /* Download is done -- print section statistics.  */
6895        if (bfd_check_format (abfd, bfd_object) == 0)
6896 @@ -565,6 +571,7 @@ m32r_upload_command (char *args, int from_tty)
6897       confused...  */
6898  
6899    clear_symtab_users (0);
6900 +  do_cleanups (cleanup);
6901  }
6902  
6903  /* Provide a prototype to silence -Wmissing-prototypes.  */
6904 diff --git a/gdb/machoread.c b/gdb/machoread.c
6905 index 22530ab..0d7578a 100644
6906 --- a/gdb/machoread.c
6907 +++ b/gdb/machoread.c
6908 @@ -454,6 +454,7 @@ macho_add_oso_symfile (oso_el *oso, bfd *abfd,
6909    asymbol **symp;
6910    struct bfd_hash_table table;
6911    int nbr_sections;
6912 +  struct cleanup *cleanup;
6913  
6914    /* Per section flag to mark which section have been rebased.  */
6915    unsigned char *sections_rebased;
6916 @@ -466,14 +467,14 @@ macho_add_oso_symfile (oso_el *oso, bfd *abfd,
6917      {
6918        warning (_("`%s': can't read symbols: %s."), oso->name,
6919                 bfd_errmsg (bfd_get_error ()));
6920 -      bfd_close (abfd);
6921 +      gdb_bfd_unref (abfd);
6922        return;
6923      }
6924  
6925    if (abfd->my_archive == NULL && oso->mtime != bfd_get_mtime (abfd))
6926      {
6927        warning (_("`%s': file time stamp mismatch."), oso->name);
6928 -      bfd_close (abfd);
6929 +      gdb_bfd_unref (abfd);
6930        return;
6931      }
6932  
6933 @@ -482,7 +483,7 @@ macho_add_oso_symfile (oso_el *oso, bfd *abfd,
6934                                oso->nbr_syms))
6935      {
6936        warning (_("`%s': can't create hash table"), oso->name);
6937 -      bfd_close (abfd);
6938 +      gdb_bfd_unref (abfd);
6939        return;
6940      }
6941  
6942 @@ -629,18 +630,15 @@ macho_add_oso_symfile (oso_el *oso, bfd *abfd,
6943  
6944    bfd_hash_table_free (&table);
6945  
6946 -  /* Make sure that the filename was malloc'ed.  The current filename comes
6947 -     either from an OSO symbol name or from an archive name.  Memory for both
6948 -     is not managed by gdb.  */
6949 -  abfd->filename = xstrdup (abfd->filename);
6950 -
6951    /* We need to clear SYMFILE_MAINLINE to avoid interractive question
6952       from symfile.c:symbol_file_add_with_addrs_or_offsets.  */
6953 +  cleanup = make_cleanup_bfd_unref (abfd);
6954    symbol_file_add_from_bfd
6955      (abfd, symfile_flags & ~(SYMFILE_MAINLINE | SYMFILE_VERBOSE), NULL,
6956       main_objfile->flags & (OBJF_REORDERED | OBJF_SHARED
6957                             | OBJF_READNOW | OBJF_USERLOADED),
6958       main_objfile);
6959 +  do_cleanups (cleanup);
6960  }
6961  
6962  /* Read symbols from the vector of oso files.  */
6963 @@ -651,6 +649,7 @@ macho_symfile_read_all_oso (struct objfile *main_objfile, int symfile_flags)
6964    int ix;
6965    VEC (oso_el) *vec;
6966    oso_el *oso;
6967 +  struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
6968  
6969    vec = oso_vector;
6970    oso_vector = NULL;
6971 @@ -677,6 +676,8 @@ macho_symfile_read_all_oso (struct objfile *main_objfile, int symfile_flags)
6972           memcpy (archive_name, oso->name, pfx_len);
6973           archive_name[pfx_len] = '\0';
6974  
6975 +         make_cleanup (xfree, archive_name);
6976 +
6977            /* Compute number of oso for this archive.  */
6978            for (last_ix = ix;
6979                 VEC_iterate (oso_el, vec, last_ix, oso2); last_ix++)
6980 @@ -686,7 +687,7 @@ macho_symfile_read_all_oso (struct objfile *main_objfile, int symfile_flags)
6981              }
6982  
6983           /* Open the archive and check the format.  */
6984 -         archive_bfd = bfd_openr (archive_name, gnutarget);
6985 +         archive_bfd = gdb_bfd_openr (archive_name, gnutarget);
6986           if (archive_bfd == NULL)
6987             {
6988               warning (_("Could not open OSO archive file \"%s\""),
6989 @@ -698,17 +699,18 @@ macho_symfile_read_all_oso (struct objfile *main_objfile, int symfile_flags)
6990             {
6991               warning (_("OSO archive file \"%s\" not an archive."),
6992                        archive_name);
6993 -             bfd_close (archive_bfd);
6994 +             gdb_bfd_unref (archive_bfd);
6995                ix = last_ix;
6996               continue;
6997             }
6998 -         member_bfd = bfd_openr_next_archived_file (archive_bfd, NULL);
6999 +
7000 +         member_bfd = gdb_bfd_openr_next_archived_file (archive_bfd, NULL);
7001  
7002           if (member_bfd == NULL)
7003             {
7004               warning (_("Could not read archive members out of "
7005                          "OSO archive \"%s\""), archive_name);
7006 -             bfd_close (archive_bfd);
7007 +             gdb_bfd_unref (archive_bfd);
7008                ix = last_ix;
7009               continue;
7010             }
7011 @@ -738,12 +740,12 @@ macho_symfile_read_all_oso (struct objfile *main_objfile, int symfile_flags)
7012                  }
7013  
7014                prev = member_bfd;
7015 -             member_bfd = bfd_openr_next_archived_file
7016 -               (archive_bfd, member_bfd);
7017 +             member_bfd = gdb_bfd_openr_next_archived_file (archive_bfd,
7018 +                                                            member_bfd);
7019  
7020                /* Free previous member if not referenced by an oso.  */
7021                if (ix2 >= last_ix)
7022 -                bfd_close (prev);
7023 +                gdb_bfd_unref (prev);
7024             }
7025            for (ix2 = ix; ix2 < last_ix; ix2++)
7026              {
7027 @@ -759,7 +761,7 @@ macho_symfile_read_all_oso (struct objfile *main_objfile, int symfile_flags)
7028         {
7029            bfd *abfd;
7030  
7031 -         abfd = bfd_openr (oso->name, gnutarget);
7032 +         abfd = gdb_bfd_openr (oso->name, gnutarget);
7033           if (!abfd)
7034              warning (_("`%s': can't open to read symbols: %s."), oso->name,
7035                       bfd_errmsg (bfd_get_error ()));
7036 @@ -771,6 +773,7 @@ macho_symfile_read_all_oso (struct objfile *main_objfile, int symfile_flags)
7037      }
7038  
7039    VEC_free (oso_el, vec);
7040 +  do_cleanups (cleanup);
7041  }
7042  
7043  /* DSYM (debug symbols) files contain the debug info of an executable.
7044 @@ -808,20 +811,17 @@ macho_check_dsym (struct objfile *objfile)
7045        warning (_("can't find UUID in %s"), objfile->name);
7046        return NULL;
7047      }
7048 -  dsym_filename = xstrdup (dsym_filename);
7049 -  dsym_bfd = bfd_openr (dsym_filename, gnutarget);
7050 +  dsym_bfd = gdb_bfd_openr (dsym_filename, gnutarget);
7051    if (dsym_bfd == NULL)
7052      {
7053        warning (_("can't open dsym file %s"), dsym_filename);
7054 -      xfree (dsym_filename);
7055        return NULL;
7056      }
7057  
7058    if (!bfd_check_format (dsym_bfd, bfd_object))
7059      {
7060 -      bfd_close (dsym_bfd);
7061 +      gdb_bfd_unref (dsym_bfd);
7062        warning (_("bad dsym file format: %s"), bfd_errmsg (bfd_get_error ()));
7063 -      xfree (dsym_filename);
7064        return NULL;
7065      }
7066  
7067 @@ -829,16 +829,14 @@ macho_check_dsym (struct objfile *objfile)
7068                                   BFD_MACH_O_LC_UUID, &dsym_uuid) == 0)
7069      {
7070        warning (_("can't find UUID in %s"), dsym_filename);
7071 -      bfd_close (dsym_bfd);
7072 -      xfree (dsym_filename);
7073 +      gdb_bfd_unref (dsym_bfd);
7074        return NULL;
7075      }
7076    if (memcmp (dsym_uuid->command.uuid.uuid, main_uuid->command.uuid.uuid,
7077                sizeof (main_uuid->command.uuid.uuid)))
7078      {
7079        warning (_("dsym file UUID doesn't match the one in %s"), objfile->name);
7080 -      bfd_close (dsym_bfd);
7081 -      xfree (dsym_filename);
7082 +      gdb_bfd_unref (dsym_bfd);
7083        return NULL;
7084      }
7085    return dsym_bfd;
7086 @@ -902,6 +900,7 @@ macho_symfile_read (struct objfile *objfile, int symfile_flags)
7087           int ix;
7088           oso_el *oso;
7089            struct bfd_section *asect, *dsect;
7090 +         struct cleanup *cleanup;
7091  
7092           if (mach_o_debug_level > 0)
7093             printf_unfiltered (_("dsym file found\n"));
7094 @@ -922,7 +921,9 @@ macho_symfile_read (struct objfile *objfile, int symfile_flags)
7095              }
7096  
7097           /* Add the dsym file as a separate file.  */
7098 +         cleanup = make_cleanup_bfd_unref (dsym_bfd);
7099            symbol_file_add_separate (dsym_bfd, symfile_flags, objfile);
7100 +         do_cleanups (cleanup);
7101  
7102           /* Don't try to read dwarf2 from main file or shared libraries.  */
7103            return;
7104 diff --git a/gdb/main.c b/gdb/main.c
7105 index d075694..e4da3f1 100644
7106 --- a/gdb/main.c
7107 +++ b/gdb/main.c
7108 @@ -37,6 +37,7 @@
7109  
7110  #include "interps.h"
7111  #include "main.h"
7112 +#include "python/python.h"
7113  #include "source.h"
7114  #include "cli/cli-cmds.h"
7115  #include "python/python.h"
7116 @@ -283,6 +284,8 @@ captured_main (void *data)
7117    char *cdarg = NULL;
7118    char *ttyarg = NULL;
7119  
7120 +  int python_script = 0;
7121 +
7122    /* These are static so that we can take their address in an
7123       initializer.  */
7124    static int print_help;
7125 @@ -471,10 +474,14 @@ captured_main (void *data)
7126        {"args", no_argument, &set_args, 1},
7127        {"l", required_argument, 0, 'l'},
7128        {"return-child-result", no_argument, &return_child_result, 1},
7129 +#if HAVE_PYTHON
7130 +      {"python", no_argument, 0, 'P'},
7131 +      {"P", no_argument, 0, 'P'},
7132 +#endif
7133        {0, no_argument, 0, 0}
7134      };
7135  
7136 -    while (1)
7137 +    while (!python_script)
7138        {
7139         int option_index;
7140  
7141 @@ -492,6 +499,9 @@ captured_main (void *data)
7142           case 0:
7143             /* Long option that just sets a flag.  */
7144             break;
7145 +         case 'P':
7146 +           python_script = 1;
7147 +           break;
7148           case OPT_SE:
7149             symarg = optarg;
7150             execarg = optarg;
7151 @@ -699,7 +709,31 @@ captured_main (void *data)
7152  
7153    /* Now that gdb_init has created the initial inferior, we're in
7154       position to set args for that inferior.  */
7155 -  if (set_args)
7156 +  if (python_script)
7157 +    {
7158 +      /* The first argument is a python script to evaluate, and
7159 +        subsequent arguments are passed to the script for
7160 +        processing there.  */
7161 +      if (optind >= argc)
7162 +       {
7163 +         fprintf_unfiltered (gdb_stderr,
7164 +                             _("%s: Python script file name required\n"),
7165 +                             argv[0]);
7166 +         exit (1);
7167 +       }
7168 +
7169 +      /* FIXME: should handle inferior I/O intelligently here.
7170 +        E.g., should be possible to run gdb in pipeline and have
7171 +        Python (and gdb) output go to stderr or file; and if a
7172 +        prompt is needed, open the tty.  */
7173 +      quiet = 1;
7174 +      /* FIXME: should read .gdbinit if, and only if, a prompt is
7175 +        requested by the script.  Though... maybe this is not
7176 +        ideal?  */
7177 +      /* FIXME: likewise, reading in history.  */
7178 +      inhibit_gdbinit = 1;
7179 +    }
7180 +  else if (set_args)
7181      {
7182        /* The remaining options are the command-line options for the
7183          inferior.  The first one is the sym/exec file, and the rest
7184 @@ -979,7 +1013,8 @@ captured_main (void *data)
7185  
7186    /* Read in the old history after all the command files have been
7187       read.  */
7188 -  init_history ();
7189 +  if (!python_script)
7190 +    init_history ();
7191  
7192    if (batch_flag)
7193      {
7194 @@ -990,13 +1025,25 @@ captured_main (void *data)
7195    /* Show time and/or space usage.  */
7196    do_cleanups (pre_stat_chain);
7197  
7198 -  /* NOTE: cagney/1999-11-07: There is probably no reason for not
7199 -     moving this loop and the code found in captured_command_loop()
7200 -     into the command_loop() proper.  The main thing holding back that
7201 -     change - SET_TOP_LEVEL() - has been eliminated.  */
7202 -  while (1)
7203 +#if HAVE_PYTHON
7204 +  if (python_script)
7205      {
7206 -      catch_errors (captured_command_loop, 0, "", RETURN_MASK_ALL);
7207 +      extern int pagination_enabled;
7208 +      pagination_enabled = 0;
7209 +      run_python_script (argc - optind, &argv[optind]);
7210 +      return 1;
7211 +    }
7212 +  else
7213 +#endif
7214 +    {
7215 +      /* NOTE: cagney/1999-11-07: There is probably no reason for not
7216 +        moving this loop and the code found in captured_command_loop()
7217 +        into the command_loop() proper.  The main thing holding back that
7218 +        change - SET_TOP_LEVEL() - has been eliminated. */
7219 +      while (1)
7220 +       {
7221 +         catch_errors (captured_command_loop, 0, "", RETURN_MASK_ALL);
7222 +       }
7223      }
7224    /* No exit -- exit is through quit_command.  */
7225  }
7226 @@ -1028,7 +1075,12 @@ print_gdb_help (struct ui_file *stream)
7227    fputs_unfiltered (_("\
7228  This is the GNU debugger.  Usage:\n\n\
7229      gdb [options] [executable-file [core-file or process-id]]\n\
7230 -    gdb [options] --args executable-file [inferior-arguments ...]\n\n\
7231 +    gdb [options] --args executable-file [inferior-arguments ...]\n"), stream);
7232 +#if HAVE_PYTHON
7233 +  fputs_unfiltered (_("\
7234 +    gdb [options] [--python|-P] script-file [script-arguments ...]\n"), stream);
7235 +#endif
7236 +  fputs_unfiltered (_("\n\
7237  Options:\n\n\
7238  "), stream);
7239    fputs_unfiltered (_("\
7240 @@ -1068,7 +1120,13 @@ Options:\n\n\
7241    --nw              Do not use a window interface.\n\
7242    --nx               Do not read "), stream);
7243    fputs_unfiltered (gdbinit, stream);
7244 -  fputs_unfiltered (_(" file.\n\
7245 +  fputs_unfiltered (_(" file.\n"), stream);
7246 +#if HAVE_PYTHON
7247 +  fputs_unfiltered (_("\
7248 +  --python, -P       Following argument is Python script file; remaining\n\
7249 +                     arguments are passed to script.\n"), stream);
7250 +#endif
7251 +  fputs_unfiltered (_("\
7252    --quiet            Do not print version number on startup.\n\
7253    --readnow          Fully read symbol files on first access.\n\
7254  "), stream);
7255 diff --git a/gdb/objfiles.c b/gdb/objfiles.c
7256 index f5e5c75..411618f 100644
7257 --- a/gdb/objfiles.c
7258 +++ b/gdb/objfiles.c
7259 @@ -53,6 +53,7 @@
7260  #include "complaints.h"
7261  #include "psymtab.h"
7262  #include "solist.h"
7263 +#include "gdb_bfd.h"
7264  
7265  /* Prototypes for local functions */
7266  
7267 @@ -195,7 +196,8 @@ allocate_objfile (bfd *abfd, int flags)
7268       that any data that is reference is saved in the per-objfile data
7269       region.  */
7270  
7271 -  objfile->obfd = gdb_bfd_ref (abfd);
7272 +  objfile->obfd = abfd;
7273 +  gdb_bfd_ref (abfd);
7274    if (abfd != NULL)
7275      {
7276        /* Look up the gdbarch associated with the BFD.  */
7277 @@ -1456,75 +1458,6 @@ objfiles_changed (void)
7278    get_objfile_pspace_data (current_program_space)->objfiles_changed_p = 1;
7279  }
7280  
7281 -/* Close ABFD, and warn if that fails.  */
7282 -
7283 -int
7284 -gdb_bfd_close_or_warn (struct bfd *abfd)
7285 -{
7286 -  int ret;
7287 -  char *name = bfd_get_filename (abfd);
7288 -
7289 -  ret = bfd_close (abfd);
7290 -
7291 -  if (!ret)
7292 -    warning (_("cannot close \"%s\": %s"),
7293 -            name, bfd_errmsg (bfd_get_error ()));
7294 -
7295 -  return ret;
7296 -}
7297 -
7298 -/* Add reference to ABFD.  Returns ABFD.  */
7299 -struct bfd *
7300 -gdb_bfd_ref (struct bfd *abfd)
7301 -{
7302 -  int *p_refcount;
7303 -
7304 -  if (abfd == NULL)
7305 -    return NULL;
7306 -
7307 -  p_refcount = bfd_usrdata (abfd);
7308 -
7309 -  if (p_refcount != NULL)
7310 -    {
7311 -      *p_refcount += 1;
7312 -      return abfd;
7313 -    }
7314 -
7315 -  p_refcount = xmalloc (sizeof (*p_refcount));
7316 -  *p_refcount = 1;
7317 -  bfd_usrdata (abfd) = p_refcount;
7318 -
7319 -  return abfd;
7320 -}
7321 -
7322 -/* Unreference and possibly close ABFD.  */
7323 -void
7324 -gdb_bfd_unref (struct bfd *abfd)
7325 -{
7326 -  int *p_refcount;
7327 -  char *name;
7328 -
7329 -  if (abfd == NULL)
7330 -    return;
7331 -
7332 -  p_refcount = bfd_usrdata (abfd);
7333 -
7334 -  /* Valid range for p_refcount: a pointer to int counter, which has a
7335 -     value of 1 (single owner) or 2 (shared).  */
7336 -  gdb_assert (*p_refcount == 1 || *p_refcount == 2);
7337 -
7338 -  *p_refcount -= 1;
7339 -  if (*p_refcount > 0)
7340 -    return;
7341 -
7342 -  xfree (p_refcount);
7343 -  bfd_usrdata (abfd) = NULL;  /* Paranoia.  */
7344 -
7345 -  name = bfd_get_filename (abfd);
7346 -  gdb_bfd_close_or_warn (abfd);
7347 -  xfree (name);
7348 -}
7349 -
7350  /* The default implementation for the "iterate_over_objfiles_in_search_order"
7351     gdbarch method.  It is equivalent to use the ALL_OBJFILES macro,
7352     searching the objfiles in the order they are stored internally,
7353 diff --git a/gdb/objfiles.h b/gdb/objfiles.h
7354 index 01c3aea..0df5798 100644
7355 --- a/gdb/objfiles.h
7356 +++ b/gdb/objfiles.h
7357 @@ -522,10 +522,6 @@ extern void set_objfile_data (struct objfile *objfile,
7358  extern void *objfile_data (struct objfile *objfile,
7359                            const struct objfile_data *data);
7360  
7361 -extern struct bfd *gdb_bfd_ref (struct bfd *abfd);
7362 -extern void gdb_bfd_unref (struct bfd *abfd);
7363 -extern int gdb_bfd_close_or_warn (struct bfd *abfd);
7364 -
7365  extern void default_iterate_over_objfiles_in_search_order
7366    (struct gdbarch *gdbarch,
7367     iterate_over_objfiles_in_search_order_cb_ftype *cb,
7368 diff --git a/gdb/p-valprint.c b/gdb/p-valprint.c
7369 index b8434ed..98a076d 100644
7370 --- a/gdb/p-valprint.c
7371 +++ b/gdb/p-valprint.c
7372 @@ -39,6 +39,7 @@
7373  #include "cp-abi.h"
7374  #include "cp-support.h"
7375  #include "exceptions.h"
7376 +#include "dwarf2loc.h"
7377  \f
7378  
7379  /* Decorations for Pascal.  */
7380 @@ -74,8 +75,31 @@ pascal_val_print (struct type *type, const gdb_byte *valaddr,
7381    struct type *char_type;
7382    CORE_ADDR addr;
7383    int want_space = 0;
7384 +  struct cleanup *back_to;
7385 +  struct type *saved_type = type;
7386 +  CORE_ADDR saved_address = address;
7387 +  
7388 +  back_to = make_cleanup (null_cleanup, 0);
7389 +  address += embedded_offset;
7390 +  type = object_address_get_data (type, &address);
7391 +  if (type == NULL)
7392 +    {
7393 +      fputs_filtered (object_address_data_not_valid (saved_type), stream);
7394 +      gdb_flush (stream);
7395 +      do_cleanups (back_to);
7396 +      return;
7397 +    }
7398 +  if (address != saved_address + embedded_offset)
7399 +    {
7400 +      size_t length = TYPE_LENGTH (type);
7401  
7402 -  CHECK_TYPEDEF (type);
7403 +      valaddr = xmalloc (length);
7404 +      make_cleanup (xfree, (gdb_byte *) valaddr);
7405 +      read_memory (address, (gdb_byte *) valaddr, length);
7406 +      embedded_offset = 0;
7407 +    }
7408 +  else
7409 +    address -= embedded_offset;
7410    switch (TYPE_CODE (type))
7411      {
7412      case TYPE_CODE_ARRAY:
7413 @@ -131,8 +155,8 @@ pascal_val_print (struct type *type, const gdb_byte *valaddr,
7414                 {
7415                   i = 0;
7416                 }
7417 -             val_print_array_elements (type, valaddr, embedded_offset,
7418 -                                       address, stream, recurse,
7419 +             val_print_array_elements (saved_type, valaddr, embedded_offset,
7420 +                                       saved_address, stream, recurse,
7421                                         original_value, options, i);
7422               fprintf_filtered (stream, "}");
7423             }
7424 @@ -170,6 +194,7 @@ pascal_val_print (struct type *type, const gdb_byte *valaddr,
7425         {
7426           /* Try to print what function it points to.  */
7427           print_address_demangle (options, gdbarch, addr, stream, demangle);
7428 +         do_cleanups (back_to);
7429           return;
7430         }
7431  
7432 @@ -271,6 +296,7 @@ pascal_val_print (struct type *type, const gdb_byte *valaddr,
7433             }
7434         }
7435  
7436 +      do_cleanups (back_to);
7437        return;
7438  
7439      case TYPE_CODE_REF:
7440 @@ -421,6 +447,7 @@ pascal_val_print (struct type *type, const gdb_byte *valaddr,
7441              TYPE_CODE (type));
7442      }
7443    gdb_flush (stream);
7444 +  do_cleanups (back_to);
7445  }
7446  \f
7447  void
7448 diff --git a/gdb/parse.c b/gdb/parse.c
7449 index 529c517..6326a01 100644
7450 --- a/gdb/parse.c
7451 +++ b/gdb/parse.c
7452 @@ -1708,6 +1708,7 @@ parser_fprintf (FILE *x, const char *y, ...)
7453  
7454  int
7455  operator_check_standard (struct expression *exp, int pos,
7456 +                        int (*type_func) (struct type *type, void *data),
7457                          int (*objfile_func) (struct objfile *objfile,
7458                                               void *data),
7459                          void *data)
7460 @@ -1749,7 +1750,7 @@ operator_check_standard (struct expression *exp, int pos,
7461             struct type *type = elts[pos + 2 + arg].type;
7462             struct objfile *objfile = TYPE_OBJFILE (type);
7463  
7464 -           if (objfile && (*objfile_func) (objfile, data))
7465 +           if (objfile && objfile_func && (*objfile_func) (objfile, data))
7466               return 1;
7467           }
7468        }
7469 @@ -1767,7 +1768,8 @@ operator_check_standard (struct expression *exp, int pos,
7470  
7471         /* Check objfile where the variable itself is placed.
7472            SYMBOL_OBJ_SECTION (symbol) may be NULL.  */
7473 -       if ((*objfile_func) (SYMBOL_SYMTAB (symbol)->objfile, data))
7474 +       if (objfile_func
7475 +           && (*objfile_func) (SYMBOL_SYMTAB (symbol)->objfile, data))
7476           return 1;
7477  
7478         /* Check objfile where is placed the code touching the variable.  */
7479 @@ -1780,24 +1782,27 @@ operator_check_standard (struct expression *exp, int pos,
7480  
7481    /* Invoke callbacks for TYPE and OBJFILE if they were set as non-NULL.  */
7482  
7483 -  if (type && TYPE_OBJFILE (type)
7484 +  if (type && type_func && (*type_func) (type, data))
7485 +    return 1;
7486 +  if (type && TYPE_OBJFILE (type) && objfile_func
7487        && (*objfile_func) (TYPE_OBJFILE (type), data))
7488      return 1;
7489 -  if (objfile && (*objfile_func) (objfile, data))
7490 +  if (objfile && objfile_func && (*objfile_func) (objfile, data))
7491      return 1;
7492  
7493    return 0;
7494  }
7495  
7496 -/* Call OBJFILE_FUNC for any TYPE and OBJFILE found being referenced by EXP.
7497 -   The functions are never called with NULL OBJFILE.  Functions get passed an
7498 -   arbitrary caller supplied DATA pointer.  If any of the functions returns
7499 -   non-zero value then (any other) non-zero value is immediately returned to
7500 -   the caller.  Otherwise zero is returned after iterating through whole EXP.
7501 -   */
7502 +/* Call TYPE_FUNC and OBJFILE_FUNC for any TYPE and OBJFILE found being
7503 +   referenced by EXP.  The functions are never called with NULL TYPE or NULL
7504 +   OBJFILE.  Functions get passed an arbitrary caller supplied DATA pointer.
7505 +   If any of the functions returns non-zero value then (any other) non-zero
7506 +   value is immediately returned to the caller.  Otherwise zero is returned
7507 +   after iterating through whole EXP.  */
7508  
7509  static int
7510  exp_iterate (struct expression *exp,
7511 +            int (*type_func) (struct type *type, void *data),
7512              int (*objfile_func) (struct objfile *objfile, void *data),
7513              void *data)
7514  {
7515 @@ -1812,7 +1817,9 @@ exp_iterate (struct expression *exp,
7516  
7517        pos = endpos - oplen;
7518        if (exp->language_defn->la_exp_desc->operator_check (exp, pos,
7519 -                                                          objfile_func, data))
7520 +                                                          type_func,
7521 +                                                          objfile_func,
7522 +                                                          data))
7523         return 1;
7524  
7525        endpos = pos;
7526 @@ -1843,8 +1850,29 @@ exp_uses_objfile (struct expression *exp, struct objfile *objfile)
7527  {
7528    gdb_assert (objfile->separate_debug_objfile_backlink == NULL);
7529  
7530 -  return exp_iterate (exp, exp_uses_objfile_iter, objfile);
7531 +  return exp_iterate (exp, NULL, exp_uses_objfile_iter, objfile);
7532 +}
7533 +
7534 +/* Helper for exp_types_mark_used.  */
7535 +
7536 +#if 0
7537 +static int
7538 +exp_types_mark_used_iter (struct type *type, void *unused)
7539 +{
7540 +  type_mark_used (type);
7541 +
7542 +  /* Continue the traversal.  */
7543 +  return 0;
7544 +}
7545 +
7546 +/* Call type_mark_used for any type contained in EXP.  */
7547 +
7548 +void
7549 +exp_types_mark_used (struct expression *exp)
7550 +{
7551 +  exp_iterate (exp, exp_types_mark_used_iter, NULL, NULL);
7552  }
7553 +#endif
7554  
7555  void
7556  _initialize_parse (void)
7557 diff --git a/gdb/parser-defs.h b/gdb/parser-defs.h
7558 index 86f3bdf..f908a61 100644
7559 --- a/gdb/parser-defs.h
7560 +++ b/gdb/parser-defs.h
7561 @@ -245,6 +245,8 @@ extern void operator_length_standard (const struct expression *, int, int *,
7562                                       int *);
7563  
7564  extern int operator_check_standard (struct expression *exp, int pos,
7565 +                                   int (*type_func) (struct type *type,
7566 +                                                     void *data),
7567                                     int (*objfile_func)
7568                                       (struct objfile *objfile, void *data),
7569                                     void *data);
7570 @@ -331,6 +333,7 @@ struct exp_descriptor
7571         value should be immediately returned to the caller.  Otherwise zero
7572         should be returned.  */
7573      int (*operator_check) (struct expression *exp, int pos,
7574 +                          int (*type_func) (struct type *type, void *data),
7575                            int (*objfile_func) (struct objfile *objfile,
7576                                                 void *data),
7577                            void *data);
7578 @@ -369,4 +372,10 @@ extern void parser_fprintf (FILE *, const char *, ...) ATTRIBUTE_PRINTF (2, 3);
7579  
7580  extern int exp_uses_objfile (struct expression *exp, struct objfile *objfile);
7581  
7582 +extern int exp_uses_objfile (struct expression *exp, struct objfile *objfile);
7583 +
7584 +#if 0
7585 +extern void exp_types_mark_used (struct expression *exp);
7586 +#endif
7587 +
7588  #endif /* PARSER_DEFS_H */
7589 diff --git a/gdb/printcmd.c b/gdb/printcmd.c
7590 index d5b5b63..4bc2b5b 100644
7591 --- a/gdb/printcmd.c
7592 +++ b/gdb/printcmd.c
7593 @@ -50,6 +50,7 @@
7594  #include "arch-utils.h"
7595  #include "cli/cli-utils.h"
7596  #include "format.h"
7597 +#include "dwarf2loc.h"
7598  
7599  #ifdef TUI
7600  #include "tui/tui.h"           /* For tui_active et al.   */
7601 @@ -968,6 +969,11 @@ print_command_1 (char *exp, int inspect, int voidprint)
7602    else
7603      val = access_value_history (0);
7604  
7605 +  /* Do not try to OBJECT_ADDRESS_SET here anything.  We are interested in the
7606 +     source variable base addresses as found by READ_VAR_VALUE.  The value here
7607 +     can be already a calculated expression address inappropriate for
7608 +     DW_OP_push_object_address.  */
7609 +
7610    if (voidprint || (val && value_type (val) &&
7611                     TYPE_CODE (value_type (val)) != TYPE_CODE_VOID))
7612      {
7613 @@ -1056,6 +1062,9 @@ output_command (char *exp, int from_tty)
7614  
7615    val = evaluate_expression (expr);
7616  
7617 +  if (VALUE_LVAL (val) == lval_memory)
7618 +    object_address_set (value_raw_address (val));
7619 +
7620    annotate_value_begin (value_type (val));
7621  
7622    get_formatted_print_options (&opts, format);
7623 @@ -1485,6 +1494,24 @@ x_command (char *exp, int from_tty)
7624         set_internalvar (lookup_internalvar ("__"), last_examine_value);
7625      }
7626  }
7627 +
7628 +#if 0
7629 +/* Call type_mark_used for any TYPEs referenced from this GDB source file.  */
7630 +
7631 +static void
7632 +print_types_mark_used (void)
7633 +{
7634 +  struct display *d;
7635 +
7636 +  if (last_examine_value)
7637 +    type_mark_used (value_type (last_examine_value));
7638 +
7639 +  for (d = display_chain; d; d = d->next)
7640 +    if (d->exp)
7641 +      exp_types_mark_used (d->exp);
7642 +}
7643 +#endif
7644 +
7645  \f
7646  
7647  /* Add an expression to the auto-display chain.
7648 @@ -1982,6 +2009,10 @@ print_variable_and_value (const char *name, struct symbol *var,
7649        struct value_print_options opts;
7650  
7651        val = read_var_value (var, frame);
7652 +
7653 +      make_cleanup_restore_selected_frame ();
7654 +      select_frame (frame);
7655 +
7656        get_user_print_options (&opts);
7657        opts.deref_ref = 1;
7658        common_val_print (val, stream, indent, &opts, current_language);
7659 @@ -2626,4 +2657,8 @@ Show printing of source filename and line number with <symbol>."), NULL,
7660    add_com ("eval", no_class, eval_command, _("\
7661  Convert \"printf format string\", arg1, arg2, arg3, ..., argn to\n\
7662  a command line, and call it."));
7663 +
7664 +#if 0
7665 +  observer_attach_mark_used (print_types_mark_used);
7666 +#endif
7667  }
7668 diff --git a/gdb/procfs.c b/gdb/procfs.c
7669 index 774df2e..4409e5b 100644
7670 --- a/gdb/procfs.c
7671 +++ b/gdb/procfs.c
7672 @@ -3486,7 +3486,7 @@ insert_dbx_link_bpt_in_file (int fd, CORE_ADDR ignored)
7673    long storage_needed;
7674    CORE_ADDR sym_addr;
7675  
7676 -  abfd = bfd_fdopenr ("unamed", 0, fd);
7677 +  abfd = gdb_bfd_fdopenr ("unamed", 0, fd);
7678    if (abfd == NULL)
7679      {
7680        warning (_("Failed to create a bfd: %s."), bfd_errmsg (bfd_get_error ()));
7681 @@ -3497,7 +3497,7 @@ insert_dbx_link_bpt_in_file (int fd, CORE_ADDR ignored)
7682      {
7683        /* Not the correct format, so we can not possibly find the dbx_link
7684          symbol in it.  */
7685 -      bfd_close (abfd);
7686 +      gdb_bfd_unref (abfd);
7687        return 0;
7688      }
7689  
7690 @@ -3511,14 +3511,14 @@ insert_dbx_link_bpt_in_file (int fd, CORE_ADDR ignored)
7691        if (dbx_link_bpt == NULL)
7692         {
7693           warning (_("Failed to insert dbx_link breakpoint."));
7694 -         bfd_close (abfd);
7695 +         gdb_bfd_unref (abfd);
7696           return 0;
7697         }
7698 -      bfd_close (abfd);
7699 +      gdb_bfd_unref (abfd);
7700        return 1;
7701      }
7702  
7703 -  bfd_close (abfd);
7704 +  gdb_bfd_unref (abfd);
7705    return 0;
7706  }
7707  
7708 diff --git a/gdb/python/lib/gdb/FrameIterator.py b/gdb/python/lib/gdb/FrameIterator.py
7709 new file mode 100644
7710 index 0000000..5654546
7711 --- /dev/null
7712 +++ b/gdb/python/lib/gdb/FrameIterator.py
7713 @@ -0,0 +1,33 @@
7714 +# Iterator over frames.
7715 +
7716 +# Copyright (C) 2008, 2009 Free Software Foundation, Inc.
7717 +
7718 +# This program is free software; you can redistribute it and/or modify
7719 +# it under the terms of the GNU General Public License as published by
7720 +# the Free Software Foundation; either version 3 of the License, or
7721 +# (at your option) any later version.
7722 +#
7723 +# This program is distributed in the hope that it will be useful,
7724 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
7725 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7726 +# GNU General Public License for more details.
7727 +#
7728 +# You should have received a copy of the GNU General Public License
7729 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
7730 +
7731 +class FrameIterator:
7732 +    """An iterator that iterates over frames."""
7733 +
7734 +    def __init__ (self, frame):
7735 +        "Initialize a FrameIterator.  FRAME is the starting frame."
7736 +        self.frame = frame
7737 +
7738 +    def __iter__ (self):
7739 +        return self
7740 +
7741 +    def next (self):
7742 +        result = self.frame
7743 +        if result is None:
7744 +            raise StopIteration
7745 +        self.frame = result.older ()
7746 +        return result
7747 diff --git a/gdb/python/lib/gdb/FrameWrapper.py b/gdb/python/lib/gdb/FrameWrapper.py
7748 new file mode 100644
7749 index 0000000..b790a54
7750 --- /dev/null
7751 +++ b/gdb/python/lib/gdb/FrameWrapper.py
7752 @@ -0,0 +1,112 @@
7753 +# Wrapper API for frames.
7754 +
7755 +# Copyright (C) 2008, 2009 Free Software Foundation, Inc.
7756 +
7757 +# This program is free software; you can redistribute it and/or modify
7758 +# it under the terms of the GNU General Public License as published by
7759 +# the Free Software Foundation; either version 3 of the License, or
7760 +# (at your option) any later version.
7761 +#
7762 +# This program is distributed in the hope that it will be useful,
7763 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
7764 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7765 +# GNU General Public License for more details.
7766 +#
7767 +# You should have received a copy of the GNU General Public License
7768 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
7769 +
7770 +import gdb
7771 +
7772 +# FIXME: arguably all this should be on Frame somehow.
7773 +class FrameWrapper:
7774 +    def __init__ (self, frame):
7775 +        self.frame = frame;
7776 +
7777 +    def write_symbol (self, stream, sym, block):
7778 +        if len (sym.linkage_name):
7779 +            nsym, is_field_of_this = gdb.lookup_symbol (sym.linkage_name, block)
7780 +            if nsym.addr_class != gdb.SYMBOL_LOC_REGISTER:
7781 +                sym = nsym
7782 +
7783 +        stream.write (sym.print_name + "=")
7784 +        try:
7785 +            val = self.read_var (sym)
7786 +            if val != None:
7787 +                val = str (val)
7788 +        # FIXME: would be nice to have a more precise exception here.
7789 +        except RuntimeError, text:
7790 +            val = text
7791 +        if val == None:
7792 +            stream.write ("???")
7793 +        else:
7794 +            stream.write (str (val))
7795 +
7796 +    def print_frame_locals (self, stream, func):
7797 +        if not func:
7798 +            return
7799 +
7800 +        first = True
7801 +        block = func.value
7802 +
7803 +        for sym in block:
7804 +            if sym.is_argument:
7805 +                continue;
7806 +
7807 +            self.write_symbol (stream, sym, block)
7808 +            stream.write ('\n')
7809 +
7810 +    def print_frame_args (self, stream, func):
7811 +        if not func:
7812 +            return
7813 +
7814 +        first = True
7815 +        block = func.value
7816 +
7817 +        for sym in block:
7818 +            if not sym.is_argument:
7819 +                continue;
7820 +
7821 +            if not first:
7822 +                stream.write (", ")
7823 +
7824 +            self.write_symbol (stream, sym, block)
7825 +            first = False
7826 +
7827 +    # FIXME: this should probably just be a method on gdb.Frame.
7828 +    # But then we need stream wrappers.
7829 +    def describe (self, stream, full):
7830 +        if self.type () == gdb.DUMMY_FRAME:
7831 +            stream.write (" <function called from gdb>\n")
7832 +        elif self.type () == gdb.SIGTRAMP_FRAME:
7833 +            stream.write (" <signal handler called>\n")
7834 +        else:
7835 +            sal = self.find_sal ()
7836 +            pc = self.pc ()
7837 +            name = self.name ()
7838 +            if not name:
7839 +                name = "??"
7840 +            if pc != sal.pc or not sal.symtab:
7841 +                stream.write (" 0x%08x in" % pc)
7842 +            stream.write (" " + name + " (")
7843 +
7844 +            func = self.function ()
7845 +            self.print_frame_args (stream, func)
7846 +
7847 +            stream.write (")")
7848 +
7849 +            if sal.symtab and sal.symtab.filename:
7850 +                stream.write (" at " + sal.symtab.filename)
7851 +                stream.write (":" + str (sal.line))
7852 +
7853 +            if not self.name () or (not sal.symtab or not sal.symtab.filename):
7854 +                lib = gdb.solib_address (pc)
7855 +                if lib:
7856 +                    stream.write (" from " + lib)
7857 +
7858 +            stream.write ("\n")
7859 +
7860 +            if full:
7861 +                self.print_frame_locals (stream, func)
7862 +
7863 +    def __getattr__ (self, name):
7864 +        return getattr (self.frame, name)
7865 diff --git a/gdb/python/lib/gdb/backtrace.py b/gdb/python/lib/gdb/backtrace.py
7866 new file mode 100644
7867 index 0000000..6bb4fb1
7868 --- /dev/null
7869 +++ b/gdb/python/lib/gdb/backtrace.py
7870 @@ -0,0 +1,42 @@
7871 +# Filtering backtrace.
7872 +
7873 +# Copyright (C) 2008, 2011 Free Software Foundation, Inc.
7874 +
7875 +# This program is free software; you can redistribute it and/or modify
7876 +# it under the terms of the GNU General Public License as published by
7877 +# the Free Software Foundation; either version 3 of the License, or
7878 +# (at your option) any later version.
7879 +#
7880 +# This program is distributed in the hope that it will be useful,
7881 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
7882 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7883 +# GNU General Public License for more details.
7884 +#
7885 +# You should have received a copy of the GNU General Public License
7886 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
7887 +
7888 +import gdb
7889 +import itertools
7890 +
7891 +# Our only exports.
7892 +__all__ = ['push_frame_filter', 'create_frame_filter']
7893 +
7894 +frame_filter = None
7895 +
7896 +def push_frame_filter (constructor):
7897 +    """Register a new backtrace filter class with the 'backtrace' command.
7898 +The filter will be passed an iterator as an argument.  The iterator
7899 +will return gdb.Frame-like objects.  The filter should in turn act as
7900 +an iterator returning such objects."""
7901 +    global frame_filter
7902 +    if frame_filter == None:
7903 +        frame_filter = constructor
7904 +    else:
7905 +        frame_filter = lambda iterator, filter = frame_filter: constructor (filter (iterator))
7906 +
7907 +def create_frame_filter (iter):
7908 +    global frame_filter
7909 +    if frame_filter is None:
7910 +        return iter
7911 +    return frame_filter (iter)
7912 +
7913 diff --git a/gdb/python/lib/gdb/command/backtrace.py b/gdb/python/lib/gdb/command/backtrace.py
7914 new file mode 100644
7915 index 0000000..eeea909
7916 --- /dev/null
7917 +++ b/gdb/python/lib/gdb/command/backtrace.py
7918 @@ -0,0 +1,106 @@
7919 +# New backtrace command.
7920 +
7921 +# Copyright (C) 2008, 2009, 2011 Free Software Foundation, Inc.
7922 +
7923 +# This program is free software; you can redistribute it and/or modify
7924 +# it under the terms of the GNU General Public License as published by
7925 +# the Free Software Foundation; either version 3 of the License, or
7926 +# (at your option) any later version.
7927 +#
7928 +# This program is distributed in the hope that it will be useful,
7929 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
7930 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7931 +# GNU General Public License for more details.
7932 +#
7933 +# You should have received a copy of the GNU General Public License
7934 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
7935 +
7936 +import gdb
7937 +import gdb.backtrace
7938 +import itertools
7939 +from gdb.FrameIterator import FrameIterator
7940 +from gdb.FrameWrapper import FrameWrapper
7941 +import sys
7942 +
7943 +class ReverseBacktraceParameter (gdb.Parameter):
7944 +    """The new-backtrace command can show backtraces in 'reverse' order.
7945 +This means that the innermost frame will be printed last.
7946 +Note that reverse backtraces are more expensive to compute."""
7947 +
7948 +    set_doc = "Enable or disable reverse backtraces."
7949 +    show_doc = "Show whether backtraces will be printed in reverse order."
7950 +
7951 +    def __init__(self):
7952 +        gdb.Parameter.__init__ (self, "reverse-backtrace",
7953 +                                gdb.COMMAND_STACK, gdb.PARAM_BOOLEAN)
7954 +        # Default to compatibility with gdb.
7955 +        self.value = False
7956 +
7957 +class FilteringBacktrace (gdb.Command):
7958 +    """Print backtrace of all stack frames, or innermost COUNT frames.
7959 +With a negative argument, print outermost -COUNT frames.
7960 +Use of the 'full' qualifier also prints the values of the local variables.
7961 +Use of the 'raw' qualifier avoids any filtering by loadable modules.
7962 +"""
7963 +
7964 +    def __init__ (self):
7965 +        # FIXME: this is not working quite well enough to replace
7966 +        # "backtrace" yet.
7967 +        gdb.Command.__init__ (self, "new-backtrace", gdb.COMMAND_STACK)
7968 +        self.reverse = ReverseBacktraceParameter()
7969 +
7970 +    def reverse_iter (self, iter):
7971 +        result = []
7972 +        for item in iter:
7973 +            result.append (item)
7974 +        result.reverse()
7975 +        return result
7976 +
7977 +    def final_n (self, iter, x):
7978 +        result = []
7979 +        for item in iter:
7980 +            result.append (item)
7981 +        return result[x:]
7982 +
7983 +    def invoke (self, arg, from_tty):
7984 +        i = 0
7985 +        count = 0
7986 +        filter = True
7987 +        full = False
7988 +
7989 +        for word in arg.split (" "):
7990 +            if word == '':
7991 +                continue
7992 +            elif word == 'raw':
7993 +                filter = False
7994 +            elif word == 'full':
7995 +                full = True
7996 +            else:
7997 +                count = int (word)
7998 +
7999 +        # FIXME: provide option to start at selected frame
8000 +        # However, should still number as if starting from newest
8001 +        newest_frame = gdb.newest_frame()
8002 +        iter = itertools.imap (FrameWrapper,
8003 +                               FrameIterator (newest_frame))
8004 +        if filter:
8005 +            iter = gdb.backtrace.create_frame_filter (iter)
8006 +
8007 +        # Now wrap in an iterator that numbers the frames.
8008 +        iter = itertools.izip (itertools.count (0), iter)
8009 +
8010 +        # Reverse if the user wanted that.
8011 +        if self.reverse.value:
8012 +            iter = self.reverse_iter (iter)
8013 +
8014 +        # Extract sub-range user wants.
8015 +        if count < 0:
8016 +            iter = self.final_n (iter, count)
8017 +        elif count > 0:
8018 +            iter = itertools.islice (iter, 0, count)
8019 +
8020 +        for pair in iter:
8021 +            sys.stdout.write ("#%-2d" % pair[0])
8022 +            pair[1].describe (sys.stdout, full)
8023 +
8024 +FilteringBacktrace()
8025 diff --git a/gdb/python/lib/gdb/command/ignore_errors.py b/gdb/python/lib/gdb/command/ignore_errors.py
8026 new file mode 100644
8027 index 0000000..6fa48ff
8028 --- /dev/null
8029 +++ b/gdb/python/lib/gdb/command/ignore_errors.py
8030 @@ -0,0 +1,37 @@
8031 +# Ignore errors in user commands.
8032 +
8033 +# Copyright (C) 2008 Free Software Foundation, Inc.
8034 +
8035 +# This program is free software; you can redistribute it and/or modify
8036 +# it under the terms of the GNU General Public License as published by
8037 +# the Free Software Foundation; either version 3 of the License, or
8038 +# (at your option) any later version.
8039 +#
8040 +# This program is distributed in the hope that it will be useful,
8041 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
8042 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8043 +# GNU General Public License for more details.
8044 +#
8045 +# You should have received a copy of the GNU General Public License
8046 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
8047 +
8048 +import gdb
8049 +
8050 +class IgnoreErrorsCommand (gdb.Command):
8051 +    """Execute a single command, ignoring all errors.
8052 +Only one-line commands are supported.
8053 +This is primarily useful in scripts."""
8054 +
8055 +    def __init__ (self):
8056 +        super (IgnoreErrorsCommand, self).__init__ ("ignore-errors",
8057 +                                                    gdb.COMMAND_OBSCURE,
8058 +                                                    # FIXME...
8059 +                                                    gdb.COMPLETE_COMMAND)
8060 +
8061 +    def invoke (self, arg, from_tty):
8062 +        try:
8063 +            gdb.execute (arg, from_tty)
8064 +        except:
8065 +            pass
8066 +
8067 +IgnoreErrorsCommand ()
8068 diff --git a/gdb/python/lib/gdb/command/pahole.py b/gdb/python/lib/gdb/command/pahole.py
8069 new file mode 100644
8070 index 0000000..21a0bf0
8071 --- /dev/null
8072 +++ b/gdb/python/lib/gdb/command/pahole.py
8073 @@ -0,0 +1,75 @@
8074 +# pahole command for gdb
8075 +
8076 +# Copyright (C) 2008, 2009 Free Software Foundation, Inc.
8077 +
8078 +# This program is free software; you can redistribute it and/or modify
8079 +# it under the terms of the GNU General Public License as published by
8080 +# the Free Software Foundation; either version 3 of the License, or
8081 +# (at your option) any later version.
8082 +#
8083 +# This program is distributed in the hope that it will be useful,
8084 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
8085 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8086 +# GNU General Public License for more details.
8087 +#
8088 +# You should have received a copy of the GNU General Public License
8089 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
8090 +
8091 +import gdb
8092 +
8093 +class Pahole (gdb.Command):
8094 +    """Show the holes in a structure.
8095 +This command takes a single argument, a type name.
8096 +It prints the type and displays comments showing where holes are."""
8097 +
8098 +    def __init__ (self):
8099 +        super (Pahole, self).__init__ ("pahole", gdb.COMMAND_NONE,
8100 +                                       gdb.COMPLETE_SYMBOL)
8101 +
8102 +    def pahole (self, type, level, name):
8103 +        if name is None:
8104 +            name = ''
8105 +        tag = type.tag
8106 +        if tag is None:
8107 +            tag = ''
8108 +        print '%sstruct %s {' % (' ' * (2 * level), tag)
8109 +        bitpos = 0
8110 +        for field in type.fields ():
8111 +            # Skip static fields.
8112 +            if not hasattr (field, ('bitpos')):
8113 +                continue
8114 +
8115 +            ftype = field.type.strip_typedefs()
8116 +
8117 +            if bitpos != field.bitpos:
8118 +                hole = field.bitpos - bitpos
8119 +                print '  /* XXX %d bit hole, try to pack */' % hole
8120 +                bitpos = field.bitpos
8121 +            if field.bitsize > 0:
8122 +                fieldsize = field.bitsize
8123 +            else:
8124 +                # TARGET_CHAR_BIT here...
8125 +                fieldsize = 8 * ftype.sizeof
8126 +
8127 +            # TARGET_CHAR_BIT
8128 +            print ' /* %3d %3d */' % (int (bitpos / 8), int (fieldsize / 8)),
8129 +            bitpos = bitpos + fieldsize
8130 +
8131 +            if ftype.code == gdb.TYPE_CODE_STRUCT:
8132 +                self.pahole (ftype, level + 1, field.name)
8133 +            else:
8134 +                print ' ' * (2 + 2 * level),
8135 +                print '%s %s' % (str (ftype), field.name)
8136 +
8137 +        print ' ' * (14 + 2 * level),
8138 +        print '} %s' % name
8139 +
8140 +    def invoke (self, arg, from_tty):
8141 +        type = gdb.lookup_type (arg)
8142 +        type = type.strip_typedefs ()
8143 +        if type.code != gdb.TYPE_CODE_STRUCT:
8144 +            raise TypeError, '%s is not a struct type' % arg
8145 +        print ' ' * 14,
8146 +        self.pahole (type, 0, '')
8147 +
8148 +Pahole()
8149 diff --git a/gdb/python/lib/gdb/command/require.py b/gdb/python/lib/gdb/command/require.py
8150 new file mode 100644
8151 index 0000000..1fbc1e8
8152 --- /dev/null
8153 +++ b/gdb/python/lib/gdb/command/require.py
8154 @@ -0,0 +1,57 @@
8155 +# Demand-loading commands.
8156 +
8157 +# Copyright (C) 2008, 2009 Free Software Foundation, Inc.
8158 +
8159 +# This program is free software; you can redistribute it and/or modify
8160 +# it under the terms of the GNU General Public License as published by
8161 +# the Free Software Foundation; either version 3 of the License, or
8162 +# (at your option) any later version.
8163 +#
8164 +# This program is distributed in the hope that it will be useful,
8165 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
8166 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8167 +# GNU General Public License for more details.
8168 +#
8169 +# You should have received a copy of the GNU General Public License
8170 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
8171 +
8172 +import gdb
8173 +import os
8174 +
8175 +class RequireCommand (gdb.Command):
8176 +    """Prefix command for requiring features."""
8177 +
8178 +    def __init__ (self):
8179 +        super (RequireCommand, self).__init__ ("require",
8180 +                                               gdb.COMMAND_SUPPORT,
8181 +                                               gdb.COMPLETE_NONE,
8182 +                                               True)
8183 +
8184 +class RequireSubcommand (gdb.Command):
8185 +    """Demand-load a command by name."""
8186 +
8187 +    def __init__ (self, name):
8188 +        self.__doc__ = "Demand-load a %s by name." % name
8189 +        super (RequireSubcommand, self).__init__ ("require %s" % name,
8190 +                                                  gdb.COMMAND_SUPPORT)
8191 +        self.name = name
8192 +
8193 +    def invoke (self, arg, from_tty):
8194 +        for cmd in arg.split():
8195 +            exec ('import gdb.' + self.name + '.' + cmd, globals ())
8196 +
8197 +    def complete (self, text, word):
8198 +        dir = gdb.pythondir + '/gdb/' + self.name
8199 +        result = []
8200 +        for file in os.listdir(dir):
8201 +            if not file.startswith (word) or not file.endswith ('.py'):
8202 +                continue
8203 +            feature = file[0:-3]
8204 +            if feature == 'require' or feature == '__init__':
8205 +                continue
8206 +            result.append (feature)
8207 +        return result
8208 +
8209 +RequireCommand()
8210 +RequireSubcommand("command")
8211 +RequireSubcommand("function")
8212 diff --git a/gdb/python/lib/gdb/command/upto.py b/gdb/python/lib/gdb/command/upto.py
8213 new file mode 100644
8214 index 0000000..faf54ed
8215 --- /dev/null
8216 +++ b/gdb/python/lib/gdb/command/upto.py
8217 @@ -0,0 +1,129 @@
8218 +# upto command.
8219 +
8220 +# Copyright (C) 2009 Free Software Foundation, Inc.
8221 +
8222 +# This program is free software; you can redistribute it and/or modify
8223 +# it under the terms of the GNU General Public License as published by
8224 +# the Free Software Foundation; either version 3 of the License, or
8225 +# (at your option) any later version.
8226 +#
8227 +# This program is distributed in the hope that it will be useful,
8228 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
8229 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8230 +# GNU General Public License for more details.
8231 +#
8232 +# You should have received a copy of the GNU General Public License
8233 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
8234 +
8235 +import gdb
8236 +import re
8237 +from gdb.FrameIterator import FrameIterator
8238 +from gdb.FrameWrapper import FrameWrapper
8239 +
8240 +class UptoPrefix (gdb.Command):
8241 +    def __init__ (self):
8242 +        super (UptoPrefix, self).__init__ ("upto", gdb.COMMAND_STACK,
8243 +                                           prefix = True)
8244 +
8245 +class UptoImplementation (gdb.Command):
8246 +    def __init__ (self, subcommand):
8247 +        super (UptoImplementation, self).__init__ ("upto " + subcommand,
8248 +                                                   gdb.COMMAND_STACK)
8249 +
8250 +    def search (self):
8251 +        saved = gdb.selected_frame ()
8252 +        iter = FrameIterator (saved)
8253 +        found = False
8254 +        try:
8255 +            for frame in iter:
8256 +                frame.select ()
8257 +                try:
8258 +                    if self.filter (frame):
8259 +                        wrapper = FrameWrapper (frame)
8260 +                        wrapper.describe (sys.stdout, False)
8261 +                        return
8262 +                except:
8263 +                    pass
8264 +        except:
8265 +            pass
8266 +        saved.select ()
8267 +        raise RuntimeError, 'Could not find a matching frame'
8268 +
8269 +    def invoke (self, arg, from_tty):
8270 +        self.rx = re.compile (arg)
8271 +        self.search ()
8272 +
8273 +class UptoSymbolCommand (UptoImplementation):
8274 +    """Select and print some calling stack frame, based on symbol.
8275 +The argument is a regular expression.  This command moves up the
8276 +stack, stopping at the first frame whose symbol matches the regular
8277 +expression."""
8278 +
8279 +    def __init__ (self):
8280 +        super (UptoSymbolCommand, self).__init__ ("symbol")
8281 +
8282 +    def filter (self, frame):
8283 +        name = frame.name ()
8284 +        if name is not None:
8285 +            if self.rx.search (name) is not None:
8286 +                return True
8287 +        return False
8288 +
8289 +class UptoSourceCommand (UptoImplementation):
8290 +    """Select and print some calling stack frame, based on source file.
8291 +The argument is a regular expression.  This command moves up the
8292 +stack, stopping at the first frame whose source file name matches the
8293 +regular expression."""
8294 +
8295 +    def __init__ (self):
8296 +        super (UptoSourceCommand, self).__init__ ("source")
8297 +
8298 +    def filter (self, frame):
8299 +        name = frame.find_sal ().symtab.filename
8300 +        if name is not None:
8301 +            if self.rx.search (name) is not None:
8302 +                return True
8303 +        return False
8304 +
8305 +class UptoObjectCommand (UptoImplementation):
8306 +    """Select and print some calling stack frame, based on object file.
8307 +The argument is a regular expression.  This command moves up the
8308 +stack, stopping at the first frame whose object file name matches the
8309 +regular expression."""
8310 +
8311 +    def __init__ (self):
8312 +        super (UptoObjectCommand, self).__init__ ("object")
8313 +
8314 +    def filter (self, frame):
8315 +        name = frame.find_sal ().symtab.objfile.filename
8316 +        if name is not None:
8317 +            if self.rx.search (name) is not None:
8318 +                return True
8319 +        return False
8320 +
8321 +class UptoWhereCommand (UptoImplementation):
8322 +    """Select and print some calling stack frame, based on expression.
8323 +The argument is an expression.  This command moves up the stack,
8324 +parsing and evaluating the expression in each frame.  This stops when
8325 +the expression evaluates to a non-zero (true) value."""
8326 +
8327 +    def __init__ (self):
8328 +        super (UptoWhereCommand, self).__init__ ("where")
8329 +
8330 +    def filter (self, frame):
8331 +        try:
8332 +            if gdb.parse_and_eval (self.expression):
8333 +                return True
8334 +        except:
8335 +            pass
8336 +        return False
8337 +
8338 +    def invoke (self, arg, from_tty):
8339 +        self.expression = arg
8340 +        self.search ()
8341 +
8342 +UptoPrefix ()
8343 +UptoSymbolCommand ()
8344 +UptoSourceCommand ()
8345 +UptoObjectCommand ()
8346 +UptoWhereCommand ()
8347 diff --git a/gdb/python/lib/gdb/function/__init__.py b/gdb/python/lib/gdb/function/__init__.py
8348 new file mode 100644
8349 index 0000000..8b13789
8350 --- /dev/null
8351 +++ b/gdb/python/lib/gdb/function/__init__.py
8352 @@ -0,0 +1 @@
8353 +
8354 diff --git a/gdb/python/lib/gdb/function/caller_is.py b/gdb/python/lib/gdb/function/caller_is.py
8355 new file mode 100644
8356 index 0000000..2b9c5c7
8357 --- /dev/null
8358 +++ b/gdb/python/lib/gdb/function/caller_is.py
8359 @@ -0,0 +1,58 @@
8360 +# Caller-is functions.
8361 +
8362 +# Copyright (C) 2008 Free Software Foundation, Inc.
8363 +
8364 +# This program is free software; you can redistribute it and/or modify
8365 +# it under the terms of the GNU General Public License as published by
8366 +# the Free Software Foundation; either version 3 of the License, or
8367 +# (at your option) any later version.
8368 +#
8369 +# This program is distributed in the hope that it will be useful,
8370 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
8371 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8372 +# GNU General Public License for more details.
8373 +#
8374 +# You should have received a copy of the GNU General Public License
8375 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
8376 +
8377 +import gdb
8378 +import re
8379 +
8380 +class CallerIs (gdb.Function):
8381 +    """Return True if the calling function's name is equal to a string.
8382 +This function takes one or two arguments.
8383 +The first argument is the name of a function; if the calling function's
8384 +name is equal to this argument, this function returns True.
8385 +The optional second argument tells this function how many stack frames
8386 +to traverse to find the calling function.  The default is 1."""
8387 +
8388 +    def __init__ (self):
8389 +        super (CallerIs, self).__init__ ("caller_is")
8390 +
8391 +    def invoke (self, name, nframes = 1):
8392 +        frame = gdb.selected_frame ()
8393 +        while nframes > 0:
8394 +            frame = frame.older ()
8395 +            nframes = nframes - 1
8396 +        return frame.name () == name.string ()
8397 +
8398 +class CallerMatches (gdb.Function):
8399 +    """Return True if the calling function's name matches a string.
8400 +This function takes one or two arguments.
8401 +The first argument is a regular expression; if the calling function's
8402 +name is matched by this argument, this function returns True.
8403 +The optional second argument tells this function how many stack frames
8404 +to traverse to find the calling function.  The default is 1."""
8405 +
8406 +    def __init__ (self):
8407 +        super (CallerMatches, self).__init__ ("caller_matches")
8408 +
8409 +    def invoke (self, name, nframes = 1):
8410 +        frame = gdb.selected_frame ()
8411 +        while nframes > 0:
8412 +            frame = frame.older ()
8413 +            nframes = nframes - 1
8414 +        return re.match (name.string (), frame.name ()) is not None
8415 +
8416 +CallerIs()
8417 +CallerMatches()
8418 diff --git a/gdb/python/lib/gdb/function/in_scope.py b/gdb/python/lib/gdb/function/in_scope.py
8419 new file mode 100644
8420 index 0000000..debb3bb
8421 --- /dev/null
8422 +++ b/gdb/python/lib/gdb/function/in_scope.py
8423 @@ -0,0 +1,47 @@
8424 +# In-scope function.
8425 +
8426 +# Copyright (C) 2008 Free Software Foundation, Inc.
8427 +
8428 +# This program is free software; you can redistribute it and/or modify
8429 +# it under the terms of the GNU General Public License as published by
8430 +# the Free Software Foundation; either version 3 of the License, or
8431 +# (at your option) any later version.
8432 +#
8433 +# This program is distributed in the hope that it will be useful,
8434 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
8435 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8436 +# GNU General Public License for more details.
8437 +#
8438 +# You should have received a copy of the GNU General Public License
8439 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
8440 +
8441 +import gdb
8442 +
8443 +class InScope (gdb.Function):
8444 +    """Return True if all the given variables or macros are in scope.
8445 +Takes one argument for each variable name to be checked."""
8446 +
8447 +    def __init__ (self):
8448 +       super (InScope, self).__init__ ("in_scope")
8449 +
8450 +    def invoke (self, *vars):
8451 +        if len (vars) == 0:
8452 +           raise TypeError, "in_scope takes at least one argument"
8453 +
8454 +        # gdb.Value isn't hashable so it can't be put in a map.
8455 +       # Convert to string first.
8456 +       wanted = set (map (lambda x: x.string (), vars))
8457 +       found = set ()
8458 +       block = gdb.selected_frame ().block ()
8459 +       while block:
8460 +           for sym in block:
8461 +               if (sym.is_argument or sym.is_constant
8462 +                     or sym.is_function or sym.is_variable):
8463 +                   if sym.name in wanted:
8464 +                       found.add (sym.name)
8465 +
8466 +           block = block.superblock
8467 +
8468 +       return wanted == found
8469 +
8470 +InScope ()
8471 diff --git a/gdb/python/py-type.c b/gdb/python/py-type.c
8472 index 98030a6..9870eec 100644
8473 --- a/gdb/python/py-type.c
8474 +++ b/gdb/python/py-type.c
8475 @@ -30,6 +30,8 @@
8476  #include "vec.h"
8477  #include "bcache.h"
8478  #include "dwarf2loc.h"
8479 +#include "observer.h"
8480 +#include "gdb_assert.h"
8481  
8482  typedef struct pyty_type_object
8483  {
8484 @@ -38,11 +40,19 @@ typedef struct pyty_type_object
8485  
8486    /* If a Type object is associated with an objfile, it is kept on a
8487       doubly-linked list, rooted in the objfile.  This lets us copy the
8488 -     underlying struct type when the objfile is deleted.  */
8489 +     underlying struct type when the objfile is deleted.
8490 +
8491 +     With NULL objfile Type still can be doubly-linked in the list
8492 +     PYTY_OBJECTS_DISCARDABLE.  */
8493    struct pyty_type_object *prev;
8494    struct pyty_type_object *next;
8495  } type_object;
8496  
8497 +#if 0
8498 +/* First element of a doubly-linked list of TYPE_DISCARDABLE Types.  */
8499 +static type_object *pyty_objects_discardable;
8500 +#endif
8501 +
8502  static PyTypeObject type_object_type;
8503  
8504  /* A Field object.  */
8505 @@ -1166,8 +1176,63 @@ typy_richcompare (PyObject *self, PyObject *other, int op)
8506  
8507  \f
8508  
8509 +/* Key associated with each objfile pointing to the first element of
8510 +   a doubly-linked list of Types associated with this objfile.  */
8511  static const struct objfile_data *typy_objfile_data_key;
8512  
8513 +/* Link TYPE_OBJ to its appropriate list.  Either to its objfile associated one
8514 +   or at least to the global list for TYPE_DISCARDABLE Types.  Permanent types
8515 +   do not get linked anywhere.  */
8516 +static void
8517 +typy_link (type_object *type_obj)
8518 +{
8519 +  type_obj->prev = NULL;
8520 +
8521 +  if (type_obj->type && TYPE_OBJFILE (type_obj->type))
8522 +    {
8523 +      struct objfile *objfile = TYPE_OBJFILE (type_obj->type);
8524 +
8525 +      type_obj->next = objfile_data (objfile, typy_objfile_data_key);
8526 +      if (type_obj->next)
8527 +       type_obj->next->prev = type_obj;
8528 +      set_objfile_data (objfile, typy_objfile_data_key, type_obj);
8529 +    }
8530 +#if 0
8531 +  else if (type_obj->type && TYPE_DISCARDABLE (type_obj->type))
8532 +    {
8533 +      type_obj->next = pyty_objects_discardable;
8534 +      if (type_obj->next)
8535 +       type_obj->next->prev = type_obj;
8536 +      pyty_objects_discardable = type_obj;
8537 +    }
8538 +#endif
8539 +  else
8540 +    type_obj->next = NULL;
8541 +}
8542 +
8543 +/* Unlink TYPE_OBJ from its current list.  Permanent types are not linked
8544 +   anywhere and this function has no effect on them.  */
8545 +static void
8546 +typy_unlink (type_object *type_obj)
8547 +{
8548 +  if (type_obj->prev)
8549 +    type_obj->prev->next = type_obj->next;
8550 +  else if (type_obj->type && TYPE_OBJFILE (type_obj->type))
8551 +    {
8552 +      /* Must reset head of list.  */
8553 +      struct objfile *objfile = TYPE_OBJFILE (type_obj->type);
8554 +
8555 +      set_objfile_data (objfile, typy_objfile_data_key, type_obj->next);
8556 +    }
8557 +#if 0
8558 +  else if (pyty_objects_discardable == type_obj)
8559 +    pyty_objects_discardable = type_obj->next;
8560 +#endif
8561 +
8562 +  if (type_obj->next)
8563 +    type_obj->next->prev = type_obj->prev;
8564 +}
8565 +
8566  static void
8567  save_objfile_types (struct objfile *objfile, void *datum)
8568  {
8569 @@ -1185,12 +1250,13 @@ save_objfile_types (struct objfile *objfile, void *datum)
8570      {
8571        type_object *next = obj->next;
8572  
8573 -      htab_empty (copied_types);
8574 +      gdb_assert (TYPE_OBJFILE (obj->type) == objfile);
8575 +      typy_unlink (obj);
8576  
8577 -      obj->type = copy_type_recursive (objfile, obj->type, copied_types);
8578 +      obj->type = copy_type_recursive (obj->type, copied_types);
8579  
8580 -      obj->next = NULL;
8581 -      obj->prev = NULL;
8582 +      gdb_assert (TYPE_OBJFILE (obj->type) == NULL);
8583 +      typy_link (obj);
8584  
8585        obj = next;
8586      }
8587 @@ -1201,43 +1267,28 @@ save_objfile_types (struct objfile *objfile, void *datum)
8588  }
8589  
8590  static void
8591 -set_type (type_object *obj, struct type *type)
8592 +typy_dealloc (PyObject *obj)
8593  {
8594 -  obj->type = type;
8595 -  obj->prev = NULL;
8596 -  if (type && TYPE_OBJFILE (type))
8597 -    {
8598 -      struct objfile *objfile = TYPE_OBJFILE (type);
8599 +  type_object *type_obj = (type_object *) obj;
8600  
8601 -      obj->next = objfile_data (objfile, typy_objfile_data_key);
8602 -      if (obj->next)
8603 -       obj->next->prev = obj;
8604 -      set_objfile_data (objfile, typy_objfile_data_key, obj);
8605 -    }
8606 -  else
8607 -    obj->next = NULL;
8608 +  typy_unlink (type_obj);
8609 +
8610 +  type_obj->ob_type->tp_free (obj);
8611  }
8612  
8613 +#if 0
8614 +/* Call type_mark_used for any TYPEs referenced from this GDB source file.  */
8615  static void
8616 -typy_dealloc (PyObject *obj)
8617 +typy_types_mark_used (void)
8618  {
8619 -  type_object *type = (type_object *) obj;
8620 -
8621 -  if (type->prev)
8622 -    type->prev->next = type->next;
8623 -  else if (type->type && TYPE_OBJFILE (type->type))
8624 -    {
8625 -      /* Must reset head of list.  */
8626 -      struct objfile *objfile = TYPE_OBJFILE (type->type);
8627 -
8628 -      if (objfile)
8629 -       set_objfile_data (objfile, typy_objfile_data_key, type->next);
8630 -    }
8631 -  if (type->next)
8632 -    type->next->prev = type->prev;
8633 +  type_object *type_obj;
8634  
8635 -  type->ob_type->tp_free (type);
8636 +  for (type_obj = pyty_objects_discardable;
8637 +       type_obj != NULL;
8638 +       type_obj = type_obj->next)
8639 +    type_mark_used (type_obj->type);
8640  }
8641 +#endif
8642  
8643  /* Return number of fields ("length" of the field dictionary).  */
8644  
8645 @@ -1457,7 +1508,10 @@ type_to_type_object (struct type *type)
8646  
8647    type_obj = PyObject_New (type_object, &type_object_type);
8648    if (type_obj)
8649 -    set_type (type_obj, type);
8650 +    {
8651 +      type_obj->type = type;
8652 +      typy_link (type_obj);
8653 +    }
8654  
8655    return (PyObject *) type_obj;
8656  }
8657 @@ -1537,6 +1591,10 @@ gdbpy_initialize_types (void)
8658  
8659    Py_INCREF (&field_object_type);
8660    PyModule_AddObject (gdb_module, "Field", (PyObject *) &field_object_type);
8661 +
8662 +#if 0
8663 +  observer_attach_mark_used (typy_types_mark_used);
8664 +#endif
8665  }
8666  
8667  \f
8668 diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c
8669 index 6f67bdb..5ddd6bc 100644
8670 --- a/gdb/python/py-value.c
8671 +++ b/gdb/python/py-value.c
8672 @@ -29,6 +29,7 @@
8673  #include "expression.h"
8674  #include "cp-abi.h"
8675  #include "python.h"
8676 +#include "observer.h"
8677  
8678  #ifdef HAVE_PYTHON
8679  
8680 @@ -1378,6 +1379,19 @@ gdbpy_is_value_object (PyObject *obj)
8681    return PyObject_TypeCheck (obj, &value_object_type);
8682  }
8683  
8684 +#if 0
8685 +/* Call type_mark_used for any TYPEs referenced from this GDB source file.  */
8686 +
8687 +static void
8688 +python_types_mark_used (void)
8689 +{
8690 +  value_object *iter;
8691 +
8692 +  for (iter = values_in_python; iter; iter = iter->next)
8693 +    type_mark_used (value_type (iter->value));
8694 +}
8695 +#endif
8696 +
8697  void
8698  gdbpy_initialize_values (void)
8699  {
8700 @@ -1388,6 +1402,10 @@ gdbpy_initialize_values (void)
8701    PyModule_AddObject (gdb_module, "Value", (PyObject *) &value_object_type);
8702  
8703    values_in_python = NULL;
8704 +
8705 +#if 0
8706 +  observer_attach_mark_used (python_types_mark_used);
8707 +#endif
8708  }
8709  
8710  \f
8711 diff --git a/gdb/python/python.c b/gdb/python/python.c
8712 index c66efe4..0211fcb 100644
8713 --- a/gdb/python/python.c
8714 +++ b/gdb/python/python.c
8715 @@ -66,10 +66,13 @@ static const char *gdbpy_should_print_stack = python_excp_message;
8716  #include "linespec.h"
8717  #include "source.h"
8718  #include "version.h"
8719 +#include "inferior.h"
8720 +#include "gdbthread.h"
8721  #include "target.h"
8722  #include "gdbthread.h"
8723  #include "observer.h"
8724  #include "interps.h"
8725 +#include "event-top.h"
8726  
8727  static PyMethodDef GdbMethods[];
8728  
8729 @@ -979,6 +982,53 @@ gdbpy_print_stack (void)
8730  
8731  /* Return the current Progspace.
8732     There always is one.  */
8733 +/* True if 'gdb -P' was used, false otherwise.  */
8734 +static int running_python_script;
8735 +
8736 +/* True if we are currently in a call to 'gdb.cli', false otherwise.  */
8737 +static int in_cli;
8738 +
8739 +/* Enter the command loop.  */
8740 +
8741 +static PyObject *
8742 +gdbpy_cli (PyObject *unused1, PyObject *unused2)
8743 +{
8744 +  if (! running_python_script || in_cli)
8745 +    return PyErr_Format (PyExc_RuntimeError, "cannot invoke CLI recursively");
8746 +
8747 +  in_cli = 1;
8748 +  cli_command_loop ();
8749 +  in_cli = 0;
8750 +
8751 +  Py_RETURN_NONE;
8752 +}
8753 +
8754 +/* Set up the Python argument vector and evaluate a script.  This is
8755 +   used to implement 'gdb -P'.  */
8756 +
8757 +void
8758 +run_python_script (int argc, char **argv)
8759 +{
8760 +  FILE *input;
8761 +
8762 +  /* We never free this, since we plan to exit at the end.  */
8763 +  ensure_python_env (get_current_arch (), current_language);
8764 +
8765 +  running_python_script = 1;
8766 +  PySys_SetArgv (argc - 1, argv + 1);
8767 +  input = fopen (argv[0], "r");
8768 +  if (! input)
8769 +    {
8770 +      fprintf (stderr, "could not open %s: %s\n", argv[0], strerror (errno));
8771 +      exit (1);
8772 +    }
8773 +  PyRun_SimpleFile (input, argv[0]);
8774 +  fclose (input);
8775 +  exit (0);
8776 +}
8777 +
8778 +\f
8779 +
8780  
8781  static PyObject *
8782  gdbpy_get_current_progspace (PyObject *unused1, PyObject *unused2)
8783 @@ -1420,6 +1470,8 @@ static PyMethodDef GdbMethods[] =
8784      "Get a value from history" },
8785    { "execute", (PyCFunction) execute_gdb_command, METH_VARARGS | METH_KEYWORDS,
8786      "Execute a gdb command" },
8787 +  { "cli", gdbpy_cli, METH_NOARGS,
8788 +    "Enter the gdb CLI" },
8789    { "parameter", gdbpy_parameter, METH_VARARGS,
8790      "Return a gdb parameter's value" },
8791  
8792 diff --git a/gdb/python/python.h b/gdb/python/python.h
8793 index dd7066f..f0f6e90 100644
8794 --- a/gdb/python/python.h
8795 +++ b/gdb/python/python.h
8796 @@ -30,6 +30,8 @@ void eval_python_from_control_command (struct command_line *);
8797  
8798  void source_python_script (FILE *file, const char *filename);
8799  
8800 +void run_python_script (int argc, char **argv);
8801 +
8802  int apply_val_pretty_printer (struct type *type, const gdb_byte *valaddr,
8803                               int embedded_offset, CORE_ADDR address,
8804                               struct ui_file *stream, int recurse,
8805 diff --git a/gdb/record.c b/gdb/record.c
8806 index bb0fe52..ec42aac 100644
8807 --- a/gdb/record.c
8808 +++ b/gdb/record.c
8809 @@ -32,6 +32,7 @@
8810  #include "gcore.h"
8811  #include "event-loop.h"
8812  #include "inf-loop.h"
8813 +#include "gdb_bfd.h"
8814  
8815  #include <signal.h>
8816  
8817 @@ -2638,7 +2639,7 @@ record_save_cleanups (void *data)
8818    bfd *obfd = data;
8819    char *pathname = xstrdup (bfd_get_filename (obfd));
8820  
8821 -  bfd_close (obfd);
8822 +  gdb_bfd_unref (obfd);
8823    unlink (pathname);
8824    xfree (pathname);
8825  }
8826 @@ -2854,7 +2855,7 @@ cmd_record_save (char *args, int from_tty)
8827      }
8828  
8829    do_cleanups (set_cleanups);
8830 -  bfd_close (obfd);
8831 +  gdb_bfd_unref (obfd);
8832    discard_cleanups (old_cleanups);
8833  
8834    /* Succeeded.  */
8835 diff --git a/gdb/remote-m32r-sdi.c b/gdb/remote-m32r-sdi.c
8836 index 47f4405..85268b6 100644
8837 --- a/gdb/remote-m32r-sdi.c
8838 +++ b/gdb/remote-m32r-sdi.c
8839 @@ -39,6 +39,7 @@
8840  #include <sys/time.h>
8841  #include <signal.h>
8842  #include <time.h>
8843 +#include "gdb_bfd.h"
8844  
8845  
8846  #include "serial.h"
8847 @@ -1257,13 +1258,13 @@ m32r_load (char *args, int from_tty)
8848    if (!filename)
8849      filename = get_exec_file (1);
8850  
8851 -  pbfd = bfd_openr (filename, gnutarget);
8852 +  pbfd = gdb_bfd_openr (filename, gnutarget);
8853    if (pbfd == NULL)
8854      {
8855        perror_with_name (filename);
8856        return;
8857      }
8858 -  old_chain = make_cleanup_bfd_close (pbfd);
8859 +  old_chain = make_cleanup_bfd_unref (pbfd);
8860  
8861    if (!bfd_check_format (pbfd, bfd_object))
8862      error (_("\"%s\" is not an object file: %s"), filename,
8863 diff --git a/gdb/remote-mips.c b/gdb/remote-mips.c
8864 index babbf19..db4381b 100644
8865 --- a/gdb/remote-mips.c
8866 +++ b/gdb/remote-mips.c
8867 @@ -36,6 +36,7 @@
8868  #include <ctype.h>
8869  #include "mips-tdep.h"
8870  #include "gdbthread.h"
8871 +#include "gdb_bfd.h"
8872  \f
8873  
8874  /* Breakpoint types.  Values 0, 1, and 2 must agree with the watch
8875 @@ -2783,20 +2784,23 @@ mips_load_srec (char *args)
8876    unsigned int i;
8877    unsigned int srec_frame = 200;
8878    int reclen;
8879 +  struct cleanup *cleanup;
8880    static int hashmark = 1;
8881  
8882    buffer = alloca (srec_frame * 2 + 256);
8883  
8884 -  abfd = bfd_openr (args, 0);
8885 +  abfd = gdb_bfd_openr (args, 0);
8886    if (!abfd)
8887      {
8888        printf_filtered ("Unable to open file %s\n", args);
8889        return;
8890      }
8891  
8892 +  cleanup = make_cleanup_bfd_unref (abfd);
8893    if (bfd_check_format (abfd, bfd_object) == 0)
8894      {
8895        printf_filtered ("File is not an object file\n");
8896 +      do_cleanups (cleanup);
8897        return;
8898      }
8899  
8900 @@ -2850,6 +2854,7 @@ mips_load_srec (char *args)
8901    send_srec (srec, reclen, abfd->start_address);
8902  
8903    serial_flush_input (mips_desc);
8904 +  do_cleanups (cleanup);
8905  }
8906  
8907  /*
8908 @@ -3366,20 +3371,23 @@ pmon_load_fast (char *file)
8909    int bintotal = 0;
8910    int final = 0;
8911    int finished = 0;
8912 +  struct cleanup *cleanup;
8913  
8914    buffer = (char *) xmalloc (MAXRECSIZE + 1);
8915    binbuf = (unsigned char *) xmalloc (BINCHUNK);
8916  
8917 -  abfd = bfd_openr (file, 0);
8918 +  abfd = gdb_bfd_openr (file, 0);
8919    if (!abfd)
8920      {
8921        printf_filtered ("Unable to open file %s\n", file);
8922        return;
8923      }
8924 +  cleanup = make_cleanup_bfd_unref (abfd);
8925  
8926    if (bfd_check_format (abfd, bfd_object) == 0)
8927      {
8928        printf_filtered ("File is not an object file\n");
8929 +      do_cleanups (cleanup);
8930        return;
8931      }
8932  
8933 @@ -3503,6 +3511,7 @@ pmon_load_fast (char *file)
8934        pmon_end_download (final, bintotal);
8935      }
8936  
8937 +  do_cleanups (cleanup);
8938    return;
8939  }
8940  
8941 diff --git a/gdb/remote.c b/gdb/remote.c
8942 index 1c9367d..f2b5e7b 100644
8943 --- a/gdb/remote.c
8944 +++ b/gdb/remote.c
8945 @@ -42,6 +42,7 @@
8946  #include "cli/cli-decode.h"
8947  #include "cli/cli-setshow.h"
8948  #include "target-descriptions.h"
8949 +#include "gdb_bfd.h"
8950  
8951  #include <ctype.h>
8952  #include <sys/time.h>
8953 @@ -9823,11 +9824,13 @@ remote_filename_p (const char *filename)
8954  bfd *
8955  remote_bfd_open (const char *remote_file, const char *target)
8956  {
8957 -  return bfd_openr_iovec (remote_file, target,
8958 -                         remote_bfd_iovec_open, NULL,
8959 -                         remote_bfd_iovec_pread,
8960 -                         remote_bfd_iovec_close,
8961 -                         remote_bfd_iovec_stat);
8962 +  bfd *abfd = gdb_bfd_openr_iovec (remote_file, target,
8963 +                                  remote_bfd_iovec_open, NULL,
8964 +                                  remote_bfd_iovec_pread,
8965 +                                  remote_bfd_iovec_close,
8966 +                                  remote_bfd_iovec_stat);
8967 +
8968 +  return abfd;
8969  }
8970  
8971  void
8972 diff --git a/gdb/rs6000-nat.c b/gdb/rs6000-nat.c
8973 index 0a2000a..9b8efd3 100644
8974 --- a/gdb/rs6000-nat.c
8975 +++ b/gdb/rs6000-nat.c
8976 @@ -730,7 +730,7 @@ static struct vmap *
8977  add_vmap (LdInfo *ldi)
8978  {
8979    bfd *abfd, *last;
8980 -  char *mem, *objname, *filename;
8981 +  char *mem, *filename;
8982    struct objfile *obj;
8983    struct vmap *vp;
8984    int fd;
8985 @@ -743,19 +743,18 @@ add_vmap (LdInfo *ldi)
8986    filename = LDI_FILENAME (ldi, arch64);
8987    mem = filename + strlen (filename) + 1;
8988    mem = xstrdup (mem);
8989 -  objname = xstrdup (filename);
8990  
8991    fd = LDI_FD (ldi, arch64);
8992    if (fd < 0)
8993      /* Note that this opens it once for every member; a possible
8994         enhancement would be to only open it once for every object.  */
8995 -    abfd = bfd_openr (objname, gnutarget);
8996 +    abfd = gdb_bfd_openr (filename, gnutarget);
8997    else
8998 -    abfd = bfd_fdopenr (objname, gnutarget, fd);
8999 +    abfd = gdb_bfd_fdopenr (filename, gnutarget, fd);
9000    if (!abfd)
9001      {
9002        warning (_("Could not open `%s' as an executable file: %s"),
9003 -              objname, bfd_errmsg (bfd_get_error ()));
9004 +              filename, bfd_errmsg (bfd_get_error ()));
9005        return NULL;
9006      }
9007  
9008 @@ -766,35 +765,44 @@ add_vmap (LdInfo *ldi)
9009  
9010    else if (bfd_check_format (abfd, bfd_archive))
9011      {
9012 -      last = 0;
9013 -      /* FIXME??? am I tossing BFDs?  bfd?  */
9014 -      while ((last = bfd_openr_next_archived_file (abfd, last)))
9015 -       if (strcmp (mem, last->filename) == 0)
9016 -         break;
9017 +      last = gdb_bfd_openr_next_archived_file (abfd, NULL);
9018 +      while (last != NULL)
9019 +       {
9020 +         bfd *next;
9021 +
9022 +         if (strcmp (mem, last->filename) == 0)
9023 +           break;
9024 +
9025 +         next = gdb_bfd_openr_next_archived_file (abfd, last);
9026 +         gdb_bfd_unref (last);
9027 +       }
9028  
9029        if (!last)
9030         {
9031 -         warning (_("\"%s\": member \"%s\" missing."), objname, mem);
9032 -         bfd_close (abfd);
9033 +         warning (_("\"%s\": member \"%s\" missing."), filename, mem);
9034 +         gdb_bfd_unref (abfd);
9035           return NULL;
9036         }
9037  
9038        if (!bfd_check_format (last, bfd_object))
9039         {
9040           warning (_("\"%s\": member \"%s\" not in executable format: %s."),
9041 -                  objname, mem, bfd_errmsg (bfd_get_error ()));
9042 -         bfd_close (last);
9043 -         bfd_close (abfd);
9044 +                  filename, mem, bfd_errmsg (bfd_get_error ()));
9045 +         gdb_bfd_unref (last);
9046 +         gdb_bfd_unref (abfd);
9047           return NULL;
9048         }
9049  
9050        vp = map_vmap (last, abfd);
9051 +      /* map_vmap acquired a reference to LAST, so we can release
9052 +        ours.  */
9053 +      gdb_bfd_unref (last);
9054      }
9055    else
9056      {
9057        warning (_("\"%s\": not in executable format: %s."),
9058 -              objname, bfd_errmsg (bfd_get_error ()));
9059 -      bfd_close (abfd);
9060 +              filename, bfd_errmsg (bfd_get_error ()));
9061 +      gdb_bfd_unref (abfd);
9062        return NULL;
9063      }
9064    obj = allocate_objfile (vp->bfd, 0);
9065 @@ -803,6 +811,11 @@ add_vmap (LdInfo *ldi)
9066    /* Always add symbols for the main objfile.  */
9067    if (vp == vmap || auto_solib_add)
9068      vmap_add_symbols (vp);
9069 +
9070 +  /* Anything needing a reference to ABFD has already acquired it, so
9071 +     release our local reference.  */
9072 +  gdb_bfd_unref (abfd);
9073 +
9074    return vp;
9075  }
9076  \f
9077 diff --git a/gdb/solib-darwin.c b/gdb/solib-darwin.c
9078 index d52fb46..db8f187 100644
9079 --- a/gdb/solib-darwin.c
9080 +++ b/gdb/solib-darwin.c
9081 @@ -28,6 +28,7 @@
9082  #include "inferior.h"
9083  #include "regcache.h"
9084  #include "gdbthread.h"
9085 +#include "gdb_bfd.h"
9086  
9087  #include "gdb_assert.h"
9088  
9089 @@ -356,6 +357,7 @@ darwin_solib_get_all_image_info_addr_at_init (struct darwin_info *info)
9090    gdb_byte *interp_name;
9091    CORE_ADDR load_addr = 0;
9092    bfd *dyld_bfd = NULL;
9093 +  struct cleanup *cleanup;
9094  
9095    /* This method doesn't work with an attached process.  */
9096    if (current_inferior ()->attach_flag)
9097 @@ -366,24 +368,31 @@ darwin_solib_get_all_image_info_addr_at_init (struct darwin_info *info)
9098    if (!interp_name)
9099      return;
9100  
9101 +  cleanup = make_cleanup (null_cleanup, NULL);
9102 +
9103    /* Create a bfd for the interpreter.  */
9104 -  dyld_bfd = bfd_openr (interp_name, gnutarget);
9105 +  dyld_bfd = gdb_bfd_openr (interp_name, gnutarget);
9106    if (dyld_bfd)
9107      {
9108        bfd *sub;
9109  
9110 +      make_cleanup_bfd_unref (dyld_bfd);
9111        sub = bfd_mach_o_fat_extract (dyld_bfd, bfd_object,
9112                                     gdbarch_bfd_arch_info (target_gdbarch));
9113        if (sub)
9114 -       dyld_bfd = sub;
9115 -      else
9116         {
9117 -         bfd_close (dyld_bfd);
9118 -         dyld_bfd = NULL;
9119 +         dyld_bfd = sub;
9120 +         gdb_bfd_ref (sub);
9121 +         make_cleanup_bfd_unref (sub);
9122         }
9123 +      else
9124 +       dyld_bfd = NULL;
9125      }
9126    if (!dyld_bfd)
9127 -    return;
9128 +    {
9129 +      do_cleanups (cleanup);
9130 +      return;
9131 +    }
9132  
9133    /* We find the dynamic linker's base address by examining
9134       the current pc (which should point at the entry point for the
9135 @@ -395,7 +404,7 @@ darwin_solib_get_all_image_info_addr_at_init (struct darwin_info *info)
9136    info->all_image_addr =
9137      lookup_symbol_from_bfd (dyld_bfd, "_dyld_all_image_infos");
9138  
9139 -  bfd_close (dyld_bfd);
9140 +  do_cleanups (cleanup);
9141  
9142    if (info->all_image_addr == 0)
9143      return;
9144 @@ -509,17 +518,10 @@ darwin_bfd_open (char *pathname)
9145                                 gdbarch_bfd_arch_info (target_gdbarch));
9146    if (!res)
9147      {
9148 -      bfd_close (abfd);
9149 -      make_cleanup (xfree, found_pathname);
9150 +      make_cleanup_bfd_unref (abfd);
9151        error (_("`%s': not a shared-library: %s"),
9152 -            found_pathname, bfd_errmsg (bfd_get_error ()));
9153 +            bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
9154      }
9155 -
9156 -  /* Make sure that the filename is malloc'ed.  The current filename
9157 -     for fat-binaries BFDs is a name that was generated by BFD, usually
9158 -     a static string containing the name of the architecture.  */
9159 -  res->filename = xstrdup (pathname);
9160 -
9161    return res;
9162  }
9163  
9164 diff --git a/gdb/solib-dsbt.c b/gdb/solib-dsbt.c
9165 index 2500c1f..fcc01a8 100644
9166 --- a/gdb/solib-dsbt.c
9167 +++ b/gdb/solib-dsbt.c
9168 @@ -30,6 +30,7 @@
9169  #include "gdbcmd.h"
9170  #include "elf-bfd.h"
9171  #include "exceptions.h"
9172 +#include "gdb_bfd.h"
9173  
9174  #define GOT_MODULE_OFFSET 4
9175  
9176 @@ -899,7 +900,7 @@ enable_break2 (void)
9177         {
9178           warning (_("Could not find symbol _dl_debug_addr in dynamic linker"));
9179           enable_break_failure_warning ();
9180 -         bfd_close (tmp_bfd);
9181 +         gdb_bfd_unref (tmp_bfd);
9182           return 0;
9183         }
9184  
9185 @@ -948,13 +949,13 @@ enable_break2 (void)
9186                      "(at address %s) from dynamic linker"),
9187                    hex_string_custom (addr + 8, 8));
9188           enable_break_failure_warning ();
9189 -         bfd_close (tmp_bfd);
9190 +         gdb_bfd_unref (tmp_bfd);
9191           return 0;
9192         }
9193        addr = extract_unsigned_integer (addr_buf, sizeof addr_buf, byte_order);
9194  
9195        /* We're done with the temporary bfd.  */
9196 -      bfd_close (tmp_bfd);
9197 +      gdb_bfd_unref (tmp_bfd);
9198  
9199        /* We're also done with the loadmap.  */
9200        xfree (ldm);
9201 diff --git a/gdb/solib-frv.c b/gdb/solib-frv.c
9202 index 2f2c8b0..0f59535 100644
9203 --- a/gdb/solib-frv.c
9204 +++ b/gdb/solib-frv.c
9205 @@ -31,6 +31,7 @@
9206  #include "gdbcmd.h"
9207  #include "elf/frv.h"
9208  #include "exceptions.h"
9209 +#include "gdb_bfd.h"
9210  
9211  /* Flag which indicates whether internal debug messages should be printed.  */
9212  static int solib_frv_debug;
9213 @@ -574,7 +575,7 @@ enable_break2 (void)
9214         {
9215           warning (_("Unable to determine dynamic linker loadmap address."));
9216           enable_break_failure_warning ();
9217 -         bfd_close (tmp_bfd);
9218 +         gdb_bfd_unref (tmp_bfd);
9219           return 0;
9220         }
9221  
9222 @@ -589,7 +590,7 @@ enable_break2 (void)
9223           warning (_("Unable to load dynamic linker loadmap at address %s."),
9224                    hex_string_custom (interp_loadmap_addr, 8));
9225           enable_break_failure_warning ();
9226 -         bfd_close (tmp_bfd);
9227 +         gdb_bfd_unref (tmp_bfd);
9228           return 0;
9229         }
9230  
9231 @@ -623,7 +624,7 @@ enable_break2 (void)
9232           warning (_("Could not find symbol _dl_debug_addr "
9233                      "in dynamic linker"));
9234           enable_break_failure_warning ();
9235 -         bfd_close (tmp_bfd);
9236 +         gdb_bfd_unref (tmp_bfd);
9237           return 0;
9238         }
9239  
9240 @@ -674,7 +675,7 @@ enable_break2 (void)
9241                      "(at address %s) from dynamic linker"),
9242                    hex_string_custom (addr + 8, 8));
9243           enable_break_failure_warning ();
9244 -         bfd_close (tmp_bfd);
9245 +         gdb_bfd_unref (tmp_bfd);
9246           return 0;
9247         }
9248        addr = extract_unsigned_integer (addr_buf, sizeof addr_buf, byte_order);
9249 @@ -686,13 +687,13 @@ enable_break2 (void)
9250                      "(at address %s) from dynamic linker"),
9251                    hex_string_custom (addr, 8));
9252           enable_break_failure_warning ();
9253 -         bfd_close (tmp_bfd);
9254 +         gdb_bfd_unref (tmp_bfd);
9255           return 0;
9256         }
9257        addr = extract_unsigned_integer (addr_buf, sizeof addr_buf, byte_order);
9258  
9259        /* We're done with the temporary bfd.  */
9260 -      bfd_close (tmp_bfd);
9261 +      gdb_bfd_unref (tmp_bfd);
9262  
9263        /* We're also done with the loadmap.  */
9264        xfree (ldm);
9265 diff --git a/gdb/solib-pa64.c b/gdb/solib-pa64.c
9266 index 2b8d061..00ed8a5 100644
9267 --- a/gdb/solib-pa64.c
9268 +++ b/gdb/solib-pa64.c
9269 @@ -362,7 +362,7 @@ manpage for methods to privately map shared library text."));
9270          to find any magic formula to find it for Solaris (appears to
9271          be trivial on GNU/Linux).  Therefore, we have to try an alternate
9272          mechanism to find the dynamic linker's base address.  */
9273 -      tmp_bfd = bfd_openr (buf, gnutarget);
9274 +      tmp_bfd = gdb_bfd_openr (buf, gnutarget);
9275        if (tmp_bfd == NULL)
9276         return;
9277  
9278 @@ -371,7 +371,7 @@ manpage for methods to privately map shared library text."));
9279         {
9280           warning (_("Unable to grok dynamic linker %s as an object file"),
9281                    buf);
9282 -         bfd_close (tmp_bfd);
9283 +         gdb_bfd_unref (tmp_bfd);
9284           return;
9285         }
9286  
9287 @@ -401,7 +401,7 @@ manpage for methods to privately map shared library text."));
9288        }
9289  
9290        /* We're done with the temporary bfd.  */
9291 -      bfd_close (tmp_bfd);
9292 +      gdb_bfd_unref (tmp_bfd);
9293      }
9294  }
9295  
9296 diff --git a/gdb/solib-spu.c b/gdb/solib-spu.c
9297 index b5454e7..f62d96c 100644
9298 --- a/gdb/solib-spu.c
9299 +++ b/gdb/solib-spu.c
9300 @@ -36,6 +36,7 @@
9301  #include "breakpoint.h"
9302  #include "gdbthread.h"
9303  #include "exceptions.h"
9304 +#include "gdb_bfd.h"
9305  
9306  #include "spu-tdep.h"
9307  
9308 @@ -325,16 +326,16 @@ spu_bfd_fopen (char *name, CORE_ADDR addr)
9309    CORE_ADDR *open_closure = xmalloc (sizeof (CORE_ADDR));
9310    *open_closure = addr;
9311  
9312 -  nbfd = bfd_openr_iovec (xstrdup (name), "elf32-spu",
9313 -                          spu_bfd_iovec_open, open_closure,
9314 -                          spu_bfd_iovec_pread, spu_bfd_iovec_close,
9315 -                         spu_bfd_iovec_stat);
9316 +  nbfd = gdb_bfd_openr_iovec (name, "elf32-spu",
9317 +                             spu_bfd_iovec_open, open_closure,
9318 +                             spu_bfd_iovec_pread, spu_bfd_iovec_close,
9319 +                             spu_bfd_iovec_stat);
9320    if (!nbfd)
9321      return NULL;
9322  
9323    if (!bfd_check_format (nbfd, bfd_object))
9324      {
9325 -      bfd_close (nbfd);
9326 +      gdb_bfd_unref (nbfd);
9327        return NULL;
9328      }
9329  
9330 diff --git a/gdb/solib-svr4.c b/gdb/solib-svr4.c
9331 index 307e483..76bd872 100644
9332 --- a/gdb/solib-svr4.c
9333 +++ b/gdb/solib-svr4.c
9334 @@ -46,6 +46,7 @@
9335  #include "exec.h"
9336  #include "auxv.h"
9337  #include "exceptions.h"
9338 +#include "gdb_bfd.h"
9339  
9340  static struct link_map_offsets *svr4_fetch_link_map_offsets (void);
9341  static int svr4_have_link_map_offsets (void);
9342 @@ -1558,9 +1559,11 @@ enable_break (struct svr4_info *info, int from_tty)
9343         goto bkpt_at_symbol;
9344  
9345        /* Now convert the TMP_BFD into a target.  That way target, as
9346 -         well as BFD operations can be used.  Note that closing the
9347 -         target will also close the underlying bfd.  */
9348 +         well as BFD operations can be used.  */
9349        tmp_bfd_target = target_bfd_reopen (tmp_bfd);
9350 +      /* target_bfd_reopen acquired its own reference, so we can
9351 +         release ours now.  */
9352 +      gdb_bfd_unref (tmp_bfd);
9353  
9354        /* On a running target, we can get the dynamic linker's base
9355           address from the shared library table.  */
9356 @@ -1670,8 +1673,9 @@ enable_break (struct svr4_info *info, int from_tty)
9357                                                        sym_addr,
9358                                                        tmp_bfd_target);
9359  
9360 -      /* We're done with both the temporary bfd and target.  Remember,
9361 -         closing the target closes the underlying bfd.  */
9362 +      /* We're done with both the temporary bfd and target.  Closing
9363 +         the target closes the underlying bfd, because it holds the
9364 +         only remaining reference.  */
9365        target_close (tmp_bfd_target, 0);
9366  
9367        if (sym_addr != 0)
9368 diff --git a/gdb/solib.c b/gdb/solib.c
9369 index 90439ba..73773f1 100644
9370 --- a/gdb/solib.c
9371 +++ b/gdb/solib.c
9372 @@ -46,6 +46,7 @@
9373  #include "solib.h"
9374  #include "interps.h"
9375  #include "filesystem.h"
9376 +#include "gdb_bfd.h"
9377  
9378  /* Architecture-specific operations.  */
9379  
9380 @@ -360,9 +361,9 @@ solib_find (char *in_pathname, int *fd)
9381     it is used as file handle to open the file.  Throws an error if the file
9382     could not be opened.  Handles both local and remote file access.
9383  
9384 -   PATHNAME must be malloc'ed by the caller.  If successful, the new BFD's
9385 -   name will point to it.  If unsuccessful, PATHNAME will be freed and the
9386 -   FD will be closed (unless FD was -1).  */
9387 +   PATHNAME must be malloc'ed by the caller.  It will be freed by this
9388 +   function.  If unsuccessful, the FD will be closed (unless FD was
9389 +   -1).  */
9390  
9391  bfd *
9392  solib_bfd_fopen (char *pathname, int fd)
9393 @@ -376,7 +377,7 @@ solib_bfd_fopen (char *pathname, int fd)
9394      }
9395    else
9396      {
9397 -      abfd = bfd_fopen (pathname, gnutarget, FOPEN_RB, fd);
9398 +      abfd = gdb_bfd_fopen (pathname, gnutarget, FOPEN_RB, fd);
9399  
9400        if (abfd)
9401         bfd_set_cacheable (abfd, 1);
9402 @@ -389,6 +390,8 @@ solib_bfd_fopen (char *pathname, int fd)
9403              pathname, bfd_errmsg (bfd_get_error ()));
9404      }
9405  
9406 +  xfree (pathname);
9407 +
9408    return abfd;
9409  }
9410  
9411 @@ -420,17 +423,16 @@ solib_bfd_open (char *pathname)
9412    /* Check bfd format.  */
9413    if (!bfd_check_format (abfd, bfd_object))
9414      {
9415 -      bfd_close (abfd);
9416 -      make_cleanup (xfree, found_pathname);
9417 +      make_cleanup_bfd_unref (abfd);
9418        error (_("`%s': not in executable format: %s"),
9419 -            found_pathname, bfd_errmsg (bfd_get_error ()));
9420 +            bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
9421      }
9422  
9423    /* Check bfd arch.  */
9424    b = gdbarch_bfd_arch_info (target_gdbarch);
9425    if (!b->compatible (b, bfd_get_arch_info (abfd)))
9426      warning (_("`%s': Shared library architecture %s is not compatible "
9427 -               "with target architecture %s."), found_pathname,
9428 +               "with target architecture %s."), bfd_get_filename (abfd),
9429               bfd_get_arch_info (abfd)->printable_name, b->printable_name);
9430  
9431    return abfd;
9432 @@ -466,7 +468,7 @@ solib_map_sections (struct so_list *so)
9433      return 0;
9434  
9435    /* Leave bfd open, core_xfer_memory and "info files" need it.  */
9436 -  so->abfd = gdb_bfd_ref (abfd);
9437 +  so->abfd = abfd;
9438  
9439    /* copy full path name into so_name, so that later symbol_file_add
9440       can find it.  */
9441 @@ -1233,7 +1235,7 @@ reload_shared_libraries_1 (int from_tty)
9442         {
9443           found_pathname = xstrdup (bfd_get_filename (abfd));
9444           make_cleanup (xfree, found_pathname);
9445 -         gdb_bfd_close_or_warn (abfd);
9446 +         gdb_bfd_unref (abfd);
9447         }
9448  
9449        /* If this shared library is no longer associated with its previous
9450 diff --git a/gdb/spu-linux-nat.c b/gdb/spu-linux-nat.c
9451 index 2dfec8c..999f1ab 100644
9452 --- a/gdb/spu-linux-nat.c
9453 +++ b/gdb/spu-linux-nat.c
9454 @@ -315,16 +315,16 @@ spu_bfd_open (ULONGEST addr)
9455    ULONGEST *open_closure = xmalloc (sizeof (ULONGEST));
9456    *open_closure = addr;
9457  
9458 -  nbfd = bfd_openr_iovec (xstrdup ("<in-memory>"), "elf32-spu",
9459 -                         spu_bfd_iovec_open, open_closure,
9460 -                         spu_bfd_iovec_pread, spu_bfd_iovec_close,
9461 -                         spu_bfd_iovec_stat);
9462 +  nbfd = gdb_bfd_openr_iovec ("<in-memory>", "elf32-spu",
9463 +                             spu_bfd_iovec_open, open_closure,
9464 +                             spu_bfd_iovec_pread, spu_bfd_iovec_close,
9465 +                             spu_bfd_iovec_stat);
9466    if (!nbfd)
9467      return NULL;
9468  
9469    if (!bfd_check_format (nbfd, bfd_object))
9470      {
9471 -      bfd_close (nbfd);
9472 +      gdb_bfd_unref (nbfd);
9473        return NULL;
9474      }
9475  
9476 @@ -374,8 +374,13 @@ spu_symbol_file_add_from_memory (int inferior_fd)
9477    /* Open BFD representing SPE executable and read its symbols.  */
9478    nbfd = spu_bfd_open (addr);
9479    if (nbfd)
9480 -    symbol_file_add_from_bfd (nbfd, SYMFILE_VERBOSE | SYMFILE_MAINLINE,
9481 -                              NULL, 0, NULL);
9482 +    {
9483 +      struct cleanup *cleanup = make_cleanup_bfd_unref (nbfd);
9484 +
9485 +      symbol_file_add_from_bfd (nbfd, SYMFILE_VERBOSE | SYMFILE_MAINLINE,
9486 +                               NULL, 0, NULL);
9487 +      do_cleanups (cleanup);
9488 +    }
9489  }
9490  
9491  
9492 diff --git a/gdb/stack.c b/gdb/stack.c
9493 index 35d379d..04aab5e 100644
9494 --- a/gdb/stack.c
9495 +++ b/gdb/stack.c
9496 @@ -509,6 +509,10 @@ print_frame_args (struct symbol *func, struct frame_info *frame,
9497    stb = mem_fileopen ();
9498    old_chain = make_cleanup_ui_file_delete (stb);
9499  
9500 +  /* Frame may be needed for check_typedef of TYPE_DYNAMIC.  */
9501 +  make_cleanup_restore_selected_frame ();
9502 +  select_frame (frame);
9503 +
9504    if (func)
9505      {
9506        struct block *b = SYMBOL_BLOCK_VALUE (func);
9507 diff --git a/gdb/symfile-mem.c b/gdb/symfile-mem.c
9508 index e80fd25..2e53be0 100644
9509 --- a/gdb/symfile-mem.c
9510 +++ b/gdb/symfile-mem.c
9511 @@ -54,6 +54,7 @@
9512  #include "observer.h"
9513  #include "auxv.h"
9514  #include "elf/common.h"
9515 +#include "gdb_bfd.h"
9516  
9517  /* Verify parameters of target_read_memory_bfd and target_read_memory are
9518     compatible.  */
9519 @@ -100,23 +101,24 @@ symbol_file_add_from_memory (struct bfd *templ, CORE_ADDR addr, char *name,
9520    if (nbfd == NULL)
9521      error (_("Failed to read a valid object file image from memory."));
9522  
9523 +  gdb_bfd_ref (nbfd);
9524    if (name == NULL)
9525 -    nbfd->filename = xstrdup ("shared object read from target memory");
9526 +    nbfd->filename = "shared object read from target memory";
9527    else
9528 -    nbfd->filename = name;
9529 -
9530 -  if (!bfd_check_format (nbfd, bfd_object))
9531      {
9532 -      /* FIXME: should be checking for errors from bfd_close (for one thing,
9533 -         on error it does not free all the storage associated with the
9534 -         bfd).  */
9535 -      bfd_close (nbfd);
9536 -      error (_("Got object file from memory but can't read symbols: %s."),
9537 -            bfd_errmsg (bfd_get_error ()));
9538 +      nbfd->filename = name;
9539 +      gdb_bfd_stash_filename (nbfd);
9540 +      xfree (name);
9541      }
9542  
9543 +  cleanup = make_cleanup_bfd_unref (nbfd);
9544 +
9545 +  if (!bfd_check_format (nbfd, bfd_object))
9546 +    error (_("Got object file from memory but can't read symbols: %s."),
9547 +          bfd_errmsg (bfd_get_error ()));
9548 +
9549    sai = alloc_section_addr_info (bfd_count_sections (nbfd));
9550 -  cleanup = make_cleanup (xfree, sai);
9551 +  make_cleanup (xfree, sai);
9552    i = 0;
9553    for (sec = nbfd->sections; sec != NULL; sec = sec->next)
9554      if ((bfd_get_section_flags (nbfd, sec) & (SEC_ALLOC|SEC_LOAD)) != 0)
9555 diff --git a/gdb/symfile.c b/gdb/symfile.c
9556 index 01252e2..95ed480 100644
9557 --- a/gdb/symfile.c
9558 +++ b/gdb/symfile.c
9559 @@ -55,6 +55,7 @@
9560  #include "solib.h"
9561  #include "remote.h"
9562  #include "stack.h"
9563 +#include "gdb_bfd.h"
9564  
9565  #include <sys/types.h>
9566  #include <fcntl.h>
9567 @@ -1036,7 +1037,7 @@ new_symfile_objfile (struct objfile *objfile, int add_flags)
9568     loaded file.
9569  
9570     ABFD is a BFD already open on the file, as from symfile_bfd_open.
9571 -   This BFD will be closed on error, and is always consumed by this function.
9572 +   A new reference is acquired by this function.
9573  
9574     ADD_FLAGS encodes verbosity, whether this is main symbol file or
9575     extra, such as dynamically loaded code, and what to do with breakpoins.
9576 @@ -1060,7 +1061,6 @@ symbol_file_add_with_addrs_or_offsets (bfd *abfd,
9577                                         int flags, struct objfile *parent)
9578  {
9579    struct objfile *objfile;
9580 -  struct cleanup *my_cleanups;
9581    const char *name = bfd_get_filename (abfd);
9582    const int from_tty = add_flags & SYMFILE_VERBOSE;
9583    const int mainline = add_flags & SYMFILE_MAINLINE;
9584 @@ -1074,8 +1074,6 @@ symbol_file_add_with_addrs_or_offsets (bfd *abfd,
9585        add_flags &= ~SYMFILE_NO_READ;
9586      }
9587  
9588 -  my_cleanups = make_cleanup_bfd_close (abfd);
9589 -
9590    /* Give user a chance to burp if we'd be
9591       interactively wiping out any existing symbols.  */
9592  
9593 @@ -1086,7 +1084,6 @@ symbol_file_add_with_addrs_or_offsets (bfd *abfd,
9594      error (_("Not confirmed."));
9595  
9596    objfile = allocate_objfile (abfd, flags | (mainline ? OBJF_MAINLINE : 0));
9597 -  discard_cleanups (my_cleanups);
9598  
9599    if (parent)
9600      add_separate_debug_objfile (objfile, parent);
9601 @@ -1207,8 +1204,13 @@ struct objfile *
9602  symbol_file_add (char *name, int add_flags, struct section_addr_info *addrs,
9603                  int flags)
9604  {
9605 -  return symbol_file_add_from_bfd (symfile_bfd_open (name), add_flags, addrs,
9606 -                                   flags, NULL);
9607 +  bfd *bfd = symfile_bfd_open (name);
9608 +  struct cleanup *cleanup = make_cleanup_bfd_unref (bfd);
9609 +  struct objfile *objf;
9610 +
9611 +  objf = symbol_file_add_from_bfd (bfd, add_flags, addrs, flags, NULL);
9612 +  do_cleanups (cleanup);
9613 +  return objf;
9614  }
9615  
9616  
9617 @@ -1350,7 +1352,7 @@ separate_debug_file_exists (const char *name, unsigned long crc,
9618    if (filename_cmp (name, parent_objfile->name) == 0)
9619      return 0;
9620  
9621 -  abfd = bfd_open_maybe_remote (name);
9622 +  abfd = gdb_bfd_open_maybe_remote (name);
9623  
9624    if (!abfd)
9625      return 0;
9626 @@ -1372,7 +1374,7 @@ separate_debug_file_exists (const char *name, unsigned long crc,
9627        if (abfd_stat.st_dev == parent_stat.st_dev
9628           && abfd_stat.st_ino == parent_stat.st_ino)
9629         {
9630 -         bfd_close (abfd);
9631 +         gdb_bfd_unref (abfd);
9632           return 0;
9633         }
9634        verified_as_different = 1;
9635 @@ -1382,7 +1384,7 @@ separate_debug_file_exists (const char *name, unsigned long crc,
9636  
9637    file_crc_p = get_file_crc (abfd, &file_crc);
9638  
9639 -  bfd_close (abfd);
9640 +  gdb_bfd_unref (abfd);
9641  
9642    if (!file_crc_p)
9643      return 0;
9644 @@ -1690,15 +1692,20 @@ set_initial_language (void)
9645  }
9646  
9647  /* If NAME is a remote name open the file using remote protocol, otherwise
9648 -   open it normally.  */
9649 +   open it normally.  Returns a new reference to the BFD.  On error,
9650 +   returns NULL with the BFD error set.  */
9651  
9652  bfd *
9653 -bfd_open_maybe_remote (const char *name)
9654 +gdb_bfd_open_maybe_remote (const char *name)
9655  {
9656 +  bfd *result;
9657 +
9658    if (remote_filename_p (name))
9659 -    return remote_bfd_open (name, gnutarget);
9660 +    result = remote_bfd_open (name, gnutarget);
9661    else
9662 -    return bfd_openr (name, gnutarget);
9663 +    result = gdb_bfd_openr (name, gnutarget);
9664 +
9665 +  return result;
9666  }
9667  
9668  
9669 @@ -1716,19 +1723,14 @@ symfile_bfd_open (char *name)
9670  
9671    if (remote_filename_p (name))
9672      {
9673 -      name = xstrdup (name);
9674        sym_bfd = remote_bfd_open (name, gnutarget);
9675        if (!sym_bfd)
9676 -       {
9677 -         make_cleanup (xfree, name);
9678 -         error (_("`%s': can't open to read symbols: %s."), name,
9679 -                bfd_errmsg (bfd_get_error ()));
9680 -       }
9681 +       error (_("`%s': can't open to read symbols: %s."), name,
9682 +              bfd_errmsg (bfd_get_error ()));
9683  
9684        if (!bfd_check_format (sym_bfd, bfd_object))
9685         {
9686 -         bfd_close (sym_bfd);
9687 -         make_cleanup (xfree, name);
9688 +         make_cleanup_bfd_unref (sym_bfd);
9689           error (_("`%s': can't read symbols: %s."), name,
9690                  bfd_errmsg (bfd_get_error ()));
9691         }
9692 @@ -1757,12 +1759,11 @@ symfile_bfd_open (char *name)
9693        perror_with_name (name);
9694      }
9695  
9696 -  /* Free 1st new malloc'd copy, but keep the 2nd malloc'd copy in
9697 -     bfd.  It'll be freed in free_objfile().  */
9698    xfree (name);
9699    name = absolute_name;
9700 +  make_cleanup (xfree, name);
9701  
9702 -  sym_bfd = bfd_fopen (name, gnutarget, FOPEN_RB, desc);
9703 +  sym_bfd = gdb_bfd_fopen (name, gnutarget, FOPEN_RB, desc);
9704    if (!sym_bfd)
9705      {
9706        make_cleanup (xfree, name);
9707 @@ -1773,18 +1774,11 @@ symfile_bfd_open (char *name)
9708  
9709    if (!bfd_check_format (sym_bfd, bfd_object))
9710      {
9711 -      /* FIXME: should be checking for errors from bfd_close (for one
9712 -         thing, on error it does not free all the storage associated
9713 -         with the bfd).  */
9714 -      bfd_close (sym_bfd);     /* This also closes desc.  */
9715 -      make_cleanup (xfree, name);
9716 +      make_cleanup_bfd_unref (sym_bfd);
9717        error (_("`%s': can't read symbols: %s."), name,
9718              bfd_errmsg (bfd_get_error ()));
9719      }
9720  
9721 -  /* bfd_usrdata exists for applications and libbfd must not touch it.  */
9722 -  gdb_assert (bfd_usrdata (sym_bfd) == NULL);
9723 -
9724    return sym_bfd;
9725  }
9726  
9727 @@ -2109,17 +2103,14 @@ generic_load (char *args, int from_tty)
9728      }
9729  
9730    /* Open the file for loading.  */
9731 -  loadfile_bfd = bfd_openr (filename, gnutarget);
9732 +  loadfile_bfd = gdb_bfd_openr (filename, gnutarget);
9733    if (loadfile_bfd == NULL)
9734      {
9735        perror_with_name (filename);
9736        return;
9737      }
9738  
9739 -  /* FIXME: should be checking for errors from bfd_close (for one thing,
9740 -     on error it does not free all the storage associated with the
9741 -     bfd).  */
9742 -  make_cleanup_bfd_close (loadfile_bfd);
9743 +  make_cleanup_bfd_unref (loadfile_bfd);
9744  
9745    if (!bfd_check_format (loadfile_bfd, bfd_object))
9746      {
9747 @@ -2518,15 +2509,18 @@ reread_symbols (void)
9748           /* Clean up any state BFD has sitting around.  We don't need
9749              to close the descriptor but BFD lacks a way of closing the
9750              BFD without closing the descriptor.  */
9751 -         obfd_filename = bfd_get_filename (objfile->obfd);
9752 -         if (!bfd_close (objfile->obfd))
9753 -           error (_("Can't close BFD for %s: %s"), objfile->name,
9754 -                  bfd_errmsg (bfd_get_error ()));
9755 -         objfile->obfd = bfd_open_maybe_remote (obfd_filename);
9756 +         {
9757 +           struct bfd *obfd = objfile->obfd;
9758 +
9759 +           obfd_filename = bfd_get_filename (objfile->obfd);
9760 +           /* Open the new BFD before freeing the old one, so that
9761 +              the filename remains live.  */
9762 +           objfile->obfd = gdb_bfd_open_maybe_remote (obfd_filename);
9763 +           gdb_bfd_unref (obfd);
9764 +         }
9765 +
9766           if (objfile->obfd == NULL)
9767             error (_("Can't open %s to read symbols."), objfile->name);
9768 -         else
9769 -           objfile->obfd = gdb_bfd_ref (objfile->obfd);
9770           /* bfd_openr sets cacheable to true, which is what we want.  */
9771           if (!bfd_check_format (objfile->obfd, bfd_object))
9772             error (_("Can't read symbols from %s: %s."), objfile->name,
9773 diff --git a/gdb/symfile.h b/gdb/symfile.h
9774 index aca7359..184a83e 100644
9775 --- a/gdb/symfile.h
9776 +++ b/gdb/symfile.h
9777 @@ -553,7 +553,7 @@ extern void find_lowest_section (bfd *, asection *, void *);
9778  
9779  extern bfd *symfile_bfd_open (char *);
9780  
9781 -extern bfd *bfd_open_maybe_remote (const char *);
9782 +extern bfd *gdb_bfd_open_maybe_remote (const char *);
9783  
9784  extern int get_section_index (struct objfile *, char *);
9785  
9786 diff --git a/gdb/testsuite/gdb.ada/packed_array.exp b/gdb/testsuite/gdb.ada/packed_array.exp
9787 index 678639c..47a2202 100644
9788 --- a/gdb/testsuite/gdb.ada/packed_array.exp
9789 +++ b/gdb/testsuite/gdb.ada/packed_array.exp
9790 @@ -60,5 +60,11 @@ gdb_test_multiple "$test" "$test" {
9791          # are.  Observed with (FSF GNU Ada 4.5.3 20110124).
9792          xfail $test
9793      }
9794 +    -re "= \\(\\)\[\r\n\]+$gdb_prompt $" {
9795 +       # archer-jankratochvil-vla resolves it as a dynamic type resolved as an
9796 +       # empty array [0..-1].
9797 +       # DW_AT_upper_bound : (DW_OP_fbreg: -48; DW_OP_deref)
9798 +        xfail $test
9799 +    }
9800  }
9801  
9802 diff --git a/gdb/testsuite/gdb.arch/x86_64-vla-pointer-foo.S b/gdb/testsuite/gdb.arch/x86_64-vla-pointer-foo.S
9803 new file mode 100644
9804 index 0000000..83faaf6
9805 --- /dev/null
9806 +++ b/gdb/testsuite/gdb.arch/x86_64-vla-pointer-foo.S
9807 @@ -0,0 +1,457 @@
9808 +       .file   "x86_64-vla-pointer.c"
9809 +       .section        .debug_abbrev,"",@progbits
9810 +.Ldebug_abbrev0:
9811 +       .section        .debug_info,"",@progbits
9812 +.Ldebug_info0:
9813 +       .section        .debug_line,"",@progbits
9814 +.Ldebug_line0:
9815 +       .text
9816 +.Ltext0:
9817 +.globl foo
9818 +       .type   foo, @function
9819 +foo:
9820 +.LFB2:
9821 +       .file 1 "x86_64-vla-pointer.c"
9822 +       .loc 1 22 0
9823 +       pushq   %rbp
9824 +.LCFI0:
9825 +       movq    %rsp, %rbp
9826 +.LCFI1:
9827 +       subq    $64, %rsp
9828 +.LCFI2:
9829 +       movl    %edi, -36(%rbp)
9830 +       .loc 1 22 0
9831 +       movq    %rsp, %rax
9832 +       movq    %rax, -48(%rbp)
9833 +       .loc 1 23 0
9834 +       movl    -36(%rbp), %edx
9835 +       movslq  %edx,%rax
9836 +       subq    $1, %rax
9837 +       movq    %rax, -24(%rbp)
9838 +       .loc 1 24 0
9839 +       movslq  %edx,%rax
9840 +       addq    $15, %rax
9841 +       addq    $15, %rax
9842 +       shrq    $4, %rax
9843 +       salq    $4, %rax
9844 +       subq    %rax, %rsp
9845 +       movq    %rsp, -56(%rbp)
9846 +       movq    -56(%rbp), %rax
9847 +       addq    $15, %rax
9848 +       shrq    $4, %rax
9849 +       salq    $4, %rax
9850 +       movq    %rax, -56(%rbp)
9851 +       movq    -56(%rbp), %rax
9852 +       movq    %rax, -16(%rbp)
9853 +       .loc 1 27 0
9854 +       movl    $0, -4(%rbp)
9855 +       jmp     .L2
9856 +.L3:
9857 +       .loc 1 28 0
9858 +       movl    -4(%rbp), %esi
9859 +       movl    -4(%rbp), %eax
9860 +       movl    %eax, %ecx
9861 +       movq    -16(%rbp), %rdx
9862 +       movslq  %esi,%rax
9863 +       movb    %cl, (%rdx,%rax)
9864 +       .loc 1 27 0
9865 +       addl    $1, -4(%rbp)
9866 +.L2:
9867 +       movl    -4(%rbp), %eax
9868 +       cmpl    -36(%rbp), %eax
9869 +       jl      .L3
9870 +       .loc 1 30 0
9871 +       .globl  break_here
9872 +break_here:
9873 +       movq    -16(%rbp), %rax
9874 +       movb    $0, (%rax)
9875 +       movq    -48(%rbp), %rsp
9876 +       .loc 1 31 0
9877 +       leave
9878 +       ret
9879 +.LFE2:
9880 +       .size   foo, .-foo
9881 +       .section        .debug_frame,"",@progbits
9882 +.Lframe0:
9883 +       .long   .LECIE0-.LSCIE0
9884 +.LSCIE0:
9885 +       .long   0xffffffff
9886 +       .byte   0x1
9887 +       .string ""
9888 +       .uleb128 0x1
9889 +       .sleb128 -8
9890 +       .byte   0x10
9891 +       .byte   0xc
9892 +       .uleb128 0x7
9893 +       .uleb128 0x8
9894 +       .byte   0x90
9895 +       .uleb128 0x1
9896 +       .align 8
9897 +.LECIE0:
9898 +.LSFDE0:
9899 +       .long   .LEFDE0-.LASFDE0
9900 +.LASFDE0:
9901 +       .long   .Lframe0
9902 +       .quad   .LFB2
9903 +       .quad   .LFE2-.LFB2
9904 +       .byte   0x4
9905 +       .long   .LCFI0-.LFB2
9906 +       .byte   0xe
9907 +       .uleb128 0x10
9908 +       .byte   0x86
9909 +       .uleb128 0x2
9910 +       .byte   0x4
9911 +       .long   .LCFI1-.LCFI0
9912 +       .byte   0xd
9913 +       .uleb128 0x6
9914 +       .align 8
9915 +.LEFDE0:
9916 +       .section        .eh_frame,"a",@progbits
9917 +.Lframe1:
9918 +       .long   .LECIE1-.LSCIE1
9919 +.LSCIE1:
9920 +       .long   0x0
9921 +       .byte   0x1
9922 +       .string "zR"
9923 +       .uleb128 0x1
9924 +       .sleb128 -8
9925 +       .byte   0x10
9926 +       .uleb128 0x1
9927 +       .byte   0x3
9928 +       .byte   0xc
9929 +       .uleb128 0x7
9930 +       .uleb128 0x8
9931 +       .byte   0x90
9932 +       .uleb128 0x1
9933 +       .align 8
9934 +.LECIE1:
9935 +.LSFDE1:
9936 +       .long   .LEFDE1-.LASFDE1
9937 +.LASFDE1:
9938 +       .long   .LASFDE1-.Lframe1
9939 +       .long   .LFB2
9940 +       .long   .LFE2-.LFB2
9941 +       .uleb128 0x0
9942 +       .byte   0x4
9943 +       .long   .LCFI0-.LFB2
9944 +       .byte   0xe
9945 +       .uleb128 0x10
9946 +       .byte   0x86
9947 +       .uleb128 0x2
9948 +       .byte   0x4
9949 +       .long   .LCFI1-.LCFI0
9950 +       .byte   0xd
9951 +       .uleb128 0x6
9952 +       .align 8
9953 +.LEFDE1:
9954 +       .text
9955 +.Letext0:
9956 +       .section        .debug_loc,"",@progbits
9957 +.Ldebug_loc0:
9958 +.LLST0:
9959 +       .quad   .LFB2-.Ltext0
9960 +       .quad   .LCFI0-.Ltext0
9961 +       .value  0x2
9962 +       .byte   0x77
9963 +       .sleb128 8
9964 +       .quad   .LCFI0-.Ltext0
9965 +       .quad   .LCFI1-.Ltext0
9966 +       .value  0x2
9967 +       .byte   0x77
9968 +       .sleb128 16
9969 +       .quad   .LCFI1-.Ltext0
9970 +       .quad   .LFE2-.Ltext0
9971 +       .value  0x2
9972 +       .byte   0x76
9973 +       .sleb128 16
9974 +       .quad   0x0
9975 +       .quad   0x0
9976 +       .section        .debug_info
9977 +.Ldebug_relative:
9978 +       .long   .Ldebug_end - .Ldebug_start
9979 +.Ldebug_start:
9980 +       .value  0x2
9981 +       .long   .Ldebug_abbrev0
9982 +       .byte   0x8
9983 +       .uleb128 0x1
9984 +       .long   .LASF2
9985 +       .byte   0x1
9986 +       .long   .LASF3
9987 +       .long   .LASF4
9988 +       .quad   .Ltext0
9989 +       .quad   .Letext0
9990 +       .long   .Ldebug_line0
9991 +       .uleb128 0x2
9992 +       .byte   0x1
9993 +       .string "foo"
9994 +       .byte   0x1
9995 +       .byte   0x16
9996 +       .byte   0x1
9997 +       .quad   .LFB2
9998 +       .quad   .LFE2
9999 +       .long   .LLST0
10000 +       .long   .Ltype_int - .Ldebug_relative
10001 +       .uleb128 0x3
10002 +       .long   .LASF5
10003 +       .byte   0x1
10004 +       .byte   0x15
10005 +       .long   .Ltype_int - .Ldebug_relative
10006 +       .byte   0x2
10007 +       .byte   0x91
10008 +       .sleb128 -52
10009 +.Ltag_pointer:
10010 +       .uleb128 0x4
10011 +       .byte   0x8     /* DW_AT_byte_size */
10012 +       .long   .Ltag_array_type - .debug_info  /* DW_AT_type */
10013 +       .uleb128 0x5    /* Abbrev Number: 5 (DW_TAG_variable) */
10014 +       .long   .LASF0
10015 +       .byte   0x1
10016 +       .byte   0x18
10017 +#if 1
10018 +       .long   .Ltag_pointer - .debug_info
10019 +#else
10020 +       /* Debugging only: Skip the typedef indirection.  */
10021 +       .long   .Ltag_array_type - .debug_info
10022 +#endif
10023 +       /* DW_AT_location: DW_FORM_block1: start */
10024 +       .byte   0x3
10025 +       .byte   0x91
10026 +       .sleb128 -32
10027 +#if 0
10028 +       .byte   0x6     /* DW_OP_deref */
10029 +#else
10030 +       .byte   0x96    /* DW_OP_nop */
10031 +#endif
10032 +       /* DW_AT_location: DW_FORM_block1: end */
10033 +       .uleb128 0x6
10034 +       .string "i"
10035 +       .byte   0x1
10036 +       .byte   0x19
10037 +       .long   .Ltype_int - .Ldebug_relative
10038 +       .byte   0x2
10039 +       .byte   0x91
10040 +       .sleb128 -20
10041 +       .byte   0x0
10042 +.Ltype_int:
10043 +       .uleb128 0x7
10044 +       .byte   0x4
10045 +       .byte   0x5
10046 +       .string "int"
10047 +.Ltag_array_type:
10048 +       .uleb128 0x8    /* Abbrev Number: 8 (DW_TAG_array_type) */
10049 +       .long   .Ltype_char - .Ldebug_relative
10050 +       .long   .Ltype_ulong - .Ldebug_relative /* DW_AT_sibling: DW_FORM_ref4 */
10051 +1:     /* DW_AT_data_location: DW_FORM_block1: start */
10052 +       .byte   2f - 3f /* length */
10053 +3:
10054 +       .byte   0x97    /* DW_OP_push_object_address */
10055 +#if 1
10056 +       .byte   0x6     /* DW_OP_deref */
10057 +#else
10058 +       .byte   0x96    /* DW_OP_nop */
10059 +#endif
10060 +2:     /* DW_AT_data_location: DW_FORM_block1: end */
10061 +       .uleb128 0x9
10062 +       .long   .Ltype_char - .Ldebug_relative  /* DW_AT_type: DW_FORM_ref4 */
10063 +       .byte   0x3
10064 +       .byte   0x91
10065 +       .sleb128 -40
10066 +       .byte   0x6
10067 +       .byte   0x0
10068 +.Ltype_ulong:
10069 +       .uleb128 0xa
10070 +       .byte   0x8
10071 +       .byte   0x7
10072 +.Ltype_char:
10073 +       .uleb128 0xb
10074 +       .byte   0x1
10075 +       .byte   0x6
10076 +       .long   .LASF1
10077 +       .byte   0x0
10078 +.Ldebug_end:
10079 +       .section        .debug_abbrev
10080 +       .uleb128 0x1
10081 +       .uleb128 0x11
10082 +       .byte   0x1
10083 +       .uleb128 0x25
10084 +       .uleb128 0xe
10085 +       .uleb128 0x13
10086 +       .uleb128 0xb
10087 +       .uleb128 0x3
10088 +       .uleb128 0xe
10089 +       .uleb128 0x1b
10090 +       .uleb128 0xe
10091 +       .uleb128 0x11
10092 +       .uleb128 0x1
10093 +       .uleb128 0x12
10094 +       .uleb128 0x1
10095 +       .uleb128 0x10
10096 +       .uleb128 0x6
10097 +       .byte   0x0
10098 +       .byte   0x0
10099 +       .uleb128 0x2
10100 +       .uleb128 0x2e
10101 +       .byte   0x1
10102 +       .uleb128 0x3f
10103 +       .uleb128 0xc
10104 +       .uleb128 0x3
10105 +       .uleb128 0x8
10106 +       .uleb128 0x3a
10107 +       .uleb128 0xb
10108 +       .uleb128 0x3b
10109 +       .uleb128 0xb
10110 +       .uleb128 0x27
10111 +       .uleb128 0xc
10112 +       .uleb128 0x11
10113 +       .uleb128 0x1
10114 +       .uleb128 0x12
10115 +       .uleb128 0x1
10116 +       .uleb128 0x40
10117 +       .uleb128 0x6
10118 +       .uleb128 0x1
10119 +       .uleb128 0x13
10120 +       .byte   0x0
10121 +       .byte   0x0
10122 +       .uleb128 0x3
10123 +       .uleb128 0x5
10124 +       .byte   0x0
10125 +       .uleb128 0x3
10126 +       .uleb128 0xe
10127 +       .uleb128 0x3a
10128 +       .uleb128 0xb
10129 +       .uleb128 0x3b
10130 +       .uleb128 0xb
10131 +       .uleb128 0x49
10132 +       .uleb128 0x13
10133 +       .uleb128 0x2
10134 +       .uleb128 0xa
10135 +       .byte   0x0
10136 +       .byte   0x0
10137 +       .uleb128 0x4    /* .Ltag_pointer abbrev */
10138 +       .uleb128 0x0f   /* DW_TAG_pointer_type */
10139 +       .byte   0x0
10140 +       .uleb128 0x0b
10141 +       .uleb128 0xb
10142 +       .uleb128 0x49
10143 +       .uleb128 0x13
10144 +       .byte   0x0
10145 +       .byte   0x0
10146 +       .uleb128 0x5
10147 +       .uleb128 0x34
10148 +       .byte   0x0
10149 +       .uleb128 0x3
10150 +       .uleb128 0xe
10151 +       .uleb128 0x3a
10152 +       .uleb128 0xb
10153 +       .uleb128 0x3b
10154 +       .uleb128 0xb
10155 +       .uleb128 0x49
10156 +       .uleb128 0x13
10157 +       .uleb128 0x2
10158 +       .uleb128 0xa
10159 +       .byte   0x0
10160 +       .byte   0x0
10161 +       .uleb128 0x6
10162 +       .uleb128 0x34
10163 +       .byte   0x0
10164 +       .uleb128 0x3
10165 +       .uleb128 0x8
10166 +       .uleb128 0x3a
10167 +       .uleb128 0xb
10168 +       .uleb128 0x3b
10169 +       .uleb128 0xb
10170 +       .uleb128 0x49
10171 +       .uleb128 0x13
10172 +       .uleb128 0x2
10173 +       .uleb128 0xa
10174 +       .byte   0x0
10175 +       .byte   0x0
10176 +       .uleb128 0x7
10177 +       .uleb128 0x24
10178 +       .byte   0x0
10179 +       .uleb128 0xb
10180 +       .uleb128 0xb
10181 +       .uleb128 0x3e
10182 +       .uleb128 0xb
10183 +       .uleb128 0x3
10184 +       .uleb128 0x8
10185 +       .byte   0x0
10186 +       .byte   0x0
10187 +       .uleb128 0x8    /* Abbrev Number: 8 (DW_TAG_array_type) */
10188 +       .uleb128 0x1
10189 +       .byte   0x1
10190 +       .uleb128 0x49   /* DW_AT_type */
10191 +       .uleb128 0x13   /* DW_FORM_ref4 */
10192 +       .uleb128 0x1    /* DW_AT_sibling */
10193 +       .uleb128 0x13   /* DW_FORM_ref4 */
10194 +       .uleb128 0x50   /* DW_AT_data_location */
10195 +       .uleb128 0xa    /* DW_FORM_block1 */
10196 +       .byte   0x0
10197 +       .byte   0x0
10198 +       .uleb128 0x9
10199 +       .uleb128 0x21
10200 +       .byte   0x0
10201 +       .uleb128 0x49   /* DW_AT_type */
10202 +       .uleb128 0x13   /* DW_FORM_ref4 */
10203 +       .uleb128 0x2f
10204 +       .uleb128 0xa
10205 +       .byte   0x0
10206 +       .byte   0x0
10207 +       .uleb128 0xa
10208 +       .uleb128 0x24
10209 +       .byte   0x0
10210 +       .uleb128 0xb
10211 +       .uleb128 0xb
10212 +       .uleb128 0x3e
10213 +       .uleb128 0xb
10214 +       .byte   0x0
10215 +       .byte   0x0
10216 +       .uleb128 0xb
10217 +       .uleb128 0x24
10218 +       .byte   0x0
10219 +       .uleb128 0xb
10220 +       .uleb128 0xb
10221 +       .uleb128 0x3e
10222 +       .uleb128 0xb
10223 +       .uleb128 0x3
10224 +       .uleb128 0xe
10225 +       .byte   0x0
10226 +       .byte   0x0
10227 +       .byte   0x0
10228 +       .section        .debug_pubnames,"",@progbits
10229 +       .long   0x16
10230 +       .value  0x2
10231 +       .long   .Ldebug_info0
10232 +       .long   0xa8
10233 +       .long   0x2d
10234 +       .string "foo"
10235 +       .long   0x0
10236 +       .section        .debug_aranges,"",@progbits
10237 +       .long   0x2c
10238 +       .value  0x2
10239 +       .long   .Ldebug_info0
10240 +       .byte   0x8
10241 +       .byte   0x0
10242 +       .value  0x0
10243 +       .value  0x0
10244 +       .quad   .Ltext0
10245 +       .quad   .Letext0-.Ltext0
10246 +       .quad   0x0
10247 +       .quad   0x0
10248 +       .section        .debug_str,"MS",@progbits,1
10249 +.LASF0:
10250 +       .string "array"
10251 +.LASF5:
10252 +       .string "size"
10253 +.LASF3:
10254 +       .string "x86_64-vla-pointer.c"
10255 +.LASF6:
10256 +       .string "array_t"
10257 +.LASF1:
10258 +       .string "char"
10259 +.LASF4:
10260 +       .string "gdb.arch"
10261 +.LASF2:
10262 +       .string "GNU C 4.3.2 20081105 (Red Hat 4.3.2-7)"
10263 +       .ident  "GCC: (GNU) 4.3.2 20081105 (Red Hat 4.3.2-7)"
10264 +       .section        .note.GNU-stack,"",@progbits
10265 diff --git a/gdb/testsuite/gdb.arch/x86_64-vla-pointer.c b/gdb/testsuite/gdb.arch/x86_64-vla-pointer.c
10266 new file mode 100644
10267 index 0000000..fe2c8f7
10268 --- /dev/null
10269 +++ b/gdb/testsuite/gdb.arch/x86_64-vla-pointer.c
10270 @@ -0,0 +1,43 @@
10271 +/* This testcase is part of GDB, the GNU debugger.
10272 +
10273 +   Copyright 2009 Free Software Foundation, Inc.
10274 +
10275 +   This program is free software; you can redistribute it and/or modify
10276 +   it under the terms of the GNU General Public License as published by
10277 +   the Free Software Foundation; either version 3 of the License, or
10278 +   (at your option) any later version.
10279 +
10280 +   This program is distributed in the hope that it will be useful,
10281 +   but WITHOUT ANY WARRANTY; without even the implied warranty of
10282 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10283 +   GNU General Public License for more details.
10284 +
10285 +   You should have received a copy of the GNU General Public License
10286 +   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
10287 +
10288 +#if 0
10289 +
10290 +void
10291 +foo (int size)
10292 +{
10293 +  typedef char array_t[size];
10294 +  array_t array;
10295 +  int i;
10296 +
10297 +  for (i = 0; i < size; i++)
10298 +    array[i] = i;
10299 +
10300 +  array[0] = 0;        /* break-here */
10301 +}
10302 +
10303 +#else
10304 +
10305 +int
10306 +main (void)
10307 +{
10308 +  foo (26);
10309 +  foo (78);
10310 +  return 0;
10311 +}
10312 +
10313 +#endif
10314 diff --git a/gdb/testsuite/gdb.arch/x86_64-vla-pointer.exp b/gdb/testsuite/gdb.arch/x86_64-vla-pointer.exp
10315 new file mode 100644
10316 index 0000000..d243cf1
10317 --- /dev/null
10318 +++ b/gdb/testsuite/gdb.arch/x86_64-vla-pointer.exp
10319 @@ -0,0 +1,66 @@
10320 +# Copyright 2009 Free Software Foundation, Inc.
10321 +
10322 +# This program is free software; you can redistribute it and/or modify
10323 +# it under the terms of the GNU General Public License as published by
10324 +# the Free Software Foundation; either version 3 of the License, or
10325 +# (at your option) any later version.
10326 +#
10327 +# This program is distributed in the hope that it will be useful,
10328 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
10329 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10330 +# GNU General Public License for more details.
10331 +#
10332 +# You should have received a copy of the GNU General Public License
10333 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
10334 +
10335 +if ![istarget "x86_64-*-*"] then {
10336 +    verbose "Skipping over gdb.arch/x86_64-vla-pointer.exp test made only for x86_64."
10337 +    return
10338 +}
10339 +
10340 +set testfile x86_64-vla-pointer
10341 +set srcasmfile ${testfile}-foo.S
10342 +set srcfile ${testfile}.c
10343 +set binfile ${objdir}/${subdir}/${testfile}
10344 +set binobjfile ${objdir}/${subdir}/${testfile}-foo.o
10345 +if  { [gdb_compile "${srcdir}/${subdir}/${srcasmfile}" "${binobjfile}" object {}] != "" } {
10346 +    untested "Couldn't compile test program"
10347 +    return -1
10348 +}
10349 +if  { [gdb_compile "${srcdir}/${subdir}/${srcfile} ${binobjfile}" "${binfile}" executable {debug}] != "" } {
10350 +    untested "Couldn't compile test program"
10351 +    return -1
10352 +}
10353 +
10354 +gdb_exit
10355 +gdb_start
10356 +gdb_reinitialize_dir $srcdir/$subdir
10357 +gdb_load ${binfile}
10358 +
10359 +if ![runto_main] {
10360 +    untested x86_64-vla-pointer
10361 +    return -1
10362 +}
10363 +
10364 +gdb_breakpoint "break_here"
10365 +
10366 +gdb_continue_to_breakpoint "break_here"
10367 +
10368 +gdb_test "whatis array" "type = char \\(\\*\\)\\\[variable\\\]" "first: whatis array"
10369 +gdb_test "ptype array" "type = char \\(\\*\\)\\\[26\\\]" "first: ptype array"
10370 +
10371 +gdb_test "whatis *array" "type = char \\\[26\\\]" "first: whatis *array"
10372 +gdb_test "ptype *array" "type = char \\\[26\\\]" "first: ptype *array"
10373 +
10374 +gdb_test "p (*array)\[1\]" "\\$\[0-9\] = 1 '\\\\001'"
10375 +gdb_test "p (*array)\[2\]" "\\$\[0-9\] = 2 '\\\\002'"
10376 +gdb_test "p (*array)\[3\]" "\\$\[0-9\] = 3 '\\\\003'"
10377 +gdb_test "p (*array)\[4\]" "\\$\[0-9\] = 4 '\\\\004'"
10378 +
10379 +gdb_continue_to_breakpoint "break_here"
10380 +
10381 +gdb_test "whatis array" "type = char \\(\\*\\)\\\[variable\\\]" "second: whatis array"
10382 +gdb_test "ptype array" "type = char \\(\\*\\)\\\[78\\\]" "second: ptype array"
10383 +
10384 +gdb_test "whatis *array" "type = char \\\[78\\\]" "second: whatis *array"
10385 +gdb_test "ptype *array" "type = char \\\[78\\\]" "second: ptype *array"
10386 diff --git a/gdb/testsuite/gdb.arch/x86_64-vla-typedef-foo.S b/gdb/testsuite/gdb.arch/x86_64-vla-typedef-foo.S
10387 new file mode 100644
10388 index 0000000..66f7a39
10389 --- /dev/null
10390 +++ b/gdb/testsuite/gdb.arch/x86_64-vla-typedef-foo.S
10391 @@ -0,0 +1,455 @@
10392 +       .file   "x86_64-vla-typedef.c"
10393 +       .section        .debug_abbrev,"",@progbits
10394 +.Ldebug_abbrev0:
10395 +       .section        .debug_info,"",@progbits
10396 +.Ldebug_info0:
10397 +       .section        .debug_line,"",@progbits
10398 +.Ldebug_line0:
10399 +       .text
10400 +.Ltext0:
10401 +.globl foo
10402 +       .type   foo, @function
10403 +foo:
10404 +.LFB2:
10405 +       .file 1 "x86_64-vla-typedef.c"
10406 +       .loc 1 22 0
10407 +       pushq   %rbp
10408 +.LCFI0:
10409 +       movq    %rsp, %rbp
10410 +.LCFI1:
10411 +       subq    $64, %rsp
10412 +.LCFI2:
10413 +       movl    %edi, -36(%rbp)
10414 +       .loc 1 22 0
10415 +       movq    %rsp, %rax
10416 +       movq    %rax, -48(%rbp)
10417 +       .loc 1 23 0
10418 +       movl    -36(%rbp), %edx
10419 +       movslq  %edx,%rax
10420 +       subq    $1, %rax
10421 +       movq    %rax, -24(%rbp)
10422 +       .loc 1 24 0
10423 +       movslq  %edx,%rax
10424 +       addq    $15, %rax
10425 +       addq    $15, %rax
10426 +       shrq    $4, %rax
10427 +       salq    $4, %rax
10428 +       subq    %rax, %rsp
10429 +       movq    %rsp, -56(%rbp)
10430 +       movq    -56(%rbp), %rax
10431 +       addq    $15, %rax
10432 +       shrq    $4, %rax
10433 +       salq    $4, %rax
10434 +       movq    %rax, -56(%rbp)
10435 +       movq    -56(%rbp), %rax
10436 +       movq    %rax, -16(%rbp)
10437 +       .loc 1 27 0
10438 +       movl    $0, -4(%rbp)
10439 +       jmp     .L2
10440 +.L3:
10441 +       .loc 1 28 0
10442 +       movl    -4(%rbp), %esi
10443 +       movl    -4(%rbp), %eax
10444 +       movl    %eax, %ecx
10445 +       movq    -16(%rbp), %rdx
10446 +       movslq  %esi,%rax
10447 +       movb    %cl, (%rdx,%rax)
10448 +       .loc 1 27 0
10449 +       addl    $1, -4(%rbp)
10450 +.L2:
10451 +       movl    -4(%rbp), %eax
10452 +       cmpl    -36(%rbp), %eax
10453 +       jl      .L3
10454 +       .loc 1 30 0
10455 +       .globl  break_here
10456 +break_here:
10457 +       movq    -16(%rbp), %rax
10458 +       movb    $0, (%rax)
10459 +       movq    -48(%rbp), %rsp
10460 +       .loc 1 31 0
10461 +       leave
10462 +       ret
10463 +.LFE2:
10464 +       .size   foo, .-foo
10465 +       .section        .debug_frame,"",@progbits
10466 +.Lframe0:
10467 +       .long   .LECIE0-.LSCIE0
10468 +.LSCIE0:
10469 +       .long   0xffffffff
10470 +       .byte   0x1
10471 +       .string ""
10472 +       .uleb128 0x1
10473 +       .sleb128 -8
10474 +       .byte   0x10
10475 +       .byte   0xc
10476 +       .uleb128 0x7
10477 +       .uleb128 0x8
10478 +       .byte   0x90
10479 +       .uleb128 0x1
10480 +       .align 8
10481 +.LECIE0:
10482 +.LSFDE0:
10483 +       .long   .LEFDE0-.LASFDE0
10484 +.LASFDE0:
10485 +       .long   .Lframe0
10486 +       .quad   .LFB2
10487 +       .quad   .LFE2-.LFB2
10488 +       .byte   0x4
10489 +       .long   .LCFI0-.LFB2
10490 +       .byte   0xe
10491 +       .uleb128 0x10
10492 +       .byte   0x86
10493 +       .uleb128 0x2
10494 +       .byte   0x4
10495 +       .long   .LCFI1-.LCFI0
10496 +       .byte   0xd
10497 +       .uleb128 0x6
10498 +       .align 8
10499 +.LEFDE0:
10500 +       .section        .eh_frame,"a",@progbits
10501 +.Lframe1:
10502 +       .long   .LECIE1-.LSCIE1
10503 +.LSCIE1:
10504 +       .long   0x0
10505 +       .byte   0x1
10506 +       .string "zR"
10507 +       .uleb128 0x1
10508 +       .sleb128 -8
10509 +       .byte   0x10
10510 +       .uleb128 0x1
10511 +       .byte   0x3
10512 +       .byte   0xc
10513 +       .uleb128 0x7
10514 +       .uleb128 0x8
10515 +       .byte   0x90
10516 +       .uleb128 0x1
10517 +       .align 8
10518 +.LECIE1:
10519 +.LSFDE1:
10520 +       .long   .LEFDE1-.LASFDE1
10521 +.LASFDE1:
10522 +       .long   .LASFDE1-.Lframe1
10523 +       .long   .LFB2
10524 +       .long   .LFE2-.LFB2
10525 +       .uleb128 0x0
10526 +       .byte   0x4
10527 +       .long   .LCFI0-.LFB2
10528 +       .byte   0xe
10529 +       .uleb128 0x10
10530 +       .byte   0x86
10531 +       .uleb128 0x2
10532 +       .byte   0x4
10533 +       .long   .LCFI1-.LCFI0
10534 +       .byte   0xd
10535 +       .uleb128 0x6
10536 +       .align 8
10537 +.LEFDE1:
10538 +       .text
10539 +.Letext0:
10540 +       .section        .debug_loc,"",@progbits
10541 +.Ldebug_loc0:
10542 +.LLST0:
10543 +       .quad   .LFB2-.Ltext0
10544 +       .quad   .LCFI0-.Ltext0
10545 +       .value  0x2
10546 +       .byte   0x77
10547 +       .sleb128 8
10548 +       .quad   .LCFI0-.Ltext0
10549 +       .quad   .LCFI1-.Ltext0
10550 +       .value  0x2
10551 +       .byte   0x77
10552 +       .sleb128 16
10553 +       .quad   .LCFI1-.Ltext0
10554 +       .quad   .LFE2-.Ltext0
10555 +       .value  0x2
10556 +       .byte   0x76
10557 +       .sleb128 16
10558 +       .quad   0x0
10559 +       .quad   0x0
10560 +       .section        .debug_info
10561 +       .long   .Ldebug_end - .Ldebug_start
10562 +.Ldebug_start:
10563 +       .value  0x2
10564 +       .long   .Ldebug_abbrev0
10565 +       .byte   0x8
10566 +       .uleb128 0x1
10567 +       .long   .LASF2
10568 +       .byte   0x1
10569 +       .long   .LASF3
10570 +       .long   .LASF4
10571 +       .quad   .Ltext0
10572 +       .quad   .Letext0
10573 +       .long   .Ldebug_line0
10574 +       .uleb128 0x2
10575 +       .byte   0x1
10576 +       .string "foo"
10577 +       .byte   0x1
10578 +       .byte   0x16
10579 +       .byte   0x1
10580 +       .quad   .LFB2
10581 +       .quad   .LFE2
10582 +       .long   .LLST0
10583 +       .long   0x83
10584 +       .uleb128 0x3
10585 +       .long   .LASF5
10586 +       .byte   0x1
10587 +       .byte   0x15
10588 +       .long   0x83
10589 +       .byte   0x2
10590 +       .byte   0x91
10591 +       .sleb128 -52
10592 +.Ltag_typedef:
10593 +       .uleb128 0x4
10594 +       .long   .LASF6
10595 +       .byte   0x1
10596 +       .byte   0x17
10597 +       .long   .Ltag_array_type - .debug_info
10598 +       .uleb128 0x5    /* Abbrev Number: 5 (DW_TAG_variable) */
10599 +       .long   .LASF0
10600 +       .byte   0x1
10601 +       .byte   0x18
10602 +#if 1
10603 +       .long   .Ltag_typedef - .debug_info
10604 +#else
10605 +       /* Debugging only: Skip the typedef indirection.  */
10606 +       .long   .Ltag_array_type - .debug_info
10607 +#endif
10608 +       /* DW_AT_location: DW_FORM_block1: start */
10609 +       .byte   0x3
10610 +       .byte   0x91
10611 +       .sleb128 -32
10612 +#if 0
10613 +       .byte   0x6     /* DW_OP_deref */
10614 +#else
10615 +       .byte   0x96    /* DW_OP_nop */
10616 +#endif
10617 +       /* DW_AT_location: DW_FORM_block1: end */
10618 +       .uleb128 0x6
10619 +       .string "i"
10620 +       .byte   0x1
10621 +       .byte   0x19
10622 +       .long   0x83
10623 +       .byte   0x2
10624 +       .byte   0x91
10625 +       .sleb128 -20
10626 +       .byte   0x0
10627 +       .uleb128 0x7
10628 +       .byte   0x4
10629 +       .byte   0x5
10630 +       .string "int"
10631 +.Ltag_array_type:
10632 +       .uleb128 0x8    /* Abbrev Number: 8 (DW_TAG_array_type) */
10633 +       .long   0xa0 + (2f - 1f)        /* DW_AT_type: DW_FORM_ref4 */
10634 +       .long   0x9d + (2f - 1f)        /* DW_AT_sibling: DW_FORM_ref4 */
10635 +1:     /* DW_AT_data_location: DW_FORM_block1: start */
10636 +       .byte   2f - 3f /* length */
10637 +3:
10638 +       .byte   0x97    /* DW_OP_push_object_address */
10639 +       .byte   0x6     /* DW_OP_deref */
10640 +2:     /* DW_AT_data_location: DW_FORM_block1: end */
10641 +       .uleb128 0x9
10642 +       .long   0x9d + (2b - 1b)        /* DW_AT_type: DW_FORM_ref4 */
10643 +       .byte   0x3
10644 +       .byte   0x91
10645 +       .sleb128 -40
10646 +       .byte   0x6
10647 +       .byte   0x0
10648 +       .uleb128 0xa
10649 +       .byte   0x8
10650 +       .byte   0x7
10651 +       .uleb128 0xb
10652 +       .byte   0x1
10653 +       .byte   0x6
10654 +       .long   .LASF1
10655 +       .byte   0x0
10656 +.Ldebug_end:
10657 +       .section        .debug_abbrev
10658 +       .uleb128 0x1
10659 +       .uleb128 0x11
10660 +       .byte   0x1
10661 +       .uleb128 0x25
10662 +       .uleb128 0xe
10663 +       .uleb128 0x13
10664 +       .uleb128 0xb
10665 +       .uleb128 0x3
10666 +       .uleb128 0xe
10667 +       .uleb128 0x1b
10668 +       .uleb128 0xe
10669 +       .uleb128 0x11
10670 +       .uleb128 0x1
10671 +       .uleb128 0x12
10672 +       .uleb128 0x1
10673 +       .uleb128 0x10
10674 +       .uleb128 0x6
10675 +       .byte   0x0
10676 +       .byte   0x0
10677 +       .uleb128 0x2
10678 +       .uleb128 0x2e
10679 +       .byte   0x1
10680 +       .uleb128 0x3f
10681 +       .uleb128 0xc
10682 +       .uleb128 0x3
10683 +       .uleb128 0x8
10684 +       .uleb128 0x3a
10685 +       .uleb128 0xb
10686 +       .uleb128 0x3b
10687 +       .uleb128 0xb
10688 +       .uleb128 0x27
10689 +       .uleb128 0xc
10690 +       .uleb128 0x11
10691 +       .uleb128 0x1
10692 +       .uleb128 0x12
10693 +       .uleb128 0x1
10694 +       .uleb128 0x40
10695 +       .uleb128 0x6
10696 +       .uleb128 0x1
10697 +       .uleb128 0x13
10698 +       .byte   0x0
10699 +       .byte   0x0
10700 +       .uleb128 0x3
10701 +       .uleb128 0x5
10702 +       .byte   0x0
10703 +       .uleb128 0x3
10704 +       .uleb128 0xe
10705 +       .uleb128 0x3a
10706 +       .uleb128 0xb
10707 +       .uleb128 0x3b
10708 +       .uleb128 0xb
10709 +       .uleb128 0x49
10710 +       .uleb128 0x13
10711 +       .uleb128 0x2
10712 +       .uleb128 0xa
10713 +       .byte   0x0
10714 +       .byte   0x0
10715 +       .uleb128 0x4
10716 +       .uleb128 0x16
10717 +       .byte   0x0
10718 +       .uleb128 0x3
10719 +       .uleb128 0xe
10720 +       .uleb128 0x3a
10721 +       .uleb128 0xb
10722 +       .uleb128 0x3b
10723 +       .uleb128 0xb
10724 +       .uleb128 0x49
10725 +       .uleb128 0x13
10726 +       .byte   0x0
10727 +       .byte   0x0
10728 +       .uleb128 0x5
10729 +       .uleb128 0x34
10730 +       .byte   0x0
10731 +       .uleb128 0x3
10732 +       .uleb128 0xe
10733 +       .uleb128 0x3a
10734 +       .uleb128 0xb
10735 +       .uleb128 0x3b
10736 +       .uleb128 0xb
10737 +       .uleb128 0x49
10738 +       .uleb128 0x13
10739 +       .uleb128 0x2
10740 +       .uleb128 0xa
10741 +       .byte   0x0
10742 +       .byte   0x0
10743 +       .uleb128 0x6
10744 +       .uleb128 0x34
10745 +       .byte   0x0
10746 +       .uleb128 0x3
10747 +       .uleb128 0x8
10748 +       .uleb128 0x3a
10749 +       .uleb128 0xb
10750 +       .uleb128 0x3b
10751 +       .uleb128 0xb
10752 +       .uleb128 0x49
10753 +       .uleb128 0x13
10754 +       .uleb128 0x2
10755 +       .uleb128 0xa
10756 +       .byte   0x0
10757 +       .byte   0x0
10758 +       .uleb128 0x7
10759 +       .uleb128 0x24
10760 +       .byte   0x0
10761 +       .uleb128 0xb
10762 +       .uleb128 0xb
10763 +       .uleb128 0x3e
10764 +       .uleb128 0xb
10765 +       .uleb128 0x3
10766 +       .uleb128 0x8
10767 +       .byte   0x0
10768 +       .byte   0x0
10769 +       .uleb128 0x8    /* Abbrev Number: 8 (DW_TAG_array_type) */
10770 +       .uleb128 0x1
10771 +       .byte   0x1
10772 +       .uleb128 0x49   /* DW_AT_type */
10773 +       .uleb128 0x13   /* DW_FORM_ref4 */
10774 +       .uleb128 0x1    /* DW_AT_sibling */
10775 +       .uleb128 0x13   /* DW_FORM_ref4 */
10776 +       .uleb128 0x50   /* DW_AT_data_location */
10777 +       .uleb128 0xa    /* DW_FORM_block1 */
10778 +       .byte   0x0
10779 +       .byte   0x0
10780 +       .uleb128 0x9
10781 +       .uleb128 0x21
10782 +       .byte   0x0
10783 +       .uleb128 0x49   /* DW_AT_type */
10784 +       .uleb128 0x13   /* DW_FORM_ref4 */
10785 +       .uleb128 0x2f
10786 +       .uleb128 0xa
10787 +       .byte   0x0
10788 +       .byte   0x0
10789 +       .uleb128 0xa
10790 +       .uleb128 0x24
10791 +       .byte   0x0
10792 +       .uleb128 0xb
10793 +       .uleb128 0xb
10794 +       .uleb128 0x3e
10795 +       .uleb128 0xb
10796 +       .byte   0x0
10797 +       .byte   0x0
10798 +       .uleb128 0xb
10799 +       .uleb128 0x24
10800 +       .byte   0x0
10801 +       .uleb128 0xb
10802 +       .uleb128 0xb
10803 +       .uleb128 0x3e
10804 +       .uleb128 0xb
10805 +       .uleb128 0x3
10806 +       .uleb128 0xe
10807 +       .byte   0x0
10808 +       .byte   0x0
10809 +       .byte   0x0
10810 +       .section        .debug_pubnames,"",@progbits
10811 +       .long   0x16
10812 +       .value  0x2
10813 +       .long   .Ldebug_info0
10814 +       .long   0xa8
10815 +       .long   0x2d
10816 +       .string "foo"
10817 +       .long   0x0
10818 +       .section        .debug_aranges,"",@progbits
10819 +       .long   0x2c
10820 +       .value  0x2
10821 +       .long   .Ldebug_info0
10822 +       .byte   0x8
10823 +       .byte   0x0
10824 +       .value  0x0
10825 +       .value  0x0
10826 +       .quad   .Ltext0
10827 +       .quad   .Letext0-.Ltext0
10828 +       .quad   0x0
10829 +       .quad   0x0
10830 +       .section        .debug_str,"MS",@progbits,1
10831 +.LASF0:
10832 +       .string "array"
10833 +.LASF5:
10834 +       .string "size"
10835 +.LASF3:
10836 +       .string "x86_64-vla-typedef.c"
10837 +.LASF6:
10838 +       .string "array_t"
10839 +.LASF1:
10840 +       .string "char"
10841 +.LASF4:
10842 +       .string "gdb.arch"
10843 +.LASF2:
10844 +       .string "GNU C 4.3.2 20081105 (Red Hat 4.3.2-7)"
10845 +       .ident  "GCC: (GNU) 4.3.2 20081105 (Red Hat 4.3.2-7)"
10846 +       .section        .note.GNU-stack,"",@progbits
10847 diff --git a/gdb/testsuite/gdb.arch/x86_64-vla-typedef.c b/gdb/testsuite/gdb.arch/x86_64-vla-typedef.c
10848 new file mode 100644
10849 index 0000000..b809c4e
10850 --- /dev/null
10851 +++ b/gdb/testsuite/gdb.arch/x86_64-vla-typedef.c
10852 @@ -0,0 +1,43 @@
10853 +/* This testcase is part of GDB, the GNU debugger.
10854 +
10855 +   Copyright 2008 Free Software Foundation, Inc.
10856 +
10857 +   This program is free software; you can redistribute it and/or modify
10858 +   it under the terms of the GNU General Public License as published by
10859 +   the Free Software Foundation; either version 3 of the License, or
10860 +   (at your option) any later version.
10861 +
10862 +   This program is distributed in the hope that it will be useful,
10863 +   but WITHOUT ANY WARRANTY; without even the implied warranty of
10864 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10865 +   GNU General Public License for more details.
10866 +
10867 +   You should have received a copy of the GNU General Public License
10868 +   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
10869 +
10870 +#if 0
10871 +
10872 +void
10873 +foo (int size)
10874 +{
10875 +  typedef char array_t[size];
10876 +  array_t array;
10877 +  int i;
10878 +
10879 +  for (i = 0; i < size; i++)
10880 +    array[i] = i;
10881 +
10882 +  array[0] = 0;        /* break-here */
10883 +}
10884 +
10885 +#else
10886 +
10887 +int
10888 +main (void)
10889 +{
10890 +  foo (26);
10891 +  foo (78);
10892 +  return 0;
10893 +}
10894 +
10895 +#endif
10896 diff --git a/gdb/testsuite/gdb.arch/x86_64-vla-typedef.exp b/gdb/testsuite/gdb.arch/x86_64-vla-typedef.exp
10897 new file mode 100644
10898 index 0000000..b05411e
10899 --- /dev/null
10900 +++ b/gdb/testsuite/gdb.arch/x86_64-vla-typedef.exp
10901 @@ -0,0 +1,64 @@
10902 +# Copyright 2009 Free Software Foundation, Inc.
10903 +
10904 +# This program is free software; you can redistribute it and/or modify
10905 +# it under the terms of the GNU General Public License as published by
10906 +# the Free Software Foundation; either version 3 of the License, or
10907 +# (at your option) any later version.
10908 +#
10909 +# This program is distributed in the hope that it will be useful,
10910 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
10911 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10912 +# GNU General Public License for more details.
10913 +#
10914 +# You should have received a copy of the GNU General Public License
10915 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
10916 +
10917 +# Test DW_AT_data_location accessed through DW_TAG_typedef intermediate.
10918 +
10919 +if ![istarget "x86_64-*-*"] then {
10920 +    verbose "Skipping over gdb.arch/x86_64-vla-typedef.exp test made only for x86_64."
10921 +    return
10922 +}
10923 +
10924 +set testfile x86_64-vla-typedef
10925 +set srcasmfile ${testfile}-foo.S
10926 +set srcfile ${testfile}.c
10927 +set binfile ${objdir}/${subdir}/${testfile}
10928 +set binobjfile ${objdir}/${subdir}/${testfile}-foo.o
10929 +if  { [gdb_compile "${srcdir}/${subdir}/${srcasmfile}" "${binobjfile}" object {}] != "" } {
10930 +    untested "Couldn't compile test program"
10931 +    return -1
10932 +}
10933 +if  { [gdb_compile "${srcdir}/${subdir}/${srcfile} ${binobjfile}" "${binfile}" executable {debug}] != "" } {
10934 +    untested "Couldn't compile test program"
10935 +    return -1
10936 +}
10937 +
10938 +gdb_exit
10939 +gdb_start
10940 +gdb_reinitialize_dir $srcdir/$subdir
10941 +gdb_load ${binfile}
10942 +
10943 +if ![runto_main] {
10944 +    untested x86_64-vla-typedef
10945 +    return -1
10946 +}
10947 +
10948 +gdb_breakpoint "break_here"
10949 +
10950 +gdb_continue_to_breakpoint "break_here"
10951 +
10952 +gdb_test "whatis array" "type = array_t" "first: whatis array"
10953 +
10954 +gdb_test "ptype array" "type = char \\\[26\\\]" "first: ptype array"
10955 +
10956 +gdb_test "p array\[1\]" "\\$\[0-9\] = 1 '\\\\001'"
10957 +gdb_test "p array\[2\]" "\\$\[0-9\] = 2 '\\\\002'"
10958 +gdb_test "p array\[3\]" "\\$\[0-9\] = 3 '\\\\003'"
10959 +gdb_test "p array\[4\]" "\\$\[0-9\] = 4 '\\\\004'"
10960 +
10961 +gdb_continue_to_breakpoint "break_here"
10962 +
10963 +gdb_test "whatis array" "type = array_t" "second: whatis array"
10964 +
10965 +gdb_test "ptype array" "type = char \\\[78\\\]" "second: ptype array"
10966 diff --git a/gdb/testsuite/gdb.base/arrayidx.c b/gdb/testsuite/gdb.base/arrayidx.c
10967 index a99137e..c3dc2d9 100644
10968 --- a/gdb/testsuite/gdb.base/arrayidx.c
10969 +++ b/gdb/testsuite/gdb.base/arrayidx.c
10970 @@ -17,6 +17,13 @@
10971  
10972  int array[] = {1, 2, 3, 4};
10973  
10974 +#ifdef __GNUC__
10975 +struct
10976 +  {
10977 +    int a[0];
10978 +  } unbound;
10979 +#endif
10980 +
10981  int
10982  main (void)
10983  {
10984 diff --git a/gdb/testsuite/gdb.base/arrayidx.exp b/gdb/testsuite/gdb.base/arrayidx.exp
10985 index cba0024..0dc0e46 100644
10986 --- a/gdb/testsuite/gdb.base/arrayidx.exp
10987 +++ b/gdb/testsuite/gdb.base/arrayidx.exp
10988 @@ -53,4 +53,12 @@ gdb_test "print array" \
10989           "\\{\\\[0\\\] = 1, \\\[1\\\] = 2, \\\[2\\\] = 3, \\\[3\\\] = 4\\}" \
10990           "Print array with array-indexes on"
10991  
10992 -
10993 +set test "p unbound.a == &unbound.a\[0\]"
10994 +gdb_test_multiple $test $test {
10995 +    -re " = 1\r\n$gdb_prompt $" {
10996 +       pass $test
10997 +    }
10998 +    -re "No symbol \"unbound\" in current context.\r\n$gdb_prompt $" {
10999 +       unsupported "$test (no GCC)"
11000 +    }
11001 +}
11002 diff --git a/gdb/testsuite/gdb.base/internal-var-field-address.c b/gdb/testsuite/gdb.base/internal-var-field-address.c
11003 new file mode 100644
11004 index 0000000..eeb7b85
11005 --- /dev/null
11006 +++ b/gdb/testsuite/gdb.base/internal-var-field-address.c
11007 @@ -0,0 +1,20 @@
11008 +/* This testcase is part of GDB, the GNU debugger.
11009 +
11010 +   Copyright 2009 Free Software Foundation, Inc.
11011 +
11012 +   This program is free software; you can redistribute it and/or modify
11013 +   it under the terms of the GNU General Public License as published by
11014 +   the Free Software Foundation; either version 3 of the License, or
11015 +   (at your option) any later version.
11016 +
11017 +   This program is distributed in the hope that it will be useful,
11018 +   but WITHOUT ANY WARRANTY; without even the implied warranty of
11019 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11020 +   GNU General Public License for more details.
11021 +
11022 +   You should have received a copy of the GNU General Public License
11023 +   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
11024 +
11025 +struct {
11026 +  int field;
11027 +} staticstruct = { 1 };
11028 diff --git a/gdb/testsuite/gdb.base/internal-var-field-address.exp b/gdb/testsuite/gdb.base/internal-var-field-address.exp
11029 new file mode 100644
11030 index 0000000..6d82e73
11031 --- /dev/null
11032 +++ b/gdb/testsuite/gdb.base/internal-var-field-address.exp
11033 @@ -0,0 +1,26 @@
11034 +# Copyright 2009 Free Software Foundation, Inc.
11035 +
11036 +# This program is free software; you can redistribute it and/or modify
11037 +# it under the terms of the GNU General Public License as published by
11038 +# the Free Software Foundation; either version 3 of the License, or
11039 +# (at your option) any later version.
11040 +#
11041 +# This program is distributed in the hope that it will be useful,
11042 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
11043 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11044 +# GNU General Public License for more details.
11045 +#
11046 +# You should have received a copy of the GNU General Public License
11047 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
11048 +
11049 +set test internal-var-field-address
11050 +set binfile ${test}.x
11051 +if  { [gdb_compile "${srcdir}/${subdir}/${test}.c" "${objdir}/${subdir}/${binfile}" object {debug}] != "" } {
11052 +    untested "Couldn't compile test program"
11053 +    return -1
11054 +}
11055 +
11056 +clean_restart $binfile
11057 +
11058 +gdb_test {set $varstruct = staticstruct}
11059 +gdb_test {p $varstruct.field} " = 1"
11060 diff --git a/gdb/testsuite/gdb.base/vla-frame.c b/gdb/testsuite/gdb.base/vla-frame.c
11061 new file mode 100644
11062 index 0000000..5750f68
11063 --- /dev/null
11064 +++ b/gdb/testsuite/gdb.base/vla-frame.c
11065 @@ -0,0 +1,31 @@
11066 +/* This testcase is part of GDB, the GNU debugger.
11067 +
11068 +   Copyright 2011 Free Software Foundation, Inc.
11069 +
11070 +   This program is free software; you can redistribute it and/or modify
11071 +   it under the terms of the GNU General Public License as published by
11072 +   the Free Software Foundation; either version 3 of the License, or
11073 +   (at your option) any later version.
11074 +
11075 +   This program is distributed in the hope that it will be useful,
11076 +   but WITHOUT ANY WARRANTY; without even the implied warranty of
11077 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11078 +   GNU General Public License for more details.
11079 +
11080 +   You should have received a copy of the GNU General Public License
11081 +   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
11082 +
11083 +#include <string.h>
11084 +
11085 +int
11086 +main (int argc, char **argv)
11087 +{
11088 +  char s[2 + argc];
11089 +  void (*f) (char *) = 0;
11090 +
11091 +  memset (s, 0, sizeof (s));
11092 +  s[0] = 'X';
11093 +
11094 +  f (s);
11095 +  return 0;
11096 +}
11097 diff --git a/gdb/testsuite/gdb.base/vla-frame.exp b/gdb/testsuite/gdb.base/vla-frame.exp
11098 new file mode 100644
11099 index 0000000..47736c7
11100 --- /dev/null
11101 +++ b/gdb/testsuite/gdb.base/vla-frame.exp
11102 @@ -0,0 +1,38 @@
11103 +# Copyright 2011 Free Software Foundation, Inc.
11104 +#
11105 +# This program is free software; you can redistribute it and/or modify
11106 +# it under the terms of the GNU General Public License as published by
11107 +# the Free Software Foundation; either version 3 of the License, or
11108 +# (at your option) any later version.
11109 +#
11110 +# This program is distributed in the hope that it will be useful,
11111 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
11112 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11113 +# GNU General Public License for more details.
11114 +#
11115 +# You should have received a copy of the GNU General Public License
11116 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
11117 +
11118 +set testfile vla-frame
11119 +set executable ${testfile}
11120 +
11121 +if { [prepare_for_testing ${testfile}.exp ${executable}] } {
11122 +    return -1
11123 +}
11124 +
11125 +if ![runto_main] {
11126 +    return -1
11127 +}
11128 +
11129 +set test "continue"
11130 +gdb_test_multiple $test $test {
11131 +    -re "Continuing\\.\r\n\r\nProgram received signal SIGSEGV, Segmentation fault\\.\r\n0x0+ in \\?\\? \\(\\)\r\n$gdb_prompt $" {
11132 +       pass $test
11133 +    }
11134 +    -re "\r\n$gdb_prompt $" {
11135 +       untested ${testfile}.exp
11136 +       return
11137 +    }
11138 +}
11139 +
11140 +gdb_test "bt full" "\r\n +s = \"X\\\\000\"\r\n.*"
11141 diff --git a/gdb/testsuite/gdb.base/vla-overflow.c b/gdb/testsuite/gdb.base/vla-overflow.c
11142 new file mode 100644
11143 index 0000000..c5d5ee0
11144 --- /dev/null
11145 +++ b/gdb/testsuite/gdb.base/vla-overflow.c
11146 @@ -0,0 +1,30 @@
11147 +/* This testcase is part of GDB, the GNU debugger.
11148 +
11149 +   Copyright 2008 Free Software Foundation, Inc.
11150 +
11151 +   This program is free software; you can redistribute it and/or modify
11152 +   it under the terms of the GNU General Public License as published by
11153 +   the Free Software Foundation; either version 3 of the License, or
11154 +   (at your option) any later version.
11155 +
11156 +   This program is distributed in the hope that it will be useful,
11157 +   but WITHOUT ANY WARRANTY; without even the implied warranty of
11158 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11159 +   GNU General Public License for more details.
11160 +
11161 +   You should have received a copy of the GNU General Public License
11162 +   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
11163 +
11164 +#include <stdlib.h>
11165 +
11166 +int
11167 +main (int argc, char **argv)
11168 +{
11169 +  int array[argc];
11170 +
11171 +  array[0] = array[0];
11172 +
11173 +  abort ();
11174 +
11175 +  return 0;
11176 +}
11177 diff --git a/gdb/testsuite/gdb.base/vla-overflow.exp b/gdb/testsuite/gdb.base/vla-overflow.exp
11178 new file mode 100644
11179 index 0000000..24a608f
11180 --- /dev/null
11181 +++ b/gdb/testsuite/gdb.base/vla-overflow.exp
11182 @@ -0,0 +1,109 @@
11183 +# Copyright 2008 Free Software Foundation, Inc.
11184 +
11185 +# This program is free software; you can redistribute it and/or modify
11186 +# it under the terms of the GNU General Public License as published by
11187 +# the Free Software Foundation; either version 3 of the License, or
11188 +# (at your option) any later version.
11189 +#
11190 +# This program is distributed in the hope that it will be useful,
11191 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
11192 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11193 +# GNU General Public License for more details.
11194 +#
11195 +# You should have received a copy of the GNU General Public License
11196 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
11197 +
11198 +# We could crash in:
11199 +# #0  block_linkage_function (bl=0x0) at ../../gdb/block.c:69
11200 +# #1  in dwarf_block_get_frame_base (...) at ../../gdb/dwarf2block.c:97
11201 +#   97   framefunc = block_linkage_function (get_frame_block (frame, NULL));
11202 +# #2  in execute_stack_op (...) at ../../gdb/dwarf2expr.c:496
11203 +# #3  in dwarf_block_exec_core () at ../../gdb/dwarf2block.c:156
11204 +# #4  dwarf_block_exec (...) at ../../gdb/dwarf2block.c:206
11205 +# #5  in range_type_count_bound_internal (...) at ../../gdb/gdbtypes.c:1430
11206 +# #6  in create_array_type (...) at ../../gdb/gdbtypes.c:840
11207 +# ...
11208 +# #21 in psymtab_to_symtab (...) at ../../gdb/symfile.c:292
11209 +# ...
11210 +# #29 in backtrace_command_1 () at ../../gdb/stack.c:1273
11211 +
11212 +set testfile vla-overflow
11213 +set shfile ${objdir}/${subdir}/${testfile}-gdb.sh
11214 +set srcfile ${testfile}.c
11215 +set binfile ${objdir}/${subdir}/${testfile}
11216 +if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
11217 +    untested "Couldn't compile test program"
11218 +    return -1
11219 +}
11220 +
11221 +set f [open "|getconf PAGESIZE" "r"]
11222 +gets $f pagesize
11223 +close $f
11224 +
11225 +gdb_exit
11226 +gdb_start
11227 +gdb_reinitialize_dir $srcdir/$subdir
11228 +gdb_load ${binfile}
11229 +
11230 +set pid_of_gdb [exp_pid -i [board_info host fileid]]
11231 +
11232 +if { [runto_main] < 0 } {
11233 +    untested vla-overflow
11234 +    return -1
11235 +}
11236 +
11237 +# Get the GDB memory size when we stay at main.
11238 +
11239 +proc memory_v_pages_get {} {
11240 +    global pid_of_gdb pagesize
11241 +    set fd [open "/proc/$pid_of_gdb/statm"]
11242 +    gets $fd line
11243 +    close $fd
11244 +    # number of pages of virtual memory
11245 +    scan $line "%d" drs
11246 +    return $drs
11247 +}
11248 +
11249 +set pages_found [memory_v_pages_get]
11250 +
11251 +# s390x with glibc-debuginfo.s390x installed used approx. 16MB.
11252 +set mb_reserve 40
11253 +verbose -log "pages_found = $pages_found, mb_reserve = $mb_reserve"
11254 +set kb_found [expr $pages_found * $pagesize / 1024]
11255 +set kb_permit [expr $kb_found + 1 * 1024 + $mb_reserve * 1024]
11256 +verbose -log "kb_found = $kb_found, kb_permit = $kb_permit"
11257 +
11258 +# Create the ulimit wrapper.
11259 +set f [open $shfile "w"]
11260 +puts $f "#! /bin/sh"
11261 +puts $f "ulimit -v $kb_permit"
11262 +puts $f "exec $GDB \"\$@\""
11263 +close $f
11264 +remote_exec host "chmod +x $shfile"
11265 +
11266 +gdb_exit
11267 +set GDBold $GDB
11268 +set GDB "$shfile"
11269 +gdb_start
11270 +set GDB $GDBold
11271 +
11272 +gdb_reinitialize_dir $srcdir/$subdir
11273 +gdb_load ${binfile}
11274 +
11275 +set pid_of_gdb [exp_pid -i [board_info host fileid]]
11276 +
11277 +# Check the size again after the second run.
11278 +# We must not stop in main as it would cache `array' and never crash later.
11279 +
11280 +gdb_run_cmd
11281 +
11282 +verbose -log "kb_found before abort() = [expr [memory_v_pages_get] * $pagesize / 1024]"
11283 +
11284 +gdb_test "" "Program received signal SIGABRT, Aborted..*" "Enter abort()"
11285 +
11286 +verbose -log "kb_found in abort() = [expr [memory_v_pages_get] * $pagesize / 1024]"
11287 +
11288 +# `abort' can get expressed as `*__GI_abort'.
11289 +gdb_test "bt" "in \[^ \]*abort \\(.* in main \\(.*" "Backtrace after abort()"
11290 +
11291 +verbose -log "kb_found in bt after abort() = [expr [memory_v_pages_get] * $pagesize / 1024]"
11292 diff --git a/gdb/testsuite/gdb.base/vla.c b/gdb/testsuite/gdb.base/vla.c
11293 new file mode 100644
11294 index 0000000..e1f3ed1
11295 --- /dev/null
11296 +++ b/gdb/testsuite/gdb.base/vla.c
11297 @@ -0,0 +1,55 @@
11298 +/* This testcase is part of GDB, the GNU debugger.
11299 +
11300 +   Copyright 2008 Free Software Foundation, Inc.
11301 +
11302 +   This program is free software; you can redistribute it and/or modify
11303 +   it under the terms of the GNU General Public License as published by
11304 +   the Free Software Foundation; either version 3 of the License, or
11305 +   (at your option) any later version.
11306 +
11307 +   This program is distributed in the hope that it will be useful,
11308 +   but WITHOUT ANY WARRANTY; without even the implied warranty of
11309 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11310 +   GNU General Public License for more details.
11311 +
11312 +   You should have received a copy of the GNU General Public License
11313 +   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
11314 +
11315 +#include <string.h>
11316 +
11317 +void
11318 +marker (void)
11319 +{
11320 +}
11321 +
11322 +void
11323 +bar (char *a, char *b, char *c, int size)
11324 +{
11325 +  memset (a, '1', size);
11326 +  memset (b, '2', size);
11327 +  memset (c, '3', 48);
11328 +}
11329 +
11330 +void
11331 +foo (int size)
11332 +{
11333 +  char temp1[size];
11334 +  char temp3[48];
11335 +
11336 +  temp1[size - 1] = '\0';
11337 +  {
11338 +    char temp2[size];
11339 +
11340 +    bar (temp1, temp2, temp3, size);
11341 +
11342 +    marker (); /* break-here */
11343 +  }
11344 +}
11345 +
11346 +int
11347 +main (void)
11348 +{
11349 +  foo (26);
11350 +  foo (78);
11351 +  return 0;
11352 +}
11353 diff --git a/gdb/testsuite/gdb.base/vla.exp b/gdb/testsuite/gdb.base/vla.exp
11354 new file mode 100644
11355 index 0000000..5da7378
11356 --- /dev/null
11357 +++ b/gdb/testsuite/gdb.base/vla.exp
11358 @@ -0,0 +1,62 @@
11359 +# Copyright 2008 Free Software Foundation, Inc.
11360 +
11361 +# This program is free software; you can redistribute it and/or modify
11362 +# it under the terms of the GNU General Public License as published by
11363 +# the Free Software Foundation; either version 3 of the License, or
11364 +# (at your option) any later version.
11365 +#
11366 +# This program is distributed in the hope that it will be useful,
11367 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
11368 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11369 +# GNU General Public License for more details.
11370 +#
11371 +# You should have received a copy of the GNU General Public License
11372 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
11373 +
11374 +set testfile vla
11375 +set srcfile ${testfile}.c
11376 +set binfile ${objdir}/${subdir}/${testfile}
11377 +if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
11378 +    untested "Couldn't compile test program"
11379 +    return -1
11380 +}
11381 +
11382 +gdb_exit
11383 +gdb_start
11384 +gdb_reinitialize_dir $srcdir/$subdir
11385 +gdb_load ${binfile}
11386 +
11387 +if ![runto_main] {
11388 +    untested vla
11389 +    return -1
11390 +}
11391 +
11392 +gdb_breakpoint [gdb_get_line_number "break-here"]
11393 +
11394 +gdb_continue_to_breakpoint "break-here"
11395 +
11396 +gdb_test "whatis temp1" "type = char \\\[variable\\\]" "first: whatis temp1"
11397 +gdb_test "whatis temp2" "type = char \\\[variable\\\]" "first: whatis temp2"
11398 +gdb_test "whatis temp3" "type = char \\\[48\\\]" "first: whatis temp3"
11399 +
11400 +gdb_test "ptype temp1" "type = char \\\[26\\\]" "first: ptype temp1"
11401 +gdb_test "ptype temp2" "type = char \\\[26\\\]" "first: ptype temp2"
11402 +gdb_test "ptype temp3" "type = char \\\[48\\\]" "first: ptype temp3"
11403 +
11404 +gdb_test "p temp1" " = '1' <repeats 26 times>" "first: print temp1"
11405 +gdb_test "p temp2" " = '2' <repeats 26 times>" "first: print temp2"
11406 +gdb_test "p temp3" " = '3' <repeats 48 times>" "first: print temp3"
11407 +
11408 +gdb_continue_to_breakpoint "break-here"
11409 +
11410 +gdb_test "whatis temp1" "type = char \\\[variable\\\]" "second: whatis temp1"
11411 +gdb_test "whatis temp2" "type = char \\\[variable\\\]" "second: whatis temp2"
11412 +gdb_test "whatis temp3" "type = char \\\[48\\\]" "second: whatis temp3"
11413 +
11414 +gdb_test "ptype temp1" "type = char \\\[78\\\]" "second: ptype temp1"
11415 +gdb_test "ptype temp2" "type = char \\\[78\\\]" "second: ptype temp2"
11416 +gdb_test "ptype temp3" "type = char \\\[48\\\]" "second: ptype temp3"
11417 +
11418 +gdb_test "p temp1" " = '1' <repeats 78 times>" "second: print temp1"
11419 +gdb_test "p temp2" " = '2' <repeats 78 times>" "second: print temp2"
11420 +gdb_test "p temp3" " = '3' <repeats 48 times>" "second: print temp3"
11421 diff --git a/gdb/testsuite/gdb.dwarf2/dw2-bound-loclist.S b/gdb/testsuite/gdb.dwarf2/dw2-bound-loclist.S
11422 new file mode 100644
11423 index 0000000..aac3baa
11424 --- /dev/null
11425 +++ b/gdb/testsuite/gdb.dwarf2/dw2-bound-loclist.S
11426 @@ -0,0 +1,246 @@
11427 +/* This testcase is part of GDB, the GNU debugger.
11428 +
11429 +   Copyright 2010 Free Software Foundation, Inc.
11430 +
11431 +   This program is free software; you can redistribute it and/or modify
11432 +   it under the terms of the GNU General Public License as published by
11433 +   the Free Software Foundation; either version 3 of the License, or
11434 +   (at your option) any later version.
11435 +
11436 +   This program is distributed in the hope that it will be useful,
11437 +   but WITHOUT ANY WARRANTY; without even the implied warranty of
11438 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11439 +   GNU General Public License for more details.
11440 +
11441 +   You should have received a copy of the GNU General Public License
11442 +   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
11443 +
11444 +/* Debug information */
11445 +
11446 +/* We will `break *main' at the very first instruction.  */
11447 +#define main_length 1
11448 +
11449 +       .section        .data
11450 +vardata:
11451 +       /* See DW_OP_lit3 + 1 (0-based).  */
11452 +       .string         "seennotseen"
11453 +
11454 +       .section        .debug_info
11455 +.Lcu1_begin:
11456 +       .4byte          .Lcu1_end - .Lcu1_start /* Length of Compilation Unit */
11457 +.Lcu1_start:
11458 +       .2byte          2                       /* DWARF version number */
11459 +       .4byte          .Ldebug_abbrev0         /* Offset Into Abbrev. Section */
11460 +       .byte           4                       /* Pointer Size (in bytes) */
11461 +
11462 +       /* CU die */
11463 +       .uleb128        1                       /* Abbrev: DW_TAG_compile_unit */
11464 +       .4byte          .Lproducer              /* DW_AT_producer */
11465 +       /* Use C++ to exploit a bug in parsing DW_AT_name "".  */
11466 +       .byte           4                       /* DW_AT_language (C++) -  */
11467 +       .4byte          main                    /* DW_AT_low_pc */
11468 +       .byte           main_length             /* DW_AT_high_pc */
11469 +
11470 +.Larray_type:
11471 +       .uleb128        2                       /* Abbrev: DW_TAG_array_type */
11472 +       .4byte          .Lchar_type-.Lcu1_begin /* DW_AT_type */
11473 +
11474 +       .uleb128        3                       /* Abbrev: DW_TAG_subrange_type */
11475 +       .4byte          .Luint_type-.Lcu1_begin /* DW_AT_type */
11476 +       .byte           0                       /* DW_AT_lower_bound */
11477 +       .4byte          .Llen_var-.Lcu1_begin   /* DW_AT_upper_bound */
11478 +       .byte           0                       /* End of children of die */
11479 +
11480 +       /* DW_AT_upper_bound is referencing an optimized-out variable.  */
11481 +.Larrayb_type:
11482 +       .uleb128        2                       /* Abbrev: DW_TAG_array_type */
11483 +       .4byte          .Lchar_type-.Lcu1_begin /* DW_AT_type */
11484 +
11485 +       .uleb128        3                       /* Abbrev: DW_TAG_subrange_type */
11486 +       .4byte          .Luint_type-.Lcu1_begin /* DW_AT_type */
11487 +       .byte           0                       /* DW_AT_lower_bound */
11488 +       .4byte          .Llenb_var-.Lcu1_begin  /* DW_AT_upper_bound */
11489 +       .byte           0                       /* End of children of die */
11490 +
11491 +       /* DW_AT_upper_bound is referencing register.  */
11492 +.Larrayreg_type:
11493 +       .uleb128        2                       /* Abbrev: DW_TAG_array_type */
11494 +       .4byte          .Lchar_type-.Lcu1_begin /* DW_AT_type */
11495 +
11496 +       .uleb128        8                       /* Abbrev: DW_TAG_subrange_type with block */
11497 +       .4byte          .Luint_type-.Lcu1_begin /* DW_AT_type */
11498 +       .byte           0                       /* DW_AT_lower_bound */
11499 +       .byte           2f - 1f                 /* DW_AT_upper_bound */
11500 +1:     .byte           0x50                    /* DW_OP_reg0 */
11501 +2:
11502 +       .byte           0                       /* End of children of die */
11503 +
11504 +.Luint_type:
11505 +       .uleb128        4                       /* Abbrev: DW_TAG_base_type */
11506 +       .4byte          .Luint_str              /* DW_AT_name */
11507 +       .byte           4                       /* DW_AT_byte_size */
11508 +       .byte           7                       /* DW_AT_encoding */
11509 +
11510 +.Lchar_type:
11511 +       .uleb128        4                       /* Abbrev: DW_TAG_base_type */
11512 +       .4byte          .Lchar_str              /* DW_AT_name */
11513 +       .byte           1                       /* DW_AT_byte_size */
11514 +       .byte           6                       /* DW_AT_encoding */
11515 +
11516 +.Llen_var:
11517 +       .uleb128        5                       /* Abbrev: DW_TAG_variable artificial */
11518 +       .byte           1                       /* DW_AT_artificial */
11519 +       .4byte          .Luint_type-.Lcu1_begin /* DW_AT_type */
11520 +       .4byte          .Llen_loclist-.Lloclist /* DW_AT_location */
11521 +
11522 +       /* optimized-out variable for b_string.  */
11523 +.Llenb_var:
11524 +       .uleb128        7                       /* Abbrev: DW_TAG_variable artificial no DW_AT_location */
11525 +       .byte           1                       /* DW_AT_artificial */
11526 +       .4byte          .Luint_type-.Lcu1_begin /* DW_AT_type */
11527 +
11528 +       .uleb128        6                       /* Abbrev: DW_TAG_variable DW_FORM_string */
11529 +       .string         "a_string"              /* DW_AT_name */
11530 +       .4byte          .Larray_type-.Lcu1_begin /* DW_AT_type */
11531 +       .byte           2f - 1f                 /* DW_AT_location */
11532 +1:     .byte           3                       /*   DW_OP_addr */
11533 +       .4byte          vardata                 /*   <addr> */
11534 +2:
11535 +
11536 +       /* DW_AT_upper_bound is referencing an optimized-out variable.  */
11537 +       .uleb128        6                       /* Abbrev: DW_TAG_variable DW_FORM_string */
11538 +       .string         "b_string"              /* DW_AT_name */
11539 +       .4byte          .Larrayb_type-.Lcu1_begin /* DW_AT_type */
11540 +       .byte           2f - 1f                 /* DW_AT_location */
11541 +1:     .byte           3                       /*   DW_OP_addr */
11542 +       .4byte          vardata                 /*   <addr> */
11543 +2:
11544 +
11545 +       /* DW_AT_upper_bound is referencing register.  */
11546 +       .uleb128        6                       /* Abbrev: DW_TAG_variable DW_FORM_string */
11547 +       .string         "reg_string"            /* DW_AT_name */
11548 +       .4byte          .Larrayreg_type-.Lcu1_begin /* DW_AT_type */
11549 +       .byte           2f - 1f                 /* DW_AT_location */
11550 +1:     .byte           3                       /*   DW_OP_addr */
11551 +       .4byte          vardata                 /*   <addr> */
11552 +2:
11553 +
11554 +       .byte           0                       /* End of children of CU */
11555 +.Lcu1_end:
11556 +
11557 +       .section        .debug_loc
11558 +.Lloclist:
11559 +.Llen_loclist:
11560 +       .4byte  0                       # Location list begin address
11561 +       .4byte  main_length             # Location list end address
11562 +       .value  2f-1f   # Location expression size
11563 +1:     .byte   0x33    # DW_OP_lit3
11564 +       .byte   0x9f    # DW_OP_stack_value
11565 +2:
11566 +       .quad   0x0     # Location list terminator begin (*.LLST2)
11567 +       .quad   0x0     # Location list terminator end (*.LLST2)
11568 +
11569 +       .section .debug_abbrev
11570 +.Ldebug_abbrev0:
11571 +       .uleb128        1                       /* Abbrev code */
11572 +       .uleb128        0x11                    /* DW_TAG_compile_unit */
11573 +       .byte           0x1                     /* has_children */
11574 +       .uleb128        0x25                    /* DW_AT_producer */
11575 +       .uleb128        0xe                     /* DW_FORM_strp */
11576 +       .uleb128        0x13                    /* DW_AT_language */
11577 +       .uleb128        0xb                     /* DW_FORM_data1 */
11578 +       .uleb128        0x11                    /* DW_AT_low_pc */
11579 +       .uleb128        0x1                     /* DW_FORM_addr */
11580 +       .uleb128        0x12                    /* DW_AT_high_pc */
11581 +       .uleb128        0xb                     /* DW_FORM_data1 */
11582 +       .byte           0x0                     /* Terminator */
11583 +       .byte           0x0                     /* Terminator */
11584 +
11585 +       .uleb128        2                       /* Abbrev code */
11586 +       .uleb128        0x1                     /* TAG: DW_TAG_array_type */
11587 +       .byte           0x1                     /* DW_children_yes */
11588 +       .uleb128        0x49                    /* DW_AT_type */
11589 +       .uleb128        0x13                    /* DW_FORM_ref4 */
11590 +       .byte           0x0                     /* Terminator */
11591 +       .byte           0x0                     /* Terminator */
11592 +
11593 +       .uleb128        3                       /* Abbrev code */
11594 +       .uleb128        0x21                    /* DW_TAG_subrange_type */
11595 +       .byte           0x0                     /* no children */
11596 +       .uleb128        0x49                    /* DW_AT_type */
11597 +       .uleb128        0x13                    /* DW_FORM_ref4 */
11598 +       .uleb128        0x22                    /* DW_AT_lower_bound */
11599 +       .uleb128        0xb                     /* DW_FORM_data1 */
11600 +       .uleb128        0x2f                    /* DW_AT_upper_bound */
11601 +       .uleb128        0x13                    /* DW_FORM_ref4 */
11602 +       .byte           0x0                     /* Terminator */
11603 +       .byte           0x0                     /* Terminator */
11604 +
11605 +       .uleb128        4                       /* Abbrev code */
11606 +       .uleb128        0x24                    /* DW_TAG_base_type */
11607 +       .byte           0x0                     /* no_children */
11608 +       .uleb128        0x3                     /* DW_AT_name */
11609 +       .uleb128        0xe                     /* DW_FORM_strp */
11610 +       .uleb128        0xb                     /* DW_AT_byte_size */
11611 +       .uleb128        0xb                     /* DW_FORM_data1 */
11612 +       .uleb128        0x3e                    /* DW_AT_encoding */
11613 +       .uleb128        0xb                     /* DW_FORM_data1 */
11614 +       .byte           0x0                     /* Terminator */
11615 +       .byte           0x0                     /* Terminator */
11616 +
11617 +       .uleb128        5                       /* Abbrev code */
11618 +       .uleb128        0x34                    /* DW_TAG_variable */
11619 +       .byte           0x0                     /* no_children */
11620 +       .uleb128        0x34                    /* DW_AT_artificial */
11621 +       .uleb128        0x0c                    /* DW_FORM_flag */
11622 +       .uleb128        0x49                    /* DW_AT_type */
11623 +       .uleb128        0x13                    /* DW_FORM_ref4 */
11624 +       .uleb128        0x02                    /* DW_AT_location */
11625 +       .uleb128        0x06                    /* DW_FORM_data4 */
11626 +       .byte           0x0                     /* Terminator */
11627 +       .byte           0x0                     /* Terminator */
11628 +
11629 +       .uleb128        6                       /* Abbrev code */
11630 +       .uleb128        0x34                    /* DW_TAG_variable */
11631 +       .byte           0x0                     /* no_children */
11632 +       .uleb128        0x3                     /* DW_AT_name */
11633 +       .uleb128        0x8                     /* DW_FORM_string */
11634 +       .uleb128        0x49                    /* DW_AT_type */
11635 +       .uleb128        0x13                    /* DW_FORM_ref4 */
11636 +       .uleb128        0x2                     /* DW_AT_location */
11637 +       .uleb128        0xa                     /* DW_FORM_block1 */
11638 +       .byte           0x0                     /* Terminator */
11639 +       .byte           0x0                     /* Terminator */
11640 +
11641 +       .uleb128        7                       /* Abbrev code */
11642 +       .uleb128        0x34                    /* DW_TAG_variable */
11643 +       .byte           0x0                     /* no_children */
11644 +       .uleb128        0x34                    /* DW_AT_artificial */
11645 +       .uleb128        0x0c                    /* DW_FORM_flag */
11646 +       .uleb128        0x49                    /* DW_AT_type */
11647 +       .uleb128        0x13                    /* DW_FORM_ref4 */
11648 +       .byte           0x0                     /* Terminator */
11649 +       .byte           0x0                     /* Terminator */
11650 +
11651 +       .uleb128        8                       /* Abbrev code */
11652 +       .uleb128        0x21                    /* DW_TAG_subrange_type with block */
11653 +       .byte           0x0                     /* no children */
11654 +       .uleb128        0x49                    /* DW_AT_type */
11655 +       .uleb128        0x13                    /* DW_FORM_ref4 */
11656 +       .uleb128        0x22                    /* DW_AT_lower_bound */
11657 +       .uleb128        0xb                     /* DW_FORM_data1 */
11658 +       .uleb128        0x2f                    /* DW_AT_upper_bound */
11659 +       .uleb128        0xa                     /* DW_FORM_block1 */
11660 +       .byte           0x0                     /* Terminator */
11661 +       .byte           0x0                     /* Terminator */
11662 +
11663 +       .byte           0x0                     /* Terminator */
11664 +
11665 +/* String table */
11666 +       .section .debug_str
11667 +.Lproducer:
11668 +       .string         "GNU C 3.3.3"
11669 +.Lchar_str:
11670 +       .string         "char"
11671 +.Luint_str:
11672 +       .string         "unsigned int"
11673 diff --git a/gdb/testsuite/gdb.dwarf2/dw2-bound-loclist.exp b/gdb/testsuite/gdb.dwarf2/dw2-bound-loclist.exp
11674 new file mode 100644
11675 index 0000000..815ed93
11676 --- /dev/null
11677 +++ b/gdb/testsuite/gdb.dwarf2/dw2-bound-loclist.exp
11678 @@ -0,0 +1,54 @@
11679 +# Copyright 2010 Free Software Foundation, Inc.
11680 +
11681 +# This program is free software; you can redistribute it and/or modify
11682 +# it under the terms of the GNU General Public License as published by
11683 +# the Free Software Foundation; either version 3 of the License, or
11684 +# (at your option) any later version.
11685 +#
11686 +# This program is distributed in the hope that it will be useful,
11687 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
11688 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11689 +# GNU General Public License for more details.
11690 +#
11691 +# You should have received a copy of the GNU General Public License
11692 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
11693 +
11694 +# Test printing variable with dynamic bounds which reference a different
11695 +# (artificial in the GCC case) variable containing loclist as its location.
11696 +# This testcase uses value (not address) of the referenced variable:
11697 +# http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43762
11698 +
11699 +# This test can only be run on targets which support DWARF-2 and use gas.
11700 +# For now pick a sampling of likely targets.
11701 +if {![istarget *-*-linux*]
11702 +    && ![istarget *-*-gnu*]
11703 +    && ![istarget *-*-elf*]
11704 +    && ![istarget *-*-openbsd*]
11705 +    && ![istarget arm-*-eabi*]
11706 +    && ![istarget powerpc-*-eabi*]} {
11707 +    return 0
11708 +}
11709 +
11710 +set testfile dw2-bound-loclist
11711 +if { [prepare_for_testing ${testfile}.exp ${testfile} [list ${testfile}.S main.c] {}] } {
11712 +    return -1
11713 +}
11714 +
11715 +# Verify it behaves at least as an unbound array without inferior.
11716 +
11717 +gdb_test "p a_string" { = 0x[0-9a-f]+ "seennotseen"}
11718 +gdb_test "ptype a_string" {type = char \[\]}
11719 +
11720 +# Not runto_main as dw2-bound-loclist.S handles only the first byte of main.
11721 +if ![runto "*main"] {
11722 +    return -1
11723 +}
11724 +
11725 +gdb_test "p a_string" { = "seen"}
11726 +gdb_test "ptype a_string" {type = char \[4\]}
11727 +
11728 +gdb_test "p b_string" { = (0x[0-9a-f]+ )?"seennotseen"}
11729 +gdb_test "ptype b_string" {type = char \[\]}
11730 +
11731 +# The register contains unpredictable value - the array size.
11732 +gdb_test "ptype reg_string" {type = char \[-?[0-9]+\]}
11733 diff --git a/gdb/testsuite/gdb.dwarf2/dw2-stripped.c b/gdb/testsuite/gdb.dwarf2/dw2-stripped.c
11734 new file mode 100644
11735 index 0000000..1f02d90
11736 --- /dev/null
11737 +++ b/gdb/testsuite/gdb.dwarf2/dw2-stripped.c
11738 @@ -0,0 +1,42 @@
11739 +/* This testcase is part of GDB, the GNU debugger.
11740 +
11741 +   Copyright 2004 Free Software Foundation, Inc.
11742 +
11743 +   This program is free software; you can redistribute it and/or modify
11744 +   it under the terms of the GNU General Public License as published by
11745 +   the Free Software Foundation; either version 2 of the License, or
11746 +   (at your option) any later version.
11747 +
11748 +   This program is distributed in the hope that it will be useful,
11749 +   but WITHOUT ANY WARRANTY; without even the implied warranty of
11750 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11751 +   GNU General Public License for more details.
11752
11753 +   You should have received a copy of the GNU General Public License
11754 +   along with this program; if not, write to the Free Software
11755 +   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
11756 +   USA.  */
11757 +
11758 +
11759 +/* The function `func1' traced into must have debug info on offset > 0;
11760 +   (DW_UNSND (attr)).  This is the reason of `func0' existence.  */
11761 +
11762 +void
11763 +func0(int a, int b)
11764 +{
11765 +}
11766 +
11767 +/* `func1' being traced into must have some arguments to dump.  */
11768 +
11769 +void
11770 +func1(int a, int b)
11771 +{
11772 +  func0 (a,b);
11773 +}
11774 +
11775 +int
11776 +main(void)
11777 +{
11778 +  func1 (1, 2);
11779 +  return 0;
11780 +}
11781 diff --git a/gdb/testsuite/gdb.dwarf2/dw2-stripped.exp b/gdb/testsuite/gdb.dwarf2/dw2-stripped.exp
11782 new file mode 100644
11783 index 0000000..1c6e84a
11784 --- /dev/null
11785 +++ b/gdb/testsuite/gdb.dwarf2/dw2-stripped.exp
11786 @@ -0,0 +1,79 @@
11787 +# Copyright 2006 Free Software Foundation, Inc.
11788 +
11789 +# This program is free software; you can redistribute it and/or modify
11790 +# it under the terms of the GNU General Public License as published by
11791 +# the Free Software Foundation; either version 2 of the License, or
11792 +# (at your option) any later version.
11793 +# 
11794 +# This program is distributed in the hope that it will be useful,
11795 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
11796 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11797 +# GNU General Public License for more details.
11798 +# 
11799 +# You should have received a copy of the GNU General Public License
11800 +# along with this program; if not, write to the Free Software
11801 +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
11802 +
11803 +# Minimal DWARF-2 unit test
11804 +
11805 +# This test can only be run on targets which support DWARF-2.
11806 +# For now pick a sampling of likely targets.
11807 +if {![istarget *-*-linux*]
11808 +    && ![istarget *-*-gnu*]
11809 +    && ![istarget *-*-elf*]
11810 +    && ![istarget *-*-openbsd*]
11811 +    && ![istarget arm-*-eabi*]
11812 +    && ![istarget powerpc-*-eabi*]} {
11813 +    return 0  
11814 +}
11815 +
11816 +set testfile "dw2-stripped"
11817 +set srcfile ${testfile}.c
11818 +set binfile ${objdir}/${subdir}/${testfile}.x
11819 +
11820 +remote_exec build "rm -f ${binfile}"
11821 +
11822 +# get the value of gcc_compiled
11823 +if [get_compiler_info ${binfile}] {
11824 +    return -1
11825 +}
11826 +
11827 +# This test can only be run on gcc as we use additional_flags=FIXME
11828 +if {$gcc_compiled == 0} {
11829 +    return 0
11830 +}
11831 +
11832 +if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug additional_flags=-ggdb3}] != "" } {
11833 +    return -1
11834 +}
11835 +
11836 +remote_exec build "objcopy -R .debug_loc ${binfile}"
11837 +set strip_output [remote_exec build "objdump -h ${binfile}"]
11838 +
11839 +set test "stripping test file preservation"
11840 +if [ regexp ".debug_info " $strip_output]  {
11841 +    pass "$test (.debug_info preserved)"
11842 +} else {
11843 +    fail "$test (.debug_info got also stripped)"
11844 +}
11845 +
11846 +set test "stripping test file functionality"
11847 +if [ regexp ".debug_loc " $strip_output]  {
11848 +    fail "$test (.debug_loc still present)"
11849 +} else {
11850 +    pass "$test (.debug_loc stripped)"
11851 +}
11852 +
11853 +gdb_exit
11854 +gdb_start
11855 +gdb_reinitialize_dir $srcdir/$subdir
11856 +gdb_load ${binfile}
11857 +
11858 +# For C programs, "start" should stop in main().
11859 +
11860 +gdb_test "start" \
11861 +         ".*main \\(\\) at .*" \
11862 +         "start"
11863 +gdb_test "step" \
11864 +         "func.* \\(.*\\) at .*" \
11865 +         "step"
11866 diff --git a/gdb/testsuite/gdb.dwarf2/dw2-struct-member-data-location.S b/gdb/testsuite/gdb.dwarf2/dw2-struct-member-data-location.S
11867 new file mode 100644
11868 index 0000000..5fcdd84
11869 --- /dev/null
11870 +++ b/gdb/testsuite/gdb.dwarf2/dw2-struct-member-data-location.S
11871 @@ -0,0 +1,83 @@
11872 +/* This testcase is part of GDB, the GNU debugger.
11873 +
11874 +   Copyright 2009 Free Software Foundation, Inc.
11875 +
11876 +   This program is free software; you can redistribute it and/or modify
11877 +   it under the terms of the GNU General Public License as published by
11878 +   the Free Software Foundation; either version 3 of the License, or
11879 +   (at your option) any later version.
11880 +
11881 +   This program is distributed in the hope that it will be useful,
11882 +   but WITHOUT ANY WARRANTY; without even the implied warranty of
11883 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11884 +   GNU General Public License for more details.
11885 +
11886 +   You should have received a copy of the GNU General Public License
11887 +   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
11888 +
11889 +/* Debug information */
11890 +
11891 +       .section .debug_info
11892 +.Lcu1_begin:
11893 +       /* CU header */
11894 +       .4byte  .Lcu1_end - .Lcu1_start         /* Length of Compilation Unit */
11895 +.Lcu1_start:
11896 +       .2byte  2                               /* DWARF Version */
11897 +       .4byte  .Labbrev1_begin                 /* Offset into abbrev section */
11898 +       .byte   4                               /* Pointer size */
11899 +
11900 +       /* CU die */
11901 +       .uleb128 1                              /* Abbrev: DW_TAG_compile_unit */
11902 +       .ascii  "dw2-struct-member-data-location.c\0"   /* DW_AT_name */
11903 +       .ascii  "GNU C 4.3.2\0"                 /* DW_AT_producer */
11904 +       .byte   1                               /* DW_AT_language (C) */
11905 +
11906 +.Ltype_uchar:
11907 +       .uleb128        2                       /* Abbrev: DW_TAG_structure_type */
11908 +       .ascii          "some_struct\0"         /* DW_AT_name */
11909 +
11910 +       .uleb128        3                       /* Abbrev: DW_TAG_member */
11911 +       .ascii          "field\0"               /* DW_AT_name */
11912 +       .byte           0                       /* DW_AT_data_member_location */
11913 +
11914 +       .byte           0                       /* End of children of some_struct */
11915 +
11916 +       .byte           0                       /* End of children of CU */
11917 +
11918 +.Lcu1_end:
11919 +
11920 +/* Abbrev table */
11921 +       .section .debug_abbrev
11922 +.Labbrev1_begin:
11923 +       .uleb128        1                       /* Abbrev code */
11924 +       .uleb128        0x11                    /* DW_TAG_compile_unit */
11925 +       .byte           1                       /* has_children */
11926 +       .uleb128        0x3                     /* DW_AT_name */
11927 +       .uleb128        0x8                     /* DW_FORM_string */
11928 +       .uleb128        0x25                    /* DW_AT_producer */
11929 +       .uleb128        0x8                     /* DW_FORM_string */
11930 +       .uleb128        0x13                    /* DW_AT_language */
11931 +       .uleb128        0xb                     /* DW_FORM_data1 */
11932 +       .byte           0x0                     /* Terminator */
11933 +       .byte           0x0                     /* Terminator */
11934 +
11935 +       .uleb128        2                       /* Abbrev code */
11936 +       .uleb128        0x13                    /* DW_TAG_structure_type */
11937 +       .byte           1                       /* has_children */
11938 +       .uleb128        0x3                     /* DW_AT_name */
11939 +       .uleb128        0x8                     /* DW_FORM_string */
11940 +       .byte           0x0                     /* Terminator */
11941 +       .byte           0x0                     /* Terminator */
11942 +
11943 +       .uleb128        3                       /* Abbrev code */
11944 +       .uleb128        0x0d                    /* DW_TAG_member */
11945 +       .byte           0                       /* has_children */
11946 +       .uleb128        0x3                     /* DW_AT_name */
11947 +       .uleb128        0x8                     /* DW_FORM_string */
11948 +       .uleb128        0x38                    /* DW_AT_data_member_location */
11949 +       .uleb128        0x0b                    /* DW_FORM_data1 */
11950 +       .byte           0x0                     /* Terminator */
11951 +       .byte           0x0                     /* Terminator */
11952 +
11953 +       .byte           0x0                     /* Terminator */
11954 +       .byte           0x0                     /* Terminator */
11955 diff --git a/gdb/testsuite/gdb.dwarf2/dw2-struct-member-data-location.exp b/gdb/testsuite/gdb.dwarf2/dw2-struct-member-data-location.exp
11956 new file mode 100644
11957 index 0000000..c41151c
11958 --- /dev/null
11959 +++ b/gdb/testsuite/gdb.dwarf2/dw2-struct-member-data-location.exp
11960 @@ -0,0 +1,37 @@
11961 +# Copyright 2009 Free Software Foundation, Inc.
11962 +
11963 +# This program is free software; you can redistribute it and/or modify
11964 +# it under the terms of the GNU General Public License as published by
11965 +# the Free Software Foundation; either version 3 of the License, or
11966 +# (at your option) any later version.
11967 +#
11968 +# This program is distributed in the hope that it will be useful,
11969 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
11970 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11971 +# GNU General Public License for more details.
11972 +#
11973 +# You should have received a copy of the GNU General Public License
11974 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
11975 +
11976 +# This test can only be run on targets which support DWARF-2 and use gas.
11977 +# For now pick a sampling of likely targets.
11978 +if {![istarget *-*-linux*]
11979 +    && ![istarget *-*-gnu*]
11980 +    && ![istarget *-*-elf*]
11981 +    && ![istarget *-*-openbsd*]
11982 +    && ![istarget arm-*-eabi*]
11983 +    && ![istarget powerpc-*-eabi*]} {
11984 +    return 0  
11985 +}
11986 +
11987 +set testfile "dw2-struct-member-data-location"
11988 +set srcfile ${testfile}.S
11989 +set binfile ${testfile}.x
11990 +
11991 +if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${objdir}/${subdir}/${binfile}" object {nodebug}] != "" } {
11992 +    return -1
11993 +}
11994 +
11995 +clean_restart $binfile
11996 +
11997 +gdb_test "ptype struct some_struct" "type = struct some_struct {\[\r\n \t\]*void field;\[\r\n \t\]*}"
11998 diff --git a/gdb/testsuite/gdb.dwarf2/dw2-subrange-no-type.S b/gdb/testsuite/gdb.dwarf2/dw2-subrange-no-type.S
11999 new file mode 100644
12000 index 0000000..9dbbf3c
12001 --- /dev/null
12002 +++ b/gdb/testsuite/gdb.dwarf2/dw2-subrange-no-type.S
12003 @@ -0,0 +1,121 @@
12004 +/* This testcase is part of GDB, the GNU debugger.
12005 +
12006 +   Copyright 2012 Free Software Foundation, Inc.
12007 +
12008 +   This program is free software; you can redistribute it and/or modify
12009 +   it under the terms of the GNU General Public License as published by
12010 +   the Free Software Foundation; either version 3 of the License, or
12011 +   (at your option) any later version.
12012 +
12013 +   This program is distributed in the hope that it will be useful,
12014 +   but WITHOUT ANY WARRANTY; without even the implied warranty of
12015 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12016 +   GNU General Public License for more details.
12017 +
12018 +   You should have received a copy of the GNU General Public License
12019 +   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
12020 +
12021 +/* Debug information */
12022 +
12023 +       .section        .data
12024 +vardata:
12025 +       .rept           129
12026 +       .ascii          "x"
12027 +       .endr
12028 +       .ascii          "UNSEEN\0"
12029 +
12030 +       .section        .debug_info
12031 +.Lcu1_begin:
12032 +       .4byte          .Lcu1_end - .Lcu1_start /* Length of Compilation Unit */
12033 +.Lcu1_start:
12034 +       .2byte          2                       /* DWARF version number */
12035 +       .4byte          .Ldebug_abbrev0         /* Offset Into Abbrev. Section */
12036 +       .byte           4                       /* Pointer Size (in bytes) */
12037 +
12038 +       /* CU die */
12039 +       .uleb128        1                       /* Abbrev: DW_TAG_compile_unit */
12040 +       .ascii          "GNU C 3.3.3\0"         /* DW_AT_producer */
12041 +       .byte           2                       /* DW_AT_language (C) -  */
12042 +
12043 +.Larray_type:
12044 +       .uleb128        2                       /* Abbrev: DW_TAG_array_type */
12045 +       .4byte          .Lchar_type-.Lcu1_begin /* DW_AT_type */
12046 +
12047 +       .uleb128        8                       /* Abbrev: DW_TAG_subrange_type without DW_AT_type */
12048 +       .byte           0                       /* DW_AT_lower_bound */
12049 +       .byte           128                     /* DW_AT_upper_bound */
12050 +
12051 +       .byte           0                       /* End of children of die */
12052 +
12053 +.Lchar_type:
12054 +       .uleb128        4                       /* Abbrev: DW_TAG_base_type */
12055 +       .ascii          "char\0"                /* DW_AT_name */
12056 +       .byte           1                       /* DW_AT_byte_size */
12057 +       .byte           6                       /* DW_AT_encoding */
12058 +
12059 +       .uleb128        6                       /* Abbrev: DW_TAG_variable DW_FORM_string */
12060 +       .ascii          "notype_string\0"       /* DW_AT_name */
12061 +       .4byte          .Larray_type-.Lcu1_begin /* DW_AT_type */
12062 +       .byte           2f - 1f                 /* DW_AT_location */
12063 +1:     .byte           3                       /*   DW_OP_addr */
12064 +       .4byte          vardata                 /*   <addr> */
12065 +2:
12066 +
12067 +       .byte           0                       /* End of children of CU */
12068 +.Lcu1_end:
12069 +
12070 +       .section .debug_abbrev
12071 +.Ldebug_abbrev0:
12072 +       .uleb128        1                       /* Abbrev code */
12073 +       .uleb128        0x11                    /* DW_TAG_compile_unit */
12074 +       .byte           0x1                     /* has_children */
12075 +       .uleb128        0x25                    /* DW_AT_producer */
12076 +       .uleb128        0x8                     /* DW_FORM_string */
12077 +       .uleb128        0x13                    /* DW_AT_language */
12078 +       .uleb128        0xb                     /* DW_FORM_data1 */
12079 +       .byte           0x0                     /* Terminator */
12080 +       .byte           0x0                     /* Terminator */
12081 +
12082 +       .uleb128        2                       /* Abbrev code */
12083 +       .uleb128        0x1                     /* TAG: DW_TAG_array_type */
12084 +       .byte           0x1                     /* DW_children_yes */
12085 +       .uleb128        0x49                    /* DW_AT_type */
12086 +       .uleb128        0x13                    /* DW_FORM_ref4 */
12087 +       .byte           0x0                     /* Terminator */
12088 +       .byte           0x0                     /* Terminator */
12089 +
12090 +       .uleb128        4                       /* Abbrev code */
12091 +       .uleb128        0x24                    /* DW_TAG_base_type */
12092 +       .byte           0x0                     /* no_children */
12093 +       .uleb128        0x3                     /* DW_AT_name */
12094 +       .uleb128        0x8                     /* DW_FORM_string */
12095 +       .uleb128        0xb                     /* DW_AT_byte_size */
12096 +       .uleb128        0xb                     /* DW_FORM_data1 */
12097 +       .uleb128        0x3e                    /* DW_AT_encoding */
12098 +       .uleb128        0xb                     /* DW_FORM_data1 */
12099 +       .byte           0x0                     /* Terminator */
12100 +       .byte           0x0                     /* Terminator */
12101 +
12102 +       .uleb128        6                       /* Abbrev code */
12103 +       .uleb128        0x34                    /* DW_TAG_variable */
12104 +       .byte           0x0                     /* no_children */
12105 +       .uleb128        0x3                     /* DW_AT_name */
12106 +       .uleb128        0x8                     /* DW_FORM_string */
12107 +       .uleb128        0x49                    /* DW_AT_type */
12108 +       .uleb128        0x13                    /* DW_FORM_ref4 */
12109 +       .uleb128        0x2                     /* DW_AT_location */
12110 +       .uleb128        0xa                     /* DW_FORM_block1 */
12111 +       .byte           0x0                     /* Terminator */
12112 +       .byte           0x0                     /* Terminator */
12113 +
12114 +       .uleb128        8                       /* Abbrev code */
12115 +       .uleb128        0x21                    /* DW_TAG_subrange_type without DW_AT_type */
12116 +       .byte           0x0                     /* no children */
12117 +       .uleb128        0x22                    /* DW_AT_lower_bound */
12118 +       .uleb128        0xb                     /* DW_FORM_data1 */
12119 +       .uleb128        0x2f                    /* DW_AT_upper_bound */
12120 +       .uleb128        0xb                     /* DW_FORM_data1 */
12121 +       .byte           0x0                     /* Terminator */
12122 +       .byte           0x0                     /* Terminator */
12123 +
12124 +       .byte           0x0                     /* Terminator */
12125 diff --git a/gdb/testsuite/gdb.dwarf2/dw2-subrange-no-type.exp b/gdb/testsuite/gdb.dwarf2/dw2-subrange-no-type.exp
12126 new file mode 100644
12127 index 0000000..a13e346
12128 --- /dev/null
12129 +++ b/gdb/testsuite/gdb.dwarf2/dw2-subrange-no-type.exp
12130 @@ -0,0 +1,39 @@
12131 +# Copyright 2012 Free Software Foundation, Inc.
12132 +
12133 +# This program is free software; you can redistribute it and/or modify
12134 +# it under the terms of the GNU General Public License as published by
12135 +# the Free Software Foundation; either version 3 of the License, or
12136 +# (at your option) any later version.
12137 +#
12138 +# This program is distributed in the hope that it will be useful,
12139 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
12140 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12141 +# GNU General Public License for more details.
12142 +#
12143 +# You should have received a copy of the GNU General Public License
12144 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
12145 +load_lib dwarf.exp
12146 +
12147 +# https://bugzilla.redhat.com/show_bug.cgi?id=806920
12148 +# read_subrange_type <TYPE_CODE (base_type) == TYPE_CODE_VOID> reinitialization
12149 +# of BASE_TYPE was done too late, it affects DW_TAG_subrange_type without
12150 +# specified DW_AT_type, present only in XLF produced code.
12151 +
12152 +# This test can only be run on targets which support DWARF-2 and use gas.
12153 +if {![dwarf2_support]} {
12154 +    return 0  
12155 +}
12156 +
12157 +set testfile dw2-subrange-no-type
12158 +set srcfile ${testfile}.S
12159 +set executable ${testfile}.x
12160 +set binfile ${objdir}/${subdir}/${executable}
12161 +
12162 +if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" object {}] != "" } {
12163 +    return -1
12164 +}
12165 +
12166 +clean_restart $executable
12167 +
12168 +gdb_test "ptype notype_string" {type = char \[129\]}
12169 +gdb_test "p notype_string" " = 'x' <repeats 129 times>"
12170 diff --git a/gdb/testsuite/gdb.fortran/dwarf-stride.exp b/gdb/testsuite/gdb.fortran/dwarf-stride.exp
12171 new file mode 100644
12172 index 0000000..d7b8bea
12173 --- /dev/null
12174 +++ b/gdb/testsuite/gdb.fortran/dwarf-stride.exp
12175 @@ -0,0 +1,42 @@
12176 +# Copyright 2009 Free Software Foundation, Inc.
12177 +
12178 +# This program is free software; you can redistribute it and/or modify
12179 +# it under the terms of the GNU General Public License as published by
12180 +# the Free Software Foundation; either version 2 of the License, or
12181 +# (at your option) any later version.
12182 +# 
12183 +# This program is distributed in the hope that it will be useful,
12184 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
12185 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12186 +# GNU General Public License for more details.
12187 +# 
12188 +# You should have received a copy of the GNU General Public License
12189 +# along with this program; if not, write to the Free Software
12190 +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  
12191 +
12192 +# This file was written by Jan Kratochvil <jan.kratochvil@redhat.com>.
12193 +
12194 +# This file is part of the gdb testsuite.  Array element stride must not be
12195 +# specified in the number of elements but in a number of bytes instead.
12196 +# Original problem:
12197 +# (gdb) p c40pt(1)
12198 +# $1 = '0-hello', ' ' <repeats 33 times>
12199 +# (gdb) p c40pt(2)
12200 +# warning: Fortran array stride not divisible by the element size
12201 +
12202 +set testfile dwarf-stride
12203 +set srcfile ${testfile}.f90
12204 +
12205 +if { [prepare_for_testing ${testfile}.exp ${testfile} ${srcfile} {debug f90}] } {
12206 +    return -1
12207 +}
12208 +
12209 +if ![runto MAIN__] then {
12210 +    perror "couldn't run to breakpoint MAIN__"
12211 +    continue
12212 +}
12213 +
12214 +gdb_breakpoint [gdb_get_line_number "break-here"]
12215 +gdb_continue_to_breakpoint "break-here" ".*break-here.*"
12216 +gdb_test "p c40pt(1)" " = '0-hello.*"
12217 +gdb_test "p c40pt(2)" " = '1-hello.*"
12218 diff --git a/gdb/testsuite/gdb.fortran/dwarf-stride.f90 b/gdb/testsuite/gdb.fortran/dwarf-stride.f90
12219 new file mode 100644
12220 index 0000000..e492b3a
12221 --- /dev/null
12222 +++ b/gdb/testsuite/gdb.fortran/dwarf-stride.f90
12223 @@ -0,0 +1,40 @@
12224 +! Copyright 2009 Free Software Foundation, Inc.
12225 +!
12226 +! This program is free software; you can redistribute it and/or modify
12227 +! it under the terms of the GNU General Public License as published by
12228 +! the Free Software Foundation; either version 2 of the License, or
12229 +! (at your option) any later version.
12230 +!
12231 +! This program is distributed in the hope that it will be useful,
12232 +! but WITHOUT ANY WARRANTY; without even the implied warranty of
12233 +! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12234 +! GNU General Public License for more details.
12235 +!
12236 +! You should have received a copy of the GNU General Public License
12237 +! along with this program; if not, write to the Free Software
12238 +! Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
12239 +!
12240 +! File written by Alan Matsuoka.
12241 +
12242 +program repro
12243 +
12244 +  type small_stride
12245 +     character*40 long_string
12246 +     integer      small_pad
12247 +  end type small_stride
12248 +
12249 +  type(small_stride), dimension (20), target :: unpleasant
12250 +  character*40, pointer, dimension(:):: c40pt
12251 +
12252 +  integer i
12253 +
12254 +  do i = 0,19
12255 +     unpleasant(i+1)%small_pad = i+1
12256 +     unpleasant(i+1)%long_string = char (ichar('0') + i) // '-hello'
12257 +  end do
12258 +
12259 +  c40pt => unpleasant%long_string
12260 +
12261 +  print *, c40pt  ! break-here
12262 +
12263 +end program repro
12264 diff --git a/gdb/testsuite/gdb.fortran/dynamic-other-frame-stub.f90 b/gdb/testsuite/gdb.fortran/dynamic-other-frame-stub.f90
12265 new file mode 100644
12266 index 0000000..261ce17
12267 --- /dev/null
12268 +++ b/gdb/testsuite/gdb.fortran/dynamic-other-frame-stub.f90
12269 @@ -0,0 +1,24 @@
12270 +! Copyright 2010 Free Software Foundation, Inc.
12271 +!
12272 +! This program is free software; you can redistribute it and/or modify
12273 +! it under the terms of the GNU General Public License as published by
12274 +! the Free Software Foundation; either version 2 of the License, or
12275 +! (at your option) any later version.
12276 +!
12277 +! This program is distributed in the hope that it will be useful,
12278 +! but WITHOUT ANY WARRANTY; without even the implied warranty of
12279 +! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12280 +! GNU General Public License for more details.
12281 +!
12282 +! You should have received a copy of the GNU General Public License
12283 +! along with this program; if not, write to the Free Software
12284 +! Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
12285 +!
12286 +! Ihis file is the Fortran source file for dynamic.exp.
12287 +! Original file written by Jakub Jelinek <jakub@redhat.com>.
12288 +! Modified for the GDB testcase by Jan Kratochvil <jan.kratochvil@redhat.com>.
12289 +
12290 +subroutine bar
12291 +  real :: dummy
12292 +  dummy = 1
12293 +end subroutine bar
12294 diff --git a/gdb/testsuite/gdb.fortran/dynamic-other-frame.exp b/gdb/testsuite/gdb.fortran/dynamic-other-frame.exp
12295 new file mode 100644
12296 index 0000000..fa41b80
12297 --- /dev/null
12298 +++ b/gdb/testsuite/gdb.fortran/dynamic-other-frame.exp
12299 @@ -0,0 +1,37 @@
12300 +# Copyright 2010 Free Software Foundation, Inc.
12301 +
12302 +# This program is free software; you can redistribute it and/or modify
12303 +# it under the terms of the GNU General Public License as published by
12304 +# the Free Software Foundation; either version 2 of the License, or
12305 +# (at your option) any later version.
12306 +# 
12307 +# This program is distributed in the hope that it will be useful,
12308 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
12309 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12310 +# GNU General Public License for more details.
12311 +# 
12312 +# You should have received a copy of the GNU General Public License
12313 +# along with this program; if not, write to the Free Software
12314 +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  
12315 +
12316 +set testfile "dynamic-other-frame"
12317 +set srcfile1 ${testfile}.f90
12318 +set srcfile2 ${testfile}-stub.f90
12319 +set objfile2 ${objdir}/${subdir}/${testfile}-stub.o
12320 +set executable ${testfile}
12321 +set binfile ${objdir}/${subdir}/${executable}
12322 +
12323 +if { [gdb_compile "${srcdir}/${subdir}/${srcfile2}" "${objfile2}" object {f90}] != ""
12324 +     || [gdb_compile "${srcdir}/${subdir}/${srcfile1} ${objfile2}" "${binfile}" executable {debug f90}] != "" } {
12325 +    untested "Couldn't compile ${srcfile1} or ${srcfile2}"
12326 +    return -1
12327 +}
12328 +
12329 +clean_restart ${executable}
12330 +
12331 +if ![runto bar_] then {
12332 +    perror "couldn't run to bar_"
12333 +    continue
12334 +}
12335 +
12336 +gdb_test "bt" {foo \(string='hello'.*}
12337 diff --git a/gdb/testsuite/gdb.fortran/dynamic-other-frame.f90 b/gdb/testsuite/gdb.fortran/dynamic-other-frame.f90
12338 new file mode 100644
12339 index 0000000..2bc637d
12340 --- /dev/null
12341 +++ b/gdb/testsuite/gdb.fortran/dynamic-other-frame.f90
12342 @@ -0,0 +1,36 @@
12343 +! Copyright 2010 Free Software Foundation, Inc.
12344 +!
12345 +! This program is free software; you can redistribute it and/or modify
12346 +! it under the terms of the GNU General Public License as published by
12347 +! the Free Software Foundation; either version 2 of the License, or
12348 +! (at your option) any later version.
12349 +!
12350 +! This program is distributed in the hope that it will be useful,
12351 +! but WITHOUT ANY WARRANTY; without even the implied warranty of
12352 +! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12353 +! GNU General Public License for more details.
12354 +!
12355 +! You should have received a copy of the GNU General Public License
12356 +! along with this program; if not, write to the Free Software
12357 +! Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
12358 +!
12359 +! Ihis file is the Fortran source file for dynamic.exp.
12360 +! Original file written by Jakub Jelinek <jakub@redhat.com>.
12361 +! Modified for the GDB testcase by Jan Kratochvil <jan.kratochvil@redhat.com>.
12362 +
12363 +subroutine foo (string)
12364 +  interface
12365 +    subroutine bar
12366 +    end subroutine
12367 +  end interface
12368 +  character string*(*)
12369 +  call bar                                ! stop-here
12370 +end subroutine foo
12371 +program test
12372 +  interface
12373 +    subroutine foo (string)
12374 +    character string*(*)
12375 +    end subroutine
12376 +  end interface
12377 +  call foo ('hello')
12378 +end
12379 diff --git a/gdb/testsuite/gdb.fortran/dynamic.exp b/gdb/testsuite/gdb.fortran/dynamic.exp
12380 new file mode 100644
12381 index 0000000..e79e94a
12382 --- /dev/null
12383 +++ b/gdb/testsuite/gdb.fortran/dynamic.exp
12384 @@ -0,0 +1,152 @@
12385 +# Copyright 2007 Free Software Foundation, Inc.
12386 +
12387 +# This program is free software; you can redistribute it and/or modify
12388 +# it under the terms of the GNU General Public License as published by
12389 +# the Free Software Foundation; either version 2 of the License, or
12390 +# (at your option) any later version.
12391 +# 
12392 +# This program is distributed in the hope that it will be useful,
12393 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
12394 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12395 +# GNU General Public License for more details.
12396 +# 
12397 +# You should have received a copy of the GNU General Public License
12398 +# along with this program; if not, write to the Free Software
12399 +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  
12400 +
12401 +# This file was written by Jan Kratochvil <jan.kratochvil@redhat.com>.
12402 +
12403 +# This file is part of the gdb testsuite.  It contains tests for dynamically
12404 +# allocated Fortran arrays.
12405 +# It depends on the GCC dynamic Fortran arrays DWARF support:
12406 +#      http://gcc.gnu.org/bugzilla/show_bug.cgi?id=22244
12407 +
12408 +set testfile "dynamic"
12409 +set srcfile ${testfile}.f90
12410 +set binfile ${objdir}/${subdir}/${testfile}
12411 +
12412 +if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug f90 quiet}] != "" } {
12413 +    untested "Couldn't compile ${srcfile}"
12414 +    return -1
12415 +}
12416 +
12417 +gdb_exit
12418 +gdb_start
12419 +gdb_reinitialize_dir $srcdir/$subdir
12420 +gdb_load ${binfile}
12421 +
12422 +if ![runto MAIN__] then {
12423 +    perror "couldn't run to breakpoint MAIN__"
12424 +    continue
12425 +}
12426 +
12427 +gdb_breakpoint [gdb_get_line_number "varx-init"]
12428 +gdb_continue_to_breakpoint "varx-init"
12429 +gdb_test "p varx" "\\$\[0-9\]* = <(object|the array) is not allocated>" "p varx unallocated"
12430 +gdb_test "ptype varx" "type = <(object|the array) is not allocated>" "ptype varx unallocated"
12431 +gdb_test "p varx(1,5,17)" "(Cannot access it|Unable to access the object) because the (object|array) is not allocated\\." "p varx(1,5,17) unallocated"
12432 +gdb_test "p varx(1,5,17)=1" "(Cannot access it|Unable to access the object) because the (object|array) is not allocated\\." "p varx(1,5,17)=1 unallocated"
12433 +gdb_test "ptype varx(1,5,17)" "(Cannot access it|Unable to access the object) because the (object|array) is not allocated\\." "ptype varx(1,5,17) unallocated"
12434 +
12435 +gdb_breakpoint [gdb_get_line_number "varx-allocated"]
12436 +gdb_continue_to_breakpoint "varx-allocated"
12437 +# $1 = (( ( 0, 0, 0, 0, 0, 0) ( 0, 0, 0, 0, 0, 0) --- , 0) ) ( ( 0, 0, ...) ...) ...)
12438 +gdb_test "ptype varx" "type = real(\\(kind=4\\)|\\*4) \\(6,5:15,17:28\\)" "ptype varx allocated"
12439 +# Intel Fortran Compiler 10.1.008 uses -1 there, GCC uses 1.
12440 +gdb_test "p l" "\\$\[0-9\]* = (\\.TRUE\\.|4294967295)" "p l if varx allocated"
12441 +
12442 +gdb_breakpoint [gdb_get_line_number "varx-filled"]
12443 +gdb_continue_to_breakpoint "varx-filled"
12444 +gdb_test "p varx(2, 5, 17)" "\\$\[0-9\]* = 6"
12445 +gdb_test "p varx(1, 5, 17)" "\\$\[0-9\]* = 7"
12446 +gdb_test "p varx(2, 6, 18)" "\\$\[0-9\]* = 8"
12447 +gdb_test "p varx(6, 15, 28)" "\\$\[0-9\]* = 9"
12448 +# The latter one is for the Intel Fortran Compiler 10.1.008 pointer type.
12449 +gdb_test "p varv" "\\$\[0-9\]* = (<(object|the array) is not associated>|.*(Cannot access it|Unable to access the object) because the object is not associated.)" "p varv unassociated"
12450 +gdb_test "ptype varv" "type = (<(object|the array) is not associated>|.*(Cannot access it|Unable to access the object) because the object is not associated.)" "ptype varv unassociated"
12451 +
12452 +set test "output varx"
12453 +gdb_test_multiple $test $test {
12454 +    -re "^output varx\r\n\[() ,6789.\]*$gdb_prompt $" {
12455 +       pass $test
12456 +    }
12457 +}
12458 +
12459 +gdb_breakpoint [gdb_get_line_number "varv-associated"]
12460 +gdb_continue_to_breakpoint "varv-associated"
12461 +gdb_test "p varx(3, 7, 19)" "\\$\[0-9\]* = 6" "p varx(3, 7, 19) with varv associated"
12462 +gdb_test "p varv(3, 7, 19)" "\\$\[0-9\]* = 6" "p varv(3, 7, 19) associated"
12463 +# Intel Fortran Compiler 10.1.008 uses -1 there, GCC uses 1.
12464 +gdb_test "p l" "\\$\[0-9\]* = (\\.TRUE\\.|4294967295)" "p l if varv associated"
12465 +gdb_test "ptype varx" "type = real(\\(kind=4\\)|\\*4) \\(6,5:15,17:28\\)" "ptype varx with varv associated"
12466 +# Intel Fortran Compiler 10.1.008 uses the pointer type.
12467 +gdb_test "ptype varv" "type = (PTR TO -> \\( )?real(\\(kind=4\\)|\\*4) \\(6,5:15,17:28\\)\\)?" "ptype varv associated"
12468 +
12469 +gdb_breakpoint [gdb_get_line_number "varv-filled"]
12470 +gdb_continue_to_breakpoint "varv-filled"
12471 +gdb_test "p varx(3, 7, 19)" "\\$\[0-9\]* = 10" "p varx(3, 7, 19) with varv filled"
12472 +gdb_test "p varv(3, 7, 19)" "\\$\[0-9\]* = 10" "p varv(3, 7, 19) filled"
12473 +
12474 +gdb_breakpoint [gdb_get_line_number "varv-deassociated"]
12475 +gdb_continue_to_breakpoint "varv-deassociated"
12476 +# The latter one is for the Intel Fortran Compiler 10.1.008 pointer type.
12477 +gdb_test "p varv" "\\$\[0-9\]* = (<(object|the array) is not associated>|.*(Cannot access it|Unable to access the object) because the object is not associated.)" "p varv deassociated"
12478 +gdb_test "ptype varv" "type = (<(object|the array) is not associated>|.*(Cannot access it|Unable to access the object) because the object is not associated.)" "ptype varv deassociated"
12479 +gdb_test "p l" "\\$\[0-9\]* = \\.FALSE\\." "p l if varv deassociated"
12480 +gdb_test "p varv(1,5,17)" "(Cannot access it|Unable to access the object) because the (object|array) is not associated\\."
12481 +gdb_test "ptype varv(1,5,17)" "(Cannot access it|Unable to access the object) because the (object|array) is not associated\\."
12482 +
12483 +gdb_breakpoint [gdb_get_line_number "varx-deallocated"]
12484 +gdb_continue_to_breakpoint "varx-deallocated"
12485 +gdb_test "p varx" "\\$\[0-9\]* = <(object|the array) is not allocated>" "p varx deallocated"
12486 +gdb_test "ptype varx" "type = <(object|the array) is not allocated>" "ptype varx deallocated"
12487 +gdb_test "p l" "\\$\[0-9\]* = \\.FALSE\\." "p l if varx deallocated"
12488 +gdb_test "p varx(1,5,17)" "(Cannot access it|Unable to access the object) because the (object|array) is not allocated\\." "p varx(1,5,17) deallocated"
12489 +gdb_test "ptype varx(1,5,17)" "(Cannot access it|Unable to access the object) because the (object|array) is not allocated\\." "ptype varx(1,5,17) deallocated"
12490 +
12491 +gdb_breakpoint [gdb_get_line_number "vary-passed"]
12492 +gdb_continue_to_breakpoint "vary-passed"
12493 +# $1 = (( ( 1, 1, 1, 1, 1, 1) ( 1, 1, 1, 1, 1, 1) --- , 1) ) ( ( 1, 1, ...) ...) ...)
12494 +gdb_test "p vary" "\\$\[0-9\]* = \\(\[()1, .\]*\\)"
12495 +
12496 +gdb_breakpoint [gdb_get_line_number "vary-filled"]
12497 +gdb_continue_to_breakpoint "vary-filled"
12498 +gdb_test "ptype vary" "type = real(\\(kind=4\\)|\\*4) \\(10,10\\)"
12499 +gdb_test "p vary(1, 1)" "\\$\[0-9\]* = 8"
12500 +gdb_test "p vary(2, 2)" "\\$\[0-9\]* = 9"
12501 +gdb_test "p vary(1, 3)" "\\$\[0-9\]* = 10"
12502 +# $1 = (( ( 3, 3, 3, 3, 3, 3) ( 3, 3, 3, 3, 3, 3) --- , 3) ) ( ( 3, 3, ...) ...) ...)
12503 +gdb_test "p varw" "\\$\[0-9\]* = \\(\[()3, .\]*\\)"
12504 +
12505 +gdb_breakpoint [gdb_get_line_number "varw-almostfilled"]
12506 +gdb_continue_to_breakpoint "varw-almostfilled"
12507 +gdb_test "ptype varw" "type = real(\\(kind=4\\)|\\*4) \\(5,4,3\\)"
12508 +gdb_test "p varw(3,1,1)=1" "\\$\[0-9\]* = 1"
12509 +# $1 = (( ( 6, 5, 1, 5, 5, 5) ( 5, 5, 5, 5, 5, 5) --- , 5) ) ( ( 5, 5, ...) ...) ...)
12510 +gdb_test "p varw" "\\$\[0-9\]* = \\( *\\( *\\( *6, *5, *1,\[()5, .\]*\\)" "p varw filled"
12511 +# "up" works with GCC but other Fortran compilers may copy the values into the
12512 +# outer function only on the exit of the inner function.
12513 +# We need both variants as depending on the arch we optionally may still be
12514 +# executing the caller line or not after `finish'.
12515 +gdb_test "finish" ".*(call bar \\(y, x\\)|call foo \\(x, z\\(2:6, 4:7, 6:8\\)\\))"
12516 +gdb_test "p z(2,4,5)" "\\$\[0-9\]* = 3"
12517 +gdb_test "p z(2,4,6)" "\\$\[0-9\]* = 6"
12518 +gdb_test "p z(2,4,7)" "\\$\[0-9\]* = 5"
12519 +gdb_test "p z(4,4,6)" "\\$\[0-9\]* = 1"
12520 +
12521 +gdb_breakpoint [gdb_get_line_number "varz-almostfilled"]
12522 +gdb_continue_to_breakpoint "varz-almostfilled"
12523 +# GCC uses the pointer type here, Intel Fortran Compiler 10.1.008 does not.
12524 +gdb_test "ptype varz" "type = (PTR TO -> \\( )?real(\\(kind=4\\)|\\*4) \\(\\*\\)\\)?"
12525 +# Intel Fortran Compiler 10.1.008 has a bug here - (2:11,7:7)
12526 +# as it produces DW_AT_lower_bound == DW_AT_upper_bound == 7.
12527 +gdb_test "ptype vart" "type = (PTR TO -> \\( )?real(\\(kind=4\\)|\\*4) \\(2:11,7:\\*\\)\\)?"
12528 +gdb_test "p varz" "\\$\[0-9\]* = \\(\\)"
12529 +gdb_test "p vart" "\\$\[0-9\]* = \\(\\)"
12530 +gdb_test "p varz(3)" "\\$\[0-9\]* = 4"
12531 +# maps to foo::vary(1,1)
12532 +gdb_test "p vart(2,7)" "\\$\[0-9\]* = 8"
12533 +# maps to foo::vary(2,2)
12534 +gdb_test "p vart(3,8)" "\\$\[0-9\]* = 9"
12535 +# maps to foo::vary(1,3)
12536 +gdb_test "p vart(2,9)" "\\$\[0-9\]* = 10"
12537 diff --git a/gdb/testsuite/gdb.fortran/dynamic.f90 b/gdb/testsuite/gdb.fortran/dynamic.f90
12538 new file mode 100644
12539 index 0000000..0f43564
12540 --- /dev/null
12541 +++ b/gdb/testsuite/gdb.fortran/dynamic.f90
12542 @@ -0,0 +1,98 @@
12543 +! Copyright 2007 Free Software Foundation, Inc.
12544 +!
12545 +! This program is free software; you can redistribute it and/or modify
12546 +! it under the terms of the GNU General Public License as published by
12547 +! the Free Software Foundation; either version 2 of the License, or
12548 +! (at your option) any later version.
12549 +!
12550 +! This program is distributed in the hope that it will be useful,
12551 +! but WITHOUT ANY WARRANTY; without even the implied warranty of
12552 +! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12553 +! GNU General Public License for more details.
12554 +!
12555 +! You should have received a copy of the GNU General Public License
12556 +! along with this program; if not, write to the Free Software
12557 +! Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
12558 +!
12559 +! Ihis file is the Fortran source file for dynamic.exp.
12560 +! Original file written by Jakub Jelinek <jakub@redhat.com>.
12561 +! Modified for the GDB testcase by Jan Kratochvil <jan.kratochvil@redhat.com>.
12562 +
12563 +subroutine baz
12564 +  real, target, allocatable :: varx (:, :, :)
12565 +  real, pointer :: varv (:, :, :)
12566 +  real, target :: varu (1, 2, 3)
12567 +  logical :: l
12568 +  allocate (varx (1:6, 5:15, 17:28))            ! varx-init
12569 +  l = allocated (varx)
12570 +  varx(:, :, :) = 6                             ! varx-allocated
12571 +  varx(1, 5, 17) = 7
12572 +  varx(2, 6, 18) = 8
12573 +  varx(6, 15, 28) = 9
12574 +  varv => varx                                  ! varx-filled
12575 +  l = associated (varv)
12576 +  varv(3, 7, 19) = 10                           ! varv-associated
12577 +  varv => null ()                               ! varv-filled
12578 +  l = associated (varv)
12579 +  deallocate (varx)                             ! varv-deassociated
12580 +  l = allocated (varx)
12581 +  varu(:, :, :) = 10                            ! varx-deallocated
12582 +  allocate (varv (1:6, 5:15, 17:28))
12583 +  l = associated (varv)
12584 +  varv(:, :, :) = 6
12585 +  varv(1, 5, 17) = 7
12586 +  varv(2, 6, 18) = 8
12587 +  varv(6, 15, 28) = 9
12588 +  deallocate (varv)
12589 +  l = associated (varv)
12590 +  varv => varu
12591 +  varv(1, 1, 1) = 6
12592 +  varv(1, 2, 3) = 7
12593 +  l = associated (varv)
12594 +end subroutine baz
12595 +subroutine foo (vary, varw)
12596 +  real :: vary (:, :)
12597 +  real :: varw (:, :, :)
12598 +  vary(:, :) = 4                                ! vary-passed
12599 +  vary(1, 1) = 8
12600 +  vary(2, 2) = 9
12601 +  vary(1, 3) = 10
12602 +  varw(:, :, :) = 5                             ! vary-filled
12603 +  varw(1, 1, 1) = 6
12604 +  varw(2, 2, 2) = 7                             ! varw-almostfilled
12605 +end subroutine foo
12606 +subroutine bar (varz, vart)
12607 +  real :: varz (*)
12608 +  real :: vart (2:11, 7:*)
12609 +  varz(1:3) = 4
12610 +  varz(2) = 5                                   ! varz-almostfilled
12611 +  vart(2,7) = vart(2,7)
12612 +end subroutine bar
12613 +program test
12614 +  interface
12615 +    subroutine foo (vary, varw)
12616 +    real :: vary (:, :)
12617 +    real :: varw (:, :, :)
12618 +    end subroutine
12619 +  end interface
12620 +  interface
12621 +    subroutine bar (varz, vart)
12622 +    real :: varz (*)
12623 +    real :: vart (2:11, 7:*)
12624 +    end subroutine
12625 +  end interface
12626 +  real :: x (10, 10), y (5), z(8, 8, 8)
12627 +  x(:,:) = 1
12628 +  y(:) = 2
12629 +  z(:,:,:) = 3
12630 +  call baz
12631 +  call foo (x, z(2:6, 4:7, 6:8))
12632 +  call bar (y, x)
12633 +  if (x (1, 1) .ne. 8 .or. x (2, 2) .ne. 9 .or. x (1, 2) .ne. 4) call abort
12634 +  if (x (1, 3) .ne. 10) call abort
12635 +  if (z (2, 4, 6) .ne. 6 .or. z (3, 5, 7) .ne. 7 .or. z (2, 4, 7) .ne. 5) call abort
12636 +  if (any (y .ne. (/4, 5, 4, 2, 2/))) call abort
12637 +  call foo (transpose (x), z)
12638 +  if (x (1, 1) .ne. 8 .or. x (2, 2) .ne. 9 .or. x (1, 2) .ne. 4) call abort
12639 +  if (x (3, 1) .ne. 10) call abort
12640 +end
12641 diff --git a/gdb/testsuite/gdb.fortran/string.exp b/gdb/testsuite/gdb.fortran/string.exp
12642 new file mode 100644
12643 index 0000000..39de2c4
12644 --- /dev/null
12645 +++ b/gdb/testsuite/gdb.fortran/string.exp
12646 @@ -0,0 +1,59 @@
12647 +# Copyright 2008 Free Software Foundation, Inc.
12648 +
12649 +# This program is free software; you can redistribute it and/or modify
12650 +# it under the terms of the GNU General Public License as published by
12651 +# the Free Software Foundation; either version 2 of the License, or
12652 +# (at your option) any later version.
12653 +# 
12654 +# This program is distributed in the hope that it will be useful,
12655 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
12656 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12657 +# GNU General Public License for more details.
12658 +# 
12659 +# You should have received a copy of the GNU General Public License
12660 +# along with this program; if not, write to the Free Software
12661 +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  
12662 +
12663 +# This file was written by Jan Kratochvil <jan.kratochvil@redhat.com>.
12664 +
12665 +# This file is part of the gdb testsuite.  It contains tests for Fortran
12666 +# strings with dynamic length.
12667 +
12668 +set testfile "string"
12669 +set srcfile ${testfile}.f90
12670 +set binfile ${objdir}/${subdir}/${testfile}
12671 +
12672 +if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug f90 quiet}] != "" } {
12673 +    untested "Couldn't compile ${srcfile}"
12674 +    return -1
12675 +}
12676 +
12677 +gdb_exit
12678 +gdb_start
12679 +gdb_reinitialize_dir $srcdir/$subdir
12680 +gdb_load ${binfile}
12681 +
12682 +if ![runto MAIN__] then {
12683 +    perror "couldn't run to breakpoint MAIN__"
12684 +    continue
12685 +}
12686 +
12687 +gdb_breakpoint [gdb_get_line_number "var-init"]
12688 +gdb_continue_to_breakpoint "var-init"
12689 +gdb_test "ptype c" "type = character(\\(kind=1\\)|\\*1)"
12690 +gdb_test "ptype d" "type = character(\\(kind=8\\)|\\*8)"
12691 +gdb_test "ptype e" "type = character(\\(kind=4\\)|\\*4)"
12692 +gdb_test "ptype f" "type = character(\\(kind=4\\)|\\*4) \\(7,8:10\\)"
12693 +gdb_test "ptype *e" "Attempt to take contents of a non-pointer value."
12694 +gdb_test "ptype *f" "type = character(\\(kind=4\\)|\\*4) \\(7\\)"
12695 +gdb_test "p c" "\\$\[0-9\]* = 'c'"
12696 +gdb_test "p d" "\\$\[0-9\]* = 'd       '"
12697 +gdb_test "p e" "\\$\[0-9\]* = 'g   '"
12698 +gdb_test "p f" "\\$\[0-9\]* = \\(\\( 'h   ', 'h   ', 'h   ', 'h   ', 'h   ', 'h   ', 'h   '\\) \\( 'h   ', 'h   ', 'h   ', 'h   ', 'h   ', 'h   ', 'h   '\\) \\( 'h   ', 'h   ', 'h   ', 'h   ', 'h   ', 'h   ', 'h   '\\) \\)"
12699 +gdb_test "p *e" "Attempt to take contents of a non-pointer value."
12700 +gdb_test "p *f" "Attempt to take contents of a non-pointer value."
12701 +
12702 +gdb_breakpoint [gdb_get_line_number "var-finish"]
12703 +gdb_continue_to_breakpoint "var-finish"
12704 +gdb_test "p e" "\\$\[0-9\]* = 'e   '" "p e re-set"
12705 +gdb_test "p f" "\\$\[0-9\]* = \\(\\( 'f   ', 'f   ', 'f   ', 'f   ', 'f   ', 'f   ', 'f   '\\) \\( 'f2  ', 'f   ', 'f   ', 'f   ', 'f   ', 'f   ', 'f   '\\) \\( 'f   ', 'f   ', 'f   ', 'f   ', 'f   ', 'f   ', 'f   '\\) \\)" "p *f re-set"
12706 diff --git a/gdb/testsuite/gdb.fortran/string.f90 b/gdb/testsuite/gdb.fortran/string.f90
12707 new file mode 100644
12708 index 0000000..226dc5d
12709 --- /dev/null
12710 +++ b/gdb/testsuite/gdb.fortran/string.f90
12711 @@ -0,0 +1,37 @@
12712 +! Copyright 2008 Free Software Foundation, Inc.
12713 +!
12714 +! This program is free software; you can redistribute it and/or modify
12715 +! it under the terms of the GNU General Public License as published by
12716 +! the Free Software Foundation; either version 2 of the License, or
12717 +! (at your option) any later version.
12718 +!
12719 +! This program is distributed in the hope that it will be useful,
12720 +! but WITHOUT ANY WARRANTY; without even the implied warranty of
12721 +! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12722 +! GNU General Public License for more details.
12723 +!
12724 +! You should have received a copy of the GNU General Public License
12725 +! along with this program; if not, write to the Free Software
12726 +! Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
12727 +!
12728 +! Ihis file is the Fortran source file for dynamic.exp.
12729 +! Original file written by Jakub Jelinek <jakub@redhat.com>.
12730 +! Modified for the GDB testcase by Jan Kratochvil <jan.kratochvil@redhat.com>.
12731 +
12732 +subroutine foo (e, f)
12733 +  character (len=1) :: c
12734 +  character (len=8) :: d
12735 +  character (len=*) :: e
12736 +  character (len=*) :: f (1:7, 8:10)
12737 +  c = 'c'
12738 +  d = 'd'
12739 +  e = 'e'                                       ! var-init
12740 +  f = 'f'
12741 +  f(1,9) = 'f2'
12742 +  c = 'c'                                       ! var-finish
12743 +end subroutine foo
12744 +  character (len=4) :: g, h (1:7, 8:10)
12745 +  g = 'g'
12746 +  h = 'h'
12747 +  call foo (g, h)
12748 +end
12749 diff --git a/gdb/testsuite/gdb.fortran/subrange.exp b/gdb/testsuite/gdb.fortran/subrange.exp
12750 new file mode 100644
12751 index 0000000..c819e23
12752 --- /dev/null
12753 +++ b/gdb/testsuite/gdb.fortran/subrange.exp
12754 @@ -0,0 +1,60 @@
12755 +# Copyright 2011 Free Software Foundation, Inc.
12756 +
12757 +# This program is free software; you can redistribute it and/or modify
12758 +# it under the terms of the GNU General Public License as published by
12759 +# the Free Software Foundation; either version 3 of the License, or
12760 +# (at your option) any later version.
12761 +#
12762 +# This program is distributed in the hope that it will be useful,
12763 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
12764 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12765 +# GNU General Public License for more details.
12766 +#
12767 +# You should have received a copy of the GNU General Public License
12768 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
12769 +
12770 +if { [skip_fortran_tests] } { return -1 }
12771 +
12772 +set testfile "subrange"
12773 +set srcfile ${testfile}.f90
12774 +if { [prepare_for_testing ${testfile}.exp ${testfile} ${srcfile} {debug f90}] } {
12775 +    return -1
12776 +}
12777 +
12778 +if ![runto MAIN__] {
12779 +    perror "Couldn't run to MAIN__"
12780 +    continue
12781 +}
12782 +
12783 +# Depending on the compiler version being used, the name of the 4-byte integer
12784 +# and real types can be printed differently.  For instance, gfortran-4.1 uses
12785 +# "int4" whereas gfortran-4.3 uses "int(kind=4)".
12786 +set int4 "(int4|integer\\(kind=4\\))"
12787 +
12788 +gdb_breakpoint [gdb_get_line_number "break-static"]
12789 +gdb_continue_to_breakpoint "break-static" ".*break-static.*"
12790 +
12791 +foreach var {a alloc ptr} {
12792 +    global pf_prefix
12793 +    set old_prefix $pf_prefix
12794 +    lappend pf_prefix "$var:"
12795 +
12796 +    gdb_test "p $var (2, 2:3)" { = \(22, 32\)}
12797 +    gdb_test "p $var (2:3, 3)" { = \(32, 33\)}
12798 +    gdb_test "p $var (1, 2:)" { = \(21, 31\)}
12799 +    gdb_test "p $var (2, :2)" { = \(12, 22\)}
12800 +    gdb_test "p $var (3, 2:2)" { = \(23\)}
12801 +    gdb_test "ptype $var (3, 2:2)" " = $int4 \\(2:2\\)"
12802 +    gdb_test "p $var (4, :)" { = \(14, 24, 34\)}
12803 +    gdb_test "p $var (:, :)" { = \(\( *11, 12, 13, 14\) \( *21, 22, 23, 24\) \( *31, 32, 33, 34\) *\)}
12804 +    gdb_test "ptype $var (:, :)" " = $int4 \\(4,3\\)"
12805 +    gdb_test "p $var (:)" "Wrong number of subscripts"
12806 +    gdb_test "p $var (:, :, :)" "Wrong number of subscripts"
12807 +
12808 +    set pf_prefix $old_prefix
12809 +}
12810 +
12811 +gdb_test_no_output {set $a=a}
12812 +delete_breakpoints
12813 +gdb_unload
12814 +gdb_test {p $a (3, 2:2)} { = \(23\)}
12815 diff --git a/gdb/testsuite/gdb.fortran/subrange.f90 b/gdb/testsuite/gdb.fortran/subrange.f90
12816 new file mode 100644
12817 index 0000000..4747ea9
12818 --- /dev/null
12819 +++ b/gdb/testsuite/gdb.fortran/subrange.f90
12820 @@ -0,0 +1,28 @@
12821 +! Copyright 2011 Free Software Foundation, Inc.
12822 +!
12823 +! This program is free software; you can redistribute it and/or modify
12824 +! it under the terms of the GNU General Public License as published by
12825 +! the Free Software Foundation; either version 3 of the License, or
12826 +! (at your option) any later version.
12827 +! 
12828 +! This program is distributed in the hope that it will be useful,
12829 +! but WITHOUT ANY WARRANTY; without even the implied warranty of
12830 +! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12831 +! GNU General Public License for more details.
12832 +! 
12833 +! You should have received a copy of the GNU General Public License
12834 +! along with this program.  If not, see <http://www.gnu.org/licenses/>.
12835 +
12836 +program test
12837 +  integer, target :: a (4, 3)
12838 +  integer, allocatable :: alloc (:, :)
12839 +  integer, pointer :: ptr (:, :)
12840 +  do 1 i = 1, 4
12841 +  do 1 j = 1, 3
12842 +    a (i, j) = j * 10 + i
12843 +1 continue
12844 +  allocate (alloc (4, 3))
12845 +  alloc = a
12846 +  ptr => a
12847 +  write (*,*) a                 ! break-static
12848 +end
12849 diff --git a/gdb/testsuite/gdb.gdb/selftest.exp b/gdb/testsuite/gdb.gdb/selftest.exp
12850 index 7645caf..f4f9c1f 100644
12851 --- a/gdb/testsuite/gdb.gdb/selftest.exp
12852 +++ b/gdb/testsuite/gdb.gdb/selftest.exp
12853 @@ -92,6 +92,10 @@ proc do_steps_and_nexts {} {
12854                 set description "step over cmdarg_vec initialization"
12855                 set command "step"
12856             }
12857 +           -re ".*python_script = 0.*$gdb_prompt $" {
12858 +               set description "step over python_script initialization"
12859 +               set command "step"
12860 +           }
12861             -re ".*pre_stat_chain = make_command_stats_cleanup.*$gdb_prompt $" {
12862                 set description "next over make_command_stats_cleanup and everything it calls"
12863                 set command "next"
12864 diff --git a/gdb/testsuite/gdb.mi/mi2-var-stale-type.c b/gdb/testsuite/gdb.mi/mi2-var-stale-type.c
12865 new file mode 100644
12866 index 0000000..ebced3c
12867 --- /dev/null
12868 +++ b/gdb/testsuite/gdb.mi/mi2-var-stale-type.c
12869 @@ -0,0 +1,26 @@
12870 +/* Copyright 2011 Free Software Foundation, Inc.
12871 +
12872 +   This file is part of GDB.
12873 +
12874 +   This program is free software; you can redistribute it and/or modify
12875 +   it under the terms of the GNU General Public License as published by
12876 +   the Free Software Foundation; either version 3 of the License, or
12877 +   (at your option) any later version.
12878 +
12879 +   This program is distributed in the hope that it will be useful,
12880 +   but WITHOUT ANY WARRANTY; without even the implied warranty of
12881 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12882 +   GNU General Public License for more details.
12883 +
12884 +   You should have received a copy of the GNU General Public License
12885 +   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
12886 +
12887 +int
12888 +main (int argc, char **argv)
12889 +{
12890 +  char vla[argc];
12891 +
12892 +  vla[0] = 0;  /* break-here */
12893 +
12894 +  return 0;
12895 +}
12896 diff --git a/gdb/testsuite/gdb.mi/mi2-var-stale-type.exp b/gdb/testsuite/gdb.mi/mi2-var-stale-type.exp
12897 new file mode 100644
12898 index 0000000..74a104e
12899 --- /dev/null
12900 +++ b/gdb/testsuite/gdb.mi/mi2-var-stale-type.exp
12901 @@ -0,0 +1,57 @@
12902 +# Copyright 2011 Free Software Foundation, Inc.
12903 +#
12904 +# This program is free software; you can redistribute it and/or modify
12905 +# it under the terms of the GNU General Public License as published by
12906 +# the Free Software Foundation; either version 3 of the License, or
12907 +# (at your option) any later version.
12908 +#
12909 +# This program is distributed in the hope that it will be useful,
12910 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
12911 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12912 +# GNU General Public License for more details.
12913 +#
12914 +# You should have received a copy of the GNU General Public License
12915 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
12916 +
12917 +load_lib mi-support.exp
12918 +set MIFLAGS "-i=mi2"
12919 +
12920 +gdb_exit
12921 +if [mi_gdb_start] {
12922 +    continue
12923 +}
12924 +
12925 +set testfile "mi2-var-stale-type"
12926 +set srcfile ${testfile}.c
12927 +set binfile ${objdir}/${subdir}/${testfile}
12928 +if {[build_executable ${testfile}.exp $testfile $srcfile] == -1} {
12929 +    return -1
12930 +}
12931 +
12932 +mi_delete_breakpoints
12933 +mi_gdb_reinitialize_dir $srcdir/$subdir
12934 +mi_gdb_load ${binfile}
12935 +
12936 +mi_gdb_test {-interpreter-exec console "maintenance set internal-error quit yes"} \
12937 +  {\^done} \
12938 +  "maintenance set internal-error quit yes"
12939 +
12940 +mi_gdb_test {-interpreter-exec console "maintenance set internal-error corefile yes"} \
12941 +  {\^done} \
12942 +  "maintenance set internal-error corefile yes"
12943 +
12944 +set line [gdb_get_line_number "break-here"]
12945 +set func "main"
12946 +
12947 +mi_gdb_test "-break-insert -t $srcfile:$line" \
12948 +  "\\^done,bkpt=\{number=\"\[0-9\]+\",type=\"breakpoint\",disp=\"del\",enabled=\"y\",addr=\"$hex\",func=\"$func\(\\\(.*\\\)\)?\",file=\".*\",line=\"$line\",times=\"0\",original-location=\".*\"\}" \
12949 +  "breakpoint at $func"
12950 +
12951 +if { [mi_run_cmd] < 0 } {
12952 +    return -1
12953 +}
12954 +mi_expect_stop "breakpoint-hit" $func ".*" ".*" "\[0-9\]+" { "" "disp=\"del\"" } "stop after initializing vla"
12955 +
12956 +mi_create_varobj "vla" "vla" "create local variable vla"
12957 +
12958 +mi_gdb_test "-var-update *" "\\^done,changelist=.*" "-var-update *"
12959 diff --git a/gdb/testsuite/gdb.opt/array-from-register-func.c b/gdb/testsuite/gdb.opt/array-from-register-func.c
12960 new file mode 100644
12961 index 0000000..729f457
12962 --- /dev/null
12963 +++ b/gdb/testsuite/gdb.opt/array-from-register-func.c
12964 @@ -0,0 +1,22 @@
12965 +/* This file is part of GDB, the GNU debugger.
12966 +
12967 +   Copyright 2009 Free Software Foundation, Inc.
12968 +
12969 +   This program is free software; you can redistribute it and/or modify
12970 +   it under the terms of the GNU General Public License as published by
12971 +   the Free Software Foundation; either version 3 of the License, or
12972 +   (at your option) any later version.
12973 +
12974 +   This program is distributed in the hope that it will be useful,
12975 +   but WITHOUT ANY WARRANTY; without even the implied warranty of
12976 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12977 +   GNU General Public License for more details.
12978 +
12979 +   You should have received a copy of the GNU General Public License
12980 +   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
12981 +
12982 +int
12983 +func (int *arr)
12984 +{
12985 +  return arr[0];
12986 +}
12987 diff --git a/gdb/testsuite/gdb.opt/array-from-register.c b/gdb/testsuite/gdb.opt/array-from-register.c
12988 new file mode 100644
12989 index 0000000..3090e7e
12990 --- /dev/null
12991 +++ b/gdb/testsuite/gdb.opt/array-from-register.c
12992 @@ -0,0 +1,28 @@
12993 +/* This file is part of GDB, the GNU debugger.
12994 +
12995 +   Copyright 2009 Free Software Foundation, Inc.
12996 +
12997 +   This program is free software; you can redistribute it and/or modify
12998 +   it under the terms of the GNU General Public License as published by
12999 +   the Free Software Foundation; either version 3 of the License, or
13000 +   (at your option) any later version.
13001 +
13002 +   This program is distributed in the hope that it will be useful,
13003 +   but WITHOUT ANY WARRANTY; without even the implied warranty of
13004 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13005 +   GNU General Public License for more details.
13006 +
13007 +   You should have received a copy of the GNU General Public License
13008 +   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
13009 +
13010 +extern int func (int *arr);
13011 +
13012 +int
13013 +main (void)
13014 +{
13015 +  int arr[] = { 42 };
13016 +
13017 +  func (arr);
13018 +
13019 +  return 0;
13020 +}
13021 diff --git a/gdb/testsuite/gdb.opt/array-from-register.exp b/gdb/testsuite/gdb.opt/array-from-register.exp
13022 new file mode 100644
13023 index 0000000..f2de718
13024 --- /dev/null
13025 +++ b/gdb/testsuite/gdb.opt/array-from-register.exp
13026 @@ -0,0 +1,33 @@
13027 +# Copyright 2009 Free Software Foundation, Inc.
13028 +#
13029 +# This program is free software; you can redistribute it and/or modify
13030 +# it under the terms of the GNU General Public License as published by
13031 +# the Free Software Foundation; either version 2 of the License, or
13032 +# (at your option) any later version.
13033 +#
13034 +# This program is distributed in the hope that it will be useful,
13035 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
13036 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13037 +# GNU General Public License for more details.
13038 +#
13039 +# You should have received a copy of the GNU General Public License
13040 +# along with this program; if not, write to the Free Software
13041 +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
13042 +#
13043 +# This file is part of the gdb testsuite.
13044 +
13045 +if { [prepare_for_testing array-from-register.exp "array-from-register"      \
13046 +                         {array-from-register.c array-from-register-func.c} \
13047 +                         {debug optimize=-O2}] } {
13048 +    return -1
13049 +}
13050 +
13051 +if ![runto func] then {
13052 +    return -1
13053 +}
13054 +
13055 +gdb_test "p arr" "\\$\[0-9\]+ = \\(int \\*\\) *0x\[0-9a-f\]+"
13056 +
13057 +# Seen regression:
13058 +# Address requested for identifier "arr" which is in register $rdi
13059 +gdb_test "p arr\[0\]" "\\$\[0-9\]+ = 42"
13060 diff --git a/gdb/testsuite/gdb.pascal/arrays.exp b/gdb/testsuite/gdb.pascal/arrays.exp
13061 new file mode 100644
13062 index 0000000..ccc6e1e
13063 --- /dev/null
13064 +++ b/gdb/testsuite/gdb.pascal/arrays.exp
13065 @@ -0,0 +1,104 @@
13066 +# Copyright 2008, 2009 Free Software Foundation, Inc.
13067 +#
13068 +# This program is free software; you can redistribute it and/or modify
13069 +# it under the terms of the GNU General Public License as published by
13070 +# the Free Software Foundation; either version 3 of the License, or
13071 +# (at your option) any later version.
13072 +#
13073 +# This program is distributed in the hope that it will be useful,
13074 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
13075 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13076 +# GNU General Public License for more details.
13077 +#
13078 +# You should have received a copy of the GNU General Public License
13079 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
13080 +
13081 +if $tracelevel then {
13082 +    strace $tracelevel
13083 +}
13084 +
13085 +load_lib "pascal.exp"
13086 +
13087 +set testfile "arrays"
13088 +set srcfile ${testfile}.pas
13089 +set binfile ${objdir}/${subdir}/${testfile}$EXEEXT
13090 +
13091 +# These tests only work with fpc, using the -gw3 compile-option
13092 +pascal_init
13093 +if { $pascal_compiler_is_fpc != 1 } {
13094 +  return -1
13095 +}
13096 +
13097 +# Detect if the fpc version is below 2.3.0
13098 +set fpc_generates_dwarf_for_dynamic_arrays 1
13099 +if { ($fpcversion_major < 2) || ( ($fpcversion_major == 2) && ($fpcversion_minor < 3))}  {
13100 +  set fpc_generates_dwarf_for_dynamic_arrays 0
13101 +}
13102 +
13103 +
13104 +if {[gdb_compile_pascal "-gw3 ${srcdir}/${subdir}/${srcfile}" "${binfile}" executable [list debug ]] != "" } {
13105 +  return -1
13106 +}
13107 +
13108 +gdb_exit
13109 +gdb_start
13110 +gdb_reinitialize_dir $srcdir/$subdir
13111 +gdb_load ${binfile}
13112 +set bp_location1 [gdb_get_line_number "set breakpoint 1 here"]
13113 +set bp_location2 [gdb_get_line_number "set breakpoint 2 here"]
13114 +
13115 +
13116 +if { [gdb_breakpoint ${srcfile}:${bp_location1}] } {
13117 +    pass "setting breakpoint 1"
13118 +}
13119 +if { [gdb_breakpoint ${srcfile}:${bp_location2}] } {
13120 +    pass "setting breakpoint 2"
13121 +}
13122 +
13123 +# Verify that "start" lands inside the right procedure.
13124 +if { [gdb_start_cmd] < 0 } {
13125 +    untested start
13126 +    return -1
13127 +}
13128 +
13129 +gdb_test "" ".* at .*${srcfile}.*" "start"
13130 +
13131 +gdb_test "cont" "Breakpoint .*:${bp_location1}.*" "Going to first breakpoint"
13132 +
13133 +gdb_test "print StatArrInt" ".* = \\{50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61\\}" "Print static array of integer type"
13134 +gdb_test "print StatArrInt_" ".* = \\{50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61\\}" "Print static array of integer"
13135 +
13136 +gdb_test "cont" "Breakpoint .*:${bp_location2}.*" "Going to second breakpoint"
13137 +
13138 +gdb_test "print StatArrChar" ".* = 'abcdefghijkl'" "Print static array of char"
13139 +gdb_test "print Stat2dArrInt" ".* = \\{\\{0, 1, 2, 3, 4\\}, \\{1, 2, 3, 4, 5\\}, \\{2, 3, 4, 5, 6\\}, \\{3, 4, 5, 6, 7\\}, \\{4, 5, 6, 7, 8\\}, \\{5, 6, 7, 8, 9\\}, \\{6, 7, 8, 9, 10\\}, \\{7, 8, 9, 10, 11\\}, \\{8, 9, 10, 11, 12\\}, \\{9, 10, 11, 12, 13\\}, \\{10, 11, 12, 13, 14\\}, \\{11, 12, 13, 14, 15\\}\\}" "Print static 2-dimensional array of integer"
13140 +
13141 +if { $fpc_generates_dwarf_for_dynamic_arrays == 0} {
13142 +  setup_xfail "*-*-*"
13143 +}
13144 +gdb_test "print DynArrInt" ".* = \\{50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62\\}" "Print dynamic array of integer type"
13145 +if { $fpc_generates_dwarf_for_dynamic_arrays == 0} {
13146 +  setup_xfail "*-*-*"
13147 +}
13148 +gdb_test "print DynArrInt_" ".* = \\{50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62\\}" "Print dynamic array of integer"
13149 +
13150 +if { $fpc_generates_dwarf_for_dynamic_arrays == 0} {
13151 +  setup_xfail "*-*-*"
13152 +}
13153 +gdb_test "print s" ".* = 'test'#0'string'" "Print string containing null-char"
13154 +
13155 +if { $fpc_generates_dwarf_for_dynamic_arrays == 0} {
13156 +  setup_xfail "*-*-*"
13157 +}
13158 +gdb_test "print DynArrStr" ".* = \\{'dstr0', 'dstr1', 'dstr2', 'dstr3', 'dstr4', 'dstr5', 'dstr6', 'dstr7', 'dstr8', 'dstr9', 'dstr10', 'dstr11', 'dstr12'\\}" "Print dynamic array of string"
13159 +
13160 +if { $fpc_generates_dwarf_for_dynamic_arrays == 0} {
13161 +  setup_xfail "*-*-*"
13162 +}
13163 +gdb_test "print StatArrStr" ".* = \\{'str0', 'str1', 'str2', 'str3', 'str4', 'str5', 'str6', 'str7', 'str8', 'str9', 'str10', 'str11', 'str12'\\}" "Print static array of string"
13164 +
13165 +if { $fpc_generates_dwarf_for_dynamic_arrays == 0} {
13166 +  setup_xfail "*-*-*"
13167 +}
13168 +gdb_test "print DynArrChar" ".* = 'abcdefghijklm'" "Print dynamic array of char"
13169 +
13170 diff --git a/gdb/testsuite/gdb.pascal/arrays.pas b/gdb/testsuite/gdb.pascal/arrays.pas
13171 new file mode 100644
13172 index 0000000..295602d
13173 --- /dev/null
13174 +++ b/gdb/testsuite/gdb.pascal/arrays.pas
13175 @@ -0,0 +1,82 @@
13176 +{
13177 + Copyright 2008, 2009 Free Software Foundation, Inc.
13178 +
13179 + This program is free software; you can redistribute it and/or modify
13180 + it under the terms of the GNU General Public License as published by
13181 + the Free Software Foundation; either version 3 of the License, or
13182 + (at your option) any later version.
13183 +
13184 + This program is distributed in the hope that it will be useful,
13185 + but WITHOUT ANY WARRANTY; without even the implied warranty of
13186 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13187 + GNU General Public License for more details.
13188 +
13189 + You should have received a copy of the GNU General Public License
13190 + along with this program.  If not, see <http://www.gnu.org/licenses/>.
13191 +}
13192 +
13193 +program arrays;
13194 +
13195 +{$mode objfpc}{$h+}
13196 +
13197 +uses sysutils;
13198 +
13199 +type TStatArrInt= array[0..11] of integer;
13200 +     TDynArrInt= array of integer;
13201 +     TStatArrStr= array[0..12] of string;
13202 +     TDynArrStr= array of string;
13203 +     TDynArrChar = array of char;
13204 +     TStatArrChar = array [0..11] of char;
13205 +
13206 +     TStat2dArrInt = array[0..11,0..4] of integer;
13207 +
13208 +var StatArrInt: TStatArrInt;
13209 +    StatArrInt_: Array[0..11] of integer;
13210 +    DynArrInt:  TDynArrInt;
13211 +    DynArrInt_: Array of integer;
13212 +    StatArrStr: TStatArrStr;
13213 +    DynArrStr: TDynArrStr;
13214 +    StatArrChar: TStatArrChar;
13215 +    DynArrChar: TDynArrChar;
13216 +
13217 +    Stat2dArrInt: TStat2dArrInt;
13218 +
13219 +    s: string;
13220 +       
13221 +    i,j : integer;
13222 +
13223 +begin
13224 +  for i := 0 to 11 do
13225 +    begin
13226 +    StatArrInt[i]:= i+50;
13227 +    StatArrInt_[i]:= i+50;
13228 +    StatArrChar[i]:= chr(ord('a')+i);
13229 +    for j := 0 to 4 do
13230 +      Stat2dArrInt[i,j]:=i+j;
13231 +    end;
13232 +  writeln(StatArrInt_[0]);
13233 +  writeln(StatArrInt[0]); { set breakpoint 1 here }
13234 +  writeln(StatArrChar[0]);
13235 +  writeln(Stat2dArrInt[0,0]);
13236 +
13237 +  setlength(DynArrInt,13);
13238 +  setlength(DynArrInt_,13);
13239 +  setlength(DynArrStr,13);
13240 +  setlength(DynArrChar,13);
13241 +  for i := 0 to 12 do
13242 +    begin
13243 +    DynArrInt[i]:= i+50;
13244 +    DynArrInt_[i]:= i+50;
13245 +    DynArrChar[i]:= chr(ord('a')+i);
13246 +    StatArrStr[i]:='str'+inttostr(i);
13247 +    DynArrStr[i]:='dstr'+inttostr(i);
13248 +    end;
13249 +  writeln(DynArrInt_[1]);
13250 +  writeln(DynArrInt[1]); 
13251 +  writeln(DynArrStr[1]); 
13252 +  writeln(StatArrStr[1]);
13253 +  writeln(DynArrChar[1]);
13254 +
13255 +  s := 'test'#0'string';
13256 +  writeln(s); { set breakpoint 2 here }
13257 +end.
13258 diff --git a/gdb/testsuite/gdb.python/py-frame.exp b/gdb/testsuite/gdb.python/py-frame.exp
13259 index 993e774..52dbd17 100644
13260 --- a/gdb/testsuite/gdb.python/py-frame.exp
13261 +++ b/gdb/testsuite/gdb.python/py-frame.exp
13262 @@ -74,8 +74,6 @@ gdb_test "python print bframe == gdb.newest_frame()" True \
13263  
13264  gdb_test "python print 'result =', f0 == f1" " = False" "test equality comparison (false)"
13265  gdb_test "python print 'result =', f0 == f0" " = True" "test equality comparison (true)"
13266 -gdb_test "python print 'result =', f0 != f1" " = True" "test inequality comparison (true)"
13267 -gdb_test "python print 'result =', f0 != f0" " = False" "test inequality comparison (false)"
13268  gdb_test "python print 'result =', f0.is_valid ()" " = True" "test Frame.is_valid"
13269  gdb_test "python print 'result =', f0.name ()" " = f2" "test Frame.name"
13270  gdb_test "python print 'result =', f0.type () == gdb.NORMAL_FRAME" " = True" "test Frame.type"
13271 @@ -90,3 +88,5 @@ gdb_test "python print 'result =', f0.read_var ('variable_which_surely_doesnt_ex
13272  gdb_test "python print 'result =', f0.read_var ('a')" " = 1" "test Frame.read_var - success"
13273  
13274  gdb_test "python print 'result =', gdb.selected_frame () == f1" " = True" "test gdb.selected_frame"
13275 +
13276 +gdb_test "python print 'result =', f0.block ()" "<gdb.Block object at 0x\[\[:xdigit:\]\]+>" "test Frame.block"
13277 diff --git a/gdb/testsuite/gdb.python/py-value.exp b/gdb/testsuite/gdb.python/py-value.exp
13278 index acfd89b..c77d9c2 100644
13279 --- a/gdb/testsuite/gdb.python/py-value.exp
13280 +++ b/gdb/testsuite/gdb.python/py-value.exp
13281 @@ -358,6 +358,15 @@ proc test_value_after_death {} {
13282      "print value's type"
13283  }
13284  
13285 +# Regression test for a cast failure.  The bug was that if we cast a
13286 +# value to its own type, gdb could crash.  This happened because we
13287 +# could end up double-freeing a struct value.
13288 +proc test_cast_regression {} {
13289 +  gdb_test "python v = gdb.Value(5)" "" "create value for cast test"
13290 +  gdb_test "python v = v.cast(v.type)" "" "cast value for cast test"
13291 +  gdb_test "python print v" "5" "print value for cast test"
13292 +}
13293 +
13294  # Regression test for invalid subscript operations.  The bug was that
13295  # the type of the value was not being checked before allowing a
13296  # subscript operation to proceed.
13297 @@ -494,6 +503,7 @@ test_value_in_inferior
13298  test_inferior_function_call
13299  test_lazy_strings
13300  test_value_after_death
13301 +test_cast_regression
13302  
13303  # Test either C or C++ values. 
13304  test_subscript_regression "${binfile}" "c"
13305 diff --git a/gdb/testsuite/lib/gdb.exp b/gdb/testsuite/lib/gdb.exp
13306 index 4f2b7c9..0daef25 100644
13307 --- a/gdb/testsuite/lib/gdb.exp
13308 +++ b/gdb/testsuite/lib/gdb.exp
13309 @@ -142,6 +142,11 @@ proc gdb_unload {} {
13310             send_gdb "y\n"
13311             exp_continue
13312         }
13313 +       -re "A program is being debugged already..*Are you sure you want to change the file.*y or n. $"\
13314 +           { send_gdb "y\n"
13315 +               verbose "\t\tUnloading symbols for program being debugged"
13316 +           exp_continue
13317 +       }
13318         -re "Discard symbol table from .*y or n.*$" {
13319             send_gdb "y\n"
13320             exp_continue
13321 diff --git a/gdb/testsuite/lib/pascal.exp b/gdb/testsuite/lib/pascal.exp
13322 index dd18d37..f973485 100644
13323 --- a/gdb/testsuite/lib/pascal.exp
13324 +++ b/gdb/testsuite/lib/pascal.exp
13325 @@ -37,6 +37,9 @@ proc pascal_init {} {
13326      global pascal_compiler_is_fpc
13327      global gpc_compiler
13328      global fpc_compiler
13329 +    global fpcversion_major
13330 +    global fpcversion_minor
13331 +    global fpcversion_release
13332      global env
13333   
13334      if { $pascal_init_done == 1 } {
13335 @@ -64,6 +67,20 @@ proc pascal_init {} {
13336             set pascal_compiler_is_fpc 1
13337             verbose -log "Free Pascal compiler found"
13338         }
13339 +
13340 +       # Detect the fpc-version
13341 +       if { $pascal_compiler_is_fpc == 1 } {
13342 +           set fpcversion_major 1
13343 +           set fpcversion_minor 0
13344 +           set fpcversion_release 0
13345 +           set fpcversion [ remote_exec host $fpc_compiler "-iV" ] 
13346 +           if [regexp {.*([0-9]+)\.([0-9]+)\.([0-9]+).?} $fpcversion] {
13347 +              regsub {.*([0-9]+)\.([0-9]+)\.([0-9]+).?\n?.?} $fpcversion {\1} fpcversion_major
13348 +              regsub {.*([0-9]+)\.([0-9]+)\.([0-9]+).?\n?.?} $fpcversion {\2} fpcversion_minor
13349 +              regsub {.*([0-9]+)\.([0-9]+)\.([0-9]+).?\n?.?} $fpcversion {\3} fpcversion_release
13350 +           }
13351 +            verbose -log "Freepascal version: $fpcversion_major.$fpcversion_minor.$fpcversion_release"
13352 +       }
13353      }
13354      set pascal_init_done 1
13355  }   
13356 diff --git a/gdb/top.c b/gdb/top.c
13357 index 061ad48..d615cfc 100644
13358 --- a/gdb/top.c
13359 +++ b/gdb/top.c
13360 @@ -352,6 +352,9 @@ prepare_execute_command (void)
13361  
13362    mark = value_mark ();
13363    cleanup = make_cleanup_value_free_to_mark (mark);
13364 +#if 0
13365 +  free_all_types ();
13366 +#endif
13367  
13368    /* With multiple threads running while the one we're examining is
13369       stopped, the dcache can get stale without us being able to detect
13370 diff --git a/gdb/typeprint.c b/gdb/typeprint.c
13371 index c25e705..498958a 100644
13372 --- a/gdb/typeprint.c
13373 +++ b/gdb/typeprint.c
13374 @@ -35,6 +35,7 @@
13375  #include "gdb_string.h"
13376  #include "exceptions.h"
13377  #include "valprint.h"
13378 +#include "dwarf2loc.h"
13379  #include <errno.h>
13380  
13381  extern void _initialize_typeprint (void);
13382 @@ -76,6 +77,9 @@ void
13383  type_print (struct type *type, const char *varstring, struct ui_file *stream,
13384             int show)
13385  {
13386 +  if (show >= 0 && current_language->la_language != language_ada)
13387 +    type = check_typedef (type);
13388 +
13389    LA_PRINT_TYPE (type, varstring, stream, show, 0);
13390  }
13391  
13392 @@ -114,7 +118,8 @@ whatis_exp (char *exp, int show)
13393  {
13394    struct expression *expr;
13395    struct value *val;
13396 -  struct cleanup *old_chain = NULL;
13397 +  /* Required at least for the object_address_set call.  */
13398 +  struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
13399    struct type *real_type = NULL;
13400    struct type *type;
13401    int full = 0;
13402 @@ -125,12 +130,13 @@ whatis_exp (char *exp, int show)
13403    if (exp)
13404      {
13405        expr = parse_expression (exp);
13406 -      old_chain = make_cleanup (free_current_contents, &expr);
13407 +      make_cleanup (free_current_contents, &expr);
13408        val = evaluate_type (expr);
13409      }
13410    else
13411      val = access_value_history (0);
13412  
13413 +  object_address_set (value_raw_address (val));
13414    type = value_type (val);
13415  
13416    get_user_print_options (&opts);
13417 @@ -158,8 +164,7 @@ whatis_exp (char *exp, int show)
13418    type_print (type, "", gdb_stdout, show);
13419    printf_filtered ("\n");
13420  
13421 -  if (exp)
13422 -    do_cleanups (old_chain);
13423 +  do_cleanups (old_chain);
13424  }
13425  
13426  static void
13427 diff --git a/gdb/utils.c b/gdb/utils.c
13428 index 5566149..6e1aa34 100644
13429 --- a/gdb/utils.c
13430 +++ b/gdb/utils.c
13431 @@ -27,6 +27,7 @@
13432  #include "exceptions.h"
13433  #include "gdbthread.h"
13434  #include "fnmatch.h"
13435 +#include "gdb_bfd.h"
13436  #ifdef HAVE_SYS_RESOURCE_H
13437  #include <sys/resource.h>
13438  #endif /* HAVE_SYS_RESOURCE_H */
13439 @@ -198,11 +199,11 @@ make_cleanup_dyn_string_delete (dyn_string_t arg)
13440  static void
13441  do_bfd_close_cleanup (void *arg)
13442  {
13443 -  bfd_close (arg);
13444 +  gdb_bfd_unref (arg);
13445  }
13446  
13447  struct cleanup *
13448 -make_cleanup_bfd_close (bfd *abfd)
13449 +make_cleanup_bfd_unref (bfd *abfd)
13450  {
13451    return make_cleanup (do_bfd_close_cleanup, abfd);
13452  }
13453 @@ -1738,6 +1739,36 @@ set_batch_flag_and_make_cleanup_restore_page_info (void)
13454    return back_to;
13455  }
13456  
13457 +/* Helper for make_cleanup_restore_page_info.  */
13458 +
13459 +static void
13460 +do_restore_selected_frame_cleanup (void *arg)
13461 +{
13462 +  struct frame_id *frame_idp = arg;
13463 +
13464 +  select_frame (frame_find_by_id (*frame_idp));
13465 +
13466 +  xfree (frame_idp);
13467 +}
13468 +
13469 +/* Provide cleanup for restoring currently selected frame.  Use frame_id for
13470 +   the case the current frame becomes stale in the meantime.  */
13471 +
13472 +struct cleanup *
13473 +make_cleanup_restore_selected_frame (void)
13474 +{
13475 +  struct frame_id *frame_idp;
13476 +
13477 +  /* get_selected_frame->get_current_frame would error otherwise.  */
13478 +  if (!has_stack_frames ())
13479 +    return make_cleanup (null_cleanup, NULL);
13480 +
13481 +  frame_idp = xmalloc (sizeof (*frame_idp));
13482 +  *frame_idp = get_frame_id (get_selected_frame (NULL));
13483 +
13484 +  return make_cleanup (do_restore_selected_frame_cleanup, frame_idp);
13485 +}
13486 +
13487  /* Set the screen size based on LINES_PER_PAGE and CHARS_PER_LINE.  */
13488  
13489  static void
13490 diff --git a/gdb/valarith.c b/gdb/valarith.c
13491 index 96d5411..37bd464 100644
13492 --- a/gdb/valarith.c
13493 +++ b/gdb/valarith.c
13494 @@ -197,7 +197,10 @@ value_subscripted_rvalue (struct value *array, LONGEST index, int lowerbound)
13495    struct type *array_type = check_typedef (value_type (array));
13496    struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (array_type));
13497    unsigned int elt_size = TYPE_LENGTH (elt_type);
13498 -  unsigned int elt_offs = elt_size * longest_to_int (index - lowerbound);
13499 +  unsigned int elt_stride
13500 +    = (TYPE_BYTE_STRIDE (TYPE_INDEX_TYPE (array_type)) == 0
13501 +       ? elt_size : TYPE_BYTE_STRIDE (TYPE_INDEX_TYPE (array_type)));
13502 +  unsigned int elt_offs = elt_stride * longest_to_int (index - lowerbound);
13503    struct value *v;
13504  
13505    if (index < lowerbound || (!TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (array_type)
13506 @@ -297,6 +300,10 @@ int
13507  binop_user_defined_p (enum exp_opcode op,
13508                       struct value *arg1, struct value *arg2)
13509  {
13510 +  /* FIXME: We should support user defined ops for dynamic types.  */
13511 +  if (TYPE_DYNAMIC (value_type (arg1)) || TYPE_DYNAMIC (value_type (arg2)))
13512 +    return 0;
13513 +
13514    return binop_types_user_defined_p (op, value_type (arg1), value_type (arg2));
13515  }
13516  
13517 diff --git a/gdb/valops.c b/gdb/valops.c
13518 index 97d889b..6ae77b8 100644
13519 --- a/gdb/valops.c
13520 +++ b/gdb/valops.c
13521 @@ -45,6 +45,7 @@
13522  #include "objfiles.h"
13523  #include "symtab.h"
13524  #include "exceptions.h"
13525 +#include "dwarf2loc.h"
13526  
13527  extern int overload_debug;
13528  /* Local functions.  */
13529 @@ -919,6 +920,65 @@ value_one (struct type *type)
13530    return val;
13531  }
13532  
13533 +/* object_address_set must be already called before this function.  */
13534 +
13535 +const char *
13536 +object_address_data_not_valid (struct type *type)
13537 +{
13538 +  /* Attributes are present only at the target type of a typedef.  Make the
13539 +     call conditional as it would otherwise loop through type_length_get.  */
13540 +  if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
13541 +    CHECK_TYPEDEF (type);
13542 +
13543 +  /* DW_AT_associated has a preference over DW_AT_allocated.  */
13544 +  if (TYPE_NOT_ASSOCIATED (type)
13545 +      || (TYPE_ASSOCIATED (type) != NULL
13546 +         && 0 == dwarf_locexpr_baton_eval (TYPE_ASSOCIATED (type))))
13547 +    return N_("object is not associated");
13548 +
13549 +  if (TYPE_NOT_ALLOCATED (type)
13550 +      || (TYPE_ALLOCATED (type) != NULL
13551 +         && 0 == dwarf_locexpr_baton_eval (TYPE_ALLOCATED (type))))
13552 +    return N_("object is not allocated");
13553 +
13554 +  return NULL;
13555 +}
13556 +
13557 +/* Return non-NULL check_typedef result on TYPE if the variable is valid.  If
13558 +   it is valid the function may store the data address (DW_AT_DATA_LOCATION) of
13559 +   TYPE at *ADDRESS_RETURN.  You must set *ADDRESS_RETURN from
13560 +   value_raw_address (VAL) before calling this function.  If no
13561 +   DW_AT_DATA_LOCATION is present for TYPE the address at *ADDRESS_RETURN is
13562 +   left unchanged.  ADDRESS_RETURN must not be NULL, use
13563 +   object_address_data_not_valid () for just the data validity check.  */
13564 +
13565 +struct type *
13566 +object_address_get_data (struct type *type, CORE_ADDR *address_return)
13567 +{
13568 +  gdb_assert (address_return != NULL);
13569 +
13570 +  object_address_set (*address_return);
13571 +
13572 +  /* TYPE_DATA_LOCATION_DWARF_BLOCK / TYPE_DATA_LOCATION_ADDR are present only
13573 +     at the target type of a typedef.  */
13574 +  CHECK_TYPEDEF (type);
13575 +
13576 +  if (object_address_data_not_valid (type) != NULL)
13577 +    {
13578 +      /* Do not try to evaluate DW_AT_data_location as it may even crash
13579 +        (it would just return the value zero in the gfortran case).  */
13580 +      return NULL;
13581 +    }
13582 +
13583 +  if (TYPE_DATA_LOCATION_IS_ADDR (type))
13584 +    *address_return = TYPE_DATA_LOCATION_ADDR (type);
13585 +  else if (TYPE_DATA_LOCATION_DWARF_BLOCK (type) != NULL)
13586 +    *address_return
13587 +      = dwarf_locexpr_baton_eval (TYPE_DATA_LOCATION_DWARF_BLOCK (type));
13588 +
13589 +  return type;
13590 +}
13591 +
13592  /* Helper function for value_at, value_at_lazy, and value_at_lazy_stack.  */
13593  
13594  static struct value *
13595 @@ -981,7 +1041,8 @@ int
13596  value_fetch_lazy (struct value *val)
13597  {
13598    gdb_assert (value_lazy (val));
13599 -  allocate_value_contents (val);
13600 +  if (VALUE_LVAL (val) != lval_memory)
13601 +    allocate_value_contents (val);
13602    if (value_bitsize (val))
13603      {
13604        /* To read a lazy bitfield, read the entire enclosing value.  This
13605 @@ -1015,12 +1076,24 @@ value_fetch_lazy (struct value *val)
13606      }
13607    else if (VALUE_LVAL (val) == lval_memory)
13608      {
13609 -      CORE_ADDR addr = value_address (val);
13610 -      int length = TYPE_LENGTH (check_typedef (value_enclosing_type (val)));
13611 +      CORE_ADDR addr = value_raw_address (val);
13612  
13613 -      if (length)
13614 -       read_value_memory (val, 0, value_stack (val),
13615 -                          addr, value_contents_all_raw (val), length);
13616 +      if (object_address_get_data (value_type (val), &addr))
13617 +       {
13618 +         struct type *type = value_enclosing_type (val);
13619 +         int length = TYPE_LENGTH (check_typedef (type));
13620 +
13621 +         if (length)
13622 +           {
13623 +             /* Delay it after object_address_get_data above.  */
13624 +             allocate_value_contents (val);
13625 +             addr += value_offset (val);
13626 +             read_value_memory (val, 0, value_stack (val),
13627 +                                addr, value_contents_all_raw (val), length);
13628 +           }
13629 +       }
13630 +      /* Just to be sure it has been called.  */
13631 +      allocate_value_contents (val);
13632      }
13633    else if (VALUE_LVAL (val) == lval_register)
13634      {
13635 @@ -1530,7 +1603,18 @@ address_of_variable (struct symbol *var, struct block *b)
13636    if ((VALUE_LVAL (val) == lval_memory && value_lazy (val))
13637        || TYPE_CODE (type) == TYPE_CODE_FUNC)
13638      {
13639 -      CORE_ADDR addr = value_address (val);
13640 +      CORE_ADDR addr;
13641 +
13642 +      if (VALUE_LVAL (val) == lval_memory)
13643 +       {
13644 +         addr = value_raw_address (val);
13645 +         if (!object_address_get_data (type, &addr))
13646 +           error (_("Can't take address of memory lvalue \"%s\"."),
13647 +                  SYMBOL_PRINT_NAME (var));
13648 +         set_value_address (val, addr);
13649 +       }
13650 +
13651 +      addr = value_address (val);
13652  
13653        return value_from_pointer (lookup_pointer_type (type), addr);
13654      }
13655 @@ -1637,6 +1721,7 @@ struct value *
13656  value_coerce_array (struct value *arg1)
13657  {
13658    struct type *type = check_typedef (value_type (arg1));
13659 +  CORE_ADDR address;
13660  
13661    /* If the user tries to do something requiring a pointer with an
13662       array that has not yet been pushed to the target, then this would
13663 @@ -1646,8 +1731,12 @@ value_coerce_array (struct value *arg1)
13664    if (VALUE_LVAL (arg1) != lval_memory)
13665      error (_("Attempt to take address of value not located in memory."));
13666  
13667 +  address = value_raw_address (arg1);
13668 +  if (!object_address_get_data (type, &address))
13669 +    error (_("Attempt to take address of non-valid value."));
13670 +
13671    return value_from_pointer (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
13672 -                            value_address (arg1));
13673 +                            address + value_offset (arg1));
13674  }
13675  
13676  /* Given a value which is a function, return a value which is a pointer
13677 @@ -3769,6 +3858,8 @@ value_slice (struct value *array, int lowbound, int length)
13678                                         TYPE_TARGET_TYPE (range_type),
13679                                         lowbound, 
13680                                         lowbound + length - 1);
13681 +  TYPE_BYTE_STRIDE (slice_range_type) = TYPE_BYTE_STRIDE (range_type);
13682 +
13683    if (TYPE_CODE (array_type) == TYPE_CODE_BITSTRING)
13684      {
13685        int i;
13686 diff --git a/gdb/valprint.c b/gdb/valprint.c
13687 index fc5942d..8333eb4 100644
13688 --- a/gdb/valprint.c
13689 +++ b/gdb/valprint.c
13690 @@ -37,6 +37,7 @@
13691  #include "gdb_obstack.h"
13692  #include "charset.h"
13693  #include <ctype.h>
13694 +#include "dwarf2loc.h"
13695  
13696  #include <errno.h>
13697  
13698 @@ -251,7 +252,6 @@ scalar_type_p (struct type *type)
13699      case TYPE_CODE_STRUCT:
13700      case TYPE_CODE_UNION:
13701      case TYPE_CODE_SET:
13702 -    case TYPE_CODE_STRING:
13703      case TYPE_CODE_BITSTRING:
13704        return 0;
13705      default:
13706 @@ -1570,6 +1570,7 @@ val_print_array_elements (struct type *type,
13707  {
13708    unsigned int things_printed = 0;
13709    unsigned len;
13710 +  struct type *saved_type = type;
13711    struct type *elttype, *index_type;
13712    unsigned eltlen;
13713    /* Position of the array element we are examining to see
13714 @@ -1578,9 +1579,33 @@ val_print_array_elements (struct type *type,
13715    /* Number of repetitions we have detected so far.  */
13716    unsigned int reps;
13717    LONGEST low_bound, high_bound;
13718 +  struct cleanup *back_to;
13719 +  CORE_ADDR saved_address = address;
13720 +  
13721 +  back_to = make_cleanup (null_cleanup, 0);
13722 +  type = object_address_get_data (type, &address);
13723 +  if (!type)
13724 +    {
13725 +      fputs_filtered (object_address_data_not_valid (type), stream);
13726 +      do_cleanups (back_to);
13727 +      return;
13728 +    }
13729 +  if (address != saved_address)
13730 +    {
13731 +      size_t length = TYPE_LENGTH (type);
13732  
13733 -  elttype = TYPE_TARGET_TYPE (type);
13734 -  eltlen = TYPE_LENGTH (check_typedef (elttype));
13735 +      valaddr = xmalloc (length);
13736 +      make_cleanup (xfree, (gdb_byte *) valaddr);
13737 +      read_memory (address, (gdb_byte *) valaddr, length);
13738 +    }
13739 +
13740 +  /* Skip typedefs but do not resolve TYPE_DYNAMIC.  */
13741 +  elttype = saved_type;
13742 +  while (TYPE_CODE (elttype) == TYPE_CODE_TYPEDEF)
13743 +    elttype = TYPE_TARGET_TYPE (elttype);
13744 +  elttype = TYPE_TARGET_TYPE (elttype);
13745 +
13746 +  eltlen = TYPE_ARRAY_BYTE_STRIDE_VALUE (type);
13747    index_type = TYPE_INDEX_TYPE (type);
13748  
13749    if (get_array_bounds (type, &low_bound, &high_bound))
13750 @@ -1667,6 +1692,8 @@ val_print_array_elements (struct type *type,
13751      {
13752        fprintf_filtered (stream, "...");
13753      }
13754 +
13755 +  do_cleanups (back_to);
13756  }
13757  
13758  /* Read LEN bytes of target memory at address MEMADDR, placing the
13759 diff --git a/gdb/value.c b/gdb/value.c
13760 index a6bb718..7b08d1f 100644
13761 --- a/gdb/value.c
13762 +++ b/gdb/value.c
13763 @@ -42,6 +42,7 @@
13764  #include <ctype.h>
13765  #include "tracepoint.h"
13766  #include "cp-abi.h"
13767 +#include "observer.h"
13768  
13769  /* Prototypes for exported functions.  */
13770  
13771 @@ -1437,12 +1438,15 @@ void
13772  set_value_component_location (struct value *component,
13773                               const struct value *whole)
13774  {
13775 +  CORE_ADDR addr;
13776 +
13777    if (whole->lval == lval_internalvar)
13778      VALUE_LVAL (component) = lval_internalvar_component;
13779    else
13780      VALUE_LVAL (component) = whole->lval;
13781  
13782    component->location = whole->location;
13783 +
13784    if (whole->lval == lval_computed)
13785      {
13786        const struct lval_funcs *funcs = whole->location.computed.funcs;
13787 @@ -1450,6 +1454,12 @@ set_value_component_location (struct value *component,
13788        if (funcs->copy_closure)
13789          component->location.computed.closure = funcs->copy_closure (whole);
13790      }
13791 +
13792 +  addr = value_raw_address (component);
13793 +  object_address_get_data (value_type (whole), &addr);
13794 +  if (component->lval != lval_internalvar
13795 +      && component->lval != lval_internalvar_component)
13796 +    set_value_address (component, addr);
13797  }
13798  
13799  \f
13800 @@ -1583,6 +1593,31 @@ show_values (char *num_exp, int from_tty)
13801        num_exp[1] = '\0';
13802      }
13803  }
13804 +
13805 +/* Sanity check for memory leaks and proper types reference counting.  */
13806 +
13807 +static void
13808 +value_history_cleanup (void *unused)
13809 +{
13810 +  while (value_history_chain)
13811 +    {
13812 +      struct value_history_chunk *chunk = value_history_chain;
13813 +      int i;
13814 +
13815 +      for (i = 0; i < ARRAY_SIZE (chunk->values); i++)
13816 +       value_free (chunk->values[i]);
13817 +
13818 +      value_history_chain = chunk->next;
13819 +      xfree (chunk);
13820 +    }
13821 +  value_history_count = 0;
13822 +
13823 +  /* Free the unreferenced types above.  */
13824 +  free_all_values ();
13825 +#if 0
13826 +  free_all_types ();
13827 +#endif
13828 +}
13829  \f
13830  /* Internal variables.  These are variables within the debugger
13831     that hold values assigned by debugger commands.
13832 @@ -2118,6 +2153,38 @@ call_internal_function (struct gdbarch *gdbarch,
13833    return (*ifn->handler) (gdbarch, language, ifn->cookie, argc, argv);
13834  }
13835  
13836 +#if 0
13837 +/* Call type_mark_used for any TYPEs referenced from this GDB source file.  */
13838 +
13839 +static void
13840 +value_types_mark_used (void)
13841 +{
13842 +  struct internalvar *var;
13843 +  struct value_history_chunk *chunk;
13844 +
13845 +  for (var = internalvars; var != NULL; var = var->next)
13846 +    switch (var->kind)
13847 +      {
13848 +      case INTERNALVAR_VALUE:
13849 +       type_mark_used (value_type (var->u.value));
13850 +       break;
13851 +
13852 +      case INTERNALVAR_INTEGER:
13853 +       type_mark_used (var->u.integer.type);
13854 +       break;
13855 +      }
13856 +
13857 +  for (chunk = value_history_chain; chunk != NULL; chunk = chunk->next)
13858 +    {
13859 +      int i;
13860 +
13861 +      for (i = 0; i < ARRAY_SIZE (chunk->values); i++)
13862 +       if (chunk->values[i])
13863 +         type_mark_used (value_type (chunk->values[i]));
13864 +    }
13865 +}
13866 +#endif
13867 +
13868  /* The 'function' command.  This does nothing -- it is just a
13869     placeholder to let "help function NAME" work.  This is also used as
13870     the implementation of the sub-command that is created when
13871 @@ -2165,11 +2232,10 @@ preserve_one_value (struct value *value, struct objfile *objfile,
13872                     htab_t copied_types)
13873  {
13874    if (TYPE_OBJFILE (value->type) == objfile)
13875 -    value->type = copy_type_recursive (objfile, value->type, copied_types);
13876 +    value->type = copy_type_recursive (value->type, copied_types);
13877  
13878    if (TYPE_OBJFILE (value->enclosing_type) == objfile)
13879 -    value->enclosing_type = copy_type_recursive (objfile,
13880 -                                                value->enclosing_type,
13881 +    value->enclosing_type = copy_type_recursive (value->enclosing_type,
13882                                                  copied_types);
13883  }
13884  
13885 @@ -2184,7 +2250,7 @@ preserve_one_internalvar (struct internalvar *var, struct objfile *objfile,
13886      case INTERNALVAR_INTEGER:
13887        if (var->u.integer.type && TYPE_OBJFILE (var->u.integer.type) == objfile)
13888         var->u.integer.type
13889 -         = copy_type_recursive (objfile, var->u.integer.type, copied_types);
13890 +         = copy_type_recursive (var->u.integer.type, copied_types);
13891        break;
13892  
13893      case INTERNALVAR_VALUE:
13894 @@ -3276,10 +3342,27 @@ readjust_indirect_value_type (struct value *value, struct type *enc_type,
13895  struct value *
13896  coerce_ref (struct value *arg)
13897  {
13898 -  struct type *value_type_arg_tmp = check_typedef (value_type (arg));
13899 +  struct type *value_type_arg_tmp;
13900    struct value *retval;
13901    struct type *enc_type;
13902  
13903 +  if (TYPE_DYNAMIC (value_type (arg)))
13904 +    {
13905 +      struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
13906 +      CORE_ADDR address;
13907 +
13908 +      value_type_arg_tmp = value_type (arg);
13909 +      address = value_raw_address (arg);
13910 +      value_type_arg_tmp = object_address_get_data (value_type_arg_tmp,
13911 +                                                   &address);
13912 +      if (! value_type_arg_tmp)
13913 +       error (_("Attempt to coerce non-valid value."));
13914 +      arg = value_at_lazy (value_type_arg_tmp, address);
13915 +      do_cleanups (cleanups);
13916 +    }
13917 +  else
13918 +    value_type_arg_tmp = check_typedef (value_type (arg));
13919 +
13920    retval = coerce_ref_if_computed (arg);
13921    if (retval)
13922      return retval;
13923 @@ -3386,4 +3469,10 @@ VARIABLE is already initialized."));
13924    add_prefix_cmd ("function", no_class, function_command, _("\
13925  Placeholder command for showing help on convenience functions."),
13926                   &functionlist, "function ", 0, &cmdlist);
13927 +
13928 +  make_final_cleanup (value_history_cleanup, NULL);
13929 +
13930 +#if 0
13931 +  observer_attach_mark_used (value_types_mark_used);
13932 +#endif
13933  }
13934 diff --git a/gdb/value.h b/gdb/value.h
13935 index d8b157f..f49e827 100644
13936 --- a/gdb/value.h
13937 +++ b/gdb/value.h
13938 @@ -533,6 +533,10 @@ extern struct value *value_from_decfloat (struct type *type,
13939                                           const gdb_byte *decbytes);
13940  extern struct value *value_from_history_ref (char *, char **);
13941  
13942 +extern const char *object_address_data_not_valid (struct type *type);
13943 +extern struct type *object_address_get_data (struct type *type,
13944 +                                            CORE_ADDR *address_return);
13945 +
13946  extern struct value *value_at (struct type *type, CORE_ADDR addr);
13947  extern struct value *value_at_lazy (struct type *type, CORE_ADDR addr);
13948  
13949 diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c
13950 index f16588a..afef615 100644
13951 --- a/gdb/windows-nat.c
13952 +++ b/gdb/windows-nat.c
13953 @@ -752,7 +752,7 @@ windows_make_so (const char *name, LPVOID load_addr)
13954        asection *text = NULL;
13955        CORE_ADDR text_vma;
13956  
13957 -      abfd = bfd_openr (so->so_name, "pei-i386");
13958 +      abfd = gdb_bfd_openr (so->so_name, "pei-i386");
13959  
13960        if (!abfd)
13961         return so;
13962 @@ -762,7 +762,7 @@ windows_make_so (const char *name, LPVOID load_addr)
13963  
13964        if (!text)
13965         {
13966 -         bfd_close (abfd);
13967 +         gdb_bfd_unref (abfd);
13968           return so;
13969         }
13970  
13971 @@ -773,7 +773,7 @@ windows_make_so (const char *name, LPVOID load_addr)
13972                                                    load_addr + 0x1000);
13973        cygwin_load_end = cygwin_load_start + bfd_section_size (abfd, text);
13974  
13975 -      bfd_close (abfd);
13976 +      gdb_bfd_unref (abfd);
13977      }
13978  #endif
13979  
This page took 1.182577 seconds and 3 git commands to generate.