| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
/* This is included from relocs_32/64.c */ |
| 3 |
|
| 4 |
#define ElfW(type) _ElfW(ELF_BITS, type) |
| 5 |
#define _ElfW(bits, type) __ElfW(bits, type) |
| 6 |
#define __ElfW(bits, type) Elf##bits##_##type |
| 7 |
|
| 8 |
#define Elf_Addr ElfW(Addr) |
| 9 |
#define Elf_Ehdr ElfW(Ehdr) |
| 10 |
#define Elf_Phdr ElfW(Phdr) |
| 11 |
#define Elf_Shdr ElfW(Shdr) |
| 12 |
#define Elf_Sym ElfW(Sym) |
| 13 |
|
| 14 |
static Elf_Ehdr ehdr; |
| 15 |
|
| 16 |
struct relocs { |
| 17 |
uint32_t *offset; |
| 18 |
unsigned long count; |
| 19 |
unsigned long size; |
| 20 |
}; |
| 21 |
|
| 22 |
static struct relocs relocs; |
| 23 |
|
| 24 |
struct section { |
| 25 |
Elf_Shdr shdr; |
| 26 |
struct section *link; |
| 27 |
Elf_Sym *symtab; |
| 28 |
Elf_Rel *reltab; |
| 29 |
char *strtab; |
| 30 |
long shdr_offset; |
| 31 |
}; |
| 32 |
static struct section *secs; |
| 33 |
|
| 34 |
static const char * const regex_sym_kernel = { |
| 35 |
/* Symbols matching these regex's should never be relocated */ |
| 36 |
"^(__crc_)", |
| 37 |
}; |
| 38 |
|
| 39 |
static regex_t sym_regex_c; |
| 40 |
|
| 41 |
static int regex_skip_reloc(const char *sym_name) |
| 42 |
{ |
| 43 |
return !regexec(&sym_regex_c, sym_name, 0, NULL, 0); |
| 44 |
} |
| 45 |
|
| 46 |
static void regex_init(void) |
| 47 |
{ |
| 48 |
char errbuf[128]; |
| 49 |
int err; |
| 50 |
|
| 51 |
err = regcomp(&sym_regex_c, regex_sym_kernel, |
| 52 |
REG_EXTENDED|REG_NOSUB); |
| 53 |
|
| 54 |
if (err) { |
| 55 |
regerror(err, &sym_regex_c, errbuf, sizeof(errbuf)); |
| 56 |
die("%s", errbuf); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
static const char *rel_type(unsigned type) |
| 61 |
{ |
| 62 |
static const char * const type_name[] = { |
| 63 |
#define REL_TYPE(X)[X] = #X |
| 64 |
REL_TYPE(R_MIPS_NONE), |
| 65 |
REL_TYPE(R_MIPS_16), |
| 66 |
REL_TYPE(R_MIPS_32), |
| 67 |
REL_TYPE(R_MIPS_REL32), |
| 68 |
REL_TYPE(R_MIPS_26), |
| 69 |
REL_TYPE(R_MIPS_HI16), |
| 70 |
REL_TYPE(R_MIPS_LO16), |
| 71 |
REL_TYPE(R_MIPS_GPREL16), |
| 72 |
REL_TYPE(R_MIPS_LITERAL), |
| 73 |
REL_TYPE(R_MIPS_GOT16), |
| 74 |
REL_TYPE(R_MIPS_PC16), |
| 75 |
REL_TYPE(R_MIPS_CALL16), |
| 76 |
REL_TYPE(R_MIPS_GPREL32), |
| 77 |
REL_TYPE(R_MIPS_64), |
| 78 |
REL_TYPE(R_MIPS_HIGHER), |
| 79 |
REL_TYPE(R_MIPS_HIGHEST), |
| 80 |
REL_TYPE(R_MIPS_PC21_S2), |
| 81 |
REL_TYPE(R_MIPS_PC26_S2), |
| 82 |
REL_TYPE(R_MIPS_PC32), |
| 83 |
#undef REL_TYPE |
| 84 |
}; |
| 85 |
const char *name = "unknown type rel type name"; |
| 86 |
|
| 87 |
if (type < ARRAY_SIZE(type_name) && type_name[type]) |
| 88 |
name = type_name[type]; |
| 89 |
return name; |
| 90 |
} |
| 91 |
|
| 92 |
static const char *sec_name(unsigned shndx) |
| 93 |
{ |
| 94 |
const char *sec_strtab; |
| 95 |
const char *name; |
| 96 |
|
| 97 |
sec_strtab = secs[ehdr.e_shstrndx].strtab; |
| 98 |
if (shndx < ehdr.e_shnum) |
| 99 |
name = sec_strtab + secs[shndx].shdr.sh_name; |
| 100 |
else if (shndx == SHN_ABS) |
| 101 |
name = "ABSOLUTE"; |
| 102 |
else if (shndx == SHN_COMMON) |
| 103 |
name = "COMMON"; |
| 104 |
else |
| 105 |
name = "<noname>"; |
| 106 |
return name; |
| 107 |
} |
| 108 |
|
| 109 |
static struct section *sec_lookup(const char *secname) |
| 110 |
{ |
| 111 |
int i; |
| 112 |
|
| 113 |
for (i = 0; i < ehdr.e_shnum; i++) |
| 114 |
if (strcmp(secname, sec_name(i)) == 0) |
| 115 |
return &secs[i]; |
| 116 |
|
| 117 |
return NULL; |
| 118 |
} |
| 119 |
|
| 120 |
static const char *sym_name(const char *sym_strtab, Elf_Sym *sym) |
| 121 |
{ |
| 122 |
const char *name; |
| 123 |
|
| 124 |
if (sym->st_name) |
| 125 |
name = sym_strtab + sym->st_name; |
| 126 |
else |
| 127 |
name = sec_name(sym->st_shndx); |
| 128 |
return name; |
| 129 |
} |
| 130 |
|
| 131 |
#if BYTE_ORDER == LITTLE_ENDIAN |
| 132 |
#define le16_to_cpu(val) (val) |
| 133 |
#define le32_to_cpu(val) (val) |
| 134 |
#define le64_to_cpu(val) (val) |
| 135 |
#define be16_to_cpu(val) bswap_16(val) |
| 136 |
#define be32_to_cpu(val) bswap_32(val) |
| 137 |
#define be64_to_cpu(val) bswap_64(val) |
| 138 |
|
| 139 |
#define cpu_to_le16(val) (val) |
| 140 |
#define cpu_to_le32(val) (val) |
| 141 |
#define cpu_to_le64(val) (val) |
| 142 |
#define cpu_to_be16(val) bswap_16(val) |
| 143 |
#define cpu_to_be32(val) bswap_32(val) |
| 144 |
#define cpu_to_be64(val) bswap_64(val) |
| 145 |
#endif |
| 146 |
#if BYTE_ORDER == BIG_ENDIAN |
| 147 |
#define le16_to_cpu(val) bswap_16(val) |
| 148 |
#define le32_to_cpu(val) bswap_32(val) |
| 149 |
#define le64_to_cpu(val) bswap_64(val) |
| 150 |
#define be16_to_cpu(val) (val) |
| 151 |
#define be32_to_cpu(val) (val) |
| 152 |
#define be64_to_cpu(val) (val) |
| 153 |
|
| 154 |
#define cpu_to_le16(val) bswap_16(val) |
| 155 |
#define cpu_to_le32(val) bswap_32(val) |
| 156 |
#define cpu_to_le64(val) bswap_64(val) |
| 157 |
#define cpu_to_be16(val) (val) |
| 158 |
#define cpu_to_be32(val) (val) |
| 159 |
#define cpu_to_be64(val) (val) |
| 160 |
#endif |
| 161 |
|
| 162 |
static uint16_t elf16_to_cpu(uint16_t val) |
| 163 |
{ |
| 164 |
if (ehdr.e_ident[EI_DATA] == ELFDATA2LSB) |
| 165 |
return le16_to_cpu(val); |
| 166 |
else |
| 167 |
return be16_to_cpu(val); |
| 168 |
} |
| 169 |
|
| 170 |
static uint32_t elf32_to_cpu(uint32_t val) |
| 171 |
{ |
| 172 |
if (ehdr.e_ident[EI_DATA] == ELFDATA2LSB) |
| 173 |
return le32_to_cpu(val); |
| 174 |
else |
| 175 |
return be32_to_cpu(val); |
| 176 |
} |
| 177 |
|
| 178 |
static uint32_t cpu_to_elf32(uint32_t val) |
| 179 |
{ |
| 180 |
if (ehdr.e_ident[EI_DATA] == ELFDATA2LSB) |
| 181 |
return cpu_to_le32(val); |
| 182 |
else |
| 183 |
return cpu_to_be32(val); |
| 184 |
} |
| 185 |
|
| 186 |
#define elf_half_to_cpu(x) elf16_to_cpu(x) |
| 187 |
#define elf_word_to_cpu(x) elf32_to_cpu(x) |
| 188 |
|
| 189 |
#if ELF_BITS == 64 |
| 190 |
static uint64_t elf64_to_cpu(uint64_t val) |
| 191 |
{ |
| 192 |
if (ehdr.e_ident[EI_DATA] == ELFDATA2LSB) |
| 193 |
return le64_to_cpu(val); |
| 194 |
else |
| 195 |
return be64_to_cpu(val); |
| 196 |
} |
| 197 |
#define elf_addr_to_cpu(x) elf64_to_cpu(x) |
| 198 |
#define elf_off_to_cpu(x) elf64_to_cpu(x) |
| 199 |
#define elf_xword_to_cpu(x) elf64_to_cpu(x) |
| 200 |
#else |
| 201 |
#define elf_addr_to_cpu(x) elf32_to_cpu(x) |
| 202 |
#define elf_off_to_cpu(x) elf32_to_cpu(x) |
| 203 |
#define elf_xword_to_cpu(x) elf32_to_cpu(x) |
| 204 |
#endif |
| 205 |
|
| 206 |
static void read_ehdr(FILE *fp) |
| 207 |
{ |
| 208 |
if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1) |
| 209 |
die("Cannot read ELF header: %s\n", strerror(errno)); |
| 210 |
|
| 211 |
if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0) |
| 212 |
die("No ELF magic\n"); |
| 213 |
|
| 214 |
if (ehdr.e_ident[EI_CLASS] != ELF_CLASS) |
| 215 |
die("Not a %d bit executable\n", ELF_BITS); |
| 216 |
|
| 217 |
if ((ehdr.e_ident[EI_DATA] != ELFDATA2LSB) && |
| 218 |
(ehdr.e_ident[EI_DATA] != ELFDATA2MSB)) |
| 219 |
die("Unknown ELF Endianness\n"); |
| 220 |
|
| 221 |
if (ehdr.e_ident[EI_VERSION] != EV_CURRENT) |
| 222 |
die("Unknown ELF version\n"); |
| 223 |
|
| 224 |
/* Convert the fields to native endian */ |
| 225 |
ehdr.e_type = elf_half_to_cpu(ehdr.e_type); |
| 226 |
ehdr.e_machine = elf_half_to_cpu(ehdr.e_machine); |
| 227 |
ehdr.e_version = elf_word_to_cpu(ehdr.e_version); |
| 228 |
ehdr.e_entry = elf_addr_to_cpu(ehdr.e_entry); |
| 229 |
ehdr.e_phoff = elf_off_to_cpu(ehdr.e_phoff); |
| 230 |
ehdr.e_shoff = elf_off_to_cpu(ehdr.e_shoff); |
| 231 |
ehdr.e_flags = elf_word_to_cpu(ehdr.e_flags); |
| 232 |
ehdr.e_ehsize = elf_half_to_cpu(ehdr.e_ehsize); |
| 233 |
ehdr.e_phentsize = elf_half_to_cpu(ehdr.e_phentsize); |
| 234 |
ehdr.e_phnum = elf_half_to_cpu(ehdr.e_phnum); |
| 235 |
ehdr.e_shentsize = elf_half_to_cpu(ehdr.e_shentsize); |
| 236 |
ehdr.e_shnum = elf_half_to_cpu(ehdr.e_shnum); |
| 237 |
ehdr.e_shstrndx = elf_half_to_cpu(ehdr.e_shstrndx); |
| 238 |
|
| 239 |
if ((ehdr.e_type != ET_EXEC) && (ehdr.e_type != ET_DYN)) |
| 240 |
die("Unsupported ELF header type\n"); |
| 241 |
|
| 242 |
if (ehdr.e_machine != ELF_MACHINE) |
| 243 |
die("Not for %s\n", ELF_MACHINE_NAME); |
| 244 |
|
| 245 |
if (ehdr.e_version != EV_CURRENT) |
| 246 |
die("Unknown ELF version\n"); |
| 247 |
|
| 248 |
if (ehdr.e_ehsize != sizeof(Elf_Ehdr)) |
| 249 |
die("Bad ELF header size\n"); |
| 250 |
|
| 251 |
if (ehdr.e_phentsize != sizeof(Elf_Phdr)) |
| 252 |
die("Bad program header entry\n"); |
| 253 |
|
| 254 |
if (ehdr.e_shentsize != sizeof(Elf_Shdr)) |
| 255 |
die("Bad section header entry\n"); |
| 256 |
|
| 257 |
if (ehdr.e_shstrndx >= ehdr.e_shnum) |
| 258 |
die("String table index out of bounds\n"); |
| 259 |
} |
| 260 |
|
| 261 |
static void read_shdrs(FILE *fp) |
| 262 |
{ |
| 263 |
int i; |
| 264 |
Elf_Shdr shdr; |
| 265 |
|
| 266 |
secs = calloc(ehdr.e_shnum, sizeof(struct section)); |
| 267 |
if (!secs) |
| 268 |
die("Unable to allocate %d section headers\n", ehdr.e_shnum); |
| 269 |
|
| 270 |
if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0) |
| 271 |
die("Seek to %d failed: %s\n", ehdr.e_shoff, strerror(errno)); |
| 272 |
|
| 273 |
for (i = 0; i < ehdr.e_shnum; i++) { |
| 274 |
struct section *sec = &secs[i]; |
| 275 |
|
| 276 |
sec->shdr_offset = ftell(fp); |
| 277 |
if (fread(&shdr, sizeof(shdr), 1, fp) != 1) |
| 278 |
die("Cannot read ELF section headers %d/%d: %s\n", |
| 279 |
i, ehdr.e_shnum, strerror(errno)); |
| 280 |
sec->shdr.sh_name = elf_word_to_cpu(shdr.sh_name); |
| 281 |
sec->shdr.sh_type = elf_word_to_cpu(shdr.sh_type); |
| 282 |
sec->shdr.sh_flags = elf_xword_to_cpu(shdr.sh_flags); |
| 283 |
sec->shdr.sh_addr = elf_addr_to_cpu(shdr.sh_addr); |
| 284 |
sec->shdr.sh_offset = elf_off_to_cpu(shdr.sh_offset); |
| 285 |
sec->shdr.sh_size = elf_xword_to_cpu(shdr.sh_size); |
| 286 |
sec->shdr.sh_link = elf_word_to_cpu(shdr.sh_link); |
| 287 |
sec->shdr.sh_info = elf_word_to_cpu(shdr.sh_info); |
| 288 |
sec->shdr.sh_addralign = elf_xword_to_cpu(shdr.sh_addralign); |
| 289 |
sec->shdr.sh_entsize = elf_xword_to_cpu(shdr.sh_entsize); |
| 290 |
if (sec->shdr.sh_link < ehdr.e_shnum) |
| 291 |
sec->link = &secs[sec->shdr.sh_link]; |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
static void read_strtabs(FILE *fp) |
| 296 |
{ |
| 297 |
int i; |
| 298 |
|
| 299 |
for (i = 0; i < ehdr.e_shnum; i++) { |
| 300 |
struct section *sec = &secs[i]; |
| 301 |
|
| 302 |
if (sec->shdr.sh_type != SHT_STRTAB) |
| 303 |
continue; |
| 304 |
|
| 305 |
sec->strtab = malloc(sec->shdr.sh_size); |
| 306 |
if (!sec->strtab) |
| 307 |
die("malloc of %d bytes for strtab failed\n", |
| 308 |
sec->shdr.sh_size); |
| 309 |
|
| 310 |
if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) |
| 311 |
die("Seek to %d failed: %s\n", |
| 312 |
sec->shdr.sh_offset, strerror(errno)); |
| 313 |
|
| 314 |
if (fread(sec->strtab, 1, sec->shdr.sh_size, fp) != |
| 315 |
sec->shdr.sh_size) |
| 316 |
die("Cannot read symbol table: %s\n", strerror(errno)); |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
static void read_symtabs(FILE *fp) |
| 321 |
{ |
| 322 |
int i, j; |
| 323 |
|
| 324 |
for (i = 0; i < ehdr.e_shnum; i++) { |
| 325 |
struct section *sec = &secs[i]; |
| 326 |
if (sec->shdr.sh_type != SHT_SYMTAB) |
| 327 |
continue; |
| 328 |
|
| 329 |
sec->symtab = malloc(sec->shdr.sh_size); |
| 330 |
if (!sec->symtab) |
| 331 |
die("malloc of %d bytes for symtab failed\n", |
| 332 |
sec->shdr.sh_size); |
| 333 |
|
| 334 |
if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) |
| 335 |
die("Seek to %d failed: %s\n", |
| 336 |
sec->shdr.sh_offset, strerror(errno)); |
| 337 |
|
| 338 |
if (fread(sec->symtab, 1, sec->shdr.sh_size, fp) != |
| 339 |
sec->shdr.sh_size) |
| 340 |
die("Cannot read symbol table: %s\n", strerror(errno)); |
| 341 |
|
| 342 |
for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Sym); j++) { |
| 343 |
Elf_Sym *sym = &sec->symtab[j]; |
| 344 |
|
| 345 |
sym->st_name = elf_word_to_cpu(sym->st_name); |
| 346 |
sym->st_value = elf_addr_to_cpu(sym->st_value); |
| 347 |
sym->st_size = elf_xword_to_cpu(sym->st_size); |
| 348 |
sym->st_shndx = elf_half_to_cpu(sym->st_shndx); |
| 349 |
} |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
static void read_relocs(FILE *fp) |
| 354 |
{ |
| 355 |
static unsigned long base; |
| 356 |
int i, j; |
| 357 |
|
| 358 |
if (!base) { |
| 359 |
struct section *sec = sec_lookup(".text"); |
| 360 |
|
| 361 |
if (!sec) |
| 362 |
die("Could not find .text section\n"); |
| 363 |
|
| 364 |
base = sec->shdr.sh_addr; |
| 365 |
} |
| 366 |
|
| 367 |
for (i = 0; i < ehdr.e_shnum; i++) { |
| 368 |
struct section *sec = &secs[i]; |
| 369 |
|
| 370 |
if (sec->shdr.sh_type != SHT_REL_TYPE) |
| 371 |
continue; |
| 372 |
|
| 373 |
sec->reltab = malloc(sec->shdr.sh_size); |
| 374 |
if (!sec->reltab) |
| 375 |
die("malloc of %d bytes for relocs failed\n", |
| 376 |
sec->shdr.sh_size); |
| 377 |
|
| 378 |
if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) |
| 379 |
die("Seek to %d failed: %s\n", |
| 380 |
sec->shdr.sh_offset, strerror(errno)); |
| 381 |
|
| 382 |
if (fread(sec->reltab, 1, sec->shdr.sh_size, fp) != |
| 383 |
sec->shdr.sh_size) |
| 384 |
die("Cannot read symbol table: %s\n", strerror(errno)); |
| 385 |
|
| 386 |
for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Rel); j++) { |
| 387 |
Elf_Rel *rel = &sec->reltab[j]; |
| 388 |
|
| 389 |
rel->r_offset = elf_addr_to_cpu(rel->r_offset); |
| 390 |
/* Set offset into kernel image */ |
| 391 |
rel->r_offset -= base; |
| 392 |
#if (ELF_BITS == 32) |
| 393 |
rel->r_info = elf_xword_to_cpu(rel->r_info); |
| 394 |
#else |
| 395 |
/* Convert MIPS64 RELA format - only the symbol |
| 396 |
* index needs converting to native endianness |
| 397 |
*/ |
| 398 |
rel->r_info = rel->r_info; |
| 399 |
ELF_R_SYM(rel->r_info) = elf32_to_cpu(ELF_R_SYM(rel->r_info)); |
| 400 |
#endif |
| 401 |
#if (SHT_REL_TYPE == SHT_RELA) |
| 402 |
rel->r_addend = elf_xword_to_cpu(rel->r_addend); |
| 403 |
#endif |
| 404 |
} |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
static void remove_relocs(FILE *fp) |
| 409 |
{ |
| 410 |
int i; |
| 411 |
Elf_Shdr shdr; |
| 412 |
|
| 413 |
for (i = 0; i < ehdr.e_shnum; i++) { |
| 414 |
struct section *sec = &secs[i]; |
| 415 |
|
| 416 |
if (sec->shdr.sh_type != SHT_REL_TYPE) |
| 417 |
continue; |
| 418 |
|
| 419 |
if (fseek(fp, sec->shdr_offset, SEEK_SET) < 0) |
| 420 |
die("Seek to %d failed: %s\n", |
| 421 |
sec->shdr_offset, strerror(errno)); |
| 422 |
|
| 423 |
if (fread(&shdr, sizeof(shdr), 1, fp) != 1) |
| 424 |
die("Cannot read ELF section headers %d/%d: %s\n", |
| 425 |
i, ehdr.e_shnum, strerror(errno)); |
| 426 |
|
| 427 |
/* Set relocation section size to 0, effectively removing it. |
| 428 |
* This is necessary due to lack of support for relocations |
| 429 |
* in objcopy when creating 32bit elf from 64bit elf. |
| 430 |
*/ |
| 431 |
shdr.sh_size = 0; |
| 432 |
|
| 433 |
if (fseek(fp, sec->shdr_offset, SEEK_SET) < 0) |
| 434 |
die("Seek to %d failed: %s\n", |
| 435 |
sec->shdr_offset, strerror(errno)); |
| 436 |
|
| 437 |
if (fwrite(&shdr, sizeof(shdr), 1, fp) != 1) |
| 438 |
die("Cannot write ELF section headers %d/%d: %s\n", |
| 439 |
i, ehdr.e_shnum, strerror(errno)); |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
static void add_reloc(struct relocs *r, uint32_t offset, unsigned type) |
| 444 |
{ |
| 445 |
/* Relocation representation in binary table: |
| 446 |
* |76543210|76543210|76543210|76543210| |
| 447 |
* | Type | offset from _text >> 2 | |
| 448 |
*/ |
| 449 |
offset >>= 2; |
| 450 |
if (offset > 0x00FFFFFF) |
| 451 |
die("Kernel image exceeds maximum size for relocation!\n"); |
| 452 |
|
| 453 |
offset = (offset & 0x00FFFFFF) | ((type & 0xFF) << 24); |
| 454 |
|
| 455 |
if (r->count == r->size) { |
| 456 |
unsigned long newsize = r->size + 50000; |
| 457 |
void *mem = realloc(r->offset, newsize * sizeof(r->offset[0])); |
| 458 |
|
| 459 |
if (!mem) |
| 460 |
die("realloc failed\n"); |
| 461 |
|
| 462 |
r->offset = mem; |
| 463 |
r->size = newsize; |
| 464 |
} |
| 465 |
r->offset[r->count++] = offset; |
| 466 |
} |
| 467 |
|
| 468 |
static void walk_relocs(int (*process)(struct section *sec, Elf_Rel *rel, |
| 469 |
Elf_Sym *sym, const char *symname)) |
| 470 |
{ |
| 471 |
int i; |
| 472 |
struct section *extab_sec = sec_lookup("__ex_table"); |
| 473 |
int extab_index = extab_sec ? extab_sec - secs : -1; |
| 474 |
|
| 475 |
/* Walk through the relocations */ |
| 476 |
for (i = 0; i < ehdr.e_shnum; i++) { |
| 477 |
char *sym_strtab; |
| 478 |
Elf_Sym *sh_symtab; |
| 479 |
struct section *sec_applies, *sec_symtab; |
| 480 |
int j; |
| 481 |
struct section *sec = &secs[i]; |
| 482 |
|
| 483 |
if (sec->shdr.sh_type != SHT_REL_TYPE) |
| 484 |
continue; |
| 485 |
|
| 486 |
if (sec->shdr.sh_info == extab_index) |
| 487 |
continue; |
| 488 |
|
| 489 |
sec_symtab = sec->link; |
| 490 |
sec_applies = &secs[sec->shdr.sh_info]; |
| 491 |
if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) |
| 492 |
continue; |
| 493 |
|
| 494 |
sh_symtab = sec_symtab->symtab; |
| 495 |
sym_strtab = sec_symtab->link->strtab; |
| 496 |
for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Rel); j++) { |
| 497 |
Elf_Rel *rel = &sec->reltab[j]; |
| 498 |
Elf_Sym *sym = &sh_symtab[ELF_R_SYM(rel->r_info)]; |
| 499 |
const char *symname = sym_name(sym_strtab, sym); |
| 500 |
|
| 501 |
process(sec, rel, sym, symname); |
| 502 |
} |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
static int do_reloc(struct section *sec, Elf_Rel *rel, Elf_Sym *sym, |
| 507 |
const char *symname) |
| 508 |
{ |
| 509 |
unsigned r_type = ELF_R_TYPE(rel->r_info); |
| 510 |
unsigned bind = ELF_ST_BIND(sym->st_info); |
| 511 |
|
| 512 |
if ((bind == STB_WEAK) && (sym->st_value == 0)) { |
| 513 |
/* Don't relocate weak symbols without a target */ |
| 514 |
return 0; |
| 515 |
} |
| 516 |
|
| 517 |
if (regex_skip_reloc(symname)) |
| 518 |
return 0; |
| 519 |
|
| 520 |
switch (r_type) { |
| 521 |
case R_MIPS_NONE: |
| 522 |
case R_MIPS_REL32: |
| 523 |
case R_MIPS_PC16: |
| 524 |
case R_MIPS_PC21_S2: |
| 525 |
case R_MIPS_PC26_S2: |
| 526 |
case R_MIPS_PC32: |
| 527 |
/* |
| 528 |
* NONE can be ignored and PC relative relocations don't |
| 529 |
* need to be adjusted. |
| 530 |
*/ |
| 531 |
case R_MIPS_HIGHEST: |
| 532 |
case R_MIPS_HIGHER: |
| 533 |
/* We support relocating within the same 4Gb segment only, |
| 534 |
* thus leaving the top 32bits unchanged |
| 535 |
*/ |
| 536 |
case R_MIPS_LO16: |
| 537 |
/* We support relocating by 64k jumps only |
| 538 |
* thus leaving the bottom 16bits unchanged |
| 539 |
*/ |
| 540 |
break; |
| 541 |
|
| 542 |
case R_MIPS_64: |
| 543 |
case R_MIPS_32: |
| 544 |
case R_MIPS_26: |
| 545 |
case R_MIPS_HI16: |
| 546 |
add_reloc(&relocs, rel->r_offset, r_type); |
| 547 |
break; |
| 548 |
|
| 549 |
default: |
| 550 |
die("Unsupported relocation type: %s (%d)\n", |
| 551 |
rel_type(r_type), r_type); |
| 552 |
break; |
| 553 |
} |
| 554 |
|
| 555 |
return 0; |
| 556 |
} |
| 557 |
|
| 558 |
static int write_reloc_as_bin(uint32_t v, FILE *f) |
| 559 |
{ |
| 560 |
unsigned char buf[4]; |
| 561 |
|
| 562 |
v = cpu_to_elf32(v); |
| 563 |
|
| 564 |
memcpy(buf, &v, sizeof(uint32_t)); |
| 565 |
return fwrite(buf, 1, 4, f); |
| 566 |
} |
| 567 |
|
| 568 |
static int write_reloc_as_text(uint32_t v, FILE *f) |
| 569 |
{ |
| 570 |
int res; |
| 571 |
|
| 572 |
res = fprintf(f, "\t.long 0x%08"PRIx32"\n", v); |
| 573 |
if (res < 0) |
| 574 |
return res; |
| 575 |
else |
| 576 |
return sizeof(uint32_t); |
| 577 |
} |
| 578 |
|
| 579 |
static void emit_relocs(int as_text, int as_bin, FILE *outf) |
| 580 |
{ |
| 581 |
int i; |
| 582 |
int (*write_reloc)(uint32_t, FILE *) = write_reloc_as_bin; |
| 583 |
int size = 0; |
| 584 |
int size_reserved; |
| 585 |
struct section *sec_reloc; |
| 586 |
|
| 587 |
sec_reloc = sec_lookup(".data.reloc"); |
| 588 |
if (!sec_reloc) |
| 589 |
die("Could not find relocation section\n"); |
| 590 |
|
| 591 |
size_reserved = sec_reloc->shdr.sh_size; |
| 592 |
|
| 593 |
/* Collect up the relocations */ |
| 594 |
walk_relocs(do_reloc); |
| 595 |
|
| 596 |
/* Print the relocations */ |
| 597 |
if (as_text) { |
| 598 |
/* Print the relocations in a form suitable that |
| 599 |
* gas will like. |
| 600 |
*/ |
| 601 |
printf(".section \".data.reloc\",\"a\"\n"); |
| 602 |
printf(".balign 4\n"); |
| 603 |
/* Output text to stdout */ |
| 604 |
write_reloc = write_reloc_as_text; |
| 605 |
outf = stdout; |
| 606 |
} else if (as_bin) { |
| 607 |
/* Output raw binary to stdout */ |
| 608 |
outf = stdout; |
| 609 |
} else { |
| 610 |
/* Seek to offset of the relocation section. |
| 611 |
* Each relocation is then written into the |
| 612 |
* vmlinux kernel image. |
| 613 |
*/ |
| 614 |
if (fseek(outf, sec_reloc->shdr.sh_offset, SEEK_SET) < 0) { |
| 615 |
die("Seek to %d failed: %s\n", |
| 616 |
sec_reloc->shdr.sh_offset, strerror(errno)); |
| 617 |
} |
| 618 |
} |
| 619 |
|
| 620 |
for (i = 0; i < relocs.count; i++) |
| 621 |
size += write_reloc(relocs.offset[i], outf); |
| 622 |
|
| 623 |
/* Print a stop, but only if we've actually written some relocs */ |
| 624 |
if (size) |
| 625 |
size += write_reloc(0, outf); |
| 626 |
|
| 627 |
if (size > size_reserved) |
| 628 |
/* Die, but suggest a value for CONFIG_RELOCATION_TABLE_SIZE |
| 629 |
* which will fix this problem and allow a bit of headroom |
| 630 |
* if more kernel features are enabled |
| 631 |
*/ |
| 632 |
die("Relocations overflow available space!\n" \ |
| 633 |
"Please adjust CONFIG_RELOCATION_TABLE_SIZE " \ |
| 634 |
"to at least 0x%08x\n", (size + 0x1000) & ~0xFFF); |
| 635 |
} |
| 636 |
|
| 637 |
/* |
| 638 |
* As an aid to debugging problems with different linkers |
| 639 |
* print summary information about the relocs. |
| 640 |
* Since different linkers tend to emit the sections in |
| 641 |
* different orders we use the section names in the output. |
| 642 |
*/ |
| 643 |
static int do_reloc_info(struct section *sec, Elf_Rel *rel, ElfW(Sym) *sym, |
| 644 |
const char *symname) |
| 645 |
{ |
| 646 |
printf("%16s 0x%08x %16s %40s %16s\n", |
| 647 |
sec_name(sec->shdr.sh_info), |
| 648 |
(unsigned int)rel->r_offset, |
| 649 |
rel_type(ELF_R_TYPE(rel->r_info)), |
| 650 |
symname, |
| 651 |
sec_name(sym->st_shndx)); |
| 652 |
return 0; |
| 653 |
} |
| 654 |
|
| 655 |
static void print_reloc_info(void) |
| 656 |
{ |
| 657 |
printf("%16s %10s %16s %40s %16s\n", |
| 658 |
"reloc section", |
| 659 |
"offset", |
| 660 |
"reloc type", |
| 661 |
"symbol", |
| 662 |
"symbol section"); |
| 663 |
walk_relocs(do_reloc_info); |
| 664 |
} |
| 665 |
|
| 666 |
#if ELF_BITS == 64 |
| 667 |
# define process process_64 |
| 668 |
#else |
| 669 |
# define process process_32 |
| 670 |
#endif |
| 671 |
|
| 672 |
void process(FILE *fp, int as_text, int as_bin, |
| 673 |
int show_reloc_info, int keep_relocs) |
| 674 |
{ |
| 675 |
regex_init(); |
| 676 |
read_ehdr(fp); |
| 677 |
read_shdrs(fp); |
| 678 |
read_strtabs(fp); |
| 679 |
read_symtabs(fp); |
| 680 |
read_relocs(fp); |
| 681 |
if (show_reloc_info) { |
| 682 |
print_reloc_info(); |
| 683 |
return; |
| 684 |
} |
| 685 |
emit_relocs(as_text, as_bin, fp); |
| 686 |
if (!keep_relocs) |
| 687 |
remove_relocs(fp); |
| 688 |
} |