GDB (xrefs)
Loading...
Searching...
No Matches
section.c
Go to the documentation of this file.
1/* DWARF 2 low-level section code
2
3 Copyright (C) 1994-2023 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#include "complaints.h"
32
33void
35{
36 complaint (_("debug info runs off end of %s section"
37 " [in module %s]"),
38 get_name (), get_file_name ());
39}
40
43{
44 gdb_assert (is_virtual);
45 return s.containing_section;
46}
47
48struct bfd *
50{
51 const dwarf2_section_info *section = this;
52 if (is_virtual)
53 {
55 gdb_assert (!section->is_virtual);
56 }
57 gdb_assert (section->s.section != nullptr);
58 return section->s.section->owner;
59}
60
61asection *
63{
64 const dwarf2_section_info *section = this;
65 if (section->is_virtual)
66 {
68 gdb_assert (!section->is_virtual);
69 }
70 return section->s.section;
71}
72
73const char *
75{
76 asection *sectp = get_bfd_section ();
77
78 gdb_assert (sectp != NULL);
79 return bfd_section_name (sectp);
80}
81
82const char *
84{
85 bfd *abfd = get_bfd_owner ();
86
87 gdb_assert (abfd != nullptr);
88 return bfd_get_filename (abfd);
89}
90
91int
93{
94 asection *sectp = get_bfd_section ();
95
96 if (sectp == NULL)
97 return 0;
98 return sectp->id;
99}
100
101int
103{
104 asection *sectp = get_bfd_section ();
105
106 gdb_assert (sectp != NULL);
107 return bfd_section_flags (sectp);
108}
109
110bool
112{
113 if (is_virtual)
114 return size == 0;
115 return s.section == NULL || size == 0;
116}
117
118void
120{
121 asection *sectp;
122 bfd *abfd;
123 gdb_byte *buf, *retbuf;
124
125 if (readin)
126 return;
127 buffer = NULL;
128 readin = true;
129
130 if (empty ())
131 return;
132
133 sectp = get_bfd_section ();
134
135 /* If this is a virtual section we need to read in the real one first. */
136 if (is_virtual)
137 {
140
141 gdb_assert (sectp != NULL);
142 if ((sectp->flags & SEC_RELOC) != 0)
143 {
144 error (_("Dwarf Error: DWP format V2 with relocations is not"
145 " supported in section %s [in module %s]"),
146 get_name (), get_file_name ());
147 }
149 /* Other code should have already caught virtual sections that don't
150 fit. */
151 gdb_assert (virtual_offset + size <= containing_section->size);
152 /* If the real section is empty or there was a problem reading the
153 section we shouldn't get here. */
154 gdb_assert (containing_section->buffer != NULL);
156 return;
157 }
158
159 /* If the section has relocations, we must read it ourselves.
160 Otherwise we attach it to the BFD. */
161 if ((sectp->flags & SEC_RELOC) == 0)
162 {
163 buffer = gdb_bfd_map_section (sectp, &size);
164 return;
165 }
166
167 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, size);
168 buffer = buf;
169
170 /* When debugging .o files, we may need to apply relocations; see
171 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
172 We never compress sections in .o files, so we only need to
173 try this when the section is not compressed. */
174 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
175 if (retbuf != NULL)
176 {
177 buffer = retbuf;
178 return;
179 }
180
181 abfd = get_bfd_owner ();
182 gdb_assert (abfd != NULL);
183
184 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
185 || bfd_read (buf, size, abfd) != size)
186 {
187 error (_("Dwarf Error: Can't read DWARF data"
188 " in section %s [in module %s]"),
189 bfd_section_name (sectp), bfd_get_filename (abfd));
190 }
191}
192
193const char *
195 const char *form_name)
196{
197 read (objfile);
198 if (buffer == NULL)
199 {
200 if (get_bfd_section () == nullptr)
201 error (_("Dwarf Error: %s used without required section"),
202 form_name);
203 else
204 error (_("Dwarf Error: %s used without %s section [in module %s]"),
205 form_name, get_name (), get_file_name ());
206 }
207 if (str_offset >= size)
208 error (_("%s pointing outside of %s section [in module %s]"),
209 form_name, get_name (), get_file_name ());
210 gdb_assert (HOST_CHAR_BIT == 8);
211 if (buffer[str_offset] == '\0')
212 return NULL;
213 return (const char *) (buffer + str_offset);
214}
#define complaint(FMT,...)
Definition complaints.h:47
#define SEEK_SET
Definition defs.h:101
const gdb_byte * gdb_bfd_map_section(asection *sectp, bfd_size_type *size)
Definition gdb_bfd.c:725
void read(struct objfile *objfile)
Definition section.c:119
bool empty() const
Definition section.c:111
bfd_size_type virtual_offset
Definition section.h:120
const char * get_name() const
Definition section.c:74
asection * get_bfd_section() const
Definition section.c:62
void overflow_complaint() const
Definition section.c:34
const char * read_string(struct objfile *objfile, LONGEST str_offset, const char *form_name)
Definition section.c:194
struct dwarf2_section_info * containing_section
Definition section.h:112
const gdb_byte * buffer
Definition section.h:115
const char * get_file_name() const
Definition section.c:83
bfd_size_type size
Definition section.h:117
union dwarf2_section_info::@54 s
int get_id() const
Definition section.c:92
struct bfd * get_bfd_owner() const
Definition section.c:49
struct dwarf2_section_info * get_containing_section() const
Definition section.c:42
int get_flags() const
Definition section.c:102
asection * section
Definition section.h:109
auto_obstack objfile_obstack
Definition objfiles.h:760
bfd_byte * symfile_relocate_debug_section(struct objfile *objfile, asection *sectp, bfd_byte *buf)
Definition symfile.c:3633