xref: /illumos-gate/usr/src/boot/efi/loader/reloc.c (revision 22028508)
1199767f8SToomas Soome /*-
2199767f8SToomas Soome  * Copyright (c) 2008-2010 Rui Paulo <rpaulo@FreeBSD.org>
3199767f8SToomas Soome  * All rights reserved.
4199767f8SToomas Soome  *
5199767f8SToomas Soome  * Redistribution and use in source and binary forms, with or without
6199767f8SToomas Soome  * modification, are permitted provided that the following conditions
7199767f8SToomas Soome  * are met:
8199767f8SToomas Soome  * 1. Redistributions of source code must retain the above copyright
9199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer.
10199767f8SToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
11199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer in the
12199767f8SToomas Soome  *    documentation and/or other materials provided with the distribution.
13199767f8SToomas Soome  *
14199767f8SToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15199767f8SToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16199767f8SToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17199767f8SToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18199767f8SToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19199767f8SToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20199767f8SToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21199767f8SToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22199767f8SToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23199767f8SToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24199767f8SToomas Soome  * SUCH DAMAGE.
25199767f8SToomas Soome  */
26199767f8SToomas Soome 
27199767f8SToomas Soome #include <sys/cdefs.h>
28199767f8SToomas Soome __FBSDID("$FreeBSD$");
29199767f8SToomas Soome 
30199767f8SToomas Soome #include <sys/types.h>
31199767f8SToomas Soome #include <elf.h>
32199767f8SToomas Soome #include <efi.h>
33199767f8SToomas Soome #include <bootstrap.h>
34199767f8SToomas Soome 
35199767f8SToomas Soome #if defined(__aarch64__)
36199767f8SToomas Soome #define	ElfW_Rel	Elf64_Rela
37199767f8SToomas Soome #define	ElfW_Dyn	Elf64_Dyn
38199767f8SToomas Soome #define	ELFW_R_TYPE	ELF64_R_TYPE
39199767f8SToomas Soome #define	ELF_RELA
40199767f8SToomas Soome #elif defined(__arm__) || defined(__i386__)
41199767f8SToomas Soome #define ElfW_Rel	Elf32_Rel
42199767f8SToomas Soome #define	ElfW_Dyn	Elf32_Dyn
43199767f8SToomas Soome #define	ELFW_R_TYPE	ELF32_R_TYPE
44199767f8SToomas Soome #elif defined(__amd64__)
45199767f8SToomas Soome #define ElfW_Rel	Elf64_Rel
46199767f8SToomas Soome #define	ElfW_Dyn	Elf64_Dyn
47199767f8SToomas Soome #define	ELFW_R_TYPE	ELF64_R_TYPE
48199767f8SToomas Soome #else
49199767f8SToomas Soome #error architecture not supported
50199767f8SToomas Soome #endif
51199767f8SToomas Soome #if defined(__aarch64__)
52199767f8SToomas Soome #define	RELOC_TYPE_NONE		R_AARCH64_NONE
53199767f8SToomas Soome #define	RELOC_TYPE_RELATIVE	R_AARCH64_RELATIVE
54199767f8SToomas Soome #elif defined(__amd64__)
55199767f8SToomas Soome #define	RELOC_TYPE_NONE		R_X86_64_NONE
56199767f8SToomas Soome #define	RELOC_TYPE_RELATIVE	R_X86_64_RELATIVE
57199767f8SToomas Soome #elif defined(__arm__)
58199767f8SToomas Soome #define	RELOC_TYPE_NONE		R_ARM_NONE
59199767f8SToomas Soome #define	RELOC_TYPE_RELATIVE	R_ARM_RELATIVE
60199767f8SToomas Soome #elif defined(__i386__)
61199767f8SToomas Soome #define	RELOC_TYPE_NONE		R_386_NONE
62199767f8SToomas Soome #define	RELOC_TYPE_RELATIVE	R_386_RELATIVE
63199767f8SToomas Soome #endif
64199767f8SToomas Soome 
65199767f8SToomas Soome /*
66199767f8SToomas Soome  * A simple relocator for EFI binaries.
67199767f8SToomas Soome  */
68199767f8SToomas Soome EFI_STATUS
_reloc(unsigned long ImageBase,ElfW_Dyn * dynamic,EFI_HANDLE image_handle,EFI_SYSTEM_TABLE * system_table)69199767f8SToomas Soome _reloc(unsigned long ImageBase, ElfW_Dyn *dynamic, EFI_HANDLE image_handle,
70199767f8SToomas Soome     EFI_SYSTEM_TABLE *system_table)
71199767f8SToomas Soome {
72199767f8SToomas Soome 	unsigned long relsz, relent;
73199767f8SToomas Soome 	unsigned long *newaddr;
74199767f8SToomas Soome 	ElfW_Rel *rel;
75199767f8SToomas Soome 	ElfW_Dyn *dynp;
76199767f8SToomas Soome 
77199767f8SToomas Soome 	/*
78199767f8SToomas Soome 	 * Find the relocation address, its size and the relocation entry.
79199767f8SToomas Soome 	 */
80199767f8SToomas Soome 	relsz = 0;
81199767f8SToomas Soome 	relent = 0;
82199767f8SToomas Soome 	for (dynp = dynamic; dynp->d_tag != DT_NULL; dynp++) {
83199767f8SToomas Soome 		switch (dynp->d_tag) {
84199767f8SToomas Soome 		case DT_REL:
85199767f8SToomas Soome 		case DT_RELA:
86199767f8SToomas Soome 			rel = (ElfW_Rel *) ((unsigned long) dynp->d_un.d_ptr +
87199767f8SToomas Soome 			    ImageBase);
88199767f8SToomas Soome 			break;
89199767f8SToomas Soome 		case DT_RELSZ:
90199767f8SToomas Soome 		case DT_RELASZ:
91199767f8SToomas Soome 			relsz = dynp->d_un.d_val;
92199767f8SToomas Soome 			break;
93199767f8SToomas Soome 		case DT_RELENT:
94199767f8SToomas Soome 		case DT_RELAENT:
95199767f8SToomas Soome 			relent = dynp->d_un.d_val;
96199767f8SToomas Soome 			break;
97199767f8SToomas Soome 		default:
98199767f8SToomas Soome 			break;
99199767f8SToomas Soome 		}
100199767f8SToomas Soome 	}
101199767f8SToomas Soome 
102199767f8SToomas Soome 	/*
103199767f8SToomas Soome 	 * Perform the actual relocation.
104199767f8SToomas Soome 	 */
105199767f8SToomas Soome 	for (; relsz > 0; relsz -= relent) {
106199767f8SToomas Soome 		switch (ELFW_R_TYPE(rel->r_info)) {
107199767f8SToomas Soome 		case RELOC_TYPE_NONE:
108199767f8SToomas Soome 			/* No relocation needs be performed. */
109199767f8SToomas Soome 			break;
110199767f8SToomas Soome 
111199767f8SToomas Soome 		case RELOC_TYPE_RELATIVE:
112199767f8SToomas Soome 			/* Address relative to the base address. */
113199767f8SToomas Soome 			newaddr = (unsigned long *)(ImageBase + rel->r_offset);
114199767f8SToomas Soome 			*newaddr += ImageBase;
115199767f8SToomas Soome 			/* Add the addend when the ABI uses them */
116199767f8SToomas Soome #ifdef ELF_RELA
117199767f8SToomas Soome 			*newaddr += rel->r_addend;
118199767f8SToomas Soome #endif
119199767f8SToomas Soome 			break;
120199767f8SToomas Soome 		default:
121199767f8SToomas Soome 			/* XXX: do we need other relocations ? */
122199767f8SToomas Soome 			break;
123199767f8SToomas Soome 		}
124199767f8SToomas Soome 		rel = (ElfW_Rel *) ((caddr_t) rel + relent);
125199767f8SToomas Soome 	}
126199767f8SToomas Soome 
127199767f8SToomas Soome 	return (EFI_SUCCESS);
128199767f8SToomas Soome }
129