Commit | Line | Data |
---|---|---|
2c86cff9 TT |
1 | /* DWARF 2 low-level section code |
2 | ||
3 | Copyright (C) 1994-2020 Free Software Foundation, Inc. | |
4 | ||
5 | Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology, | |
6 | Inc. with support from Florida State University (under contract | |
7 | with the Ada Joint Program Office), and Silicon Graphics, Inc. | |
8 | Initial contribution by Brent Benson, Harris Computer Systems, Inc., | |
9 | based on Fred Fish's (Cygnus Support) implementation of DWARF 1 | |
10 | support. | |
11 | ||
12 | This file is part of GDB. | |
13 | ||
14 | This program is free software; you can redistribute it and/or modify | |
15 | it under the terms of the GNU General Public License as published by | |
16 | the Free Software Foundation; either version 3 of the License, or | |
17 | (at your option) any later version. | |
18 | ||
19 | This program is distributed in the hope that it will be useful, | |
20 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
22 | GNU General Public License for more details. | |
23 | ||
24 | You should have received a copy of the GNU General Public License | |
25 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
26 | ||
27 | #include "defs.h" | |
28 | #include "dwarf2/section.h" | |
29 | #include "gdb_bfd.h" | |
30 | #include "objfiles.h" | |
3d27bbdb TT |
31 | #include "complaints.h" |
32 | ||
33 | void | |
34 | dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section) | |
35 | { | |
36 | complaint (_("debug info runs off end of %s section" | |
37 | " [in module %s]"), | |
38 | section->get_name (), | |
39 | section->get_file_name ()); | |
40 | } | |
2c86cff9 TT |
41 | |
42 | struct dwarf2_section_info * | |
96b79293 | 43 | dwarf2_section_info::get_containing_section () const |
2c86cff9 | 44 | { |
96b79293 TT |
45 | gdb_assert (is_virtual); |
46 | return s.containing_section; | |
2c86cff9 TT |
47 | } |
48 | ||
49 | struct bfd * | |
96b79293 | 50 | dwarf2_section_info::get_bfd_owner () const |
2c86cff9 | 51 | { |
96b79293 TT |
52 | const dwarf2_section_info *section = this; |
53 | if (is_virtual) | |
2c86cff9 | 54 | { |
96b79293 | 55 | section = get_containing_section (); |
2c86cff9 TT |
56 | gdb_assert (!section->is_virtual); |
57 | } | |
58 | return section->s.section->owner; | |
59 | } | |
60 | ||
61 | asection * | |
96b79293 | 62 | dwarf2_section_info::get_bfd_section () const |
2c86cff9 | 63 | { |
96b79293 | 64 | const dwarf2_section_info *section = this; |
2c86cff9 TT |
65 | if (section->is_virtual) |
66 | { | |
96b79293 | 67 | section = get_containing_section (); |
2c86cff9 TT |
68 | gdb_assert (!section->is_virtual); |
69 | } | |
70 | return section->s.section; | |
71 | } | |
72 | ||
73 | const char * | |
96b79293 | 74 | dwarf2_section_info::get_name () const |
2c86cff9 | 75 | { |
96b79293 | 76 | asection *sectp = get_bfd_section (); |
2c86cff9 TT |
77 | |
78 | gdb_assert (sectp != NULL); | |
79 | return bfd_section_name (sectp); | |
80 | } | |
81 | ||
82 | const char * | |
96b79293 | 83 | dwarf2_section_info::get_file_name () const |
2c86cff9 | 84 | { |
96b79293 | 85 | bfd *abfd = get_bfd_owner (); |
2c86cff9 TT |
86 | |
87 | return bfd_get_filename (abfd); | |
88 | } | |
89 | ||
90 | int | |
96b79293 | 91 | dwarf2_section_info::get_id () const |
2c86cff9 | 92 | { |
96b79293 | 93 | asection *sectp = get_bfd_section (); |
2c86cff9 TT |
94 | |
95 | if (sectp == NULL) | |
96 | return 0; | |
97 | return sectp->id; | |
98 | } | |
99 | ||
100 | int | |
96b79293 | 101 | dwarf2_section_info::get_flags () const |
2c86cff9 | 102 | { |
96b79293 | 103 | asection *sectp = get_bfd_section (); |
2c86cff9 TT |
104 | |
105 | gdb_assert (sectp != NULL); | |
106 | return bfd_section_flags (sectp); | |
107 | } | |
108 | ||
96b79293 TT |
109 | bool |
110 | dwarf2_section_info::empty () const | |
2c86cff9 | 111 | { |
96b79293 TT |
112 | if (is_virtual) |
113 | return size == 0; | |
114 | return s.section == NULL || size == 0; | |
2c86cff9 TT |
115 | } |
116 | ||
117 | void | |
96b79293 | 118 | dwarf2_section_info::read (struct objfile *objfile) |
2c86cff9 TT |
119 | { |
120 | asection *sectp; | |
121 | bfd *abfd; | |
122 | gdb_byte *buf, *retbuf; | |
123 | ||
96b79293 | 124 | if (readin) |
2c86cff9 | 125 | return; |
96b79293 TT |
126 | buffer = NULL; |
127 | readin = true; | |
2c86cff9 | 128 | |
96b79293 | 129 | if (empty ()) |
2c86cff9 TT |
130 | return; |
131 | ||
96b79293 | 132 | sectp = get_bfd_section (); |
2c86cff9 TT |
133 | |
134 | /* If this is a virtual section we need to read in the real one first. */ | |
96b79293 | 135 | if (is_virtual) |
2c86cff9 TT |
136 | { |
137 | struct dwarf2_section_info *containing_section = | |
96b79293 | 138 | get_containing_section (); |
2c86cff9 TT |
139 | |
140 | gdb_assert (sectp != NULL); | |
141 | if ((sectp->flags & SEC_RELOC) != 0) | |
142 | { | |
143 | error (_("Dwarf Error: DWP format V2 with relocations is not" | |
144 | " supported in section %s [in module %s]"), | |
96b79293 | 145 | get_name (), get_file_name ()); |
2c86cff9 | 146 | } |
96b79293 | 147 | containing_section->read (objfile); |
2c86cff9 TT |
148 | /* Other code should have already caught virtual sections that don't |
149 | fit. */ | |
96b79293 | 150 | gdb_assert (virtual_offset + size <= containing_section->size); |
2c86cff9 TT |
151 | /* If the real section is empty or there was a problem reading the |
152 | section we shouldn't get here. */ | |
153 | gdb_assert (containing_section->buffer != NULL); | |
96b79293 | 154 | buffer = containing_section->buffer + virtual_offset; |
2c86cff9 TT |
155 | return; |
156 | } | |
157 | ||
158 | /* If the section has relocations, we must read it ourselves. | |
159 | Otherwise we attach it to the BFD. */ | |
160 | if ((sectp->flags & SEC_RELOC) == 0) | |
161 | { | |
96b79293 | 162 | buffer = gdb_bfd_map_section (sectp, &size); |
2c86cff9 TT |
163 | return; |
164 | } | |
165 | ||
96b79293 TT |
166 | buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, size); |
167 | buffer = buf; | |
2c86cff9 TT |
168 | |
169 | /* When debugging .o files, we may need to apply relocations; see | |
170 | http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html . | |
171 | We never compress sections in .o files, so we only need to | |
172 | try this when the section is not compressed. */ | |
173 | retbuf = symfile_relocate_debug_section (objfile, sectp, buf); | |
174 | if (retbuf != NULL) | |
175 | { | |
96b79293 | 176 | buffer = retbuf; |
2c86cff9 TT |
177 | return; |
178 | } | |
179 | ||
96b79293 | 180 | abfd = get_bfd_owner (); |
2c86cff9 TT |
181 | gdb_assert (abfd != NULL); |
182 | ||
183 | if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0 | |
96b79293 | 184 | || bfd_bread (buf, size, abfd) != size) |
2c86cff9 TT |
185 | { |
186 | error (_("Dwarf Error: Can't read DWARF data" | |
187 | " in section %s [in module %s]"), | |
188 | bfd_section_name (sectp), bfd_get_filename (abfd)); | |
189 | } | |
190 | } |