New drop, with first cut of section layout code.
[binutils-gdb.git] / gold / layout.cc
1 // layout.cc -- lay out output file sections for gold
2
3 #include "gold.h"
4
5 #include <cassert>
6 #include <cstring>
7 #include <iostream>
8 #include <utility>
9
10 #include "output.h"
11 #include "layout.h"
12
13 namespace gold
14 {
15
16 // Layout_task methods.
17
18 Layout_task::~Layout_task()
19 {
20 }
21
22 // This task can be run when it is unblocked.
23
24 Task::Is_runnable_type
25 Layout_task::is_runnable(Workqueue*)
26 {
27 if (this->this_blocker_->is_blocked())
28 return IS_BLOCKED;
29 return IS_RUNNABLE;
30 }
31
32 // We don't need to hold any locks for the duration of this task. In
33 // fact this task will be the only one running.
34
35 Task_locker*
36 Layout_task::locks(Workqueue*)
37 {
38 return NULL;
39 }
40
41 // Lay out the sections. This is called after all the input objects
42 // have been read.
43
44 void
45 Layout_task::run(Workqueue*)
46 {
47 Layout layout(this->options_);
48 for (Object_list::const_iterator p = this->input_objects_->begin();
49 p != this->input_objects_->end();
50 ++p)
51 (*p)->layout(&layout);
52 }
53
54 // Layout methods.
55
56 // Hash a key we use to look up an output section mapping.
57
58 size_t
59 Layout::Hash_key::operator()(const Layout::Key& k) const
60 {
61 return reinterpret_cast<size_t>(k.first) + k.second.first + k.second.second;
62 }
63
64 // Whether to include this section in the link.
65
66 template<int size, bool big_endian>
67 bool
68 Layout::include_section(Object*, const char*,
69 const elfcpp::Shdr<size, big_endian>& shdr)
70 {
71 // Some section types are never linked. Some are only linked when
72 // doing a relocateable link.
73 switch (shdr.get_sh_type())
74 {
75 case elfcpp::SHT_NULL:
76 case elfcpp::SHT_SYMTAB:
77 case elfcpp::SHT_DYNSYM:
78 case elfcpp::SHT_STRTAB:
79 case elfcpp::SHT_HASH:
80 case elfcpp::SHT_DYNAMIC:
81 case elfcpp::SHT_SYMTAB_SHNDX:
82 return false;
83
84 case elfcpp::SHT_RELA:
85 case elfcpp::SHT_REL:
86 case elfcpp::SHT_GROUP:
87 return this->options_.is_relocatable();
88
89 default:
90 // FIXME: Handle stripping debug sections here.
91 return true;
92 }
93 }
94
95 // Return the output section to use for input section NAME, with
96 // header HEADER, from object OBJECT. Set *OFF to the offset of this
97 // input section without the output section.
98
99 template<int size, bool big_endian>
100 Output_section*
101 Layout::layout(Object* object, const char* name,
102 const elfcpp::Shdr<size, big_endian>& shdr, off_t* off)
103 {
104 if (!this->include_section(object, name, shdr))
105 return NULL;
106
107 // Unless we are doing a relocateable link, .gnu.linkonce sections
108 // are laid out as though they were named for the sections are
109 // placed into.
110 if (!this->options_.is_relocatable() && Layout::is_linkonce(name))
111 name = Layout::linkonce_output_name(name);
112
113 // FIXME: Handle SHF_OS_NONCONFORMING here.
114
115 // Canonicalize the section name.
116 name = this->namepool_.add(name);
117
118 // Find the output section. The output section is selected based on
119 // the section name, type, and flags.
120
121 // FIXME: If we want to do relaxation, we need to modify this
122 // algorithm. We also build a list of input sections for each
123 // output section. Then we relax all the input sections. Then we
124 // walk down the list and adjust all the offsets.
125
126 elfcpp::Elf_Word type = shdr.get_sh_type();
127 elfcpp::Elf_Xword flags = shdr.get_sh_flags();
128 const Key key(name, std::make_pair(type, flags));
129 const std::pair<Key, Output_section*> v(key, NULL);
130 std::pair<Section_name_map::iterator, bool> ins(
131 this->section_name_map_.insert(v));
132
133 Output_section* os;
134 if (!ins.second)
135 os = ins.first->second;
136 else
137 {
138 // This is the first time we've seen this name/type/flags
139 // combination.
140 os = this->make_output_section(name, type, flags);
141 ins.first->second = os;
142 }
143
144 // FIXME: Handle SHF_LINK_ORDER somewhere.
145
146 *off = os->add_input_section(object, name, shdr);
147
148 return os;
149 }
150
151 // Return whether SEG1 should be before SEG2 in the output file. This
152 // is based entirely on the segment type and flags. When this is
153 // called the segment addresses has normally not yet been set.
154
155 bool
156 Layout::segment_precedes(const Output_segment* seg1,
157 const Output_segment* seg2)
158 {
159 elfcpp::Elf_Word type1 = seg1->type();
160 elfcpp::Elf_Word type2 = seg2->type();
161
162 // The single PT_PHDR segment is required to precede any loadable
163 // segment. We simply make it always first.
164 if (type1 == elfcpp::PT_PHDR)
165 {
166 assert(type2 != elfcpp::PT_PHDR);
167 return true;
168 }
169 if (type2 == elfcpp::PT_PHDR)
170 return false;
171
172 // The single PT_INTERP segment is required to precede any loadable
173 // segment. We simply make it always second.
174 if (type1 == elfcpp::PT_INTERP)
175 {
176 assert(type2 != elfcpp::PT_INTERP);
177 return true;
178 }
179 if (type2 == elfcpp::PT_INTERP)
180 return false;
181
182 // We then put PT_LOAD segments before any other segments.
183 if (type1 == elfcpp::PT_LOAD && type2 != elfcpp::PT_LOAD)
184 return true;
185 if (type2 == elfcpp::PT_LOAD && type1 != elfcpp::PT_LOAD)
186 return false;
187
188 const elfcpp::Elf_Word flags1 = seg1->flags();
189 const elfcpp::Elf_Word flags2 = seg2->flags();
190
191 // The order of non-PT_LOAD segments is unimportant. We simply sort
192 // by the numeric segment type and flags values. There should not
193 // be more than one segment with the same type and flags.
194 if (type1 != elfcpp::PT_LOAD)
195 {
196 if (type1 != type2)
197 return type1 < type2;
198 assert(flags1 != flags2);
199 return flags1 < flags2;
200 }
201
202 // We sort PT_LOAD segments based on the flags. Readonly segments
203 // come before writable segments. Then executable segments come
204 // before non-executable segments. Then the unlikely case of a
205 // non-readable segment comes before the normal case of a readable
206 // segment. If there are multiple segments with the same type and
207 // flags, we require that the address be set, and we sort by
208 // virtual address and then physical address.
209 if ((flags1 & elfcpp::PF_W) != (flags2 & elfcpp::PF_W))
210 return (flags1 & elfcpp::PF_W) == 0;
211 if ((flags1 & elfcpp::PF_X) != (flags2 & elfcpp::PF_X))
212 return (flags1 & elfcpp::PF_X) != 0;
213 if ((flags1 & elfcpp::PF_R) != (flags2 & elfcpp::PF_R))
214 return (flags1 & elfcpp::PF_R) == 0;
215
216 uint64_t vaddr1 = seg1->vaddr();
217 uint64_t vaddr2 = seg2->vaddr();
218 if (vaddr1 != vaddr2)
219 return vaddr1 < vaddr2;
220
221 uint64_t paddr1 = seg1->paddr();
222 uint64_t paddr2 = seg2->paddr();
223 assert(paddr1 != paddr2);
224 return paddr1 < paddr2;
225 }
226
227 // Map section flags to segment flags.
228
229 elfcpp::Elf_Word
230 Layout::section_flags_to_segment(elfcpp::Elf_Xword flags)
231 {
232 elfcpp::Elf_Word ret = elfcpp::PF_R;
233 if ((flags & elfcpp::SHF_WRITE) != 0)
234 ret |= elfcpp::PF_W;
235 if ((flags & elfcpp::SHF_EXECINSTR) != 0)
236 ret |= elfcpp::PF_X;
237 return ret;
238 }
239
240 // Make a new Output_section, and attach it to segments as
241 // appropriate.
242
243 Output_section*
244 Layout::make_output_section(const char* name, elfcpp::Elf_Word type,
245 elfcpp::Elf_Xword flags)
246 {
247 Output_section* os = new Output_section(name, type, flags);
248
249 if ((flags & elfcpp::SHF_ALLOC) == 0)
250 this->section_list_.push_back(os);
251 else
252 {
253 // This output section goes into a PT_LOAD segment.
254
255 elfcpp::Elf_Word seg_flags = Layout::section_flags_to_segment(flags);
256
257 // The only thing we really care about for PT_LOAD segments is
258 // whether or not they are writable, so that is how we search
259 // for them. People who need segments sorted on some other
260 // basis will have to wait until we implement a mechanism for
261 // them to describe the segments they want.
262
263 Segment_list::const_iterator p;
264 for (p = this->segment_list_.begin();
265 p != this->segment_list_.end();
266 ++p)
267 {
268 if ((*p)->type() == elfcpp::PT_LOAD
269 && ((*p)->flags() & elfcpp::PF_W) == (seg_flags & elfcpp::PF_W))
270 {
271 (*p)->add_output_section(os);
272 if ((*p)->flags() != seg_flags)
273 (*p)->update_flags(seg_flags);
274 break;
275 }
276 }
277
278 if (p == this->segment_list_.end())
279 {
280 Output_segment* oseg = new Output_segment(elfcpp::PT_LOAD,
281 seg_flags);
282 this->segment_list_.push_back(oseg);
283 oseg->add_output_section(os);
284 }
285
286 // If we see a loadable SHT_NOTE section, we create a PT_NOTE
287 // segment.
288 if (type == elfcpp::SHT_NOTE)
289 {
290 // See if we already have an equivalent PT_NOTE segment.
291 for (p = this->segment_list_.begin();
292 p != segment_list_.end();
293 ++p)
294 {
295 if ((*p)->type() == elfcpp::PT_NOTE
296 && (((*p)->flags() & elfcpp::PF_W)
297 == (seg_flags & elfcpp::PF_W)))
298 {
299 (*p)->add_output_section(os);
300 if ((*p)->flags() != seg_flags)
301 (*p)->update_flags(seg_flags);
302 break;
303 }
304 }
305
306 if (p == this->segment_list_.end())
307 {
308 Output_segment* oseg = new Output_segment(elfcpp::PT_NOTE,
309 seg_flags);
310 this->segment_list_.push_back(oseg);
311 oseg->add_output_section(os);
312 }
313 }
314 }
315
316 return os;
317 }
318
319 // The mapping of .gnu.linkonce section names to real section names.
320
321 #define MAPPING_INIT(f, t) { f, sizeof(f) - 1, t }
322 const Layout::Linkonce_mapping Layout::linkonce_mapping[] =
323 {
324 MAPPING_INIT("d.rel.ro", ".data.rel.ro"), // Must be before "d".
325 MAPPING_INIT("t", ".text"),
326 MAPPING_INIT("r", ".rodata"),
327 MAPPING_INIT("d", ".data"),
328 MAPPING_INIT("b", ".bss"),
329 MAPPING_INIT("s", ".sdata"),
330 MAPPING_INIT("sb", ".sbss"),
331 MAPPING_INIT("s2", ".sdata2"),
332 MAPPING_INIT("sb2", ".sbss2"),
333 MAPPING_INIT("wi", ".debug_info"),
334 MAPPING_INIT("td", ".tdata"),
335 MAPPING_INIT("tb", ".tbss"),
336 MAPPING_INIT("lr", ".lrodata"),
337 MAPPING_INIT("l", ".ldata"),
338 MAPPING_INIT("lb", ".lbss"),
339 };
340 #undef MAPPING_INIT
341
342 const int Layout::linkonce_mapping_count =
343 sizeof(Layout::linkonce_mapping) / sizeof(Layout::linkonce_mapping[0]);
344
345 // Return the name of the output section to use for a .gnu.linkonce
346 // section. This is based on the default ELF linker script of the old
347 // GNU linker. For example, we map a name like ".gnu.linkonce.t.foo"
348 // to ".text".
349
350 const char*
351 Layout::linkonce_output_name(const char* name)
352 {
353 const char* s = name + sizeof(".gnu.linkonce") - 1;
354 if (*s != '.')
355 return name;
356 ++s;
357 const Linkonce_mapping* plm = linkonce_mapping;
358 for (int i = 0; i < linkonce_mapping_count; ++i, ++plm)
359 {
360 if (strncmp(s, plm->from, plm->fromlen) == 0 && s[plm->fromlen] == '.')
361 return plm->to;
362 }
363 return name;
364 }
365
366 // Record the signature of a comdat section, and return whether to
367 // include it in the link. If GROUP is true, this is a regular
368 // section group. If GROUP is false, this is a group signature
369 // derived from the name of a linkonce section. We want linkonce
370 // signatures and group signatures to block each other, but we don't
371 // want a linkonce signature to block another linkonce signature.
372
373 bool
374 Layout::add_comdat(const char* signature, bool group)
375 {
376 std::string sig(signature);
377 std::pair<Signatures::iterator, bool> ins(
378 this->signatures_.insert(std::make_pair(signature, group)));
379
380 if (ins.second)
381 {
382 // This is the first time we've seen this signature.
383 return true;
384 }
385
386 if (ins.first->second)
387 {
388 // We've already seen a real section group with this signature.
389 return false;
390 }
391 else if (group)
392 {
393 // This is a real section group, and we've already seen a
394 // linkonce section with tihs signature. Record that we've seen
395 // a section group, and don't include this section group.
396 ins.first->second = true;
397 return false;
398 }
399 else
400 {
401 // We've already seen a linkonce section and this is a linkonce
402 // section. These don't block each other--this may be the same
403 // symbol name with different section types.
404 return true;
405 }
406 }
407
408 // Instantiate the templates we need. We could use the configure
409 // script to restrict this to only the ones for implemented targets.
410
411 template
412 Output_section*
413 Layout::layout<32, false>(Object* object, const char* name,
414 const elfcpp::Shdr<32, false>& shdr, off_t*);
415
416 template
417 Output_section*
418 Layout::layout<32, true>(Object* object, const char* name,
419 const elfcpp::Shdr<32, true>& shdr, off_t*);
420
421 template
422 Output_section*
423 Layout::layout<64, false>(Object* object, const char* name,
424 const elfcpp::Shdr<64, false>& shdr, off_t*);
425
426 template
427 Output_section*
428 Layout::layout<64, true>(Object* object, const char* name,
429 const elfcpp::Shdr<64, true>& shdr, off_t*);
430
431
432 } // End namespace gold.