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 // A small wrapper around SHT_STRTAB data mapped to memory. It checks that the
232 // index is not out of bounds and the string is NULL-terminated.
237 // Construct an Elf_strtab for a section with contents *P and size SIZE.
238 Elf_strtab(const unsigned char* p
, size_t size
);
240 // Return the file offset to the section headers.
242 get_c_string(size_t offset
, const char** cstring
) const
244 if (offset
>= this->usable_size_
)
246 *cstring
= this->base_
+ offset
;
251 // Contents of the section mapped to memory.
253 // One larger that the position of the last NULL character in the section.
254 // For valid SHT_STRTAB sections, this is the size of the section.
258 // Inline function definitions.
260 // Check for presence of the ELF magic number.
263 Elf_recognizer::is_elf_file(const unsigned char* ehdr_buf
, int size
)
268 static unsigned char elfmagic
[4] =
270 elfcpp::ELFMAG0
, elfcpp::ELFMAG1
,
271 elfcpp::ELFMAG2
, elfcpp::ELFMAG3
273 return memcmp(ehdr_buf
, elfmagic
, 4) == 0;
279 // Print a number to a string.
282 internal_printf_int(const char* format
, int arg
)
285 snprintf(buf
, sizeof(buf
), format
, arg
);
286 return std::string(buf
);
289 } // End anonymous namespace.
291 // Check the validity of the ELF header.
294 Elf_recognizer::is_valid_header(
295 const unsigned char* ehdr_buf
,
301 if (bufsize
< elfcpp::EI_NIDENT
)
303 *error
= _("ELF file too short");
307 int v
= ehdr_buf
[elfcpp::EI_VERSION
];
308 if (v
!= elfcpp::EV_CURRENT
)
310 if (v
== elfcpp::EV_NONE
)
311 *error
= _("invalid ELF version 0");
313 *error
= internal_printf_int(_("unsupported ELF version %d"), v
);
317 int c
= ehdr_buf
[elfcpp::EI_CLASS
];
318 if (c
== elfcpp::ELFCLASSNONE
)
320 *error
= _("invalid ELF class 0");
323 else if (c
!= elfcpp::ELFCLASS32
324 && c
!= elfcpp::ELFCLASS64
)
326 *error
= internal_printf_int(_("unsupported ELF class %d"), c
);
330 int d
= ehdr_buf
[elfcpp::EI_DATA
];
331 if (d
== elfcpp::ELFDATANONE
)
333 *error
= _("invalid ELF data encoding");
336 else if (d
!= elfcpp::ELFDATA2LSB
337 && d
!= elfcpp::ELFDATA2MSB
)
339 *error
= internal_printf_int(_("unsupported ELF data encoding %d"), d
);
343 *big_endian
= (d
== elfcpp::ELFDATA2MSB
);
345 if (c
== elfcpp::ELFCLASS32
)
347 if (bufsize
< elfcpp::Elf_sizes
<32>::ehdr_size
)
349 *error
= _("ELF file too short");
356 if (bufsize
< elfcpp::Elf_sizes
<64>::ehdr_size
)
358 *error
= _("ELF file too short");
367 // Template function definitions.
369 // Construct an Elf_file given an ELF file header.
371 template<int size
, bool big_endian
, typename File
>
373 Elf_file
<size
, big_endian
, File
>::construct(File
* file
, const Ef_ehdr
& ehdr
)
376 this->shoff_
= ehdr
.get_e_shoff();
377 this->shnum_
= ehdr
.get_e_shnum();
378 this->shstrndx_
= ehdr
.get_e_shstrndx();
379 this->large_shndx_offset_
= 0;
380 if (ehdr
.get_e_ehsize() != This::ehdr_size
)
381 file
->error(_("bad e_ehsize (%d != %d)"),
382 ehdr
.get_e_ehsize(), This::ehdr_size
);
383 if (ehdr
.get_e_shentsize() != This::shdr_size
)
384 file
->error(_("bad e_shentsize (%d != %d)"),
385 ehdr
.get_e_shentsize(), This::shdr_size
);
388 // Construct an ELF file.
390 template<int size
, bool big_endian
, typename File
>
392 Elf_file
<size
, big_endian
, File
>::Elf_file(File
* file
)
394 typename
File::View
v(file
->view(file_header_offset
, This::ehdr_size
));
395 this->construct(file
, Ef_ehdr(v
.data()));
398 // Initialize the shnum_ and shstrndx_ fields, handling overflow.
400 template<int size
, bool big_endian
, typename File
>
402 Elf_file
<size
, big_endian
, File
>::initialize_shnum()
404 if ((this->shnum_
== 0 || this->shstrndx_
== SHN_XINDEX
)
405 && this->shoff_
!= 0)
407 typename
File::View
v(this->file_
->view(this->shoff_
, This::shdr_size
));
408 Ef_shdr
shdr(v
.data());
410 if (this->shnum_
== 0)
411 this->shnum_
= shdr
.get_sh_size();
413 if (this->shstrndx_
== SHN_XINDEX
)
415 this->shstrndx_
= shdr
.get_sh_link();
417 // Versions of the GNU binutils between 2.12 and 2.18 did
418 // not handle objects with more than SHN_LORESERVE sections
419 // correctly. All large section indexes were offset by
420 // 0x100. Some information can be found here:
421 // http://sourceware.org/bugzilla/show_bug.cgi?id=5900 .
422 // Fortunately these object files are easy to detect, as the
423 // GNU binutils always put the section header string table
424 // near the end of the list of sections. Thus if the
425 // section header string table index is larger than the
426 // number of sections, then we know we have to subtract
427 // 0x100 to get the real section index.
428 if (this->shstrndx_
>= this->shnum_
)
430 if (this->shstrndx_
>= elfcpp::SHN_LORESERVE
+ 0x100)
432 this->large_shndx_offset_
= - 0x100;
433 this->shstrndx_
-= 0x100;
435 if (this->shstrndx_
>= this->shnum_
)
436 this->file_
->error(_("bad shstrndx: %u >= %u"),
437 this->shstrndx_
, this->shnum_
);
443 // Find section with sh_type equal to TYPE and return its index.
444 // Returns SHN_UNDEF if not found.
446 template<int size
, bool big_endian
, typename File
>
448 Elf_file
<size
, big_endian
, File
>::find_section_by_type(unsigned int type
)
450 unsigned int shnum
= this->shnum();
451 typename
File::View
v(this->file_
->view(this->shoff_
,
452 This::shdr_size
* shnum
));
453 for (unsigned int i
= 0; i
< shnum
; i
++)
455 Ef_shdr
shdr(v
.data() + This::shdr_size
* i
);
456 if (shdr
.get_sh_type() == type
)
462 // Return the file offset of the section header of section SHNDX.
464 template<int size
, bool big_endian
, typename File
>
466 Elf_file
<size
, big_endian
, File
>::section_header_offset(unsigned int shndx
)
468 if (shndx
>= this->shnum())
469 this->file_
->error(_("section_header_offset: bad shndx %u >= %u"),
470 shndx
, this->shnum());
471 return this->shoff_
+ This::shdr_size
* shndx
;
474 // Return the name of section SHNDX.
476 template<int size
, bool big_endian
, typename File
>
478 Elf_file
<size
, big_endian
, File
>::section_name(unsigned int shndx
)
480 File
* const file
= this->file_
;
482 // Get the section name offset.
483 unsigned int sh_name
;
485 typename
File::View
v(file
->view(this->section_header_offset(shndx
),
487 Ef_shdr
shdr(v
.data());
488 sh_name
= shdr
.get_sh_name();
491 // Get the file offset for the section name string table data.
495 const unsigned int shstrndx
= this->shstrndx_
;
496 typename
File::View
v(file
->view(this->section_header_offset(shstrndx
),
498 Ef_shdr
shstr_shdr(v
.data());
499 shstr_off
= shstr_shdr
.get_sh_offset();
500 shstr_size
= shstr_shdr
.get_sh_size();
503 if (sh_name
>= shstr_size
)
504 file
->error(_("bad section name offset for section %u: %u"),
507 typename
File::View
v(file
->view(shstr_off
, shstr_size
));
509 const unsigned char* datau
= v
.data();
510 const char* data
= reinterpret_cast<const char*>(datau
);
511 const void* p
= ::memchr(data
+ sh_name
, '\0', shstr_size
- sh_name
);
513 file
->error(_("missing null terminator for name of section %u"),
516 size_t len
= static_cast<const char*>(p
) - (data
+ sh_name
);
518 return std::string(data
+ sh_name
, len
);
521 // Return the contents of section SHNDX.
523 template<int size
, bool big_endian
, typename File
>
524 typename
File::Location
525 Elf_file
<size
, big_endian
, File
>::section_contents(unsigned int shndx
)
527 File
* const file
= this->file_
;
529 if (shndx
>= this->shnum())
530 file
->error(_("section_contents: bad shndx %u >= %u"),
531 shndx
, this->shnum());
533 typename
File::View
v(file
->view(this->section_header_offset(shndx
),
535 Ef_shdr
shdr(v
.data());
536 return typename
File::Location(shdr
.get_sh_offset(), shdr
.get_sh_size());
539 // Get the size of section SHNDX.
541 template<int size
, bool big_endian
, typename File
>
542 typename Elf_types
<size
>::Elf_WXword
543 Elf_file
<size
, big_endian
, File
>::section_size(unsigned int shndx
)
545 File
* const file
= this->file_
;
547 if (shndx
>= this->shnum())
548 file
->error(_("section_size: bad shndx %u >= %u"),
549 shndx
, this->shnum());
551 typename
File::View
v(file
->view(this->section_header_offset(shndx
),
554 Ef_shdr
shdr(v
.data());
555 return shdr
.get_sh_size();
558 // Return the section flags of section SHNDX.
560 template<int size
, bool big_endian
, typename File
>
561 typename Elf_types
<size
>::Elf_WXword
562 Elf_file
<size
, big_endian
, File
>::section_flags(unsigned int shndx
)
564 File
* const file
= this->file_
;
566 if (shndx
>= this->shnum())
567 file
->error(_("section_flags: bad shndx %u >= %u"),
568 shndx
, this->shnum());
570 typename
File::View
v(file
->view(this->section_header_offset(shndx
),
573 Ef_shdr
shdr(v
.data());
574 return shdr
.get_sh_flags();
577 // Return the address of section SHNDX.
579 template<int size
, bool big_endian
, typename File
>
580 typename Elf_types
<size
>::Elf_Addr
581 Elf_file
<size
, big_endian
, File
>::section_addr(unsigned int shndx
)
583 File
* const file
= this->file_
;
585 if (shndx
>= this->shnum())
586 file
->error(_("section_flags: bad shndx %u >= %u"),
587 shndx
, this->shnum());
589 typename
File::View
v(file
->view(this->section_header_offset(shndx
),
592 Ef_shdr
shdr(v
.data());
593 return shdr
.get_sh_addr();
596 // Return the type of section SHNDX.
598 template<int size
, bool big_endian
, typename File
>
600 Elf_file
<size
, big_endian
, File
>::section_type(unsigned int shndx
)
602 File
* const file
= this->file_
;
604 if (shndx
>= this->shnum())
605 file
->error(_("section_type: bad shndx %u >= %u"),
606 shndx
, this->shnum());
608 typename
File::View
v(file
->view(this->section_header_offset(shndx
),
611 Ef_shdr
shdr(v
.data());
612 return shdr
.get_sh_type();
615 // Return the sh_link field of section SHNDX.
617 template<int size
, bool big_endian
, typename File
>
619 Elf_file
<size
, big_endian
, File
>::section_link(unsigned int shndx
)
621 File
* const file
= this->file_
;
623 if (shndx
>= this->shnum())
624 file
->error(_("section_link: bad shndx %u >= %u"),
625 shndx
, this->shnum());
627 typename
File::View
v(file
->view(this->section_header_offset(shndx
),
630 Ef_shdr
shdr(v
.data());
631 return shdr
.get_sh_link();
634 // Return the sh_info field of section SHNDX.
636 template<int size
, bool big_endian
, typename File
>
638 Elf_file
<size
, big_endian
, File
>::section_info(unsigned int shndx
)
640 File
* const file
= this->file_
;
642 if (shndx
>= this->shnum())
643 file
->error(_("section_info: bad shndx %u >= %u"),
644 shndx
, this->shnum());
646 typename
File::View
v(file
->view(this->section_header_offset(shndx
),
649 Ef_shdr
shdr(v
.data());
650 return shdr
.get_sh_info();
653 // Return the sh_addralign field of section SHNDX.
655 template<int size
, bool big_endian
, typename File
>
656 typename Elf_types
<size
>::Elf_WXword
657 Elf_file
<size
, big_endian
, File
>::section_addralign(unsigned int shndx
)
659 File
* const file
= this->file_
;
661 if (shndx
>= this->shnum())
662 file
->error(_("section_addralign: bad shndx %u >= %u"),
663 shndx
, this->shnum());
665 typename
File::View
v(file
->view(this->section_header_offset(shndx
),
668 Ef_shdr
shdr(v
.data());
669 return shdr
.get_sh_addralign();
673 Elf_strtab::Elf_strtab(const unsigned char* p
, size_t size
)
675 // Check if the section is NUL-terminated. If it isn't, we ignore
676 // the last part to make sure we don't return non-NUL-terminated
678 while (size
> 0 && p
[size
- 1] != 0)
680 this->base_
= reinterpret_cast<const char*>(p
);
681 this->usable_size_
= size
;
684 } // End namespace elfcpp.
686 #endif // !defined(ELFCPP_FILE_H)