blob: f9b19524da1122ee1d004d7b5b4fef0e6d8c7c09 [file] [log] [blame]
Thomas Gleixner4317cf952019-05-31 01:09:38 -07001/* SPDX-License-Identifier: GPL-2.0-only */
Steven Rostedtc28d5072010-10-13 19:06:14 -04002/*
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 Rostedtc28d5072010-10-13 19:06:14 -040019 */
20#undef append_func
Wu Zhangjin412910c2010-10-27 18:59:08 +080021#undef is_fake_mcount
22#undef fn_is_fake_mcount
23#undef MIPS_is_fake_mcount
Martin Schwidefsky07d8b592011-05-10 10:10:40 +020024#undef mcount_adjust
Steven Rostedtc28d5072010-10-13 19:06:14 -040025#undef sift_rel_mcount
Steven Rostedtffd618f2011-04-08 03:58:48 -040026#undef nop_mcount
Steven Rostedtc28d5072010-10-13 19:06:14 -040027#undef find_secsym_ndx
28#undef __has_rel_mcount
29#undef has_rel_mcount
30#undef tot_relsize
Steven Rostedt37762cb2011-04-20 20:47:34 -040031#undef get_mcountsym
Sami Tolvanen4ef57b22020-04-24 12:30:46 -070032#undef find_symtab
33#undef get_shnum
34#undef set_shnum
35#undef get_shstrndx
36#undef get_symindex
Steven Rostedt41b402a2011-04-20 21:13:06 -040037#undef get_sym_str_and_relp
Steven Rostedtc28d5072010-10-13 19:06:14 -040038#undef do_func
Wu Zhangjin412910c2010-10-27 18:59:08 +080039#undef Elf_Addr
Steven Rostedtc28d5072010-10-13 19:06:14 -040040#undef Elf_Ehdr
41#undef Elf_Shdr
42#undef Elf_Rel
43#undef Elf_Rela
44#undef Elf_Sym
45#undef ELF_R_SYM
John Reisera2d493582010-10-27 18:59:07 +080046#undef Elf_r_sym
Steven Rostedtc28d5072010-10-13 19:06:14 -040047#undef ELF_R_INFO
John Reisera2d493582010-10-27 18:59:07 +080048#undef Elf_r_info
Steven Rostedtc28d5072010-10-13 19:06:14 -040049#undef ELF_ST_BIND
Rabin Vincent9905ce82011-05-11 22:53:51 +053050#undef ELF_ST_TYPE
John Reisera2d493582010-10-27 18:59:07 +080051#undef fn_ELF_R_SYM
52#undef fn_ELF_R_INFO
Steven Rostedtc28d5072010-10-13 19:06:14 -040053#undef uint_t
54#undef _w
55#undef _align
56#undef _size
57
58#ifdef RECORD_MCOUNT_64
59# define append_func append64
60# define sift_rel_mcount sift64_rel_mcount
Steven Rostedtffd618f2011-04-08 03:58:48 -040061# define nop_mcount nop_mcount_64
Steven Rostedtc28d5072010-10-13 19:06:14 -040062# define find_secsym_ndx find64_secsym_ndx
63# define __has_rel_mcount __has64_rel_mcount
64# define has_rel_mcount has64_rel_mcount
65# define tot_relsize tot64_relsize
Sami Tolvanen4ef57b22020-04-24 12:30:46 -070066# define find_symtab find_symtab64
67# define get_shnum get_shnum64
68# define set_shnum set_shnum64
69# define get_shstrndx get_shstrndx64
70# define get_symindex get_symindex64
Steven Rostedt41b402a2011-04-20 21:13:06 -040071# define get_sym_str_and_relp get_sym_str_and_relp_64
Steven Rostedtc28d5072010-10-13 19:06:14 -040072# define do_func do64
Steven Rostedt37762cb2011-04-20 20:47:34 -040073# define get_mcountsym get_mcountsym_64
Wu Zhangjin412910c2010-10-27 18:59:08 +080074# define is_fake_mcount is_fake_mcount64
75# define fn_is_fake_mcount fn_is_fake_mcount64
76# define MIPS_is_fake_mcount MIPS64_is_fake_mcount
Martin Schwidefsky07d8b592011-05-10 10:10:40 +020077# define mcount_adjust mcount_adjust_64
Wu Zhangjin412910c2010-10-27 18:59:08 +080078# define Elf_Addr Elf64_Addr
Steven Rostedtc28d5072010-10-13 19:06:14 -040079# define Elf_Ehdr Elf64_Ehdr
80# define Elf_Shdr Elf64_Shdr
81# define Elf_Rel Elf64_Rel
82# define Elf_Rela Elf64_Rela
83# define Elf_Sym Elf64_Sym
84# define ELF_R_SYM ELF64_R_SYM
John Reisera2d493582010-10-27 18:59:07 +080085# define Elf_r_sym Elf64_r_sym
Steven Rostedtc28d5072010-10-13 19:06:14 -040086# define ELF_R_INFO ELF64_R_INFO
John Reisera2d493582010-10-27 18:59:07 +080087# define Elf_r_info Elf64_r_info
Steven Rostedtc28d5072010-10-13 19:06:14 -040088# define ELF_ST_BIND ELF64_ST_BIND
Rabin Vincent9905ce82011-05-11 22:53:51 +053089# define ELF_ST_TYPE ELF64_ST_TYPE
John Reisera2d493582010-10-27 18:59:07 +080090# define fn_ELF_R_SYM fn_ELF64_R_SYM
91# define fn_ELF_R_INFO fn_ELF64_R_INFO
Steven Rostedtc28d5072010-10-13 19:06:14 -040092# define uint_t uint64_t
93# define _w w8
94# define _align 7u
95# define _size 8
96#else
97# define append_func append32
98# define sift_rel_mcount sift32_rel_mcount
Steven Rostedtffd618f2011-04-08 03:58:48 -040099# define nop_mcount nop_mcount_32
Steven Rostedtc28d5072010-10-13 19:06:14 -0400100# define find_secsym_ndx find32_secsym_ndx
101# define __has_rel_mcount __has32_rel_mcount
102# define has_rel_mcount has32_rel_mcount
103# define tot_relsize tot32_relsize
Sami Tolvanen4ef57b22020-04-24 12:30:46 -0700104# define find_symtab find_symtab32
105# define get_shnum get_shnum32
106# define set_shnum set_shnum32
107# define get_shstrndx get_shstrndx32
108# define get_symindex get_symindex32
Steven Rostedt41b402a2011-04-20 21:13:06 -0400109# define get_sym_str_and_relp get_sym_str_and_relp_32
Steven Rostedtc28d5072010-10-13 19:06:14 -0400110# define do_func do32
Steven Rostedt37762cb2011-04-20 20:47:34 -0400111# define get_mcountsym get_mcountsym_32
Wu Zhangjin412910c2010-10-27 18:59:08 +0800112# define is_fake_mcount is_fake_mcount32
113# define fn_is_fake_mcount fn_is_fake_mcount32
114# define MIPS_is_fake_mcount MIPS32_is_fake_mcount
Martin Schwidefsky07d8b592011-05-10 10:10:40 +0200115# define mcount_adjust mcount_adjust_32
Wu Zhangjin412910c2010-10-27 18:59:08 +0800116# define Elf_Addr Elf32_Addr
Steven Rostedtc28d5072010-10-13 19:06:14 -0400117# define Elf_Ehdr Elf32_Ehdr
118# define Elf_Shdr Elf32_Shdr
119# define Elf_Rel Elf32_Rel
120# define Elf_Rela Elf32_Rela
121# define Elf_Sym Elf32_Sym
122# define ELF_R_SYM ELF32_R_SYM
John Reisera2d493582010-10-27 18:59:07 +0800123# define Elf_r_sym Elf32_r_sym
Steven Rostedtc28d5072010-10-13 19:06:14 -0400124# define ELF_R_INFO ELF32_R_INFO
John Reisera2d493582010-10-27 18:59:07 +0800125# define Elf_r_info Elf32_r_info
Steven Rostedtc28d5072010-10-13 19:06:14 -0400126# define ELF_ST_BIND ELF32_ST_BIND
Rabin Vincent9905ce82011-05-11 22:53:51 +0530127# define ELF_ST_TYPE ELF32_ST_TYPE
John Reisera2d493582010-10-27 18:59:07 +0800128# define fn_ELF_R_SYM fn_ELF32_R_SYM
129# define fn_ELF_R_INFO fn_ELF32_R_INFO
Steven Rostedtc28d5072010-10-13 19:06:14 -0400130# define uint_t uint32_t
131# define _w w
132# define _align 3u
133# define _size 4
134#endif
135
Wu Zhangjin412910c2010-10-27 18:59:08 +0800136/* Functions and pointers that do_file() may override for specific e_machine. */
137static int fn_is_fake_mcount(Elf_Rel const *rp)
138{
139 return 0;
140}
141static int (*is_fake_mcount)(Elf_Rel const *rp) = fn_is_fake_mcount;
142
John Reisera2d493582010-10-27 18:59:07 +0800143static uint_t fn_ELF_R_SYM(Elf_Rel const *rp)
144{
145 return ELF_R_SYM(_w(rp->r_info));
146}
147static uint_t (*Elf_r_sym)(Elf_Rel const *rp) = fn_ELF_R_SYM;
148
149static void fn_ELF_R_INFO(Elf_Rel *const rp, unsigned sym, unsigned type)
150{
John Reisere63233f72010-11-22 19:41:44 -0800151 rp->r_info = _w(ELF_R_INFO(sym, type));
John Reisera2d493582010-10-27 18:59:07 +0800152}
153static void (*Elf_r_info)(Elf_Rel *const rp, unsigned sym, unsigned type) = fn_ELF_R_INFO;
154
Martin Schwidefsky07d8b592011-05-10 10:10:40 +0200155static int mcount_adjust = 0;
156
Wu Zhangjin412910c2010-10-27 18:59:08 +0800157/*
158 * MIPS mcount long call has 2 _mcount symbols, only the position of the 1st
159 * _mcount symbol is needed for dynamic function tracer, with it, to disable
160 * tracing(ftrace_make_nop), the instruction in the position is replaced with
161 * the "b label" instruction, to enable tracing(ftrace_make_call), replace the
162 * instruction back. So, here, we set the 2nd one as fake and filter it.
163 *
164 * c: 3c030000 lui v1,0x0 <--> b label
165 * c: R_MIPS_HI16 _mcount
166 * c: R_MIPS_NONE *ABS*
167 * c: R_MIPS_NONE *ABS*
168 * 10: 64630000 daddiu v1,v1,0
169 * 10: R_MIPS_LO16 _mcount
170 * 10: R_MIPS_NONE *ABS*
171 * 10: R_MIPS_NONE *ABS*
172 * 14: 03e0082d move at,ra
173 * 18: 0060f809 jalr v1
174 * label:
175 */
176#define MIPS_FAKEMCOUNT_OFFSET 4
177
178static int MIPS_is_fake_mcount(Elf_Rel const *rp)
179{
Alex Smith91ad11d2014-06-17 10:39:53 +0100180 static Elf_Addr old_r_offset = ~(Elf_Addr)0;
Wu Zhangjin412910c2010-10-27 18:59:08 +0800181 Elf_Addr current_r_offset = _w(rp->r_offset);
182 int is_fake;
183
Alex Smith91ad11d2014-06-17 10:39:53 +0100184 is_fake = (old_r_offset != ~(Elf_Addr)0) &&
Wu Zhangjin412910c2010-10-27 18:59:08 +0800185 (current_r_offset - old_r_offset == MIPS_FAKEMCOUNT_OFFSET);
186 old_r_offset = current_r_offset;
187
188 return is_fake;
189}
John Reisera2d493582010-10-27 18:59:07 +0800190
Sami Tolvanen4ef57b22020-04-24 12:30:46 -0700191static unsigned int get_symindex(Elf_Sym const *sym, Elf32_Word const *symtab,
192 Elf32_Word const *symtab_shndx)
193{
194 unsigned long offset;
195 int index;
196
197 if (sym->st_shndx != SHN_XINDEX)
198 return w2(sym->st_shndx);
199
200 offset = (unsigned long)sym - (unsigned long)symtab;
201 index = offset / sizeof(*sym);
202
203 return w(symtab_shndx[index]);
204}
205
206static unsigned int get_shnum(Elf_Ehdr const *ehdr, Elf_Shdr const *shdr0)
207{
208 if (shdr0 && !ehdr->e_shnum)
209 return w(shdr0->sh_size);
210
211 return w2(ehdr->e_shnum);
212}
213
214static void set_shnum(Elf_Ehdr *ehdr, Elf_Shdr *shdr0, unsigned int new_shnum)
215{
216 if (new_shnum >= SHN_LORESERVE) {
217 ehdr->e_shnum = 0;
218 shdr0->sh_size = w(new_shnum);
219 } else
220 ehdr->e_shnum = w2(new_shnum);
221}
222
223static int get_shstrndx(Elf_Ehdr const *ehdr, Elf_Shdr const *shdr0)
224{
225 if (ehdr->e_shstrndx != SHN_XINDEX)
226 return w2(ehdr->e_shstrndx);
227
228 return w(shdr0->sh_link);
229}
230
231static void find_symtab(Elf_Ehdr *const ehdr, Elf_Shdr const *shdr0,
232 unsigned const nhdr, Elf32_Word **symtab,
233 Elf32_Word **symtab_shndx)
234{
235 Elf_Shdr const *relhdr;
236 unsigned k;
237
238 *symtab = NULL;
239 *symtab_shndx = NULL;
240
241 for (relhdr = shdr0, k = nhdr; k; --k, ++relhdr) {
242 if (relhdr->sh_type == SHT_SYMTAB)
243 *symtab = (void *)ehdr + relhdr->sh_offset;
244 else if (relhdr->sh_type == SHT_SYMTAB_SHNDX)
245 *symtab_shndx = (void *)ehdr + relhdr->sh_offset;
246
247 if (*symtab && *symtab_shndx)
248 break;
249 }
250}
251
Steven Rostedtc28d5072010-10-13 19:06:14 -0400252/* Append the new shstrtab, Elf_Shdr[], __mcount_loc and its relocations. */
Matt Helsley3f1df122019-07-31 11:24:12 -0700253static int append_func(Elf_Ehdr *const ehdr,
Steven Rostedtc28d5072010-10-13 19:06:14 -0400254 Elf_Shdr *const shstr,
255 uint_t const *const mloc0,
256 uint_t const *const mlocp,
257 Elf_Rel const *const mrel0,
258 Elf_Rel const *const mrelp,
259 unsigned int const rel_entsize,
260 unsigned int const symsec_sh_link)
261{
262 /* Begin constructing output file */
263 Elf_Shdr mcsec;
264 char const *mc_name = (sizeof(Elf_Rela) == rel_entsize)
265 ? ".rela__mcount_loc"
266 : ".rel__mcount_loc";
Steven Rostedtc28d5072010-10-13 19:06:14 -0400267 uint_t const old_shoff = _w(ehdr->e_shoff);
268 uint_t const old_shstr_sh_size = _w(shstr->sh_size);
269 uint_t const old_shstr_sh_offset = _w(shstr->sh_offset);
Sami Tolvanen4ef57b22020-04-24 12:30:46 -0700270 Elf_Shdr *const shdr0 = (Elf_Shdr *)(old_shoff + (void *)ehdr);
271 unsigned int const old_shnum = get_shnum(ehdr, shdr0);
272 unsigned int const new_shnum = 2 + old_shnum; /* {.rel,}__mcount_loc */
Steven Rostedtc28d5072010-10-13 19:06:14 -0400273 uint_t t = 1 + strlen(mc_name) + _w(shstr->sh_size);
274 uint_t new_e_shoff;
275
276 shstr->sh_size = _w(t);
277 shstr->sh_offset = _w(sb.st_size);
278 t += sb.st_size;
279 t += (_align & -t); /* word-byte align */
280 new_e_shoff = t;
281
Sami Tolvanen4ef57b22020-04-24 12:30:46 -0700282 set_shnum(ehdr, shdr0, new_shnum);
283
Steven Rostedtc28d5072010-10-13 19:06:14 -0400284 /* body for new shstrtab */
Matt Helsley3f1df122019-07-31 11:24:12 -0700285 if (ulseek(sb.st_size, SEEK_SET) < 0)
286 return -1;
287 if (uwrite(old_shstr_sh_offset + (void *)ehdr, old_shstr_sh_size) < 0)
288 return -1;
289 if (uwrite(mc_name, 1 + strlen(mc_name)) < 0)
290 return -1;
Steven Rostedtc28d5072010-10-13 19:06:14 -0400291
292 /* old(modified) Elf_Shdr table, word-byte aligned */
Matt Helsley3f1df122019-07-31 11:24:12 -0700293 if (ulseek(t, SEEK_SET) < 0)
294 return -1;
Steven Rostedtc28d5072010-10-13 19:06:14 -0400295 t += sizeof(Elf_Shdr) * old_shnum;
Matt Helsley3f1df122019-07-31 11:24:12 -0700296 if (uwrite(old_shoff + (void *)ehdr,
297 sizeof(Elf_Shdr) * old_shnum) < 0)
298 return -1;
Steven Rostedtc28d5072010-10-13 19:06:14 -0400299
300 /* new sections __mcount_loc and .rel__mcount_loc */
301 t += 2*sizeof(mcsec);
302 mcsec.sh_name = w((sizeof(Elf_Rela) == rel_entsize) + strlen(".rel")
303 + old_shstr_sh_size);
304 mcsec.sh_type = w(SHT_PROGBITS);
305 mcsec.sh_flags = _w(SHF_ALLOC);
306 mcsec.sh_addr = 0;
307 mcsec.sh_offset = _w(t);
308 mcsec.sh_size = _w((void *)mlocp - (void *)mloc0);
309 mcsec.sh_link = 0;
310 mcsec.sh_info = 0;
311 mcsec.sh_addralign = _w(_size);
312 mcsec.sh_entsize = _w(_size);
Matt Helsley3f1df122019-07-31 11:24:12 -0700313 if (uwrite(&mcsec, sizeof(mcsec)) < 0)
314 return -1;
Steven Rostedtc28d5072010-10-13 19:06:14 -0400315
316 mcsec.sh_name = w(old_shstr_sh_size);
317 mcsec.sh_type = (sizeof(Elf_Rela) == rel_entsize)
318 ? w(SHT_RELA)
319 : w(SHT_REL);
320 mcsec.sh_flags = 0;
321 mcsec.sh_addr = 0;
322 mcsec.sh_offset = _w((void *)mlocp - (void *)mloc0 + t);
323 mcsec.sh_size = _w((void *)mrelp - (void *)mrel0);
324 mcsec.sh_link = w(symsec_sh_link);
325 mcsec.sh_info = w(old_shnum);
326 mcsec.sh_addralign = _w(_size);
327 mcsec.sh_entsize = _w(rel_entsize);
Steven Rostedtc28d5072010-10-13 19:06:14 -0400328
Matt Helsley3f1df122019-07-31 11:24:12 -0700329 if (uwrite(&mcsec, sizeof(mcsec)) < 0)
330 return -1;
331
332 if (uwrite(mloc0, (void *)mlocp - (void *)mloc0) < 0)
333 return -1;
334 if (uwrite(mrel0, (void *)mrelp - (void *)mrel0) < 0)
335 return -1;
Steven Rostedtc28d5072010-10-13 19:06:14 -0400336
337 ehdr->e_shoff = _w(new_e_shoff);
Matt Helsley3f1df122019-07-31 11:24:12 -0700338 if (ulseek(0, SEEK_SET) < 0)
339 return -1;
340 if (uwrite(ehdr, sizeof(*ehdr)) < 0)
341 return -1;
342 return 0;
Steven Rostedtc28d5072010-10-13 19:06:14 -0400343}
344
Steven Rostedt37762cb2011-04-20 20:47:34 -0400345static unsigned get_mcountsym(Elf_Sym const *const sym0,
346 Elf_Rel const *relp,
347 char const *const str0)
348{
349 unsigned mcountsym = 0;
350
351 Elf_Sym const *const symp =
352 &sym0[Elf_r_sym(relp)];
353 char const *symname = &str0[w(symp->st_name)];
354 char const *mcount = gpfx == '_' ? "_mcount" : "mcount";
Steven Rostedt48bb5dc2011-02-09 13:13:23 -0500355 char const *fentry = "__fentry__";
Steven Rostedt37762cb2011-04-20 20:47:34 -0400356
357 if (symname[0] == '.')
358 ++symname; /* ppc64 hack */
359 if (strcmp(mcount, symname) == 0 ||
Steven Rostedt48bb5dc2011-02-09 13:13:23 -0500360 (altmcount && strcmp(altmcount, symname) == 0) ||
361 (strcmp(fentry, symname) == 0))
Steven Rostedt37762cb2011-04-20 20:47:34 -0400362 mcountsym = Elf_r_sym(relp);
363
364 return mcountsym;
365}
366
Steven Rostedt41b402a2011-04-20 21:13:06 -0400367static void get_sym_str_and_relp(Elf_Shdr const *const relhdr,
368 Elf_Ehdr const *const ehdr,
369 Elf_Sym const **sym0,
370 char const **str0,
371 Elf_Rel const **relp)
372{
373 Elf_Shdr *const shdr0 = (Elf_Shdr *)(_w(ehdr->e_shoff)
374 + (void *)ehdr);
375 unsigned const symsec_sh_link = w(relhdr->sh_link);
376 Elf_Shdr const *const symsec = &shdr0[symsec_sh_link];
377 Elf_Shdr const *const strsec = &shdr0[w(symsec->sh_link)];
378 Elf_Rel const *const rel0 = (Elf_Rel const *)(_w(relhdr->sh_offset)
379 + (void *)ehdr);
380
381 *sym0 = (Elf_Sym const *)(_w(symsec->sh_offset)
382 + (void *)ehdr);
383
384 *str0 = (char const *)(_w(strsec->sh_offset)
385 + (void *)ehdr);
386
387 *relp = rel0;
388}
389
Steven Rostedtc28d5072010-10-13 19:06:14 -0400390/*
391 * Look at the relocations in order to find the calls to mcount.
392 * Accumulate the section offsets that are found, and their relocation info,
393 * onto the end of the existing arrays.
394 */
395static uint_t *sift_rel_mcount(uint_t *mlocp,
396 unsigned const offbase,
397 Elf_Rel **const mrelpp,
398 Elf_Shdr const *const relhdr,
399 Elf_Ehdr const *const ehdr,
400 unsigned const recsym,
401 uint_t const recval,
402 unsigned const reltype)
403{
404 uint_t *const mloc0 = mlocp;
405 Elf_Rel *mrelp = *mrelpp;
Steven Rostedt41b402a2011-04-20 21:13:06 -0400406 Elf_Sym const *sym0;
407 char const *str0;
408 Elf_Rel const *relp;
Steven Rostedtc28d5072010-10-13 19:06:14 -0400409 unsigned rel_entsize = _w(relhdr->sh_entsize);
410 unsigned const nrel = _w(relhdr->sh_size) / rel_entsize;
Steven Rostedtc28d5072010-10-13 19:06:14 -0400411 unsigned mcountsym = 0;
412 unsigned t;
413
Steven Rostedt41b402a2011-04-20 21:13:06 -0400414 get_sym_str_and_relp(relhdr, ehdr, &sym0, &str0, &relp);
415
Steven Rostedtc28d5072010-10-13 19:06:14 -0400416 for (t = nrel; t; --t) {
Steven Rostedt37762cb2011-04-20 20:47:34 -0400417 if (!mcountsym)
418 mcountsym = get_mcountsym(sym0, relp, str0);
Steven Rostedtc28d5072010-10-13 19:06:14 -0400419
Naveen N. Rao80e53022019-06-27 00:08:01 +0530420 if (mcountsym && mcountsym == Elf_r_sym(relp) &&
421 !is_fake_mcount(relp)) {
Martin Schwidefsky07d8b592011-05-10 10:10:40 +0200422 uint_t const addend =
423 _w(_w(relp->r_offset) - recval + mcount_adjust);
Steven Rostedtc28d5072010-10-13 19:06:14 -0400424 mrelp->r_offset = _w(offbase
425 + ((void *)mlocp - (void *)mloc0));
John Reisera2d493582010-10-27 18:59:07 +0800426 Elf_r_info(mrelp, recsym, reltype);
Steven Rostedtdd5477f2011-04-06 13:21:17 -0400427 if (rel_entsize == sizeof(Elf_Rela)) {
Steven Rostedtc28d5072010-10-13 19:06:14 -0400428 ((Elf_Rela *)mrelp)->r_addend = addend;
429 *mlocp++ = 0;
430 } else
431 *mlocp++ = addend;
432
433 mrelp = (Elf_Rel *)(rel_entsize + (void *)mrelp);
434 }
435 relp = (Elf_Rel const *)(rel_entsize + (void *)relp);
436 }
437 *mrelpp = mrelp;
438 return mlocp;
439}
440
Steven Rostedtffd618f2011-04-08 03:58:48 -0400441/*
442 * Read the relocation table again, but this time its called on sections
443 * that are not going to be traced. The mcount calls here will be converted
444 * into nops.
445 */
Matt Helsley3f1df122019-07-31 11:24:12 -0700446static int nop_mcount(Elf_Shdr const *const relhdr,
447 Elf_Ehdr const *const ehdr,
448 const char *const txtname)
Steven Rostedtffd618f2011-04-08 03:58:48 -0400449{
450 Elf_Shdr *const shdr0 = (Elf_Shdr *)(_w(ehdr->e_shoff)
451 + (void *)ehdr);
Steven Rostedt41b402a2011-04-20 21:13:06 -0400452 Elf_Sym const *sym0;
453 char const *str0;
454 Elf_Rel const *relp;
455 Elf_Shdr const *const shdr = &shdr0[w(relhdr->sh_info)];
Steven Rostedtffd618f2011-04-08 03:58:48 -0400456 unsigned rel_entsize = _w(relhdr->sh_entsize);
457 unsigned const nrel = _w(relhdr->sh_size) / rel_entsize;
Steven Rostedtffd618f2011-04-08 03:58:48 -0400458 unsigned mcountsym = 0;
459 unsigned t;
Steven Rostedtdfad3d52011-04-12 18:53:25 -0400460 int once = 0;
Steven Rostedtffd618f2011-04-08 03:58:48 -0400461
Steven Rostedt41b402a2011-04-20 21:13:06 -0400462 get_sym_str_and_relp(relhdr, ehdr, &sym0, &str0, &relp);
463
Steven Rostedtffd618f2011-04-08 03:58:48 -0400464 for (t = nrel; t; --t) {
465 int ret = -1;
466
Steven Rostedt37762cb2011-04-20 20:47:34 -0400467 if (!mcountsym)
468 mcountsym = get_mcountsym(sym0, relp, str0);
Steven Rostedtffd618f2011-04-08 03:58:48 -0400469
Steven Rostedtdfad3d52011-04-12 18:53:25 -0400470 if (mcountsym == Elf_r_sym(relp) && !is_fake_mcount(relp)) {
Steven Rostedt (VMware)7f8557b2019-10-09 11:05:38 -0400471 if (make_nop)
libinc84da8b2015-11-03 08:58:47 +0800472 ret = make_nop((void *)ehdr, _w(shdr->sh_offset) + _w(relp->r_offset));
Steven Rostedtdfad3d52011-04-12 18:53:25 -0400473 if (warn_on_notrace_sect && !once) {
474 printf("Section %s has mcount callers being ignored\n",
475 txtname);
476 once = 1;
477 /* just warn? */
478 if (!make_nop)
Matt Helsley3f1df122019-07-31 11:24:12 -0700479 return 0;
Steven Rostedtdfad3d52011-04-12 18:53:25 -0400480 }
481 }
Steven Rostedtffd618f2011-04-08 03:58:48 -0400482
483 /*
484 * If we successfully removed the mcount, mark the relocation
485 * as a nop (don't do anything with it).
486 */
487 if (!ret) {
488 Elf_Rel rel;
489 rel = *(Elf_Rel *)relp;
490 Elf_r_info(&rel, Elf_r_sym(relp), rel_type_nop);
Matt Helsley3f1df122019-07-31 11:24:12 -0700491 if (ulseek((void *)relp - (void *)ehdr, SEEK_SET) < 0)
492 return -1;
493 if (uwrite(&rel, sizeof(rel)) < 0)
494 return -1;
Steven Rostedtffd618f2011-04-08 03:58:48 -0400495 }
496 relp = (Elf_Rel const *)(rel_entsize + (void *)relp);
497 }
Matt Helsley3f1df122019-07-31 11:24:12 -0700498 return 0;
Steven Rostedtffd618f2011-04-08 03:58:48 -0400499}
500
Steven Rostedtc28d5072010-10-13 19:06:14 -0400501/*
502 * Find a symbol in the given section, to be used as the base for relocating
503 * the table of offsets of calls to mcount. A local or global symbol suffices,
504 * but avoid a Weak symbol because it may be overridden; the change in value
505 * would invalidate the relocations of the offsets of the calls to mcount.
506 * Often the found symbol will be the unnamed local symbol generated by
507 * GNU 'as' for the start of each section. For example:
508 * Num: Value Size Type Bind Vis Ndx Name
509 * 2: 00000000 0 SECTION LOCAL DEFAULT 1
510 */
Matt Helsley3f1df122019-07-31 11:24:12 -0700511static int find_secsym_ndx(unsigned const txtndx,
Steven Rostedtc28d5072010-10-13 19:06:14 -0400512 char const *const txtname,
513 uint_t *const recvalp,
Matt Helsley3f1df122019-07-31 11:24:12 -0700514 unsigned int *sym_index,
Steven Rostedtc28d5072010-10-13 19:06:14 -0400515 Elf_Shdr const *const symhdr,
Sami Tolvanen4ef57b22020-04-24 12:30:46 -0700516 Elf32_Word const *symtab,
517 Elf32_Word const *symtab_shndx,
Steven Rostedtc28d5072010-10-13 19:06:14 -0400518 Elf_Ehdr const *const ehdr)
519{
520 Elf_Sym const *const sym0 = (Elf_Sym const *)(_w(symhdr->sh_offset)
521 + (void *)ehdr);
522 unsigned const nsym = _w(symhdr->sh_size) / _w(symhdr->sh_entsize);
523 Elf_Sym const *symp;
524 unsigned t;
525
526 for (symp = sym0, t = nsym; t; --t, ++symp) {
527 unsigned int const st_bind = ELF_ST_BIND(symp->st_info);
528
Sami Tolvanen4ef57b22020-04-24 12:30:46 -0700529 if (txtndx == get_symindex(symp, symtab, symtab_shndx)
Steven Rostedtc28d5072010-10-13 19:06:14 -0400530 /* avoid STB_WEAK */
531 && (STB_LOCAL == st_bind || STB_GLOBAL == st_bind)) {
Rabin Vincent9905ce82011-05-11 22:53:51 +0530532 /* function symbols on ARM have quirks, avoid them */
533 if (w2(ehdr->e_machine) == EM_ARM
534 && ELF_ST_TYPE(symp->st_info) == STT_FUNC)
535 continue;
536
Steven Rostedtc28d5072010-10-13 19:06:14 -0400537 *recvalp = _w(symp->st_value);
Matt Helsley3f1df122019-07-31 11:24:12 -0700538 *sym_index = symp - sym0;
539 return 0;
Steven Rostedtc28d5072010-10-13 19:06:14 -0400540 }
541 }
nixiaomingac5db1f2018-05-24 11:16:12 +0800542 fprintf(stderr, "Cannot find symbol for section %u: %s.\n",
Steven Rostedtc28d5072010-10-13 19:06:14 -0400543 txtndx, txtname);
Matt Helsley3f1df122019-07-31 11:24:12 -0700544 return -1;
Steven Rostedtc28d5072010-10-13 19:06:14 -0400545}
546
Steven Rostedtc28d5072010-10-13 19:06:14 -0400547/* Evade ISO C restriction: no declaration after statement in has_rel_mcount. */
Matt Helsley3aec8632019-07-31 11:24:13 -0700548static char const * __has_rel_mcount(Elf_Shdr const *const relhdr, /* reltype */
549 Elf_Shdr const *const shdr0,
550 char const *const shstrtab,
551 char const *const fname)
Steven Rostedtc28d5072010-10-13 19:06:14 -0400552{
553 /* .sh_info depends on .sh_type == SHT_REL[,A] */
554 Elf_Shdr const *const txthdr = &shdr0[w(relhdr->sh_info)];
555 char const *const txtname = &shstrtab[w(txthdr->sh_name)];
556
Steven Rostedtdd5477f2011-04-06 13:21:17 -0400557 if (strcmp("__mcount_loc", txtname) == 0) {
Steven Rostedtc28d5072010-10-13 19:06:14 -0400558 fprintf(stderr, "warning: __mcount_loc already exists: %s\n",
559 fname);
Matt Helsley3f1df122019-07-31 11:24:12 -0700560 return already_has_rel_mcount;
Steven Rostedtc28d5072010-10-13 19:06:14 -0400561 }
Steven Rostedtdd5477f2011-04-06 13:21:17 -0400562 if (w(txthdr->sh_type) != SHT_PROGBITS ||
David Daney2e885052011-12-19 17:42:42 -0800563 !(_w(txthdr->sh_flags) & SHF_EXECINSTR))
Steven Rostedtc28d5072010-10-13 19:06:14 -0400564 return NULL;
565 return txtname;
566}
567
568static char const *has_rel_mcount(Elf_Shdr const *const relhdr,
569 Elf_Shdr const *const shdr0,
570 char const *const shstrtab,
571 char const *const fname)
572{
Steven Rostedtdd5477f2011-04-06 13:21:17 -0400573 if (w(relhdr->sh_type) != SHT_REL && w(relhdr->sh_type) != SHT_RELA)
Steven Rostedtc28d5072010-10-13 19:06:14 -0400574 return NULL;
575 return __has_rel_mcount(relhdr, shdr0, shstrtab, fname);
576}
577
578
579static unsigned tot_relsize(Elf_Shdr const *const shdr0,
580 unsigned nhdr,
581 const char *const shstrtab,
582 const char *const fname)
583{
584 unsigned totrelsz = 0;
585 Elf_Shdr const *shdrp = shdr0;
Steven Rostedtffd618f2011-04-08 03:58:48 -0400586 char const *txtname;
Steven Rostedtc28d5072010-10-13 19:06:14 -0400587
588 for (; nhdr; --nhdr, ++shdrp) {
Steven Rostedtffd618f2011-04-08 03:58:48 -0400589 txtname = has_rel_mcount(shdrp, shdr0, shstrtab, fname);
Matt Helsley3f1df122019-07-31 11:24:12 -0700590 if (txtname == already_has_rel_mcount) {
591 totrelsz = 0;
592 break;
593 }
Steven Rostedtffd618f2011-04-08 03:58:48 -0400594 if (txtname && is_mcounted_section_name(txtname))
Steven Rostedtc28d5072010-10-13 19:06:14 -0400595 totrelsz += _w(shdrp->sh_size);
596 }
597 return totrelsz;
598}
599
Steven Rostedtc28d5072010-10-13 19:06:14 -0400600/* Overall supervision for Elf32 ET_REL file. */
Matt Helsley3aec8632019-07-31 11:24:13 -0700601static int do_func(Elf_Ehdr *const ehdr, char const *const fname,
602 unsigned const reltype)
Steven Rostedtc28d5072010-10-13 19:06:14 -0400603{
604 Elf_Shdr *const shdr0 = (Elf_Shdr *)(_w(ehdr->e_shoff)
605 + (void *)ehdr);
Sami Tolvanen4ef57b22020-04-24 12:30:46 -0700606 unsigned const nhdr = get_shnum(ehdr, shdr0);
607 Elf_Shdr *const shstr = &shdr0[get_shstrndx(ehdr, shdr0)];
Steven Rostedtc28d5072010-10-13 19:06:14 -0400608 char const *const shstrtab = (char const *)(_w(shstr->sh_offset)
609 + (void *)ehdr);
610
611 Elf_Shdr const *relhdr;
612 unsigned k;
613
Sami Tolvanen4ef57b22020-04-24 12:30:46 -0700614 Elf32_Word *symtab;
615 Elf32_Word *symtab_shndx;
616
Steven Rostedtc28d5072010-10-13 19:06:14 -0400617 /* Upper bound on space: assume all relevant relocs are for mcount. */
Matt Helsley3f1df122019-07-31 11:24:12 -0700618 unsigned totrelsz;
Steven Rostedtc28d5072010-10-13 19:06:14 -0400619
Matt Helsley3f1df122019-07-31 11:24:12 -0700620 Elf_Rel * mrel0;
621 Elf_Rel * mrelp;
622
623 uint_t * mloc0;
624 uint_t * mlocp;
Steven Rostedtc28d5072010-10-13 19:06:14 -0400625
626 unsigned rel_entsize = 0;
627 unsigned symsec_sh_link = 0;
628
Matt Helsley3f1df122019-07-31 11:24:12 -0700629 int result = 0;
630
631 totrelsz = tot_relsize(shdr0, nhdr, shstrtab, fname);
632 if (totrelsz == 0)
633 return 0;
634 mrel0 = umalloc(totrelsz);
635 mrelp = mrel0;
636 if (!mrel0)
637 return -1;
638
639 /* 2*sizeof(address) <= sizeof(Elf_Rel) */
640 mloc0 = umalloc(totrelsz>>1);
641 mlocp = mloc0;
642 if (!mloc0) {
643 free(mrel0);
644 return -1;
645 }
646
Sami Tolvanen4ef57b22020-04-24 12:30:46 -0700647 find_symtab(ehdr, shdr0, nhdr, &symtab, &symtab_shndx);
648
Steven Rostedtc28d5072010-10-13 19:06:14 -0400649 for (relhdr = shdr0, k = nhdr; k; --k, ++relhdr) {
650 char const *const txtname = has_rel_mcount(relhdr, shdr0,
651 shstrtab, fname);
Matt Helsley3f1df122019-07-31 11:24:12 -0700652 if (txtname == already_has_rel_mcount) {
653 result = 0;
654 file_updated = 0;
655 goto out; /* Nothing to be done; don't append! */
656 }
Steven Rostedtffd618f2011-04-08 03:58:48 -0400657 if (txtname && is_mcounted_section_name(txtname)) {
Matt Helsley3f1df122019-07-31 11:24:12 -0700658 unsigned int recsym;
Steven Rostedtc28d5072010-10-13 19:06:14 -0400659 uint_t recval = 0;
Matt Helsley3f1df122019-07-31 11:24:12 -0700660
661 symsec_sh_link = w(relhdr->sh_link);
662 result = find_secsym_ndx(w(relhdr->sh_info), txtname,
663 &recval, &recsym,
664 &shdr0[symsec_sh_link],
Sami Tolvanen4ef57b22020-04-24 12:30:46 -0700665 symtab, symtab_shndx,
Matt Helsley3f1df122019-07-31 11:24:12 -0700666 ehdr);
667 if (result)
668 goto out;
Steven Rostedtc28d5072010-10-13 19:06:14 -0400669
670 rel_entsize = _w(relhdr->sh_entsize);
671 mlocp = sift_rel_mcount(mlocp,
672 (void *)mlocp - (void *)mloc0, &mrelp,
673 relhdr, ehdr, recsym, recval, reltype);
Steven Rostedtdfad3d52011-04-12 18:53:25 -0400674 } else if (txtname && (warn_on_notrace_sect || make_nop)) {
Steven Rostedtffd618f2011-04-08 03:58:48 -0400675 /*
676 * This section is ignored by ftrace, but still
677 * has mcount calls. Convert them to nops now.
678 */
Matt Helsley3f1df122019-07-31 11:24:12 -0700679 if (nop_mcount(relhdr, ehdr, txtname) < 0) {
680 result = -1;
681 goto out;
682 }
Steven Rostedtc28d5072010-10-13 19:06:14 -0400683 }
684 }
Matt Helsley3f1df122019-07-31 11:24:12 -0700685 if (!result && mloc0 != mlocp)
686 result = append_func(ehdr, shstr, mloc0, mlocp, mrel0, mrelp,
687 rel_entsize, symsec_sh_link);
688out:
Steven Rostedtc28d5072010-10-13 19:06:14 -0400689 free(mrel0);
690 free(mloc0);
Matt Helsley3f1df122019-07-31 11:24:12 -0700691 return result;
Steven Rostedtc28d5072010-10-13 19:06:14 -0400692}