Thomas Gleixner | 4317cf95 | 2019-05-31 01:09:38 -0700 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 2 | /* |
| 3 | * recordmcount.h |
| 4 | * |
| 5 | * This code was taken out of recordmcount.c written by |
| 6 | * Copyright 2009 John F. Reiser <jreiser@BitWagon.com>. All rights reserved. |
| 7 | * |
| 8 | * The original code had the same algorithms for both 32bit |
| 9 | * and 64bit ELF files, but the code was duplicated to support |
| 10 | * the difference in structures that were used. This |
| 11 | * file creates a macro of everything that is different between |
| 12 | * the 64 and 32 bit code, such that by including this header |
| 13 | * twice we can create both sets of functions by including this |
| 14 | * header once with RECORD_MCOUNT_64 undefined, and again with |
| 15 | * it defined. |
| 16 | * |
| 17 | * This conversion to macros was done by: |
| 18 | * Copyright 2010 Steven Rostedt <srostedt@redhat.com>, Red Hat Inc. |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 19 | */ |
| 20 | #undef append_func |
Wu Zhangjin | 412910c | 2010-10-27 18:59:08 +0800 | [diff] [blame] | 21 | #undef is_fake_mcount |
| 22 | #undef fn_is_fake_mcount |
| 23 | #undef MIPS_is_fake_mcount |
Martin Schwidefsky | 07d8b59 | 2011-05-10 10:10:40 +0200 | [diff] [blame] | 24 | #undef mcount_adjust |
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 | 41b402a | 2011-04-20 21:13:06 -0400 | [diff] [blame] | 32 | #undef get_sym_str_and_relp |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 33 | #undef do_func |
Wu Zhangjin | 412910c | 2010-10-27 18:59:08 +0800 | [diff] [blame] | 34 | #undef Elf_Addr |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 35 | #undef Elf_Ehdr |
| 36 | #undef Elf_Shdr |
| 37 | #undef Elf_Rel |
| 38 | #undef Elf_Rela |
| 39 | #undef Elf_Sym |
| 40 | #undef ELF_R_SYM |
John Reiser | a2d49358 | 2010-10-27 18:59:07 +0800 | [diff] [blame] | 41 | #undef Elf_r_sym |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 42 | #undef ELF_R_INFO |
John Reiser | a2d49358 | 2010-10-27 18:59:07 +0800 | [diff] [blame] | 43 | #undef Elf_r_info |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 44 | #undef ELF_ST_BIND |
Rabin Vincent | 9905ce8 | 2011-05-11 22:53:51 +0530 | [diff] [blame] | 45 | #undef ELF_ST_TYPE |
John Reiser | a2d49358 | 2010-10-27 18:59:07 +0800 | [diff] [blame] | 46 | #undef fn_ELF_R_SYM |
| 47 | #undef fn_ELF_R_INFO |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 48 | #undef uint_t |
| 49 | #undef _w |
| 50 | #undef _align |
| 51 | #undef _size |
| 52 | |
| 53 | #ifdef RECORD_MCOUNT_64 |
| 54 | # define append_func append64 |
| 55 | # define sift_rel_mcount sift64_rel_mcount |
Steven Rostedt | ffd618f | 2011-04-08 03:58:48 -0400 | [diff] [blame] | 56 | # define nop_mcount nop_mcount_64 |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 57 | # define find_secsym_ndx find64_secsym_ndx |
| 58 | # define __has_rel_mcount __has64_rel_mcount |
| 59 | # define has_rel_mcount has64_rel_mcount |
| 60 | # define tot_relsize tot64_relsize |
Steven Rostedt | 41b402a | 2011-04-20 21:13:06 -0400 | [diff] [blame] | 61 | # define get_sym_str_and_relp get_sym_str_and_relp_64 |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 62 | # define do_func do64 |
Steven Rostedt | 37762cb | 2011-04-20 20:47:34 -0400 | [diff] [blame] | 63 | # define get_mcountsym get_mcountsym_64 |
Wu Zhangjin | 412910c | 2010-10-27 18:59:08 +0800 | [diff] [blame] | 64 | # define is_fake_mcount is_fake_mcount64 |
| 65 | # define fn_is_fake_mcount fn_is_fake_mcount64 |
| 66 | # define MIPS_is_fake_mcount MIPS64_is_fake_mcount |
Martin Schwidefsky | 07d8b59 | 2011-05-10 10:10:40 +0200 | [diff] [blame] | 67 | # define mcount_adjust mcount_adjust_64 |
Wu Zhangjin | 412910c | 2010-10-27 18:59:08 +0800 | [diff] [blame] | 68 | # define Elf_Addr Elf64_Addr |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 69 | # define Elf_Ehdr Elf64_Ehdr |
| 70 | # define Elf_Shdr Elf64_Shdr |
| 71 | # define Elf_Rel Elf64_Rel |
| 72 | # define Elf_Rela Elf64_Rela |
| 73 | # define Elf_Sym Elf64_Sym |
| 74 | # define ELF_R_SYM ELF64_R_SYM |
John Reiser | a2d49358 | 2010-10-27 18:59:07 +0800 | [diff] [blame] | 75 | # define Elf_r_sym Elf64_r_sym |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 76 | # define ELF_R_INFO ELF64_R_INFO |
John Reiser | a2d49358 | 2010-10-27 18:59:07 +0800 | [diff] [blame] | 77 | # define Elf_r_info Elf64_r_info |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 78 | # define ELF_ST_BIND ELF64_ST_BIND |
Rabin Vincent | 9905ce8 | 2011-05-11 22:53:51 +0530 | [diff] [blame] | 79 | # define ELF_ST_TYPE ELF64_ST_TYPE |
John Reiser | a2d49358 | 2010-10-27 18:59:07 +0800 | [diff] [blame] | 80 | # define fn_ELF_R_SYM fn_ELF64_R_SYM |
| 81 | # define fn_ELF_R_INFO fn_ELF64_R_INFO |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 82 | # define uint_t uint64_t |
| 83 | # define _w w8 |
| 84 | # define _align 7u |
| 85 | # define _size 8 |
| 86 | #else |
| 87 | # define append_func append32 |
| 88 | # define sift_rel_mcount sift32_rel_mcount |
Steven Rostedt | ffd618f | 2011-04-08 03:58:48 -0400 | [diff] [blame] | 89 | # define nop_mcount nop_mcount_32 |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 90 | # define find_secsym_ndx find32_secsym_ndx |
| 91 | # define __has_rel_mcount __has32_rel_mcount |
| 92 | # define has_rel_mcount has32_rel_mcount |
| 93 | # define tot_relsize tot32_relsize |
Steven Rostedt | 41b402a | 2011-04-20 21:13:06 -0400 | [diff] [blame] | 94 | # define get_sym_str_and_relp get_sym_str_and_relp_32 |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 95 | # define do_func do32 |
Steven Rostedt | 37762cb | 2011-04-20 20:47:34 -0400 | [diff] [blame] | 96 | # define get_mcountsym get_mcountsym_32 |
Wu Zhangjin | 412910c | 2010-10-27 18:59:08 +0800 | [diff] [blame] | 97 | # define is_fake_mcount is_fake_mcount32 |
| 98 | # define fn_is_fake_mcount fn_is_fake_mcount32 |
| 99 | # define MIPS_is_fake_mcount MIPS32_is_fake_mcount |
Martin Schwidefsky | 07d8b59 | 2011-05-10 10:10:40 +0200 | [diff] [blame] | 100 | # define mcount_adjust mcount_adjust_32 |
Wu Zhangjin | 412910c | 2010-10-27 18:59:08 +0800 | [diff] [blame] | 101 | # define Elf_Addr Elf32_Addr |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 102 | # define Elf_Ehdr Elf32_Ehdr |
| 103 | # define Elf_Shdr Elf32_Shdr |
| 104 | # define Elf_Rel Elf32_Rel |
| 105 | # define Elf_Rela Elf32_Rela |
| 106 | # define Elf_Sym Elf32_Sym |
| 107 | # define ELF_R_SYM ELF32_R_SYM |
John Reiser | a2d49358 | 2010-10-27 18:59:07 +0800 | [diff] [blame] | 108 | # define Elf_r_sym Elf32_r_sym |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 109 | # define ELF_R_INFO ELF32_R_INFO |
John Reiser | a2d49358 | 2010-10-27 18:59:07 +0800 | [diff] [blame] | 110 | # define Elf_r_info Elf32_r_info |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 111 | # define ELF_ST_BIND ELF32_ST_BIND |
Rabin Vincent | 9905ce8 | 2011-05-11 22:53:51 +0530 | [diff] [blame] | 112 | # define ELF_ST_TYPE ELF32_ST_TYPE |
John Reiser | a2d49358 | 2010-10-27 18:59:07 +0800 | [diff] [blame] | 113 | # define fn_ELF_R_SYM fn_ELF32_R_SYM |
| 114 | # define fn_ELF_R_INFO fn_ELF32_R_INFO |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 115 | # define uint_t uint32_t |
| 116 | # define _w w |
| 117 | # define _align 3u |
| 118 | # define _size 4 |
| 119 | #endif |
| 120 | |
Wu Zhangjin | 412910c | 2010-10-27 18:59:08 +0800 | [diff] [blame] | 121 | /* Functions and pointers that do_file() may override for specific e_machine. */ |
| 122 | static int fn_is_fake_mcount(Elf_Rel const *rp) |
| 123 | { |
| 124 | return 0; |
| 125 | } |
| 126 | static int (*is_fake_mcount)(Elf_Rel const *rp) = fn_is_fake_mcount; |
| 127 | |
John Reiser | a2d49358 | 2010-10-27 18:59:07 +0800 | [diff] [blame] | 128 | static uint_t fn_ELF_R_SYM(Elf_Rel const *rp) |
| 129 | { |
| 130 | return ELF_R_SYM(_w(rp->r_info)); |
| 131 | } |
| 132 | static uint_t (*Elf_r_sym)(Elf_Rel const *rp) = fn_ELF_R_SYM; |
| 133 | |
| 134 | static void fn_ELF_R_INFO(Elf_Rel *const rp, unsigned sym, unsigned type) |
| 135 | { |
John Reiser | e63233f7 | 2010-11-22 19:41:44 -0800 | [diff] [blame] | 136 | rp->r_info = _w(ELF_R_INFO(sym, type)); |
John Reiser | a2d49358 | 2010-10-27 18:59:07 +0800 | [diff] [blame] | 137 | } |
| 138 | static void (*Elf_r_info)(Elf_Rel *const rp, unsigned sym, unsigned type) = fn_ELF_R_INFO; |
| 139 | |
Martin Schwidefsky | 07d8b59 | 2011-05-10 10:10:40 +0200 | [diff] [blame] | 140 | static int mcount_adjust = 0; |
| 141 | |
Wu Zhangjin | 412910c | 2010-10-27 18:59:08 +0800 | [diff] [blame] | 142 | /* |
| 143 | * MIPS mcount long call has 2 _mcount symbols, only the position of the 1st |
| 144 | * _mcount symbol is needed for dynamic function tracer, with it, to disable |
| 145 | * tracing(ftrace_make_nop), the instruction in the position is replaced with |
| 146 | * the "b label" instruction, to enable tracing(ftrace_make_call), replace the |
| 147 | * instruction back. So, here, we set the 2nd one as fake and filter it. |
| 148 | * |
| 149 | * c: 3c030000 lui v1,0x0 <--> b label |
| 150 | * c: R_MIPS_HI16 _mcount |
| 151 | * c: R_MIPS_NONE *ABS* |
| 152 | * c: R_MIPS_NONE *ABS* |
| 153 | * 10: 64630000 daddiu v1,v1,0 |
| 154 | * 10: R_MIPS_LO16 _mcount |
| 155 | * 10: R_MIPS_NONE *ABS* |
| 156 | * 10: R_MIPS_NONE *ABS* |
| 157 | * 14: 03e0082d move at,ra |
| 158 | * 18: 0060f809 jalr v1 |
| 159 | * label: |
| 160 | */ |
| 161 | #define MIPS_FAKEMCOUNT_OFFSET 4 |
| 162 | |
| 163 | static int MIPS_is_fake_mcount(Elf_Rel const *rp) |
| 164 | { |
Alex Smith | 91ad11d | 2014-06-17 10:39:53 +0100 | [diff] [blame] | 165 | static Elf_Addr old_r_offset = ~(Elf_Addr)0; |
Wu Zhangjin | 412910c | 2010-10-27 18:59:08 +0800 | [diff] [blame] | 166 | Elf_Addr current_r_offset = _w(rp->r_offset); |
| 167 | int is_fake; |
| 168 | |
Alex Smith | 91ad11d | 2014-06-17 10:39:53 +0100 | [diff] [blame] | 169 | is_fake = (old_r_offset != ~(Elf_Addr)0) && |
Wu Zhangjin | 412910c | 2010-10-27 18:59:08 +0800 | [diff] [blame] | 170 | (current_r_offset - old_r_offset == MIPS_FAKEMCOUNT_OFFSET); |
| 171 | old_r_offset = current_r_offset; |
| 172 | |
| 173 | return is_fake; |
| 174 | } |
John Reiser | a2d49358 | 2010-10-27 18:59:07 +0800 | [diff] [blame] | 175 | |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 176 | /* Append the new shstrtab, Elf_Shdr[], __mcount_loc and its relocations. */ |
Matt Helsley | 3f1df12 | 2019-07-31 11:24:12 -0700 | [diff] [blame] | 177 | static int append_func(Elf_Ehdr *const ehdr, |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 178 | Elf_Shdr *const shstr, |
| 179 | uint_t const *const mloc0, |
| 180 | uint_t const *const mlocp, |
| 181 | Elf_Rel const *const mrel0, |
| 182 | Elf_Rel const *const mrelp, |
| 183 | unsigned int const rel_entsize, |
| 184 | unsigned int const symsec_sh_link) |
| 185 | { |
| 186 | /* Begin constructing output file */ |
| 187 | Elf_Shdr mcsec; |
| 188 | char const *mc_name = (sizeof(Elf_Rela) == rel_entsize) |
| 189 | ? ".rela__mcount_loc" |
| 190 | : ".rel__mcount_loc"; |
| 191 | unsigned const old_shnum = w2(ehdr->e_shnum); |
| 192 | uint_t const old_shoff = _w(ehdr->e_shoff); |
| 193 | uint_t const old_shstr_sh_size = _w(shstr->sh_size); |
| 194 | uint_t const old_shstr_sh_offset = _w(shstr->sh_offset); |
| 195 | uint_t t = 1 + strlen(mc_name) + _w(shstr->sh_size); |
| 196 | uint_t new_e_shoff; |
| 197 | |
| 198 | shstr->sh_size = _w(t); |
| 199 | shstr->sh_offset = _w(sb.st_size); |
| 200 | t += sb.st_size; |
| 201 | t += (_align & -t); /* word-byte align */ |
| 202 | new_e_shoff = t; |
| 203 | |
| 204 | /* body for new shstrtab */ |
Matt Helsley | 3f1df12 | 2019-07-31 11:24:12 -0700 | [diff] [blame] | 205 | if (ulseek(sb.st_size, SEEK_SET) < 0) |
| 206 | return -1; |
| 207 | if (uwrite(old_shstr_sh_offset + (void *)ehdr, old_shstr_sh_size) < 0) |
| 208 | return -1; |
| 209 | if (uwrite(mc_name, 1 + strlen(mc_name)) < 0) |
| 210 | return -1; |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 211 | |
| 212 | /* old(modified) Elf_Shdr table, word-byte aligned */ |
Matt Helsley | 3f1df12 | 2019-07-31 11:24:12 -0700 | [diff] [blame] | 213 | if (ulseek(t, SEEK_SET) < 0) |
| 214 | return -1; |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 215 | t += sizeof(Elf_Shdr) * old_shnum; |
Matt Helsley | 3f1df12 | 2019-07-31 11:24:12 -0700 | [diff] [blame] | 216 | if (uwrite(old_shoff + (void *)ehdr, |
| 217 | sizeof(Elf_Shdr) * old_shnum) < 0) |
| 218 | return -1; |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 219 | |
| 220 | /* new sections __mcount_loc and .rel__mcount_loc */ |
| 221 | t += 2*sizeof(mcsec); |
| 222 | mcsec.sh_name = w((sizeof(Elf_Rela) == rel_entsize) + strlen(".rel") |
| 223 | + old_shstr_sh_size); |
| 224 | mcsec.sh_type = w(SHT_PROGBITS); |
| 225 | mcsec.sh_flags = _w(SHF_ALLOC); |
| 226 | mcsec.sh_addr = 0; |
| 227 | mcsec.sh_offset = _w(t); |
| 228 | mcsec.sh_size = _w((void *)mlocp - (void *)mloc0); |
| 229 | mcsec.sh_link = 0; |
| 230 | mcsec.sh_info = 0; |
| 231 | mcsec.sh_addralign = _w(_size); |
| 232 | mcsec.sh_entsize = _w(_size); |
Matt Helsley | 3f1df12 | 2019-07-31 11:24:12 -0700 | [diff] [blame] | 233 | if (uwrite(&mcsec, sizeof(mcsec)) < 0) |
| 234 | return -1; |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 235 | |
| 236 | mcsec.sh_name = w(old_shstr_sh_size); |
| 237 | mcsec.sh_type = (sizeof(Elf_Rela) == rel_entsize) |
| 238 | ? w(SHT_RELA) |
| 239 | : w(SHT_REL); |
| 240 | mcsec.sh_flags = 0; |
| 241 | mcsec.sh_addr = 0; |
| 242 | mcsec.sh_offset = _w((void *)mlocp - (void *)mloc0 + t); |
| 243 | mcsec.sh_size = _w((void *)mrelp - (void *)mrel0); |
| 244 | mcsec.sh_link = w(symsec_sh_link); |
| 245 | mcsec.sh_info = w(old_shnum); |
| 246 | mcsec.sh_addralign = _w(_size); |
| 247 | mcsec.sh_entsize = _w(rel_entsize); |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 248 | |
Matt Helsley | 3f1df12 | 2019-07-31 11:24:12 -0700 | [diff] [blame] | 249 | if (uwrite(&mcsec, sizeof(mcsec)) < 0) |
| 250 | return -1; |
| 251 | |
| 252 | if (uwrite(mloc0, (void *)mlocp - (void *)mloc0) < 0) |
| 253 | return -1; |
| 254 | if (uwrite(mrel0, (void *)mrelp - (void *)mrel0) < 0) |
| 255 | return -1; |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 256 | |
| 257 | ehdr->e_shoff = _w(new_e_shoff); |
| 258 | ehdr->e_shnum = w2(2 + w2(ehdr->e_shnum)); /* {.rel,}__mcount_loc */ |
Matt Helsley | 3f1df12 | 2019-07-31 11:24:12 -0700 | [diff] [blame] | 259 | if (ulseek(0, SEEK_SET) < 0) |
| 260 | return -1; |
| 261 | if (uwrite(ehdr, sizeof(*ehdr)) < 0) |
| 262 | return -1; |
| 263 | return 0; |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 264 | } |
| 265 | |
Steven Rostedt | 37762cb | 2011-04-20 20:47:34 -0400 | [diff] [blame] | 266 | static unsigned get_mcountsym(Elf_Sym const *const sym0, |
| 267 | Elf_Rel const *relp, |
| 268 | char const *const str0) |
| 269 | { |
| 270 | unsigned mcountsym = 0; |
| 271 | |
| 272 | Elf_Sym const *const symp = |
| 273 | &sym0[Elf_r_sym(relp)]; |
| 274 | char const *symname = &str0[w(symp->st_name)]; |
| 275 | char const *mcount = gpfx == '_' ? "_mcount" : "mcount"; |
Steven Rostedt | 48bb5dc | 2011-02-09 13:13:23 -0500 | [diff] [blame] | 276 | char const *fentry = "__fentry__"; |
Steven Rostedt | 37762cb | 2011-04-20 20:47:34 -0400 | [diff] [blame] | 277 | |
| 278 | if (symname[0] == '.') |
| 279 | ++symname; /* ppc64 hack */ |
| 280 | if (strcmp(mcount, symname) == 0 || |
Steven Rostedt | 48bb5dc | 2011-02-09 13:13:23 -0500 | [diff] [blame] | 281 | (altmcount && strcmp(altmcount, symname) == 0) || |
| 282 | (strcmp(fentry, symname) == 0)) |
Steven Rostedt | 37762cb | 2011-04-20 20:47:34 -0400 | [diff] [blame] | 283 | mcountsym = Elf_r_sym(relp); |
| 284 | |
| 285 | return mcountsym; |
| 286 | } |
| 287 | |
Steven Rostedt | 41b402a | 2011-04-20 21:13:06 -0400 | [diff] [blame] | 288 | static void get_sym_str_and_relp(Elf_Shdr const *const relhdr, |
| 289 | Elf_Ehdr const *const ehdr, |
| 290 | Elf_Sym const **sym0, |
| 291 | char const **str0, |
| 292 | Elf_Rel const **relp) |
| 293 | { |
| 294 | Elf_Shdr *const shdr0 = (Elf_Shdr *)(_w(ehdr->e_shoff) |
| 295 | + (void *)ehdr); |
| 296 | unsigned const symsec_sh_link = w(relhdr->sh_link); |
| 297 | Elf_Shdr const *const symsec = &shdr0[symsec_sh_link]; |
| 298 | Elf_Shdr const *const strsec = &shdr0[w(symsec->sh_link)]; |
| 299 | Elf_Rel const *const rel0 = (Elf_Rel const *)(_w(relhdr->sh_offset) |
| 300 | + (void *)ehdr); |
| 301 | |
| 302 | *sym0 = (Elf_Sym const *)(_w(symsec->sh_offset) |
| 303 | + (void *)ehdr); |
| 304 | |
| 305 | *str0 = (char const *)(_w(strsec->sh_offset) |
| 306 | + (void *)ehdr); |
| 307 | |
| 308 | *relp = rel0; |
| 309 | } |
| 310 | |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 311 | /* |
| 312 | * Look at the relocations in order to find the calls to mcount. |
| 313 | * Accumulate the section offsets that are found, and their relocation info, |
| 314 | * onto the end of the existing arrays. |
| 315 | */ |
| 316 | static uint_t *sift_rel_mcount(uint_t *mlocp, |
| 317 | unsigned const offbase, |
| 318 | Elf_Rel **const mrelpp, |
| 319 | Elf_Shdr const *const relhdr, |
| 320 | Elf_Ehdr const *const ehdr, |
| 321 | unsigned const recsym, |
| 322 | uint_t const recval, |
| 323 | unsigned const reltype) |
| 324 | { |
| 325 | uint_t *const mloc0 = mlocp; |
| 326 | Elf_Rel *mrelp = *mrelpp; |
Steven Rostedt | 41b402a | 2011-04-20 21:13:06 -0400 | [diff] [blame] | 327 | Elf_Sym const *sym0; |
| 328 | char const *str0; |
| 329 | Elf_Rel const *relp; |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 330 | unsigned rel_entsize = _w(relhdr->sh_entsize); |
| 331 | unsigned const nrel = _w(relhdr->sh_size) / rel_entsize; |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 332 | unsigned mcountsym = 0; |
| 333 | unsigned t; |
| 334 | |
Steven Rostedt | 41b402a | 2011-04-20 21:13:06 -0400 | [diff] [blame] | 335 | get_sym_str_and_relp(relhdr, ehdr, &sym0, &str0, &relp); |
| 336 | |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 337 | for (t = nrel; t; --t) { |
Steven Rostedt | 37762cb | 2011-04-20 20:47:34 -0400 | [diff] [blame] | 338 | if (!mcountsym) |
| 339 | mcountsym = get_mcountsym(sym0, relp, str0); |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 340 | |
Naveen N. Rao | 80e5302 | 2019-06-27 00:08:01 +0530 | [diff] [blame] | 341 | if (mcountsym && mcountsym == Elf_r_sym(relp) && |
| 342 | !is_fake_mcount(relp)) { |
Martin Schwidefsky | 07d8b59 | 2011-05-10 10:10:40 +0200 | [diff] [blame] | 343 | uint_t const addend = |
| 344 | _w(_w(relp->r_offset) - recval + mcount_adjust); |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 345 | mrelp->r_offset = _w(offbase |
| 346 | + ((void *)mlocp - (void *)mloc0)); |
John Reiser | a2d49358 | 2010-10-27 18:59:07 +0800 | [diff] [blame] | 347 | Elf_r_info(mrelp, recsym, reltype); |
Steven Rostedt | dd5477f | 2011-04-06 13:21:17 -0400 | [diff] [blame] | 348 | if (rel_entsize == sizeof(Elf_Rela)) { |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 349 | ((Elf_Rela *)mrelp)->r_addend = addend; |
| 350 | *mlocp++ = 0; |
| 351 | } else |
| 352 | *mlocp++ = addend; |
| 353 | |
| 354 | mrelp = (Elf_Rel *)(rel_entsize + (void *)mrelp); |
| 355 | } |
| 356 | relp = (Elf_Rel const *)(rel_entsize + (void *)relp); |
| 357 | } |
| 358 | *mrelpp = mrelp; |
| 359 | return mlocp; |
| 360 | } |
| 361 | |
Steven Rostedt | ffd618f | 2011-04-08 03:58:48 -0400 | [diff] [blame] | 362 | /* |
| 363 | * Read the relocation table again, but this time its called on sections |
| 364 | * that are not going to be traced. The mcount calls here will be converted |
| 365 | * into nops. |
| 366 | */ |
Matt Helsley | 3f1df12 | 2019-07-31 11:24:12 -0700 | [diff] [blame] | 367 | static int nop_mcount(Elf_Shdr const *const relhdr, |
| 368 | Elf_Ehdr const *const ehdr, |
| 369 | const char *const txtname) |
Steven Rostedt | ffd618f | 2011-04-08 03:58:48 -0400 | [diff] [blame] | 370 | { |
| 371 | Elf_Shdr *const shdr0 = (Elf_Shdr *)(_w(ehdr->e_shoff) |
| 372 | + (void *)ehdr); |
Steven Rostedt | 41b402a | 2011-04-20 21:13:06 -0400 | [diff] [blame] | 373 | Elf_Sym const *sym0; |
| 374 | char const *str0; |
| 375 | Elf_Rel const *relp; |
| 376 | Elf_Shdr const *const shdr = &shdr0[w(relhdr->sh_info)]; |
Steven Rostedt | ffd618f | 2011-04-08 03:58:48 -0400 | [diff] [blame] | 377 | unsigned rel_entsize = _w(relhdr->sh_entsize); |
| 378 | unsigned const nrel = _w(relhdr->sh_size) / rel_entsize; |
Steven Rostedt | ffd618f | 2011-04-08 03:58:48 -0400 | [diff] [blame] | 379 | unsigned mcountsym = 0; |
| 380 | unsigned t; |
Steven Rostedt | dfad3d5 | 2011-04-12 18:53:25 -0400 | [diff] [blame] | 381 | int once = 0; |
Steven Rostedt | ffd618f | 2011-04-08 03:58:48 -0400 | [diff] [blame] | 382 | |
Steven Rostedt | 41b402a | 2011-04-20 21:13:06 -0400 | [diff] [blame] | 383 | get_sym_str_and_relp(relhdr, ehdr, &sym0, &str0, &relp); |
| 384 | |
Steven Rostedt | ffd618f | 2011-04-08 03:58:48 -0400 | [diff] [blame] | 385 | for (t = nrel; t; --t) { |
| 386 | int ret = -1; |
| 387 | |
Steven Rostedt | 37762cb | 2011-04-20 20:47:34 -0400 | [diff] [blame] | 388 | if (!mcountsym) |
| 389 | mcountsym = get_mcountsym(sym0, relp, str0); |
Steven Rostedt | ffd618f | 2011-04-08 03:58:48 -0400 | [diff] [blame] | 390 | |
Steven Rostedt | dfad3d5 | 2011-04-12 18:53:25 -0400 | [diff] [blame] | 391 | if (mcountsym == Elf_r_sym(relp) && !is_fake_mcount(relp)) { |
Matt Helsley | 3f1df12 | 2019-07-31 11:24:12 -0700 | [diff] [blame] | 392 | if (make_nop) { |
libin | c84da8b | 2015-11-03 08:58:47 +0800 | [diff] [blame] | 393 | ret = make_nop((void *)ehdr, _w(shdr->sh_offset) + _w(relp->r_offset)); |
Matt Helsley | 3f1df12 | 2019-07-31 11:24:12 -0700 | [diff] [blame] | 394 | if (ret < 0) |
| 395 | return -1; |
| 396 | } |
Steven Rostedt | dfad3d5 | 2011-04-12 18:53:25 -0400 | [diff] [blame] | 397 | if (warn_on_notrace_sect && !once) { |
| 398 | printf("Section %s has mcount callers being ignored\n", |
| 399 | txtname); |
| 400 | once = 1; |
| 401 | /* just warn? */ |
| 402 | if (!make_nop) |
Matt Helsley | 3f1df12 | 2019-07-31 11:24:12 -0700 | [diff] [blame] | 403 | return 0; |
Steven Rostedt | dfad3d5 | 2011-04-12 18:53:25 -0400 | [diff] [blame] | 404 | } |
| 405 | } |
Steven Rostedt | ffd618f | 2011-04-08 03:58:48 -0400 | [diff] [blame] | 406 | |
| 407 | /* |
| 408 | * If we successfully removed the mcount, mark the relocation |
| 409 | * as a nop (don't do anything with it). |
| 410 | */ |
| 411 | if (!ret) { |
| 412 | Elf_Rel rel; |
| 413 | rel = *(Elf_Rel *)relp; |
| 414 | Elf_r_info(&rel, Elf_r_sym(relp), rel_type_nop); |
Matt Helsley | 3f1df12 | 2019-07-31 11:24:12 -0700 | [diff] [blame] | 415 | if (ulseek((void *)relp - (void *)ehdr, SEEK_SET) < 0) |
| 416 | return -1; |
| 417 | if (uwrite(&rel, sizeof(rel)) < 0) |
| 418 | return -1; |
Steven Rostedt | ffd618f | 2011-04-08 03:58:48 -0400 | [diff] [blame] | 419 | } |
| 420 | relp = (Elf_Rel const *)(rel_entsize + (void *)relp); |
| 421 | } |
Matt Helsley | 3f1df12 | 2019-07-31 11:24:12 -0700 | [diff] [blame] | 422 | return 0; |
Steven Rostedt | ffd618f | 2011-04-08 03:58:48 -0400 | [diff] [blame] | 423 | } |
| 424 | |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 425 | /* |
| 426 | * Find a symbol in the given section, to be used as the base for relocating |
| 427 | * the table of offsets of calls to mcount. A local or global symbol suffices, |
| 428 | * but avoid a Weak symbol because it may be overridden; the change in value |
| 429 | * would invalidate the relocations of the offsets of the calls to mcount. |
| 430 | * Often the found symbol will be the unnamed local symbol generated by |
| 431 | * GNU 'as' for the start of each section. For example: |
| 432 | * Num: Value Size Type Bind Vis Ndx Name |
| 433 | * 2: 00000000 0 SECTION LOCAL DEFAULT 1 |
| 434 | */ |
Matt Helsley | 3f1df12 | 2019-07-31 11:24:12 -0700 | [diff] [blame] | 435 | static int find_secsym_ndx(unsigned const txtndx, |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 436 | char const *const txtname, |
| 437 | uint_t *const recvalp, |
Matt Helsley | 3f1df12 | 2019-07-31 11:24:12 -0700 | [diff] [blame] | 438 | unsigned int *sym_index, |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 439 | Elf_Shdr const *const symhdr, |
| 440 | Elf_Ehdr const *const ehdr) |
| 441 | { |
| 442 | Elf_Sym const *const sym0 = (Elf_Sym const *)(_w(symhdr->sh_offset) |
| 443 | + (void *)ehdr); |
| 444 | unsigned const nsym = _w(symhdr->sh_size) / _w(symhdr->sh_entsize); |
| 445 | Elf_Sym const *symp; |
| 446 | unsigned t; |
| 447 | |
| 448 | for (symp = sym0, t = nsym; t; --t, ++symp) { |
| 449 | unsigned int const st_bind = ELF_ST_BIND(symp->st_info); |
| 450 | |
| 451 | if (txtndx == w2(symp->st_shndx) |
| 452 | /* avoid STB_WEAK */ |
| 453 | && (STB_LOCAL == st_bind || STB_GLOBAL == st_bind)) { |
Rabin Vincent | 9905ce8 | 2011-05-11 22:53:51 +0530 | [diff] [blame] | 454 | /* function symbols on ARM have quirks, avoid them */ |
| 455 | if (w2(ehdr->e_machine) == EM_ARM |
| 456 | && ELF_ST_TYPE(symp->st_info) == STT_FUNC) |
| 457 | continue; |
| 458 | |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 459 | *recvalp = _w(symp->st_value); |
Matt Helsley | 3f1df12 | 2019-07-31 11:24:12 -0700 | [diff] [blame] | 460 | *sym_index = symp - sym0; |
| 461 | return 0; |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 462 | } |
| 463 | } |
nixiaoming | ac5db1f | 2018-05-24 11:16:12 +0800 | [diff] [blame] | 464 | fprintf(stderr, "Cannot find symbol for section %u: %s.\n", |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 465 | txtndx, txtname); |
Matt Helsley | 3f1df12 | 2019-07-31 11:24:12 -0700 | [diff] [blame] | 466 | cleanup(); |
| 467 | return -1; |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 468 | } |
| 469 | |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 470 | /* Evade ISO C restriction: no declaration after statement in has_rel_mcount. */ |
Matt Helsley | 3aec863 | 2019-07-31 11:24:13 -0700 | [diff] [blame^] | 471 | static char const * __has_rel_mcount(Elf_Shdr const *const relhdr, /* reltype */ |
| 472 | Elf_Shdr const *const shdr0, |
| 473 | char const *const shstrtab, |
| 474 | char const *const fname) |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 475 | { |
| 476 | /* .sh_info depends on .sh_type == SHT_REL[,A] */ |
| 477 | Elf_Shdr const *const txthdr = &shdr0[w(relhdr->sh_info)]; |
| 478 | char const *const txtname = &shstrtab[w(txthdr->sh_name)]; |
| 479 | |
Steven Rostedt | dd5477f | 2011-04-06 13:21:17 -0400 | [diff] [blame] | 480 | if (strcmp("__mcount_loc", txtname) == 0) { |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 481 | fprintf(stderr, "warning: __mcount_loc already exists: %s\n", |
| 482 | fname); |
Matt Helsley | 3f1df12 | 2019-07-31 11:24:12 -0700 | [diff] [blame] | 483 | cleanup(); |
| 484 | return already_has_rel_mcount; |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 485 | } |
Steven Rostedt | dd5477f | 2011-04-06 13:21:17 -0400 | [diff] [blame] | 486 | if (w(txthdr->sh_type) != SHT_PROGBITS || |
David Daney | 2e88505 | 2011-12-19 17:42:42 -0800 | [diff] [blame] | 487 | !(_w(txthdr->sh_flags) & SHF_EXECINSTR)) |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 488 | return NULL; |
| 489 | return txtname; |
| 490 | } |
| 491 | |
| 492 | static char const *has_rel_mcount(Elf_Shdr const *const relhdr, |
| 493 | Elf_Shdr const *const shdr0, |
| 494 | char const *const shstrtab, |
| 495 | char const *const fname) |
| 496 | { |
Steven Rostedt | dd5477f | 2011-04-06 13:21:17 -0400 | [diff] [blame] | 497 | 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] | 498 | return NULL; |
| 499 | return __has_rel_mcount(relhdr, shdr0, shstrtab, fname); |
| 500 | } |
| 501 | |
| 502 | |
| 503 | static unsigned tot_relsize(Elf_Shdr const *const shdr0, |
| 504 | unsigned nhdr, |
| 505 | const char *const shstrtab, |
| 506 | const char *const fname) |
| 507 | { |
| 508 | unsigned totrelsz = 0; |
| 509 | Elf_Shdr const *shdrp = shdr0; |
Steven Rostedt | ffd618f | 2011-04-08 03:58:48 -0400 | [diff] [blame] | 510 | char const *txtname; |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 511 | |
| 512 | for (; nhdr; --nhdr, ++shdrp) { |
Steven Rostedt | ffd618f | 2011-04-08 03:58:48 -0400 | [diff] [blame] | 513 | txtname = has_rel_mcount(shdrp, shdr0, shstrtab, fname); |
Matt Helsley | 3f1df12 | 2019-07-31 11:24:12 -0700 | [diff] [blame] | 514 | if (txtname == already_has_rel_mcount) { |
| 515 | totrelsz = 0; |
| 516 | break; |
| 517 | } |
Steven Rostedt | ffd618f | 2011-04-08 03:58:48 -0400 | [diff] [blame] | 518 | if (txtname && is_mcounted_section_name(txtname)) |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 519 | totrelsz += _w(shdrp->sh_size); |
| 520 | } |
| 521 | return totrelsz; |
| 522 | } |
| 523 | |
| 524 | |
| 525 | /* Overall supervision for Elf32 ET_REL file. */ |
Matt Helsley | 3aec863 | 2019-07-31 11:24:13 -0700 | [diff] [blame^] | 526 | static int do_func(Elf_Ehdr *const ehdr, char const *const fname, |
| 527 | unsigned const reltype) |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 528 | { |
| 529 | Elf_Shdr *const shdr0 = (Elf_Shdr *)(_w(ehdr->e_shoff) |
| 530 | + (void *)ehdr); |
| 531 | unsigned const nhdr = w2(ehdr->e_shnum); |
| 532 | Elf_Shdr *const shstr = &shdr0[w2(ehdr->e_shstrndx)]; |
| 533 | char const *const shstrtab = (char const *)(_w(shstr->sh_offset) |
| 534 | + (void *)ehdr); |
| 535 | |
| 536 | Elf_Shdr const *relhdr; |
| 537 | unsigned k; |
| 538 | |
| 539 | /* Upper bound on space: assume all relevant relocs are for mcount. */ |
Matt Helsley | 3f1df12 | 2019-07-31 11:24:12 -0700 | [diff] [blame] | 540 | unsigned totrelsz; |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 541 | |
Matt Helsley | 3f1df12 | 2019-07-31 11:24:12 -0700 | [diff] [blame] | 542 | Elf_Rel * mrel0; |
| 543 | Elf_Rel * mrelp; |
| 544 | |
| 545 | uint_t * mloc0; |
| 546 | uint_t * mlocp; |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 547 | |
| 548 | unsigned rel_entsize = 0; |
| 549 | unsigned symsec_sh_link = 0; |
| 550 | |
Matt Helsley | 3f1df12 | 2019-07-31 11:24:12 -0700 | [diff] [blame] | 551 | int result = 0; |
| 552 | |
| 553 | totrelsz = tot_relsize(shdr0, nhdr, shstrtab, fname); |
| 554 | if (totrelsz == 0) |
| 555 | return 0; |
| 556 | mrel0 = umalloc(totrelsz); |
| 557 | mrelp = mrel0; |
| 558 | if (!mrel0) |
| 559 | return -1; |
| 560 | |
| 561 | /* 2*sizeof(address) <= sizeof(Elf_Rel) */ |
| 562 | mloc0 = umalloc(totrelsz>>1); |
| 563 | mlocp = mloc0; |
| 564 | if (!mloc0) { |
| 565 | free(mrel0); |
| 566 | return -1; |
| 567 | } |
| 568 | |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 569 | for (relhdr = shdr0, k = nhdr; k; --k, ++relhdr) { |
| 570 | char const *const txtname = has_rel_mcount(relhdr, shdr0, |
| 571 | shstrtab, fname); |
Matt Helsley | 3f1df12 | 2019-07-31 11:24:12 -0700 | [diff] [blame] | 572 | if (txtname == already_has_rel_mcount) { |
| 573 | result = 0; |
| 574 | file_updated = 0; |
| 575 | goto out; /* Nothing to be done; don't append! */ |
| 576 | } |
Steven Rostedt | ffd618f | 2011-04-08 03:58:48 -0400 | [diff] [blame] | 577 | if (txtname && is_mcounted_section_name(txtname)) { |
Matt Helsley | 3f1df12 | 2019-07-31 11:24:12 -0700 | [diff] [blame] | 578 | unsigned int recsym; |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 579 | uint_t recval = 0; |
Matt Helsley | 3f1df12 | 2019-07-31 11:24:12 -0700 | [diff] [blame] | 580 | |
| 581 | symsec_sh_link = w(relhdr->sh_link); |
| 582 | result = find_secsym_ndx(w(relhdr->sh_info), txtname, |
| 583 | &recval, &recsym, |
| 584 | &shdr0[symsec_sh_link], |
| 585 | ehdr); |
| 586 | if (result) |
| 587 | goto out; |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 588 | |
| 589 | rel_entsize = _w(relhdr->sh_entsize); |
| 590 | mlocp = sift_rel_mcount(mlocp, |
| 591 | (void *)mlocp - (void *)mloc0, &mrelp, |
| 592 | relhdr, ehdr, recsym, recval, reltype); |
Steven Rostedt | dfad3d5 | 2011-04-12 18:53:25 -0400 | [diff] [blame] | 593 | } else if (txtname && (warn_on_notrace_sect || make_nop)) { |
Steven Rostedt | ffd618f | 2011-04-08 03:58:48 -0400 | [diff] [blame] | 594 | /* |
| 595 | * This section is ignored by ftrace, but still |
| 596 | * has mcount calls. Convert them to nops now. |
| 597 | */ |
Matt Helsley | 3f1df12 | 2019-07-31 11:24:12 -0700 | [diff] [blame] | 598 | if (nop_mcount(relhdr, ehdr, txtname) < 0) { |
| 599 | result = -1; |
| 600 | goto out; |
| 601 | } |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 602 | } |
| 603 | } |
Matt Helsley | 3f1df12 | 2019-07-31 11:24:12 -0700 | [diff] [blame] | 604 | if (!result && mloc0 != mlocp) |
| 605 | result = append_func(ehdr, shstr, mloc0, mlocp, mrel0, mrelp, |
| 606 | rel_entsize, symsec_sh_link); |
| 607 | out: |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 608 | free(mrel0); |
| 609 | free(mloc0); |
Matt Helsley | 3f1df12 | 2019-07-31 11:24:12 -0700 | [diff] [blame] | 610 | return result; |
Steven Rostedt | c28d507 | 2010-10-13 19:06:14 -0400 | [diff] [blame] | 611 | } |