All of lore.kernel.org
 help / color / mirror / Atom feed
* [U-Boot] [PATCH] efi_loader: correctly apply relocations from the .reloc section
@ 2018-05-02 23:36 Ivan Gorinov
  2018-05-03  7:22 ` Heinrich Schuchardt
  2018-05-03  7:45 ` [U-Boot] " Alexander Graf
  0 siblings, 2 replies; 3+ messages in thread
From: Ivan Gorinov @ 2018-05-02 23:36 UTC (permalink / raw)
  To: u-boot

Instead of difference between preferred and actual image base, the
actual base is added to the fields specified in the .reloc section.

Use ImageBase from PE optional header to compute the delta,
exit early if the image is loaded at the preferred address.

Signed-off-by: Ivan Gorinov <ivan.gorinov@intel.com>
---
 lib/efi_loader/efi_image_loader.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/lib/efi_loader/efi_image_loader.c b/lib/efi_loader/efi_image_loader.c
index d5fbba3..80c08d2 100644
--- a/lib/efi_loader/efi_image_loader.c
+++ b/lib/efi_loader/efi_image_loader.c
@@ -93,11 +93,16 @@ void efi_print_image_infos(void *pc)
 }
 
 static efi_status_t efi_loader_relocate(const IMAGE_BASE_RELOCATION *rel,
-			unsigned long rel_size, void *efi_reloc)
+			unsigned long rel_size, void *efi_reloc,
+			unsigned long pref_address)
 {
+	unsigned long delta = (unsigned long)efi_reloc - pref_address;
 	const IMAGE_BASE_RELOCATION *end;
 	int i;
 
+	if (delta == 0)
+		return EFI_SUCCESS;
+
 	end = (const IMAGE_BASE_RELOCATION *)((const char *)rel + rel_size);
 	while (rel < end - 1 && rel->SizeOfBlock) {
 		const uint16_t *relocs = (const uint16_t *)(rel + 1);
@@ -106,7 +111,6 @@ static efi_status_t efi_loader_relocate(const IMAGE_BASE_RELOCATION *rel,
 			uint32_t offset = (uint32_t)(*relocs & 0xfff) +
 					  rel->VirtualAddress;
 			int type = *relocs >> EFI_PAGE_SHIFT;
-			unsigned long delta = (unsigned long)efi_reloc;
 			uint64_t *x64 = efi_reloc + offset;
 			uint32_t *x32 = efi_reloc + offset;
 			uint16_t *x16 = efi_reloc + offset;
@@ -194,6 +198,7 @@ void *efi_load_pe(void *efi, struct efi_loaded_image *loaded_image_info)
 	unsigned long rel_size;
 	int rel_idx = IMAGE_DIRECTORY_ENTRY_BASERELOC;
 	void *entry;
+	uint64_t image_base;
 	uint64_t image_size;
 	unsigned long virt_size = 0;
 	int supported = 0;
@@ -237,6 +242,7 @@ void *efi_load_pe(void *efi, struct efi_loaded_image *loaded_image_info)
 	if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
 		IMAGE_NT_HEADERS64 *nt64 = (void *)nt;
 		IMAGE_OPTIONAL_HEADER64 *opt = &nt64->OptionalHeader;
+		image_base = opt->ImageBase;
 		image_size = opt->SizeOfImage;
 		efi_set_code_and_data_type(loaded_image_info, opt->Subsystem);
 		efi_reloc = efi_alloc(virt_size,
@@ -252,6 +258,7 @@ void *efi_load_pe(void *efi, struct efi_loaded_image *loaded_image_info)
 		virt_size = ALIGN(virt_size, opt->SectionAlignment);
 	} else if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
 		IMAGE_OPTIONAL_HEADER32 *opt = &nt->OptionalHeader;
+		image_base = opt->ImageBase;
 		image_size = opt->SizeOfImage;
 		efi_set_code_and_data_type(loaded_image_info, opt->Subsystem);
 		efi_reloc = efi_alloc(virt_size,
@@ -282,7 +289,8 @@ void *efi_load_pe(void *efi, struct efi_loaded_image *loaded_image_info)
 	}
 
 	/* Run through relocations */
-	if (efi_loader_relocate(rel, rel_size, efi_reloc) != EFI_SUCCESS) {
+	if (efi_loader_relocate(rel, rel_size, efi_reloc,
+				(unsigned long)image_base) != EFI_SUCCESS) {
 		efi_free_pages((uintptr_t) efi_reloc,
 			       (virt_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT);
 		return NULL;
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [U-Boot] [PATCH] efi_loader: correctly apply relocations from the .reloc section
  2018-05-02 23:36 [U-Boot] [PATCH] efi_loader: correctly apply relocations from the .reloc section Ivan Gorinov
@ 2018-05-03  7:22 ` Heinrich Schuchardt
  2018-05-03  7:45 ` [U-Boot] " Alexander Graf
  1 sibling, 0 replies; 3+ messages in thread
From: Heinrich Schuchardt @ 2018-05-03  7:22 UTC (permalink / raw)
  To: u-boot

On 05/03/2018 01:36 AM, Ivan Gorinov wrote:
> Instead of difference between preferred and actual image base, the
> actual base is added to the fields specified in the .reloc section.
> 
> Use ImageBase from PE optional header to compute the delta,
> exit early if the image is loaded at the preferred address.
> 
> Signed-off-by: Ivan Gorinov <ivan.gorinov@intel.com>

The PE spec has this sentence:
"To apply a base relocation, the difference is calculated between the 
preferred base address and the base where the image is actually loaded."

The spec further defines ImageBase as "The preferred address of the 
first byte of image when loaded into memory."

EDK2 also calculates the relocations offset as:
BaseAddress - OptionalHeader.ImageBase
in MdePkg/Library/BasePeCoffLib/BasePeCoff.c and
in DuetPkg/EfiLdr/PeLoader.c

Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [U-Boot] efi_loader: correctly apply relocations from the .reloc section
  2018-05-02 23:36 [U-Boot] [PATCH] efi_loader: correctly apply relocations from the .reloc section Ivan Gorinov
  2018-05-03  7:22 ` Heinrich Schuchardt
@ 2018-05-03  7:45 ` Alexander Graf
  1 sibling, 0 replies; 3+ messages in thread
From: Alexander Graf @ 2018-05-03  7:45 UTC (permalink / raw)
  To: u-boot

> Instead of difference between preferred and actual image base, the
> actual base is added to the fields specified in the .reloc section.
> 
> Use ImageBase from PE optional header to compute the delta,
> exit early if the image is loaded at the preferred address.
> 
> Signed-off-by: Ivan Gorinov <ivan.gorinov@intel.com>
> Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

Thanks, applied to efi-next

Alex

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2018-05-03  7:45 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-05-02 23:36 [U-Boot] [PATCH] efi_loader: correctly apply relocations from the .reloc section Ivan Gorinov
2018-05-03  7:22 ` Heinrich Schuchardt
2018-05-03  7:45 ` [U-Boot] " Alexander Graf

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.