1 // layout.cc -- lay out output file sections for gold
16 // Layout_task methods.
18 Layout_task::~Layout_task()
22 // This task can be run when it is unblocked.
24 Task::Is_runnable_type
25 Layout_task::is_runnable(Workqueue
*)
27 if (this->this_blocker_
->is_blocked())
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.
36 Layout_task::locks(Workqueue
*)
41 // Lay out the sections. This is called after all the input objects
45 Layout_task::run(Workqueue
*)
47 Layout
layout(this->options_
);
48 for (Object_list::const_iterator p
= this->input_objects_
->begin();
49 p
!= this->input_objects_
->end();
51 (*p
)->layout(&layout
);
56 // Hash a key we use to look up an output section mapping.
59 Layout::Hash_key::operator()(const Layout::Key
& k
) const
61 return reinterpret_cast<size_t>(k
.first
) + k
.second
.first
+ k
.second
.second
;
64 // Whether to include this section in the link.
66 template<int size
, bool big_endian
>
68 Layout::include_section(Object
*, const char*,
69 const elfcpp::Shdr
<size
, big_endian
>& shdr
)
71 // Some section types are never linked. Some are only linked when
72 // doing a relocateable link.
73 switch (shdr
.get_sh_type())
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
:
84 case elfcpp::SHT_RELA
:
86 case elfcpp::SHT_GROUP
:
87 return this->options_
.is_relocatable();
90 // FIXME: Handle stripping debug sections here.
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.
99 template<int size
, bool big_endian
>
101 Layout::layout(Object
* object
, const char* name
,
102 const elfcpp::Shdr
<size
, big_endian
>& shdr
, off_t
* off
)
104 if (!this->include_section(object
, name
, shdr
))
107 // Unless we are doing a relocateable link, .gnu.linkonce sections
108 // are laid out as though they were named for the sections are
110 if (!this->options_
.is_relocatable() && Layout::is_linkonce(name
))
111 name
= Layout::linkonce_output_name(name
);
113 // FIXME: Handle SHF_OS_NONCONFORMING here.
115 // Canonicalize the section name.
116 name
= this->namepool_
.add(name
);
118 // Find the output section. The output section is selected based on
119 // the section name, type, and flags.
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.
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
));
135 os
= ins
.first
->second
;
138 // This is the first time we've seen this name/type/flags
140 os
= this->make_output_section(name
, type
, flags
);
141 ins
.first
->second
= os
;
144 // FIXME: Handle SHF_LINK_ORDER somewhere.
146 *off
= os
->add_input_section(object
, name
, shdr
);
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.
156 Layout::segment_precedes(const Output_segment
* seg1
,
157 const Output_segment
* seg2
)
159 elfcpp::Elf_Word type1
= seg1
->type();
160 elfcpp::Elf_Word type2
= seg2
->type();
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
)
166 assert(type2
!= elfcpp::PT_PHDR
);
169 if (type2
== elfcpp::PT_PHDR
)
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
)
176 assert(type2
!= elfcpp::PT_INTERP
);
179 if (type2
== elfcpp::PT_INTERP
)
182 // We then put PT_LOAD segments before any other segments.
183 if (type1
== elfcpp::PT_LOAD
&& type2
!= elfcpp::PT_LOAD
)
185 if (type2
== elfcpp::PT_LOAD
&& type1
!= elfcpp::PT_LOAD
)
188 const elfcpp::Elf_Word flags1
= seg1
->flags();
189 const elfcpp::Elf_Word flags2
= seg2
->flags();
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
)
197 return type1
< type2
;
198 assert(flags1
!= flags2
);
199 return flags1
< flags2
;
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;
216 uint64_t vaddr1
= seg1
->vaddr();
217 uint64_t vaddr2
= seg2
->vaddr();
218 if (vaddr1
!= vaddr2
)
219 return vaddr1
< vaddr2
;
221 uint64_t paddr1
= seg1
->paddr();
222 uint64_t paddr2
= seg2
->paddr();
223 assert(paddr1
!= paddr2
);
224 return paddr1
< paddr2
;
227 // Map section flags to segment flags.
230 Layout::section_flags_to_segment(elfcpp::Elf_Xword flags
)
232 elfcpp::Elf_Word ret
= elfcpp::PF_R
;
233 if ((flags
& elfcpp::SHF_WRITE
) != 0)
235 if ((flags
& elfcpp::SHF_EXECINSTR
) != 0)
240 // Make a new Output_section, and attach it to segments as
244 Layout::make_output_section(const char* name
, elfcpp::Elf_Word type
,
245 elfcpp::Elf_Xword flags
)
247 Output_section
* os
= new Output_section(name
, type
, flags
);
249 if ((flags
& elfcpp::SHF_ALLOC
) == 0)
250 this->section_list_
.push_back(os
);
253 // This output section goes into a PT_LOAD segment.
255 elfcpp::Elf_Word seg_flags
= Layout::section_flags_to_segment(flags
);
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.
263 Segment_list::const_iterator p
;
264 for (p
= this->segment_list_
.begin();
265 p
!= this->segment_list_
.end();
268 if ((*p
)->type() == elfcpp::PT_LOAD
269 && ((*p
)->flags() & elfcpp::PF_W
) == (seg_flags
& elfcpp::PF_W
))
271 (*p
)->add_output_section(os
);
272 if ((*p
)->flags() != seg_flags
)
273 (*p
)->update_flags(seg_flags
);
278 if (p
== this->segment_list_
.end())
280 Output_segment
* oseg
= new Output_segment(elfcpp::PT_LOAD
,
282 this->segment_list_
.push_back(oseg
);
283 oseg
->add_output_section(os
);
286 // If we see a loadable SHT_NOTE section, we create a PT_NOTE
288 if (type
== elfcpp::SHT_NOTE
)
290 // See if we already have an equivalent PT_NOTE segment.
291 for (p
= this->segment_list_
.begin();
292 p
!= segment_list_
.end();
295 if ((*p
)->type() == elfcpp::PT_NOTE
296 && (((*p
)->flags() & elfcpp::PF_W
)
297 == (seg_flags
& elfcpp::PF_W
)))
299 (*p
)->add_output_section(os
);
300 if ((*p
)->flags() != seg_flags
)
301 (*p
)->update_flags(seg_flags
);
306 if (p
== this->segment_list_
.end())
308 Output_segment
* oseg
= new Output_segment(elfcpp::PT_NOTE
,
310 this->segment_list_
.push_back(oseg
);
311 oseg
->add_output_section(os
);
319 // The mapping of .gnu.linkonce section names to real section names.
321 #define MAPPING_INIT(f, t) { f, sizeof(f) - 1, t }
322 const Layout::Linkonce_mapping
Layout::linkonce_mapping
[] =
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"),
342 const int Layout::linkonce_mapping_count
=
343 sizeof(Layout::linkonce_mapping
) / sizeof(Layout::linkonce_mapping
[0]);
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"
351 Layout::linkonce_output_name(const char* name
)
353 const char* s
= name
+ sizeof(".gnu.linkonce") - 1;
357 const Linkonce_mapping
* plm
= linkonce_mapping
;
358 for (int i
= 0; i
< linkonce_mapping_count
; ++i
, ++plm
)
360 if (strncmp(s
, plm
->from
, plm
->fromlen
) == 0 && s
[plm
->fromlen
] == '.')
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.
374 Layout::add_comdat(const char* signature
, bool group
)
376 std::string
sig(signature
);
377 std::pair
<Signatures::iterator
, bool> ins(
378 this->signatures_
.insert(std::make_pair(signature
, group
)));
382 // This is the first time we've seen this signature.
386 if (ins
.first
->second
)
388 // We've already seen a real section group with this signature.
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;
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.
408 // Instantiate the templates we need. We could use the configure
409 // script to restrict this to only the ones for implemented targets.
413 Layout::layout
<32, false>(Object
* object
, const char* name
,
414 const elfcpp::Shdr
<32, false>& shdr
, off_t
*);
418 Layout::layout
<32, true>(Object
* object
, const char* name
,
419 const elfcpp::Shdr
<32, true>& shdr
, off_t
*);
423 Layout::layout
<64, false>(Object
* object
, const char* name
,
424 const elfcpp::Shdr
<64, false>& shdr
, off_t
*);
428 Layout::layout
<64, true>(Object
* object
, const char* name
,
429 const elfcpp::Shdr
<64, true>& shdr
, off_t
*);
432 } // End namespace gold.