Add translations for various sub-directories
[binutils-gdb.git] / gdb / dwarf2 / leb.c
blob1b7202e6240e5b6f35e8141ac86acd3b706d235c
1 /* Low-level DWARF 2 reading code
3 Copyright (C) 1994-2024 Free Software Foundation, Inc.
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.
12 This file is part of GDB.
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.
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.
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/>. */
27 #include "dwarf2/leb.h"
29 ULONGEST
30 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
31 unsigned int *bytes_read_ptr)
33 ULONGEST result;
34 unsigned int num_read;
35 int shift;
36 unsigned char byte;
38 result = 0;
39 shift = 0;
40 num_read = 0;
41 while (1)
43 byte = bfd_get_8 (abfd, buf);
44 buf++;
45 num_read++;
46 result |= ((ULONGEST) (byte & 127) << shift);
47 if ((byte & 128) == 0)
49 break;
51 shift += 7;
53 *bytes_read_ptr = num_read;
54 return result;
57 LONGEST
58 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
59 unsigned int *bytes_read_ptr)
61 ULONGEST result;
62 int shift, num_read;
63 unsigned char byte;
65 result = 0;
66 shift = 0;
67 num_read = 0;
68 while (1)
70 byte = bfd_get_8 (abfd, buf);
71 buf++;
72 num_read++;
73 result |= ((ULONGEST) (byte & 127) << shift);
74 shift += 7;
75 if ((byte & 128) == 0)
77 break;
80 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
81 result |= -(((ULONGEST) 1) << shift);
82 *bytes_read_ptr = num_read;
83 return result;
86 /* See leb.h. */
88 LONGEST
89 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read,
90 bool handle_nonstd)
92 LONGEST length = bfd_get_32 (abfd, buf);
94 if (length == 0xffffffff)
96 length = bfd_get_64 (abfd, buf + 4);
97 *bytes_read = 12;
99 else if (handle_nonstd && length == 0)
101 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
102 length = bfd_get_64 (abfd, buf);
103 *bytes_read = 8;
105 else
107 *bytes_read = 4;
110 return length;
113 /* See leb.h. */
115 LONGEST
116 read_offset (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
118 LONGEST retval = 0;
120 switch (offset_size)
122 case 4:
123 retval = bfd_get_32 (abfd, buf);
124 break;
125 case 8:
126 retval = bfd_get_64 (abfd, buf);
127 break;
128 default:
129 internal_error (_("read_offset_1: bad switch [in module %s]"),
130 bfd_get_filename (abfd));
133 return retval;