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> |
| 5 | #include <string.h> |
| 6 | #include <errno.h> |
| 7 | #include <unistd.h> |
| 8 | #include <elf.h> |
| 9 | #include <byteswap.h> |
| 10 | #define USE_BSD |
| 11 | #include <endian.h> |
H. Peter Anvin | 873b527 | 2009-12-14 13:55:20 -0800 | [diff] [blame] | 12 | #include <regex.h> |
Matt Fleming | 55f9709 | 2012-02-28 13:37:21 +0000 | [diff] [blame] | 13 | #include <tools/le_byteshift.h> |
H. Peter Anvin | 873b527 | 2009-12-14 13:55:20 -0800 | [diff] [blame] | 14 | |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame^] | 15 | #define ElfW(type) _ElfW(ELF_BITS, type) |
| 16 | #define _ElfW(bits, type) __ElfW(bits, type) |
| 17 | #define __ElfW(bits, type) Elf##bits##_##type |
| 18 | |
| 19 | #define ELF_BITS 32 |
| 20 | #define ELF_MACHINE EM_386 |
| 21 | #define ELF_MACHINE_NAME "i386" |
| 22 | #define SHT_REL_TYPE SHT_REL |
| 23 | |
| 24 | #define ELF_CLASS ELFCLASS32 |
| 25 | #define ELF_R_SYM(val) ELF32_R_SYM(val) |
| 26 | #define ELF_R_TYPE(val) ELF32_R_TYPE(val) |
| 27 | #define ELF_ST_TYPE(o) ELF32_ST_TYPE(o) |
| 28 | #define ELF_ST_BIND(o) ELF32_ST_BIND(o) |
| 29 | #define ELF_ST_VISIBILITY(o) ELF32_ST_VISIBILITY(o) |
| 30 | |
| 31 | #define Elf_Rel ElfW(Rel) |
| 32 | #define Elf_Ehdr ElfW(Ehdr) |
| 33 | #define Elf_Phdr ElfW(Phdr) |
| 34 | #define Elf_Shdr ElfW(Shdr) |
| 35 | #define Elf_Sym ElfW(Sym) |
| 36 | |
H. Peter Anvin | 873b527 | 2009-12-14 13:55:20 -0800 | [diff] [blame] | 37 | static void die(char *fmt, ...); |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 38 | |
Robert P. J. Day | ca82018 | 2007-02-17 19:10:01 +0100 | [diff] [blame] | 39 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame^] | 40 | static Elf_Ehdr ehdr; |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 41 | static unsigned long reloc_count, reloc_idx; |
| 42 | static unsigned long *relocs; |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 43 | static unsigned long reloc16_count, reloc16_idx; |
| 44 | static unsigned long *relocs16; |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 45 | |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 46 | struct section { |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame^] | 47 | Elf_Shdr shdr; |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 48 | struct section *link; |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame^] | 49 | Elf_Sym *symtab; |
| 50 | Elf_Rel *reltab; |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 51 | char *strtab; |
| 52 | }; |
| 53 | static struct section *secs; |
| 54 | |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 55 | enum symtype { |
| 56 | S_ABS, |
| 57 | S_REL, |
| 58 | S_SEG, |
| 59 | S_LIN, |
| 60 | S_NSYMTYPES |
| 61 | }; |
| 62 | |
| 63 | static const char * const sym_regex_kernel[S_NSYMTYPES] = { |
Vivek Goyal | 6a044b3 | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 64 | /* |
| 65 | * Following symbols have been audited. There values are constant and do |
| 66 | * not change if bzImage is loaded at a different physical address than |
| 67 | * the address for which it has been compiled. Don't warn user about |
| 68 | * absolute relocations present w.r.t these symbols. |
| 69 | */ |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 70 | [S_ABS] = |
H. Peter Anvin | 873b527 | 2009-12-14 13:55:20 -0800 | [diff] [blame] | 71 | "^(xen_irq_disable_direct_reloc$|" |
| 72 | "xen_save_fl_direct_reloc$|" |
| 73 | "VDSO|" |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 74 | "__crc_)", |
Vivek Goyal | 6a044b3 | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 75 | |
H. Peter Anvin | 873b527 | 2009-12-14 13:55:20 -0800 | [diff] [blame] | 76 | /* |
| 77 | * These symbols are known to be relative, even if the linker marks them |
| 78 | * as absolute (typically defined outside any section in the linker script.) |
| 79 | */ |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 80 | [S_REL] = |
H. Peter Anvin | a3e854d | 2012-05-18 00:24:09 -0700 | [diff] [blame] | 81 | "^(__init_(begin|end)|" |
| 82 | "__x86_cpu_dev_(start|end)|" |
| 83 | "(__parainstructions|__alt_instructions)(|_end)|" |
| 84 | "(__iommu_table|__apicdrivers|__smp_locks)(|_end)|" |
H. Peter Anvin | fd95281 | 2012-05-23 14:02:34 -0700 | [diff] [blame] | 85 | "__(start|end)_pci_.*|" |
| 86 | "__(start|end)_builtin_fw|" |
| 87 | "__(start|stop)___ksymtab(|_gpl|_unused|_unused_gpl|_gpl_future)|" |
| 88 | "__(start|stop)___kcrctab(|_gpl|_unused|_unused_gpl|_gpl_future)|" |
| 89 | "__(start|stop)___param|" |
| 90 | "__(start|stop)___modver|" |
| 91 | "__(start|stop)___bug_table|" |
| 92 | "__tracedata_(start|end)|" |
| 93 | "__(start|stop)_notes|" |
| 94 | "__end_rodata|" |
| 95 | "__initramfs_start|" |
H. Peter Anvin | ea17e74 | 2012-05-24 07:01:38 -0700 | [diff] [blame] | 96 | "(jiffies|jiffies_64)|" |
H. Peter Anvin | a3e854d | 2012-05-18 00:24:09 -0700 | [diff] [blame] | 97 | "_end)$" |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | |
| 101 | static const char * const sym_regex_realmode[S_NSYMTYPES] = { |
| 102 | /* |
H. Peter Anvin | 2a6de31 | 2012-05-08 21:22:31 +0300 | [diff] [blame] | 103 | * These symbols are known to be relative, even if the linker marks them |
| 104 | * as absolute (typically defined outside any section in the linker script.) |
| 105 | */ |
| 106 | [S_REL] = |
| 107 | "^pa_", |
| 108 | |
| 109 | /* |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 110 | * These are 16-bit segment symbols when compiling 16-bit code. |
| 111 | */ |
| 112 | [S_SEG] = |
| 113 | "^real_mode_seg$", |
| 114 | |
| 115 | /* |
| 116 | * These are offsets belonging to segments, as opposed to linear addresses, |
| 117 | * when compiling 16-bit code. |
| 118 | */ |
| 119 | [S_LIN] = |
| 120 | "^pa_", |
| 121 | }; |
| 122 | |
| 123 | static const char * const *sym_regex; |
| 124 | |
| 125 | static regex_t sym_regex_c[S_NSYMTYPES]; |
| 126 | static int is_reloc(enum symtype type, const char *sym_name) |
H. Peter Anvin | 873b527 | 2009-12-14 13:55:20 -0800 | [diff] [blame] | 127 | { |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 128 | return sym_regex[type] && |
| 129 | !regexec(&sym_regex_c[type], sym_name, 0, NULL, 0); |
H. Peter Anvin | 873b527 | 2009-12-14 13:55:20 -0800 | [diff] [blame] | 130 | } |
| 131 | |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 132 | static void regex_init(int use_real_mode) |
H. Peter Anvin | 873b527 | 2009-12-14 13:55:20 -0800 | [diff] [blame] | 133 | { |
| 134 | char errbuf[128]; |
| 135 | int err; |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 136 | int i; |
H. Peter Anvin | 873b527 | 2009-12-14 13:55:20 -0800 | [diff] [blame] | 137 | |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 138 | if (use_real_mode) |
| 139 | sym_regex = sym_regex_realmode; |
| 140 | else |
| 141 | sym_regex = sym_regex_kernel; |
| 142 | |
| 143 | for (i = 0; i < S_NSYMTYPES; i++) { |
| 144 | if (!sym_regex[i]) |
| 145 | continue; |
| 146 | |
| 147 | err = regcomp(&sym_regex_c[i], sym_regex[i], |
| 148 | REG_EXTENDED|REG_NOSUB); |
| 149 | |
| 150 | if (err) { |
| 151 | regerror(err, &sym_regex_c[i], errbuf, sizeof errbuf); |
| 152 | die("%s", errbuf); |
| 153 | } |
H. Peter Anvin | 873b527 | 2009-12-14 13:55:20 -0800 | [diff] [blame] | 154 | } |
Vivek Goyal | 6a044b3 | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 155 | } |
| 156 | |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 157 | static void die(char *fmt, ...) |
| 158 | { |
| 159 | va_list ap; |
| 160 | va_start(ap, fmt); |
| 161 | vfprintf(stderr, fmt, ap); |
| 162 | va_end(ap); |
| 163 | exit(1); |
| 164 | } |
| 165 | |
| 166 | static const char *sym_type(unsigned type) |
| 167 | { |
| 168 | static const char *type_name[] = { |
| 169 | #define SYM_TYPE(X) [X] = #X |
| 170 | SYM_TYPE(STT_NOTYPE), |
| 171 | SYM_TYPE(STT_OBJECT), |
| 172 | SYM_TYPE(STT_FUNC), |
| 173 | SYM_TYPE(STT_SECTION), |
| 174 | SYM_TYPE(STT_FILE), |
| 175 | SYM_TYPE(STT_COMMON), |
| 176 | SYM_TYPE(STT_TLS), |
| 177 | #undef SYM_TYPE |
| 178 | }; |
| 179 | const char *name = "unknown sym type name"; |
Robert P. J. Day | ca82018 | 2007-02-17 19:10:01 +0100 | [diff] [blame] | 180 | if (type < ARRAY_SIZE(type_name)) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 181 | name = type_name[type]; |
| 182 | } |
| 183 | return name; |
| 184 | } |
| 185 | |
| 186 | static const char *sym_bind(unsigned bind) |
| 187 | { |
| 188 | static const char *bind_name[] = { |
| 189 | #define SYM_BIND(X) [X] = #X |
| 190 | SYM_BIND(STB_LOCAL), |
| 191 | SYM_BIND(STB_GLOBAL), |
| 192 | SYM_BIND(STB_WEAK), |
| 193 | #undef SYM_BIND |
| 194 | }; |
| 195 | const char *name = "unknown sym bind name"; |
Robert P. J. Day | ca82018 | 2007-02-17 19:10:01 +0100 | [diff] [blame] | 196 | if (bind < ARRAY_SIZE(bind_name)) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 197 | name = bind_name[bind]; |
| 198 | } |
| 199 | return name; |
| 200 | } |
| 201 | |
| 202 | static const char *sym_visibility(unsigned visibility) |
| 203 | { |
| 204 | static const char *visibility_name[] = { |
| 205 | #define SYM_VISIBILITY(X) [X] = #X |
| 206 | SYM_VISIBILITY(STV_DEFAULT), |
| 207 | SYM_VISIBILITY(STV_INTERNAL), |
| 208 | SYM_VISIBILITY(STV_HIDDEN), |
| 209 | SYM_VISIBILITY(STV_PROTECTED), |
| 210 | #undef SYM_VISIBILITY |
| 211 | }; |
| 212 | const char *name = "unknown sym visibility name"; |
Robert P. J. Day | ca82018 | 2007-02-17 19:10:01 +0100 | [diff] [blame] | 213 | if (visibility < ARRAY_SIZE(visibility_name)) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 214 | name = visibility_name[visibility]; |
| 215 | } |
| 216 | return name; |
| 217 | } |
| 218 | |
| 219 | static const char *rel_type(unsigned type) |
| 220 | { |
| 221 | static const char *type_name[] = { |
| 222 | #define REL_TYPE(X) [X] = #X |
| 223 | REL_TYPE(R_386_NONE), |
| 224 | REL_TYPE(R_386_32), |
| 225 | REL_TYPE(R_386_PC32), |
| 226 | REL_TYPE(R_386_GOT32), |
| 227 | REL_TYPE(R_386_PLT32), |
| 228 | REL_TYPE(R_386_COPY), |
| 229 | REL_TYPE(R_386_GLOB_DAT), |
| 230 | REL_TYPE(R_386_JMP_SLOT), |
| 231 | REL_TYPE(R_386_RELATIVE), |
| 232 | REL_TYPE(R_386_GOTOFF), |
| 233 | REL_TYPE(R_386_GOTPC), |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 234 | REL_TYPE(R_386_8), |
| 235 | REL_TYPE(R_386_PC8), |
| 236 | REL_TYPE(R_386_16), |
| 237 | REL_TYPE(R_386_PC16), |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 238 | #undef REL_TYPE |
| 239 | }; |
| 240 | const char *name = "unknown type rel type name"; |
H. Peter Anvin | 873b527 | 2009-12-14 13:55:20 -0800 | [diff] [blame] | 241 | if (type < ARRAY_SIZE(type_name) && type_name[type]) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 242 | name = type_name[type]; |
| 243 | } |
| 244 | return name; |
| 245 | } |
| 246 | |
| 247 | static const char *sec_name(unsigned shndx) |
| 248 | { |
| 249 | const char *sec_strtab; |
| 250 | const char *name; |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 251 | sec_strtab = secs[ehdr.e_shstrndx].strtab; |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 252 | name = "<noname>"; |
| 253 | if (shndx < ehdr.e_shnum) { |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 254 | name = sec_strtab + secs[shndx].shdr.sh_name; |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 255 | } |
| 256 | else if (shndx == SHN_ABS) { |
| 257 | name = "ABSOLUTE"; |
| 258 | } |
| 259 | else if (shndx == SHN_COMMON) { |
| 260 | name = "COMMON"; |
| 261 | } |
| 262 | return name; |
| 263 | } |
| 264 | |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame^] | 265 | 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] | 266 | { |
| 267 | const char *name; |
| 268 | name = "<noname>"; |
| 269 | if (sym->st_name) { |
| 270 | name = sym_strtab + sym->st_name; |
| 271 | } |
| 272 | else { |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 273 | name = sec_name(sym->st_shndx); |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 274 | } |
| 275 | return name; |
| 276 | } |
| 277 | |
| 278 | |
| 279 | |
Linus Torvalds | 13da9e2 | 2010-05-26 08:30:15 -0700 | [diff] [blame] | 280 | #if BYTE_ORDER == LITTLE_ENDIAN |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 281 | #define le16_to_cpu(val) (val) |
| 282 | #define le32_to_cpu(val) (val) |
| 283 | #endif |
Linus Torvalds | 13da9e2 | 2010-05-26 08:30:15 -0700 | [diff] [blame] | 284 | #if BYTE_ORDER == BIG_ENDIAN |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 285 | #define le16_to_cpu(val) bswap_16(val) |
| 286 | #define le32_to_cpu(val) bswap_32(val) |
| 287 | #endif |
| 288 | |
| 289 | static uint16_t elf16_to_cpu(uint16_t val) |
| 290 | { |
| 291 | return le16_to_cpu(val); |
| 292 | } |
| 293 | |
| 294 | static uint32_t elf32_to_cpu(uint32_t val) |
| 295 | { |
| 296 | return le32_to_cpu(val); |
| 297 | } |
| 298 | |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame^] | 299 | #define elf_half_to_cpu(x) elf16_to_cpu(x) |
| 300 | #define elf_word_to_cpu(x) elf32_to_cpu(x) |
| 301 | #define elf_addr_to_cpu(x) elf32_to_cpu(x) |
| 302 | #define elf_off_to_cpu(x) elf32_to_cpu(x) |
| 303 | #define elf_xword_to_cpu(x) elf32_to_cpu(x) |
| 304 | |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 305 | static void read_ehdr(FILE *fp) |
| 306 | { |
| 307 | if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1) { |
| 308 | die("Cannot read ELF header: %s\n", |
| 309 | strerror(errno)); |
| 310 | } |
Cyrill Gorcunov | 8bd1796 | 2008-05-03 14:18:03 +0400 | [diff] [blame] | 311 | if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 312 | die("No ELF magic\n"); |
| 313 | } |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame^] | 314 | if (ehdr.e_ident[EI_CLASS] != ELF_CLASS) { |
| 315 | die("Not a %d bit executable\n", ELF_BITS); |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 316 | } |
| 317 | if (ehdr.e_ident[EI_DATA] != ELFDATA2LSB) { |
| 318 | die("Not a LSB ELF executable\n"); |
| 319 | } |
| 320 | if (ehdr.e_ident[EI_VERSION] != EV_CURRENT) { |
| 321 | die("Unknown ELF version\n"); |
| 322 | } |
| 323 | /* Convert the fields to native endian */ |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame^] | 324 | ehdr.e_type = elf_half_to_cpu(ehdr.e_type); |
| 325 | ehdr.e_machine = elf_half_to_cpu(ehdr.e_machine); |
| 326 | ehdr.e_version = elf_word_to_cpu(ehdr.e_version); |
| 327 | ehdr.e_entry = elf_addr_to_cpu(ehdr.e_entry); |
| 328 | ehdr.e_phoff = elf_off_to_cpu(ehdr.e_phoff); |
| 329 | ehdr.e_shoff = elf_off_to_cpu(ehdr.e_shoff); |
| 330 | ehdr.e_flags = elf_word_to_cpu(ehdr.e_flags); |
| 331 | ehdr.e_ehsize = elf_half_to_cpu(ehdr.e_ehsize); |
| 332 | ehdr.e_phentsize = elf_half_to_cpu(ehdr.e_phentsize); |
| 333 | ehdr.e_phnum = elf_half_to_cpu(ehdr.e_phnum); |
| 334 | ehdr.e_shentsize = elf_half_to_cpu(ehdr.e_shentsize); |
| 335 | ehdr.e_shnum = elf_half_to_cpu(ehdr.e_shnum); |
| 336 | ehdr.e_shstrndx = elf_half_to_cpu(ehdr.e_shstrndx); |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 337 | |
| 338 | if ((ehdr.e_type != ET_EXEC) && (ehdr.e_type != ET_DYN)) { |
| 339 | die("Unsupported ELF header type\n"); |
| 340 | } |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame^] | 341 | if (ehdr.e_machine != ELF_MACHINE) { |
| 342 | die("Not for %s\n", ELF_MACHINE_NAME); |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 343 | } |
| 344 | if (ehdr.e_version != EV_CURRENT) { |
| 345 | die("Unknown ELF version\n"); |
| 346 | } |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame^] | 347 | if (ehdr.e_ehsize != sizeof(Elf_Ehdr)) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 348 | die("Bad Elf header size\n"); |
| 349 | } |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame^] | 350 | if (ehdr.e_phentsize != sizeof(Elf_Phdr)) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 351 | die("Bad program header entry\n"); |
| 352 | } |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame^] | 353 | if (ehdr.e_shentsize != sizeof(Elf_Shdr)) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 354 | die("Bad section header entry\n"); |
| 355 | } |
| 356 | if (ehdr.e_shstrndx >= ehdr.e_shnum) { |
| 357 | die("String table index out of bounds\n"); |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | static void read_shdrs(FILE *fp) |
| 362 | { |
| 363 | int i; |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame^] | 364 | Elf_Shdr shdr; |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 365 | |
| 366 | secs = calloc(ehdr.e_shnum, sizeof(struct section)); |
| 367 | if (!secs) { |
| 368 | die("Unable to allocate %d section headers\n", |
| 369 | ehdr.e_shnum); |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 370 | } |
| 371 | if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0) { |
| 372 | die("Seek to %d failed: %s\n", |
| 373 | ehdr.e_shoff, strerror(errno)); |
| 374 | } |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 375 | for (i = 0; i < ehdr.e_shnum; i++) { |
| 376 | struct section *sec = &secs[i]; |
| 377 | if (fread(&shdr, sizeof shdr, 1, fp) != 1) |
| 378 | die("Cannot read ELF section headers %d/%d: %s\n", |
| 379 | i, ehdr.e_shnum, strerror(errno)); |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame^] | 380 | sec->shdr.sh_name = elf_word_to_cpu(shdr.sh_name); |
| 381 | sec->shdr.sh_type = elf_word_to_cpu(shdr.sh_type); |
| 382 | sec->shdr.sh_flags = elf_xword_to_cpu(shdr.sh_flags); |
| 383 | sec->shdr.sh_addr = elf_addr_to_cpu(shdr.sh_addr); |
| 384 | sec->shdr.sh_offset = elf_off_to_cpu(shdr.sh_offset); |
| 385 | sec->shdr.sh_size = elf_xword_to_cpu(shdr.sh_size); |
| 386 | sec->shdr.sh_link = elf_word_to_cpu(shdr.sh_link); |
| 387 | sec->shdr.sh_info = elf_word_to_cpu(shdr.sh_info); |
| 388 | sec->shdr.sh_addralign = elf_xword_to_cpu(shdr.sh_addralign); |
| 389 | sec->shdr.sh_entsize = elf_xword_to_cpu(shdr.sh_entsize); |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 390 | if (sec->shdr.sh_link < ehdr.e_shnum) |
| 391 | sec->link = &secs[sec->shdr.sh_link]; |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | } |
| 395 | |
| 396 | static void read_strtabs(FILE *fp) |
| 397 | { |
| 398 | int i; |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 399 | for (i = 0; i < ehdr.e_shnum; i++) { |
| 400 | struct section *sec = &secs[i]; |
| 401 | if (sec->shdr.sh_type != SHT_STRTAB) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 402 | continue; |
| 403 | } |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 404 | sec->strtab = malloc(sec->shdr.sh_size); |
| 405 | if (!sec->strtab) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 406 | die("malloc of %d bytes for strtab failed\n", |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 407 | sec->shdr.sh_size); |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 408 | } |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 409 | if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 410 | die("Seek to %d failed: %s\n", |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 411 | sec->shdr.sh_offset, strerror(errno)); |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 412 | } |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 413 | if (fread(sec->strtab, 1, sec->shdr.sh_size, fp) |
| 414 | != sec->shdr.sh_size) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 415 | die("Cannot read symbol table: %s\n", |
| 416 | strerror(errno)); |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | static void read_symtabs(FILE *fp) |
| 422 | { |
| 423 | int i,j; |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 424 | for (i = 0; i < ehdr.e_shnum; i++) { |
| 425 | struct section *sec = &secs[i]; |
| 426 | if (sec->shdr.sh_type != SHT_SYMTAB) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 427 | continue; |
| 428 | } |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 429 | sec->symtab = malloc(sec->shdr.sh_size); |
| 430 | if (!sec->symtab) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 431 | die("malloc of %d bytes for symtab failed\n", |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 432 | sec->shdr.sh_size); |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 433 | } |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 434 | if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 435 | die("Seek to %d failed: %s\n", |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 436 | sec->shdr.sh_offset, strerror(errno)); |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 437 | } |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 438 | if (fread(sec->symtab, 1, sec->shdr.sh_size, fp) |
| 439 | != sec->shdr.sh_size) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 440 | die("Cannot read symbol table: %s\n", |
| 441 | strerror(errno)); |
| 442 | } |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame^] | 443 | for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Sym); j++) { |
| 444 | Elf_Sym *sym = &sec->symtab[j]; |
| 445 | sym->st_name = elf_word_to_cpu(sym->st_name); |
| 446 | sym->st_value = elf_addr_to_cpu(sym->st_value); |
| 447 | sym->st_size = elf_xword_to_cpu(sym->st_size); |
| 448 | sym->st_shndx = elf_half_to_cpu(sym->st_shndx); |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | |
| 454 | static void read_relocs(FILE *fp) |
| 455 | { |
| 456 | int i,j; |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 457 | for (i = 0; i < ehdr.e_shnum; i++) { |
| 458 | struct section *sec = &secs[i]; |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame^] | 459 | if (sec->shdr.sh_type != SHT_REL_TYPE) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 460 | continue; |
| 461 | } |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 462 | sec->reltab = malloc(sec->shdr.sh_size); |
| 463 | if (!sec->reltab) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 464 | die("malloc of %d bytes for relocs failed\n", |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 465 | sec->shdr.sh_size); |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 466 | } |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 467 | if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 468 | die("Seek to %d failed: %s\n", |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 469 | sec->shdr.sh_offset, strerror(errno)); |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 470 | } |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 471 | if (fread(sec->reltab, 1, sec->shdr.sh_size, fp) |
| 472 | != sec->shdr.sh_size) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 473 | die("Cannot read symbol table: %s\n", |
| 474 | strerror(errno)); |
| 475 | } |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame^] | 476 | for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Rel); j++) { |
| 477 | Elf_Rel *rel = &sec->reltab[j]; |
| 478 | rel->r_offset = elf_addr_to_cpu(rel->r_offset); |
| 479 | rel->r_info = elf_xword_to_cpu(rel->r_info); |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 480 | } |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | |
| 485 | static void print_absolute_symbols(void) |
| 486 | { |
| 487 | int i; |
| 488 | printf("Absolute symbols\n"); |
| 489 | printf(" Num: Value Size Type Bind Visibility Name\n"); |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 490 | for (i = 0; i < ehdr.e_shnum; i++) { |
| 491 | struct section *sec = &secs[i]; |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 492 | char *sym_strtab; |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 493 | int j; |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 494 | |
| 495 | if (sec->shdr.sh_type != SHT_SYMTAB) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 496 | continue; |
| 497 | } |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 498 | sym_strtab = sec->link->strtab; |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame^] | 499 | for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Sym); j++) { |
| 500 | Elf_Sym *sym; |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 501 | const char *name; |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 502 | sym = &sec->symtab[j]; |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 503 | name = sym_name(sym_strtab, sym); |
| 504 | if (sym->st_shndx != SHN_ABS) { |
| 505 | continue; |
| 506 | } |
| 507 | printf("%5d %08x %5d %10s %10s %12s %s\n", |
| 508 | j, sym->st_value, sym->st_size, |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame^] | 509 | sym_type(ELF_ST_TYPE(sym->st_info)), |
| 510 | sym_bind(ELF_ST_BIND(sym->st_info)), |
| 511 | sym_visibility(ELF_ST_VISIBILITY(sym->st_other)), |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 512 | name); |
| 513 | } |
| 514 | } |
| 515 | printf("\n"); |
| 516 | } |
| 517 | |
| 518 | static void print_absolute_relocs(void) |
| 519 | { |
Vivek Goyal | 6a044b3 | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 520 | int i, printed = 0; |
| 521 | |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 522 | for (i = 0; i < ehdr.e_shnum; i++) { |
| 523 | struct section *sec = &secs[i]; |
| 524 | struct section *sec_applies, *sec_symtab; |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 525 | char *sym_strtab; |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame^] | 526 | Elf_Sym *sh_symtab; |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 527 | int j; |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame^] | 528 | if (sec->shdr.sh_type != SHT_REL_TYPE) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 529 | continue; |
| 530 | } |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 531 | sec_symtab = sec->link; |
| 532 | sec_applies = &secs[sec->shdr.sh_info]; |
| 533 | if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 534 | continue; |
| 535 | } |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 536 | sh_symtab = sec_symtab->symtab; |
| 537 | sym_strtab = sec_symtab->link->strtab; |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame^] | 538 | for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Rel); j++) { |
| 539 | Elf_Rel *rel; |
| 540 | Elf_Sym *sym; |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 541 | const char *name; |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 542 | rel = &sec->reltab[j]; |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame^] | 543 | sym = &sh_symtab[ELF_R_SYM(rel->r_info)]; |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 544 | name = sym_name(sym_strtab, sym); |
| 545 | if (sym->st_shndx != SHN_ABS) { |
| 546 | continue; |
| 547 | } |
Vivek Goyal | 6a044b3 | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 548 | |
| 549 | /* Absolute symbols are not relocated if bzImage is |
| 550 | * loaded at a non-compiled address. Display a warning |
| 551 | * to user at compile time about the absolute |
| 552 | * relocations present. |
| 553 | * |
| 554 | * User need to audit the code to make sure |
| 555 | * some symbols which should have been section |
| 556 | * relative have not become absolute because of some |
| 557 | * linker optimization or wrong programming usage. |
| 558 | * |
| 559 | * Before warning check if this absolute symbol |
| 560 | * relocation is harmless. |
| 561 | */ |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 562 | if (is_reloc(S_ABS, name) || is_reloc(S_REL, name)) |
Vivek Goyal | 6a044b3 | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 563 | continue; |
| 564 | |
| 565 | if (!printed) { |
| 566 | printf("WARNING: Absolute relocations" |
| 567 | " present\n"); |
| 568 | printf("Offset Info Type Sym.Value " |
| 569 | "Sym.Name\n"); |
| 570 | printed = 1; |
| 571 | } |
| 572 | |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 573 | printf("%08x %08x %10s %08x %s\n", |
| 574 | rel->r_offset, |
| 575 | rel->r_info, |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame^] | 576 | rel_type(ELF_R_TYPE(rel->r_info)), |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 577 | sym->st_value, |
| 578 | name); |
| 579 | } |
| 580 | } |
Vivek Goyal | 6a044b3 | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 581 | |
| 582 | if (printed) |
| 583 | printf("\n"); |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 584 | } |
| 585 | |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame^] | 586 | static void walk_relocs(void (*visit)(Elf_Rel *rel, Elf_Sym *sym), |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 587 | int use_real_mode) |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 588 | { |
| 589 | int i; |
| 590 | /* Walk through the relocations */ |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 591 | for (i = 0; i < ehdr.e_shnum; i++) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 592 | char *sym_strtab; |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame^] | 593 | Elf_Sym *sh_symtab; |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 594 | struct section *sec_applies, *sec_symtab; |
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 | struct section *sec = &secs[i]; |
| 597 | |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame^] | 598 | if (sec->shdr.sh_type != SHT_REL_TYPE) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 599 | continue; |
| 600 | } |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 601 | sec_symtab = sec->link; |
| 602 | sec_applies = &secs[sec->shdr.sh_info]; |
| 603 | if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 604 | continue; |
| 605 | } |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 606 | sh_symtab = sec_symtab->symtab; |
H. Peter Anvin | cc65f1e | 2008-10-03 13:00:56 -0700 | [diff] [blame] | 607 | sym_strtab = sec_symtab->link->strtab; |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame^] | 608 | for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Rel); j++) { |
| 609 | Elf_Rel *rel; |
| 610 | Elf_Sym *sym; |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 611 | unsigned r_type; |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 612 | const char *symname; |
H. Peter Anvin | 24ab82b | 2012-05-18 09:52:01 -0700 | [diff] [blame] | 613 | int shn_abs; |
| 614 | |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 615 | rel = &sec->reltab[j]; |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame^] | 616 | sym = &sh_symtab[ELF_R_SYM(rel->r_info)]; |
| 617 | r_type = ELF_R_TYPE(rel->r_info); |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 618 | |
H. Peter Anvin | 24ab82b | 2012-05-18 09:52:01 -0700 | [diff] [blame] | 619 | shn_abs = sym->st_shndx == SHN_ABS; |
| 620 | |
H. Peter Anvin | 873b527 | 2009-12-14 13:55:20 -0800 | [diff] [blame] | 621 | switch (r_type) { |
| 622 | case R_386_NONE: |
| 623 | case R_386_PC32: |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 624 | case R_386_PC16: |
| 625 | case R_386_PC8: |
Tejun Heo | 46176b4 | 2009-05-26 14:42:40 +0900 | [diff] [blame] | 626 | /* |
| 627 | * NONE can be ignored and and PC relative |
| 628 | * relocations don't need to be adjusted. |
| 629 | */ |
H. Peter Anvin | 873b527 | 2009-12-14 13:55:20 -0800 | [diff] [blame] | 630 | break; |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 631 | |
| 632 | case R_386_16: |
| 633 | symname = sym_name(sym_strtab, sym); |
| 634 | if (!use_real_mode) |
| 635 | goto bad; |
H. Peter Anvin | 24ab82b | 2012-05-18 09:52:01 -0700 | [diff] [blame] | 636 | if (shn_abs) { |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 637 | if (is_reloc(S_ABS, symname)) |
| 638 | break; |
| 639 | else if (!is_reloc(S_SEG, symname)) |
| 640 | goto bad; |
| 641 | } else { |
| 642 | if (is_reloc(S_LIN, symname)) |
| 643 | goto bad; |
| 644 | else |
| 645 | break; |
| 646 | } |
| 647 | visit(rel, sym); |
| 648 | break; |
| 649 | |
H. Peter Anvin | 873b527 | 2009-12-14 13:55:20 -0800 | [diff] [blame] | 650 | case R_386_32: |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 651 | symname = sym_name(sym_strtab, sym); |
H. Peter Anvin | 24ab82b | 2012-05-18 09:52:01 -0700 | [diff] [blame] | 652 | if (shn_abs) { |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 653 | if (is_reloc(S_ABS, symname)) |
| 654 | break; |
| 655 | else if (!is_reloc(S_REL, symname)) |
| 656 | goto bad; |
| 657 | } else { |
| 658 | if (use_real_mode && |
| 659 | !is_reloc(S_LIN, symname)) |
| 660 | break; |
| 661 | } |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 662 | visit(rel, sym); |
H. Peter Anvin | 873b527 | 2009-12-14 13:55:20 -0800 | [diff] [blame] | 663 | break; |
| 664 | default: |
| 665 | die("Unsupported relocation type: %s (%d)\n", |
| 666 | rel_type(r_type), r_type); |
| 667 | break; |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 668 | bad: |
| 669 | symname = sym_name(sym_strtab, sym); |
H. Peter Anvin | 24ab82b | 2012-05-18 09:52:01 -0700 | [diff] [blame] | 670 | die("Invalid %s %s relocation: %s\n", |
| 671 | shn_abs ? "absolute" : "relative", |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 672 | rel_type(r_type), symname); |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 673 | } |
| 674 | } |
| 675 | } |
| 676 | } |
| 677 | |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame^] | 678 | static void count_reloc(Elf_Rel *rel, Elf_Sym *sym) |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 679 | { |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame^] | 680 | if (ELF_R_TYPE(rel->r_info) == R_386_16) |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 681 | reloc16_count++; |
| 682 | else |
| 683 | reloc_count++; |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 684 | } |
| 685 | |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame^] | 686 | static void collect_reloc(Elf_Rel *rel, Elf_Sym *sym) |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 687 | { |
| 688 | /* Remember the address that needs to be adjusted. */ |
Kees Cook | bf11655 | 2013-04-12 13:13:42 -0700 | [diff] [blame^] | 689 | if (ELF_R_TYPE(rel->r_info) == R_386_16) |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 690 | relocs16[reloc16_idx++] = rel->r_offset; |
| 691 | else |
| 692 | relocs[reloc_idx++] = rel->r_offset; |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | static int cmp_relocs(const void *va, const void *vb) |
| 696 | { |
| 697 | const unsigned long *a, *b; |
| 698 | a = va; b = vb; |
| 699 | return (*a == *b)? 0 : (*a > *b)? 1 : -1; |
| 700 | } |
| 701 | |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 702 | static int write32(unsigned int v, FILE *f) |
| 703 | { |
| 704 | unsigned char buf[4]; |
| 705 | |
| 706 | put_unaligned_le32(v, buf); |
| 707 | return fwrite(buf, 1, 4, f) == 4 ? 0 : -1; |
| 708 | } |
| 709 | |
| 710 | static void emit_relocs(int as_text, int use_real_mode) |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 711 | { |
| 712 | int i; |
| 713 | /* Count how many relocations I have and allocate space for them. */ |
| 714 | reloc_count = 0; |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 715 | walk_relocs(count_reloc, use_real_mode); |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 716 | relocs = malloc(reloc_count * sizeof(relocs[0])); |
| 717 | if (!relocs) { |
| 718 | die("malloc of %d entries for relocs failed\n", |
| 719 | reloc_count); |
| 720 | } |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 721 | |
| 722 | relocs16 = malloc(reloc16_count * sizeof(relocs[0])); |
| 723 | if (!relocs16) { |
| 724 | die("malloc of %d entries for relocs16 failed\n", |
| 725 | reloc16_count); |
| 726 | } |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 727 | /* Collect up the relocations */ |
| 728 | reloc_idx = 0; |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 729 | walk_relocs(collect_reloc, use_real_mode); |
| 730 | |
| 731 | if (reloc16_count && !use_real_mode) |
| 732 | die("Segment relocations found but --realmode not specified\n"); |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 733 | |
| 734 | /* Order the relocations for more efficient processing */ |
| 735 | qsort(relocs, reloc_count, sizeof(relocs[0]), cmp_relocs); |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 736 | qsort(relocs16, reloc16_count, sizeof(relocs16[0]), cmp_relocs); |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 737 | |
| 738 | /* Print the relocations */ |
| 739 | if (as_text) { |
| 740 | /* Print the relocations in a form suitable that |
| 741 | * gas will like. |
| 742 | */ |
| 743 | printf(".section \".data.reloc\",\"a\"\n"); |
| 744 | printf(".balign 4\n"); |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 745 | if (use_real_mode) { |
| 746 | printf("\t.long %lu\n", reloc16_count); |
| 747 | for (i = 0; i < reloc16_count; i++) |
| 748 | printf("\t.long 0x%08lx\n", relocs16[i]); |
| 749 | printf("\t.long %lu\n", reloc_count); |
| 750 | for (i = 0; i < reloc_count; i++) { |
| 751 | printf("\t.long 0x%08lx\n", relocs[i]); |
| 752 | } |
| 753 | } else { |
| 754 | /* Print a stop */ |
| 755 | printf("\t.long 0x%08lx\n", (unsigned long)0); |
| 756 | for (i = 0; i < reloc_count; i++) { |
| 757 | printf("\t.long 0x%08lx\n", relocs[i]); |
| 758 | } |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 759 | } |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 760 | |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 761 | printf("\n"); |
| 762 | } |
| 763 | else { |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 764 | if (use_real_mode) { |
| 765 | write32(reloc16_count, stdout); |
| 766 | for (i = 0; i < reloc16_count; i++) |
| 767 | write32(relocs16[i], stdout); |
| 768 | write32(reloc_count, stdout); |
| 769 | |
| 770 | /* Now print each relocation */ |
| 771 | for (i = 0; i < reloc_count; i++) |
| 772 | write32(relocs[i], stdout); |
| 773 | } else { |
| 774 | /* Print a stop */ |
| 775 | write32(0, stdout); |
| 776 | |
| 777 | /* Now print each relocation */ |
| 778 | for (i = 0; i < reloc_count; i++) { |
| 779 | write32(relocs[i], stdout); |
| 780 | } |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 781 | } |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | static void usage(void) |
| 786 | { |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 787 | die("relocs [--abs-syms|--abs-relocs|--text|--realmode] vmlinux\n"); |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | int main(int argc, char **argv) |
| 791 | { |
Vivek Goyal | 6a044b3 | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 792 | int show_absolute_syms, show_absolute_relocs; |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 793 | int as_text, use_real_mode; |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 794 | const char *fname; |
| 795 | FILE *fp; |
| 796 | int i; |
| 797 | |
Vivek Goyal | 6a044b3 | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 798 | show_absolute_syms = 0; |
| 799 | show_absolute_relocs = 0; |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 800 | as_text = 0; |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 801 | use_real_mode = 0; |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 802 | fname = NULL; |
H. Peter Anvin | 908ec7a | 2008-06-30 14:42:18 -0700 | [diff] [blame] | 803 | for (i = 1; i < argc; i++) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 804 | char *arg = argv[i]; |
| 805 | if (*arg == '-') { |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 806 | if (strcmp(arg, "--abs-syms") == 0) { |
Vivek Goyal | 6a044b3 | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 807 | show_absolute_syms = 1; |
| 808 | continue; |
| 809 | } |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 810 | if (strcmp(arg, "--abs-relocs") == 0) { |
Vivek Goyal | 6a044b3 | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 811 | show_absolute_relocs = 1; |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 812 | continue; |
| 813 | } |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 814 | if (strcmp(arg, "--text") == 0) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 815 | as_text = 1; |
| 816 | continue; |
| 817 | } |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 818 | if (strcmp(arg, "--realmode") == 0) { |
| 819 | use_real_mode = 1; |
| 820 | continue; |
| 821 | } |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 822 | } |
| 823 | else if (!fname) { |
| 824 | fname = arg; |
| 825 | continue; |
| 826 | } |
| 827 | usage(); |
| 828 | } |
| 829 | if (!fname) { |
| 830 | usage(); |
| 831 | } |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 832 | regex_init(use_real_mode); |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 833 | fp = fopen(fname, "r"); |
| 834 | if (!fp) { |
| 835 | die("Cannot open %s: %s\n", |
| 836 | fname, strerror(errno)); |
| 837 | } |
| 838 | read_ehdr(fp); |
| 839 | read_shdrs(fp); |
| 840 | read_strtabs(fp); |
| 841 | read_symtabs(fp); |
| 842 | read_relocs(fp); |
Vivek Goyal | 6a044b3 | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 843 | if (show_absolute_syms) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 844 | print_absolute_symbols(); |
Cong Ding | 65315d4 | 2013-01-14 17:13:35 +0000 | [diff] [blame] | 845 | goto out; |
Vivek Goyal | 6a044b3 | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 846 | } |
| 847 | if (show_absolute_relocs) { |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 848 | print_absolute_relocs(); |
Cong Ding | 65315d4 | 2013-01-14 17:13:35 +0000 | [diff] [blame] | 849 | goto out; |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 850 | } |
H. Peter Anvin | 6520fe5 | 2012-05-08 21:22:24 +0300 | [diff] [blame] | 851 | emit_relocs(as_text, use_real_mode); |
Cong Ding | 65315d4 | 2013-01-14 17:13:35 +0000 | [diff] [blame] | 852 | out: |
| 853 | fclose(fp); |
Eric W. Biederman | 968de4f | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 854 | return 0; |
| 855 | } |