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