blob: b0a67eaf2ce8c12246809cbffbdae5719b75d753 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Naveen N. Raod2332092015-04-28 17:35:35 +05302/*
Naveen N. Raod2332092015-04-28 17:35:35 +05303 *
4 * Copyright (C) 2015 Naveen N. Rao, IBM Corporation
5 */
6
7#include "debug.h"
8#include "symbol.h"
Naveen N. Rao031b84c2015-04-28 17:35:37 +05309#include "map.h"
Naveen N. Raod5c2e2c2015-04-28 17:35:39 +053010#include "probe-event.h"
Naveen N. Rao44ca9342017-03-08 13:56:10 +053011#include "probe-file.h"
Naveen N. Raod2332092015-04-28 17:35:35 +053012
13#ifdef HAVE_LIBELF_SUPPORT
14bool elf__needs_adjust_symbols(GElf_Ehdr ehdr)
15{
16 return ehdr.e_type == ET_EXEC ||
17 ehdr.e_type == ET_REL ||
18 ehdr.e_type == ET_DYN;
19}
Ananth N Mavinakayanahallic50fc0a2015-04-28 17:35:38 +053020
Naveen N. Raod2332092015-04-28 17:35:35 +053021#endif
Naveen N. Raofb6d5942015-04-28 17:35:36 +053022
Naveen N. Raofb6d5942015-04-28 17:35:36 +053023int arch__choose_best_symbol(struct symbol *syma,
24 struct symbol *symb __maybe_unused)
25{
26 char *sym = syma->name;
27
Sandipan Dasfa694162018-08-28 14:38:48 +053028#if !defined(_CALL_ELF) || _CALL_ELF != 2
Naveen N. Raofb6d5942015-04-28 17:35:36 +053029 /* Skip over any initial dot */
30 if (*sym == '.')
31 sym++;
Sandipan Dasfa694162018-08-28 14:38:48 +053032#endif
Naveen N. Raofb6d5942015-04-28 17:35:36 +053033
34 /* Avoid "SyS" kernel syscall aliases */
35 if (strlen(sym) >= 3 && !strncmp(sym, "SyS", 3))
36 return SYMBOL_B;
37 if (strlen(sym) >= 10 && !strncmp(sym, "compat_SyS", 10))
38 return SYMBOL_B;
39
40 return SYMBOL_A;
41}
Naveen N. Rao031b84c2015-04-28 17:35:37 +053042
Sandipan Dasfa694162018-08-28 14:38:48 +053043#if !defined(_CALL_ELF) || _CALL_ELF != 2
Naveen N. Rao031b84c2015-04-28 17:35:37 +053044/* Allow matching against dot variants */
45int arch__compare_symbol_names(const char *namea, const char *nameb)
46{
47 /* Skip over initial dot */
48 if (*namea == '.')
49 namea++;
50 if (*nameb == '.')
51 nameb++;
52
53 return strcmp(namea, nameb);
54}
Paul Clarked80406452017-04-25 13:15:49 -050055
56int arch__compare_symbol_names_n(const char *namea, const char *nameb,
57 unsigned int n)
58{
59 /* Skip over initial dot */
60 if (*namea == '.')
61 namea++;
62 if (*nameb == '.')
63 nameb++;
64
65 return strncmp(namea, nameb, n);
66}
Masami Hiramatsu4b3a2712017-12-09 01:28:12 +090067
68const char *arch__normalize_symbol_name(const char *name)
69{
70 /* Skip over initial dot */
71 if (name && *name == '.')
72 name++;
73 return name;
74}
Naveen N. Raofb6d5942015-04-28 17:35:36 +053075#endif
Naveen N. Raod5c2e2c2015-04-28 17:35:39 +053076
77#if defined(_CALL_ELF) && _CALL_ELF == 2
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +053078
Naveen N. Rao0b3c2262016-04-12 14:40:50 +053079#ifdef HAVE_LIBELF_SUPPORT
80void arch__sym_update(struct symbol *s, GElf_Sym *sym)
81{
82 s->arch_sym = sym->st_other;
83}
84#endif
85
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +053086#define PPC64LE_LEP_OFFSET 8
87
88void arch__fix_tev_from_maps(struct perf_probe_event *pev,
Naveen N. Rao0b3c2262016-04-12 14:40:50 +053089 struct probe_trace_event *tev, struct map *map,
90 struct symbol *sym)
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +053091{
Naveen N. Rao0b3c2262016-04-12 14:40:50 +053092 int lep_offset;
93
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +053094 /*
Naveen N. Rao239aeba2016-04-12 14:40:49 +053095 * When probing at a function entry point, we normally always want the
96 * LEP since that catches calls to the function through both the GEP and
97 * the LEP. Hence, we would like to probe at an offset of 8 bytes if
98 * the user only specified the function entry.
99 *
100 * However, if the user specifies an offset, we fall back to using the
101 * GEP since all userspace applications (objdump/readelf) show function
102 * disassembly with offsets from the GEP.
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +0530103 */
Naveen N. Rao44ca9342017-03-08 13:56:10 +0530104 if (pev->point.offset || !map || !sym)
Naveen N. Rao239aeba2016-04-12 14:40:49 +0530105 return;
106
Naveen N. Rao44ca9342017-03-08 13:56:10 +0530107 /* For kretprobes, add an offset only if the kernel supports it */
108 if (!pev->uprobes && pev->point.retprobe) {
109#ifdef HAVE_LIBELF_SUPPORT
110 if (!kretprobe_offset_is_supported())
111#endif
112 return;
113 }
114
Naveen N. Rao0b3c2262016-04-12 14:40:50 +0530115 lep_offset = PPC64_LOCAL_ENTRY_OFFSET(sym->arch_sym);
116
Naveen N. Rao239aeba2016-04-12 14:40:49 +0530117 if (map->dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS)
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +0530118 tev->point.offset += PPC64LE_LEP_OFFSET;
Naveen N. Rao0b3c2262016-04-12 14:40:50 +0530119 else if (lep_offset) {
120 if (pev->uprobes)
121 tev->point.address += lep_offset;
122 else
123 tev->point.offset += lep_offset;
124 }
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +0530125}
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500126
Ravi Bangoriaf046f3d2016-08-11 14:43:59 -0300127#ifdef HAVE_LIBELF_SUPPORT
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500128void arch__post_process_probe_trace_events(struct perf_probe_event *pev,
129 int ntevs)
130{
131 struct probe_trace_event *tev;
132 struct map *map;
133 struct symbol *sym = NULL;
134 struct rb_node *tmp;
135 int i = 0;
136
Krister Johansen544abd42017-07-05 18:48:10 -0700137 map = get_target_map(pev->target, pev->nsi, pev->uprobes);
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300138 if (!map || map__load(map) < 0)
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500139 return;
140
141 for (i = 0; i < ntevs; i++) {
142 tev = &pev->tevs[i];
143 map__for_each_symbol(map, sym, tmp) {
Sandipan Das354b0642018-08-09 21:49:29 +0530144 if (map->unmap_ip(map, sym->start) == tev->point.address) {
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500145 arch__fix_tev_from_maps(pev, tev, map, sym);
Sandipan Das354b0642018-08-09 21:49:29 +0530146 break;
147 }
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500148 }
149 }
150}
Ravi Bangoriaf046f3d2016-08-11 14:43:59 -0300151#endif /* HAVE_LIBELF_SUPPORT */
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500152
Naveen N. Raod5c2e2c2015-04-28 17:35:39 +0530153#endif