Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  * recordmcount.h
0004  *
0005  * This code was taken out of recordmcount.c written by
0006  * Copyright 2009 John F. Reiser <jreiser@BitWagon.com>.  All rights reserved.
0007  *
0008  * The original code had the same algorithms for both 32bit
0009  * and 64bit ELF files, but the code was duplicated to support
0010  * the difference in structures that were used. This
0011  * file creates a macro of everything that is different between
0012  * the 64 and 32 bit code, such that by including this header
0013  * twice we can create both sets of functions by including this
0014  * header once with RECORD_MCOUNT_64 undefined, and again with
0015  * it defined.
0016  *
0017  * This conversion to macros was done by:
0018  * Copyright 2010 Steven Rostedt <srostedt@redhat.com>, Red Hat Inc.
0019  */
0020 #undef append_func
0021 #undef is_fake_mcount
0022 #undef fn_is_fake_mcount
0023 #undef MIPS_is_fake_mcount
0024 #undef mcount_adjust
0025 #undef sift_rel_mcount
0026 #undef nop_mcount
0027 #undef find_secsym_ndx
0028 #undef __has_rel_mcount
0029 #undef has_rel_mcount
0030 #undef tot_relsize
0031 #undef get_mcountsym
0032 #undef find_symtab
0033 #undef get_shnum
0034 #undef set_shnum
0035 #undef get_shstrndx
0036 #undef get_symindex
0037 #undef get_sym_str_and_relp
0038 #undef do_func
0039 #undef Elf_Addr
0040 #undef Elf_Ehdr
0041 #undef Elf_Shdr
0042 #undef Elf_Rel
0043 #undef Elf_Rela
0044 #undef Elf_Sym
0045 #undef ELF_R_SYM
0046 #undef Elf_r_sym
0047 #undef ELF_R_INFO
0048 #undef Elf_r_info
0049 #undef ELF_ST_BIND
0050 #undef ELF_ST_TYPE
0051 #undef fn_ELF_R_SYM
0052 #undef fn_ELF_R_INFO
0053 #undef uint_t
0054 #undef _w
0055 #undef _align
0056 #undef _size
0057 
0058 #ifdef RECORD_MCOUNT_64
0059 # define append_func        append64
0060 # define sift_rel_mcount    sift64_rel_mcount
0061 # define nop_mcount     nop_mcount_64
0062 # define find_secsym_ndx    find64_secsym_ndx
0063 # define __has_rel_mcount   __has64_rel_mcount
0064 # define has_rel_mcount     has64_rel_mcount
0065 # define tot_relsize        tot64_relsize
0066 # define find_symtab        find_symtab64
0067 # define get_shnum      get_shnum64
0068 # define set_shnum      set_shnum64
0069 # define get_shstrndx       get_shstrndx64
0070 # define get_symindex       get_symindex64
0071 # define get_sym_str_and_relp   get_sym_str_and_relp_64
0072 # define do_func        do64
0073 # define get_mcountsym      get_mcountsym_64
0074 # define is_fake_mcount     is_fake_mcount64
0075 # define fn_is_fake_mcount  fn_is_fake_mcount64
0076 # define MIPS_is_fake_mcount    MIPS64_is_fake_mcount
0077 # define mcount_adjust      mcount_adjust_64
0078 # define Elf_Addr       Elf64_Addr
0079 # define Elf_Ehdr       Elf64_Ehdr
0080 # define Elf_Shdr       Elf64_Shdr
0081 # define Elf_Rel        Elf64_Rel
0082 # define Elf_Rela       Elf64_Rela
0083 # define Elf_Sym        Elf64_Sym
0084 # define ELF_R_SYM      ELF64_R_SYM
0085 # define Elf_r_sym      Elf64_r_sym
0086 # define ELF_R_INFO     ELF64_R_INFO
0087 # define Elf_r_info     Elf64_r_info
0088 # define ELF_ST_BIND        ELF64_ST_BIND
0089 # define ELF_ST_TYPE        ELF64_ST_TYPE
0090 # define fn_ELF_R_SYM       fn_ELF64_R_SYM
0091 # define fn_ELF_R_INFO      fn_ELF64_R_INFO
0092 # define uint_t         uint64_t
0093 # define _w         w8
0094 # define _align         7u
0095 # define _size          8
0096 #else
0097 # define append_func        append32
0098 # define sift_rel_mcount    sift32_rel_mcount
0099 # define nop_mcount     nop_mcount_32
0100 # define find_secsym_ndx    find32_secsym_ndx
0101 # define __has_rel_mcount   __has32_rel_mcount
0102 # define has_rel_mcount     has32_rel_mcount
0103 # define tot_relsize        tot32_relsize
0104 # define find_symtab        find_symtab32
0105 # define get_shnum      get_shnum32
0106 # define set_shnum      set_shnum32
0107 # define get_shstrndx       get_shstrndx32
0108 # define get_symindex       get_symindex32
0109 # define get_sym_str_and_relp   get_sym_str_and_relp_32
0110 # define do_func        do32
0111 # define get_mcountsym      get_mcountsym_32
0112 # define is_fake_mcount     is_fake_mcount32
0113 # define fn_is_fake_mcount  fn_is_fake_mcount32
0114 # define MIPS_is_fake_mcount    MIPS32_is_fake_mcount
0115 # define mcount_adjust      mcount_adjust_32
0116 # define Elf_Addr       Elf32_Addr
0117 # define Elf_Ehdr       Elf32_Ehdr
0118 # define Elf_Shdr       Elf32_Shdr
0119 # define Elf_Rel        Elf32_Rel
0120 # define Elf_Rela       Elf32_Rela
0121 # define Elf_Sym        Elf32_Sym
0122 # define ELF_R_SYM      ELF32_R_SYM
0123 # define Elf_r_sym      Elf32_r_sym
0124 # define ELF_R_INFO     ELF32_R_INFO
0125 # define Elf_r_info     Elf32_r_info
0126 # define ELF_ST_BIND        ELF32_ST_BIND
0127 # define ELF_ST_TYPE        ELF32_ST_TYPE
0128 # define fn_ELF_R_SYM       fn_ELF32_R_SYM
0129 # define fn_ELF_R_INFO      fn_ELF32_R_INFO
0130 # define uint_t         uint32_t
0131 # define _w         w
0132 # define _align         3u
0133 # define _size          4
0134 #endif
0135 
0136 /* Functions and pointers that do_file() may override for specific e_machine. */
0137 static int fn_is_fake_mcount(Elf_Rel const *rp)
0138 {
0139     return 0;
0140 }
0141 static int (*is_fake_mcount)(Elf_Rel const *rp) = fn_is_fake_mcount;
0142 
0143 static uint_t fn_ELF_R_SYM(Elf_Rel const *rp)
0144 {
0145     return ELF_R_SYM(_w(rp->r_info));
0146 }
0147 static uint_t (*Elf_r_sym)(Elf_Rel const *rp) = fn_ELF_R_SYM;
0148 
0149 static void fn_ELF_R_INFO(Elf_Rel *const rp, unsigned sym, unsigned type)
0150 {
0151     rp->r_info = _w(ELF_R_INFO(sym, type));
0152 }
0153 static void (*Elf_r_info)(Elf_Rel *const rp, unsigned sym, unsigned type) = fn_ELF_R_INFO;
0154 
0155 static int mcount_adjust = 0;
0156 
0157 /*
0158  * MIPS mcount long call has 2 _mcount symbols, only the position of the 1st
0159  * _mcount symbol is needed for dynamic function tracer, with it, to disable
0160  * tracing(ftrace_make_nop), the instruction in the position is replaced with
0161  * the "b label" instruction, to enable tracing(ftrace_make_call), replace the
0162  * instruction back. So, here, we set the 2nd one as fake and filter it.
0163  *
0164  * c:   3c030000    lui v1,0x0      <-->    b   label
0165  *      c: R_MIPS_HI16  _mcount
0166  *      c: R_MIPS_NONE  *ABS*
0167  *      c: R_MIPS_NONE  *ABS*
0168  * 10:  64630000    daddiu  v1,v1,0
0169  *      10: R_MIPS_LO16 _mcount
0170  *      10: R_MIPS_NONE *ABS*
0171  *      10: R_MIPS_NONE *ABS*
0172  * 14:  03e0082d    move    at,ra
0173  * 18:  0060f809    jalr    v1
0174  * label:
0175  */
0176 #define MIPS_FAKEMCOUNT_OFFSET  4
0177 
0178 static int MIPS_is_fake_mcount(Elf_Rel const *rp)
0179 {
0180     static Elf_Addr old_r_offset = ~(Elf_Addr)0;
0181     Elf_Addr current_r_offset = _w(rp->r_offset);
0182     int is_fake;
0183 
0184     is_fake = (old_r_offset != ~(Elf_Addr)0) &&
0185         (current_r_offset - old_r_offset == MIPS_FAKEMCOUNT_OFFSET);
0186     old_r_offset = current_r_offset;
0187 
0188     return is_fake;
0189 }
0190 
0191 static unsigned int get_symindex(Elf_Sym const *sym, Elf32_Word const *symtab,
0192                  Elf32_Word const *symtab_shndx)
0193 {
0194     unsigned long offset;
0195     unsigned short shndx = w2(sym->st_shndx);
0196     int index;
0197 
0198     if (shndx > SHN_UNDEF && shndx < SHN_LORESERVE)
0199         return shndx;
0200 
0201     if (shndx == SHN_XINDEX) {
0202         offset = (unsigned long)sym - (unsigned long)symtab;
0203         index = offset / sizeof(*sym);
0204 
0205         return w(symtab_shndx[index]);
0206     }
0207 
0208     return 0;
0209 }
0210 
0211 static unsigned int get_shnum(Elf_Ehdr const *ehdr, Elf_Shdr const *shdr0)
0212 {
0213     if (shdr0 && !ehdr->e_shnum)
0214         return w(shdr0->sh_size);
0215 
0216     return w2(ehdr->e_shnum);
0217 }
0218 
0219 static void set_shnum(Elf_Ehdr *ehdr, Elf_Shdr *shdr0, unsigned int new_shnum)
0220 {
0221     if (new_shnum >= SHN_LORESERVE) {
0222         ehdr->e_shnum = 0;
0223         shdr0->sh_size = w(new_shnum);
0224     } else
0225         ehdr->e_shnum = w2(new_shnum);
0226 }
0227 
0228 static int get_shstrndx(Elf_Ehdr const *ehdr, Elf_Shdr const *shdr0)
0229 {
0230     if (ehdr->e_shstrndx != SHN_XINDEX)
0231         return w2(ehdr->e_shstrndx);
0232 
0233     return w(shdr0->sh_link);
0234 }
0235 
0236 static void find_symtab(Elf_Ehdr *const ehdr, Elf_Shdr const *shdr0,
0237             unsigned const nhdr, Elf32_Word **symtab,
0238             Elf32_Word **symtab_shndx)
0239 {
0240     Elf_Shdr const *relhdr;
0241     unsigned k;
0242 
0243     *symtab = NULL;
0244     *symtab_shndx = NULL;
0245 
0246     for (relhdr = shdr0, k = nhdr; k; --k, ++relhdr) {
0247         if (relhdr->sh_type == SHT_SYMTAB)
0248             *symtab = (void *)ehdr + relhdr->sh_offset;
0249         else if (relhdr->sh_type == SHT_SYMTAB_SHNDX)
0250             *symtab_shndx = (void *)ehdr + relhdr->sh_offset;
0251 
0252         if (*symtab && *symtab_shndx)
0253             break;
0254     }
0255 }
0256 
0257 /* Append the new shstrtab, Elf_Shdr[], __mcount_loc and its relocations. */
0258 static int append_func(Elf_Ehdr *const ehdr,
0259             Elf_Shdr *const shstr,
0260             uint_t const *const mloc0,
0261             uint_t const *const mlocp,
0262             Elf_Rel const *const mrel0,
0263             Elf_Rel const *const mrelp,
0264             unsigned int const rel_entsize,
0265             unsigned int const symsec_sh_link)
0266 {
0267     /* Begin constructing output file */
0268     Elf_Shdr mcsec;
0269     char const *mc_name = (sizeof(Elf_Rela) == rel_entsize)
0270         ? ".rela__mcount_loc"
0271         :  ".rel__mcount_loc";
0272     uint_t const old_shoff = _w(ehdr->e_shoff);
0273     uint_t const old_shstr_sh_size   = _w(shstr->sh_size);
0274     uint_t const old_shstr_sh_offset = _w(shstr->sh_offset);
0275     Elf_Shdr *const shdr0 = (Elf_Shdr *)(old_shoff + (void *)ehdr);
0276     unsigned int const old_shnum = get_shnum(ehdr, shdr0);
0277     unsigned int const new_shnum = 2 + old_shnum; /* {.rel,}__mcount_loc */
0278     uint_t t = 1 + strlen(mc_name) + _w(shstr->sh_size);
0279     uint_t new_e_shoff;
0280 
0281     shstr->sh_size = _w(t);
0282     shstr->sh_offset = _w(sb.st_size);
0283     t += sb.st_size;
0284     t += (_align & -t);  /* word-byte align */
0285     new_e_shoff = t;
0286 
0287     set_shnum(ehdr, shdr0, new_shnum);
0288 
0289     /* body for new shstrtab */
0290     if (ulseek(sb.st_size, SEEK_SET) < 0)
0291         return -1;
0292     if (uwrite(old_shstr_sh_offset + (void *)ehdr, old_shstr_sh_size) < 0)
0293         return -1;
0294     if (uwrite(mc_name, 1 + strlen(mc_name)) < 0)
0295         return -1;
0296 
0297     /* old(modified) Elf_Shdr table, word-byte aligned */
0298     if (ulseek(t, SEEK_SET) < 0)
0299         return -1;
0300     t += sizeof(Elf_Shdr) * old_shnum;
0301     if (uwrite(old_shoff + (void *)ehdr,
0302            sizeof(Elf_Shdr) * old_shnum) < 0)
0303         return -1;
0304 
0305     /* new sections __mcount_loc and .rel__mcount_loc */
0306     t += 2*sizeof(mcsec);
0307     mcsec.sh_name = w((sizeof(Elf_Rela) == rel_entsize) + strlen(".rel")
0308         + old_shstr_sh_size);
0309     mcsec.sh_type = w(SHT_PROGBITS);
0310     mcsec.sh_flags = _w(SHF_ALLOC);
0311     mcsec.sh_addr = 0;
0312     mcsec.sh_offset = _w(t);
0313     mcsec.sh_size = _w((void *)mlocp - (void *)mloc0);
0314     mcsec.sh_link = 0;
0315     mcsec.sh_info = 0;
0316     mcsec.sh_addralign = _w(_size);
0317     mcsec.sh_entsize = _w(_size);
0318     if (uwrite(&mcsec, sizeof(mcsec)) < 0)
0319         return -1;
0320 
0321     mcsec.sh_name = w(old_shstr_sh_size);
0322     mcsec.sh_type = (sizeof(Elf_Rela) == rel_entsize)
0323         ? w(SHT_RELA)
0324         : w(SHT_REL);
0325     mcsec.sh_flags = 0;
0326     mcsec.sh_addr = 0;
0327     mcsec.sh_offset = _w((void *)mlocp - (void *)mloc0 + t);
0328     mcsec.sh_size   = _w((void *)mrelp - (void *)mrel0);
0329     mcsec.sh_link = w(symsec_sh_link);
0330     mcsec.sh_info = w(old_shnum);
0331     mcsec.sh_addralign = _w(_size);
0332     mcsec.sh_entsize = _w(rel_entsize);
0333 
0334     if (uwrite(&mcsec, sizeof(mcsec)) < 0)
0335         return -1;
0336 
0337     if (uwrite(mloc0, (void *)mlocp - (void *)mloc0) < 0)
0338         return -1;
0339     if (uwrite(mrel0, (void *)mrelp - (void *)mrel0) < 0)
0340         return -1;
0341 
0342     ehdr->e_shoff = _w(new_e_shoff);
0343     if (ulseek(0, SEEK_SET) < 0)
0344         return -1;
0345     if (uwrite(ehdr, sizeof(*ehdr)) < 0)
0346         return -1;
0347     return 0;
0348 }
0349 
0350 static unsigned get_mcountsym(Elf_Sym const *const sym0,
0351                   Elf_Rel const *relp,
0352                   char const *const str0)
0353 {
0354     unsigned mcountsym = 0;
0355 
0356     Elf_Sym const *const symp =
0357         &sym0[Elf_r_sym(relp)];
0358     char const *symname = &str0[w(symp->st_name)];
0359     char const *mcount = gpfx == '_' ? "_mcount" : "mcount";
0360     char const *fentry = "__fentry__";
0361 
0362     if (symname[0] == '.')
0363         ++symname;  /* ppc64 hack */
0364     if (strcmp(mcount, symname) == 0 ||
0365         (altmcount && strcmp(altmcount, symname) == 0) ||
0366         (strcmp(fentry, symname) == 0))
0367         mcountsym = Elf_r_sym(relp);
0368 
0369     return mcountsym;
0370 }
0371 
0372 static void get_sym_str_and_relp(Elf_Shdr const *const relhdr,
0373                  Elf_Ehdr const *const ehdr,
0374                  Elf_Sym const **sym0,
0375                  char const **str0,
0376                  Elf_Rel const **relp)
0377 {
0378     Elf_Shdr *const shdr0 = (Elf_Shdr *)(_w(ehdr->e_shoff)
0379         + (void *)ehdr);
0380     unsigned const symsec_sh_link = w(relhdr->sh_link);
0381     Elf_Shdr const *const symsec = &shdr0[symsec_sh_link];
0382     Elf_Shdr const *const strsec = &shdr0[w(symsec->sh_link)];
0383     Elf_Rel const *const rel0 = (Elf_Rel const *)(_w(relhdr->sh_offset)
0384         + (void *)ehdr);
0385 
0386     *sym0 = (Elf_Sym const *)(_w(symsec->sh_offset)
0387                   + (void *)ehdr);
0388 
0389     *str0 = (char const *)(_w(strsec->sh_offset)
0390                    + (void *)ehdr);
0391 
0392     *relp = rel0;
0393 }
0394 
0395 /*
0396  * Look at the relocations in order to find the calls to mcount.
0397  * Accumulate the section offsets that are found, and their relocation info,
0398  * onto the end of the existing arrays.
0399  */
0400 static uint_t *sift_rel_mcount(uint_t *mlocp,
0401                    unsigned const offbase,
0402                    Elf_Rel **const mrelpp,
0403                    Elf_Shdr const *const relhdr,
0404                    Elf_Ehdr const *const ehdr,
0405                    unsigned const recsym,
0406                    uint_t const recval,
0407                    unsigned const reltype)
0408 {
0409     uint_t *const mloc0 = mlocp;
0410     Elf_Rel *mrelp = *mrelpp;
0411     Elf_Sym const *sym0;
0412     char const *str0;
0413     Elf_Rel const *relp;
0414     unsigned rel_entsize = _w(relhdr->sh_entsize);
0415     unsigned const nrel = _w(relhdr->sh_size) / rel_entsize;
0416     unsigned mcountsym = 0;
0417     unsigned t;
0418 
0419     get_sym_str_and_relp(relhdr, ehdr, &sym0, &str0, &relp);
0420 
0421     for (t = nrel; t; --t) {
0422         if (!mcountsym)
0423             mcountsym = get_mcountsym(sym0, relp, str0);
0424 
0425         if (mcountsym && mcountsym == Elf_r_sym(relp) &&
0426                 !is_fake_mcount(relp)) {
0427             uint_t const addend =
0428                 _w(_w(relp->r_offset) - recval + mcount_adjust);
0429             mrelp->r_offset = _w(offbase
0430                 + ((void *)mlocp - (void *)mloc0));
0431             Elf_r_info(mrelp, recsym, reltype);
0432             if (rel_entsize == sizeof(Elf_Rela)) {
0433                 ((Elf_Rela *)mrelp)->r_addend = addend;
0434                 *mlocp++ = 0;
0435             } else
0436                 *mlocp++ = addend;
0437 
0438             mrelp = (Elf_Rel *)(rel_entsize + (void *)mrelp);
0439         }
0440         relp = (Elf_Rel const *)(rel_entsize + (void *)relp);
0441     }
0442     *mrelpp = mrelp;
0443     return mlocp;
0444 }
0445 
0446 /*
0447  * Read the relocation table again, but this time its called on sections
0448  * that are not going to be traced. The mcount calls here will be converted
0449  * into nops.
0450  */
0451 static int nop_mcount(Elf_Shdr const *const relhdr,
0452               Elf_Ehdr const *const ehdr,
0453               const char *const txtname)
0454 {
0455     Elf_Shdr *const shdr0 = (Elf_Shdr *)(_w(ehdr->e_shoff)
0456         + (void *)ehdr);
0457     Elf_Sym const *sym0;
0458     char const *str0;
0459     Elf_Rel const *relp;
0460     Elf_Shdr const *const shdr = &shdr0[w(relhdr->sh_info)];
0461     unsigned rel_entsize = _w(relhdr->sh_entsize);
0462     unsigned const nrel = _w(relhdr->sh_size) / rel_entsize;
0463     unsigned mcountsym = 0;
0464     unsigned t;
0465     int once = 0;
0466 
0467     get_sym_str_and_relp(relhdr, ehdr, &sym0, &str0, &relp);
0468 
0469     for (t = nrel; t; --t) {
0470         int ret = -1;
0471 
0472         if (!mcountsym)
0473             mcountsym = get_mcountsym(sym0, relp, str0);
0474 
0475         if (mcountsym == Elf_r_sym(relp) && !is_fake_mcount(relp)) {
0476             if (make_nop)
0477                 ret = make_nop((void *)ehdr, _w(shdr->sh_offset) + _w(relp->r_offset));
0478             if (warn_on_notrace_sect && !once) {
0479                 printf("Section %s has mcount callers being ignored\n",
0480                        txtname);
0481                 once = 1;
0482                 /* just warn? */
0483                 if (!make_nop)
0484                     return 0;
0485             }
0486         }
0487 
0488         /*
0489          * If we successfully removed the mcount, mark the relocation
0490          * as a nop (don't do anything with it).
0491          */
0492         if (!ret) {
0493             Elf_Rel rel;
0494             rel = *(Elf_Rel *)relp;
0495             Elf_r_info(&rel, Elf_r_sym(relp), rel_type_nop);
0496             if (ulseek((void *)relp - (void *)ehdr, SEEK_SET) < 0)
0497                 return -1;
0498             if (uwrite(&rel, sizeof(rel)) < 0)
0499                 return -1;
0500         }
0501         relp = (Elf_Rel const *)(rel_entsize + (void *)relp);
0502     }
0503     return 0;
0504 }
0505 
0506 /*
0507  * Find a symbol in the given section, to be used as the base for relocating
0508  * the table of offsets of calls to mcount.  A local or global symbol suffices,
0509  * but avoid a Weak symbol because it may be overridden; the change in value
0510  * would invalidate the relocations of the offsets of the calls to mcount.
0511  * Often the found symbol will be the unnamed local symbol generated by
0512  * GNU 'as' for the start of each section.  For example:
0513  *    Num:    Value  Size Type    Bind   Vis      Ndx Name
0514  *      2: 00000000     0 SECTION LOCAL  DEFAULT    1
0515  */
0516 static int find_secsym_ndx(unsigned const txtndx,
0517                 char const *const txtname,
0518                 uint_t *const recvalp,
0519                 unsigned int *sym_index,
0520                 Elf_Shdr const *const symhdr,
0521                 Elf32_Word const *symtab,
0522                 Elf32_Word const *symtab_shndx,
0523                 Elf_Ehdr const *const ehdr)
0524 {
0525     Elf_Sym const *const sym0 = (Elf_Sym const *)(_w(symhdr->sh_offset)
0526         + (void *)ehdr);
0527     unsigned const nsym = _w(symhdr->sh_size) / _w(symhdr->sh_entsize);
0528     Elf_Sym const *symp;
0529     unsigned t;
0530 
0531     for (symp = sym0, t = nsym; t; --t, ++symp) {
0532         unsigned int const st_bind = ELF_ST_BIND(symp->st_info);
0533 
0534         if (txtndx == get_symindex(symp, symtab, symtab_shndx)
0535             /* avoid STB_WEAK */
0536             && (STB_LOCAL == st_bind || STB_GLOBAL == st_bind)) {
0537             /* function symbols on ARM have quirks, avoid them */
0538             if (w2(ehdr->e_machine) == EM_ARM
0539                 && ELF_ST_TYPE(symp->st_info) == STT_FUNC)
0540                 continue;
0541 
0542             *recvalp = _w(symp->st_value);
0543             *sym_index = symp - sym0;
0544             return 0;
0545         }
0546     }
0547     fprintf(stderr, "Cannot find symbol for section %u: %s.\n",
0548         txtndx, txtname);
0549     return -1;
0550 }
0551 
0552 /* Evade ISO C restriction: no declaration after statement in has_rel_mcount. */
0553 static char const * __has_rel_mcount(Elf_Shdr const *const relhdr, /* reltype */
0554                      Elf_Shdr const *const shdr0,
0555                      char const *const shstrtab,
0556                      char const *const fname)
0557 {
0558     /* .sh_info depends on .sh_type == SHT_REL[,A] */
0559     Elf_Shdr const *const txthdr = &shdr0[w(relhdr->sh_info)];
0560     char const *const txtname = &shstrtab[w(txthdr->sh_name)];
0561 
0562     if (strcmp("__mcount_loc", txtname) == 0) {
0563         fprintf(stderr, "warning: __mcount_loc already exists: %s\n",
0564             fname);
0565         return already_has_rel_mcount;
0566     }
0567     if (w(txthdr->sh_type) != SHT_PROGBITS ||
0568         !(_w(txthdr->sh_flags) & SHF_EXECINSTR))
0569         return NULL;
0570     return txtname;
0571 }
0572 
0573 static char const *has_rel_mcount(Elf_Shdr const *const relhdr,
0574                   Elf_Shdr const *const shdr0,
0575                   char const *const shstrtab,
0576                   char const *const fname)
0577 {
0578     if (w(relhdr->sh_type) != SHT_REL && w(relhdr->sh_type) != SHT_RELA)
0579         return NULL;
0580     return __has_rel_mcount(relhdr, shdr0, shstrtab, fname);
0581 }
0582 
0583 
0584 static unsigned tot_relsize(Elf_Shdr const *const shdr0,
0585                 unsigned nhdr,
0586                 const char *const shstrtab,
0587                 const char *const fname)
0588 {
0589     unsigned totrelsz = 0;
0590     Elf_Shdr const *shdrp = shdr0;
0591     char const *txtname;
0592 
0593     for (; nhdr; --nhdr, ++shdrp) {
0594         txtname = has_rel_mcount(shdrp, shdr0, shstrtab, fname);
0595         if (txtname == already_has_rel_mcount) {
0596             totrelsz = 0;
0597             break;
0598         }
0599         if (txtname && is_mcounted_section_name(txtname))
0600             totrelsz += _w(shdrp->sh_size);
0601     }
0602     return totrelsz;
0603 }
0604 
0605 /* Overall supervision for Elf32 ET_REL file. */
0606 static int do_func(Elf_Ehdr *const ehdr, char const *const fname,
0607            unsigned const reltype)
0608 {
0609     Elf_Shdr *const shdr0 = (Elf_Shdr *)(_w(ehdr->e_shoff)
0610         + (void *)ehdr);
0611     unsigned const nhdr = get_shnum(ehdr, shdr0);
0612     Elf_Shdr *const shstr = &shdr0[get_shstrndx(ehdr, shdr0)];
0613     char const *const shstrtab = (char const *)(_w(shstr->sh_offset)
0614         + (void *)ehdr);
0615 
0616     Elf_Shdr const *relhdr;
0617     unsigned k;
0618 
0619     Elf32_Word *symtab;
0620     Elf32_Word *symtab_shndx;
0621 
0622     /* Upper bound on space: assume all relevant relocs are for mcount. */
0623     unsigned       totrelsz;
0624 
0625     Elf_Rel *      mrel0;
0626     Elf_Rel *      mrelp;
0627 
0628     uint_t *      mloc0;
0629     uint_t *      mlocp;
0630 
0631     unsigned rel_entsize = 0;
0632     unsigned symsec_sh_link = 0;
0633 
0634     int result = 0;
0635 
0636     totrelsz = tot_relsize(shdr0, nhdr, shstrtab, fname);
0637     if (totrelsz == 0)
0638         return 0;
0639     mrel0 = umalloc(totrelsz);
0640     mrelp = mrel0;
0641     if (!mrel0)
0642         return -1;
0643 
0644     /* 2*sizeof(address) <= sizeof(Elf_Rel) */
0645     mloc0 = umalloc(totrelsz>>1);
0646     mlocp = mloc0;
0647     if (!mloc0) {
0648         free(mrel0);
0649         return -1;
0650     }
0651 
0652     find_symtab(ehdr, shdr0, nhdr, &symtab, &symtab_shndx);
0653 
0654     for (relhdr = shdr0, k = nhdr; k; --k, ++relhdr) {
0655         char const *const txtname = has_rel_mcount(relhdr, shdr0,
0656             shstrtab, fname);
0657         if (txtname == already_has_rel_mcount) {
0658             result = 0;
0659             file_updated = 0;
0660             goto out; /* Nothing to be done; don't append! */
0661         }
0662         if (txtname && is_mcounted_section_name(txtname)) {
0663             unsigned int recsym;
0664             uint_t recval = 0;
0665 
0666             symsec_sh_link = w(relhdr->sh_link);
0667             result = find_secsym_ndx(w(relhdr->sh_info), txtname,
0668                         &recval, &recsym,
0669                         &shdr0[symsec_sh_link],
0670                         symtab, symtab_shndx,
0671                         ehdr);
0672             if (result)
0673                 goto out;
0674 
0675             rel_entsize = _w(relhdr->sh_entsize);
0676             mlocp = sift_rel_mcount(mlocp,
0677                 (void *)mlocp - (void *)mloc0, &mrelp,
0678                 relhdr, ehdr, recsym, recval, reltype);
0679         } else if (txtname && (warn_on_notrace_sect || make_nop)) {
0680             /*
0681              * This section is ignored by ftrace, but still
0682              * has mcount calls. Convert them to nops now.
0683              */
0684             if (nop_mcount(relhdr, ehdr, txtname) < 0) {
0685                 result = -1;
0686                 goto out;
0687             }
0688         }
0689     }
0690     if (!result && mloc0 != mlocp)
0691         result = append_func(ehdr, shstr, mloc0, mlocp, mrel0, mrelp,
0692                      rel_entsize, symsec_sh_link);
0693 out:
0694     free(mrel0);
0695     free(mloc0);
0696     return result;
0697 }