blob: 0d5ae4a0f20c24884f3f3ee2464105f7a4786ec3 [file] [log] [blame]
John Reiser81d38582010-10-13 15:12:54 -04001/*
2 * recordmcount.c: construct a table of the locations of calls to 'mcount'
3 * so that ftrace can find them quickly.
4 * Copyright 2009 John F. Reiser <jreiser@BitWagon.com>. All rights reserved.
5 * Licensed under the GNU General Public License, version 2 (GPLv2).
6 *
7 * Restructured to fit Linux format, as well as other updates:
8 * Copyright 2010 Steven Rostedt <srostedt@redhat.com>, Red Hat Inc.
9 */
10
11/*
12 * Strategy: alter the .o file in-place.
13 *
14 * Append a new STRTAB that has the new section names, followed by a new array
15 * ElfXX_Shdr[] that has the new section headers, followed by the section
16 * contents for __mcount_loc and its relocations. The old shstrtab strings,
17 * and the old ElfXX_Shdr[] array, remain as "garbage" (commonly, a couple
18 * kilobytes.) Subsequent processing by /bin/ld (or the kernel module loader)
19 * will ignore the garbage regions, because they are not designated by the
20 * new .e_shoff nor the new ElfXX_Shdr[]. [In order to remove the garbage,
21 * then use "ld -r" to create a new file that omits the garbage.]
22 */
23
24#include <sys/types.h>
25#include <sys/mman.h>
26#include <sys/stat.h>
Steven Rostedtdfad3d52011-04-12 18:53:25 -040027#include <getopt.h>
John Reiser81d38582010-10-13 15:12:54 -040028#include <elf.h>
29#include <fcntl.h>
30#include <setjmp.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34#include <unistd.h>
35
36static int fd_map; /* File descriptor for file being modified. */
37static int mmap_failed; /* Boolean flag. */
John Reiser81d38582010-10-13 15:12:54 -040038static char gpfx; /* prefix for global symbol name (sometimes '_') */
39static struct stat sb; /* Remember .st_size, etc. */
40static jmp_buf jmpenv; /* setjmp/longjmp per-file error escape */
Rabin Vincented604532010-11-30 17:36:48 +010041static const char *altmcount; /* alternate mcount symbol name */
Steven Rostedtdfad3d52011-04-12 18:53:25 -040042static int warn_on_notrace_sect; /* warn when section has mcount not being recorded */
Steven Rostedt (Red Hat)7e7340a2015-12-15 16:06:10 -050043static void *file_map; /* pointer of the mapped file */
44static void *file_end; /* pointer to the end of the mapped file */
45static int file_updated; /* flag to state file was changed */
46static void *file_ptr; /* current file pointer location */
47static void *file_append; /* added to the end of the file */
48static size_t file_append_size; /* how much is added to end of file */
John Reiser81d38582010-10-13 15:12:54 -040049
50/* setjmp() return values */
51enum {
52 SJ_SETJMP = 0, /* hardwired first return */
53 SJ_FAIL,
54 SJ_SUCCEED
55};
56
57/* Per-file resource cleanup when multiple files. */
58static void
59cleanup(void)
60{
61 if (!mmap_failed)
Steven Rostedt (Red Hat)7e7340a2015-12-15 16:06:10 -050062 munmap(file_map, sb.st_size);
John Reiser81d38582010-10-13 15:12:54 -040063 else
Steven Rostedt (Red Hat)7e7340a2015-12-15 16:06:10 -050064 free(file_map);
65 file_map = NULL;
66 free(file_append);
67 file_append = NULL;
68 file_append_size = 0;
69 file_updated = 0;
John Reiser81d38582010-10-13 15:12:54 -040070}
71
72static void __attribute__((noreturn))
73fail_file(void)
74{
75 cleanup();
76 longjmp(jmpenv, SJ_FAIL);
77}
78
79static void __attribute__((noreturn))
80succeed_file(void)
81{
82 cleanup();
83 longjmp(jmpenv, SJ_SUCCEED);
84}
85
86/* ulseek, uread, ...: Check return value for errors. */
87
88static off_t
89ulseek(int const fd, off_t const offset, int const whence)
90{
Steven Rostedt (Red Hat)7e7340a2015-12-15 16:06:10 -050091 switch (whence) {
92 case SEEK_SET:
93 file_ptr = file_map + offset;
94 break;
95 case SEEK_CUR:
96 file_ptr += offset;
97 break;
98 case SEEK_END:
99 file_ptr = file_map + (sb.st_size - offset);
100 break;
101 }
102 if (file_ptr < file_map) {
103 fprintf(stderr, "lseek: seek before file\n");
John Reiser81d38582010-10-13 15:12:54 -0400104 fail_file();
105 }
Steven Rostedt (Red Hat)7e7340a2015-12-15 16:06:10 -0500106 return file_ptr - file_map;
John Reiser81d38582010-10-13 15:12:54 -0400107}
108
109static size_t
110uread(int const fd, void *const buf, size_t const count)
111{
112 size_t const n = read(fd, buf, count);
113 if (n != count) {
114 perror("read");
115 fail_file();
116 }
117 return n;
118}
119
120static size_t
121uwrite(int const fd, void const *const buf, size_t const count)
122{
Steven Rostedt (Red Hat)7e7340a2015-12-15 16:06:10 -0500123 size_t cnt = count;
124 off_t idx = 0;
125
126 file_updated = 1;
127
128 if (file_ptr + count >= file_end) {
129 off_t aoffset = (file_ptr + count) - file_end;
130
131 if (aoffset > file_append_size) {
132 file_append = realloc(file_append, aoffset);
133 file_append_size = aoffset;
134 }
135 if (!file_append) {
136 perror("write");
137 fail_file();
138 }
139 if (file_ptr < file_end) {
140 cnt = file_end - file_ptr;
141 } else {
142 cnt = 0;
143 idx = aoffset - count;
144 }
John Reiser81d38582010-10-13 15:12:54 -0400145 }
Steven Rostedt (Red Hat)7e7340a2015-12-15 16:06:10 -0500146
147 if (cnt)
148 memcpy(file_ptr, buf, cnt);
149
150 if (cnt < count)
151 memcpy(file_append + idx, buf + cnt, count - cnt);
152
153 file_ptr += count;
154 return count;
John Reiser81d38582010-10-13 15:12:54 -0400155}
156
157static void *
158umalloc(size_t size)
159{
160 void *const addr = malloc(size);
Steven Rostedtdd5477f2011-04-06 13:21:17 -0400161 if (addr == 0) {
John Reiser81d38582010-10-13 15:12:54 -0400162 fprintf(stderr, "malloc failed: %zu bytes\n", size);
163 fail_file();
164 }
165 return addr;
166}
167
Steven Rostedtffd618f2011-04-08 03:58:48 -0400168static unsigned char ideal_nop5_x86_64[5] = { 0x0f, 0x1f, 0x44, 0x00, 0x00 };
169static unsigned char ideal_nop5_x86_32[5] = { 0x3e, 0x8d, 0x74, 0x26, 0x00 };
170static unsigned char *ideal_nop;
171
172static char rel_type_nop;
173
174static int (*make_nop)(void *map, size_t const offset);
175
176static int make_nop_x86(void *map, size_t const offset)
177{
178 uint32_t *ptr;
179 unsigned char *op;
180
181 /* Confirm we have 0xe8 0x0 0x0 0x0 0x0 */
182 ptr = map + offset;
183 if (*ptr != 0)
184 return -1;
185
186 op = map + offset - 1;
187 if (*op != 0xe8)
188 return -1;
189
190 /* convert to nop */
191 ulseek(fd_map, offset - 1, SEEK_SET);
192 uwrite(fd_map, ideal_nop, 5);
193 return 0;
194}
195
John Reiser81d38582010-10-13 15:12:54 -0400196/*
197 * Get the whole file as a programming convenience in order to avoid
198 * malloc+lseek+read+free of many pieces. If successful, then mmap
199 * avoids copying unused pieces; else just read the whole file.
200 * Open for both read and write; new info will be appended to the file.
201 * Use MAP_PRIVATE so that a few changes to the in-memory ElfXX_Ehdr
202 * do not propagate to the file until an explicit overwrite at the last.
203 * This preserves most aspects of consistency (all except .st_size)
204 * for simultaneous readers of the file while we are appending to it.
205 * However, multiple writers still are bad. We choose not to use
206 * locking because it is expensive and the use case of kernel build
207 * makes multiple writers unlikely.
208 */
209static void *mmap_file(char const *fname)
210{
Steven Rostedt (Red Hat)7e7340a2015-12-15 16:06:10 -0500211 fd_map = open(fname, O_RDONLY);
Steven Rostedtdd5477f2011-04-06 13:21:17 -0400212 if (fd_map < 0 || fstat(fd_map, &sb) < 0) {
John Reiser81d38582010-10-13 15:12:54 -0400213 perror(fname);
214 fail_file();
215 }
216 if (!S_ISREG(sb.st_mode)) {
217 fprintf(stderr, "not a regular file: %s\n", fname);
218 fail_file();
219 }
Steven Rostedt (Red Hat)7e7340a2015-12-15 16:06:10 -0500220 file_map = mmap(0, sb.st_size, PROT_READ|PROT_WRITE, MAP_PRIVATE,
221 fd_map, 0);
John Reiser81d38582010-10-13 15:12:54 -0400222 mmap_failed = 0;
Steven Rostedt (Red Hat)7e7340a2015-12-15 16:06:10 -0500223 if (file_map == MAP_FAILED) {
John Reiser81d38582010-10-13 15:12:54 -0400224 mmap_failed = 1;
Steven Rostedt (Red Hat)7e7340a2015-12-15 16:06:10 -0500225 file_map = umalloc(sb.st_size);
226 uread(fd_map, file_map, sb.st_size);
John Reiser81d38582010-10-13 15:12:54 -0400227 }
Steven Rostedt (Red Hat)7e7340a2015-12-15 16:06:10 -0500228 close(fd_map);
229
230 file_end = file_map + sb.st_size;
231
232 return file_map;
233}
234
235static void write_file(const char *fname)
236{
237 char tmp_file[strlen(fname) + 4];
238 size_t n;
239
240 if (!file_updated)
241 return;
242
243 sprintf(tmp_file, "%s.rc", fname);
244
245 /*
246 * After reading the entire file into memory, delete it
247 * and write it back, to prevent weird side effects of modifying
248 * an object file in place.
249 */
250 fd_map = open(tmp_file, O_WRONLY | O_TRUNC | O_CREAT, sb.st_mode);
251 if (fd_map < 0) {
252 perror(fname);
253 fail_file();
254 }
255 n = write(fd_map, file_map, sb.st_size);
256 if (n != sb.st_size) {
257 perror("write");
258 fail_file();
259 }
260 if (file_append_size) {
261 n = write(fd_map, file_append, file_append_size);
262 if (n != file_append_size) {
263 perror("write");
Russell King91626782015-12-11 12:09:03 +0000264 fail_file();
265 }
Russell King91626782015-12-11 12:09:03 +0000266 }
Steven Rostedt (Red Hat)7e7340a2015-12-15 16:06:10 -0500267 close(fd_map);
268 if (rename(tmp_file, fname) < 0) {
269 perror(fname);
270 fail_file();
271 }
John Reiser81d38582010-10-13 15:12:54 -0400272}
273
274/* w8rev, w8nat, ...: Handle endianness. */
275
276static uint64_t w8rev(uint64_t const x)
277{
278 return ((0xff & (x >> (0 * 8))) << (7 * 8))
279 | ((0xff & (x >> (1 * 8))) << (6 * 8))
280 | ((0xff & (x >> (2 * 8))) << (5 * 8))
281 | ((0xff & (x >> (3 * 8))) << (4 * 8))
282 | ((0xff & (x >> (4 * 8))) << (3 * 8))
283 | ((0xff & (x >> (5 * 8))) << (2 * 8))
284 | ((0xff & (x >> (6 * 8))) << (1 * 8))
285 | ((0xff & (x >> (7 * 8))) << (0 * 8));
286}
287
288static uint32_t w4rev(uint32_t const x)
289{
290 return ((0xff & (x >> (0 * 8))) << (3 * 8))
291 | ((0xff & (x >> (1 * 8))) << (2 * 8))
292 | ((0xff & (x >> (2 * 8))) << (1 * 8))
293 | ((0xff & (x >> (3 * 8))) << (0 * 8));
294}
295
296static uint32_t w2rev(uint16_t const x)
297{
298 return ((0xff & (x >> (0 * 8))) << (1 * 8))
299 | ((0xff & (x >> (1 * 8))) << (0 * 8));
300}
301
302static uint64_t w8nat(uint64_t const x)
303{
304 return x;
305}
306
307static uint32_t w4nat(uint32_t const x)
308{
309 return x;
310}
311
312static uint32_t w2nat(uint16_t const x)
313{
314 return x;
315}
316
317static uint64_t (*w8)(uint64_t);
318static uint32_t (*w)(uint32_t);
319static uint32_t (*w2)(uint16_t);
320
321/* Names of the sections that could contain calls to mcount. */
322static int
323is_mcounted_section_name(char const *const txtname)
324{
Steven Rostedtdd5477f2011-04-06 13:21:17 -0400325 return strcmp(".text", txtname) == 0 ||
326 strcmp(".ref.text", txtname) == 0 ||
327 strcmp(".sched.text", txtname) == 0 ||
328 strcmp(".spinlock.text", txtname) == 0 ||
329 strcmp(".irqentry.text", txtname) == 0 ||
Steven Rostedt9f087e72011-04-06 14:10:22 -0400330 strcmp(".kprobes.text", txtname) == 0 ||
Steven Rostedtdd5477f2011-04-06 13:21:17 -0400331 strcmp(".text.unlikely", txtname) == 0;
John Reiser81d38582010-10-13 15:12:54 -0400332}
333
Steven Rostedtc28d5072010-10-13 19:06:14 -0400334/* 32 bit and 64 bit are very similar */
335#include "recordmcount.h"
336#define RECORD_MCOUNT_64
337#include "recordmcount.h"
John Reiser81d38582010-10-13 15:12:54 -0400338
John Reisera2d49352010-10-27 18:59:07 +0800339/* 64-bit EM_MIPS has weird ELF64_Rela.r_info.
340 * http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf
341 * We interpret Table 29 Relocation Operation (Elf64_Rel, Elf64_Rela) [p.40]
342 * to imply the order of the members; the spec does not say so.
343 * typedef unsigned char Elf64_Byte;
344 * fails on MIPS64 because their <elf.h> already has it!
345 */
346
347typedef uint8_t myElf64_Byte; /* Type for a 8-bit quantity. */
348
349union mips_r_info {
350 Elf64_Xword r_info;
351 struct {
352 Elf64_Word r_sym; /* Symbol index. */
353 myElf64_Byte r_ssym; /* Special symbol. */
354 myElf64_Byte r_type3; /* Third relocation. */
355 myElf64_Byte r_type2; /* Second relocation. */
356 myElf64_Byte r_type; /* First relocation. */
357 } r_mips;
358};
359
360static uint64_t MIPS64_r_sym(Elf64_Rel const *rp)
361{
362 return w(((union mips_r_info){ .r_info = rp->r_info }).r_mips.r_sym);
363}
364
365static void MIPS64_r_info(Elf64_Rel *const rp, unsigned sym, unsigned type)
366{
367 rp->r_info = ((union mips_r_info){
368 .r_mips = { .r_sym = w(sym), .r_type = type }
369 }).r_info;
370}
371
John Reiser81d38582010-10-13 15:12:54 -0400372static void
373do_file(char const *const fname)
374{
375 Elf32_Ehdr *const ehdr = mmap_file(fname);
376 unsigned int reltype = 0;
377
John Reiser81d38582010-10-13 15:12:54 -0400378 w = w4nat;
379 w2 = w2nat;
380 w8 = w8nat;
381 switch (ehdr->e_ident[EI_DATA]) {
382 static unsigned int const endian = 1;
Steven Rostedte90b0c82011-04-06 13:32:24 -0400383 default:
John Reiser81d38582010-10-13 15:12:54 -0400384 fprintf(stderr, "unrecognized ELF data encoding %d: %s\n",
385 ehdr->e_ident[EI_DATA], fname);
386 fail_file();
Steven Rostedte90b0c82011-04-06 13:32:24 -0400387 break;
388 case ELFDATA2LSB:
Steven Rostedtdd5477f2011-04-06 13:21:17 -0400389 if (*(unsigned char const *)&endian != 1) {
John Reiser81d38582010-10-13 15:12:54 -0400390 /* main() is big endian, file.o is little endian. */
391 w = w4rev;
392 w2 = w2rev;
393 w8 = w8rev;
394 }
Steven Rostedte90b0c82011-04-06 13:32:24 -0400395 break;
396 case ELFDATA2MSB:
Steven Rostedtdd5477f2011-04-06 13:21:17 -0400397 if (*(unsigned char const *)&endian != 0) {
John Reiser81d38582010-10-13 15:12:54 -0400398 /* main() is little endian, file.o is big endian. */
399 w = w4rev;
400 w2 = w2rev;
401 w8 = w8rev;
402 }
Steven Rostedte90b0c82011-04-06 13:32:24 -0400403 break;
John Reiser81d38582010-10-13 15:12:54 -0400404 } /* end switch */
Steven Rostedtdd5477f2011-04-06 13:21:17 -0400405 if (memcmp(ELFMAG, ehdr->e_ident, SELFMAG) != 0
406 || w2(ehdr->e_type) != ET_REL
407 || ehdr->e_ident[EI_VERSION] != EV_CURRENT) {
John Reiser81d38582010-10-13 15:12:54 -0400408 fprintf(stderr, "unrecognized ET_REL file %s\n", fname);
409 fail_file();
410 }
411
412 gpfx = 0;
413 switch (w2(ehdr->e_machine)) {
Steven Rostedte90b0c82011-04-06 13:32:24 -0400414 default:
John Reiser81d38582010-10-13 15:12:54 -0400415 fprintf(stderr, "unrecognized e_machine %d %s\n",
416 w2(ehdr->e_machine), fname);
417 fail_file();
Steven Rostedte90b0c82011-04-06 13:32:24 -0400418 break;
Steven Rostedtffd618f2011-04-08 03:58:48 -0400419 case EM_386:
420 reltype = R_386_32;
421 make_nop = make_nop_x86;
422 ideal_nop = ideal_nop5_x86_32;
Martin Schwidefsky521ccb52011-05-10 10:10:41 +0200423 mcount_adjust_32 = -1;
Steven Rostedtffd618f2011-04-08 03:58:48 -0400424 break;
Rabin Vincented604532010-11-30 17:36:48 +0100425 case EM_ARM: reltype = R_ARM_ABS32;
426 altmcount = "__gnu_mcount_nc";
427 break;
John Reiser81d38582010-10-13 15:12:54 -0400428 case EM_IA_64: reltype = R_IA64_IMM64; gpfx = '_'; break;
John Reisera2d49352010-10-27 18:59:07 +0800429 case EM_MIPS: /* reltype: e_class */ gpfx = '_'; break;
John Reiser81d38582010-10-13 15:12:54 -0400430 case EM_PPC: reltype = R_PPC_ADDR32; gpfx = '_'; break;
431 case EM_PPC64: reltype = R_PPC64_ADDR64; gpfx = '_'; break;
432 case EM_S390: /* reltype: e_class */ gpfx = '_'; break;
433 case EM_SH: reltype = R_SH_DIR32; break;
434 case EM_SPARCV9: reltype = R_SPARC_64; gpfx = '_'; break;
Steven Rostedtffd618f2011-04-08 03:58:48 -0400435 case EM_X86_64:
436 make_nop = make_nop_x86;
437 ideal_nop = ideal_nop5_x86_64;
438 reltype = R_X86_64_64;
Martin Schwidefsky521ccb52011-05-10 10:10:41 +0200439 mcount_adjust_64 = -1;
Steven Rostedtffd618f2011-04-08 03:58:48 -0400440 break;
John Reiser81d38582010-10-13 15:12:54 -0400441 } /* end switch */
442
443 switch (ehdr->e_ident[EI_CLASS]) {
Steven Rostedte90b0c82011-04-06 13:32:24 -0400444 default:
John Reiser81d38582010-10-13 15:12:54 -0400445 fprintf(stderr, "unrecognized ELF class %d %s\n",
446 ehdr->e_ident[EI_CLASS], fname);
447 fail_file();
Steven Rostedte90b0c82011-04-06 13:32:24 -0400448 break;
449 case ELFCLASS32:
Steven Rostedtdd5477f2011-04-06 13:21:17 -0400450 if (w2(ehdr->e_ehsize) != sizeof(Elf32_Ehdr)
451 || w2(ehdr->e_shentsize) != sizeof(Elf32_Shdr)) {
John Reiser81d38582010-10-13 15:12:54 -0400452 fprintf(stderr,
453 "unrecognized ET_REL file: %s\n", fname);
454 fail_file();
455 }
Martin Schwidefskyf2963882011-05-10 10:10:43 +0200456 if (w2(ehdr->e_machine) == EM_S390) {
John Reiser81d38582010-10-13 15:12:54 -0400457 reltype = R_390_32;
Martin Schwidefskyf2963882011-05-10 10:10:43 +0200458 mcount_adjust_32 = -4;
459 }
Steven Rostedtdd5477f2011-04-06 13:21:17 -0400460 if (w2(ehdr->e_machine) == EM_MIPS) {
John Reisera2d49352010-10-27 18:59:07 +0800461 reltype = R_MIPS_32;
Wu Zhangjin412910c2010-10-27 18:59:08 +0800462 is_fake_mcount32 = MIPS32_is_fake_mcount;
463 }
John Reiser81d38582010-10-13 15:12:54 -0400464 do32(ehdr, fname, reltype);
Steven Rostedte90b0c82011-04-06 13:32:24 -0400465 break;
John Reiser81d38582010-10-13 15:12:54 -0400466 case ELFCLASS64: {
467 Elf64_Ehdr *const ghdr = (Elf64_Ehdr *)ehdr;
Steven Rostedtdd5477f2011-04-06 13:21:17 -0400468 if (w2(ghdr->e_ehsize) != sizeof(Elf64_Ehdr)
469 || w2(ghdr->e_shentsize) != sizeof(Elf64_Shdr)) {
John Reiser81d38582010-10-13 15:12:54 -0400470 fprintf(stderr,
471 "unrecognized ET_REL file: %s\n", fname);
472 fail_file();
473 }
Martin Schwidefskyf2963882011-05-10 10:10:43 +0200474 if (w2(ghdr->e_machine) == EM_S390) {
John Reiser81d38582010-10-13 15:12:54 -0400475 reltype = R_390_64;
Martin Schwidefskyf2963882011-05-10 10:10:43 +0200476 mcount_adjust_64 = -8;
477 }
Steven Rostedtdd5477f2011-04-06 13:21:17 -0400478 if (w2(ghdr->e_machine) == EM_MIPS) {
John Reisera2d49352010-10-27 18:59:07 +0800479 reltype = R_MIPS_64;
480 Elf64_r_sym = MIPS64_r_sym;
481 Elf64_r_info = MIPS64_r_info;
Wu Zhangjin412910c2010-10-27 18:59:08 +0800482 is_fake_mcount64 = MIPS64_is_fake_mcount;
John Reisera2d49352010-10-27 18:59:07 +0800483 }
John Reiser81d38582010-10-13 15:12:54 -0400484 do64(ghdr, fname, reltype);
Steven Rostedte90b0c82011-04-06 13:32:24 -0400485 break;
486 }
John Reiser81d38582010-10-13 15:12:54 -0400487 } /* end switch */
488
Steven Rostedt (Red Hat)7e7340a2015-12-15 16:06:10 -0500489 write_file(fname);
John Reiser81d38582010-10-13 15:12:54 -0400490 cleanup();
491}
492
493int
Steven Rostedtdfad3d52011-04-12 18:53:25 -0400494main(int argc, char *argv[])
John Reiser81d38582010-10-13 15:12:54 -0400495{
Rabin Vincentcd3478f2010-11-30 17:33:53 +0100496 const char ftrace[] = "/ftrace.o";
Steven Rostedt44475862010-10-15 11:49:47 -0400497 int ftrace_size = sizeof(ftrace) - 1;
John Reiser81d38582010-10-13 15:12:54 -0400498 int n_error = 0; /* gcc-4.3.0 false positive complaint */
Steven Rostedtdfad3d52011-04-12 18:53:25 -0400499 int c;
500 int i;
Steven Rostedt44475862010-10-15 11:49:47 -0400501
Steven Rostedtdfad3d52011-04-12 18:53:25 -0400502 while ((c = getopt(argc, argv, "w")) >= 0) {
503 switch (c) {
504 case 'w':
505 warn_on_notrace_sect = 1;
506 break;
507 default:
508 fprintf(stderr, "usage: recordmcount [-w] file.o...\n");
509 return 0;
510 }
511 }
512
513 if ((argc - optind) < 1) {
514 fprintf(stderr, "usage: recordmcount [-w] file.o...\n");
Steven Rostedt44475862010-10-15 11:49:47 -0400515 return 0;
516 }
517
518 /* Process each file in turn, allowing deep failure. */
Steven Rostedtdfad3d52011-04-12 18:53:25 -0400519 for (i = optind; i < argc; i++) {
520 char *file = argv[i];
John Reiser81d38582010-10-13 15:12:54 -0400521 int const sjval = setjmp(jmpenv);
Steven Rostedt44475862010-10-15 11:49:47 -0400522 int len;
523
524 /*
525 * The file kernel/trace/ftrace.o references the mcount
526 * function but does not call it. Since ftrace.o should
527 * not be traced anyway, we just skip it.
528 */
Steven Rostedtdfad3d52011-04-12 18:53:25 -0400529 len = strlen(file);
Steven Rostedt44475862010-10-15 11:49:47 -0400530 if (len >= ftrace_size &&
Steven Rostedtdfad3d52011-04-12 18:53:25 -0400531 strcmp(file + (len - ftrace_size), ftrace) == 0)
Steven Rostedt44475862010-10-15 11:49:47 -0400532 continue;
533
John Reiser81d38582010-10-13 15:12:54 -0400534 switch (sjval) {
Steven Rostedte90b0c82011-04-06 13:32:24 -0400535 default:
Steven Rostedtdfad3d52011-04-12 18:53:25 -0400536 fprintf(stderr, "internal error: %s\n", file);
John Reiser81d38582010-10-13 15:12:54 -0400537 exit(1);
Steven Rostedte90b0c82011-04-06 13:32:24 -0400538 break;
539 case SJ_SETJMP: /* normal sequence */
John Reiser81d38582010-10-13 15:12:54 -0400540 /* Avoid problems if early cleanup() */
541 fd_map = -1;
John Reiser81d38582010-10-13 15:12:54 -0400542 mmap_failed = 1;
Steven Rostedt (Red Hat)7e7340a2015-12-15 16:06:10 -0500543 file_map = NULL;
544 file_ptr = NULL;
545 file_updated = 0;
Steven Rostedtdfad3d52011-04-12 18:53:25 -0400546 do_file(file);
Steven Rostedte90b0c82011-04-06 13:32:24 -0400547 break;
548 case SJ_FAIL: /* error in do_file or below */
Colin Ian Kingbaab2fe2015-12-30 23:06:41 +0000549 fprintf(stderr, "%s: failed\n", file);
John Reiser81d38582010-10-13 15:12:54 -0400550 ++n_error;
Steven Rostedte90b0c82011-04-06 13:32:24 -0400551 break;
552 case SJ_SUCCEED: /* premature success */
John Reiser81d38582010-10-13 15:12:54 -0400553 /* do nothing */
Steven Rostedte90b0c82011-04-06 13:32:24 -0400554 break;
John Reiser81d38582010-10-13 15:12:54 -0400555 } /* end switch */
556 }
557 return !!n_error;
558}
559
560