~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/scripts/recordmcount.h

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /scripts/recordmcount.h (Version linux-6.12-rc7) and /scripts/recordmcount.h (Version linux-6.11.7)


  1 /* SPDX-License-Identifier: GPL-2.0-only */         1 /* SPDX-License-Identifier: GPL-2.0-only */
  2 /*                                                  2 /*
  3  * recordmcount.h                                   3  * recordmcount.h
  4  *                                                  4  *
  5  * This code was taken out of recordmcount.c w      5  * This code was taken out of recordmcount.c written by
  6  * Copyright 2009 John F. Reiser <jreiser@BitW      6  * Copyright 2009 John F. Reiser <jreiser@BitWagon.com>.  All rights reserved.
  7  *                                                  7  *
  8  * The original code had the same algorithms f      8  * The original code had the same algorithms for both 32bit
  9  * and 64bit ELF files, but the code was dupli      9  * and 64bit ELF files, but the code was duplicated to support
 10  * the difference in structures that were used     10  * the difference in structures that were used. This
 11  * file creates a macro of everything that is      11  * file creates a macro of everything that is different between
 12  * the 64 and 32 bit code, such that by includ     12  * the 64 and 32 bit code, such that by including this header
 13  * twice we can create both sets of functions      13  * twice we can create both sets of functions by including this
 14  * header once with RECORD_MCOUNT_64 undefined     14  * header once with RECORD_MCOUNT_64 undefined, and again with
 15  * it defined.                                     15  * it defined.
 16  *                                                 16  *
 17  * This conversion to macros was done by:          17  * This conversion to macros was done by:
 18  * Copyright 2010 Steven Rostedt <srostedt@red     18  * Copyright 2010 Steven Rostedt <srostedt@redhat.com>, Red Hat Inc.
 19  */                                                19  */
 20 #undef append_func                                 20 #undef append_func
 21 #undef is_fake_mcount                              21 #undef is_fake_mcount
 22 #undef fn_is_fake_mcount                           22 #undef fn_is_fake_mcount
 23 #undef MIPS_is_fake_mcount                         23 #undef MIPS_is_fake_mcount
 24 #undef mcount_adjust                               24 #undef mcount_adjust
 25 #undef sift_rel_mcount                             25 #undef sift_rel_mcount
 26 #undef nop_mcount                                  26 #undef nop_mcount
 27 #undef find_secsym_ndx                             27 #undef find_secsym_ndx
 28 #undef __has_rel_mcount                            28 #undef __has_rel_mcount
 29 #undef has_rel_mcount                              29 #undef has_rel_mcount
 30 #undef tot_relsize                                 30 #undef tot_relsize
 31 #undef get_mcountsym                               31 #undef get_mcountsym
 32 #undef find_symtab                                 32 #undef find_symtab
 33 #undef get_shnum                                   33 #undef get_shnum
 34 #undef set_shnum                                   34 #undef set_shnum
 35 #undef get_shstrndx                                35 #undef get_shstrndx
 36 #undef get_symindex                                36 #undef get_symindex
 37 #undef get_sym_str_and_relp                        37 #undef get_sym_str_and_relp
 38 #undef do_func                                     38 #undef do_func
 39 #undef Elf_Addr                                    39 #undef Elf_Addr
 40 #undef Elf_Ehdr                                    40 #undef Elf_Ehdr
 41 #undef Elf_Shdr                                    41 #undef Elf_Shdr
 42 #undef Elf_Rel                                     42 #undef Elf_Rel
 43 #undef Elf_Rela                                    43 #undef Elf_Rela
 44 #undef Elf_Sym                                     44 #undef Elf_Sym
 45 #undef ELF_R_SYM                                   45 #undef ELF_R_SYM
 46 #undef Elf_r_sym                                   46 #undef Elf_r_sym
 47 #undef ELF_R_INFO                                  47 #undef ELF_R_INFO
 48 #undef Elf_r_info                                  48 #undef Elf_r_info
 49 #undef ELF_ST_BIND                                 49 #undef ELF_ST_BIND
 50 #undef ELF_ST_TYPE                                 50 #undef ELF_ST_TYPE
 51 #undef fn_ELF_R_SYM                                51 #undef fn_ELF_R_SYM
 52 #undef fn_ELF_R_INFO                               52 #undef fn_ELF_R_INFO
 53 #undef uint_t                                      53 #undef uint_t
 54 #undef _w                                          54 #undef _w
 55 #undef _align                                      55 #undef _align
 56 #undef _size                                       56 #undef _size
 57                                                    57 
 58 #ifdef RECORD_MCOUNT_64                            58 #ifdef RECORD_MCOUNT_64
 59 # define append_func            append64           59 # define append_func            append64
 60 # define sift_rel_mcount        sift64_rel_mco     60 # define sift_rel_mcount        sift64_rel_mcount
 61 # define nop_mcount             nop_mcount_64      61 # define nop_mcount             nop_mcount_64
 62 # define find_secsym_ndx        find64_secsym_     62 # define find_secsym_ndx        find64_secsym_ndx
 63 # define __has_rel_mcount       __has64_rel_mc     63 # define __has_rel_mcount       __has64_rel_mcount
 64 # define has_rel_mcount         has64_rel_mcou     64 # define has_rel_mcount         has64_rel_mcount
 65 # define tot_relsize            tot64_relsize      65 # define tot_relsize            tot64_relsize
 66 # define find_symtab            find_symtab64      66 # define find_symtab            find_symtab64
 67 # define get_shnum              get_shnum64        67 # define get_shnum              get_shnum64
 68 # define set_shnum              set_shnum64        68 # define set_shnum              set_shnum64
 69 # define get_shstrndx           get_shstrndx64     69 # define get_shstrndx           get_shstrndx64
 70 # define get_symindex           get_symindex64     70 # define get_symindex           get_symindex64
 71 # define get_sym_str_and_relp   get_sym_str_an     71 # define get_sym_str_and_relp   get_sym_str_and_relp_64
 72 # define do_func                do64               72 # define do_func                do64
 73 # define get_mcountsym          get_mcountsym_     73 # define get_mcountsym          get_mcountsym_64
 74 # define is_fake_mcount         is_fake_mcount     74 # define is_fake_mcount         is_fake_mcount64
 75 # define fn_is_fake_mcount      fn_is_fake_mco     75 # define fn_is_fake_mcount      fn_is_fake_mcount64
 76 # define MIPS_is_fake_mcount    MIPS64_is_fake     76 # define MIPS_is_fake_mcount    MIPS64_is_fake_mcount
 77 # define mcount_adjust          mcount_adjust_     77 # define mcount_adjust          mcount_adjust_64
 78 # define Elf_Addr               Elf64_Addr         78 # define Elf_Addr               Elf64_Addr
 79 # define Elf_Ehdr               Elf64_Ehdr         79 # define Elf_Ehdr               Elf64_Ehdr
 80 # define Elf_Shdr               Elf64_Shdr         80 # define Elf_Shdr               Elf64_Shdr
 81 # define Elf_Rel                Elf64_Rel          81 # define Elf_Rel                Elf64_Rel
 82 # define Elf_Rela               Elf64_Rela         82 # define Elf_Rela               Elf64_Rela
 83 # define Elf_Sym                Elf64_Sym          83 # define Elf_Sym                Elf64_Sym
 84 # define ELF_R_SYM              ELF64_R_SYM        84 # define ELF_R_SYM              ELF64_R_SYM
 85 # define Elf_r_sym              Elf64_r_sym        85 # define Elf_r_sym              Elf64_r_sym
 86 # define ELF_R_INFO             ELF64_R_INFO       86 # define ELF_R_INFO             ELF64_R_INFO
 87 # define Elf_r_info             Elf64_r_info       87 # define Elf_r_info             Elf64_r_info
 88 # define ELF_ST_BIND            ELF64_ST_BIND      88 # define ELF_ST_BIND            ELF64_ST_BIND
 89 # define ELF_ST_TYPE            ELF64_ST_TYPE      89 # define ELF_ST_TYPE            ELF64_ST_TYPE
 90 # define fn_ELF_R_SYM           fn_ELF64_R_SYM     90 # define fn_ELF_R_SYM           fn_ELF64_R_SYM
 91 # define fn_ELF_R_INFO          fn_ELF64_R_INF     91 # define fn_ELF_R_INFO          fn_ELF64_R_INFO
 92 # define uint_t                 uint64_t           92 # define uint_t                 uint64_t
 93 # define _w                     w8                 93 # define _w                     w8
 94 # define _align                 7u                 94 # define _align                 7u
 95 # define _size                  8                  95 # define _size                  8
 96 #else                                              96 #else
 97 # define append_func            append32           97 # define append_func            append32
 98 # define sift_rel_mcount        sift32_rel_mco     98 # define sift_rel_mcount        sift32_rel_mcount
 99 # define nop_mcount             nop_mcount_32      99 # define nop_mcount             nop_mcount_32
100 # define find_secsym_ndx        find32_secsym_    100 # define find_secsym_ndx        find32_secsym_ndx
101 # define __has_rel_mcount       __has32_rel_mc    101 # define __has_rel_mcount       __has32_rel_mcount
102 # define has_rel_mcount         has32_rel_mcou    102 # define has_rel_mcount         has32_rel_mcount
103 # define tot_relsize            tot32_relsize     103 # define tot_relsize            tot32_relsize
104 # define find_symtab            find_symtab32     104 # define find_symtab            find_symtab32
105 # define get_shnum              get_shnum32       105 # define get_shnum              get_shnum32
106 # define set_shnum              set_shnum32       106 # define set_shnum              set_shnum32
107 # define get_shstrndx           get_shstrndx32    107 # define get_shstrndx           get_shstrndx32
108 # define get_symindex           get_symindex32    108 # define get_symindex           get_symindex32
109 # define get_sym_str_and_relp   get_sym_str_an    109 # define get_sym_str_and_relp   get_sym_str_and_relp_32
110 # define do_func                do32              110 # define do_func                do32
111 # define get_mcountsym          get_mcountsym_    111 # define get_mcountsym          get_mcountsym_32
112 # define is_fake_mcount         is_fake_mcount    112 # define is_fake_mcount         is_fake_mcount32
113 # define fn_is_fake_mcount      fn_is_fake_mco    113 # define fn_is_fake_mcount      fn_is_fake_mcount32
114 # define MIPS_is_fake_mcount    MIPS32_is_fake    114 # define MIPS_is_fake_mcount    MIPS32_is_fake_mcount
115 # define mcount_adjust          mcount_adjust_    115 # define mcount_adjust          mcount_adjust_32
116 # define Elf_Addr               Elf32_Addr        116 # define Elf_Addr               Elf32_Addr
117 # define Elf_Ehdr               Elf32_Ehdr        117 # define Elf_Ehdr               Elf32_Ehdr
118 # define Elf_Shdr               Elf32_Shdr        118 # define Elf_Shdr               Elf32_Shdr
119 # define Elf_Rel                Elf32_Rel         119 # define Elf_Rel                Elf32_Rel
120 # define Elf_Rela               Elf32_Rela        120 # define Elf_Rela               Elf32_Rela
121 # define Elf_Sym                Elf32_Sym         121 # define Elf_Sym                Elf32_Sym
122 # define ELF_R_SYM              ELF32_R_SYM       122 # define ELF_R_SYM              ELF32_R_SYM
123 # define Elf_r_sym              Elf32_r_sym       123 # define Elf_r_sym              Elf32_r_sym
124 # define ELF_R_INFO             ELF32_R_INFO      124 # define ELF_R_INFO             ELF32_R_INFO
125 # define Elf_r_info             Elf32_r_info      125 # define Elf_r_info             Elf32_r_info
126 # define ELF_ST_BIND            ELF32_ST_BIND     126 # define ELF_ST_BIND            ELF32_ST_BIND
127 # define ELF_ST_TYPE            ELF32_ST_TYPE     127 # define ELF_ST_TYPE            ELF32_ST_TYPE
128 # define fn_ELF_R_SYM           fn_ELF32_R_SYM    128 # define fn_ELF_R_SYM           fn_ELF32_R_SYM
129 # define fn_ELF_R_INFO          fn_ELF32_R_INF    129 # define fn_ELF_R_INFO          fn_ELF32_R_INFO
130 # define uint_t                 uint32_t          130 # define uint_t                 uint32_t
131 # define _w                     w                 131 # define _w                     w
132 # define _align                 3u                132 # define _align                 3u
133 # define _size                  4                 133 # define _size                  4
134 #endif                                            134 #endif
135                                                   135 
136 /* Functions and pointers that do_file() may o    136 /* Functions and pointers that do_file() may override for specific e_machine. */
137 static int fn_is_fake_mcount(Elf_Rel const *rp    137 static int fn_is_fake_mcount(Elf_Rel const *rp)
138 {                                                 138 {
139         return 0;                                 139         return 0;
140 }                                                 140 }
141 static int (*is_fake_mcount)(Elf_Rel const *rp    141 static int (*is_fake_mcount)(Elf_Rel const *rp) = fn_is_fake_mcount;
142                                                   142 
143 static uint_t fn_ELF_R_SYM(Elf_Rel const *rp)     143 static uint_t fn_ELF_R_SYM(Elf_Rel const *rp)
144 {                                                 144 {
145         return ELF_R_SYM(_w(rp->r_info));         145         return ELF_R_SYM(_w(rp->r_info));
146 }                                                 146 }
147 static uint_t (*Elf_r_sym)(Elf_Rel const *rp)     147 static uint_t (*Elf_r_sym)(Elf_Rel const *rp) = fn_ELF_R_SYM;
148                                                   148 
149 static void fn_ELF_R_INFO(Elf_Rel *const rp, u    149 static void fn_ELF_R_INFO(Elf_Rel *const rp, unsigned sym, unsigned type)
150 {                                                 150 {
151         rp->r_info = _w(ELF_R_INFO(sym, type))    151         rp->r_info = _w(ELF_R_INFO(sym, type));
152 }                                                 152 }
153 static void (*Elf_r_info)(Elf_Rel *const rp, u    153 static void (*Elf_r_info)(Elf_Rel *const rp, unsigned sym, unsigned type) = fn_ELF_R_INFO;
154                                                   154 
155 static int mcount_adjust = 0;                     155 static int mcount_adjust = 0;
156                                                   156 
157 /*                                                157 /*
158  * MIPS mcount long call has 2 _mcount symbols    158  * MIPS mcount long call has 2 _mcount symbols, only the position of the 1st
159  * _mcount symbol is needed for dynamic functi    159  * _mcount symbol is needed for dynamic function tracer, with it, to disable
160  * tracing(ftrace_make_nop), the instruction i    160  * tracing(ftrace_make_nop), the instruction in the position is replaced with
161  * the "b label" instruction, to enable tracin    161  * the "b label" instruction, to enable tracing(ftrace_make_call), replace the
162  * instruction back. So, here, we set the 2nd     162  * instruction back. So, here, we set the 2nd one as fake and filter it.
163  *                                                163  *
164  * c:   3c030000        lui     v1,0x0            164  * c:   3c030000        lui     v1,0x0          <-->    b       label
165  *              c: R_MIPS_HI16  _mcount           165  *              c: R_MIPS_HI16  _mcount
166  *              c: R_MIPS_NONE  *ABS*             166  *              c: R_MIPS_NONE  *ABS*
167  *              c: R_MIPS_NONE  *ABS*             167  *              c: R_MIPS_NONE  *ABS*
168  * 10:  64630000        daddiu  v1,v1,0           168  * 10:  64630000        daddiu  v1,v1,0
169  *              10: R_MIPS_LO16 _mcount           169  *              10: R_MIPS_LO16 _mcount
170  *              10: R_MIPS_NONE *ABS*             170  *              10: R_MIPS_NONE *ABS*
171  *              10: R_MIPS_NONE *ABS*             171  *              10: R_MIPS_NONE *ABS*
172  * 14:  03e0082d        move    at,ra             172  * 14:  03e0082d        move    at,ra
173  * 18:  0060f809        jalr    v1                173  * 18:  0060f809        jalr    v1
174  * label:                                         174  * label:
175  */                                               175  */
176 #define MIPS_FAKEMCOUNT_OFFSET  4                 176 #define MIPS_FAKEMCOUNT_OFFSET  4
177                                                   177 
178 static int MIPS_is_fake_mcount(Elf_Rel const *    178 static int MIPS_is_fake_mcount(Elf_Rel const *rp)
179 {                                                 179 {
180         static Elf_Addr old_r_offset = ~(Elf_A    180         static Elf_Addr old_r_offset = ~(Elf_Addr)0;
181         Elf_Addr current_r_offset = _w(rp->r_o    181         Elf_Addr current_r_offset = _w(rp->r_offset);
182         int is_fake;                              182         int is_fake;
183                                                   183 
184         is_fake = (old_r_offset != ~(Elf_Addr)    184         is_fake = (old_r_offset != ~(Elf_Addr)0) &&
185                 (current_r_offset - old_r_offs    185                 (current_r_offset - old_r_offset == MIPS_FAKEMCOUNT_OFFSET);
186         old_r_offset = current_r_offset;          186         old_r_offset = current_r_offset;
187                                                   187 
188         return is_fake;                           188         return is_fake;
189 }                                                 189 }
190                                                   190 
191 static unsigned int get_symindex(Elf_Sym const    191 static unsigned int get_symindex(Elf_Sym const *sym, Elf32_Word const *symtab,
192                                  Elf32_Word co    192                                  Elf32_Word const *symtab_shndx)
193 {                                                 193 {
194         unsigned long offset;                     194         unsigned long offset;
195         unsigned short shndx = w2(sym->st_shnd    195         unsigned short shndx = w2(sym->st_shndx);
196         int index;                                196         int index;
197                                                   197 
198         if (shndx > SHN_UNDEF && shndx < SHN_L    198         if (shndx > SHN_UNDEF && shndx < SHN_LORESERVE)
199                 return shndx;                     199                 return shndx;
200                                                   200 
201         if (shndx == SHN_XINDEX) {                201         if (shndx == SHN_XINDEX) {
202                 offset = (unsigned long)sym -     202                 offset = (unsigned long)sym - (unsigned long)symtab;
203                 index = offset / sizeof(*sym);    203                 index = offset / sizeof(*sym);
204                                                   204 
205                 return w(symtab_shndx[index]);    205                 return w(symtab_shndx[index]);
206         }                                         206         }
207                                                   207 
208         return 0;                                 208         return 0;
209 }                                                 209 }
210                                                   210 
211 static unsigned int get_shnum(Elf_Ehdr const *    211 static unsigned int get_shnum(Elf_Ehdr const *ehdr, Elf_Shdr const *shdr0)
212 {                                                 212 {
213         if (shdr0 && !ehdr->e_shnum)              213         if (shdr0 && !ehdr->e_shnum)
214                 return w(shdr0->sh_size);         214                 return w(shdr0->sh_size);
215                                                   215 
216         return w2(ehdr->e_shnum);                 216         return w2(ehdr->e_shnum);
217 }                                                 217 }
218                                                   218 
219 static void set_shnum(Elf_Ehdr *ehdr, Elf_Shdr    219 static void set_shnum(Elf_Ehdr *ehdr, Elf_Shdr *shdr0, unsigned int new_shnum)
220 {                                                 220 {
221         if (new_shnum >= SHN_LORESERVE) {         221         if (new_shnum >= SHN_LORESERVE) {
222                 ehdr->e_shnum = 0;                222                 ehdr->e_shnum = 0;
223                 shdr0->sh_size = w(new_shnum);    223                 shdr0->sh_size = w(new_shnum);
224         } else                                    224         } else
225                 ehdr->e_shnum = w2(new_shnum);    225                 ehdr->e_shnum = w2(new_shnum);
226 }                                                 226 }
227                                                   227 
228 static int get_shstrndx(Elf_Ehdr const *ehdr,     228 static int get_shstrndx(Elf_Ehdr const *ehdr, Elf_Shdr const *shdr0)
229 {                                                 229 {
230         if (ehdr->e_shstrndx != SHN_XINDEX)       230         if (ehdr->e_shstrndx != SHN_XINDEX)
231                 return w2(ehdr->e_shstrndx);      231                 return w2(ehdr->e_shstrndx);
232                                                   232 
233         return w(shdr0->sh_link);                 233         return w(shdr0->sh_link);
234 }                                                 234 }
235                                                   235 
236 static void find_symtab(Elf_Ehdr *const ehdr,     236 static void find_symtab(Elf_Ehdr *const ehdr, Elf_Shdr const *shdr0,
237                         unsigned const nhdr, E    237                         unsigned const nhdr, Elf32_Word **symtab,
238                         Elf32_Word **symtab_sh    238                         Elf32_Word **symtab_shndx)
239 {                                                 239 {
240         Elf_Shdr const *relhdr;                   240         Elf_Shdr const *relhdr;
241         unsigned k;                               241         unsigned k;
242                                                   242 
243         *symtab = NULL;                           243         *symtab = NULL;
244         *symtab_shndx = NULL;                     244         *symtab_shndx = NULL;
245                                                   245 
246         for (relhdr = shdr0, k = nhdr; k; --k,    246         for (relhdr = shdr0, k = nhdr; k; --k, ++relhdr) {
247                 if (relhdr->sh_type == SHT_SYM    247                 if (relhdr->sh_type == SHT_SYMTAB)
248                         *symtab = (void *)ehdr    248                         *symtab = (void *)ehdr + relhdr->sh_offset;
249                 else if (relhdr->sh_type == SH    249                 else if (relhdr->sh_type == SHT_SYMTAB_SHNDX)
250                         *symtab_shndx = (void     250                         *symtab_shndx = (void *)ehdr + relhdr->sh_offset;
251                                                   251 
252                 if (*symtab && *symtab_shndx)     252                 if (*symtab && *symtab_shndx)
253                         break;                    253                         break;
254         }                                         254         }
255 }                                                 255 }
256                                                   256 
257 /* Append the new shstrtab, Elf_Shdr[], __mcou    257 /* Append the new shstrtab, Elf_Shdr[], __mcount_loc and its relocations. */
258 static int append_func(Elf_Ehdr *const ehdr,      258 static int append_func(Elf_Ehdr *const ehdr,
259                         Elf_Shdr *const shstr,    259                         Elf_Shdr *const shstr,
260                         uint_t const *const ml    260                         uint_t const *const mloc0,
261                         uint_t const *const ml    261                         uint_t const *const mlocp,
262                         Elf_Rel const *const m    262                         Elf_Rel const *const mrel0,
263                         Elf_Rel const *const m    263                         Elf_Rel const *const mrelp,
264                         unsigned int const rel    264                         unsigned int const rel_entsize,
265                         unsigned int const sym    265                         unsigned int const symsec_sh_link)
266 {                                                 266 {
267         /* Begin constructing output file */      267         /* Begin constructing output file */
268         Elf_Shdr mcsec;                           268         Elf_Shdr mcsec;
269         char const *mc_name = (sizeof(Elf_Rela    269         char const *mc_name = (sizeof(Elf_Rela) == rel_entsize)
270                 ? ".rela__mcount_loc"             270                 ? ".rela__mcount_loc"
271                 :  ".rel__mcount_loc";            271                 :  ".rel__mcount_loc";
272         uint_t const old_shoff = _w(ehdr->e_sh    272         uint_t const old_shoff = _w(ehdr->e_shoff);
273         uint_t const old_shstr_sh_size   = _w(    273         uint_t const old_shstr_sh_size   = _w(shstr->sh_size);
274         uint_t const old_shstr_sh_offset = _w(    274         uint_t const old_shstr_sh_offset = _w(shstr->sh_offset);
275         Elf_Shdr *const shdr0 = (Elf_Shdr *)(o    275         Elf_Shdr *const shdr0 = (Elf_Shdr *)(old_shoff + (void *)ehdr);
276         unsigned int const old_shnum = get_shn    276         unsigned int const old_shnum = get_shnum(ehdr, shdr0);
277         unsigned int const new_shnum = 2 + old    277         unsigned int const new_shnum = 2 + old_shnum; /* {.rel,}__mcount_loc */
278         uint_t t = 1 + strlen(mc_name) + _w(sh    278         uint_t t = 1 + strlen(mc_name) + _w(shstr->sh_size);
279         uint_t new_e_shoff;                       279         uint_t new_e_shoff;
280                                                   280 
281         shstr->sh_size = _w(t);                   281         shstr->sh_size = _w(t);
282         shstr->sh_offset = _w(sb.st_size);        282         shstr->sh_offset = _w(sb.st_size);
283         t += sb.st_size;                          283         t += sb.st_size;
284         t += (_align & -t);  /* word-byte alig    284         t += (_align & -t);  /* word-byte align */
285         new_e_shoff = t;                          285         new_e_shoff = t;
286                                                   286 
287         set_shnum(ehdr, shdr0, new_shnum);        287         set_shnum(ehdr, shdr0, new_shnum);
288                                                   288 
289         /* body for new shstrtab */               289         /* body for new shstrtab */
290         if (ulseek(sb.st_size, SEEK_SET) < 0)     290         if (ulseek(sb.st_size, SEEK_SET) < 0)
291                 return -1;                        291                 return -1;
292         if (uwrite(old_shstr_sh_offset + (void    292         if (uwrite(old_shstr_sh_offset + (void *)ehdr, old_shstr_sh_size) < 0)
293                 return -1;                        293                 return -1;
294         if (uwrite(mc_name, 1 + strlen(mc_name    294         if (uwrite(mc_name, 1 + strlen(mc_name)) < 0)
295                 return -1;                        295                 return -1;
296                                                   296 
297         /* old(modified) Elf_Shdr table, word-    297         /* old(modified) Elf_Shdr table, word-byte aligned */
298         if (ulseek(t, SEEK_SET) < 0)              298         if (ulseek(t, SEEK_SET) < 0)
299                 return -1;                        299                 return -1;
300         t += sizeof(Elf_Shdr) * old_shnum;        300         t += sizeof(Elf_Shdr) * old_shnum;
301         if (uwrite(old_shoff + (void *)ehdr,      301         if (uwrite(old_shoff + (void *)ehdr,
302                sizeof(Elf_Shdr) * old_shnum) <    302                sizeof(Elf_Shdr) * old_shnum) < 0)
303                 return -1;                        303                 return -1;
304                                                   304 
305         /* new sections __mcount_loc and .rel_    305         /* new sections __mcount_loc and .rel__mcount_loc */
306         t += 2*sizeof(mcsec);                     306         t += 2*sizeof(mcsec);
307         mcsec.sh_name = w((sizeof(Elf_Rela) ==    307         mcsec.sh_name = w((sizeof(Elf_Rela) == rel_entsize) + strlen(".rel")
308                 + old_shstr_sh_size);             308                 + old_shstr_sh_size);
309         mcsec.sh_type = w(SHT_PROGBITS);          309         mcsec.sh_type = w(SHT_PROGBITS);
310         mcsec.sh_flags = _w(SHF_ALLOC);           310         mcsec.sh_flags = _w(SHF_ALLOC);
311         mcsec.sh_addr = 0;                        311         mcsec.sh_addr = 0;
312         mcsec.sh_offset = _w(t);                  312         mcsec.sh_offset = _w(t);
313         mcsec.sh_size = _w((void *)mlocp - (vo    313         mcsec.sh_size = _w((void *)mlocp - (void *)mloc0);
314         mcsec.sh_link = 0;                        314         mcsec.sh_link = 0;
315         mcsec.sh_info = 0;                        315         mcsec.sh_info = 0;
316         mcsec.sh_addralign = _w(_size);           316         mcsec.sh_addralign = _w(_size);
317         mcsec.sh_entsize = _w(_size);             317         mcsec.sh_entsize = _w(_size);
318         if (uwrite(&mcsec, sizeof(mcsec)) < 0)    318         if (uwrite(&mcsec, sizeof(mcsec)) < 0)
319                 return -1;                        319                 return -1;
320                                                   320 
321         mcsec.sh_name = w(old_shstr_sh_size);     321         mcsec.sh_name = w(old_shstr_sh_size);
322         mcsec.sh_type = (sizeof(Elf_Rela) == r    322         mcsec.sh_type = (sizeof(Elf_Rela) == rel_entsize)
323                 ? w(SHT_RELA)                     323                 ? w(SHT_RELA)
324                 : w(SHT_REL);                     324                 : w(SHT_REL);
325         mcsec.sh_flags = 0;                       325         mcsec.sh_flags = 0;
326         mcsec.sh_addr = 0;                        326         mcsec.sh_addr = 0;
327         mcsec.sh_offset = _w((void *)mlocp - (    327         mcsec.sh_offset = _w((void *)mlocp - (void *)mloc0 + t);
328         mcsec.sh_size   = _w((void *)mrelp - (    328         mcsec.sh_size   = _w((void *)mrelp - (void *)mrel0);
329         mcsec.sh_link = w(symsec_sh_link);        329         mcsec.sh_link = w(symsec_sh_link);
330         mcsec.sh_info = w(old_shnum);             330         mcsec.sh_info = w(old_shnum);
331         mcsec.sh_addralign = _w(_size);           331         mcsec.sh_addralign = _w(_size);
332         mcsec.sh_entsize = _w(rel_entsize);       332         mcsec.sh_entsize = _w(rel_entsize);
333                                                   333 
334         if (uwrite(&mcsec, sizeof(mcsec)) < 0)    334         if (uwrite(&mcsec, sizeof(mcsec)) < 0)
335                 return -1;                        335                 return -1;
336                                                   336 
337         if (uwrite(mloc0, (void *)mlocp - (voi    337         if (uwrite(mloc0, (void *)mlocp - (void *)mloc0) < 0)
338                 return -1;                        338                 return -1;
339         if (uwrite(mrel0, (void *)mrelp - (voi    339         if (uwrite(mrel0, (void *)mrelp - (void *)mrel0) < 0)
340                 return -1;                        340                 return -1;
341                                                   341 
342         ehdr->e_shoff = _w(new_e_shoff);          342         ehdr->e_shoff = _w(new_e_shoff);
343         if (ulseek(0, SEEK_SET) < 0)              343         if (ulseek(0, SEEK_SET) < 0)
344                 return -1;                        344                 return -1;
345         if (uwrite(ehdr, sizeof(*ehdr)) < 0)      345         if (uwrite(ehdr, sizeof(*ehdr)) < 0)
346                 return -1;                        346                 return -1;
347         return 0;                                 347         return 0;
348 }                                                 348 }
349                                                   349 
350 static unsigned get_mcountsym(Elf_Sym const *c    350 static unsigned get_mcountsym(Elf_Sym const *const sym0,
351                               Elf_Rel const *r    351                               Elf_Rel const *relp,
352                               char const *cons    352                               char const *const str0)
353 {                                                 353 {
354         unsigned mcountsym = 0;                   354         unsigned mcountsym = 0;
355                                                   355 
356         Elf_Sym const *const symp =               356         Elf_Sym const *const symp =
357                 &sym0[Elf_r_sym(relp)];           357                 &sym0[Elf_r_sym(relp)];
358         char const *symname = &str0[w(symp->st    358         char const *symname = &str0[w(symp->st_name)];
359         char const *mcount = gpfx == '_' ? "_m    359         char const *mcount = gpfx == '_' ? "_mcount" : "mcount";
360         char const *fentry = "__fentry__";        360         char const *fentry = "__fentry__";
361                                                   361 
362         if (symname[0] == '.')                    362         if (symname[0] == '.')
363                 ++symname;  /* ppc64 hack */      363                 ++symname;  /* ppc64 hack */
364         if (strcmp(mcount, symname) == 0 ||       364         if (strcmp(mcount, symname) == 0 ||
365             (altmcount && strcmp(altmcount, sy    365             (altmcount && strcmp(altmcount, symname) == 0) ||
366             (strcmp(fentry, symname) == 0))       366             (strcmp(fentry, symname) == 0))
367                 mcountsym = Elf_r_sym(relp);      367                 mcountsym = Elf_r_sym(relp);
368                                                   368 
369         return mcountsym;                         369         return mcountsym;
370 }                                                 370 }
371                                                   371 
372 static void get_sym_str_and_relp(Elf_Shdr cons    372 static void get_sym_str_and_relp(Elf_Shdr const *const relhdr,
373                                  Elf_Ehdr cons    373                                  Elf_Ehdr const *const ehdr,
374                                  Elf_Sym const    374                                  Elf_Sym const **sym0,
375                                  char const **    375                                  char const **str0,
376                                  Elf_Rel const    376                                  Elf_Rel const **relp)
377 {                                                 377 {
378         Elf_Shdr *const shdr0 = (Elf_Shdr *)(_    378         Elf_Shdr *const shdr0 = (Elf_Shdr *)(_w(ehdr->e_shoff)
379                 + (void *)ehdr);                  379                 + (void *)ehdr);
380         unsigned const symsec_sh_link = w(relh    380         unsigned const symsec_sh_link = w(relhdr->sh_link);
381         Elf_Shdr const *const symsec = &shdr0[    381         Elf_Shdr const *const symsec = &shdr0[symsec_sh_link];
382         Elf_Shdr const *const strsec = &shdr0[    382         Elf_Shdr const *const strsec = &shdr0[w(symsec->sh_link)];
383         Elf_Rel const *const rel0 = (Elf_Rel c    383         Elf_Rel const *const rel0 = (Elf_Rel const *)(_w(relhdr->sh_offset)
384                 + (void *)ehdr);                  384                 + (void *)ehdr);
385                                                   385 
386         *sym0 = (Elf_Sym const *)(_w(symsec->s    386         *sym0 = (Elf_Sym const *)(_w(symsec->sh_offset)
387                                   + (void *)eh    387                                   + (void *)ehdr);
388                                                   388 
389         *str0 = (char const *)(_w(strsec->sh_o    389         *str0 = (char const *)(_w(strsec->sh_offset)
390                                + (void *)ehdr)    390                                + (void *)ehdr);
391                                                   391 
392         *relp = rel0;                             392         *relp = rel0;
393 }                                                 393 }
394                                                   394 
395 /*                                                395 /*
396  * Look at the relocations in order to find th    396  * Look at the relocations in order to find the calls to mcount.
397  * Accumulate the section offsets that are fou    397  * Accumulate the section offsets that are found, and their relocation info,
398  * onto the end of the existing arrays.           398  * onto the end of the existing arrays.
399  */                                               399  */
400 static uint_t *sift_rel_mcount(uint_t *mlocp,     400 static uint_t *sift_rel_mcount(uint_t *mlocp,
401                                unsigned const     401                                unsigned const offbase,
402                                Elf_Rel **const    402                                Elf_Rel **const mrelpp,
403                                Elf_Shdr const     403                                Elf_Shdr const *const relhdr,
404                                Elf_Ehdr const     404                                Elf_Ehdr const *const ehdr,
405                                unsigned const     405                                unsigned const recsym,
406                                uint_t const re    406                                uint_t const recval,
407                                unsigned const     407                                unsigned const reltype)
408 {                                                 408 {
409         uint_t *const mloc0 = mlocp;              409         uint_t *const mloc0 = mlocp;
410         Elf_Rel *mrelp = *mrelpp;                 410         Elf_Rel *mrelp = *mrelpp;
411         Elf_Sym const *sym0;                      411         Elf_Sym const *sym0;
412         char const *str0;                         412         char const *str0;
413         Elf_Rel const *relp;                      413         Elf_Rel const *relp;
414         unsigned rel_entsize = _w(relhdr->sh_e    414         unsigned rel_entsize = _w(relhdr->sh_entsize);
415         unsigned const nrel = _w(relhdr->sh_si    415         unsigned const nrel = _w(relhdr->sh_size) / rel_entsize;
416         unsigned mcountsym = 0;                   416         unsigned mcountsym = 0;
417         unsigned t;                               417         unsigned t;
418                                                   418 
419         get_sym_str_and_relp(relhdr, ehdr, &sy    419         get_sym_str_and_relp(relhdr, ehdr, &sym0, &str0, &relp);
420                                                   420 
421         for (t = nrel; t; --t) {                  421         for (t = nrel; t; --t) {
422                 if (!mcountsym)                   422                 if (!mcountsym)
423                         mcountsym = get_mcount    423                         mcountsym = get_mcountsym(sym0, relp, str0);
424                                                   424 
425                 if (mcountsym && mcountsym ==     425                 if (mcountsym && mcountsym == Elf_r_sym(relp) &&
426                                 !is_fake_mcoun    426                                 !is_fake_mcount(relp)) {
427                         uint_t const addend =     427                         uint_t const addend =
428                                 _w(_w(relp->r_    428                                 _w(_w(relp->r_offset) - recval + mcount_adjust);
429                         mrelp->r_offset = _w(o    429                         mrelp->r_offset = _w(offbase
430                                 + ((void *)mlo    430                                 + ((void *)mlocp - (void *)mloc0));
431                         Elf_r_info(mrelp, recs    431                         Elf_r_info(mrelp, recsym, reltype);
432                         if (rel_entsize == siz    432                         if (rel_entsize == sizeof(Elf_Rela)) {
433                                 ((Elf_Rela *)m    433                                 ((Elf_Rela *)mrelp)->r_addend = addend;
434                                 *mlocp++ = 0;     434                                 *mlocp++ = 0;
435                         } else                    435                         } else
436                                 *mlocp++ = add    436                                 *mlocp++ = addend;
437                                                   437 
438                         mrelp = (Elf_Rel *)(re    438                         mrelp = (Elf_Rel *)(rel_entsize + (void *)mrelp);
439                 }                                 439                 }
440                 relp = (Elf_Rel const *)(rel_e    440                 relp = (Elf_Rel const *)(rel_entsize + (void *)relp);
441         }                                         441         }
442         *mrelpp = mrelp;                          442         *mrelpp = mrelp;
443         return mlocp;                             443         return mlocp;
444 }                                                 444 }
445                                                   445 
446 /*                                                446 /*
447  * Read the relocation table again, but this t    447  * Read the relocation table again, but this time its called on sections
448  * that are not going to be traced. The mcount    448  * that are not going to be traced. The mcount calls here will be converted
449  * into nops.                                     449  * into nops.
450  */                                               450  */
451 static int nop_mcount(Elf_Shdr const *const re    451 static int nop_mcount(Elf_Shdr const *const relhdr,
452                       Elf_Ehdr const *const eh    452                       Elf_Ehdr const *const ehdr,
453                       const char *const txtnam    453                       const char *const txtname)
454 {                                                 454 {
455         Elf_Shdr *const shdr0 = (Elf_Shdr *)(_    455         Elf_Shdr *const shdr0 = (Elf_Shdr *)(_w(ehdr->e_shoff)
456                 + (void *)ehdr);                  456                 + (void *)ehdr);
457         Elf_Sym const *sym0;                      457         Elf_Sym const *sym0;
458         char const *str0;                         458         char const *str0;
459         Elf_Rel const *relp;                      459         Elf_Rel const *relp;
460         Elf_Shdr const *const shdr = &shdr0[w(    460         Elf_Shdr const *const shdr = &shdr0[w(relhdr->sh_info)];
461         unsigned rel_entsize = _w(relhdr->sh_e    461         unsigned rel_entsize = _w(relhdr->sh_entsize);
462         unsigned const nrel = _w(relhdr->sh_si    462         unsigned const nrel = _w(relhdr->sh_size) / rel_entsize;
463         unsigned mcountsym = 0;                   463         unsigned mcountsym = 0;
464         unsigned t;                               464         unsigned t;
465         int once = 0;                             465         int once = 0;
466                                                   466 
467         get_sym_str_and_relp(relhdr, ehdr, &sy    467         get_sym_str_and_relp(relhdr, ehdr, &sym0, &str0, &relp);
468                                                   468 
469         for (t = nrel; t; --t) {                  469         for (t = nrel; t; --t) {
470                 int ret = -1;                     470                 int ret = -1;
471                                                   471 
472                 if (!mcountsym)                   472                 if (!mcountsym)
473                         mcountsym = get_mcount    473                         mcountsym = get_mcountsym(sym0, relp, str0);
474                                                   474 
475                 if (mcountsym == Elf_r_sym(rel    475                 if (mcountsym == Elf_r_sym(relp) && !is_fake_mcount(relp)) {
476                         if (make_nop)             476                         if (make_nop)
477                                 ret = make_nop    477                                 ret = make_nop((void *)ehdr, _w(shdr->sh_offset) + _w(relp->r_offset));
478                         if (warn_on_notrace_se    478                         if (warn_on_notrace_sect && !once) {
479                                 printf("Sectio    479                                 printf("Section %s has mcount callers being ignored\n",
480                                        txtname    480                                        txtname);
481                                 once = 1;         481                                 once = 1;
482                                 /* just warn?     482                                 /* just warn? */
483                                 if (!make_nop)    483                                 if (!make_nop)
484                                         return    484                                         return 0;
485                         }                         485                         }
486                 }                                 486                 }
487                                                   487 
488                 /*                                488                 /*
489                  * If we successfully removed     489                  * If we successfully removed the mcount, mark the relocation
490                  * as a nop (don't do anything    490                  * as a nop (don't do anything with it).
491                  */                               491                  */
492                 if (!ret) {                       492                 if (!ret) {
493                         Elf_Rel rel;              493                         Elf_Rel rel;
494                         rel = *(Elf_Rel *)relp    494                         rel = *(Elf_Rel *)relp;
495                         Elf_r_info(&rel, Elf_r    495                         Elf_r_info(&rel, Elf_r_sym(relp), rel_type_nop);
496                         if (ulseek((void *)rel    496                         if (ulseek((void *)relp - (void *)ehdr, SEEK_SET) < 0)
497                                 return -1;        497                                 return -1;
498                         if (uwrite(&rel, sizeo    498                         if (uwrite(&rel, sizeof(rel)) < 0)
499                                 return -1;        499                                 return -1;
500                 }                                 500                 }
501                 relp = (Elf_Rel const *)(rel_e    501                 relp = (Elf_Rel const *)(rel_entsize + (void *)relp);
502         }                                         502         }
503         return 0;                                 503         return 0;
504 }                                                 504 }
505                                                   505 
506 /*                                                506 /*
507  * Find a symbol in the given section, to be u    507  * Find a symbol in the given section, to be used as the base for relocating
508  * the table of offsets of calls to mcount.  A    508  * the table of offsets of calls to mcount.  A local or global symbol suffices,
509  * but avoid a Weak symbol because it may be o    509  * but avoid a Weak symbol because it may be overridden; the change in value
510  * would invalidate the relocations of the off    510  * would invalidate the relocations of the offsets of the calls to mcount.
511  * Often the found symbol will be the unnamed     511  * Often the found symbol will be the unnamed local symbol generated by
512  * GNU 'as' for the start of each section.  Fo    512  * GNU 'as' for the start of each section.  For example:
513  *    Num:    Value  Size Type    Bind   Vis      513  *    Num:    Value  Size Type    Bind   Vis      Ndx Name
514  *      2: 00000000     0 SECTION LOCAL  DEFAU    514  *      2: 00000000     0 SECTION LOCAL  DEFAULT    1
515  */                                               515  */
516 static int find_secsym_ndx(unsigned const txtn    516 static int find_secsym_ndx(unsigned const txtndx,
517                                 char const *co    517                                 char const *const txtname,
518                                 uint_t *const     518                                 uint_t *const recvalp,
519                                 unsigned int *    519                                 unsigned int *sym_index,
520                                 Elf_Shdr const    520                                 Elf_Shdr const *const symhdr,
521                                 Elf32_Word con    521                                 Elf32_Word const *symtab,
522                                 Elf32_Word con    522                                 Elf32_Word const *symtab_shndx,
523                                 Elf_Ehdr const    523                                 Elf_Ehdr const *const ehdr)
524 {                                                 524 {
525         Elf_Sym const *const sym0 = (Elf_Sym c    525         Elf_Sym const *const sym0 = (Elf_Sym const *)(_w(symhdr->sh_offset)
526                 + (void *)ehdr);                  526                 + (void *)ehdr);
527         unsigned const nsym = _w(symhdr->sh_si    527         unsigned const nsym = _w(symhdr->sh_size) / _w(symhdr->sh_entsize);
528         Elf_Sym const *symp;                      528         Elf_Sym const *symp;
529         unsigned t;                               529         unsigned t;
530                                                   530 
531         for (symp = sym0, t = nsym; t; --t, ++    531         for (symp = sym0, t = nsym; t; --t, ++symp) {
532                 unsigned int const st_bind = E    532                 unsigned int const st_bind = ELF_ST_BIND(symp->st_info);
533                                                   533 
534                 if (txtndx == get_symindex(sym    534                 if (txtndx == get_symindex(symp, symtab, symtab_shndx)
535                         /* avoid STB_WEAK */      535                         /* avoid STB_WEAK */
536                     && (STB_LOCAL == st_bind |    536                     && (STB_LOCAL == st_bind || STB_GLOBAL == st_bind)) {
537                         /* function symbols on    537                         /* function symbols on ARM have quirks, avoid them */
538                         if (w2(ehdr->e_machine    538                         if (w2(ehdr->e_machine) == EM_ARM
539                             && ELF_ST_TYPE(sym    539                             && ELF_ST_TYPE(symp->st_info) == STT_FUNC)
540                                 continue;         540                                 continue;
541                                                   541 
542                         *recvalp = _w(symp->st    542                         *recvalp = _w(symp->st_value);
543                         *sym_index = symp - sy    543                         *sym_index = symp - sym0;
544                         return 0;                 544                         return 0;
545                 }                                 545                 }
546         }                                         546         }
547         fprintf(stderr, "Cannot find symbol fo    547         fprintf(stderr, "Cannot find symbol for section %u: %s.\n",
548                 txtndx, txtname);                 548                 txtndx, txtname);
549         return -1;                                549         return -1;
550 }                                                 550 }
551                                                   551 
552 /* Evade ISO C restriction: no declaration aft    552 /* Evade ISO C restriction: no declaration after statement in has_rel_mcount. */
553 static char const * __has_rel_mcount(Elf_Shdr     553 static char const * __has_rel_mcount(Elf_Shdr const *const relhdr, /* reltype */
554                                      Elf_Shdr     554                                      Elf_Shdr const *const shdr0,
555                                      char cons    555                                      char const *const shstrtab,
556                                      char cons    556                                      char const *const fname)
557 {                                                 557 {
558         /* .sh_info depends on .sh_type == SHT    558         /* .sh_info depends on .sh_type == SHT_REL[,A] */
559         Elf_Shdr const *const txthdr = &shdr0[    559         Elf_Shdr const *const txthdr = &shdr0[w(relhdr->sh_info)];
560         char const *const txtname = &shstrtab[    560         char const *const txtname = &shstrtab[w(txthdr->sh_name)];
561                                                   561 
562         if (strcmp("__mcount_loc", txtname) ==    562         if (strcmp("__mcount_loc", txtname) == 0) {
563                 fprintf(stderr, "warning: __mc    563                 fprintf(stderr, "warning: __mcount_loc already exists: %s\n",
564                         fname);                   564                         fname);
565                 return already_has_rel_mcount;    565                 return already_has_rel_mcount;
566         }                                         566         }
567         if (w(txthdr->sh_type) != SHT_PROGBITS    567         if (w(txthdr->sh_type) != SHT_PROGBITS ||
568             !(_w(txthdr->sh_flags) & SHF_EXECI    568             !(_w(txthdr->sh_flags) & SHF_EXECINSTR))
569                 return NULL;                      569                 return NULL;
570         return txtname;                           570         return txtname;
571 }                                                 571 }
572                                                   572 
573 static char const *has_rel_mcount(Elf_Shdr con    573 static char const *has_rel_mcount(Elf_Shdr const *const relhdr,
574                                   Elf_Shdr con    574                                   Elf_Shdr const *const shdr0,
575                                   char const *    575                                   char const *const shstrtab,
576                                   char const *    576                                   char const *const fname)
577 {                                                 577 {
578         if (w(relhdr->sh_type) != SHT_REL && w    578         if (w(relhdr->sh_type) != SHT_REL && w(relhdr->sh_type) != SHT_RELA)
579                 return NULL;                      579                 return NULL;
580         return __has_rel_mcount(relhdr, shdr0,    580         return __has_rel_mcount(relhdr, shdr0, shstrtab, fname);
581 }                                                 581 }
582                                                   582 
583                                                   583 
584 static unsigned tot_relsize(Elf_Shdr const *co    584 static unsigned tot_relsize(Elf_Shdr const *const shdr0,
585                             unsigned nhdr,        585                             unsigned nhdr,
586                             const char *const     586                             const char *const shstrtab,
587                             const char *const     587                             const char *const fname)
588 {                                                 588 {
589         unsigned totrelsz = 0;                    589         unsigned totrelsz = 0;
590         Elf_Shdr const *shdrp = shdr0;            590         Elf_Shdr const *shdrp = shdr0;
591         char const *txtname;                      591         char const *txtname;
592                                                   592 
593         for (; nhdr; --nhdr, ++shdrp) {           593         for (; nhdr; --nhdr, ++shdrp) {
594                 txtname = has_rel_mcount(shdrp    594                 txtname = has_rel_mcount(shdrp, shdr0, shstrtab, fname);
595                 if (txtname == already_has_rel    595                 if (txtname == already_has_rel_mcount) {
596                         totrelsz = 0;             596                         totrelsz = 0;
597                         break;                    597                         break;
598                 }                                 598                 }
599                 if (txtname && is_mcounted_sec    599                 if (txtname && is_mcounted_section_name(txtname))
600                         totrelsz += _w(shdrp->    600                         totrelsz += _w(shdrp->sh_size);
601         }                                         601         }
602         return totrelsz;                          602         return totrelsz;
603 }                                                 603 }
604                                                   604 
605 /* Overall supervision for Elf32 ET_REL file.     605 /* Overall supervision for Elf32 ET_REL file. */
606 static int do_func(Elf_Ehdr *const ehdr, char     606 static int do_func(Elf_Ehdr *const ehdr, char const *const fname,
607                    unsigned const reltype)        607                    unsigned const reltype)
608 {                                                 608 {
609         Elf_Shdr *const shdr0 = (Elf_Shdr *)(_    609         Elf_Shdr *const shdr0 = (Elf_Shdr *)(_w(ehdr->e_shoff)
610                 + (void *)ehdr);                  610                 + (void *)ehdr);
611         unsigned const nhdr = get_shnum(ehdr,     611         unsigned const nhdr = get_shnum(ehdr, shdr0);
612         Elf_Shdr *const shstr = &shdr0[get_shs    612         Elf_Shdr *const shstr = &shdr0[get_shstrndx(ehdr, shdr0)];
613         char const *const shstrtab = (char con    613         char const *const shstrtab = (char const *)(_w(shstr->sh_offset)
614                 + (void *)ehdr);                  614                 + (void *)ehdr);
615                                                   615 
616         Elf_Shdr const *relhdr;                   616         Elf_Shdr const *relhdr;
617         unsigned k;                               617         unsigned k;
618                                                   618 
619         Elf32_Word *symtab;                       619         Elf32_Word *symtab;
620         Elf32_Word *symtab_shndx;                 620         Elf32_Word *symtab_shndx;
621                                                   621 
622         /* Upper bound on space: assume all re    622         /* Upper bound on space: assume all relevant relocs are for mcount. */
623         unsigned       totrelsz;                  623         unsigned       totrelsz;
624                                                   624 
625         Elf_Rel *      mrel0;                     625         Elf_Rel *      mrel0;
626         Elf_Rel *      mrelp;                     626         Elf_Rel *      mrelp;
627                                                   627 
628         uint_t *      mloc0;                      628         uint_t *      mloc0;
629         uint_t *      mlocp;                      629         uint_t *      mlocp;
630                                                   630 
631         unsigned rel_entsize = 0;                 631         unsigned rel_entsize = 0;
632         unsigned symsec_sh_link = 0;              632         unsigned symsec_sh_link = 0;
633                                                   633 
634         int result = 0;                           634         int result = 0;
635                                                   635 
636         totrelsz = tot_relsize(shdr0, nhdr, sh    636         totrelsz = tot_relsize(shdr0, nhdr, shstrtab, fname);
637         if (totrelsz == 0)                        637         if (totrelsz == 0)
638                 return 0;                         638                 return 0;
639         mrel0 = umalloc(totrelsz);                639         mrel0 = umalloc(totrelsz);
640         mrelp = mrel0;                            640         mrelp = mrel0;
641         if (!mrel0)                               641         if (!mrel0)
642                 return -1;                        642                 return -1;
643                                                   643 
644         /* 2*sizeof(address) <= sizeof(Elf_Rel    644         /* 2*sizeof(address) <= sizeof(Elf_Rel) */
645         mloc0 = umalloc(totrelsz>>1);             645         mloc0 = umalloc(totrelsz>>1);
646         mlocp = mloc0;                            646         mlocp = mloc0;
647         if (!mloc0) {                             647         if (!mloc0) {
648                 free(mrel0);                      648                 free(mrel0);
649                 return -1;                        649                 return -1;
650         }                                         650         }
651                                                   651 
652         find_symtab(ehdr, shdr0, nhdr, &symtab    652         find_symtab(ehdr, shdr0, nhdr, &symtab, &symtab_shndx);
653                                                   653 
654         for (relhdr = shdr0, k = nhdr; k; --k,    654         for (relhdr = shdr0, k = nhdr; k; --k, ++relhdr) {
655                 char const *const txtname = ha    655                 char const *const txtname = has_rel_mcount(relhdr, shdr0,
656                         shstrtab, fname);         656                         shstrtab, fname);
657                 if (txtname == already_has_rel    657                 if (txtname == already_has_rel_mcount) {
658                         result = 0;               658                         result = 0;
659                         file_updated = 0;         659                         file_updated = 0;
660                         goto out; /* Nothing t    660                         goto out; /* Nothing to be done; don't append! */
661                 }                                 661                 }
662                 if (txtname && is_mcounted_sec    662                 if (txtname && is_mcounted_section_name(txtname)) {
663                         unsigned int recsym;      663                         unsigned int recsym;
664                         uint_t recval = 0;        664                         uint_t recval = 0;
665                                                   665 
666                         symsec_sh_link = w(rel    666                         symsec_sh_link = w(relhdr->sh_link);
667                         result = find_secsym_n    667                         result = find_secsym_ndx(w(relhdr->sh_info), txtname,
668                                                   668                                                 &recval, &recsym,
669                                                   669                                                 &shdr0[symsec_sh_link],
670                                                   670                                                 symtab, symtab_shndx,
671                                                   671                                                 ehdr);
672                         if (result)               672                         if (result)
673                                 goto out;         673                                 goto out;
674                                                   674 
675                         rel_entsize = _w(relhd    675                         rel_entsize = _w(relhdr->sh_entsize);
676                         mlocp = sift_rel_mcoun    676                         mlocp = sift_rel_mcount(mlocp,
677                                 (void *)mlocp     677                                 (void *)mlocp - (void *)mloc0, &mrelp,
678                                 relhdr, ehdr,     678                                 relhdr, ehdr, recsym, recval, reltype);
679                 } else if (txtname && (warn_on    679                 } else if (txtname && (warn_on_notrace_sect || make_nop)) {
680                         /*                        680                         /*
681                          * This section is ign    681                          * This section is ignored by ftrace, but still
682                          * has mcount calls. C    682                          * has mcount calls. Convert them to nops now.
683                          */                       683                          */
684                         if (nop_mcount(relhdr,    684                         if (nop_mcount(relhdr, ehdr, txtname) < 0) {
685                                 result = -1;      685                                 result = -1;
686                                 goto out;         686                                 goto out;
687                         }                         687                         }
688                 }                                 688                 }
689         }                                         689         }
690         if (!result && mloc0 != mlocp)            690         if (!result && mloc0 != mlocp)
691                 result = append_func(ehdr, shs    691                 result = append_func(ehdr, shstr, mloc0, mlocp, mrel0, mrelp,
692                                      rel_entsi    692                                      rel_entsize, symsec_sh_link);
693 out:                                              693 out:
694         free(mrel0);                              694         free(mrel0);
695         free(mloc0);                              695         free(mloc0);
696         return result;                            696         return result;
697 }                                                 697 }
698                                                   698 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php