Skip to content

Commit 0966504

Browse files
libbacktrace: use correct directory/filename for DWARF 5
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.
1 parent 5496ee0 commit 0966504

File tree

1 file changed

+33
-31
lines changed

1 file changed

+33
-31
lines changed

dwarf.c

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2677,19 +2677,20 @@ read_v2_paths (struct backtrace_state *state, struct unit *u,
26772677
++hdr->dirs_count;
26782678
}
26792679

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

2692-
i = 0;
2692+
hdr->dirs[0] = u->comp_dir;
2693+
i = 1;
26932694
while (*hdr_buf->buf != '\0')
26942695
{
26952696
if (hdr_buf->reported_underflow)
@@ -2716,14 +2717,19 @@ read_v2_paths (struct backtrace_state *state, struct unit *u,
27162717
++hdr->filenames_count;
27172718
}
27182719

2720+
/* The index of the first entry in the list of file names is 1. Index 0 is
2721+
used for the DW_AT_name of the compilation unit. To simplify index
2722+
handling, we set entry 0 to the compilation unit file name. */
2723+
++hdr->filenames_count;
27192724
hdr->filenames = ((const char **)
27202725
backtrace_alloc (state,
27212726
hdr->filenames_count * sizeof (char *),
27222727
hdr_buf->error_callback,
27232728
hdr_buf->data));
27242729
if (hdr->filenames == NULL)
27252730
return 0;
2726-
i = 0;
2731+
hdr->filenames[0] = u->filename;
2732+
i = 1;
27272733
while (*hdr_buf->buf != '\0')
27282734
{
27292735
const char *filename;
@@ -2737,7 +2743,7 @@ read_v2_paths (struct backtrace_state *state, struct unit *u,
27372743
return 0;
27382744
dir_index = read_uleb128 (hdr_buf);
27392745
if (IS_ABSOLUTE_PATH (filename)
2740-
|| (dir_index == 0 && u->comp_dir == NULL))
2746+
|| (dir_index < hdr->dirs_count && hdr->dirs[dir_index] == NULL))
27412747
hdr->filenames[i] = filename;
27422748
else
27432749
{
@@ -2746,10 +2752,8 @@ read_v2_paths (struct backtrace_state *state, struct unit *u,
27462752
size_t filename_len;
27472753
char *s;
27482754

2749-
if (dir_index == 0)
2750-
dir = u->comp_dir;
2751-
else if (dir_index - 1 < hdr->dirs_count)
2752-
dir = hdr->dirs[dir_index - 1];
2755+
if (dir_index < hdr->dirs_count)
2756+
dir = hdr->dirs[dir_index];
27532757
else
27542758
{
27552759
dwarf_buf_error (hdr_buf,
@@ -3037,8 +3041,8 @@ read_line_header (struct backtrace_state *state, struct dwarf_data *ddata,
30373041

30383042
static int
30393043
read_line_program (struct backtrace_state *state, struct dwarf_data *ddata,
3040-
struct unit *u, const struct line_header *hdr,
3041-
struct dwarf_buf *line_buf, struct line_vector *vec)
3044+
const struct line_header *hdr, struct dwarf_buf *line_buf,
3045+
struct line_vector *vec)
30423046
{
30433047
uint64_t address;
30443048
unsigned int op_index;
@@ -3048,8 +3052,8 @@ read_line_program (struct backtrace_state *state, struct dwarf_data *ddata,
30483052

30493053
address = 0;
30503054
op_index = 0;
3051-
if (hdr->filenames_count > 0)
3052-
reset_filename = hdr->filenames[0];
3055+
if (hdr->filenames_count > 1)
3056+
reset_filename = hdr->filenames[1];
30533057
else
30543058
reset_filename = "";
30553059
filename = reset_filename;
@@ -3114,10 +3118,8 @@ read_line_program (struct backtrace_state *state, struct dwarf_data *ddata,
31143118
size_t f_len;
31153119
char *p;
31163120

3117-
if (dir_index == 0 && hdr->version < 5)
3118-
dir = u->comp_dir;
3119-
else if (dir_index - 1 < hdr->dirs_count)
3120-
dir = hdr->dirs[dir_index - 1];
3121+
if (dir_index < hdr->dirs_count)
3122+
dir = hdr->dirs[dir_index];
31213123
else
31223124
{
31233125
dwarf_buf_error (line_buf,
@@ -3184,14 +3186,14 @@ read_line_program (struct backtrace_state *state, struct dwarf_data *ddata,
31843186
filename = "";
31853187
else
31863188
{
3187-
if (fileno - 1 >= hdr->filenames_count)
3189+
if (fileno >= hdr->filenames_count)
31883190
{
31893191
dwarf_buf_error (line_buf,
31903192
("invalid file number in "
31913193
"line number program"));
31923194
return 0;
31933195
}
3194-
filename = hdr->filenames[fileno - 1];
3196+
filename = hdr->filenames[fileno];
31953197
}
31963198
}
31973199
break;
@@ -3281,7 +3283,7 @@ read_line_info (struct backtrace_state *state, struct dwarf_data *ddata,
32813283
if (!read_line_header (state, ddata, u, is_dwarf64, &line_buf, hdr))
32823284
goto fail;
32833285

3284-
if (!read_line_program (state, ddata, u, hdr, &line_buf, &vec))
3286+
if (!read_line_program (state, ddata, hdr, &line_buf, &vec))
32853287
goto fail;
32863288

32873289
if (line_buf.reported_underflow)
@@ -3622,15 +3624,15 @@ read_function_entry (struct backtrace_state *state, struct dwarf_data *ddata,
36223624
function->caller_filename = "";
36233625
else
36243626
{
3625-
if (val.u.uint - 1 >= lhdr->filenames_count)
3627+
if (val.u.uint >= lhdr->filenames_count)
36263628
{
36273629
dwarf_buf_error (unit_buf,
36283630
("invalid file number in "
36293631
"DW_AT_call_file attribute"));
36303632
return 0;
36313633
}
36323634
function->caller_filename =
3633-
lhdr->filenames[val.u.uint - 1];
3635+
lhdr->filenames[val.u.uint];
36343636
}
36353637
}
36363638
break;

0 commit comments

Comments
 (0)