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.
25 #include <sys/types.h>
30 #include "ao-verbose.h"
33 * Look through the Elf file for symbols that can be adjusted before
34 * the image is written to the device
36 static struct ao_sym *
37 load_symbols (Elf *e, int *num_symbolsp)
40 Elf_Data *symbol_data = NULL;
46 struct ao_sym *symbols = NULL;
47 struct ao_sym *symbol;
51 if (elf_getshdrstrndx(e, &shstrndx) < 0)
59 while ((scn = elf_nextscn(e, scn)) != NULL) {
61 if (gelf_getshdr(scn, &shdr) != &shdr)
64 if (shdr.sh_type == SHT_SYMTAB) {
65 symbol_data = elf_getdata(scn, NULL);
66 symbol_count = shdr.sh_size / shdr.sh_entsize;
74 for (i = 0; i < symbol_count; i++) {
75 gelf_getsym(symbol_data, i, &sym);
77 symbol_name = elf_strptr(e, shdr.sh_link, sym.st_name);
81 if (num_symbols == size_symbols) {
82 struct ao_sym *new_symbols;
88 new_size = size_symbols * 2;
89 new_symbols = realloc(symbols, new_size * sizeof (struct ao_sym));
93 symbols = new_symbols;
94 size_symbols = new_size;
96 symbol = &symbols[num_symbols];
97 memset(symbol, 0, sizeof (struct ao_sym));
98 symbol->name = strdup(symbol_name);
101 symbol->addr = sym.st_value;
102 ao_printf(AO_VERBOSE_EXE, "Add symbol %s: %08x\n", symbol->name, symbol->addr);
105 *num_symbolsp = num_symbols;
108 for (i = 0; i < num_symbols; i++)
109 free(symbols[i].name);
119 static struct ao_hex_image *
120 new_load (uint32_t addr, uint32_t len)
122 struct ao_hex_image *new;
125 new = calloc (1, sizeof (struct ao_hex_image) + len);
135 load_paste(struct ao_hex_image *into, struct ao_hex_image *from)
137 if (from->address < into->address || into->address + into->length < from->address + from->length)
140 memcpy(into->data + from->address - into->address, from->data, from->length);
144 * Make a new load structure large enough to hold the old one and
147 static struct ao_hex_image *
148 expand_load(struct ao_hex_image *from, uint32_t address, uint32_t length)
150 struct ao_hex_image *new;
153 uint32_t from_last = from->address + from->length;
154 uint32_t last = address + length;
156 if (address > from->address)
157 address = from->address;
158 if (last < from_last)
161 length = last - address;
163 if (address == from->address && length == from->length)
166 new = new_load(address, length);
168 load_paste(new, from);
175 * Create a new load structure with data from the existing one
178 static struct ao_hex_image *
179 load_write(struct ao_hex_image *from, uint32_t address, uint32_t length, void *data)
181 struct ao_hex_image *new;
183 new = expand_load(from, address, length);
184 memcpy(new->data + address - new->address, data, length);
189 * Construct a large in-memory block for all
190 * of the loaded sections of the program
192 static struct ao_hex_image *
203 struct ao_hex_image *load = NULL;
210 if (elf_getshdrstrndx(e, &shstrndx) < 0)
213 if (elf_getphdrnum(e, &nphdr) < 0)
216 if (elf_getshdrnum(e, &nshdr) < 0)
220 * As far as I can tell, all of the phdr sections should
221 * be flashed to memory
223 for (p = 0; p < nphdr; p++) {
226 gelf_getphdr(e, p, &phdr);
228 if (phdr.p_type != PT_LOAD)
231 /* Get the associated file section */
234 fprintf (stderr, "offset %08x vaddr %08x paddr %08x filesz %08x memsz %08x\n",
235 (uint32_t) phdr.p_offset,
236 (uint32_t) phdr.p_vaddr,
237 (uint32_t) phdr.p_paddr,
238 (uint32_t) phdr.p_filesz,
239 (uint32_t) phdr.p_memsz);
242 for (s = 0; s < nshdr; s++) {
243 scn = elf_getscn(e, s);
246 fprintf (stderr, "getscn failed\n");
249 if (gelf_getshdr(scn, &shdr) != &shdr) {
250 fprintf (stderr, "gelf_getshdr failed\n");
255 section_name = elf_strptr(e, shstrndx, shdr.sh_name);
258 if (phdr.p_offset <= shdr.sh_offset && shdr.sh_offset < phdr.p_offset + phdr.p_filesz) {
260 if (shdr.sh_size == 0)
263 sh_paddr = phdr.p_paddr + shdr.sh_offset - phdr.p_offset;
266 fprintf (stderr, "\tsize %08x rom %08x exec %08x %s\n",
267 (uint32_t) shdr.sh_size,
269 (uint32_t) shdr.sh_addr,
273 data = elf_getdata(scn, NULL);
275 /* Write the section data into the memory block */
276 load = load_write(load, sh_paddr, shdr.sh_size, data->d_buf);
284 * Open the specified ELF file and
285 * check for the symbols we need
288 struct ao_hex_image *
289 ao_load_elf(char *name, struct ao_sym **symbols, int *num_symbols)
294 struct ao_hex_image *image;
296 if (elf_version(EV_CURRENT) == EV_NONE)
299 fd = open(name, O_RDONLY, 0);
304 e = elf_begin(fd, ELF_C_READ, NULL);
309 if (elf_kind(e) != ELF_K_ELF)
312 if (elf_getshdrstrndx(e, &shstrndx) != 0)
316 *symbols = load_symbols(e, num_symbols);
320 fprintf (stderr, "Cannot create memory image from file\n");