0003-Add-helper-function-for-basename.patch 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. From 2296679efa547104ea52bf60cdda19e07c8d1e26 Mon Sep 17 00:00:00 2001
  2. From: Khem Raj <raj.khem@gmail.com>
  3. Date: Sun, 10 Dec 2023 12:20:33 -0800
  4. Subject: [PATCH] Add helper function for basename
  5. musl does not provide GNU version of basename and lately have removed
  6. the definiton from string.h [1] which exposes this problem. It can be
  7. made to work by providing a local implementation of basename which
  8. implements the GNU basename behavior, this makes it work across C
  9. libraries which have POSIX implementation only.
  10. [1] https://git.musl-libc.org/cgit/musl/commit/?id=725e17ed6dff4d0cd22487bb64470881e86a92e7
  11. * lib/system.h (xbasename): New static inline functions.
  12. Poison basename.
  13. * libdw/dwarf_getsrc_file.c (dwarf_getsrc_file): Use xbasename.
  14. * libdwfl/core-file.c (dwfl_core_file_report): Likewise.
  15. * libdwfl/dwfl_module_getsrc_file.c (dwfl_module_getsrc_file):
  16. Likewise.
  17. * libdwfl/dwfl_segment_report_module.c (dwfl_segment_report_module):
  18. Likewise.
  19. * libdwfl/find-debuginfo.c (find_debuginfo_in_path): Likewise.
  20. * libdwfl/link_map.c (report_r_debug): Likewise.
  21. * libdwfl/linux-kernel-modules.c (try_kernel_name): Likewise.
  22. * src/addr2line.c (print_dwarf_function): Likewise.
  23. (print_src): Likewise.
  24. * src/ar.c (do_oper_insert): Likewise.
  25. And cast away const in entry.key assignment.
  26. * src/nm.c (show_symbols): Use xbasename.
  27. * src/stack.c (module_callback): Likewise.
  28. * src/strip.c (handle_elf): Likewise.
  29. * tests/show-die-info.c: Include system.h.
  30. (dwarf_tag_string): Use xbasename.
  31. * tests/varlocs.c: Likewise.
  32. * debuginfod/debuginfod.cxx: Move include system.h to the end.
  33. (register_file_name): Rename basename to filename.
  34. Signed-off-by: Khem Raj <raj.khem@gmail.com>
  35. Signed-off-by: Mark Wielaard <mark@klomp.org>
  36. Upstream: https://sourceware.org/git/?p=elfutils.git;a=commit;h=a2194f6b305bf0d0b9dd49dccd0a5c21994c8eea
  37. Signed-off-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
  38. ---
  39. debuginfod/debuginfod.cxx | 10 ++++++++--
  40. lib/system.h | 14 ++++++++++++++
  41. libdw/dwarf_getsrc_file.c | 2 +-
  42. libdwfl/core-file.c | 2 +-
  43. libdwfl/dwfl_module_getsrc_file.c | 2 +-
  44. libdwfl/dwfl_segment_report_module.c | 2 +-
  45. libdwfl/find-debuginfo.c | 6 +++---
  46. libdwfl/link_map.c | 2 +-
  47. libdwfl/linux-kernel-modules.c | 2 +-
  48. src/addr2line.c | 4 ++--
  49. src/ar.c | 4 ++--
  50. src/nm.c | 4 ++--
  51. src/stack.c | 2 +-
  52. src/strip.c | 2 +-
  53. tests/show-die-info.c | 3 ++-
  54. tests/varlocs.c | 2 +-
  55. 16 files changed, 42 insertions(+), 21 deletions(-)
  56. diff --git a/debuginfod/debuginfod.cxx b/debuginfod/debuginfod.cxx
  57. index 99b1f2b9..3b69a621 100644
  58. --- a/debuginfod/debuginfod.cxx
  59. +++ b/debuginfod/debuginfod.cxx
  60. @@ -44,10 +44,11 @@ extern "C" {
  61. }
  62. #endif
  63. +#ifdef HAVE_EXECINFO_H
  64. extern "C" {
  65. -#include "printversion.h"
  66. -#include "system.h"
  67. +#include <execinfo.h>
  68. }
  69. +#endif
  70. #include "debuginfod.h"
  71. #include <dwarf.h>
  72. @@ -127,6 +128,11 @@ using namespace std;
  73. #define tid() pthread_self()
  74. #endif
  75. +extern "C" {
  76. +#include "printversion.h"
  77. +#include "system.h"
  78. +}
  79. +
  80. inline bool
  81. string_endswith(const string& haystack, const string& needle)
  82. diff --git a/lib/system.h b/lib/system.h
  83. index 1c914efc..0db12d99 100644
  84. --- a/lib/system.h
  85. +++ b/lib/system.h
  86. @@ -1,6 +1,7 @@
  87. /* Declarations for common convenience functions.
  88. Copyright (C) 2006-2011 Red Hat, Inc.
  89. Copyright (C) 2022 Mark J. Wielaard <mark@klomp.org>
  90. + Copyright (C) 2023 Khem Raj.
  91. This file is part of elfutils.
  92. This file is free software; you can redistribute it and/or modify
  93. @@ -211,4 +212,17 @@ extern char *__cxa_demangle (const char *mangled_name, char *output_buffer,
  94. extern int never_defined_just_used_for_checking[(expr) ? 1 : -1] \
  95. __attribute__ ((unused))
  96. +/* We really want a basename implementation that doesn't modify the
  97. + input argument. Normally you get that from string.h with _GNU_SOURCE
  98. + define. But some libc implementations don't define it and other
  99. + define it, but provide an implementation that still modifies the
  100. + argument. So define our own and poison a bare basename symbol. */
  101. +static inline const char *
  102. +xbasename(const char *s)
  103. +{
  104. + const char *p = strrchr(s, '/');
  105. + return p ? p+1 : s;
  106. +}
  107. +#pragma GCC poison basename
  108. +
  109. #endif /* system.h */
  110. diff --git a/libdw/dwarf_getsrc_file.c b/libdw/dwarf_getsrc_file.c
  111. index 5289c7da..03da431c 100644
  112. --- a/libdw/dwarf_getsrc_file.c
  113. +++ b/libdw/dwarf_getsrc_file.c
  114. @@ -98,7 +98,7 @@ dwarf_getsrc_file (Dwarf *dbg, const char *fname, int lineno, int column,
  115. /* Match the name with the name the user provided. */
  116. const char *fname2 = line->files->info[lastfile].name;
  117. if (is_basename)
  118. - lastmatch = strcmp (basename (fname2), fname) == 0;
  119. + lastmatch = strcmp (xbasename (fname2), fname) == 0;
  120. else
  121. lastmatch = strcmp (fname2, fname) == 0;
  122. }
  123. diff --git a/libdwfl/core-file.c b/libdwfl/core-file.c
  124. index 87c940cb..89527d23 100644
  125. --- a/libdwfl/core-file.c
  126. +++ b/libdwfl/core-file.c
  127. @@ -595,7 +595,7 @@ dwfl_core_file_report (Dwfl *dwfl, Elf *elf, const char *executable)
  128. if (! __libdwfl_dynamic_vaddr_get (module->elf, &file_dynamic_vaddr))
  129. continue;
  130. Dwfl_Module *mod;
  131. - mod = __libdwfl_report_elf (dwfl, basename (module->name), module->name,
  132. + mod = __libdwfl_report_elf (dwfl, xbasename (module->name), module->name,
  133. module->fd, module->elf,
  134. module->l_ld - file_dynamic_vaddr,
  135. true, true);
  136. diff --git a/libdwfl/dwfl_module_getsrc_file.c b/libdwfl/dwfl_module_getsrc_file.c
  137. index 513af6b8..fc144225 100644
  138. --- a/libdwfl/dwfl_module_getsrc_file.c
  139. +++ b/libdwfl/dwfl_module_getsrc_file.c
  140. @@ -103,7 +103,7 @@ dwfl_module_getsrc_file (Dwfl_Module *mod,
  141. {
  142. /* Match the name with the name the user provided. */
  143. lastfile = file;
  144. - lastmatch = !strcmp (is_basename ? basename (file) : file,
  145. + lastmatch = !strcmp (is_basename ? xbasename (file) : file,
  146. fname);
  147. }
  148. }
  149. diff --git a/libdwfl/dwfl_segment_report_module.c b/libdwfl/dwfl_segment_report_module.c
  150. index 3ef62a7d..d0df7100 100644
  151. --- a/libdwfl/dwfl_segment_report_module.c
  152. +++ b/libdwfl/dwfl_segment_report_module.c
  153. @@ -718,7 +718,7 @@ dwfl_segment_report_module (Dwfl *dwfl, int ndx, const char *name,
  154. bias += fixup;
  155. if (module->name[0] != '\0')
  156. {
  157. - name = basename (module->name);
  158. + name = xbasename (module->name);
  159. name_is_final = true;
  160. }
  161. break;
  162. diff --git a/libdwfl/find-debuginfo.c b/libdwfl/find-debuginfo.c
  163. index 7f7ab632..b358c774 100644
  164. --- a/libdwfl/find-debuginfo.c
  165. +++ b/libdwfl/find-debuginfo.c
  166. @@ -164,7 +164,7 @@ find_debuginfo_in_path (Dwfl_Module *mod, const char *file_name,
  167. {
  168. bool cancheck = debuglink_crc != (GElf_Word) 0;
  169. - const char *file_basename = file_name == NULL ? NULL : basename (file_name);
  170. + const char *file_basename = file_name == NULL ? NULL : xbasename (file_name);
  171. char *localname = NULL;
  172. /* We invent a debuglink .debug name if NULL, but then want to try the
  173. @@ -278,7 +278,7 @@ find_debuginfo_in_path (Dwfl_Module *mod, const char *file_name,
  174. else
  175. {
  176. subdir = NULL;
  177. - file = basename (debuglink_file);
  178. + file = xbasename (debuglink_file);
  179. }
  180. try_file_basename = debuglink_null;
  181. break;
  182. @@ -306,7 +306,7 @@ find_debuginfo_in_path (Dwfl_Module *mod, const char *file_name,
  183. if (mod->dw != NULL && (p[0] == '\0' || p[0] == '/'))
  184. {
  185. fd = try_open (&main_stat, dir, ".dwz",
  186. - basename (file), &fname);
  187. + xbasename (file), &fname);
  188. if (fd < 0)
  189. {
  190. if (errno != ENOENT && errno != ENOTDIR)
  191. diff --git a/libdwfl/link_map.c b/libdwfl/link_map.c
  192. index 06d85eb6..9d6b68c1 100644
  193. --- a/libdwfl/link_map.c
  194. +++ b/libdwfl/link_map.c
  195. @@ -469,7 +469,7 @@ report_r_debug (uint_fast8_t elfclass, uint_fast8_t elfdata,
  196. if (r_debug_info_module == NULL)
  197. {
  198. // XXX hook for sysroot
  199. - mod = __libdwfl_report_elf (dwfl, basename (name),
  200. + mod = __libdwfl_report_elf (dwfl, xbasename (name),
  201. name, fd, elf, base,
  202. true, true);
  203. if (mod != NULL)
  204. diff --git a/libdwfl/linux-kernel-modules.c b/libdwfl/linux-kernel-modules.c
  205. index 58c0c417..e9faba26 100644
  206. --- a/libdwfl/linux-kernel-modules.c
  207. +++ b/libdwfl/linux-kernel-modules.c
  208. @@ -116,7 +116,7 @@ try_kernel_name (Dwfl *dwfl, char **fname, bool try_debug)
  209. /* Try the file's unadorned basename as DEBUGLINK_FILE,
  210. to look only for "vmlinux" files. */
  211. fd = INTUSE(dwfl_standard_find_debuginfo) (&fakemod, NULL, NULL, 0,
  212. - *fname, basename (*fname),
  213. + *fname, xbasename (*fname),
  214. 0, &fakemod.debug.name);
  215. if (fakemod.debug.name != NULL)
  216. diff --git a/src/addr2line.c b/src/addr2line.c
  217. index d902d791..d87e5b45 100644
  218. --- a/src/addr2line.c
  219. +++ b/src/addr2line.c
  220. @@ -385,7 +385,7 @@ print_dwarf_function (Dwfl_Module *mod, Dwarf_Addr addr)
  221. if (file == NULL)
  222. file = "???";
  223. else if (only_basenames)
  224. - file = basename (file);
  225. + file = xbasename (file);
  226. else if (use_comp_dir && file[0] != '/')
  227. {
  228. const char *const *dirs;
  229. @@ -568,7 +568,7 @@ print_src (const char *src, int lineno, int linecol, Dwarf_Die *cu)
  230. const char *comp_dir_sep = "";
  231. if (only_basenames)
  232. - src = basename (src);
  233. + src = xbasename (src);
  234. else if (use_comp_dir && src[0] != '/')
  235. {
  236. Dwarf_Attribute attr;
  237. diff --git a/src/ar.c b/src/ar.c
  238. index 3bcb18fe..e6d6d58f 100644
  239. --- a/src/ar.c
  240. +++ b/src/ar.c
  241. @@ -1133,7 +1133,7 @@ do_oper_insert (int oper, const char *arfname, char **argv, int argc,
  242. for (int cnt = 0; cnt < argc; ++cnt)
  243. {
  244. ENTRY entry;
  245. - entry.key = full_path ? argv[cnt] : basename (argv[cnt]);
  246. + entry.key = full_path ? argv[cnt] : (char*)xbasename (argv[cnt]);
  247. entry.data = &argv[cnt];
  248. if (hsearch (entry, ENTER) == NULL)
  249. error_exit (errno, _("cannot insert into hash table"));
  250. @@ -1242,7 +1242,7 @@ do_oper_insert (int oper, const char *arfname, char **argv, int argc,
  251. /* Open all the new files, get their sizes and add all symbols. */
  252. for (int cnt = 0; cnt < argc; ++cnt)
  253. {
  254. - const char *bname = basename (argv[cnt]);
  255. + const char *bname = xbasename (argv[cnt]);
  256. size_t bnamelen = strlen (bname);
  257. if (found[cnt] == NULL)
  258. {
  259. diff --git a/src/nm.c b/src/nm.c
  260. index fbdee8e1..3675f59b 100644
  261. --- a/src/nm.c
  262. +++ b/src/nm.c
  263. @@ -1417,7 +1417,7 @@ show_symbols (int fd, Ebl *ebl, GElf_Ehdr *ehdr,
  264. int lineno;
  265. (void) dwarf_lineno (line, &lineno);
  266. const char *file = dwarf_linesrc (line, NULL, NULL);
  267. - file = (file != NULL) ? basename (file) : "???";
  268. + file = (file != NULL) ? xbasename (file) : "???";
  269. int n;
  270. n = obstack_printf (&whereob, "%s:%d%c", file,
  271. lineno, '\0');
  272. @@ -1448,7 +1448,7 @@ show_symbols (int fd, Ebl *ebl, GElf_Ehdr *ehdr,
  273. {
  274. /* We found the line. */
  275. int n = obstack_printf (&whereob, "%s:%" PRIu64 "%c",
  276. - basename ((*found)->file),
  277. + xbasename ((*found)->file),
  278. (*found)->lineno,
  279. '\0');
  280. sym_mem[nentries_used].where = obstack_finish (&whereob);
  281. diff --git a/src/stack.c b/src/stack.c
  282. index 534aa93c..f4c5ba8c 100644
  283. --- a/src/stack.c
  284. +++ b/src/stack.c
  285. @@ -152,7 +152,7 @@ module_callback (Dwfl_Module *mod, void **userdata __attribute__((unused)),
  286. int width = get_addr_width (mod);
  287. printf ("0x%0*" PRIx64 "-0x%0*" PRIx64 " %s\n",
  288. - width, start, width, end, basename (name));
  289. + width, start, width, end, xbasename (name));
  290. const unsigned char *id;
  291. GElf_Addr id_vaddr;
  292. diff --git a/src/strip.c b/src/strip.c
  293. index 2a2cc801..88977a5c 100644
  294. --- a/src/strip.c
  295. +++ b/src/strip.c
  296. @@ -1800,7 +1800,7 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
  297. elf_errmsg (-1));
  298. }
  299. - char *debug_basename = basename (debug_fname_embed ?: debug_fname);
  300. + const char *debug_basename = xbasename (debug_fname_embed ?: debug_fname);
  301. off_t crc_offset = strlen (debug_basename) + 1;
  302. /* Align to 4 byte boundary */
  303. crc_offset = ((crc_offset - 1) & ~3) + 4;
  304. diff --git a/tests/show-die-info.c b/tests/show-die-info.c
  305. index 1a3191cd..bda459a5 100644
  306. --- a/tests/show-die-info.c
  307. +++ b/tests/show-die-info.c
  308. @@ -27,6 +27,7 @@
  309. #include <unistd.h>
  310. #include "../libdw/known-dwarf.h"
  311. +#include "../lib/system.h"
  312. static const char *
  313. dwarf_tag_string (unsigned int tag)
  314. @@ -318,7 +319,7 @@ main (int argc, char *argv[])
  315. int fd = open (argv[cnt], O_RDONLY);
  316. Dwarf *dbg;
  317. - printf ("file: %s\n", basename (argv[cnt]));
  318. + printf ("file: %s\n", xbasename (argv[cnt]));
  319. dbg = dwarf_begin (fd, DWARF_C_READ);
  320. if (dbg == NULL)
  321. diff --git a/tests/varlocs.c b/tests/varlocs.c
  322. index 8e563fd3..1004f969 100644
  323. --- a/tests/varlocs.c
  324. +++ b/tests/varlocs.c
  325. @@ -1120,7 +1120,7 @@ main (int argc, char *argv[])
  326. const char *name = (modname[0] != '\0'
  327. ? modname
  328. - : basename (mainfile));
  329. + : xbasename (mainfile));
  330. printf ("module '%s'\n", name);
  331. print_die (&cudie, "CU", 0);
  332. --
  333. 2.48.1