blob: 54bb39e30fefc5c3fda439ad338eda12a6383e89 [file] [log] [blame]
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001/* Get offset of next compilation unit.
2 Copyright (C) 2000, 2002 Red Hat, Inc.
3 Written by Ulrich Drepper <drepper@redhat.com>, 2000.
4
5 This program is Open Source software; you can redistribute it and/or
6 modify it under the terms of the Open Software License version 1.0 as
7 published by the Open Source Initiative.
8
9 You should have received a copy of the Open Software License along
10 with this program; if not, you may obtain a copy of the Open Software
11 License version 1.0 from http://www.opensource.org/licenses/osl.php or
12 by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
13 3001 King Ranch Road, Ukiah, CA 95482. */
14
15#ifdef HAVE_CONFIG_H
16# include <config.h>
17#endif
18
19#include <assert.h>
20#include <stdlib.h>
21
22#include <libdwarfP.h>
23
24
25/* A good initial size for the abbreviation hashing table. */
26#define DEFAULT_ABBREV_HASH_SIZE 257
27
28
29int
30internal_function
31__libdwarf_get_cu_at_offset (Dwarf_Debug dbg, Dwarf_Unsigned offset,
32 Dwarf_CU_Info *result_cu, Dwarf_Error *error)
33{
34 Dwarf_CU_Info cu;
35 Dwarf_Small *cu_bytes;
36 Dwarf_Unsigned length;
37 Dwarf_Half offset_size;
38
39 /* Make sure there is enough space in the .debug_info section for at
40 least the initial word. We cannot test the rest since we don't
41 know yet whether this is a 64-bit object or not. */
42 if (unlikely (offset + 4 >= dbg->sections[IDX_debug_info].size))
43 {
44 dbg->cu_list_current = NULL;
45 return DW_DLV_NO_ENTRY;
46 }
47
48 /* This points into the .debug_info section to the beginning of the
49 CU entry. */
50 cu_bytes = (Dwarf_Small *) dbg->sections[IDX_debug_info].addr + offset;
51
52 /* The format of the CU header is described in dwarf2p1 7.5.1:
53
54 1. A 4-byte or 12-byte unsigned integer representing the length
55 of the .debug_info contribution for that compilation unit, not
56 including the length field itself. In the 32-bit DWARF format,
57 this is a 4-byte unsigned integer (which must be less than
58 0xffffff00); in the 64-bit DWARF format, this consists of the
59 4-byte value 0xffffffff followed by an 8-byte unsigned integer
60 that gives the actual length (see Section 7.4).
61
62 2. A 2-byte unsigned integer representing the version of the
63 DWARF information for that compilation unit. For DWARF Version
64 2.1, the value in this field is 2.
65
66 3. A 4-byte or 8-byte unsigned offset into the .debug_abbrev
67 section. This offset associates the compilation unit with a
68 particular set of debugging information entry abbreviations. In
69 the 32-bit DWARF format, this is a 4-byte unsigned length; in
70 the 64-bit DWARF format, this is an 8-byte unsigned length (see
71 Section 7.4).
72
73 4. A 1-byte unsigned integer representing the size in bytes of
74 an address on the target architecture. If the system uses
75 segmented addressing, this value represents the size of the
76 offset portion of an address. */
77 length = read_4ubyte_unaligned (dbg, cu_bytes);
78 cu_bytes += 4;
79 offset_size = 4;
80 if (length == 0xffffffff)
81 offset_size = 8;
82
83 /* Now we know how large the header is. Note the trick in the
84 computation. If the offset_size is 4 the '- 4' term undoes the
85 '2 *'. If offset_size is 8 this term computes the size of the
86 escape value plus the 8 byte offset. */
87 if (unlikely (offset + 2 * offset_size - 4 + sizeof (Dwarf_Half)
88 + offset_size + sizeof (Dwarf_Small)
89 >= dbg->sections[IDX_debug_info].size))
90 {
91 dbg->cu_list_current = NULL;
92 return DW_DLV_NO_ENTRY;
93 }
94
95 if (length == 0xffffffff)
96 {
97 /* This is a 64-bit DWARF format. */
98 length = read_8ubyte_unaligned (dbg, cu_bytes);
99 cu_bytes += 8;
100 }
101
102 /* We know we have enough room in the .debug_section. Allocate the
103 result data structure. */
104 cu = (Dwarf_CU_Info) malloc (sizeof (struct Dwarf_CU_Info_s));
105 if (cu == NULL)
106 {
107 __libdwarf_error (dbg, error, DW_E_NOMEM);
108 return DW_DLV_ERROR;
109 }
110
111 /* Store the values in the data structure. */
112 cu->offset = offset;
113 cu->offset_size = offset_size;
114 cu->length = length;
115
116 /* Store the version stamp. Always a 16-bit value. */
117 cu->version_stamp = read_2ubyte_unaligned (dbg, cu_bytes);
118 cu_bytes += 2;
119
120 /* Get offset in .debug_abbrev. Note that the size of the entry
121 depends on whether this is a 32-bit or 64-bit DWARF definition. */
122 if (offset_size == 4)
123 cu->abbrev_offset = read_4ubyte_unaligned (dbg, cu_bytes);
124 else
125 cu->abbrev_offset = read_8ubyte_unaligned (dbg, cu_bytes);
126 cu_bytes += offset_size;
127 cu->last_abbrev_offset = cu->abbrev_offset;
128
129 /* The address size. Always an 8-bit value. */
130 cu->address_size = *cu_bytes++;
131
132 /* Store the header length. */
133 cu->header_length = (cu_bytes
134 - ((Dwarf_Small *) dbg->sections[IDX_debug_info].addr
135 + offset));
136
137 /* Initilize a few more members. */
138 if (unlikely (Dwarf_Abbrev_Hash_init (&cu->abbrev_hash,
139 DEFAULT_ABBREV_HASH_SIZE) != 0))
140 {
141 free (cu);
142 __libdwarf_error (dbg, error, DW_E_NOMEM);
143 return DW_DLV_ERROR;
144 }
145
146 /* Remember the debugging handle. */
147 cu->dbg = dbg;
148
149 /* There is no other entry yet. */
150 cu->next = NULL;
151
152 /* Enqueue the new entry. */
153 if (dbg->cu_list == NULL)
154 dbg->cu_list = dbg->cu_list_tail = cu;
155 else
156 {
157 dbg->cu_list_tail->next = cu;
158 dbg->cu_list_tail = cu;
159 }
160 *result_cu = cu;
161
162 return DW_DLV_OK;
163}
164
165
166int
167dwarf_next_cu_header (dbg, cu_header_length, version_stamp, abbrev_offset,
168 address_size, next_cu_header, error)
169 Dwarf_Debug dbg;
170 Dwarf_Unsigned *cu_header_length;
171 Dwarf_Half *version_stamp;
172 Dwarf_Unsigned *abbrev_offset;
173 Dwarf_Half *address_size;
174 Dwarf_Unsigned *next_cu_header;
175 Dwarf_Error *error;
176{
177 Dwarf_Unsigned offset_next_cu;
178 Dwarf_CU_Info cu;
179
180 if (dbg == NULL)
181 return DW_DLV_ERROR;
182
183 /* Determine offset of next CU header. If we don't have a current
184 CU this is the first call and we start right at the beginning. */
185 if (dbg->cu_list_current == NULL)
186 {
187 offset_next_cu = 0;
188 cu = dbg->cu_list;
189 }
190 else
191 {
192 /* We can compute the offset from the information we read from
193 the last CU header. */
194 cu = dbg->cu_list_current;
195
196 offset_next_cu = cu->offset + 2 * cu->offset_size - 4 + cu->length;
197
198 /* See whether the next entry entry is available. */
199 cu = cu->next;
200 }
201
202 /* If the entry is not yet available get it. */
203 if (cu == NULL)
204 {
205 int res = __libdwarf_get_cu_at_offset (dbg, offset_next_cu, &cu, error);
206
207 /* If it still does not exist, fail. Note that this can mean an
208 error or that we reached the end. */
209 if (res != DW_DLV_OK)
210 return res;
211
212 dbg->cu_list_current = cu;
213 assert (cu != NULL);
214 }
215
216 /* See get_cu_at_offset for an explanation of the trick in this
217 formula. */
218 *next_cu_header = offset_next_cu + 2 * cu->offset_size - 4 + cu->length;
219
220 /* Extract the information and put it where the user wants it. */
221 if (cu_header_length != NULL)
222 *cu_header_length = cu->header_length;
223
224 if (version_stamp != NULL)
225 *version_stamp = cu->version_stamp;
226
227 if (abbrev_offset != NULL)
228 *abbrev_offset = cu->abbrev_offset;
229
230 if (address_size != NULL)
231 *address_size = cu->address_size;
232
233 return DW_DLV_OK;
234}