OpenCores
URL https://opencores.org/ocsvn/open8_urisc/open8_urisc/trunk

Subversion Repositories open8_urisc

[/] [open8_urisc/] [trunk/] [gnu/] [binutils/] [gold/] [compressed_output.cc] - Blame information for rev 143

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 khays
// compressed_output.cc -- manage compressed debug sections for gold
2
 
3
// Copyright 2007, 2008, 2010 Free Software Foundation, Inc.
4
// Written by Ian Lance Taylor <iant@google.com>.
5
 
6
// This file is part of gold.
7
 
8
// This program is free software; you can redistribute it and/or modify
9
// it under the terms of the GNU General Public License as published by
10
// the Free Software Foundation; either version 3 of the License, or
11
// (at your option) any later version.
12
 
13
// This program is distributed in the hope that it will be useful,
14
// but WITHOUT ANY WARRANTY; without even the implied warranty of
15
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
// GNU General Public License for more details.
17
 
18
// You should have received a copy of the GNU General Public License
19
// along with this program; if not, write to the Free Software
20
// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21
// MA 02110-1301, USA.
22
 
23
#include "gold.h"
24
 
25
#ifdef HAVE_ZLIB_H
26
#include <zlib.h>
27
#endif
28
 
29
#include "parameters.h"
30
#include "options.h"
31
#include "compressed_output.h"
32
 
33
namespace gold
34
{
35
 
36
#ifdef HAVE_ZLIB_H
37
 
38
// Compress UNCOMPRESSED_DATA of size UNCOMPRESSED_SIZE.  Returns true
39
// if it successfully compressed, false if it failed for any reason
40
// (including not having zlib support in the library).  If it returns
41
// true, it allocates memory for the compressed data using new, and
42
// sets *COMPRESSED_DATA and *COMPRESSED_SIZE to appropriate values.
43
// It also writes a header before COMPRESSED_DATA: 4 bytes saying
44
// "ZLIB", and 8 bytes indicating the uncompressed size, in big-endian
45
// order.
46
 
47
static bool
48
zlib_compress(const unsigned char* uncompressed_data,
49
              unsigned long uncompressed_size,
50
              unsigned char** compressed_data,
51
              unsigned long* compressed_size)
52
{
53
  const int header_size = 12;
54
  *compressed_size = uncompressed_size + uncompressed_size / 1000 + 128;
55
  *compressed_data = new unsigned char[*compressed_size + header_size];
56
 
57
  int compress_level;
58
  if (parameters->options().optimize() >= 1)
59
    compress_level = 9;
60
  else
61
    compress_level = 1;
62
 
63
  int rc = compress2(reinterpret_cast<Bytef*>(*compressed_data) + header_size,
64
                     compressed_size,
65
                     reinterpret_cast<const Bytef*>(uncompressed_data),
66
                     uncompressed_size,
67
                     compress_level);
68
  if (rc == Z_OK)
69
    {
70
      memcpy(*compressed_data, "ZLIB", 4);
71
      elfcpp::Swap_unaligned<64, true>::writeval(*compressed_data + 4,
72
                                                 uncompressed_size);
73
      *compressed_size += header_size;
74
      return true;
75
    }
76
  else
77
    {
78
      delete[] *compressed_data;
79
      *compressed_data = NULL;
80
      return false;
81
    }
82
}
83
 
84
// Decompress COMPRESSED_DATA of size COMPRESSED_SIZE, into a buffer
85
// UNCOMPRESSED_DATA of size UNCOMPRESSED_SIZE.  Returns TRUE if it
86
// decompressed successfully, false if it failed.  The buffer, of
87
// appropriate size, is provided by the caller, and is typically part
88
// of the memory-mapped output file.
89
 
90
static bool
91
zlib_decompress(const unsigned char* compressed_data,
92
                unsigned long compressed_size,
93
                unsigned char* uncompressed_data,
94
                unsigned long uncompressed_size)
95
{
96
  z_stream strm;
97
  int rc;
98
 
99
  /* It is possible the section consists of several compressed
100
     buffers concatenated together, so we uncompress in a loop.  */
101
  strm.zalloc = NULL;
102
  strm.zfree = NULL;
103
  strm.opaque = NULL;
104
  strm.avail_in = compressed_size;
105
  strm.next_in = const_cast<Bytef*>(compressed_data);
106
  strm.avail_out = uncompressed_size;
107
 
108
  rc = inflateInit(&strm);
109
  while (strm.avail_in > 0)
110
    {
111
      if (rc != Z_OK)
112
        return false;
113
      strm.next_out = ((Bytef*) uncompressed_data
114
                       + (uncompressed_size - strm.avail_out));
115
      rc = inflate(&strm, Z_FINISH);
116
      if (rc != Z_STREAM_END)
117
        return false;
118
      rc = inflateReset(&strm);
119
    }
120
  rc = inflateEnd(&strm);
121
  if (rc != Z_OK || strm.avail_out != 0)
122
    return false;
123
 
124
  return true;
125
}
126
 
127
#else // !defined(HAVE_ZLIB_H)
128
 
129
static bool
130
zlib_compress(const unsigned char*, unsigned long,
131
              unsigned char**, unsigned long*)
132
{
133
  return false;
134
}
135
 
136
static bool
137
zlib_decompress(const unsigned char*, unsigned long,
138
                unsigned char*, unsigned long)
139
{
140
  return false;
141
}
142
 
143
#endif // !defined(HAVE_ZLIB_H)
144
 
145
// Read the compression header of a compressed debug section and return
146
// the uncompressed size.
147
 
148
uint64_t
149
get_uncompressed_size(const unsigned char* compressed_data,
150
                      section_size_type compressed_size)
151
{
152
  const unsigned int zlib_header_size = 12;
153
 
154
  /* Verify the compression header.  Currently, we support only zlib
155
     compression, so it should be "ZLIB" followed by the uncompressed
156
     section size, 8 bytes in big-endian order.  */
157
  if (compressed_size >= zlib_header_size
158
      && strncmp(reinterpret_cast<const char*>(compressed_data),
159
                 "ZLIB", 4) == 0)
160
    return elfcpp::Swap_unaligned<64, true>::readval(compressed_data + 4);
161
  return -1ULL;
162
}
163
 
164
// Decompress a compressed debug section directly into the output file.
165
 
166
bool
167
decompress_input_section(const unsigned char* compressed_data,
168
                         unsigned long compressed_size,
169
                         unsigned char* uncompressed_data,
170
                         unsigned long uncompressed_size)
171
{
172
  const unsigned int zlib_header_size = 12;
173
 
174
  /* Verify the compression header.  Currently, we support only zlib
175
     compression, so it should be "ZLIB" followed by the uncompressed
176
     section size, 8 bytes in big-endian order.  */
177
  if (compressed_size >= zlib_header_size
178
      && strncmp(reinterpret_cast<const char*>(compressed_data),
179
                 "ZLIB", 4) == 0)
180
    {
181
      unsigned long uncompressed_size_check =
182
          elfcpp::Swap_unaligned<64, true>::readval(compressed_data + 4);
183
      gold_assert(uncompressed_size_check == uncompressed_size);
184
      return zlib_decompress(compressed_data + zlib_header_size,
185
                             compressed_size - zlib_header_size,
186
                             uncompressed_data,
187
                             uncompressed_size);
188
    }
189
  return false;
190
}
191
 
192
// Class Output_compressed_section.
193
 
194
// Set the final data size of a compressed section.  This is where
195
// we actually compress the section data.
196
 
197
void
198
Output_compressed_section::set_final_data_size()
199
{
200
  off_t uncompressed_size = this->postprocessing_buffer_size();
201
 
202
  // (Try to) compress the data.
203
  unsigned long compressed_size;
204
  unsigned char* uncompressed_data = this->postprocessing_buffer();
205
 
206
  // At this point the contents of all regular input sections will
207
  // have been copied into the postprocessing buffer, and relocations
208
  // will have been applied.  Now we need to copy in the contents of
209
  // anything other than a regular input section.
210
  this->write_to_postprocessing_buffer();
211
 
212
  bool success = false;
213
  if (strcmp(this->options_->compress_debug_sections(), "zlib") == 0)
214
    success = zlib_compress(uncompressed_data, uncompressed_size,
215
                            &this->data_, &compressed_size);
216
  if (success)
217
    {
218
      // This converts .debug_foo to .zdebug_foo
219
      this->new_section_name_ = std::string(".z") + (this->name() + 1);
220
      this->set_name(this->new_section_name_.c_str());
221
      this->set_data_size(compressed_size);
222
    }
223
  else
224
    {
225
      gold_warning(_("not compressing section data: zlib error"));
226
      gold_assert(this->data_ == NULL);
227
      this->set_data_size(uncompressed_size);
228
    }
229
}
230
 
231
// Write out a compressed section.  If we couldn't compress, we just
232
// write it out as normal, uncompressed data.
233
 
234
void
235
Output_compressed_section::do_write(Output_file* of)
236
{
237
  off_t offset = this->offset();
238
  off_t data_size = this->data_size();
239
  unsigned char* view = of->get_output_view(offset, data_size);
240
  if (this->data_ == NULL)
241
    memcpy(view, this->postprocessing_buffer(), data_size);
242
  else
243
    memcpy(view, this->data_, data_size);
244
  of->write_output_view(offset, data_size, view);
245
}
246
 
247
} // End namespace gold.

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.