Commit 48bb5dc6cd9d30fe0d594947563da1f8bd9abada

Authored by Steven Rostedt
Committed by Steven Rostedt
1 parent a0e0fac633

ftrace: Make recordmcount.c handle __fentry__

With gcc 4.6.0 the -mfentry feature places the function profiling
call at the start of the function. When this is used, the call is
to __fentry__ and not mcount.

Change recordmcount.c to record both callers to __fentry__ and
mcount.

Link: http://lkml.kernel.org/r/20120807194058.990674363@goodmis.org

Acked-by: H. Peter Anvin <hpa@linux.intel.com>
Acked-by: John Reiser <jreiser@bitwagon.com>
Acked-by: Ingo Molnar <mingo@kernel.org>
Cc: Andi Kleen <andi@firstfloor.org>
Signed-off-by: Steven Rostedt <rostedt@goodmis.org>

Showing 1 changed file with 3 additions and 1 deletions Inline Diff

scripts/recordmcount.h
1 /* 1 /*
2 * recordmcount.h 2 * recordmcount.h
3 * 3 *
4 * This code was taken out of recordmcount.c written by 4 * This code was taken out of recordmcount.c written by
5 * Copyright 2009 John F. Reiser <jreiser@BitWagon.com>. All rights reserved. 5 * Copyright 2009 John F. Reiser <jreiser@BitWagon.com>. All rights reserved.
6 * 6 *
7 * The original code had the same algorithms for both 32bit 7 * The original code had the same algorithms for both 32bit
8 * and 64bit ELF files, but the code was duplicated to support 8 * and 64bit ELF files, but the code was duplicated to support
9 * the difference in structures that were used. This 9 * the difference in structures that were used. This
10 * file creates a macro of everything that is different between 10 * file creates a macro of everything that is different between
11 * the 64 and 32 bit code, such that by including this header 11 * the 64 and 32 bit code, such that by including this header
12 * twice we can create both sets of functions by including this 12 * twice we can create both sets of functions by including this
13 * header once with RECORD_MCOUNT_64 undefined, and again with 13 * header once with RECORD_MCOUNT_64 undefined, and again with
14 * it defined. 14 * it defined.
15 * 15 *
16 * This conversion to macros was done by: 16 * This conversion to macros was done by:
17 * Copyright 2010 Steven Rostedt <srostedt@redhat.com>, Red Hat Inc. 17 * Copyright 2010 Steven Rostedt <srostedt@redhat.com>, Red Hat Inc.
18 * 18 *
19 * Licensed under the GNU General Public License, version 2 (GPLv2). 19 * Licensed under the GNU General Public License, version 2 (GPLv2).
20 */ 20 */
21 #undef append_func 21 #undef append_func
22 #undef is_fake_mcount 22 #undef is_fake_mcount
23 #undef fn_is_fake_mcount 23 #undef fn_is_fake_mcount
24 #undef MIPS_is_fake_mcount 24 #undef MIPS_is_fake_mcount
25 #undef mcount_adjust 25 #undef mcount_adjust
26 #undef sift_rel_mcount 26 #undef sift_rel_mcount
27 #undef nop_mcount 27 #undef nop_mcount
28 #undef find_secsym_ndx 28 #undef find_secsym_ndx
29 #undef __has_rel_mcount 29 #undef __has_rel_mcount
30 #undef has_rel_mcount 30 #undef has_rel_mcount
31 #undef tot_relsize 31 #undef tot_relsize
32 #undef get_mcountsym 32 #undef get_mcountsym
33 #undef get_sym_str_and_relp 33 #undef get_sym_str_and_relp
34 #undef do_func 34 #undef do_func
35 #undef Elf_Addr 35 #undef Elf_Addr
36 #undef Elf_Ehdr 36 #undef Elf_Ehdr
37 #undef Elf_Shdr 37 #undef Elf_Shdr
38 #undef Elf_Rel 38 #undef Elf_Rel
39 #undef Elf_Rela 39 #undef Elf_Rela
40 #undef Elf_Sym 40 #undef Elf_Sym
41 #undef ELF_R_SYM 41 #undef ELF_R_SYM
42 #undef Elf_r_sym 42 #undef Elf_r_sym
43 #undef ELF_R_INFO 43 #undef ELF_R_INFO
44 #undef Elf_r_info 44 #undef Elf_r_info
45 #undef ELF_ST_BIND 45 #undef ELF_ST_BIND
46 #undef ELF_ST_TYPE 46 #undef ELF_ST_TYPE
47 #undef fn_ELF_R_SYM 47 #undef fn_ELF_R_SYM
48 #undef fn_ELF_R_INFO 48 #undef fn_ELF_R_INFO
49 #undef uint_t 49 #undef uint_t
50 #undef _w 50 #undef _w
51 #undef _align 51 #undef _align
52 #undef _size 52 #undef _size
53 53
54 #ifdef RECORD_MCOUNT_64 54 #ifdef RECORD_MCOUNT_64
55 # define append_func append64 55 # define append_func append64
56 # define sift_rel_mcount sift64_rel_mcount 56 # define sift_rel_mcount sift64_rel_mcount
57 # define nop_mcount nop_mcount_64 57 # define nop_mcount nop_mcount_64
58 # define find_secsym_ndx find64_secsym_ndx 58 # define find_secsym_ndx find64_secsym_ndx
59 # define __has_rel_mcount __has64_rel_mcount 59 # define __has_rel_mcount __has64_rel_mcount
60 # define has_rel_mcount has64_rel_mcount 60 # define has_rel_mcount has64_rel_mcount
61 # define tot_relsize tot64_relsize 61 # define tot_relsize tot64_relsize
62 # define get_sym_str_and_relp get_sym_str_and_relp_64 62 # define get_sym_str_and_relp get_sym_str_and_relp_64
63 # define do_func do64 63 # define do_func do64
64 # define get_mcountsym get_mcountsym_64 64 # define get_mcountsym get_mcountsym_64
65 # define is_fake_mcount is_fake_mcount64 65 # define is_fake_mcount is_fake_mcount64
66 # define fn_is_fake_mcount fn_is_fake_mcount64 66 # define fn_is_fake_mcount fn_is_fake_mcount64
67 # define MIPS_is_fake_mcount MIPS64_is_fake_mcount 67 # define MIPS_is_fake_mcount MIPS64_is_fake_mcount
68 # define mcount_adjust mcount_adjust_64 68 # define mcount_adjust mcount_adjust_64
69 # define Elf_Addr Elf64_Addr 69 # define Elf_Addr Elf64_Addr
70 # define Elf_Ehdr Elf64_Ehdr 70 # define Elf_Ehdr Elf64_Ehdr
71 # define Elf_Shdr Elf64_Shdr 71 # define Elf_Shdr Elf64_Shdr
72 # define Elf_Rel Elf64_Rel 72 # define Elf_Rel Elf64_Rel
73 # define Elf_Rela Elf64_Rela 73 # define Elf_Rela Elf64_Rela
74 # define Elf_Sym Elf64_Sym 74 # define Elf_Sym Elf64_Sym
75 # define ELF_R_SYM ELF64_R_SYM 75 # define ELF_R_SYM ELF64_R_SYM
76 # define Elf_r_sym Elf64_r_sym 76 # define Elf_r_sym Elf64_r_sym
77 # define ELF_R_INFO ELF64_R_INFO 77 # define ELF_R_INFO ELF64_R_INFO
78 # define Elf_r_info Elf64_r_info 78 # define Elf_r_info Elf64_r_info
79 # define ELF_ST_BIND ELF64_ST_BIND 79 # define ELF_ST_BIND ELF64_ST_BIND
80 # define ELF_ST_TYPE ELF64_ST_TYPE 80 # define ELF_ST_TYPE ELF64_ST_TYPE
81 # define fn_ELF_R_SYM fn_ELF64_R_SYM 81 # define fn_ELF_R_SYM fn_ELF64_R_SYM
82 # define fn_ELF_R_INFO fn_ELF64_R_INFO 82 # define fn_ELF_R_INFO fn_ELF64_R_INFO
83 # define uint_t uint64_t 83 # define uint_t uint64_t
84 # define _w w8 84 # define _w w8
85 # define _align 7u 85 # define _align 7u
86 # define _size 8 86 # define _size 8
87 #else 87 #else
88 # define append_func append32 88 # define append_func append32
89 # define sift_rel_mcount sift32_rel_mcount 89 # define sift_rel_mcount sift32_rel_mcount
90 # define nop_mcount nop_mcount_32 90 # define nop_mcount nop_mcount_32
91 # define find_secsym_ndx find32_secsym_ndx 91 # define find_secsym_ndx find32_secsym_ndx
92 # define __has_rel_mcount __has32_rel_mcount 92 # define __has_rel_mcount __has32_rel_mcount
93 # define has_rel_mcount has32_rel_mcount 93 # define has_rel_mcount has32_rel_mcount
94 # define tot_relsize tot32_relsize 94 # define tot_relsize tot32_relsize
95 # define get_sym_str_and_relp get_sym_str_and_relp_32 95 # define get_sym_str_and_relp get_sym_str_and_relp_32
96 # define do_func do32 96 # define do_func do32
97 # define get_mcountsym get_mcountsym_32 97 # define get_mcountsym get_mcountsym_32
98 # define is_fake_mcount is_fake_mcount32 98 # define is_fake_mcount is_fake_mcount32
99 # define fn_is_fake_mcount fn_is_fake_mcount32 99 # define fn_is_fake_mcount fn_is_fake_mcount32
100 # define MIPS_is_fake_mcount MIPS32_is_fake_mcount 100 # define MIPS_is_fake_mcount MIPS32_is_fake_mcount
101 # define mcount_adjust mcount_adjust_32 101 # define mcount_adjust mcount_adjust_32
102 # define Elf_Addr Elf32_Addr 102 # define Elf_Addr Elf32_Addr
103 # define Elf_Ehdr Elf32_Ehdr 103 # define Elf_Ehdr Elf32_Ehdr
104 # define Elf_Shdr Elf32_Shdr 104 # define Elf_Shdr Elf32_Shdr
105 # define Elf_Rel Elf32_Rel 105 # define Elf_Rel Elf32_Rel
106 # define Elf_Rela Elf32_Rela 106 # define Elf_Rela Elf32_Rela
107 # define Elf_Sym Elf32_Sym 107 # define Elf_Sym Elf32_Sym
108 # define ELF_R_SYM ELF32_R_SYM 108 # define ELF_R_SYM ELF32_R_SYM
109 # define Elf_r_sym Elf32_r_sym 109 # define Elf_r_sym Elf32_r_sym
110 # define ELF_R_INFO ELF32_R_INFO 110 # define ELF_R_INFO ELF32_R_INFO
111 # define Elf_r_info Elf32_r_info 111 # define Elf_r_info Elf32_r_info
112 # define ELF_ST_BIND ELF32_ST_BIND 112 # define ELF_ST_BIND ELF32_ST_BIND
113 # define ELF_ST_TYPE ELF32_ST_TYPE 113 # define ELF_ST_TYPE ELF32_ST_TYPE
114 # define fn_ELF_R_SYM fn_ELF32_R_SYM 114 # define fn_ELF_R_SYM fn_ELF32_R_SYM
115 # define fn_ELF_R_INFO fn_ELF32_R_INFO 115 # define fn_ELF_R_INFO fn_ELF32_R_INFO
116 # define uint_t uint32_t 116 # define uint_t uint32_t
117 # define _w w 117 # define _w w
118 # define _align 3u 118 # define _align 3u
119 # define _size 4 119 # define _size 4
120 #endif 120 #endif
121 121
122 /* Functions and pointers that do_file() may override for specific e_machine. */ 122 /* Functions and pointers that do_file() may override for specific e_machine. */
123 static int fn_is_fake_mcount(Elf_Rel const *rp) 123 static int fn_is_fake_mcount(Elf_Rel const *rp)
124 { 124 {
125 return 0; 125 return 0;
126 } 126 }
127 static int (*is_fake_mcount)(Elf_Rel const *rp) = fn_is_fake_mcount; 127 static int (*is_fake_mcount)(Elf_Rel const *rp) = fn_is_fake_mcount;
128 128
129 static uint_t fn_ELF_R_SYM(Elf_Rel const *rp) 129 static uint_t fn_ELF_R_SYM(Elf_Rel const *rp)
130 { 130 {
131 return ELF_R_SYM(_w(rp->r_info)); 131 return ELF_R_SYM(_w(rp->r_info));
132 } 132 }
133 static uint_t (*Elf_r_sym)(Elf_Rel const *rp) = fn_ELF_R_SYM; 133 static uint_t (*Elf_r_sym)(Elf_Rel const *rp) = fn_ELF_R_SYM;
134 134
135 static void fn_ELF_R_INFO(Elf_Rel *const rp, unsigned sym, unsigned type) 135 static void fn_ELF_R_INFO(Elf_Rel *const rp, unsigned sym, unsigned type)
136 { 136 {
137 rp->r_info = _w(ELF_R_INFO(sym, type)); 137 rp->r_info = _w(ELF_R_INFO(sym, type));
138 } 138 }
139 static void (*Elf_r_info)(Elf_Rel *const rp, unsigned sym, unsigned type) = fn_ELF_R_INFO; 139 static void (*Elf_r_info)(Elf_Rel *const rp, unsigned sym, unsigned type) = fn_ELF_R_INFO;
140 140
141 static int mcount_adjust = 0; 141 static int mcount_adjust = 0;
142 142
143 /* 143 /*
144 * MIPS mcount long call has 2 _mcount symbols, only the position of the 1st 144 * MIPS mcount long call has 2 _mcount symbols, only the position of the 1st
145 * _mcount symbol is needed for dynamic function tracer, with it, to disable 145 * _mcount symbol is needed for dynamic function tracer, with it, to disable
146 * tracing(ftrace_make_nop), the instruction in the position is replaced with 146 * tracing(ftrace_make_nop), the instruction in the position is replaced with
147 * the "b label" instruction, to enable tracing(ftrace_make_call), replace the 147 * the "b label" instruction, to enable tracing(ftrace_make_call), replace the
148 * instruction back. So, here, we set the 2nd one as fake and filter it. 148 * instruction back. So, here, we set the 2nd one as fake and filter it.
149 * 149 *
150 * c: 3c030000 lui v1,0x0 <--> b label 150 * c: 3c030000 lui v1,0x0 <--> b label
151 * c: R_MIPS_HI16 _mcount 151 * c: R_MIPS_HI16 _mcount
152 * c: R_MIPS_NONE *ABS* 152 * c: R_MIPS_NONE *ABS*
153 * c: R_MIPS_NONE *ABS* 153 * c: R_MIPS_NONE *ABS*
154 * 10: 64630000 daddiu v1,v1,0 154 * 10: 64630000 daddiu v1,v1,0
155 * 10: R_MIPS_LO16 _mcount 155 * 10: R_MIPS_LO16 _mcount
156 * 10: R_MIPS_NONE *ABS* 156 * 10: R_MIPS_NONE *ABS*
157 * 10: R_MIPS_NONE *ABS* 157 * 10: R_MIPS_NONE *ABS*
158 * 14: 03e0082d move at,ra 158 * 14: 03e0082d move at,ra
159 * 18: 0060f809 jalr v1 159 * 18: 0060f809 jalr v1
160 * label: 160 * label:
161 */ 161 */
162 #define MIPS_FAKEMCOUNT_OFFSET 4 162 #define MIPS_FAKEMCOUNT_OFFSET 4
163 163
164 static int MIPS_is_fake_mcount(Elf_Rel const *rp) 164 static int MIPS_is_fake_mcount(Elf_Rel const *rp)
165 { 165 {
166 static Elf_Addr old_r_offset; 166 static Elf_Addr old_r_offset;
167 Elf_Addr current_r_offset = _w(rp->r_offset); 167 Elf_Addr current_r_offset = _w(rp->r_offset);
168 int is_fake; 168 int is_fake;
169 169
170 is_fake = old_r_offset && 170 is_fake = old_r_offset &&
171 (current_r_offset - old_r_offset == MIPS_FAKEMCOUNT_OFFSET); 171 (current_r_offset - old_r_offset == MIPS_FAKEMCOUNT_OFFSET);
172 old_r_offset = current_r_offset; 172 old_r_offset = current_r_offset;
173 173
174 return is_fake; 174 return is_fake;
175 } 175 }
176 176
177 /* Append the new shstrtab, Elf_Shdr[], __mcount_loc and its relocations. */ 177 /* Append the new shstrtab, Elf_Shdr[], __mcount_loc and its relocations. */
178 static void append_func(Elf_Ehdr *const ehdr, 178 static void append_func(Elf_Ehdr *const ehdr,
179 Elf_Shdr *const shstr, 179 Elf_Shdr *const shstr,
180 uint_t const *const mloc0, 180 uint_t const *const mloc0,
181 uint_t const *const mlocp, 181 uint_t const *const mlocp,
182 Elf_Rel const *const mrel0, 182 Elf_Rel const *const mrel0,
183 Elf_Rel const *const mrelp, 183 Elf_Rel const *const mrelp,
184 unsigned int const rel_entsize, 184 unsigned int const rel_entsize,
185 unsigned int const symsec_sh_link) 185 unsigned int const symsec_sh_link)
186 { 186 {
187 /* Begin constructing output file */ 187 /* Begin constructing output file */
188 Elf_Shdr mcsec; 188 Elf_Shdr mcsec;
189 char const *mc_name = (sizeof(Elf_Rela) == rel_entsize) 189 char const *mc_name = (sizeof(Elf_Rela) == rel_entsize)
190 ? ".rela__mcount_loc" 190 ? ".rela__mcount_loc"
191 : ".rel__mcount_loc"; 191 : ".rel__mcount_loc";
192 unsigned const old_shnum = w2(ehdr->e_shnum); 192 unsigned const old_shnum = w2(ehdr->e_shnum);
193 uint_t const old_shoff = _w(ehdr->e_shoff); 193 uint_t const old_shoff = _w(ehdr->e_shoff);
194 uint_t const old_shstr_sh_size = _w(shstr->sh_size); 194 uint_t const old_shstr_sh_size = _w(shstr->sh_size);
195 uint_t const old_shstr_sh_offset = _w(shstr->sh_offset); 195 uint_t const old_shstr_sh_offset = _w(shstr->sh_offset);
196 uint_t t = 1 + strlen(mc_name) + _w(shstr->sh_size); 196 uint_t t = 1 + strlen(mc_name) + _w(shstr->sh_size);
197 uint_t new_e_shoff; 197 uint_t new_e_shoff;
198 198
199 shstr->sh_size = _w(t); 199 shstr->sh_size = _w(t);
200 shstr->sh_offset = _w(sb.st_size); 200 shstr->sh_offset = _w(sb.st_size);
201 t += sb.st_size; 201 t += sb.st_size;
202 t += (_align & -t); /* word-byte align */ 202 t += (_align & -t); /* word-byte align */
203 new_e_shoff = t; 203 new_e_shoff = t;
204 204
205 /* body for new shstrtab */ 205 /* body for new shstrtab */
206 ulseek(fd_map, sb.st_size, SEEK_SET); 206 ulseek(fd_map, sb.st_size, SEEK_SET);
207 uwrite(fd_map, old_shstr_sh_offset + (void *)ehdr, old_shstr_sh_size); 207 uwrite(fd_map, old_shstr_sh_offset + (void *)ehdr, old_shstr_sh_size);
208 uwrite(fd_map, mc_name, 1 + strlen(mc_name)); 208 uwrite(fd_map, mc_name, 1 + strlen(mc_name));
209 209
210 /* old(modified) Elf_Shdr table, word-byte aligned */ 210 /* old(modified) Elf_Shdr table, word-byte aligned */
211 ulseek(fd_map, t, SEEK_SET); 211 ulseek(fd_map, t, SEEK_SET);
212 t += sizeof(Elf_Shdr) * old_shnum; 212 t += sizeof(Elf_Shdr) * old_shnum;
213 uwrite(fd_map, old_shoff + (void *)ehdr, 213 uwrite(fd_map, old_shoff + (void *)ehdr,
214 sizeof(Elf_Shdr) * old_shnum); 214 sizeof(Elf_Shdr) * old_shnum);
215 215
216 /* new sections __mcount_loc and .rel__mcount_loc */ 216 /* new sections __mcount_loc and .rel__mcount_loc */
217 t += 2*sizeof(mcsec); 217 t += 2*sizeof(mcsec);
218 mcsec.sh_name = w((sizeof(Elf_Rela) == rel_entsize) + strlen(".rel") 218 mcsec.sh_name = w((sizeof(Elf_Rela) == rel_entsize) + strlen(".rel")
219 + old_shstr_sh_size); 219 + old_shstr_sh_size);
220 mcsec.sh_type = w(SHT_PROGBITS); 220 mcsec.sh_type = w(SHT_PROGBITS);
221 mcsec.sh_flags = _w(SHF_ALLOC); 221 mcsec.sh_flags = _w(SHF_ALLOC);
222 mcsec.sh_addr = 0; 222 mcsec.sh_addr = 0;
223 mcsec.sh_offset = _w(t); 223 mcsec.sh_offset = _w(t);
224 mcsec.sh_size = _w((void *)mlocp - (void *)mloc0); 224 mcsec.sh_size = _w((void *)mlocp - (void *)mloc0);
225 mcsec.sh_link = 0; 225 mcsec.sh_link = 0;
226 mcsec.sh_info = 0; 226 mcsec.sh_info = 0;
227 mcsec.sh_addralign = _w(_size); 227 mcsec.sh_addralign = _w(_size);
228 mcsec.sh_entsize = _w(_size); 228 mcsec.sh_entsize = _w(_size);
229 uwrite(fd_map, &mcsec, sizeof(mcsec)); 229 uwrite(fd_map, &mcsec, sizeof(mcsec));
230 230
231 mcsec.sh_name = w(old_shstr_sh_size); 231 mcsec.sh_name = w(old_shstr_sh_size);
232 mcsec.sh_type = (sizeof(Elf_Rela) == rel_entsize) 232 mcsec.sh_type = (sizeof(Elf_Rela) == rel_entsize)
233 ? w(SHT_RELA) 233 ? w(SHT_RELA)
234 : w(SHT_REL); 234 : w(SHT_REL);
235 mcsec.sh_flags = 0; 235 mcsec.sh_flags = 0;
236 mcsec.sh_addr = 0; 236 mcsec.sh_addr = 0;
237 mcsec.sh_offset = _w((void *)mlocp - (void *)mloc0 + t); 237 mcsec.sh_offset = _w((void *)mlocp - (void *)mloc0 + t);
238 mcsec.sh_size = _w((void *)mrelp - (void *)mrel0); 238 mcsec.sh_size = _w((void *)mrelp - (void *)mrel0);
239 mcsec.sh_link = w(symsec_sh_link); 239 mcsec.sh_link = w(symsec_sh_link);
240 mcsec.sh_info = w(old_shnum); 240 mcsec.sh_info = w(old_shnum);
241 mcsec.sh_addralign = _w(_size); 241 mcsec.sh_addralign = _w(_size);
242 mcsec.sh_entsize = _w(rel_entsize); 242 mcsec.sh_entsize = _w(rel_entsize);
243 uwrite(fd_map, &mcsec, sizeof(mcsec)); 243 uwrite(fd_map, &mcsec, sizeof(mcsec));
244 244
245 uwrite(fd_map, mloc0, (void *)mlocp - (void *)mloc0); 245 uwrite(fd_map, mloc0, (void *)mlocp - (void *)mloc0);
246 uwrite(fd_map, mrel0, (void *)mrelp - (void *)mrel0); 246 uwrite(fd_map, mrel0, (void *)mrelp - (void *)mrel0);
247 247
248 ehdr->e_shoff = _w(new_e_shoff); 248 ehdr->e_shoff = _w(new_e_shoff);
249 ehdr->e_shnum = w2(2 + w2(ehdr->e_shnum)); /* {.rel,}__mcount_loc */ 249 ehdr->e_shnum = w2(2 + w2(ehdr->e_shnum)); /* {.rel,}__mcount_loc */
250 ulseek(fd_map, 0, SEEK_SET); 250 ulseek(fd_map, 0, SEEK_SET);
251 uwrite(fd_map, ehdr, sizeof(*ehdr)); 251 uwrite(fd_map, ehdr, sizeof(*ehdr));
252 } 252 }
253 253
254 static unsigned get_mcountsym(Elf_Sym const *const sym0, 254 static unsigned get_mcountsym(Elf_Sym const *const sym0,
255 Elf_Rel const *relp, 255 Elf_Rel const *relp,
256 char const *const str0) 256 char const *const str0)
257 { 257 {
258 unsigned mcountsym = 0; 258 unsigned mcountsym = 0;
259 259
260 Elf_Sym const *const symp = 260 Elf_Sym const *const symp =
261 &sym0[Elf_r_sym(relp)]; 261 &sym0[Elf_r_sym(relp)];
262 char const *symname = &str0[w(symp->st_name)]; 262 char const *symname = &str0[w(symp->st_name)];
263 char const *mcount = gpfx == '_' ? "_mcount" : "mcount"; 263 char const *mcount = gpfx == '_' ? "_mcount" : "mcount";
264 char const *fentry = "__fentry__";
264 265
265 if (symname[0] == '.') 266 if (symname[0] == '.')
266 ++symname; /* ppc64 hack */ 267 ++symname; /* ppc64 hack */
267 if (strcmp(mcount, symname) == 0 || 268 if (strcmp(mcount, symname) == 0 ||
268 (altmcount && strcmp(altmcount, symname) == 0)) 269 (altmcount && strcmp(altmcount, symname) == 0) ||
270 (strcmp(fentry, symname) == 0))
269 mcountsym = Elf_r_sym(relp); 271 mcountsym = Elf_r_sym(relp);
270 272
271 return mcountsym; 273 return mcountsym;
272 } 274 }
273 275
274 static void get_sym_str_and_relp(Elf_Shdr const *const relhdr, 276 static void get_sym_str_and_relp(Elf_Shdr const *const relhdr,
275 Elf_Ehdr const *const ehdr, 277 Elf_Ehdr const *const ehdr,
276 Elf_Sym const **sym0, 278 Elf_Sym const **sym0,
277 char const **str0, 279 char const **str0,
278 Elf_Rel const **relp) 280 Elf_Rel const **relp)
279 { 281 {
280 Elf_Shdr *const shdr0 = (Elf_Shdr *)(_w(ehdr->e_shoff) 282 Elf_Shdr *const shdr0 = (Elf_Shdr *)(_w(ehdr->e_shoff)
281 + (void *)ehdr); 283 + (void *)ehdr);
282 unsigned const symsec_sh_link = w(relhdr->sh_link); 284 unsigned const symsec_sh_link = w(relhdr->sh_link);
283 Elf_Shdr const *const symsec = &shdr0[symsec_sh_link]; 285 Elf_Shdr const *const symsec = &shdr0[symsec_sh_link];
284 Elf_Shdr const *const strsec = &shdr0[w(symsec->sh_link)]; 286 Elf_Shdr const *const strsec = &shdr0[w(symsec->sh_link)];
285 Elf_Rel const *const rel0 = (Elf_Rel const *)(_w(relhdr->sh_offset) 287 Elf_Rel const *const rel0 = (Elf_Rel const *)(_w(relhdr->sh_offset)
286 + (void *)ehdr); 288 + (void *)ehdr);
287 289
288 *sym0 = (Elf_Sym const *)(_w(symsec->sh_offset) 290 *sym0 = (Elf_Sym const *)(_w(symsec->sh_offset)
289 + (void *)ehdr); 291 + (void *)ehdr);
290 292
291 *str0 = (char const *)(_w(strsec->sh_offset) 293 *str0 = (char const *)(_w(strsec->sh_offset)
292 + (void *)ehdr); 294 + (void *)ehdr);
293 295
294 *relp = rel0; 296 *relp = rel0;
295 } 297 }
296 298
297 /* 299 /*
298 * Look at the relocations in order to find the calls to mcount. 300 * Look at the relocations in order to find the calls to mcount.
299 * Accumulate the section offsets that are found, and their relocation info, 301 * Accumulate the section offsets that are found, and their relocation info,
300 * onto the end of the existing arrays. 302 * onto the end of the existing arrays.
301 */ 303 */
302 static uint_t *sift_rel_mcount(uint_t *mlocp, 304 static uint_t *sift_rel_mcount(uint_t *mlocp,
303 unsigned const offbase, 305 unsigned const offbase,
304 Elf_Rel **const mrelpp, 306 Elf_Rel **const mrelpp,
305 Elf_Shdr const *const relhdr, 307 Elf_Shdr const *const relhdr,
306 Elf_Ehdr const *const ehdr, 308 Elf_Ehdr const *const ehdr,
307 unsigned const recsym, 309 unsigned const recsym,
308 uint_t const recval, 310 uint_t const recval,
309 unsigned const reltype) 311 unsigned const reltype)
310 { 312 {
311 uint_t *const mloc0 = mlocp; 313 uint_t *const mloc0 = mlocp;
312 Elf_Rel *mrelp = *mrelpp; 314 Elf_Rel *mrelp = *mrelpp;
313 Elf_Sym const *sym0; 315 Elf_Sym const *sym0;
314 char const *str0; 316 char const *str0;
315 Elf_Rel const *relp; 317 Elf_Rel const *relp;
316 unsigned rel_entsize = _w(relhdr->sh_entsize); 318 unsigned rel_entsize = _w(relhdr->sh_entsize);
317 unsigned const nrel = _w(relhdr->sh_size) / rel_entsize; 319 unsigned const nrel = _w(relhdr->sh_size) / rel_entsize;
318 unsigned mcountsym = 0; 320 unsigned mcountsym = 0;
319 unsigned t; 321 unsigned t;
320 322
321 get_sym_str_and_relp(relhdr, ehdr, &sym0, &str0, &relp); 323 get_sym_str_and_relp(relhdr, ehdr, &sym0, &str0, &relp);
322 324
323 for (t = nrel; t; --t) { 325 for (t = nrel; t; --t) {
324 if (!mcountsym) 326 if (!mcountsym)
325 mcountsym = get_mcountsym(sym0, relp, str0); 327 mcountsym = get_mcountsym(sym0, relp, str0);
326 328
327 if (mcountsym == Elf_r_sym(relp) && !is_fake_mcount(relp)) { 329 if (mcountsym == Elf_r_sym(relp) && !is_fake_mcount(relp)) {
328 uint_t const addend = 330 uint_t const addend =
329 _w(_w(relp->r_offset) - recval + mcount_adjust); 331 _w(_w(relp->r_offset) - recval + mcount_adjust);
330 mrelp->r_offset = _w(offbase 332 mrelp->r_offset = _w(offbase
331 + ((void *)mlocp - (void *)mloc0)); 333 + ((void *)mlocp - (void *)mloc0));
332 Elf_r_info(mrelp, recsym, reltype); 334 Elf_r_info(mrelp, recsym, reltype);
333 if (rel_entsize == sizeof(Elf_Rela)) { 335 if (rel_entsize == sizeof(Elf_Rela)) {
334 ((Elf_Rela *)mrelp)->r_addend = addend; 336 ((Elf_Rela *)mrelp)->r_addend = addend;
335 *mlocp++ = 0; 337 *mlocp++ = 0;
336 } else 338 } else
337 *mlocp++ = addend; 339 *mlocp++ = addend;
338 340
339 mrelp = (Elf_Rel *)(rel_entsize + (void *)mrelp); 341 mrelp = (Elf_Rel *)(rel_entsize + (void *)mrelp);
340 } 342 }
341 relp = (Elf_Rel const *)(rel_entsize + (void *)relp); 343 relp = (Elf_Rel const *)(rel_entsize + (void *)relp);
342 } 344 }
343 *mrelpp = mrelp; 345 *mrelpp = mrelp;
344 return mlocp; 346 return mlocp;
345 } 347 }
346 348
347 /* 349 /*
348 * Read the relocation table again, but this time its called on sections 350 * Read the relocation table again, but this time its called on sections
349 * that are not going to be traced. The mcount calls here will be converted 351 * that are not going to be traced. The mcount calls here will be converted
350 * into nops. 352 * into nops.
351 */ 353 */
352 static void nop_mcount(Elf_Shdr const *const relhdr, 354 static void nop_mcount(Elf_Shdr const *const relhdr,
353 Elf_Ehdr const *const ehdr, 355 Elf_Ehdr const *const ehdr,
354 const char *const txtname) 356 const char *const txtname)
355 { 357 {
356 Elf_Shdr *const shdr0 = (Elf_Shdr *)(_w(ehdr->e_shoff) 358 Elf_Shdr *const shdr0 = (Elf_Shdr *)(_w(ehdr->e_shoff)
357 + (void *)ehdr); 359 + (void *)ehdr);
358 Elf_Sym const *sym0; 360 Elf_Sym const *sym0;
359 char const *str0; 361 char const *str0;
360 Elf_Rel const *relp; 362 Elf_Rel const *relp;
361 Elf_Shdr const *const shdr = &shdr0[w(relhdr->sh_info)]; 363 Elf_Shdr const *const shdr = &shdr0[w(relhdr->sh_info)];
362 unsigned rel_entsize = _w(relhdr->sh_entsize); 364 unsigned rel_entsize = _w(relhdr->sh_entsize);
363 unsigned const nrel = _w(relhdr->sh_size) / rel_entsize; 365 unsigned const nrel = _w(relhdr->sh_size) / rel_entsize;
364 unsigned mcountsym = 0; 366 unsigned mcountsym = 0;
365 unsigned t; 367 unsigned t;
366 int once = 0; 368 int once = 0;
367 369
368 get_sym_str_and_relp(relhdr, ehdr, &sym0, &str0, &relp); 370 get_sym_str_and_relp(relhdr, ehdr, &sym0, &str0, &relp);
369 371
370 for (t = nrel; t; --t) { 372 for (t = nrel; t; --t) {
371 int ret = -1; 373 int ret = -1;
372 374
373 if (!mcountsym) 375 if (!mcountsym)
374 mcountsym = get_mcountsym(sym0, relp, str0); 376 mcountsym = get_mcountsym(sym0, relp, str0);
375 377
376 if (mcountsym == Elf_r_sym(relp) && !is_fake_mcount(relp)) { 378 if (mcountsym == Elf_r_sym(relp) && !is_fake_mcount(relp)) {
377 if (make_nop) 379 if (make_nop)
378 ret = make_nop((void *)ehdr, shdr->sh_offset + relp->r_offset); 380 ret = make_nop((void *)ehdr, shdr->sh_offset + relp->r_offset);
379 if (warn_on_notrace_sect && !once) { 381 if (warn_on_notrace_sect && !once) {
380 printf("Section %s has mcount callers being ignored\n", 382 printf("Section %s has mcount callers being ignored\n",
381 txtname); 383 txtname);
382 once = 1; 384 once = 1;
383 /* just warn? */ 385 /* just warn? */
384 if (!make_nop) 386 if (!make_nop)
385 return; 387 return;
386 } 388 }
387 } 389 }
388 390
389 /* 391 /*
390 * If we successfully removed the mcount, mark the relocation 392 * If we successfully removed the mcount, mark the relocation
391 * as a nop (don't do anything with it). 393 * as a nop (don't do anything with it).
392 */ 394 */
393 if (!ret) { 395 if (!ret) {
394 Elf_Rel rel; 396 Elf_Rel rel;
395 rel = *(Elf_Rel *)relp; 397 rel = *(Elf_Rel *)relp;
396 Elf_r_info(&rel, Elf_r_sym(relp), rel_type_nop); 398 Elf_r_info(&rel, Elf_r_sym(relp), rel_type_nop);
397 ulseek(fd_map, (void *)relp - (void *)ehdr, SEEK_SET); 399 ulseek(fd_map, (void *)relp - (void *)ehdr, SEEK_SET);
398 uwrite(fd_map, &rel, sizeof(rel)); 400 uwrite(fd_map, &rel, sizeof(rel));
399 } 401 }
400 relp = (Elf_Rel const *)(rel_entsize + (void *)relp); 402 relp = (Elf_Rel const *)(rel_entsize + (void *)relp);
401 } 403 }
402 } 404 }
403 405
404 406
405 /* 407 /*
406 * Find a symbol in the given section, to be used as the base for relocating 408 * Find a symbol in the given section, to be used as the base for relocating
407 * the table of offsets of calls to mcount. A local or global symbol suffices, 409 * the table of offsets of calls to mcount. A local or global symbol suffices,
408 * but avoid a Weak symbol because it may be overridden; the change in value 410 * but avoid a Weak symbol because it may be overridden; the change in value
409 * would invalidate the relocations of the offsets of the calls to mcount. 411 * would invalidate the relocations of the offsets of the calls to mcount.
410 * Often the found symbol will be the unnamed local symbol generated by 412 * Often the found symbol will be the unnamed local symbol generated by
411 * GNU 'as' for the start of each section. For example: 413 * GNU 'as' for the start of each section. For example:
412 * Num: Value Size Type Bind Vis Ndx Name 414 * Num: Value Size Type Bind Vis Ndx Name
413 * 2: 00000000 0 SECTION LOCAL DEFAULT 1 415 * 2: 00000000 0 SECTION LOCAL DEFAULT 1
414 */ 416 */
415 static unsigned find_secsym_ndx(unsigned const txtndx, 417 static unsigned find_secsym_ndx(unsigned const txtndx,
416 char const *const txtname, 418 char const *const txtname,
417 uint_t *const recvalp, 419 uint_t *const recvalp,
418 Elf_Shdr const *const symhdr, 420 Elf_Shdr const *const symhdr,
419 Elf_Ehdr const *const ehdr) 421 Elf_Ehdr const *const ehdr)
420 { 422 {
421 Elf_Sym const *const sym0 = (Elf_Sym const *)(_w(symhdr->sh_offset) 423 Elf_Sym const *const sym0 = (Elf_Sym const *)(_w(symhdr->sh_offset)
422 + (void *)ehdr); 424 + (void *)ehdr);
423 unsigned const nsym = _w(symhdr->sh_size) / _w(symhdr->sh_entsize); 425 unsigned const nsym = _w(symhdr->sh_size) / _w(symhdr->sh_entsize);
424 Elf_Sym const *symp; 426 Elf_Sym const *symp;
425 unsigned t; 427 unsigned t;
426 428
427 for (symp = sym0, t = nsym; t; --t, ++symp) { 429 for (symp = sym0, t = nsym; t; --t, ++symp) {
428 unsigned int const st_bind = ELF_ST_BIND(symp->st_info); 430 unsigned int const st_bind = ELF_ST_BIND(symp->st_info);
429 431
430 if (txtndx == w2(symp->st_shndx) 432 if (txtndx == w2(symp->st_shndx)
431 /* avoid STB_WEAK */ 433 /* avoid STB_WEAK */
432 && (STB_LOCAL == st_bind || STB_GLOBAL == st_bind)) { 434 && (STB_LOCAL == st_bind || STB_GLOBAL == st_bind)) {
433 /* function symbols on ARM have quirks, avoid them */ 435 /* function symbols on ARM have quirks, avoid them */
434 if (w2(ehdr->e_machine) == EM_ARM 436 if (w2(ehdr->e_machine) == EM_ARM
435 && ELF_ST_TYPE(symp->st_info) == STT_FUNC) 437 && ELF_ST_TYPE(symp->st_info) == STT_FUNC)
436 continue; 438 continue;
437 439
438 *recvalp = _w(symp->st_value); 440 *recvalp = _w(symp->st_value);
439 return symp - sym0; 441 return symp - sym0;
440 } 442 }
441 } 443 }
442 fprintf(stderr, "Cannot find symbol for section %d: %s.\n", 444 fprintf(stderr, "Cannot find symbol for section %d: %s.\n",
443 txtndx, txtname); 445 txtndx, txtname);
444 fail_file(); 446 fail_file();
445 } 447 }
446 448
447 449
448 /* Evade ISO C restriction: no declaration after statement in has_rel_mcount. */ 450 /* Evade ISO C restriction: no declaration after statement in has_rel_mcount. */
449 static char const * 451 static char const *
450 __has_rel_mcount(Elf_Shdr const *const relhdr, /* is SHT_REL or SHT_RELA */ 452 __has_rel_mcount(Elf_Shdr const *const relhdr, /* is SHT_REL or SHT_RELA */
451 Elf_Shdr const *const shdr0, 453 Elf_Shdr const *const shdr0,
452 char const *const shstrtab, 454 char const *const shstrtab,
453 char const *const fname) 455 char const *const fname)
454 { 456 {
455 /* .sh_info depends on .sh_type == SHT_REL[,A] */ 457 /* .sh_info depends on .sh_type == SHT_REL[,A] */
456 Elf_Shdr const *const txthdr = &shdr0[w(relhdr->sh_info)]; 458 Elf_Shdr const *const txthdr = &shdr0[w(relhdr->sh_info)];
457 char const *const txtname = &shstrtab[w(txthdr->sh_name)]; 459 char const *const txtname = &shstrtab[w(txthdr->sh_name)];
458 460
459 if (strcmp("__mcount_loc", txtname) == 0) { 461 if (strcmp("__mcount_loc", txtname) == 0) {
460 fprintf(stderr, "warning: __mcount_loc already exists: %s\n", 462 fprintf(stderr, "warning: __mcount_loc already exists: %s\n",
461 fname); 463 fname);
462 succeed_file(); 464 succeed_file();
463 } 465 }
464 if (w(txthdr->sh_type) != SHT_PROGBITS || 466 if (w(txthdr->sh_type) != SHT_PROGBITS ||
465 !(_w(txthdr->sh_flags) & SHF_EXECINSTR)) 467 !(_w(txthdr->sh_flags) & SHF_EXECINSTR))
466 return NULL; 468 return NULL;
467 return txtname; 469 return txtname;
468 } 470 }
469 471
470 static char const *has_rel_mcount(Elf_Shdr const *const relhdr, 472 static char const *has_rel_mcount(Elf_Shdr const *const relhdr,
471 Elf_Shdr const *const shdr0, 473 Elf_Shdr const *const shdr0,
472 char const *const shstrtab, 474 char const *const shstrtab,
473 char const *const fname) 475 char const *const fname)
474 { 476 {
475 if (w(relhdr->sh_type) != SHT_REL && w(relhdr->sh_type) != SHT_RELA) 477 if (w(relhdr->sh_type) != SHT_REL && w(relhdr->sh_type) != SHT_RELA)
476 return NULL; 478 return NULL;
477 return __has_rel_mcount(relhdr, shdr0, shstrtab, fname); 479 return __has_rel_mcount(relhdr, shdr0, shstrtab, fname);
478 } 480 }
479 481
480 482
481 static unsigned tot_relsize(Elf_Shdr const *const shdr0, 483 static unsigned tot_relsize(Elf_Shdr const *const shdr0,
482 unsigned nhdr, 484 unsigned nhdr,
483 const char *const shstrtab, 485 const char *const shstrtab,
484 const char *const fname) 486 const char *const fname)
485 { 487 {
486 unsigned totrelsz = 0; 488 unsigned totrelsz = 0;
487 Elf_Shdr const *shdrp = shdr0; 489 Elf_Shdr const *shdrp = shdr0;
488 char const *txtname; 490 char const *txtname;
489 491
490 for (; nhdr; --nhdr, ++shdrp) { 492 for (; nhdr; --nhdr, ++shdrp) {
491 txtname = has_rel_mcount(shdrp, shdr0, shstrtab, fname); 493 txtname = has_rel_mcount(shdrp, shdr0, shstrtab, fname);
492 if (txtname && is_mcounted_section_name(txtname)) 494 if (txtname && is_mcounted_section_name(txtname))
493 totrelsz += _w(shdrp->sh_size); 495 totrelsz += _w(shdrp->sh_size);
494 } 496 }
495 return totrelsz; 497 return totrelsz;
496 } 498 }
497 499
498 500
499 /* Overall supervision for Elf32 ET_REL file. */ 501 /* Overall supervision for Elf32 ET_REL file. */
500 static void 502 static void
501 do_func(Elf_Ehdr *const ehdr, char const *const fname, unsigned const reltype) 503 do_func(Elf_Ehdr *const ehdr, char const *const fname, unsigned const reltype)
502 { 504 {
503 Elf_Shdr *const shdr0 = (Elf_Shdr *)(_w(ehdr->e_shoff) 505 Elf_Shdr *const shdr0 = (Elf_Shdr *)(_w(ehdr->e_shoff)
504 + (void *)ehdr); 506 + (void *)ehdr);
505 unsigned const nhdr = w2(ehdr->e_shnum); 507 unsigned const nhdr = w2(ehdr->e_shnum);
506 Elf_Shdr *const shstr = &shdr0[w2(ehdr->e_shstrndx)]; 508 Elf_Shdr *const shstr = &shdr0[w2(ehdr->e_shstrndx)];
507 char const *const shstrtab = (char const *)(_w(shstr->sh_offset) 509 char const *const shstrtab = (char const *)(_w(shstr->sh_offset)
508 + (void *)ehdr); 510 + (void *)ehdr);
509 511
510 Elf_Shdr const *relhdr; 512 Elf_Shdr const *relhdr;
511 unsigned k; 513 unsigned k;
512 514
513 /* Upper bound on space: assume all relevant relocs are for mcount. */ 515 /* Upper bound on space: assume all relevant relocs are for mcount. */
514 unsigned const totrelsz = tot_relsize(shdr0, nhdr, shstrtab, fname); 516 unsigned const totrelsz = tot_relsize(shdr0, nhdr, shstrtab, fname);
515 Elf_Rel *const mrel0 = umalloc(totrelsz); 517 Elf_Rel *const mrel0 = umalloc(totrelsz);
516 Elf_Rel * mrelp = mrel0; 518 Elf_Rel * mrelp = mrel0;
517 519
518 /* 2*sizeof(address) <= sizeof(Elf_Rel) */ 520 /* 2*sizeof(address) <= sizeof(Elf_Rel) */
519 uint_t *const mloc0 = umalloc(totrelsz>>1); 521 uint_t *const mloc0 = umalloc(totrelsz>>1);
520 uint_t * mlocp = mloc0; 522 uint_t * mlocp = mloc0;
521 523
522 unsigned rel_entsize = 0; 524 unsigned rel_entsize = 0;
523 unsigned symsec_sh_link = 0; 525 unsigned symsec_sh_link = 0;
524 526
525 for (relhdr = shdr0, k = nhdr; k; --k, ++relhdr) { 527 for (relhdr = shdr0, k = nhdr; k; --k, ++relhdr) {
526 char const *const txtname = has_rel_mcount(relhdr, shdr0, 528 char const *const txtname = has_rel_mcount(relhdr, shdr0,
527 shstrtab, fname); 529 shstrtab, fname);
528 if (txtname && is_mcounted_section_name(txtname)) { 530 if (txtname && is_mcounted_section_name(txtname)) {
529 uint_t recval = 0; 531 uint_t recval = 0;
530 unsigned const recsym = find_secsym_ndx( 532 unsigned const recsym = find_secsym_ndx(
531 w(relhdr->sh_info), txtname, &recval, 533 w(relhdr->sh_info), txtname, &recval,
532 &shdr0[symsec_sh_link = w(relhdr->sh_link)], 534 &shdr0[symsec_sh_link = w(relhdr->sh_link)],
533 ehdr); 535 ehdr);
534 536
535 rel_entsize = _w(relhdr->sh_entsize); 537 rel_entsize = _w(relhdr->sh_entsize);
536 mlocp = sift_rel_mcount(mlocp, 538 mlocp = sift_rel_mcount(mlocp,
537 (void *)mlocp - (void *)mloc0, &mrelp, 539 (void *)mlocp - (void *)mloc0, &mrelp,
538 relhdr, ehdr, recsym, recval, reltype); 540 relhdr, ehdr, recsym, recval, reltype);
539 } else if (txtname && (warn_on_notrace_sect || make_nop)) { 541 } else if (txtname && (warn_on_notrace_sect || make_nop)) {
540 /* 542 /*
541 * This section is ignored by ftrace, but still 543 * This section is ignored by ftrace, but still
542 * has mcount calls. Convert them to nops now. 544 * has mcount calls. Convert them to nops now.
543 */ 545 */
544 nop_mcount(relhdr, ehdr, txtname); 546 nop_mcount(relhdr, ehdr, txtname);
545 } 547 }
546 } 548 }
547 if (mloc0 != mlocp) { 549 if (mloc0 != mlocp) {
548 append_func(ehdr, shstr, mloc0, mlocp, mrel0, mrelp, 550 append_func(ehdr, shstr, mloc0, mlocp, mrel0, mrelp,
549 rel_entsize, symsec_sh_link); 551 rel_entsize, symsec_sh_link);
550 } 552 }
551 free(mrel0); 553 free(mrel0);
552 free(mloc0); 554 free(mloc0);
553 } 555 }
554 556