Commit | Line | Data |
---|---|---|
bae7f79e ILT |
1 | // object.h -- support for an object file for linking in gold -*- C++ -*- |
2 | ||
3 | #ifndef GOLD_OBJECT_H | |
4 | #define GOLD_OBJECT_H | |
5 | ||
14bfc3f5 | 6 | #include <cassert> |
a2fb1b05 | 7 | #include <vector> |
14bfc3f5 | 8 | |
bae7f79e | 9 | #include "elfcpp.h" |
bae7f79e | 10 | #include "fileread.h" |
14bfc3f5 ILT |
11 | #include "target.h" |
12 | #include "symtab.h" | |
bae7f79e ILT |
13 | |
14 | namespace gold | |
15 | { | |
16 | ||
a2fb1b05 ILT |
17 | class Output_section; |
18 | class Layout; | |
19 | ||
bae7f79e ILT |
20 | // Data to pass from read_symbols() to add_symbols(). |
21 | ||
22 | struct Read_symbols_data | |
23 | { | |
24 | // Symbol data. | |
25 | File_view* symbols; | |
26 | // Size of symbol data in bytes. | |
27 | off_t symbols_size; | |
28 | // Symbol names. | |
29 | File_view* symbol_names; | |
30 | // Size of symbol name data in bytes. | |
31 | off_t symbol_names_size; | |
32 | }; | |
33 | ||
34 | // Object is an interface which represents either a 32-bit or a 64-bit | |
14bfc3f5 ILT |
35 | // input object. This can be a regular object file (ET_REL) or a |
36 | // shared object (ET_DYN). The actual instantiations are | |
37 | // Sized_object<32> and Sized_object<64> | |
bae7f79e ILT |
38 | |
39 | class Object | |
40 | { | |
41 | public: | |
42 | // NAME is the name of the object as we would report it to the user | |
43 | // (e.g., libfoo.a(bar.o) if this is in an archive. INPUT_FILE is | |
44 | // used to read the file. OFFSET is the offset within the input | |
45 | // file--0 for a .o or .so file, something else for a .a file. | |
14bfc3f5 ILT |
46 | Object(const std::string& name, Input_file* input_file, bool is_dynamic, |
47 | off_t offset = 0) | |
48 | : name_(name), input_file_(input_file), offset_(offset), | |
a2fb1b05 ILT |
49 | shnum_(0), is_dynamic_(is_dynamic), target_(NULL), |
50 | map_to_output_() | |
bae7f79e ILT |
51 | { } |
52 | ||
53 | virtual ~Object() | |
54 | { } | |
55 | ||
14bfc3f5 | 56 | // Return the name of the object as we would report it to the tuser. |
bae7f79e ILT |
57 | const std::string& |
58 | name() const | |
59 | { return this->name_; } | |
60 | ||
14bfc3f5 ILT |
61 | // Return whether this is a dynamic object. |
62 | bool | |
63 | is_dynamic() const | |
64 | { return this->is_dynamic_; } | |
65 | ||
14bfc3f5 ILT |
66 | // Return the target structure associated with this object. |
67 | Target* | |
a2fb1b05 | 68 | target() const |
14bfc3f5 ILT |
69 | { return this->target_; } |
70 | ||
a2fb1b05 ILT |
71 | // Lock the underlying file. |
72 | void | |
73 | lock() | |
74 | { this->input_file_->file().lock(); } | |
75 | ||
76 | // Unlock the underlying file. | |
77 | void | |
78 | unlock() | |
79 | { this->input_file_->file().unlock(); } | |
80 | ||
81 | // Return whether the underlying file is locked. | |
82 | bool | |
83 | is_locked() const | |
84 | { return this->input_file_->file().is_locked(); } | |
85 | ||
14bfc3f5 ILT |
86 | // Return the sized target structure associated with this object. |
87 | // This is like the target method but it returns a pointer of | |
88 | // appropriate checked type. | |
89 | template<int size, bool big_endian> | |
90 | Sized_target<size, big_endian>* | |
91 | sized_target(); | |
bae7f79e | 92 | |
a2fb1b05 ILT |
93 | // Read the symbol and relocation information. |
94 | Read_symbols_data | |
95 | read_symbols() | |
96 | { return this->do_read_symbols(); } | |
97 | ||
98 | // Add symbol information to the global symbol table. | |
99 | void | |
100 | add_symbols(Symbol_table* symtab, Read_symbols_data rd) | |
101 | { this->do_add_symbols(symtab, rd); } | |
102 | ||
103 | // Pass sections which should be included in the link to the Layout | |
104 | // object, and record where the sections go in the output file. | |
105 | void | |
106 | layout(Layout* lay) | |
107 | { this->do_layout(lay); } | |
108 | ||
bae7f79e | 109 | protected: |
a2fb1b05 ILT |
110 | // What we need to know to map an input section to an output |
111 | // section. We keep an array of these, one for each input section, | |
112 | // indexed by the input section number. | |
113 | struct Map_to_output | |
114 | { | |
115 | // The output section. This is NULL if the input section is to be | |
116 | // discarded. | |
117 | Output_section* output_section; | |
118 | // The offset within the output section. | |
119 | off_t offset; | |
120 | }; | |
121 | ||
bae7f79e ILT |
122 | // Read the symbols--implemented by child class. |
123 | virtual Read_symbols_data | |
124 | do_read_symbols() = 0; | |
125 | ||
126 | // Add symbol information to the global symbol table--implemented by | |
127 | // child class. | |
128 | virtual void | |
14bfc3f5 | 129 | do_add_symbols(Symbol_table*, Read_symbols_data) = 0; |
bae7f79e | 130 | |
a2fb1b05 ILT |
131 | // Lay out sections--implemented by child class. |
132 | virtual void | |
133 | do_layout(Layout*) = 0; | |
134 | ||
bae7f79e ILT |
135 | // Get the file. |
136 | Input_file* | |
137 | input_file() const | |
138 | { return this->input_file_; } | |
139 | ||
140 | // Get the offset into the file. | |
141 | off_t | |
142 | offset() const | |
143 | { return this->offset_; } | |
144 | ||
145 | // Get a view into the underlying file. | |
146 | const unsigned char* | |
147 | get_view(off_t start, off_t size); | |
148 | ||
a2fb1b05 ILT |
149 | // Get the number of sections. |
150 | unsigned int | |
151 | shnum(void) const | |
152 | { return this->shnum_; } | |
153 | ||
154 | // Set the number of sections. | |
155 | void | |
156 | set_shnum(int shnum) | |
157 | { this->shnum_ = shnum; } | |
158 | ||
14bfc3f5 ILT |
159 | // Set the target. |
160 | void | |
161 | set_target(Target* target) | |
162 | { this->target_ = target; } | |
163 | ||
bae7f79e ILT |
164 | // Read data from the underlying file. |
165 | void | |
166 | read(off_t start, off_t size, void* p); | |
167 | ||
168 | // Get a lasting view into the underlying file. | |
169 | File_view* | |
170 | get_lasting_view(off_t start, off_t size); | |
171 | ||
a2fb1b05 ILT |
172 | // Return the vector mapping input sections to output sections. |
173 | std::vector<Map_to_output>& | |
174 | map_to_output() | |
175 | { return this->map_to_output_; } | |
176 | ||
bae7f79e ILT |
177 | private: |
178 | // This class may not be copied. | |
179 | Object(const Object&); | |
180 | Object& operator=(const Object&); | |
181 | ||
182 | // Name of object as printed to use. | |
183 | std::string name_; | |
184 | // For reading the file. | |
185 | Input_file* input_file_; | |
186 | // Offset within the file--0 for an object file, non-0 for an | |
187 | // archive. | |
188 | off_t offset_; | |
a2fb1b05 ILT |
189 | // Number of input sections. |
190 | unsigned int shnum_; | |
14bfc3f5 ILT |
191 | // Whether this is a dynamic object. |
192 | bool is_dynamic_; | |
193 | // Target functions--may be NULL if the target is not known. | |
194 | Target* target_; | |
a2fb1b05 ILT |
195 | // Mapping from input sections to output section. |
196 | std::vector<Map_to_output> map_to_output_; | |
bae7f79e ILT |
197 | }; |
198 | ||
14bfc3f5 ILT |
199 | // Implement sized_target inline for efficiency. This approach breaks |
200 | // static type checking, but is made safe using asserts. | |
201 | ||
202 | template<int size, bool big_endian> | |
203 | inline Sized_target<size, big_endian>* | |
204 | Object::sized_target() | |
205 | { | |
206 | assert(this->target_->get_size() == size); | |
207 | assert(this->target_->is_big_endian() ? big_endian : !big_endian); | |
208 | return static_cast<Sized_target<size, big_endian>*>(this->target_); | |
209 | } | |
210 | ||
211 | // A regular object file. This is size and endian specific. | |
bae7f79e ILT |
212 | |
213 | template<int size, bool big_endian> | |
214 | class Sized_object : public Object | |
215 | { | |
216 | public: | |
217 | Sized_object(const std::string& name, Input_file* input_file, off_t offset, | |
218 | const typename elfcpp::Ehdr<size, big_endian>&); | |
219 | ||
220 | ~Sized_object(); | |
221 | ||
a2fb1b05 | 222 | // Set up the object file based on the ELF header. |
bae7f79e ILT |
223 | void |
224 | setup(const typename elfcpp::Ehdr<size, big_endian>&); | |
225 | ||
a2fb1b05 | 226 | // Read the symbols. |
bae7f79e ILT |
227 | Read_symbols_data |
228 | do_read_symbols(); | |
229 | ||
a2fb1b05 | 230 | // Add the symbols to the symbol table. |
bae7f79e | 231 | void |
14bfc3f5 ILT |
232 | do_add_symbols(Symbol_table*, Read_symbols_data); |
233 | ||
a2fb1b05 ILT |
234 | // Lay out the input sections. |
235 | void | |
236 | do_layout(Layout*); | |
237 | ||
238 | // Return the appropriate Sized_target structure. | |
14bfc3f5 ILT |
239 | Sized_target<size, big_endian>* |
240 | sized_target() | |
241 | { return this->Object::sized_target<size, big_endian>(); } | |
bae7f79e ILT |
242 | |
243 | private: | |
244 | // This object may not be copied. | |
245 | Sized_object(const Sized_object&); | |
246 | Sized_object& operator=(const Sized_object&); | |
247 | ||
a2fb1b05 ILT |
248 | // For convenience. |
249 | typedef Sized_object<size, big_endian> This; | |
250 | static const int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size; | |
251 | static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size; | |
252 | static const int sym_size = elfcpp::Elf_sizes<size>::sym_size; | |
253 | ||
254 | // Whether to include a section group in the link. | |
255 | bool | |
256 | include_section_group(Layout*, unsigned int, | |
257 | const elfcpp::Shdr<size, big_endian>&, | |
258 | std::vector<bool>*); | |
259 | ||
260 | // Whether to include a linkonce section in the link. | |
261 | bool | |
262 | include_linkonce_section(Layout*, const char*, | |
263 | const elfcpp::Shdr<size, big_endian>&); | |
264 | ||
bae7f79e ILT |
265 | // ELF file header e_flags field. |
266 | unsigned int flags_; | |
bae7f79e ILT |
267 | // File offset of section header table. |
268 | off_t shoff_; | |
bae7f79e ILT |
269 | // Offset of SHT_STRTAB section holding section names. |
270 | unsigned int shstrndx_; | |
271 | // Index of SHT_SYMTAB section. | |
272 | unsigned int symtab_shnum_; | |
14bfc3f5 ILT |
273 | // The entries in the symbol table for the external symbols. |
274 | Symbol** symbols_; | |
bae7f79e ILT |
275 | }; |
276 | ||
a2fb1b05 ILT |
277 | // The type of the list of input objects. |
278 | ||
279 | typedef std::list<Object*> Object_list; | |
280 | ||
bae7f79e ILT |
281 | // Return an Object appropriate for the input file. P is BYTES long, |
282 | // and holds the ELF header. | |
283 | ||
284 | extern Object* make_elf_object(const std::string& name, Input_file*, | |
285 | off_t offset, const unsigned char* p, | |
286 | off_t bytes); | |
287 | ||
288 | } // end namespace gold | |
289 | ||
290 | #endif // !defined(GOLD_OBJECT_H) |