2006-11-29 Paul Brook <paul@codesourcery.com>
[deliverable/binutils-gdb.git] / gold / layout.h
CommitLineData
a2fb1b05
ILT
1// layout.h -- lay out output file sections for gold -*- C++ -*-
2
3#ifndef GOLD_LAYOUT_H
4#define GOLD_LAYOUT_H
5
6#include <list>
7#include <string>
8#include <utility>
9#include <vector>
10
a2fb1b05
ILT
11#include "workqueue.h"
12#include "object.h"
13#include "stringpool.h"
14
15namespace gold
16{
17
ead1e424 18class General_options;
54dc6425 19class Input_objects;
75f65a3e 20class Symbol_table;
ead1e424 21class Output_section_data;
a2fb1b05 22class Output_section;
54dc6425 23class Output_section_symtab;
75f65a3e 24class Output_section_headers;
a2fb1b05 25class Output_segment;
54dc6425 26class Output_data;
61ba1cf9 27class Target;
a2fb1b05 28
92e059d8
ILT
29// This task function handles mapping the input sections to output
30// sections and laying them out in memory.
a2fb1b05 31
92e059d8 32class Layout_task_runner : public Task_function_runner
a2fb1b05
ILT
33{
34 public:
35 // OPTIONS is the command line options, INPUT_OBJECTS is the list of
92e059d8
ILT
36 // input objects, SYMTAB is the symbol table, LAYOUT is the layout
37 // object.
38 Layout_task_runner(const General_options& options,
39 const Input_objects* input_objects,
40 Symbol_table* symtab,
41 Layout* layout)
75f65a3e 42 : options_(options), input_objects_(input_objects), symtab_(symtab),
92e059d8 43 layout_(layout)
a2fb1b05
ILT
44 { }
45
92e059d8 46 // Run the operation.
a2fb1b05
ILT
47 void
48 run(Workqueue*);
49
50 private:
92e059d8
ILT
51 Layout_task_runner(const Layout_task_runner&);
52 Layout_task_runner& operator=(const Layout_task_runner&);
a2fb1b05
ILT
53
54 const General_options& options_;
54dc6425 55 const Input_objects* input_objects_;
75f65a3e 56 Symbol_table* symtab_;
12e14209 57 Layout* layout_;
a2fb1b05
ILT
58};
59
60// This class handles the details of laying out input sections.
61
62class Layout
63{
64 public:
54dc6425
ILT
65 Layout(const General_options& options);
66
ead1e424
ILT
67 // Given an input section SHNDX, named NAME, with data in SHDR, from
68 // the object file OBJECT, return the output section where this
69 // input section should go. Set *OFFSET to the offset within the
70 // output section.
a2fb1b05
ILT
71 template<int size, bool big_endian>
72 Output_section*
f6ce93d6 73 layout(Relobj *object, unsigned int shndx, const char* name,
a2fb1b05
ILT
74 const elfcpp::Shdr<size, big_endian>& shdr, off_t* offset);
75
ead1e424
ILT
76 // Add an Output_section_data to the layout. This is used for
77 // special sections like the GOT section.
78 void
79 add_output_section_data(const char* name, elfcpp::Elf_Word type,
80 elfcpp::Elf_Xword flags,
81 Output_section_data*);
82
61ba1cf9
ILT
83 // Return the Stringpool used for symbol names.
84 const Stringpool*
85 sympool() const
86 { return &this->sympool_; }
87
a2fb1b05
ILT
88 // Return whether a section is a .gnu.linkonce section, given the
89 // section name.
90 static inline bool
91 is_linkonce(const char* name)
92 { return strncmp(name, ".gnu.linkonce", sizeof(".gnu.linkonce") - 1) == 0; }
93
94 // Record the signature of a comdat section, and return whether to
95 // include it in the link. The GROUP parameter is true for a
96 // section group signature, false for a signature derived from a
97 // .gnu.linkonce section.
98 bool
99 add_comdat(const char*, bool group);
100
54dc6425 101 // Finalize the layout after all the input sections have been added.
75f65a3e
ILT
102 off_t
103 finalize(const Input_objects*, Symbol_table*);
54dc6425 104
92e059d8
ILT
105 // Return the TLS segment.
106 Output_segment*
107 tls_segment() const
108 { return this->tls_segment_; }
109
61ba1cf9
ILT
110 // Write out data not associated with an input file or the symbol
111 // table.
112 void
113 write_data(Output_file*) const;
114
ead1e424
ILT
115 // Return an output section named NAME, or NULL if there is none.
116 Output_section*
117 find_output_section(const char* name) const;
118
119 // Return an output segment of type TYPE, with segment flags SET set
120 // and segment flags CLEAR clear. Return NULL if there is none.
121 Output_segment*
122 find_output_segment(elfcpp::PT type, elfcpp::Elf_Word set,
123 elfcpp::Elf_Word clear) const;
124
54dc6425
ILT
125 // The list of segments.
126
127 typedef std::vector<Output_segment*> Segment_list;
128
129 // The list of sections not attached to a segment.
130
131 typedef std::list<Output_section*> Section_list;
132
133 // The list of information to write out which is not attached to
134 // either a section or a segment.
135 typedef std::list<Output_data*> Data_list;
136
a2fb1b05
ILT
137 private:
138 Layout(const Layout&);
139 Layout& operator=(const Layout&);
140
141 // Mapping from .gnu.linkonce section names to output section names.
142 struct Linkonce_mapping
143 {
144 const char* from;
145 int fromlen;
146 const char* to;
ead1e424 147 int tolen;
a2fb1b05
ILT
148 };
149 static const Linkonce_mapping linkonce_mapping[];
150 static const int linkonce_mapping_count;
151
75f65a3e
ILT
152 // Find the first read-only PT_LOAD segment, creating one if
153 // necessary.
154 Output_segment*
155 find_first_load_seg();
156
54dc6425
ILT
157 // Create the output sections for the symbol table.
158 void
61ba1cf9 159 create_symtab_sections(int size, const Input_objects*, Symbol_table*, off_t*,
75f65a3e
ILT
160 Output_section** osymtab,
161 Output_section** ostrtab);
54dc6425 162
75f65a3e
ILT
163 // Create the .shstrtab section.
164 Output_section*
165 create_shstrtab();
166
167 // Create the section header table.
168 Output_section_headers*
61ba1cf9 169 create_shdrs(int size, bool big_endian, off_t*);
54dc6425 170
dbe717ef
ILT
171 // Create the dynamic symbol table.
172 void
173 create_dynamic_symtab(int size, Symbol_table*);
174
175 // Create the .dynamic section and PT_DYNAMIC segment.
176 void
177 create_dynamic_section();
178
179 // Create the .interp section and PT_INTERP segment.
180 void
181 create_interp(const Target* target);
182
a2fb1b05
ILT
183 // Return whether to include this section in the link.
184 template<int size, bool big_endian>
185 bool
186 include_section(Object* object, const char* name,
187 const elfcpp::Shdr<size, big_endian>&);
188
ead1e424
ILT
189 // Return the output section name to use given an input section
190 // name. Set *PLEN to the length of the name. *PLEN must be
191 // initialized to the length of NAME.
192 static const char*
193 output_section_name(const char* name, size_t* plen);
194
a2fb1b05 195 // Return the output section name to use for a linkonce section
ead1e424 196 // name. PLEN is as for output_section_name.
a2fb1b05 197 static const char*
ead1e424
ILT
198 linkonce_output_name(const char* name, size_t* plen);
199
200 // Return the output section for NAME, TYPE and FLAGS.
201 Output_section*
f0641a0b
ILT
202 get_output_section(const char* name, Stringpool::Key name_key,
203 elfcpp::Elf_Word type, elfcpp::Elf_Xword flags);
a2fb1b05
ILT
204
205 // Create a new Output_section.
206 Output_section*
207 make_output_section(const char* name, elfcpp::Elf_Word type,
208 elfcpp::Elf_Xword flags);
209
dbe717ef
ILT
210 // Set the final file offsets of all the segments.
211 off_t
212 set_segment_offsets(const Target*, Output_segment*, unsigned int* pshndx);
213
214 // Set the final file offsets and section indices of all the
215 // sections not associated with a segment.
216 off_t
217 set_section_offsets(off_t, unsigned int *pshndx);
218
a2fb1b05
ILT
219 // Return whether SEG1 comes before SEG2 in the output file.
220 static bool
b3168e9d 221 segment_precedes(const Output_segment* seg1, const Output_segment* seg2);
a2fb1b05
ILT
222
223 // Map from section flags to segment flags.
224 static elfcpp::Elf_Word
225 section_flags_to_segment(elfcpp::Elf_Xword flags);
226
227 // A mapping used for group signatures.
228 typedef Unordered_map<std::string, bool> Signatures;
229
230 // Mapping from input section name/type/flags to output section. We
231 // use canonicalized strings here.
232
f0641a0b 233 typedef std::pair<Stringpool::Key,
a2fb1b05
ILT
234 std::pair<elfcpp::Elf_Word, elfcpp::Elf_Xword> > Key;
235
236 struct Hash_key
237 {
238 size_t
239 operator()(const Key& k) const;
240 };
241
242 typedef Unordered_map<Key, Output_section*, Hash_key> Section_name_map;
243
244 // A comparison class for segments.
245
246 struct Compare_segments
247 {
248 bool
249 operator()(const Output_segment* seg1, const Output_segment* seg2)
250 { return Layout::segment_precedes(seg1, seg2); }
251 };
252
a2fb1b05
ILT
253 // A reference to the options on the command line.
254 const General_options& options_;
255 // The output section names.
256 Stringpool namepool_;
75f65a3e
ILT
257 // The output symbol names.
258 Stringpool sympool_;
a2fb1b05
ILT
259 // The list of group sections and linkonce sections which we have seen.
260 Signatures signatures_;
261 // The mapping from input section name/type/flags to output sections.
262 Section_name_map section_name_map_;
263 // The list of output segments.
264 Segment_list segment_list_;
265 // The list of output sections which are not attached to any output
266 // segment.
267 Section_list section_list_;
61ba1cf9
ILT
268 // The list of sections which require special output because they
269 // are not comprised of input sections.
270 Data_list special_output_list_;
92e059d8
ILT
271 // A pointer to the PT_TLS segment if there is one.
272 Output_segment* tls_segment_;
61ba1cf9
ILT
273};
274
275// This task handles writing out data which is not part of a section
276// or segment.
277
278class Write_data_task : public Task
279{
280 public:
281 Write_data_task(const Layout* layout, Output_file* of,
282 Task_token* final_blocker)
283 : layout_(layout), of_(of), final_blocker_(final_blocker)
284 { }
285
286 // The standard Task methods.
287
288 Is_runnable_type
289 is_runnable(Workqueue*);
290
291 Task_locker*
292 locks(Workqueue*);
293
294 void
295 run(Workqueue*);
296
297 private:
298 const Layout* layout_;
299 Output_file* of_;
300 Task_token* final_blocker_;
301};
302
303// This task handles writing out the global symbols.
304
305class Write_symbols_task : public Task
306{
307 public:
308 Write_symbols_task(const Symbol_table* symtab, const Target* target,
309 const Stringpool* sympool, Output_file* of,
310 Task_token* final_blocker)
311 : symtab_(symtab), target_(target), sympool_(sympool), of_(of),
312 final_blocker_(final_blocker)
313 { }
314
315 // The standard Task methods.
316
317 Is_runnable_type
318 is_runnable(Workqueue*);
319
320 Task_locker*
321 locks(Workqueue*);
322
323 void
324 run(Workqueue*);
325
326 private:
327 const Symbol_table* symtab_;
328 const Target* target_;
329 const Stringpool* sympool_;
330 Output_file* of_;
331 Task_token* final_blocker_;
332};
333
92e059d8 334// This task function handles closing the file.
61ba1cf9 335
92e059d8 336class Close_task_runner : public Task_function_runner
61ba1cf9
ILT
337{
338 public:
92e059d8
ILT
339 Close_task_runner(Output_file* of)
340 : of_(of)
61ba1cf9
ILT
341 { }
342
92e059d8 343 // Run the operation.
61ba1cf9
ILT
344 void
345 run(Workqueue*);
346
347 private:
348 Output_file* of_;
a2fb1b05
ILT
349};
350
ead1e424
ILT
351// A small helper function to align an address.
352
353inline uint64_t
354align_address(uint64_t address, uint64_t addralign)
355{
356 if (addralign != 0)
357 address = (address + addralign - 1) &~ (addralign - 1);
358 return address;
359}
360
a2fb1b05
ILT
361} // End namespace gold.
362
363#endif // !defined(GOLD_LAYOUT_H)
This page took 0.044685 seconds and 4 git commands to generate.