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