]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - binutils/od-elf32_avr.c
Automatic date update in version.in
[thirdparty/binutils-gdb.git] / binutils / od-elf32_avr.c
CommitLineData
2ebecbb1 1/* od-avrelf.c -- dump information about an AVR elf object file.
fd67aa11 2 Copyright (C) 2011-2024 Free Software Foundation, Inc.
2ebecbb1
DC
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"
137c83d6 34#include "elf32-avr.h"
2ebecbb1
DC
35
36/* Index of the options in the options[] array. */
37#define OPT_MEMUSAGE 0
137c83d6 38#define OPT_AVRPROP 1
2ebecbb1
DC
39
40/* List of actions. */
41static struct objdump_private_option options[] =
42 {
43 { "mem-usage", 0 },
137c83d6 44 { "avr-prop", 0},
2ebecbb1
DC
45 { NULL, 0 }
46 };
47
48/* Display help. */
49
50static void
51elf32_avr_help (FILE *stream)
52{
53 fprintf (stream, _("\
54For AVR ELF files:\n\
55 mem-usage Display memory usage\n\
137c83d6 56 avr-prop Display contents of .avr.prop section\n\
2ebecbb1
DC
57"));
58}
59
60typedef struct tagDeviceInfo
61{
62 uint32_t flash_start;
63 uint32_t flash_size;
64 uint32_t ram_start;
65 uint32_t ram_size;
66 uint32_t eeprom_start;
67 uint32_t eeprom_size;
68 char * name;
69} deviceinfo;
70
71
72/* Return TRUE if ABFD is handled. */
73
74static int
75elf32_avr_filter (bfd *abfd)
76{
77 return bfd_get_flavour (abfd) == bfd_target_elf_flavour;
78}
79
1cfcf300 80static char *
2ebecbb1
DC
81elf32_avr_get_note_section_contents (bfd *abfd, bfd_size_type *size)
82{
83 asection *section;
1cfcf300 84 bfd_byte *contents;
2ebecbb1 85
1cfcf300
AM
86 section = bfd_get_section_by_name (abfd, ".note.gnu.avr.deviceinfo");
87 if (section == NULL)
2ebecbb1
DC
88 return NULL;
89
1cfcf300
AM
90 if (!bfd_malloc_and_get_section (abfd, section, &contents))
91 {
92 free (contents);
93 contents = NULL;
94 }
2ebecbb1 95
1cfcf300
AM
96 *size = bfd_section_size (section);
97 return (char *) contents;
2ebecbb1
DC
98}
99
1cfcf300
AM
100static char *
101elf32_avr_get_note_desc (bfd *abfd, char *contents, bfd_size_type size,
102 bfd_size_type *descsz)
2ebecbb1
DC
103{
104 Elf_External_Note *xnp = (Elf_External_Note *) contents;
105 Elf_Internal_Note in;
106
107 if (offsetof (Elf_External_Note, name) > size)
108 return NULL;
109
110 in.type = bfd_get_32 (abfd, xnp->type);
111 in.namesz = bfd_get_32 (abfd, xnp->namesz);
112 in.namedata = xnp->name;
113 if (in.namesz > contents - in.namedata + size)
114 return NULL;
115
1cfcf300
AM
116 if (in.namesz != 4 || strcmp (in.namedata, "AVR") != 0)
117 return NULL;
118
2ebecbb1
DC
119 in.descsz = bfd_get_32 (abfd, xnp->descsz);
120 in.descdata = in.namedata + align_power (in.namesz, 2);
1cfcf300
AM
121 if (in.descsz < 6 * sizeof (uint32_t)
122 || in.descdata >= contents + size
123 || in.descsz > contents - in.descdata + size)
2ebecbb1
DC
124 return NULL;
125
1cfcf300
AM
126 /* If the note has a string table, ensure it is 0 terminated. */
127 if (in.descsz > 8 * sizeof (uint32_t))
128 in.descdata[in.descsz - 1] = 0;
2ebecbb1 129
1cfcf300 130 *descsz = in.descsz;
2ebecbb1
DC
131 return in.descdata;
132}
133
134static void
135elf32_avr_get_device_info (bfd *abfd, char *description,
1cfcf300 136 bfd_size_type desc_size, deviceinfo *device)
2ebecbb1
DC
137{
138 if (description == NULL)
139 return;
140
141 const bfd_size_type memory_sizes = 6;
142
1cfcf300
AM
143 memcpy (device, description, memory_sizes * sizeof (uint32_t));
144 desc_size -= memory_sizes * sizeof (uint32_t);
145 if (desc_size < 8)
146 return;
2ebecbb1 147
1cfcf300 148 uint32_t *stroffset_table = (uint32_t *) description + memory_sizes;
2ebecbb1 149 bfd_size_type stroffset_table_size = bfd_get_32 (abfd, stroffset_table);
2ebecbb1
DC
150
151 /* If the only content is the size itself, there's nothing in the table */
1cfcf300 152 if (stroffset_table_size < 8)
2ebecbb1 153 return;
1cfcf300
AM
154 if (desc_size <= stroffset_table_size)
155 return;
156 desc_size -= stroffset_table_size;
2ebecbb1
DC
157
158 /* First entry is the device name index. */
159 uint32_t device_name_index = bfd_get_32 (abfd, stroffset_table + 1);
1cfcf300
AM
160 if (device_name_index >= desc_size)
161 return;
2ebecbb1 162
1cfcf300 163 char *str_table = (char *) stroffset_table + stroffset_table_size;
2ebecbb1
DC
164 device->name = str_table + device_name_index;
165}
166
167static void
168elf32_avr_get_memory_usage (bfd *abfd,
9d3fcfe0
NC
169 bfd_size_type *text_usage,
170 bfd_size_type *data_usage,
171 bfd_size_type *eeprom_usage)
2ebecbb1
DC
172{
173
174 bfd_size_type avr_datasize = 0;
175 bfd_size_type avr_textsize = 0;
176 bfd_size_type avr_bsssize = 0;
177 bfd_size_type bootloadersize = 0;
178 bfd_size_type noinitsize = 0;
179 bfd_size_type eepromsize = 0;
9d3fcfe0 180 bfd_size_type res;
2ebecbb1
DC
181 asection *section;
182
183 if ((section = bfd_get_section_by_name (abfd, ".data")) != NULL)
fd361982 184 avr_datasize = bfd_section_size (section);
2ebecbb1 185 if ((section = bfd_get_section_by_name (abfd, ".text")) != NULL)
fd361982 186 avr_textsize = bfd_section_size (section);
2ebecbb1 187 if ((section = bfd_get_section_by_name (abfd, ".bss")) != NULL)
fd361982 188 avr_bsssize = bfd_section_size (section);
2ebecbb1 189 if ((section = bfd_get_section_by_name (abfd, ".bootloader")) != NULL)
fd361982 190 bootloadersize = bfd_section_size (section);
2ebecbb1 191 if ((section = bfd_get_section_by_name (abfd, ".noinit")) != NULL)
fd361982 192 noinitsize = bfd_section_size (section);
2ebecbb1 193 if ((section = bfd_get_section_by_name (abfd, ".eeprom")) != NULL)
fd361982 194 eepromsize = bfd_section_size (section);
2ebecbb1 195
9d3fcfe0
NC
196 /* PR 27285: Check for overflow. */
197 res = avr_textsize + avr_datasize;
198 if (res < avr_textsize || res < avr_datasize)
199 {
200 fprintf (stderr, _("Warning: textsize (%#lx) + datasize (%#lx) overflows size type\n"),
201 (long) avr_textsize, (long) avr_datasize);
202 res = (bfd_size_type) -1;
203 }
204 else
205 {
206 bfd_size_type res2;
207 res2 = res + bootloadersize;
208 if (res2 < bootloadersize || res2 < res)
209 {
210 fprintf (stderr, _("Warning: textsize (%#lx) + datasize (%#lx) + bootloadersize (%#lx) overflows size type\n"),
211 (long) avr_textsize, (long) avr_datasize, (long) bootloadersize);
212 res2 = (bfd_size_type) -1;
213 }
214 res = res2;
215 }
216 *text_usage = res;
217
218 res = avr_datasize + avr_bsssize;
219 if (res < avr_datasize || res < avr_bsssize)
220 {
221 fprintf (stderr, _("Warning: datatsize (%#lx) + bssssize (%#lx) overflows size type\n"),
222 (long) avr_datasize, (long) avr_bsssize);
223 res = (bfd_size_type) -1;
224 }
225 else
226 {
227 bfd_size_type res2;
228
229 res2 = res + noinitsize;
230 if (res2 < res || res2 < noinitsize)
231 {
232 fprintf (stderr, _("Warning: datasize (%#lx) + bsssize (%#lx) + noinitsize (%#lx) overflows size type\n"),
233 (long) avr_datasize, (long) avr_bsssize, (long) noinitsize);
234 res2 = (bfd_size_type) -1;
235 }
236 res = res2;
237 }
238 *data_usage = res;
239
2ebecbb1
DC
240 *eeprom_usage = eepromsize;
241}
242
243static void
244elf32_avr_dump_mem_usage (bfd *abfd)
245{
246 char *description = NULL;
1cfcf300 247 bfd_size_type sec_size, desc_size;
2ebecbb1 248
4e327239 249 deviceinfo device = { 0, 0, 0, 0, 0, 0, NULL };
2ebecbb1
DC
250 device.name = "Unknown";
251
252 bfd_size_type data_usage = 0;
253 bfd_size_type text_usage = 0;
254 bfd_size_type eeprom_usage = 0;
255
1cfcf300 256 char *contents = elf32_avr_get_note_section_contents (abfd, &sec_size);
2ebecbb1
DC
257
258 if (contents != NULL)
259 {
1cfcf300
AM
260 description = elf32_avr_get_note_desc (abfd, contents, sec_size,
261 &desc_size);
262 elf32_avr_get_device_info (abfd, description, desc_size, &device);
2ebecbb1
DC
263 }
264
265 elf32_avr_get_memory_usage (abfd, &text_usage, &data_usage,
9d3fcfe0 266 &eeprom_usage);
2ebecbb1
DC
267
268 printf ("AVR Memory Usage\n"
269 "----------------\n"
270 "Device: %s\n\n", device.name);
271
272 /* Text size */
b5c37946 273 printf ("Program:%8" PRIu64 " bytes", (uint64_t) text_usage);
2ebecbb1 274 if (device.flash_size > 0)
b5c37946 275 printf (" (%2.1f%% Full)", (double) text_usage / device.flash_size * 100);
2ebecbb1
DC
276
277 printf ("\n(.text + .data + .bootloader)\n\n");
278
279 /* Data size */
b5c37946 280 printf ("Data: %8" PRIu64 " bytes", (uint64_t) data_usage);
2ebecbb1 281 if (device.ram_size > 0)
b5c37946 282 printf (" (%2.1f%% Full)", (double) data_usage / device.ram_size * 100);
2ebecbb1
DC
283
284 printf ("\n(.data + .bss + .noinit)\n\n");
285
286 /* EEPROM size */
287 if (eeprom_usage > 0)
288 {
b5c37946 289 printf ("EEPROM: %8" PRIu64 " bytes", (uint64_t) eeprom_usage);
2ebecbb1 290 if (device.eeprom_size > 0)
b5c37946
SJ
291 printf (" (%2.1f%% Full)",
292 (double) eeprom_usage / device.eeprom_size * 100);
2ebecbb1
DC
293
294 printf ("\n(.eeprom)\n\n");
295 }
296
297 if (contents != NULL)
298 free (contents);
299
300}
301
137c83d6
AB
302static void
303elf32_avr_dump_avr_prop (bfd *abfd)
304{
305 struct avr_property_record_list *r_list;
306 unsigned int i;
307
308 r_list = avr_elf32_load_property_records (abfd);
309 if (r_list == NULL)
310 return;
311
312 printf ("\nContents of `%s' section:\n\n", r_list->section->name);
313
314 printf (" Version: %d\n", r_list->version);
315 printf (" Flags: %#x\n\n", r_list->flags);
316
317 for (i = 0; i < r_list->record_count; ++i)
318 {
b5c37946 319 printf (" %d %s @ %s + %#08" PRIx64" (%#08" PRIx64 ")\n",
fd361982
AM
320 i,
321 avr_elf32_property_record_name (&r_list->records [i]),
322 r_list->records [i].section->name,
b5c37946
SJ
323 (uint64_t) r_list->records [i].offset,
324 ((uint64_t) bfd_section_vma (r_list->records [i].section)
fd361982 325 + r_list->records [i].offset));
137c83d6
AB
326 switch (r_list->records [i].type)
327 {
328 case RECORD_ORG:
329 /* Nothing else to print. */
330 break;
331 case RECORD_ORG_AND_FILL:
332 printf (" Fill: %#08lx\n",
333 r_list->records [i].data.org.fill);
334 break;
335 case RECORD_ALIGN:
431ff075 336 printf (" Align: %#08lx\n",
137c83d6
AB
337 r_list->records [i].data.align.bytes);
338 break;
339 case RECORD_ALIGN_AND_FILL:
431ff075 340 printf (" Align: %#08lx, Fill: %#08lx\n",
137c83d6 341 r_list->records [i].data.align.bytes,
431ff075 342 r_list->records [i].data.align.fill);
137c83d6
AB
343 break;
344 }
345 }
346
347 free (r_list);
348}
349
2ebecbb1
DC
350static void
351elf32_avr_dump (bfd *abfd)
352{
353 if (options[OPT_MEMUSAGE].selected)
354 elf32_avr_dump_mem_usage (abfd);
137c83d6
AB
355 if (options[OPT_AVRPROP].selected)
356 elf32_avr_dump_avr_prop (abfd);
2ebecbb1
DC
357}
358
359const struct objdump_private_desc objdump_private_desc_elf32_avr =
360 {
361 elf32_avr_help,
362 elf32_avr_filter,
363 elf32_avr_dump,
364 options
365 };