blob: baf187bee983c33b6323cfea1b8591ca963fc934 [file] [log] [blame]
Steven Rostedtc28d5072010-10-13 19:06:14 -04001/*
2 * recordmcount.h
3 *
4 * This code was taken out of recordmcount.c written by
5 * Copyright 2009 John F. Reiser <jreiser@BitWagon.com>. All rights reserved.
6 *
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
14 * it defined.
15 *
16 * This conversion to macros was done by:
17 * Copyright 2010 Steven Rostedt <srostedt@redhat.com>, Red Hat Inc.
18 *
19 * Licensed under the GNU General Public License, version 2 (GPLv2).
20 */
21#undef append_func
Wu Zhangjin412910c2010-10-27 18:59:08 +080022#undef is_fake_mcount
23#undef fn_is_fake_mcount
24#undef MIPS_is_fake_mcount
Steven Rostedtc28d5072010-10-13 19:06:14 -040025#undef sift_rel_mcount
26#undef find_secsym_ndx
27#undef __has_rel_mcount
28#undef has_rel_mcount
29#undef tot_relsize
30#undef do_func
Wu Zhangjin412910c2010-10-27 18:59:08 +080031#undef Elf_Addr
Steven Rostedtc28d5072010-10-13 19:06:14 -040032#undef Elf_Ehdr
33#undef Elf_Shdr
34#undef Elf_Rel
35#undef Elf_Rela
36#undef Elf_Sym
37#undef ELF_R_SYM
John Reisera2d493582010-10-27 18:59:07 +080038#undef Elf_r_sym
Steven Rostedtc28d5072010-10-13 19:06:14 -040039#undef ELF_R_INFO
John Reisera2d493582010-10-27 18:59:07 +080040#undef Elf_r_info
Steven Rostedtc28d5072010-10-13 19:06:14 -040041#undef ELF_ST_BIND
John Reisera2d493582010-10-27 18:59:07 +080042#undef fn_ELF_R_SYM
43#undef fn_ELF_R_INFO
Steven Rostedtc28d5072010-10-13 19:06:14 -040044#undef uint_t
45#undef _w
46#undef _align
47#undef _size
48
49#ifdef RECORD_MCOUNT_64
50# define append_func append64
51# define sift_rel_mcount sift64_rel_mcount
52# define find_secsym_ndx find64_secsym_ndx
53# define __has_rel_mcount __has64_rel_mcount
54# define has_rel_mcount has64_rel_mcount
55# define tot_relsize tot64_relsize
56# define do_func do64
Wu Zhangjin412910c2010-10-27 18:59:08 +080057# define is_fake_mcount is_fake_mcount64
58# define fn_is_fake_mcount fn_is_fake_mcount64
59# define MIPS_is_fake_mcount MIPS64_is_fake_mcount
60# define Elf_Addr Elf64_Addr
Steven Rostedtc28d5072010-10-13 19:06:14 -040061# define Elf_Ehdr Elf64_Ehdr
62# define Elf_Shdr Elf64_Shdr
63# define Elf_Rel Elf64_Rel
64# define Elf_Rela Elf64_Rela
65# define Elf_Sym Elf64_Sym
66# define ELF_R_SYM ELF64_R_SYM
John Reisera2d493582010-10-27 18:59:07 +080067# define Elf_r_sym Elf64_r_sym
Steven Rostedtc28d5072010-10-13 19:06:14 -040068# define ELF_R_INFO ELF64_R_INFO
John Reisera2d493582010-10-27 18:59:07 +080069# define Elf_r_info Elf64_r_info
Steven Rostedtc28d5072010-10-13 19:06:14 -040070# define ELF_ST_BIND ELF64_ST_BIND
John Reisera2d493582010-10-27 18:59:07 +080071# define fn_ELF_R_SYM fn_ELF64_R_SYM
72# define fn_ELF_R_INFO fn_ELF64_R_INFO
Steven Rostedtc28d5072010-10-13 19:06:14 -040073# define uint_t uint64_t
74# define _w w8
75# define _align 7u
76# define _size 8
77#else
78# define append_func append32
79# define sift_rel_mcount sift32_rel_mcount
80# define find_secsym_ndx find32_secsym_ndx
81# define __has_rel_mcount __has32_rel_mcount
82# define has_rel_mcount has32_rel_mcount
83# define tot_relsize tot32_relsize
84# define do_func do32
Wu Zhangjin412910c2010-10-27 18:59:08 +080085# define is_fake_mcount is_fake_mcount32
86# define fn_is_fake_mcount fn_is_fake_mcount32
87# define MIPS_is_fake_mcount MIPS32_is_fake_mcount
88# define Elf_Addr Elf32_Addr
Steven Rostedtc28d5072010-10-13 19:06:14 -040089# define Elf_Ehdr Elf32_Ehdr
90# define Elf_Shdr Elf32_Shdr
91# define Elf_Rel Elf32_Rel
92# define Elf_Rela Elf32_Rela
93# define Elf_Sym Elf32_Sym
94# define ELF_R_SYM ELF32_R_SYM
John Reisera2d493582010-10-27 18:59:07 +080095# define Elf_r_sym Elf32_r_sym
Steven Rostedtc28d5072010-10-13 19:06:14 -040096# define ELF_R_INFO ELF32_R_INFO
John Reisera2d493582010-10-27 18:59:07 +080097# define Elf_r_info Elf32_r_info
Steven Rostedtc28d5072010-10-13 19:06:14 -040098# define ELF_ST_BIND ELF32_ST_BIND
John Reisera2d493582010-10-27 18:59:07 +080099# define fn_ELF_R_SYM fn_ELF32_R_SYM
100# define fn_ELF_R_INFO fn_ELF32_R_INFO
Steven Rostedtc28d5072010-10-13 19:06:14 -0400101# define uint_t uint32_t
102# define _w w
103# define _align 3u
104# define _size 4
105#endif
106
Wu Zhangjin412910c2010-10-27 18:59:08 +0800107/* Functions and pointers that do_file() may override for specific e_machine. */
108static int fn_is_fake_mcount(Elf_Rel const *rp)
109{
110 return 0;
111}
112static int (*is_fake_mcount)(Elf_Rel const *rp) = fn_is_fake_mcount;
113
John Reisera2d493582010-10-27 18:59:07 +0800114static uint_t fn_ELF_R_SYM(Elf_Rel const *rp)
115{
116 return ELF_R_SYM(_w(rp->r_info));
117}
118static uint_t (*Elf_r_sym)(Elf_Rel const *rp) = fn_ELF_R_SYM;
119
120static void fn_ELF_R_INFO(Elf_Rel *const rp, unsigned sym, unsigned type)
121{
John Reisere63233f72010-11-22 19:41:44 -0800122 rp->r_info = _w(ELF_R_INFO(sym, type));
John Reisera2d493582010-10-27 18:59:07 +0800123}
124static void (*Elf_r_info)(Elf_Rel *const rp, unsigned sym, unsigned type) = fn_ELF_R_INFO;
125
Wu Zhangjin412910c2010-10-27 18:59:08 +0800126/*
127 * MIPS mcount long call has 2 _mcount symbols, only the position of the 1st
128 * _mcount symbol is needed for dynamic function tracer, with it, to disable
129 * tracing(ftrace_make_nop), the instruction in the position is replaced with
130 * the "b label" instruction, to enable tracing(ftrace_make_call), replace the
131 * instruction back. So, here, we set the 2nd one as fake and filter it.
132 *
133 * c: 3c030000 lui v1,0x0 <--> b label
134 * c: R_MIPS_HI16 _mcount
135 * c: R_MIPS_NONE *ABS*
136 * c: R_MIPS_NONE *ABS*
137 * 10: 64630000 daddiu v1,v1,0
138 * 10: R_MIPS_LO16 _mcount
139 * 10: R_MIPS_NONE *ABS*
140 * 10: R_MIPS_NONE *ABS*
141 * 14: 03e0082d move at,ra
142 * 18: 0060f809 jalr v1
143 * label:
144 */
145#define MIPS_FAKEMCOUNT_OFFSET 4
146
147static int MIPS_is_fake_mcount(Elf_Rel const *rp)
148{
149 static Elf_Addr old_r_offset;
150 Elf_Addr current_r_offset = _w(rp->r_offset);
151 int is_fake;
152
153 is_fake = old_r_offset &&
154 (current_r_offset - old_r_offset == MIPS_FAKEMCOUNT_OFFSET);
155 old_r_offset = current_r_offset;
156
157 return is_fake;
158}
John Reisera2d493582010-10-27 18:59:07 +0800159
Steven Rostedtc28d5072010-10-13 19:06:14 -0400160/* Append the new shstrtab, Elf_Shdr[], __mcount_loc and its relocations. */
161static void append_func(Elf_Ehdr *const ehdr,
162 Elf_Shdr *const shstr,
163 uint_t const *const mloc0,
164 uint_t const *const mlocp,
165 Elf_Rel const *const mrel0,
166 Elf_Rel const *const mrelp,
167 unsigned int const rel_entsize,
168 unsigned int const symsec_sh_link)
169{
170 /* Begin constructing output file */
171 Elf_Shdr mcsec;
172 char const *mc_name = (sizeof(Elf_Rela) == rel_entsize)
173 ? ".rela__mcount_loc"
174 : ".rel__mcount_loc";
175 unsigned const old_shnum = w2(ehdr->e_shnum);
176 uint_t const old_shoff = _w(ehdr->e_shoff);
177 uint_t const old_shstr_sh_size = _w(shstr->sh_size);
178 uint_t const old_shstr_sh_offset = _w(shstr->sh_offset);
179 uint_t t = 1 + strlen(mc_name) + _w(shstr->sh_size);
180 uint_t new_e_shoff;
181
182 shstr->sh_size = _w(t);
183 shstr->sh_offset = _w(sb.st_size);
184 t += sb.st_size;
185 t += (_align & -t); /* word-byte align */
186 new_e_shoff = t;
187
188 /* body for new shstrtab */
189 ulseek(fd_map, sb.st_size, SEEK_SET);
190 uwrite(fd_map, old_shstr_sh_offset + (void *)ehdr, old_shstr_sh_size);
191 uwrite(fd_map, mc_name, 1 + strlen(mc_name));
192
193 /* old(modified) Elf_Shdr table, word-byte aligned */
194 ulseek(fd_map, t, SEEK_SET);
195 t += sizeof(Elf_Shdr) * old_shnum;
196 uwrite(fd_map, old_shoff + (void *)ehdr,
197 sizeof(Elf_Shdr) * old_shnum);
198
199 /* new sections __mcount_loc and .rel__mcount_loc */
200 t += 2*sizeof(mcsec);
201 mcsec.sh_name = w((sizeof(Elf_Rela) == rel_entsize) + strlen(".rel")
202 + old_shstr_sh_size);
203 mcsec.sh_type = w(SHT_PROGBITS);
204 mcsec.sh_flags = _w(SHF_ALLOC);
205 mcsec.sh_addr = 0;
206 mcsec.sh_offset = _w(t);
207 mcsec.sh_size = _w((void *)mlocp - (void *)mloc0);
208 mcsec.sh_link = 0;
209 mcsec.sh_info = 0;
210 mcsec.sh_addralign = _w(_size);
211 mcsec.sh_entsize = _w(_size);
212 uwrite(fd_map, &mcsec, sizeof(mcsec));
213
214 mcsec.sh_name = w(old_shstr_sh_size);
215 mcsec.sh_type = (sizeof(Elf_Rela) == rel_entsize)
216 ? w(SHT_RELA)
217 : w(SHT_REL);
218 mcsec.sh_flags = 0;
219 mcsec.sh_addr = 0;
220 mcsec.sh_offset = _w((void *)mlocp - (void *)mloc0 + t);
221 mcsec.sh_size = _w((void *)mrelp - (void *)mrel0);
222 mcsec.sh_link = w(symsec_sh_link);
223 mcsec.sh_info = w(old_shnum);
224 mcsec.sh_addralign = _w(_size);
225 mcsec.sh_entsize = _w(rel_entsize);
226 uwrite(fd_map, &mcsec, sizeof(mcsec));
227
228 uwrite(fd_map, mloc0, (void *)mlocp - (void *)mloc0);
229 uwrite(fd_map, mrel0, (void *)mrelp - (void *)mrel0);
230
231 ehdr->e_shoff = _w(new_e_shoff);
232 ehdr->e_shnum = w2(2 + w2(ehdr->e_shnum)); /* {.rel,}__mcount_loc */
233 ulseek(fd_map, 0, SEEK_SET);
234 uwrite(fd_map, ehdr, sizeof(*ehdr));
235}
236
Steven Rostedtc28d5072010-10-13 19:06:14 -0400237/*
238 * Look at the relocations in order to find the calls to mcount.
239 * Accumulate the section offsets that are found, and their relocation info,
240 * onto the end of the existing arrays.
241 */
242static uint_t *sift_rel_mcount(uint_t *mlocp,
243 unsigned const offbase,
244 Elf_Rel **const mrelpp,
245 Elf_Shdr const *const relhdr,
246 Elf_Ehdr const *const ehdr,
247 unsigned const recsym,
248 uint_t const recval,
249 unsigned const reltype)
250{
251 uint_t *const mloc0 = mlocp;
252 Elf_Rel *mrelp = *mrelpp;
253 Elf_Shdr *const shdr0 = (Elf_Shdr *)(_w(ehdr->e_shoff)
254 + (void *)ehdr);
255 unsigned const symsec_sh_link = w(relhdr->sh_link);
256 Elf_Shdr const *const symsec = &shdr0[symsec_sh_link];
257 Elf_Sym const *const sym0 = (Elf_Sym const *)(_w(symsec->sh_offset)
258 + (void *)ehdr);
259
260 Elf_Shdr const *const strsec = &shdr0[w(symsec->sh_link)];
261 char const *const str0 = (char const *)(_w(strsec->sh_offset)
262 + (void *)ehdr);
263
264 Elf_Rel const *const rel0 = (Elf_Rel const *)(_w(relhdr->sh_offset)
265 + (void *)ehdr);
266 unsigned rel_entsize = _w(relhdr->sh_entsize);
267 unsigned const nrel = _w(relhdr->sh_size) / rel_entsize;
268 Elf_Rel const *relp = rel0;
269
270 unsigned mcountsym = 0;
271 unsigned t;
272
273 for (t = nrel; t; --t) {
274 if (!mcountsym) {
275 Elf_Sym const *const symp =
John Reisera2d493582010-10-27 18:59:07 +0800276 &sym0[Elf_r_sym(relp)];
Steven Rostedtc28d5072010-10-13 19:06:14 -0400277 char const *symname = &str0[w(symp->st_name)];
Rabin Vincented604532010-11-30 17:36:48 +0100278 char const *mcount = '_' == gpfx ? "_mcount" : "mcount";
Steven Rostedtc28d5072010-10-13 19:06:14 -0400279
280 if ('.' == symname[0])
281 ++symname; /* ppc64 hack */
Rabin Vincented604532010-11-30 17:36:48 +0100282 if (0 == strcmp(mcount, symname) ||
283 (altmcount && 0 == strcmp(altmcount, symname)))
John Reisera2d493582010-10-27 18:59:07 +0800284 mcountsym = Elf_r_sym(relp);
Steven Rostedtc28d5072010-10-13 19:06:14 -0400285 }
286
Wu Zhangjin412910c2010-10-27 18:59:08 +0800287 if (mcountsym == Elf_r_sym(relp) && !is_fake_mcount(relp)) {
Steven Rostedtc28d5072010-10-13 19:06:14 -0400288 uint_t const addend = _w(_w(relp->r_offset) - recval);
289
290 mrelp->r_offset = _w(offbase
291 + ((void *)mlocp - (void *)mloc0));
John Reisera2d493582010-10-27 18:59:07 +0800292 Elf_r_info(mrelp, recsym, reltype);
Steven Rostedtc28d5072010-10-13 19:06:14 -0400293 if (sizeof(Elf_Rela) == rel_entsize) {
294 ((Elf_Rela *)mrelp)->r_addend = addend;
295 *mlocp++ = 0;
296 } else
297 *mlocp++ = addend;
298
299 mrelp = (Elf_Rel *)(rel_entsize + (void *)mrelp);
300 }
301 relp = (Elf_Rel const *)(rel_entsize + (void *)relp);
302 }
303 *mrelpp = mrelp;
304 return mlocp;
305}
306
307
308/*
309 * Find a symbol in the given section, to be used as the base for relocating
310 * the table of offsets of calls to mcount. A local or global symbol suffices,
311 * but avoid a Weak symbol because it may be overridden; the change in value
312 * would invalidate the relocations of the offsets of the calls to mcount.
313 * Often the found symbol will be the unnamed local symbol generated by
314 * GNU 'as' for the start of each section. For example:
315 * Num: Value Size Type Bind Vis Ndx Name
316 * 2: 00000000 0 SECTION LOCAL DEFAULT 1
317 */
318static unsigned find_secsym_ndx(unsigned const txtndx,
319 char const *const txtname,
320 uint_t *const recvalp,
321 Elf_Shdr const *const symhdr,
322 Elf_Ehdr const *const ehdr)
323{
324 Elf_Sym const *const sym0 = (Elf_Sym const *)(_w(symhdr->sh_offset)
325 + (void *)ehdr);
326 unsigned const nsym = _w(symhdr->sh_size) / _w(symhdr->sh_entsize);
327 Elf_Sym const *symp;
328 unsigned t;
329
330 for (symp = sym0, t = nsym; t; --t, ++symp) {
331 unsigned int const st_bind = ELF_ST_BIND(symp->st_info);
332
333 if (txtndx == w2(symp->st_shndx)
334 /* avoid STB_WEAK */
335 && (STB_LOCAL == st_bind || STB_GLOBAL == st_bind)) {
336 *recvalp = _w(symp->st_value);
337 return symp - sym0;
338 }
339 }
340 fprintf(stderr, "Cannot find symbol for section %d: %s.\n",
341 txtndx, txtname);
342 fail_file();
343}
344
345
346/* Evade ISO C restriction: no declaration after statement in has_rel_mcount. */
347static char const *
348__has_rel_mcount(Elf_Shdr const *const relhdr, /* is SHT_REL or SHT_RELA */
349 Elf_Shdr const *const shdr0,
350 char const *const shstrtab,
351 char const *const fname)
352{
353 /* .sh_info depends on .sh_type == SHT_REL[,A] */
354 Elf_Shdr const *const txthdr = &shdr0[w(relhdr->sh_info)];
355 char const *const txtname = &shstrtab[w(txthdr->sh_name)];
356
357 if (0 == strcmp("__mcount_loc", txtname)) {
358 fprintf(stderr, "warning: __mcount_loc already exists: %s\n",
359 fname);
360 succeed_file();
361 }
362 if (SHT_PROGBITS != w(txthdr->sh_type) ||
363 !is_mcounted_section_name(txtname))
364 return NULL;
365 return txtname;
366}
367
368static char const *has_rel_mcount(Elf_Shdr const *const relhdr,
369 Elf_Shdr const *const shdr0,
370 char const *const shstrtab,
371 char const *const fname)
372{
373 if (SHT_REL != w(relhdr->sh_type) && SHT_RELA != w(relhdr->sh_type))
374 return NULL;
375 return __has_rel_mcount(relhdr, shdr0, shstrtab, fname);
376}
377
378
379static unsigned tot_relsize(Elf_Shdr const *const shdr0,
380 unsigned nhdr,
381 const char *const shstrtab,
382 const char *const fname)
383{
384 unsigned totrelsz = 0;
385 Elf_Shdr const *shdrp = shdr0;
386
387 for (; nhdr; --nhdr, ++shdrp) {
388 if (has_rel_mcount(shdrp, shdr0, shstrtab, fname))
389 totrelsz += _w(shdrp->sh_size);
390 }
391 return totrelsz;
392}
393
394
395/* Overall supervision for Elf32 ET_REL file. */
396static void
397do_func(Elf_Ehdr *const ehdr, char const *const fname, unsigned const reltype)
398{
399 Elf_Shdr *const shdr0 = (Elf_Shdr *)(_w(ehdr->e_shoff)
400 + (void *)ehdr);
401 unsigned const nhdr = w2(ehdr->e_shnum);
402 Elf_Shdr *const shstr = &shdr0[w2(ehdr->e_shstrndx)];
403 char const *const shstrtab = (char const *)(_w(shstr->sh_offset)
404 + (void *)ehdr);
405
406 Elf_Shdr const *relhdr;
407 unsigned k;
408
409 /* Upper bound on space: assume all relevant relocs are for mcount. */
410 unsigned const totrelsz = tot_relsize(shdr0, nhdr, shstrtab, fname);
411 Elf_Rel *const mrel0 = umalloc(totrelsz);
412 Elf_Rel * mrelp = mrel0;
413
414 /* 2*sizeof(address) <= sizeof(Elf_Rel) */
415 uint_t *const mloc0 = umalloc(totrelsz>>1);
416 uint_t * mlocp = mloc0;
417
418 unsigned rel_entsize = 0;
419 unsigned symsec_sh_link = 0;
420
421 for (relhdr = shdr0, k = nhdr; k; --k, ++relhdr) {
422 char const *const txtname = has_rel_mcount(relhdr, shdr0,
423 shstrtab, fname);
424 if (txtname) {
425 uint_t recval = 0;
426 unsigned const recsym = find_secsym_ndx(
427 w(relhdr->sh_info), txtname, &recval,
428 &shdr0[symsec_sh_link = w(relhdr->sh_link)],
429 ehdr);
430
431 rel_entsize = _w(relhdr->sh_entsize);
432 mlocp = sift_rel_mcount(mlocp,
433 (void *)mlocp - (void *)mloc0, &mrelp,
434 relhdr, ehdr, recsym, recval, reltype);
435 }
436 }
437 if (mloc0 != mlocp) {
438 append_func(ehdr, shstr, mloc0, mlocp, mrel0, mrelp,
439 rel_entsize, symsec_sh_link);
440 }
441 free(mrel0);
442 free(mloc0);
443}