Fixed multiple warnings for -Wall
[simavr] / simavr / sim / sim_elf.c
1 /*
2         sim_elf.c
3
4         Loads a .elf file, extract the code, the data, the eeprom and
5         the "mcu" specification section, also load usable code symbols
6         to be able to print meaningful trace information.
7
8         Copyright 2008, 2009 Michel Pollet <buserror@gmail.com>
9
10         This file is part of simavr.
11
12         simavr is free software: you can redistribute it and/or modify
13         it under the terms of the GNU General Public License as published by
14         the Free Software Foundation, either version 3 of the License, or
15         (at your option) any later version.
16
17         simavr is distributed in the hope that it will be useful,
18         but WITHOUT ANY WARRANTY; without even the implied warranty of
19         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20         GNU General Public License for more details.
21
22         You should have received a copy of the GNU General Public License
23         along with simavr.  If not, see <http://www.gnu.org/licenses/>.
24  */
25
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <libelf.h>
33 #include <gelf.h>
34
35 #include "sim_elf.h"
36 #include "sim_vcd_file.h"
37 #include "avr_eeprom.h"
38
39 void avr_load_firmware(avr_t * avr, elf_firmware_t * firmware)
40 {
41         avr->frequency = firmware->frequency;
42 #if CONFIG_SIMAVR_TRACE
43         avr->codeline = firmware->codeline;
44 #endif
45         avr_loadcode(avr, firmware->flash, firmware->flashsize, firmware->flashbase);
46         avr->codeend = firmware->flashsize + firmware->flashbase - firmware->datasize;
47         if (firmware->eeprom && firmware->eesize) {
48                 avr_eeprom_desc_t d = { .ee = firmware->eeprom, .offset = 0, .size = firmware->eesize };
49                 avr_ioctl(avr, AVR_IOCTL_EEPROM_SET, &d);
50         }
51         avr_set_command_register(avr, firmware->command_register_addr);
52         if (firmware->tracecount == 0)
53                 return;
54         avr->vcd = malloc(sizeof(*avr->vcd));
55         memset(avr->vcd, 0, sizeof(*avr->vcd));
56         avr_vcd_init(avr, 
57                 firmware->tracename[0] ? firmware->tracename: "gtkwave_trace.vcd",
58                 avr->vcd,
59                 firmware->traceperiod >= 1000 ? firmware->traceperiod : 1000);
60         
61         printf("Creating VCD trace file '%s'\n", avr->vcd->filename);
62         for (int ti = 0; ti < firmware->tracecount; ti++) {
63                 if (firmware->trace[ti].mask == 0xff || firmware->trace[ti].mask == 0) {
64                         // easy one
65                         avr_irq_t * all = avr_iomem_getirq(avr, firmware->trace[ti].addr, AVR_IOMEM_IRQ_ALL);
66                         if (!all) {
67                                 printf("%s: unable to attach trace to address %04x\n",
68                                         __FUNCTION__, firmware->trace[ti].addr);
69                         } else {
70                                 avr_vcd_add_signal(avr->vcd, all, 8, firmware->trace[ti].name);
71                         }
72                 } else {
73                         int count = 0;
74                         for (int bi = 0; bi < 8; bi++)
75                                 if (firmware->trace[ti].mask & (1 << bi))
76                                         count++;
77                         for (int bi = 0; bi < 8; bi++)
78                                 if (firmware->trace[ti].mask & (1 << bi)) {
79                                         avr_irq_t * bit = avr_iomem_getirq(avr, firmware->trace[ti].addr, bi);
80                                         if (!bit) {
81                                                 printf("%s: unable to attach trace to address %04x\n",
82                                                         __FUNCTION__, firmware->trace[ti].addr);
83                                                 break;
84                                         }
85                                         
86                                         if (count == 1) {
87                                                 avr_vcd_add_signal(avr->vcd, bit, 1, firmware->trace[ti].name);
88                                                 break;
89                                         }
90                                         char comp[128];
91                                         sprintf(comp, "%s.%d", firmware->trace[ti].name, bi);
92                                         avr_vcd_add_signal(avr->vcd, bit, 1, firmware->trace[ti].name);                                 
93                                 }
94                 }
95         }
96         // if the firmware has specified a command register, do NOT start the trace here
97         // the firmware probably knows best when to start/stop it
98         if (!firmware->command_register_addr)
99                 avr_vcd_start(avr->vcd);
100 }
101
102 static void elf_parse_mmcu_section(elf_firmware_t * firmware, uint8_t * src, uint32_t size)
103 {
104         while (size) {
105                 uint8_t tag = *src++;
106                 uint8_t ts = *src++;
107                 int next = size > 2 + ts ? 2 + ts : size;
108         //      printf("elf_parse_mmcu_section %d, %d / %d\n", tag, ts, size);
109                 switch (tag) {
110                         case AVR_MMCU_TAG_FREQUENCY:
111                                 firmware->frequency =
112                                         src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
113                                 break;
114                         case AVR_MMCU_TAG_NAME:
115                                 strcpy(firmware->mmcu, (char*)src);
116                                 break;          
117                         case AVR_MMCU_TAG_VCD_TRACE: {
118                                 uint8_t mask = src[0];
119                                 uint16_t addr = src[1] | (src[2] << 8);
120                                 char * name = (char*)src + 3;
121                                 printf("AVR_MMCU_TAG_VCD_TRACE %04x:%02x - %s\n", addr, mask, name);
122                                 firmware->trace[firmware->tracecount].mask = mask;
123                                 firmware->trace[firmware->tracecount].addr = addr;
124                                 strncpy(firmware->trace[firmware->tracecount].name, name, 
125                                         sizeof(firmware->trace[firmware->tracecount].name));
126                                 firmware->tracecount++;
127                         }       break;
128                         case AVR_MMCU_TAG_VCD_FILENAME: {
129                                 strcpy(firmware->tracename, (char*)src);
130                         }       break;
131                         case AVR_MMCU_TAG_VCD_PERIOD: {
132                                 firmware->traceperiod =
133                                         src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
134                         }       break;
135                         case AVR_MMCU_TAG_SIMAVR_COMMAND: {
136                                 firmware->command_register_addr = src[0] | (src[1] << 8);
137                         }       break;
138                 }
139                 size -= next;
140                 src += next - 2; // already incremented
141         }
142 }
143
144 int elf_read_firmware(const char * file, elf_firmware_t * firmware)
145 {
146         Elf32_Ehdr elf_header;                  /* ELF header */
147         Elf *elf = NULL;                       /* Our Elf pointer for libelf */
148         int fd; // File Descriptor
149
150         if ((fd = open(file, O_RDONLY)) == -1 ||
151                         (read(fd, &elf_header, sizeof(elf_header))) < sizeof(elf_header)) {
152                 printf("could not read %s\n", file);
153                 perror(file);
154                 close(fd);
155                 return -1;
156         }
157
158         Elf_Data *data_data = NULL, 
159                 *data_text = NULL,
160                 *data_ee = NULL;                /* Data Descriptor */
161
162         memset(firmware, 0, sizeof(*firmware));
163 #if ELF_SYMBOLS
164         //int bitesize = ((avr->flashend+1) >> 1) * sizeof(avr_symbol_t);
165         firmware->codesize = 32768;
166         int bitesize = firmware->codesize * sizeof(avr_symbol_t);
167         firmware->codeline = malloc(bitesize);
168         memset(firmware->codeline,0, bitesize);
169 #endif
170
171         /* this is actualy mandatory !! otherwise elf_begin() fails */
172         if (elf_version(EV_CURRENT) == EV_NONE) {
173                         /* library out of date - recover from error */
174         }
175         // Iterate through section headers again this time well stop when we find symbols
176         elf = elf_begin(fd, ELF_C_READ, NULL);
177         //printf("Loading elf %s : %p\n", file, elf);
178
179         Elf_Scn *scn = NULL;                   /* Section Descriptor */
180
181         while ((scn = elf_nextscn(elf, scn)) != NULL) {
182                 GElf_Shdr shdr;                 /* Section Header */
183                 gelf_getshdr(scn, &shdr);
184                 char * name = elf_strptr(elf, elf_header.e_shstrndx, shdr.sh_name);
185         //      printf("Walking elf section '%s'\n", name);
186
187                 if (!strcmp(name, ".text"))
188                         data_text = elf_getdata(scn, NULL);
189                 else if (!strcmp(name, ".data"))
190                         data_data = elf_getdata(scn, NULL);
191                 else if (!strcmp(name, ".eeprom"))
192                         data_ee = elf_getdata(scn, NULL);
193                 else if (!strcmp(name, ".bss")) {
194                         Elf_Data *s = elf_getdata(scn, NULL);
195                         firmware->bsssize = s->d_size;
196                 } else if (!strcmp(name, ".mmcu")) {
197                         Elf_Data *s = elf_getdata(scn, NULL);
198                         elf_parse_mmcu_section(firmware, s->d_buf, s->d_size);
199                         //printf("%s: avr_mcu_t size %ld / read %ld\n", __FUNCTION__, sizeof(struct avr_mcu_t), s->d_size);
200                 //      avr->frequency = f_cpu;
201                 }
202 #if ELF_SYMBOLS
203                 // When we find a section header marked SHT_SYMTAB stop and get symbols
204                 if (shdr.sh_type == SHT_SYMTAB) {
205                         // edata points to our symbol table
206                         Elf_Data *edata = elf_getdata(scn, NULL);
207
208                         // how many symbols are there? this number comes from the size of
209                         // the section divided by the entry size
210                         int symbol_count = shdr.sh_size / shdr.sh_entsize;
211
212                         // loop through to grab all symbols
213                         for (int i = 0; i < symbol_count; i++) {
214                                 GElf_Sym sym;                   /* Symbol */
215                                 // libelf grabs the symbol data using gelf_getsym()
216                                 gelf_getsym(edata, i, &sym);
217
218                                 // print out the value and size
219                         //      printf("%08x %08d ", sym.st_value, sym.st_size);
220                                 if (ELF32_ST_BIND(sym.st_info) == STB_GLOBAL || 
221                                                 ELF32_ST_TYPE(sym.st_info) == STT_FUNC || 
222                                                 ELF32_ST_TYPE(sym.st_info) == STT_OBJECT) {
223                                         const char * name = elf_strptr(elf, shdr.sh_link, sym.st_name);
224
225                                         // type of symbol
226                                         if (sym.st_value & 0xfff00000) {
227
228                                         } else {
229                                                 // code
230                                                 if (firmware->codeline[sym.st_value >> 1] == NULL) {
231                                                         avr_symbol_t * s = firmware->codeline[sym.st_value >> 1] = malloc(sizeof(avr_symbol_t*));
232                                                         s->symbol = strdup(name);
233                                                         s->addr = sym.st_value;
234                                                 }
235                                         }
236                                 }
237                         }
238                 }
239 #endif
240         }
241 #if ELF_SYMBOLS
242         avr_symbol_t * last = NULL;
243         for (int i = 0; i < firmware->codesize; i++) {
244                 if (!firmware->codeline[i])
245                         firmware->codeline[i] = last;
246                 else
247                         last = firmware->codeline[i];
248         }
249 #endif
250         uint32_t offset = 0;
251         firmware->flashsize =
252                         (data_text ? data_text->d_size : 0) +
253                         (data_data ? data_data->d_size : 0);
254         firmware->flash = malloc(firmware->flashsize);
255         if (data_text) {
256         //      hdump("code", data_text->d_buf, data_text->d_size);
257                 memcpy(firmware->flash + offset, data_text->d_buf, data_text->d_size);
258                 offset += data_text->d_size;
259                 printf("Loaded %d .text\n", data_text->d_size);
260         }
261         if (data_data) {
262         //      hdump("data", data_data->d_buf, data_data->d_size);
263                 memcpy(firmware->flash + offset, data_data->d_buf, data_data->d_size);
264                 printf("Loaded %d .data\n", data_data->d_size);
265                 offset += data_data->d_size;
266                 firmware->datasize = data_data->d_size;
267         }
268         if (data_ee) {
269         //      hdump("eeprom", data_ee->d_buf, data_ee->d_size);
270                 firmware->eeprom = malloc(data_ee->d_size);
271                 memcpy(firmware->eeprom, data_ee->d_buf, data_ee->d_size);
272                 printf("Loaded %d .eeprom\n", data_ee->d_size);
273                 firmware->eesize = data_ee->d_size;
274         }
275 //      hdump("flash", avr->flash, offset);
276         elf_end(elf);
277         close(fd);
278         return 0;
279 }
280