relocs.c 27.1 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2
/* This is included from relocs_32/64.c */
3

4 5 6 7
#define ElfW(type)		_ElfW(ELF_BITS, type)
#define _ElfW(bits, type)	__ElfW(bits, type)
#define __ElfW(bits, type)	Elf##bits##_##type

8
#define Elf_Addr		ElfW(Addr)
9 10 11 12 13
#define Elf_Ehdr		ElfW(Ehdr)
#define Elf_Phdr		ElfW(Phdr)
#define Elf_Shdr		ElfW(Shdr)
#define Elf_Sym			ElfW(Sym)

14 15 16
static Elf_Ehdr		ehdr;
static unsigned long	shnum;
static unsigned int	shstrndx;
17 18 19 20 21 22 23 24 25

struct relocs {
	uint32_t	*offset;
	unsigned long	count;
	unsigned long	size;
};

static struct relocs relocs16;
static struct relocs relocs32;
26 27
#if ELF_BITS == 64
static struct relocs relocs32neg;
28
static struct relocs relocs64;
29
#endif
30

31
struct section {
32
	Elf_Shdr       shdr;
33
	struct section *link;
34 35
	Elf_Sym        *symtab;
	Elf_Rel        *reltab;
36 37 38 39
	char           *strtab;
};
static struct section *secs;

40
static const char * const sym_regex_kernel[S_NSYMTYPES] = {
41 42 43 44 45 46
/*
 * Following symbols have been audited. There values are constant and do
 * not change if bzImage is loaded at a different physical address than
 * the address for which it has been compiled. Don't warn user about
 * absolute relocations present w.r.t these symbols.
 */
47
	[S_ABS] =
48 49 50
	"^(xen_irq_disable_direct_reloc$|"
	"xen_save_fl_direct_reloc$|"
	"VDSO|"
51
	"__crc_)",
52

53 54 55 56
/*
 * These symbols are known to be relative, even if the linker marks them
 * as absolute (typically defined outside any section in the linker script.)
 */
57
	[S_REL] =
58 59 60 61
	"^(__init_(begin|end)|"
	"__x86_cpu_dev_(start|end)|"
	"(__parainstructions|__alt_instructions)(|_end)|"
	"(__iommu_table|__apicdrivers|__smp_locks)(|_end)|"
62 63 64 65 66 67 68 69 70 71
	"__(start|end)_pci_.*|"
	"__(start|end)_builtin_fw|"
	"__(start|stop)___ksymtab(|_gpl|_unused|_unused_gpl|_gpl_future)|"
	"__(start|stop)___kcrctab(|_gpl|_unused|_unused_gpl|_gpl_future)|"
	"__(start|stop)___param|"
	"__(start|stop)___modver|"
	"__(start|stop)___bug_table|"
	"__tracedata_(start|end)|"
	"__(start|stop)_notes|"
	"__end_rodata|"
72
	"__end_rodata_aligned|"
73
	"__initramfs_start|"
74
	"(jiffies|jiffies_64)|"
75
#if ELF_BITS == 64
76 77 78 79
	"__per_cpu_load|"
	"init_per_cpu__.*|"
	"__end_rodata_hpage_align|"
#endif
80
	"__vvar_page|"
81
	"_end)$"
82 83 84 85
};


static const char * const sym_regex_realmode[S_NSYMTYPES] = {
86 87 88 89 90 91 92
/*
 * These symbols are known to be relative, even if the linker marks them
 * as absolute (typically defined outside any section in the linker script.)
 */
	[S_REL] =
	"^pa_",

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
/*
 * These are 16-bit segment symbols when compiling 16-bit code.
 */
	[S_SEG] =
	"^real_mode_seg$",

/*
 * These are offsets belonging to segments, as opposed to linear addresses,
 * when compiling 16-bit code.
 */
	[S_LIN] =
	"^pa_",
};

static const char * const *sym_regex;

static regex_t sym_regex_c[S_NSYMTYPES];
static int is_reloc(enum symtype type, const char *sym_name)
111
{
112 113
	return sym_regex[type] &&
		!regexec(&sym_regex_c[type], sym_name, 0, NULL, 0);
114
}
115

116
static void regex_init(int use_real_mode)
117 118 119
{
        char errbuf[128];
        int err;
120
	int i;
121

122 123 124 125 126 127 128 129 130 131 132 133 134
	if (use_real_mode)
		sym_regex = sym_regex_realmode;
	else
		sym_regex = sym_regex_kernel;

	for (i = 0; i < S_NSYMTYPES; i++) {
		if (!sym_regex[i])
			continue;

		err = regcomp(&sym_regex_c[i], sym_regex[i],
			      REG_EXTENDED|REG_NOSUB);

		if (err) {
135
			regerror(err, &sym_regex_c[i], errbuf, sizeof(errbuf));
136 137
			die("%s", errbuf);
		}
138
        }
139 140
}

141 142 143 144 145 146 147 148 149 150 151 152 153 154
static const char *sym_type(unsigned type)
{
	static const char *type_name[] = {
#define SYM_TYPE(X) [X] = #X
		SYM_TYPE(STT_NOTYPE),
		SYM_TYPE(STT_OBJECT),
		SYM_TYPE(STT_FUNC),
		SYM_TYPE(STT_SECTION),
		SYM_TYPE(STT_FILE),
		SYM_TYPE(STT_COMMON),
		SYM_TYPE(STT_TLS),
#undef SYM_TYPE
	};
	const char *name = "unknown sym type name";
155
	if (type < ARRAY_SIZE(type_name)) {
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
		name = type_name[type];
	}
	return name;
}

static const char *sym_bind(unsigned bind)
{
	static const char *bind_name[] = {
#define SYM_BIND(X) [X] = #X
		SYM_BIND(STB_LOCAL),
		SYM_BIND(STB_GLOBAL),
		SYM_BIND(STB_WEAK),
#undef SYM_BIND
	};
	const char *name = "unknown sym bind name";
171
	if (bind < ARRAY_SIZE(bind_name)) {
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
		name = bind_name[bind];
	}
	return name;
}

static const char *sym_visibility(unsigned visibility)
{
	static const char *visibility_name[] = {
#define SYM_VISIBILITY(X) [X] = #X
		SYM_VISIBILITY(STV_DEFAULT),
		SYM_VISIBILITY(STV_INTERNAL),
		SYM_VISIBILITY(STV_HIDDEN),
		SYM_VISIBILITY(STV_PROTECTED),
#undef SYM_VISIBILITY
	};
	const char *name = "unknown sym visibility name";
188
	if (visibility < ARRAY_SIZE(visibility_name)) {
189 190 191 192 193 194 195 196 197
		name = visibility_name[visibility];
	}
	return name;
}

static const char *rel_type(unsigned type)
{
	static const char *type_name[] = {
#define REL_TYPE(X) [X] = #X
198
#if ELF_BITS == 64
199 200
		REL_TYPE(R_X86_64_NONE),
		REL_TYPE(R_X86_64_64),
201
		REL_TYPE(R_X86_64_PC64),
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
		REL_TYPE(R_X86_64_PC32),
		REL_TYPE(R_X86_64_GOT32),
		REL_TYPE(R_X86_64_PLT32),
		REL_TYPE(R_X86_64_COPY),
		REL_TYPE(R_X86_64_GLOB_DAT),
		REL_TYPE(R_X86_64_JUMP_SLOT),
		REL_TYPE(R_X86_64_RELATIVE),
		REL_TYPE(R_X86_64_GOTPCREL),
		REL_TYPE(R_X86_64_32),
		REL_TYPE(R_X86_64_32S),
		REL_TYPE(R_X86_64_16),
		REL_TYPE(R_X86_64_PC16),
		REL_TYPE(R_X86_64_8),
		REL_TYPE(R_X86_64_PC8),
#else
217 218 219 220 221 222 223 224 225 226 227
		REL_TYPE(R_386_NONE),
		REL_TYPE(R_386_32),
		REL_TYPE(R_386_PC32),
		REL_TYPE(R_386_GOT32),
		REL_TYPE(R_386_PLT32),
		REL_TYPE(R_386_COPY),
		REL_TYPE(R_386_GLOB_DAT),
		REL_TYPE(R_386_JMP_SLOT),
		REL_TYPE(R_386_RELATIVE),
		REL_TYPE(R_386_GOTOFF),
		REL_TYPE(R_386_GOTPC),
228 229 230 231
		REL_TYPE(R_386_8),
		REL_TYPE(R_386_PC8),
		REL_TYPE(R_386_16),
		REL_TYPE(R_386_PC16),
232
#endif
233 234 235
#undef REL_TYPE
	};
	const char *name = "unknown type rel type name";
236
	if (type < ARRAY_SIZE(type_name) && type_name[type]) {
237 238 239 240 241 242 243 244 245
		name = type_name[type];
	}
	return name;
}

static const char *sec_name(unsigned shndx)
{
	const char *sec_strtab;
	const char *name;
246
	sec_strtab = secs[shstrndx].strtab;
247
	name = "<noname>";
248
	if (shndx < shnum) {
249
		name = sec_strtab + secs[shndx].shdr.sh_name;
250 251 252 253 254 255 256 257 258 259
	}
	else if (shndx == SHN_ABS) {
		name = "ABSOLUTE";
	}
	else if (shndx == SHN_COMMON) {
		name = "COMMON";
	}
	return name;
}

260
static const char *sym_name(const char *sym_strtab, Elf_Sym *sym)
261 262 263 264 265 266 267
{
	const char *name;
	name = "<noname>";
	if (sym->st_name) {
		name = sym_strtab + sym->st_name;
	}
	else {
268
		name = sec_name(sym->st_shndx);
269 270 271 272
	}
	return name;
}

273 274 275
static Elf_Sym *sym_lookup(const char *symname)
{
	int i;
276
	for (i = 0; i < shnum; i++) {
277 278 279 280 281
		struct section *sec = &secs[i];
		long nsyms;
		char *strtab;
		Elf_Sym *symtab;
		Elf_Sym *sym;
282

283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
		if (sec->shdr.sh_type != SHT_SYMTAB)
			continue;

		nsyms = sec->shdr.sh_size/sizeof(Elf_Sym);
		symtab = sec->symtab;
		strtab = sec->link->strtab;

		for (sym = symtab; --nsyms >= 0; sym++) {
			if (!sym->st_name)
				continue;
			if (strcmp(symname, strtab + sym->st_name) == 0)
				return sym;
		}
	}
	return 0;
}
299

300
#if BYTE_ORDER == LITTLE_ENDIAN
301 302
#define le16_to_cpu(val) (val)
#define le32_to_cpu(val) (val)
303
#define le64_to_cpu(val) (val)
304
#endif
305
#if BYTE_ORDER == BIG_ENDIAN
306 307
#define le16_to_cpu(val) bswap_16(val)
#define le32_to_cpu(val) bswap_32(val)
308
#define le64_to_cpu(val) bswap_64(val)
309 310 311 312 313 314 315 316 317 318 319 320
#endif

static uint16_t elf16_to_cpu(uint16_t val)
{
	return le16_to_cpu(val);
}

static uint32_t elf32_to_cpu(uint32_t val)
{
	return le32_to_cpu(val);
}

321 322
#define elf_half_to_cpu(x)	elf16_to_cpu(x)
#define elf_word_to_cpu(x)	elf32_to_cpu(x)
323

324
#if ELF_BITS == 64
325 326 327 328 329 330 331 332
static uint64_t elf64_to_cpu(uint64_t val)
{
        return le64_to_cpu(val);
}
#define elf_addr_to_cpu(x)	elf64_to_cpu(x)
#define elf_off_to_cpu(x)	elf64_to_cpu(x)
#define elf_xword_to_cpu(x)	elf64_to_cpu(x)
#else
333 334 335
#define elf_addr_to_cpu(x)	elf32_to_cpu(x)
#define elf_off_to_cpu(x)	elf32_to_cpu(x)
#define elf_xword_to_cpu(x)	elf32_to_cpu(x)
336
#endif
337

338 339 340 341 342 343
static void read_ehdr(FILE *fp)
{
	if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1) {
		die("Cannot read ELF header: %s\n",
			strerror(errno));
	}
344
	if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0) {
345 346
		die("No ELF magic\n");
	}
347 348
	if (ehdr.e_ident[EI_CLASS] != ELF_CLASS) {
		die("Not a %d bit executable\n", ELF_BITS);
349 350 351 352 353 354 355 356
	}
	if (ehdr.e_ident[EI_DATA] != ELFDATA2LSB) {
		die("Not a LSB ELF executable\n");
	}
	if (ehdr.e_ident[EI_VERSION] != EV_CURRENT) {
		die("Unknown ELF version\n");
	}
	/* Convert the fields to native endian */
357 358 359 360 361 362 363 364 365 366 367 368 369
	ehdr.e_type      = elf_half_to_cpu(ehdr.e_type);
	ehdr.e_machine   = elf_half_to_cpu(ehdr.e_machine);
	ehdr.e_version   = elf_word_to_cpu(ehdr.e_version);
	ehdr.e_entry     = elf_addr_to_cpu(ehdr.e_entry);
	ehdr.e_phoff     = elf_off_to_cpu(ehdr.e_phoff);
	ehdr.e_shoff     = elf_off_to_cpu(ehdr.e_shoff);
	ehdr.e_flags     = elf_word_to_cpu(ehdr.e_flags);
	ehdr.e_ehsize    = elf_half_to_cpu(ehdr.e_ehsize);
	ehdr.e_phentsize = elf_half_to_cpu(ehdr.e_phentsize);
	ehdr.e_phnum     = elf_half_to_cpu(ehdr.e_phnum);
	ehdr.e_shentsize = elf_half_to_cpu(ehdr.e_shentsize);
	ehdr.e_shnum     = elf_half_to_cpu(ehdr.e_shnum);
	ehdr.e_shstrndx  = elf_half_to_cpu(ehdr.e_shstrndx);
370

371 372 373 374
	shnum = ehdr.e_shnum;
	shstrndx = ehdr.e_shstrndx;

	if ((ehdr.e_type != ET_EXEC) && (ehdr.e_type != ET_DYN))
375
		die("Unsupported ELF header type\n");
376
	if (ehdr.e_machine != ELF_MACHINE)
377
		die("Not for %s\n", ELF_MACHINE_NAME);
378
	if (ehdr.e_version != EV_CURRENT)
379
		die("Unknown ELF version\n");
380
	if (ehdr.e_ehsize != sizeof(Elf_Ehdr))
381
		die("Bad Elf header size\n");
382
	if (ehdr.e_phentsize != sizeof(Elf_Phdr))
383
		die("Bad program header entry\n");
384
	if (ehdr.e_shentsize != sizeof(Elf_Shdr))
385
		die("Bad section header entry\n");
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401


	if (shnum == SHN_UNDEF || shstrndx == SHN_XINDEX) {
		Elf_Shdr shdr;

		if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0)
			die("Seek to %d failed: %s\n", ehdr.e_shoff, strerror(errno));

		if (fread(&shdr, sizeof(shdr), 1, fp) != 1)
			die("Cannot read initial ELF section header: %s\n", strerror(errno));

		if (shnum == SHN_UNDEF)
			shnum = elf_xword_to_cpu(shdr.sh_size);

		if (shstrndx == SHN_XINDEX)
			shstrndx = elf_word_to_cpu(shdr.sh_link);
402
	}
403 404

	if (shstrndx >= shnum)
405 406 407 408 409 410
		die("String table index out of bounds\n");
}

static void read_shdrs(FILE *fp)
{
	int i;
411
	Elf_Shdr shdr;
412

413
	secs = calloc(shnum, sizeof(struct section));
414 415
	if (!secs) {
		die("Unable to allocate %d section headers\n",
416
		    shnum);
417 418 419 420 421
	}
	if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0) {
		die("Seek to %d failed: %s\n",
			ehdr.e_shoff, strerror(errno));
	}
422
	for (i = 0; i < shnum; i++) {
423
		struct section *sec = &secs[i];
424
		if (fread(&shdr, sizeof(shdr), 1, fp) != 1)
425
			die("Cannot read ELF section headers %d/%d: %s\n",
426
			    i, shnum, strerror(errno));
427 428 429 430 431 432 433 434 435 436
		sec->shdr.sh_name      = elf_word_to_cpu(shdr.sh_name);
		sec->shdr.sh_type      = elf_word_to_cpu(shdr.sh_type);
		sec->shdr.sh_flags     = elf_xword_to_cpu(shdr.sh_flags);
		sec->shdr.sh_addr      = elf_addr_to_cpu(shdr.sh_addr);
		sec->shdr.sh_offset    = elf_off_to_cpu(shdr.sh_offset);
		sec->shdr.sh_size      = elf_xword_to_cpu(shdr.sh_size);
		sec->shdr.sh_link      = elf_word_to_cpu(shdr.sh_link);
		sec->shdr.sh_info      = elf_word_to_cpu(shdr.sh_info);
		sec->shdr.sh_addralign = elf_xword_to_cpu(shdr.sh_addralign);
		sec->shdr.sh_entsize   = elf_xword_to_cpu(shdr.sh_entsize);
437
		if (sec->shdr.sh_link < shnum)
438
			sec->link = &secs[sec->shdr.sh_link];
439 440 441 442 443 444 445
	}

}

static void read_strtabs(FILE *fp)
{
	int i;
446
	for (i = 0; i < shnum; i++) {
447 448
		struct section *sec = &secs[i];
		if (sec->shdr.sh_type != SHT_STRTAB) {
449 450
			continue;
		}
451 452
		sec->strtab = malloc(sec->shdr.sh_size);
		if (!sec->strtab) {
453
			die("malloc of %d bytes for strtab failed\n",
454
				sec->shdr.sh_size);
455
		}
456
		if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
457
			die("Seek to %d failed: %s\n",
458
				sec->shdr.sh_offset, strerror(errno));
459
		}
460 461
		if (fread(sec->strtab, 1, sec->shdr.sh_size, fp)
		    != sec->shdr.sh_size) {
462 463 464 465 466 467 468 469 470
			die("Cannot read symbol table: %s\n",
				strerror(errno));
		}
	}
}

static void read_symtabs(FILE *fp)
{
	int i,j;
471
	for (i = 0; i < shnum; i++) {
472 473
		struct section *sec = &secs[i];
		if (sec->shdr.sh_type != SHT_SYMTAB) {
474 475
			continue;
		}
476 477
		sec->symtab = malloc(sec->shdr.sh_size);
		if (!sec->symtab) {
478
			die("malloc of %d bytes for symtab failed\n",
479
				sec->shdr.sh_size);
480
		}
481
		if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
482
			die("Seek to %d failed: %s\n",
483
				sec->shdr.sh_offset, strerror(errno));
484
		}
485 486
		if (fread(sec->symtab, 1, sec->shdr.sh_size, fp)
		    != sec->shdr.sh_size) {
487 488 489
			die("Cannot read symbol table: %s\n",
				strerror(errno));
		}
490 491 492 493 494 495
		for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Sym); j++) {
			Elf_Sym *sym = &sec->symtab[j];
			sym->st_name  = elf_word_to_cpu(sym->st_name);
			sym->st_value = elf_addr_to_cpu(sym->st_value);
			sym->st_size  = elf_xword_to_cpu(sym->st_size);
			sym->st_shndx = elf_half_to_cpu(sym->st_shndx);
496 497 498 499 500 501 502 503
		}
	}
}


static void read_relocs(FILE *fp)
{
	int i,j;
504
	for (i = 0; i < shnum; i++) {
505
		struct section *sec = &secs[i];
506
		if (sec->shdr.sh_type != SHT_REL_TYPE) {
507 508
			continue;
		}
509 510
		sec->reltab = malloc(sec->shdr.sh_size);
		if (!sec->reltab) {
511
			die("malloc of %d bytes for relocs failed\n",
512
				sec->shdr.sh_size);
513
		}
514
		if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
515
			die("Seek to %d failed: %s\n",
516
				sec->shdr.sh_offset, strerror(errno));
517
		}
518 519
		if (fread(sec->reltab, 1, sec->shdr.sh_size, fp)
		    != sec->shdr.sh_size) {
520 521 522
			die("Cannot read symbol table: %s\n",
				strerror(errno));
		}
523 524 525 526
		for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Rel); j++) {
			Elf_Rel *rel = &sec->reltab[j];
			rel->r_offset = elf_addr_to_cpu(rel->r_offset);
			rel->r_info   = elf_xword_to_cpu(rel->r_info);
527 528 529
#if (SHT_REL_TYPE == SHT_RELA)
			rel->r_addend = elf_xword_to_cpu(rel->r_addend);
#endif
530 531 532 533 534 535 536 537
		}
	}
}


static void print_absolute_symbols(void)
{
	int i;
538 539
	const char *format;

540
	if (ELF_BITS == 64)
541 542 543 544
		format = "%5d %016"PRIx64" %5"PRId64" %10s %10s %12s %s\n";
	else
		format = "%5d %08"PRIx32"  %5"PRId32" %10s %10s %12s %s\n";

545 546
	printf("Absolute symbols\n");
	printf(" Num:    Value Size  Type       Bind        Visibility  Name\n");
547
	for (i = 0; i < shnum; i++) {
548
		struct section *sec = &secs[i];
549 550
		char *sym_strtab;
		int j;
551 552

		if (sec->shdr.sh_type != SHT_SYMTAB) {
553 554
			continue;
		}
555
		sym_strtab = sec->link->strtab;
556 557
		for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Sym); j++) {
			Elf_Sym *sym;
558
			const char *name;
559
			sym = &sec->symtab[j];
560 561 562 563
			name = sym_name(sym_strtab, sym);
			if (sym->st_shndx != SHN_ABS) {
				continue;
			}
564
			printf(format,
565
				j, sym->st_value, sym->st_size,
566 567 568
				sym_type(ELF_ST_TYPE(sym->st_info)),
				sym_bind(ELF_ST_BIND(sym->st_info)),
				sym_visibility(ELF_ST_VISIBILITY(sym->st_other)),
569 570 571 572 573 574 575 576
				name);
		}
	}
	printf("\n");
}

static void print_absolute_relocs(void)
{
577
	int i, printed = 0;
578 579
	const char *format;

580
	if (ELF_BITS == 64)
581 582 583
		format = "%016"PRIx64" %016"PRIx64" %10s %016"PRIx64"  %s\n";
	else
		format = "%08"PRIx32" %08"PRIx32" %10s %08"PRIx32"  %s\n";
584

585
	for (i = 0; i < shnum; i++) {
586 587
		struct section *sec = &secs[i];
		struct section *sec_applies, *sec_symtab;
588
		char *sym_strtab;
589
		Elf_Sym *sh_symtab;
590
		int j;
591
		if (sec->shdr.sh_type != SHT_REL_TYPE) {
592 593
			continue;
		}
594 595 596
		sec_symtab  = sec->link;
		sec_applies = &secs[sec->shdr.sh_info];
		if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) {
597 598
			continue;
		}
599 600
		sh_symtab  = sec_symtab->symtab;
		sym_strtab = sec_symtab->link->strtab;
601 602 603
		for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Rel); j++) {
			Elf_Rel *rel;
			Elf_Sym *sym;
604
			const char *name;
605
			rel = &sec->reltab[j];
606
			sym = &sh_symtab[ELF_R_SYM(rel->r_info)];
607 608 609 610
			name = sym_name(sym_strtab, sym);
			if (sym->st_shndx != SHN_ABS) {
				continue;
			}
611 612 613 614 615 616 617 618 619 620 621 622 623 624

			/* Absolute symbols are not relocated if bzImage is
			 * loaded at a non-compiled address. Display a warning
			 * to user at compile time about the absolute
			 * relocations present.
			 *
			 * User need to audit the code to make sure
			 * some symbols which should have been section
			 * relative have not become absolute because of some
			 * linker optimization or wrong programming usage.
			 *
			 * Before warning check if this absolute symbol
			 * relocation is harmless.
			 */
625
			if (is_reloc(S_ABS, name) || is_reloc(S_REL, name))
626 627 628 629 630 631 632 633 634 635
				continue;

			if (!printed) {
				printf("WARNING: Absolute relocations"
					" present\n");
				printf("Offset     Info     Type     Sym.Value "
					"Sym.Name\n");
				printed = 1;
			}

636
			printf(format,
637 638
				rel->r_offset,
				rel->r_info,
639
				rel_type(ELF_R_TYPE(rel->r_info)),
640 641 642 643
				sym->st_value,
				name);
		}
	}
644 645 646

	if (printed)
		printf("\n");
647 648
}

649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
static void add_reloc(struct relocs *r, uint32_t offset)
{
	if (r->count == r->size) {
		unsigned long newsize = r->size + 50000;
		void *mem = realloc(r->offset, newsize * sizeof(r->offset[0]));

		if (!mem)
			die("realloc of %ld entries for relocs failed\n",
                                newsize);
		r->offset = mem;
		r->size = newsize;
	}
	r->offset[r->count++] = offset;
}

static void walk_relocs(int (*process)(struct section *sec, Elf_Rel *rel,
			Elf_Sym *sym, const char *symname))
666 667 668
{
	int i;
	/* Walk through the relocations */
669
	for (i = 0; i < shnum; i++) {
670
		char *sym_strtab;
671
		Elf_Sym *sh_symtab;
672
		struct section *sec_applies, *sec_symtab;
673
		int j;
674 675
		struct section *sec = &secs[i];

676
		if (sec->shdr.sh_type != SHT_REL_TYPE) {
677 678
			continue;
		}
679 680 681
		sec_symtab  = sec->link;
		sec_applies = &secs[sec->shdr.sh_info];
		if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) {
682 683
			continue;
		}
684
		sh_symtab = sec_symtab->symtab;
685
		sym_strtab = sec_symtab->link->strtab;
686
		for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Rel); j++) {
687 688 689
			Elf_Rel *rel = &sec->reltab[j];
			Elf_Sym *sym = &sh_symtab[ELF_R_SYM(rel->r_info)];
			const char *symname = sym_name(sym_strtab, sym);
690

691 692 693 694 695
			process(sec, rel, sym, symname);
		}
	}
}

696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
/*
 * The .data..percpu section is a special case for x86_64 SMP kernels.
 * It is used to initialize the actual per_cpu areas and to provide
 * definitions for the per_cpu variables that correspond to their offsets
 * within the percpu area. Since the values of all of the symbols need
 * to be offsets from the start of the per_cpu area the virtual address
 * (sh_addr) of .data..percpu is 0 in SMP kernels.
 *
 * This means that:
 *
 *	Relocations that reference symbols in the per_cpu area do not
 *	need further relocation (since the value is an offset relative
 *	to the start of the per_cpu area that does not change).
 *
 *	Relocations that apply to the per_cpu area need to have their
 *	offset adjusted by by the value of __per_cpu_load to make them
 *	point to the correct place in the loaded image (because the
 *	virtual address of .data..percpu is 0).
 *
 * For non SMP kernels .data..percpu is linked as part of the normal
 * kernel data and does not require special treatment.
 *
 */
static int per_cpu_shndx	= -1;
720
static Elf_Addr per_cpu_load_addr;
721 722 723 724

static void percpu_init(void)
{
	int i;
725
	for (i = 0; i < shnum; i++) {
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
		ElfW(Sym) *sym;
		if (strcmp(sec_name(i), ".data..percpu"))
			continue;

		if (secs[i].shdr.sh_addr != 0)	/* non SMP kernel */
			return;

		sym = sym_lookup("__per_cpu_load");
		if (!sym)
			die("can't find __per_cpu_load\n");

		per_cpu_shndx = i;
		per_cpu_load_addr = sym->st_value;
		return;
	}
}

743 744
#if ELF_BITS == 64

745 746
/*
 * Check to see if a symbol lies in the .data..percpu section.
747 748 749 750 751 752 753
 *
 * The linker incorrectly associates some symbols with the
 * .data..percpu section so we also need to check the symbol
 * name to make sure that we classify the symbol correctly.
 *
 * The GNU linker incorrectly associates:
 *	__init_begin
754
 *	__per_cpu_load
755 756
 *
 * The "gold" linker incorrectly associates:
757
 *	init_per_cpu__fixed_percpu_data
758
 *	init_per_cpu__gdt_page
759 760 761 762
 */
static int is_percpu_sym(ElfW(Sym) *sym, const char *symname)
{
	return (sym->st_shndx == per_cpu_shndx) &&
763
		strcmp(symname, "__init_begin") &&
764
		strcmp(symname, "__per_cpu_load") &&
765
		strncmp(symname, "init_per_cpu_", 13);
766 767
}

768

769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
static int do_reloc64(struct section *sec, Elf_Rel *rel, ElfW(Sym) *sym,
		      const char *symname)
{
	unsigned r_type = ELF64_R_TYPE(rel->r_info);
	ElfW(Addr) offset = rel->r_offset;
	int shn_abs = (sym->st_shndx == SHN_ABS) && !is_reloc(S_REL, symname);

	if (sym->st_shndx == SHN_UNDEF)
		return 0;

	/*
	 * Adjust the offset if this reloc applies to the percpu section.
	 */
	if (sec->shdr.sh_info == per_cpu_shndx)
		offset += per_cpu_load_addr;

	switch (r_type) {
	case R_X86_64_NONE:
787 788 789
		/* NONE can be ignored. */
		break;

790
	case R_X86_64_PC32:
791
	case R_X86_64_PLT32:
792
		/*
793 794
		 * PC relative relocations don't need to be adjusted unless
		 * referencing a percpu symbol.
795 796
		 *
		 * NB: R_X86_64_PLT32 can be treated as R_X86_64_PC32.
797
		 */
798 799
		if (is_percpu_sym(sym, symname))
			add_reloc(&relocs32neg, offset);
800 801
		break;

802 803 804 805 806 807 808 809 810
	case R_X86_64_PC64:
		/*
		 * Only used by jump labels
		 */
		if (is_percpu_sym(sym, symname))
			die("Invalid R_X86_64_PC64 relocation against per-CPU symbol %s\n",
			    symname);
		break;

811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
	case R_X86_64_32:
	case R_X86_64_32S:
	case R_X86_64_64:
		/*
		 * References to the percpu area don't need to be adjusted.
		 */
		if (is_percpu_sym(sym, symname))
			break;

		if (shn_abs) {
			/*
			 * Whitelisted absolute symbols do not require
			 * relocation.
			 */
			if (is_reloc(S_ABS, symname))
				break;

			die("Invalid absolute %s relocation: %s\n",
			    rel_type(r_type), symname);
			break;
		}

		/*
		 * Relocation offsets for 64 bit kernels are output
		 * as 32 bits and sign extended back to 64 bits when
		 * the relocations are processed.
		 * Make sure that the offset will fit.
		 */
		if ((int32_t)offset != (int64_t)offset)
			die("Relocation offset doesn't fit in 32 bits\n");

		if (r_type == R_X86_64_64)
			add_reloc(&relocs64, offset);
		else
			add_reloc(&relocs32, offset);
		break;

	default:
		die("Unsupported relocation type: %s (%d)\n",
		    rel_type(r_type), r_type);
		break;
	}

	return 0;
}

857
#else
858 859 860

static int do_reloc32(struct section *sec, Elf_Rel *rel, Elf_Sym *sym,
		      const char *symname)
861 862 863 864 865 866 867 868 869
{
	unsigned r_type = ELF32_R_TYPE(rel->r_info);
	int shn_abs = (sym->st_shndx == SHN_ABS) && !is_reloc(S_REL, symname);

	switch (r_type) {
	case R_386_NONE:
	case R_386_PC32:
	case R_386_PC16:
	case R_386_PC8:
870
	case R_386_PLT32:
871
		/*
872 873 874
		 * NONE can be ignored and PC relative relocations don't need
		 * to be adjusted. Because sym must be defined, R_386_PLT32 can
		 * be treated the same way as R_386_PC32.
875 876 877 878 879 880 881 882 883 884
		 */
		break;

	case R_386_32:
		if (shn_abs) {
			/*
			 * Whitelisted absolute symbols do not require
			 * relocation.
			 */
			if (is_reloc(S_ABS, symname))
885
				break;
886

887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
			die("Invalid absolute %s relocation: %s\n",
			    rel_type(r_type), symname);
			break;
		}

		add_reloc(&relocs32, rel->r_offset);
		break;

	default:
		die("Unsupported relocation type: %s (%d)\n",
		    rel_type(r_type), r_type);
		break;
	}

	return 0;
}

static int do_reloc_real(struct section *sec, Elf_Rel *rel, Elf_Sym *sym,
			 const char *symname)
{
	unsigned r_type = ELF32_R_TYPE(rel->r_info);
	int shn_abs = (sym->st_shndx == SHN_ABS) && !is_reloc(S_REL, symname);

	switch (r_type) {
	case R_386_NONE:
	case R_386_PC32:
	case R_386_PC16:
	case R_386_PC8:
915
	case R_386_PLT32:
916
		/*
917 918 919
		 * NONE can be ignored and PC relative relocations don't need
		 * to be adjusted. Because sym must be defined, R_386_PLT32 can
		 * be treated the same way as R_386_PC32.
920 921 922 923 924 925 926 927 928 929
		 */
		break;

	case R_386_16:
		if (shn_abs) {
			/*
			 * Whitelisted absolute symbols do not require
			 * relocation.
			 */
			if (is_reloc(S_ABS, symname))
930 931
				break;

932 933 934 935 936 937
			if (is_reloc(S_SEG, symname)) {
				add_reloc(&relocs16, rel->r_offset);
				break;
			}
		} else {
			if (!is_reloc(S_LIN, symname))
938
				break;
939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955
		}
		die("Invalid %s %s relocation: %s\n",
		    shn_abs ? "absolute" : "relative",
		    rel_type(r_type), symname);
		break;

	case R_386_32:
		if (shn_abs) {
			/*
			 * Whitelisted absolute symbols do not require
			 * relocation.
			 */
			if (is_reloc(S_ABS, symname))
				break;

			if (is_reloc(S_REL, symname)) {
				add_reloc(&relocs32, rel->r_offset);
956
				break;
957
			}
958 959 960 961
		} else {
			if (is_reloc(S_LIN, symname))
				add_reloc(&relocs32, rel->r_offset);
			break;
962
		}
963 964 965 966
		die("Invalid %s %s relocation: %s\n",
		    shn_abs ? "absolute" : "relative",
		    rel_type(r_type), symname);
		break;
967

968 969 970 971 972
	default:
		die("Unsupported relocation type: %s (%d)\n",
		    rel_type(r_type), r_type);
		break;
	}
973

974
	return 0;
975 976
}

977 978
#endif

979 980
static int cmp_relocs(const void *va, const void *vb)
{
981
	const uint32_t *a, *b;
982 983 984 985
	a = va; b = vb;
	return (*a == *b)? 0 : (*a > *b)? 1 : -1;
}

986 987 988 989 990 991
static void sort_relocs(struct relocs *r)
{
	qsort(r->offset, r->count, sizeof(r->offset[0]), cmp_relocs);
}

static int write32(uint32_t v, FILE *f)
992 993 994 995 996 997 998
{
	unsigned char buf[4];

	put_unaligned_le32(v, buf);
	return fwrite(buf, 1, 4, f) == 4 ? 0 : -1;
}

999 1000 1001 1002 1003
static int write32_as_text(uint32_t v, FILE *f)
{
	return fprintf(f, "\t.long 0x%08"PRIx32"\n", v) > 0 ? 0 : -1;
}

1004
static void emit_relocs(int as_text, int use_real_mode)
1005 1006
{
	int i;
1007
	int (*write_reloc)(uint32_t, FILE *) = write32;
1008 1009 1010
	int (*do_reloc)(struct section *sec, Elf_Rel *rel, Elf_Sym *sym,
			const char *symname);

1011 1012
#if ELF_BITS == 64
	if (!use_real_mode)
1013
		do_reloc = do_reloc64;
1014 1015 1016 1017
	else
		die("--realmode not valid for a 64-bit ELF file");
#else
	if (!use_real_mode)
1018 1019 1020
		do_reloc = do_reloc32;
	else
		do_reloc = do_reloc_real;
1021
#endif
1022

1023
	/* Collect up the relocations */
1024
	walk_relocs(do_reloc);
1025

1026
	if (relocs16.count && !use_real_mode)
1027
		die("Segment relocations found but --realmode not specified\n");
1028 1029

	/* Order the relocations for more efficient processing */
1030
	sort_relocs(&relocs32);
1031 1032
#if ELF_BITS == 64
	sort_relocs(&relocs32neg);
1033
	sort_relocs(&relocs64);
1034 1035
#else
	sort_relocs(&relocs16);
1036
#endif
1037 1038 1039 1040 1041 1042 1043 1044

	/* Print the relocations */
	if (as_text) {
		/* Print the relocations in a form suitable that
		 * gas will like.
		 */
		printf(".section \".data.reloc\",\"a\"\n");
		printf(".balign 4\n");
1045
		write_reloc = write32_as_text;
1046
	}
1047

1048 1049 1050 1051 1052 1053 1054 1055 1056
	if (use_real_mode) {
		write_reloc(relocs16.count, stdout);
		for (i = 0; i < relocs16.count; i++)
			write_reloc(relocs16.offset[i], stdout);

		write_reloc(relocs32.count, stdout);
		for (i = 0; i < relocs32.count; i++)
			write_reloc(relocs32.offset[i], stdout);
	} else {
1057 1058 1059
#if ELF_BITS == 64
		/* Print a stop */
		write_reloc(0, stdout);
1060

1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
		/* Now print each relocation */
		for (i = 0; i < relocs64.count; i++)
			write_reloc(relocs64.offset[i], stdout);

		/* Print a stop */
		write_reloc(0, stdout);

		/* Now print each inverse 32-bit relocation */
		for (i = 0; i < relocs32neg.count; i++)
			write_reloc(relocs32neg.offset[i], stdout);
#endif
1072

1073 1074 1075 1076 1077 1078
		/* Print a stop */
		write_reloc(0, stdout);

		/* Now print each relocation */
		for (i = 0; i < relocs32.count; i++)
			write_reloc(relocs32.offset[i], stdout);
1079 1080 1081
	}
}

1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
/*
 * As an aid to debugging problems with different linkers
 * print summary information about the relocs.
 * Since different linkers tend to emit the sections in
 * different orders we use the section names in the output.
 */
static int do_reloc_info(struct section *sec, Elf_Rel *rel, ElfW(Sym) *sym,
				const char *symname)
{
	printf("%s\t%s\t%s\t%s\n",
		sec_name(sec->shdr.sh_info),
		rel_type(ELF_R_TYPE(rel->r_info)),
		symname,
		sec_name(sym->st_shndx));
	return 0;
}

static void print_reloc_info(void)
{
	printf("reloc section\treloc type\tsymbol\tsymbol section\n");
	walk_relocs(do_reloc_info);
}

1105 1106 1107 1108 1109
#if ELF_BITS == 64
# define process process_64
#else
# define process process_32
#endif
1110

1111
void process(FILE *fp, int use_real_mode, int as_text,
1112 1113
	     int show_absolute_syms, int show_absolute_relocs,
	     int show_reloc_info)
1114
{
1115
	regex_init(use_real_mode);
1116 1117 1118 1119 1120
	read_ehdr(fp);
	read_shdrs(fp);
	read_strtabs(fp);
	read_symtabs(fp);
	read_relocs(fp);
1121
	if (ELF_BITS == 64)
1122
		percpu_init();
1123
	if (show_absolute_syms) {
1124
		print_absolute_symbols();
1125
		return;
1126 1127
	}
	if (show_absolute_relocs) {
1128
		print_absolute_relocs();
1129
		return;
1130
	}
1131 1132 1133 1134
	if (show_reloc_info) {
		print_reloc_info();
		return;
	}
1135
	emit_relocs(as_text, use_real_mode);
1136
}