daily update
[deliverable/binutils-gdb.git] / gold / layout.h
CommitLineData
a2fb1b05
ILT
1// layout.h -- lay out output file sections for gold -*- C++ -*-
2
6cb15b7f
ILT
3// Copyright 2006, 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
a2fb1b05
ILT
23#ifndef GOLD_LAYOUT_H
24#define GOLD_LAYOUT_H
25
26#include <list>
27#include <string>
28#include <utility>
29#include <vector>
30
a2fb1b05
ILT
31#include "workqueue.h"
32#include "object.h"
14b31740 33#include "dynobj.h"
a2fb1b05
ILT
34#include "stringpool.h"
35
36namespace gold
37{
38
ead1e424 39class General_options;
54dc6425 40class Input_objects;
75f65a3e 41class Symbol_table;
ead1e424 42class Output_section_data;
a2fb1b05 43class Output_section;
75f65a3e 44class Output_section_headers;
a2fb1b05 45class Output_segment;
54dc6425 46class Output_data;
a3ad94ed 47class Output_data_dynamic;
61ba1cf9 48class Target;
a2fb1b05 49
92e059d8
ILT
50// This task function handles mapping the input sections to output
51// sections and laying them out in memory.
a2fb1b05 52
92e059d8 53class Layout_task_runner : public Task_function_runner
a2fb1b05
ILT
54{
55 public:
56 // OPTIONS is the command line options, INPUT_OBJECTS is the list of
92e059d8
ILT
57 // input objects, SYMTAB is the symbol table, LAYOUT is the layout
58 // object.
59 Layout_task_runner(const General_options& options,
60 const Input_objects* input_objects,
61 Symbol_table* symtab,
62 Layout* layout)
75f65a3e 63 : options_(options), input_objects_(input_objects), symtab_(symtab),
92e059d8 64 layout_(layout)
a2fb1b05
ILT
65 { }
66
92e059d8 67 // Run the operation.
a2fb1b05
ILT
68 void
69 run(Workqueue*);
70
71 private:
92e059d8
ILT
72 Layout_task_runner(const Layout_task_runner&);
73 Layout_task_runner& operator=(const Layout_task_runner&);
a2fb1b05
ILT
74
75 const General_options& options_;
54dc6425 76 const Input_objects* input_objects_;
75f65a3e 77 Symbol_table* symtab_;
12e14209 78 Layout* layout_;
a2fb1b05
ILT
79};
80
81// This class handles the details of laying out input sections.
82
83class Layout
84{
85 public:
54dc6425
ILT
86 Layout(const General_options& options);
87
ead1e424
ILT
88 // Given an input section SHNDX, named NAME, with data in SHDR, from
89 // the object file OBJECT, return the output section where this
90 // input section should go. Set *OFFSET to the offset within the
91 // output section.
a2fb1b05
ILT
92 template<int size, bool big_endian>
93 Output_section*
f6ce93d6 94 layout(Relobj *object, unsigned int shndx, const char* name,
a2fb1b05
ILT
95 const elfcpp::Shdr<size, big_endian>& shdr, off_t* offset);
96
ead1e424
ILT
97 // Add an Output_section_data to the layout. This is used for
98 // special sections like the GOT section.
99 void
100 add_output_section_data(const char* name, elfcpp::Elf_Word type,
101 elfcpp::Elf_Xword flags,
102 Output_section_data*);
103
a3ad94ed
ILT
104 // Create dynamic sections if necessary.
105 void
106 create_initial_dynamic_sections(const Input_objects*, Symbol_table*);
107
bfd58944
ILT
108 // Define __start and __stop symbols for output sections.
109 void
110 define_section_symbols(Symbol_table*, const Target*);
111
61ba1cf9
ILT
112 // Return the Stringpool used for symbol names.
113 const Stringpool*
114 sympool() const
115 { return &this->sympool_; }
116
16649710
ILT
117 // Return the Stringpool used for dynamic symbol names and dynamic
118 // tags.
119 const Stringpool*
120 dynpool() const
121 { return &this->dynpool_; }
122
a2fb1b05
ILT
123 // Return whether a section is a .gnu.linkonce section, given the
124 // section name.
125 static inline bool
126 is_linkonce(const char* name)
127 { return strncmp(name, ".gnu.linkonce", sizeof(".gnu.linkonce") - 1) == 0; }
128
129 // Record the signature of a comdat section, and return whether to
130 // include it in the link. The GROUP parameter is true for a
131 // section group signature, false for a signature derived from a
132 // .gnu.linkonce section.
133 bool
134 add_comdat(const char*, bool group);
135
54dc6425 136 // Finalize the layout after all the input sections have been added.
75f65a3e
ILT
137 off_t
138 finalize(const Input_objects*, Symbol_table*);
54dc6425 139
e44fcf3b
ILT
140 // Return the size of the output file.
141 off_t
142 output_file_size() const
143 { return this->output_file_size_; }
144
16649710
ILT
145 // Return the TLS segment. This will return NULL if there isn't
146 // one.
92e059d8
ILT
147 Output_segment*
148 tls_segment() const
149 { return this->tls_segment_; }
150
16649710
ILT
151 // Return the normal symbol table.
152 Output_section*
153 symtab_section() const
154 {
155 gold_assert(this->symtab_section_ != NULL);
156 return this->symtab_section_;
157 }
158
159 // Return the dynamic symbol table.
160 Output_section*
161 dynsym_section() const
162 {
163 gold_assert(this->dynsym_section_ != NULL);
164 return this->dynsym_section_;
165 }
166
167 // Return the dynamic tags.
168 Output_data_dynamic*
169 dynamic_data() const
170 { return this->dynamic_data_; }
171
61ba1cf9
ILT
172 // Write out data not associated with an input file or the symbol
173 // table.
174 void
9025d29d 175 write_data(const Symbol_table*, Output_file*) const;
61ba1cf9 176
ead1e424
ILT
177 // Return an output section named NAME, or NULL if there is none.
178 Output_section*
179 find_output_section(const char* name) const;
180
181 // Return an output segment of type TYPE, with segment flags SET set
182 // and segment flags CLEAR clear. Return NULL if there is none.
183 Output_segment*
184 find_output_segment(elfcpp::PT type, elfcpp::Elf_Word set,
185 elfcpp::Elf_Word clear) const;
186
54dc6425
ILT
187 // The list of segments.
188
189 typedef std::vector<Output_segment*> Segment_list;
190
191 // The list of sections not attached to a segment.
192
a3ad94ed 193 typedef std::vector<Output_section*> Section_list;
54dc6425
ILT
194
195 // The list of information to write out which is not attached to
196 // either a section or a segment.
a3ad94ed 197 typedef std::vector<Output_data*> Data_list;
54dc6425 198
a2fb1b05
ILT
199 private:
200 Layout(const Layout&);
201 Layout& operator=(const Layout&);
202
203 // Mapping from .gnu.linkonce section names to output section names.
204 struct Linkonce_mapping
205 {
206 const char* from;
207 int fromlen;
208 const char* to;
ead1e424 209 int tolen;
a2fb1b05
ILT
210 };
211 static const Linkonce_mapping linkonce_mapping[];
212 static const int linkonce_mapping_count;
213
3151305a
ILT
214 // Handle an exception frame section.
215 template<int size, bool big_endian>
216 void
217 layout_eh_frame(Relobj*, unsigned int, const char*,
218 const elfcpp::Shdr<size, big_endian>&,
219 Output_section*, off_t*);
220
4f211c8b
ILT
221 // Create a .note section for gold.
222 void
223 create_note_section();
224
75f65a3e
ILT
225 // Find the first read-only PT_LOAD segment, creating one if
226 // necessary.
227 Output_segment*
228 find_first_load_seg();
229
54dc6425
ILT
230 // Create the output sections for the symbol table.
231 void
9025d29d 232 create_symtab_sections(const Input_objects*, Symbol_table*, off_t*);
54dc6425 233
75f65a3e
ILT
234 // Create the .shstrtab section.
235 Output_section*
236 create_shstrtab();
237
238 // Create the section header table.
239 Output_section_headers*
9025d29d 240 create_shdrs(off_t*);
54dc6425 241
dbe717ef
ILT
242 // Create the dynamic symbol table.
243 void
14b31740
ILT
244 create_dynamic_symtab(const Target*, Symbol_table*, Output_section** pdynstr,
245 unsigned int* plocal_dynamic_count,
246 std::vector<Symbol*>* pdynamic_symbols,
247 Versions* versions);
dbe717ef 248
a3ad94ed 249 // Finish the .dynamic section and PT_DYNAMIC segment.
dbe717ef 250 void
16649710 251 finish_dynamic_section(const Input_objects*, const Symbol_table*);
dbe717ef
ILT
252
253 // Create the .interp section and PT_INTERP segment.
254 void
255 create_interp(const Target* target);
256
14b31740
ILT
257 // Create the version sections.
258 void
9025d29d 259 create_version_sections(const Versions*,
46fe1623 260 const Symbol_table*,
14b31740
ILT
261 unsigned int local_symcount,
262 const std::vector<Symbol*>& dynamic_symbols,
263 const Output_section* dynstr);
264
265 template<int size, bool big_endian>
266 void
267 sized_create_version_sections(const Versions* versions,
46fe1623 268 const Symbol_table*,
14b31740
ILT
269 unsigned int local_symcount,
270 const std::vector<Symbol*>& dynamic_symbols,
91da9340
ILT
271 const Output_section* dynstr
272 ACCEPT_SIZE_ENDIAN);
14b31740 273
a2fb1b05
ILT
274 // Return whether to include this section in the link.
275 template<int size, bool big_endian>
276 bool
277 include_section(Object* object, const char* name,
278 const elfcpp::Shdr<size, big_endian>&);
279
ead1e424
ILT
280 // Return the output section name to use given an input section
281 // name. Set *PLEN to the length of the name. *PLEN must be
282 // initialized to the length of NAME.
283 static const char*
284 output_section_name(const char* name, size_t* plen);
285
a2fb1b05 286 // Return the output section name to use for a linkonce section
ead1e424 287 // name. PLEN is as for output_section_name.
a2fb1b05 288 static const char*
ead1e424
ILT
289 linkonce_output_name(const char* name, size_t* plen);
290
291 // Return the output section for NAME, TYPE and FLAGS.
292 Output_section*
f0641a0b
ILT
293 get_output_section(const char* name, Stringpool::Key name_key,
294 elfcpp::Elf_Word type, elfcpp::Elf_Xword flags);
a2fb1b05
ILT
295
296 // Create a new Output_section.
297 Output_section*
298 make_output_section(const char* name, elfcpp::Elf_Word type,
299 elfcpp::Elf_Xword flags);
300
dbe717ef
ILT
301 // Set the final file offsets of all the segments.
302 off_t
303 set_segment_offsets(const Target*, Output_segment*, unsigned int* pshndx);
304
14b31740 305 // Set the final file offsets and section indexes of all the
dbe717ef
ILT
306 // sections not associated with a segment.
307 off_t
308 set_section_offsets(off_t, unsigned int *pshndx);
309
a2fb1b05
ILT
310 // Return whether SEG1 comes before SEG2 in the output file.
311 static bool
b3168e9d 312 segment_precedes(const Output_segment* seg1, const Output_segment* seg2);
a2fb1b05
ILT
313
314 // Map from section flags to segment flags.
315 static elfcpp::Elf_Word
316 section_flags_to_segment(elfcpp::Elf_Xword flags);
317
318 // A mapping used for group signatures.
319 typedef Unordered_map<std::string, bool> Signatures;
320
321 // Mapping from input section name/type/flags to output section. We
322 // use canonicalized strings here.
323
f0641a0b 324 typedef std::pair<Stringpool::Key,
a2fb1b05
ILT
325 std::pair<elfcpp::Elf_Word, elfcpp::Elf_Xword> > Key;
326
327 struct Hash_key
328 {
329 size_t
330 operator()(const Key& k) const;
331 };
332
333 typedef Unordered_map<Key, Output_section*, Hash_key> Section_name_map;
334
335 // A comparison class for segments.
336
337 struct Compare_segments
338 {
339 bool
340 operator()(const Output_segment* seg1, const Output_segment* seg2)
341 { return Layout::segment_precedes(seg1, seg2); }
342 };
343
a2fb1b05
ILT
344 // A reference to the options on the command line.
345 const General_options& options_;
346 // The output section names.
347 Stringpool namepool_;
75f65a3e
ILT
348 // The output symbol names.
349 Stringpool sympool_;
a3ad94ed
ILT
350 // The dynamic strings, if needed.
351 Stringpool dynpool_;
a2fb1b05
ILT
352 // The list of group sections and linkonce sections which we have seen.
353 Signatures signatures_;
354 // The mapping from input section name/type/flags to output sections.
355 Section_name_map section_name_map_;
356 // The list of output segments.
357 Segment_list segment_list_;
a3ad94ed
ILT
358 // The list of output sections.
359 Section_list section_list_;
a2fb1b05
ILT
360 // The list of output sections which are not attached to any output
361 // segment.
a3ad94ed
ILT
362 Section_list unattached_section_list_;
363 // The list of unattached Output_data objects which require special
364 // handling because they are not Output_sections.
61ba1cf9 365 Data_list special_output_list_;
92e059d8
ILT
366 // A pointer to the PT_TLS segment if there is one.
367 Output_segment* tls_segment_;
a3ad94ed
ILT
368 // The SHT_SYMTAB output section.
369 Output_section* symtab_section_;
370 // The SHT_DYNSYM output section if there is one.
371 Output_section* dynsym_section_;
372 // The SHT_DYNAMIC output section if there is one.
373 Output_section* dynamic_section_;
16649710
ILT
374 // The dynamic data which goes into dynamic_section_.
375 Output_data_dynamic* dynamic_data_;
3151305a
ILT
376 // The exception frame section.
377 Output_section* eh_frame_section_;
e44fcf3b
ILT
378 // The size of the output file.
379 off_t output_file_size_;
61ba1cf9
ILT
380};
381
382// This task handles writing out data which is not part of a section
383// or segment.
384
385class Write_data_task : public Task
386{
387 public:
a3ad94ed 388 Write_data_task(const Layout* layout, const Symbol_table* symtab,
9025d29d
ILT
389 Output_file* of, Task_token* final_blocker)
390 : layout_(layout), symtab_(symtab), of_(of), final_blocker_(final_blocker)
61ba1cf9
ILT
391 { }
392
393 // The standard Task methods.
394
395 Is_runnable_type
396 is_runnable(Workqueue*);
397
398 Task_locker*
399 locks(Workqueue*);
400
401 void
402 run(Workqueue*);
403
404 private:
405 const Layout* layout_;
a3ad94ed 406 const Symbol_table* symtab_;
61ba1cf9
ILT
407 Output_file* of_;
408 Task_token* final_blocker_;
409};
410
411// This task handles writing out the global symbols.
412
413class Write_symbols_task : public Task
414{
415 public:
416 Write_symbols_task(const Symbol_table* symtab, const Target* target,
16649710
ILT
417 const Stringpool* sympool, const Stringpool* dynpool,
418 Output_file* of, Task_token* final_blocker)
419 : symtab_(symtab), target_(target), sympool_(sympool), dynpool_(dynpool),
420 of_(of), final_blocker_(final_blocker)
61ba1cf9
ILT
421 { }
422
423 // The standard Task methods.
424
425 Is_runnable_type
426 is_runnable(Workqueue*);
427
428 Task_locker*
429 locks(Workqueue*);
430
431 void
432 run(Workqueue*);
433
434 private:
435 const Symbol_table* symtab_;
436 const Target* target_;
437 const Stringpool* sympool_;
16649710 438 const Stringpool* dynpool_;
61ba1cf9
ILT
439 Output_file* of_;
440 Task_token* final_blocker_;
441};
442
92e059d8 443// This task function handles closing the file.
61ba1cf9 444
92e059d8 445class Close_task_runner : public Task_function_runner
61ba1cf9
ILT
446{
447 public:
92e059d8
ILT
448 Close_task_runner(Output_file* of)
449 : of_(of)
61ba1cf9
ILT
450 { }
451
92e059d8 452 // Run the operation.
61ba1cf9
ILT
453 void
454 run(Workqueue*);
455
456 private:
457 Output_file* of_;
a2fb1b05
ILT
458};
459
ead1e424
ILT
460// A small helper function to align an address.
461
462inline uint64_t
463align_address(uint64_t address, uint64_t addralign)
464{
465 if (addralign != 0)
466 address = (address + addralign - 1) &~ (addralign - 1);
467 return address;
468}
469
a2fb1b05
ILT
470} // End namespace gold.
471
472#endif // !defined(GOLD_LAYOUT_H)
This page took 0.08325 seconds and 4 git commands to generate.