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