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