1 // layout.h -- lay out output file sections for gold -*- C++ -*-
12 #include "workqueue.h"
14 #include "stringpool.h"
22 class Output_section_symtab
;
23 class Output_section_headers
;
27 // This Task handles mapping the input sections to output sections and
28 // laying them out in memory.
30 class Layout_task
: public Task
33 // OPTIONS is the command line options, INPUT_OBJECTS is the list of
34 // input objects, THIS_BLOCKER is a token which blocks this task
35 // from executing until all the input symbols have been read.
36 Layout_task(const General_options
& options
,
37 const Input_objects
* input_objects
,
39 Task_token
* this_blocker
)
40 : options_(options
), input_objects_(input_objects
), symtab_(symtab
),
41 this_blocker_(this_blocker
)
46 // The standard Task methods.
49 is_runnable(Workqueue
*);
58 Layout_task(const Layout_task
&);
59 Layout_task
& operator=(const Layout_task
&);
61 const General_options
& options_
;
62 const Input_objects
* input_objects_
;
63 Symbol_table
* symtab_
;
64 Task_token
* this_blocker_
;
67 // This class handles the details of laying out input sections.
72 Layout(const General_options
& options
);
74 // Initialize the object.
78 // Given an input section named NAME with data in SHDR from the
79 // object file OBJECT, return the output section where this input
80 // section should go. Set *OFFSET to the offset within the output
82 template<int size
, bool big_endian
>
84 layout(Object
*object
, const char* name
,
85 const elfcpp::Shdr
<size
, big_endian
>& shdr
, off_t
* offset
);
87 // Return whether a section is a .gnu.linkonce section, given the
90 is_linkonce(const char* name
)
91 { return strncmp(name
, ".gnu.linkonce", sizeof(".gnu.linkonce") - 1) == 0; }
93 // Record the signature of a comdat section, and return whether to
94 // include it in the link. The GROUP parameter is true for a
95 // section group signature, false for a signature derived from a
96 // .gnu.linkonce section.
98 add_comdat(const char*, bool group
);
100 // Finalize the layout after all the input sections have been added.
102 finalize(const Input_objects
*, Symbol_table
*);
104 // The list of segments.
106 typedef std::vector
<Output_segment
*> Segment_list
;
108 // The list of sections not attached to a segment.
110 typedef std::list
<Output_section
*> Section_list
;
112 // The list of information to write out which is not attached to
113 // either a section or a segment.
114 typedef std::list
<Output_data
*> Data_list
;
117 Layout(const Layout
&);
118 Layout
& operator=(const Layout
&);
120 // Mapping from .gnu.linkonce section names to output section names.
121 struct Linkonce_mapping
127 static const Linkonce_mapping linkonce_mapping
[];
128 static const int linkonce_mapping_count
;
130 // Find the first read-only PT_LOAD segment, creating one if
133 find_first_load_seg();
135 // Set the final file offsets of all the segments.
137 set_segment_offsets(const Target
*, Output_segment
*);
139 // Set the final file offsets of all the sections not associated
142 set_section_offsets(off_t
);
144 // Create the output sections for the symbol table.
146 create_symtab_sections(const Input_objects
*, Symbol_table
*,
147 Output_section
** osymtab
,
148 Output_section
** ostrtab
);
150 // Create the .shstrtab section.
154 // Create the section header table.
155 Output_section_headers
*
156 create_shdrs(int size
, off_t
);
158 // Return whether to include this section in the link.
159 template<int size
, bool big_endian
>
161 include_section(Object
* object
, const char* name
,
162 const elfcpp::Shdr
<size
, big_endian
>&);
164 // Return the output section name to use for a linkonce section
167 linkonce_output_name(const char* name
);
169 // Create a new Output_section.
171 make_output_section(const char* name
, elfcpp::Elf_Word type
,
172 elfcpp::Elf_Xword flags
);
174 // Return whether SEG1 comes before SEG2 in the output file.
176 segment_precedes(const Output_segment
* seg1
, const Output_segment
* seg2
);
178 // Map from section flags to segment flags.
179 static elfcpp::Elf_Word
180 section_flags_to_segment(elfcpp::Elf_Xword flags
);
182 // A mapping used for group signatures.
183 typedef Unordered_map
<std::string
, bool> Signatures
;
185 // Mapping from input section name/type/flags to output section. We
186 // use canonicalized strings here.
188 typedef std::pair
<const char*,
189 std::pair
<elfcpp::Elf_Word
, elfcpp::Elf_Xword
> > Key
;
194 operator()(const Key
& k
) const;
197 typedef Unordered_map
<Key
, Output_section
*, Hash_key
> Section_name_map
;
199 // A comparison class for segments.
201 struct Compare_segments
204 operator()(const Output_segment
* seg1
, const Output_segment
* seg2
)
205 { return Layout::segment_precedes(seg1
, seg2
); }
208 // A reference to the options on the command line.
209 const General_options
& options_
;
210 // The output section names.
211 Stringpool namepool_
;
212 // The output symbol names.
214 // The list of group sections and linkonce sections which we have seen.
215 Signatures signatures_
;
216 // The mapping from input section name/type/flags to output sections.
217 Section_name_map section_name_map_
;
218 // The list of output segments.
219 Segment_list segment_list_
;
220 // The list of output sections which are not attached to any output
222 Section_list section_list_
;
225 } // End namespace gold.
227 #endif // !defined(GOLD_LAYOUT_H)