#include <elf.h>
#include <endian.h>
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <generated/autoconf.h>
Go to the source code of this file.
|
#define | HYP_SECTION_PREFIX ".hyp" |
|
#define | HYP_RELOC_SECTION ".hyp.reloc" |
|
#define | HYP_SECTION_SYMBOL_PREFIX "__hyp_section_" |
|
#define | R_AARCH64_ABS64 257 |
|
#define | R_AARCH64_PREL64 260 |
|
#define | R_AARCH64_PREL32 261 |
|
#define | R_AARCH64_PREL16 262 |
|
#define | R_AARCH64_PLT32 314 |
|
#define | R_AARCH64_LD_PREL_LO19 273 |
|
#define | R_AARCH64_ADR_PREL_LO21 274 |
|
#define | R_AARCH64_ADR_PREL_PG_HI21 275 |
|
#define | R_AARCH64_ADR_PREL_PG_HI21_NC 276 |
|
#define | R_AARCH64_ADD_ABS_LO12_NC 277 |
|
#define | R_AARCH64_LDST8_ABS_LO12_NC 278 |
|
#define | R_AARCH64_TSTBR14 279 |
|
#define | R_AARCH64_CONDBR19 280 |
|
#define | R_AARCH64_JUMP26 282 |
|
#define | R_AARCH64_CALL26 283 |
|
#define | R_AARCH64_LDST16_ABS_LO12_NC 284 |
|
#define | R_AARCH64_LDST32_ABS_LO12_NC 285 |
|
#define | R_AARCH64_LDST64_ABS_LO12_NC 286 |
|
#define | R_AARCH64_MOVW_PREL_G0 287 |
|
#define | R_AARCH64_MOVW_PREL_G0_NC 288 |
|
#define | R_AARCH64_MOVW_PREL_G1 289 |
|
#define | R_AARCH64_MOVW_PREL_G1_NC 290 |
|
#define | R_AARCH64_MOVW_PREL_G2 291 |
|
#define | R_AARCH64_MOVW_PREL_G2_NC 292 |
|
#define | R_AARCH64_MOVW_PREL_G3 293 |
|
#define | R_AARCH64_LDST128_ABS_LO12_NC 299 |
|
#define | fatal_error(fmt, ...) |
|
#define | fatal_perror(msg) |
|
#define | assert_op(lhs, rhs, fmt, op) |
|
#define | assert_eq(lhs, rhs, fmt) assert_op(lhs, rhs, fmt, ==) |
|
#define | assert_ne(lhs, rhs, fmt) assert_op(lhs, rhs, fmt, !=) |
|
#define | assert_lt(lhs, rhs, fmt) assert_op(lhs, rhs, fmt, <) |
|
#define | assert_ge(lhs, rhs, fmt) assert_op(lhs, rhs, fmt, >=) |
|
#define | elf_ptr(type, off) ((type *)(elf.begin + (off))) |
|
#define | for_each_section(var) for (var = elf.sh_table; var < elf.sh_table + elf16toh(elf.ehdr->e_shnum); ++var) |
|
#define | for_each_rela(shdr, var) |
|
◆ assert_eq
◆ assert_ge
◆ assert_lt
◆ assert_ne
◆ assert_op
#define assert_op |
( |
|
lhs, |
|
|
|
rhs, |
|
|
|
fmt, |
|
|
|
op |
|
) |
| |
Value: ({ \
typeof(lhs) _lhs = (lhs); \
typeof(rhs) _rhs = (rhs); \
\
if (!(_lhs op _rhs)) { \
fatal_error("assertion " #lhs " " #op " " #rhs \
" failed (lhs=" fmt ", rhs=" fmt \
", line=%d)", _lhs, _rhs, __LINE__); \
} \
})
Definition at line 177 of file gen-hyprel.c.
◆ elf_ptr
#define elf_ptr |
( |
|
type, |
|
|
|
off |
|
) |
| ((type *)(elf.begin + (off))) |
◆ fatal_error
#define fatal_error |
( |
|
fmt, |
|
|
|
... |
|
) |
| |
Value: ({ \
fprintf(stderr,
"error: %s: " fmt "\n", \
elf.path, ## __VA_ARGS__); \
exit(EXIT_FAILURE); \
__builtin_unreachable(); \
})
Definition at line 161 of file gen-hyprel.c.
◆ fatal_perror
#define fatal_perror |
( |
|
msg | ) |
|
Value: ({ \
fprintf(stderr, "error: %s: " msg ": %s\n", \
elf.path, strerror(errno)); \
exit(EXIT_FAILURE); \
__builtin_unreachable(); \
})
Definition at line 169 of file gen-hyprel.c.
◆ for_each_rela
#define for_each_rela |
( |
|
shdr, |
|
|
|
var |
|
) |
| |
Value: for (var =
elf_ptr(Elf64_Rela, elf64toh(shdr->sh_offset)); \
var <
elf_ptr(Elf64_Rela, elf64toh(shdr->sh_offset) + elf64toh(shdr->sh_size)); var++)
#define elf_ptr(type, off)
Definition at line 205 of file gen-hyprel.c.
◆ for_each_section
◆ HYP_RELOC_SECTION
#define HYP_RELOC_SECTION ".hyp.reloc" |
◆ HYP_SECTION_PREFIX
#define HYP_SECTION_PREFIX ".hyp" |
◆ HYP_SECTION_SYMBOL_PREFIX
#define HYP_SECTION_SYMBOL_PREFIX "__hyp_section_" |
◆ R_AARCH64_ABS64
#define R_AARCH64_ABS64 257 |
◆ R_AARCH64_ADD_ABS_LO12_NC
#define R_AARCH64_ADD_ABS_LO12_NC 277 |
◆ R_AARCH64_ADR_PREL_LO21
#define R_AARCH64_ADR_PREL_LO21 274 |
◆ R_AARCH64_ADR_PREL_PG_HI21
#define R_AARCH64_ADR_PREL_PG_HI21 275 |
◆ R_AARCH64_ADR_PREL_PG_HI21_NC
#define R_AARCH64_ADR_PREL_PG_HI21_NC 276 |
◆ R_AARCH64_CALL26
#define R_AARCH64_CALL26 283 |
◆ R_AARCH64_CONDBR19
#define R_AARCH64_CONDBR19 280 |
◆ R_AARCH64_JUMP26
#define R_AARCH64_JUMP26 282 |
◆ R_AARCH64_LD_PREL_LO19
#define R_AARCH64_LD_PREL_LO19 273 |
◆ R_AARCH64_LDST128_ABS_LO12_NC
#define R_AARCH64_LDST128_ABS_LO12_NC 299 |
◆ R_AARCH64_LDST16_ABS_LO12_NC
#define R_AARCH64_LDST16_ABS_LO12_NC 284 |
◆ R_AARCH64_LDST32_ABS_LO12_NC
#define R_AARCH64_LDST32_ABS_LO12_NC 285 |
◆ R_AARCH64_LDST64_ABS_LO12_NC
#define R_AARCH64_LDST64_ABS_LO12_NC 286 |
◆ R_AARCH64_LDST8_ABS_LO12_NC
#define R_AARCH64_LDST8_ABS_LO12_NC 278 |
◆ R_AARCH64_MOVW_PREL_G0
#define R_AARCH64_MOVW_PREL_G0 287 |
◆ R_AARCH64_MOVW_PREL_G0_NC
#define R_AARCH64_MOVW_PREL_G0_NC 288 |
◆ R_AARCH64_MOVW_PREL_G1
#define R_AARCH64_MOVW_PREL_G1 289 |
◆ R_AARCH64_MOVW_PREL_G1_NC
#define R_AARCH64_MOVW_PREL_G1_NC 290 |
◆ R_AARCH64_MOVW_PREL_G2
#define R_AARCH64_MOVW_PREL_G2 291 |
◆ R_AARCH64_MOVW_PREL_G2_NC
#define R_AARCH64_MOVW_PREL_G2_NC 292 |
◆ R_AARCH64_MOVW_PREL_G3
#define R_AARCH64_MOVW_PREL_G3 293 |
◆ R_AARCH64_PLT32
#define R_AARCH64_PLT32 314 |
◆ R_AARCH64_PREL16
#define R_AARCH64_PREL16 262 |
◆ R_AARCH64_PREL32
#define R_AARCH64_PREL32 261 |
◆ R_AARCH64_PREL64
#define R_AARCH64_PREL64 260 |
◆ R_AARCH64_TSTBR14
#define R_AARCH64_TSTBR14 279 |
◆ emit_all_relocs()
static void emit_all_relocs |
( |
void |
| ) |
|
|
static |
Definition at line 426 of file gen-hyprel.c.
431 switch (elf32toh(shdr->sh_type)) {
#define for_each_section(var)
static const char * section_name(Elf64_Shdr *shdr)
static void emit_rela_section(Elf64_Shdr *sh_rela)
#define fatal_error(fmt,...)
◆ emit_epilogue()
static void emit_epilogue |
( |
void |
| ) |
|
|
static |
◆ emit_prologue()
static void emit_prologue |
( |
void |
| ) |
|
|
static |
◆ emit_rela_abs64()
static void emit_rela_abs64 |
( |
Elf64_Rela * |
rela, |
|
|
const char * |
sh_orig_name |
|
) |
| |
|
static |
Definition at line 326 of file gen-hyprel.c.
329 static size_t reloc_offset;
340 printf(
".reloc %lu, R_AARCH64_PREL32, %s%s + 0x%lx\n",
342 elf64toh(rela->r_offset));
#define HYP_SECTION_SYMBOL_PREFIX
◆ emit_rela_section()
static void emit_rela_section |
( |
Elf64_Shdr * |
sh_rela | ) |
|
|
static |
Definition at line 360 of file gen-hyprel.c.
362 Elf64_Shdr *sh_orig = &
elf.sh_table[elf32toh(sh_rela->sh_info)];
373 uint32_t type = (uint32_t)elf64toh(rela->r_info);
376 assert_lt(elf64toh(rela->r_offset), elf64toh(sh_orig->sh_size),
"0x%lx");
#define R_AARCH64_MOVW_PREL_G3
#define R_AARCH64_LDST8_ABS_LO12_NC
#define R_AARCH64_CONDBR19
static bool starts_with(const char *str, const char *prefix)
#define R_AARCH64_LDST64_ABS_LO12_NC
#define assert_lt(lhs, rhs, fmt)
#define R_AARCH64_MOVW_PREL_G2
#define HYP_SECTION_PREFIX
static void emit_rela_abs64(Elf64_Rela *rela, const char *sh_orig_name)
#define R_AARCH64_MOVW_PREL_G2_NC
#define R_AARCH64_ADR_PREL_PG_HI21_NC
#define R_AARCH64_MOVW_PREL_G1
#define R_AARCH64_LDST128_ABS_LO12_NC
#define R_AARCH64_MOVW_PREL_G0_NC
#define R_AARCH64_LD_PREL_LO19
#define for_each_rela(shdr, var)
#define R_AARCH64_ADD_ABS_LO12_NC
#define R_AARCH64_ADR_PREL_LO21
static void emit_section_prologue(const char *sh_orig_name)
#define R_AARCH64_MOVW_PREL_G0
#define R_AARCH64_TSTBR14
#define R_AARCH64_LDST32_ABS_LO12_NC
#define R_AARCH64_LDST16_ABS_LO12_NC
#define R_AARCH64_ADR_PREL_PG_HI21
#define R_AARCH64_MOVW_PREL_G1_NC
◆ emit_section_prologue()
static void emit_section_prologue |
( |
const char * |
sh_orig_name | ) |
|
|
static |
◆ init_elf()
static void init_elf |
( |
const char * |
path | ) |
|
|
static |
Definition at line 245 of file gen-hyprel.c.
254 fd = open(
path, O_RDONLY);
259 ret = fstat(fd, &stat);
266 elf.begin = mmap(0, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
267 if (
elf.begin == MAP_FAILED) {
286 assert_eq(
elf.ehdr->e_ident[EI_CLASS], ELFCLASS64,
"%u");
289 assert_eq(elf16toh(
elf.ehdr->e_machine), EM_AARCH64,
"%u");
static Elf64_Shdr * section_by_idx(uint16_t idx)
static const char * section_begin(Elf64_Shdr *shdr)
#define assert_eq(lhs, rhs, fmt)
#define fatal_perror(msg)
static Elf64_Shdr * section_by_off(Elf64_Off off)
#define assert_ge(lhs, rhs, fmt)
◆ main()
int main |
( |
int |
argc, |
|
|
const char ** |
argv |
|
) |
| |
Definition at line 442 of file gen-hyprel.c.
445 fprintf(stderr,
"Usage: %s <elf_input>\n", argv[0]);
static void init_elf(const char *path)
static void emit_all_relocs(void)
static void emit_epilogue(void)
static void emit_prologue(void)
◆ section_begin()
static const char* section_begin |
( |
Elf64_Shdr * |
shdr | ) |
|
|
inlinestatic |
◆ section_by_idx()
static Elf64_Shdr* section_by_idx |
( |
uint16_t |
idx | ) |
|
|
inlinestatic |
Definition at line 235 of file gen-hyprel.c.
238 return &
elf.sh_table[idx];
#define assert_ne(lhs, rhs, fmt)
◆ section_by_off()
static Elf64_Shdr* section_by_off |
( |
Elf64_Off |
off | ) |
|
|
inlinestatic |
◆ section_name()
static const char* section_name |
( |
Elf64_Shdr * |
shdr | ) |
|
|
inlinestatic |
Definition at line 216 of file gen-hyprel.c.
218 return elf.sh_string + elf32toh(shdr->sh_name);
◆ starts_with()
static bool starts_with |
( |
const char * |
str, |
|
|
const char * |
prefix |
|
) |
| |
|
inlinestatic |
Definition at line 210 of file gen-hyprel.c.
212 return memcmp(str, prefix, strlen(prefix)) == 0;
◆ begin
◆ ehdr
◆ path
◆ sh_string
◆ sh_table
◆ size