Commit 766c5803 authored by Matt Redfearn's avatar Matt Redfearn Committed by Ralf Baechle

MIPS: tools: Add relocs tool

This tool is based on the x86/boot/tools/relocs tool.

It parses the relocations present in the vmlinux elf file, building a
table of relocations that will be necessary to run the kernel from an
address other than its link address. This table is inserted into the
vmlinux elf, in the .data.relocs section. The table is subsequently used
by the code in arch/mips/kernel/relocate.c (added later) to relocate the
kernel.

The tool, by default, also marks all relocation sections as 0 length.
This is due to objcopy currently being unable to handle copying the
relocations between 64 and 32 bit elf files as is done when building a
64 bit kernel.
Signed-off-by: default avatarMatt Redfearn <matt.redfearn@imgtec.com>
Cc: linux-mips@linux-mips.org
Cc: kernel-hardening@lists.openwall.com
Cc: linux-kernel@vger.kernel.org
Patchwork: https://patchwork.linux-mips.org/patch/12981/Signed-off-by: default avatarRalf Baechle <ralf@linux-mips.org>
parent cb4253aa
hostprogs-y += relocs
relocs-objs += relocs_32.o
relocs-objs += relocs_64.o
relocs-objs += relocs_main.o
PHONY += relocs
relocs: $(obj)/relocs
@:
This diff is collapsed.
#ifndef RELOCS_H
#define RELOCS_H
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <elf.h>
#include <byteswap.h>
#define USE_BSD
#include <endian.h>
#include <regex.h>
void die(char *fmt, ...);
/*
* Introduced for MIPSr6
*/
#ifndef R_MIPS_PC21_S2
#define R_MIPS_PC21_S2 60
#endif
#ifndef R_MIPS_PC26_S2
#define R_MIPS_PC26_S2 61
#endif
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
enum symtype {
S_ABS,
S_REL,
S_SEG,
S_LIN,
S_NSYMTYPES
};
void process_32(FILE *fp, int as_text, int as_bin,
int show_reloc_info, int keep_relocs);
void process_64(FILE *fp, int as_text, int as_bin,
int show_reloc_info, int keep_relocs);
#endif /* RELOCS_H */
#include "relocs.h"
#define ELF_BITS 32
#define ELF_MACHINE EM_MIPS
#define ELF_MACHINE_NAME "MIPS"
#define SHT_REL_TYPE SHT_REL
#define Elf_Rel ElfW(Rel)
#define ELF_CLASS ELFCLASS32
#define ELF_R_SYM(val) ELF32_R_SYM(val)
#define ELF_R_TYPE(val) ELF32_R_TYPE(val)
#define ELF_ST_TYPE(o) ELF32_ST_TYPE(o)
#define ELF_ST_BIND(o) ELF32_ST_BIND(o)
#define ELF_ST_VISIBILITY(o) ELF32_ST_VISIBILITY(o)
#include "relocs.c"
#include "relocs.h"
#define ELF_BITS 64
#define ELF_MACHINE EM_MIPS
#define ELF_MACHINE_NAME "MIPS64"
#define SHT_REL_TYPE SHT_RELA
#define Elf_Rel Elf64_Rela
typedef uint8_t Elf64_Byte;
typedef struct {
Elf64_Word r_sym; /* Symbol index. */
Elf64_Byte r_ssym; /* Special symbol. */
Elf64_Byte r_type3; /* Third relocation. */
Elf64_Byte r_type2; /* Second relocation. */
Elf64_Byte r_type; /* First relocation. */
} Elf64_Mips_Rela;
#define ELF_CLASS ELFCLASS64
#define ELF_R_SYM(val) (((Elf64_Mips_Rela *)(&val))->r_sym)
#define ELF_R_TYPE(val) (((Elf64_Mips_Rela *)(&val))->r_type)
#define ELF_ST_TYPE(o) ELF64_ST_TYPE(o)
#define ELF_ST_BIND(o) ELF64_ST_BIND(o)
#define ELF_ST_VISIBILITY(o) ELF64_ST_VISIBILITY(o)
#include "relocs.c"
#include <stdio.h>
#include <stdint.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <endian.h>
#include <elf.h>
#include "relocs.h"
void die(char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
exit(1);
}
static void usage(void)
{
die("relocs [--reloc-info|--text|--bin|--keep] vmlinux\n");
}
int main(int argc, char **argv)
{
int show_reloc_info, as_text, as_bin, keep_relocs;
const char *fname;
FILE *fp;
int i;
unsigned char e_ident[EI_NIDENT];
show_reloc_info = 0;
as_text = 0;
as_bin = 0;
keep_relocs = 0;
fname = NULL;
for (i = 1; i < argc; i++) {
char *arg = argv[i];
if (*arg == '-') {
if (strcmp(arg, "--reloc-info") == 0) {
show_reloc_info = 1;
continue;
}
if (strcmp(arg, "--text") == 0) {
as_text = 1;
continue;
}
if (strcmp(arg, "--bin") == 0) {
as_bin = 1;
continue;
}
if (strcmp(arg, "--keep") == 0) {
keep_relocs = 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));
if (fread(&e_ident, 1, EI_NIDENT, fp) != EI_NIDENT)
die("Cannot read %s: %s", fname, strerror(errno));
rewind(fp);
if (e_ident[EI_CLASS] == ELFCLASS64)
process_64(fp, as_text, as_bin, show_reloc_info, keep_relocs);
else
process_32(fp, as_text, as_bin, show_reloc_info, keep_relocs);
fclose(fp);
return 0;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment