2007-11-30 01:35:27 +01:00
|
|
|
// compressed_output.cc -- manage compressed output sections for gold
|
|
|
|
|
2008-03-13 22:04:21 +01:00
|
|
|
// Copyright 2007, 2008 Free Software Foundation, Inc.
|
2007-11-30 01:35:27 +01:00
|
|
|
// Written by Ian Lance Taylor <iant@google.com>.
|
|
|
|
|
|
|
|
// This file is part of gold.
|
|
|
|
|
|
|
|
// This program is free software; you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation; either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
|
|
|
|
// MA 02110-1301, USA.
|
|
|
|
|
|
|
|
#include "gold.h"
|
|
|
|
|
|
|
|
#ifdef HAVE_ZLIB_H
|
|
|
|
#include <zlib.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "parameters.h"
|
2008-01-19 00:26:48 +01:00
|
|
|
#include "options.h"
|
2007-12-01 07:34:12 +01:00
|
|
|
#include "compressed_output.h"
|
2007-11-30 01:35:27 +01:00
|
|
|
|
|
|
|
namespace gold
|
|
|
|
{
|
|
|
|
|
|
|
|
// Compress UNCOMPRESSED_DATA of size UNCOMPRESSED_SIZE. Returns true
|
|
|
|
// if it successfully compressed, false if it failed for any reason
|
|
|
|
// (including not having zlib support in the library). If it returns
|
|
|
|
// true, it allocates memory for the compressed data using new, and
|
|
|
|
// sets *COMPRESSED_DATA and *COMPRESSED_SIZE to appropriate values.
|
2008-04-03 16:18:37 +02:00
|
|
|
// It also writes a header before COMPRESSED_DATA: 4 bytes saying
|
|
|
|
// "ZLIB", and 8 bytes indicating the uncompressed size, in big-endian
|
|
|
|
// order.
|
2007-11-30 01:35:27 +01:00
|
|
|
|
2007-11-30 23:38:01 +01:00
|
|
|
#ifdef HAVE_ZLIB_H
|
|
|
|
|
2007-11-30 01:35:27 +01:00
|
|
|
static bool
|
2008-04-03 16:18:37 +02:00
|
|
|
zlib_compress(const unsigned char* uncompressed_data,
|
|
|
|
unsigned long uncompressed_size,
|
|
|
|
unsigned char** compressed_data,
|
|
|
|
unsigned long* compressed_size)
|
2007-11-30 01:35:27 +01:00
|
|
|
{
|
2008-04-03 16:18:37 +02:00
|
|
|
const int header_size = 12;
|
2007-11-30 01:35:27 +01:00
|
|
|
*compressed_size = uncompressed_size + uncompressed_size / 1000 + 128;
|
2008-04-03 16:18:37 +02:00
|
|
|
*compressed_data = new unsigned char[*compressed_size + header_size];
|
2007-11-30 01:35:27 +01:00
|
|
|
|
|
|
|
int compress_level;
|
2008-02-28 01:18:24 +01:00
|
|
|
if (parameters->options().optimize() >= 1)
|
2007-11-30 01:35:27 +01:00
|
|
|
compress_level = 9;
|
|
|
|
else
|
|
|
|
compress_level = 1;
|
|
|
|
|
2008-04-03 16:18:37 +02:00
|
|
|
int rc = compress2(reinterpret_cast<Bytef*>(*compressed_data) + header_size,
|
2007-11-30 01:35:27 +01:00
|
|
|
compressed_size,
|
|
|
|
reinterpret_cast<const Bytef*>(uncompressed_data),
|
|
|
|
uncompressed_size,
|
|
|
|
compress_level);
|
|
|
|
if (rc == Z_OK)
|
2008-04-03 16:18:37 +02:00
|
|
|
{
|
|
|
|
memcpy(*compressed_data, "ZLIB", 4);
|
|
|
|
elfcpp::Swap_unaligned<64, true>::writeval(*compressed_data + 4,
|
|
|
|
uncompressed_size);
|
|
|
|
*compressed_size += header_size;
|
|
|
|
return true;
|
|
|
|
}
|
2007-11-30 01:35:27 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
delete[] *compressed_data;
|
|
|
|
*compressed_data = NULL;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-30 23:38:01 +01:00
|
|
|
#else // !defined(HAVE_ZLIB_H)
|
|
|
|
|
|
|
|
static bool
|
2008-04-03 16:18:37 +02:00
|
|
|
zlib_compress(const unsigned char*, unsigned long,
|
|
|
|
unsigned char**, unsigned long*)
|
2007-11-30 23:38:01 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // !defined(HAVE_ZLIB_H)
|
|
|
|
|
2007-12-01 07:34:12 +01:00
|
|
|
// Class Output_compressed_section.
|
2007-11-30 01:35:27 +01:00
|
|
|
|
|
|
|
// Set the final data size of a compressed section. This is where
|
|
|
|
// we actually compress the section data.
|
|
|
|
|
|
|
|
void
|
2007-12-01 07:34:12 +01:00
|
|
|
Output_compressed_section::set_final_data_size()
|
2007-11-30 01:35:27 +01:00
|
|
|
{
|
2007-12-01 07:34:12 +01:00
|
|
|
off_t uncompressed_size = this->postprocessing_buffer_size();
|
2007-11-30 01:35:27 +01:00
|
|
|
|
|
|
|
// (Try to) compress the data.
|
|
|
|
unsigned long compressed_size;
|
2008-04-03 16:18:37 +02:00
|
|
|
unsigned char* uncompressed_data = this->postprocessing_buffer();
|
2007-12-01 07:34:12 +01:00
|
|
|
|
|
|
|
// At this point the contents of all regular input sections will
|
|
|
|
// have been copied into the postprocessing buffer, and relocations
|
|
|
|
// will have been applied. Now we need to copy in the contents of
|
|
|
|
// anything other than a regular input section.
|
|
|
|
this->write_to_postprocessing_buffer();
|
2007-11-30 01:35:27 +01:00
|
|
|
|
|
|
|
bool success = false;
|
2008-03-05 00:10:38 +01:00
|
|
|
if (strcmp(this->options_->compress_debug_sections(), "zlib") == 0)
|
2007-11-30 01:35:27 +01:00
|
|
|
success = zlib_compress(uncompressed_data, uncompressed_size,
|
|
|
|
&this->data_, &compressed_size);
|
|
|
|
if (success)
|
|
|
|
{
|
2008-04-03 16:18:37 +02:00
|
|
|
// This converts .debug_foo to .zdebug_foo
|
|
|
|
this->new_section_name_ = std::string(".z") + (this->name() + 1);
|
2007-12-01 07:34:12 +01:00
|
|
|
this->set_name(this->new_section_name_.c_str());
|
2007-11-30 01:35:27 +01:00
|
|
|
this->set_data_size(compressed_size);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-12-01 07:34:12 +01:00
|
|
|
gold_warning(_("not compressing section data: zlib error"));
|
2007-11-30 01:35:27 +01:00
|
|
|
gold_assert(this->data_ == NULL);
|
|
|
|
this->set_data_size(uncompressed_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write out a compressed section. If we couldn't compress, we just
|
|
|
|
// write it out as normal, uncompressed data.
|
|
|
|
|
|
|
|
void
|
2007-12-01 07:34:12 +01:00
|
|
|
Output_compressed_section::do_write(Output_file* of)
|
2007-11-30 01:35:27 +01:00
|
|
|
{
|
2007-12-01 07:34:12 +01:00
|
|
|
off_t offset = this->offset();
|
|
|
|
off_t data_size = this->data_size();
|
|
|
|
unsigned char* view = of->get_output_view(offset, data_size);
|
|
|
|
if (this->data_ == NULL)
|
|
|
|
memcpy(view, this->postprocessing_buffer(), data_size);
|
2007-11-30 01:35:27 +01:00
|
|
|
else
|
2007-12-01 07:34:12 +01:00
|
|
|
memcpy(view, this->data_, data_size);
|
|
|
|
of->write_output_view(offset, data_size, view);
|
2007-11-30 01:35:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // End namespace gold.
|