Merge tag 'for-linus-20181019' of git://git.kernel.dk/linux-block
[linux-2.6-block.git] / tools / perf / arch / powerpc / util / sym-handling.c
CommitLineData
d2332098
NR
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2, as
4 * published by the Free Software Foundation.
5 *
6 * Copyright (C) 2015 Naveen N. Rao, IBM Corporation
7 */
8
9#include "debug.h"
10#include "symbol.h"
031b84c4 11#include "map.h"
d5c2e2c1 12#include "probe-event.h"
44ca9341 13#include "probe-file.h"
d2332098
NR
14
15#ifdef HAVE_LIBELF_SUPPORT
16bool elf__needs_adjust_symbols(GElf_Ehdr ehdr)
17{
18 return ehdr.e_type == ET_EXEC ||
19 ehdr.e_type == ET_REL ||
20 ehdr.e_type == ET_DYN;
21}
c50fc0a4 22
d2332098 23#endif
fb6d5942 24
fb6d5942
NR
25int arch__choose_best_symbol(struct symbol *syma,
26 struct symbol *symb __maybe_unused)
27{
28 char *sym = syma->name;
29
fa694160 30#if !defined(_CALL_ELF) || _CALL_ELF != 2
fb6d5942
NR
31 /* Skip over any initial dot */
32 if (*sym == '.')
33 sym++;
fa694160 34#endif
fb6d5942
NR
35
36 /* Avoid "SyS" kernel syscall aliases */
37 if (strlen(sym) >= 3 && !strncmp(sym, "SyS", 3))
38 return SYMBOL_B;
39 if (strlen(sym) >= 10 && !strncmp(sym, "compat_SyS", 10))
40 return SYMBOL_B;
41
42 return SYMBOL_A;
43}
031b84c4 44
fa694160 45#if !defined(_CALL_ELF) || _CALL_ELF != 2
031b84c4
NR
46/* Allow matching against dot variants */
47int arch__compare_symbol_names(const char *namea, const char *nameb)
48{
49 /* Skip over initial dot */
50 if (*namea == '.')
51 namea++;
52 if (*nameb == '.')
53 nameb++;
54
55 return strcmp(namea, nameb);
56}
d8040645
PC
57
58int arch__compare_symbol_names_n(const char *namea, const char *nameb,
59 unsigned int n)
60{
61 /* Skip over initial dot */
62 if (*namea == '.')
63 namea++;
64 if (*nameb == '.')
65 nameb++;
66
67 return strncmp(namea, nameb, n);
68}
4b3a2716
MH
69
70const char *arch__normalize_symbol_name(const char *name)
71{
72 /* Skip over initial dot */
73 if (name && *name == '.')
74 name++;
75 return name;
76}
fb6d5942 77#endif
d5c2e2c1
NR
78
79#if defined(_CALL_ELF) && _CALL_ELF == 2
7b6ff0bd 80
0b3c2264
NR
81#ifdef HAVE_LIBELF_SUPPORT
82void arch__sym_update(struct symbol *s, GElf_Sym *sym)
83{
84 s->arch_sym = sym->st_other;
85}
86#endif
87
7b6ff0bd
NR
88#define PPC64LE_LEP_OFFSET 8
89
90void arch__fix_tev_from_maps(struct perf_probe_event *pev,
0b3c2264
NR
91 struct probe_trace_event *tev, struct map *map,
92 struct symbol *sym)
7b6ff0bd 93{
0b3c2264
NR
94 int lep_offset;
95
7b6ff0bd 96 /*
239aeba7
NR
97 * When probing at a function entry point, we normally always want the
98 * LEP since that catches calls to the function through both the GEP and
99 * the LEP. Hence, we would like to probe at an offset of 8 bytes if
100 * the user only specified the function entry.
101 *
102 * However, if the user specifies an offset, we fall back to using the
103 * GEP since all userspace applications (objdump/readelf) show function
104 * disassembly with offsets from the GEP.
7b6ff0bd 105 */
44ca9341 106 if (pev->point.offset || !map || !sym)
239aeba7
NR
107 return;
108
44ca9341
NR
109 /* For kretprobes, add an offset only if the kernel supports it */
110 if (!pev->uprobes && pev->point.retprobe) {
111#ifdef HAVE_LIBELF_SUPPORT
112 if (!kretprobe_offset_is_supported())
113#endif
114 return;
115 }
116
0b3c2264
NR
117 lep_offset = PPC64_LOCAL_ENTRY_OFFSET(sym->arch_sym);
118
239aeba7 119 if (map->dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS)
7b6ff0bd 120 tev->point.offset += PPC64LE_LEP_OFFSET;
0b3c2264
NR
121 else if (lep_offset) {
122 if (pev->uprobes)
123 tev->point.address += lep_offset;
124 else
125 tev->point.offset += lep_offset;
126 }
7b6ff0bd 127}
99e608b5 128
f046f3df 129#ifdef HAVE_LIBELF_SUPPORT
99e608b5
RB
130void arch__post_process_probe_trace_events(struct perf_probe_event *pev,
131 int ntevs)
132{
133 struct probe_trace_event *tev;
134 struct map *map;
135 struct symbol *sym = NULL;
136 struct rb_node *tmp;
137 int i = 0;
138
544abd44 139 map = get_target_map(pev->target, pev->nsi, pev->uprobes);
be39db9f 140 if (!map || map__load(map) < 0)
99e608b5
RB
141 return;
142
143 for (i = 0; i < ntevs; i++) {
144 tev = &pev->tevs[i];
145 map__for_each_symbol(map, sym, tmp) {
354b064b 146 if (map->unmap_ip(map, sym->start) == tev->point.address) {
99e608b5 147 arch__fix_tev_from_maps(pev, tev, map, sym);
354b064b
SD
148 break;
149 }
99e608b5
RB
150 }
151 }
152}
f046f3df 153#endif /* HAVE_LIBELF_SUPPORT */
99e608b5 154
d5c2e2c1 155#endif