The code to get the symbol, string, and relp pointers in the two functions sift_rel_mcount() and nop_mcount() are identical and also non-trivial. Moving this duplicate code into a single helper function makes the code easier to read and more maintainable.
Showing the changes to scripts (1 of 1 file). Show all
Unified · Split
| @@ -29,6 +29,7 @@ · 28 unmodified lines | ||
| 29 | 29 | #undef has_rel_mcount |
| 30 | 30 | #undef tot_relsize |
| 31 | 31 | #undef get_mcountsym |
| 32 | +#undef get_sym_str_and_relp | |
| 32 | 33 | #undef do_func |
| 33 | 34 | #undef Elf_Addr |
| 34 | 35 | #undef Elf_Ehdr |
| @@ -56,6 +57,7 @@ · 21 unmodified lines | ||
| 56 | 57 | # define __has_rel_mcount __has64_rel_mcount |
| 57 | 58 | # define has_rel_mcount has64_rel_mcount |
| 58 | 59 | # define tot_relsize tot64_relsize |
| 60 | +# define get_sym_str_and_relp get_sym_str_and_relp_64 | |
| 59 | 61 | # define do_func do64 |
| 60 | 62 | # define get_mcountsym get_mcountsym_64 |
| 61 | 63 | # define is_fake_mcount is_fake_mcount64 |
| @@ -86,6 +88,7 @@ · 24 unmodified lines | ||
| 86 | 88 | # define __has_rel_mcount __has32_rel_mcount |
| 87 | 89 | # define has_rel_mcount has32_rel_mcount |
| 88 | 90 | # define tot_relsize tot32_relsize |
| 91 | +# define get_sym_str_and_relp get_sym_str_and_relp_32 | |
| 89 | 92 | # define do_func do32 |
| 90 | 93 | # define get_mcountsym get_mcountsym_32 |
| 91 | 94 | # define is_fake_mcount is_fake_mcount32 |
| @@ -260,6 +263,29 @@ static unsigned get_mcountsym(Elf_Sym const *const sym0, · 168 unmodified lines | ||
| 260 | 263 | return mcountsym; |
| 261 | 264 | } |
| 262 | 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 | + | |
| 263 | 289 | /* |
| 264 | 290 | * Look at the relocations in order to find the calls to mcount. |
| 265 | 291 | * Accumulate the section offsets that are found, and their relocation info, |
| @@ -276,26 +302,16 @@ static uint_t *sift_rel_mcount(uint_t *mlocp, · 10 unmodified lines | ||
| 276 | 302 | { |
| 277 | 303 | uint_t *const mloc0 = mlocp; |
| 278 | 304 | Elf_Rel *mrelp = *mrelpp; |
| 279 | − Elf_Shdr *const shdr0 = (Elf_Shdr *)(_w(ehdr->e_shoff) | |
| 280 | − + (void *)ehdr); | |
| 281 | − unsigned const symsec_sh_link = w(relhdr->sh_link); | |
| 282 | − Elf_Shdr const *const symsec = &shdr0[symsec_sh_link]; | |
| 283 | − Elf_Sym const *const sym0 = (Elf_Sym const *)(_w(symsec->sh_offset) | |
| 284 | − + (void *)ehdr); | |
| 285 | − | |
| 286 | − Elf_Shdr const *const strsec = &shdr0[w(symsec->sh_link)]; | |
| 287 | − char const *const str0 = (char const *)(_w(strsec->sh_offset) | |
| 288 | − + (void *)ehdr); | |
| 289 | − | |
| 290 | − Elf_Rel const *const rel0 = (Elf_Rel const *)(_w(relhdr->sh_offset) | |
| 291 | − + (void *)ehdr); | |
| 305 | + Elf_Sym const *sym0; | |
| 306 | + char const *str0; | |
| 307 | + Elf_Rel const *relp; | |
| 292 | 308 | unsigned rel_entsize = _w(relhdr->sh_entsize); |
| 293 | 309 | unsigned const nrel = _w(relhdr->sh_size) / rel_entsize; |
| 294 | − Elf_Rel const *relp = rel0; | |
| 295 | − | |
| 296 | 310 | unsigned mcountsym = 0; |
| 297 | 311 | unsigned t; |
| 298 | 312 | |
| 313 | + get_sym_str_and_relp(relhdr, ehdr, &sym0, &str0, &relp); | |
| 314 | + | |
| 299 | 315 | for (t = nrel; t; --t) { |
| 300 | 316 | if (!mcountsym) |
| 301 | 317 | mcountsym = get_mcountsym(sym0, relp, str0); |
| @@ -331,27 +347,18 @@ static void nop_mcount(Elf_Shdr const *const relhdr, · 29 unmodified lines | ||
| 331 | 347 | { |
| 332 | 348 | Elf_Shdr *const shdr0 = (Elf_Shdr *)(_w(ehdr->e_shoff) |
| 333 | 349 | + (void *)ehdr); |
| 334 | − unsigned const symsec_sh_link = w(relhdr->sh_link); | |
| 335 | − Elf_Shdr const *const symsec = &shdr0[symsec_sh_link]; | |
| 336 | − Elf_Sym const *const sym0 = (Elf_Sym const *)(_w(symsec->sh_offset) | |
| 337 | − + (void *)ehdr); | |
| 338 | − | |
| 339 | − Elf_Shdr const *const strsec = &shdr0[w(symsec->sh_link)]; | |
| 340 | − char const *const str0 = (char const *)(_w(strsec->sh_offset) | |
| 341 | − + (void *)ehdr); | |
| 342 | − | |
| 343 | − Elf_Rel const *const rel0 = (Elf_Rel const *)(_w(relhdr->sh_offset) | |
| 344 | − + (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)]; | |
| 345 | 354 | unsigned rel_entsize = _w(relhdr->sh_entsize); |
| 346 | 355 | unsigned const nrel = _w(relhdr->sh_size) / rel_entsize; |
| 347 | − Elf_Rel const *relp = rel0; | |
| 348 | − | |
| 349 | − Elf_Shdr const *const shdr = &shdr0[w(relhdr->sh_info)]; | |
| 350 | − | |
| 351 | 356 | unsigned mcountsym = 0; |
| 352 | 357 | unsigned t; |
| 353 | 358 | int once = 0; |
| 354 | 359 | |
| 360 | + get_sym_str_and_relp(relhdr, ehdr, &sym0, &str0, &relp); | |
| 361 | + | |
| 355 | 362 | for (t = nrel; t; --t) { |
| 356 | 363 | int ret = -1; |
| 357 | 364 | |