xref: /linux-6.15/scripts/recordmcount.h (revision 68eb1d2e)
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 {
318 	Elf_Shdr *const shdr0 = (Elf_Shdr *)(_w(ehdr->e_shoff)
319 		+ (void *)ehdr);
320 	unsigned const symsec_sh_link = w(relhdr->sh_link);
321 	Elf_Shdr const *const symsec = &shdr0[symsec_sh_link];
322 	Elf_Sym const *const sym0 = (Elf_Sym const *)(_w(symsec->sh_offset)
323 		+ (void *)ehdr);
324 
325 	Elf_Shdr const *const strsec = &shdr0[w(symsec->sh_link)];
326 	char const *const str0 = (char const *)(_w(strsec->sh_offset)
327 		+ (void *)ehdr);
328 
329 	Elf_Rel const *const rel0 = (Elf_Rel const *)(_w(relhdr->sh_offset)
330 		+ (void *)ehdr);
331 	unsigned rel_entsize = _w(relhdr->sh_entsize);
332 	unsigned const nrel = _w(relhdr->sh_size) / rel_entsize;
333 	Elf_Rel const *relp = rel0;
334 
335 	Elf_Shdr const *const shdr = &shdr0[w(relhdr->sh_info)];
336 
337 	unsigned mcountsym = 0;
338 	unsigned t;
339 
340 	for (t = nrel; t; --t) {
341 		int ret = -1;
342 
343 		if (!mcountsym) {
344 			Elf_Sym const *const symp =
345 				&sym0[Elf_r_sym(relp)];
346 			char const *symname = &str0[w(symp->st_name)];
347 			char const *mcount = gpfx == '_' ? "_mcount" : "mcount";
348 
349 			if (symname[0] == '.')
350 				++symname;  /* ppc64 hack */
351 			if (strcmp(mcount, symname) == 0 ||
352 			    (altmcount && strcmp(altmcount, symname) == 0))
353 				mcountsym = Elf_r_sym(relp);
354 		}
355 
356 		if (mcountsym == Elf_r_sym(relp) && !is_fake_mcount(relp))
357 			ret = make_nop((void *)ehdr, shdr->sh_offset + relp->r_offset);
358 
359 		/*
360 		 * If we successfully removed the mcount, mark the relocation
361 		 * as a nop (don't do anything with it).
362 		 */
363 		if (!ret) {
364 			Elf_Rel rel;
365 			rel = *(Elf_Rel *)relp;
366 			Elf_r_info(&rel, Elf_r_sym(relp), rel_type_nop);
367 			ulseek(fd_map, (void *)relp - (void *)ehdr, SEEK_SET);
368 			uwrite(fd_map, &rel, sizeof(rel));
369 		}
370 		relp = (Elf_Rel const *)(rel_entsize + (void *)relp);
371 	}
372 }
373 
374 
375 /*
376  * Find a symbol in the given section, to be used as the base for relocating
377  * the table of offsets of calls to mcount.  A local or global symbol suffices,
378  * but avoid a Weak symbol because it may be overridden; the change in value
379  * would invalidate the relocations of the offsets of the calls to mcount.
380  * Often the found symbol will be the unnamed local symbol generated by
381  * GNU 'as' for the start of each section.  For example:
382  *    Num:    Value  Size Type    Bind   Vis      Ndx Name
383  *      2: 00000000     0 SECTION LOCAL  DEFAULT    1
384  */
385 static unsigned find_secsym_ndx(unsigned const txtndx,
386 				char const *const txtname,
387 				uint_t *const recvalp,
388 				Elf_Shdr const *const symhdr,
389 				Elf_Ehdr const *const ehdr)
390 {
391 	Elf_Sym const *const sym0 = (Elf_Sym const *)(_w(symhdr->sh_offset)
392 		+ (void *)ehdr);
393 	unsigned const nsym = _w(symhdr->sh_size) / _w(symhdr->sh_entsize);
394 	Elf_Sym const *symp;
395 	unsigned t;
396 
397 	for (symp = sym0, t = nsym; t; --t, ++symp) {
398 		unsigned int const st_bind = ELF_ST_BIND(symp->st_info);
399 
400 		if (txtndx == w2(symp->st_shndx)
401 			/* avoid STB_WEAK */
402 		    && (STB_LOCAL == st_bind || STB_GLOBAL == st_bind)) {
403 			*recvalp = _w(symp->st_value);
404 			return symp - sym0;
405 		}
406 	}
407 	fprintf(stderr, "Cannot find symbol for section %d: %s.\n",
408 		txtndx, txtname);
409 	fail_file();
410 }
411 
412 
413 /* Evade ISO C restriction: no declaration after statement in has_rel_mcount. */
414 static char const *
415 __has_rel_mcount(Elf_Shdr const *const relhdr,  /* is SHT_REL or SHT_RELA */
416 		 Elf_Shdr const *const shdr0,
417 		 char const *const shstrtab,
418 		 char const *const fname)
419 {
420 	/* .sh_info depends on .sh_type == SHT_REL[,A] */
421 	Elf_Shdr const *const txthdr = &shdr0[w(relhdr->sh_info)];
422 	char const *const txtname = &shstrtab[w(txthdr->sh_name)];
423 
424 	if (strcmp("__mcount_loc", txtname) == 0) {
425 		fprintf(stderr, "warning: __mcount_loc already exists: %s\n",
426 			fname);
427 		succeed_file();
428 	}
429 	if (w(txthdr->sh_type) != SHT_PROGBITS ||
430 	    !(w(txthdr->sh_flags) & SHF_EXECINSTR))
431 		return NULL;
432 	return txtname;
433 }
434 
435 static char const *has_rel_mcount(Elf_Shdr const *const relhdr,
436 				  Elf_Shdr const *const shdr0,
437 				  char const *const shstrtab,
438 				  char const *const fname)
439 {
440 	if (w(relhdr->sh_type) != SHT_REL && w(relhdr->sh_type) != SHT_RELA)
441 		return NULL;
442 	return __has_rel_mcount(relhdr, shdr0, shstrtab, fname);
443 }
444 
445 
446 static unsigned tot_relsize(Elf_Shdr const *const shdr0,
447 			    unsigned nhdr,
448 			    const char *const shstrtab,
449 			    const char *const fname)
450 {
451 	unsigned totrelsz = 0;
452 	Elf_Shdr const *shdrp = shdr0;
453 	char const *txtname;
454 
455 	for (; nhdr; --nhdr, ++shdrp) {
456 		txtname = has_rel_mcount(shdrp, shdr0, shstrtab, fname);
457 		if (txtname && is_mcounted_section_name(txtname))
458 			totrelsz += _w(shdrp->sh_size);
459 	}
460 	return totrelsz;
461 }
462 
463 
464 /* Overall supervision for Elf32 ET_REL file. */
465 static void
466 do_func(Elf_Ehdr *const ehdr, char const *const fname, unsigned const reltype)
467 {
468 	Elf_Shdr *const shdr0 = (Elf_Shdr *)(_w(ehdr->e_shoff)
469 		+ (void *)ehdr);
470 	unsigned const nhdr = w2(ehdr->e_shnum);
471 	Elf_Shdr *const shstr = &shdr0[w2(ehdr->e_shstrndx)];
472 	char const *const shstrtab = (char const *)(_w(shstr->sh_offset)
473 		+ (void *)ehdr);
474 
475 	Elf_Shdr const *relhdr;
476 	unsigned k;
477 
478 	/* Upper bound on space: assume all relevant relocs are for mcount. */
479 	unsigned const totrelsz = tot_relsize(shdr0, nhdr, shstrtab, fname);
480 	Elf_Rel *const mrel0 = umalloc(totrelsz);
481 	Elf_Rel *      mrelp = mrel0;
482 
483 	/* 2*sizeof(address) <= sizeof(Elf_Rel) */
484 	uint_t *const mloc0 = umalloc(totrelsz>>1);
485 	uint_t *      mlocp = mloc0;
486 
487 	unsigned rel_entsize = 0;
488 	unsigned symsec_sh_link = 0;
489 
490 	for (relhdr = shdr0, k = nhdr; k; --k, ++relhdr) {
491 		char const *const txtname = has_rel_mcount(relhdr, shdr0,
492 			shstrtab, fname);
493 		if (txtname && is_mcounted_section_name(txtname)) {
494 			uint_t recval = 0;
495 			unsigned const recsym = find_secsym_ndx(
496 				w(relhdr->sh_info), txtname, &recval,
497 				&shdr0[symsec_sh_link = w(relhdr->sh_link)],
498 				ehdr);
499 
500 			rel_entsize = _w(relhdr->sh_entsize);
501 			mlocp = sift_rel_mcount(mlocp,
502 				(void *)mlocp - (void *)mloc0, &mrelp,
503 				relhdr, ehdr, recsym, recval, reltype);
504 		} else if (make_nop && txtname) {
505 			/*
506 			 * This section is ignored by ftrace, but still
507 			 * has mcount calls. Convert them to nops now.
508 			 */
509 			nop_mcount(relhdr, ehdr);
510 		}
511 	}
512 	if (mloc0 != mlocp) {
513 		append_func(ehdr, shstr, mloc0, mlocp, mrel0, mrelp,
514 			    rel_entsize, symsec_sh_link);
515 	}
516 	free(mrel0);
517 	free(mloc0);
518 }
519