leb.c revision 1.1.1.3 1 1.1 christos /* Low-level DWARF 2 reading code
2 1.1 christos
3 1.1.1.3 christos Copyright (C) 1994-2024 Free Software Foundation, Inc.
4 1.1 christos
5 1.1 christos Adapted by Gary Funck (gary (at) intrepid.com), Intrepid Technology,
6 1.1 christos Inc. with support from Florida State University (under contract
7 1.1 christos with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 1.1 christos Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 1.1 christos based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 1.1 christos support.
11 1.1 christos
12 1.1 christos This file is part of GDB.
13 1.1 christos
14 1.1 christos This program is free software; you can redistribute it and/or modify
15 1.1 christos it under the terms of the GNU General Public License as published by
16 1.1 christos the Free Software Foundation; either version 3 of the License, or
17 1.1 christos (at your option) any later version.
18 1.1 christos
19 1.1 christos This program is distributed in the hope that it will be useful,
20 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
21 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 1.1 christos GNU General Public License for more details.
23 1.1 christos
24 1.1 christos You should have received a copy of the GNU General Public License
25 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */
26 1.1 christos
27 1.1 christos #include "dwarf2/leb.h"
28 1.1 christos
29 1.1 christos ULONGEST
30 1.1 christos read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
31 1.1 christos unsigned int *bytes_read_ptr)
32 1.1 christos {
33 1.1 christos ULONGEST result;
34 1.1 christos unsigned int num_read;
35 1.1 christos int shift;
36 1.1 christos unsigned char byte;
37 1.1 christos
38 1.1 christos result = 0;
39 1.1 christos shift = 0;
40 1.1 christos num_read = 0;
41 1.1 christos while (1)
42 1.1 christos {
43 1.1 christos byte = bfd_get_8 (abfd, buf);
44 1.1 christos buf++;
45 1.1 christos num_read++;
46 1.1 christos result |= ((ULONGEST) (byte & 127) << shift);
47 1.1 christos if ((byte & 128) == 0)
48 1.1 christos {
49 1.1 christos break;
50 1.1 christos }
51 1.1 christos shift += 7;
52 1.1 christos }
53 1.1 christos *bytes_read_ptr = num_read;
54 1.1 christos return result;
55 1.1 christos }
56 1.1 christos
57 1.1 christos LONGEST
58 1.1 christos read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
59 1.1 christos unsigned int *bytes_read_ptr)
60 1.1 christos {
61 1.1 christos ULONGEST result;
62 1.1 christos int shift, num_read;
63 1.1 christos unsigned char byte;
64 1.1 christos
65 1.1 christos result = 0;
66 1.1 christos shift = 0;
67 1.1 christos num_read = 0;
68 1.1 christos while (1)
69 1.1 christos {
70 1.1 christos byte = bfd_get_8 (abfd, buf);
71 1.1 christos buf++;
72 1.1 christos num_read++;
73 1.1 christos result |= ((ULONGEST) (byte & 127) << shift);
74 1.1 christos shift += 7;
75 1.1 christos if ((byte & 128) == 0)
76 1.1 christos {
77 1.1 christos break;
78 1.1 christos }
79 1.1 christos }
80 1.1 christos if ((shift < 8 * sizeof (result)) && (byte & 0x40))
81 1.1 christos result |= -(((ULONGEST) 1) << shift);
82 1.1 christos *bytes_read_ptr = num_read;
83 1.1 christos return result;
84 1.1 christos }
85 1.1 christos
86 1.1 christos /* See leb.h. */
87 1.1 christos
88 1.1 christos LONGEST
89 1.1 christos read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read,
90 1.1 christos bool handle_nonstd)
91 1.1 christos {
92 1.1 christos LONGEST length = bfd_get_32 (abfd, buf);
93 1.1 christos
94 1.1 christos if (length == 0xffffffff)
95 1.1 christos {
96 1.1 christos length = bfd_get_64 (abfd, buf + 4);
97 1.1 christos *bytes_read = 12;
98 1.1 christos }
99 1.1 christos else if (handle_nonstd && length == 0)
100 1.1 christos {
101 1.1 christos /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
102 1.1 christos length = bfd_get_64 (abfd, buf);
103 1.1 christos *bytes_read = 8;
104 1.1 christos }
105 1.1 christos else
106 1.1 christos {
107 1.1 christos *bytes_read = 4;
108 1.1 christos }
109 1.1 christos
110 1.1 christos return length;
111 1.1 christos }
112 1.1 christos
113 1.1 christos /* See leb.h. */
114 1.1 christos
115 1.1 christos LONGEST
116 1.1 christos read_offset (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
117 1.1 christos {
118 1.1 christos LONGEST retval = 0;
119 1.1 christos
120 1.1 christos switch (offset_size)
121 1.1 christos {
122 1.1 christos case 4:
123 1.1 christos retval = bfd_get_32 (abfd, buf);
124 1.1 christos break;
125 1.1 christos case 8:
126 1.1 christos retval = bfd_get_64 (abfd, buf);
127 1.1 christos break;
128 1.1 christos default:
129 1.1.1.2 christos internal_error (_("read_offset_1: bad switch [in module %s]"),
130 1.1 christos bfd_get_filename (abfd));
131 1.1 christos }
132 1.1 christos
133 1.1 christos return retval;
134 1.1 christos }
135