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