remap.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* Remap file names for debug info for GNU assembler.
  2. Copyright (C) 2007-2022 Free Software Foundation, Inc.
  3. This file is part of GAS, the GNU Assembler.
  4. GAS is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. GAS is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GAS; see the file COPYING. If not, write to the Free
  14. Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
  15. 02110-1301, USA. */
  16. #include "as.h"
  17. #include "filenames.h"
  18. /* Structure recording the mapping from source file and directory
  19. names at compile time to those to be embedded in debug
  20. information. */
  21. typedef struct debug_prefix_map
  22. {
  23. const char *old_prefix;
  24. const char *new_prefix;
  25. size_t old_len;
  26. size_t new_len;
  27. struct debug_prefix_map *next;
  28. } debug_prefix_map;
  29. /* Linked list of such structures. */
  30. debug_prefix_map *debug_prefix_maps;
  31. /* Record a debug file prefix mapping. ARG is the argument to
  32. -fdebug-prefix-map and must be of the form OLD=NEW. */
  33. void
  34. add_debug_prefix_map (const char *arg)
  35. {
  36. debug_prefix_map *map;
  37. const char *p;
  38. char *o;
  39. p = strchr (arg, '=');
  40. if (!p)
  41. {
  42. as_fatal (_("invalid argument '%s' to -fdebug-prefix-map"), arg);
  43. return;
  44. }
  45. map = XNEW (debug_prefix_map);
  46. o = xstrdup (arg);
  47. map->old_prefix = o;
  48. map->old_len = p - arg;
  49. o[map->old_len] = 0;
  50. p++;
  51. map->new_prefix = xstrdup (p);
  52. map->new_len = strlen (p);
  53. map->next = debug_prefix_maps;
  54. debug_prefix_maps = map;
  55. }
  56. /* Perform user-specified mapping of debug filename prefixes. Returns
  57. a newly allocated buffer containing the name corresponding to FILENAME.
  58. It is the caller's responsibility to free the buffer. */
  59. const char *
  60. remap_debug_filename (const char *filename)
  61. {
  62. debug_prefix_map *map;
  63. for (map = debug_prefix_maps; map; map = map->next)
  64. if (filename_ncmp (filename, map->old_prefix, map->old_len) == 0)
  65. {
  66. const char *name = filename + map->old_len;
  67. return concat (map->new_prefix, name, NULL);
  68. }
  69. return xstrdup (filename);
  70. }