From Craig Silverstein: Add support for compressing .debug_str section.
[external/binutils.git] / gold / compressed_output.h
1 // compressed_output.h -- compressed output sections for gold  -*- C++ -*-
2
3 // Copyright 2007 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 // We support compressing .debug_* sections on output.  (And,
24 // potentially one day, other sections.)  This is a form of
25 // relaxation.  This file adds support for merging and emitting the
26 // compressed sections.
27
28 #ifndef GOLD_COMPRESSED_OUTPUT_H
29 #define GOLD_COMPRESSED_OUTPUT_H
30
31 #include <string>
32 #include <vector>
33
34 #include "output.h"
35 #include "merge.h"
36
37 namespace gold
38 {
39
40 class General_options;
41
42 // This is used for compressing a section before emitting it in the
43 // output file.  This only works for unloaded sections, since it
44 // assumes the final section contents are available at
45 // set_final_data_size() time.  For loaded sections (those that end up
46 // in segments), this is not true; relocations are applied after
47 // set_final_data_size() is called.  However, for unloaded sections,
48 // we can -- and do -- postpone calling finalize_data_size() until
49 // after relocations are applies.
50
51 class Output_compressed_section_data : public Output_section_data
52 {
53  public:
54   Output_compressed_section_data(uint64_t addralign,
55                                  const General_options& options)
56     : Output_section_data(addralign), options_(options), data_(NULL)
57   { }
58
59  protected:
60   // Add an input section.
61   bool
62   do_add_input_section(Relobj* object, unsigned int shndx);
63
64   // Set the final data size.
65   void
66   set_final_data_size();
67
68   // Change the name of the output section to reflect it's compressed.
69   const char*
70   do_modified_output_section_name(const char* name);
71
72   // Write the data to the file.
73   void
74   do_write(Output_file*);
75
76  private:
77   struct Object_entry
78   {
79     Relobj* object;
80     unsigned int shndx;
81     const unsigned char* contents;
82     off_t length;
83
84     Object_entry(Relobj* o, unsigned int s)
85       : object(o), shndx(s), contents(NULL), length(0)
86     { }
87   };
88
89   const General_options& options_;
90   std::vector<Object_entry> objects_;
91   char* data_;
92   std::string new_section_name_;
93 };
94
95 // This is a special case for when the output section is a string
96 // section and does not have any relocations to apply to it.
97
98 template<typename Char_type>
99 class Output_compressed_string : public Output_merge_string<Char_type>
100 {
101  public:
102   Output_compressed_string(uint64_t addralign,
103                            const General_options& options)
104     : Output_merge_string<Char_type>(addralign),
105       options_(options), compressed_data_(NULL)
106   { }
107
108   ~Output_compressed_string()
109   { delete[] compressed_data_; }
110
111  protected:
112   // Add an input section.
113   bool
114   do_add_input_section(Relobj* object, unsigned int shndx);
115
116   // Set the final data size.  Also compresses the buffer.
117   void
118   set_final_data_size();
119
120   // Change the name of the output section to reflect it's compressed.
121   const char*
122   do_modified_output_section_name(const char* name);
123
124   // Write the data to the file.
125   void
126   do_write(Output_file*);
127
128  private:
129   const General_options& options_;
130   char* compressed_data_;
131   // This is just a buffer to store the section name in "permanent" storage.
132   std::string new_section_name_;
133 };
134
135 } // End namespace gold.
136
137 #endif // !defined(GOLD_COMPRESSED_OUTPUT_H)