od-elf32_avr.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /* od-avrelf.c -- dump information about an AVR elf object file.
  2. Copyright (C) 2011-2022 Free Software Foundation, Inc.
  3. Written by Senthil Kumar Selvaraj, Atmel.
  4. This file is part of GNU Binutils.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, 51 Franklin Street - Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. #include "sysdep.h"
  18. #include <stddef.h>
  19. #include <time.h>
  20. #include <stdint.h>
  21. #include "safe-ctype.h"
  22. #include "bfd.h"
  23. #include "objdump.h"
  24. #include "bucomm.h"
  25. #include "bfdlink.h"
  26. #include "bfd.h"
  27. #include "elf/external.h"
  28. #include "elf/internal.h"
  29. #include "elf32-avr.h"
  30. /* Index of the options in the options[] array. */
  31. #define OPT_MEMUSAGE 0
  32. #define OPT_AVRPROP 1
  33. /* List of actions. */
  34. static struct objdump_private_option options[] =
  35. {
  36. { "mem-usage", 0 },
  37. { "avr-prop", 0},
  38. { NULL, 0 }
  39. };
  40. /* Display help. */
  41. static void
  42. elf32_avr_help (FILE *stream)
  43. {
  44. fprintf (stream, _("\
  45. For AVR ELF files:\n\
  46. mem-usage Display memory usage\n\
  47. avr-prop Display contents of .avr.prop section\n\
  48. "));
  49. }
  50. typedef struct tagDeviceInfo
  51. {
  52. uint32_t flash_start;
  53. uint32_t flash_size;
  54. uint32_t ram_start;
  55. uint32_t ram_size;
  56. uint32_t eeprom_start;
  57. uint32_t eeprom_size;
  58. char * name;
  59. } deviceinfo;
  60. /* Return TRUE if ABFD is handled. */
  61. static int
  62. elf32_avr_filter (bfd *abfd)
  63. {
  64. return bfd_get_flavour (abfd) == bfd_target_elf_flavour;
  65. }
  66. static char *
  67. elf32_avr_get_note_section_contents (bfd *abfd, bfd_size_type *size)
  68. {
  69. asection *section;
  70. bfd_byte *contents;
  71. section = bfd_get_section_by_name (abfd, ".note.gnu.avr.deviceinfo");
  72. if (section == NULL)
  73. return NULL;
  74. if (!bfd_malloc_and_get_section (abfd, section, &contents))
  75. {
  76. free (contents);
  77. contents = NULL;
  78. }
  79. *size = bfd_section_size (section);
  80. return (char *) contents;
  81. }
  82. static char *
  83. elf32_avr_get_note_desc (bfd *abfd, char *contents, bfd_size_type size,
  84. bfd_size_type *descsz)
  85. {
  86. Elf_External_Note *xnp = (Elf_External_Note *) contents;
  87. Elf_Internal_Note in;
  88. if (offsetof (Elf_External_Note, name) > size)
  89. return NULL;
  90. in.type = bfd_get_32 (abfd, xnp->type);
  91. in.namesz = bfd_get_32 (abfd, xnp->namesz);
  92. in.namedata = xnp->name;
  93. if (in.namesz > contents - in.namedata + size)
  94. return NULL;
  95. if (in.namesz != 4 || strcmp (in.namedata, "AVR") != 0)
  96. return NULL;
  97. in.descsz = bfd_get_32 (abfd, xnp->descsz);
  98. in.descdata = in.namedata + align_power (in.namesz, 2);
  99. if (in.descsz < 6 * sizeof (uint32_t)
  100. || in.descdata >= contents + size
  101. || in.descsz > contents - in.descdata + size)
  102. return NULL;
  103. /* If the note has a string table, ensure it is 0 terminated. */
  104. if (in.descsz > 8 * sizeof (uint32_t))
  105. in.descdata[in.descsz - 1] = 0;
  106. *descsz = in.descsz;
  107. return in.descdata;
  108. }
  109. static void
  110. elf32_avr_get_device_info (bfd *abfd, char *description,
  111. bfd_size_type desc_size, deviceinfo *device)
  112. {
  113. if (description == NULL)
  114. return;
  115. const bfd_size_type memory_sizes = 6;
  116. memcpy (device, description, memory_sizes * sizeof (uint32_t));
  117. desc_size -= memory_sizes * sizeof (uint32_t);
  118. if (desc_size < 8)
  119. return;
  120. uint32_t *stroffset_table = (uint32_t *) description + memory_sizes;
  121. bfd_size_type stroffset_table_size = bfd_get_32 (abfd, stroffset_table);
  122. /* If the only content is the size itself, there's nothing in the table */
  123. if (stroffset_table_size < 8)
  124. return;
  125. if (desc_size <= stroffset_table_size)
  126. return;
  127. desc_size -= stroffset_table_size;
  128. /* First entry is the device name index. */
  129. uint32_t device_name_index = bfd_get_32 (abfd, stroffset_table + 1);
  130. if (device_name_index >= desc_size)
  131. return;
  132. char *str_table = (char *) stroffset_table + stroffset_table_size;
  133. device->name = str_table + device_name_index;
  134. }
  135. static void
  136. elf32_avr_get_memory_usage (bfd *abfd,
  137. bfd_size_type *text_usage,
  138. bfd_size_type *data_usage,
  139. bfd_size_type *eeprom_usage)
  140. {
  141. bfd_size_type avr_datasize = 0;
  142. bfd_size_type avr_textsize = 0;
  143. bfd_size_type avr_bsssize = 0;
  144. bfd_size_type bootloadersize = 0;
  145. bfd_size_type noinitsize = 0;
  146. bfd_size_type eepromsize = 0;
  147. bfd_size_type res;
  148. asection *section;
  149. if ((section = bfd_get_section_by_name (abfd, ".data")) != NULL)
  150. avr_datasize = bfd_section_size (section);
  151. if ((section = bfd_get_section_by_name (abfd, ".text")) != NULL)
  152. avr_textsize = bfd_section_size (section);
  153. if ((section = bfd_get_section_by_name (abfd, ".bss")) != NULL)
  154. avr_bsssize = bfd_section_size (section);
  155. if ((section = bfd_get_section_by_name (abfd, ".bootloader")) != NULL)
  156. bootloadersize = bfd_section_size (section);
  157. if ((section = bfd_get_section_by_name (abfd, ".noinit")) != NULL)
  158. noinitsize = bfd_section_size (section);
  159. if ((section = bfd_get_section_by_name (abfd, ".eeprom")) != NULL)
  160. eepromsize = bfd_section_size (section);
  161. /* PR 27285: Check for overflow. */
  162. res = avr_textsize + avr_datasize;
  163. if (res < avr_textsize || res < avr_datasize)
  164. {
  165. fprintf (stderr, _("Warning: textsize (%#lx) + datasize (%#lx) overflows size type\n"),
  166. (long) avr_textsize, (long) avr_datasize);
  167. res = (bfd_size_type) -1;
  168. }
  169. else
  170. {
  171. bfd_size_type res2;
  172. res2 = res + bootloadersize;
  173. if (res2 < bootloadersize || res2 < res)
  174. {
  175. fprintf (stderr, _("Warning: textsize (%#lx) + datasize (%#lx) + bootloadersize (%#lx) overflows size type\n"),
  176. (long) avr_textsize, (long) avr_datasize, (long) bootloadersize);
  177. res2 = (bfd_size_type) -1;
  178. }
  179. res = res2;
  180. }
  181. *text_usage = res;
  182. res = avr_datasize + avr_bsssize;
  183. if (res < avr_datasize || res < avr_bsssize)
  184. {
  185. fprintf (stderr, _("Warning: datatsize (%#lx) + bssssize (%#lx) overflows size type\n"),
  186. (long) avr_datasize, (long) avr_bsssize);
  187. res = (bfd_size_type) -1;
  188. }
  189. else
  190. {
  191. bfd_size_type res2;
  192. res2 = res + noinitsize;
  193. if (res2 < res || res2 < noinitsize)
  194. {
  195. fprintf (stderr, _("Warning: datasize (%#lx) + bsssize (%#lx) + noinitsize (%#lx) overflows size type\n"),
  196. (long) avr_datasize, (long) avr_bsssize, (long) noinitsize);
  197. res2 = (bfd_size_type) -1;
  198. }
  199. res = res2;
  200. }
  201. *data_usage = res;
  202. *eeprom_usage = eepromsize;
  203. }
  204. static void
  205. elf32_avr_dump_mem_usage (bfd *abfd)
  206. {
  207. char *description = NULL;
  208. bfd_size_type sec_size, desc_size;
  209. deviceinfo device = { 0, 0, 0, 0, 0, 0, NULL };
  210. device.name = "Unknown";
  211. bfd_size_type data_usage = 0;
  212. bfd_size_type text_usage = 0;
  213. bfd_size_type eeprom_usage = 0;
  214. char *contents = elf32_avr_get_note_section_contents (abfd, &sec_size);
  215. if (contents != NULL)
  216. {
  217. description = elf32_avr_get_note_desc (abfd, contents, sec_size,
  218. &desc_size);
  219. elf32_avr_get_device_info (abfd, description, desc_size, &device);
  220. }
  221. elf32_avr_get_memory_usage (abfd, &text_usage, &data_usage,
  222. &eeprom_usage);
  223. printf ("AVR Memory Usage\n"
  224. "----------------\n"
  225. "Device: %s\n\n", device.name);
  226. /* Text size */
  227. printf ("Program:%8lu bytes", text_usage);
  228. if (device.flash_size > 0)
  229. printf (" (%2.1f%% Full)", ((float) text_usage / device.flash_size) * 100);
  230. printf ("\n(.text + .data + .bootloader)\n\n");
  231. /* Data size */
  232. printf ("Data: %8lu bytes", data_usage);
  233. if (device.ram_size > 0)
  234. printf (" (%2.1f%% Full)", ((float) data_usage / device.ram_size) * 100);
  235. printf ("\n(.data + .bss + .noinit)\n\n");
  236. /* EEPROM size */
  237. if (eeprom_usage > 0)
  238. {
  239. printf ("EEPROM: %8lu bytes", eeprom_usage);
  240. if (device.eeprom_size > 0)
  241. printf (" (%2.1f%% Full)", ((float) eeprom_usage / device.eeprom_size) * 100);
  242. printf ("\n(.eeprom)\n\n");
  243. }
  244. if (contents != NULL)
  245. free (contents);
  246. }
  247. static void
  248. elf32_avr_dump_avr_prop (bfd *abfd)
  249. {
  250. struct avr_property_record_list *r_list;
  251. unsigned int i;
  252. r_list = avr_elf32_load_property_records (abfd);
  253. if (r_list == NULL)
  254. return;
  255. printf ("\nContents of `%s' section:\n\n", r_list->section->name);
  256. printf (" Version: %d\n", r_list->version);
  257. printf (" Flags: %#x\n\n", r_list->flags);
  258. for (i = 0; i < r_list->record_count; ++i)
  259. {
  260. printf (" %d %s @ %s + %#08lx (%#08lx)\n",
  261. i,
  262. avr_elf32_property_record_name (&r_list->records [i]),
  263. r_list->records [i].section->name,
  264. r_list->records [i].offset,
  265. (bfd_section_vma (r_list->records [i].section)
  266. + r_list->records [i].offset));
  267. switch (r_list->records [i].type)
  268. {
  269. case RECORD_ORG:
  270. /* Nothing else to print. */
  271. break;
  272. case RECORD_ORG_AND_FILL:
  273. printf (" Fill: %#08lx\n",
  274. r_list->records [i].data.org.fill);
  275. break;
  276. case RECORD_ALIGN:
  277. printf (" Align: %#08lx\n",
  278. r_list->records [i].data.align.bytes);
  279. break;
  280. case RECORD_ALIGN_AND_FILL:
  281. printf (" Align: %#08lx, Fill: %#08lx\n",
  282. r_list->records [i].data.align.bytes,
  283. r_list->records [i].data.align.fill);
  284. break;
  285. }
  286. }
  287. free (r_list);
  288. }
  289. static void
  290. elf32_avr_dump (bfd *abfd)
  291. {
  292. if (options[OPT_MEMUSAGE].selected)
  293. elf32_avr_dump_mem_usage (abfd);
  294. if (options[OPT_AVRPROP].selected)
  295. elf32_avr_dump_avr_prop (abfd);
  296. }
  297. const struct objdump_private_desc objdump_private_desc_elf32_avr =
  298. {
  299. elf32_avr_help,
  300. elf32_avr_filter,
  301. elf32_avr_dump,
  302. options
  303. };