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