4 * This code was taken out of recordmcount.c written by
5 * Copyright 2009 John F. Reiser <jreiser@BitWagon.com>. All rights reserved.
7 * The original code had the same algorithms for both 32bit
8 * and 64bit ELF files, but the code was duplicated to support
9 * the difference in structures that were used. This
10 * file creates a macro of everything that is different between
11 * the 64 and 32 bit code, such that by including this header
12 * twice we can create both sets of functions by including this
13 * header once with RECORD_MCOUNT_64 undefined, and again with
16 * This conversion to macros was done by:
17 * Copyright 2010 Steven Rostedt <srostedt@redhat.com>, Red Hat Inc.
19 * Licensed under the GNU General Public License, version 2 (GPLv2).
22 #undef sift_rel_mcount
23 #undef find_secsym_ndx
24 #undef __has_rel_mcount
45 #ifdef RECORD_MCOUNT_64
46 # define append_func append64
47 # define sift_rel_mcount sift64_rel_mcount
48 # define find_secsym_ndx find64_secsym_ndx
49 # define __has_rel_mcount __has64_rel_mcount
50 # define has_rel_mcount has64_rel_mcount
51 # define tot_relsize tot64_relsize
53 # define Elf_Ehdr Elf64_Ehdr
54 # define Elf_Shdr Elf64_Shdr
55 # define Elf_Rel Elf64_Rel
56 # define Elf_Rela Elf64_Rela
57 # define Elf_Sym Elf64_Sym
58 # define ELF_R_SYM ELF64_R_SYM
59 # define Elf_r_sym Elf64_r_sym
60 # define ELF_R_INFO ELF64_R_INFO
61 # define Elf_r_info Elf64_r_info
62 # define ELF_ST_BIND ELF64_ST_BIND
63 # define fn_ELF_R_SYM fn_ELF64_R_SYM
64 # define fn_ELF_R_INFO fn_ELF64_R_INFO
65 # define uint_t uint64_t
70 # define append_func append32
71 # define sift_rel_mcount sift32_rel_mcount
72 # define find_secsym_ndx find32_secsym_ndx
73 # define __has_rel_mcount __has32_rel_mcount
74 # define has_rel_mcount has32_rel_mcount
75 # define tot_relsize tot32_relsize
77 # define Elf_Ehdr Elf32_Ehdr
78 # define Elf_Shdr Elf32_Shdr
79 # define Elf_Rel Elf32_Rel
80 # define Elf_Rela Elf32_Rela
81 # define Elf_Sym Elf32_Sym
82 # define ELF_R_SYM ELF32_R_SYM
83 # define Elf_r_sym Elf32_r_sym
84 # define ELF_R_INFO ELF32_R_INFO
85 # define Elf_r_info Elf32_r_info
86 # define ELF_ST_BIND ELF32_ST_BIND
87 # define fn_ELF_R_SYM fn_ELF32_R_SYM
88 # define fn_ELF_R_INFO fn_ELF32_R_INFO
89 # define uint_t uint32_t
95 /* Functions and pointers that 64-bit EM_MIPS can override. */
96 static uint_t fn_ELF_R_SYM(Elf_Rel const *rp)
98 return ELF_R_SYM(_w(rp->r_info));
100 static uint_t (*Elf_r_sym)(Elf_Rel const *rp) = fn_ELF_R_SYM;
102 static void fn_ELF_R_INFO(Elf_Rel *const rp, unsigned sym, unsigned type)
104 rp->r_info = ELF_R_INFO(sym, type);
106 static void (*Elf_r_info)(Elf_Rel *const rp, unsigned sym, unsigned type) = fn_ELF_R_INFO;
109 /* Append the new shstrtab, Elf_Shdr[], __mcount_loc and its relocations. */
110 static void append_func(Elf_Ehdr *const ehdr,
111 Elf_Shdr *const shstr,
112 uint_t const *const mloc0,
113 uint_t const *const mlocp,
114 Elf_Rel const *const mrel0,
115 Elf_Rel const *const mrelp,
116 unsigned int const rel_entsize,
117 unsigned int const symsec_sh_link)
119 /* Begin constructing output file */
121 char const *mc_name = (sizeof(Elf_Rela) == rel_entsize)
122 ? ".rela__mcount_loc"
123 : ".rel__mcount_loc";
124 unsigned const old_shnum = w2(ehdr->e_shnum);
125 uint_t const old_shoff = _w(ehdr->e_shoff);
126 uint_t const old_shstr_sh_size = _w(shstr->sh_size);
127 uint_t const old_shstr_sh_offset = _w(shstr->sh_offset);
128 uint_t t = 1 + strlen(mc_name) + _w(shstr->sh_size);
131 shstr->sh_size = _w(t);
132 shstr->sh_offset = _w(sb.st_size);
134 t += (_align & -t); /* word-byte align */
137 /* body for new shstrtab */
138 ulseek(fd_map, sb.st_size, SEEK_SET);
139 uwrite(fd_map, old_shstr_sh_offset + (void *)ehdr, old_shstr_sh_size);
140 uwrite(fd_map, mc_name, 1 + strlen(mc_name));
142 /* old(modified) Elf_Shdr table, word-byte aligned */
143 ulseek(fd_map, t, SEEK_SET);
144 t += sizeof(Elf_Shdr) * old_shnum;
145 uwrite(fd_map, old_shoff + (void *)ehdr,
146 sizeof(Elf_Shdr) * old_shnum);
148 /* new sections __mcount_loc and .rel__mcount_loc */
149 t += 2*sizeof(mcsec);
150 mcsec.sh_name = w((sizeof(Elf_Rela) == rel_entsize) + strlen(".rel")
151 + old_shstr_sh_size);
152 mcsec.sh_type = w(SHT_PROGBITS);
153 mcsec.sh_flags = _w(SHF_ALLOC);
155 mcsec.sh_offset = _w(t);
156 mcsec.sh_size = _w((void *)mlocp - (void *)mloc0);
159 mcsec.sh_addralign = _w(_size);
160 mcsec.sh_entsize = _w(_size);
161 uwrite(fd_map, &mcsec, sizeof(mcsec));
163 mcsec.sh_name = w(old_shstr_sh_size);
164 mcsec.sh_type = (sizeof(Elf_Rela) == rel_entsize)
169 mcsec.sh_offset = _w((void *)mlocp - (void *)mloc0 + t);
170 mcsec.sh_size = _w((void *)mrelp - (void *)mrel0);
171 mcsec.sh_link = w(symsec_sh_link);
172 mcsec.sh_info = w(old_shnum);
173 mcsec.sh_addralign = _w(_size);
174 mcsec.sh_entsize = _w(rel_entsize);
175 uwrite(fd_map, &mcsec, sizeof(mcsec));
177 uwrite(fd_map, mloc0, (void *)mlocp - (void *)mloc0);
178 uwrite(fd_map, mrel0, (void *)mrelp - (void *)mrel0);
180 ehdr->e_shoff = _w(new_e_shoff);
181 ehdr->e_shnum = w2(2 + w2(ehdr->e_shnum)); /* {.rel,}__mcount_loc */
182 ulseek(fd_map, 0, SEEK_SET);
183 uwrite(fd_map, ehdr, sizeof(*ehdr));
188 * Look at the relocations in order to find the calls to mcount.
189 * Accumulate the section offsets that are found, and their relocation info,
190 * onto the end of the existing arrays.
192 static uint_t *sift_rel_mcount(uint_t *mlocp,
193 unsigned const offbase,
194 Elf_Rel **const mrelpp,
195 Elf_Shdr const *const relhdr,
196 Elf_Ehdr const *const ehdr,
197 unsigned const recsym,
199 unsigned const reltype)
201 uint_t *const mloc0 = mlocp;
202 Elf_Rel *mrelp = *mrelpp;
203 Elf_Shdr *const shdr0 = (Elf_Shdr *)(_w(ehdr->e_shoff)
205 unsigned const symsec_sh_link = w(relhdr->sh_link);
206 Elf_Shdr const *const symsec = &shdr0[symsec_sh_link];
207 Elf_Sym const *const sym0 = (Elf_Sym const *)(_w(symsec->sh_offset)
210 Elf_Shdr const *const strsec = &shdr0[w(symsec->sh_link)];
211 char const *const str0 = (char const *)(_w(strsec->sh_offset)
214 Elf_Rel const *const rel0 = (Elf_Rel const *)(_w(relhdr->sh_offset)
216 unsigned rel_entsize = _w(relhdr->sh_entsize);
217 unsigned const nrel = _w(relhdr->sh_size) / rel_entsize;
218 Elf_Rel const *relp = rel0;
220 unsigned mcountsym = 0;
223 for (t = nrel; t; --t) {
225 Elf_Sym const *const symp =
226 &sym0[Elf_r_sym(relp)];
227 char const *symname = &str0[w(symp->st_name)];
229 if ('.' == symname[0])
230 ++symname; /* ppc64 hack */
231 if (0 == strcmp((('_' == gpfx) ? "_mcount" : "mcount"),
233 mcountsym = Elf_r_sym(relp);
236 if (mcountsym == Elf_r_sym(relp)) {
237 uint_t const addend = _w(_w(relp->r_offset) - recval);
239 mrelp->r_offset = _w(offbase
240 + ((void *)mlocp - (void *)mloc0));
241 Elf_r_info(mrelp, recsym, reltype);
242 if (sizeof(Elf_Rela) == rel_entsize) {
243 ((Elf_Rela *)mrelp)->r_addend = addend;
248 mrelp = (Elf_Rel *)(rel_entsize + (void *)mrelp);
250 relp = (Elf_Rel const *)(rel_entsize + (void *)relp);
258 * Find a symbol in the given section, to be used as the base for relocating
259 * the table of offsets of calls to mcount. A local or global symbol suffices,
260 * but avoid a Weak symbol because it may be overridden; the change in value
261 * would invalidate the relocations of the offsets of the calls to mcount.
262 * Often the found symbol will be the unnamed local symbol generated by
263 * GNU 'as' for the start of each section. For example:
264 * Num: Value Size Type Bind Vis Ndx Name
265 * 2: 00000000 0 SECTION LOCAL DEFAULT 1
267 static unsigned find_secsym_ndx(unsigned const txtndx,
268 char const *const txtname,
269 uint_t *const recvalp,
270 Elf_Shdr const *const symhdr,
271 Elf_Ehdr const *const ehdr)
273 Elf_Sym const *const sym0 = (Elf_Sym const *)(_w(symhdr->sh_offset)
275 unsigned const nsym = _w(symhdr->sh_size) / _w(symhdr->sh_entsize);
279 for (symp = sym0, t = nsym; t; --t, ++symp) {
280 unsigned int const st_bind = ELF_ST_BIND(symp->st_info);
282 if (txtndx == w2(symp->st_shndx)
284 && (STB_LOCAL == st_bind || STB_GLOBAL == st_bind)) {
285 *recvalp = _w(symp->st_value);
289 fprintf(stderr, "Cannot find symbol for section %d: %s.\n",
295 /* Evade ISO C restriction: no declaration after statement in has_rel_mcount. */
297 __has_rel_mcount(Elf_Shdr const *const relhdr, /* is SHT_REL or SHT_RELA */
298 Elf_Shdr const *const shdr0,
299 char const *const shstrtab,
300 char const *const fname)
302 /* .sh_info depends on .sh_type == SHT_REL[,A] */
303 Elf_Shdr const *const txthdr = &shdr0[w(relhdr->sh_info)];
304 char const *const txtname = &shstrtab[w(txthdr->sh_name)];
306 if (0 == strcmp("__mcount_loc", txtname)) {
307 fprintf(stderr, "warning: __mcount_loc already exists: %s\n",
311 if (SHT_PROGBITS != w(txthdr->sh_type) ||
312 !is_mcounted_section_name(txtname))
317 static char const *has_rel_mcount(Elf_Shdr const *const relhdr,
318 Elf_Shdr const *const shdr0,
319 char const *const shstrtab,
320 char const *const fname)
322 if (SHT_REL != w(relhdr->sh_type) && SHT_RELA != w(relhdr->sh_type))
324 return __has_rel_mcount(relhdr, shdr0, shstrtab, fname);
328 static unsigned tot_relsize(Elf_Shdr const *const shdr0,
330 const char *const shstrtab,
331 const char *const fname)
333 unsigned totrelsz = 0;
334 Elf_Shdr const *shdrp = shdr0;
336 for (; nhdr; --nhdr, ++shdrp) {
337 if (has_rel_mcount(shdrp, shdr0, shstrtab, fname))
338 totrelsz += _w(shdrp->sh_size);
344 /* Overall supervision for Elf32 ET_REL file. */
346 do_func(Elf_Ehdr *const ehdr, char const *const fname, unsigned const reltype)
348 Elf_Shdr *const shdr0 = (Elf_Shdr *)(_w(ehdr->e_shoff)
350 unsigned const nhdr = w2(ehdr->e_shnum);
351 Elf_Shdr *const shstr = &shdr0[w2(ehdr->e_shstrndx)];
352 char const *const shstrtab = (char const *)(_w(shstr->sh_offset)
355 Elf_Shdr const *relhdr;
358 /* Upper bound on space: assume all relevant relocs are for mcount. */
359 unsigned const totrelsz = tot_relsize(shdr0, nhdr, shstrtab, fname);
360 Elf_Rel *const mrel0 = umalloc(totrelsz);
361 Elf_Rel * mrelp = mrel0;
363 /* 2*sizeof(address) <= sizeof(Elf_Rel) */
364 uint_t *const mloc0 = umalloc(totrelsz>>1);
365 uint_t * mlocp = mloc0;
367 unsigned rel_entsize = 0;
368 unsigned symsec_sh_link = 0;
370 for (relhdr = shdr0, k = nhdr; k; --k, ++relhdr) {
371 char const *const txtname = has_rel_mcount(relhdr, shdr0,
375 unsigned const recsym = find_secsym_ndx(
376 w(relhdr->sh_info), txtname, &recval,
377 &shdr0[symsec_sh_link = w(relhdr->sh_link)],
380 rel_entsize = _w(relhdr->sh_entsize);
381 mlocp = sift_rel_mcount(mlocp,
382 (void *)mlocp - (void *)mloc0, &mrelp,
383 relhdr, ehdr, recsym, recval, reltype);
386 if (mloc0 != mlocp) {
387 append_func(ehdr, shstr, mloc0, mlocp, mrel0, mrelp,
388 rel_entsize, symsec_sh_link);