od-elf32_avr.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /* od-avrelf.c -- dump information about an AVR elf object file.
  2. Copyright (C) 2011-2015 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. if ((section = bfd_get_section_by_name (abfd, ".note.gnu.avr.deviceinfo")) == NULL)
  71. return NULL;
  72. *size = bfd_get_section_size (section);
  73. char *contents = (char *) xmalloc (*size);
  74. bfd_get_section_contents (abfd, section, contents, 0, *size);
  75. return contents;
  76. }
  77. static char* elf32_avr_get_note_desc (bfd *abfd, char *contents,
  78. bfd_size_type size)
  79. {
  80. Elf_External_Note *xnp = (Elf_External_Note *) contents;
  81. Elf_Internal_Note in;
  82. if (offsetof (Elf_External_Note, name) > size)
  83. return NULL;
  84. in.type = bfd_get_32 (abfd, xnp->type);
  85. in.namesz = bfd_get_32 (abfd, xnp->namesz);
  86. in.namedata = xnp->name;
  87. if (in.namesz > contents - in.namedata + size)
  88. return NULL;
  89. in.descsz = bfd_get_32 (abfd, xnp->descsz);
  90. in.descdata = in.namedata + align_power (in.namesz, 2);
  91. if (in.descsz != 0
  92. && (in.descdata >= contents + size
  93. || in.descsz > contents - in.descdata + size))
  94. return NULL;
  95. if (strcmp (in.namedata, "AVR") != 0)
  96. return NULL;
  97. return in.descdata;
  98. }
  99. static void
  100. elf32_avr_get_device_info (bfd *abfd, char *description,
  101. deviceinfo *device)
  102. {
  103. if (description == NULL)
  104. return;
  105. const bfd_size_type memory_sizes = 6;
  106. memcpy (device, description, memory_sizes * sizeof(uint32_t));
  107. device->name = NULL;
  108. uint32_t *stroffset_table = ((uint32_t *) description) + memory_sizes;
  109. bfd_size_type stroffset_table_size = bfd_get_32 (abfd, stroffset_table);
  110. char *str_table = ((char *) stroffset_table) + stroffset_table_size;
  111. /* If the only content is the size itself, there's nothing in the table */
  112. if (stroffset_table_size == 4)
  113. return;
  114. /* First entry is the device name index. */
  115. uint32_t device_name_index = bfd_get_32 (abfd, stroffset_table + 1);
  116. device->name = str_table + device_name_index;
  117. }
  118. static void
  119. elf32_avr_get_memory_usage (bfd *abfd,
  120. bfd_size_type *text_usage,
  121. bfd_size_type *data_usage,
  122. bfd_size_type *eeprom_usage)
  123. {
  124. bfd_size_type avr_datasize = 0;
  125. bfd_size_type avr_textsize = 0;
  126. bfd_size_type avr_bsssize = 0;
  127. bfd_size_type bootloadersize = 0;
  128. bfd_size_type noinitsize = 0;
  129. bfd_size_type eepromsize = 0;
  130. asection *section;
  131. if ((section = bfd_get_section_by_name (abfd, ".data")) != NULL)
  132. avr_datasize = bfd_section_size (abfd, section);
  133. if ((section = bfd_get_section_by_name (abfd, ".text")) != NULL)
  134. avr_textsize = bfd_section_size (abfd, section);
  135. if ((section = bfd_get_section_by_name (abfd, ".bss")) != NULL)
  136. avr_bsssize = bfd_section_size (abfd, section);
  137. if ((section = bfd_get_section_by_name (abfd, ".bootloader")) != NULL)
  138. bootloadersize = bfd_section_size (abfd, section);
  139. if ((section = bfd_get_section_by_name (abfd, ".noinit")) != NULL)
  140. noinitsize = bfd_section_size (abfd, section);
  141. if ((section = bfd_get_section_by_name (abfd, ".eeprom")) != NULL)
  142. eepromsize = bfd_section_size (abfd, section);
  143. *text_usage = avr_textsize + avr_datasize + bootloadersize;
  144. *data_usage = avr_datasize + avr_bsssize + noinitsize;
  145. *eeprom_usage = eepromsize;
  146. }
  147. static void
  148. elf32_avr_dump_mem_usage (bfd *abfd)
  149. {
  150. char *description = NULL;
  151. bfd_size_type note_section_size = 0;
  152. deviceinfo device = { 0, 0, 0, 0, 0, 0, NULL };
  153. device.name = "Unknown";
  154. bfd_size_type data_usage = 0;
  155. bfd_size_type text_usage = 0;
  156. bfd_size_type eeprom_usage = 0;
  157. char *contents = elf32_avr_get_note_section_contents (abfd,
  158. &note_section_size);
  159. if (contents != NULL)
  160. {
  161. description = elf32_avr_get_note_desc (abfd, contents, note_section_size);
  162. elf32_avr_get_device_info (abfd, description, &device);
  163. }
  164. elf32_avr_get_memory_usage (abfd, &text_usage, &data_usage,
  165. &eeprom_usage);
  166. printf ("AVR Memory Usage\n"
  167. "----------------\n"
  168. "Device: %s\n\n", device.name);
  169. /* Text size */
  170. printf ("Program:%8ld bytes", text_usage);
  171. if (device.flash_size > 0)
  172. printf (" (%2.1f%% Full)", ((float) text_usage / device.flash_size) * 100);
  173. printf ("\n(.text + .data + .bootloader)\n\n");
  174. /* Data size */
  175. printf ("Data: %8ld bytes", data_usage);
  176. if (device.ram_size > 0)
  177. printf (" (%2.1f%% Full)", ((float) data_usage / device.ram_size) * 100);
  178. printf ("\n(.data + .bss + .noinit)\n\n");
  179. /* EEPROM size */
  180. if (eeprom_usage > 0)
  181. {
  182. printf ("EEPROM: %8ld bytes", eeprom_usage);
  183. if (device.eeprom_size > 0)
  184. printf (" (%2.1f%% Full)", ((float) eeprom_usage / device.eeprom_size) * 100);
  185. printf ("\n(.eeprom)\n\n");
  186. }
  187. if (contents != NULL)
  188. free (contents);
  189. }
  190. static void
  191. elf32_avr_dump_avr_prop (bfd *abfd)
  192. {
  193. struct avr_property_record_list *r_list;
  194. unsigned int i;
  195. r_list = avr_elf32_load_property_records (abfd);
  196. if (r_list == NULL)
  197. return;
  198. printf ("\nContents of `%s' section:\n\n", r_list->section->name);
  199. printf (" Version: %d\n", r_list->version);
  200. printf (" Flags: %#x\n\n", r_list->flags);
  201. for (i = 0; i < r_list->record_count; ++i)
  202. {
  203. printf (" %d %s @ %s + %#08lx (%#08lx)\n",
  204. i,
  205. avr_elf32_property_record_name (&r_list->records [i]),
  206. r_list->records [i].section->name,
  207. r_list->records [i].offset,
  208. (bfd_get_section_vma (abfd, r_list->records [i].section)
  209. + r_list->records [i].offset));
  210. switch (r_list->records [i].type)
  211. {
  212. case RECORD_ORG:
  213. /* Nothing else to print. */
  214. break;
  215. case RECORD_ORG_AND_FILL:
  216. printf (" Fill: %#08lx\n",
  217. r_list->records [i].data.org.fill);
  218. break;
  219. case RECORD_ALIGN:
  220. printf (" Align: %#08lx\n",
  221. r_list->records [i].data.align.bytes);
  222. break;
  223. case RECORD_ALIGN_AND_FILL:
  224. printf (" Align: %#08lx, Fill: %#08lx\n",
  225. r_list->records [i].data.align.bytes,
  226. r_list->records [i].data.align.fill);
  227. break;
  228. }
  229. }
  230. free (r_list);
  231. }
  232. static void
  233. elf32_avr_dump (bfd *abfd)
  234. {
  235. if (options[OPT_MEMUSAGE].selected)
  236. elf32_avr_dump_mem_usage (abfd);
  237. if (options[OPT_AVRPROP].selected)
  238. elf32_avr_dump_avr_prop (abfd);
  239. }
  240. const struct objdump_private_desc objdump_private_desc_elf32_avr =
  241. {
  242. elf32_avr_help,
  243. elf32_avr_filter,
  244. elf32_avr_dump,
  245. options
  246. };