Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 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 |
Wu Zhangjin | 412910c | 2010-10-27 18:59:08 +0800 | [diff] [blame] | 22 | #undef is_fake_mcount |
| 23 | #undef fn_is_fake_mcount |
| 24 | #undef MIPS_is_fake_mcount |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 25 | #undef sift_rel_mcount |
Steven Rostedt | ffd618f | 2011-04-08 03:58:48 -0400 | [diff] [blame] | 26 | #undef nop_mcount |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 27 | #undef find_secsym_ndx |
| 28 | #undef __has_rel_mcount |
| 29 | #undef has_rel_mcount |
| 30 | #undef tot_relsize |
Steven Rostedt | 37762cb | 2011-04-20 20:47:34 -0400 | [diff] [blame^] | 31 | #undef get_mcountsym |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 32 | #undef do_func |
Wu Zhangjin | 412910c | 2010-10-27 18:59:08 +0800 | [diff] [blame] | 33 | #undef Elf_Addr |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 34 | #undef Elf_Ehdr |
| 35 | #undef Elf_Shdr |
| 36 | #undef Elf_Rel |
| 37 | #undef Elf_Rela |
| 38 | #undef Elf_Sym |
| 39 | #undef ELF_R_SYM |
John Reiser | a2d49358 | 2010-10-27 18:59:07 +0800 | [diff] [blame] | 40 | #undef Elf_r_sym |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 41 | #undef ELF_R_INFO |
John Reiser | a2d49358 | 2010-10-27 18:59:07 +0800 | [diff] [blame] | 42 | #undef Elf_r_info |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 43 | #undef ELF_ST_BIND |
John Reiser | a2d49358 | 2010-10-27 18:59:07 +0800 | [diff] [blame] | 44 | #undef fn_ELF_R_SYM |
| 45 | #undef fn_ELF_R_INFO |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 46 | #undef uint_t |
| 47 | #undef _w |
| 48 | #undef _align |
| 49 | #undef _size |
| 50 | |
| 51 | #ifdef RECORD_MCOUNT_64 |
| 52 | # define append_func append64 |
| 53 | # define sift_rel_mcount sift64_rel_mcount |
Steven Rostedt | ffd618f | 2011-04-08 03:58:48 -0400 | [diff] [blame] | 54 | # define nop_mcount nop_mcount_64 |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 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 |
| 59 | # define do_func do64 |
Steven Rostedt | 37762cb | 2011-04-20 20:47:34 -0400 | [diff] [blame^] | 60 | # define get_mcountsym get_mcountsym_64 |
Wu Zhangjin | 412910c | 2010-10-27 18:59:08 +0800 | [diff] [blame] | 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 |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 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 |
John Reiser | a2d49358 | 2010-10-27 18:59:07 +0800 | [diff] [blame] | 71 | # define Elf_r_sym Elf64_r_sym |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 72 | # define ELF_R_INFO ELF64_R_INFO |
John Reiser | a2d49358 | 2010-10-27 18:59:07 +0800 | [diff] [blame] | 73 | # define Elf_r_info Elf64_r_info |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 74 | # define ELF_ST_BIND ELF64_ST_BIND |
John Reiser | a2d49358 | 2010-10-27 18:59:07 +0800 | [diff] [blame] | 75 | # define fn_ELF_R_SYM fn_ELF64_R_SYM |
| 76 | # define fn_ELF_R_INFO fn_ELF64_R_INFO |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 77 | # define uint_t uint64_t |
| 78 | # define _w w8 |
| 79 | # define _align 7u |
| 80 | # define _size 8 |
| 81 | #else |
| 82 | # define append_func append32 |
| 83 | # define sift_rel_mcount sift32_rel_mcount |
Steven Rostedt | ffd618f | 2011-04-08 03:58:48 -0400 | [diff] [blame] | 84 | # define nop_mcount nop_mcount_32 |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 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 |
| 89 | # define do_func do32 |
Steven Rostedt | 37762cb | 2011-04-20 20:47:34 -0400 | [diff] [blame^] | 90 | # define get_mcountsym get_mcountsym_32 |
Wu Zhangjin | 412910c | 2010-10-27 18:59:08 +0800 | [diff] [blame] | 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 |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 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 |
John Reiser | a2d49358 | 2010-10-27 18:59:07 +0800 | [diff] [blame] | 101 | # define Elf_r_sym Elf32_r_sym |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 102 | # define ELF_R_INFO ELF32_R_INFO |
John Reiser | a2d49358 | 2010-10-27 18:59:07 +0800 | [diff] [blame] | 103 | # define Elf_r_info Elf32_r_info |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 104 | # define ELF_ST_BIND ELF32_ST_BIND |
John Reiser | a2d49358 | 2010-10-27 18:59:07 +0800 | [diff] [blame] | 105 | # define fn_ELF_R_SYM fn_ELF32_R_SYM |
| 106 | # define fn_ELF_R_INFO fn_ELF32_R_INFO |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 107 | # define uint_t uint32_t |
| 108 | # define _w w |
| 109 | # define _align 3u |
| 110 | # define _size 4 |
| 111 | #endif |
| 112 | |
Wu Zhangjin | 412910c | 2010-10-27 18:59:08 +0800 | [diff] [blame] | 113 | /* Functions and pointers that do_file() may override for specific e_machine. */ |
| 114 | static int fn_is_fake_mcount(Elf_Rel const *rp) |
| 115 | { |
| 116 | return 0; |
| 117 | } |
| 118 | static int (*is_fake_mcount)(Elf_Rel const *rp) = fn_is_fake_mcount; |
| 119 | |
John Reiser | a2d49358 | 2010-10-27 18:59:07 +0800 | [diff] [blame] | 120 | static uint_t fn_ELF_R_SYM(Elf_Rel const *rp) |
| 121 | { |
| 122 | return ELF_R_SYM(_w(rp->r_info)); |
| 123 | } |
| 124 | static uint_t (*Elf_r_sym)(Elf_Rel const *rp) = fn_ELF_R_SYM; |
| 125 | |
| 126 | static void fn_ELF_R_INFO(Elf_Rel *const rp, unsigned sym, unsigned type) |
| 127 | { |
John Reiser | e63233f7 | 2010-11-22 19:41:44 -0800 | [diff] [blame] | 128 | rp->r_info = _w(ELF_R_INFO(sym, type)); |
John Reiser | a2d49358 | 2010-10-27 18:59:07 +0800 | [diff] [blame] | 129 | } |
| 130 | static void (*Elf_r_info)(Elf_Rel *const rp, unsigned sym, unsigned type) = fn_ELF_R_INFO; |
| 131 | |
Wu Zhangjin | 412910c | 2010-10-27 18:59:08 +0800 | [diff] [blame] | 132 | /* |
| 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. |
| 138 | * |
| 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 |
| 149 | * label: |
| 150 | */ |
| 151 | #define MIPS_FAKEMCOUNT_OFFSET 4 |
| 152 | |
| 153 | static int MIPS_is_fake_mcount(Elf_Rel const *rp) |
| 154 | { |
| 155 | static Elf_Addr old_r_offset; |
| 156 | Elf_Addr current_r_offset = _w(rp->r_offset); |
| 157 | int is_fake; |
| 158 | |
| 159 | is_fake = old_r_offset && |
| 160 | (current_r_offset - old_r_offset == MIPS_FAKEMCOUNT_OFFSET); |
| 161 | old_r_offset = current_r_offset; |
| 162 | |
| 163 | return is_fake; |
| 164 | } |
John Reiser | a2d49358 | 2010-10-27 18:59:07 +0800 | [diff] [blame] | 165 | |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 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) |
| 175 | { |
| 176 | /* Begin constructing output file */ |
| 177 | Elf_Shdr mcsec; |
| 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); |
| 186 | uint_t new_e_shoff; |
| 187 | |
| 188 | shstr->sh_size = _w(t); |
| 189 | shstr->sh_offset = _w(sb.st_size); |
| 190 | t += sb.st_size; |
| 191 | t += (_align & -t); /* word-byte align */ |
| 192 | new_e_shoff = t; |
| 193 | |
| 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)); |
| 198 | |
| 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); |
| 204 | |
| 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); |
| 211 | mcsec.sh_addr = 0; |
| 212 | mcsec.sh_offset = _w(t); |
| 213 | mcsec.sh_size = _w((void *)mlocp - (void *)mloc0); |
| 214 | mcsec.sh_link = 0; |
| 215 | mcsec.sh_info = 0; |
| 216 | mcsec.sh_addralign = _w(_size); |
| 217 | mcsec.sh_entsize = _w(_size); |
| 218 | uwrite(fd_map, &mcsec, sizeof(mcsec)); |
| 219 | |
| 220 | mcsec.sh_name = w(old_shstr_sh_size); |
| 221 | mcsec.sh_type = (sizeof(Elf_Rela) == rel_entsize) |
| 222 | ? w(SHT_RELA) |
| 223 | : w(SHT_REL); |
| 224 | mcsec.sh_flags = 0; |
| 225 | mcsec.sh_addr = 0; |
| 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)); |
| 233 | |
| 234 | uwrite(fd_map, mloc0, (void *)mlocp - (void *)mloc0); |
| 235 | uwrite(fd_map, mrel0, (void *)mrelp - (void *)mrel0); |
| 236 | |
| 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)); |
| 241 | } |
| 242 | |
Steven Rostedt | 37762cb | 2011-04-20 20:47:34 -0400 | [diff] [blame^] | 243 | static unsigned get_mcountsym(Elf_Sym const *const sym0, |
| 244 | Elf_Rel const *relp, |
| 245 | char const *const str0) |
| 246 | { |
| 247 | unsigned mcountsym = 0; |
| 248 | |
| 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"; |
| 253 | |
| 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); |
| 259 | |
| 260 | return mcountsym; |
| 261 | } |
| 262 | |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 263 | /* |
| 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. |
| 267 | */ |
| 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, |
| 274 | uint_t const recval, |
| 275 | unsigned const reltype) |
| 276 | { |
| 277 | uint_t *const mloc0 = mlocp; |
| 278 | Elf_Rel *mrelp = *mrelpp; |
| 279 | Elf_Shdr *const shdr0 = (Elf_Shdr *)(_w(ehdr->e_shoff) |
| 280 | + (void *)ehdr); |
| 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) |
| 284 | + (void *)ehdr); |
| 285 | |
| 286 | Elf_Shdr const *const strsec = &shdr0[w(symsec->sh_link)]; |
| 287 | char const *const str0 = (char const *)(_w(strsec->sh_offset) |
| 288 | + (void *)ehdr); |
| 289 | |
| 290 | Elf_Rel const *const rel0 = (Elf_Rel const *)(_w(relhdr->sh_offset) |
| 291 | + (void *)ehdr); |
| 292 | unsigned rel_entsize = _w(relhdr->sh_entsize); |
| 293 | unsigned const nrel = _w(relhdr->sh_size) / rel_entsize; |
| 294 | Elf_Rel const *relp = rel0; |
| 295 | |
| 296 | unsigned mcountsym = 0; |
| 297 | unsigned t; |
| 298 | |
| 299 | for (t = nrel; t; --t) { |
Steven Rostedt | 37762cb | 2011-04-20 20:47:34 -0400 | [diff] [blame^] | 300 | if (!mcountsym) |
| 301 | mcountsym = get_mcountsym(sym0, relp, str0); |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 302 | |
Wu Zhangjin | 412910c | 2010-10-27 18:59:08 +0800 | [diff] [blame] | 303 | if (mcountsym == Elf_r_sym(relp) && !is_fake_mcount(relp)) { |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 304 | uint_t const addend = _w(_w(relp->r_offset) - recval); |
| 305 | |
| 306 | mrelp->r_offset = _w(offbase |
| 307 | + ((void *)mlocp - (void *)mloc0)); |
John Reiser | a2d49358 | 2010-10-27 18:59:07 +0800 | [diff] [blame] | 308 | Elf_r_info(mrelp, recsym, reltype); |
Steven Rostedt | dd5477f | 2011-04-06 13:21:17 -0400 | [diff] [blame] | 309 | if (rel_entsize == sizeof(Elf_Rela)) { |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 310 | ((Elf_Rela *)mrelp)->r_addend = addend; |
| 311 | *mlocp++ = 0; |
| 312 | } else |
| 313 | *mlocp++ = addend; |
| 314 | |
| 315 | mrelp = (Elf_Rel *)(rel_entsize + (void *)mrelp); |
| 316 | } |
| 317 | relp = (Elf_Rel const *)(rel_entsize + (void *)relp); |
| 318 | } |
| 319 | *mrelpp = mrelp; |
| 320 | return mlocp; |
| 321 | } |
| 322 | |
Steven Rostedt | ffd618f | 2011-04-08 03:58:48 -0400 | [diff] [blame] | 323 | /* |
| 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 |
| 326 | * into nops. |
| 327 | */ |
| 328 | static void nop_mcount(Elf_Shdr const *const relhdr, |
Steven Rostedt | dfad3d5 | 2011-04-12 18:53:25 -0400 | [diff] [blame] | 329 | Elf_Ehdr const *const ehdr, |
| 330 | const char *const txtname) |
Steven Rostedt | ffd618f | 2011-04-08 03:58:48 -0400 | [diff] [blame] | 331 | { |
| 332 | Elf_Shdr *const shdr0 = (Elf_Shdr *)(_w(ehdr->e_shoff) |
| 333 | + (void *)ehdr); |
| 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) |
| 337 | + (void *)ehdr); |
| 338 | |
| 339 | Elf_Shdr const *const strsec = &shdr0[w(symsec->sh_link)]; |
| 340 | char const *const str0 = (char const *)(_w(strsec->sh_offset) |
| 341 | + (void *)ehdr); |
| 342 | |
| 343 | Elf_Rel const *const rel0 = (Elf_Rel const *)(_w(relhdr->sh_offset) |
| 344 | + (void *)ehdr); |
| 345 | unsigned rel_entsize = _w(relhdr->sh_entsize); |
| 346 | unsigned const nrel = _w(relhdr->sh_size) / rel_entsize; |
| 347 | Elf_Rel const *relp = rel0; |
| 348 | |
| 349 | Elf_Shdr const *const shdr = &shdr0[w(relhdr->sh_info)]; |
| 350 | |
| 351 | unsigned mcountsym = 0; |
| 352 | unsigned t; |
Steven Rostedt | dfad3d5 | 2011-04-12 18:53:25 -0400 | [diff] [blame] | 353 | int once = 0; |
Steven Rostedt | ffd618f | 2011-04-08 03:58:48 -0400 | [diff] [blame] | 354 | |
| 355 | for (t = nrel; t; --t) { |
| 356 | int ret = -1; |
| 357 | |
Steven Rostedt | 37762cb | 2011-04-20 20:47:34 -0400 | [diff] [blame^] | 358 | if (!mcountsym) |
| 359 | mcountsym = get_mcountsym(sym0, relp, str0); |
Steven Rostedt | ffd618f | 2011-04-08 03:58:48 -0400 | [diff] [blame] | 360 | |
Steven Rostedt | dfad3d5 | 2011-04-12 18:53:25 -0400 | [diff] [blame] | 361 | if (mcountsym == Elf_r_sym(relp) && !is_fake_mcount(relp)) { |
| 362 | if (make_nop) |
| 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", |
| 366 | txtname); |
| 367 | once = 1; |
| 368 | /* just warn? */ |
| 369 | if (!make_nop) |
| 370 | return; |
| 371 | } |
| 372 | } |
Steven Rostedt | ffd618f | 2011-04-08 03:58:48 -0400 | [diff] [blame] | 373 | |
| 374 | /* |
| 375 | * If we successfully removed the mcount, mark the relocation |
| 376 | * as a nop (don't do anything with it). |
| 377 | */ |
| 378 | if (!ret) { |
| 379 | Elf_Rel rel; |
| 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)); |
| 384 | } |
| 385 | relp = (Elf_Rel const *)(rel_entsize + (void *)relp); |
| 386 | } |
| 387 | } |
| 388 | |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 389 | |
| 390 | /* |
| 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 |
| 399 | */ |
| 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) |
| 405 | { |
| 406 | Elf_Sym const *const sym0 = (Elf_Sym const *)(_w(symhdr->sh_offset) |
| 407 | + (void *)ehdr); |
| 408 | unsigned const nsym = _w(symhdr->sh_size) / _w(symhdr->sh_entsize); |
| 409 | Elf_Sym const *symp; |
| 410 | unsigned t; |
| 411 | |
| 412 | for (symp = sym0, t = nsym; t; --t, ++symp) { |
| 413 | unsigned int const st_bind = ELF_ST_BIND(symp->st_info); |
| 414 | |
| 415 | if (txtndx == w2(symp->st_shndx) |
| 416 | /* avoid STB_WEAK */ |
| 417 | && (STB_LOCAL == st_bind || STB_GLOBAL == st_bind)) { |
| 418 | *recvalp = _w(symp->st_value); |
| 419 | return symp - sym0; |
| 420 | } |
| 421 | } |
| 422 | fprintf(stderr, "Cannot find symbol for section %d: %s.\n", |
| 423 | txtndx, txtname); |
| 424 | fail_file(); |
| 425 | } |
| 426 | |
| 427 | |
| 428 | /* Evade ISO C restriction: no declaration after statement in has_rel_mcount. */ |
| 429 | static char const * |
| 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) |
| 434 | { |
| 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)]; |
| 438 | |
Steven Rostedt | dd5477f | 2011-04-06 13:21:17 -0400 | [diff] [blame] | 439 | if (strcmp("__mcount_loc", txtname) == 0) { |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 440 | fprintf(stderr, "warning: __mcount_loc already exists: %s\n", |
| 441 | fname); |
| 442 | succeed_file(); |
| 443 | } |
Steven Rostedt | dd5477f | 2011-04-06 13:21:17 -0400 | [diff] [blame] | 444 | if (w(txthdr->sh_type) != SHT_PROGBITS || |
Steven Rostedt | ffd618f | 2011-04-08 03:58:48 -0400 | [diff] [blame] | 445 | !(w(txthdr->sh_flags) & SHF_EXECINSTR)) |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 446 | return NULL; |
| 447 | return txtname; |
| 448 | } |
| 449 | |
| 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) |
| 454 | { |
Steven Rostedt | dd5477f | 2011-04-06 13:21:17 -0400 | [diff] [blame] | 455 | if (w(relhdr->sh_type) != SHT_REL && w(relhdr->sh_type) != SHT_RELA) |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 456 | return NULL; |
| 457 | return __has_rel_mcount(relhdr, shdr0, shstrtab, fname); |
| 458 | } |
| 459 | |
| 460 | |
| 461 | static unsigned tot_relsize(Elf_Shdr const *const shdr0, |
| 462 | unsigned nhdr, |
| 463 | const char *const shstrtab, |
| 464 | const char *const fname) |
| 465 | { |
| 466 | unsigned totrelsz = 0; |
| 467 | Elf_Shdr const *shdrp = shdr0; |
Steven Rostedt | ffd618f | 2011-04-08 03:58:48 -0400 | [diff] [blame] | 468 | char const *txtname; |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 469 | |
| 470 | for (; nhdr; --nhdr, ++shdrp) { |
Steven Rostedt | ffd618f | 2011-04-08 03:58:48 -0400 | [diff] [blame] | 471 | txtname = has_rel_mcount(shdrp, shdr0, shstrtab, fname); |
| 472 | if (txtname && is_mcounted_section_name(txtname)) |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 473 | totrelsz += _w(shdrp->sh_size); |
| 474 | } |
| 475 | return totrelsz; |
| 476 | } |
| 477 | |
| 478 | |
| 479 | /* Overall supervision for Elf32 ET_REL file. */ |
| 480 | static void |
| 481 | do_func(Elf_Ehdr *const ehdr, char const *const fname, unsigned const reltype) |
| 482 | { |
| 483 | Elf_Shdr *const shdr0 = (Elf_Shdr *)(_w(ehdr->e_shoff) |
| 484 | + (void *)ehdr); |
| 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) |
| 488 | + (void *)ehdr); |
| 489 | |
| 490 | Elf_Shdr const *relhdr; |
| 491 | unsigned k; |
| 492 | |
| 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; |
| 497 | |
| 498 | /* 2*sizeof(address) <= sizeof(Elf_Rel) */ |
| 499 | uint_t *const mloc0 = umalloc(totrelsz>>1); |
| 500 | uint_t * mlocp = mloc0; |
| 501 | |
| 502 | unsigned rel_entsize = 0; |
| 503 | unsigned symsec_sh_link = 0; |
| 504 | |
| 505 | for (relhdr = shdr0, k = nhdr; k; --k, ++relhdr) { |
| 506 | char const *const txtname = has_rel_mcount(relhdr, shdr0, |
| 507 | shstrtab, fname); |
Steven Rostedt | ffd618f | 2011-04-08 03:58:48 -0400 | [diff] [blame] | 508 | if (txtname && is_mcounted_section_name(txtname)) { |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 509 | uint_t recval = 0; |
| 510 | unsigned const recsym = find_secsym_ndx( |
| 511 | w(relhdr->sh_info), txtname, &recval, |
| 512 | &shdr0[symsec_sh_link = w(relhdr->sh_link)], |
| 513 | ehdr); |
| 514 | |
| 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); |
Steven Rostedt | dfad3d5 | 2011-04-12 18:53:25 -0400 | [diff] [blame] | 519 | } else if (txtname && (warn_on_notrace_sect || make_nop)) { |
Steven Rostedt | ffd618f | 2011-04-08 03:58:48 -0400 | [diff] [blame] | 520 | /* |
| 521 | * This section is ignored by ftrace, but still |
| 522 | * has mcount calls. Convert them to nops now. |
| 523 | */ |
Steven Rostedt | dfad3d5 | 2011-04-12 18:53:25 -0400 | [diff] [blame] | 524 | nop_mcount(relhdr, ehdr, txtname); |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 525 | } |
| 526 | } |
| 527 | if (mloc0 != mlocp) { |
| 528 | append_func(ehdr, shstr, mloc0, mlocp, mrel0, mrelp, |
| 529 | rel_entsize, symsec_sh_link); |
| 530 | } |
| 531 | free(mrel0); |
| 532 | free(mloc0); |
| 533 | } |