Path: blob/master/arch/x86/boot/compressed/relocs.c
10820 views
#include <stdio.h>1#include <stdarg.h>2#include <stdlib.h>3#include <stdint.h>4#include <string.h>5#include <errno.h>6#include <unistd.h>7#include <elf.h>8#include <byteswap.h>9#define USE_BSD10#include <endian.h>11#include <regex.h>1213static void die(char *fmt, ...);1415#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))16static Elf32_Ehdr ehdr;17static unsigned long reloc_count, reloc_idx;18static unsigned long *relocs;1920struct section {21Elf32_Shdr shdr;22struct section *link;23Elf32_Sym *symtab;24Elf32_Rel *reltab;25char *strtab;26};27static struct section *secs;2829/*30* Following symbols have been audited. There values are constant and do31* not change if bzImage is loaded at a different physical address than32* the address for which it has been compiled. Don't warn user about33* absolute relocations present w.r.t these symbols.34*/35static const char abs_sym_regex[] =36"^(xen_irq_disable_direct_reloc$|"37"xen_save_fl_direct_reloc$|"38"VDSO|"39"__crc_)";40static regex_t abs_sym_regex_c;41static int is_abs_reloc(const char *sym_name)42{43return !regexec(&abs_sym_regex_c, sym_name, 0, NULL, 0);44}4546/*47* These symbols are known to be relative, even if the linker marks them48* as absolute (typically defined outside any section in the linker script.)49*/50static const char rel_sym_regex[] =51"^_end$";52static regex_t rel_sym_regex_c;53static int is_rel_reloc(const char *sym_name)54{55return !regexec(&rel_sym_regex_c, sym_name, 0, NULL, 0);56}5758static void regex_init(void)59{60char errbuf[128];61int err;6263err = regcomp(&abs_sym_regex_c, abs_sym_regex,64REG_EXTENDED|REG_NOSUB);65if (err) {66regerror(err, &abs_sym_regex_c, errbuf, sizeof errbuf);67die("%s", errbuf);68}6970err = regcomp(&rel_sym_regex_c, rel_sym_regex,71REG_EXTENDED|REG_NOSUB);72if (err) {73regerror(err, &rel_sym_regex_c, errbuf, sizeof errbuf);74die("%s", errbuf);75}76}7778static void die(char *fmt, ...)79{80va_list ap;81va_start(ap, fmt);82vfprintf(stderr, fmt, ap);83va_end(ap);84exit(1);85}8687static const char *sym_type(unsigned type)88{89static const char *type_name[] = {90#define SYM_TYPE(X) [X] = #X91SYM_TYPE(STT_NOTYPE),92SYM_TYPE(STT_OBJECT),93SYM_TYPE(STT_FUNC),94SYM_TYPE(STT_SECTION),95SYM_TYPE(STT_FILE),96SYM_TYPE(STT_COMMON),97SYM_TYPE(STT_TLS),98#undef SYM_TYPE99};100const char *name = "unknown sym type name";101if (type < ARRAY_SIZE(type_name)) {102name = type_name[type];103}104return name;105}106107static const char *sym_bind(unsigned bind)108{109static const char *bind_name[] = {110#define SYM_BIND(X) [X] = #X111SYM_BIND(STB_LOCAL),112SYM_BIND(STB_GLOBAL),113SYM_BIND(STB_WEAK),114#undef SYM_BIND115};116const char *name = "unknown sym bind name";117if (bind < ARRAY_SIZE(bind_name)) {118name = bind_name[bind];119}120return name;121}122123static const char *sym_visibility(unsigned visibility)124{125static const char *visibility_name[] = {126#define SYM_VISIBILITY(X) [X] = #X127SYM_VISIBILITY(STV_DEFAULT),128SYM_VISIBILITY(STV_INTERNAL),129SYM_VISIBILITY(STV_HIDDEN),130SYM_VISIBILITY(STV_PROTECTED),131#undef SYM_VISIBILITY132};133const char *name = "unknown sym visibility name";134if (visibility < ARRAY_SIZE(visibility_name)) {135name = visibility_name[visibility];136}137return name;138}139140static const char *rel_type(unsigned type)141{142static const char *type_name[] = {143#define REL_TYPE(X) [X] = #X144REL_TYPE(R_386_NONE),145REL_TYPE(R_386_32),146REL_TYPE(R_386_PC32),147REL_TYPE(R_386_GOT32),148REL_TYPE(R_386_PLT32),149REL_TYPE(R_386_COPY),150REL_TYPE(R_386_GLOB_DAT),151REL_TYPE(R_386_JMP_SLOT),152REL_TYPE(R_386_RELATIVE),153REL_TYPE(R_386_GOTOFF),154REL_TYPE(R_386_GOTPC),155#undef REL_TYPE156};157const char *name = "unknown type rel type name";158if (type < ARRAY_SIZE(type_name) && type_name[type]) {159name = type_name[type];160}161return name;162}163164static const char *sec_name(unsigned shndx)165{166const char *sec_strtab;167const char *name;168sec_strtab = secs[ehdr.e_shstrndx].strtab;169name = "<noname>";170if (shndx < ehdr.e_shnum) {171name = sec_strtab + secs[shndx].shdr.sh_name;172}173else if (shndx == SHN_ABS) {174name = "ABSOLUTE";175}176else if (shndx == SHN_COMMON) {177name = "COMMON";178}179return name;180}181182static const char *sym_name(const char *sym_strtab, Elf32_Sym *sym)183{184const char *name;185name = "<noname>";186if (sym->st_name) {187name = sym_strtab + sym->st_name;188}189else {190name = sec_name(secs[sym->st_shndx].shdr.sh_name);191}192return name;193}194195196197#if BYTE_ORDER == LITTLE_ENDIAN198#define le16_to_cpu(val) (val)199#define le32_to_cpu(val) (val)200#endif201#if BYTE_ORDER == BIG_ENDIAN202#define le16_to_cpu(val) bswap_16(val)203#define le32_to_cpu(val) bswap_32(val)204#endif205206static uint16_t elf16_to_cpu(uint16_t val)207{208return le16_to_cpu(val);209}210211static uint32_t elf32_to_cpu(uint32_t val)212{213return le32_to_cpu(val);214}215216static void read_ehdr(FILE *fp)217{218if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1) {219die("Cannot read ELF header: %s\n",220strerror(errno));221}222if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0) {223die("No ELF magic\n");224}225if (ehdr.e_ident[EI_CLASS] != ELFCLASS32) {226die("Not a 32 bit executable\n");227}228if (ehdr.e_ident[EI_DATA] != ELFDATA2LSB) {229die("Not a LSB ELF executable\n");230}231if (ehdr.e_ident[EI_VERSION] != EV_CURRENT) {232die("Unknown ELF version\n");233}234/* Convert the fields to native endian */235ehdr.e_type = elf16_to_cpu(ehdr.e_type);236ehdr.e_machine = elf16_to_cpu(ehdr.e_machine);237ehdr.e_version = elf32_to_cpu(ehdr.e_version);238ehdr.e_entry = elf32_to_cpu(ehdr.e_entry);239ehdr.e_phoff = elf32_to_cpu(ehdr.e_phoff);240ehdr.e_shoff = elf32_to_cpu(ehdr.e_shoff);241ehdr.e_flags = elf32_to_cpu(ehdr.e_flags);242ehdr.e_ehsize = elf16_to_cpu(ehdr.e_ehsize);243ehdr.e_phentsize = elf16_to_cpu(ehdr.e_phentsize);244ehdr.e_phnum = elf16_to_cpu(ehdr.e_phnum);245ehdr.e_shentsize = elf16_to_cpu(ehdr.e_shentsize);246ehdr.e_shnum = elf16_to_cpu(ehdr.e_shnum);247ehdr.e_shstrndx = elf16_to_cpu(ehdr.e_shstrndx);248249if ((ehdr.e_type != ET_EXEC) && (ehdr.e_type != ET_DYN)) {250die("Unsupported ELF header type\n");251}252if (ehdr.e_machine != EM_386) {253die("Not for x86\n");254}255if (ehdr.e_version != EV_CURRENT) {256die("Unknown ELF version\n");257}258if (ehdr.e_ehsize != sizeof(Elf32_Ehdr)) {259die("Bad Elf header size\n");260}261if (ehdr.e_phentsize != sizeof(Elf32_Phdr)) {262die("Bad program header entry\n");263}264if (ehdr.e_shentsize != sizeof(Elf32_Shdr)) {265die("Bad section header entry\n");266}267if (ehdr.e_shstrndx >= ehdr.e_shnum) {268die("String table index out of bounds\n");269}270}271272static void read_shdrs(FILE *fp)273{274int i;275Elf32_Shdr shdr;276277secs = calloc(ehdr.e_shnum, sizeof(struct section));278if (!secs) {279die("Unable to allocate %d section headers\n",280ehdr.e_shnum);281}282if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0) {283die("Seek to %d failed: %s\n",284ehdr.e_shoff, strerror(errno));285}286for (i = 0; i < ehdr.e_shnum; i++) {287struct section *sec = &secs[i];288if (fread(&shdr, sizeof shdr, 1, fp) != 1)289die("Cannot read ELF section headers %d/%d: %s\n",290i, ehdr.e_shnum, strerror(errno));291sec->shdr.sh_name = elf32_to_cpu(shdr.sh_name);292sec->shdr.sh_type = elf32_to_cpu(shdr.sh_type);293sec->shdr.sh_flags = elf32_to_cpu(shdr.sh_flags);294sec->shdr.sh_addr = elf32_to_cpu(shdr.sh_addr);295sec->shdr.sh_offset = elf32_to_cpu(shdr.sh_offset);296sec->shdr.sh_size = elf32_to_cpu(shdr.sh_size);297sec->shdr.sh_link = elf32_to_cpu(shdr.sh_link);298sec->shdr.sh_info = elf32_to_cpu(shdr.sh_info);299sec->shdr.sh_addralign = elf32_to_cpu(shdr.sh_addralign);300sec->shdr.sh_entsize = elf32_to_cpu(shdr.sh_entsize);301if (sec->shdr.sh_link < ehdr.e_shnum)302sec->link = &secs[sec->shdr.sh_link];303}304305}306307static void read_strtabs(FILE *fp)308{309int i;310for (i = 0; i < ehdr.e_shnum; i++) {311struct section *sec = &secs[i];312if (sec->shdr.sh_type != SHT_STRTAB) {313continue;314}315sec->strtab = malloc(sec->shdr.sh_size);316if (!sec->strtab) {317die("malloc of %d bytes for strtab failed\n",318sec->shdr.sh_size);319}320if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {321die("Seek to %d failed: %s\n",322sec->shdr.sh_offset, strerror(errno));323}324if (fread(sec->strtab, 1, sec->shdr.sh_size, fp)325!= sec->shdr.sh_size) {326die("Cannot read symbol table: %s\n",327strerror(errno));328}329}330}331332static void read_symtabs(FILE *fp)333{334int i,j;335for (i = 0; i < ehdr.e_shnum; i++) {336struct section *sec = &secs[i];337if (sec->shdr.sh_type != SHT_SYMTAB) {338continue;339}340sec->symtab = malloc(sec->shdr.sh_size);341if (!sec->symtab) {342die("malloc of %d bytes for symtab failed\n",343sec->shdr.sh_size);344}345if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {346die("Seek to %d failed: %s\n",347sec->shdr.sh_offset, strerror(errno));348}349if (fread(sec->symtab, 1, sec->shdr.sh_size, fp)350!= sec->shdr.sh_size) {351die("Cannot read symbol table: %s\n",352strerror(errno));353}354for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Sym); j++) {355Elf32_Sym *sym = &sec->symtab[j];356sym->st_name = elf32_to_cpu(sym->st_name);357sym->st_value = elf32_to_cpu(sym->st_value);358sym->st_size = elf32_to_cpu(sym->st_size);359sym->st_shndx = elf16_to_cpu(sym->st_shndx);360}361}362}363364365static void read_relocs(FILE *fp)366{367int i,j;368for (i = 0; i < ehdr.e_shnum; i++) {369struct section *sec = &secs[i];370if (sec->shdr.sh_type != SHT_REL) {371continue;372}373sec->reltab = malloc(sec->shdr.sh_size);374if (!sec->reltab) {375die("malloc of %d bytes for relocs failed\n",376sec->shdr.sh_size);377}378if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {379die("Seek to %d failed: %s\n",380sec->shdr.sh_offset, strerror(errno));381}382if (fread(sec->reltab, 1, sec->shdr.sh_size, fp)383!= sec->shdr.sh_size) {384die("Cannot read symbol table: %s\n",385strerror(errno));386}387for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Rel); j++) {388Elf32_Rel *rel = &sec->reltab[j];389rel->r_offset = elf32_to_cpu(rel->r_offset);390rel->r_info = elf32_to_cpu(rel->r_info);391}392}393}394395396static void print_absolute_symbols(void)397{398int i;399printf("Absolute symbols\n");400printf(" Num: Value Size Type Bind Visibility Name\n");401for (i = 0; i < ehdr.e_shnum; i++) {402struct section *sec = &secs[i];403char *sym_strtab;404Elf32_Sym *sh_symtab;405int j;406407if (sec->shdr.sh_type != SHT_SYMTAB) {408continue;409}410sh_symtab = sec->symtab;411sym_strtab = sec->link->strtab;412for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Sym); j++) {413Elf32_Sym *sym;414const char *name;415sym = &sec->symtab[j];416name = sym_name(sym_strtab, sym);417if (sym->st_shndx != SHN_ABS) {418continue;419}420printf("%5d %08x %5d %10s %10s %12s %s\n",421j, sym->st_value, sym->st_size,422sym_type(ELF32_ST_TYPE(sym->st_info)),423sym_bind(ELF32_ST_BIND(sym->st_info)),424sym_visibility(ELF32_ST_VISIBILITY(sym->st_other)),425name);426}427}428printf("\n");429}430431static void print_absolute_relocs(void)432{433int i, printed = 0;434435for (i = 0; i < ehdr.e_shnum; i++) {436struct section *sec = &secs[i];437struct section *sec_applies, *sec_symtab;438char *sym_strtab;439Elf32_Sym *sh_symtab;440int j;441if (sec->shdr.sh_type != SHT_REL) {442continue;443}444sec_symtab = sec->link;445sec_applies = &secs[sec->shdr.sh_info];446if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) {447continue;448}449sh_symtab = sec_symtab->symtab;450sym_strtab = sec_symtab->link->strtab;451for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Rel); j++) {452Elf32_Rel *rel;453Elf32_Sym *sym;454const char *name;455rel = &sec->reltab[j];456sym = &sh_symtab[ELF32_R_SYM(rel->r_info)];457name = sym_name(sym_strtab, sym);458if (sym->st_shndx != SHN_ABS) {459continue;460}461462/* Absolute symbols are not relocated if bzImage is463* loaded at a non-compiled address. Display a warning464* to user at compile time about the absolute465* relocations present.466*467* User need to audit the code to make sure468* some symbols which should have been section469* relative have not become absolute because of some470* linker optimization or wrong programming usage.471*472* Before warning check if this absolute symbol473* relocation is harmless.474*/475if (is_abs_reloc(name) || is_rel_reloc(name))476continue;477478if (!printed) {479printf("WARNING: Absolute relocations"480" present\n");481printf("Offset Info Type Sym.Value "482"Sym.Name\n");483printed = 1;484}485486printf("%08x %08x %10s %08x %s\n",487rel->r_offset,488rel->r_info,489rel_type(ELF32_R_TYPE(rel->r_info)),490sym->st_value,491name);492}493}494495if (printed)496printf("\n");497}498499static void walk_relocs(void (*visit)(Elf32_Rel *rel, Elf32_Sym *sym))500{501int i;502/* Walk through the relocations */503for (i = 0; i < ehdr.e_shnum; i++) {504char *sym_strtab;505Elf32_Sym *sh_symtab;506struct section *sec_applies, *sec_symtab;507int j;508struct section *sec = &secs[i];509510if (sec->shdr.sh_type != SHT_REL) {511continue;512}513sec_symtab = sec->link;514sec_applies = &secs[sec->shdr.sh_info];515if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) {516continue;517}518sh_symtab = sec_symtab->symtab;519sym_strtab = sec_symtab->link->strtab;520for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Rel); j++) {521Elf32_Rel *rel;522Elf32_Sym *sym;523unsigned r_type;524rel = &sec->reltab[j];525sym = &sh_symtab[ELF32_R_SYM(rel->r_info)];526r_type = ELF32_R_TYPE(rel->r_info);527/* Don't visit relocations to absolute symbols */528if (sym->st_shndx == SHN_ABS &&529!is_rel_reloc(sym_name(sym_strtab, sym))) {530continue;531}532switch (r_type) {533case R_386_NONE:534case R_386_PC32:535/*536* NONE can be ignored and and PC relative537* relocations don't need to be adjusted.538*/539break;540case R_386_32:541/* Visit relocations that need to be adjusted */542visit(rel, sym);543break;544default:545die("Unsupported relocation type: %s (%d)\n",546rel_type(r_type), r_type);547break;548}549}550}551}552553static void count_reloc(Elf32_Rel *rel, Elf32_Sym *sym)554{555reloc_count += 1;556}557558static void collect_reloc(Elf32_Rel *rel, Elf32_Sym *sym)559{560/* Remember the address that needs to be adjusted. */561relocs[reloc_idx++] = rel->r_offset;562}563564static int cmp_relocs(const void *va, const void *vb)565{566const unsigned long *a, *b;567a = va; b = vb;568return (*a == *b)? 0 : (*a > *b)? 1 : -1;569}570571static void emit_relocs(int as_text)572{573int i;574/* Count how many relocations I have and allocate space for them. */575reloc_count = 0;576walk_relocs(count_reloc);577relocs = malloc(reloc_count * sizeof(relocs[0]));578if (!relocs) {579die("malloc of %d entries for relocs failed\n",580reloc_count);581}582/* Collect up the relocations */583reloc_idx = 0;584walk_relocs(collect_reloc);585586/* Order the relocations for more efficient processing */587qsort(relocs, reloc_count, sizeof(relocs[0]), cmp_relocs);588589/* Print the relocations */590if (as_text) {591/* Print the relocations in a form suitable that592* gas will like.593*/594printf(".section \".data.reloc\",\"a\"\n");595printf(".balign 4\n");596for (i = 0; i < reloc_count; i++) {597printf("\t .long 0x%08lx\n", relocs[i]);598}599printf("\n");600}601else {602unsigned char buf[4];603/* Print a stop */604fwrite("\0\0\0\0", 4, 1, stdout);605/* Now print each relocation */606for (i = 0; i < reloc_count; i++) {607buf[0] = (relocs[i] >> 0) & 0xff;608buf[1] = (relocs[i] >> 8) & 0xff;609buf[2] = (relocs[i] >> 16) & 0xff;610buf[3] = (relocs[i] >> 24) & 0xff;611fwrite(buf, 4, 1, stdout);612}613}614}615616static void usage(void)617{618die("relocs [--abs-syms |--abs-relocs | --text] vmlinux\n");619}620621int main(int argc, char **argv)622{623int show_absolute_syms, show_absolute_relocs;624int as_text;625const char *fname;626FILE *fp;627int i;628629regex_init();630631show_absolute_syms = 0;632show_absolute_relocs = 0;633as_text = 0;634fname = NULL;635for (i = 1; i < argc; i++) {636char *arg = argv[i];637if (*arg == '-') {638if (strcmp(argv[1], "--abs-syms") == 0) {639show_absolute_syms = 1;640continue;641}642643if (strcmp(argv[1], "--abs-relocs") == 0) {644show_absolute_relocs = 1;645continue;646}647else if (strcmp(argv[1], "--text") == 0) {648as_text = 1;649continue;650}651}652else if (!fname) {653fname = arg;654continue;655}656usage();657}658if (!fname) {659usage();660}661fp = fopen(fname, "r");662if (!fp) {663die("Cannot open %s: %s\n",664fname, strerror(errno));665}666read_ehdr(fp);667read_shdrs(fp);668read_strtabs(fp);669read_symtabs(fp);670read_relocs(fp);671if (show_absolute_syms) {672print_absolute_symbols();673return 0;674}675if (show_absolute_relocs) {676print_absolute_relocs();677return 0;678}679emit_relocs(as_text);680return 0;681}682683684