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>
32 * Look through the Elf file for symbols that can be adjusted before
33 * the image is written to the device
36 find_symbols (Elf *e, struct ao_elf_sym *symbols, int num_symbols)
39 Elf_Data *symbol_data = NULL;
42 int i, symbol_count, s;
46 if (elf_getshdrstrndx(e, &shstrndx) < 0)
54 while ((scn = elf_nextscn(e, scn)) != NULL) {
56 if (gelf_getshdr(scn, &shdr) != &shdr)
59 if (shdr.sh_type == SHT_SYMTAB) {
60 symbol_data = elf_getdata(scn, NULL);
61 symbol_count = shdr.sh_size / shdr.sh_entsize;
69 for (i = 0; i < symbol_count; i++) {
70 gelf_getsym(symbol_data, i, &sym);
72 symbol_name = elf_strptr(e, shdr.sh_link, sym.st_name);
74 for (s = 0; s < num_symbols; s++)
75 if (!strcmp (symbols[s].name, symbol_name)) {
76 symbols[s].addr = sym.st_value;
77 symbols[s].found = true;
80 for (s = 0; s < num_symbols; s++)
81 if (symbols[s].required && !symbols[s].found)
91 static struct ao_hex_image *
92 new_load (uint32_t addr, uint32_t len)
94 struct ao_hex_image *new;
97 new = calloc (1, sizeof (struct ao_hex_image) + len);
107 load_paste(struct ao_hex_image *into, struct ao_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
119 static struct ao_hex_image *
120 expand_load(struct ao_hex_image *from, uint32_t address, uint32_t length)
122 struct ao_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
150 static struct ao_hex_image *
151 load_write(struct ao_hex_image *from, uint32_t address, uint32_t length, void *data)
153 struct ao_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 ao_hex_image *
175 struct ao_hex_image *load = NULL;
180 if (elf_getshdrstrndx(e, &shstrndx) < 0)
183 if (elf_getphdrnum(e, &nphdr) < 0)
186 if (elf_getshdrnum(e, &nshdr) < 0)
190 * As far as I can tell, all of the phdr sections should
191 * be flashed to memory
193 for (p = 0; p < nphdr; p++) {
196 gelf_getphdr(e, p, &phdr);
198 if (phdr.p_type != PT_LOAD)
201 /* Get the associated file section */
204 printf ("offset %08x vaddr %08x paddr %08x filesz %08x memsz %08x\n",
205 (uint32_t) phdr.p_offset,
206 (uint32_t) phdr.p_vaddr,
207 (uint32_t) phdr.p_paddr,
208 (uint32_t) phdr.p_filesz,
209 (uint32_t) phdr.p_memsz);
212 for (s = 0; s < nshdr; s++) {
213 scn = elf_getscn(e, s);
216 printf ("getscn failed\n");
219 if (gelf_getshdr(scn, &shdr) != &shdr) {
220 printf ("gelf_getshdr failed\n");
224 section_name = elf_strptr(e, shstrndx, shdr.sh_name);
226 if (phdr.p_offset <= shdr.sh_offset && shdr.sh_offset < phdr.p_offset + phdr.p_filesz) {
228 if (shdr.sh_size == 0)
231 sh_paddr = phdr.p_paddr + shdr.sh_offset - phdr.p_offset;
233 printf ("\tsize %08x rom %08x exec %08x %s\n",
234 (uint32_t) shdr.sh_size,
236 (uint32_t) shdr.sh_addr,
239 data = elf_getdata(scn, NULL);
241 /* Write the section data into the memory block */
242 load = load_write(load, sh_paddr, shdr.sh_size, data->d_buf);
250 * Open the specified ELF file and
251 * check for the symbols we need
254 struct ao_hex_image *
255 ao_load_elf(char *name, struct ao_elf_sym *symbols, int num_symbols)
260 struct ao_hex_image *image;
262 if (elf_version(EV_CURRENT) == EV_NONE)
265 fd = open(name, O_RDONLY, 0);
270 e = elf_begin(fd, ELF_C_READ, NULL);
275 if (elf_kind(e) != ELF_K_ELF)
278 if (elf_getshdrstrndx(e, &shstrndx) != 0)
281 if (!find_symbols(e, symbols, num_symbols)) {
282 fprintf (stderr, "Cannot find required symbols\n");
288 fprintf (stderr, "Cannot create memory image from file\n");