summaryrefslogtreecommitdiff
path: root/roms/SLOF/lib/libelf/elf.c
blob: 33d92111fdb8b3b84bed152a47dccbe9def7a27c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/******************************************************************************
 * Copyright (c) 2004, 2011 IBM Corporation
 * All rights reserved.
 * This program and the accompanying materials
 * are made available under the terms of the BSD License
 * which accompanies this distribution, and is available at
 * http://www.opensource.org/licenses/bsd-license.php
 *
 * Contributors:
 *     IBM Corporation - initial implementation
 *****************************************************************************/

/*
 * ELF loader
 */

#include <string.h>
#include <cache.h>
#include <libelf.h>
#include <byteorder.h>

/**
 * elf_check_file tests if the file at file_addr is
 * a correct endian, ELF PPC executable
 * @param file_addr  pointer to the start of the ELF file
 * @return           the class (1 for 32 bit, 2 for 64 bit)
 *                   -1 if it is not an ELF file
 *                   -2 if it has the wrong endianness
 *                   -3 if it is not an ELF executable
 *                   -4 if it is not for PPC
 */
static int
elf_check_file(unsigned long *file_addr)
{
	struct ehdr *ehdr = (struct ehdr *) file_addr;
	/* check if it is an ELF image at all */
	if (cpu_to_be32(ehdr->ei_ident) != 0x7f454c46)
		return -1;

	/* endian check */
#ifdef __BIG_ENDIAN__
	if (ehdr->ei_data != 2)
		/* not a big endian image */
#else
	if (ehdr->ei_data == 2)
		/* not a little endian image */
#endif
		return -2;

	/* check if it is an ELF executable ... and also
	 * allow DYN files, since this is specified by ePAPR */
	if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN)
		return -3;

	/* check if it is a PPC ELF executable */
	if (ehdr->e_machine != 0x14 && ehdr->e_machine != 0x15)
		return -4;

	return ehdr->ei_class;
}

/**
 * load_elf_file tries to load the ELF file specified in file_addr
 *
 * it first checks if the file is a PPC ELF executable and then loads
 * the segments depending if it is a 64bit or 32 bit ELF file
 *
 * @param file_addr  pointer to the start of the elf file
 * @param entry      pointer where the ELF loader will store
 *                   the entry point
 * @param pre_load   handler that is called before copying a segment
 * @param post_load  handler that is called after copying a segment
 * @return           1 for a 32 bit file
 *                   2 for a 64 bit file
 *                   anything else means an error during load
 */
int
elf_load_file(void *file_addr, unsigned long *entry,
              int (*pre_load)(void*, long),
              void (*post_load)(void*, long))
{
	int type = elf_check_file(file_addr);

	switch (type) {
	case 1:
		*entry = elf_load_segments32(file_addr, 0, pre_load, post_load);
		break;
	case 2:
		*entry = elf_load_segments64(file_addr, 0, pre_load, post_load);
		break;
	}

	return type;
}


/**
 * load_elf_file_to_addr loads an ELF file to given address.
 * This is useful for 64-bit vmlinux images that use the virtual entry
 * point address in their headers, and thereby need a special treatment.
 *
 * @param file_addr  pointer to the start of the elf file
 * @param entry      pointer where the ELF loader will store
 *                   the entry point
 * @param pre_load   handler that is called before copying a segment
 * @param post_load  handler that is called after copying a segment
 * @return           1 for a 32 bit file
 *                   2 for a 64 bit file
 *                   anything else means an error during load
 */
int
elf_load_file_to_addr(void *file_addr, void *addr, unsigned long *entry,
                      int (*pre_load)(void*, long),
                      void (*post_load)(void*, long))
{
	int type;
	long offset;

	type = elf_check_file(file_addr);

	switch (type) {
	case 1:
		/* Parse 32-bit image */
		offset = (long)addr - elf_get_base_addr32(file_addr);
		*entry = elf_load_segments32(file_addr, offset, pre_load,
		                             post_load) + offset;
		// TODO: elf_relocate32(...)
		break;
	case 2:
		/* Parse 64-bit image */
		offset = (long)addr - elf_get_base_addr64(file_addr);
		*entry = elf_load_segments64(file_addr, offset, pre_load,
		                             post_load) + offset;
		elf_relocate64(file_addr, offset);
		break;
	}

	return type;
}


/**
 * Get the base load address of the ELF image
 * @return  The base address or -1 for error
 */
long
elf_get_base_addr(void *file_addr)
{
	int type;

	type = elf_check_file(file_addr);

	switch (type) {
	case 1:
		/* Return 32-bit image base address */
		return elf_get_base_addr32(file_addr);
		break;
	case 2:
		/* Return 64-bit image base address */
		return elf_get_base_addr64(file_addr);
		break;
	}

	return -1;
}