1 /* SPDX-License-Identifier: GPL-2.0-only */
5 * This code was taken out of recordmcount.c written by
6 * Copyright 2009 John F. Reiser <jreiser@BitWagon.com>. All rights reserved.
8 * The original code had the same algorithms for both 32bit
9 * and 64bit ELF files, but the code was duplicated to support
10 * the difference in structures that were used. This
11 * file creates a macro of everything that is different between
12 * the 64 and 32 bit code, such that by including this header
13 * twice we can create both sets of functions by including this
14 * header once with RECORD_MCOUNT_64 undefined, and again with
17 * This conversion to macros was done by:
18 * Copyright 2010 Steven Rostedt <srostedt@redhat.com>, Red Hat Inc.
22 #undef fn_is_fake_mcount
23 #undef MIPS_is_fake_mcount
25 #undef sift_rel_mcount
27 #undef find_secsym_ndx
28 #undef __has_rel_mcount
32 #undef get_sym_str_and_relp
53 #ifdef RECORD_MCOUNT_64
54 # define append_func append64
55 # define sift_rel_mcount sift64_rel_mcount
56 # define nop_mcount nop_mcount_64
57 # define find_secsym_ndx find64_secsym_ndx
58 # define __has_rel_mcount __has64_rel_mcount
59 # define has_rel_mcount has64_rel_mcount
60 # define tot_relsize tot64_relsize
61 # define get_sym_str_and_relp get_sym_str_and_relp_64
63 # define get_mcountsym get_mcountsym_64
64 # define is_fake_mcount is_fake_mcount64
65 # define fn_is_fake_mcount fn_is_fake_mcount64
66 # define MIPS_is_fake_mcount MIPS64_is_fake_mcount
67 # define mcount_adjust mcount_adjust_64
68 # define Elf_Addr Elf64_Addr
69 # define Elf_Ehdr Elf64_Ehdr
70 # define Elf_Shdr Elf64_Shdr
71 # define Elf_Rel Elf64_Rel
72 # define Elf_Rela Elf64_Rela
73 # define Elf_Sym Elf64_Sym
74 # define ELF_R_SYM ELF64_R_SYM
75 # define Elf_r_sym Elf64_r_sym
76 # define ELF_R_INFO ELF64_R_INFO
77 # define Elf_r_info Elf64_r_info
78 # define ELF_ST_BIND ELF64_ST_BIND
79 # define ELF_ST_TYPE ELF64_ST_TYPE
80 # define fn_ELF_R_SYM fn_ELF64_R_SYM
81 # define fn_ELF_R_INFO fn_ELF64_R_INFO
82 # define uint_t uint64_t
87 # define append_func append32
88 # define sift_rel_mcount sift32_rel_mcount
89 # define nop_mcount nop_mcount_32
90 # define find_secsym_ndx find32_secsym_ndx
91 # define __has_rel_mcount __has32_rel_mcount
92 # define has_rel_mcount has32_rel_mcount
93 # define tot_relsize tot32_relsize
94 # define get_sym_str_and_relp get_sym_str_and_relp_32
96 # define get_mcountsym get_mcountsym_32
97 # define is_fake_mcount is_fake_mcount32
98 # define fn_is_fake_mcount fn_is_fake_mcount32
99 # define MIPS_is_fake_mcount MIPS32_is_fake_mcount
100 # define mcount_adjust mcount_adjust_32
101 # define Elf_Addr Elf32_Addr
102 # define Elf_Ehdr Elf32_Ehdr
103 # define Elf_Shdr Elf32_Shdr
104 # define Elf_Rel Elf32_Rel
105 # define Elf_Rela Elf32_Rela
106 # define Elf_Sym Elf32_Sym
107 # define ELF_R_SYM ELF32_R_SYM
108 # define Elf_r_sym Elf32_r_sym
109 # define ELF_R_INFO ELF32_R_INFO
110 # define Elf_r_info Elf32_r_info
111 # define ELF_ST_BIND ELF32_ST_BIND
112 # define ELF_ST_TYPE ELF32_ST_TYPE
113 # define fn_ELF_R_SYM fn_ELF32_R_SYM
114 # define fn_ELF_R_INFO fn_ELF32_R_INFO
115 # define uint_t uint32_t
121 /* Functions and pointers that do_file() may override for specific e_machine. */
122 static int fn_is_fake_mcount(Elf_Rel
const *rp
)
126 static int (*is_fake_mcount
)(Elf_Rel
const *rp
) = fn_is_fake_mcount
;
128 static uint_t
fn_ELF_R_SYM(Elf_Rel
const *rp
)
130 return ELF_R_SYM(_w(rp
->r_info
));
132 static uint_t (*Elf_r_sym
)(Elf_Rel
const *rp
) = fn_ELF_R_SYM
;
134 static void fn_ELF_R_INFO(Elf_Rel
*const rp
, unsigned sym
, unsigned type
)
136 rp
->r_info
= _w(ELF_R_INFO(sym
, type
));
138 static void (*Elf_r_info
)(Elf_Rel
*const rp
, unsigned sym
, unsigned type
) = fn_ELF_R_INFO
;
140 static int mcount_adjust
= 0;
143 * MIPS mcount long call has 2 _mcount symbols, only the position of the 1st
144 * _mcount symbol is needed for dynamic function tracer, with it, to disable
145 * tracing(ftrace_make_nop), the instruction in the position is replaced with
146 * the "b label" instruction, to enable tracing(ftrace_make_call), replace the
147 * instruction back. So, here, we set the 2nd one as fake and filter it.
149 * c: 3c030000 lui v1,0x0 <--> b label
150 * c: R_MIPS_HI16 _mcount
151 * c: R_MIPS_NONE *ABS*
152 * c: R_MIPS_NONE *ABS*
153 * 10: 64630000 daddiu v1,v1,0
154 * 10: R_MIPS_LO16 _mcount
155 * 10: R_MIPS_NONE *ABS*
156 * 10: R_MIPS_NONE *ABS*
157 * 14: 03e0082d move at,ra
158 * 18: 0060f809 jalr v1
161 #define MIPS_FAKEMCOUNT_OFFSET 4
163 static int MIPS_is_fake_mcount(Elf_Rel
const *rp
)
165 static Elf_Addr old_r_offset
= ~(Elf_Addr
)0;
166 Elf_Addr current_r_offset
= _w(rp
->r_offset
);
169 is_fake
= (old_r_offset
!= ~(Elf_Addr
)0) &&
170 (current_r_offset
- old_r_offset
== MIPS_FAKEMCOUNT_OFFSET
);
171 old_r_offset
= current_r_offset
;
176 /* Append the new shstrtab, Elf_Shdr[], __mcount_loc and its relocations. */
177 static void append_func(Elf_Ehdr
*const ehdr
,
178 Elf_Shdr
*const shstr
,
179 uint_t
const *const mloc0
,
180 uint_t
const *const mlocp
,
181 Elf_Rel
const *const mrel0
,
182 Elf_Rel
const *const mrelp
,
183 unsigned int const rel_entsize
,
184 unsigned int const symsec_sh_link
)
186 /* Begin constructing output file */
188 char const *mc_name
= (sizeof(Elf_Rela
) == rel_entsize
)
189 ? ".rela__mcount_loc"
190 : ".rel__mcount_loc";
191 unsigned const old_shnum
= w2(ehdr
->e_shnum
);
192 uint_t
const old_shoff
= _w(ehdr
->e_shoff
);
193 uint_t
const old_shstr_sh_size
= _w(shstr
->sh_size
);
194 uint_t
const old_shstr_sh_offset
= _w(shstr
->sh_offset
);
195 uint_t t
= 1 + strlen(mc_name
) + _w(shstr
->sh_size
);
198 shstr
->sh_size
= _w(t
);
199 shstr
->sh_offset
= _w(sb
.st_size
);
201 t
+= (_align
& -t
); /* word-byte align */
204 /* body for new shstrtab */
205 ulseek(fd_map
, sb
.st_size
, SEEK_SET
);
206 uwrite(fd_map
, old_shstr_sh_offset
+ (void *)ehdr
, old_shstr_sh_size
);
207 uwrite(fd_map
, mc_name
, 1 + strlen(mc_name
));
209 /* old(modified) Elf_Shdr table, word-byte aligned */
210 ulseek(fd_map
, t
, SEEK_SET
);
211 t
+= sizeof(Elf_Shdr
) * old_shnum
;
212 uwrite(fd_map
, old_shoff
+ (void *)ehdr
,
213 sizeof(Elf_Shdr
) * old_shnum
);
215 /* new sections __mcount_loc and .rel__mcount_loc */
216 t
+= 2*sizeof(mcsec
);
217 mcsec
.sh_name
= w((sizeof(Elf_Rela
) == rel_entsize
) + strlen(".rel")
218 + old_shstr_sh_size
);
219 mcsec
.sh_type
= w(SHT_PROGBITS
);
220 mcsec
.sh_flags
= _w(SHF_ALLOC
);
222 mcsec
.sh_offset
= _w(t
);
223 mcsec
.sh_size
= _w((void *)mlocp
- (void *)mloc0
);
226 mcsec
.sh_addralign
= _w(_size
);
227 mcsec
.sh_entsize
= _w(_size
);
228 uwrite(fd_map
, &mcsec
, sizeof(mcsec
));
230 mcsec
.sh_name
= w(old_shstr_sh_size
);
231 mcsec
.sh_type
= (sizeof(Elf_Rela
) == rel_entsize
)
236 mcsec
.sh_offset
= _w((void *)mlocp
- (void *)mloc0
+ t
);
237 mcsec
.sh_size
= _w((void *)mrelp
- (void *)mrel0
);
238 mcsec
.sh_link
= w(symsec_sh_link
);
239 mcsec
.sh_info
= w(old_shnum
);
240 mcsec
.sh_addralign
= _w(_size
);
241 mcsec
.sh_entsize
= _w(rel_entsize
);
242 uwrite(fd_map
, &mcsec
, sizeof(mcsec
));
244 uwrite(fd_map
, mloc0
, (void *)mlocp
- (void *)mloc0
);
245 uwrite(fd_map
, mrel0
, (void *)mrelp
- (void *)mrel0
);
247 ehdr
->e_shoff
= _w(new_e_shoff
);
248 ehdr
->e_shnum
= w2(2 + w2(ehdr
->e_shnum
)); /* {.rel,}__mcount_loc */
249 ulseek(fd_map
, 0, SEEK_SET
);
250 uwrite(fd_map
, ehdr
, sizeof(*ehdr
));
253 static unsigned get_mcountsym(Elf_Sym
const *const sym0
,
255 char const *const str0
)
257 unsigned mcountsym
= 0;
259 Elf_Sym
const *const symp
=
260 &sym0
[Elf_r_sym(relp
)];
261 char const *symname
= &str0
[w(symp
->st_name
)];
262 char const *mcount
= gpfx
== '_' ? "_mcount" : "mcount";
263 char const *fentry
= "__fentry__";
265 if (symname
[0] == '.')
266 ++symname
; /* ppc64 hack */
267 if (strcmp(mcount
, symname
) == 0 ||
268 (altmcount
&& strcmp(altmcount
, symname
) == 0) ||
269 (strcmp(fentry
, symname
) == 0))
270 mcountsym
= Elf_r_sym(relp
);
275 static void get_sym_str_and_relp(Elf_Shdr
const *const relhdr
,
276 Elf_Ehdr
const *const ehdr
,
277 Elf_Sym
const **sym0
,
279 Elf_Rel
const **relp
)
281 Elf_Shdr
*const shdr0
= (Elf_Shdr
*)(_w(ehdr
->e_shoff
)
283 unsigned const symsec_sh_link
= w(relhdr
->sh_link
);
284 Elf_Shdr
const *const symsec
= &shdr0
[symsec_sh_link
];
285 Elf_Shdr
const *const strsec
= &shdr0
[w(symsec
->sh_link
)];
286 Elf_Rel
const *const rel0
= (Elf_Rel
const *)(_w(relhdr
->sh_offset
)
289 *sym0
= (Elf_Sym
const *)(_w(symsec
->sh_offset
)
292 *str0
= (char const *)(_w(strsec
->sh_offset
)
299 * Look at the relocations in order to find the calls to mcount.
300 * Accumulate the section offsets that are found, and their relocation info,
301 * onto the end of the existing arrays.
303 static uint_t
*sift_rel_mcount(uint_t
*mlocp
,
304 unsigned const offbase
,
305 Elf_Rel
**const mrelpp
,
306 Elf_Shdr
const *const relhdr
,
307 Elf_Ehdr
const *const ehdr
,
308 unsigned const recsym
,
310 unsigned const reltype
)
312 uint_t
*const mloc0
= mlocp
;
313 Elf_Rel
*mrelp
= *mrelpp
;
317 unsigned rel_entsize
= _w(relhdr
->sh_entsize
);
318 unsigned const nrel
= _w(relhdr
->sh_size
) / rel_entsize
;
319 unsigned mcountsym
= 0;
322 get_sym_str_and_relp(relhdr
, ehdr
, &sym0
, &str0
, &relp
);
324 for (t
= nrel
; t
; --t
) {
326 mcountsym
= get_mcountsym(sym0
, relp
, str0
);
328 if (mcountsym
== Elf_r_sym(relp
) && !is_fake_mcount(relp
)) {
329 uint_t
const addend
=
330 _w(_w(relp
->r_offset
) - recval
+ mcount_adjust
);
331 mrelp
->r_offset
= _w(offbase
332 + ((void *)mlocp
- (void *)mloc0
));
333 Elf_r_info(mrelp
, recsym
, reltype
);
334 if (rel_entsize
== sizeof(Elf_Rela
)) {
335 ((Elf_Rela
*)mrelp
)->r_addend
= addend
;
340 mrelp
= (Elf_Rel
*)(rel_entsize
+ (void *)mrelp
);
342 relp
= (Elf_Rel
const *)(rel_entsize
+ (void *)relp
);
349 * Read the relocation table again, but this time its called on sections
350 * that are not going to be traced. The mcount calls here will be converted
353 static void nop_mcount(Elf_Shdr
const *const relhdr
,
354 Elf_Ehdr
const *const ehdr
,
355 const char *const txtname
)
357 Elf_Shdr
*const shdr0
= (Elf_Shdr
*)(_w(ehdr
->e_shoff
)
362 Elf_Shdr
const *const shdr
= &shdr0
[w(relhdr
->sh_info
)];
363 unsigned rel_entsize
= _w(relhdr
->sh_entsize
);
364 unsigned const nrel
= _w(relhdr
->sh_size
) / rel_entsize
;
365 unsigned mcountsym
= 0;
369 get_sym_str_and_relp(relhdr
, ehdr
, &sym0
, &str0
, &relp
);
371 for (t
= nrel
; t
; --t
) {
375 mcountsym
= get_mcountsym(sym0
, relp
, str0
);
377 if (mcountsym
== Elf_r_sym(relp
) && !is_fake_mcount(relp
)) {
379 ret
= make_nop((void *)ehdr
, _w(shdr
->sh_offset
) + _w(relp
->r_offset
));
380 if (warn_on_notrace_sect
&& !once
) {
381 printf("Section %s has mcount callers being ignored\n",
391 * If we successfully removed the mcount, mark the relocation
392 * as a nop (don't do anything with it).
396 rel
= *(Elf_Rel
*)relp
;
397 Elf_r_info(&rel
, Elf_r_sym(relp
), rel_type_nop
);
398 ulseek(fd_map
, (void *)relp
- (void *)ehdr
, SEEK_SET
);
399 uwrite(fd_map
, &rel
, sizeof(rel
));
401 relp
= (Elf_Rel
const *)(rel_entsize
+ (void *)relp
);
407 * Find a symbol in the given section, to be used as the base for relocating
408 * the table of offsets of calls to mcount. A local or global symbol suffices,
409 * but avoid a Weak symbol because it may be overridden; the change in value
410 * would invalidate the relocations of the offsets of the calls to mcount.
411 * Often the found symbol will be the unnamed local symbol generated by
412 * GNU 'as' for the start of each section. For example:
413 * Num: Value Size Type Bind Vis Ndx Name
414 * 2: 00000000 0 SECTION LOCAL DEFAULT 1
416 static unsigned find_secsym_ndx(unsigned const txtndx
,
417 char const *const txtname
,
418 uint_t
*const recvalp
,
419 Elf_Shdr
const *const symhdr
,
420 Elf_Ehdr
const *const ehdr
)
422 Elf_Sym
const *const sym0
= (Elf_Sym
const *)(_w(symhdr
->sh_offset
)
424 unsigned const nsym
= _w(symhdr
->sh_size
) / _w(symhdr
->sh_entsize
);
428 for (symp
= sym0
, t
= nsym
; t
; --t
, ++symp
) {
429 unsigned int const st_bind
= ELF_ST_BIND(symp
->st_info
);
431 if (txtndx
== w2(symp
->st_shndx
)
433 && (STB_LOCAL
== st_bind
|| STB_GLOBAL
== st_bind
)) {
434 /* function symbols on ARM have quirks, avoid them */
435 if (w2(ehdr
->e_machine
) == EM_ARM
436 && ELF_ST_TYPE(symp
->st_info
) == STT_FUNC
)
439 *recvalp
= _w(symp
->st_value
);
443 fprintf(stderr
, "Cannot find symbol for section %u: %s.\n",
449 /* Evade ISO C restriction: no declaration after statement in has_rel_mcount. */
451 __has_rel_mcount(Elf_Shdr
const *const relhdr
, /* is SHT_REL or SHT_RELA */
452 Elf_Shdr
const *const shdr0
,
453 char const *const shstrtab
,
454 char const *const fname
)
456 /* .sh_info depends on .sh_type == SHT_REL[,A] */
457 Elf_Shdr
const *const txthdr
= &shdr0
[w(relhdr
->sh_info
)];
458 char const *const txtname
= &shstrtab
[w(txthdr
->sh_name
)];
460 if (strcmp("__mcount_loc", txtname
) == 0) {
461 fprintf(stderr
, "warning: __mcount_loc already exists: %s\n",
465 if (w(txthdr
->sh_type
) != SHT_PROGBITS
||
466 !(_w(txthdr
->sh_flags
) & SHF_EXECINSTR
))
471 static char const *has_rel_mcount(Elf_Shdr
const *const relhdr
,
472 Elf_Shdr
const *const shdr0
,
473 char const *const shstrtab
,
474 char const *const fname
)
476 if (w(relhdr
->sh_type
) != SHT_REL
&& w(relhdr
->sh_type
) != SHT_RELA
)
478 return __has_rel_mcount(relhdr
, shdr0
, shstrtab
, fname
);
482 static unsigned tot_relsize(Elf_Shdr
const *const shdr0
,
484 const char *const shstrtab
,
485 const char *const fname
)
487 unsigned totrelsz
= 0;
488 Elf_Shdr
const *shdrp
= shdr0
;
491 for (; nhdr
; --nhdr
, ++shdrp
) {
492 txtname
= has_rel_mcount(shdrp
, shdr0
, shstrtab
, fname
);
493 if (txtname
&& is_mcounted_section_name(txtname
))
494 totrelsz
+= _w(shdrp
->sh_size
);
500 /* Overall supervision for Elf32 ET_REL file. */
502 do_func(Elf_Ehdr
*const ehdr
, char const *const fname
, unsigned const reltype
)
504 Elf_Shdr
*const shdr0
= (Elf_Shdr
*)(_w(ehdr
->e_shoff
)
506 unsigned const nhdr
= w2(ehdr
->e_shnum
);
507 Elf_Shdr
*const shstr
= &shdr0
[w2(ehdr
->e_shstrndx
)];
508 char const *const shstrtab
= (char const *)(_w(shstr
->sh_offset
)
511 Elf_Shdr
const *relhdr
;
514 /* Upper bound on space: assume all relevant relocs are for mcount. */
515 unsigned const totrelsz
= tot_relsize(shdr0
, nhdr
, shstrtab
, fname
);
516 Elf_Rel
*const mrel0
= umalloc(totrelsz
);
517 Elf_Rel
* mrelp
= mrel0
;
519 /* 2*sizeof(address) <= sizeof(Elf_Rel) */
520 uint_t
*const mloc0
= umalloc(totrelsz
>>1);
521 uint_t
* mlocp
= mloc0
;
523 unsigned rel_entsize
= 0;
524 unsigned symsec_sh_link
= 0;
526 for (relhdr
= shdr0
, k
= nhdr
; k
; --k
, ++relhdr
) {
527 char const *const txtname
= has_rel_mcount(relhdr
, shdr0
,
529 if (txtname
&& is_mcounted_section_name(txtname
)) {
531 unsigned const recsym
= find_secsym_ndx(
532 w(relhdr
->sh_info
), txtname
, &recval
,
533 &shdr0
[symsec_sh_link
= w(relhdr
->sh_link
)],
536 rel_entsize
= _w(relhdr
->sh_entsize
);
537 mlocp
= sift_rel_mcount(mlocp
,
538 (void *)mlocp
- (void *)mloc0
, &mrelp
,
539 relhdr
, ehdr
, recsym
, recval
, reltype
);
540 } else if (txtname
&& (warn_on_notrace_sect
|| make_nop
)) {
542 * This section is ignored by ftrace, but still
543 * has mcount calls. Convert them to nops now.
545 nop_mcount(relhdr
, ehdr
, txtname
);
548 if (mloc0
!= mlocp
) {
549 append_func(ehdr
, shstr
, mloc0
, mlocp
, mrel0
, mrelp
,
550 rel_entsize
, symsec_sh_link
);