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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26 #include <sys/types.h>
31 #include "ao-verbose.h"
34 * Look through the Elf file for symbols that can be adjusted before
35 * the image is written to the device
37 static struct ao_sym *
38 load_symbols (Elf *e, int *num_symbolsp)
41 Elf_Data *symbol_data = NULL;
47 struct ao_sym *symbols = NULL;
48 struct ao_sym *symbol;
52 if (elf_getshdrstrndx(e, &shstrndx) < 0)
60 while ((scn = elf_nextscn(e, scn)) != NULL) {
62 if (gelf_getshdr(scn, &shdr) != &shdr)
65 if (shdr.sh_type == SHT_SYMTAB) {
66 symbol_data = elf_getdata(scn, NULL);
67 symbol_count = shdr.sh_size / shdr.sh_entsize;
75 for (i = 0; i < symbol_count; i++) {
76 gelf_getsym(symbol_data, i, &sym);
78 symbol_name = elf_strptr(e, shdr.sh_link, sym.st_name);
82 if (num_symbols == size_symbols) {
83 struct ao_sym *new_symbols;
89 new_size = size_symbols * 2;
90 new_symbols = realloc(symbols, new_size * sizeof (struct ao_sym));
94 symbols = new_symbols;
95 size_symbols = new_size;
97 symbol = &symbols[num_symbols];
98 memset(symbol, 0, sizeof (struct ao_sym));
99 symbol->name = strdup(symbol_name);
102 symbol->addr = sym.st_value;
103 ao_printf(AO_VERBOSE_EXE, "Add symbol %s: %08x\n", symbol->name, symbol->addr);
106 *num_symbolsp = num_symbols;
109 for (i = 0; i < num_symbols; i++)
110 free(symbols[i].name);
120 static struct ao_hex_image *
121 new_load (uint32_t addr, uint32_t len)
123 struct ao_hex_image *new;
126 new = calloc (1, sizeof (struct ao_hex_image) + len);
136 load_paste(struct ao_hex_image *into, struct ao_hex_image *from)
138 if (from->address < into->address || into->address + into->length < from->address + from->length)
141 memcpy(into->data + from->address - into->address, from->data, from->length);
145 * Make a new load structure large enough to hold the old one and
148 static struct ao_hex_image *
149 expand_load(struct ao_hex_image *from, uint32_t address, uint32_t length)
151 struct ao_hex_image *new;
154 uint32_t from_last = from->address + from->length;
155 uint32_t last = address + length;
157 if (address > from->address)
158 address = from->address;
159 if (last < from_last)
162 length = last - address;
164 if (address == from->address && length == from->length)
167 new = new_load(address, length);
169 load_paste(new, from);
176 * Create a new load structure with data from the existing one
179 static struct ao_hex_image *
180 load_write(struct ao_hex_image *from, uint32_t address, uint32_t length, void *data)
182 struct ao_hex_image *new;
184 new = expand_load(from, address, length);
185 memcpy(new->data + address - new->address, data, length);
191 * Construct a large in-memory block for all
192 * of the loaded sections of the program
194 static struct ao_hex_image *
205 struct ao_hex_image *load = NULL;
212 if (elf_getshdrstrndx(e, &shstrndx) < 0)
215 if (elf_getphdrnum(e, &nphdr) < 0)
218 if (elf_getshdrnum(e, &nshdr) < 0)
222 * As far as I can tell, all of the phdr sections should
223 * be flashed to memory
225 for (p = 0; p < nphdr; p++) {
228 gelf_getphdr(e, p, &phdr);
230 if (phdr.p_type != PT_LOAD)
233 /* Get the associated file section */
236 fprintf (stderr, "offset %08x vaddr %08x paddr %08x filesz %08x memsz %08x\n",
237 (uint32_t) phdr.p_offset,
238 (uint32_t) phdr.p_vaddr,
239 (uint32_t) phdr.p_paddr,
240 (uint32_t) phdr.p_filesz,
241 (uint32_t) phdr.p_memsz);
244 for (s = 0; s < nshdr; s++) {
245 scn = elf_getscn(e, s);
248 fprintf (stderr, "getscn failed\n");
251 if (gelf_getshdr(scn, &shdr) != &shdr) {
252 fprintf (stderr, "gelf_getshdr failed\n");
257 section_name = elf_strptr(e, shstrndx, shdr.sh_name);
260 if (shdr.sh_size != 0 && shdr.sh_type != SHT_NOBITS && (shdr.sh_flags & SHF_ALLOC) &&
261 phdr.p_offset <= shdr.sh_offset && shdr.sh_offset < phdr.p_offset + phdr.p_filesz)
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");