1 // elfcpp_file.h -- file access for elfcpp -*- C++ -*-
3 // Copyright 2006, 2007, Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of elfcpp.
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Library General Public License
10 // as published by the Free Software Foundation; either version 2, or
11 // (at your option) any later version.
13 // In addition to the permissions in the GNU Library General Public
14 // License, the Free Software Foundation gives you unlimited
15 // permission to link the compiled version of this file into
16 // combinations with other programs, and to distribute those
17 // combinations without any restriction coming from the use of this
18 // file. (The Library Public License restrictions do apply in other
19 // respects; for example, they cover modification of the file, and
20 /// distribution when not linked into a combined executable.)
22 // This program is distributed in the hope that it will be useful, but
23 // WITHOUT ANY WARRANTY; without even the implied warranty of
24 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 // Library General Public License for more details.
27 // You should have received a copy of the GNU Library General Public
28 // License along with this program; if not, write to the Free Software
29 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
32 // This header file defines the class Elf_file which can be used to
33 // read useful data from an ELF file. The functions here are all
34 // templates which take a file interface object as a parameter. This
35 // type must have a subtype View. This type must support two methods:
36 // View view(off_t file_offset, off_t data_size)
37 // returns a View for the specified part of the file.
38 // void error(const char* printf_format, ...)
39 // prints an error message and does not return. The subtype View must
41 // const unsigned char* data()
42 // which returns a pointer to a buffer containing the requested data.
43 // This general interface is used to read data from the file. Objects
44 // of type View will never survive longer than the elfcpp function.
46 // Some of these functions must return a reference to part of the
47 // file. To use these, the file interface must support a subtype
49 // Location(off_t file_offset, off_t data_size)
50 // To use this in conjunction with the accessors types Shdr, etc., the
51 // file interface should support an overload of view:
52 // View view(Location)
53 // This permits writing
54 // elfcpp::Shdr shdr(file, ef.section_header(n));
66 // A simple helper class to recognize if a file has an ELF header.
71 // Maximum header size. The user should try to read this much of
72 // the file when using this class.
74 static const int max_header_size = Elf_sizes<64>::ehdr_size;
76 // Checks if the file contains the ELF magic. Other header fields
80 is_elf_file(const unsigned char* ehdr_buf, int size);
82 // Check if EHDR_BUF/BUFSIZE is a valid header of a 32-bit or
83 // 64-bit, little-endian or big-endian ELF file. Assumes
84 // is_elf_file() has been checked to be true. If the header is not
85 // valid, *ERROR contains a human-readable error message. If is is,
86 // *SIZE is set to either 32 or 64, *BIG_ENDIAN is set to indicate
87 // whether the file is big-endian.
90 is_valid_header(const unsigned char* ehdr_buf, off_t bufsize,
91 int* size, bool* big_endian,
95 // This object is used to read an ELF file.
96 // SIZE: The size of file, 32 or 64.
97 // BIG_ENDIAN: Whether the file is in big-endian format.
98 // FILE: A file reading type as described above.
100 template<int size, bool big_endian, typename File>
104 typedef Elf_file<size, big_endian, File> This;
107 static const int ehdr_size = Elf_sizes<size>::ehdr_size;
108 static const int phdr_size = Elf_sizes<size>::phdr_size;
109 static const int shdr_size = Elf_sizes<size>::shdr_size;
110 static const int sym_size = Elf_sizes<size>::sym_size;
111 static const int rel_size = Elf_sizes<size>::rel_size;
112 static const int rela_size = Elf_sizes<size>::rela_size;
114 typedef Ehdr<size, big_endian> Ef_ehdr;
115 typedef Phdr<size, big_endian> Ef_phdr;
116 typedef Shdr<size, big_endian> Ef_shdr;
117 typedef Sym<size, big_endian> Ef_sym;
119 // Construct an Elf_file given an ELF file header.
120 Elf_file(File* file, const Ef_ehdr& ehdr)
121 { this->construct(file, ehdr); }
123 // Construct an ELF file.
125 Elf_file(File* file);
127 // Return the file offset to the section headers.
130 { return this->shoff_; }
132 // Find the first section with an sh_type field equal to TYPE and
133 // return its index. Returns SHN_UNDEF if there is no such section.
135 find_section_by_type(unsigned int type);
137 // Return the number of sections.
141 this->initialize_shnum();
145 // Return the section index of the section name string table.
149 this->initialize_shnum();
150 return this->shstrndx_;
153 // Return the value to subtract from section indexes >=
154 // SHN_LORESERVE. See the comment in initialize_shnum.
158 this->initialize_shnum();
159 return this->large_shndx_offset_;
162 // Return the location of the header of section SHNDX.
163 typename File::Location
164 section_header(unsigned int shndx)
166 return typename File::Location(this->section_header_offset(shndx),
170 // Return the name of section SHNDX.
172 section_name(unsigned int shndx);
174 // Return the location of the contents of section SHNDX.
175 typename File::Location
176 section_contents(unsigned int shndx);
178 // Return the size of section SHNDX.
179 typename Elf_types<size>::Elf_WXword
180 section_size(unsigned int shndx);
182 // Return the flags of section SHNDX.
183 typename Elf_types<size>::Elf_WXword
184 section_flags(unsigned int shndx);
186 // Return the address of section SHNDX.
187 typename Elf_types<size>::Elf_Addr
188 section_addr(unsigned int shndx);
190 // Return the type of section SHNDX.
192 section_type(unsigned int shndx);
194 // Return the link field of section SHNDX.
196 section_link(unsigned int shndx);
198 // Return the info field of section SHNDX.
200 section_info(unsigned int shndx);
202 // Return the addralign field of section SHNDX.
203 typename Elf_types<size>::Elf_WXword
204 section_addralign(unsigned int shndx);
207 // Shared constructor code.
209 construct(File* file, const Ef_ehdr& ehdr);
211 // Initialize shnum_ and shstrndx_.
215 // Return the file offset of the header of section SHNDX.
217 section_header_offset(unsigned int shndx);
219 // The file we are reading.
221 // The file offset to the section headers.
223 // The number of sections.
225 // The section index of the section name string table.
226 unsigned int shstrndx_;
227 // Offset to add to sections larger than SHN_LORESERVE.
228 int large_shndx_offset_;
231 // Inline function definitions.
233 // Check for presence of the ELF magic number.
236 Elf_recognizer::is_elf_file(const unsigned char* ehdr_buf, int size)
241 static unsigned char elfmagic[4] =
243 elfcpp::ELFMAG0, elfcpp::ELFMAG1,
244 elfcpp::ELFMAG2, elfcpp::ELFMAG3
246 return memcmp(ehdr_buf, elfmagic, 4) == 0;
252 // Print a number to a string.
255 internal_printf_int(const char* format, int arg)
258 snprintf(buf, sizeof(buf), format, arg);
259 return std::string(buf);
262 } // End anonymous namespace.
264 // Check the validity of the ELF header.
267 Elf_recognizer::is_valid_header(
268 const unsigned char* ehdr_buf,
274 if (bufsize < elfcpp::EI_NIDENT)
276 *error = _("ELF file too short");
280 int v = ehdr_buf[elfcpp::EI_VERSION];
281 if (v != elfcpp::EV_CURRENT)
283 if (v == elfcpp::EV_NONE)
284 *error = _("invalid ELF version 0");
286 *error = internal_printf_int(_("unsupported ELF version %d"), v);
290 int c = ehdr_buf[elfcpp::EI_CLASS];
291 if (c == elfcpp::ELFCLASSNONE)
293 *error = _("invalid ELF class 0");
296 else if (c != elfcpp::ELFCLASS32
297 && c != elfcpp::ELFCLASS64)
299 *error = internal_printf_int(_("unsupported ELF class %d"), c);
303 int d = ehdr_buf[elfcpp::EI_DATA];
304 if (d == elfcpp::ELFDATANONE)
306 *error = _("invalid ELF data encoding");
309 else if (d != elfcpp::ELFDATA2LSB
310 && d != elfcpp::ELFDATA2MSB)
312 *error = internal_printf_int(_("unsupported ELF data encoding %d"), d);
316 *big_endian = (d == elfcpp::ELFDATA2MSB);
318 if (c == elfcpp::ELFCLASS32)
320 if (bufsize < elfcpp::Elf_sizes<32>::ehdr_size)
322 *error = _("ELF file too short");
329 if (bufsize < elfcpp::Elf_sizes<64>::ehdr_size)
331 *error = _("ELF file too short");
340 // Template function definitions.
342 // Construct an Elf_file given an ELF file header.
344 template<int size, bool big_endian, typename File>
346 Elf_file<size, big_endian, File>::construct(File* file, const Ef_ehdr& ehdr)
349 this->shoff_ = ehdr.get_e_shoff();
350 this->shnum_ = ehdr.get_e_shnum();
351 this->shstrndx_ = ehdr.get_e_shstrndx();
352 this->large_shndx_offset_ = 0;
353 if (ehdr.get_e_ehsize() != This::ehdr_size)
354 file->error(_("bad e_ehsize (%d != %d)"),
355 ehdr.get_e_ehsize(), This::ehdr_size);
356 if (ehdr.get_e_shentsize() != This::shdr_size)
357 file->error(_("bad e_shentsize (%d != %d)"),
358 ehdr.get_e_shentsize(), This::shdr_size);
361 // Construct an ELF file.
363 template<int size, bool big_endian, typename File>
365 Elf_file<size, big_endian, File>::Elf_file(File* file)
367 typename File::View v(file->view(file_header_offset, This::ehdr_size));
368 this->construct(file, Ef_ehdr(v.data()));
371 // Initialize the shnum_ and shstrndx_ fields, handling overflow.
373 template<int size, bool big_endian, typename File>
375 Elf_file<size, big_endian, File>::initialize_shnum()
377 if ((this->shnum_ == 0 || this->shstrndx_ == SHN_XINDEX)
378 && this->shoff_ != 0)
380 typename File::View v(this->file_->view(this->shoff_, This::shdr_size));
381 Ef_shdr shdr(v.data());
383 if (this->shnum_ == 0)
384 this->shnum_ = shdr.get_sh_size();
386 if (this->shstrndx_ == SHN_XINDEX)
388 this->shstrndx_ = shdr.get_sh_link();
390 // Versions of the GNU binutils between 2.12 and 2.18 did
391 // not handle objects with more than SHN_LORESERVE sections
392 // correctly. All large section indexes were offset by
393 // 0x100. Some information can be found here:
394 // http://sourceware.org/bugzilla/show_bug.cgi?id=5900 .
395 // Fortunately these object files are easy to detect, as the
396 // GNU binutils always put the section header string table
397 // near the end of the list of sections. Thus if the
398 // section header string table index is larger than the
399 // number of sections, then we know we have to subtract
400 // 0x100 to get the real section index.
401 if (this->shstrndx_ >= this->shnum_)
403 if (this->shstrndx_ >= elfcpp::SHN_LORESERVE + 0x100)
405 this->large_shndx_offset_ = - 0x100;
406 this->shstrndx_ -= 0x100;
408 if (this->shstrndx_ >= this->shnum_)
409 this->file_->error(_("bad shstrndx: %u >= %u"),
410 this->shstrndx_, this->shnum_);
416 // Find section with sh_type equal to TYPE and return its index.
417 // Returns SHN_UNDEF if not found.
419 template<int size, bool big_endian, typename File>
421 Elf_file<size, big_endian, File>::find_section_by_type(unsigned int type)
423 unsigned int shnum = this->shnum();
424 typename File::View v(this->file_->view(this->shoff_,
425 This::shdr_size * shnum));
426 for (unsigned int i = 0; i < shnum; i++)
428 Ef_shdr shdr(v.data() + This::shdr_size * i);
429 if (shdr.get_sh_type() == type)
435 // Return the file offset of the section header of section SHNDX.
437 template<int size, bool big_endian, typename File>
439 Elf_file<size, big_endian, File>::section_header_offset(unsigned int shndx)
441 if (shndx >= this->shnum())
442 this->file_->error(_("section_header_offset: bad shndx %u >= %u"),
443 shndx, this->shnum());
444 return this->shoff_ + This::shdr_size * shndx;
447 // Return the name of section SHNDX.
449 template<int size, bool big_endian, typename File>
451 Elf_file<size, big_endian, File>::section_name(unsigned int shndx)
453 File* const file = this->file_;
455 // Get the section name offset.
456 unsigned int sh_name;
458 typename File::View v(file->view(this->section_header_offset(shndx),
460 Ef_shdr shdr(v.data());
461 sh_name = shdr.get_sh_name();
464 // Get the file offset for the section name string table data.
468 const unsigned int shstrndx = this->shstrndx_;
469 typename File::View v(file->view(this->section_header_offset(shstrndx),
471 Ef_shdr shstr_shdr(v.data());
472 shstr_off = shstr_shdr.get_sh_offset();
473 shstr_size = shstr_shdr.get_sh_size();
476 if (sh_name >= shstr_size)
477 file->error(_("bad section name offset for section %u: %u"),
480 typename File::View v(file->view(shstr_off, shstr_size));
482 const unsigned char* datau = v.data();
483 const char* data = reinterpret_cast<const char*>(datau);
484 const void* p = ::memchr(data + sh_name, '\0', shstr_size - sh_name);
486 file->error(_("missing null terminator for name of section %u"),
489 size_t len = static_cast<const char*>(p) - (data + sh_name);
491 return std::string(data + sh_name, len);
494 // Return the contents of section SHNDX.
496 template<int size, bool big_endian, typename File>
497 typename File::Location
498 Elf_file<size, big_endian, File>::section_contents(unsigned int shndx)
500 File* const file = this->file_;
502 if (shndx >= this->shnum())
503 file->error(_("section_contents: bad shndx %u >= %u"),
504 shndx, this->shnum());
506 typename File::View v(file->view(this->section_header_offset(shndx),
508 Ef_shdr shdr(v.data());
509 return typename File::Location(shdr.get_sh_offset(), shdr.get_sh_size());
512 // Get the size of section SHNDX.
514 template<int size, bool big_endian, typename File>
515 typename Elf_types<size>::Elf_WXword
516 Elf_file<size, big_endian, File>::section_size(unsigned int shndx)
518 File* const file = this->file_;
520 if (shndx >= this->shnum())
521 file->error(_("section_size: bad shndx %u >= %u"),
522 shndx, this->shnum());
524 typename File::View v(file->view(this->section_header_offset(shndx),
527 Ef_shdr shdr(v.data());
528 return shdr.get_sh_size();
531 // Return the section flags of section SHNDX.
533 template<int size, bool big_endian, typename File>
534 typename Elf_types<size>::Elf_WXword
535 Elf_file<size, big_endian, File>::section_flags(unsigned int shndx)
537 File* const file = this->file_;
539 if (shndx >= this->shnum())
540 file->error(_("section_flags: bad shndx %u >= %u"),
541 shndx, this->shnum());
543 typename File::View v(file->view(this->section_header_offset(shndx),
546 Ef_shdr shdr(v.data());
547 return shdr.get_sh_flags();
550 // Return the address of section SHNDX.
552 template<int size, bool big_endian, typename File>
553 typename Elf_types<size>::Elf_Addr
554 Elf_file<size, big_endian, File>::section_addr(unsigned int shndx)
556 File* const file = this->file_;
558 if (shndx >= this->shnum())
559 file->error(_("section_flags: bad shndx %u >= %u"),
560 shndx, this->shnum());
562 typename File::View v(file->view(this->section_header_offset(shndx),
565 Ef_shdr shdr(v.data());
566 return shdr.get_sh_addr();
569 // Return the type of section SHNDX.
571 template<int size, bool big_endian, typename File>
573 Elf_file<size, big_endian, File>::section_type(unsigned int shndx)
575 File* const file = this->file_;
577 if (shndx >= this->shnum())
578 file->error(_("section_type: bad shndx %u >= %u"),
579 shndx, this->shnum());
581 typename File::View v(file->view(this->section_header_offset(shndx),
584 Ef_shdr shdr(v.data());
585 return shdr.get_sh_type();
588 // Return the sh_link field of section SHNDX.
590 template<int size, bool big_endian, typename File>
592 Elf_file<size, big_endian, File>::section_link(unsigned int shndx)
594 File* const file = this->file_;
596 if (shndx >= this->shnum())
597 file->error(_("section_link: bad shndx %u >= %u"),
598 shndx, this->shnum());
600 typename File::View v(file->view(this->section_header_offset(shndx),
603 Ef_shdr shdr(v.data());
604 return shdr.get_sh_link();
607 // Return the sh_info field of section SHNDX.
609 template<int size, bool big_endian, typename File>
611 Elf_file<size, big_endian, File>::section_info(unsigned int shndx)
613 File* const file = this->file_;
615 if (shndx >= this->shnum())
616 file->error(_("section_info: bad shndx %u >= %u"),
617 shndx, this->shnum());
619 typename File::View v(file->view(this->section_header_offset(shndx),
622 Ef_shdr shdr(v.data());
623 return shdr.get_sh_info();
626 // Return the sh_addralign field of section SHNDX.
628 template<int size, bool big_endian, typename File>
629 typename Elf_types<size>::Elf_WXword
630 Elf_file<size, big_endian, File>::section_addralign(unsigned int shndx)
632 File* const file = this->file_;
634 if (shndx >= this->shnum())
635 file->error(_("section_addralign: bad shndx %u >= %u"),
636 shndx, this->shnum());
638 typename File::View v(file->view(this->section_header_offset(shndx),
641 Ef_shdr shdr(v.data());
642 return shdr.get_sh_addralign();
645 } // End namespace elfcpp.
647 #endif // !defined(ELFCPP_FILE_H)