xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/dwarf2/section.c (revision 6881a4007f077b54e5f51159c52b9b25f57deb0d)
17d62b00eSchristos /* DWARF 2 low-level section code
27d62b00eSchristos 
3*6881a400Schristos    Copyright (C) 1994-2023 Free Software Foundation, Inc.
47d62b00eSchristos 
57d62b00eSchristos    Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
67d62b00eSchristos    Inc.  with support from Florida State University (under contract
77d62b00eSchristos    with the Ada Joint Program Office), and Silicon Graphics, Inc.
87d62b00eSchristos    Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
97d62b00eSchristos    based on Fred Fish's (Cygnus Support) implementation of DWARF 1
107d62b00eSchristos    support.
117d62b00eSchristos 
127d62b00eSchristos    This file is part of GDB.
137d62b00eSchristos 
147d62b00eSchristos    This program is free software; you can redistribute it and/or modify
157d62b00eSchristos    it under the terms of the GNU General Public License as published by
167d62b00eSchristos    the Free Software Foundation; either version 3 of the License, or
177d62b00eSchristos    (at your option) any later version.
187d62b00eSchristos 
197d62b00eSchristos    This program is distributed in the hope that it will be useful,
207d62b00eSchristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
217d62b00eSchristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
227d62b00eSchristos    GNU General Public License for more details.
237d62b00eSchristos 
247d62b00eSchristos    You should have received a copy of the GNU General Public License
257d62b00eSchristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
267d62b00eSchristos 
277d62b00eSchristos #include "defs.h"
287d62b00eSchristos #include "dwarf2/section.h"
297d62b00eSchristos #include "gdb_bfd.h"
307d62b00eSchristos #include "objfiles.h"
317d62b00eSchristos #include "complaints.h"
327d62b00eSchristos 
337d62b00eSchristos void
347d62b00eSchristos dwarf2_section_info::overflow_complaint () const
357d62b00eSchristos {
367d62b00eSchristos   complaint (_("debug info runs off end of %s section"
377d62b00eSchristos 	       " [in module %s]"),
387d62b00eSchristos 	     get_name (), get_file_name ());
397d62b00eSchristos }
407d62b00eSchristos 
417d62b00eSchristos struct dwarf2_section_info *
427d62b00eSchristos dwarf2_section_info::get_containing_section () const
437d62b00eSchristos {
447d62b00eSchristos   gdb_assert (is_virtual);
457d62b00eSchristos   return s.containing_section;
467d62b00eSchristos }
477d62b00eSchristos 
487d62b00eSchristos struct bfd *
497d62b00eSchristos dwarf2_section_info::get_bfd_owner () const
507d62b00eSchristos {
517d62b00eSchristos   const dwarf2_section_info *section = this;
527d62b00eSchristos   if (is_virtual)
537d62b00eSchristos     {
547d62b00eSchristos       section = get_containing_section ();
557d62b00eSchristos       gdb_assert (!section->is_virtual);
567d62b00eSchristos     }
57*6881a400Schristos   gdb_assert (section->s.section != nullptr);
587d62b00eSchristos   return section->s.section->owner;
597d62b00eSchristos }
607d62b00eSchristos 
617d62b00eSchristos asection *
627d62b00eSchristos dwarf2_section_info::get_bfd_section () const
637d62b00eSchristos {
647d62b00eSchristos   const dwarf2_section_info *section = this;
657d62b00eSchristos   if (section->is_virtual)
667d62b00eSchristos     {
677d62b00eSchristos       section = get_containing_section ();
687d62b00eSchristos       gdb_assert (!section->is_virtual);
697d62b00eSchristos     }
707d62b00eSchristos   return section->s.section;
717d62b00eSchristos }
727d62b00eSchristos 
737d62b00eSchristos const char *
747d62b00eSchristos dwarf2_section_info::get_name () const
757d62b00eSchristos {
767d62b00eSchristos   asection *sectp = get_bfd_section ();
777d62b00eSchristos 
787d62b00eSchristos   gdb_assert (sectp != NULL);
797d62b00eSchristos   return bfd_section_name (sectp);
807d62b00eSchristos }
817d62b00eSchristos 
827d62b00eSchristos const char *
837d62b00eSchristos dwarf2_section_info::get_file_name () const
847d62b00eSchristos {
857d62b00eSchristos   bfd *abfd = get_bfd_owner ();
867d62b00eSchristos 
87*6881a400Schristos   gdb_assert (abfd != nullptr);
887d62b00eSchristos   return bfd_get_filename (abfd);
897d62b00eSchristos }
907d62b00eSchristos 
917d62b00eSchristos int
927d62b00eSchristos dwarf2_section_info::get_id () const
937d62b00eSchristos {
947d62b00eSchristos   asection *sectp = get_bfd_section ();
957d62b00eSchristos 
967d62b00eSchristos   if (sectp == NULL)
977d62b00eSchristos     return 0;
987d62b00eSchristos   return sectp->id;
997d62b00eSchristos }
1007d62b00eSchristos 
1017d62b00eSchristos int
1027d62b00eSchristos dwarf2_section_info::get_flags () const
1037d62b00eSchristos {
1047d62b00eSchristos   asection *sectp = get_bfd_section ();
1057d62b00eSchristos 
1067d62b00eSchristos   gdb_assert (sectp != NULL);
1077d62b00eSchristos   return bfd_section_flags (sectp);
1087d62b00eSchristos }
1097d62b00eSchristos 
1107d62b00eSchristos bool
1117d62b00eSchristos dwarf2_section_info::empty () const
1127d62b00eSchristos {
1137d62b00eSchristos   if (is_virtual)
1147d62b00eSchristos     return size == 0;
1157d62b00eSchristos   return s.section == NULL || size == 0;
1167d62b00eSchristos }
1177d62b00eSchristos 
1187d62b00eSchristos void
1197d62b00eSchristos dwarf2_section_info::read (struct objfile *objfile)
1207d62b00eSchristos {
1217d62b00eSchristos   asection *sectp;
1227d62b00eSchristos   bfd *abfd;
1237d62b00eSchristos   gdb_byte *buf, *retbuf;
1247d62b00eSchristos 
1257d62b00eSchristos   if (readin)
1267d62b00eSchristos     return;
1277d62b00eSchristos   buffer = NULL;
1287d62b00eSchristos   readin = true;
1297d62b00eSchristos 
1307d62b00eSchristos   if (empty ())
1317d62b00eSchristos     return;
1327d62b00eSchristos 
1337d62b00eSchristos   sectp = get_bfd_section ();
1347d62b00eSchristos 
1357d62b00eSchristos   /* If this is a virtual section we need to read in the real one first.  */
1367d62b00eSchristos   if (is_virtual)
1377d62b00eSchristos     {
1387d62b00eSchristos       struct dwarf2_section_info *containing_section =
1397d62b00eSchristos 	get_containing_section ();
1407d62b00eSchristos 
1417d62b00eSchristos       gdb_assert (sectp != NULL);
1427d62b00eSchristos       if ((sectp->flags & SEC_RELOC) != 0)
1437d62b00eSchristos 	{
1447d62b00eSchristos 	  error (_("Dwarf Error: DWP format V2 with relocations is not"
1457d62b00eSchristos 		   " supported in section %s [in module %s]"),
1467d62b00eSchristos 		 get_name (), get_file_name ());
1477d62b00eSchristos 	}
1487d62b00eSchristos       containing_section->read (objfile);
1497d62b00eSchristos       /* Other code should have already caught virtual sections that don't
1507d62b00eSchristos 	 fit.  */
1517d62b00eSchristos       gdb_assert (virtual_offset + size <= containing_section->size);
1527d62b00eSchristos       /* If the real section is empty or there was a problem reading the
1537d62b00eSchristos 	 section we shouldn't get here.  */
1547d62b00eSchristos       gdb_assert (containing_section->buffer != NULL);
1557d62b00eSchristos       buffer = containing_section->buffer + virtual_offset;
1567d62b00eSchristos       return;
1577d62b00eSchristos     }
1587d62b00eSchristos 
1597d62b00eSchristos   /* If the section has relocations, we must read it ourselves.
1607d62b00eSchristos      Otherwise we attach it to the BFD.  */
1617d62b00eSchristos   if ((sectp->flags & SEC_RELOC) == 0)
1627d62b00eSchristos     {
1637d62b00eSchristos       buffer = gdb_bfd_map_section (sectp, &size);
1647d62b00eSchristos       return;
1657d62b00eSchristos     }
1667d62b00eSchristos 
1677d62b00eSchristos   buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, size);
1687d62b00eSchristos   buffer = buf;
1697d62b00eSchristos 
1707d62b00eSchristos   /* When debugging .o files, we may need to apply relocations; see
1717d62b00eSchristos      http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1727d62b00eSchristos      We never compress sections in .o files, so we only need to
1737d62b00eSchristos      try this when the section is not compressed.  */
1747d62b00eSchristos   retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1757d62b00eSchristos   if (retbuf != NULL)
1767d62b00eSchristos     {
1777d62b00eSchristos       buffer = retbuf;
1787d62b00eSchristos       return;
1797d62b00eSchristos     }
1807d62b00eSchristos 
1817d62b00eSchristos   abfd = get_bfd_owner ();
1827d62b00eSchristos   gdb_assert (abfd != NULL);
1837d62b00eSchristos 
1847d62b00eSchristos   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1857d62b00eSchristos       || bfd_bread (buf, size, abfd) != size)
1867d62b00eSchristos     {
1877d62b00eSchristos       error (_("Dwarf Error: Can't read DWARF data"
1887d62b00eSchristos 	       " in section %s [in module %s]"),
1897d62b00eSchristos 	     bfd_section_name (sectp), bfd_get_filename (abfd));
1907d62b00eSchristos     }
1917d62b00eSchristos }
1927d62b00eSchristos 
1937d62b00eSchristos const char *
1947d62b00eSchristos dwarf2_section_info::read_string (struct objfile *objfile, LONGEST str_offset,
1957d62b00eSchristos 				  const char *form_name)
1967d62b00eSchristos {
1977d62b00eSchristos   read (objfile);
1987d62b00eSchristos   if (buffer == NULL)
199*6881a400Schristos     {
200*6881a400Schristos       if (get_bfd_section () == nullptr)
201*6881a400Schristos 	error (_("Dwarf Error: %s used without required section"),
202*6881a400Schristos 	       form_name);
203*6881a400Schristos       else
204*6881a400Schristos 	error (_("Dwarf Error: %s used without %s section [in module %s]"),
2057d62b00eSchristos 	       form_name, get_name (), get_file_name ());
206*6881a400Schristos     }
2077d62b00eSchristos   if (str_offset >= size)
2087d62b00eSchristos     error (_("%s pointing outside of %s section [in module %s]"),
2097d62b00eSchristos 	   form_name, get_name (), get_file_name ());
2107d62b00eSchristos   gdb_assert (HOST_CHAR_BIT == 8);
2117d62b00eSchristos   if (buffer[str_offset] == '\0')
2127d62b00eSchristos     return NULL;
2137d62b00eSchristos   return (const char *) (buffer + str_offset);
2147d62b00eSchristos }
215