2006-09-08 H.J. Lu <hongjiu.lu@intel.com>
[binutils.git] / gold / object.cc
blobbad7f478467ecadd9166ab89ca0eaeb3e724c70c
1 // object.cc -- support for an object file for linking in gold
3 #include "gold.h"
5 #include <cerrno>
6 #include <cstring>
7 #include <cassert>
9 #include "object.h"
10 #include "target-select.h"
12 namespace gold
15 // Class Object.
17 const unsigned char*
18 Object::get_view(off_t start, off_t size)
20 return this->input_file_->file().get_view(start + this->offset_, size);
23 void
24 Object::read(off_t start, off_t size, void* p)
26 this->input_file_->file().read(start + this->offset_, size, p);
29 File_view*
30 Object::get_lasting_view(off_t start, off_t size)
32 return this->input_file_->file().get_lasting_view(start + this->offset_,
33 size);
36 // Class Sized_object.
38 template<int size, bool big_endian>
39 Sized_object<size, big_endian>::Sized_object(
40 const std::string& name,
41 Input_file* input_file,
42 off_t offset,
43 const elfcpp::Ehdr<size, big_endian>& ehdr)
44 : Object(name, input_file, false, offset),
45 osabi_(ehdr.get_e_ident()[elfcpp::EI_OSABI]),
46 abiversion_(ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]),
47 machine_(ehdr.get_e_machine()),
48 flags_(ehdr.get_e_flags()),
49 shoff_(ehdr.get_e_shoff()),
50 shnum_(0),
51 shstrndx_(0),
52 symtab_shnum_(0),
53 symbols_(NULL)
55 if (ehdr.get_e_ehsize() != elfcpp::Elf_sizes<size>::ehdr_size)
57 fprintf(stderr, _("%s: %s: bad e_ehsize field (%d != %d)\n"),
58 program_name, this->name().c_str(), ehdr.get_e_ehsize(),
59 elfcpp::Elf_sizes<size>::ehdr_size);
60 gold_exit(false);
62 if (ehdr.get_e_shentsize() != elfcpp::Elf_sizes<size>::shdr_size)
64 fprintf(stderr, _("%s: %s: bad e_shentsize field (%d != %d)\n"),
65 program_name, this->name().c_str(), ehdr.get_e_shentsize(),
66 elfcpp::Elf_sizes<size>::shdr_size);
67 gold_exit(false);
71 template<int size, bool big_endian>
72 Sized_object<size, big_endian>::~Sized_object()
76 // Set up an object file bsaed on the file header. This sets up the
77 // target and reads the section information.
79 template<int size, bool big_endian>
80 void
81 Sized_object<size, big_endian>::setup(
82 const elfcpp::Ehdr<size, big_endian>& ehdr)
84 Target* target = select_target(this->machine_, size, big_endian,
85 this->osabi_, this->abiversion_);
86 if (target == NULL)
88 fprintf(stderr, _("%s: %s: unsupported ELF machine number %d\n"),
89 program_name, this->name().c_str(), this->machine_);
90 gold_exit(false);
92 this->set_target(target);
93 unsigned int shnum = ehdr.get_e_shnum();
94 unsigned int shstrndx = ehdr.get_e_shstrndx();
95 if ((shnum == 0 || shstrndx == elfcpp::SHN_XINDEX)
96 && this->shoff_ != 0)
98 const unsigned char* p = this->get_view
99 (this->shoff_, elfcpp::Elf_sizes<size>::shdr_size);
100 elfcpp::Shdr<size, big_endian> shdr(p);
101 if (shnum == 0)
102 shnum = shdr.get_sh_size();
103 if (shstrndx == elfcpp::SHN_XINDEX)
104 shstrndx = shdr.get_sh_link();
106 this->shnum_ = shnum;
107 this->shstrndx_ = shstrndx;
109 if (shnum == 0)
110 return;
112 // Find the SHT_SYMTAB section.
113 const unsigned char* p = this->get_view
114 (this->shoff_, shnum * elfcpp::Elf_sizes<size>::shdr_size);
115 // Skip the first section, which is always empty.
116 p += elfcpp::Elf_sizes<size>::shdr_size;
117 for (unsigned int i = 1; i < shnum; ++i)
119 elfcpp::Shdr<size, big_endian> shdr(p);
120 if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
122 this->symtab_shnum_ = i;
123 break;
125 p += elfcpp::Elf_sizes<size>::shdr_size;
129 // Read the symbols and relocations from an object file.
131 template<int size, bool big_endian>
132 Read_symbols_data
133 Sized_object<size, big_endian>::do_read_symbols()
135 if (this->symtab_shnum_ == 0)
137 // No symbol table. Weird but legal.
138 Read_symbols_data ret;
139 ret.symbols = NULL;
140 ret.symbols_size = 0;
141 ret.symbol_names = NULL;
142 ret.symbol_names_size = 0;
143 return ret;
146 int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
148 // Read the symbol table section header.
149 off_t symtabshdroff = this->shoff_ + (this->symtab_shnum_ * shdr_size);
150 const unsigned char* psymtabshdr = this->get_view(symtabshdroff, shdr_size);
151 elfcpp::Shdr<size, big_endian> symtabshdr(psymtabshdr);
152 assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
154 // We only need the external symbols.
155 int sym_size = elfcpp::Elf_sizes<size>::sym_size;
156 off_t locsize = symtabshdr.get_sh_info() * sym_size;
157 off_t extoff = symtabshdr.get_sh_offset() + locsize;
158 off_t extsize = symtabshdr.get_sh_size() - locsize;
160 // Read the symbol table.
161 File_view* fvsymtab = this->get_lasting_view(extoff, extsize);
163 // Read the section header for the symbol names.
164 unsigned int strtab_shnum = symtabshdr.get_sh_link();
165 if (strtab_shnum == 0 || strtab_shnum >= this->shnum_)
167 fprintf(stderr, _("%s: %s: invalid symbol table name index: %u\n"),
168 program_name, this->name().c_str(), strtab_shnum);
169 gold_exit(false);
171 off_t strtabshdroff = this->shoff_ + (strtab_shnum * shdr_size);
172 const unsigned char *pstrtabshdr = this->get_view(strtabshdroff, shdr_size);
173 elfcpp::Shdr<size, big_endian> strtabshdr(pstrtabshdr);
174 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
176 fprintf(stderr,
177 _("%s: %s: symbol table name section has wrong type: %u\n"),
178 program_name, this->name().c_str(),
179 static_cast<unsigned int>(strtabshdr.get_sh_type()));
180 gold_exit(false);
183 // Read the symbol names.
184 File_view* fvstrtab = this->get_lasting_view(strtabshdr.get_sh_offset(),
185 strtabshdr.get_sh_size());
187 Read_symbols_data ret;
188 ret.symbols = fvsymtab;
189 ret.symbols_size = extsize;
190 ret.symbol_names = fvstrtab;
191 ret.symbol_names_size = strtabshdr.get_sh_size();
193 return ret;
196 // Add the symbols to the symbol table.
198 template<int size, bool big_endian>
199 void
200 Sized_object<size, big_endian>::do_add_symbols(Symbol_table* symtab,
201 Read_symbols_data sd)
203 if (sd.symbols == NULL)
205 assert(sd.symbol_names == NULL);
206 return;
209 unsigned int sym_size = elfcpp::Elf_sizes<size>::sym_size;
210 size_t symcount = sd.symbols_size / sym_size;
211 if (symcount * sym_size != sd.symbols_size)
213 fprintf(stderr,
214 _("%s: %s: size of symbols is not multiple of symbol size\n"),
215 program_name, this->name().c_str());
216 gold_exit(false);
219 this->symbols_ = new Symbol*[symcount];
221 const elfcpp::Sym<size, big_endian>* syms =
222 reinterpret_cast<const elfcpp::Sym<size, big_endian>*>(sd.symbols->data());
223 const char* sym_names =
224 reinterpret_cast<const char*>(sd.symbol_names->data());
225 symtab->add_from_object(this, syms, symcount, sym_names,
226 sd.symbol_names_size, this->symbols_);
229 } // End namespace gold.
231 namespace
234 using namespace gold;
236 // Read an ELF file with the header and return the appropriate
237 // instance of Object.
239 template<int size, bool big_endian>
240 Object*
241 make_elf_sized_object(const std::string& name, Input_file* input_file,
242 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
244 int et = ehdr.get_e_type();
245 if (et != elfcpp::ET_REL && et != elfcpp::ET_DYN)
247 fprintf(stderr, "%s: %s: unsupported ELF type %d\n",
248 program_name, name.c_str(), static_cast<int>(et));
249 gold_exit(false);
252 if (et == elfcpp::ET_REL)
254 Sized_object<size, big_endian>* obj =
255 new Sized_object<size, big_endian>(name, input_file, offset, ehdr);
256 obj->setup(ehdr);
257 return obj;
259 else
261 // elfcpp::ET_DYN
262 fprintf(stderr, _("%s: %s: dynamic objects are not yet supported\n"),
263 program_name, name.c_str());
264 gold_exit(false);
265 // Sized_dynobj<size, big_endian>* obj =
266 // new Sized_dynobj<size, big_endian>(this->input_.name(), input_file,
267 // offset, ehdr);
268 // obj->setup(ehdr);
269 // return obj;
273 } // End anonymous namespace.
275 namespace gold
278 // Read an ELF file and return the appropriate instance of Object.
280 Object*
281 make_elf_object(const std::string& name, Input_file* input_file, off_t offset,
282 const unsigned char* p, off_t bytes)
284 if (bytes < elfcpp::EI_NIDENT)
286 fprintf(stderr, _("%s: %s: ELF file too short\n"),
287 program_name, name.c_str());
288 gold_exit(false);
291 int v = p[elfcpp::EI_VERSION];
292 if (v != elfcpp::EV_CURRENT)
294 if (v == elfcpp::EV_NONE)
295 fprintf(stderr, _("%s: %s: invalid ELF version 0\n"),
296 program_name, name.c_str());
297 else
298 fprintf(stderr, _("%s: %s: unsupported ELF version %d\n"),
299 program_name, name.c_str(), v);
300 gold_exit(false);
303 int c = p[elfcpp::EI_CLASS];
304 if (c == elfcpp::ELFCLASSNONE)
306 fprintf(stderr, _("%s: %s: invalid ELF class 0\n"),
307 program_name, name.c_str());
308 gold_exit(false);
310 else if (c != elfcpp::ELFCLASS32
311 && c != elfcpp::ELFCLASS64)
313 fprintf(stderr, _("%s: %s: unsupported ELF class %d\n"),
314 program_name, name.c_str(), c);
315 gold_exit(false);
318 int d = p[elfcpp::EI_DATA];
319 if (d == elfcpp::ELFDATANONE)
321 fprintf(stderr, _("%s: %s: invalid ELF data encoding\n"),
322 program_name, name.c_str());
323 gold_exit(false);
325 else if (d != elfcpp::ELFDATA2LSB
326 && d != elfcpp::ELFDATA2MSB)
328 fprintf(stderr, _("%s: %s: unsupported ELF data encoding %d\n"),
329 program_name, name.c_str(), d);
330 gold_exit(false);
333 bool big_endian = d == elfcpp::ELFDATA2MSB;
335 if (c == elfcpp::ELFCLASS32)
337 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
339 fprintf(stderr, _("%s: %s: ELF file too short\n"),
340 program_name, name.c_str());
341 gold_exit(false);
343 if (big_endian)
345 elfcpp::Ehdr<32, true> ehdr(p);
346 return make_elf_sized_object<32, true>(name, input_file,
347 offset, ehdr);
349 else
351 elfcpp::Ehdr<32, false> ehdr(p);
352 return make_elf_sized_object<32, false>(name, input_file,
353 offset, ehdr);
356 else
358 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
360 fprintf(stderr, _("%s: %s: ELF file too short\n"),
361 program_name, name.c_str());
362 gold_exit(false);
364 if (big_endian)
366 elfcpp::Ehdr<64, true> ehdr(p);
367 return make_elf_sized_object<64, true>(name, input_file,
368 offset, ehdr);
370 else
372 elfcpp::Ehdr<64, false> ehdr(p);
373 return make_elf_sized_object<64, false>(name, input_file,
374 offset, ehdr);
379 // Instantiate the templates we need. We could use the configure
380 // script to restrict this to only the ones for implemented targets.
382 template
383 class Sized_object<32, false>;
385 template
386 class Sized_object<32, true>;
388 template
389 class Sized_object<64, false>;
391 template
392 class Sized_object<64, true>;
394 } // End namespace gold.