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