4 * This code was taken out of recordmcount.c written by
5 * Copyright 2009 John F. Reiser <jreiser@BitWagon.com>. All rights reserved.
7 * The original code had the same algorithms for both 32bit
8 * and 64bit ELF files, but the code was duplicated to support
9 * the difference in structures that were used. This
10 * file creates a macro of everything that is different between
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
13 * header once with RECORD_MCOUNT_64 undefined, and again with
16 * This conversion to macros was done by:
17 * Copyright 2010 Steven Rostedt <srostedt@redhat.com>, Red Hat Inc.
19 * Licensed under the GNU General Public License, version 2 (GPLv2).
23 #undef fn_is_fake_mcount
24 #undef MIPS_is_fake_mcount
26 #undef sift_rel_mcount
28 #undef find_secsym_ndx
29 #undef __has_rel_mcount
33 #undef get_sym_str_and_relp
54 #ifdef RECORD_MCOUNT_64
55 # define append_func append64
56 # define sift_rel_mcount sift64_rel_mcount
57 # define nop_mcount nop_mcount_64
58 # define find_secsym_ndx find64_secsym_ndx
59 # define __has_rel_mcount __has64_rel_mcount
60 # define has_rel_mcount has64_rel_mcount
61 # define tot_relsize tot64_relsize
62 # define get_sym_str_and_relp get_sym_str_and_relp_64
64 # define get_mcountsym get_mcountsym_64
65 # define is_fake_mcount is_fake_mcount64
66 # define fn_is_fake_mcount fn_is_fake_mcount64
67 # define MIPS_is_fake_mcount MIPS64_is_fake_mcount
68 # define mcount_adjust mcount_adjust_64
69 # define Elf_Addr Elf64_Addr
70 # define Elf_Ehdr Elf64_Ehdr
71 # define Elf_Shdr Elf64_Shdr
72 # define Elf_Rel Elf64_Rel
73 # define Elf_Rela Elf64_Rela
74 # define Elf_Sym Elf64_Sym
75 # define ELF_R_SYM ELF64_R_SYM
76 # define Elf_r_sym Elf64_r_sym
77 # define ELF_R_INFO ELF64_R_INFO
78 # define Elf_r_info Elf64_r_info
79 # define ELF_ST_BIND ELF64_ST_BIND
80 # define ELF_ST_TYPE ELF64_ST_TYPE
81 # define fn_ELF_R_SYM fn_ELF64_R_SYM
82 # define fn_ELF_R_INFO fn_ELF64_R_INFO
83 # define uint_t uint64_t
88 # define append_func append32
89 # define sift_rel_mcount sift32_rel_mcount
90 # define nop_mcount nop_mcount_32
91 # define find_secsym_ndx find32_secsym_ndx
92 # define __has_rel_mcount __has32_rel_mcount
93 # define has_rel_mcount has32_rel_mcount
94 # define tot_relsize tot32_relsize
95 # define get_sym_str_and_relp get_sym_str_and_relp_32
97 # define get_mcountsym get_mcountsym_32
98 # define is_fake_mcount is_fake_mcount32
99 # define fn_is_fake_mcount fn_is_fake_mcount32
100 # define MIPS_is_fake_mcount MIPS32_is_fake_mcount
101 # define mcount_adjust mcount_adjust_32
102 # define Elf_Addr Elf32_Addr
103 # define Elf_Ehdr Elf32_Ehdr
104 # define Elf_Shdr Elf32_Shdr
105 # define Elf_Rel Elf32_Rel
106 # define Elf_Rela Elf32_Rela
107 # define Elf_Sym Elf32_Sym
108 # define ELF_R_SYM ELF32_R_SYM
109 # define Elf_r_sym Elf32_r_sym
110 # define ELF_R_INFO ELF32_R_INFO
111 # define Elf_r_info Elf32_r_info
112 # define ELF_ST_BIND ELF32_ST_BIND
113 # define ELF_ST_TYPE ELF32_ST_TYPE
114 # define fn_ELF_R_SYM fn_ELF32_R_SYM
115 # define fn_ELF_R_INFO fn_ELF32_R_INFO
116 # define uint_t uint32_t
122 /* Functions and pointers that do_file() may override for specific e_machine. */
123 static int fn_is_fake_mcount(Elf_Rel
const *rp
)
127 static int (*is_fake_mcount
)(Elf_Rel
const *rp
) = fn_is_fake_mcount
;
129 static uint_t
fn_ELF_R_SYM(Elf_Rel
const *rp
)
131 return ELF_R_SYM(_w(rp
->r_info
));
133 static uint_t (*Elf_r_sym
)(Elf_Rel
const *rp
) = fn_ELF_R_SYM
;
135 static void fn_ELF_R_INFO(Elf_Rel
*const rp
, unsigned sym
, unsigned type
)
137 rp
->r_info
= _w(ELF_R_INFO(sym
, type
));
139 static void (*Elf_r_info
)(Elf_Rel
*const rp
, unsigned sym
, unsigned type
) = fn_ELF_R_INFO
;
141 static int mcount_adjust
= 0;
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
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
148 * instruction back. So, here, we set the 2nd one as fake and filter it.
150 * c: 3c030000 lui v1,0x0 <--> b label
151 * c: R_MIPS_HI16 _mcount
152 * c: R_MIPS_NONE *ABS*
153 * c: R_MIPS_NONE *ABS*
154 * 10: 64630000 daddiu v1,v1,0
155 * 10: R_MIPS_LO16 _mcount
156 * 10: R_MIPS_NONE *ABS*
157 * 10: R_MIPS_NONE *ABS*
158 * 14: 03e0082d move at,ra
159 * 18: 0060f809 jalr v1
162 #define MIPS_FAKEMCOUNT_OFFSET 4
164 static int MIPS_is_fake_mcount(Elf_Rel
const *rp
)
166 static Elf_Addr old_r_offset
= ~(Elf_Addr
)0;
167 Elf_Addr current_r_offset
= _w(rp
->r_offset
);
170 is_fake
= (old_r_offset
!= ~(Elf_Addr
)0) &&
171 (current_r_offset
- old_r_offset
== MIPS_FAKEMCOUNT_OFFSET
);
172 old_r_offset
= current_r_offset
;
177 /* Append the new shstrtab, Elf_Shdr[], __mcount_loc and its relocations. */
178 static void append_func(Elf_Ehdr
*const ehdr
,
179 Elf_Shdr
*const shstr
,
180 uint_t
const *const mloc0
,
181 uint_t
const *const mlocp
,
182 Elf_Rel
const *const mrel0
,
183 Elf_Rel
const *const mrelp
,
184 unsigned int const rel_entsize
,
185 unsigned int const symsec_sh_link
)
187 /* Begin constructing output file */
189 char const *mc_name
= (sizeof(Elf_Rela
) == rel_entsize
)
190 ? ".rela__mcount_loc"
191 : ".rel__mcount_loc";
192 unsigned const old_shnum
= w2(ehdr
->e_shnum
);
193 uint_t
const old_shoff
= _w(ehdr
->e_shoff
);
194 uint_t
const old_shstr_sh_size
= _w(shstr
->sh_size
);
195 uint_t
const old_shstr_sh_offset
= _w(shstr
->sh_offset
);
196 uint_t t
= 1 + strlen(mc_name
) + _w(shstr
->sh_size
);
199 shstr
->sh_size
= _w(t
);
200 shstr
->sh_offset
= _w(sb
.st_size
);
202 t
+= (_align
& -t
); /* word-byte align */
205 /* body for new shstrtab */
206 ulseek(fd_map
, sb
.st_size
, SEEK_SET
);
207 uwrite(fd_map
, old_shstr_sh_offset
+ (void *)ehdr
, old_shstr_sh_size
);
208 uwrite(fd_map
, mc_name
, 1 + strlen(mc_name
));
210 /* old(modified) Elf_Shdr table, word-byte aligned */
211 ulseek(fd_map
, t
, SEEK_SET
);
212 t
+= sizeof(Elf_Shdr
) * old_shnum
;
213 uwrite(fd_map
, old_shoff
+ (void *)ehdr
,
214 sizeof(Elf_Shdr
) * old_shnum
);
216 /* new sections __mcount_loc and .rel__mcount_loc */
217 t
+= 2*sizeof(mcsec
);
218 mcsec
.sh_name
= w((sizeof(Elf_Rela
) == rel_entsize
) + strlen(".rel")
219 + old_shstr_sh_size
);
220 mcsec
.sh_type
= w(SHT_PROGBITS
);
221 mcsec
.sh_flags
= _w(SHF_ALLOC
);
223 mcsec
.sh_offset
= _w(t
);
224 mcsec
.sh_size
= _w((void *)mlocp
- (void *)mloc0
);
227 mcsec
.sh_addralign
= _w(_size
);
228 mcsec
.sh_entsize
= _w(_size
);
229 uwrite(fd_map
, &mcsec
, sizeof(mcsec
));
231 mcsec
.sh_name
= w(old_shstr_sh_size
);
232 mcsec
.sh_type
= (sizeof(Elf_Rela
) == rel_entsize
)
237 mcsec
.sh_offset
= _w((void *)mlocp
- (void *)mloc0
+ t
);
238 mcsec
.sh_size
= _w((void *)mrelp
- (void *)mrel0
);
239 mcsec
.sh_link
= w(symsec_sh_link
);
240 mcsec
.sh_info
= w(old_shnum
);
241 mcsec
.sh_addralign
= _w(_size
);
242 mcsec
.sh_entsize
= _w(rel_entsize
);
243 uwrite(fd_map
, &mcsec
, sizeof(mcsec
));
245 uwrite(fd_map
, mloc0
, (void *)mlocp
- (void *)mloc0
);
246 uwrite(fd_map
, mrel0
, (void *)mrelp
- (void *)mrel0
);
248 ehdr
->e_shoff
= _w(new_e_shoff
);
249 ehdr
->e_shnum
= w2(2 + w2(ehdr
->e_shnum
)); /* {.rel,}__mcount_loc */
250 ulseek(fd_map
, 0, SEEK_SET
);
251 uwrite(fd_map
, ehdr
, sizeof(*ehdr
));
254 static unsigned get_mcountsym(Elf_Sym
const *const sym0
,
256 char const *const str0
)
258 unsigned mcountsym
= 0;
260 Elf_Sym
const *const symp
=
261 &sym0
[Elf_r_sym(relp
)];
262 char const *symname
= &str0
[w(symp
->st_name
)];
263 char const *mcount
= gpfx
== '_' ? "_mcount" : "mcount";
264 char const *fentry
= "__fentry__";
266 if (symname
[0] == '.')
267 ++symname
; /* ppc64 hack */
268 if (strcmp(mcount
, symname
) == 0 ||
269 (altmcount
&& strcmp(altmcount
, symname
) == 0) ||
270 (strcmp(fentry
, symname
) == 0))
271 mcountsym
= Elf_r_sym(relp
);
276 static void get_sym_str_and_relp(Elf_Shdr
const *const relhdr
,
277 Elf_Ehdr
const *const ehdr
,
278 Elf_Sym
const **sym0
,
280 Elf_Rel
const **relp
)
282 Elf_Shdr
*const shdr0
= (Elf_Shdr
*)(_w(ehdr
->e_shoff
)
284 unsigned const symsec_sh_link
= w(relhdr
->sh_link
);
285 Elf_Shdr
const *const symsec
= &shdr0
[symsec_sh_link
];
286 Elf_Shdr
const *const strsec
= &shdr0
[w(symsec
->sh_link
)];
287 Elf_Rel
const *const rel0
= (Elf_Rel
const *)(_w(relhdr
->sh_offset
)
290 *sym0
= (Elf_Sym
const *)(_w(symsec
->sh_offset
)
293 *str0
= (char const *)(_w(strsec
->sh_offset
)
300 * Look at the relocations in order to find the calls to mcount.
301 * Accumulate the section offsets that are found, and their relocation info,
302 * onto the end of the existing arrays.
304 static uint_t
*sift_rel_mcount(uint_t
*mlocp
,
305 unsigned const offbase
,
306 Elf_Rel
**const mrelpp
,
307 Elf_Shdr
const *const relhdr
,
308 Elf_Ehdr
const *const ehdr
,
309 unsigned const recsym
,
311 unsigned const reltype
)
313 uint_t
*const mloc0
= mlocp
;
314 Elf_Rel
*mrelp
= *mrelpp
;
318 unsigned rel_entsize
= _w(relhdr
->sh_entsize
);
319 unsigned const nrel
= _w(relhdr
->sh_size
) / rel_entsize
;
320 unsigned mcountsym
= 0;
323 get_sym_str_and_relp(relhdr
, ehdr
, &sym0
, &str0
, &relp
);
325 for (t
= nrel
; t
; --t
) {
327 mcountsym
= get_mcountsym(sym0
, relp
, str0
);
329 if (mcountsym
&& mcountsym
== Elf_r_sym(relp
) &&
330 !is_fake_mcount(relp
)) {
331 uint_t
const addend
=
332 _w(_w(relp
->r_offset
) - recval
+ mcount_adjust
);
333 mrelp
->r_offset
= _w(offbase
334 + ((void *)mlocp
- (void *)mloc0
));
335 Elf_r_info(mrelp
, recsym
, reltype
);
336 if (rel_entsize
== sizeof(Elf_Rela
)) {
337 ((Elf_Rela
*)mrelp
)->r_addend
= addend
;
342 mrelp
= (Elf_Rel
*)(rel_entsize
+ (void *)mrelp
);
344 relp
= (Elf_Rel
const *)(rel_entsize
+ (void *)relp
);
351 * Read the relocation table again, but this time its called on sections
352 * that are not going to be traced. The mcount calls here will be converted
355 static void nop_mcount(Elf_Shdr
const *const relhdr
,
356 Elf_Ehdr
const *const ehdr
,
357 const char *const txtname
)
359 Elf_Shdr
*const shdr0
= (Elf_Shdr
*)(_w(ehdr
->e_shoff
)
364 Elf_Shdr
const *const shdr
= &shdr0
[w(relhdr
->sh_info
)];
365 unsigned rel_entsize
= _w(relhdr
->sh_entsize
);
366 unsigned const nrel
= _w(relhdr
->sh_size
) / rel_entsize
;
367 unsigned mcountsym
= 0;
371 get_sym_str_and_relp(relhdr
, ehdr
, &sym0
, &str0
, &relp
);
373 for (t
= nrel
; t
; --t
) {
377 mcountsym
= get_mcountsym(sym0
, relp
, str0
);
379 if (mcountsym
== Elf_r_sym(relp
) && !is_fake_mcount(relp
)) {
381 ret
= make_nop((void *)ehdr
, _w(shdr
->sh_offset
) + _w(relp
->r_offset
));
382 if (warn_on_notrace_sect
&& !once
) {
383 printf("Section %s has mcount callers being ignored\n",
393 * If we successfully removed the mcount, mark the relocation
394 * as a nop (don't do anything with it).
398 rel
= *(Elf_Rel
*)relp
;
399 Elf_r_info(&rel
, Elf_r_sym(relp
), rel_type_nop
);
400 ulseek(fd_map
, (void *)relp
- (void *)ehdr
, SEEK_SET
);
401 uwrite(fd_map
, &rel
, sizeof(rel
));
403 relp
= (Elf_Rel
const *)(rel_entsize
+ (void *)relp
);
409 * Find a symbol in the given section, to be used as the base for relocating
410 * the table of offsets of calls to mcount. A local or global symbol suffices,
411 * but avoid a Weak symbol because it may be overridden; the change in value
412 * would invalidate the relocations of the offsets of the calls to mcount.
413 * Often the found symbol will be the unnamed local symbol generated by
414 * GNU 'as' for the start of each section. For example:
415 * Num: Value Size Type Bind Vis Ndx Name
416 * 2: 00000000 0 SECTION LOCAL DEFAULT 1
418 static unsigned find_secsym_ndx(unsigned const txtndx
,
419 char const *const txtname
,
420 uint_t
*const recvalp
,
421 Elf_Shdr
const *const symhdr
,
422 Elf_Ehdr
const *const ehdr
)
424 Elf_Sym
const *const sym0
= (Elf_Sym
const *)(_w(symhdr
->sh_offset
)
426 unsigned const nsym
= _w(symhdr
->sh_size
) / _w(symhdr
->sh_entsize
);
430 for (symp
= sym0
, t
= nsym
; t
; --t
, ++symp
) {
431 unsigned int const st_bind
= ELF_ST_BIND(symp
->st_info
);
433 if (txtndx
== w2(symp
->st_shndx
)
435 && (STB_LOCAL
== st_bind
|| STB_GLOBAL
== st_bind
)) {
436 /* function symbols on ARM have quirks, avoid them */
437 if (w2(ehdr
->e_machine
) == EM_ARM
438 && ELF_ST_TYPE(symp
->st_info
) == STT_FUNC
)
441 *recvalp
= _w(symp
->st_value
);
445 fprintf(stderr
, "Cannot find symbol for section %d: %s.\n",
451 /* Evade ISO C restriction: no declaration after statement in has_rel_mcount. */
453 __has_rel_mcount(Elf_Shdr
const *const relhdr
, /* is SHT_REL or SHT_RELA */
454 Elf_Shdr
const *const shdr0
,
455 char const *const shstrtab
,
456 char const *const fname
)
458 /* .sh_info depends on .sh_type == SHT_REL[,A] */
459 Elf_Shdr
const *const txthdr
= &shdr0
[w(relhdr
->sh_info
)];
460 char const *const txtname
= &shstrtab
[w(txthdr
->sh_name
)];
462 if (strcmp("__mcount_loc", txtname
) == 0) {
463 fprintf(stderr
, "warning: __mcount_loc already exists: %s\n",
467 if (w(txthdr
->sh_type
) != SHT_PROGBITS
||
468 !(_w(txthdr
->sh_flags
) & SHF_EXECINSTR
))
473 static char const *has_rel_mcount(Elf_Shdr
const *const relhdr
,
474 Elf_Shdr
const *const shdr0
,
475 char const *const shstrtab
,
476 char const *const fname
)
478 if (w(relhdr
->sh_type
) != SHT_REL
&& w(relhdr
->sh_type
) != SHT_RELA
)
480 return __has_rel_mcount(relhdr
, shdr0
, shstrtab
, fname
);
484 static unsigned tot_relsize(Elf_Shdr
const *const shdr0
,
486 const char *const shstrtab
,
487 const char *const fname
)
489 unsigned totrelsz
= 0;
490 Elf_Shdr
const *shdrp
= shdr0
;
493 for (; nhdr
; --nhdr
, ++shdrp
) {
494 txtname
= has_rel_mcount(shdrp
, shdr0
, shstrtab
, fname
);
495 if (txtname
&& is_mcounted_section_name(txtname
))
496 totrelsz
+= _w(shdrp
->sh_size
);
502 /* Overall supervision for Elf32 ET_REL file. */
504 do_func(Elf_Ehdr
*const ehdr
, char const *const fname
, unsigned const reltype
)
506 Elf_Shdr
*const shdr0
= (Elf_Shdr
*)(_w(ehdr
->e_shoff
)
508 unsigned const nhdr
= w2(ehdr
->e_shnum
);
509 Elf_Shdr
*const shstr
= &shdr0
[w2(ehdr
->e_shstrndx
)];
510 char const *const shstrtab
= (char const *)(_w(shstr
->sh_offset
)
513 Elf_Shdr
const *relhdr
;
516 /* Upper bound on space: assume all relevant relocs are for mcount. */
517 unsigned const totrelsz
= tot_relsize(shdr0
, nhdr
, shstrtab
, fname
);
518 Elf_Rel
*const mrel0
= umalloc(totrelsz
);
519 Elf_Rel
* mrelp
= mrel0
;
521 /* 2*sizeof(address) <= sizeof(Elf_Rel) */
522 uint_t
*const mloc0
= umalloc(totrelsz
>>1);
523 uint_t
* mlocp
= mloc0
;
525 unsigned rel_entsize
= 0;
526 unsigned symsec_sh_link
= 0;
528 for (relhdr
= shdr0
, k
= nhdr
; k
; --k
, ++relhdr
) {
529 char const *const txtname
= has_rel_mcount(relhdr
, shdr0
,
531 if (txtname
&& is_mcounted_section_name(txtname
)) {
533 unsigned const recsym
= find_secsym_ndx(
534 w(relhdr
->sh_info
), txtname
, &recval
,
535 &shdr0
[symsec_sh_link
= w(relhdr
->sh_link
)],
538 rel_entsize
= _w(relhdr
->sh_entsize
);
539 mlocp
= sift_rel_mcount(mlocp
,
540 (void *)mlocp
- (void *)mloc0
, &mrelp
,
541 relhdr
, ehdr
, recsym
, recval
, reltype
);
542 } else if (txtname
&& (warn_on_notrace_sect
|| make_nop
)) {
544 * This section is ignored by ftrace, but still
545 * has mcount calls. Convert them to nops now.
547 nop_mcount(relhdr
, ehdr
, txtname
);
550 if (mloc0
!= mlocp
) {
551 append_func(ehdr
, shstr
, mloc0
, mlocp
, mrel0
, mrelp
,
552 rel_entsize
, symsec_sh_link
);