Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdarg.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <stdint.h> |
Kees Cook | 5d442e6 | 2013-04-12 13:13:43 -0700 | [diff] [blame] | 5 | #include <inttypes.h> |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 6 | #include <string.h> |
| 7 | #include <errno.h> |
| 8 | #include <unistd.h> |
| 9 | #include <elf.h> |
| 10 | #include <byteswap.h> |
| 11 | #define USE_BSD |
| 12 | #include <endian.h> |
H. Peter Anvin | 873b527 | 2009-12-14 13:55:20 -0800 | [diff] [blame] | 13 | #include <regex.h> |
Matt Fleming | 55f9709 | 2012-02-28 13:37:21 +0000 | [diff] [blame] | 14 | #include <tools/le_byteshift.h> |
H. Peter Anvin | 873b527 | 2009-12-14 13:55:20 -0800 | [diff] [blame] | 15 | |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame] | 16 | #define ElfW(type) _ElfW(ELF_BITS, type) |
| 17 | #define _ElfW(bits, type) __ElfW(bits, type) |
| 18 | #define __ElfW(bits, type) Elf##bits##_##type |
| 19 | |
Kees Cook | 946166a | 2013-04-12 13:13:44 -0700 | [diff] [blame^] | 20 | #ifndef ELF_BITS |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame] | 21 | #define ELF_BITS 32 |
Kees Cook | 946166a | 2013-04-12 13:13:44 -0700 | [diff] [blame^] | 22 | #endif |
| 23 | |
| 24 | #if (ELF_BITS == 64) |
| 25 | #define ELF_MACHINE EM_X86_64 |
| 26 | #define ELF_MACHINE_NAME "x86_64" |
| 27 | #define SHT_REL_TYPE SHT_RELA |
| 28 | #define Elf_Rel Elf64_Rela |
| 29 | #else |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame] | 30 | #define ELF_MACHINE EM_386 |
| 31 | #define ELF_MACHINE_NAME "i386" |
| 32 | #define SHT_REL_TYPE SHT_REL |
Kees Cook | 946166a | 2013-04-12 13:13:44 -0700 | [diff] [blame^] | 33 | #define Elf_Rel ElfW(Rel) |
| 34 | #endif |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame] | 35 | |
Kees Cook | 946166a | 2013-04-12 13:13:44 -0700 | [diff] [blame^] | 36 | #if (ELF_BITS == 64) |
| 37 | #define ELF_CLASS ELFCLASS64 |
| 38 | #define ELF_R_SYM(val) ELF64_R_SYM(val) |
| 39 | #define ELF_R_TYPE(val) ELF64_R_TYPE(val) |
| 40 | #define ELF_ST_TYPE(o) ELF64_ST_TYPE(o) |
| 41 | #define ELF_ST_BIND(o) ELF64_ST_BIND(o) |
| 42 | #define ELF_ST_VISIBILITY(o) ELF64_ST_VISIBILITY(o) |
| 43 | #else |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame] | 44 | #define ELF_CLASS ELFCLASS32 |
| 45 | #define ELF_R_SYM(val) ELF32_R_SYM(val) |
| 46 | #define ELF_R_TYPE(val) ELF32_R_TYPE(val) |
| 47 | #define ELF_ST_TYPE(o) ELF32_ST_TYPE(o) |
| 48 | #define ELF_ST_BIND(o) ELF32_ST_BIND(o) |
| 49 | #define ELF_ST_VISIBILITY(o) ELF32_ST_VISIBILITY(o) |
Kees Cook | 946166a | 2013-04-12 13:13:44 -0700 | [diff] [blame^] | 50 | #endif |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame] | 51 | |
Kees Cook | 946166a | 2013-04-12 13:13:44 -0700 | [diff] [blame^] | 52 | #define Elf_Addr ElfW(Addr) |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame] | 53 | #define Elf_Ehdr ElfW(Ehdr) |
| 54 | #define Elf_Phdr ElfW(Phdr) |
| 55 | #define Elf_Shdr ElfW(Shdr) |
| 56 | #define Elf_Sym ElfW(Sym) |
| 57 | |
H. Peter Anvin | 873b527 | 2009-12-14 13:55:20 -0800 | [diff] [blame] | 58 | static void die(char *fmt, ...); |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 59 | |
Robert P. J. Day | ca82018 | 2007-02-17 19:10:01 +0100 | [diff] [blame] | 60 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame] | 61 | static Elf_Ehdr ehdr; |
Kees Cook | 5d442e6 | 2013-04-12 13:13:43 -0700 | [diff] [blame] | 62 | |
| 63 | struct relocs { |
| 64 | uint32_t *offset; |
| 65 | unsigned long count; |
| 66 | unsigned long size; |
| 67 | }; |
| 68 | |
| 69 | static struct relocs relocs16; |
| 70 | static struct relocs relocs32; |
Kees Cook | 946166a | 2013-04-12 13:13:44 -0700 | [diff] [blame^] | 71 | static struct relocs relocs64; |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 72 | |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 73 | struct section { |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame] | 74 | Elf_Shdr shdr; |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 75 | struct section *link; |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame] | 76 | Elf_Sym *symtab; |
| 77 | Elf_Rel *reltab; |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 78 | char *strtab; |
| 79 | }; |
| 80 | static struct section *secs; |
| 81 | |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 82 | enum symtype { |
| 83 | S_ABS, |
| 84 | S_REL, |
| 85 | S_SEG, |
| 86 | S_LIN, |
| 87 | S_NSYMTYPES |
| 88 | }; |
| 89 | |
| 90 | static const char * const sym_regex_kernel[S_NSYMTYPES] = { |
Vivek Goyal | 6a044b3 | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 91 | /* |
| 92 | * Following symbols have been audited. There values are constant and do |
| 93 | * not change if bzImage is loaded at a different physical address than |
| 94 | * the address for which it has been compiled. Don't warn user about |
| 95 | * absolute relocations present w.r.t these symbols. |
| 96 | */ |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 97 | [S_ABS] = |
H. Peter Anvin | 873b527 | 2009-12-14 13:55:20 -0800 | [diff] [blame] | 98 | "^(xen_irq_disable_direct_reloc$|" |
| 99 | "xen_save_fl_direct_reloc$|" |
| 100 | "VDSO|" |
Kees Cook | 946166a | 2013-04-12 13:13:44 -0700 | [diff] [blame^] | 101 | #if (ELF_BITS == 64) |
| 102 | "__vvar_page|" |
| 103 | #endif |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 104 | "__crc_)", |
Vivek Goyal | 6a044b3 | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 105 | |
H. Peter Anvin | 873b527 | 2009-12-14 13:55:20 -0800 | [diff] [blame] | 106 | /* |
| 107 | * These symbols are known to be relative, even if the linker marks them |
| 108 | * as absolute (typically defined outside any section in the linker script.) |
| 109 | */ |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 110 | [S_REL] = |
H. Peter Anvin | a3e854d | 2012-05-18 00:24:09 -0700 | [diff] [blame] | 111 | "^(__init_(begin|end)|" |
| 112 | "__x86_cpu_dev_(start|end)|" |
| 113 | "(__parainstructions|__alt_instructions)(|_end)|" |
| 114 | "(__iommu_table|__apicdrivers|__smp_locks)(|_end)|" |
H. Peter Anvin | fd95281 | 2012-05-23 14:02:34 -0700 | [diff] [blame] | 115 | "__(start|end)_pci_.*|" |
| 116 | "__(start|end)_builtin_fw|" |
| 117 | "__(start|stop)___ksymtab(|_gpl|_unused|_unused_gpl|_gpl_future)|" |
| 118 | "__(start|stop)___kcrctab(|_gpl|_unused|_unused_gpl|_gpl_future)|" |
| 119 | "__(start|stop)___param|" |
| 120 | "__(start|stop)___modver|" |
| 121 | "__(start|stop)___bug_table|" |
| 122 | "__tracedata_(start|end)|" |
| 123 | "__(start|stop)_notes|" |
| 124 | "__end_rodata|" |
| 125 | "__initramfs_start|" |
H. Peter Anvin | ea17e74 | 2012-05-24 07:01:38 -0700 | [diff] [blame] | 126 | "(jiffies|jiffies_64)|" |
Kees Cook | 946166a | 2013-04-12 13:13:44 -0700 | [diff] [blame^] | 127 | #if (ELF_BITS == 64) |
| 128 | "__per_cpu_load|" |
| 129 | "init_per_cpu__.*|" |
| 130 | "__end_rodata_hpage_align|" |
| 131 | #endif |
H. Peter Anvin | a3e854d | 2012-05-18 00:24:09 -0700 | [diff] [blame] | 132 | "_end)$" |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | |
| 136 | static const char * const sym_regex_realmode[S_NSYMTYPES] = { |
| 137 | /* |
H. Peter Anvin | 2a6de31 | 2012-05-08 21:22:31 +0300 | [diff] [blame] | 138 | * These symbols are known to be relative, even if the linker marks them |
| 139 | * as absolute (typically defined outside any section in the linker script.) |
| 140 | */ |
| 141 | [S_REL] = |
| 142 | "^pa_", |
| 143 | |
| 144 | /* |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 145 | * These are 16-bit segment symbols when compiling 16-bit code. |
| 146 | */ |
| 147 | [S_SEG] = |
| 148 | "^real_mode_seg$", |
| 149 | |
| 150 | /* |
| 151 | * These are offsets belonging to segments, as opposed to linear addresses, |
| 152 | * when compiling 16-bit code. |
| 153 | */ |
| 154 | [S_LIN] = |
| 155 | "^pa_", |
| 156 | }; |
| 157 | |
| 158 | static const char * const *sym_regex; |
| 159 | |
| 160 | static regex_t sym_regex_c[S_NSYMTYPES]; |
| 161 | static int is_reloc(enum symtype type, const char *sym_name) |
H. Peter Anvin | 873b527 | 2009-12-14 13:55:20 -0800 | [diff] [blame] | 162 | { |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 163 | return sym_regex[type] && |
| 164 | !regexec(&sym_regex_c[type], sym_name, 0, NULL, 0); |
H. Peter Anvin | 873b527 | 2009-12-14 13:55:20 -0800 | [diff] [blame] | 165 | } |
| 166 | |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 167 | static void regex_init(int use_real_mode) |
H. Peter Anvin | 873b527 | 2009-12-14 13:55:20 -0800 | [diff] [blame] | 168 | { |
| 169 | char errbuf[128]; |
| 170 | int err; |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 171 | int i; |
H. Peter Anvin | 873b527 | 2009-12-14 13:55:20 -0800 | [diff] [blame] | 172 | |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 173 | if (use_real_mode) |
| 174 | sym_regex = sym_regex_realmode; |
| 175 | else |
| 176 | sym_regex = sym_regex_kernel; |
| 177 | |
| 178 | for (i = 0; i < S_NSYMTYPES; i++) { |
| 179 | if (!sym_regex[i]) |
| 180 | continue; |
| 181 | |
| 182 | err = regcomp(&sym_regex_c[i], sym_regex[i], |
| 183 | REG_EXTENDED|REG_NOSUB); |
| 184 | |
| 185 | if (err) { |
| 186 | regerror(err, &sym_regex_c[i], errbuf, sizeof errbuf); |
| 187 | die("%s", errbuf); |
| 188 | } |
H. Peter Anvin | 873b527 | 2009-12-14 13:55:20 -0800 | [diff] [blame] | 189 | } |
Vivek Goyal | 6a044b3 | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 190 | } |
| 191 | |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 192 | static void die(char *fmt, ...) |
| 193 | { |
| 194 | va_list ap; |
| 195 | va_start(ap, fmt); |
| 196 | vfprintf(stderr, fmt, ap); |
| 197 | va_end(ap); |
| 198 | exit(1); |
| 199 | } |
| 200 | |
| 201 | static const char *sym_type(unsigned type) |
| 202 | { |
| 203 | static const char *type_name[] = { |
| 204 | #define SYM_TYPE(X) [X] = #X |
| 205 | SYM_TYPE(STT_NOTYPE), |
| 206 | SYM_TYPE(STT_OBJECT), |
| 207 | SYM_TYPE(STT_FUNC), |
| 208 | SYM_TYPE(STT_SECTION), |
| 209 | SYM_TYPE(STT_FILE), |
| 210 | SYM_TYPE(STT_COMMON), |
| 211 | SYM_TYPE(STT_TLS), |
| 212 | #undef SYM_TYPE |
| 213 | }; |
| 214 | const char *name = "unknown sym type name"; |
Robert P. J. Day | ca82018 | 2007-02-17 19:10:01 +0100 | [diff] [blame] | 215 | if (type < ARRAY_SIZE(type_name)) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 216 | name = type_name[type]; |
| 217 | } |
| 218 | return name; |
| 219 | } |
| 220 | |
| 221 | static const char *sym_bind(unsigned bind) |
| 222 | { |
| 223 | static const char *bind_name[] = { |
| 224 | #define SYM_BIND(X) [X] = #X |
| 225 | SYM_BIND(STB_LOCAL), |
| 226 | SYM_BIND(STB_GLOBAL), |
| 227 | SYM_BIND(STB_WEAK), |
| 228 | #undef SYM_BIND |
| 229 | }; |
| 230 | const char *name = "unknown sym bind name"; |
Robert P. J. Day | ca82018 | 2007-02-17 19:10:01 +0100 | [diff] [blame] | 231 | if (bind < ARRAY_SIZE(bind_name)) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 232 | name = bind_name[bind]; |
| 233 | } |
| 234 | return name; |
| 235 | } |
| 236 | |
| 237 | static const char *sym_visibility(unsigned visibility) |
| 238 | { |
| 239 | static const char *visibility_name[] = { |
| 240 | #define SYM_VISIBILITY(X) [X] = #X |
| 241 | SYM_VISIBILITY(STV_DEFAULT), |
| 242 | SYM_VISIBILITY(STV_INTERNAL), |
| 243 | SYM_VISIBILITY(STV_HIDDEN), |
| 244 | SYM_VISIBILITY(STV_PROTECTED), |
| 245 | #undef SYM_VISIBILITY |
| 246 | }; |
| 247 | const char *name = "unknown sym visibility name"; |
Robert P. J. Day | ca82018 | 2007-02-17 19:10:01 +0100 | [diff] [blame] | 248 | if (visibility < ARRAY_SIZE(visibility_name)) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 249 | name = visibility_name[visibility]; |
| 250 | } |
| 251 | return name; |
| 252 | } |
| 253 | |
| 254 | static const char *rel_type(unsigned type) |
| 255 | { |
| 256 | static const char *type_name[] = { |
| 257 | #define REL_TYPE(X) [X] = #X |
Kees Cook | 946166a | 2013-04-12 13:13:44 -0700 | [diff] [blame^] | 258 | #if (ELF_BITS == 64) |
| 259 | REL_TYPE(R_X86_64_NONE), |
| 260 | REL_TYPE(R_X86_64_64), |
| 261 | REL_TYPE(R_X86_64_PC32), |
| 262 | REL_TYPE(R_X86_64_GOT32), |
| 263 | REL_TYPE(R_X86_64_PLT32), |
| 264 | REL_TYPE(R_X86_64_COPY), |
| 265 | REL_TYPE(R_X86_64_GLOB_DAT), |
| 266 | REL_TYPE(R_X86_64_JUMP_SLOT), |
| 267 | REL_TYPE(R_X86_64_RELATIVE), |
| 268 | REL_TYPE(R_X86_64_GOTPCREL), |
| 269 | REL_TYPE(R_X86_64_32), |
| 270 | REL_TYPE(R_X86_64_32S), |
| 271 | REL_TYPE(R_X86_64_16), |
| 272 | REL_TYPE(R_X86_64_PC16), |
| 273 | REL_TYPE(R_X86_64_8), |
| 274 | REL_TYPE(R_X86_64_PC8), |
| 275 | #else |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 276 | REL_TYPE(R_386_NONE), |
| 277 | REL_TYPE(R_386_32), |
| 278 | REL_TYPE(R_386_PC32), |
| 279 | REL_TYPE(R_386_GOT32), |
| 280 | REL_TYPE(R_386_PLT32), |
| 281 | REL_TYPE(R_386_COPY), |
| 282 | REL_TYPE(R_386_GLOB_DAT), |
| 283 | REL_TYPE(R_386_JMP_SLOT), |
| 284 | REL_TYPE(R_386_RELATIVE), |
| 285 | REL_TYPE(R_386_GOTOFF), |
| 286 | REL_TYPE(R_386_GOTPC), |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 287 | REL_TYPE(R_386_8), |
| 288 | REL_TYPE(R_386_PC8), |
| 289 | REL_TYPE(R_386_16), |
| 290 | REL_TYPE(R_386_PC16), |
Kees Cook | 946166a | 2013-04-12 13:13:44 -0700 | [diff] [blame^] | 291 | #endif |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 292 | #undef REL_TYPE |
| 293 | }; |
| 294 | const char *name = "unknown type rel type name"; |
H. Peter Anvin | 873b527 | 2009-12-14 13:55:20 -0800 | [diff] [blame] | 295 | if (type < ARRAY_SIZE(type_name) && type_name[type]) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 296 | name = type_name[type]; |
| 297 | } |
| 298 | return name; |
| 299 | } |
| 300 | |
| 301 | static const char *sec_name(unsigned shndx) |
| 302 | { |
| 303 | const char *sec_strtab; |
| 304 | const char *name; |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 305 | sec_strtab = secs[ehdr.e_shstrndx].strtab; |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 306 | name = "<noname>"; |
| 307 | if (shndx < ehdr.e_shnum) { |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 308 | name = sec_strtab + secs[shndx].shdr.sh_name; |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 309 | } |
| 310 | else if (shndx == SHN_ABS) { |
| 311 | name = "ABSOLUTE"; |
| 312 | } |
| 313 | else if (shndx == SHN_COMMON) { |
| 314 | name = "COMMON"; |
| 315 | } |
| 316 | return name; |
| 317 | } |
| 318 | |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame] | 319 | static const char *sym_name(const char *sym_strtab, Elf_Sym *sym) |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 320 | { |
| 321 | const char *name; |
| 322 | name = "<noname>"; |
| 323 | if (sym->st_name) { |
| 324 | name = sym_strtab + sym->st_name; |
| 325 | } |
| 326 | else { |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 327 | name = sec_name(sym->st_shndx); |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 328 | } |
| 329 | return name; |
| 330 | } |
| 331 | |
Kees Cook | 946166a | 2013-04-12 13:13:44 -0700 | [diff] [blame^] | 332 | static Elf_Sym *sym_lookup(const char *symname) |
| 333 | { |
| 334 | int i; |
| 335 | for (i = 0; i < ehdr.e_shnum; i++) { |
| 336 | struct section *sec = &secs[i]; |
| 337 | long nsyms; |
| 338 | char *strtab; |
| 339 | Elf_Sym *symtab; |
| 340 | Elf_Sym *sym; |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 341 | |
Kees Cook | 946166a | 2013-04-12 13:13:44 -0700 | [diff] [blame^] | 342 | if (sec->shdr.sh_type != SHT_SYMTAB) |
| 343 | continue; |
| 344 | |
| 345 | nsyms = sec->shdr.sh_size/sizeof(Elf_Sym); |
| 346 | symtab = sec->symtab; |
| 347 | strtab = sec->link->strtab; |
| 348 | |
| 349 | for (sym = symtab; --nsyms >= 0; sym++) { |
| 350 | if (!sym->st_name) |
| 351 | continue; |
| 352 | if (strcmp(symname, strtab + sym->st_name) == 0) |
| 353 | return sym; |
| 354 | } |
| 355 | } |
| 356 | return 0; |
| 357 | } |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 358 | |
Linus Torvalds | 13da9e2 | 2010-05-26 08:30:15 -0700 | [diff] [blame] | 359 | #if BYTE_ORDER == LITTLE_ENDIAN |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 360 | #define le16_to_cpu(val) (val) |
| 361 | #define le32_to_cpu(val) (val) |
Kees Cook | 946166a | 2013-04-12 13:13:44 -0700 | [diff] [blame^] | 362 | #define le64_to_cpu(val) (val) |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 363 | #endif |
Linus Torvalds | 13da9e2 | 2010-05-26 08:30:15 -0700 | [diff] [blame] | 364 | #if BYTE_ORDER == BIG_ENDIAN |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 365 | #define le16_to_cpu(val) bswap_16(val) |
| 366 | #define le32_to_cpu(val) bswap_32(val) |
Kees Cook | 946166a | 2013-04-12 13:13:44 -0700 | [diff] [blame^] | 367 | #define le64_to_cpu(val) bswap_64(val) |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 368 | #endif |
| 369 | |
| 370 | static uint16_t elf16_to_cpu(uint16_t val) |
| 371 | { |
| 372 | return le16_to_cpu(val); |
| 373 | } |
| 374 | |
| 375 | static uint32_t elf32_to_cpu(uint32_t val) |
| 376 | { |
| 377 | return le32_to_cpu(val); |
| 378 | } |
| 379 | |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame] | 380 | #define elf_half_to_cpu(x) elf16_to_cpu(x) |
| 381 | #define elf_word_to_cpu(x) elf32_to_cpu(x) |
Kees Cook | 946166a | 2013-04-12 13:13:44 -0700 | [diff] [blame^] | 382 | |
| 383 | #if (ELF_BITS == 64) |
| 384 | static uint64_t elf64_to_cpu(uint64_t val) |
| 385 | { |
| 386 | return le64_to_cpu(val); |
| 387 | } |
| 388 | #define elf_addr_to_cpu(x) elf64_to_cpu(x) |
| 389 | #define elf_off_to_cpu(x) elf64_to_cpu(x) |
| 390 | #define elf_xword_to_cpu(x) elf64_to_cpu(x) |
| 391 | #else |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame] | 392 | #define elf_addr_to_cpu(x) elf32_to_cpu(x) |
| 393 | #define elf_off_to_cpu(x) elf32_to_cpu(x) |
| 394 | #define elf_xword_to_cpu(x) elf32_to_cpu(x) |
Kees Cook | 946166a | 2013-04-12 13:13:44 -0700 | [diff] [blame^] | 395 | #endif |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame] | 396 | |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 397 | static void read_ehdr(FILE *fp) |
| 398 | { |
| 399 | if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1) { |
| 400 | die("Cannot read ELF header: %s\n", |
| 401 | strerror(errno)); |
| 402 | } |
Cyrill Gorcunov | 8bd1796 | 2008-05-03 14:18:03 +0400 | [diff] [blame] | 403 | if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 404 | die("No ELF magic\n"); |
| 405 | } |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame] | 406 | if (ehdr.e_ident[EI_CLASS] != ELF_CLASS) { |
| 407 | die("Not a %d bit executable\n", ELF_BITS); |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 408 | } |
| 409 | if (ehdr.e_ident[EI_DATA] != ELFDATA2LSB) { |
| 410 | die("Not a LSB ELF executable\n"); |
| 411 | } |
| 412 | if (ehdr.e_ident[EI_VERSION] != EV_CURRENT) { |
| 413 | die("Unknown ELF version\n"); |
| 414 | } |
| 415 | /* Convert the fields to native endian */ |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame] | 416 | ehdr.e_type = elf_half_to_cpu(ehdr.e_type); |
| 417 | ehdr.e_machine = elf_half_to_cpu(ehdr.e_machine); |
| 418 | ehdr.e_version = elf_word_to_cpu(ehdr.e_version); |
| 419 | ehdr.e_entry = elf_addr_to_cpu(ehdr.e_entry); |
| 420 | ehdr.e_phoff = elf_off_to_cpu(ehdr.e_phoff); |
| 421 | ehdr.e_shoff = elf_off_to_cpu(ehdr.e_shoff); |
| 422 | ehdr.e_flags = elf_word_to_cpu(ehdr.e_flags); |
| 423 | ehdr.e_ehsize = elf_half_to_cpu(ehdr.e_ehsize); |
| 424 | ehdr.e_phentsize = elf_half_to_cpu(ehdr.e_phentsize); |
| 425 | ehdr.e_phnum = elf_half_to_cpu(ehdr.e_phnum); |
| 426 | ehdr.e_shentsize = elf_half_to_cpu(ehdr.e_shentsize); |
| 427 | ehdr.e_shnum = elf_half_to_cpu(ehdr.e_shnum); |
| 428 | ehdr.e_shstrndx = elf_half_to_cpu(ehdr.e_shstrndx); |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 429 | |
| 430 | if ((ehdr.e_type != ET_EXEC) && (ehdr.e_type != ET_DYN)) { |
| 431 | die("Unsupported ELF header type\n"); |
| 432 | } |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame] | 433 | if (ehdr.e_machine != ELF_MACHINE) { |
| 434 | die("Not for %s\n", ELF_MACHINE_NAME); |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 435 | } |
| 436 | if (ehdr.e_version != EV_CURRENT) { |
| 437 | die("Unknown ELF version\n"); |
| 438 | } |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame] | 439 | if (ehdr.e_ehsize != sizeof(Elf_Ehdr)) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 440 | die("Bad Elf header size\n"); |
| 441 | } |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame] | 442 | if (ehdr.e_phentsize != sizeof(Elf_Phdr)) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 443 | die("Bad program header entry\n"); |
| 444 | } |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame] | 445 | if (ehdr.e_shentsize != sizeof(Elf_Shdr)) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 446 | die("Bad section header entry\n"); |
| 447 | } |
| 448 | if (ehdr.e_shstrndx >= ehdr.e_shnum) { |
| 449 | die("String table index out of bounds\n"); |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | static void read_shdrs(FILE *fp) |
| 454 | { |
| 455 | int i; |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame] | 456 | Elf_Shdr shdr; |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 457 | |
| 458 | secs = calloc(ehdr.e_shnum, sizeof(struct section)); |
| 459 | if (!secs) { |
| 460 | die("Unable to allocate %d section headers\n", |
| 461 | ehdr.e_shnum); |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 462 | } |
| 463 | if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0) { |
| 464 | die("Seek to %d failed: %s\n", |
| 465 | ehdr.e_shoff, strerror(errno)); |
| 466 | } |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 467 | for (i = 0; i < ehdr.e_shnum; i++) { |
| 468 | struct section *sec = &secs[i]; |
| 469 | if (fread(&shdr, sizeof shdr, 1, fp) != 1) |
| 470 | die("Cannot read ELF section headers %d/%d: %s\n", |
| 471 | i, ehdr.e_shnum, strerror(errno)); |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame] | 472 | sec->shdr.sh_name = elf_word_to_cpu(shdr.sh_name); |
| 473 | sec->shdr.sh_type = elf_word_to_cpu(shdr.sh_type); |
| 474 | sec->shdr.sh_flags = elf_xword_to_cpu(shdr.sh_flags); |
| 475 | sec->shdr.sh_addr = elf_addr_to_cpu(shdr.sh_addr); |
| 476 | sec->shdr.sh_offset = elf_off_to_cpu(shdr.sh_offset); |
| 477 | sec->shdr.sh_size = elf_xword_to_cpu(shdr.sh_size); |
| 478 | sec->shdr.sh_link = elf_word_to_cpu(shdr.sh_link); |
| 479 | sec->shdr.sh_info = elf_word_to_cpu(shdr.sh_info); |
| 480 | sec->shdr.sh_addralign = elf_xword_to_cpu(shdr.sh_addralign); |
| 481 | sec->shdr.sh_entsize = elf_xword_to_cpu(shdr.sh_entsize); |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 482 | if (sec->shdr.sh_link < ehdr.e_shnum) |
| 483 | sec->link = &secs[sec->shdr.sh_link]; |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | } |
| 487 | |
| 488 | static void read_strtabs(FILE *fp) |
| 489 | { |
| 490 | int i; |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 491 | for (i = 0; i < ehdr.e_shnum; i++) { |
| 492 | struct section *sec = &secs[i]; |
| 493 | if (sec->shdr.sh_type != SHT_STRTAB) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 494 | continue; |
| 495 | } |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 496 | sec->strtab = malloc(sec->shdr.sh_size); |
| 497 | if (!sec->strtab) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 498 | die("malloc of %d bytes for strtab failed\n", |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 499 | sec->shdr.sh_size); |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 500 | } |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 501 | if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 502 | die("Seek to %d failed: %s\n", |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 503 | sec->shdr.sh_offset, strerror(errno)); |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 504 | } |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 505 | if (fread(sec->strtab, 1, sec->shdr.sh_size, fp) |
| 506 | != sec->shdr.sh_size) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 507 | die("Cannot read symbol table: %s\n", |
| 508 | strerror(errno)); |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | static void read_symtabs(FILE *fp) |
| 514 | { |
| 515 | int i,j; |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 516 | for (i = 0; i < ehdr.e_shnum; i++) { |
| 517 | struct section *sec = &secs[i]; |
| 518 | if (sec->shdr.sh_type != SHT_SYMTAB) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 519 | continue; |
| 520 | } |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 521 | sec->symtab = malloc(sec->shdr.sh_size); |
| 522 | if (!sec->symtab) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 523 | die("malloc of %d bytes for symtab failed\n", |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 524 | sec->shdr.sh_size); |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 525 | } |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 526 | if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 527 | die("Seek to %d failed: %s\n", |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 528 | sec->shdr.sh_offset, strerror(errno)); |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 529 | } |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 530 | if (fread(sec->symtab, 1, sec->shdr.sh_size, fp) |
| 531 | != sec->shdr.sh_size) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 532 | die("Cannot read symbol table: %s\n", |
| 533 | strerror(errno)); |
| 534 | } |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame] | 535 | for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Sym); j++) { |
| 536 | Elf_Sym *sym = &sec->symtab[j]; |
| 537 | sym->st_name = elf_word_to_cpu(sym->st_name); |
| 538 | sym->st_value = elf_addr_to_cpu(sym->st_value); |
| 539 | sym->st_size = elf_xword_to_cpu(sym->st_size); |
| 540 | sym->st_shndx = elf_half_to_cpu(sym->st_shndx); |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | |
| 546 | static void read_relocs(FILE *fp) |
| 547 | { |
| 548 | int i,j; |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 549 | for (i = 0; i < ehdr.e_shnum; i++) { |
| 550 | struct section *sec = &secs[i]; |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame] | 551 | if (sec->shdr.sh_type != SHT_REL_TYPE) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 552 | continue; |
| 553 | } |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 554 | sec->reltab = malloc(sec->shdr.sh_size); |
| 555 | if (!sec->reltab) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 556 | die("malloc of %d bytes for relocs failed\n", |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 557 | sec->shdr.sh_size); |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 558 | } |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 559 | if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 560 | die("Seek to %d failed: %s\n", |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 561 | sec->shdr.sh_offset, strerror(errno)); |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 562 | } |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 563 | if (fread(sec->reltab, 1, sec->shdr.sh_size, fp) |
| 564 | != sec->shdr.sh_size) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 565 | die("Cannot read symbol table: %s\n", |
| 566 | strerror(errno)); |
| 567 | } |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame] | 568 | for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Rel); j++) { |
| 569 | Elf_Rel *rel = &sec->reltab[j]; |
| 570 | rel->r_offset = elf_addr_to_cpu(rel->r_offset); |
| 571 | rel->r_info = elf_xword_to_cpu(rel->r_info); |
Kees Cook | 946166a | 2013-04-12 13:13:44 -0700 | [diff] [blame^] | 572 | #if (SHT_REL_TYPE == SHT_RELA) |
| 573 | rel->r_addend = elf_xword_to_cpu(rel->r_addend); |
| 574 | #endif |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 575 | } |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | |
| 580 | static void print_absolute_symbols(void) |
| 581 | { |
| 582 | int i; |
Kees Cook | 946166a | 2013-04-12 13:13:44 -0700 | [diff] [blame^] | 583 | const char *format; |
| 584 | |
| 585 | if (ehdr.e_ident[EI_CLASS] == ELFCLASS64) |
| 586 | format = "%5d %016"PRIx64" %5"PRId64" %10s %10s %12s %s\n"; |
| 587 | else |
| 588 | format = "%5d %08"PRIx32" %5"PRId32" %10s %10s %12s %s\n"; |
| 589 | |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 590 | printf("Absolute symbols\n"); |
| 591 | printf(" Num: Value Size Type Bind Visibility Name\n"); |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 592 | for (i = 0; i < ehdr.e_shnum; i++) { |
| 593 | struct section *sec = &secs[i]; |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 594 | char *sym_strtab; |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 595 | int j; |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 596 | |
| 597 | if (sec->shdr.sh_type != SHT_SYMTAB) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 598 | continue; |
| 599 | } |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 600 | sym_strtab = sec->link->strtab; |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame] | 601 | for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Sym); j++) { |
| 602 | Elf_Sym *sym; |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 603 | const char *name; |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 604 | sym = &sec->symtab[j]; |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 605 | name = sym_name(sym_strtab, sym); |
| 606 | if (sym->st_shndx != SHN_ABS) { |
| 607 | continue; |
| 608 | } |
Kees Cook | 946166a | 2013-04-12 13:13:44 -0700 | [diff] [blame^] | 609 | printf(format, |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 610 | j, sym->st_value, sym->st_size, |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame] | 611 | sym_type(ELF_ST_TYPE(sym->st_info)), |
| 612 | sym_bind(ELF_ST_BIND(sym->st_info)), |
| 613 | sym_visibility(ELF_ST_VISIBILITY(sym->st_other)), |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 614 | name); |
| 615 | } |
| 616 | } |
| 617 | printf("\n"); |
| 618 | } |
| 619 | |
| 620 | static void print_absolute_relocs(void) |
| 621 | { |
Vivek Goyal | 6a044b3 | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 622 | int i, printed = 0; |
Kees Cook | 946166a | 2013-04-12 13:13:44 -0700 | [diff] [blame^] | 623 | const char *format; |
| 624 | |
| 625 | if (ehdr.e_ident[EI_CLASS] == ELFCLASS64) |
| 626 | format = "%016"PRIx64" %016"PRIx64" %10s %016"PRIx64" %s\n"; |
| 627 | else |
| 628 | format = "%08"PRIx32" %08"PRIx32" %10s %08"PRIx32" %s\n"; |
Vivek Goyal | 6a044b3 | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 629 | |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 630 | for (i = 0; i < ehdr.e_shnum; i++) { |
| 631 | struct section *sec = &secs[i]; |
| 632 | struct section *sec_applies, *sec_symtab; |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 633 | char *sym_strtab; |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame] | 634 | Elf_Sym *sh_symtab; |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 635 | int j; |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame] | 636 | if (sec->shdr.sh_type != SHT_REL_TYPE) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 637 | continue; |
| 638 | } |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 639 | sec_symtab = sec->link; |
| 640 | sec_applies = &secs[sec->shdr.sh_info]; |
| 641 | if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 642 | continue; |
| 643 | } |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 644 | sh_symtab = sec_symtab->symtab; |
| 645 | sym_strtab = sec_symtab->link->strtab; |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame] | 646 | for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Rel); j++) { |
| 647 | Elf_Rel *rel; |
| 648 | Elf_Sym *sym; |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 649 | const char *name; |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 650 | rel = &sec->reltab[j]; |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame] | 651 | sym = &sh_symtab[ELF_R_SYM(rel->r_info)]; |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 652 | name = sym_name(sym_strtab, sym); |
| 653 | if (sym->st_shndx != SHN_ABS) { |
| 654 | continue; |
| 655 | } |
Vivek Goyal | 6a044b3 | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 656 | |
| 657 | /* Absolute symbols are not relocated if bzImage is |
| 658 | * loaded at a non-compiled address. Display a warning |
| 659 | * to user at compile time about the absolute |
| 660 | * relocations present. |
| 661 | * |
| 662 | * User need to audit the code to make sure |
| 663 | * some symbols which should have been section |
| 664 | * relative have not become absolute because of some |
| 665 | * linker optimization or wrong programming usage. |
| 666 | * |
| 667 | * Before warning check if this absolute symbol |
| 668 | * relocation is harmless. |
| 669 | */ |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 670 | if (is_reloc(S_ABS, name) || is_reloc(S_REL, name)) |
Vivek Goyal | 6a044b3 | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 671 | continue; |
| 672 | |
| 673 | if (!printed) { |
| 674 | printf("WARNING: Absolute relocations" |
| 675 | " present\n"); |
| 676 | printf("Offset Info Type Sym.Value " |
| 677 | "Sym.Name\n"); |
| 678 | printed = 1; |
| 679 | } |
| 680 | |
Kees Cook | 946166a | 2013-04-12 13:13:44 -0700 | [diff] [blame^] | 681 | printf(format, |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 682 | rel->r_offset, |
| 683 | rel->r_info, |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame] | 684 | rel_type(ELF_R_TYPE(rel->r_info)), |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 685 | sym->st_value, |
| 686 | name); |
| 687 | } |
| 688 | } |
Vivek Goyal | 6a044b3 | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 689 | |
| 690 | if (printed) |
| 691 | printf("\n"); |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 692 | } |
| 693 | |
Kees Cook | 5d442e6 | 2013-04-12 13:13:43 -0700 | [diff] [blame] | 694 | static void add_reloc(struct relocs *r, uint32_t offset) |
| 695 | { |
| 696 | if (r->count == r->size) { |
| 697 | unsigned long newsize = r->size + 50000; |
| 698 | void *mem = realloc(r->offset, newsize * sizeof(r->offset[0])); |
| 699 | |
| 700 | if (!mem) |
| 701 | die("realloc of %ld entries for relocs failed\n", |
| 702 | newsize); |
| 703 | r->offset = mem; |
| 704 | r->size = newsize; |
| 705 | } |
| 706 | r->offset[r->count++] = offset; |
| 707 | } |
| 708 | |
| 709 | static void walk_relocs(int (*process)(struct section *sec, Elf_Rel *rel, |
| 710 | Elf_Sym *sym, const char *symname)) |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 711 | { |
| 712 | int i; |
| 713 | /* Walk through the relocations */ |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 714 | for (i = 0; i < ehdr.e_shnum; i++) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 715 | char *sym_strtab; |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame] | 716 | Elf_Sym *sh_symtab; |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 717 | struct section *sec_applies, *sec_symtab; |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 718 | int j; |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 719 | struct section *sec = &secs[i]; |
| 720 | |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame] | 721 | if (sec->shdr.sh_type != SHT_REL_TYPE) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 722 | continue; |
| 723 | } |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 724 | sec_symtab = sec->link; |
| 725 | sec_applies = &secs[sec->shdr.sh_info]; |
| 726 | if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 727 | continue; |
| 728 | } |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 729 | sh_symtab = sec_symtab->symtab; |
H. Peter Anvin | cc65f1e | 2008-10-03 13:00:56 -0700 | [diff] [blame] | 730 | sym_strtab = sec_symtab->link->strtab; |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame] | 731 | for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Rel); j++) { |
Kees Cook | 5d442e6 | 2013-04-12 13:13:43 -0700 | [diff] [blame] | 732 | Elf_Rel *rel = &sec->reltab[j]; |
| 733 | Elf_Sym *sym = &sh_symtab[ELF_R_SYM(rel->r_info)]; |
| 734 | const char *symname = sym_name(sym_strtab, sym); |
H. Peter Anvin | 24ab82b | 2012-05-18 09:52:01 -0700 | [diff] [blame] | 735 | |
Kees Cook | 5d442e6 | 2013-04-12 13:13:43 -0700 | [diff] [blame] | 736 | process(sec, rel, sym, symname); |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 737 | } |
| 738 | } |
| 739 | } |
| 740 | |
Kees Cook | 946166a | 2013-04-12 13:13:44 -0700 | [diff] [blame^] | 741 | /* |
| 742 | * The .data..percpu section is a special case for x86_64 SMP kernels. |
| 743 | * It is used to initialize the actual per_cpu areas and to provide |
| 744 | * definitions for the per_cpu variables that correspond to their offsets |
| 745 | * within the percpu area. Since the values of all of the symbols need |
| 746 | * to be offsets from the start of the per_cpu area the virtual address |
| 747 | * (sh_addr) of .data..percpu is 0 in SMP kernels. |
| 748 | * |
| 749 | * This means that: |
| 750 | * |
| 751 | * Relocations that reference symbols in the per_cpu area do not |
| 752 | * need further relocation (since the value is an offset relative |
| 753 | * to the start of the per_cpu area that does not change). |
| 754 | * |
| 755 | * Relocations that apply to the per_cpu area need to have their |
| 756 | * offset adjusted by by the value of __per_cpu_load to make them |
| 757 | * point to the correct place in the loaded image (because the |
| 758 | * virtual address of .data..percpu is 0). |
| 759 | * |
| 760 | * For non SMP kernels .data..percpu is linked as part of the normal |
| 761 | * kernel data and does not require special treatment. |
| 762 | * |
| 763 | */ |
| 764 | static int per_cpu_shndx = -1; |
| 765 | Elf_Addr per_cpu_load_addr; |
| 766 | |
| 767 | static void percpu_init(void) |
| 768 | { |
| 769 | int i; |
| 770 | for (i = 0; i < ehdr.e_shnum; i++) { |
| 771 | ElfW(Sym) *sym; |
| 772 | if (strcmp(sec_name(i), ".data..percpu")) |
| 773 | continue; |
| 774 | |
| 775 | if (secs[i].shdr.sh_addr != 0) /* non SMP kernel */ |
| 776 | return; |
| 777 | |
| 778 | sym = sym_lookup("__per_cpu_load"); |
| 779 | if (!sym) |
| 780 | die("can't find __per_cpu_load\n"); |
| 781 | |
| 782 | per_cpu_shndx = i; |
| 783 | per_cpu_load_addr = sym->st_value; |
| 784 | return; |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | /* |
| 789 | * Check to see if a symbol lies in the .data..percpu section. |
| 790 | * For some as yet not understood reason the "__init_begin" |
| 791 | * symbol which immediately preceeds the .data..percpu section |
| 792 | * also shows up as it it were part of it so we do an explict |
| 793 | * check for that symbol name and ignore it. |
| 794 | */ |
| 795 | static int is_percpu_sym(ElfW(Sym) *sym, const char *symname) |
| 796 | { |
| 797 | return (sym->st_shndx == per_cpu_shndx) && |
| 798 | strcmp(symname, "__init_begin"); |
| 799 | } |
| 800 | |
| 801 | static int do_reloc64(struct section *sec, Elf_Rel *rel, ElfW(Sym) *sym, |
| 802 | const char *symname) |
| 803 | { |
| 804 | unsigned r_type = ELF64_R_TYPE(rel->r_info); |
| 805 | ElfW(Addr) offset = rel->r_offset; |
| 806 | int shn_abs = (sym->st_shndx == SHN_ABS) && !is_reloc(S_REL, symname); |
| 807 | |
| 808 | if (sym->st_shndx == SHN_UNDEF) |
| 809 | return 0; |
| 810 | |
| 811 | /* |
| 812 | * Adjust the offset if this reloc applies to the percpu section. |
| 813 | */ |
| 814 | if (sec->shdr.sh_info == per_cpu_shndx) |
| 815 | offset += per_cpu_load_addr; |
| 816 | |
| 817 | switch (r_type) { |
| 818 | case R_X86_64_NONE: |
| 819 | case R_X86_64_PC32: |
| 820 | /* |
| 821 | * NONE can be ignored and PC relative relocations don't |
| 822 | * need to be adjusted. |
| 823 | */ |
| 824 | break; |
| 825 | |
| 826 | case R_X86_64_32: |
| 827 | case R_X86_64_32S: |
| 828 | case R_X86_64_64: |
| 829 | /* |
| 830 | * References to the percpu area don't need to be adjusted. |
| 831 | */ |
| 832 | if (is_percpu_sym(sym, symname)) |
| 833 | break; |
| 834 | |
| 835 | if (shn_abs) { |
| 836 | /* |
| 837 | * Whitelisted absolute symbols do not require |
| 838 | * relocation. |
| 839 | */ |
| 840 | if (is_reloc(S_ABS, symname)) |
| 841 | break; |
| 842 | |
| 843 | die("Invalid absolute %s relocation: %s\n", |
| 844 | rel_type(r_type), symname); |
| 845 | break; |
| 846 | } |
| 847 | |
| 848 | /* |
| 849 | * Relocation offsets for 64 bit kernels are output |
| 850 | * as 32 bits and sign extended back to 64 bits when |
| 851 | * the relocations are processed. |
| 852 | * Make sure that the offset will fit. |
| 853 | */ |
| 854 | if ((int32_t)offset != (int64_t)offset) |
| 855 | die("Relocation offset doesn't fit in 32 bits\n"); |
| 856 | |
| 857 | if (r_type == R_X86_64_64) |
| 858 | add_reloc(&relocs64, offset); |
| 859 | else |
| 860 | add_reloc(&relocs32, offset); |
| 861 | break; |
| 862 | |
| 863 | default: |
| 864 | die("Unsupported relocation type: %s (%d)\n", |
| 865 | rel_type(r_type), r_type); |
| 866 | break; |
| 867 | } |
| 868 | |
| 869 | return 0; |
| 870 | } |
| 871 | |
| 872 | |
| 873 | static int do_reloc32(struct section *sec, Elf_Rel *rel, Elf_Sym *sym, |
| 874 | const char *symname) |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 875 | { |
Kees Cook | 5d442e6 | 2013-04-12 13:13:43 -0700 | [diff] [blame] | 876 | unsigned r_type = ELF32_R_TYPE(rel->r_info); |
| 877 | int shn_abs = (sym->st_shndx == SHN_ABS) && !is_reloc(S_REL, symname); |
| 878 | |
| 879 | switch (r_type) { |
| 880 | case R_386_NONE: |
| 881 | case R_386_PC32: |
| 882 | case R_386_PC16: |
| 883 | case R_386_PC8: |
| 884 | /* |
| 885 | * NONE can be ignored and PC relative relocations don't |
| 886 | * need to be adjusted. |
| 887 | */ |
| 888 | break; |
| 889 | |
| 890 | case R_386_32: |
| 891 | if (shn_abs) { |
| 892 | /* |
| 893 | * Whitelisted absolute symbols do not require |
| 894 | * relocation. |
| 895 | */ |
| 896 | if (is_reloc(S_ABS, symname)) |
| 897 | break; |
| 898 | |
| 899 | die("Invalid absolute %s relocation: %s\n", |
| 900 | rel_type(r_type), symname); |
| 901 | break; |
| 902 | } |
| 903 | |
| 904 | add_reloc(&relocs32, rel->r_offset); |
| 905 | break; |
| 906 | |
| 907 | default: |
| 908 | die("Unsupported relocation type: %s (%d)\n", |
| 909 | rel_type(r_type), r_type); |
| 910 | break; |
| 911 | } |
| 912 | |
| 913 | return 0; |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 914 | } |
| 915 | |
Kees Cook | 5d442e6 | 2013-04-12 13:13:43 -0700 | [diff] [blame] | 916 | static int do_reloc_real(struct section *sec, Elf_Rel *rel, Elf_Sym *sym, |
| 917 | const char *symname) |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 918 | { |
Kees Cook | 5d442e6 | 2013-04-12 13:13:43 -0700 | [diff] [blame] | 919 | unsigned r_type = ELF32_R_TYPE(rel->r_info); |
| 920 | int shn_abs = (sym->st_shndx == SHN_ABS) && !is_reloc(S_REL, symname); |
| 921 | |
| 922 | switch (r_type) { |
| 923 | case R_386_NONE: |
| 924 | case R_386_PC32: |
| 925 | case R_386_PC16: |
| 926 | case R_386_PC8: |
| 927 | /* |
| 928 | * NONE can be ignored and PC relative relocations don't |
| 929 | * need to be adjusted. |
| 930 | */ |
| 931 | break; |
| 932 | |
| 933 | case R_386_16: |
| 934 | if (shn_abs) { |
| 935 | /* |
| 936 | * Whitelisted absolute symbols do not require |
| 937 | * relocation. |
| 938 | */ |
| 939 | if (is_reloc(S_ABS, symname)) |
| 940 | break; |
| 941 | |
| 942 | if (is_reloc(S_SEG, symname)) { |
| 943 | add_reloc(&relocs16, rel->r_offset); |
| 944 | break; |
| 945 | } |
| 946 | } else { |
| 947 | if (!is_reloc(S_LIN, symname)) |
| 948 | break; |
| 949 | } |
| 950 | die("Invalid %s %s relocation: %s\n", |
| 951 | shn_abs ? "absolute" : "relative", |
| 952 | rel_type(r_type), symname); |
| 953 | break; |
| 954 | |
| 955 | case R_386_32: |
| 956 | if (shn_abs) { |
| 957 | /* |
| 958 | * Whitelisted absolute symbols do not require |
| 959 | * relocation. |
| 960 | */ |
| 961 | if (is_reloc(S_ABS, symname)) |
| 962 | break; |
| 963 | |
| 964 | if (is_reloc(S_REL, symname)) { |
| 965 | add_reloc(&relocs32, rel->r_offset); |
| 966 | break; |
| 967 | } |
| 968 | } else { |
| 969 | if (is_reloc(S_LIN, symname)) |
| 970 | add_reloc(&relocs32, rel->r_offset); |
| 971 | break; |
| 972 | } |
| 973 | die("Invalid %s %s relocation: %s\n", |
| 974 | shn_abs ? "absolute" : "relative", |
| 975 | rel_type(r_type), symname); |
| 976 | break; |
| 977 | |
| 978 | default: |
| 979 | die("Unsupported relocation type: %s (%d)\n", |
| 980 | rel_type(r_type), r_type); |
| 981 | break; |
| 982 | } |
| 983 | |
| 984 | return 0; |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 985 | } |
| 986 | |
| 987 | static int cmp_relocs(const void *va, const void *vb) |
| 988 | { |
Kees Cook | 5d442e6 | 2013-04-12 13:13:43 -0700 | [diff] [blame] | 989 | const uint32_t *a, *b; |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 990 | a = va; b = vb; |
| 991 | return (*a == *b)? 0 : (*a > *b)? 1 : -1; |
| 992 | } |
| 993 | |
Kees Cook | 5d442e6 | 2013-04-12 13:13:43 -0700 | [diff] [blame] | 994 | static void sort_relocs(struct relocs *r) |
| 995 | { |
| 996 | qsort(r->offset, r->count, sizeof(r->offset[0]), cmp_relocs); |
| 997 | } |
| 998 | |
| 999 | static int write32(uint32_t v, FILE *f) |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 1000 | { |
| 1001 | unsigned char buf[4]; |
| 1002 | |
| 1003 | put_unaligned_le32(v, buf); |
| 1004 | return fwrite(buf, 1, 4, f) == 4 ? 0 : -1; |
| 1005 | } |
| 1006 | |
Kees Cook | 5d442e6 | 2013-04-12 13:13:43 -0700 | [diff] [blame] | 1007 | static int write32_as_text(uint32_t v, FILE *f) |
| 1008 | { |
| 1009 | return fprintf(f, "\t.long 0x%08"PRIx32"\n", v) > 0 ? 0 : -1; |
| 1010 | } |
| 1011 | |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 1012 | static void emit_relocs(int as_text, int use_real_mode) |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 1013 | { |
| 1014 | int i; |
Kees Cook | 5d442e6 | 2013-04-12 13:13:43 -0700 | [diff] [blame] | 1015 | int (*write_reloc)(uint32_t, FILE *) = write32; |
Kees Cook | 946166a | 2013-04-12 13:13:44 -0700 | [diff] [blame^] | 1016 | int (*do_reloc)(struct section *sec, Elf_Rel *rel, Elf_Sym *sym, |
| 1017 | const char *symname); |
| 1018 | |
| 1019 | if (ehdr.e_ident[EI_CLASS] == ELFCLASS64) |
| 1020 | do_reloc = do_reloc64; |
| 1021 | else if (!use_real_mode) |
| 1022 | do_reloc = do_reloc32; |
| 1023 | else |
| 1024 | do_reloc = do_reloc_real; |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 1025 | |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 1026 | /* Collect up the relocations */ |
Kees Cook | 946166a | 2013-04-12 13:13:44 -0700 | [diff] [blame^] | 1027 | walk_relocs(do_reloc); |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 1028 | |
Kees Cook | 5d442e6 | 2013-04-12 13:13:43 -0700 | [diff] [blame] | 1029 | if (relocs16.count && !use_real_mode) |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 1030 | die("Segment relocations found but --realmode not specified\n"); |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 1031 | |
| 1032 | /* Order the relocations for more efficient processing */ |
Kees Cook | 5d442e6 | 2013-04-12 13:13:43 -0700 | [diff] [blame] | 1033 | sort_relocs(&relocs16); |
| 1034 | sort_relocs(&relocs32); |
Kees Cook | 946166a | 2013-04-12 13:13:44 -0700 | [diff] [blame^] | 1035 | sort_relocs(&relocs64); |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 1036 | |
| 1037 | /* Print the relocations */ |
| 1038 | if (as_text) { |
| 1039 | /* Print the relocations in a form suitable that |
| 1040 | * gas will like. |
| 1041 | */ |
| 1042 | printf(".section \".data.reloc\",\"a\"\n"); |
| 1043 | printf(".balign 4\n"); |
Kees Cook | 5d442e6 | 2013-04-12 13:13:43 -0700 | [diff] [blame] | 1044 | write_reloc = write32_as_text; |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 1045 | } |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 1046 | |
Kees Cook | 5d442e6 | 2013-04-12 13:13:43 -0700 | [diff] [blame] | 1047 | if (use_real_mode) { |
| 1048 | write_reloc(relocs16.count, stdout); |
| 1049 | for (i = 0; i < relocs16.count; i++) |
| 1050 | write_reloc(relocs16.offset[i], stdout); |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 1051 | |
Kees Cook | 5d442e6 | 2013-04-12 13:13:43 -0700 | [diff] [blame] | 1052 | write_reloc(relocs32.count, stdout); |
| 1053 | for (i = 0; i < relocs32.count; i++) |
| 1054 | write_reloc(relocs32.offset[i], stdout); |
| 1055 | } else { |
Kees Cook | 946166a | 2013-04-12 13:13:44 -0700 | [diff] [blame^] | 1056 | if (ehdr.e_ident[EI_CLASS] == ELFCLASS64) { |
| 1057 | /* Print a stop */ |
| 1058 | write_reloc(0, stdout); |
| 1059 | |
| 1060 | /* Now print each relocation */ |
| 1061 | for (i = 0; i < relocs64.count; i++) |
| 1062 | write_reloc(relocs64.offset[i], stdout); |
| 1063 | } |
| 1064 | |
Kees Cook | 5d442e6 | 2013-04-12 13:13:43 -0700 | [diff] [blame] | 1065 | /* Print a stop */ |
| 1066 | write_reloc(0, stdout); |
| 1067 | |
| 1068 | /* Now print each relocation */ |
| 1069 | for (i = 0; i < relocs32.count; i++) |
| 1070 | write_reloc(relocs32.offset[i], stdout); |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 1071 | } |
| 1072 | } |
| 1073 | |
| 1074 | static void usage(void) |
| 1075 | { |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 1076 | die("relocs [--abs-syms|--abs-relocs|--text|--realmode] vmlinux\n"); |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | int main(int argc, char **argv) |
| 1080 | { |
Vivek Goyal | 6a044b3 | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 1081 | int show_absolute_syms, show_absolute_relocs; |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 1082 | int as_text, use_real_mode; |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 1083 | const char *fname; |
| 1084 | FILE *fp; |
| 1085 | int i; |
| 1086 | |
Vivek Goyal | 6a044b3 | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 1087 | show_absolute_syms = 0; |
| 1088 | show_absolute_relocs = 0; |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 1089 | as_text = 0; |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 1090 | use_real_mode = 0; |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 1091 | fname = NULL; |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 1092 | for (i = 1; i < argc; i++) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 1093 | char *arg = argv[i]; |
| 1094 | if (*arg == '-') { |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 1095 | if (strcmp(arg, "--abs-syms") == 0) { |
Vivek Goyal | 6a044b3 | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 1096 | show_absolute_syms = 1; |
| 1097 | continue; |
| 1098 | } |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 1099 | if (strcmp(arg, "--abs-relocs") == 0) { |
Vivek Goyal | 6a044b3 | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 1100 | show_absolute_relocs = 1; |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 1101 | continue; |
| 1102 | } |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 1103 | if (strcmp(arg, "--text") == 0) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 1104 | as_text = 1; |
| 1105 | continue; |
| 1106 | } |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 1107 | if (strcmp(arg, "--realmode") == 0) { |
| 1108 | use_real_mode = 1; |
| 1109 | continue; |
| 1110 | } |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 1111 | } |
| 1112 | else if (!fname) { |
| 1113 | fname = arg; |
| 1114 | continue; |
| 1115 | } |
| 1116 | usage(); |
| 1117 | } |
| 1118 | if (!fname) { |
| 1119 | usage(); |
| 1120 | } |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 1121 | regex_init(use_real_mode); |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 1122 | fp = fopen(fname, "r"); |
| 1123 | if (!fp) { |
| 1124 | die("Cannot open %s: %s\n", |
| 1125 | fname, strerror(errno)); |
| 1126 | } |
| 1127 | read_ehdr(fp); |
| 1128 | read_shdrs(fp); |
| 1129 | read_strtabs(fp); |
| 1130 | read_symtabs(fp); |
| 1131 | read_relocs(fp); |
Kees Cook | 946166a | 2013-04-12 13:13:44 -0700 | [diff] [blame^] | 1132 | if (ehdr.e_ident[EI_CLASS] == ELFCLASS64) |
| 1133 | percpu_init(); |
Vivek Goyal | 6a044b3 | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 1134 | if (show_absolute_syms) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 1135 | print_absolute_symbols(); |
Cong Ding | 65315d4 | 2013-01-14 17:13:35 +0000 | [diff] [blame] | 1136 | goto out; |
Vivek Goyal | 6a044b3 | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 1137 | } |
| 1138 | if (show_absolute_relocs) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 1139 | print_absolute_relocs(); |
Cong Ding | 65315d4 | 2013-01-14 17:13:35 +0000 | [diff] [blame] | 1140 | goto out; |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 1141 | } |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 1142 | emit_relocs(as_text, use_real_mode); |
Cong Ding | 65315d4 | 2013-01-14 17:13:35 +0000 | [diff] [blame] | 1143 | out: |
| 1144 | fclose(fp); |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 1145 | return 0; |
| 1146 | } |