blob: c456051ca55dee2d37284b11423cb6d21d23906f [file] [log] [blame]
Brian Silverman86497922018-02-10 19:28:39 -05001/* Get macro information.
2 Copyright (C) 2002-2009, 2014 Red Hat, Inc.
3 This file is part of elfutils.
4 Written by Ulrich Drepper <drepper@redhat.com>, 2002.
5
6 This file is free software; you can redistribute it and/or modify
7 it under the terms of either
8
9 * the GNU Lesser General Public License as published by the Free
10 Software Foundation; either version 3 of the License, or (at
11 your option) any later version
12
13 or
14
15 * the GNU General Public License as published by the Free
16 Software Foundation; either version 2 of the License, or (at
17 your option) any later version
18
19 or both in parallel, as here.
20
21 elfutils is distributed in the hope that it will be useful, but
22 WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 General Public License for more details.
25
26 You should have received copies of the GNU General Public License and
27 the GNU Lesser General Public License along with this program. If
28 not, see <http://www.gnu.org/licenses/>. */
29
30#ifdef HAVE_CONFIG_H
31# include <config.h>
32#endif
33
34#include <assert.h>
35#include <dwarf.h>
36#include <search.h>
37#include <stdlib.h>
38#include <string.h>
39
40#include <libdwP.h>
41
42static int
43get_offset_from (Dwarf_Die *die, int name, Dwarf_Word *retp)
44{
45 /* Get the appropriate attribute. */
46 Dwarf_Attribute attr;
47 if (INTUSE(dwarf_attr) (die, name, &attr) == NULL)
48 return -1;
49
50 /* Offset into the corresponding section. */
51 return INTUSE(dwarf_formudata) (&attr, retp);
52}
53
54static int
55macro_op_compare (const void *p1, const void *p2)
56{
57 const Dwarf_Macro_Op_Table *t1 = (const Dwarf_Macro_Op_Table *) p1;
58 const Dwarf_Macro_Op_Table *t2 = (const Dwarf_Macro_Op_Table *) p2;
59
60 if (t1->offset < t2->offset)
61 return -1;
62 if (t1->offset > t2->offset)
63 return 1;
64
65 if (t1->sec_index < t2->sec_index)
66 return -1;
67 if (t1->sec_index > t2->sec_index)
68 return 1;
69
70 return 0;
71}
72
73static void
74build_table (Dwarf_Macro_Op_Table *table,
75 Dwarf_Macro_Op_Proto op_protos[static 255])
76{
77 unsigned ct = 0;
78 for (unsigned i = 1; i < 256; ++i)
79 if (op_protos[i - 1].forms != NULL)
80 table->table[table->opcodes[i - 1] = ct++] = op_protos[i - 1];
81 else
82 table->opcodes[i - 1] = 0xff;
83}
84
85#define MACRO_PROTO(NAME, ...) \
86 Dwarf_Macro_Op_Proto NAME = ({ \
87 static const uint8_t proto[] = {__VA_ARGS__}; \
88 (Dwarf_Macro_Op_Proto) {sizeof proto, proto}; \
89 })
90
91enum { macinfo_data_size = offsetof (Dwarf_Macro_Op_Table, table[5]) };
92static unsigned char macinfo_data[macinfo_data_size]
93 __attribute__ ((aligned (__alignof (Dwarf_Macro_Op_Table))));
94
95static __attribute__ ((constructor)) void
96init_macinfo_table (void)
97{
98 MACRO_PROTO (p_udata_str, DW_FORM_udata, DW_FORM_string);
99 MACRO_PROTO (p_udata_udata, DW_FORM_udata, DW_FORM_udata);
100 MACRO_PROTO (p_none);
101
102 Dwarf_Macro_Op_Proto op_protos[255] =
103 {
104 [DW_MACINFO_define - 1] = p_udata_str,
105 [DW_MACINFO_undef - 1] = p_udata_str,
106 [DW_MACINFO_vendor_ext - 1] = p_udata_str,
107 [DW_MACINFO_start_file - 1] = p_udata_udata,
108 [DW_MACINFO_end_file - 1] = p_none,
109 /* If you are adding more elements to this array, increase
110 MACINFO_DATA_SIZE above. */
111 };
112
113 Dwarf_Macro_Op_Table *macinfo_table = (void *) macinfo_data;
114 memset (macinfo_table, 0, sizeof macinfo_data);
115 build_table (macinfo_table, op_protos);
116 macinfo_table->sec_index = IDX_debug_macinfo;
117}
118
119static Dwarf_Macro_Op_Table *
120get_macinfo_table (Dwarf *dbg, Dwarf_Word macoff, Dwarf_Die *cudie)
121{
122 assert (cudie != NULL);
123
124 Dwarf_Attribute attr_mem, *attr
125 = INTUSE(dwarf_attr) (cudie, DW_AT_stmt_list, &attr_mem);
126 Dwarf_Off line_offset = (Dwarf_Off) -1;
127 if (attr != NULL)
128 if (unlikely (INTUSE(dwarf_formudata) (attr, &line_offset) != 0))
129 return NULL;
130
131 Dwarf_Macro_Op_Table *table = libdw_alloc (dbg, Dwarf_Macro_Op_Table,
132 macinfo_data_size, 1);
133 memcpy (table, macinfo_data, macinfo_data_size);
134
135 table->offset = macoff;
136 table->sec_index = IDX_debug_macinfo;
137 table->line_offset = line_offset;
138 table->is_64bit = cudie->cu->address_size == 8;
139 table->comp_dir = __libdw_getcompdir (cudie);
140
141 return table;
142}
143
144static Dwarf_Macro_Op_Table *
145get_table_for_offset (Dwarf *dbg, Dwarf_Word macoff,
146 const unsigned char *readp,
147 const unsigned char *const endp,
148 Dwarf_Die *cudie)
149{
150 const unsigned char *startp = readp;
151
152 /* Request at least 3 bytes for header. */
153 if (readp + 3 > endp)
154 {
155 invalid_dwarf:
156 __libdw_seterrno (DWARF_E_INVALID_DWARF);
157 return NULL;
158 }
159
160 uint16_t version = read_2ubyte_unaligned_inc (dbg, readp);
161 if (version != 4 && version != 5)
162 {
163 __libdw_seterrno (DWARF_E_INVALID_VERSION);
164 return NULL;
165 }
166
167 uint8_t flags = *readp++;
168 bool is_64bit = (flags & 0x1) != 0;
169
170 Dwarf_Off line_offset = (Dwarf_Off) -1;
171 if ((flags & 0x2) != 0)
172 {
173 line_offset = read_addr_unaligned_inc (is_64bit ? 8 : 4, dbg, readp);
174 if (readp > endp)
175 goto invalid_dwarf;
176 }
177 else if (cudie != NULL)
178 {
179 Dwarf_Attribute attr_mem, *attr
180 = INTUSE(dwarf_attr) (cudie, DW_AT_stmt_list, &attr_mem);
181 if (attr != NULL)
182 if (unlikely (INTUSE(dwarf_formudata) (attr, &line_offset) != 0))
183 return NULL;
184 }
185
186 /* """The macinfo entry types defined in this standard may, but
187 might not, be described in the table""".
188
189 I.e. these may be present. It's tempting to simply skip them,
190 but it's probably more correct to tolerate that a producer tweaks
191 the way certain opcodes are encoded, for whatever reasons. */
192
193 MACRO_PROTO (p_udata_str, DW_FORM_udata, DW_FORM_string);
194 MACRO_PROTO (p_udata_strp, DW_FORM_udata, DW_FORM_strp);
195 MACRO_PROTO (p_udata_udata, DW_FORM_udata, DW_FORM_udata);
196 MACRO_PROTO (p_secoffset, DW_FORM_sec_offset);
197 MACRO_PROTO (p_none);
198
199 Dwarf_Macro_Op_Proto op_protos[255] =
200 {
201 [DW_MACRO_define - 1] = p_udata_str,
202 [DW_MACRO_undef - 1] = p_udata_str,
203 [DW_MACRO_define_strp - 1] = p_udata_strp,
204 [DW_MACRO_undef_strp - 1] = p_udata_strp,
205 [DW_MACRO_start_file - 1] = p_udata_udata,
206 [DW_MACRO_end_file - 1] = p_none,
207 [DW_MACRO_import - 1] = p_secoffset,
208 /* When adding support for DWARF5 supplementary object files and
209 indirect string tables also add support for DW_MACRO_define_sup,
210 DW_MACRO_undef_sup, DW_MACRO_import_sup, DW_MACRO_define_strx
211 and DW_MACRO_undef_strx. */
212 };
213
214 if ((flags & 0x4) != 0)
215 {
216 unsigned count = *readp++;
217 for (unsigned i = 0; i < count; ++i)
218 {
219 unsigned opcode = *readp++;
220
221 Dwarf_Macro_Op_Proto e;
222 if (readp >= endp)
223 goto invalid;
224 get_uleb128 (e.nforms, readp, endp);
225 e.forms = readp;
226 op_protos[opcode - 1] = e;
227
228 readp += e.nforms;
229 if (readp > endp)
230 {
231 invalid:
232 __libdw_seterrno (DWARF_E_INVALID_DWARF);
233 return NULL;
234 }
235 }
236 }
237
238 size_t ct = 0;
239 for (unsigned i = 1; i < 256; ++i)
240 if (op_protos[i - 1].forms != NULL)
241 ++ct;
242
243 /* We support at most 0xfe opcodes defined in the table, as 0xff is
244 a value that means that given opcode is not stored at all. But
245 that should be fine, as opcode 0 is not allocated. */
246 assert (ct < 0xff);
247
248 size_t macop_table_size = offsetof (Dwarf_Macro_Op_Table, table[ct]);
249
250 Dwarf_Macro_Op_Table *table = libdw_alloc (dbg, Dwarf_Macro_Op_Table,
251 macop_table_size, 1);
252
253 *table = (Dwarf_Macro_Op_Table) {
254 .offset = macoff,
255 .sec_index = IDX_debug_macro,
256 .line_offset = line_offset,
257 .header_len = readp - startp,
258 .version = version,
259 .is_64bit = is_64bit,
260
261 /* NULL if CUDIE is NULL or DW_AT_comp_dir is absent. */
262 .comp_dir = __libdw_getcompdir (cudie),
263 };
264 build_table (table, op_protos);
265
266 return table;
267}
268
269static Dwarf_Macro_Op_Table *
270cache_op_table (Dwarf *dbg, int sec_index, Dwarf_Off macoff,
271 const unsigned char *startp,
272 const unsigned char *const endp,
273 Dwarf_Die *cudie)
274{
275 Dwarf_Macro_Op_Table fake = { .offset = macoff, .sec_index = sec_index };
276 Dwarf_Macro_Op_Table **found = tfind (&fake, &dbg->macro_ops,
277 macro_op_compare);
278 if (found != NULL)
279 return *found;
280
281 Dwarf_Macro_Op_Table *table = sec_index == IDX_debug_macro
282 ? get_table_for_offset (dbg, macoff, startp, endp, cudie)
283 : get_macinfo_table (dbg, macoff, cudie);
284
285 if (table == NULL)
286 return NULL;
287
288 Dwarf_Macro_Op_Table **ret = tsearch (table, &dbg->macro_ops,
289 macro_op_compare);
290 if (unlikely (ret == NULL))
291 {
292 __libdw_seterrno (DWARF_E_NOMEM);
293 return NULL;
294 }
295
296 return *ret;
297}
298
299static ptrdiff_t
300read_macros (Dwarf *dbg, int sec_index,
301 Dwarf_Off macoff, int (*callback) (Dwarf_Macro *, void *),
302 void *arg, ptrdiff_t offset, bool accept_0xff,
303 Dwarf_Die *cudie)
304{
305 Elf_Data *d = dbg->sectiondata[sec_index];
306 if (unlikely (d == NULL || d->d_buf == NULL))
307 {
308 __libdw_seterrno (DWARF_E_NO_ENTRY);
309 return -1;
310 }
311
312 if (unlikely (macoff >= d->d_size))
313 {
314 __libdw_seterrno (DWARF_E_INVALID_DWARF);
315 return -1;
316 }
317
318 const unsigned char *const startp = d->d_buf + macoff;
319 const unsigned char *const endp = d->d_buf + d->d_size;
320
321 Dwarf_Macro_Op_Table *table = cache_op_table (dbg, sec_index, macoff,
322 startp, endp, cudie);
323 if (table == NULL)
324 return -1;
325
326 if (offset == 0)
327 offset = table->header_len;
328
329 assert (offset >= 0);
330 assert (offset < endp - startp);
331 const unsigned char *readp = startp + offset;
332
333 while (readp < endp)
334 {
335 unsigned int opcode = *readp++;
336 if (opcode == 0)
337 /* Nothing more to do. */
338 return 0;
339
340 if (unlikely (opcode == 0xff && ! accept_0xff))
341 {
342 /* See comment below at dwarf_getmacros for explanation of
343 why we are doing this. */
344 __libdw_seterrno (DWARF_E_INVALID_OPCODE);
345 return -1;
346 }
347
348 unsigned int idx = table->opcodes[opcode - 1];
349 if (idx == 0xff)
350 {
351 __libdw_seterrno (DWARF_E_INVALID_OPCODE);
352 return -1;
353 }
354
355 Dwarf_Macro_Op_Proto *proto = &table->table[idx];
356
357 /* A fake CU with bare minimum data to fool dwarf_formX into
358 doing the right thing with the attributes that we put out.
359 We pretend it is the same version as the actual table.
360 Version 4 for the old GNU extension, version 5 for DWARF5. */
361 Dwarf_CU fake_cu = {
362 .dbg = dbg,
363 .sec_idx = sec_index,
364 .version = table->version,
365 .offset_size = table->is_64bit ? 8 : 4,
366 .startp = (void *) startp + offset,
367 .endp = (void *) endp,
368 };
369
370 Dwarf_Attribute *attributes;
371 Dwarf_Attribute *attributesp = NULL;
372 Dwarf_Attribute nattributes[8];
373 if (unlikely (proto->nforms > 8))
374 {
375 attributesp = malloc (sizeof (Dwarf_Attribute) * proto->nforms);
376 if (attributesp == NULL)
377 {
378 __libdw_seterrno (DWARF_E_NOMEM);
379 return -1;
380 }
381 attributes = attributesp;
382 }
383 else
384 attributes = &nattributes[0];
385
386 for (Dwarf_Word i = 0; i < proto->nforms; ++i)
387 {
388 /* We pretend this is a DW_AT_GNU_macros attribute so that
389 DW_FORM_sec_offset forms get correctly interpreted as
390 offset into .debug_macro. */
391 attributes[i].code = DW_AT_GNU_macros;
392 attributes[i].form = proto->forms[i];
393 attributes[i].valp = (void *) readp;
394 attributes[i].cu = &fake_cu;
395
396 size_t len = __libdw_form_val_len (&fake_cu, proto->forms[i], readp);
397 if (unlikely (len == (size_t) -1))
398 {
399 free (attributesp);
400 return -1;
401 }
402
403 readp += len;
404 }
405
406 Dwarf_Macro macro = {
407 .table = table,
408 .opcode = opcode,
409 .attributes = attributes,
410 };
411
412 int res = callback (&macro, arg);
413 if (unlikely (attributesp != NULL))
414 free (attributesp);
415
416 if (res != DWARF_CB_OK)
417 return readp - startp;
418 }
419
420 return 0;
421}
422
423/* Token layout:
424
425 - The highest bit is used for distinguishing between callers that
426 know that opcode 0xff may have one of two incompatible meanings.
427 The mask that we use for selecting this bit is
428 DWARF_GETMACROS_START.
429
430 - The rest of the token (31 or 63 bits) encodes address inside the
431 macro unit.
432
433 Besides, token value of 0 signals end of iteration and -1 is
434 reserved for signaling errors. That means it's impossible to
435 represent maximum offset of a .debug_macro unit to new-style
436 callers (which in practice decreases the permissible macro unit
437 size by another 1 byte). */
438
439static ptrdiff_t
440token_from_offset (ptrdiff_t offset, bool accept_0xff)
441{
442 if (offset == -1 || offset == 0)
443 return offset;
444
445 /* Make sure the offset didn't overflow into the flag bit. */
446 if ((offset & DWARF_GETMACROS_START) != 0)
447 {
448 __libdw_seterrno (DWARF_E_TOO_BIG);
449 return -1;
450 }
451
452 if (accept_0xff)
453 offset |= DWARF_GETMACROS_START;
454
455 return offset;
456}
457
458static ptrdiff_t
459offset_from_token (ptrdiff_t token, bool *accept_0xffp)
460{
461 *accept_0xffp = (token & DWARF_GETMACROS_START) != 0;
462 token &= ~DWARF_GETMACROS_START;
463
464 return token;
465}
466
467static ptrdiff_t
468gnu_macros_getmacros_off (Dwarf *dbg, Dwarf_Off macoff,
469 int (*callback) (Dwarf_Macro *, void *),
470 void *arg, ptrdiff_t offset, bool accept_0xff,
471 Dwarf_Die *cudie)
472{
473 assert (offset >= 0);
474
475 if (macoff >= dbg->sectiondata[IDX_debug_macro]->d_size)
476 {
477 __libdw_seterrno (DWARF_E_INVALID_OFFSET);
478 return -1;
479 }
480
481 return read_macros (dbg, IDX_debug_macro, macoff,
482 callback, arg, offset, accept_0xff, cudie);
483}
484
485static ptrdiff_t
486macro_info_getmacros_off (Dwarf *dbg, Dwarf_Off macoff,
487 int (*callback) (Dwarf_Macro *, void *),
488 void *arg, ptrdiff_t offset, Dwarf_Die *cudie)
489{
490 assert (offset >= 0);
491
492 return read_macros (dbg, IDX_debug_macinfo, macoff,
493 callback, arg, offset, true, cudie);
494}
495
496ptrdiff_t
497dwarf_getmacros_off (Dwarf *dbg, Dwarf_Off macoff,
498 int (*callback) (Dwarf_Macro *, void *),
499 void *arg, ptrdiff_t token)
500{
501 if (dbg == NULL)
502 {
503 __libdw_seterrno (DWARF_E_NO_DWARF);
504 return -1;
505 }
506
507 bool accept_0xff;
508 ptrdiff_t offset = offset_from_token (token, &accept_0xff);
509 assert (accept_0xff);
510
511 offset = gnu_macros_getmacros_off (dbg, macoff, callback, arg, offset,
512 accept_0xff, NULL);
513
514 return token_from_offset (offset, accept_0xff);
515}
516
517ptrdiff_t
518dwarf_getmacros (Dwarf_Die *cudie, int (*callback) (Dwarf_Macro *, void *),
519 void *arg, ptrdiff_t token)
520{
521 if (cudie == NULL)
522 {
523 __libdw_seterrno (DWARF_E_NO_DWARF);
524 return -1;
525 }
526
527 /* This function might be called from a code that expects to see
528 DW_MACINFO_* opcodes, not DW_MACRO_{GNU_,}* ones. It is fine to
529 serve most DW_MACRO_{GNU_,}* opcodes to such code, because those
530 whose values are the same as DW_MACINFO_* ones also have the same
531 behavior. It is not very likely that a .debug_macro section
532 would only use the part of opcode space that it shares with
533 .debug_macinfo, but it is possible. Serving the opcodes that are
534 only valid in DW_MACRO_{GNU_,}* domain is OK as well, because
535 clients in general need to be ready that newer standards define
536 more opcodes, and have coping mechanisms for unfamiliar opcodes.
537
538 The one exception to the above rule is opcode 0xff, which has
539 concrete semantics in .debug_macinfo, but falls into vendor block
540 in .debug_macro, and can be assigned to do whatever. There is
541 some small probability that the two opcodes would look
542 superficially similar enough that a client would be confused and
543 misbehave as a result. For this reason, we refuse to serve
544 through this interface 0xff's originating from .debug_macro
545 unless the TOKEN that we obtained indicates the call originates
546 from a new-style caller. See above for details on what
547 information is encoded into tokens. */
548
549 bool accept_0xff;
550 ptrdiff_t offset = offset_from_token (token, &accept_0xff);
551
552 /* DW_AT_macro_info */
553 if (dwarf_hasattr (cudie, DW_AT_macro_info))
554 {
555 Dwarf_Word macoff;
556 if (get_offset_from (cudie, DW_AT_macro_info, &macoff) != 0)
557 return -1;
558 offset = macro_info_getmacros_off (cudie->cu->dbg, macoff,
559 callback, arg, offset, cudie);
560 }
561 else
562 {
563 /* DW_AT_GNU_macros, DW_AT_macros */
564 Dwarf_Word macoff;
565 if (get_offset_from (cudie, DW_AT_GNU_macros, &macoff) != 0)
566 return -1;
567 offset = gnu_macros_getmacros_off (cudie->cu->dbg, macoff,
568 callback, arg, offset, accept_0xff,
569 cudie);
570 }
571
572 return token_from_offset (offset, accept_0xff);
573}