4317cf95ca024 (Thomas Gleixner 2019-05-31 01:09:38 -0700 1) // SPDX-License-Identifier: GPL-2.0-only
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 2) /*
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 3) * recordmcount.c: construct a table of the locations of calls to 'mcount'
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 4) * so that ftrace can find them quickly.
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 5) * Copyright 2009 John F. Reiser <jreiser@BitWagon.com>. All rights reserved.
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 6) *
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 7) * Restructured to fit Linux format, as well as other updates:
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 8) * Copyright 2010 Steven Rostedt <srostedt@redhat.com>, Red Hat Inc.
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 9) */
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 10)
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 11) /*
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 12) * Strategy: alter the .o file in-place.
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 13) *
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 14) * Append a new STRTAB that has the new section names, followed by a new array
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 15) * ElfXX_Shdr[] that has the new section headers, followed by the section
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 16) * contents for __mcount_loc and its relocations. The old shstrtab strings,
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 17) * and the old ElfXX_Shdr[] array, remain as "garbage" (commonly, a couple
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 18) * kilobytes.) Subsequent processing by /bin/ld (or the kernel module loader)
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 19) * will ignore the garbage regions, because they are not designated by the
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 20) * new .e_shoff nor the new ElfXX_Shdr[]. [In order to remove the garbage,
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 21) * then use "ld -r" to create a new file that omits the garbage.]
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 22) */
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 23)
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 24) #include <sys/types.h>
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 25) #include <sys/mman.h>
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 26) #include <sys/stat.h>
dfad3d598c4bb (Steven Rostedt 2011-04-12 18:53:25 -0400 27) #include <getopt.h>
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 28) #include <elf.h>
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 29) #include <fcntl.h>
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 30) #include <stdio.h>
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 31) #include <stdlib.h>
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 32) #include <string.h>
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 33) #include <unistd.h>
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 34)
af64d2aa872a1 (AKASHI Takahiro 2014-04-30 10:54:32 +0100 35) #ifndef EM_AARCH64
af64d2aa872a1 (AKASHI Takahiro 2014-04-30 10:54:32 +0100 36) #define EM_AARCH64 183
2ee8a74f2a5da (Li Bin 2015-10-30 16:31:04 +0800 37) #define R_AARCH64_NONE 0
af64d2aa872a1 (AKASHI Takahiro 2014-04-30 10:54:32 +0100 38) #define R_AARCH64_ABS64 257
af64d2aa872a1 (AKASHI Takahiro 2014-04-30 10:54:32 +0100 39) #endif
af64d2aa872a1 (AKASHI Takahiro 2014-04-30 10:54:32 +0100 40)
927d780ee371d (Alex Sverdlin 2020-01-08 15:57:47 +0100 41) #define R_ARM_PC24 1
927d780ee371d (Alex Sverdlin 2020-01-08 15:57:47 +0100 42) #define R_ARM_THM_CALL 10
927d780ee371d (Alex Sverdlin 2020-01-08 15:57:47 +0100 43) #define R_ARM_CALL 28
927d780ee371d (Alex Sverdlin 2020-01-08 15:57:47 +0100 44)
3df14264ad993 (Christophe Leroy 2020-08-10 08:48:22 +0000 45) #define R_AARCH64_CALL26 283
3df14264ad993 (Christophe Leroy 2020-08-10 08:48:22 +0000 46)
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 47) static int fd_map; /* File descriptor for file being modified. */
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 48) static int mmap_failed; /* Boolean flag. */
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 49) static char gpfx; /* prefix for global symbol name (sometimes '_') */
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 50) static struct stat sb; /* Remember .st_size, etc. */
ed60453fa8f8f (Rabin Vincent 2010-11-30 17:36:48 +0100 51) static const char *altmcount; /* alternate mcount symbol name */
dfad3d598c4bb (Steven Rostedt 2011-04-12 18:53:25 -0400 52) static int warn_on_notrace_sect; /* warn when section has mcount not being recorded */
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 53) static void *file_map; /* pointer of the mapped file */
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 54) static void *file_end; /* pointer to the end of the mapped file */
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 55) static int file_updated; /* flag to state file was changed */
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 56) static void *file_ptr; /* current file pointer location */
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 57)
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 58) static void *file_append; /* added to the end of the file */
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 59) static size_t file_append_size; /* how much is added to end of file */
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 60)
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 61) /* Per-file resource cleanup when multiple files. */
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 62) static void file_append_cleanup(void)
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 63) {
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 64) free(file_append);
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 65) file_append = NULL;
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 66) file_append_size = 0;
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 67) file_updated = 0;
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 68) }
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 69)
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 70) static void mmap_cleanup(void)
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 71) {
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 72) if (!mmap_failed)
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 73) munmap(file_map, sb.st_size);
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 74) else
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 75) free(file_map);
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 76) file_map = NULL;
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 77) }
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 78)
a146207916092 (Matt Helsley 2019-07-24 14:04:56 -0700 79) /* ulseek, uwrite, ...: Check return value for errors. */
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 80)
3aec8638246ff (Matt Helsley 2019-07-31 11:24:13 -0700 81) static off_t ulseek(off_t const offset, int const whence)
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 82) {
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 83) switch (whence) {
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 84) case SEEK_SET:
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 85) file_ptr = file_map + offset;
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 86) break;
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 87) case SEEK_CUR:
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 88) file_ptr += offset;
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 89) break;
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 90) case SEEK_END:
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 91) file_ptr = file_map + (sb.st_size - offset);
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 92) break;
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 93) }
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 94) if (file_ptr < file_map) {
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 95) fprintf(stderr, "lseek: seek before file\n");
3f1df12019f33 (Matt Helsley 2019-07-31 11:24:12 -0700 96) return -1;
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 97) }
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 98) return file_ptr - file_map;
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 99) }
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 100)
3aec8638246ff (Matt Helsley 2019-07-31 11:24:13 -0700 101) static ssize_t uwrite(void const *const buf, size_t const count)
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 102) {
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 103) size_t cnt = count;
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 104) off_t idx = 0;
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 105)
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 106) file_updated = 1;
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 107)
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 108) if (file_ptr + count >= file_end) {
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 109) off_t aoffset = (file_ptr + count) - file_end;
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 110)
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 111) if (aoffset > file_append_size) {
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 112) file_append = realloc(file_append, aoffset);
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 113) file_append_size = aoffset;
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 114) }
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 115) if (!file_append) {
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 116) perror("write");
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 117) file_append_cleanup();
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 118) mmap_cleanup();
3f1df12019f33 (Matt Helsley 2019-07-31 11:24:12 -0700 119) return -1;
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 120) }
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 121) if (file_ptr < file_end) {
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 122) cnt = file_end - file_ptr;
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 123) } else {
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 124) cnt = 0;
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 125) idx = aoffset - count;
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 126) }
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 127) }
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 128)
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 129) if (cnt)
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 130) memcpy(file_ptr, buf, cnt);
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 131)
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 132) if (cnt < count)
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 133) memcpy(file_append + idx, buf + cnt, count - cnt);
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 134)
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 135) file_ptr += count;
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 136) return count;
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 137) }
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 138)
3aec8638246ff (Matt Helsley 2019-07-31 11:24:13 -0700 139) static void * umalloc(size_t size)
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 140) {
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 141) void *const addr = malloc(size);
dd5477ff3ba97 (Steven Rostedt 2011-04-06 13:21:17 -0400 142) if (addr == 0) {
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 143) fprintf(stderr, "malloc failed: %zu bytes\n", size);
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 144) file_append_cleanup();
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 145) mmap_cleanup();
3f1df12019f33 (Matt Helsley 2019-07-31 11:24:12 -0700 146) return NULL;
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 147) }
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 148) return addr;
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 149) }
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 150)
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 151) /*
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 152) * Get the whole file as a programming convenience in order to avoid
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 153) * malloc+lseek+read+free of many pieces. If successful, then mmap
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 154) * avoids copying unused pieces; else just read the whole file.
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 155) * Open for both read and write; new info will be appended to the file.
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 156) * Use MAP_PRIVATE so that a few changes to the in-memory ElfXX_Ehdr
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 157) * do not propagate to the file until an explicit overwrite at the last.
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 158) * This preserves most aspects of consistency (all except .st_size)
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 159) * for simultaneous readers of the file while we are appending to it.
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 160) * However, multiple writers still are bad. We choose not to use
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 161) * locking because it is expensive and the use case of kernel build
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 162) * makes multiple writers unlikely.
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 163) */
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 164) static void *mmap_file(char const *fname)
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 165) {
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 166) /* Avoid problems if early cleanup() */
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 167) fd_map = -1;
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 168) mmap_failed = 1;
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 169) file_map = NULL;
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 170) file_ptr = NULL;
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 171) file_updated = 0;
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 172) sb.st_size = 0;
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 173)
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 174) fd_map = open(fname, O_RDONLY);
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 175) if (fd_map < 0) {
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 176) perror(fname);
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 177) return NULL;
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 178) }
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 179) if (fstat(fd_map, &sb) < 0) {
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 180) perror(fname);
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 181) goto out;
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 182) }
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 183) if (!S_ISREG(sb.st_mode)) {
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 184) fprintf(stderr, "not a regular file: %s\n", fname);
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 185) goto out;
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 186) }
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 187) file_map = mmap(0, sb.st_size, PROT_READ|PROT_WRITE, MAP_PRIVATE,
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 188) fd_map, 0);
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 189) if (file_map == MAP_FAILED) {
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 190) mmap_failed = 1;
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 191) file_map = umalloc(sb.st_size);
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 192) if (!file_map) {
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 193) perror(fname);
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 194) goto out;
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 195) }
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 196) if (read(fd_map, file_map, sb.st_size) != sb.st_size) {
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 197) perror(fname);
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 198) free(file_map);
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 199) file_map = NULL;
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 200) goto out;
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 201) }
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 202) } else
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 203) mmap_failed = 0;
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 204) out:
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 205) close(fd_map);
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 206) fd_map = -1;
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 207)
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 208) file_end = file_map + sb.st_size;
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 209)
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 210) return file_map;
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 211) }
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 212)
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 213)
ffd618fa39284 (Steven Rostedt 2011-04-08 03:58:48 -0400 214) static unsigned char ideal_nop5_x86_64[5] = { 0x0f, 0x1f, 0x44, 0x00, 0x00 };
ffd618fa39284 (Steven Rostedt 2011-04-08 03:58:48 -0400 215) static unsigned char ideal_nop5_x86_32[5] = { 0x3e, 0x8d, 0x74, 0x26, 0x00 };
ffd618fa39284 (Steven Rostedt 2011-04-08 03:58:48 -0400 216) static unsigned char *ideal_nop;
ffd618fa39284 (Steven Rostedt 2011-04-08 03:58:48 -0400 217)
ffd618fa39284 (Steven Rostedt 2011-04-08 03:58:48 -0400 218) static char rel_type_nop;
ffd618fa39284 (Steven Rostedt 2011-04-08 03:58:48 -0400 219)
ffd618fa39284 (Steven Rostedt 2011-04-08 03:58:48 -0400 220) static int (*make_nop)(void *map, size_t const offset);
ffd618fa39284 (Steven Rostedt 2011-04-08 03:58:48 -0400 221)
ffd618fa39284 (Steven Rostedt 2011-04-08 03:58:48 -0400 222) static int make_nop_x86(void *map, size_t const offset)
ffd618fa39284 (Steven Rostedt 2011-04-08 03:58:48 -0400 223) {
ffd618fa39284 (Steven Rostedt 2011-04-08 03:58:48 -0400 224) uint32_t *ptr;
ffd618fa39284 (Steven Rostedt 2011-04-08 03:58:48 -0400 225) unsigned char *op;
ffd618fa39284 (Steven Rostedt 2011-04-08 03:58:48 -0400 226)
ffd618fa39284 (Steven Rostedt 2011-04-08 03:58:48 -0400 227) /* Confirm we have 0xe8 0x0 0x0 0x0 0x0 */
ffd618fa39284 (Steven Rostedt 2011-04-08 03:58:48 -0400 228) ptr = map + offset;
ffd618fa39284 (Steven Rostedt 2011-04-08 03:58:48 -0400 229) if (*ptr != 0)
ffd618fa39284 (Steven Rostedt 2011-04-08 03:58:48 -0400 230) return -1;
ffd618fa39284 (Steven Rostedt 2011-04-08 03:58:48 -0400 231)
ffd618fa39284 (Steven Rostedt 2011-04-08 03:58:48 -0400 232) op = map + offset - 1;
ffd618fa39284 (Steven Rostedt 2011-04-08 03:58:48 -0400 233) if (*op != 0xe8)
ffd618fa39284 (Steven Rostedt 2011-04-08 03:58:48 -0400 234) return -1;
ffd618fa39284 (Steven Rostedt 2011-04-08 03:58:48 -0400 235)
ffd618fa39284 (Steven Rostedt 2011-04-08 03:58:48 -0400 236) /* convert to nop */
3f1df12019f33 (Matt Helsley 2019-07-31 11:24:12 -0700 237) if (ulseek(offset - 1, SEEK_SET) < 0)
3f1df12019f33 (Matt Helsley 2019-07-31 11:24:12 -0700 238) return -1;
3f1df12019f33 (Matt Helsley 2019-07-31 11:24:12 -0700 239) if (uwrite(ideal_nop, 5) < 0)
3f1df12019f33 (Matt Helsley 2019-07-31 11:24:12 -0700 240) return -1;
ffd618fa39284 (Steven Rostedt 2011-04-08 03:58:48 -0400 241) return 0;
ffd618fa39284 (Steven Rostedt 2011-04-08 03:58:48 -0400 242) }
ffd618fa39284 (Steven Rostedt 2011-04-08 03:58:48 -0400 243)
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 244) static unsigned char ideal_nop4_arm_le[4] = { 0x00, 0x00, 0xa0, 0xe1 }; /* mov r0, r0 */
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 245) static unsigned char ideal_nop4_arm_be[4] = { 0xe1, 0xa0, 0x00, 0x00 }; /* mov r0, r0 */
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 246) static unsigned char *ideal_nop4_arm;
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 247)
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 248) static unsigned char bl_mcount_arm_le[4] = { 0xfe, 0xff, 0xff, 0xeb }; /* bl */
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 249) static unsigned char bl_mcount_arm_be[4] = { 0xeb, 0xff, 0xff, 0xfe }; /* bl */
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 250) static unsigned char *bl_mcount_arm;
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 251)
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 252) static unsigned char push_arm_le[4] = { 0x04, 0xe0, 0x2d, 0xe5 }; /* push {lr} */
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 253) static unsigned char push_arm_be[4] = { 0xe5, 0x2d, 0xe0, 0x04 }; /* push {lr} */
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 254) static unsigned char *push_arm;
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 255)
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 256) static unsigned char ideal_nop2_thumb_le[2] = { 0x00, 0xbf }; /* nop */
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 257) static unsigned char ideal_nop2_thumb_be[2] = { 0xbf, 0x00 }; /* nop */
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 258) static unsigned char *ideal_nop2_thumb;
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 259)
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 260) static unsigned char push_bl_mcount_thumb_le[6] = { 0x00, 0xb5, 0xff, 0xf7, 0xfe, 0xff }; /* push {lr}, bl */
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 261) static unsigned char push_bl_mcount_thumb_be[6] = { 0xb5, 0x00, 0xf7, 0xff, 0xff, 0xfe }; /* push {lr}, bl */
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 262) static unsigned char *push_bl_mcount_thumb;
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 263)
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 264) static int make_nop_arm(void *map, size_t const offset)
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 265) {
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 266) char *ptr;
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 267) int cnt = 1;
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 268) int nop_size;
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 269) size_t off = offset;
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 270)
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 271) ptr = map + offset;
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 272) if (memcmp(ptr, bl_mcount_arm, 4) == 0) {
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 273) if (memcmp(ptr - 4, push_arm, 4) == 0) {
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 274) off -= 4;
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 275) cnt = 2;
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 276) }
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 277) ideal_nop = ideal_nop4_arm;
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 278) nop_size = 4;
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 279) } else if (memcmp(ptr - 2, push_bl_mcount_thumb, 6) == 0) {
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 280) cnt = 3;
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 281) nop_size = 2;
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 282) off -= 2;
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 283) ideal_nop = ideal_nop2_thumb;
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 284) } else
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 285) return -1;
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 286)
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 287) /* Convert to nop */
3f1df12019f33 (Matt Helsley 2019-07-31 11:24:12 -0700 288) if (ulseek(off, SEEK_SET) < 0)
3f1df12019f33 (Matt Helsley 2019-07-31 11:24:12 -0700 289) return -1;
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 290)
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 291) do {
3f1df12019f33 (Matt Helsley 2019-07-31 11:24:12 -0700 292) if (uwrite(ideal_nop, nop_size) < 0)
3f1df12019f33 (Matt Helsley 2019-07-31 11:24:12 -0700 293) return -1;
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 294) } while (--cnt > 0);
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 295)
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 296) return 0;
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 297) }
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 298)
2ee8a74f2a5da (Li Bin 2015-10-30 16:31:04 +0800 299) static unsigned char ideal_nop4_arm64[4] = {0x1f, 0x20, 0x03, 0xd5};
2ee8a74f2a5da (Li Bin 2015-10-30 16:31:04 +0800 300) static int make_nop_arm64(void *map, size_t const offset)
2ee8a74f2a5da (Li Bin 2015-10-30 16:31:04 +0800 301) {
2ee8a74f2a5da (Li Bin 2015-10-30 16:31:04 +0800 302) uint32_t *ptr;
2ee8a74f2a5da (Li Bin 2015-10-30 16:31:04 +0800 303)
2ee8a74f2a5da (Li Bin 2015-10-30 16:31:04 +0800 304) ptr = map + offset;
2ee8a74f2a5da (Li Bin 2015-10-30 16:31:04 +0800 305) /* bl <_mcount> is 0x94000000 before relocation */
2ee8a74f2a5da (Li Bin 2015-10-30 16:31:04 +0800 306) if (*ptr != 0x94000000)
2ee8a74f2a5da (Li Bin 2015-10-30 16:31:04 +0800 307) return -1;
2ee8a74f2a5da (Li Bin 2015-10-30 16:31:04 +0800 308)
2ee8a74f2a5da (Li Bin 2015-10-30 16:31:04 +0800 309) /* Convert to nop */
3f1df12019f33 (Matt Helsley 2019-07-31 11:24:12 -0700 310) if (ulseek(offset, SEEK_SET) < 0)
3f1df12019f33 (Matt Helsley 2019-07-31 11:24:12 -0700 311) return -1;
3f1df12019f33 (Matt Helsley 2019-07-31 11:24:12 -0700 312) if (uwrite(ideal_nop, 4) < 0)
3f1df12019f33 (Matt Helsley 2019-07-31 11:24:12 -0700 313) return -1;
2ee8a74f2a5da (Li Bin 2015-10-30 16:31:04 +0800 314) return 0;
2ee8a74f2a5da (Li Bin 2015-10-30 16:31:04 +0800 315) }
2ee8a74f2a5da (Li Bin 2015-10-30 16:31:04 +0800 316)
3f1df12019f33 (Matt Helsley 2019-07-31 11:24:12 -0700 317) static int write_file(const char *fname)
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 318) {
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 319) char tmp_file[strlen(fname) + 4];
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 320) size_t n;
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 321)
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 322) if (!file_updated)
3f1df12019f33 (Matt Helsley 2019-07-31 11:24:12 -0700 323) return 0;
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 324)
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 325) sprintf(tmp_file, "%s.rc", fname);
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 326)
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 327) /*
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 328) * After reading the entire file into memory, delete it
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 329) * and write it back, to prevent weird side effects of modifying
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 330) * an object file in place.
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 331) */
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 332) fd_map = open(tmp_file, O_WRONLY | O_TRUNC | O_CREAT, sb.st_mode);
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 333) if (fd_map < 0) {
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 334) perror(fname);
3f1df12019f33 (Matt Helsley 2019-07-31 11:24:12 -0700 335) return -1;
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 336) }
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 337) n = write(fd_map, file_map, sb.st_size);
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 338) if (n != sb.st_size) {
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 339) perror("write");
3f1df12019f33 (Matt Helsley 2019-07-31 11:24:12 -0700 340) close(fd_map);
3f1df12019f33 (Matt Helsley 2019-07-31 11:24:12 -0700 341) return -1;
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 342) }
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 343) if (file_append_size) {
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 344) n = write(fd_map, file_append, file_append_size);
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 345) if (n != file_append_size) {
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 346) perror("write");
3f1df12019f33 (Matt Helsley 2019-07-31 11:24:12 -0700 347) close(fd_map);
3f1df12019f33 (Matt Helsley 2019-07-31 11:24:12 -0700 348) return -1;
dd39a26538e37 (Russell King 2015-12-11 12:09:03 +0000 349) }
dd39a26538e37 (Russell King 2015-12-11 12:09:03 +0000 350) }
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 351) close(fd_map);
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 352) if (rename(tmp_file, fname) < 0) {
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 353) perror(fname);
3f1df12019f33 (Matt Helsley 2019-07-31 11:24:12 -0700 354) return -1;
a50bd43935586 (Steven Rostedt (Red Hat) 2015-12-15 16:06:10 -0500 355) }
3f1df12019f33 (Matt Helsley 2019-07-31 11:24:12 -0700 356) return 0;
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 357) }
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 358)
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 359) /* w8rev, w8nat, ...: Handle endianness. */
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 360)
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 361) static uint64_t w8rev(uint64_t const x)
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 362) {
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 363) return ((0xff & (x >> (0 * 8))) << (7 * 8))
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 364) | ((0xff & (x >> (1 * 8))) << (6 * 8))
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 365) | ((0xff & (x >> (2 * 8))) << (5 * 8))
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 366) | ((0xff & (x >> (3 * 8))) << (4 * 8))
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 367) | ((0xff & (x >> (4 * 8))) << (3 * 8))
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 368) | ((0xff & (x >> (5 * 8))) << (2 * 8))
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 369) | ((0xff & (x >> (6 * 8))) << (1 * 8))
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 370) | ((0xff & (x >> (7 * 8))) << (0 * 8));
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 371) }
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 372)
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 373) static uint32_t w4rev(uint32_t const x)
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 374) {
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 375) return ((0xff & (x >> (0 * 8))) << (3 * 8))
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 376) | ((0xff & (x >> (1 * 8))) << (2 * 8))
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 377) | ((0xff & (x >> (2 * 8))) << (1 * 8))
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 378) | ((0xff & (x >> (3 * 8))) << (0 * 8));
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 379) }
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 380)
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 381) static uint32_t w2rev(uint16_t const x)
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 382) {
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 383) return ((0xff & (x >> (0 * 8))) << (1 * 8))
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 384) | ((0xff & (x >> (1 * 8))) << (0 * 8));
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 385) }
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 386)
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 387) static uint64_t w8nat(uint64_t const x)
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 388) {
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 389) return x;
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 390) }
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 391)
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 392) static uint32_t w4nat(uint32_t const x)
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 393) {
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 394) return x;
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 395) }
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 396)
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 397) static uint32_t w2nat(uint16_t const x)
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 398) {
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 399) return x;
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 400) }
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 401)
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 402) static uint64_t (*w8)(uint64_t);
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 403) static uint32_t (*w)(uint32_t);
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 404) static uint32_t (*w2)(uint16_t);
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 405)
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 406) /* Names of the sections that could contain calls to mcount. */
3aec8638246ff (Matt Helsley 2019-07-31 11:24:13 -0700 407) static int is_mcounted_section_name(char const *const txtname)
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 408) {
9c8e2f6d3d361 (Joe Lawrence 2018-11-20 15:19:18 -0500 409) return strncmp(".text", txtname, 5) == 0 ||
42c269c88dc14 (Steven Rostedt (VMware) 2017-03-03 16:15:39 -0500 410) strcmp(".init.text", txtname) == 0 ||
dd5477ff3ba97 (Steven Rostedt 2011-04-06 13:21:17 -0400 411) strcmp(".ref.text", txtname) == 0 ||
dd5477ff3ba97 (Steven Rostedt 2011-04-06 13:21:17 -0400 412) strcmp(".sched.text", txtname) == 0 ||
dd5477ff3ba97 (Steven Rostedt 2011-04-06 13:21:17 -0400 413) strcmp(".spinlock.text", txtname) == 0 ||
dd5477ff3ba97 (Steven Rostedt 2011-04-06 13:21:17 -0400 414) strcmp(".irqentry.text", txtname) == 0 ||
e436fd61a8f62 (Dmitry Vyukov 2016-09-28 15:22:36 -0700 415) strcmp(".softirqentry.text", txtname) == 0 ||
9f087e7612115 (Steven Rostedt 2011-04-06 14:10:22 -0400 416) strcmp(".kprobes.text", txtname) == 0 ||
1bd95be204aa3 (Matt Helsley 2019-07-24 14:04:55 -0700 417) strcmp(".cpuidle.text", txtname) == 0;
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 418) }
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 419)
3f1df12019f33 (Matt Helsley 2019-07-31 11:24:12 -0700 420) static char const *already_has_rel_mcount = "success"; /* our work here is done! */
3f1df12019f33 (Matt Helsley 2019-07-31 11:24:12 -0700 421)
c28d5077f8d79 (Steven Rostedt 2010-10-13 19:06:14 -0400 422) /* 32 bit and 64 bit are very similar */
c28d5077f8d79 (Steven Rostedt 2010-10-13 19:06:14 -0400 423) #include "recordmcount.h"
c28d5077f8d79 (Steven Rostedt 2010-10-13 19:06:14 -0400 424) #define RECORD_MCOUNT_64
c28d5077f8d79 (Steven Rostedt 2010-10-13 19:06:14 -0400 425) #include "recordmcount.h"
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 426)
927d780ee371d (Alex Sverdlin 2020-01-08 15:57:47 +0100 427) static int arm_is_fake_mcount(Elf32_Rel const *rp)
927d780ee371d (Alex Sverdlin 2020-01-08 15:57:47 +0100 428) {
927d780ee371d (Alex Sverdlin 2020-01-08 15:57:47 +0100 429) switch (ELF32_R_TYPE(w(rp->r_info))) {
927d780ee371d (Alex Sverdlin 2020-01-08 15:57:47 +0100 430) case R_ARM_THM_CALL:
927d780ee371d (Alex Sverdlin 2020-01-08 15:57:47 +0100 431) case R_ARM_CALL:
927d780ee371d (Alex Sverdlin 2020-01-08 15:57:47 +0100 432) case R_ARM_PC24:
927d780ee371d (Alex Sverdlin 2020-01-08 15:57:47 +0100 433) return 0;
927d780ee371d (Alex Sverdlin 2020-01-08 15:57:47 +0100 434) }
927d780ee371d (Alex Sverdlin 2020-01-08 15:57:47 +0100 435)
927d780ee371d (Alex Sverdlin 2020-01-08 15:57:47 +0100 436) return 1;
927d780ee371d (Alex Sverdlin 2020-01-08 15:57:47 +0100 437) }
927d780ee371d (Alex Sverdlin 2020-01-08 15:57:47 +0100 438)
ea0eada45632f (Gregory Herrero 2020-07-17 16:33:38 +0200 439) static int arm64_is_fake_mcount(Elf64_Rel const *rp)
ea0eada45632f (Gregory Herrero 2020-07-17 16:33:38 +0200 440) {
999340d51174c (Chen Jun 2021-02-22 13:58:40 +0000 441) return ELF64_R_TYPE(w8(rp->r_info)) != R_AARCH64_CALL26;
ea0eada45632f (Gregory Herrero 2020-07-17 16:33:38 +0200 442) }
ea0eada45632f (Gregory Herrero 2020-07-17 16:33:38 +0200 443)
a2d49358ba9bc (John Reiser 2010-10-27 18:59:07 +0800 444) /* 64-bit EM_MIPS has weird ELF64_Rela.r_info.
a2d49358ba9bc (John Reiser 2010-10-27 18:59:07 +0800 445) * http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf
a2d49358ba9bc (John Reiser 2010-10-27 18:59:07 +0800 446) * We interpret Table 29 Relocation Operation (Elf64_Rel, Elf64_Rela) [p.40]
a2d49358ba9bc (John Reiser 2010-10-27 18:59:07 +0800 447) * to imply the order of the members; the spec does not say so.
a2d49358ba9bc (John Reiser 2010-10-27 18:59:07 +0800 448) * typedef unsigned char Elf64_Byte;
a2d49358ba9bc (John Reiser 2010-10-27 18:59:07 +0800 449) * fails on MIPS64 because their <elf.h> already has it!
a2d49358ba9bc (John Reiser 2010-10-27 18:59:07 +0800 450) */
a2d49358ba9bc (John Reiser 2010-10-27 18:59:07 +0800 451)
a2d49358ba9bc (John Reiser 2010-10-27 18:59:07 +0800 452) typedef uint8_t myElf64_Byte; /* Type for a 8-bit quantity. */
a2d49358ba9bc (John Reiser 2010-10-27 18:59:07 +0800 453)
a2d49358ba9bc (John Reiser 2010-10-27 18:59:07 +0800 454) union mips_r_info {
a2d49358ba9bc (John Reiser 2010-10-27 18:59:07 +0800 455) Elf64_Xword r_info;
a2d49358ba9bc (John Reiser 2010-10-27 18:59:07 +0800 456) struct {
a2d49358ba9bc (John Reiser 2010-10-27 18:59:07 +0800 457) Elf64_Word r_sym; /* Symbol index. */
a2d49358ba9bc (John Reiser 2010-10-27 18:59:07 +0800 458) myElf64_Byte r_ssym; /* Special symbol. */
a2d49358ba9bc (John Reiser 2010-10-27 18:59:07 +0800 459) myElf64_Byte r_type3; /* Third relocation. */
a2d49358ba9bc (John Reiser 2010-10-27 18:59:07 +0800 460) myElf64_Byte r_type2; /* Second relocation. */
a2d49358ba9bc (John Reiser 2010-10-27 18:59:07 +0800 461) myElf64_Byte r_type; /* First relocation. */
a2d49358ba9bc (John Reiser 2010-10-27 18:59:07 +0800 462) } r_mips;
a2d49358ba9bc (John Reiser 2010-10-27 18:59:07 +0800 463) };
a2d49358ba9bc (John Reiser 2010-10-27 18:59:07 +0800 464)
a2d49358ba9bc (John Reiser 2010-10-27 18:59:07 +0800 465) static uint64_t MIPS64_r_sym(Elf64_Rel const *rp)
a2d49358ba9bc (John Reiser 2010-10-27 18:59:07 +0800 466) {
a2d49358ba9bc (John Reiser 2010-10-27 18:59:07 +0800 467) return w(((union mips_r_info){ .r_info = rp->r_info }).r_mips.r_sym);
a2d49358ba9bc (John Reiser 2010-10-27 18:59:07 +0800 468) }
a2d49358ba9bc (John Reiser 2010-10-27 18:59:07 +0800 469)
a2d49358ba9bc (John Reiser 2010-10-27 18:59:07 +0800 470) static void MIPS64_r_info(Elf64_Rel *const rp, unsigned sym, unsigned type)
a2d49358ba9bc (John Reiser 2010-10-27 18:59:07 +0800 471) {
a2d49358ba9bc (John Reiser 2010-10-27 18:59:07 +0800 472) rp->r_info = ((union mips_r_info){
a2d49358ba9bc (John Reiser 2010-10-27 18:59:07 +0800 473) .r_mips = { .r_sym = w(sym), .r_type = type }
a2d49358ba9bc (John Reiser 2010-10-27 18:59:07 +0800 474) }).r_info;
a2d49358ba9bc (John Reiser 2010-10-27 18:59:07 +0800 475) }
a2d49358ba9bc (John Reiser 2010-10-27 18:59:07 +0800 476)
3aec8638246ff (Matt Helsley 2019-07-31 11:24:13 -0700 477) static int do_file(char const *const fname)
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 478) {
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 479) unsigned int reltype = 0;
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 480) Elf32_Ehdr *ehdr;
3f1df12019f33 (Matt Helsley 2019-07-31 11:24:12 -0700 481) int rc = -1;
3f1df12019f33 (Matt Helsley 2019-07-31 11:24:12 -0700 482)
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 483) ehdr = mmap_file(fname);
3f1df12019f33 (Matt Helsley 2019-07-31 11:24:12 -0700 484) if (!ehdr)
3f1df12019f33 (Matt Helsley 2019-07-31 11:24:12 -0700 485) goto out;
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 486)
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 487) w = w4nat;
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 488) w2 = w2nat;
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 489) w8 = w8nat;
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 490) switch (ehdr->e_ident[EI_DATA]) {
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 491) static unsigned int const endian = 1;
e90b0c8bf2119 (Steven Rostedt 2011-04-06 13:32:24 -0400 492) default:
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 493) fprintf(stderr, "unrecognized ELF data encoding %d: %s\n",
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 494) ehdr->e_ident[EI_DATA], fname);
3f1df12019f33 (Matt Helsley 2019-07-31 11:24:12 -0700 495) goto out;
e90b0c8bf2119 (Steven Rostedt 2011-04-06 13:32:24 -0400 496) case ELFDATA2LSB:
dd5477ff3ba97 (Steven Rostedt 2011-04-06 13:21:17 -0400 497) if (*(unsigned char const *)&endian != 1) {
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 498) /* main() is big endian, file.o is little endian. */
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 499) w = w4rev;
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 500) w2 = w2rev;
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 501) w8 = w8rev;
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 502) }
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 503) ideal_nop4_arm = ideal_nop4_arm_le;
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 504) bl_mcount_arm = bl_mcount_arm_le;
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 505) push_arm = push_arm_le;
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 506) ideal_nop2_thumb = ideal_nop2_thumb_le;
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 507) push_bl_mcount_thumb = push_bl_mcount_thumb_le;
e90b0c8bf2119 (Steven Rostedt 2011-04-06 13:32:24 -0400 508) break;
e90b0c8bf2119 (Steven Rostedt 2011-04-06 13:32:24 -0400 509) case ELFDATA2MSB:
dd5477ff3ba97 (Steven Rostedt 2011-04-06 13:21:17 -0400 510) if (*(unsigned char const *)&endian != 0) {
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 511) /* main() is little endian, file.o is big endian. */
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 512) w = w4rev;
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 513) w2 = w2rev;
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 514) w8 = w8rev;
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 515) }
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 516) ideal_nop4_arm = ideal_nop4_arm_be;
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 517) bl_mcount_arm = bl_mcount_arm_be;
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 518) push_arm = push_arm_be;
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 519) ideal_nop2_thumb = ideal_nop2_thumb_be;
9648dc15772d7 (Stephen Boyd 2016-10-18 16:42:00 -0700 520) push_bl_mcount_thumb = push_bl_mcount_thumb_be;
e90b0c8bf2119 (Steven Rostedt 2011-04-06 13:32:24 -0400 521) break;
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 522) } /* end switch */
2e63152bc1909 (Matt Helsley 2019-07-31 11:24:14 -0700 523) if (memcmp(ELFMAG, ehdr->e_ident, SELFMAG) != 0 ||
2e63152bc1909 (Matt Helsley 2019-07-31 11:24:14 -0700 524) w2(ehdr->e_type) != ET_REL ||
2e63152bc1909 (Matt Helsley 2019-07-31 11:24:14 -0700 525) ehdr->e_ident[EI_VERSION] != EV_CURRENT) {
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 526) fprintf(stderr, "unrecognized ET_REL file %s\n", fname);
3f1df12019f33 (Matt Helsley 2019-07-31 11:24:12 -0700 527) goto out;
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 528) }
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 529)
2e63152bc1909 (Matt Helsley 2019-07-31 11:24:14 -0700 530) gpfx = '_';
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 531) switch (w2(ehdr->e_machine)) {
e90b0c8bf2119 (Steven Rostedt 2011-04-06 13:32:24 -0400 532) default:
ac5db1fc89bf8 (nixiaoming 2018-05-24 11:16:12 +0800 533) fprintf(stderr, "unrecognized e_machine %u %s\n",
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 534) w2(ehdr->e_machine), fname);
3f1df12019f33 (Matt Helsley 2019-07-31 11:24:12 -0700 535) goto out;
ffd618fa39284 (Steven Rostedt 2011-04-08 03:58:48 -0400 536) case EM_386:
ffd618fa39284 (Steven Rostedt 2011-04-08 03:58:48 -0400 537) reltype = R_386_32;
46a2b61ecb286 (Li Bin 2015-10-28 16:23:26 +0800 538) rel_type_nop = R_386_NONE;
ffd618fa39284 (Steven Rostedt 2011-04-08 03:58:48 -0400 539) make_nop = make_nop_x86;
ffd618fa39284 (Steven Rostedt 2011-04-08 03:58:48 -0400 540) ideal_nop = ideal_nop5_x86_32;
521ccb5c4aece (Martin Schwidefsky 2011-05-10 10:10:41 +0200 541) mcount_adjust_32 = -1;
2e63152bc1909 (Matt Helsley 2019-07-31 11:24:14 -0700 542) gpfx = 0;
2e63152bc1909 (Matt Helsley 2019-07-31 11:24:14 -0700 543) break;
2e63152bc1909 (Matt Helsley 2019-07-31 11:24:14 -0700 544) case EM_ARM:
2e63152bc1909 (Matt Helsley 2019-07-31 11:24:14 -0700 545) reltype = R_ARM_ABS32;
2e63152bc1909 (Matt Helsley 2019-07-31 11:24:14 -0700 546) altmcount = "__gnu_mcount_nc";
2e63152bc1909 (Matt Helsley 2019-07-31 11:24:14 -0700 547) make_nop = make_nop_arm;
2e63152bc1909 (Matt Helsley 2019-07-31 11:24:14 -0700 548) rel_type_nop = R_ARM_NONE;
927d780ee371d (Alex Sverdlin 2020-01-08 15:57:47 +0100 549) is_fake_mcount32 = arm_is_fake_mcount;
2e63152bc1909 (Matt Helsley 2019-07-31 11:24:14 -0700 550) gpfx = 0;
ffd618fa39284 (Steven Rostedt 2011-04-08 03:58:48 -0400 551) break;
af64d2aa872a1 (AKASHI Takahiro 2014-04-30 10:54:32 +0100 552) case EM_AARCH64:
2e63152bc1909 (Matt Helsley 2019-07-31 11:24:14 -0700 553) reltype = R_AARCH64_ABS64;
2e63152bc1909 (Matt Helsley 2019-07-31 11:24:14 -0700 554) make_nop = make_nop_arm64;
2e63152bc1909 (Matt Helsley 2019-07-31 11:24:14 -0700 555) rel_type_nop = R_AARCH64_NONE;
2e63152bc1909 (Matt Helsley 2019-07-31 11:24:14 -0700 556) ideal_nop = ideal_nop4_arm64;
ea0eada45632f (Gregory Herrero 2020-07-17 16:33:38 +0200 557) is_fake_mcount64 = arm64_is_fake_mcount;
2e63152bc1909 (Matt Helsley 2019-07-31 11:24:14 -0700 558) break;
2e63152bc1909 (Matt Helsley 2019-07-31 11:24:14 -0700 559) case EM_IA_64: reltype = R_IA64_IMM64; break;
2e63152bc1909 (Matt Helsley 2019-07-31 11:24:14 -0700 560) case EM_MIPS: /* reltype: e_class */ break;
2e63152bc1909 (Matt Helsley 2019-07-31 11:24:14 -0700 561) case EM_PPC: reltype = R_PPC_ADDR32; break;
2e63152bc1909 (Matt Helsley 2019-07-31 11:24:14 -0700 562) case EM_PPC64: reltype = R_PPC64_ADDR64; break;
2e63152bc1909 (Matt Helsley 2019-07-31 11:24:14 -0700 563) case EM_S390: /* reltype: e_class */ break;
2e63152bc1909 (Matt Helsley 2019-07-31 11:24:14 -0700 564) case EM_SH: reltype = R_SH_DIR32; gpfx = 0; break;
2e63152bc1909 (Matt Helsley 2019-07-31 11:24:14 -0700 565) case EM_SPARCV9: reltype = R_SPARC_64; break;
ffd618fa39284 (Steven Rostedt 2011-04-08 03:58:48 -0400 566) case EM_X86_64:
ffd618fa39284 (Steven Rostedt 2011-04-08 03:58:48 -0400 567) make_nop = make_nop_x86;
ffd618fa39284 (Steven Rostedt 2011-04-08 03:58:48 -0400 568) ideal_nop = ideal_nop5_x86_64;
ffd618fa39284 (Steven Rostedt 2011-04-08 03:58:48 -0400 569) reltype = R_X86_64_64;
46a2b61ecb286 (Li Bin 2015-10-28 16:23:26 +0800 570) rel_type_nop = R_X86_64_NONE;
521ccb5c4aece (Martin Schwidefsky 2011-05-10 10:10:41 +0200 571) mcount_adjust_64 = -1;
2e63152bc1909 (Matt Helsley 2019-07-31 11:24:14 -0700 572) gpfx = 0;
ffd618fa39284 (Steven Rostedt 2011-04-08 03:58:48 -0400 573) break;
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 574) } /* end switch */
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 575)
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 576) switch (ehdr->e_ident[EI_CLASS]) {
e90b0c8bf2119 (Steven Rostedt 2011-04-06 13:32:24 -0400 577) default:
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 578) fprintf(stderr, "unrecognized ELF class %d %s\n",
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 579) ehdr->e_ident[EI_CLASS], fname);
3f1df12019f33 (Matt Helsley 2019-07-31 11:24:12 -0700 580) goto out;
e90b0c8bf2119 (Steven Rostedt 2011-04-06 13:32:24 -0400 581) case ELFCLASS32:
dd5477ff3ba97 (Steven Rostedt 2011-04-06 13:21:17 -0400 582) if (w2(ehdr->e_ehsize) != sizeof(Elf32_Ehdr)
dd5477ff3ba97 (Steven Rostedt 2011-04-06 13:21:17 -0400 583) || w2(ehdr->e_shentsize) != sizeof(Elf32_Shdr)) {
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 584) fprintf(stderr,
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 585) "unrecognized ET_REL file: %s\n", fname);
3f1df12019f33 (Matt Helsley 2019-07-31 11:24:12 -0700 586) goto out;
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 587) }
dd5477ff3ba97 (Steven Rostedt 2011-04-06 13:21:17 -0400 588) if (w2(ehdr->e_machine) == EM_MIPS) {
a2d49358ba9bc (John Reiser 2010-10-27 18:59:07 +0800 589) reltype = R_MIPS_32;
412910cd046c1 (Wu Zhangjin 2010-10-27 18:59:08 +0800 590) is_fake_mcount32 = MIPS32_is_fake_mcount;
412910cd046c1 (Wu Zhangjin 2010-10-27 18:59:08 +0800 591) }
3f1df12019f33 (Matt Helsley 2019-07-31 11:24:12 -0700 592) if (do32(ehdr, fname, reltype) < 0)
3f1df12019f33 (Matt Helsley 2019-07-31 11:24:12 -0700 593) goto out;
e90b0c8bf2119 (Steven Rostedt 2011-04-06 13:32:24 -0400 594) break;
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 595) case ELFCLASS64: {
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 596) Elf64_Ehdr *const ghdr = (Elf64_Ehdr *)ehdr;
dd5477ff3ba97 (Steven Rostedt 2011-04-06 13:21:17 -0400 597) if (w2(ghdr->e_ehsize) != sizeof(Elf64_Ehdr)
dd5477ff3ba97 (Steven Rostedt 2011-04-06 13:21:17 -0400 598) || w2(ghdr->e_shentsize) != sizeof(Elf64_Shdr)) {
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 599) fprintf(stderr,
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 600) "unrecognized ET_REL file: %s\n", fname);
3f1df12019f33 (Matt Helsley 2019-07-31 11:24:12 -0700 601) goto out;
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 602) }
f296388682805 (Martin Schwidefsky 2011-05-10 10:10:43 +0200 603) if (w2(ghdr->e_machine) == EM_S390) {
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 604) reltype = R_390_64;
c933146a5e41e (Heiko Carstens 2014-10-15 12:17:38 +0200 605) mcount_adjust_64 = -14;
f296388682805 (Martin Schwidefsky 2011-05-10 10:10:43 +0200 606) }
dd5477ff3ba97 (Steven Rostedt 2011-04-06 13:21:17 -0400 607) if (w2(ghdr->e_machine) == EM_MIPS) {
a2d49358ba9bc (John Reiser 2010-10-27 18:59:07 +0800 608) reltype = R_MIPS_64;
a2d49358ba9bc (John Reiser 2010-10-27 18:59:07 +0800 609) Elf64_r_sym = MIPS64_r_sym;
a2d49358ba9bc (John Reiser 2010-10-27 18:59:07 +0800 610) Elf64_r_info = MIPS64_r_info;
412910cd046c1 (Wu Zhangjin 2010-10-27 18:59:08 +0800 611) is_fake_mcount64 = MIPS64_is_fake_mcount;
a2d49358ba9bc (John Reiser 2010-10-27 18:59:07 +0800 612) }
3f1df12019f33 (Matt Helsley 2019-07-31 11:24:12 -0700 613) if (do64(ghdr, fname, reltype) < 0)
3f1df12019f33 (Matt Helsley 2019-07-31 11:24:12 -0700 614) goto out;
e90b0c8bf2119 (Steven Rostedt 2011-04-06 13:32:24 -0400 615) break;
e90b0c8bf2119 (Steven Rostedt 2011-04-06 13:32:24 -0400 616) }
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 617) } /* end switch */
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 618)
3f1df12019f33 (Matt Helsley 2019-07-31 11:24:12 -0700 619) rc = write_file(fname);
3f1df12019f33 (Matt Helsley 2019-07-31 11:24:12 -0700 620) out:
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 621) file_append_cleanup();
4fbcf0741697a (Matt Helsley 2019-07-31 11:24:16 -0700 622) mmap_cleanup();
3f1df12019f33 (Matt Helsley 2019-07-31 11:24:12 -0700 623) return rc;
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 624) }
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 625)
3aec8638246ff (Matt Helsley 2019-07-31 11:24:13 -0700 626) int main(int argc, char *argv[])
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 627) {
cd3478f2bd8f2 (Rabin Vincent 2010-11-30 17:33:53 +0100 628) const char ftrace[] = "/ftrace.o";
4447586364398 (Steven Rostedt 2010-10-15 11:49:47 -0400 629) int ftrace_size = sizeof(ftrace) - 1;
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 630) int n_error = 0; /* gcc-4.3.0 false positive complaint */
dfad3d598c4bb (Steven Rostedt 2011-04-12 18:53:25 -0400 631) int c;
dfad3d598c4bb (Steven Rostedt 2011-04-12 18:53:25 -0400 632) int i;
4447586364398 (Steven Rostedt 2010-10-15 11:49:47 -0400 633)
dfad3d598c4bb (Steven Rostedt 2011-04-12 18:53:25 -0400 634) while ((c = getopt(argc, argv, "w")) >= 0) {
dfad3d598c4bb (Steven Rostedt 2011-04-12 18:53:25 -0400 635) switch (c) {
dfad3d598c4bb (Steven Rostedt 2011-04-12 18:53:25 -0400 636) case 'w':
dfad3d598c4bb (Steven Rostedt 2011-04-12 18:53:25 -0400 637) warn_on_notrace_sect = 1;
dfad3d598c4bb (Steven Rostedt 2011-04-12 18:53:25 -0400 638) break;
dfad3d598c4bb (Steven Rostedt 2011-04-12 18:53:25 -0400 639) default:
dfad3d598c4bb (Steven Rostedt 2011-04-12 18:53:25 -0400 640) fprintf(stderr, "usage: recordmcount [-w] file.o...\n");
dfad3d598c4bb (Steven Rostedt 2011-04-12 18:53:25 -0400 641) return 0;
dfad3d598c4bb (Steven Rostedt 2011-04-12 18:53:25 -0400 642) }
dfad3d598c4bb (Steven Rostedt 2011-04-12 18:53:25 -0400 643) }
dfad3d598c4bb (Steven Rostedt 2011-04-12 18:53:25 -0400 644)
dfad3d598c4bb (Steven Rostedt 2011-04-12 18:53:25 -0400 645) if ((argc - optind) < 1) {
dfad3d598c4bb (Steven Rostedt 2011-04-12 18:53:25 -0400 646) fprintf(stderr, "usage: recordmcount [-w] file.o...\n");
4447586364398 (Steven Rostedt 2010-10-15 11:49:47 -0400 647) return 0;
4447586364398 (Steven Rostedt 2010-10-15 11:49:47 -0400 648) }
4447586364398 (Steven Rostedt 2010-10-15 11:49:47 -0400 649)
4447586364398 (Steven Rostedt 2010-10-15 11:49:47 -0400 650) /* Process each file in turn, allowing deep failure. */
dfad3d598c4bb (Steven Rostedt 2011-04-12 18:53:25 -0400 651) for (i = optind; i < argc; i++) {
dfad3d598c4bb (Steven Rostedt 2011-04-12 18:53:25 -0400 652) char *file = argv[i];
4447586364398 (Steven Rostedt 2010-10-15 11:49:47 -0400 653) int len;
4447586364398 (Steven Rostedt 2010-10-15 11:49:47 -0400 654)
4447586364398 (Steven Rostedt 2010-10-15 11:49:47 -0400 655) /*
4447586364398 (Steven Rostedt 2010-10-15 11:49:47 -0400 656) * The file kernel/trace/ftrace.o references the mcount
4447586364398 (Steven Rostedt 2010-10-15 11:49:47 -0400 657) * function but does not call it. Since ftrace.o should
4447586364398 (Steven Rostedt 2010-10-15 11:49:47 -0400 658) * not be traced anyway, we just skip it.
4447586364398 (Steven Rostedt 2010-10-15 11:49:47 -0400 659) */
dfad3d598c4bb (Steven Rostedt 2011-04-12 18:53:25 -0400 660) len = strlen(file);
4447586364398 (Steven Rostedt 2010-10-15 11:49:47 -0400 661) if (len >= ftrace_size &&
dfad3d598c4bb (Steven Rostedt 2011-04-12 18:53:25 -0400 662) strcmp(file + (len - ftrace_size), ftrace) == 0)
4447586364398 (Steven Rostedt 2010-10-15 11:49:47 -0400 663) continue;
4447586364398 (Steven Rostedt 2010-10-15 11:49:47 -0400 664)
3f1df12019f33 (Matt Helsley 2019-07-31 11:24:12 -0700 665) if (do_file(file)) {
713a3e4de707f (Colin Ian King 2015-12-30 23:06:41 +0000 666) fprintf(stderr, "%s: failed\n", file);
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 667) ++n_error;
3f1df12019f33 (Matt Helsley 2019-07-31 11:24:12 -0700 668) }
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 669) }
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 670) return !!n_error;
81d3858d3131a (John Reiser 2010-10-13 15:12:54 -0400 671) }