Home | History | Annotate | Line # | Download | only in dwarf2
      1 /* Low-level DWARF 2 reading code
      2 
      3    Copyright (C) 1994-2024 Free Software Foundation, Inc.
      4 
      5    Adapted by Gary Funck (gary (at) 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 #ifndef GDB_DWARF2_LEB_H
     28 #define GDB_DWARF2_LEB_H
     29 
     30 /* Read dwarf information from a buffer.  */
     31 
     32 static inline unsigned int
     33 read_1_byte (bfd *abfd, const gdb_byte *buf)
     34 {
     35   return bfd_get_8 (abfd, buf);
     36 }
     37 
     38 static inline int
     39 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
     40 {
     41   return bfd_get_signed_8 (abfd, buf);
     42 }
     43 
     44 static inline unsigned int
     45 read_2_bytes (bfd *abfd, const gdb_byte *buf)
     46 {
     47   return bfd_get_16 (abfd, buf);
     48 }
     49 
     50 static inline int
     51 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
     52 {
     53   return bfd_get_signed_16 (abfd, buf);
     54 }
     55 
     56 /* Read the next three bytes (little-endian order) as an unsigned integer.  */
     57 static inline unsigned int
     58 read_3_bytes (bfd *abfd, const gdb_byte *buf)
     59 {
     60   return bfd_get_24 (abfd, buf);
     61 }
     62 
     63 static inline unsigned int
     64 read_4_bytes (bfd *abfd, const gdb_byte *buf)
     65 {
     66   return bfd_get_32 (abfd, buf);
     67 }
     68 
     69 static inline int
     70 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
     71 {
     72   return bfd_get_signed_32 (abfd, buf);
     73 }
     74 
     75 static inline ULONGEST
     76 read_8_bytes (bfd *abfd, const gdb_byte *buf)
     77 {
     78   return bfd_get_64 (abfd, buf);
     79 }
     80 
     81 extern LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
     82 
     83 extern ULONGEST read_unsigned_leb128 (bfd *, const gdb_byte *, unsigned int *);
     84 
     85 /* Read the initial length from a section.  The (draft) DWARF 3
     86    specification allows the initial length to take up either 4 bytes
     87    or 12 bytes.  If the first 4 bytes are 0xffffffff, then the next 8
     88    bytes describe the length and all offsets will be 8 bytes in length
     89    instead of 4.
     90 
     91    An older, non-standard 64-bit format is also handled by this
     92    function.  The older format in question stores the initial length
     93    as an 8-byte quantity without an escape value.  Lengths greater
     94    than 2^32 aren't very common which means that the initial 4 bytes
     95    is almost always zero.  Since a length value of zero doesn't make
     96    sense for the 32-bit format, this initial zero can be considered to
     97    be an escape value which indicates the presence of the older 64-bit
     98    format.  As written, the code can't detect (old format) lengths
     99    greater than 4GB.  If it becomes necessary to handle lengths
    100    somewhat larger than 4GB, we could allow other small values (such
    101    as the non-sensical values of 1, 2, and 3) to also be used as
    102    escape values indicating the presence of the old format.
    103 
    104    The value returned via bytes_read should be used to increment the
    105    relevant pointer after calling read_initial_length().
    106 
    107    [ Note:  read_initial_length() and read_offset() are based on the
    108      document entitled "DWARF Debugging Information Format", revision
    109      3, draft 8, dated November 19, 2001.  This document was obtained
    110      from:
    111 
    112 	http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
    113 
    114      This document is only a draft and is subject to change.  (So beware.)
    115 
    116      Details regarding the older, non-standard 64-bit format were
    117      determined empirically by examining 64-bit ELF files produced by
    118      the SGI toolchain on an IRIX 6.5 machine.
    119 
    120      - Kevin, July 16, 2002
    121    ] */
    122 extern LONGEST read_initial_length (bfd *abfd, const gdb_byte *buf,
    123 				    unsigned int *bytes_read,
    124 				    bool handle_nonstd = true);
    125 
    126 /* Read an offset from the data stream.  */
    127 extern LONGEST read_offset (bfd *abfd, const gdb_byte *buf,
    128 			    unsigned int offset_size);
    129 
    130 static inline const gdb_byte *
    131 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
    132 {
    133   /* If the size of a host char is 8 bits, we can return a pointer
    134      to the buffer, otherwise we have to copy the data to a buffer
    135      allocated on the temporary obstack.  */
    136   gdb_assert (HOST_CHAR_BIT == 8);
    137   return buf;
    138 }
    139 
    140 static inline const char *
    141 read_direct_string (bfd *abfd, const gdb_byte *buf,
    142 		    unsigned int *bytes_read_ptr)
    143 {
    144   /* If the size of a host char is 8 bits, we can return a pointer
    145      to the string, otherwise we have to copy the string to a buffer
    146      allocated on the temporary obstack.  */
    147   gdb_assert (HOST_CHAR_BIT == 8);
    148   if (*buf == '\0')
    149     {
    150       *bytes_read_ptr = 1;
    151       return NULL;
    152     }
    153   *bytes_read_ptr = strlen ((const char *) buf) + 1;
    154   return (const char *) buf;
    155 }
    156 
    157 #endif /* GDB_DWARF2_LEB_H */
    158