mirror of
https://github.com/torvalds/linux.git
synced 2026-10-23 15:32:04 +02:00
Extend elf-parse to be able to read the symbol table of vmlinux in kallsyms. This requires the ability to open ELF files in read-only mode, so provide elf_map_ro() to do so. It also requires accessors for section flags and symbol bindings, so provide these via shdr_flags() and sym_bind(). It's useful to be able to open an ELF file, map it, and then have easy access to its section headers, names and symbol table, so introduce struct elf_file to store this. In order to obtain this data also provide elf_open_ro() and elf_close(). Also, check for the file being an ELF file first in elf_parse(). This is the logical thing to check for first, but additionally prevents kallsyms from having to check this it self. Assisted-by: LLM Reviewed-by: Kees Cook <kees@kernel.org> Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org> Link: https://patch.msgid.link/20260923-build-speedup-v4-7-73128809a4a4@kernel.org Signed-off-by: Nathan Chancellor <nathan@kernel.org>
360 lines
8.2 KiB
C
360 lines
8.2 KiB
C
/* SPDX-License-Identifier: GPL-2.0-only */
|
|
#ifndef _SCRIPTS_ELF_PARSE_H
|
|
#define _SCRIPTS_ELF_PARSE_H
|
|
|
|
#include <elf.h>
|
|
|
|
#include <tools/be_byteshift.h>
|
|
#include <tools/le_byteshift.h>
|
|
|
|
typedef union {
|
|
Elf32_Ehdr e32;
|
|
Elf64_Ehdr e64;
|
|
} Elf_Ehdr;
|
|
|
|
typedef union {
|
|
Elf32_Shdr e32;
|
|
Elf64_Shdr e64;
|
|
} Elf_Shdr;
|
|
|
|
typedef union {
|
|
Elf32_Sym e32;
|
|
Elf64_Sym e64;
|
|
} Elf_Sym;
|
|
|
|
typedef union {
|
|
Elf32_Rela e32;
|
|
Elf64_Rela e64;
|
|
} Elf_Rela;
|
|
|
|
struct elf_funcs {
|
|
int (*compare_extable)(const void *a, const void *b);
|
|
uint64_t (*ehdr_shoff)(Elf_Ehdr *ehdr);
|
|
uint16_t (*ehdr_shstrndx)(Elf_Ehdr *ehdr);
|
|
uint16_t (*ehdr_shentsize)(Elf_Ehdr *ehdr);
|
|
uint16_t (*ehdr_shnum)(Elf_Ehdr *ehdr);
|
|
uint64_t (*shdr_addr)(Elf_Shdr *shdr);
|
|
uint64_t (*shdr_offset)(Elf_Shdr *shdr);
|
|
uint64_t (*shdr_size)(Elf_Shdr *shdr);
|
|
uint64_t (*shdr_entsize)(Elf_Shdr *shdr);
|
|
uint64_t (*shdr_flags)(Elf_Shdr *shdr);
|
|
uint32_t (*shdr_link)(Elf_Shdr *shdr);
|
|
uint32_t (*shdr_name)(Elf_Shdr *shdr);
|
|
uint32_t (*shdr_type)(Elf_Shdr *shdr);
|
|
uint8_t (*sym_type)(Elf_Sym *sym);
|
|
uint8_t (*sym_bind)(Elf_Sym *sym);
|
|
uint32_t (*sym_name)(Elf_Sym *sym);
|
|
uint64_t (*sym_value)(Elf_Sym *sym);
|
|
uint16_t (*sym_shndx)(Elf_Sym *sym);
|
|
uint64_t (*rela_offset)(Elf_Rela *rela);
|
|
uint64_t (*rela_info)(Elf_Rela *rela);
|
|
uint64_t (*rela_addend)(Elf_Rela *rela);
|
|
void (*rela_write_addend)(Elf_Rela *rela, uint64_t val);
|
|
uint32_t (*r)(const uint32_t *);
|
|
uint16_t (*r2)(const uint16_t *);
|
|
uint64_t (*r8)(const uint64_t *);
|
|
void (*w)(uint32_t, uint32_t *);
|
|
void (*w8)(uint64_t, uint64_t *);
|
|
};
|
|
|
|
extern struct elf_funcs elf_parser;
|
|
|
|
static inline uint64_t ehdr64_shoff(Elf_Ehdr *ehdr)
|
|
{
|
|
return elf_parser.r8(&ehdr->e64.e_shoff);
|
|
}
|
|
|
|
static inline uint64_t ehdr32_shoff(Elf_Ehdr *ehdr)
|
|
{
|
|
return elf_parser.r(&ehdr->e32.e_shoff);
|
|
}
|
|
|
|
static inline uint64_t ehdr_shoff(Elf_Ehdr *ehdr)
|
|
{
|
|
return elf_parser.ehdr_shoff(ehdr);
|
|
}
|
|
|
|
#define EHDR_HALF(fn_name) \
|
|
static inline uint16_t ehdr64_##fn_name(Elf_Ehdr *ehdr) \
|
|
{ \
|
|
return elf_parser.r2(&ehdr->e64.e_##fn_name); \
|
|
} \
|
|
\
|
|
static inline uint16_t ehdr32_##fn_name(Elf_Ehdr *ehdr) \
|
|
{ \
|
|
return elf_parser.r2(&ehdr->e32.e_##fn_name); \
|
|
} \
|
|
\
|
|
static inline uint16_t ehdr_##fn_name(Elf_Ehdr *ehdr) \
|
|
{ \
|
|
return elf_parser.ehdr_##fn_name(ehdr); \
|
|
}
|
|
|
|
EHDR_HALF(shentsize)
|
|
EHDR_HALF(shstrndx)
|
|
EHDR_HALF(shnum)
|
|
|
|
#define SHDR_WORD(fn_name) \
|
|
static inline uint32_t shdr64_##fn_name(Elf_Shdr *shdr) \
|
|
{ \
|
|
return elf_parser.r(&shdr->e64.sh_##fn_name); \
|
|
} \
|
|
\
|
|
static inline uint32_t shdr32_##fn_name(Elf_Shdr *shdr) \
|
|
{ \
|
|
return elf_parser.r(&shdr->e32.sh_##fn_name); \
|
|
} \
|
|
\
|
|
static inline uint32_t shdr_##fn_name(Elf_Shdr *shdr) \
|
|
{ \
|
|
return elf_parser.shdr_##fn_name(shdr); \
|
|
}
|
|
|
|
#define SHDR_ADDR(fn_name) \
|
|
static inline uint64_t shdr64_##fn_name(Elf_Shdr *shdr) \
|
|
{ \
|
|
return elf_parser.r8(&shdr->e64.sh_##fn_name); \
|
|
} \
|
|
\
|
|
static inline uint64_t shdr32_##fn_name(Elf_Shdr *shdr) \
|
|
{ \
|
|
return elf_parser.r(&shdr->e32.sh_##fn_name); \
|
|
} \
|
|
\
|
|
static inline uint64_t shdr_##fn_name(Elf_Shdr *shdr) \
|
|
{ \
|
|
return elf_parser.shdr_##fn_name(shdr); \
|
|
}
|
|
|
|
#define SHDR_WORD(fn_name) \
|
|
static inline uint32_t shdr64_##fn_name(Elf_Shdr *shdr) \
|
|
{ \
|
|
return elf_parser.r(&shdr->e64.sh_##fn_name); \
|
|
} \
|
|
\
|
|
static inline uint32_t shdr32_##fn_name(Elf_Shdr *shdr) \
|
|
{ \
|
|
return elf_parser.r(&shdr->e32.sh_##fn_name); \
|
|
} \
|
|
static inline uint32_t shdr_##fn_name(Elf_Shdr *shdr) \
|
|
{ \
|
|
return elf_parser.shdr_##fn_name(shdr); \
|
|
}
|
|
|
|
SHDR_ADDR(addr)
|
|
SHDR_ADDR(offset)
|
|
SHDR_ADDR(size)
|
|
SHDR_ADDR(entsize)
|
|
SHDR_ADDR(flags)
|
|
|
|
SHDR_WORD(link)
|
|
SHDR_WORD(name)
|
|
SHDR_WORD(type)
|
|
|
|
#define SYM_ADDR(fn_name) \
|
|
static inline uint64_t sym64_##fn_name(Elf_Sym *sym) \
|
|
{ \
|
|
return elf_parser.r8(&sym->e64.st_##fn_name); \
|
|
} \
|
|
\
|
|
static inline uint64_t sym32_##fn_name(Elf_Sym *sym) \
|
|
{ \
|
|
return elf_parser.r(&sym->e32.st_##fn_name); \
|
|
} \
|
|
\
|
|
static inline uint64_t sym_##fn_name(Elf_Sym *sym) \
|
|
{ \
|
|
return elf_parser.sym_##fn_name(sym); \
|
|
}
|
|
|
|
#define SYM_WORD(fn_name) \
|
|
static inline uint32_t sym64_##fn_name(Elf_Sym *sym) \
|
|
{ \
|
|
return elf_parser.r(&sym->e64.st_##fn_name); \
|
|
} \
|
|
\
|
|
static inline uint32_t sym32_##fn_name(Elf_Sym *sym) \
|
|
{ \
|
|
return elf_parser.r(&sym->e32.st_##fn_name); \
|
|
} \
|
|
\
|
|
static inline uint32_t sym_##fn_name(Elf_Sym *sym) \
|
|
{ \
|
|
return elf_parser.sym_##fn_name(sym); \
|
|
}
|
|
|
|
#define SYM_HALF(fn_name) \
|
|
static inline uint16_t sym64_##fn_name(Elf_Sym *sym) \
|
|
{ \
|
|
return elf_parser.r2(&sym->e64.st_##fn_name); \
|
|
} \
|
|
\
|
|
static inline uint16_t sym32_##fn_name(Elf_Sym *sym) \
|
|
{ \
|
|
return elf_parser.r2(&sym->e32.st_##fn_name); \
|
|
} \
|
|
\
|
|
static inline uint16_t sym_##fn_name(Elf_Sym *sym) \
|
|
{ \
|
|
return elf_parser.sym_##fn_name(sym); \
|
|
}
|
|
|
|
static inline uint8_t sym64_type(Elf_Sym *sym)
|
|
{
|
|
return ELF64_ST_TYPE(sym->e64.st_info);
|
|
}
|
|
|
|
static inline uint8_t sym32_type(Elf_Sym *sym)
|
|
{
|
|
return ELF32_ST_TYPE(sym->e32.st_info);
|
|
}
|
|
|
|
static inline uint8_t sym_type(Elf_Sym *sym)
|
|
{
|
|
return elf_parser.sym_type(sym);
|
|
}
|
|
|
|
static inline uint8_t sym64_bind(Elf_Sym *sym)
|
|
{
|
|
return ELF64_ST_BIND(sym->e64.st_info);
|
|
}
|
|
|
|
static inline uint8_t sym32_bind(Elf_Sym *sym)
|
|
{
|
|
return ELF32_ST_BIND(sym->e32.st_info);
|
|
}
|
|
|
|
static inline uint8_t sym_bind(Elf_Sym *sym)
|
|
{
|
|
return elf_parser.sym_bind(sym);
|
|
}
|
|
|
|
SYM_ADDR(value)
|
|
SYM_WORD(name)
|
|
SYM_HALF(shndx)
|
|
|
|
#define __maybe_unused __attribute__((__unused__))
|
|
|
|
#define RELA_ADDR(fn_name) \
|
|
static inline uint64_t rela64_##fn_name(Elf_Rela *rela) \
|
|
{ \
|
|
return elf_parser.r8((uint64_t *)&rela->e64.r_##fn_name); \
|
|
} \
|
|
\
|
|
static inline uint64_t rela32_##fn_name(Elf_Rela *rela) \
|
|
{ \
|
|
return elf_parser.r((uint32_t *)&rela->e32.r_##fn_name); \
|
|
} \
|
|
\
|
|
static inline uint64_t __maybe_unused rela_##fn_name(Elf_Rela *rela) \
|
|
{ \
|
|
return elf_parser.rela_##fn_name(rela); \
|
|
}
|
|
|
|
RELA_ADDR(offset)
|
|
RELA_ADDR(info)
|
|
RELA_ADDR(addend)
|
|
|
|
static inline void rela64_write_addend(Elf_Rela *rela, uint64_t val)
|
|
{
|
|
elf_parser.w8(val, (uint64_t *)&rela->e64.r_addend);
|
|
}
|
|
|
|
static inline void rela32_write_addend(Elf_Rela *rela, uint64_t val)
|
|
{
|
|
elf_parser.w(val, (uint32_t *)&rela->e32.r_addend);
|
|
}
|
|
|
|
static inline uint32_t rbe(const uint32_t *x)
|
|
{
|
|
return get_unaligned_be32(x);
|
|
}
|
|
|
|
static inline uint16_t r2be(const uint16_t *x)
|
|
{
|
|
return get_unaligned_be16(x);
|
|
}
|
|
|
|
static inline uint64_t r8be(const uint64_t *x)
|
|
{
|
|
return get_unaligned_be64(x);
|
|
}
|
|
|
|
static inline uint32_t rle(const uint32_t *x)
|
|
{
|
|
return get_unaligned_le32(x);
|
|
}
|
|
|
|
static inline uint16_t r2le(const uint16_t *x)
|
|
{
|
|
return get_unaligned_le16(x);
|
|
}
|
|
|
|
static inline uint64_t r8le(const uint64_t *x)
|
|
{
|
|
return get_unaligned_le64(x);
|
|
}
|
|
|
|
static inline void wbe(uint32_t val, uint32_t *x)
|
|
{
|
|
put_unaligned_be32(val, x);
|
|
}
|
|
|
|
static inline void wle(uint32_t val, uint32_t *x)
|
|
{
|
|
put_unaligned_le32(val, x);
|
|
}
|
|
|
|
static inline void w8be(uint64_t val, uint64_t *x)
|
|
{
|
|
put_unaligned_be64(val, x);
|
|
}
|
|
|
|
static inline void w8le(uint64_t val, uint64_t *x)
|
|
{
|
|
put_unaligned_le64(val, x);
|
|
}
|
|
|
|
void *elf_map(char const *fname, size_t *size, uint32_t types);
|
|
void *elf_map_ro(char const *fname, size_t *size, uint32_t types);
|
|
void elf_unmap(void *addr, size_t size);
|
|
int elf_map_machine(void *addr);
|
|
int elf_map_long_size(void *addr);
|
|
|
|
/* A mapped file with its section headers, section names and symbol table. */
|
|
struct elf_file {
|
|
void *base;
|
|
size_t size;
|
|
const char *shdrs;
|
|
unsigned int shnum, shentsize;
|
|
const char *shstrtab;
|
|
Elf_Shdr *symtab;
|
|
const char *strtab;
|
|
size_t nr_syms;
|
|
};
|
|
|
|
int elf_open_ro(char const *fname, uint32_t types, struct elf_file *elf);
|
|
void elf_close(struct elf_file *elf);
|
|
|
|
static inline Elf_Shdr *elf_section(const struct elf_file *elf,
|
|
unsigned int index)
|
|
{
|
|
return (Elf_Shdr *)(elf->shdrs + (size_t)index * elf->shentsize);
|
|
}
|
|
|
|
static inline const char *elf_section_name(const struct elf_file *elf,
|
|
Elf_Shdr *shdr)
|
|
{
|
|
return elf->shstrtab + shdr_name(shdr);
|
|
}
|
|
|
|
static inline Elf_Sym *elf_symbol(const struct elf_file *elf, size_t index)
|
|
{
|
|
const char *base = elf->base;
|
|
|
|
return (Elf_Sym *)(base + shdr_offset(elf->symtab) +
|
|
index * shdr_entsize(elf->symtab));
|
|
}
|
|
|
|
#endif /* _SCRIPTS_ELF_PARSE_H */
|