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" | |
31 | ||
32 | struct dwarf2_section_info * | |
96b79293 | 33 | dwarf2_section_info::get_containing_section () const |
2c86cff9 | 34 | { |
96b79293 TT |
35 | gdb_assert (is_virtual); |
36 | return s.containing_section; | |
2c86cff9 TT |
37 | } |
38 | ||
39 | struct bfd * | |
96b79293 | 40 | dwarf2_section_info::get_bfd_owner () const |
2c86cff9 | 41 | { |
96b79293 TT |
42 | const dwarf2_section_info *section = this; |
43 | if (is_virtual) | |
2c86cff9 | 44 | { |
96b79293 | 45 | section = get_containing_section (); |
2c86cff9 TT |
46 | gdb_assert (!section->is_virtual); |
47 | } | |
48 | return section->s.section->owner; | |
49 | } | |
50 | ||
51 | asection * | |
96b79293 | 52 | dwarf2_section_info::get_bfd_section () const |
2c86cff9 | 53 | { |
96b79293 | 54 | const dwarf2_section_info *section = this; |
2c86cff9 TT |
55 | if (section->is_virtual) |
56 | { | |
96b79293 | 57 | section = get_containing_section (); |
2c86cff9 TT |
58 | gdb_assert (!section->is_virtual); |
59 | } | |
60 | return section->s.section; | |
61 | } | |
62 | ||
63 | const char * | |
96b79293 | 64 | dwarf2_section_info::get_name () const |
2c86cff9 | 65 | { |
96b79293 | 66 | asection *sectp = get_bfd_section (); |
2c86cff9 TT |
67 | |
68 | gdb_assert (sectp != NULL); | |
69 | return bfd_section_name (sectp); | |
70 | } | |
71 | ||
72 | const char * | |
96b79293 | 73 | dwarf2_section_info::get_file_name () const |
2c86cff9 | 74 | { |
96b79293 | 75 | bfd *abfd = get_bfd_owner (); |
2c86cff9 TT |
76 | |
77 | return bfd_get_filename (abfd); | |
78 | } | |
79 | ||
80 | int | |
96b79293 | 81 | dwarf2_section_info::get_id () const |
2c86cff9 | 82 | { |
96b79293 | 83 | asection *sectp = get_bfd_section (); |
2c86cff9 TT |
84 | |
85 | if (sectp == NULL) | |
86 | return 0; | |
87 | return sectp->id; | |
88 | } | |
89 | ||
90 | int | |
96b79293 | 91 | dwarf2_section_info::get_flags () const |
2c86cff9 | 92 | { |
96b79293 | 93 | asection *sectp = get_bfd_section (); |
2c86cff9 TT |
94 | |
95 | gdb_assert (sectp != NULL); | |
96 | return bfd_section_flags (sectp); | |
97 | } | |
98 | ||
96b79293 TT |
99 | bool |
100 | dwarf2_section_info::empty () const | |
2c86cff9 | 101 | { |
96b79293 TT |
102 | if (is_virtual) |
103 | return size == 0; | |
104 | return s.section == NULL || size == 0; | |
2c86cff9 TT |
105 | } |
106 | ||
107 | void | |
96b79293 | 108 | dwarf2_section_info::read (struct objfile *objfile) |
2c86cff9 TT |
109 | { |
110 | asection *sectp; | |
111 | bfd *abfd; | |
112 | gdb_byte *buf, *retbuf; | |
113 | ||
96b79293 | 114 | if (readin) |
2c86cff9 | 115 | return; |
96b79293 TT |
116 | buffer = NULL; |
117 | readin = true; | |
2c86cff9 | 118 | |
96b79293 | 119 | if (empty ()) |
2c86cff9 TT |
120 | return; |
121 | ||
96b79293 | 122 | sectp = get_bfd_section (); |
2c86cff9 TT |
123 | |
124 | /* If this is a virtual section we need to read in the real one first. */ | |
96b79293 | 125 | if (is_virtual) |
2c86cff9 TT |
126 | { |
127 | struct dwarf2_section_info *containing_section = | |
96b79293 | 128 | get_containing_section (); |
2c86cff9 TT |
129 | |
130 | gdb_assert (sectp != NULL); | |
131 | if ((sectp->flags & SEC_RELOC) != 0) | |
132 | { | |
133 | error (_("Dwarf Error: DWP format V2 with relocations is not" | |
134 | " supported in section %s [in module %s]"), | |
96b79293 | 135 | get_name (), get_file_name ()); |
2c86cff9 | 136 | } |
96b79293 | 137 | containing_section->read (objfile); |
2c86cff9 TT |
138 | /* Other code should have already caught virtual sections that don't |
139 | fit. */ | |
96b79293 | 140 | gdb_assert (virtual_offset + size <= containing_section->size); |
2c86cff9 TT |
141 | /* If the real section is empty or there was a problem reading the |
142 | section we shouldn't get here. */ | |
143 | gdb_assert (containing_section->buffer != NULL); | |
96b79293 | 144 | buffer = containing_section->buffer + virtual_offset; |
2c86cff9 TT |
145 | return; |
146 | } | |
147 | ||
148 | /* If the section has relocations, we must read it ourselves. | |
149 | Otherwise we attach it to the BFD. */ | |
150 | if ((sectp->flags & SEC_RELOC) == 0) | |
151 | { | |
96b79293 | 152 | buffer = gdb_bfd_map_section (sectp, &size); |
2c86cff9 TT |
153 | return; |
154 | } | |
155 | ||
96b79293 TT |
156 | buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, size); |
157 | buffer = buf; | |
2c86cff9 TT |
158 | |
159 | /* When debugging .o files, we may need to apply relocations; see | |
160 | http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html . | |
161 | We never compress sections in .o files, so we only need to | |
162 | try this when the section is not compressed. */ | |
163 | retbuf = symfile_relocate_debug_section (objfile, sectp, buf); | |
164 | if (retbuf != NULL) | |
165 | { | |
96b79293 | 166 | buffer = retbuf; |
2c86cff9 TT |
167 | return; |
168 | } | |
169 | ||
96b79293 | 170 | abfd = get_bfd_owner (); |
2c86cff9 TT |
171 | gdb_assert (abfd != NULL); |
172 | ||
173 | if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0 | |
96b79293 | 174 | || bfd_bread (buf, size, abfd) != size) |
2c86cff9 TT |
175 | { |
176 | error (_("Dwarf Error: Can't read DWARF data" | |
177 | " in section %s [in module %s]"), | |
178 | bfd_section_name (sectp), bfd_get_filename (abfd)); | |
179 | } | |
180 | } |