* incremental.cc (Incremental_inputs_header_data): Renamed from
[external/binutils.git] / gold / incremental.cc
1 // inremental.cc -- incremental linking support for gold
2
3 // Copyright 2009 Free Software Foundation, Inc.
4 // Written by Mikolaj Zalewski <mikolajz@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 #include "elfcpp.h"
25 #include "output.h"
26 #include "incremental.h"
27
28 using elfcpp::Convert;
29
30 namespace gold {
31
32 // Version information. Will change frequently during the development, later
33 // we could think about backward (and forward?) compatibility.
34 const int INCREMENTAL_LINK_VERSION = 1;
35
36 namespace internal {
37
38 // Header of the .gnu_incremental_input section.
39 struct Incremental_inputs_header_data
40 {
41   // Incremental linker version.
42   elfcpp::Elf_Word version;
43
44   // Numer of input files in the link.
45   elfcpp::Elf_Word input_file_count;
46
47   // Offset of command line options in .gnu_incremental_strtab.
48   elfcpp::Elf_Word command_line_offset;
49
50   // Padding.
51   elfcpp::Elf_Word reserved;
52 };
53
54 // Data stored in .gnu_incremental_input after the header for each of the
55 // Incremental_input_header_data::input_file_count input entries.
56 struct Incremental_inputs_entry_data
57 {
58   // Offset of file name in .gnu_incremental_strtab section.
59   elfcpp::Elf_Word filename_offset;
60
61   // Offset of data in .gnu_incremental_input.
62   elfcpp::Elf_Word data_offset;
63
64   // Timestamp (in seconds).
65   elfcpp::Elf_Xword timestamp_sec;
66
67   // Nano-second part of timestamp (if supported).
68   elfcpp::Elf_Word timestamp_usec;
69
70   // Type of the input entry.
71   elfcpp::Elf_Half input_type;
72
73   // Padding.
74   elfcpp::Elf_Half reserved;
75 };
76
77 }
78
79 // Accessors.
80
81 // See internal::Incremental_input_header for fields descriptions.
82 template<int size, bool big_endian>
83 class Incremental_inputs_header_write
84 {
85  public:
86   Incremental_inputs_header_write(unsigned char *p)
87     : p_(reinterpret_cast<internal::Incremental_inputs_header_data*>(p))
88   { }
89   
90   static const int data_size = sizeof(internal::Incremental_inputs_header_data);
91
92   void
93   put_version(elfcpp::Elf_Word v)
94   { this->p_->version = Convert<32, big_endian>::convert_host(v); }
95
96   void
97   put_input_file_count(elfcpp::Elf_Word v)
98   { this->p_->input_file_count = Convert<32, big_endian>::convert_host(v); }
99
100   void
101   put_command_line_offset(elfcpp::Elf_Word v)
102   { this->p_->command_line_offset = Convert<32, big_endian>::convert_host(v); }
103
104   void
105   put_reserved(elfcpp::Elf_Word v)
106   { this->p_->reserved = Convert<32, big_endian>::convert_host(v); }
107
108  private:
109   internal::Incremental_inputs_header_data* p_;
110 };
111
112 // See internal::Incremental_input_entry for fields descriptions.
113 template<int size, bool big_endian>
114 class Incremental_inputs_entry_write
115 {
116  public:
117   Incremental_inputs_entry_write(unsigned char *p)
118     : p_(reinterpret_cast<internal::Incremental_inputs_entry_data*>(p))
119   { }
120
121   static const int data_size = sizeof(internal::Incremental_inputs_entry_data);
122
123   void
124   put_filename_offset(elfcpp::Elf_Word v)
125   { this->p_->filename_offset = Convert<32, big_endian>::convert_host(v); }
126
127   void
128   put_data_offset(elfcpp::Elf_Word v)
129   { this->p_->data_offset = Convert<32, big_endian>::convert_host(v); }
130
131   void
132   put_timestamp_sec(elfcpp::Elf_Word v)
133   { this->p_->timestamp_sec = Convert<32, big_endian>::convert_host(v); }
134
135   void
136   put_timestamp_usec(elfcpp::Elf_Word v)
137   { this->p_->timestamp_usec = Convert<32, big_endian>::convert_host(v); }
138
139   void
140   put_input_type(elfcpp::Elf_Word v)
141   { this->p_->input_type = Convert<32, big_endian>::convert_host(v); }
142
143   void
144   put_reserved(elfcpp::Elf_Word v)
145   { this->p_->reserved = Convert<32, big_endian>::convert_host(v); }
146
147  private:
148   internal::Incremental_inputs_entry_data* p_;
149 };
150
151 // Add the command line to the string table, setting
152 // command_line_key_.  In incremental builds, the command line is
153 // stored in .gnu_incremental_inputs so that the next linker run can
154 // check if the command line options didn't change.
155
156 void
157 Incremental_inputs::report_command_line(int argc, const char* const* argv)
158 {
159   // Always store 'gold' as argv[0] to avoid a full relink if the user used a
160   // different path to the linker.
161   std::string args("gold");
162   // Copied from collect_argv in main.cc.
163   for (int i = 1; i < argc; ++i)
164     {
165       args.append(" '");
166       // Now append argv[i], but with all single-quotes escaped
167       const char* argpos = argv[i];
168       while (1)
169         {
170           const int len = strcspn(argpos, "'");
171           args.append(argpos, len);
172           if (argpos[len] == '\0')
173             break;
174           args.append("'\"'\"'");
175           argpos += len + 1;
176         }
177       args.append("'");
178     }
179   this->strtab_->add(args.c_str(), true, &this->command_line_key_);
180 }
181
182 // Finalize the incremental link information.  Called from
183 // Layout::finalize.
184
185 void
186 Incremental_inputs::finalize()
187 {
188   this->strtab_->set_string_offsets();
189 }
190
191 // Create the content of the .gnu_incremental_inputs section.
192
193 Output_section_data*
194 Incremental_inputs::create_incremental_inputs_section_data()
195 {
196   switch (parameters->size_and_endianness())
197     {
198 #ifdef HAVE_TARGET_32_LITTLE
199     case Parameters::TARGET_32_LITTLE:
200       return this->sized_create_inputs_section_data<32, false>();
201 #endif
202 #ifdef HAVE_TARGET_32_BIG
203     case Parameters::TARGET_32_BIG:
204       return this->sized_create_inputs_section_data<32, true>();
205 #endif
206 #ifdef HAVE_TARGET_64_LITTLE
207     case Parameters::TARGET_64_LITTLE:
208       return this->sized_create_inputs_section_data<64, false>();
209 #endif
210 #ifdef HAVE_TARGET_64_BIG
211     case Parameters::TARGET_64_BIG:
212       return this->sized_create_inputs_section_data<64, true>();
213 #endif
214     default:
215       gold_unreachable();
216     }  
217 }
218
219 // Sized creation of .gnu_incremental_inputs section.
220
221 template<int size, bool big_endian>
222 Output_section_data*
223 Incremental_inputs::sized_create_inputs_section_data()
224 {  
225   unsigned int sz =
226       Incremental_inputs_header_write<size, big_endian>::data_size;
227   unsigned char* buffer = new unsigned char[sz];
228   Incremental_inputs_header_write<size, big_endian> header_writer(buffer);
229   
230   gold_assert(this->command_line_key_ > 0);
231   int cmd_offset = this->strtab_->get_offset_from_key(this->command_line_key_);
232   
233   header_writer.put_version(INCREMENTAL_LINK_VERSION);
234   header_writer.put_input_file_count(0);   // TODO: store input files data.
235   header_writer.put_command_line_offset(cmd_offset);
236   header_writer.put_reserved(0);
237   
238   return new Output_data_const_buffer(buffer, sz, 8,
239       "** incremental link inputs list");
240 }
241
242 } // End namespace gold.