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