]> bbs.cooldavid.org Git - net-next-2.6.git/blob - scripts/recordmcount.h
ftrace/MIPS: Add MIPS64 support for C version of recordmcount
[net-next-2.6.git] / scripts / recordmcount.h
1 /*
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
22 #undef sift_rel_mcount
23 #undef find_secsym_ndx
24 #undef __has_rel_mcount
25 #undef has_rel_mcount
26 #undef tot_relsize
27 #undef do_func
28 #undef Elf_Ehdr
29 #undef Elf_Shdr
30 #undef Elf_Rel
31 #undef Elf_Rela
32 #undef Elf_Sym
33 #undef ELF_R_SYM
34 #undef Elf_r_sym
35 #undef ELF_R_INFO
36 #undef Elf_r_info
37 #undef ELF_ST_BIND
38 #undef fn_ELF_R_SYM
39 #undef fn_ELF_R_INFO
40 #undef uint_t
41 #undef _w
42 #undef _align
43 #undef _size
44
45 #ifdef RECORD_MCOUNT_64
46 # define append_func            append64
47 # define sift_rel_mcount        sift64_rel_mcount
48 # define find_secsym_ndx        find64_secsym_ndx
49 # define __has_rel_mcount       __has64_rel_mcount
50 # define has_rel_mcount         has64_rel_mcount
51 # define tot_relsize            tot64_relsize
52 # define do_func                do64
53 # define Elf_Ehdr               Elf64_Ehdr
54 # define Elf_Shdr               Elf64_Shdr
55 # define Elf_Rel                Elf64_Rel
56 # define Elf_Rela               Elf64_Rela
57 # define Elf_Sym                Elf64_Sym
58 # define ELF_R_SYM              ELF64_R_SYM
59 # define Elf_r_sym              Elf64_r_sym
60 # define ELF_R_INFO             ELF64_R_INFO
61 # define Elf_r_info             Elf64_r_info
62 # define ELF_ST_BIND            ELF64_ST_BIND
63 # define fn_ELF_R_SYM           fn_ELF64_R_SYM
64 # define fn_ELF_R_INFO          fn_ELF64_R_INFO
65 # define uint_t                 uint64_t
66 # define _w                     w8
67 # define _align                 7u
68 # define _size                  8
69 #else
70 # define append_func            append32
71 # define sift_rel_mcount        sift32_rel_mcount
72 # define find_secsym_ndx        find32_secsym_ndx
73 # define __has_rel_mcount       __has32_rel_mcount
74 # define has_rel_mcount         has32_rel_mcount
75 # define tot_relsize            tot32_relsize
76 # define do_func                do32
77 # define Elf_Ehdr               Elf32_Ehdr
78 # define Elf_Shdr               Elf32_Shdr
79 # define Elf_Rel                Elf32_Rel
80 # define Elf_Rela               Elf32_Rela
81 # define Elf_Sym                Elf32_Sym
82 # define ELF_R_SYM              ELF32_R_SYM
83 # define Elf_r_sym              Elf32_r_sym
84 # define ELF_R_INFO             ELF32_R_INFO
85 # define Elf_r_info             Elf32_r_info
86 # define ELF_ST_BIND            ELF32_ST_BIND
87 # define fn_ELF_R_SYM           fn_ELF32_R_SYM
88 # define fn_ELF_R_INFO          fn_ELF32_R_INFO
89 # define uint_t                 uint32_t
90 # define _w                     w
91 # define _align                 3u
92 # define _size                  4
93 #endif
94
95 /* Functions and pointers that 64-bit EM_MIPS can override. */
96 static uint_t fn_ELF_R_SYM(Elf_Rel const *rp)
97 {
98         return ELF_R_SYM(_w(rp->r_info));
99 }
100 static uint_t (*Elf_r_sym)(Elf_Rel const *rp) = fn_ELF_R_SYM;
101
102 static void fn_ELF_R_INFO(Elf_Rel *const rp, unsigned sym, unsigned type)
103 {
104         rp->r_info = ELF_R_INFO(sym, type);
105 }
106 static void (*Elf_r_info)(Elf_Rel *const rp, unsigned sym, unsigned type) = fn_ELF_R_INFO;
107
108
109 /* Append the new shstrtab, Elf_Shdr[], __mcount_loc and its relocations. */
110 static void append_func(Elf_Ehdr *const ehdr,
111                         Elf_Shdr *const shstr,
112                         uint_t const *const mloc0,
113                         uint_t const *const mlocp,
114                         Elf_Rel const *const mrel0,
115                         Elf_Rel const *const mrelp,
116                         unsigned int const rel_entsize,
117                         unsigned int const symsec_sh_link)
118 {
119         /* Begin constructing output file */
120         Elf_Shdr mcsec;
121         char const *mc_name = (sizeof(Elf_Rela) == rel_entsize)
122                 ? ".rela__mcount_loc"
123                 :  ".rel__mcount_loc";
124         unsigned const old_shnum = w2(ehdr->e_shnum);
125         uint_t const old_shoff = _w(ehdr->e_shoff);
126         uint_t const old_shstr_sh_size   = _w(shstr->sh_size);
127         uint_t const old_shstr_sh_offset = _w(shstr->sh_offset);
128         uint_t t = 1 + strlen(mc_name) + _w(shstr->sh_size);
129         uint_t new_e_shoff;
130
131         shstr->sh_size = _w(t);
132         shstr->sh_offset = _w(sb.st_size);
133         t += sb.st_size;
134         t += (_align & -t);  /* word-byte align */
135         new_e_shoff = t;
136
137         /* body for new shstrtab */
138         ulseek(fd_map, sb.st_size, SEEK_SET);
139         uwrite(fd_map, old_shstr_sh_offset + (void *)ehdr, old_shstr_sh_size);
140         uwrite(fd_map, mc_name, 1 + strlen(mc_name));
141
142         /* old(modified) Elf_Shdr table, word-byte aligned */
143         ulseek(fd_map, t, SEEK_SET);
144         t += sizeof(Elf_Shdr) * old_shnum;
145         uwrite(fd_map, old_shoff + (void *)ehdr,
146                sizeof(Elf_Shdr) * old_shnum);
147
148         /* new sections __mcount_loc and .rel__mcount_loc */
149         t += 2*sizeof(mcsec);
150         mcsec.sh_name = w((sizeof(Elf_Rela) == rel_entsize) + strlen(".rel")
151                 + old_shstr_sh_size);
152         mcsec.sh_type = w(SHT_PROGBITS);
153         mcsec.sh_flags = _w(SHF_ALLOC);
154         mcsec.sh_addr = 0;
155         mcsec.sh_offset = _w(t);
156         mcsec.sh_size = _w((void *)mlocp - (void *)mloc0);
157         mcsec.sh_link = 0;
158         mcsec.sh_info = 0;
159         mcsec.sh_addralign = _w(_size);
160         mcsec.sh_entsize = _w(_size);
161         uwrite(fd_map, &mcsec, sizeof(mcsec));
162
163         mcsec.sh_name = w(old_shstr_sh_size);
164         mcsec.sh_type = (sizeof(Elf_Rela) == rel_entsize)
165                 ? w(SHT_RELA)
166                 : w(SHT_REL);
167         mcsec.sh_flags = 0;
168         mcsec.sh_addr = 0;
169         mcsec.sh_offset = _w((void *)mlocp - (void *)mloc0 + t);
170         mcsec.sh_size   = _w((void *)mrelp - (void *)mrel0);
171         mcsec.sh_link = w(symsec_sh_link);
172         mcsec.sh_info = w(old_shnum);
173         mcsec.sh_addralign = _w(_size);
174         mcsec.sh_entsize = _w(rel_entsize);
175         uwrite(fd_map, &mcsec, sizeof(mcsec));
176
177         uwrite(fd_map, mloc0, (void *)mlocp - (void *)mloc0);
178         uwrite(fd_map, mrel0, (void *)mrelp - (void *)mrel0);
179
180         ehdr->e_shoff = _w(new_e_shoff);
181         ehdr->e_shnum = w2(2 + w2(ehdr->e_shnum));  /* {.rel,}__mcount_loc */
182         ulseek(fd_map, 0, SEEK_SET);
183         uwrite(fd_map, ehdr, sizeof(*ehdr));
184 }
185
186
187 /*
188  * Look at the relocations in order to find the calls to mcount.
189  * Accumulate the section offsets that are found, and their relocation info,
190  * onto the end of the existing arrays.
191  */
192 static uint_t *sift_rel_mcount(uint_t *mlocp,
193                                unsigned const offbase,
194                                Elf_Rel **const mrelpp,
195                                Elf_Shdr const *const relhdr,
196                                Elf_Ehdr const *const ehdr,
197                                unsigned const recsym,
198                                uint_t const recval,
199                                unsigned const reltype)
200 {
201         uint_t *const mloc0 = mlocp;
202         Elf_Rel *mrelp = *mrelpp;
203         Elf_Shdr *const shdr0 = (Elf_Shdr *)(_w(ehdr->e_shoff)
204                 + (void *)ehdr);
205         unsigned const symsec_sh_link = w(relhdr->sh_link);
206         Elf_Shdr const *const symsec = &shdr0[symsec_sh_link];
207         Elf_Sym const *const sym0 = (Elf_Sym const *)(_w(symsec->sh_offset)
208                 + (void *)ehdr);
209
210         Elf_Shdr const *const strsec = &shdr0[w(symsec->sh_link)];
211         char const *const str0 = (char const *)(_w(strsec->sh_offset)
212                 + (void *)ehdr);
213
214         Elf_Rel const *const rel0 = (Elf_Rel const *)(_w(relhdr->sh_offset)
215                 + (void *)ehdr);
216         unsigned rel_entsize = _w(relhdr->sh_entsize);
217         unsigned const nrel = _w(relhdr->sh_size) / rel_entsize;
218         Elf_Rel const *relp = rel0;
219
220         unsigned mcountsym = 0;
221         unsigned t;
222
223         for (t = nrel; t; --t) {
224                 if (!mcountsym) {
225                         Elf_Sym const *const symp =
226                                 &sym0[Elf_r_sym(relp)];
227                         char const *symname = &str0[w(symp->st_name)];
228
229                         if ('.' == symname[0])
230                                 ++symname;  /* ppc64 hack */
231                         if (0 == strcmp((('_' == gpfx) ? "_mcount" : "mcount"),
232                                         symname))
233                                 mcountsym = Elf_r_sym(relp);
234                 }
235
236                 if (mcountsym == Elf_r_sym(relp)) {
237                         uint_t const addend = _w(_w(relp->r_offset) - recval);
238
239                         mrelp->r_offset = _w(offbase
240                                 + ((void *)mlocp - (void *)mloc0));
241                         Elf_r_info(mrelp, recsym, reltype);
242                         if (sizeof(Elf_Rela) == rel_entsize) {
243                                 ((Elf_Rela *)mrelp)->r_addend = addend;
244                                 *mlocp++ = 0;
245                         } else
246                                 *mlocp++ = addend;
247
248                         mrelp = (Elf_Rel *)(rel_entsize + (void *)mrelp);
249                 }
250                 relp = (Elf_Rel const *)(rel_entsize + (void *)relp);
251         }
252         *mrelpp = mrelp;
253         return mlocp;
254 }
255
256
257 /*
258  * Find a symbol in the given section, to be used as the base for relocating
259  * the table of offsets of calls to mcount.  A local or global symbol suffices,
260  * but avoid a Weak symbol because it may be overridden; the change in value
261  * would invalidate the relocations of the offsets of the calls to mcount.
262  * Often the found symbol will be the unnamed local symbol generated by
263  * GNU 'as' for the start of each section.  For example:
264  *    Num:    Value  Size Type    Bind   Vis      Ndx Name
265  *      2: 00000000     0 SECTION LOCAL  DEFAULT    1
266  */
267 static unsigned find_secsym_ndx(unsigned const txtndx,
268                                 char const *const txtname,
269                                 uint_t *const recvalp,
270                                 Elf_Shdr const *const symhdr,
271                                 Elf_Ehdr const *const ehdr)
272 {
273         Elf_Sym const *const sym0 = (Elf_Sym const *)(_w(symhdr->sh_offset)
274                 + (void *)ehdr);
275         unsigned const nsym = _w(symhdr->sh_size) / _w(symhdr->sh_entsize);
276         Elf_Sym const *symp;
277         unsigned t;
278
279         for (symp = sym0, t = nsym; t; --t, ++symp) {
280                 unsigned int const st_bind = ELF_ST_BIND(symp->st_info);
281
282                 if (txtndx == w2(symp->st_shndx)
283                         /* avoid STB_WEAK */
284                     && (STB_LOCAL == st_bind || STB_GLOBAL == st_bind)) {
285                         *recvalp = _w(symp->st_value);
286                         return symp - sym0;
287                 }
288         }
289         fprintf(stderr, "Cannot find symbol for section %d: %s.\n",
290                 txtndx, txtname);
291         fail_file();
292 }
293
294
295 /* Evade ISO C restriction: no declaration after statement in has_rel_mcount. */
296 static char const *
297 __has_rel_mcount(Elf_Shdr const *const relhdr,  /* is SHT_REL or SHT_RELA */
298                  Elf_Shdr const *const shdr0,
299                  char const *const shstrtab,
300                  char const *const fname)
301 {
302         /* .sh_info depends on .sh_type == SHT_REL[,A] */
303         Elf_Shdr const *const txthdr = &shdr0[w(relhdr->sh_info)];
304         char const *const txtname = &shstrtab[w(txthdr->sh_name)];
305
306         if (0 == strcmp("__mcount_loc", txtname)) {
307                 fprintf(stderr, "warning: __mcount_loc already exists: %s\n",
308                         fname);
309                 succeed_file();
310         }
311         if (SHT_PROGBITS != w(txthdr->sh_type) ||
312             !is_mcounted_section_name(txtname))
313                 return NULL;
314         return txtname;
315 }
316
317 static char const *has_rel_mcount(Elf_Shdr const *const relhdr,
318                                   Elf_Shdr const *const shdr0,
319                                   char const *const shstrtab,
320                                   char const *const fname)
321 {
322         if (SHT_REL  != w(relhdr->sh_type) && SHT_RELA != w(relhdr->sh_type))
323                 return NULL;
324         return __has_rel_mcount(relhdr, shdr0, shstrtab, fname);
325 }
326
327
328 static unsigned tot_relsize(Elf_Shdr const *const shdr0,
329                             unsigned nhdr,
330                             const char *const shstrtab,
331                             const char *const fname)
332 {
333         unsigned totrelsz = 0;
334         Elf_Shdr const *shdrp = shdr0;
335
336         for (; nhdr; --nhdr, ++shdrp) {
337                 if (has_rel_mcount(shdrp, shdr0, shstrtab, fname))
338                         totrelsz += _w(shdrp->sh_size);
339         }
340         return totrelsz;
341 }
342
343
344 /* Overall supervision for Elf32 ET_REL file. */
345 static void
346 do_func(Elf_Ehdr *const ehdr, char const *const fname, unsigned const reltype)
347 {
348         Elf_Shdr *const shdr0 = (Elf_Shdr *)(_w(ehdr->e_shoff)
349                 + (void *)ehdr);
350         unsigned const nhdr = w2(ehdr->e_shnum);
351         Elf_Shdr *const shstr = &shdr0[w2(ehdr->e_shstrndx)];
352         char const *const shstrtab = (char const *)(_w(shstr->sh_offset)
353                 + (void *)ehdr);
354
355         Elf_Shdr const *relhdr;
356         unsigned k;
357
358         /* Upper bound on space: assume all relevant relocs are for mcount. */
359         unsigned const totrelsz = tot_relsize(shdr0, nhdr, shstrtab, fname);
360         Elf_Rel *const mrel0 = umalloc(totrelsz);
361         Elf_Rel *      mrelp = mrel0;
362
363         /* 2*sizeof(address) <= sizeof(Elf_Rel) */
364         uint_t *const mloc0 = umalloc(totrelsz>>1);
365         uint_t *      mlocp = mloc0;
366
367         unsigned rel_entsize = 0;
368         unsigned symsec_sh_link = 0;
369
370         for (relhdr = shdr0, k = nhdr; k; --k, ++relhdr) {
371                 char const *const txtname = has_rel_mcount(relhdr, shdr0,
372                         shstrtab, fname);
373                 if (txtname) {
374                         uint_t recval = 0;
375                         unsigned const recsym = find_secsym_ndx(
376                                 w(relhdr->sh_info), txtname, &recval,
377                                 &shdr0[symsec_sh_link = w(relhdr->sh_link)],
378                                 ehdr);
379
380                         rel_entsize = _w(relhdr->sh_entsize);
381                         mlocp = sift_rel_mcount(mlocp,
382                                 (void *)mlocp - (void *)mloc0, &mrelp,
383                                 relhdr, ehdr, recsym, recval, reltype);
384                 }
385         }
386         if (mloc0 != mlocp) {
387                 append_func(ehdr, shstr, mloc0, mlocp, mrel0, mrelp,
388                             rel_entsize, symsec_sh_link);
389         }
390         free(mrel0);
391         free(mloc0);
392 }