]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - sys/boot/i386/efi/reloc.c
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / sys / boot / i386 / efi / reloc.c
1 /*-
2  * Copyright (c) 2008-2010 Rui Paulo <rpaulo@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/types.h>
31 #include <sys/elf32.h>
32 #include <efi.h>
33
34 /*
35  * XXX: we can't include sys/systm.h.
36  */
37 #ifndef CTASSERT                /* Allow lint to override */
38 #define CTASSERT(x)             _CTASSERT(x, __LINE__)
39 #define _CTASSERT(x, y)         __CTASSERT(x, y)
40 #define __CTASSERT(x, y)        typedef char __assert ## y[(x) ? 1 : -1]
41 #endif
42
43
44 /*
45  * A simple relocator for IA32 EFI binaries.
46  */
47 EFI_STATUS
48 _reloc(unsigned long ImageBase, Elf32_Dyn *dynamic, EFI_HANDLE image_handle,
49     EFI_SYSTEM_TABLE *system_table)
50 {
51         unsigned long relsz, relent;
52         unsigned long *newaddr;
53         Elf32_Rel *rel;
54         Elf32_Dyn *dynp;
55
56         /*
57          * Find the relocation address, its size and the relocation entry.
58          */
59         relsz = 0;
60         relent = 0;
61         for (dynp = dynamic; dynp->d_tag != DT_NULL; dynp++) {
62                 switch (dynp->d_tag) {
63                 case DT_REL:
64                         rel = (Elf32_Rel *) ((unsigned long) dynp->d_un.d_ptr +
65                             ImageBase);
66                         break;
67                 case DT_RELSZ:
68                         relsz = dynp->d_un.d_val;
69                         break;
70                 case DT_RELENT:
71                         relent = dynp->d_un.d_val;
72                         break;
73                 default:
74                         break;
75                 }
76         }
77
78         /*
79          * Perform the actual relocation.
80          * XXX: We are reusing code for the amd64 version of this, but
81          * we must make sure the relocation types are the same.
82          */
83         CTASSERT(R_386_NONE == R_X86_64_NONE);
84         CTASSERT(R_386_RELATIVE == R_X86_64_RELATIVE);
85         for (; relsz > 0; relsz -= relent) {
86                 switch (ELF32_R_TYPE(rel->r_info)) {
87                 case R_386_NONE:
88                         /* No relocation needs be performed. */
89                         break;
90                 case R_386_RELATIVE:
91                         /* Address relative to the base address. */
92                         newaddr = (unsigned long *)(ImageBase + rel->r_offset);
93                         *newaddr += ImageBase;
94                         break;
95                 default:
96                         /* XXX: do we need other relocations ? */
97                         return (EFI_LOAD_ERROR);
98                 }
99                 rel = (Elf32_Rel *) ((caddr_t) rel + relent);
100         }
101
102         return (EFI_SUCCESS);
103 }