static ptrdiff_t getlocations_addr (Dwarf_Attribute *attr, ptrdiff_t offset, Dwarf_Addr *basep, Dwarf_Addr *startp, Dwarf_Addr *endp, Dwarf_Addr address, const Elf_Data *locs, Dwarf_Op **expr, size_t *exprlen) { unsigned char *readp = locs->d_buf + offset; unsigned char *readendp = locs->d_buf + locs->d_size; next: if (readendp - readp < attr->cu->address_size * 2) { invalid: __libdw_seterrno (DWARF_E_INVALID_DWARF); return -1; } Dwarf_Addr begin; Dwarf_Addr end; switch (__libdw_read_begin_end_pair_inc (attr->cu->dbg, IDX_debug_loc, &readp, attr->cu->address_size, &begin, &end, basep)) { case 0: /* got location range. */ break; case 1: /* base address setup. */ goto next; case 2: /* end of loclist */ return 0; default: /* error */ return -1; } if (readendp - readp < 2) goto invalid; /* We have a location expression. */ Dwarf_Block block; block.length = read_2ubyte_unaligned_inc (attr->cu->dbg, readp); block.data = readp; if (readendp - readp < (ptrdiff_t) block.length) goto invalid; readp += block.length; *startp = *basep + begin; *endp = *basep + end; /* If address is minus one we want them all, otherwise only matching. */ if (address != (Dwarf_Word) -1 && (address < *startp || address >= *endp)) goto next; if (getlocation (attr->cu, &block, expr, exprlen, IDX_debug_loc) != 0) return -1; return readp - (unsigned char *) locs->d_buf; }
int dwarf_getlocation_implicit_value (Dwarf_Attribute *attr, const Dwarf_Op *op, Dwarf_Block *return_block) { if (attr == NULL) return -1; struct loc_block_s fake = { .addr = (void *) op }; struct loc_block_s **found = tfind (&fake, &attr->cu->locs, loc_compare); if (unlikely (found == NULL)) { __libdw_seterrno (DWARF_E_NO_BLOCK); return -1; } return_block->length = (*found)->length; return_block->data = (*found)->data; return 0; } /* DW_AT_data_member_location can be a constant as well as a loclistptr. Only data[48] indicate a loclistptr. */ static int check_constant_offset (Dwarf_Attribute *attr, Dwarf_Op **llbuf, size_t *listlen) { if (attr->code != DW_AT_data_member_location) return 1; switch (attr->form) { /* Punt for any non-constant form. */ default: return 1; case DW_FORM_data1: case DW_FORM_data2: case DW_FORM_data4: case DW_FORM_data8: case DW_FORM_sdata: case DW_FORM_udata: break; } /* Check whether we already cached this location. */ struct loc_s fake = { .addr = attr->valp }; struct loc_s **found = tfind (&fake, &attr->cu->locs, loc_compare); if (found == NULL) { Dwarf_Word offset; if (INTUSE(dwarf_formudata) (attr, &offset) != 0) return -1; Dwarf_Op *result = libdw_alloc (attr->cu->dbg, Dwarf_Op, sizeof (Dwarf_Op), 1); result->atom = DW_OP_plus_uconst; result->number = offset; result->number2 = 0; result->offset = 0; /* Insert a record in the search tree so we can find it again later. */ struct loc_s *newp = libdw_alloc (attr->cu->dbg, struct loc_s, sizeof (struct loc_s), 1); newp->addr = attr->valp; newp->loc = result; newp->nloc = 1; found = tsearch (newp, &attr->cu->locs, loc_compare); } assert ((*found)->nloc == 1); if (llbuf != NULL) { *llbuf = (*found)->loc; *listlen = 1; } return 0; } int internal_function __libdw_intern_expression (Dwarf *dbg, bool other_byte_order, unsigned int address_size, unsigned int ref_size, void **cache, const Dwarf_Block *block, bool cfap, bool valuep, Dwarf_Op **llbuf, size_t *listlen, int sec_index) { /* Empty location expressions don't have any ops to intern. */ if (block->length == 0) { *listlen = 0; return 0; } /* Check whether we already looked at this list. */ struct loc_s fake = { .addr = block->data }; struct loc_s **found = tfind (&fake, cache, loc_compare); if (found != NULL) { /* We already saw it. */ *llbuf = (*found)->loc; *listlen = (*found)->nloc; if (valuep) { assert (*listlen > 1); assert ((*llbuf)[*listlen - 1].atom == DW_OP_stack_value); } return 0; } const unsigned char *data = block->data; const unsigned char *const end_data = data + block->length; const struct { bool other_byte_order; } bo = { other_byte_order }; struct loclist *loclist = NULL; unsigned int n = 0; /* Stack allocate at most this many locs. */ #define MAX_STACK_LOCS 256 struct loclist stack_locs[MAX_STACK_LOCS]; #define NEW_LOC() ({ struct loclist *ll; \ ll = (likely (n < MAX_STACK_LOCS) \ ? &stack_locs[n] \ : malloc (sizeof (struct loclist))); \ if (unlikely (ll == NULL)) \ goto nomem; \ n++; \ ll->next = loclist; \ loclist = ll; \ ll; }) if (cfap) { /* Synthesize the operation to push the CFA before the expression. */ struct loclist *newloc = NEW_LOC (); newloc->atom = DW_OP_call_frame_cfa; newloc->number = 0; newloc->number2 = 0; newloc->offset = -1; } /* Decode the opcodes. It is possible in some situations to have a block of size zero. */ while (data < end_data) { struct loclist *newloc; newloc = NEW_LOC (); newloc->number = 0; newloc->number2 = 0; newloc->offset = data - block->data; switch ((newloc->atom = *data++)) { case DW_OP_addr: /* Address, depends on address size of CU. */ if (dbg == NULL) { // XXX relocation? if (address_size == 4) { if (unlikely (data + 4 > end_data)) goto invalid; else newloc->number = read_4ubyte_unaligned_inc (&bo, data); } else { if (unlikely (data + 8 > end_data)) goto invalid; else newloc->number = read_8ubyte_unaligned_inc (&bo, data); } } else if (__libdw_read_address_inc (dbg, sec_index, &data, address_size, &newloc->number)) goto invalid; break; case DW_OP_call_ref: /* DW_FORM_ref_addr, depends on offset size of CU. */ if (dbg == NULL || __libdw_read_offset_inc (dbg, sec_index, &data, ref_size, &newloc->number, IDX_debug_info, 0)) goto invalid; break; case DW_OP_deref: case DW_OP_dup: case DW_OP_drop: case DW_OP_over: case DW_OP_swap: case DW_OP_rot: case DW_OP_xderef: case DW_OP_abs: case DW_OP_and: case DW_OP_div: case DW_OP_minus: case DW_OP_mod: case DW_OP_mul: case DW_OP_neg: case DW_OP_not: case DW_OP_or: case DW_OP_plus: case DW_OP_shl: case DW_OP_shr: case DW_OP_shra: case DW_OP_xor: case DW_OP_eq: case DW_OP_ge: case DW_OP_gt: case DW_OP_le: case DW_OP_lt: case DW_OP_ne: case DW_OP_lit0 ... DW_OP_lit31: case DW_OP_reg0 ... DW_OP_reg31: case DW_OP_nop: case DW_OP_push_object_address: case DW_OP_call_frame_cfa: case DW_OP_form_tls_address: case DW_OP_GNU_push_tls_address: case DW_OP_stack_value: /* No operand. */ break; case DW_OP_const1u: case DW_OP_pick: case DW_OP_deref_size: case DW_OP_xderef_size: if (unlikely (data >= end_data)) { invalid: __libdw_seterrno (DWARF_E_INVALID_DWARF); returnmem: /* Free any dynamicly allocated loclists, if any. */ while (n > MAX_STACK_LOCS) { struct loclist *loc = loclist; loclist = loc->next; free (loc); n--; } return -1; } newloc->number = *data++; break; case DW_OP_const1s: if (unlikely (data >= end_data)) goto invalid; newloc->number = *((int8_t *) data); ++data; break; case DW_OP_const2u: if (unlikely (data + 2 > end_data)) goto invalid; newloc->number = read_2ubyte_unaligned_inc (&bo, data); break; case DW_OP_const2s: case DW_OP_skip: case DW_OP_bra: case DW_OP_call2: if (unlikely (data + 2 > end_data)) goto invalid; newloc->number = read_2sbyte_unaligned_inc (&bo, data); break; case DW_OP_const4u: if (unlikely (data + 4 > end_data)) goto invalid; newloc->number = read_4ubyte_unaligned_inc (&bo, data); break; case DW_OP_const4s: case DW_OP_call4: case DW_OP_GNU_parameter_ref: if (unlikely (data + 4 > end_data)) goto invalid; newloc->number = read_4sbyte_unaligned_inc (&bo, data); break; case DW_OP_const8u: if (unlikely (data + 8 > end_data)) goto invalid; newloc->number = read_8ubyte_unaligned_inc (&bo, data); break; case DW_OP_const8s: if (unlikely (data + 8 > end_data)) goto invalid; newloc->number = read_8sbyte_unaligned_inc (&bo, data); break; case DW_OP_constu: case DW_OP_plus_uconst: case DW_OP_regx: case DW_OP_piece: case DW_OP_GNU_convert: case DW_OP_GNU_reinterpret: get_uleb128 (newloc->number, data, end_data); break; case DW_OP_consts: case DW_OP_breg0 ... DW_OP_breg31: case DW_OP_fbreg: get_sleb128 (newloc->number, data, end_data); break; case DW_OP_bregx: get_uleb128 (newloc->number, data, end_data); if (unlikely (data >= end_data)) goto invalid; get_sleb128 (newloc->number2, data, end_data); break; case DW_OP_bit_piece: case DW_OP_GNU_regval_type: get_uleb128 (newloc->number, data, end_data); if (unlikely (data >= end_data)) goto invalid; get_uleb128 (newloc->number2, data, end_data); break; case DW_OP_implicit_value: case DW_OP_GNU_entry_value: /* This cannot be used in a CFI expression. */ if (unlikely (dbg == NULL)) goto invalid; /* start of block inc. len. */ newloc->number2 = (Dwarf_Word) (uintptr_t) data; get_uleb128 (newloc->number, data, end_data); /* Block length. */ if (unlikely ((Dwarf_Word) (end_data - data) < newloc->number)) goto invalid; data += newloc->number; /* Skip the block. */ break; case DW_OP_GNU_implicit_pointer: /* DW_FORM_ref_addr, depends on offset size of CU. */ if (dbg == NULL || __libdw_read_offset_inc (dbg, sec_index, &data, ref_size, &newloc->number, IDX_debug_info, 0)) goto invalid; if (unlikely (data >= end_data)) goto invalid; get_uleb128 (newloc->number2, data, end_data); /* Byte offset. */ break; case DW_OP_GNU_deref_type: if (unlikely (data + 1 >= end_data)) goto invalid; newloc->number = *data++; get_uleb128 (newloc->number2, data, end_data); break; case DW_OP_GNU_const_type: { size_t size; get_uleb128 (newloc->number, data, end_data); if (unlikely (data >= end_data)) goto invalid; /* start of block inc. len. */ newloc->number2 = (Dwarf_Word) (uintptr_t) data; size = *data++; if (unlikely ((Dwarf_Word) (end_data - data) < size)) goto invalid; data += size; /* Skip the block. */ } break; default: goto invalid; } } if (unlikely (n == 0)) { /* This is not allowed. It would mean an empty location expression, which we handled already as a special case above. */ goto invalid; } if (valuep) { struct loclist *newloc = NEW_LOC (); newloc->atom = DW_OP_stack_value; newloc->number = 0; newloc->number2 = 0; newloc->offset = data - block->data; } /* Allocate the array. */ Dwarf_Op *result; if (dbg != NULL) result = libdw_alloc (dbg, Dwarf_Op, sizeof (Dwarf_Op), n); else { result = malloc (sizeof *result * n); if (result == NULL) { nomem: __libdw_seterrno (DWARF_E_NOMEM); goto returnmem; } } /* Store the result. */ *llbuf = result; *listlen = n; do { /* We populate the array from the back since the list is backwards. */ --n; result[n].atom = loclist->atom; result[n].number = loclist->number; result[n].number2 = loclist->number2; result[n].offset = loclist->offset; if (result[n].atom == DW_OP_implicit_value) store_implicit_value (dbg, cache, &result[n]); struct loclist *loc = loclist; loclist = loclist->next; if (unlikely (n + 1 > MAX_STACK_LOCS)) free (loc); } while (n > 0); /* Insert a record in the search tree so that we can find it again later. */ struct loc_s *newp; if (dbg != NULL) newp = libdw_alloc (dbg, struct loc_s, sizeof (struct loc_s), 1); else { newp = malloc (sizeof *newp); if (newp == NULL) { free (result); goto nomem; } } newp->addr = block->data; newp->loc = result; newp->nloc = *listlen; (void) tsearch (newp, cache, loc_compare); /* We did it. */ return 0; } static int getlocation (struct Dwarf_CU *cu, const Dwarf_Block *block, Dwarf_Op **llbuf, size_t *listlen, int sec_index) { /* Empty location expressions don't have any ops to intern. Note that synthetic empty_cu doesn't have an associated DWARF dbg. */ if (block->length == 0) { *listlen = 0; return 0; } return __libdw_intern_expression (cu->dbg, cu->dbg->other_byte_order, cu->address_size, (cu->version == 2 ? cu->address_size : cu->offset_size), &cu->locs, block, false, false, llbuf, listlen, sec_index); } int dwarf_getlocation (Dwarf_Attribute *attr, Dwarf_Op **llbuf, size_t *listlen) { if (! attr_ok (attr)) return -1; int result = check_constant_offset (attr, llbuf, listlen); if (result != 1) return result; /* If it has a block form, it's a single location expression. */ Dwarf_Block block; if (INTUSE(dwarf_formblock) (attr, &block) != 0) return -1; return getlocation (attr->cu, &block, llbuf, listlen, cu_sec_idx (attr->cu)); } static int attr_base_address (Dwarf_Attribute *attr, Dwarf_Addr *basep) { /* Fetch the CU's base address. */ Dwarf_Die cudie = CUDIE (attr->cu); /* Find the base address of the compilation unit. It will normally be specified by DW_AT_low_pc. In DWARF-3 draft 4, the base address could be overridden by DW_AT_entry_pc. It's been removed, but GCC emits DW_AT_entry_pc and not DW_AT_lowpc for compilation units with discontinuous ranges. */ Dwarf_Attribute attr_mem; if (unlikely (INTUSE(dwarf_lowpc) (&cudie, basep) != 0) && INTUSE(dwarf_formaddr) (INTUSE(dwarf_attr) (&cudie, DW_AT_entry_pc, &attr_mem), basep) != 0) { if (INTUSE(dwarf_errno) () != 0) return -1; /* The compiler provided no base address when it should have. Buggy GCC does this when it used absolute addresses in the location list and no DW_AT_ranges. */ *basep = 0; } return 0; }
static int getlocation (struct Dwarf_CU *cu, const Dwarf_Block *block, Dwarf_Op **llbuf, size_t *listlen) { Dwarf *dbg = cu->dbg; /* Check whether we already looked at this list. */ struct loc_s fake = { .addr = block->data }; struct loc_s **found = tfind (&fake, &cu->locs, loc_compare); if (found != NULL) { /* We already saw it. */ *llbuf = (*found)->loc; *listlen = (*found)->nloc; return 0; } const unsigned char *data = block->data; const unsigned char *const end_data = data + block->length; struct loclist *loclist = NULL; unsigned int n = 0; /* Decode the opcodes. It is possible in some situations to have a block of size zero. */ while (data < end_data) { struct loclist *newloc; newloc = (struct loclist *) alloca (sizeof (struct loclist)); newloc->number = 0; newloc->number2 = 0; newloc->offset = data - block->data; newloc->next = loclist; loclist = newloc; ++n; switch ((newloc->atom = *data++)) { case DW_OP_addr: /* Address, depends on address size of CU. */ if (cu->address_size == 4) { if (unlikely (data + 4 > end_data)) { invalid: __libdw_seterrno (DWARF_E_INVALID_DWARF); return -1; } newloc->number = read_4ubyte_unaligned_inc (dbg, data); } else { if (unlikely (data + 8 > end_data)) goto invalid; newloc->number = read_8ubyte_unaligned_inc (dbg, data); } break; case DW_OP_deref: case DW_OP_dup: case DW_OP_drop: case DW_OP_over: case DW_OP_swap: case DW_OP_rot: case DW_OP_xderef: case DW_OP_abs: case DW_OP_and: case DW_OP_div: case DW_OP_minus: case DW_OP_mod: case DW_OP_mul: case DW_OP_neg: case DW_OP_not: case DW_OP_or: case DW_OP_plus: case DW_OP_shl: case DW_OP_shr: case DW_OP_shra: case DW_OP_xor: case DW_OP_eq: case DW_OP_ge: case DW_OP_gt: case DW_OP_le: case DW_OP_lt: case DW_OP_ne: case DW_OP_lit0 ... DW_OP_lit31: case DW_OP_reg0 ... DW_OP_reg31: case DW_OP_nop: case DW_OP_push_object_address: case DW_OP_call_ref: /* No operand. */ break; case DW_OP_const1u: case DW_OP_pick: case DW_OP_deref_size: case DW_OP_xderef_size: if (unlikely (data >= end_data)) goto invalid; newloc->number = *data++; break; case DW_OP_const1s: if (unlikely (data >= end_data)) goto invalid; newloc->number = *((int8_t *) data); ++data; break; case DW_OP_const2u: if (unlikely (data + 2 > end_data)) goto invalid; newloc->number = read_2ubyte_unaligned_inc (dbg, data); break; case DW_OP_const2s: case DW_OP_skip: case DW_OP_bra: case DW_OP_call2: if (unlikely (data + 2 > end_data)) goto invalid; newloc->number = read_2sbyte_unaligned_inc (dbg, data); break; case DW_OP_const4u: if (unlikely (data + 4 > end_data)) goto invalid; newloc->number = read_4ubyte_unaligned_inc (dbg, data); break; case DW_OP_const4s: case DW_OP_call4: if (unlikely (data + 4 > end_data)) goto invalid; newloc->number = read_4sbyte_unaligned_inc (dbg, data); break; case DW_OP_const8u: if (unlikely (data + 8 > end_data)) goto invalid; newloc->number = read_8ubyte_unaligned_inc (dbg, data); break; case DW_OP_const8s: if (unlikely (data + 8 > end_data)) goto invalid; newloc->number = read_8sbyte_unaligned_inc (dbg, data); break; case DW_OP_constu: case DW_OP_plus_uconst: case DW_OP_regx: case DW_OP_piece: /* XXX Check size. */ get_uleb128 (newloc->number, data); break; case DW_OP_consts: case DW_OP_breg0 ... DW_OP_breg31: case DW_OP_fbreg: /* XXX Check size. */ get_sleb128 (newloc->number, data); break; case DW_OP_bregx: /* XXX Check size. */ get_uleb128 (newloc->number, data); get_sleb128 (newloc->number2, data); break; default: goto invalid; } } if (unlikely (n == 0)) { /* This is not allowed. XXX Is it? */ goto invalid; } /* Allocate the array. */ Dwarf_Op *result = libdw_alloc (dbg, Dwarf_Op, sizeof (Dwarf_Op), n); /* Store the result. */ *llbuf = result; *listlen = n; do { /* We populate the array from the back since the list is backwards. */ --n; result[n].atom = loclist->atom; result[n].number = loclist->number; result[n].number2 = loclist->number2; result[n].offset = loclist->offset; loclist = loclist->next; } while (n > 0); /* Insert a record in the search tree so that we can find it again later. */ struct loc_s *newp = libdw_alloc (dbg, struct loc_s, sizeof (struct loc_s), 1); newp->addr = block->data; newp->loc = result; newp->nloc = *listlen; (void) tsearch (newp, &cu->locs, loc_compare); /* We did it. */ return 0; } int dwarf_getlocation (attr, llbuf, listlen) Dwarf_Attribute *attr; Dwarf_Op **llbuf; size_t *listlen; { if (! attr_ok (attr)) return -1; /* If it has a block form, it's a single location expression. */ Dwarf_Block block; if (INTUSE(dwarf_formblock) (attr, &block) != 0) return -1; return getlocation (attr->cu, &block, llbuf, listlen); } int dwarf_getlocation_addr (attr, address, llbufs, listlens, maxlocs) Dwarf_Attribute *attr; Dwarf_Addr address; Dwarf_Op **llbufs; size_t *listlens; size_t maxlocs; { if (! attr_ok (attr)) return -1; if (llbufs == NULL) maxlocs = SIZE_MAX; /* If it has a block form, it's a single location expression. */ Dwarf_Block block; if (INTUSE(dwarf_formblock) (attr, &block) == 0) { if (maxlocs == 0) return 0; if (llbufs != NULL && getlocation (attr->cu, &block, &llbufs[0], &listlens[0]) != 0) return -1; return listlens[0] == 0 ? 0 : 1; } int error = INTUSE(dwarf_errno) (); if (unlikely (error != DWARF_E_NO_BLOCK)) { __libdw_seterrno (error); return -1; } /* Must have the form data4 or data8 which act as an offset. */ Dwarf_Word offset; if (unlikely (INTUSE(dwarf_formudata) (attr, &offset) != 0)) return -1; const Elf_Data *d = attr->cu->dbg->sectiondata[IDX_debug_loc]; if (unlikely (d == NULL)) { __libdw_seterrno (DWARF_E_NO_LOCLIST); return -1; } Dwarf_Addr base = (Dwarf_Addr) -1; unsigned char *readp = d->d_buf + offset; size_t got = 0; while (got < maxlocs) { if ((unsigned char *) d->d_buf + d->d_size - readp < attr->cu->address_size * 2) { invalid: __libdw_seterrno (DWARF_E_INVALID_DWARF); return -1; } Dwarf_Addr begin; Dwarf_Addr end; if (attr->cu->address_size == 8) { begin = read_8ubyte_unaligned_inc (attr->cu->dbg, readp); end = read_8ubyte_unaligned_inc (attr->cu->dbg, readp); if (begin == (Elf64_Addr) -1l) /* Base address entry. */ { base = end; if (unlikely (base == (Dwarf_Addr) -1)) goto invalid; continue; } } else { begin = read_4ubyte_unaligned_inc (attr->cu->dbg, readp); end = read_4ubyte_unaligned_inc (attr->cu->dbg, readp); if (begin == (Elf32_Addr) -1) /* Base address entry. */ { base = end; continue; } } if (begin == 0 && end == 0) /* End of list entry. */ break; if ((unsigned char *) d->d_buf + d->d_size - readp < 2) goto invalid; /* We have a location expression. */ block.length = read_2ubyte_unaligned_inc (attr->cu->dbg, readp); block.data = readp; if ((unsigned char *) d->d_buf + d->d_size - readp < (ptrdiff_t) block.length) goto invalid; readp += block.length; if (base == (Dwarf_Addr) -1) { /* Fetch the CU's base address. */ Dwarf_Die cudie = CUDIE (attr->cu); /* Find the base address of the compilation unit. It will normally be specified by DW_AT_low_pc. In DWARF-3 draft 4, the base address could be overridden by DW_AT_entry_pc. It's been removed, but GCC emits DW_AT_entry_pc and not DW_AT_lowpc for compilation units with discontinuous ranges. */ Dwarf_Attribute attr_mem; if (unlikely (INTUSE(dwarf_lowpc) (&cudie, &base) != 0) && INTUSE(dwarf_formaddr) (INTUSE(dwarf_attr) (&cudie, DW_AT_entry_pc, &attr_mem), &base) != 0) { if (INTUSE(dwarf_errno) () != 0) return -1; /* The compiler provided no base address when it should have. Buggy GCC does this when it used absolute addresses in the location list and no DW_AT_ranges. */ base = 0; } } if (address >= base + begin && address < base + end) { /* This one matches the address. */ if (llbufs != NULL && unlikely (getlocation (attr->cu, &block, &llbufs[got], &listlens[got]) != 0)) return -1; ++got; } } return got; }
int dwarf_getsrclines (Dwarf_Die *cudie, Dwarf_Lines **lines, size_t *nlines) { if (unlikely (cudie == NULL || INTUSE(dwarf_tag) (cudie) != DW_TAG_compile_unit)) return -1; int res = -1; /* Get the information if it is not already known. */ struct Dwarf_CU *const cu = cudie->cu; if (cu->lines == NULL) { /* Failsafe mode: no data found. */ cu->lines = (void *) -1l; cu->files = (void *) -1l; /* The die must have a statement list associated. */ Dwarf_Attribute stmt_list_mem; Dwarf_Attribute *stmt_list = INTUSE(dwarf_attr) (cudie, DW_AT_stmt_list, &stmt_list_mem); /* Get the offset into the .debug_line section. NB: this call also checks whether the previous dwarf_attr call failed. */ Dwarf_Word offset; if (INTUSE(dwarf_formudata) (stmt_list, &offset) != 0) goto out; Dwarf *dbg = cu->dbg; if (dbg->sectiondata[IDX_debug_line] == NULL) { __libdw_seterrno (DWARF_E_NO_DEBUG_LINE); goto out; } const uint8_t *linep = dbg->sectiondata[IDX_debug_line]->d_buf + offset; const uint8_t *lineendp = (dbg->sectiondata[IDX_debug_line]->d_buf + dbg->sectiondata[IDX_debug_line]->d_size); /* Get the compilation directory. */ Dwarf_Attribute compdir_attr_mem; Dwarf_Attribute *compdir_attr = INTUSE(dwarf_attr) (cudie, DW_AT_comp_dir, &compdir_attr_mem); const char *comp_dir = INTUSE(dwarf_formstring) (compdir_attr); if (unlikely (linep + 4 > lineendp)) { invalid_data: __libdw_seterrno (DWARF_E_INVALID_DEBUG_LINE); goto out; } Dwarf_Word unit_length = read_4ubyte_unaligned_inc (dbg, linep); unsigned int length = 4; if (unlikely (unit_length == DWARF3_LENGTH_64_BIT)) { if (unlikely (linep + 8 > lineendp)) goto invalid_data; unit_length = read_8ubyte_unaligned_inc (dbg, linep); length = 8; } /* Check whether we have enough room in the section. */ if (unit_length < 2 + length + 5 * 1 || unlikely (linep + unit_length > lineendp)) goto invalid_data; lineendp = linep + unit_length; /* The next element of the header is the version identifier. */ uint_fast16_t version = read_2ubyte_unaligned_inc (dbg, linep); if (unlikely (version > DWARF_VERSION)) { __libdw_seterrno (DWARF_E_VERSION); goto out; } /* Next comes the header length. */ Dwarf_Word header_length; if (length == 4) header_length = read_4ubyte_unaligned_inc (dbg, linep); else header_length = read_8ubyte_unaligned_inc (dbg, linep); const unsigned char *header_start = linep; /* Next the minimum instruction length. */ uint_fast8_t minimum_instr_len = *linep++; /* Then the flag determining the default value of the is_stmt register. */ uint_fast8_t default_is_stmt = *linep++; /* Now the line base. */ int_fast8_t line_base = *((int_fast8_t *) linep); ++linep; /* And the line range. */ uint_fast8_t line_range = *linep++; /* The opcode base. */ uint_fast8_t opcode_base = *linep++; /* Remember array with the standard opcode length (-1 to account for the opcode with value zero not being mentioned). */ const uint8_t *standard_opcode_lengths = linep - 1; linep += opcode_base - 1; if (unlikely (linep >= lineendp)) goto invalid_data; /* First comes the list of directories. Add the compilation directory first since the index zero is used for it. */ struct dirlist { const char *dir; size_t len; struct dirlist *next; } comp_dir_elem = { .dir = comp_dir, .len = comp_dir ? strlen (comp_dir) : 0, .next = NULL }; struct dirlist *dirlist = &comp_dir_elem; unsigned int ndirlist = 1; // XXX Directly construct array to conserve memory? while (*linep != 0) { struct dirlist *new_dir = (struct dirlist *) alloca (sizeof (*new_dir)); new_dir->dir = (char *) linep; uint8_t *endp = memchr (linep, '\0', lineendp - linep); if (endp == NULL) goto invalid_data; new_dir->len = endp - linep; new_dir->next = dirlist; dirlist = new_dir; ++ndirlist; linep = endp + 1; } /* Skip the final NUL byte. */ ++linep; /* Rearrange the list in array form. */ struct dirlist **dirarray = (struct dirlist **) alloca (ndirlist * sizeof (*dirarray)); for (unsigned int n = ndirlist; n-- > 0; dirlist = dirlist->next) dirarray[n] = dirlist; /* Now read the files. */ struct filelist null_file = { .info = { .name = "???", .mtime = 0, .length = 0 }, .next = NULL }; struct filelist *filelist = &null_file; unsigned int nfilelist = 1; if (unlikely (linep >= lineendp)) goto invalid_data; while (*linep != 0) { struct filelist *new_file = (struct filelist *) alloca (sizeof (*new_file)); /* First comes the file name. */ char *fname = (char *) linep; uint8_t *endp = memchr (fname, '\0', lineendp - linep); if (endp == NULL) goto invalid_data; size_t fnamelen = endp - (uint8_t *) fname; linep = endp + 1; /* Then the index. */ Dwarf_Word diridx; get_uleb128 (diridx, linep); if (unlikely (diridx >= ndirlist)) { __libdw_seterrno (DWARF_E_INVALID_DIR_IDX); goto out; } if (*fname == '/') /* It's an absolute path. */ new_file->info.name = fname; else { new_file->info.name = libdw_alloc (dbg, char, 1, dirarray[diridx]->len + 1 + fnamelen + 1); char *cp = new_file->info.name; if (dirarray[diridx]->dir != NULL) { /* This value could be NULL in case the DW_AT_comp_dir was not present. We cannot do much in this case. The easiest thing is to convert the path in an absolute path. */ cp = stpcpy (cp, dirarray[diridx]->dir); } *cp++ = '/'; strcpy (cp, fname); assert (strlen (new_file->info.name) < dirarray[diridx]->len + 1 + fnamelen + 1); } /* Next comes the modification time. */ get_uleb128 (new_file->info.mtime, linep); /* Finally the length of the file. */ get_uleb128 (new_file->info.length, linep); new_file->next = filelist; filelist = new_file; ++nfilelist; } /* Skip the final NUL byte. */ ++linep; /* Consistency check. */ if (unlikely (linep != header_start + header_length)) { __libdw_seterrno (DWARF_E_INVALID_DWARF); goto out; } /* We are about to process the statement program. Initialize the state machine registers (see 6.2.2 in the v2.1 specification). */ Dwarf_Word address = 0; size_t file = 1; size_t line = 1; size_t column = 0; uint_fast8_t is_stmt = default_is_stmt; int basic_block = 0; int prologue_end = 0; int epilogue_begin = 0; /* Process the instructions. */ struct linelist *linelist = NULL; unsigned int nlinelist = 0; while (linep < lineendp) { struct linelist *new_line; unsigned int opcode; unsigned int u128; int s128; /* Read the opcode. */ opcode = *linep++; /* Is this a special opcode? */ if (likely (opcode >= opcode_base)) { /* Yes. Handling this is quite easy since the opcode value is computed with opcode = (desired line increment - line_base) + (line_range * address advance) + opcode_base */ int line_increment = (line_base + (opcode - opcode_base) % line_range); unsigned int address_increment = (minimum_instr_len * ((opcode - opcode_base) / line_range)); /* Perform the increments. */ line += line_increment; address += address_increment; /* Add a new line with the current state machine values. */ NEW_LINE (0); /* Reset the flags. */ basic_block = 0; prologue_end = 0; epilogue_begin = 0; } else if (opcode == 0) { /* This an extended opcode. */ if (unlikely (linep + 2 > lineendp)) goto invalid_data; /* The length. */ unsigned int len = *linep++; if (unlikely (linep + len > lineendp)) goto invalid_data; /* The sub-opcode. */ opcode = *linep++; switch (opcode) { case DW_LNE_end_sequence: /* Add a new line with the current state machine values. The is the end of the sequence. */ NEW_LINE (1); /* Reset the registers. */ address = 0; file = 1; line = 1; column = 0; is_stmt = default_is_stmt; basic_block = 0; prologue_end = 0; epilogue_begin = 0; break; case DW_LNE_set_address: /* The value is an address. The size is defined as apporiate for the target machine. We use the address size field from the CU header. */ if (cu->address_size == 4) address = read_4ubyte_unaligned_inc (dbg, linep); else address = read_8ubyte_unaligned_inc (dbg, linep); break; case DW_LNE_define_file: { char *fname = (char *) linep; uint8_t *endp = memchr (linep, '\0', lineendp - linep); if (endp == NULL) goto invalid_data; size_t fnamelen = endp - linep; linep = endp + 1; unsigned int diridx; get_uleb128 (diridx, linep); Dwarf_Word mtime; get_uleb128 (mtime, linep); Dwarf_Word filelength; get_uleb128 (filelength, linep); struct filelist *new_file = (struct filelist *) alloca (sizeof (*new_file)); if (fname[0] == '/') new_file->info.name = fname; else { new_file->info.name = libdw_alloc (dbg, char, 1, (dirarray[diridx]->len + 1 + fnamelen + 1)); char *cp = new_file->info.name; if (dirarray[diridx]->dir != NULL) /* This value could be NULL in case the DW_AT_comp_dir was not present. We cannot do much in this case. The easiest thing is to convert the path in an absolute path. */ cp = stpcpy (cp, dirarray[diridx]->dir); *cp++ = '/'; strcpy (cp, fname); } new_file->info.mtime = mtime; new_file->info.length = filelength; new_file->next = filelist; filelist = new_file; ++nfilelist; } break; default: /* Unknown, ignore it. */ linep += len - 1; break; } } else if (opcode <= DW_LNS_set_epilogue_begin) { /* This is a known standard opcode. */ switch (opcode) { case DW_LNS_copy: /* Takes no argument. */ if (unlikely (standard_opcode_lengths[opcode] != 0)) goto invalid_data; /* Add a new line with the current state machine values. */ NEW_LINE (0); /* Reset the flags. */ basic_block = 0; /* XXX Whether the following two lines are necessary is unclear. I guess the current v2.1 specification has a bug in that it says clearing these two registers is not necessary. */ prologue_end = 0; epilogue_begin = 0; break; case DW_LNS_advance_pc: /* Takes one uleb128 parameter which is added to the address. */ if (unlikely (standard_opcode_lengths[opcode] != 1)) goto invalid_data; get_uleb128 (u128, linep); address += minimum_instr_len * u128; break; case DW_LNS_advance_line: /* Takes one sleb128 parameter which is added to the line. */ if (unlikely (standard_opcode_lengths[opcode] != 1)) goto invalid_data; get_sleb128 (s128, linep); line += s128; break; case DW_LNS_set_file: /* Takes one uleb128 parameter which is stored in file. */ if (unlikely (standard_opcode_lengths[opcode] != 1)) goto invalid_data; get_uleb128 (u128, linep); file = u128; break; case DW_LNS_set_column: /* Takes one uleb128 parameter which is stored in column. */ if (unlikely (standard_opcode_lengths[opcode] != 1)) goto invalid_data; get_uleb128 (u128, linep); column = u128; break; case DW_LNS_negate_stmt: /* Takes no argument. */ if (unlikely (standard_opcode_lengths[opcode] != 0)) goto invalid_data; is_stmt = 1 - is_stmt; break; case DW_LNS_set_basic_block: /* Takes no argument. */ if (unlikely (standard_opcode_lengths[opcode] != 0)) goto invalid_data; basic_block = 1; break; case DW_LNS_const_add_pc: /* Takes no argument. */ if (unlikely (standard_opcode_lengths[opcode] != 0)) goto invalid_data; address += (minimum_instr_len * ((255 - opcode_base) / line_range)); break; case DW_LNS_fixed_advance_pc: /* Takes one 16 bit parameter which is added to the address. */ if (unlikely (standard_opcode_lengths[opcode] != 1)) goto invalid_data; address += read_2ubyte_unaligned_inc (dbg, linep); break; case DW_LNS_set_prologue_end: /* Takes no argument. */ if (unlikely (standard_opcode_lengths[opcode] != 0)) goto invalid_data; prologue_end = 1; break; case DW_LNS_set_epilogue_begin: /* Takes no argument. */ if (unlikely (standard_opcode_lengths[opcode] != 0)) goto invalid_data; epilogue_begin = 1; break; } } else { /* This is a new opcode the generator but not we know about. Read the parameters associated with it but then discard everything. Read all the parameters for this opcode. */ for (int n = standard_opcode_lengths[opcode]; n > 0; --n) get_uleb128 (u128, linep); /* Next round, ignore this opcode. */ continue; } }
int dwarf_getsrclines (Dwarf_Die *cudie, Dwarf_Lines **lines, size_t *nlines) { if (unlikely (cudie == NULL || (INTUSE(dwarf_tag) (cudie) != DW_TAG_compile_unit && INTUSE(dwarf_tag) (cudie) != DW_TAG_partial_unit))) return -1; int res = -1; struct linelist *linelist = NULL; unsigned int nlinelist = 0; /* If there are a large number of lines don't blow up the stack. Keep track of the last malloced linelist record and free them through the next pointer at the end. */ #define MAX_STACK_ALLOC 4096 struct linelist *malloc_linelist = NULL; /* Get the information if it is not already known. */ struct Dwarf_CU *const cu = cudie->cu; if (cu->lines == NULL) { /* Failsafe mode: no data found. */ cu->lines = (void *) -1l; cu->files = (void *) -1l; /* The die must have a statement list associated. */ Dwarf_Attribute stmt_list_mem; Dwarf_Attribute *stmt_list = INTUSE(dwarf_attr) (cudie, DW_AT_stmt_list, &stmt_list_mem); /* Get the offset into the .debug_line section. NB: this call also checks whether the previous dwarf_attr call failed. */ const unsigned char *lineendp; const unsigned char *linep = __libdw_formptr (stmt_list, IDX_debug_line, DWARF_E_NO_DEBUG_LINE, (unsigned char **) &lineendp, NULL); if (linep == NULL) goto out; /* Get the compilation directory. */ Dwarf_Attribute compdir_attr_mem; Dwarf_Attribute *compdir_attr = INTUSE(dwarf_attr) (cudie, DW_AT_comp_dir, &compdir_attr_mem); const char *comp_dir = INTUSE(dwarf_formstring) (compdir_attr); if (unlikely (linep + 4 > lineendp)) { invalid_data: __libdw_seterrno (DWARF_E_INVALID_DEBUG_LINE); goto out; } Dwarf *dbg = cu->dbg; Dwarf_Word unit_length = read_4ubyte_unaligned_inc (dbg, linep); unsigned int length = 4; if (unlikely (unit_length == DWARF3_LENGTH_64_BIT)) { if (unlikely (linep + 8 > lineendp)) goto invalid_data; unit_length = read_8ubyte_unaligned_inc (dbg, linep); length = 8; } /* Check whether we have enough room in the section. */ if (unit_length < 2 + length + 5 * 1 || unlikely (linep + unit_length > lineendp)) goto invalid_data; lineendp = linep + unit_length; /* The next element of the header is the version identifier. */ uint_fast16_t version = read_2ubyte_unaligned_inc (dbg, linep); if (unlikely (version < 2) || unlikely (version > 4)) { __libdw_seterrno (DWARF_E_VERSION); goto out; } /* Next comes the header length. */ Dwarf_Word header_length; if (length == 4) header_length = read_4ubyte_unaligned_inc (dbg, linep); else header_length = read_8ubyte_unaligned_inc (dbg, linep); const unsigned char *header_start = linep; /* Next the minimum instruction length. */ uint_fast8_t minimum_instr_len = *linep++; /* Next the maximum operations per instruction, in version 4 format. */ uint_fast8_t max_ops_per_instr = 1; if (version >= 4) { if (unlikely (lineendp - linep < 5)) goto invalid_data; max_ops_per_instr = *linep++; if (unlikely (max_ops_per_instr == 0)) goto invalid_data; } /* Then the flag determining the default value of the is_stmt register. */ uint_fast8_t default_is_stmt = *linep++; /* Now the line base. */ int_fast8_t line_base = (int8_t) *linep++; /* And the line range. */ uint_fast8_t line_range = *linep++; /* The opcode base. */ uint_fast8_t opcode_base = *linep++; /* Remember array with the standard opcode length (-1 to account for the opcode with value zero not being mentioned). */ const uint8_t *standard_opcode_lengths = linep - 1; if (unlikely (lineendp - linep < opcode_base - 1)) goto invalid_data; linep += opcode_base - 1; /* First comes the list of directories. Add the compilation directory first since the index zero is used for it. */ struct dirlist { const char *dir; size_t len; struct dirlist *next; } comp_dir_elem = { .dir = comp_dir, .len = comp_dir ? strlen (comp_dir) : 0, .next = NULL }; struct dirlist *dirlist = &comp_dir_elem; unsigned int ndirlist = 1; // XXX Directly construct array to conserve memory? while (*linep != 0) { struct dirlist *new_dir = (struct dirlist *) alloca (sizeof (*new_dir)); new_dir->dir = (char *) linep; uint8_t *endp = memchr (linep, '\0', lineendp - linep); if (endp == NULL) goto invalid_data; new_dir->len = endp - linep; new_dir->next = dirlist; dirlist = new_dir; ++ndirlist; linep = endp + 1; } /* Skip the final NUL byte. */ ++linep; /* Rearrange the list in array form. */ struct dirlist **dirarray = (struct dirlist **) alloca (ndirlist * sizeof (*dirarray)); for (unsigned int n = ndirlist; n-- > 0; dirlist = dirlist->next) dirarray[n] = dirlist; /* Now read the files. */ struct filelist null_file = { .info = { .name = "???", .mtime = 0, .length = 0 }, .next = NULL }; struct filelist *filelist = &null_file; unsigned int nfilelist = 1; if (unlikely (linep >= lineendp)) goto invalid_data; while (*linep != 0) { struct filelist *new_file = (struct filelist *) alloca (sizeof (*new_file)); /* First comes the file name. */ char *fname = (char *) linep; uint8_t *endp = memchr (fname, '\0', lineendp - linep); if (endp == NULL) goto invalid_data; size_t fnamelen = endp - (uint8_t *) fname; linep = endp + 1; /* Then the index. */ Dwarf_Word diridx; get_uleb128 (diridx, linep); if (unlikely (diridx >= ndirlist)) { __libdw_seterrno (DWARF_E_INVALID_DIR_IDX); goto out; } if (*fname == '/') /* It's an absolute path. */ new_file->info.name = fname; else { new_file->info.name = libdw_alloc (dbg, char, 1, dirarray[diridx]->len + 1 + fnamelen + 1); char *cp = new_file->info.name; if (dirarray[diridx]->dir != NULL) { /* This value could be NULL in case the DW_AT_comp_dir was not present. We cannot do much in this case. The easiest thing is to convert the path in an absolute path. */ cp = stpcpy (cp, dirarray[diridx]->dir); } *cp++ = '/'; strcpy (cp, fname); assert (strlen (new_file->info.name) < dirarray[diridx]->len + 1 + fnamelen + 1); } /* Next comes the modification time. */ get_uleb128 (new_file->info.mtime, linep); /* Finally the length of the file. */ get_uleb128 (new_file->info.length, linep); new_file->next = filelist; filelist = new_file; ++nfilelist; } /* Skip the final NUL byte. */ ++linep; /* Consistency check. */ if (unlikely (linep != header_start + header_length)) { __libdw_seterrno (DWARF_E_INVALID_DWARF); goto out; } /* We are about to process the statement program. Initialize the state machine registers (see 6.2.2 in the v2.1 specification). */ Dwarf_Word addr = 0; unsigned int op_index = 0; unsigned int file = 1; int line = 1; unsigned int column = 0; uint_fast8_t is_stmt = default_is_stmt; bool basic_block = false; bool prologue_end = false; bool epilogue_begin = false; unsigned int isa = 0; unsigned int discriminator = 0; /* Apply the "operation advance" from a special opcode or DW_LNS_advance_pc (as per DWARF4 6.2.5.1). */ inline void advance_pc (unsigned int op_advance) { addr += minimum_instr_len * ((op_index + op_advance) / max_ops_per_instr); op_index = (op_index + op_advance) % max_ops_per_instr; }