diff options
author | Simon Marchi <simon.marchi@efficios.com> | 2022-04-20 17:03:25 -0400 |
---|---|---|
committer | Simon Marchi <simon.marchi@efficios.com> | 2022-04-21 11:11:21 -0400 |
commit | ffaebc199ed7b1f7e539c938e8b234d2a4ad9df9 (patch) | |
tree | f08456fdf016edb0a24c821f7cba7f2dc21e09b0 /gdb/dwarf2 | |
parent | 78088b89602ad5c2b68142d6a56481fb792725aa (diff) | |
download | binutils-ffaebc199ed7b1f7e539c938e8b234d2a4ad9df9.zip binutils-ffaebc199ed7b1f7e539c938e8b234d2a4ad9df9.tar.gz binutils-ffaebc199ed7b1f7e539c938e8b234d2a4ad9df9.tar.bz2 |
gdbsupport: add path_join function
In this review [1], Eli pointed out that we should be careful when
concatenating file names to avoid duplicated slashes. On Windows, a
double slash at the beginning of a file path has a special meaning. So
naively concatenating "/" and "foo/bar" would give "//foo/bar", which
would not give the desired results. We already have a few spots doing:
if (first_path ends with a slash)
path = first_path + second_path
else
path = first_path + slash + second_path
In general, I think it's nice to avoid superfluous slashes in file
paths, since they might end up visible to the user and look a bit
unprofessional.
Introduce the path_join function that can be used to join multiple path
components together (along with unit tests).
I initially wanted to make it possible to join two absolute paths, to
support the use case of prepending a sysroot path to a target file path,
or the prepending the debug-file-directory to a target file path. But
the code in solib_find_1 shows that it is more complex than this anyway
(for example, when the right hand side is a Windows path with a drive
letter). So I don't think we need to support that case in path_join.
That also keeps the implementation simpler.
Change a few spots to use path_join to show how it can be used. I
believe that all the spots I changed are guarded by some checks that
ensure the right hand side operand is not an absolute path.
Regression-tested on Ubuntu 18.04. Built-tested on Windows, and I also
ran the new unit-test there.
[1] https://sourceware.org/pipermail/gdb-patches/2022-April/187559.html
Change-Id: I0df889f7e3f644e045f42ff429277b732eb6c752
Diffstat (limited to 'gdb/dwarf2')
-rw-r--r-- | gdb/dwarf2/line-header.c | 3 | ||||
-rw-r--r-- | gdb/dwarf2/read.c | 42 |
2 files changed, 21 insertions, 24 deletions
diff --git a/gdb/dwarf2/line-header.c b/gdb/dwarf2/line-header.c index 77e7e55..8f4eb4f 100644 --- a/gdb/dwarf2/line-header.c +++ b/gdb/dwarf2/line-header.c @@ -24,6 +24,7 @@ #include "dwarf2/read.h" #include "complaints.h" #include "filenames.h" +#include "gdbsupport/pathstuff.h" void line_header::add_include_dir (const char *include_dir) @@ -73,7 +74,7 @@ line_header::file_file_name (int file) const { const char *dir = fe->include_dir (this); if (dir != NULL) - return string_printf ("%s/%s", dir, fe->name); + return path_join (dir, fe->name); } return fe->name; diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c index bb72e0e..8586463 100644 --- a/gdb/dwarf2/read.c +++ b/gdb/dwarf2/read.c @@ -1280,7 +1280,7 @@ static const char *compute_include_file_name (const struct line_header *lh, const file_entry &fe, const file_and_directory &cu_info, - gdb::unique_xmalloc_ptr<char> *name_holder); + std::string &name_holder); static htab_up allocate_signatured_type_table (); @@ -2780,9 +2780,9 @@ dw2_get_file_names_reader (const struct die_reader_specs *reader, { for (const auto &entry : lh->file_names ()) { - gdb::unique_xmalloc_ptr<char> name_holder; + std::string name_holder; const char *include_name = - compute_include_file_name (lh.get (), entry, fnd, &name_holder); + compute_include_file_name (lh.get (), entry, fnd, name_holder); if (include_name != nullptr) { include_name = per_objfile->objfile->intern (include_name); @@ -11098,14 +11098,13 @@ open_dwo_file (dwarf2_per_objfile *per_objfile, if (comp_dir != NULL) { - gdb::unique_xmalloc_ptr<char> path_to_try - (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL)); + std::string path_to_try = path_join (comp_dir, file_name); /* NOTE: If comp_dir is a relative path, this will also try the search path, which seems useful. */ - gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (), - 0 /*is_dwp*/, - 1 /*search_cwd*/)); + gdb_bfd_ref_ptr abfd (try_open_dwop_file + (per_objfile, path_to_try.c_str (), 0 /*is_dwp*/, 1 /*search_cwd*/)); + if (abfd != NULL) return abfd; } @@ -19697,14 +19696,14 @@ dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu) static const char * compute_include_file_name (const struct line_header *lh, const file_entry &fe, const file_and_directory &cu_info, - gdb::unique_xmalloc_ptr<char> *name_holder) + std::string &name_holder) { const char *include_name = fe.name; const char *include_name_to_compare = include_name; const char *dir_name = fe.include_dir (lh); - gdb::unique_xmalloc_ptr<char> hold_compare; + std::string hold_compare; if (!IS_ABSOLUTE_PATH (include_name) && (dir_name != nullptr || cu_info.get_comp_dir () != nullptr)) { @@ -19731,27 +19730,24 @@ compute_include_file_name (const struct line_header *lh, const file_entry &fe, if (dir_name != NULL) { - name_holder->reset (concat (dir_name, SLASH_STRING, - include_name, (char *) NULL)); - include_name = name_holder->get (); + name_holder = path_join (dir_name, include_name); + include_name = name_holder.c_str (); include_name_to_compare = include_name; } if (!IS_ABSOLUTE_PATH (include_name) && cu_info.get_comp_dir () != nullptr) { - hold_compare.reset (concat (cu_info.get_comp_dir (), SLASH_STRING, - include_name, (char *) NULL)); - include_name_to_compare = hold_compare.get (); + hold_compare = path_join (cu_info.get_comp_dir (), include_name); + include_name_to_compare = hold_compare.c_str (); } } - gdb::unique_xmalloc_ptr<char> copied_name; + std::string copied_name; const char *cu_filename = cu_info.get_name (); if (!IS_ABSOLUTE_PATH (cu_filename) && cu_info.get_comp_dir () != nullptr) { - copied_name.reset (concat (cu_info.get_comp_dir (), SLASH_STRING, - cu_filename, (char *) NULL)); - cu_filename = copied_name.get (); + copied_name = path_join (cu_info.get_comp_dir (), cu_filename); + cu_filename = copied_name.c_str (); } if (FILENAME_CMP (include_name_to_compare, cu_filename) == 0) @@ -20486,7 +20482,7 @@ static void dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename, const char *dirname) { - gdb::unique_xmalloc_ptr<char> copy; + std::string copy; /* In order not to lose the line information directory, we concatenate it to the filename when it makes sense. @@ -20497,8 +20493,8 @@ dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename, if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL) { - copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL)); - filename = copy.get (); + copy = path_join (dirname, filename); + filename = copy.c_str (); } cu->get_builder ()->start_subfile (filename); |