static int do_notes;
static int do_archive_index;
static int is_32bit_elf;
+static int decompress_dumps;
struct group_list
{
{"hex-dump", required_argument, 0, 'x'},
{"relocated-dump", required_argument, 0, 'R'},
{"string-dump", required_argument, 0, 'p'},
+ {"decompress", no_argument, 0, 'z'},
#ifdef SUPPORT_DISASSEMBLY
{"instruction-dump", required_argument, 0, 'i'},
#endif
Dump the contents of section <number|name> as strings\n\
-R --relocated-dump=<number|name>\n\
Dump the contents of section <number|name> as relocated bytes\n\
+ -z --decompress Decompress section before dumping it\n\
-w[lLiaprmfFsoRt] or\n\
--debug-dump[=rawline,=decodedline,=info,=abbrev,=pubnames,=aranges,=macro,=frames,\n\
=frames-interp,=str,=loc,=Ranges,=pubtypes,\n\
usage (stderr);
while ((c = getopt_long
- (argc, argv, "ADHINR:SVWacdeghi:lnp:rstuvw::x:", options, NULL)) != EOF)
+ (argc, argv, "ADHINR:SVWacdeghi:lnp:rstuvw::x:z", options, NULL)) != EOF)
{
switch (c)
{
case 'R':
request_dump (RELOC_DUMP);
break;
+ case 'z':
+ decompress_dumps++;
+ break;
case 'w':
do_dump++;
if (optarg == 0)
_("section contents"));
}
+/* Uncompresses a section that was compressed using zlib, in place. */
+
+static bfd_boolean
+uncompress_section_contents (unsigned char **buffer,
+ dwarf_size_type uncompressed_size,
+ dwarf_size_type *size)
+{
+ dwarf_size_type compressed_size = *size;
+ unsigned char * compressed_buffer = *buffer;
+ unsigned char * uncompressed_buffer;
+ z_stream strm;
+ int rc;
+
+ /* It is possible the section consists of several compressed
+ buffers concatenated together, so we uncompress in a loop. */
+ /* PR 18313: The state field in the z_stream structure is supposed
+ to be invisible to the user (ie us), but some compilers will
+ still complain about it being used without initialisation. So
+ we first zero the entire z_stream structure and then set the fields
+ that we need. */
+ memset (& strm, 0, sizeof strm);
+ strm.avail_in = compressed_size;
+ strm.next_in = (Bytef *) compressed_buffer;
+ strm.avail_out = uncompressed_size;
+ uncompressed_buffer = (unsigned char *) xmalloc (uncompressed_size);
+
+ rc = inflateInit (& strm);
+ while (strm.avail_in > 0)
+ {
+ if (rc != Z_OK)
+ goto fail;
+ strm.next_out = ((Bytef *) uncompressed_buffer
+ + (uncompressed_size - strm.avail_out));
+ rc = inflate (&strm, Z_FINISH);
+ if (rc != Z_STREAM_END)
+ goto fail;
+ rc = inflateReset (& strm);
+ }
+ rc = inflateEnd (& strm);
+ if (rc != Z_OK
+ || strm.avail_out != 0)
+ goto fail;
+
+ *buffer = uncompressed_buffer;
+ *size = uncompressed_size;
+ return TRUE;
+
+ fail:
+ free (uncompressed_buffer);
+ /* Indicate decompression failure. */
+ *buffer = NULL;
+ return FALSE;
+}
static void
dump_section_as_strings (Elf_Internal_Shdr * section, FILE * file)
{
- Elf_Internal_Shdr * relsec;
- bfd_size_type num_bytes;
- char * data;
- char * end;
- char * start;
- bfd_boolean some_strings_shown;
-
- start = get_section_contents (section, file);
+ Elf_Internal_Shdr * relsec;
+ bfd_size_type num_bytes;
+ char * data;
+ char * end;
+ char * real_start;
+ char * start;
+ bfd_boolean some_strings_shown;
+
+ real_start = start = get_section_contents (section, file);
if (start == NULL)
return;
+ num_bytes = section->sh_size;
printf (_("\nString dump of section '%s':\n"), printable_section_name (section));
+ if (decompress_dumps)
+ {
+ dwarf_size_type new_size = num_bytes;
+ dwarf_size_type uncompressed_size = 0;
+
+ if ((section->sh_flags & SHF_COMPRESSED) != 0)
+ {
+ Elf_Internal_Chdr chdr;
+ unsigned int compression_header_size
+ = get_compression_header (& chdr, (unsigned char *) start);
+
+ if (chdr.ch_type == ELFCOMPRESS_ZLIB
+ && chdr.ch_addralign == section->sh_addralign)
+ {
+ uncompressed_size = chdr.ch_size;
+ start += compression_header_size;
+ new_size -= compression_header_size;
+ }
+ }
+ else if (new_size > 12 && streq ((char *) start, "ZLIB"))
+ {
+ /* Read the zlib header. In this case, it should be "ZLIB"
+ followed by the uncompressed section size, 8 bytes in
+ big-endian order. */
+ uncompressed_size = start[4]; uncompressed_size <<= 8;
+ uncompressed_size += start[5]; uncompressed_size <<= 8;
+ uncompressed_size += start[6]; uncompressed_size <<= 8;
+ uncompressed_size += start[7]; uncompressed_size <<= 8;
+ uncompressed_size += start[8]; uncompressed_size <<= 8;
+ uncompressed_size += start[9]; uncompressed_size <<= 8;
+ uncompressed_size += start[10]; uncompressed_size <<= 8;
+ uncompressed_size += start[11];
+ start += 12;
+ new_size -= 12;
+ }
+
+ if (uncompressed_size
+ && uncompress_section_contents ((unsigned char **) & start,
+ uncompressed_size, & new_size))
+ num_bytes = new_size;
+ }
+
/* If the section being dumped has relocations against it the user might
be expecting these relocations to have been applied. Check for this
case and issue a warning message in order to avoid confusion.
break;
}
- num_bytes = section->sh_size;
data = start;
end = start + num_bytes;
some_strings_shown = FALSE;
if (! some_strings_shown)
printf (_(" No strings found in this section."));
- free (start);
+ free (real_start);
putchar ('\n');
}
bfd_boolean relocate)
{
Elf_Internal_Shdr * relsec;
- bfd_size_type bytes;
- bfd_vma addr;
- unsigned char * data;
- unsigned char * start;
-
- start = (unsigned char *) get_section_contents (section, file);
+ bfd_size_type bytes;
+ bfd_size_type section_size;
+ bfd_vma addr;
+ unsigned char * data;
+ unsigned char * real_start;
+ unsigned char * start;
+
+ real_start = start = (unsigned char *) get_section_contents (section, file);
if (start == NULL)
return;
+ section_size = section->sh_size;
printf (_("\nHex dump of section '%s':\n"), printable_section_name (section));
+ if (decompress_dumps)
+ {
+ dwarf_size_type new_size = section_size;
+ dwarf_size_type uncompressed_size = 0;
+
+ if ((section->sh_flags & SHF_COMPRESSED) != 0)
+ {
+ Elf_Internal_Chdr chdr;
+ unsigned int compression_header_size
+ = get_compression_header (& chdr, start);
+
+ if (chdr.ch_type == ELFCOMPRESS_ZLIB
+ && chdr.ch_addralign == section->sh_addralign)
+ {
+ uncompressed_size = chdr.ch_size;
+ start += compression_header_size;
+ new_size -= compression_header_size;
+ }
+ }
+ else if (new_size > 12 && streq ((char *) start, "ZLIB"))
+ {
+ /* Read the zlib header. In this case, it should be "ZLIB"
+ followed by the uncompressed section size, 8 bytes in
+ big-endian order. */
+ uncompressed_size = start[4]; uncompressed_size <<= 8;
+ uncompressed_size += start[5]; uncompressed_size <<= 8;
+ uncompressed_size += start[6]; uncompressed_size <<= 8;
+ uncompressed_size += start[7]; uncompressed_size <<= 8;
+ uncompressed_size += start[8]; uncompressed_size <<= 8;
+ uncompressed_size += start[9]; uncompressed_size <<= 8;
+ uncompressed_size += start[10]; uncompressed_size <<= 8;
+ uncompressed_size += start[11];
+ start += 12;
+ new_size -= 12;
+ }
+
+ if (uncompressed_size
+ && uncompress_section_contents (& start, uncompressed_size,
+ & new_size))
+ section_size = new_size;
+ }
+
if (relocate)
{
- apply_relocations (file, section, start, section->sh_size, NULL, NULL);
+ apply_relocations (file, section, start, section_size, NULL, NULL);
}
else
{
}
addr = section->sh_addr;
- bytes = section->sh_size;
+ bytes = section_size;
data = start;
while (bytes)
bytes -= lbytes;
}
- free (start);
+ free (real_start);
putchar ('\n');
}
-/* Uncompresses a section that was compressed using zlib, in place. */
-
-static int
-uncompress_section_contents (unsigned char **buffer,
- dwarf_size_type uncompressed_size,
- dwarf_size_type *size)
-{
- dwarf_size_type compressed_size = *size;
- unsigned char * compressed_buffer = *buffer;
- unsigned char * uncompressed_buffer;
- z_stream strm;
- int rc;
-
- /* It is possible the section consists of several compressed
- buffers concatenated together, so we uncompress in a loop. */
- /* PR 18313: The state field in the z_stream structure is supposed
- to be invisible to the user (ie us), but some compilers will
- still complain about it being used without initialisation. So
- we first zero the entire z_stream structure and then set the fields
- that we need. */
- memset (& strm, 0, sizeof strm);
- strm.avail_in = compressed_size;
- strm.next_in = (Bytef *) compressed_buffer;
- strm.avail_out = uncompressed_size;
- uncompressed_buffer = (unsigned char *) xmalloc (uncompressed_size);
-
- rc = inflateInit (& strm);
- while (strm.avail_in > 0)
- {
- if (rc != Z_OK)
- goto fail;
- strm.next_out = ((Bytef *) uncompressed_buffer
- + (uncompressed_size - strm.avail_out));
- rc = inflate (&strm, Z_FINISH);
- if (rc != Z_STREAM_END)
- goto fail;
- rc = inflateReset (& strm);
- }
- rc = inflateEnd (& strm);
- if (rc != Z_OK
- || strm.avail_out != 0)
- goto fail;
-
- *buffer = uncompressed_buffer;
- *size = uncompressed_size;
- return 1;
-
- fail:
- free (uncompressed_buffer);
- /* Indicate decompression failure. */
- *buffer = NULL;
- return 0;
-}
-
static int
load_specific_debug_section (enum dwarf_section_display_enum debug,
const Elf_Internal_Shdr * sec, void * file)