932dc85371b355a1e006f60c4011208c009e1cc2
[fw/altos] / ao-tools / lib / 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 <err.h>
19 #include <stdio.h>
20 #include <stdint.h>
21 #include <stdlib.h>
22 #include <stdbool.h>
23 #include <unistd.h>
24 #include <string.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include "ao-elf.h"
29 #include "ao-hex.h"
30
31 /*
32  * Look through the Elf file for symbols that can be adjusted before
33  * the image is written to the device
34  */
35 static bool
36 find_symbols (Elf *e, struct ao_elf_sym *symbols, int num_symbols)
37 {
38         Elf_Scn         *scn;
39         Elf_Data        *symbol_data = NULL;
40         GElf_Shdr       shdr;
41         GElf_Sym        sym;
42         int             i, symbol_count, s;
43         char            *symbol_name;
44         size_t          shstrndx;
45
46         if (elf_getshdrstrndx(e, &shstrndx) < 0)
47                 return false;
48
49         /*
50          * Find the symbols
51          */
52
53         scn = NULL;
54         while ((scn = elf_nextscn(e, scn)) != NULL) {
55
56                 if (gelf_getshdr(scn, &shdr) != &shdr)
57                         return false;
58
59                 if (shdr.sh_type == SHT_SYMTAB) {
60                         symbol_data = elf_getdata(scn, NULL);
61                         symbol_count = shdr.sh_size / shdr.sh_entsize;
62                         break;
63                 }
64         }
65
66         if (!symbol_data)
67                 return false;
68
69         for (i = 0; i < symbol_count; i++) {
70                 gelf_getsym(symbol_data, i, &sym);
71
72                 symbol_name = elf_strptr(e, shdr.sh_link, sym.st_name);
73
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;
78                         }
79         }
80         for (s = 0; s < num_symbols; s++)
81                 if (symbols[s].required && !symbols[s].found)
82                         return false;
83         return true;
84 }
85
86 static uint32_t
87 round4(uint32_t a) {
88         return (a + 3) & ~3;
89 }
90
91 static struct ao_hex_image *
92 new_load (uint32_t addr, uint32_t len)
93 {
94         struct ao_hex_image *new;
95
96         len = round4(len);
97         new = calloc (1, sizeof (struct ao_hex_image) + len);
98         if (!new)
99                 abort();
100
101         new->address = addr;
102         new->length = len;
103         return new;
104 }
105
106 static void
107 load_paste(struct ao_hex_image *into, struct ao_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 static struct ao_hex_image *
120 expand_load(struct ao_hex_image *from, uint32_t address, uint32_t length)
121 {
122         struct ao_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 static struct ao_hex_image *
151 load_write(struct ao_hex_image *from, uint32_t address, uint32_t length, void *data)
152 {
153         struct ao_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 ao_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         size_t          nphdr;
172         size_t          p;
173         GElf_Phdr       phdr;
174         GElf_Addr       sh_paddr;
175         struct ao_hex_image     *load = NULL;
176         char            *section_name;
177         size_t          nshdr;
178         size_t          s;
179         
180         if (elf_getshdrstrndx(e, &shstrndx) < 0)
181                 return 0;
182
183         if (elf_getphdrnum(e, &nphdr) < 0)
184                 return 0;
185
186         if (elf_getshdrnum(e, &nshdr) < 0)
187                 return 0;
188
189         /*
190          * As far as I can tell, all of the phdr sections should
191          * be flashed to memory
192          */
193         for (p = 0; p < nphdr; p++) {
194
195                 /* Find this phdr */
196                 gelf_getphdr(e, p, &phdr);
197
198                 if (phdr.p_type != PT_LOAD)
199                         continue;
200
201                 /* Get the associated file section */
202
203 #if 0
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);
210 #endif
211                 
212                 for (s = 0; s < nshdr; s++) {
213                         scn = elf_getscn(e, s);
214
215                         if (!scn) {
216                                 printf ("getscn failed\n");
217                                 abort();
218                         }
219                         if (gelf_getshdr(scn, &shdr) != &shdr) {
220                                 printf ("gelf_getshdr failed\n");
221                                 abort();
222                         }
223
224                         section_name = elf_strptr(e, shstrndx, shdr.sh_name);
225
226                         if (phdr.p_offset <= shdr.sh_offset && shdr.sh_offset < phdr.p_offset + phdr.p_filesz) {
227                                         
228                                 if (shdr.sh_size == 0)
229                                         continue;
230
231                                 sh_paddr = phdr.p_paddr + shdr.sh_offset - phdr.p_offset;
232
233                                 printf ("\tsize %08x rom %08x exec %08x %s\n",
234                                         (uint32_t) shdr.sh_size,
235                                         (uint32_t) sh_paddr,
236                                         (uint32_t) shdr.sh_addr,
237                                         section_name);
238
239                                 data = elf_getdata(scn, NULL);
240
241                                 /* Write the section data into the memory block */
242                                 load = load_write(load, sh_paddr, shdr.sh_size, data->d_buf);
243                         }
244                 }
245         }
246         return load;
247 }
248
249 /*
250  * Open the specified ELF file and
251  * check for the symbols we need
252  */
253
254 struct ao_hex_image *
255 ao_load_elf(char *name, struct ao_elf_sym *symbols, int num_symbols)
256 {
257         int             fd;
258         Elf             *e;
259         size_t          shstrndx;
260         struct ao_hex_image     *image;
261
262         if (elf_version(EV_CURRENT) == EV_NONE)
263                 return NULL;
264
265         fd = open(name, O_RDONLY, 0);
266
267         if (fd < 0)
268                 return NULL;
269
270         e = elf_begin(fd, ELF_C_READ, NULL);
271
272         if (!e)
273                 return NULL;
274
275         if (elf_kind(e) != ELF_K_ELF)
276                 return NULL;
277
278         if (elf_getshdrstrndx(e, &shstrndx) != 0)
279                 return NULL;
280
281         if (!find_symbols(e, symbols, num_symbols)) {
282                 fprintf (stderr, "Cannot find required symbols\n");
283                 return NULL;
284         }
285
286         image = get_load(e);
287         if (!image) {
288                 fprintf (stderr, "Cannot create memory image from file\n");
289                 return NULL;
290         }
291
292         return image;
293 }