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