* od-elf32_avr.c: Forgot to add a new file.
[external/binutils.git] / binutils / od-elf32_avr.c
1 /* od-avrelf.c -- dump information about an AVR elf object file.
2    Copyright (C) 2011-2014 Free Software Foundation, Inc.
3    Written by Senthil Kumar Selvaraj, Atmel.
4
5    This file is part of GNU Binutils.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3, or (at your option)
10    any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, 51 Franklin Street - Fifth Floor, Boston,
20    MA 02110-1301, USA.  */
21
22 #include "sysdep.h"
23 #include <stddef.h>
24 #include <time.h>
25 #include <stdint.h>
26 #include "safe-ctype.h"
27 #include "bfd.h"
28 #include "objdump.h"
29 #include "bucomm.h"
30 #include "bfdlink.h"
31 #include "bfd.h"
32 #include "elf/external.h"
33 #include "elf/internal.h"
34
35 /* Index of the options in the options[] array.  */
36 #define OPT_MEMUSAGE 0
37
38 /* List of actions.  */
39 static struct objdump_private_option options[] =
40   {
41     { "mem-usage", 0 },
42     { NULL, 0 }
43   };
44
45 /* Display help.  */
46
47 static void
48 elf32_avr_help (FILE *stream)
49 {
50   fprintf (stream, _("\
51 For AVR ELF files:\n\
52   mem-usage   Display memory usage\n\
53 "));
54 }
55
56 typedef struct tagDeviceInfo
57 {
58     uint32_t flash_start;
59     uint32_t flash_size;
60     uint32_t ram_start;
61     uint32_t ram_size;
62     uint32_t eeprom_start;
63     uint32_t eeprom_size;
64     char * name;
65 } deviceinfo;
66
67
68 /* Return TRUE if ABFD is handled.  */
69
70 static int
71 elf32_avr_filter (bfd *abfd)
72 {
73   return bfd_get_flavour (abfd) == bfd_target_elf_flavour;
74 }
75
76 static char*
77 elf32_avr_get_note_section_contents (bfd *abfd, bfd_size_type *size)
78 {
79   asection *section;
80
81   if ((section = bfd_get_section_by_name (abfd, ".note.gnu.avr.deviceinfo")) == NULL)
82     return NULL;
83
84   *size = bfd_get_section_size (section);
85   char *contents = (char *) xmalloc (*size);
86   bfd_get_section_contents (abfd, section, contents, 0, *size);
87
88   return contents;
89 }
90
91 static char* elf32_avr_get_note_desc (bfd *abfd, char *contents,
92         bfd_size_type size)
93 {
94   Elf_External_Note *xnp = (Elf_External_Note *) contents;
95   Elf_Internal_Note in;
96
97   if (offsetof (Elf_External_Note, name) > size)
98     return NULL;
99
100   in.type = bfd_get_32 (abfd, xnp->type);
101   in.namesz = bfd_get_32 (abfd, xnp->namesz);
102   in.namedata = xnp->name;
103   if (in.namesz > contents - in.namedata + size)
104     return NULL;
105
106   in.descsz = bfd_get_32 (abfd, xnp->descsz);
107   in.descdata = in.namedata + align_power (in.namesz, 2);
108   if (in.descsz != 0
109         && (in.descdata >= contents + size
110             || in.descsz > contents - in.descdata + size))
111     return NULL;
112
113   if (strcmp (in.namedata, "AVR") != 0)
114     return NULL;
115
116   return in.descdata;
117 }
118
119 static void
120 elf32_avr_get_device_info (bfd *abfd, char *description,
121         deviceinfo *device)
122 {
123   if (description == NULL)
124     return;
125
126   const bfd_size_type memory_sizes = 6;
127
128   memcpy (device, description, memory_sizes * sizeof(uint32_t));
129   device->name = NULL;
130
131   uint32_t *stroffset_table = ((uint32_t *) description) + memory_sizes;
132   bfd_size_type stroffset_table_size = bfd_get_32 (abfd, stroffset_table);
133   char *str_table = ((char *) stroffset_table) + stroffset_table_size;
134
135   /* If the only content is the size itself, there's nothing in the table */
136   if (stroffset_table_size == 4)
137     return;
138
139   /* First entry is the device name index. */
140   uint32_t device_name_index = bfd_get_32 (abfd, stroffset_table + 1);
141
142   device->name = str_table + device_name_index;
143 }
144
145 static void
146 elf32_avr_get_memory_usage (bfd *abfd,
147         bfd_size_type *text_usage,
148         bfd_size_type *data_usage,
149         bfd_size_type *eeprom_usage)
150 {
151
152   bfd_size_type avr_datasize = 0;
153   bfd_size_type avr_textsize = 0;
154   bfd_size_type avr_bsssize = 0;
155   bfd_size_type bootloadersize = 0;
156   bfd_size_type noinitsize = 0;
157   bfd_size_type eepromsize = 0;
158   asection *section;
159
160   if ((section = bfd_get_section_by_name (abfd, ".data")) != NULL)
161     avr_datasize = bfd_section_size (abfd, section);
162   if ((section = bfd_get_section_by_name (abfd, ".text")) != NULL)
163     avr_textsize = bfd_section_size (abfd, section);
164   if ((section = bfd_get_section_by_name (abfd, ".bss")) != NULL)
165     avr_bsssize = bfd_section_size (abfd, section);
166   if ((section = bfd_get_section_by_name (abfd, ".bootloader")) != NULL)
167     bootloadersize = bfd_section_size (abfd, section);
168   if ((section = bfd_get_section_by_name (abfd, ".noinit")) != NULL)
169     noinitsize = bfd_section_size (abfd, section);
170   if ((section = bfd_get_section_by_name (abfd, ".eeprom")) != NULL)
171     eepromsize = bfd_section_size (abfd, section);
172
173   *text_usage = avr_textsize + avr_datasize + bootloadersize;
174   *data_usage = avr_datasize + avr_bsssize + noinitsize;
175   *eeprom_usage = eepromsize;
176 }
177
178 static void
179 elf32_avr_dump_mem_usage (bfd *abfd)
180 {
181   char *description = NULL;
182   bfd_size_type note_section_size = 0;
183
184   deviceinfo device = {0};
185   device.name = "Unknown";
186
187   bfd_size_type data_usage = 0;
188   bfd_size_type text_usage = 0;
189   bfd_size_type eeprom_usage = 0;
190
191   char *contents = elf32_avr_get_note_section_contents (abfd,
192     &note_section_size);
193
194   if (contents != NULL)
195     {
196       description = elf32_avr_get_note_desc (abfd, contents, note_section_size);
197       elf32_avr_get_device_info (abfd, description, &device);
198     }
199
200   elf32_avr_get_memory_usage (abfd, &text_usage, &data_usage,
201      &eeprom_usage);
202
203   printf ("AVR Memory Usage\n"
204           "----------------\n"
205           "Device: %s\n\n", device.name);
206
207   /* Text size */
208   printf ("Program:%8ld bytes", text_usage);
209   if (device.flash_size > 0)
210     printf (" (%2.1f%% Full)", ((float) text_usage / device.flash_size) * 100);
211
212   printf ("\n(.text + .data + .bootloader)\n\n");
213
214   /* Data size */
215   printf ("Data:   %8ld bytes", data_usage);
216   if (device.ram_size > 0)
217     printf (" (%2.1f%% Full)", ((float) data_usage / device.ram_size) * 100);
218
219   printf ("\n(.data + .bss + .noinit)\n\n");
220
221   /* EEPROM size */
222   if (eeprom_usage > 0)
223     {
224       printf ("EEPROM: %8ld bytes", eeprom_usage);
225       if (device.eeprom_size > 0)
226         printf (" (%2.1f%% Full)", ((float) eeprom_usage / device.eeprom_size) * 100);
227
228       printf ("\n(.eeprom)\n\n");
229     }
230
231   if (contents != NULL)
232     free (contents);
233
234 }
235
236 static void
237 elf32_avr_dump (bfd *abfd)
238 {
239   if (options[OPT_MEMUSAGE].selected)
240     elf32_avr_dump_mem_usage (abfd);
241 }
242
243 const struct objdump_private_desc objdump_private_desc_elf32_avr =
244   {
245     elf32_avr_help,
246     elf32_avr_filter,
247     elf32_avr_dump,
248     options
249   };