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 #include "probe-file.h"
14 
15 #ifdef HAVE_LIBELF_SUPPORT
16 bool 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 }
22 
23 #endif
24 
25 int arch__choose_best_symbol(struct symbol *syma,
26 			     struct symbol *symb __maybe_unused)
27 {
28 	char *sym = syma->name;
29 
30 #if !defined(_CALL_ELF) || _CALL_ELF != 2
31 	/* Skip over any initial dot */
32 	if (*sym == '.')
33 		sym++;
34 #endif
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 }
44 
45 #if !defined(_CALL_ELF) || _CALL_ELF != 2
46 /* Allow matching against dot variants */
47 int 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 }
57 
58 int 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 }
69 
70 const char *arch__normalize_symbol_name(const char *name)
71 {
72 	/* Skip over initial dot */
73 	if (name && *name == '.')
74 		name++;
75 	return name;
76 }
77 #endif
78 
79 #if defined(_CALL_ELF) && _CALL_ELF == 2
80 
81 #ifdef HAVE_LIBELF_SUPPORT
82 void arch__sym_update(struct symbol *s, GElf_Sym *sym)
83 {
84 	s->arch_sym = sym->st_other;
85 }
86 #endif
87 
88 #define PPC64LE_LEP_OFFSET	8
89 
90 void arch__fix_tev_from_maps(struct perf_probe_event *pev,
91 			     struct probe_trace_event *tev, struct map *map,
92 			     struct symbol *sym)
93 {
94 	int lep_offset;
95 
96 	/*
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.
105 	 */
106 	if (pev->point.offset || !map || !sym)
107 		return;
108 
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 
117 	lep_offset = PPC64_LOCAL_ENTRY_OFFSET(sym->arch_sym);
118 
119 	if (map->dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS)
120 		tev->point.offset += PPC64LE_LEP_OFFSET;
121 	else if (lep_offset) {
122 		if (pev->uprobes)
123 			tev->point.address += lep_offset;
124 		else
125 			tev->point.offset += lep_offset;
126 	}
127 }
128 
129 #ifdef HAVE_LIBELF_SUPPORT
130 void 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 
139 	map = get_target_map(pev->target, pev->nsi, pev->uprobes);
140 	if (!map || map__load(map) < 0)
141 		return;
142 
143 	for (i = 0; i < ntevs; i++) {
144 		tev = &pev->tevs[i];
145 		map__for_each_symbol(map, sym, tmp) {
146 			if (map->unmap_ip(map, sym->start) == tev->point.address) {
147 				arch__fix_tev_from_maps(pev, tev, map, sym);
148 				break;
149 			}
150 		}
151 	}
152 }
153 #endif /* HAVE_LIBELF_SUPPORT */
154 
155 #endif
156