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