2 * Copyright © 2013 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
27 #include "ao-stmload.h"
30 * Look through the Elf file for the AltOS symbols
31 * that can be adjusted before the image is written
38 Elf_Data *symbol_data = NULL;
41 int i, symbol_count, s;
47 if (elf_getshdrstrndx(e, &shstrndx) < 0)
55 while ((scn = elf_nextscn(e, scn)) != NULL) {
57 if (gelf_getshdr(scn, &shdr) != &shdr)
60 if (shdr.sh_type == SHT_SYMTAB) {
61 symbol_data = elf_getdata(scn, NULL);
62 symbol_count = shdr.sh_size / shdr.sh_entsize;
70 for (i = 0; i < symbol_count; i++) {
71 gelf_getsym(symbol_data, i, &sym);
73 symbol_name = elf_strptr(e, shdr.sh_link, sym.st_name);
75 for (s = 0; s < ao_num_symbols; s++)
76 if (!strcmp (ao_symbols[s].name, symbol_name)) {
78 ao_symbols[s].addr = sym.st_value;
79 if (ao_symbols[s].required)
84 return required >= ao_num_required_symbols;
87 uint32_t round4(uint32_t a) {
92 new_load (uint32_t addr, uint32_t len)
94 struct hex_image *new;
97 new = calloc (1, sizeof (struct hex_image) + len);
107 load_paste(struct hex_image *into, struct hex_image *from)
109 if (from->address < into->address || into->address + into->length < from->address + from->length)
112 memcpy(into->data + from->address - into->address, from->data, from->length);
116 * Make a new load structure large enough to hold the old one and
120 expand_load(struct hex_image *from, uint32_t address, uint32_t length)
122 struct hex_image *new;
125 uint32_t from_last = from->address + from->length;
126 uint32_t last = address + length;
128 if (address > from->address)
129 address = from->address;
130 if (last < from_last)
133 length = last - address;
135 if (address == from->address && length == from->length)
138 new = new_load(address, length);
140 load_paste(new, from);
147 * Create a new load structure with data from the existing one
151 load_write(struct hex_image *from, uint32_t address, uint32_t length, void *data)
153 struct hex_image *new;
155 new = expand_load(from, address, length);
156 memcpy(new->data + address - new->address, data, length);
161 * Construct a large in-memory block for all
162 * of the loaded sections of the program
164 static struct hex_image *
178 struct hex_image *load = NULL;
183 if (elf_getshdrstrndx(e, &shstrndx) < 0)
186 if (elf_getphdrnum(e, &nphdr) < 0)
189 if (elf_getshdrnum(e, &nshdr) < 0)
193 * As far as I can tell, all of the phdr sections should
194 * be flashed to memory
196 for (p = 0; p < nphdr; p++) {
199 gelf_getphdr(e, p, &phdr);
201 if (phdr.p_type != PT_LOAD)
204 p_offset = phdr.p_offset;
205 /* Get the associated file section */
208 printf ("offset %08x vaddr %08x paddr %08x filesz %08x memsz %08x\n",
209 (uint32_t) phdr.p_offset,
210 (uint32_t) phdr.p_vaddr,
211 (uint32_t) phdr.p_paddr,
212 (uint32_t) phdr.p_filesz,
213 (uint32_t) phdr.p_memsz);
216 for (s = 0; s < nshdr; s++) {
217 scn = elf_getscn(e, s);
220 printf ("getscn failed\n");
223 if (gelf_getshdr(scn, &shdr) != &shdr) {
224 printf ("gelf_getshdr failed\n");
228 section_name = elf_strptr(e, shstrndx, shdr.sh_name);
230 if (phdr.p_offset <= shdr.sh_offset && shdr.sh_offset < phdr.p_offset + phdr.p_filesz) {
232 if (shdr.sh_size == 0)
235 sh_paddr = phdr.p_paddr + shdr.sh_offset - phdr.p_offset;
237 printf ("\tsize %08x rom %08x exec %08x %s\n",
238 (uint32_t) shdr.sh_size,
240 (uint32_t) shdr.sh_addr,
243 data = elf_getdata(scn, NULL);
245 /* Write the section data into the memory block */
246 load = load_write(load, sh_paddr, shdr.sh_size, data->d_buf);
254 * Open the specified ELF file and
255 * check for the symbols we need
259 ao_load_elf(char *name)
264 Elf_Data *symbol_data = NULL;
267 size_t n, shstrndx, sz;
268 int i, symbol_count, s;
270 struct hex_image *image;
272 if (elf_version(EV_CURRENT) == EV_NONE)
275 fd = open(name, O_RDONLY, 0);
280 e = elf_begin(fd, ELF_C_READ, NULL);
285 if (elf_kind(e) != ELF_K_ELF)
288 if (elf_getshdrstrndx(e, &shstrndx) != 0)
291 if (!find_symbols(e)) {
292 fprintf (stderr, "Cannot find required symbols\n");
298 fprintf (stderr, "Cannot create memory image from file\n");