Skip to content

Commit

Permalink
libbacktrace: use correct directory/filename for DWARF 5
Browse files Browse the repository at this point in the history
	PR debug/98716
	* dwarf.c (read_v2_paths): Allocate zero entry for dirs and
	filenames.
	(read_line_program): Remove parameter u, change caller.  Don't
	subtract one from dirs and filenames index.
	(read_function_entry): Don't subtract one from filenames index.
  • Loading branch information
ianlancetaylor committed Jan 18, 2021
1 parent 5496ee0 commit 0966504
Showing 1 changed file with 33 additions and 31 deletions.
64 changes: 33 additions & 31 deletions dwarf.c
Original file line number Diff line number Diff line change
Expand Up @@ -2677,19 +2677,20 @@ read_v2_paths (struct backtrace_state *state, struct unit *u,
++hdr->dirs_count;
}

hdr->dirs = NULL;
if (hdr->dirs_count != 0)
{
hdr->dirs = ((const char **)
backtrace_alloc (state,
hdr->dirs_count * sizeof (const char *),
hdr_buf->error_callback,
hdr_buf->data));
if (hdr->dirs == NULL)
return 0;
}
/* The index of the first entry in the list of directories is 1. Index 0 is
used for the current directory of the compilation. To simplify index
handling, we set entry 0 to the compilation unit directory. */
++hdr->dirs_count;
hdr->dirs = ((const char **)
backtrace_alloc (state,
hdr->dirs_count * sizeof (const char *),
hdr_buf->error_callback,
hdr_buf->data));
if (hdr->dirs == NULL)
return 0;

i = 0;
hdr->dirs[0] = u->comp_dir;
i = 1;
while (*hdr_buf->buf != '\0')
{
if (hdr_buf->reported_underflow)
Expand All @@ -2716,14 +2717,19 @@ read_v2_paths (struct backtrace_state *state, struct unit *u,
++hdr->filenames_count;
}

/* The index of the first entry in the list of file names is 1. Index 0 is
used for the DW_AT_name of the compilation unit. To simplify index
handling, we set entry 0 to the compilation unit file name. */
++hdr->filenames_count;
hdr->filenames = ((const char **)
backtrace_alloc (state,
hdr->filenames_count * sizeof (char *),
hdr_buf->error_callback,
hdr_buf->data));
if (hdr->filenames == NULL)
return 0;
i = 0;
hdr->filenames[0] = u->filename;
i = 1;
while (*hdr_buf->buf != '\0')
{
const char *filename;
Expand All @@ -2737,7 +2743,7 @@ read_v2_paths (struct backtrace_state *state, struct unit *u,
return 0;
dir_index = read_uleb128 (hdr_buf);
if (IS_ABSOLUTE_PATH (filename)
|| (dir_index == 0 && u->comp_dir == NULL))
|| (dir_index < hdr->dirs_count && hdr->dirs[dir_index] == NULL))
hdr->filenames[i] = filename;
else
{
Expand All @@ -2746,10 +2752,8 @@ read_v2_paths (struct backtrace_state *state, struct unit *u,
size_t filename_len;
char *s;

if (dir_index == 0)
dir = u->comp_dir;
else if (dir_index - 1 < hdr->dirs_count)
dir = hdr->dirs[dir_index - 1];
if (dir_index < hdr->dirs_count)
dir = hdr->dirs[dir_index];
else
{
dwarf_buf_error (hdr_buf,
Expand Down Expand Up @@ -3037,8 +3041,8 @@ read_line_header (struct backtrace_state *state, struct dwarf_data *ddata,

static int
read_line_program (struct backtrace_state *state, struct dwarf_data *ddata,
struct unit *u, const struct line_header *hdr,
struct dwarf_buf *line_buf, struct line_vector *vec)
const struct line_header *hdr, struct dwarf_buf *line_buf,
struct line_vector *vec)
{
uint64_t address;
unsigned int op_index;
Expand All @@ -3048,8 +3052,8 @@ read_line_program (struct backtrace_state *state, struct dwarf_data *ddata,

address = 0;
op_index = 0;
if (hdr->filenames_count > 0)
reset_filename = hdr->filenames[0];
if (hdr->filenames_count > 1)
reset_filename = hdr->filenames[1];
else
reset_filename = "";
filename = reset_filename;
Expand Down Expand Up @@ -3114,10 +3118,8 @@ read_line_program (struct backtrace_state *state, struct dwarf_data *ddata,
size_t f_len;
char *p;

if (dir_index == 0 && hdr->version < 5)
dir = u->comp_dir;
else if (dir_index - 1 < hdr->dirs_count)
dir = hdr->dirs[dir_index - 1];
if (dir_index < hdr->dirs_count)
dir = hdr->dirs[dir_index];
else
{
dwarf_buf_error (line_buf,
Expand Down Expand Up @@ -3184,14 +3186,14 @@ read_line_program (struct backtrace_state *state, struct dwarf_data *ddata,
filename = "";
else
{
if (fileno - 1 >= hdr->filenames_count)
if (fileno >= hdr->filenames_count)
{
dwarf_buf_error (line_buf,
("invalid file number in "
"line number program"));
return 0;
}
filename = hdr->filenames[fileno - 1];
filename = hdr->filenames[fileno];
}
}
break;
Expand Down Expand Up @@ -3281,7 +3283,7 @@ read_line_info (struct backtrace_state *state, struct dwarf_data *ddata,
if (!read_line_header (state, ddata, u, is_dwarf64, &line_buf, hdr))
goto fail;

if (!read_line_program (state, ddata, u, hdr, &line_buf, &vec))
if (!read_line_program (state, ddata, hdr, &line_buf, &vec))
goto fail;

if (line_buf.reported_underflow)
Expand Down Expand Up @@ -3622,15 +3624,15 @@ read_function_entry (struct backtrace_state *state, struct dwarf_data *ddata,
function->caller_filename = "";
else
{
if (val.u.uint - 1 >= lhdr->filenames_count)
if (val.u.uint >= lhdr->filenames_count)
{
dwarf_buf_error (unit_buf,
("invalid file number in "
"DW_AT_call_file attribute"));
return 0;
}
function->caller_filename =
lhdr->filenames[val.u.uint - 1];
lhdr->filenames[val.u.uint];
}
}
break;
Expand Down

0 comments on commit 0966504

Please sign in to comment.