New drop, with first cut of section layout code.
[external/binutils.git] / gold / layout.h
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
11 #include "options.h"
12 #include "workqueue.h"
13 #include "object.h"
14 #include "stringpool.h"
15
16 namespace gold
17 {
18
19 class Output_section;
20 class Output_segment;
21
22 // This Task handles mapping the input sections to output sections and
23 // laying them out in memory.
24
25 class Layout_task : public Task
26 {
27  public:
28   // OPTIONS is the command line options, INPUT_OBJECTS is the list of
29   // input objects, THIS_BLOCKER is a token which blocks this task
30   // from executing until all the input symbols have been read.
31   Layout_task(const General_options& options, const Object_list* input_objects,
32               Task_token* this_blocker)
33     : options_(options), input_objects_(input_objects),
34       this_blocker_(this_blocker)
35   { }
36
37   ~Layout_task();
38
39   // The standard Task methods.
40
41   Is_runnable_type
42   is_runnable(Workqueue*);
43
44   Task_locker*
45   locks(Workqueue*);
46
47   void
48   run(Workqueue*);
49
50  private:
51   Layout_task(const Layout_task&);
52   Layout_task& operator=(const Layout_task&);
53
54   const General_options& options_;
55   const Object_list* input_objects_;
56   Task_token* this_blocker_;
57 };
58
59 // This class handles the details of laying out input sections.
60
61 class Layout
62 {
63  public:
64   Layout(const General_options& options)
65     : options_(options), namepool_(), signatures_(),
66       section_name_map_(), segment_list_()
67   { }
68
69   // Given an input section named NAME with data in SHDR from the
70   // object file OBJECT, return the output section where this input
71   // section should go.  Set *OFFSET to the offset within the output
72   // section.
73   template<int size, bool big_endian>
74   Output_section*
75   layout(Object *object, const char* name,
76          const elfcpp::Shdr<size, big_endian>& shdr, off_t* offset);
77
78   // Return whether a section is a .gnu.linkonce section, given the
79   // section name.
80   static inline bool
81   is_linkonce(const char* name)
82   { return strncmp(name, ".gnu.linkonce", sizeof(".gnu.linkonce") - 1) == 0; }
83
84   // Record the signature of a comdat section, and return whether to
85   // include it in the link.  The GROUP parameter is true for a
86   // section group signature, false for a signature derived from a
87   // .gnu.linkonce section.
88   bool
89   add_comdat(const char*, bool group);
90
91  private:
92   Layout(const Layout&);
93   Layout& operator=(const Layout&);
94
95   // Mapping from .gnu.linkonce section names to output section names.
96   struct Linkonce_mapping
97   {
98     const char* from;
99     int fromlen;
100     const char* to;
101   };
102   static const Linkonce_mapping linkonce_mapping[];
103   static const int linkonce_mapping_count;
104
105   // Return whether to include this section in the link.
106   template<int size, bool big_endian>
107   bool
108   include_section(Object* object, const char* name,
109                   const elfcpp::Shdr<size, big_endian>&);
110
111   // Return the output section name to use for a linkonce section
112   // name.
113   static const char*
114   linkonce_output_name(const char* name);
115
116   // Create a new Output_section.
117   Output_section*
118   make_output_section(const char* name, elfcpp::Elf_Word type,
119                       elfcpp::Elf_Xword flags);
120
121   // Return whether SEG1 comes before SEG2 in the output file.
122   static bool
123   Layout::segment_precedes(const Output_segment* seg1,
124                            const Output_segment* seg2);
125
126   // Map from section flags to segment flags.
127   static elfcpp::Elf_Word
128   section_flags_to_segment(elfcpp::Elf_Xword flags);
129
130   // A mapping used for group signatures.
131   typedef Unordered_map<std::string, bool> Signatures;
132
133   // Mapping from input section name/type/flags to output section.  We
134   // use canonicalized strings here.
135
136   typedef std::pair<const char*,
137                     std::pair<elfcpp::Elf_Word, elfcpp::Elf_Xword> > Key;
138
139   struct Hash_key
140   {
141     size_t
142     operator()(const Key& k) const;
143   };
144
145   typedef Unordered_map<Key, Output_section*, Hash_key> Section_name_map;
146
147   // A comparison class for segments.
148
149   struct Compare_segments
150   {
151     bool
152     operator()(const Output_segment* seg1, const Output_segment* seg2)
153     { return Layout::segment_precedes(seg1, seg2); }
154   };
155
156   // The list of segments.
157
158   typedef std::list<Output_segment*> Segment_list;
159
160   // The list of sections not attached to a segment.
161
162   typedef std::list<Output_section*> Section_list;
163
164   // A reference to the options on the command line.
165   const General_options& options_;
166   // The output section names.
167   Stringpool namepool_;
168   // The list of group sections and linkonce sections which we have seen.
169   Signatures signatures_;
170   // The mapping from input section name/type/flags to output sections.
171   Section_name_map section_name_map_;
172   // The list of output segments.
173   Segment_list segment_list_;
174   // The list of output sections which are not attached to any output
175   // segment.
176   Section_list section_list_;
177 };
178
179 } // End namespace gold.
180
181 #endif // !defined(GOLD_LAYOUT_H)