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
25 #undef sift_rel_mcount
27 #undef find_secsym_ndx
28 #undef __has_rel_mcount
51 #ifdef RECORD_MCOUNT_64
52 # define append_func append64
53 # define sift_rel_mcount sift64_rel_mcount
54 # define nop_mcount nop_mcount_64
55 # define find_secsym_ndx find64_secsym_ndx
56 # define __has_rel_mcount __has64_rel_mcount
57 # define has_rel_mcount has64_rel_mcount
58 # define tot_relsize tot64_relsize
60 # define get_mcountsym get_mcountsym_64
61 # define is_fake_mcount is_fake_mcount64
62 # define fn_is_fake_mcount fn_is_fake_mcount64
63 # define MIPS_is_fake_mcount MIPS64_is_fake_mcount
64 # define Elf_Addr Elf64_Addr
65 # define Elf_Ehdr Elf64_Ehdr
66 # define Elf_Shdr Elf64_Shdr
67 # define Elf_Rel Elf64_Rel
68 # define Elf_Rela Elf64_Rela
69 # define Elf_Sym Elf64_Sym
70 # define ELF_R_SYM ELF64_R_SYM
71 # define Elf_r_sym Elf64_r_sym
72 # define ELF_R_INFO ELF64_R_INFO
73 # define Elf_r_info Elf64_r_info
74 # define ELF_ST_BIND ELF64_ST_BIND
75 # define fn_ELF_R_SYM fn_ELF64_R_SYM
76 # define fn_ELF_R_INFO fn_ELF64_R_INFO
77 # define uint_t uint64_t
82 # define append_func append32
83 # define sift_rel_mcount sift32_rel_mcount
84 # define nop_mcount nop_mcount_32
85 # define find_secsym_ndx find32_secsym_ndx
86 # define __has_rel_mcount __has32_rel_mcount
87 # define has_rel_mcount has32_rel_mcount
88 # define tot_relsize tot32_relsize
90 # define get_mcountsym get_mcountsym_32
91 # define is_fake_mcount is_fake_mcount32
92 # define fn_is_fake_mcount fn_is_fake_mcount32
93 # define MIPS_is_fake_mcount MIPS32_is_fake_mcount
94 # define Elf_Addr Elf32_Addr
95 # define Elf_Ehdr Elf32_Ehdr
96 # define Elf_Shdr Elf32_Shdr
97 # define Elf_Rel Elf32_Rel
98 # define Elf_Rela Elf32_Rela
99 # define Elf_Sym Elf32_Sym
100 # define ELF_R_SYM ELF32_R_SYM
101 # define Elf_r_sym Elf32_r_sym
102 # define ELF_R_INFO ELF32_R_INFO
103 # define Elf_r_info Elf32_r_info
104 # define ELF_ST_BIND ELF32_ST_BIND
105 # define fn_ELF_R_SYM fn_ELF32_R_SYM
106 # define fn_ELF_R_INFO fn_ELF32_R_INFO
107 # define uint_t uint32_t
113 /* Functions and pointers that do_file() may override for specific e_machine. */
114 static int fn_is_fake_mcount(Elf_Rel const *rp)
118 static int (*is_fake_mcount)(Elf_Rel const *rp) = fn_is_fake_mcount;
120 static uint_t fn_ELF_R_SYM(Elf_Rel const *rp)
122 return ELF_R_SYM(_w(rp->r_info));
124 static uint_t (*Elf_r_sym)(Elf_Rel const *rp) = fn_ELF_R_SYM;
126 static void fn_ELF_R_INFO(Elf_Rel *const rp, unsigned sym, unsigned type)
128 rp->r_info = _w(ELF_R_INFO(sym, type));
130 static void (*Elf_r_info)(Elf_Rel *const rp, unsigned sym, unsigned type) = fn_ELF_R_INFO;
133 * MIPS mcount long call has 2 _mcount symbols, only the position of the 1st
134 * _mcount symbol is needed for dynamic function tracer, with it, to disable
135 * tracing(ftrace_make_nop), the instruction in the position is replaced with
136 * the "b label" instruction, to enable tracing(ftrace_make_call), replace the
137 * instruction back. So, here, we set the 2nd one as fake and filter it.
139 * c: 3c030000 lui v1,0x0 <--> b label
140 * c: R_MIPS_HI16 _mcount
141 * c: R_MIPS_NONE *ABS*
142 * c: R_MIPS_NONE *ABS*
143 * 10: 64630000 daddiu v1,v1,0
144 * 10: R_MIPS_LO16 _mcount
145 * 10: R_MIPS_NONE *ABS*
146 * 10: R_MIPS_NONE *ABS*
147 * 14: 03e0082d move at,ra
148 * 18: 0060f809 jalr v1
151 #define MIPS_FAKEMCOUNT_OFFSET 4
153 static int MIPS_is_fake_mcount(Elf_Rel const *rp)
155 static Elf_Addr old_r_offset;
156 Elf_Addr current_r_offset = _w(rp->r_offset);
159 is_fake = old_r_offset &&
160 (current_r_offset - old_r_offset == MIPS_FAKEMCOUNT_OFFSET);
161 old_r_offset = current_r_offset;
166 /* Append the new shstrtab, Elf_Shdr[], __mcount_loc and its relocations. */
167 static void append_func(Elf_Ehdr *const ehdr,
168 Elf_Shdr *const shstr,
169 uint_t const *const mloc0,
170 uint_t const *const mlocp,
171 Elf_Rel const *const mrel0,
172 Elf_Rel const *const mrelp,
173 unsigned int const rel_entsize,
174 unsigned int const symsec_sh_link)
176 /* Begin constructing output file */
178 char const *mc_name = (sizeof(Elf_Rela) == rel_entsize)
179 ? ".rela__mcount_loc"
180 : ".rel__mcount_loc";
181 unsigned const old_shnum = w2(ehdr->e_shnum);
182 uint_t const old_shoff = _w(ehdr->e_shoff);
183 uint_t const old_shstr_sh_size = _w(shstr->sh_size);
184 uint_t const old_shstr_sh_offset = _w(shstr->sh_offset);
185 uint_t t = 1 + strlen(mc_name) + _w(shstr->sh_size);
188 shstr->sh_size = _w(t);
189 shstr->sh_offset = _w(sb.st_size);
191 t += (_align & -t); /* word-byte align */
194 /* body for new shstrtab */
195 ulseek(fd_map, sb.st_size, SEEK_SET);
196 uwrite(fd_map, old_shstr_sh_offset + (void *)ehdr, old_shstr_sh_size);
197 uwrite(fd_map, mc_name, 1 + strlen(mc_name));
199 /* old(modified) Elf_Shdr table, word-byte aligned */
200 ulseek(fd_map, t, SEEK_SET);
201 t += sizeof(Elf_Shdr) * old_shnum;
202 uwrite(fd_map, old_shoff + (void *)ehdr,
203 sizeof(Elf_Shdr) * old_shnum);
205 /* new sections __mcount_loc and .rel__mcount_loc */
206 t += 2*sizeof(mcsec);
207 mcsec.sh_name = w((sizeof(Elf_Rela) == rel_entsize) + strlen(".rel")
208 + old_shstr_sh_size);
209 mcsec.sh_type = w(SHT_PROGBITS);
210 mcsec.sh_flags = _w(SHF_ALLOC);
212 mcsec.sh_offset = _w(t);
213 mcsec.sh_size = _w((void *)mlocp - (void *)mloc0);
216 mcsec.sh_addralign = _w(_size);
217 mcsec.sh_entsize = _w(_size);
218 uwrite(fd_map, &mcsec, sizeof(mcsec));
220 mcsec.sh_name = w(old_shstr_sh_size);
221 mcsec.sh_type = (sizeof(Elf_Rela) == rel_entsize)
226 mcsec.sh_offset = _w((void *)mlocp - (void *)mloc0 + t);
227 mcsec.sh_size = _w((void *)mrelp - (void *)mrel0);
228 mcsec.sh_link = w(symsec_sh_link);
229 mcsec.sh_info = w(old_shnum);
230 mcsec.sh_addralign = _w(_size);
231 mcsec.sh_entsize = _w(rel_entsize);
232 uwrite(fd_map, &mcsec, sizeof(mcsec));
234 uwrite(fd_map, mloc0, (void *)mlocp - (void *)mloc0);
235 uwrite(fd_map, mrel0, (void *)mrelp - (void *)mrel0);
237 ehdr->e_shoff = _w(new_e_shoff);
238 ehdr->e_shnum = w2(2 + w2(ehdr->e_shnum)); /* {.rel,}__mcount_loc */
239 ulseek(fd_map, 0, SEEK_SET);
240 uwrite(fd_map, ehdr, sizeof(*ehdr));
243 static unsigned get_mcountsym(Elf_Sym const *const sym0,
245 char const *const str0)
247 unsigned mcountsym = 0;
249 Elf_Sym const *const symp =
250 &sym0[Elf_r_sym(relp)];
251 char const *symname = &str0[w(symp->st_name)];
252 char const *mcount = gpfx == '_' ? "_mcount" : "mcount";
254 if (symname[0] == '.')
255 ++symname; /* ppc64 hack */
256 if (strcmp(mcount, symname) == 0 ||
257 (altmcount && strcmp(altmcount, symname) == 0))
258 mcountsym = Elf_r_sym(relp);
264 * Look at the relocations in order to find the calls to mcount.
265 * Accumulate the section offsets that are found, and their relocation info,
266 * onto the end of the existing arrays.
268 static uint_t *sift_rel_mcount(uint_t *mlocp,
269 unsigned const offbase,
270 Elf_Rel **const mrelpp,
271 Elf_Shdr const *const relhdr,
272 Elf_Ehdr const *const ehdr,
273 unsigned const recsym,
275 unsigned const reltype)
277 uint_t *const mloc0 = mlocp;
278 Elf_Rel *mrelp = *mrelpp;
279 Elf_Shdr *const shdr0 = (Elf_Shdr *)(_w(ehdr->e_shoff)
281 unsigned const symsec_sh_link = w(relhdr->sh_link);
282 Elf_Shdr const *const symsec = &shdr0[symsec_sh_link];
283 Elf_Sym const *const sym0 = (Elf_Sym const *)(_w(symsec->sh_offset)
286 Elf_Shdr const *const strsec = &shdr0[w(symsec->sh_link)];
287 char const *const str0 = (char const *)(_w(strsec->sh_offset)
290 Elf_Rel const *const rel0 = (Elf_Rel const *)(_w(relhdr->sh_offset)
292 unsigned rel_entsize = _w(relhdr->sh_entsize);
293 unsigned const nrel = _w(relhdr->sh_size) / rel_entsize;
294 Elf_Rel const *relp = rel0;
296 unsigned mcountsym = 0;
299 for (t = nrel; t; --t) {
301 mcountsym = get_mcountsym(sym0, relp, str0);
303 if (mcountsym == Elf_r_sym(relp) && !is_fake_mcount(relp)) {
304 uint_t const addend = _w(_w(relp->r_offset) - recval);
306 mrelp->r_offset = _w(offbase
307 + ((void *)mlocp - (void *)mloc0));
308 Elf_r_info(mrelp, recsym, reltype);
309 if (rel_entsize == sizeof(Elf_Rela)) {
310 ((Elf_Rela *)mrelp)->r_addend = addend;
315 mrelp = (Elf_Rel *)(rel_entsize + (void *)mrelp);
317 relp = (Elf_Rel const *)(rel_entsize + (void *)relp);
324 * Read the relocation table again, but this time its called on sections
325 * that are not going to be traced. The mcount calls here will be converted
328 static void nop_mcount(Elf_Shdr const *const relhdr,
329 Elf_Ehdr const *const ehdr,
330 const char *const txtname)
332 Elf_Shdr *const shdr0 = (Elf_Shdr *)(_w(ehdr->e_shoff)
334 unsigned const symsec_sh_link = w(relhdr->sh_link);
335 Elf_Shdr const *const symsec = &shdr0[symsec_sh_link];
336 Elf_Sym const *const sym0 = (Elf_Sym const *)(_w(symsec->sh_offset)
339 Elf_Shdr const *const strsec = &shdr0[w(symsec->sh_link)];
340 char const *const str0 = (char const *)(_w(strsec->sh_offset)
343 Elf_Rel const *const rel0 = (Elf_Rel const *)(_w(relhdr->sh_offset)
345 unsigned rel_entsize = _w(relhdr->sh_entsize);
346 unsigned const nrel = _w(relhdr->sh_size) / rel_entsize;
347 Elf_Rel const *relp = rel0;
349 Elf_Shdr const *const shdr = &shdr0[w(relhdr->sh_info)];
351 unsigned mcountsym = 0;
355 for (t = nrel; t; --t) {
359 mcountsym = get_mcountsym(sym0, relp, str0);
361 if (mcountsym == Elf_r_sym(relp) && !is_fake_mcount(relp)) {
363 ret = make_nop((void *)ehdr, shdr->sh_offset + relp->r_offset);
364 if (warn_on_notrace_sect && !once) {
365 printf("Section %s has mcount callers being ignored\n",
375 * If we successfully removed the mcount, mark the relocation
376 * as a nop (don't do anything with it).
380 rel = *(Elf_Rel *)relp;
381 Elf_r_info(&rel, Elf_r_sym(relp), rel_type_nop);
382 ulseek(fd_map, (void *)relp - (void *)ehdr, SEEK_SET);
383 uwrite(fd_map, &rel, sizeof(rel));
385 relp = (Elf_Rel const *)(rel_entsize + (void *)relp);
391 * Find a symbol in the given section, to be used as the base for relocating
392 * the table of offsets of calls to mcount. A local or global symbol suffices,
393 * but avoid a Weak symbol because it may be overridden; the change in value
394 * would invalidate the relocations of the offsets of the calls to mcount.
395 * Often the found symbol will be the unnamed local symbol generated by
396 * GNU 'as' for the start of each section. For example:
397 * Num: Value Size Type Bind Vis Ndx Name
398 * 2: 00000000 0 SECTION LOCAL DEFAULT 1
400 static unsigned find_secsym_ndx(unsigned const txtndx,
401 char const *const txtname,
402 uint_t *const recvalp,
403 Elf_Shdr const *const symhdr,
404 Elf_Ehdr const *const ehdr)
406 Elf_Sym const *const sym0 = (Elf_Sym const *)(_w(symhdr->sh_offset)
408 unsigned const nsym = _w(symhdr->sh_size) / _w(symhdr->sh_entsize);
412 for (symp = sym0, t = nsym; t; --t, ++symp) {
413 unsigned int const st_bind = ELF_ST_BIND(symp->st_info);
415 if (txtndx == w2(symp->st_shndx)
417 && (STB_LOCAL == st_bind || STB_GLOBAL == st_bind)) {
418 *recvalp = _w(symp->st_value);
422 fprintf(stderr, "Cannot find symbol for section %d: %s.\n",
428 /* Evade ISO C restriction: no declaration after statement in has_rel_mcount. */
430 __has_rel_mcount(Elf_Shdr const *const relhdr, /* is SHT_REL or SHT_RELA */
431 Elf_Shdr const *const shdr0,
432 char const *const shstrtab,
433 char const *const fname)
435 /* .sh_info depends on .sh_type == SHT_REL[,A] */
436 Elf_Shdr const *const txthdr = &shdr0[w(relhdr->sh_info)];
437 char const *const txtname = &shstrtab[w(txthdr->sh_name)];
439 if (strcmp("__mcount_loc", txtname) == 0) {
440 fprintf(stderr, "warning: __mcount_loc already exists: %s\n",
444 if (w(txthdr->sh_type) != SHT_PROGBITS ||
445 !(w(txthdr->sh_flags) & SHF_EXECINSTR))
450 static char const *has_rel_mcount(Elf_Shdr const *const relhdr,
451 Elf_Shdr const *const shdr0,
452 char const *const shstrtab,
453 char const *const fname)
455 if (w(relhdr->sh_type) != SHT_REL && w(relhdr->sh_type) != SHT_RELA)
457 return __has_rel_mcount(relhdr, shdr0, shstrtab, fname);
461 static unsigned tot_relsize(Elf_Shdr const *const shdr0,
463 const char *const shstrtab,
464 const char *const fname)
466 unsigned totrelsz = 0;
467 Elf_Shdr const *shdrp = shdr0;
470 for (; nhdr; --nhdr, ++shdrp) {
471 txtname = has_rel_mcount(shdrp, shdr0, shstrtab, fname);
472 if (txtname && is_mcounted_section_name(txtname))
473 totrelsz += _w(shdrp->sh_size);
479 /* Overall supervision for Elf32 ET_REL file. */
481 do_func(Elf_Ehdr *const ehdr, char const *const fname, unsigned const reltype)
483 Elf_Shdr *const shdr0 = (Elf_Shdr *)(_w(ehdr->e_shoff)
485 unsigned const nhdr = w2(ehdr->e_shnum);
486 Elf_Shdr *const shstr = &shdr0[w2(ehdr->e_shstrndx)];
487 char const *const shstrtab = (char const *)(_w(shstr->sh_offset)
490 Elf_Shdr const *relhdr;
493 /* Upper bound on space: assume all relevant relocs are for mcount. */
494 unsigned const totrelsz = tot_relsize(shdr0, nhdr, shstrtab, fname);
495 Elf_Rel *const mrel0 = umalloc(totrelsz);
496 Elf_Rel * mrelp = mrel0;
498 /* 2*sizeof(address) <= sizeof(Elf_Rel) */
499 uint_t *const mloc0 = umalloc(totrelsz>>1);
500 uint_t * mlocp = mloc0;
502 unsigned rel_entsize = 0;
503 unsigned symsec_sh_link = 0;
505 for (relhdr = shdr0, k = nhdr; k; --k, ++relhdr) {
506 char const *const txtname = has_rel_mcount(relhdr, shdr0,
508 if (txtname && is_mcounted_section_name(txtname)) {
510 unsigned const recsym = find_secsym_ndx(
511 w(relhdr->sh_info), txtname, &recval,
512 &shdr0[symsec_sh_link = w(relhdr->sh_link)],
515 rel_entsize = _w(relhdr->sh_entsize);
516 mlocp = sift_rel_mcount(mlocp,
517 (void *)mlocp - (void *)mloc0, &mrelp,
518 relhdr, ehdr, recsym, recval, reltype);
519 } else if (txtname && (warn_on_notrace_sect || make_nop)) {
521 * This section is ignored by ftrace, but still
522 * has mcount calls. Convert them to nops now.
524 nop_mcount(relhdr, ehdr, txtname);
527 if (mloc0 != mlocp) {
528 append_func(ehdr, shstr, mloc0, mlocp, mrel0, mrelp,
529 rel_entsize, symsec_sh_link);