1 // object.cc -- support for an object file for linking in gold
10 #include "target-select.h"
18 Object::get_view(off_t start, off_t size)
20 return this->input_file_->file().get_view(start + this->offset_, size);
24 Object::read(off_t start, off_t size, void* p)
26 this->input_file_->file().read(start + this->offset_, size, p);
30 Object::get_lasting_view(off_t start, off_t size)
32 return this->input_file_->file().get_lasting_view(start + this->offset_,
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,
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()),
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);
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);
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>
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_);
88 fprintf(stderr, _("%s: %s: unsupported ELF machine number %d\n"),
89 program_name, this->name().c_str(), this->machine_);
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)
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);
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;
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;
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>
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;
140 ret.symbols_size = 0;
141 ret.symbol_names = NULL;
142 ret.symbol_names_size = 0;
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);
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)
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()));
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();
196 // Add the symbols to the symbol table.
198 template<int size, bool big_endian>
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);
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)
214 _("%s: %s: size of symbols is not multiple of symbol size\n"),
215 program_name, this->name().c_str());
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.
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>
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));
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);
262 fprintf(stderr, _("%s: %s: dynamic objects are not yet supported\n"),
263 program_name, name.c_str());
265 // Sized_dynobj<size, big_endian>* obj =
266 // new Sized_dynobj<size, big_endian>(this->input_.name(), input_file,
273 } // End anonymous namespace.
278 // Read an ELF file and return the appropriate instance of 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());
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());
298 fprintf(stderr, _("%s: %s: unsupported ELF version %d\n"),
299 program_name, name.c_str(), v);
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());
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);
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());
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);
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());
345 elfcpp::Ehdr<32, true> ehdr(p);
346 return make_elf_sized_object<32, true>(name, input_file,
351 elfcpp::Ehdr<32, false> ehdr(p);
352 return make_elf_sized_object<32, false>(name, input_file,
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());
366 elfcpp::Ehdr<64, true> ehdr(p);
367 return make_elf_sized_object<64, true>(name, input_file,
372 elfcpp::Ehdr<64, false> ehdr(p);
373 return make_elf_sized_object<64, false>(name, input_file,
379 // Instantiate the templates we need. We could use the configure
380 // script to restrict this to only the ones for implemented targets.
383 class Sized_object<32, false>;
386 class Sized_object<32, true>;
389 class Sized_object<64, false>;
392 class Sized_object<64, true>;
394 } // End namespace gold.