GNU Linux-libre 4.9.294-gnu1
[releases.git] / tools / perf / arch / powerpc / util / sym-handling.c
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"
11 #include "map.h"
12 #include "probe-event.h"
13
14 #ifdef HAVE_LIBELF_SUPPORT
15 bool elf__needs_adjust_symbols(GElf_Ehdr ehdr)
16 {
17         return ehdr.e_type == ET_EXEC ||
18                ehdr.e_type == ET_REL ||
19                ehdr.e_type == ET_DYN;
20 }
21
22 #endif
23
24 int arch__choose_best_symbol(struct symbol *syma,
25                              struct symbol *symb __maybe_unused)
26 {
27         char *sym = syma->name;
28
29 #if !defined(_CALL_ELF) || _CALL_ELF != 2
30         /* Skip over any initial dot */
31         if (*sym == '.')
32                 sym++;
33 #endif
34
35         /* Avoid "SyS" kernel syscall aliases */
36         if (strlen(sym) >= 3 && !strncmp(sym, "SyS", 3))
37                 return SYMBOL_B;
38         if (strlen(sym) >= 10 && !strncmp(sym, "compat_SyS", 10))
39                 return SYMBOL_B;
40
41         return SYMBOL_A;
42 }
43
44 #if !defined(_CALL_ELF) || _CALL_ELF != 2
45 /* Allow matching against dot variants */
46 int arch__compare_symbol_names(const char *namea, const char *nameb)
47 {
48         /* Skip over initial dot */
49         if (*namea == '.')
50                 namea++;
51         if (*nameb == '.')
52                 nameb++;
53
54         return strcmp(namea, nameb);
55 }
56 #endif
57
58 #if defined(_CALL_ELF) && _CALL_ELF == 2
59
60 #ifdef HAVE_LIBELF_SUPPORT
61 void arch__sym_update(struct symbol *s, GElf_Sym *sym)
62 {
63         s->arch_sym = sym->st_other;
64 }
65 #endif
66
67 #define PPC64LE_LEP_OFFSET      8
68
69 void arch__fix_tev_from_maps(struct perf_probe_event *pev,
70                              struct probe_trace_event *tev, struct map *map,
71                              struct symbol *sym)
72 {
73         int lep_offset;
74
75         /*
76          * When probing at a function entry point, we normally always want the
77          * LEP since that catches calls to the function through both the GEP and
78          * the LEP. Hence, we would like to probe at an offset of 8 bytes if
79          * the user only specified the function entry.
80          *
81          * However, if the user specifies an offset, we fall back to using the
82          * GEP since all userspace applications (objdump/readelf) show function
83          * disassembly with offsets from the GEP.
84          *
85          * In addition, we shouldn't specify an offset for kretprobes.
86          */
87         if (pev->point.offset || (!pev->uprobes && pev->point.retprobe) ||
88             !map || !sym)
89                 return;
90
91         lep_offset = PPC64_LOCAL_ENTRY_OFFSET(sym->arch_sym);
92
93         if (map->dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS)
94                 tev->point.offset += PPC64LE_LEP_OFFSET;
95         else if (lep_offset) {
96                 if (pev->uprobes)
97                         tev->point.address += lep_offset;
98                 else
99                         tev->point.offset += lep_offset;
100         }
101 }
102
103 #ifdef HAVE_LIBELF_SUPPORT
104 void arch__post_process_probe_trace_events(struct perf_probe_event *pev,
105                                            int ntevs)
106 {
107         struct probe_trace_event *tev;
108         struct map *map;
109         struct symbol *sym = NULL;
110         struct rb_node *tmp;
111         int i = 0;
112
113         map = get_target_map(pev->target, pev->uprobes);
114         if (!map || map__load(map) < 0)
115                 return;
116
117         for (i = 0; i < ntevs; i++) {
118                 tev = &pev->tevs[i];
119                 map__for_each_symbol(map, sym, tmp) {
120                         if (map->unmap_ip(map, sym->start) == tev->point.address) {
121                                 arch__fix_tev_from_maps(pev, tev, map, sym);
122                                 break;
123                         }
124                 }
125         }
126 }
127 #endif /* HAVE_LIBELF_SUPPORT */
128
129 #endif