relocs.c 13.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <elf.h>
#include <byteswap.h>
#define USE_BSD
#include <endian.h>

#define MAX_SHDRS 100
static Elf32_Ehdr ehdr;
static Elf32_Shdr shdr[MAX_SHDRS];
static Elf32_Sym  *symtab[MAX_SHDRS];
static Elf32_Rel  *reltab[MAX_SHDRS];
static char *strtab[MAX_SHDRS];
static unsigned long reloc_count, reloc_idx;
static unsigned long *relocs;

static void die(char *fmt, ...)
{
	va_list ap;
	va_start(ap, fmt);
	vfprintf(stderr, fmt, ap);
	va_end(ap);
	exit(1);
}

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";
	if (type < sizeof(type_name)/sizeof(type_name[0])) {
		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";
	if (bind < sizeof(bind_name)/sizeof(bind_name[0])) {
		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";
	if (visibility < sizeof(visibility_name)/sizeof(visibility_name[0])) {
		name = visibility_name[visibility];
	}
	return name;
}

static const char *rel_type(unsigned type)
{
	static const char *type_name[] = {
#define REL_TYPE(X) [X] = #X
		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),
#undef REL_TYPE
	};
	const char *name = "unknown type rel type name";
	if (type < sizeof(type_name)/sizeof(type_name[0])) {
		name = type_name[type];
	}
	return name;
}

static const char *sec_name(unsigned shndx)
{
	const char *sec_strtab;
	const char *name;
	sec_strtab = strtab[ehdr.e_shstrndx];
	name = "<noname>";
	if (shndx < ehdr.e_shnum) {
		name = sec_strtab + shdr[shndx].sh_name;
	}
	else if (shndx == SHN_ABS) {
		name = "ABSOLUTE";
	}
	else if (shndx == SHN_COMMON) {
		name = "COMMON";
	}
	return name;
}

static const char *sym_name(const char *sym_strtab, Elf32_Sym *sym)
{
	const char *name;
	name = "<noname>";
	if (sym->st_name) {
		name = sym_strtab + sym->st_name;
	}
	else {
		name = sec_name(shdr[sym->st_shndx].sh_name);
	}
	return name;
}



#if BYTE_ORDER == LITTLE_ENDIAN
#define le16_to_cpu(val) (val)
#define le32_to_cpu(val) (val)
#endif
#if BYTE_ORDER == BIG_ENDIAN
#define le16_to_cpu(val) bswap_16(val)
#define le32_to_cpu(val) bswap_32(val)
#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);
}

static void read_ehdr(FILE *fp)
{
	if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1) {
		die("Cannot read ELF header: %s\n",
			strerror(errno));
	}
	if (memcmp(ehdr.e_ident, ELFMAG, 4) != 0) {
		die("No ELF magic\n");
	}
	if (ehdr.e_ident[EI_CLASS] != ELFCLASS32) {
		die("Not a 32 bit executable\n");
	}
	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 */
	ehdr.e_type      = elf16_to_cpu(ehdr.e_type);
	ehdr.e_machine   = elf16_to_cpu(ehdr.e_machine);
	ehdr.e_version   = elf32_to_cpu(ehdr.e_version);
	ehdr.e_entry     = elf32_to_cpu(ehdr.e_entry);
	ehdr.e_phoff     = elf32_to_cpu(ehdr.e_phoff);
	ehdr.e_shoff     = elf32_to_cpu(ehdr.e_shoff);
	ehdr.e_flags     = elf32_to_cpu(ehdr.e_flags);
	ehdr.e_ehsize    = elf16_to_cpu(ehdr.e_ehsize);
	ehdr.e_phentsize = elf16_to_cpu(ehdr.e_phentsize);
	ehdr.e_phnum     = elf16_to_cpu(ehdr.e_phnum);
	ehdr.e_shentsize = elf16_to_cpu(ehdr.e_shentsize);
	ehdr.e_shnum     = elf16_to_cpu(ehdr.e_shnum);
	ehdr.e_shstrndx  = elf16_to_cpu(ehdr.e_shstrndx);

	if ((ehdr.e_type != ET_EXEC) && (ehdr.e_type != ET_DYN)) {
		die("Unsupported ELF header type\n");
	}
	if (ehdr.e_machine != EM_386) {
		die("Not for x86\n");
	}
	if (ehdr.e_version != EV_CURRENT) {
		die("Unknown ELF version\n");
	}
	if (ehdr.e_ehsize != sizeof(Elf32_Ehdr)) {
		die("Bad Elf header size\n");
	}
	if (ehdr.e_phentsize != sizeof(Elf32_Phdr)) {
		die("Bad program header entry\n");
	}
	if (ehdr.e_shentsize != sizeof(Elf32_Shdr)) {
		die("Bad section header entry\n");
	}
	if (ehdr.e_shstrndx >= ehdr.e_shnum) {
		die("String table index out of bounds\n");
	}
}

static void read_shdrs(FILE *fp)
{
	int i;
	if (ehdr.e_shnum > MAX_SHDRS) {
		die("%d section headers supported: %d\n",
			ehdr.e_shnum, MAX_SHDRS);
	}
	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[0]), ehdr.e_shnum, fp) != ehdr.e_shnum) {
		die("Cannot read ELF section headers: %s\n",
			strerror(errno));
	}
	for(i = 0; i < ehdr.e_shnum; i++) {
		shdr[i].sh_name      = elf32_to_cpu(shdr[i].sh_name);
		shdr[i].sh_type      = elf32_to_cpu(shdr[i].sh_type);
		shdr[i].sh_flags     = elf32_to_cpu(shdr[i].sh_flags);
		shdr[i].sh_addr      = elf32_to_cpu(shdr[i].sh_addr);
		shdr[i].sh_offset    = elf32_to_cpu(shdr[i].sh_offset);
		shdr[i].sh_size      = elf32_to_cpu(shdr[i].sh_size);
		shdr[i].sh_link      = elf32_to_cpu(shdr[i].sh_link);
		shdr[i].sh_info      = elf32_to_cpu(shdr[i].sh_info);
		shdr[i].sh_addralign = elf32_to_cpu(shdr[i].sh_addralign);
		shdr[i].sh_entsize   = elf32_to_cpu(shdr[i].sh_entsize);
	}

}

static void read_strtabs(FILE *fp)
{
	int i;
	for(i = 0; i < ehdr.e_shnum; i++) {
		if (shdr[i].sh_type != SHT_STRTAB) {
			continue;
		}
		strtab[i] = malloc(shdr[i].sh_size);
		if (!strtab[i]) {
			die("malloc of %d bytes for strtab failed\n",
				shdr[i].sh_size);
		}
		if (fseek(fp, shdr[i].sh_offset, SEEK_SET) < 0) {
			die("Seek to %d failed: %s\n",
				shdr[i].sh_offset, strerror(errno));
		}
		if (fread(strtab[i], 1, shdr[i].sh_size, fp) != shdr[i].sh_size) {
			die("Cannot read symbol table: %s\n",
				strerror(errno));
		}
	}
}

static void read_symtabs(FILE *fp)
{
	int i,j;
	for(i = 0; i < ehdr.e_shnum; i++) {
		if (shdr[i].sh_type != SHT_SYMTAB) {
			continue;
		}
		symtab[i] = malloc(shdr[i].sh_size);
		if (!symtab[i]) {
			die("malloc of %d bytes for symtab failed\n",
				shdr[i].sh_size);
		}
		if (fseek(fp, shdr[i].sh_offset, SEEK_SET) < 0) {
			die("Seek to %d failed: %s\n",
				shdr[i].sh_offset, strerror(errno));
		}
		if (fread(symtab[i], 1, shdr[i].sh_size, fp) != shdr[i].sh_size) {
			die("Cannot read symbol table: %s\n",
				strerror(errno));
		}
		for(j = 0; j < shdr[i].sh_size/sizeof(symtab[i][0]); j++) {
			symtab[i][j].st_name  = elf32_to_cpu(symtab[i][j].st_name);
			symtab[i][j].st_value = elf32_to_cpu(symtab[i][j].st_value);
			symtab[i][j].st_size  = elf32_to_cpu(symtab[i][j].st_size);
			symtab[i][j].st_shndx = elf16_to_cpu(symtab[i][j].st_shndx);
		}
	}
}


static void read_relocs(FILE *fp)
{
	int i,j;
	for(i = 0; i < ehdr.e_shnum; i++) {
		if (shdr[i].sh_type != SHT_REL) {
			continue;
		}
		reltab[i] = malloc(shdr[i].sh_size);
		if (!reltab[i]) {
			die("malloc of %d bytes for relocs failed\n",
				shdr[i].sh_size);
		}
		if (fseek(fp, shdr[i].sh_offset, SEEK_SET) < 0) {
			die("Seek to %d failed: %s\n",
				shdr[i].sh_offset, strerror(errno));
		}
		if (fread(reltab[i], 1, shdr[i].sh_size, fp) != shdr[i].sh_size) {
			die("Cannot read symbol table: %s\n",
				strerror(errno));
		}
		for(j = 0; j < shdr[i].sh_size/sizeof(reltab[0][0]); j++) {
			reltab[i][j].r_offset = elf32_to_cpu(reltab[i][j].r_offset);
			reltab[i][j].r_info   = elf32_to_cpu(reltab[i][j].r_info);
		}
	}
}


static void print_absolute_symbols(void)
{
	int i;
	printf("Absolute symbols\n");
	printf(" Num:    Value Size  Type       Bind        Visibility  Name\n");
	for(i = 0; i < ehdr.e_shnum; i++) {
		char *sym_strtab;
		Elf32_Sym *sh_symtab;
		int j;
		if (shdr[i].sh_type != SHT_SYMTAB) {
			continue;
		}
		sh_symtab = symtab[i];
		sym_strtab = strtab[shdr[i].sh_link];
		for(j = 0; j < shdr[i].sh_size/sizeof(symtab[0][0]); j++) {
			Elf32_Sym *sym;
			const char *name;
			sym = &symtab[i][j];
			name = sym_name(sym_strtab, sym);
			if (sym->st_shndx != SHN_ABS) {
				continue;
			}
			printf("%5d %08x %5d %10s %10s %12s %s\n",
				j, sym->st_value, sym->st_size,
				sym_type(ELF32_ST_TYPE(sym->st_info)),
				sym_bind(ELF32_ST_BIND(sym->st_info)),
				sym_visibility(ELF32_ST_VISIBILITY(sym->st_other)),
				name);
		}
	}
	printf("\n");
}

static void print_absolute_relocs(void)
{
	int i;
	printf("Absolute relocations\n");
	printf("Offset     Info     Type     Sym.Value Sym.Name\n");
	for(i = 0; i < ehdr.e_shnum; i++) {
		char *sym_strtab;
		Elf32_Sym *sh_symtab;
		unsigned sec_applies, sec_symtab;
		int j;
		if (shdr[i].sh_type != SHT_REL) {
			continue;
		}
		sec_symtab  = shdr[i].sh_link;
		sec_applies = shdr[i].sh_info;
		if (!(shdr[sec_applies].sh_flags & SHF_ALLOC)) {
			continue;
		}
		sh_symtab = symtab[sec_symtab];
		sym_strtab = strtab[shdr[sec_symtab].sh_link];
		for(j = 0; j < shdr[i].sh_size/sizeof(reltab[0][0]); j++) {
			Elf32_Rel *rel;
			Elf32_Sym *sym;
			const char *name;
			rel = &reltab[i][j];
			sym = &sh_symtab[ELF32_R_SYM(rel->r_info)];
			name = sym_name(sym_strtab, sym);
			if (sym->st_shndx != SHN_ABS) {
				continue;
			}
			printf("%08x %08x %10s %08x  %s\n",
				rel->r_offset,
				rel->r_info,
				rel_type(ELF32_R_TYPE(rel->r_info)),
				sym->st_value,
				name);
		}
	}
	printf("\n");
}

static void walk_relocs(void (*visit)(Elf32_Rel *rel, Elf32_Sym *sym))
{
	int i;
	/* Walk through the relocations */
	for(i = 0; i < ehdr.e_shnum; i++) {
		char *sym_strtab;
		Elf32_Sym *sh_symtab;
		unsigned sec_applies, sec_symtab;
		int j;
		if (shdr[i].sh_type != SHT_REL) {
			continue;
		}
		sec_symtab  = shdr[i].sh_link;
		sec_applies = shdr[i].sh_info;
		if (!(shdr[sec_applies].sh_flags & SHF_ALLOC)) {
			continue;
		}
		sh_symtab = symtab[sec_symtab];
		sym_strtab = strtab[shdr[sec_symtab].sh_link];
		for(j = 0; j < shdr[i].sh_size/sizeof(reltab[0][0]); j++) {
			Elf32_Rel *rel;
			Elf32_Sym *sym;
			unsigned r_type;
			rel = &reltab[i][j];
			sym = &sh_symtab[ELF32_R_SYM(rel->r_info)];
			r_type = ELF32_R_TYPE(rel->r_info);
			/* Don't visit relocations to absolute symbols */
			if (sym->st_shndx == SHN_ABS) {
				continue;
			}
			if (r_type == R_386_PC32) {
				/* PC relative relocations don't need to be adjusted */
			}
			else if (r_type == R_386_32) {
				/* Visit relocations that need to be adjusted */
				visit(rel, sym);
			}
			else {
				die("Unsupported relocation type: %d\n", r_type);
			}
		}
	}
}

static void count_reloc(Elf32_Rel *rel, Elf32_Sym *sym)
{
	reloc_count += 1;
}

static void collect_reloc(Elf32_Rel *rel, Elf32_Sym *sym)
{
	/* Remember the address that needs to be adjusted. */
	relocs[reloc_idx++] = rel->r_offset;
}

static int cmp_relocs(const void *va, const void *vb)
{
	const unsigned long *a, *b;
	a = va; b = vb;
	return (*a == *b)? 0 : (*a > *b)? 1 : -1;
}

static void emit_relocs(int as_text)
{
	int i;
	/* Count how many relocations I have and allocate space for them. */
	reloc_count = 0;
	walk_relocs(count_reloc);
	relocs = malloc(reloc_count * sizeof(relocs[0]));
	if (!relocs) {
		die("malloc of %d entries for relocs failed\n",
			reloc_count);
	}
	/* Collect up the relocations */
	reloc_idx = 0;
	walk_relocs(collect_reloc);

	/* Order the relocations for more efficient processing */
	qsort(relocs, reloc_count, sizeof(relocs[0]), cmp_relocs);

	/* 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");
		for(i = 0; i < reloc_count; i++) {
			printf("\t .long 0x%08lx\n", relocs[i]);
		}
		printf("\n");
	}
	else {
		unsigned char buf[4];
		buf[0] = buf[1] = buf[2] = buf[3] = 0;
		/* Print a stop */
		printf("%c%c%c%c", buf[0], buf[1], buf[2], buf[3]);
		/* Now print each relocation */
		for(i = 0; i < reloc_count; i++) {
			buf[0] = (relocs[i] >>  0) & 0xff;
			buf[1] = (relocs[i] >>  8) & 0xff;
			buf[2] = (relocs[i] >> 16) & 0xff;
			buf[3] = (relocs[i] >> 24) & 0xff;
			printf("%c%c%c%c", buf[0], buf[1], buf[2], buf[3]);
		}
	}
}

static void usage(void)
{
	die("i386_reloc [--abs | --text] vmlinux\n");
}

int main(int argc, char **argv)
{
	int show_absolute;
	int as_text;
	const char *fname;
	FILE *fp;
	int i;

	show_absolute = 0;
	as_text = 0;
	fname = NULL;
	for(i = 1; i < argc; i++) {
		char *arg = argv[i];
		if (*arg == '-') {
			if (strcmp(argv[1], "--abs") == 0) {
				show_absolute = 1;
				continue;
			}
			else if (strcmp(argv[1], "--text") == 0) {
				as_text = 1;
				continue;
			}
		}
		else if (!fname) {
			fname = arg;
			continue;
		}
		usage();
	}
	if (!fname) {
		usage();
	}
	fp = fopen(fname, "r");
	if (!fp) {
		die("Cannot open %s: %s\n",
			fname, strerror(errno));
	}
	read_ehdr(fp);
	read_shdrs(fp);
	read_strtabs(fp);
	read_symtabs(fp);
	read_relocs(fp);
	if (show_absolute) {
		print_absolute_symbols();
		print_absolute_relocs();
		return 0;
	}
	emit_relocs(as_text);
	return 0;
}