doc: Work on AltosUI Pyro config docs a bit more.
[fw/altos] / ao-tools / ao-stmload / ao-elf.c
1 /*
2  * Copyright © 2013 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 #include "ao-elf.h"
19 #include <err.h>
20 #include <gelf.h>
21 #include <stdio.h>
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <string.h>
26 #include "ccdbg.h"
27 #include "ao-stmload.h"
28
29 /*
30  * Look through the Elf file for the AltOS symbols
31  * that can be adjusted before the image is written
32  * to the device
33  */
34 static int
35 find_symbols (Elf *e)
36 {
37         Elf_Scn         *scn;
38         Elf_Data        *symbol_data = NULL;
39         GElf_Shdr       shdr;
40         GElf_Sym        sym;
41         int             i, symbol_count, s;
42         int             required = 0;
43         char            *symbol_name;
44         char            *section_name;
45         size_t          shstrndx;
46
47         if (elf_getshdrstrndx(e, &shstrndx) < 0)
48                 return 0;
49
50         /*
51          * Find the symbols
52          */
53
54         scn = NULL;
55         while ((scn = elf_nextscn(e, scn)) != NULL) {
56
57                 if (gelf_getshdr(scn, &shdr) != &shdr)
58                         return 0;
59
60                 if (shdr.sh_type == SHT_SYMTAB) {
61                         symbol_data = elf_getdata(scn, NULL);
62                         symbol_count = shdr.sh_size / shdr.sh_entsize;
63                         break;
64                 }
65         }
66
67         if (!symbol_data)
68                 return 0;
69
70         for (i = 0; i < symbol_count; i++) {
71                 gelf_getsym(symbol_data, i, &sym);
72
73                 symbol_name = elf_strptr(e, shdr.sh_link, sym.st_name);
74
75                 for (s = 0; s < ao_num_symbols; s++)
76                         if (!strcmp (ao_symbols[s].name, symbol_name)) {
77                                 int     t;
78                                 ao_symbols[s].addr = sym.st_value;
79                                 if (ao_symbols[s].required)
80                                         ++required;
81                         }
82         }
83
84         return required >= ao_num_required_symbols;
85 }
86
87 uint32_t round4(uint32_t a) {
88         return (a + 3) & ~3;
89 }
90
91 struct hex_image *
92 new_load (uint32_t addr, uint32_t len)
93 {
94         struct hex_image *new;
95
96         len = round4(len);
97         new = calloc (1, sizeof (struct hex_image) + len);
98         if (!new)
99                 abort();
100
101         new->address = addr;
102         new->length = len;
103         return new;
104 }
105
106 void
107 load_paste(struct hex_image *into, struct hex_image *from)
108 {
109         if (from->address < into->address || into->address + into->length < from->address + from->length)
110                 abort();
111
112         memcpy(into->data + from->address - into->address, from->data, from->length);
113 }
114
115 /*
116  * Make a new load structure large enough to hold the old one and
117  * the new data
118  */
119 struct hex_image *
120 expand_load(struct hex_image *from, uint32_t address, uint32_t length)
121 {
122         struct hex_image        *new;
123
124         if (from) {
125                 uint32_t        from_last = from->address + from->length;
126                 uint32_t        last = address + length;
127
128                 if (address > from->address)
129                         address = from->address;
130                 if (last < from_last)
131                         last = from_last;
132
133                 length = last - address;
134
135                 if (address == from->address && length == from->length)
136                         return from;
137         }
138         new = new_load(address, length);
139         if (from) {
140                 load_paste(new, from);
141                 free (from);
142         }
143         return new;
144 }
145
146 /*
147  * Create a new load structure with data from the existing one
148  * and the new data
149  */
150 struct hex_image *
151 load_write(struct hex_image *from, uint32_t address, uint32_t length, void *data)
152 {
153         struct hex_image        *new;
154
155         new = expand_load(from, address, length);
156         memcpy(new->data + address - new->address, data, length);
157         return new;
158 }
159
160 /*
161  * Construct a large in-memory block for all
162  * of the loaded sections of the program
163  */
164 static struct hex_image *
165 get_load(Elf *e)
166 {
167         Elf_Scn         *scn;
168         size_t          shstrndx;
169         GElf_Shdr       shdr;
170         Elf_Data        *data;
171         char            *got_name;
172         size_t          nphdr;
173         size_t          p;
174         GElf_Phdr       phdr;
175         GElf_Addr       p_paddr;
176         GElf_Off        p_offset;
177         GElf_Addr       sh_paddr;
178         struct hex_image        *load = NULL;
179         char            *section_name;
180         size_t          nshdr;
181         size_t          s;
182         
183         if (elf_getshdrstrndx(e, &shstrndx) < 0)
184                 return 0;
185
186         if (elf_getphdrnum(e, &nphdr) < 0)
187                 return 0;
188
189         if (elf_getshdrnum(e, &nshdr) < 0)
190                 return 0;
191
192         /*
193          * As far as I can tell, all of the phdr sections should
194          * be flashed to memory
195          */
196         for (p = 0; p < nphdr; p++) {
197
198                 /* Find this phdr */
199                 gelf_getphdr(e, p, &phdr);
200
201                 if (phdr.p_type != PT_LOAD)
202                         continue;
203
204                 p_offset = phdr.p_offset;
205                 /* Get the associated file section */
206
207 #if 0
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);
214 #endif
215                 
216                 for (s = 0; s < nshdr; s++) {
217                         scn = elf_getscn(e, s);
218
219                         if (!scn) {
220                                 printf ("getscn failed\n");
221                                 abort();
222                         }
223                         if (gelf_getshdr(scn, &shdr) != &shdr) {
224                                 printf ("gelf_getshdr failed\n");
225                                 abort();
226                         }
227
228                         section_name = elf_strptr(e, shstrndx, shdr.sh_name);
229
230                         if (phdr.p_offset <= shdr.sh_offset && shdr.sh_offset < phdr.p_offset + phdr.p_filesz) {
231                                         
232                                 if (shdr.sh_size == 0)
233                                         continue;
234
235                                 sh_paddr = phdr.p_paddr + shdr.sh_offset - phdr.p_offset;
236
237                                 printf ("\tsize %08x rom %08x exec %08x %s\n",
238                                         (uint32_t) shdr.sh_size,
239                                         (uint32_t) sh_paddr,
240                                         (uint32_t) shdr.sh_addr,
241                                         section_name);
242
243                                 data = elf_getdata(scn, NULL);
244
245                                 /* Write the section data into the memory block */
246                                 load = load_write(load, sh_paddr, shdr.sh_size, data->d_buf);
247                         }
248                 }
249         }
250         return load;
251 }
252
253 /*
254  * Open the specified ELF file and
255  * check for the symbols we need
256  */
257
258 struct hex_image *
259 ao_load_elf(char *name)
260 {
261         int             fd;
262         Elf             *e;
263         Elf_Scn         *scn;
264         Elf_Data        *symbol_data = NULL;
265         GElf_Shdr       shdr;
266         GElf_Sym        sym;
267         size_t          n, shstrndx, sz;
268         int             i, symbol_count, s;
269         int             required = 0;
270         struct hex_image        *image;
271
272         if (elf_version(EV_CURRENT) == EV_NONE)
273                 return NULL;
274
275         fd = open(name, O_RDONLY, 0);
276
277         if (fd < 0)
278                 return NULL;
279
280         e = elf_begin(fd, ELF_C_READ, NULL);
281
282         if (!e)
283                 return NULL;
284
285         if (elf_kind(e) != ELF_K_ELF)
286                 return NULL;
287
288         if (elf_getshdrstrndx(e, &shstrndx) != 0)
289                 return NULL;
290
291         if (!find_symbols(e)) {
292                 fprintf (stderr, "Cannot find required symbols\n");
293                 return NULL;
294         }
295
296         image = get_load(e);
297         if (!image) {
298                 fprintf (stderr, "Cannot create memory image from file\n");
299                 return NULL;
300         }
301
302         return image;
303 }