2018-06-22 23:15:12 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2018 Intel Corporation. All rights reserved.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*/
|
|
|
|
|
2018-11-30 20:10:22 +08:00
|
|
|
#include <types.h>
|
2018-06-22 23:15:12 +08:00
|
|
|
#include <reloc.h>
|
2021-04-23 15:50:57 +08:00
|
|
|
#include <asm/boot/ld_sym.h>
|
2018-06-22 23:15:12 +08:00
|
|
|
|
2018-11-26 18:15:00 +08:00
|
|
|
#ifdef CONFIG_RELOC
|
2019-01-25 12:14:02 +08:00
|
|
|
#define DT_NULL 0U /* end of .dynamic section */
|
|
|
|
#define DT_RELA 7U /* relocation table */
|
|
|
|
#define DT_RELASZ 8U /* size of reloc table */
|
|
|
|
#define DT_RELAENT 9U /* size of one entry */
|
2018-06-22 23:15:12 +08:00
|
|
|
|
2018-12-27 22:02:26 +08:00
|
|
|
#define R_X86_64_RELATIVE 8UL
|
|
|
|
|
2018-11-26 18:15:00 +08:00
|
|
|
struct Elf64_Dyn {
|
|
|
|
uint64_t d_tag;
|
|
|
|
uint64_t d_ptr;
|
|
|
|
};
|
|
|
|
|
2018-06-22 23:15:12 +08:00
|
|
|
struct Elf64_Rel {
|
|
|
|
uint64_t r_offset;
|
|
|
|
uint64_t r_info;
|
|
|
|
uint64_t reserved;
|
|
|
|
};
|
2018-11-26 18:15:00 +08:00
|
|
|
#endif
|
2018-06-22 23:15:12 +08:00
|
|
|
|
2018-09-10 13:21:56 +08:00
|
|
|
static inline uint64_t elf64_r_type(uint64_t i)
|
|
|
|
{
|
|
|
|
return (i & 0xffffffffUL);
|
|
|
|
}
|
|
|
|
|
2018-10-29 23:28:32 +08:00
|
|
|
/* get the delta between CONFIG_HV_RAM_START and the actual load address */
|
2018-06-22 23:15:12 +08:00
|
|
|
uint64_t get_hv_image_delta(void)
|
|
|
|
{
|
|
|
|
uint64_t addr;
|
|
|
|
|
|
|
|
asm volatile (" call 0f\n"
|
|
|
|
"0: pop %%rax\n"
|
|
|
|
" sub $0b, %%rax\n"
|
|
|
|
" mov %%rax, %0\n"
|
|
|
|
: "=m" (addr)
|
|
|
|
:
|
|
|
|
: "%rax");
|
|
|
|
|
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
2019-03-13 06:23:11 +08:00
|
|
|
/* get the actual Hypervisor load address (HVA) */
|
2018-06-22 23:15:12 +08:00
|
|
|
uint64_t get_hv_image_base(void)
|
|
|
|
{
|
2018-10-29 23:28:32 +08:00
|
|
|
return (get_hv_image_delta() + CONFIG_HV_RAM_START);
|
2018-06-22 23:15:12 +08:00
|
|
|
}
|
|
|
|
|
2018-09-29 15:46:27 +08:00
|
|
|
void relocate(void)
|
2018-06-22 23:15:12 +08:00
|
|
|
{
|
2018-07-12 02:18:29 +08:00
|
|
|
#ifdef CONFIG_RELOC
|
2018-06-22 23:15:12 +08:00
|
|
|
struct Elf64_Dyn *dyn;
|
2019-12-05 03:04:36 +08:00
|
|
|
struct Elf64_Rel *entry = NULL;
|
|
|
|
uint8_t *rela_start = NULL, *rela_end = NULL;
|
|
|
|
uint64_t rela_size = 0;
|
|
|
|
uint64_t delta, entry_size = 0;
|
2018-06-22 23:15:12 +08:00
|
|
|
uint64_t trampoline_end;
|
2020-03-03 02:16:50 +08:00
|
|
|
uint64_t primary_entry_end;
|
2018-06-22 23:15:12 +08:00
|
|
|
uint64_t *addr;
|
|
|
|
|
|
|
|
/* get the delta that needs to be patched */
|
|
|
|
delta = get_hv_image_delta();
|
2019-01-25 12:14:02 +08:00
|
|
|
if (delta != 0U) {
|
|
|
|
|
|
|
|
/* Look for the descriptoin of relocation sections */
|
|
|
|
for (dyn = (struct Elf64_Dyn *)_DYNAMIC; dyn->d_tag != DT_NULL; dyn++) {
|
|
|
|
switch (dyn->d_tag) {
|
|
|
|
case DT_RELA:
|
2019-12-05 03:04:36 +08:00
|
|
|
rela_start = (uint8_t *)(dyn->d_ptr + delta);
|
2019-01-25 12:14:02 +08:00
|
|
|
break;
|
|
|
|
case DT_RELASZ:
|
2019-12-05 03:04:36 +08:00
|
|
|
rela_size = dyn->d_ptr;
|
2019-01-25 12:14:02 +08:00
|
|
|
break;
|
|
|
|
case DT_RELAENT:
|
2019-12-05 03:04:36 +08:00
|
|
|
entry_size = dyn->d_ptr;
|
2019-01-25 12:14:02 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* if no RELA/RELASZ found, both start and end will be initialized to NULL, and later while loop won't be executed */
|
|
|
|
break;
|
|
|
|
}
|
2018-06-22 23:15:12 +08:00
|
|
|
}
|
|
|
|
|
2019-01-25 12:14:02 +08:00
|
|
|
/*
|
|
|
|
* Need to subtract the relocation delta to get the correct
|
|
|
|
* absolute addresses
|
|
|
|
*/
|
|
|
|
trampoline_end = (uint64_t)(&ld_trampoline_end) - delta;
|
2020-03-03 02:16:50 +08:00
|
|
|
primary_entry_end = (uint64_t)(&ld_entry_end) - delta;
|
2019-01-25 12:14:02 +08:00
|
|
|
|
2019-12-05 03:04:36 +08:00
|
|
|
rela_end = rela_start + rela_size;
|
|
|
|
while (rela_start < rela_end) {
|
|
|
|
entry = (struct Elf64_Rel *)rela_start;
|
|
|
|
if ((elf64_r_type(entry->r_info)) == R_X86_64_RELATIVE) {
|
|
|
|
addr = (uint64_t *)(delta + entry->r_offset);
|
2019-01-25 12:14:02 +08:00
|
|
|
|
|
|
|
/*
|
2020-03-03 02:16:50 +08:00
|
|
|
* we won't fixup any symbols from trampoline.S or cpu_primary.S
|
2019-01-25 12:14:02 +08:00
|
|
|
* for a number of reasons:
|
|
|
|
*
|
|
|
|
* - trampoline code itself takes another relocation,
|
|
|
|
* so any entries for trampoline symbols can't be fixed up
|
|
|
|
* through .rela sections
|
|
|
|
* - Linker option "-z noreloc-overflow" could force R_X86_32
|
|
|
|
* to R_X86_64 in the relocation sections, which could make
|
2020-03-03 02:16:50 +08:00
|
|
|
* the fixed up code dirty.
|
2019-01-25 12:14:02 +08:00
|
|
|
*/
|
2020-03-03 02:16:50 +08:00
|
|
|
if ((entry->r_offset > trampoline_end) && (entry->r_offset > primary_entry_end)) {
|
2019-01-25 12:14:02 +08:00
|
|
|
*addr += delta;
|
|
|
|
}
|
2018-06-22 23:15:12 +08:00
|
|
|
}
|
2019-12-05 03:04:36 +08:00
|
|
|
rela_start += entry_size;
|
2018-06-22 23:15:12 +08:00
|
|
|
}
|
|
|
|
}
|
2018-07-11 19:47:54 +08:00
|
|
|
#endif
|
2018-07-12 02:18:29 +08:00
|
|
|
}
|