1*a9fa9459Szrj // elfcpp_file.h -- file access for elfcpp -*- C++ -*-
2*a9fa9459Szrj
3*a9fa9459Szrj // Copyright (C) 2006-2016 Free Software Foundation, Inc.
4*a9fa9459Szrj // Written by Ian Lance Taylor <iant@google.com>.
5*a9fa9459Szrj
6*a9fa9459Szrj // This file is part of elfcpp.
7*a9fa9459Szrj
8*a9fa9459Szrj // This program is free software; you can redistribute it and/or
9*a9fa9459Szrj // modify it under the terms of the GNU Library General Public License
10*a9fa9459Szrj // as published by the Free Software Foundation; either version 2, or
11*a9fa9459Szrj // (at your option) any later version.
12*a9fa9459Szrj
13*a9fa9459Szrj // In addition to the permissions in the GNU Library General Public
14*a9fa9459Szrj // License, the Free Software Foundation gives you unlimited
15*a9fa9459Szrj // permission to link the compiled version of this file into
16*a9fa9459Szrj // combinations with other programs, and to distribute those
17*a9fa9459Szrj // combinations without any restriction coming from the use of this
18*a9fa9459Szrj // file. (The Library Public License restrictions do apply in other
19*a9fa9459Szrj // respects; for example, they cover modification of the file, and
20*a9fa9459Szrj /// distribution when not linked into a combined executable.)
21*a9fa9459Szrj
22*a9fa9459Szrj // This program is distributed in the hope that it will be useful, but
23*a9fa9459Szrj // WITHOUT ANY WARRANTY; without even the implied warranty of
24*a9fa9459Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25*a9fa9459Szrj // Library General Public License for more details.
26*a9fa9459Szrj
27*a9fa9459Szrj // You should have received a copy of the GNU Library General Public
28*a9fa9459Szrj // License along with this program; if not, write to the Free Software
29*a9fa9459Szrj // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
30*a9fa9459Szrj // 02110-1301, USA.
31*a9fa9459Szrj
32*a9fa9459Szrj // This header file defines the class Elf_file which can be used to
33*a9fa9459Szrj // read useful data from an ELF file. The functions here are all
34*a9fa9459Szrj // templates which take a file interface object as a parameter. This
35*a9fa9459Szrj // type must have a subtype View. This type must support two methods:
36*a9fa9459Szrj // View view(off_t file_offset, off_t data_size)
37*a9fa9459Szrj // returns a View for the specified part of the file.
38*a9fa9459Szrj // void error(const char* printf_format, ...)
39*a9fa9459Szrj // prints an error message and does not return. The subtype View must
40*a9fa9459Szrj // support a method
41*a9fa9459Szrj // const unsigned char* data()
42*a9fa9459Szrj // which returns a pointer to a buffer containing the requested data.
43*a9fa9459Szrj // This general interface is used to read data from the file. Objects
44*a9fa9459Szrj // of type View will never survive longer than the elfcpp function.
45*a9fa9459Szrj
46*a9fa9459Szrj // Some of these functions must return a reference to part of the
47*a9fa9459Szrj // file. To use these, the file interface must support a subtype
48*a9fa9459Szrj // Location:
49*a9fa9459Szrj // Location(off_t file_offset, off_t data_size)
50*a9fa9459Szrj // To use this in conjunction with the accessors types Shdr, etc., the
51*a9fa9459Szrj // file interface should support an overload of view:
52*a9fa9459Szrj // View view(Location)
53*a9fa9459Szrj // This permits writing
54*a9fa9459Szrj // elfcpp::Shdr shdr(file, ef.section_header(n));
55*a9fa9459Szrj
56*a9fa9459Szrj #ifndef ELFCPP_FILE_H
57*a9fa9459Szrj #define ELFCPP_FILE_H
58*a9fa9459Szrj
59*a9fa9459Szrj #include <string>
60*a9fa9459Szrj #include <cstdio>
61*a9fa9459Szrj #include <cstring>
62*a9fa9459Szrj
63*a9fa9459Szrj #include "elfcpp.h"
64*a9fa9459Szrj
65*a9fa9459Szrj namespace elfcpp
66*a9fa9459Szrj {
67*a9fa9459Szrj
68*a9fa9459Szrj // A simple helper class to recognize if a file has an ELF header.
69*a9fa9459Szrj
70*a9fa9459Szrj class Elf_recognizer
71*a9fa9459Szrj {
72*a9fa9459Szrj public:
73*a9fa9459Szrj // Maximum header size. The user should try to read this much of
74*a9fa9459Szrj // the file when using this class.
75*a9fa9459Szrj
76*a9fa9459Szrj static const int max_header_size = Elf_sizes<64>::ehdr_size;
77*a9fa9459Szrj
78*a9fa9459Szrj // Checks if the file contains the ELF magic. Other header fields
79*a9fa9459Szrj // are not checked.
80*a9fa9459Szrj
81*a9fa9459Szrj static bool
82*a9fa9459Szrj is_elf_file(const unsigned char* ehdr_buf, int size);
83*a9fa9459Szrj
84*a9fa9459Szrj // Check if EHDR_BUF/BUFSIZE is a valid header of a 32-bit or
85*a9fa9459Szrj // 64-bit, little-endian or big-endian ELF file. Assumes
86*a9fa9459Szrj // is_elf_file() has been checked to be true. If the header is not
87*a9fa9459Szrj // valid, *ERROR contains a human-readable error message. If is is,
88*a9fa9459Szrj // *SIZE is set to either 32 or 64, *BIG_ENDIAN is set to indicate
89*a9fa9459Szrj // whether the file is big-endian.
90*a9fa9459Szrj
91*a9fa9459Szrj static bool
92*a9fa9459Szrj is_valid_header(const unsigned char* ehdr_buf, off_t bufsize,
93*a9fa9459Szrj int* size, bool* big_endian,
94*a9fa9459Szrj std::string* error);
95*a9fa9459Szrj };
96*a9fa9459Szrj
97*a9fa9459Szrj // This object is used to read an ELF file.
98*a9fa9459Szrj // SIZE: The size of file, 32 or 64.
99*a9fa9459Szrj // BIG_ENDIAN: Whether the file is in big-endian format.
100*a9fa9459Szrj // FILE: A file reading type as described above.
101*a9fa9459Szrj
102*a9fa9459Szrj template<int size, bool big_endian, typename File>
103*a9fa9459Szrj class Elf_file
104*a9fa9459Szrj {
105*a9fa9459Szrj private:
106*a9fa9459Szrj typedef Elf_file<size, big_endian, File> This;
107*a9fa9459Szrj
108*a9fa9459Szrj public:
109*a9fa9459Szrj static const int ehdr_size = Elf_sizes<size>::ehdr_size;
110*a9fa9459Szrj static const int phdr_size = Elf_sizes<size>::phdr_size;
111*a9fa9459Szrj static const int shdr_size = Elf_sizes<size>::shdr_size;
112*a9fa9459Szrj static const int sym_size = Elf_sizes<size>::sym_size;
113*a9fa9459Szrj static const int rel_size = Elf_sizes<size>::rel_size;
114*a9fa9459Szrj static const int rela_size = Elf_sizes<size>::rela_size;
115*a9fa9459Szrj
116*a9fa9459Szrj typedef Ehdr<size, big_endian> Ef_ehdr;
117*a9fa9459Szrj typedef Phdr<size, big_endian> Ef_phdr;
118*a9fa9459Szrj typedef Shdr<size, big_endian> Ef_shdr;
119*a9fa9459Szrj typedef Sym<size, big_endian> Ef_sym;
120*a9fa9459Szrj
121*a9fa9459Szrj // Construct an Elf_file given an ELF file header.
Elf_file(File * file,const Ef_ehdr & ehdr)122*a9fa9459Szrj Elf_file(File* file, const Ef_ehdr& ehdr)
123*a9fa9459Szrj { this->construct(file, ehdr); }
124*a9fa9459Szrj
125*a9fa9459Szrj // Construct an ELF file.
126*a9fa9459Szrj inline
127*a9fa9459Szrj Elf_file(File* file);
128*a9fa9459Szrj
129*a9fa9459Szrj // Return the file offset to the section headers.
130*a9fa9459Szrj off_t
shoff()131*a9fa9459Szrj shoff() const
132*a9fa9459Szrj { return this->shoff_; }
133*a9fa9459Szrj
134*a9fa9459Szrj // Find the first section with an sh_type field equal to TYPE and
135*a9fa9459Szrj // return its index. Returns SHN_UNDEF if there is no such section.
136*a9fa9459Szrj unsigned int
137*a9fa9459Szrj find_section_by_type(unsigned int type);
138*a9fa9459Szrj
139*a9fa9459Szrj // Return the number of sections.
140*a9fa9459Szrj unsigned int
shnum()141*a9fa9459Szrj shnum()
142*a9fa9459Szrj {
143*a9fa9459Szrj this->initialize_shnum();
144*a9fa9459Szrj return this->shnum_;
145*a9fa9459Szrj }
146*a9fa9459Szrj
147*a9fa9459Szrj unsigned int
shnum()148*a9fa9459Szrj shnum() const
149*a9fa9459Szrj {
150*a9fa9459Szrj if (this->shnum_ == 0 && this->shoff_ != 0)
151*a9fa9459Szrj this->file_->error(_("ELF file has not been initialized yet"
152*a9fa9459Szrj " (internal error)"));
153*a9fa9459Szrj return this->shnum_;
154*a9fa9459Szrj }
155*a9fa9459Szrj
156*a9fa9459Szrj // Return the section index of the section name string table.
157*a9fa9459Szrj unsigned int
shstrndx()158*a9fa9459Szrj shstrndx()
159*a9fa9459Szrj {
160*a9fa9459Szrj this->initialize_shnum();
161*a9fa9459Szrj return this->shstrndx_;
162*a9fa9459Szrj }
163*a9fa9459Szrj
164*a9fa9459Szrj unsigned int
shstrndx()165*a9fa9459Szrj shstrndx() const
166*a9fa9459Szrj {
167*a9fa9459Szrj if (this->shstrndx_ == SHN_XINDEX && this->shoff_ != 0)
168*a9fa9459Szrj {
169*a9fa9459Szrj this->file_->error(_("ELF file has not been initialized yet"
170*a9fa9459Szrj " (internal error)"));
171*a9fa9459Szrj return 0;
172*a9fa9459Szrj }
173*a9fa9459Szrj return this->shstrndx_;
174*a9fa9459Szrj }
175*a9fa9459Szrj
176*a9fa9459Szrj // Return the value to subtract from section indexes >=
177*a9fa9459Szrj // SHN_LORESERVE. See the comment in initialize_shnum.
178*a9fa9459Szrj int
large_shndx_offset()179*a9fa9459Szrj large_shndx_offset()
180*a9fa9459Szrj {
181*a9fa9459Szrj this->initialize_shnum();
182*a9fa9459Szrj return this->large_shndx_offset_;
183*a9fa9459Szrj }
184*a9fa9459Szrj
185*a9fa9459Szrj int
large_shndx_offset()186*a9fa9459Szrj large_shndx_offset() const
187*a9fa9459Szrj {
188*a9fa9459Szrj if (this->shstrndx_ == SHN_XINDEX && this->shoff_ != 0)
189*a9fa9459Szrj this->file_->error(_("ELF file has not been initialized yet"
190*a9fa9459Szrj " (internal error)"));
191*a9fa9459Szrj return this->large_shndx_offset_;
192*a9fa9459Szrj }
193*a9fa9459Szrj
194*a9fa9459Szrj // Return the location of the header of section SHNDX.
195*a9fa9459Szrj typename File::Location
section_header(unsigned int shndx)196*a9fa9459Szrj section_header(unsigned int shndx)
197*a9fa9459Szrj {
198*a9fa9459Szrj return typename File::Location(this->section_header_offset(shndx),
199*a9fa9459Szrj shdr_size);
200*a9fa9459Szrj }
201*a9fa9459Szrj
202*a9fa9459Szrj // Return the name of section SHNDX.
203*a9fa9459Szrj std::string
204*a9fa9459Szrj section_name(unsigned int shndx) const;
205*a9fa9459Szrj
206*a9fa9459Szrj // Return the location of the contents of section SHNDX.
207*a9fa9459Szrj typename File::Location
208*a9fa9459Szrj section_contents(unsigned int shndx);
209*a9fa9459Szrj
210*a9fa9459Szrj // Return the size of section SHNDX.
211*a9fa9459Szrj typename Elf_types<size>::Elf_WXword
212*a9fa9459Szrj section_size(unsigned int shndx);
213*a9fa9459Szrj
214*a9fa9459Szrj // Return the flags of section SHNDX.
215*a9fa9459Szrj typename Elf_types<size>::Elf_WXword
216*a9fa9459Szrj section_flags(unsigned int shndx);
217*a9fa9459Szrj
218*a9fa9459Szrj // Return the address of section SHNDX.
219*a9fa9459Szrj typename Elf_types<size>::Elf_Addr
220*a9fa9459Szrj section_addr(unsigned int shndx);
221*a9fa9459Szrj
222*a9fa9459Szrj // Return the type of section SHNDX.
223*a9fa9459Szrj Elf_Word
224*a9fa9459Szrj section_type(unsigned int shndx);
225*a9fa9459Szrj
226*a9fa9459Szrj // Return the link field of section SHNDX.
227*a9fa9459Szrj Elf_Word
228*a9fa9459Szrj section_link(unsigned int shndx);
229*a9fa9459Szrj
230*a9fa9459Szrj // Return the info field of section SHNDX.
231*a9fa9459Szrj Elf_Word
232*a9fa9459Szrj section_info(unsigned int shndx);
233*a9fa9459Szrj
234*a9fa9459Szrj // Return the addralign field of section SHNDX.
235*a9fa9459Szrj typename Elf_types<size>::Elf_WXword
236*a9fa9459Szrj section_addralign(unsigned int shndx);
237*a9fa9459Szrj
238*a9fa9459Szrj private:
239*a9fa9459Szrj // Shared constructor code.
240*a9fa9459Szrj void
241*a9fa9459Szrj construct(File* file, const Ef_ehdr& ehdr);
242*a9fa9459Szrj
243*a9fa9459Szrj // Initialize shnum_ and shstrndx_.
244*a9fa9459Szrj void
245*a9fa9459Szrj initialize_shnum();
246*a9fa9459Szrj
247*a9fa9459Szrj // Return the file offset of the header of section SHNDX.
248*a9fa9459Szrj off_t
249*a9fa9459Szrj section_header_offset(unsigned int shndx) const;
250*a9fa9459Szrj
251*a9fa9459Szrj // The file we are reading.
252*a9fa9459Szrj File* file_;
253*a9fa9459Szrj // The file offset to the section headers.
254*a9fa9459Szrj off_t shoff_;
255*a9fa9459Szrj // The number of sections.
256*a9fa9459Szrj unsigned int shnum_;
257*a9fa9459Szrj // The section index of the section name string table.
258*a9fa9459Szrj unsigned int shstrndx_;
259*a9fa9459Szrj // Offset to add to sections larger than SHN_LORESERVE.
260*a9fa9459Szrj int large_shndx_offset_;
261*a9fa9459Szrj };
262*a9fa9459Szrj
263*a9fa9459Szrj // A small wrapper around SHT_STRTAB data mapped to memory. It checks that the
264*a9fa9459Szrj // index is not out of bounds and the string is NULL-terminated.
265*a9fa9459Szrj
266*a9fa9459Szrj class Elf_strtab
267*a9fa9459Szrj {
268*a9fa9459Szrj public:
269*a9fa9459Szrj // Construct an Elf_strtab for a section with contents *P and size SIZE.
270*a9fa9459Szrj Elf_strtab(const unsigned char* p, size_t size);
271*a9fa9459Szrj
272*a9fa9459Szrj // Return the file offset to the section headers.
273*a9fa9459Szrj bool
get_c_string(size_t offset,const char ** cstring)274*a9fa9459Szrj get_c_string(size_t offset, const char** cstring) const
275*a9fa9459Szrj {
276*a9fa9459Szrj if (offset >= this->usable_size_)
277*a9fa9459Szrj return false;
278*a9fa9459Szrj *cstring = this->base_ + offset;
279*a9fa9459Szrj return true;
280*a9fa9459Szrj }
281*a9fa9459Szrj
282*a9fa9459Szrj private:
283*a9fa9459Szrj // Contents of the section mapped to memory.
284*a9fa9459Szrj const char* base_;
285*a9fa9459Szrj // One larger that the position of the last NULL character in the section.
286*a9fa9459Szrj // For valid SHT_STRTAB sections, this is the size of the section.
287*a9fa9459Szrj size_t usable_size_;
288*a9fa9459Szrj };
289*a9fa9459Szrj
290*a9fa9459Szrj // Inline function definitions.
291*a9fa9459Szrj
292*a9fa9459Szrj // Check for presence of the ELF magic number.
293*a9fa9459Szrj
294*a9fa9459Szrj inline bool
is_elf_file(const unsigned char * ehdr_buf,int size)295*a9fa9459Szrj Elf_recognizer::is_elf_file(const unsigned char* ehdr_buf, int size)
296*a9fa9459Szrj {
297*a9fa9459Szrj if (size < 4)
298*a9fa9459Szrj return false;
299*a9fa9459Szrj
300*a9fa9459Szrj static unsigned char elfmagic[4] =
301*a9fa9459Szrj {
302*a9fa9459Szrj elfcpp::ELFMAG0, elfcpp::ELFMAG1,
303*a9fa9459Szrj elfcpp::ELFMAG2, elfcpp::ELFMAG3
304*a9fa9459Szrj };
305*a9fa9459Szrj return memcmp(ehdr_buf, elfmagic, 4) == 0;
306*a9fa9459Szrj }
307*a9fa9459Szrj
308*a9fa9459Szrj namespace
309*a9fa9459Szrj {
310*a9fa9459Szrj
311*a9fa9459Szrj // Print a number to a string.
312*a9fa9459Szrj
313*a9fa9459Szrj inline std::string
internal_printf_int(const char * format,int arg)314*a9fa9459Szrj internal_printf_int(const char* format, int arg)
315*a9fa9459Szrj {
316*a9fa9459Szrj char buf[256];
317*a9fa9459Szrj snprintf(buf, sizeof(buf), format, arg);
318*a9fa9459Szrj return std::string(buf);
319*a9fa9459Szrj }
320*a9fa9459Szrj
321*a9fa9459Szrj } // End anonymous namespace.
322*a9fa9459Szrj
323*a9fa9459Szrj // Check the validity of the ELF header.
324*a9fa9459Szrj
325*a9fa9459Szrj inline bool
is_valid_header(const unsigned char * ehdr_buf,off_t bufsize,int * size,bool * big_endian,std::string * error)326*a9fa9459Szrj Elf_recognizer::is_valid_header(
327*a9fa9459Szrj const unsigned char* ehdr_buf,
328*a9fa9459Szrj off_t bufsize,
329*a9fa9459Szrj int* size,
330*a9fa9459Szrj bool* big_endian,
331*a9fa9459Szrj std::string* error)
332*a9fa9459Szrj {
333*a9fa9459Szrj if (bufsize < elfcpp::EI_NIDENT)
334*a9fa9459Szrj {
335*a9fa9459Szrj *error = _("ELF file too short");
336*a9fa9459Szrj return false;
337*a9fa9459Szrj }
338*a9fa9459Szrj
339*a9fa9459Szrj int v = ehdr_buf[elfcpp::EI_VERSION];
340*a9fa9459Szrj if (v != elfcpp::EV_CURRENT)
341*a9fa9459Szrj {
342*a9fa9459Szrj if (v == elfcpp::EV_NONE)
343*a9fa9459Szrj *error = _("invalid ELF version 0");
344*a9fa9459Szrj else
345*a9fa9459Szrj *error = internal_printf_int(_("unsupported ELF version %d"), v);
346*a9fa9459Szrj return false;
347*a9fa9459Szrj }
348*a9fa9459Szrj
349*a9fa9459Szrj int c = ehdr_buf[elfcpp::EI_CLASS];
350*a9fa9459Szrj if (c == elfcpp::ELFCLASSNONE)
351*a9fa9459Szrj {
352*a9fa9459Szrj *error = _("invalid ELF class 0");
353*a9fa9459Szrj return false;
354*a9fa9459Szrj }
355*a9fa9459Szrj else if (c != elfcpp::ELFCLASS32
356*a9fa9459Szrj && c != elfcpp::ELFCLASS64)
357*a9fa9459Szrj {
358*a9fa9459Szrj *error = internal_printf_int(_("unsupported ELF class %d"), c);
359*a9fa9459Szrj return false;
360*a9fa9459Szrj }
361*a9fa9459Szrj
362*a9fa9459Szrj int d = ehdr_buf[elfcpp::EI_DATA];
363*a9fa9459Szrj if (d == elfcpp::ELFDATANONE)
364*a9fa9459Szrj {
365*a9fa9459Szrj *error = _("invalid ELF data encoding");
366*a9fa9459Szrj return false;
367*a9fa9459Szrj }
368*a9fa9459Szrj else if (d != elfcpp::ELFDATA2LSB
369*a9fa9459Szrj && d != elfcpp::ELFDATA2MSB)
370*a9fa9459Szrj {
371*a9fa9459Szrj *error = internal_printf_int(_("unsupported ELF data encoding %d"), d);
372*a9fa9459Szrj return false;
373*a9fa9459Szrj }
374*a9fa9459Szrj
375*a9fa9459Szrj *big_endian = (d == elfcpp::ELFDATA2MSB);
376*a9fa9459Szrj
377*a9fa9459Szrj if (c == elfcpp::ELFCLASS32)
378*a9fa9459Szrj {
379*a9fa9459Szrj if (bufsize < elfcpp::Elf_sizes<32>::ehdr_size)
380*a9fa9459Szrj {
381*a9fa9459Szrj *error = _("ELF file too short");
382*a9fa9459Szrj return false;
383*a9fa9459Szrj }
384*a9fa9459Szrj *size = 32;
385*a9fa9459Szrj }
386*a9fa9459Szrj else
387*a9fa9459Szrj {
388*a9fa9459Szrj if (bufsize < elfcpp::Elf_sizes<64>::ehdr_size)
389*a9fa9459Szrj {
390*a9fa9459Szrj *error = _("ELF file too short");
391*a9fa9459Szrj return false;
392*a9fa9459Szrj }
393*a9fa9459Szrj *size = 64;
394*a9fa9459Szrj }
395*a9fa9459Szrj
396*a9fa9459Szrj return true;
397*a9fa9459Szrj }
398*a9fa9459Szrj
399*a9fa9459Szrj // Template function definitions.
400*a9fa9459Szrj
401*a9fa9459Szrj // Construct an Elf_file given an ELF file header.
402*a9fa9459Szrj
403*a9fa9459Szrj template<int size, bool big_endian, typename File>
404*a9fa9459Szrj void
construct(File * file,const Ef_ehdr & ehdr)405*a9fa9459Szrj Elf_file<size, big_endian, File>::construct(File* file, const Ef_ehdr& ehdr)
406*a9fa9459Szrj {
407*a9fa9459Szrj this->file_ = file;
408*a9fa9459Szrj this->shoff_ = ehdr.get_e_shoff();
409*a9fa9459Szrj this->shnum_ = ehdr.get_e_shnum();
410*a9fa9459Szrj this->shstrndx_ = ehdr.get_e_shstrndx();
411*a9fa9459Szrj this->large_shndx_offset_ = 0;
412*a9fa9459Szrj if (ehdr.get_e_ehsize() != This::ehdr_size)
413*a9fa9459Szrj file->error(_("bad e_ehsize (%d != %d)"),
414*a9fa9459Szrj ehdr.get_e_ehsize(), This::ehdr_size);
415*a9fa9459Szrj if (ehdr.get_e_shentsize() != This::shdr_size)
416*a9fa9459Szrj file->error(_("bad e_shentsize (%d != %d)"),
417*a9fa9459Szrj ehdr.get_e_shentsize(), This::shdr_size);
418*a9fa9459Szrj }
419*a9fa9459Szrj
420*a9fa9459Szrj // Construct an ELF file.
421*a9fa9459Szrj
422*a9fa9459Szrj template<int size, bool big_endian, typename File>
423*a9fa9459Szrj inline
Elf_file(File * file)424*a9fa9459Szrj Elf_file<size, big_endian, File>::Elf_file(File* file)
425*a9fa9459Szrj {
426*a9fa9459Szrj typename File::View v(file->view(file_header_offset, This::ehdr_size));
427*a9fa9459Szrj this->construct(file, Ef_ehdr(v.data()));
428*a9fa9459Szrj }
429*a9fa9459Szrj
430*a9fa9459Szrj // Initialize the shnum_ and shstrndx_ fields, handling overflow.
431*a9fa9459Szrj
432*a9fa9459Szrj template<int size, bool big_endian, typename File>
433*a9fa9459Szrj void
initialize_shnum()434*a9fa9459Szrj Elf_file<size, big_endian, File>::initialize_shnum()
435*a9fa9459Szrj {
436*a9fa9459Szrj if ((this->shnum_ == 0 || this->shstrndx_ == SHN_XINDEX)
437*a9fa9459Szrj && this->shoff_ != 0)
438*a9fa9459Szrj {
439*a9fa9459Szrj typename File::View v(this->file_->view(this->shoff_, This::shdr_size));
440*a9fa9459Szrj Ef_shdr shdr(v.data());
441*a9fa9459Szrj
442*a9fa9459Szrj if (this->shnum_ == 0)
443*a9fa9459Szrj this->shnum_ = shdr.get_sh_size();
444*a9fa9459Szrj
445*a9fa9459Szrj if (this->shstrndx_ == SHN_XINDEX)
446*a9fa9459Szrj {
447*a9fa9459Szrj this->shstrndx_ = shdr.get_sh_link();
448*a9fa9459Szrj
449*a9fa9459Szrj // Versions of the GNU binutils between 2.12 and 2.18 did
450*a9fa9459Szrj // not handle objects with more than SHN_LORESERVE sections
451*a9fa9459Szrj // correctly. All large section indexes were offset by
452*a9fa9459Szrj // 0x100. Some information can be found here:
453*a9fa9459Szrj // http://sourceware.org/bugzilla/show_bug.cgi?id=5900 .
454*a9fa9459Szrj // Fortunately these object files are easy to detect, as the
455*a9fa9459Szrj // GNU binutils always put the section header string table
456*a9fa9459Szrj // near the end of the list of sections. Thus if the
457*a9fa9459Szrj // section header string table index is larger than the
458*a9fa9459Szrj // number of sections, then we know we have to subtract
459*a9fa9459Szrj // 0x100 to get the real section index.
460*a9fa9459Szrj if (this->shstrndx_ >= this->shnum_)
461*a9fa9459Szrj {
462*a9fa9459Szrj if (this->shstrndx_ >= elfcpp::SHN_LORESERVE + 0x100)
463*a9fa9459Szrj {
464*a9fa9459Szrj this->large_shndx_offset_ = - 0x100;
465*a9fa9459Szrj this->shstrndx_ -= 0x100;
466*a9fa9459Szrj }
467*a9fa9459Szrj if (this->shstrndx_ >= this->shnum_)
468*a9fa9459Szrj this->file_->error(_("bad shstrndx: %u >= %u"),
469*a9fa9459Szrj this->shstrndx_, this->shnum_);
470*a9fa9459Szrj }
471*a9fa9459Szrj }
472*a9fa9459Szrj }
473*a9fa9459Szrj }
474*a9fa9459Szrj
475*a9fa9459Szrj // Find section with sh_type equal to TYPE and return its index.
476*a9fa9459Szrj // Returns SHN_UNDEF if not found.
477*a9fa9459Szrj
478*a9fa9459Szrj template<int size, bool big_endian, typename File>
479*a9fa9459Szrj unsigned int
find_section_by_type(unsigned int type)480*a9fa9459Szrj Elf_file<size, big_endian, File>::find_section_by_type(unsigned int type)
481*a9fa9459Szrj {
482*a9fa9459Szrj unsigned int shnum = this->shnum();
483*a9fa9459Szrj typename File::View v(this->file_->view(this->shoff_,
484*a9fa9459Szrj This::shdr_size * shnum));
485*a9fa9459Szrj for (unsigned int i = 0; i < shnum; i++)
486*a9fa9459Szrj {
487*a9fa9459Szrj Ef_shdr shdr(v.data() + This::shdr_size * i);
488*a9fa9459Szrj if (shdr.get_sh_type() == type)
489*a9fa9459Szrj return i;
490*a9fa9459Szrj }
491*a9fa9459Szrj return SHN_UNDEF;
492*a9fa9459Szrj }
493*a9fa9459Szrj
494*a9fa9459Szrj // Return the file offset of the section header of section SHNDX.
495*a9fa9459Szrj
496*a9fa9459Szrj template<int size, bool big_endian, typename File>
497*a9fa9459Szrj off_t
section_header_offset(unsigned int shndx)498*a9fa9459Szrj Elf_file<size, big_endian, File>::section_header_offset(unsigned int shndx) const
499*a9fa9459Szrj {
500*a9fa9459Szrj if (shndx >= this->shnum())
501*a9fa9459Szrj this->file_->error(_("section_header_offset: bad shndx %u >= %u"),
502*a9fa9459Szrj shndx, this->shnum());
503*a9fa9459Szrj return this->shoff_ + This::shdr_size * shndx;
504*a9fa9459Szrj }
505*a9fa9459Szrj
506*a9fa9459Szrj // Return the name of section SHNDX.
507*a9fa9459Szrj
508*a9fa9459Szrj template<int size, bool big_endian, typename File>
509*a9fa9459Szrj std::string
section_name(unsigned int shndx)510*a9fa9459Szrj Elf_file<size, big_endian, File>::section_name(unsigned int shndx) const
511*a9fa9459Szrj {
512*a9fa9459Szrj File* const file = this->file_;
513*a9fa9459Szrj
514*a9fa9459Szrj // Get the section name offset.
515*a9fa9459Szrj unsigned int sh_name;
516*a9fa9459Szrj {
517*a9fa9459Szrj typename File::View v(file->view(this->section_header_offset(shndx),
518*a9fa9459Szrj This::shdr_size));
519*a9fa9459Szrj Ef_shdr shdr(v.data());
520*a9fa9459Szrj sh_name = shdr.get_sh_name();
521*a9fa9459Szrj }
522*a9fa9459Szrj
523*a9fa9459Szrj // Get the file offset for the section name string table data.
524*a9fa9459Szrj off_t shstr_off;
525*a9fa9459Szrj typename Elf_types<size>::Elf_WXword shstr_size;
526*a9fa9459Szrj {
527*a9fa9459Szrj const unsigned int shstrndx = this->shstrndx_;
528*a9fa9459Szrj typename File::View v(file->view(this->section_header_offset(shstrndx),
529*a9fa9459Szrj This::shdr_size));
530*a9fa9459Szrj Ef_shdr shstr_shdr(v.data());
531*a9fa9459Szrj shstr_off = shstr_shdr.get_sh_offset();
532*a9fa9459Szrj shstr_size = shstr_shdr.get_sh_size();
533*a9fa9459Szrj }
534*a9fa9459Szrj
535*a9fa9459Szrj if (sh_name >= shstr_size)
536*a9fa9459Szrj file->error(_("bad section name offset for section %u: %u"),
537*a9fa9459Szrj shndx, sh_name);
538*a9fa9459Szrj
539*a9fa9459Szrj typename File::View v(file->view(shstr_off, shstr_size));
540*a9fa9459Szrj
541*a9fa9459Szrj const unsigned char* datau = v.data();
542*a9fa9459Szrj const char* data = reinterpret_cast<const char*>(datau);
543*a9fa9459Szrj const void* p = ::memchr(data + sh_name, '\0', shstr_size - sh_name);
544*a9fa9459Szrj if (p == NULL)
545*a9fa9459Szrj file->error(_("missing null terminator for name of section %u"),
546*a9fa9459Szrj shndx);
547*a9fa9459Szrj
548*a9fa9459Szrj size_t len = static_cast<const char*>(p) - (data + sh_name);
549*a9fa9459Szrj
550*a9fa9459Szrj return std::string(data + sh_name, len);
551*a9fa9459Szrj }
552*a9fa9459Szrj
553*a9fa9459Szrj // Return the contents of section SHNDX.
554*a9fa9459Szrj
555*a9fa9459Szrj template<int size, bool big_endian, typename File>
556*a9fa9459Szrj typename File::Location
section_contents(unsigned int shndx)557*a9fa9459Szrj Elf_file<size, big_endian, File>::section_contents(unsigned int shndx)
558*a9fa9459Szrj {
559*a9fa9459Szrj File* const file = this->file_;
560*a9fa9459Szrj
561*a9fa9459Szrj if (shndx >= this->shnum())
562*a9fa9459Szrj file->error(_("section_contents: bad shndx %u >= %u"),
563*a9fa9459Szrj shndx, this->shnum());
564*a9fa9459Szrj
565*a9fa9459Szrj typename File::View v(file->view(this->section_header_offset(shndx),
566*a9fa9459Szrj This::shdr_size));
567*a9fa9459Szrj Ef_shdr shdr(v.data());
568*a9fa9459Szrj return typename File::Location(shdr.get_sh_offset(), shdr.get_sh_size());
569*a9fa9459Szrj }
570*a9fa9459Szrj
571*a9fa9459Szrj // Get the size of section SHNDX.
572*a9fa9459Szrj
573*a9fa9459Szrj template<int size, bool big_endian, typename File>
574*a9fa9459Szrj typename Elf_types<size>::Elf_WXword
section_size(unsigned int shndx)575*a9fa9459Szrj Elf_file<size, big_endian, File>::section_size(unsigned int shndx)
576*a9fa9459Szrj {
577*a9fa9459Szrj File* const file = this->file_;
578*a9fa9459Szrj
579*a9fa9459Szrj if (shndx >= this->shnum())
580*a9fa9459Szrj file->error(_("section_size: bad shndx %u >= %u"),
581*a9fa9459Szrj shndx, this->shnum());
582*a9fa9459Szrj
583*a9fa9459Szrj typename File::View v(file->view(this->section_header_offset(shndx),
584*a9fa9459Szrj This::shdr_size));
585*a9fa9459Szrj
586*a9fa9459Szrj Ef_shdr shdr(v.data());
587*a9fa9459Szrj return shdr.get_sh_size();
588*a9fa9459Szrj }
589*a9fa9459Szrj
590*a9fa9459Szrj // Return the section flags of section SHNDX.
591*a9fa9459Szrj
592*a9fa9459Szrj template<int size, bool big_endian, typename File>
593*a9fa9459Szrj typename Elf_types<size>::Elf_WXword
section_flags(unsigned int shndx)594*a9fa9459Szrj Elf_file<size, big_endian, File>::section_flags(unsigned int shndx)
595*a9fa9459Szrj {
596*a9fa9459Szrj File* const file = this->file_;
597*a9fa9459Szrj
598*a9fa9459Szrj if (shndx >= this->shnum())
599*a9fa9459Szrj file->error(_("section_flags: bad shndx %u >= %u"),
600*a9fa9459Szrj shndx, this->shnum());
601*a9fa9459Szrj
602*a9fa9459Szrj typename File::View v(file->view(this->section_header_offset(shndx),
603*a9fa9459Szrj This::shdr_size));
604*a9fa9459Szrj
605*a9fa9459Szrj Ef_shdr shdr(v.data());
606*a9fa9459Szrj return shdr.get_sh_flags();
607*a9fa9459Szrj }
608*a9fa9459Szrj
609*a9fa9459Szrj // Return the address of section SHNDX.
610*a9fa9459Szrj
611*a9fa9459Szrj template<int size, bool big_endian, typename File>
612*a9fa9459Szrj typename Elf_types<size>::Elf_Addr
section_addr(unsigned int shndx)613*a9fa9459Szrj Elf_file<size, big_endian, File>::section_addr(unsigned int shndx)
614*a9fa9459Szrj {
615*a9fa9459Szrj File* const file = this->file_;
616*a9fa9459Szrj
617*a9fa9459Szrj if (shndx >= this->shnum())
618*a9fa9459Szrj file->error(_("section_flags: bad shndx %u >= %u"),
619*a9fa9459Szrj shndx, this->shnum());
620*a9fa9459Szrj
621*a9fa9459Szrj typename File::View v(file->view(this->section_header_offset(shndx),
622*a9fa9459Szrj This::shdr_size));
623*a9fa9459Szrj
624*a9fa9459Szrj Ef_shdr shdr(v.data());
625*a9fa9459Szrj return shdr.get_sh_addr();
626*a9fa9459Szrj }
627*a9fa9459Szrj
628*a9fa9459Szrj // Return the type of section SHNDX.
629*a9fa9459Szrj
630*a9fa9459Szrj template<int size, bool big_endian, typename File>
631*a9fa9459Szrj Elf_Word
section_type(unsigned int shndx)632*a9fa9459Szrj Elf_file<size, big_endian, File>::section_type(unsigned int shndx)
633*a9fa9459Szrj {
634*a9fa9459Szrj File* const file = this->file_;
635*a9fa9459Szrj
636*a9fa9459Szrj if (shndx >= this->shnum())
637*a9fa9459Szrj file->error(_("section_type: bad shndx %u >= %u"),
638*a9fa9459Szrj shndx, this->shnum());
639*a9fa9459Szrj
640*a9fa9459Szrj typename File::View v(file->view(this->section_header_offset(shndx),
641*a9fa9459Szrj This::shdr_size));
642*a9fa9459Szrj
643*a9fa9459Szrj Ef_shdr shdr(v.data());
644*a9fa9459Szrj return shdr.get_sh_type();
645*a9fa9459Szrj }
646*a9fa9459Szrj
647*a9fa9459Szrj // Return the sh_link field of section SHNDX.
648*a9fa9459Szrj
649*a9fa9459Szrj template<int size, bool big_endian, typename File>
650*a9fa9459Szrj Elf_Word
section_link(unsigned int shndx)651*a9fa9459Szrj Elf_file<size, big_endian, File>::section_link(unsigned int shndx)
652*a9fa9459Szrj {
653*a9fa9459Szrj File* const file = this->file_;
654*a9fa9459Szrj
655*a9fa9459Szrj if (shndx >= this->shnum())
656*a9fa9459Szrj file->error(_("section_link: bad shndx %u >= %u"),
657*a9fa9459Szrj shndx, this->shnum());
658*a9fa9459Szrj
659*a9fa9459Szrj typename File::View v(file->view(this->section_header_offset(shndx),
660*a9fa9459Szrj This::shdr_size));
661*a9fa9459Szrj
662*a9fa9459Szrj Ef_shdr shdr(v.data());
663*a9fa9459Szrj return shdr.get_sh_link();
664*a9fa9459Szrj }
665*a9fa9459Szrj
666*a9fa9459Szrj // Return the sh_info field of section SHNDX.
667*a9fa9459Szrj
668*a9fa9459Szrj template<int size, bool big_endian, typename File>
669*a9fa9459Szrj Elf_Word
section_info(unsigned int shndx)670*a9fa9459Szrj Elf_file<size, big_endian, File>::section_info(unsigned int shndx)
671*a9fa9459Szrj {
672*a9fa9459Szrj File* const file = this->file_;
673*a9fa9459Szrj
674*a9fa9459Szrj if (shndx >= this->shnum())
675*a9fa9459Szrj file->error(_("section_info: bad shndx %u >= %u"),
676*a9fa9459Szrj shndx, this->shnum());
677*a9fa9459Szrj
678*a9fa9459Szrj typename File::View v(file->view(this->section_header_offset(shndx),
679*a9fa9459Szrj This::shdr_size));
680*a9fa9459Szrj
681*a9fa9459Szrj Ef_shdr shdr(v.data());
682*a9fa9459Szrj return shdr.get_sh_info();
683*a9fa9459Szrj }
684*a9fa9459Szrj
685*a9fa9459Szrj // Return the sh_addralign field of section SHNDX.
686*a9fa9459Szrj
687*a9fa9459Szrj template<int size, bool big_endian, typename File>
688*a9fa9459Szrj typename Elf_types<size>::Elf_WXword
section_addralign(unsigned int shndx)689*a9fa9459Szrj Elf_file<size, big_endian, File>::section_addralign(unsigned int shndx)
690*a9fa9459Szrj {
691*a9fa9459Szrj File* const file = this->file_;
692*a9fa9459Szrj
693*a9fa9459Szrj if (shndx >= this->shnum())
694*a9fa9459Szrj file->error(_("section_addralign: bad shndx %u >= %u"),
695*a9fa9459Szrj shndx, this->shnum());
696*a9fa9459Szrj
697*a9fa9459Szrj typename File::View v(file->view(this->section_header_offset(shndx),
698*a9fa9459Szrj This::shdr_size));
699*a9fa9459Szrj
700*a9fa9459Szrj Ef_shdr shdr(v.data());
701*a9fa9459Szrj return shdr.get_sh_addralign();
702*a9fa9459Szrj }
703*a9fa9459Szrj
704*a9fa9459Szrj inline
Elf_strtab(const unsigned char * p,size_t size)705*a9fa9459Szrj Elf_strtab::Elf_strtab(const unsigned char* p, size_t size)
706*a9fa9459Szrj {
707*a9fa9459Szrj // Check if the section is NUL-terminated. If it isn't, we ignore
708*a9fa9459Szrj // the last part to make sure we don't return non-NUL-terminated
709*a9fa9459Szrj // strings.
710*a9fa9459Szrj while (size > 0 && p[size - 1] != 0)
711*a9fa9459Szrj size--;
712*a9fa9459Szrj this->base_ = reinterpret_cast<const char*>(p);
713*a9fa9459Szrj this->usable_size_ = size;
714*a9fa9459Szrj }
715*a9fa9459Szrj
716*a9fa9459Szrj } // End namespace elfcpp.
717*a9fa9459Szrj
718*a9fa9459Szrj #endif // !defined(ELFCPP_FILE_H)
719