* symfile.h (struct sym_fns): Add new field sym_read_linetable.
[deliverable/binutils-gdb.git] / elfcpp / elfcpp_file.h
CommitLineData
645f8123
ILT
1// elfcpp_file.h -- file access for elfcpp -*- C++ -*-
2
3// This header file defines the class Elf_file which can be used to
4// read useful data from an ELF file. The functions here are all
5// templates which take a file interface object as a parameter. This
6// type must have a subtype View. This type must support two methods:
7// View view(off_t file_offset, off_t data_size)
8// returns a View for the specified part of the file.
9// void error(const char* printf_format, ...)
10// prints an error message and does not return. The subtype View must
11// support a method
12// const unsigned char* data()
13// which returns a pointer to a buffer containing the requested data.
14// This general interface is used to read data from the file. Objects
15// of type View will never survive longer than the elfcpp function.
16
17// Some of these functions must return a reference to part of the
18// file. To use these, the file interface must support a subtype
19// Location:
20// Location(off_t file_offset, off_t data_size)
21// To use this in conjunction with the accessors types Shdr, etc., the
22// file interface should support an overload of view:
23// View view(Location)
24// This permits writing
25// elfcpp::Shdr shdr(file, ef.section_header(n));
26
27#ifndef ELFPCP_FILE_H
28#define ELFCPP_FILE_H
29
30#include <string>
31#include <cstring>
32
33namespace elfcpp
34{
35
36// This object is used to read an ELF file.
37// SIZE: The size of file, 32 or 64.
38// BIG_ENDIAN: Whether the file is in big-endian format.
39// FILE: A file reading type as described above.
40
41template<int size, bool big_endian, typename File>
42class Elf_file
43{
44 private:
45 typedef Elf_file<size, big_endian, File> This;
46
47 public:
48 static const int ehdr_size = Elf_sizes<size>::ehdr_size;
49 static const int phdr_size = Elf_sizes<size>::phdr_size;
50 static const int shdr_size = Elf_sizes<size>::shdr_size;
51 static const int sym_size = Elf_sizes<size>::sym_size;
52 static const int rel_size = Elf_sizes<size>::rel_size;
53 static const int rela_size = Elf_sizes<size>::rela_size;
54
55 typedef Ehdr<size, big_endian> Ef_ehdr;
56 typedef Phdr<size, big_endian> Ef_phdr;
57 typedef Shdr<size, big_endian> Ef_shdr;
58 typedef Sym<size, big_endian> Ef_sym;
59
60 // Construct an Elf_file given an ELF file header.
61 Elf_file(File* file, const Ef_ehdr& ehdr)
62 { this->construct(file, ehdr); }
63
64 // Construct an ELF file.
65 inline
66 Elf_file(File* file);
67
68 // Return the file offset to the section headers.
69 off_t
70 shoff() const
71 { return this->shoff_; }
72
73 // Return the number of sections.
74 unsigned int
75 shnum()
76 {
77 this->initialize_shnum();
78 return this->shnum_;
79 }
80
81 // Return the section index of the section name string table.
82 unsigned int
83 shstrndx()
84 {
85 this->initialize_shnum();
86 return this->shstrndx_;
87 }
88
89 // Return the location of the header of section SHNDX.
90 typename File::Location
91 section_header(unsigned int shndx)
92 {
93 return typename File::Location(this->section_header_offset(shndx),
94 shdr_size);
95 }
96
97 // Return the name of section SHNDX.
98 std::string
99 section_name(unsigned int shndx);
100
101 // Return the location of the contents of section SHNDX.
102 typename File::Location
103 section_contents(unsigned int shndx);
104
a3ad94ed
ILT
105 // Return the flags of section SHNDX.
106 typename Elf_types<size>::Elf_WXword
107 section_flags(unsigned int shndx);
108
e698bc31
ILT
109 // Return the type of section SHNDX.
110 Elf_Word
111 section_type(unsigned int shndx);
112
645f8123
ILT
113 private:
114 // Shared constructor code.
115 void
116 construct(File* file, const Ef_ehdr& ehdr);
117
118 // Initialize shnum_ and shstrndx_.
119 void
120 initialize_shnum();
121
122 // Return the file offset of the header of section SHNDX.
123 off_t
124 section_header_offset(unsigned int shndx);
125
126 // The file we are reading.
127 File* file_;
128 // The file offset to the section headers.
129 off_t shoff_;
130 // The number of sections.
131 unsigned int shnum_;
132 // The section index of the section name string table.
133 unsigned int shstrndx_;
134};
135
136// Template function definitions.
137
138// Construct an Elf_file given an ELF file header.
139
140template<int size, bool big_endian, typename File>
141void
142Elf_file<size, big_endian, File>::construct(File* file, const Ef_ehdr& ehdr)
143{
144 this->file_ = file;
145 this->shoff_ = ehdr.get_e_shoff();
146 this->shnum_ = ehdr.get_e_shnum();
147 this->shstrndx_ = ehdr.get_e_shstrndx();
148 if (ehdr.get_e_ehsize() != This::ehdr_size)
149 file->error(_("bad e_ehsize (%d != %d)"),
150 ehdr.get_e_ehsize(), This::ehdr_size);
151 if (ehdr.get_e_shentsize() != This::shdr_size)
152 file->error(_("bad e_shentsize (%d != %d)"),
153 ehdr.get_e_shentsize(), This::shdr_size);
154}
155
156// Construct an ELF file.
157
158template<int size, bool big_endian, typename File>
159inline
160Elf_file<size, big_endian, File>::Elf_file(File* file)
161{
162 typename File::View v(file->view(file_header_offset, This::ehdr_size));
163 this->construct(file, Ef_ehdr(v.data()));
164}
165
166// Initialize the shnum_ and shstrndx_ fields, handling overflow.
167
168template<int size, bool big_endian, typename File>
169void
170Elf_file<size, big_endian, File>::initialize_shnum()
171{
172 if ((this->shnum_ == 0 || this->shstrndx_ == SHN_XINDEX)
173 && this->shoff_ != 0)
174 {
175 typename File::View v(this->file_->view(this->shoff_, This::shdr_size));
176 Ef_shdr shdr(v.data());
177 if (this->shnum_ == 0)
178 this->shnum_ = shdr.get_sh_size();
179 if (this->shstrndx_ == SHN_XINDEX)
180 this->shstrndx_ = shdr.get_sh_link();
181 }
182}
183
184// Return the file offset of the section header of section SHNDX.
185
186template<int size, bool big_endian, typename File>
187off_t
188Elf_file<size, big_endian, File>::section_header_offset(unsigned int shndx)
189{
190 if (shndx >= this->shnum())
191 this->file_->error(_("section_header_offset: bad shndx %u >= %u"),
192 shndx, this->shnum());
193 return this->shoff_ + This::shdr_size * shndx;
194}
195
196// Return the name of section SHNDX.
197
198template<int size, bool big_endian, typename File>
199std::string
200Elf_file<size, big_endian, File>::section_name(unsigned int shndx)
201{
202 File* const file = this->file_;
203
204 // Get the section name offset.
205 unsigned int sh_name;
206 {
207 typename File::View v(file->view(this->section_header_offset(shndx),
208 This::shdr_size));
209 Ef_shdr shdr(v.data());
210 sh_name = shdr.get_sh_name();
211 }
212
213 // Get the file offset for the section name string table data.
214 off_t shstr_off;
215 off_t shstr_size;
216 {
217 const unsigned int shstrndx = this->shstrndx_;
218 typename File::View v(file->view(this->section_header_offset(shstrndx),
219 This::shdr_size));
220 Ef_shdr shstr_shdr(v.data());
221 shstr_off = shstr_shdr.get_sh_offset();
222 shstr_size = shstr_shdr.get_sh_size();
223 }
224
225 if (sh_name >= shstr_size)
226 file->error(_("bad section name offset for section %u: %u"),
227 shndx, sh_name);
228
229 typename File::View v(file->view(shstr_off, shstr_size));
230
231 const unsigned char* datau = v.data();
232 const char* data = reinterpret_cast<const char*>(datau);
233 const void* p = ::memchr(data + sh_name, '\0', shstr_size - sh_name);
234 if (p == NULL)
235 file->error(_("missing null terminator for name of section %u"),
236 shndx);
237
238 size_t len = static_cast<const char*>(p) - (data + sh_name);
239
240 return std::string(data + sh_name, len);
241}
242
243// Return the contents of section SHNDX.
244
245template<int size, bool big_endian, typename File>
246typename File::Location
247Elf_file<size, big_endian, File>::section_contents(unsigned int shndx)
248{
249 File* const file = this->file_;
250
251 if (shndx >= this->shnum())
252 file->error(_("section_contents: bad shndx %u >= %u"),
253 shndx, this->shnum());
254
255 typename File::View v(file->view(this->section_header_offset(shndx),
256 This::shdr_size));
257 Ef_shdr shdr(v.data());
258 return typename File::Location(shdr.get_sh_offset(), shdr.get_sh_size());
259}
260
a3ad94ed
ILT
261// Return the section flags of section SHNDX.
262
263template<int size, bool big_endian, typename File>
264typename Elf_types<size>::Elf_WXword
265Elf_file<size, big_endian, File>::section_flags(unsigned int shndx)
266{
267 File* const file = this->file_;
268
269 if (shndx >= this->shnum())
270 file->error(_("section_flags: bad shndx %u >= %u"),
271 shndx, this->shnum());
272
273 typename File::View v(file->view(this->section_header_offset(shndx),
274 This::shdr_size));
275
276 Ef_shdr shdr(v.data());
277 return shdr.get_sh_flags();
278}
279
e698bc31
ILT
280// Return the type of section SHNDX.
281
282template<int size, bool big_endian, typename File>
283Elf_Word
284Elf_file<size, big_endian, File>::section_type(unsigned int shndx)
285{
286 File* const file = this->file_;
287
288 if (shndx >= this->shnum())
289 file->error(_("section_type: bad shndx %u >= %u"),
290 shndx, this->shnum());
291
292 typename File::View v(file->view(this->section_header_offset(shndx),
293 This::shdr_size));
294
295 Ef_shdr shdr(v.data());
296 return shdr.get_sh_type();
297}
298
645f8123
ILT
299} // End namespace elfcpp.
300
301#endif // !defined(ELFCPP_FILE_H)
This page took 0.061034 seconds and 4 git commands to generate.