+/* Read a section into its appropriate place in the dwarf2_debug
+ struct (indicated by SECTION_BUFFER and SECTION_SIZE). If SYMS is
+ not NULL, use bfd_simple_get_relocated_section_contents to read the
+ section contents, otherwise use bfd_get_section_contents. Fail if
+ the located section does not contain at least OFFSET bytes. */
+
+static bfd_boolean
+read_section (bfd * abfd,
+ const char * section_name,
+ const char * compressed_section_name,
+ asymbol ** syms,
+ bfd_uint64_t offset,
+ bfd_byte ** section_buffer,
+ bfd_size_type * section_size)
+{
+ asection *msec;
+ bfd_boolean section_is_compressed = FALSE;
+
+ /* read_section is a noop if the section has already been read. */
+ if (!*section_buffer)
+ {
+ msec = bfd_get_section_by_name (abfd, section_name);
+ if (! msec && compressed_section_name)
+ {
+ msec = bfd_get_section_by_name (abfd, compressed_section_name);
+ section_is_compressed = TRUE;
+ }
+ if (! msec)
+ {
+ (*_bfd_error_handler) (_("Dwarf Error: Can't find %s section."), section_name);
+ bfd_set_error (bfd_error_bad_value);
+ return FALSE;
+ }
+
+ *section_size = msec->rawsize ? msec->rawsize : msec->size;
+ if (syms)
+ {
+ *section_buffer
+ = bfd_simple_get_relocated_section_contents (abfd, msec, NULL, syms);
+ if (! *section_buffer)
+ return FALSE;
+ }
+ else
+ {
+ *section_buffer = bfd_malloc (*section_size);
+ if (! *section_buffer)
+ return FALSE;
+ if (! bfd_get_section_contents (abfd, msec, *section_buffer,
+ 0, *section_size))
+ return FALSE;
+ }
+
+ if (section_is_compressed)
+ {
+ if (! bfd_uncompress_section_contents (section_buffer, section_size))
+ {
+ (*_bfd_error_handler) (_("Dwarf Error: unable to decompress %s section."), compressed_section_name);
+ bfd_set_error (bfd_error_bad_value);
+ return FALSE;
+ }
+ }
+ }
+
+ /* It is possible to get a bad value for the offset into the section
+ that the client wants. Validate it here to avoid trouble later. */
+ if (offset != 0 && offset >= *section_size)
+ {
+ (*_bfd_error_handler) (_("Dwarf Error: Offset (%lu) greater than or equal to %s size (%lu)."),
+ (long) offset, section_name, *section_size);
+ bfd_set_error (bfd_error_bad_value);
+ return FALSE;
+ }
+
+ return TRUE;
+}
+