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
50 #ifdef RECORD_MCOUNT_64
51 # define append_func append64
52 # define sift_rel_mcount sift64_rel_mcount
53 # define nop_mcount nop_mcount_64
54 # define find_secsym_ndx find64_secsym_ndx
55 # define __has_rel_mcount __has64_rel_mcount
56 # define has_rel_mcount has64_rel_mcount
57 # define tot_relsize tot64_relsize
59 # define is_fake_mcount is_fake_mcount64
60 # define fn_is_fake_mcount fn_is_fake_mcount64
61 # define MIPS_is_fake_mcount MIPS64_is_fake_mcount
62 # define Elf_Addr Elf64_Addr
63 # define Elf_Ehdr Elf64_Ehdr
64 # define Elf_Shdr Elf64_Shdr
65 # define Elf_Rel Elf64_Rel
66 # define Elf_Rela Elf64_Rela
67 # define Elf_Sym Elf64_Sym
68 # define ELF_R_SYM ELF64_R_SYM
69 # define Elf_r_sym Elf64_r_sym
70 # define ELF_R_INFO ELF64_R_INFO
71 # define Elf_r_info Elf64_r_info
72 # define ELF_ST_BIND ELF64_ST_BIND
73 # define fn_ELF_R_SYM fn_ELF64_R_SYM
74 # define fn_ELF_R_INFO fn_ELF64_R_INFO
75 # define uint_t uint64_t
80 # define append_func append32
81 # define sift_rel_mcount sift32_rel_mcount
82 # define nop_mcount nop_mcount_32
83 # define find_secsym_ndx find32_secsym_ndx
84 # define __has_rel_mcount __has32_rel_mcount
85 # define has_rel_mcount has32_rel_mcount
86 # define tot_relsize tot32_relsize
88 # define is_fake_mcount is_fake_mcount32
89 # define fn_is_fake_mcount fn_is_fake_mcount32
90 # define MIPS_is_fake_mcount MIPS32_is_fake_mcount
91 # define Elf_Addr Elf32_Addr
92 # define Elf_Ehdr Elf32_Ehdr
93 # define Elf_Shdr Elf32_Shdr
94 # define Elf_Rel Elf32_Rel
95 # define Elf_Rela Elf32_Rela
96 # define Elf_Sym Elf32_Sym
97 # define ELF_R_SYM ELF32_R_SYM
98 # define Elf_r_sym Elf32_r_sym
99 # define ELF_R_INFO ELF32_R_INFO
100 # define Elf_r_info Elf32_r_info
101 # define ELF_ST_BIND ELF32_ST_BIND
102 # define fn_ELF_R_SYM fn_ELF32_R_SYM
103 # define fn_ELF_R_INFO fn_ELF32_R_INFO
104 # define uint_t uint32_t
110 /* Functions and pointers that do_file() may override for specific e_machine. */
111 static int fn_is_fake_mcount(Elf_Rel const *rp)
115 static int (*is_fake_mcount)(Elf_Rel const *rp) = fn_is_fake_mcount;
117 static uint_t fn_ELF_R_SYM(Elf_Rel const *rp)
119 return ELF_R_SYM(_w(rp->r_info));
121 static uint_t (*Elf_r_sym)(Elf_Rel const *rp) = fn_ELF_R_SYM;
123 static void fn_ELF_R_INFO(Elf_Rel *const rp, unsigned sym, unsigned type)
125 rp->r_info = _w(ELF_R_INFO(sym, type));
127 static void (*Elf_r_info)(Elf_Rel *const rp, unsigned sym, unsigned type) = fn_ELF_R_INFO;
130 * MIPS mcount long call has 2 _mcount symbols, only the position of the 1st
131 * _mcount symbol is needed for dynamic function tracer, with it, to disable
132 * tracing(ftrace_make_nop), the instruction in the position is replaced with
133 * the "b label" instruction, to enable tracing(ftrace_make_call), replace the
134 * instruction back. So, here, we set the 2nd one as fake and filter it.
136 * c: 3c030000 lui v1,0x0 <--> b label
137 * c: R_MIPS_HI16 _mcount
138 * c: R_MIPS_NONE *ABS*
139 * c: R_MIPS_NONE *ABS*
140 * 10: 64630000 daddiu v1,v1,0
141 * 10: R_MIPS_LO16 _mcount
142 * 10: R_MIPS_NONE *ABS*
143 * 10: R_MIPS_NONE *ABS*
144 * 14: 03e0082d move at,ra
145 * 18: 0060f809 jalr v1
148 #define MIPS_FAKEMCOUNT_OFFSET 4
150 static int MIPS_is_fake_mcount(Elf_Rel const *rp)
152 static Elf_Addr old_r_offset;
153 Elf_Addr current_r_offset = _w(rp->r_offset);
156 is_fake = old_r_offset &&
157 (current_r_offset - old_r_offset == MIPS_FAKEMCOUNT_OFFSET);
158 old_r_offset = current_r_offset;
163 /* Append the new shstrtab, Elf_Shdr[], __mcount_loc and its relocations. */
164 static void append_func(Elf_Ehdr *const ehdr,
165 Elf_Shdr *const shstr,
166 uint_t const *const mloc0,
167 uint_t const *const mlocp,
168 Elf_Rel const *const mrel0,
169 Elf_Rel const *const mrelp,
170 unsigned int const rel_entsize,
171 unsigned int const symsec_sh_link)
173 /* Begin constructing output file */
175 char const *mc_name = (sizeof(Elf_Rela) == rel_entsize)
176 ? ".rela__mcount_loc"
177 : ".rel__mcount_loc";
178 unsigned const old_shnum = w2(ehdr->e_shnum);
179 uint_t const old_shoff = _w(ehdr->e_shoff);
180 uint_t const old_shstr_sh_size = _w(shstr->sh_size);
181 uint_t const old_shstr_sh_offset = _w(shstr->sh_offset);
182 uint_t t = 1 + strlen(mc_name) + _w(shstr->sh_size);
185 shstr->sh_size = _w(t);
186 shstr->sh_offset = _w(sb.st_size);
188 t += (_align & -t); /* word-byte align */
191 /* body for new shstrtab */
192 ulseek(fd_map, sb.st_size, SEEK_SET);
193 uwrite(fd_map, old_shstr_sh_offset + (void *)ehdr, old_shstr_sh_size);
194 uwrite(fd_map, mc_name, 1 + strlen(mc_name));
196 /* old(modified) Elf_Shdr table, word-byte aligned */
197 ulseek(fd_map, t, SEEK_SET);
198 t += sizeof(Elf_Shdr) * old_shnum;
199 uwrite(fd_map, old_shoff + (void *)ehdr,
200 sizeof(Elf_Shdr) * old_shnum);
202 /* new sections __mcount_loc and .rel__mcount_loc */
203 t += 2*sizeof(mcsec);
204 mcsec.sh_name = w((sizeof(Elf_Rela) == rel_entsize) + strlen(".rel")
205 + old_shstr_sh_size);
206 mcsec.sh_type = w(SHT_PROGBITS);
207 mcsec.sh_flags = _w(SHF_ALLOC);
209 mcsec.sh_offset = _w(t);
210 mcsec.sh_size = _w((void *)mlocp - (void *)mloc0);
213 mcsec.sh_addralign = _w(_size);
214 mcsec.sh_entsize = _w(_size);
215 uwrite(fd_map, &mcsec, sizeof(mcsec));
217 mcsec.sh_name = w(old_shstr_sh_size);
218 mcsec.sh_type = (sizeof(Elf_Rela) == rel_entsize)
223 mcsec.sh_offset = _w((void *)mlocp - (void *)mloc0 + t);
224 mcsec.sh_size = _w((void *)mrelp - (void *)mrel0);
225 mcsec.sh_link = w(symsec_sh_link);
226 mcsec.sh_info = w(old_shnum);
227 mcsec.sh_addralign = _w(_size);
228 mcsec.sh_entsize = _w(rel_entsize);
229 uwrite(fd_map, &mcsec, sizeof(mcsec));
231 uwrite(fd_map, mloc0, (void *)mlocp - (void *)mloc0);
232 uwrite(fd_map, mrel0, (void *)mrelp - (void *)mrel0);
234 ehdr->e_shoff = _w(new_e_shoff);
235 ehdr->e_shnum = w2(2 + w2(ehdr->e_shnum)); /* {.rel,}__mcount_loc */
236 ulseek(fd_map, 0, SEEK_SET);
237 uwrite(fd_map, ehdr, sizeof(*ehdr));
241 * Look at the relocations in order to find the calls to mcount.
242 * Accumulate the section offsets that are found, and their relocation info,
243 * onto the end of the existing arrays.
245 static uint_t *sift_rel_mcount(uint_t *mlocp,
246 unsigned const offbase,
247 Elf_Rel **const mrelpp,
248 Elf_Shdr const *const relhdr,
249 Elf_Ehdr const *const ehdr,
250 unsigned const recsym,
252 unsigned const reltype)
254 uint_t *const mloc0 = mlocp;
255 Elf_Rel *mrelp = *mrelpp;
256 Elf_Shdr *const shdr0 = (Elf_Shdr *)(_w(ehdr->e_shoff)
258 unsigned const symsec_sh_link = w(relhdr->sh_link);
259 Elf_Shdr const *const symsec = &shdr0[symsec_sh_link];
260 Elf_Sym const *const sym0 = (Elf_Sym const *)(_w(symsec->sh_offset)
263 Elf_Shdr const *const strsec = &shdr0[w(symsec->sh_link)];
264 char const *const str0 = (char const *)(_w(strsec->sh_offset)
267 Elf_Rel const *const rel0 = (Elf_Rel const *)(_w(relhdr->sh_offset)
269 unsigned rel_entsize = _w(relhdr->sh_entsize);
270 unsigned const nrel = _w(relhdr->sh_size) / rel_entsize;
271 Elf_Rel const *relp = rel0;
273 unsigned mcountsym = 0;
276 for (t = nrel; t; --t) {
278 Elf_Sym const *const symp =
279 &sym0[Elf_r_sym(relp)];
280 char const *symname = &str0[w(symp->st_name)];
281 char const *mcount = gpfx == '_' ? "_mcount" : "mcount";
283 if (symname[0] == '.')
284 ++symname; /* ppc64 hack */
285 if (strcmp(mcount, symname) == 0 ||
286 (altmcount && strcmp(altmcount, symname) == 0))
287 mcountsym = Elf_r_sym(relp);
290 if (mcountsym == Elf_r_sym(relp) && !is_fake_mcount(relp)) {
291 uint_t const addend = _w(_w(relp->r_offset) - recval);
293 mrelp->r_offset = _w(offbase
294 + ((void *)mlocp - (void *)mloc0));
295 Elf_r_info(mrelp, recsym, reltype);
296 if (rel_entsize == sizeof(Elf_Rela)) {
297 ((Elf_Rela *)mrelp)->r_addend = addend;
302 mrelp = (Elf_Rel *)(rel_entsize + (void *)mrelp);
304 relp = (Elf_Rel const *)(rel_entsize + (void *)relp);
311 * Read the relocation table again, but this time its called on sections
312 * that are not going to be traced. The mcount calls here will be converted
315 static void nop_mcount(Elf_Shdr const *const relhdr,
316 Elf_Ehdr const *const ehdr)
318 Elf_Shdr *const shdr0 = (Elf_Shdr *)(_w(ehdr->e_shoff)
320 unsigned const symsec_sh_link = w(relhdr->sh_link);
321 Elf_Shdr const *const symsec = &shdr0[symsec_sh_link];
322 Elf_Sym const *const sym0 = (Elf_Sym const *)(_w(symsec->sh_offset)
325 Elf_Shdr const *const strsec = &shdr0[w(symsec->sh_link)];
326 char const *const str0 = (char const *)(_w(strsec->sh_offset)
329 Elf_Rel const *const rel0 = (Elf_Rel const *)(_w(relhdr->sh_offset)
331 unsigned rel_entsize = _w(relhdr->sh_entsize);
332 unsigned const nrel = _w(relhdr->sh_size) / rel_entsize;
333 Elf_Rel const *relp = rel0;
335 Elf_Shdr const *const shdr = &shdr0[w(relhdr->sh_info)];
337 unsigned mcountsym = 0;
340 for (t = nrel; t; --t) {
344 Elf_Sym const *const symp =
345 &sym0[Elf_r_sym(relp)];
346 char const *symname = &str0[w(symp->st_name)];
347 char const *mcount = gpfx == '_' ? "_mcount" : "mcount";
349 if (symname[0] == '.')
350 ++symname; /* ppc64 hack */
351 if (strcmp(mcount, symname) == 0 ||
352 (altmcount && strcmp(altmcount, symname) == 0))
353 mcountsym = Elf_r_sym(relp);
356 if (mcountsym == Elf_r_sym(relp) && !is_fake_mcount(relp))
357 ret = make_nop((void *)ehdr, shdr->sh_offset + relp->r_offset);
360 * If we successfully removed the mcount, mark the relocation
361 * as a nop (don't do anything with it).
365 rel = *(Elf_Rel *)relp;
366 Elf_r_info(&rel, Elf_r_sym(relp), rel_type_nop);
367 ulseek(fd_map, (void *)relp - (void *)ehdr, SEEK_SET);
368 uwrite(fd_map, &rel, sizeof(rel));
370 relp = (Elf_Rel const *)(rel_entsize + (void *)relp);
376 * Find a symbol in the given section, to be used as the base for relocating
377 * the table of offsets of calls to mcount. A local or global symbol suffices,
378 * but avoid a Weak symbol because it may be overridden; the change in value
379 * would invalidate the relocations of the offsets of the calls to mcount.
380 * Often the found symbol will be the unnamed local symbol generated by
381 * GNU 'as' for the start of each section. For example:
382 * Num: Value Size Type Bind Vis Ndx Name
383 * 2: 00000000 0 SECTION LOCAL DEFAULT 1
385 static unsigned find_secsym_ndx(unsigned const txtndx,
386 char const *const txtname,
387 uint_t *const recvalp,
388 Elf_Shdr const *const symhdr,
389 Elf_Ehdr const *const ehdr)
391 Elf_Sym const *const sym0 = (Elf_Sym const *)(_w(symhdr->sh_offset)
393 unsigned const nsym = _w(symhdr->sh_size) / _w(symhdr->sh_entsize);
397 for (symp = sym0, t = nsym; t; --t, ++symp) {
398 unsigned int const st_bind = ELF_ST_BIND(symp->st_info);
400 if (txtndx == w2(symp->st_shndx)
402 && (STB_LOCAL == st_bind || STB_GLOBAL == st_bind)) {
403 *recvalp = _w(symp->st_value);
407 fprintf(stderr, "Cannot find symbol for section %d: %s.\n",
413 /* Evade ISO C restriction: no declaration after statement in has_rel_mcount. */
415 __has_rel_mcount(Elf_Shdr const *const relhdr, /* is SHT_REL or SHT_RELA */
416 Elf_Shdr const *const shdr0,
417 char const *const shstrtab,
418 char const *const fname)
420 /* .sh_info depends on .sh_type == SHT_REL[,A] */
421 Elf_Shdr const *const txthdr = &shdr0[w(relhdr->sh_info)];
422 char const *const txtname = &shstrtab[w(txthdr->sh_name)];
424 if (strcmp("__mcount_loc", txtname) == 0) {
425 fprintf(stderr, "warning: __mcount_loc already exists: %s\n",
429 if (w(txthdr->sh_type) != SHT_PROGBITS ||
430 !(w(txthdr->sh_flags) & SHF_EXECINSTR))
435 static char const *has_rel_mcount(Elf_Shdr const *const relhdr,
436 Elf_Shdr const *const shdr0,
437 char const *const shstrtab,
438 char const *const fname)
440 if (w(relhdr->sh_type) != SHT_REL && w(relhdr->sh_type) != SHT_RELA)
442 return __has_rel_mcount(relhdr, shdr0, shstrtab, fname);
446 static unsigned tot_relsize(Elf_Shdr const *const shdr0,
448 const char *const shstrtab,
449 const char *const fname)
451 unsigned totrelsz = 0;
452 Elf_Shdr const *shdrp = shdr0;
455 for (; nhdr; --nhdr, ++shdrp) {
456 txtname = has_rel_mcount(shdrp, shdr0, shstrtab, fname);
457 if (txtname && is_mcounted_section_name(txtname))
458 totrelsz += _w(shdrp->sh_size);
464 /* Overall supervision for Elf32 ET_REL file. */
466 do_func(Elf_Ehdr *const ehdr, char const *const fname, unsigned const reltype)
468 Elf_Shdr *const shdr0 = (Elf_Shdr *)(_w(ehdr->e_shoff)
470 unsigned const nhdr = w2(ehdr->e_shnum);
471 Elf_Shdr *const shstr = &shdr0[w2(ehdr->e_shstrndx)];
472 char const *const shstrtab = (char const *)(_w(shstr->sh_offset)
475 Elf_Shdr const *relhdr;
478 /* Upper bound on space: assume all relevant relocs are for mcount. */
479 unsigned const totrelsz = tot_relsize(shdr0, nhdr, shstrtab, fname);
480 Elf_Rel *const mrel0 = umalloc(totrelsz);
481 Elf_Rel * mrelp = mrel0;
483 /* 2*sizeof(address) <= sizeof(Elf_Rel) */
484 uint_t *const mloc0 = umalloc(totrelsz>>1);
485 uint_t * mlocp = mloc0;
487 unsigned rel_entsize = 0;
488 unsigned symsec_sh_link = 0;
490 for (relhdr = shdr0, k = nhdr; k; --k, ++relhdr) {
491 char const *const txtname = has_rel_mcount(relhdr, shdr0,
493 if (txtname && is_mcounted_section_name(txtname)) {
495 unsigned const recsym = find_secsym_ndx(
496 w(relhdr->sh_info), txtname, &recval,
497 &shdr0[symsec_sh_link = w(relhdr->sh_link)],
500 rel_entsize = _w(relhdr->sh_entsize);
501 mlocp = sift_rel_mcount(mlocp,
502 (void *)mlocp - (void *)mloc0, &mrelp,
503 relhdr, ehdr, recsym, recval, reltype);
504 } else if (make_nop && txtname) {
506 * This section is ignored by ftrace, but still
507 * has mcount calls. Convert them to nops now.
509 nop_mcount(relhdr, ehdr);
512 if (mloc0 != mlocp) {
513 append_func(ehdr, shstr, mloc0, mlocp, mrel0, mrelp,
514 rel_entsize, symsec_sh_link);