summaryrefslogtreecommitdiffstats
path: root/qemu/roms/SLOF/lib/libelf/elf32.c
blob: fea5cf420ec48cf81e7392349e01f821e5b60c70 (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/******************************************************************************
 * 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
 *****************************************************************************/

/*
 * 32-bit ELF loader
 */
#include <stdio.h> 
#include <string.h>
#include <libelf.h>
#include <byteorder.h>

struct ehdr32 {
	uint32_t ei_ident;
	uint8_t ei_class;
	uint8_t ei_data;
	uint8_t ei_version;
	uint8_t ei_pad[9];
	uint16_t e_type;
	uint16_t e_machine;
	uint32_t e_version;
	uint32_t e_entry;
	uint32_t e_phoff;
	uint32_t e_shoff;
	uint32_t e_flags;
	uint16_t e_ehsize;
	uint16_t e_phentsize;
	uint16_t e_phnum;
	uint16_t e_shentsize;
	uint16_t e_shnum;
	uint16_t e_shstrndx;
};

struct phdr32 {
	uint32_t p_type;
	uint32_t p_offset;
	uint32_t p_vaddr;
	uint32_t p_paddr;
	uint32_t p_filesz;
	uint32_t p_memsz;
	uint32_t p_flags;
	uint32_t p_align;
};


static struct phdr32*
get_phdr32(void *file_addr)
{
	return (struct phdr32 *) (((unsigned char *)file_addr)
		+ ((struct ehdr32 *)file_addr)->e_phoff);
}

static void
load_segment(void *file_addr, struct phdr32 *phdr, signed long offset,
             int (*pre_load)(void*, long),
             void (*post_load)(void*, long))
{
	unsigned long src = phdr->p_offset + (unsigned long) file_addr;
	unsigned long destaddr;

	destaddr = (unsigned long)phdr->p_paddr;
	destaddr = destaddr + offset;

	/* check if we're allowed to copy */
	if (pre_load != NULL) {
		if (pre_load((void*)destaddr, phdr->p_memsz) != 0)
			return;
	}

	/* copy into storage */
	memmove((void *)destaddr, (void *)src, phdr->p_filesz);

	/* clear bss */
	memset((void *)(destaddr + phdr->p_filesz), 0,
	       phdr->p_memsz - phdr->p_filesz);

	if (phdr->p_memsz && post_load) {
		post_load((void*)destaddr, phdr->p_memsz);
	}
}

unsigned int
elf_load_segments32(void *file_addr, signed long offset,
                    int (*pre_load)(void*, long),
                    void (*post_load)(void*, long))
{
	struct ehdr32 *ehdr = (struct ehdr32 *) file_addr;
	/* Calculate program header address */
	struct phdr32 *phdr = get_phdr32(file_addr);
	int i;

	/* loop e_phnum times */
	for (i = 0; i <= ehdr->e_phnum; i++) {
		/* PT_LOAD ? */
		if (phdr->p_type == 1) {
			if (phdr->p_paddr != phdr->p_vaddr) {
				printf("ELF32: VirtAddr(%lx) != PhysAddr(%lx) not supported, aborting\n",
					(long)phdr->p_vaddr, (long)phdr->p_paddr);
				return 0;
			}

			/* copy segment */
			load_segment(file_addr, phdr, offset, pre_load,
			             post_load);
		}
		/* step to next header */
		phdr = (struct phdr32 *)(((uint8_t *)phdr) + ehdr->e_phentsize);
	}

	/* Entry point is always a virtual address, so translate it
	 * to physical before returning it */
	return ehdr->e_entry;
}

/**
 * Return the base address for loading (i.e. the address of the first PT_LOAD
 * segment)
 * @param  file_addr	pointer to the ELF file in memory
 * @return		the base address
 */
long
elf_get_base_addr32(void *file_addr)
{
	struct ehdr32 *ehdr = (struct ehdr32 *) file_addr;
	struct phdr32 *phdr = get_phdr32(file_addr);
	int i;

	/* loop e_phnum times */
	for (i = 0; i <= ehdr->e_phnum; i++) {
		/* PT_LOAD ? */
		if (phdr->p_type == 1) {
			return phdr->p_paddr;
		}
		/* step to next header */
		phdr = (struct phdr32 *)(((uint8_t *)phdr) + ehdr->e_phentsize);
	}

	return 0;
}

uint32_t elf_get_eflags_32(void *file_addr)
{
	struct ehdr32 *ehdr = (struct ehdr32 *) file_addr;

	return ehdr->e_flags;
}

void
elf_byteswap_header32(void *file_addr)
{
	struct ehdr32 *ehdr = (struct ehdr32 *) file_addr;
	struct phdr32 *phdr;
	int i;

	bswap_16p(&ehdr->e_type);
	bswap_16p(&ehdr->e_machine);
	bswap_32p(&ehdr->e_version);
	bswap_32p(&ehdr->e_entry);
	bswap_32p(&ehdr->e_phoff);
	bswap_32p(&ehdr->e_shoff);
	bswap_32p(&ehdr->e_flags);
	bswap_16p(&ehdr->e_ehsize);
	bswap_16p(&ehdr->e_phentsize);
	bswap_16p(&ehdr->e_phnum);
	bswap_16p(&ehdr->e_shentsize);
	bswap_16p(&ehdr->e_shnum);
	bswap_16p(&ehdr->e_shstrndx);

	phdr = get_phdr32(file_addr);

	/* loop e_phnum times */
	for (i = 0; i <= ehdr->e_phnum; i++) {
		bswap_32p(&phdr->p_type);
		bswap_32p(&phdr->p_offset);
		bswap_32p(&phdr->p_vaddr);
		bswap_32p(&phdr->p_paddr);
		bswap_32p(&phdr->p_filesz);
		bswap_32p(&phdr->p_memsz);
		bswap_32p(&phdr->p_flags);
		bswap_32p(&phdr->p_align);

		/* step to next header */
		phdr = (struct phdr32 *)(((uint8_t *)phdr) + ehdr->e_phentsize);
	}
}