+ if (dump_sections != NULL)
+ {
+ struct section_add * pdump;
+
+ for (pdump = dump_sections; pdump != NULL; pdump = pdump->next)
+ {
+ asection * sec;
+
+ sec = bfd_get_section_by_name (ibfd, pdump->name);
+ if (sec == NULL)
+ {
+ bfd_nonfatal_message (NULL, ibfd, NULL,
+ _("can't dump section '%s' - it does not exist"),
+ pdump->name);
+ continue;
+ }
+
+ if ((bfd_get_section_flags (ibfd, sec) & SEC_HAS_CONTENTS) == 0)
+ {
+ bfd_nonfatal_message (NULL, ibfd, sec,
+ _("can't dump section - it has no contents"));
+ continue;
+ }
+
+ bfd_size_type size = bfd_get_section_size (sec);
+ if (size == 0)
+ {
+ bfd_nonfatal_message (NULL, ibfd, sec,
+ _("can't dump section - it is empty"));
+ continue;
+ }
+
+ FILE * f;
+ f = fopen (pdump->filename, FOPEN_WB);
+ if (f == NULL)
+ {
+ bfd_nonfatal_message (pdump->filename, NULL, NULL,
+ _("could not open section dump file"));
+ continue;
+ }
+
+ bfd_byte * contents = xmalloc (size);
+ if (bfd_get_section_contents (ibfd, sec, contents, 0, size))
+ {
+ if (fwrite (contents, 1, size, f) != size)
+ fatal (_("error writing section contents to %s (error: %s)"),
+ pdump->filename,
+ strerror (errno));
+ }
+ else
+ bfd_nonfatal_message (NULL, ibfd, sec,
+ _("could not retrieve section contents"));
+
+ fclose (f);
+ free (contents);
+ }
+ }
+