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