aboutsummaryrefslogtreecommitdiffstats
path: root/elff/elf_file.cc
blob: 3f1825ecbc775adf910b7dde0084df42f3cf156f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/* Copyright (C) 2007-2010 The Android Open Source Project
**
** This software is licensed under the terms of the GNU General Public
** License version 2, as published by the Free Software Foundation, and
** may be copied, distributed, and modified under those terms.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
*/

/*
 * Contains implementation of ElfFile classes that encapsulate an ELF file.
 */

#include "string.h"
#include "elf_file.h"
#include "elf_alloc.h"
#include "dwarf_cu.h"
#include "dwarf_utils.h"

#include <fcntl.h>
#ifndef O_BINARY
#define O_BINARY 0
#endif

/* Tags to parse when collecting info about routines. */
static const Dwarf_Tag parse_rt_tags[] = {
  DW_TAG_compile_unit,
  DW_TAG_partial_unit,
  DW_TAG_inlined_subroutine,
  DW_TAG_subprogram,
  0
};
static const DwarfParseContext parse_rt_context = { parse_rt_tags };

//=============================================================================
// Base ElfFile implementation
//=============================================================================

ElfFile::ElfFile()
    : fixed_base_address_(0),
      elf_handle_((MapFile*)-1),
      elf_file_path_(NULL),
      allocator_(NULL),
      sec_table_(NULL),
      sec_count_(0),
      sec_entry_size_(0),
      last_cu_(NULL),
      cu_count_(0),
      is_exec_(0) {
}

ElfFile::~ElfFile() {
  DwarfCU* cu_to_del = last_cu_;
  while (cu_to_del != NULL) {
    DwarfCU* next_cu_to_del = cu_to_del->prev_cu_;
    delete cu_to_del;
    cu_to_del = next_cu_to_del;
  }

  if (mapfile_is_valid(elf_handle_)) {
    mapfile_close(elf_handle_);
  }

  if (elf_file_path_ != NULL) {
    delete[] elf_file_path_;
  }

  if (sec_table_ != NULL) {
    delete[] reinterpret_cast<Elf_Byte*>(sec_table_);
  }

  /* Must be deleted last! */
  if (allocator_ != NULL) {
    delete allocator_;
  }
}

ElfFile* ElfFile::Create(const char* path) {
  ElfFile* ret = NULL;
  /* Allocate enough space on the stack to fit the largest ELF file header. */
  Elf64_FHdr header;
  const Elf_CommonHdr* elf_hdr = &header.common;

  assert(path != NULL && *path != '\0');
  if (path == NULL || *path == '\0') {
    _set_errno(EINVAL);
    return NULL;
  }

  /*
   * Open ELF file, and read its header (the largest one possible).
   */
  MapFile* file_handle = mapfile_open(path, O_RDONLY | O_BINARY, 0);
  if (!mapfile_is_valid(file_handle)) {
    return NULL;
  }
  const ssize_t read_bytes = mapfile_read(file_handle, &header, sizeof(header));
  mapfile_close(file_handle);
  assert(read_bytes != -1 && read_bytes == sizeof(header));
  if (read_bytes == -1 || read_bytes != sizeof(header)) {
    if (read_bytes != -1) {
      _set_errno(EINVAL);
    }
    return NULL;
  }

  /* Lets see if this is an ELF file at all. */
  if (memcmp(elf_hdr->e_ident, ELFMAG, SELFMAG) != 0) {
    /* File is not an ELF file. */
    _set_errno(ENOEXEC);
    return NULL;
  }

  /* Lets check ELF's "bitness". */
  assert(elf_hdr->ei_info.ei_class == ELFCLASS32 ||
         elf_hdr->ei_info.ei_class == ELFCLASS64);
  if (elf_hdr->ei_info.ei_class != ELFCLASS32 &&
      elf_hdr->ei_info.ei_class != ELFCLASS64) {
    /* Neither 32, or 64-bit ELF file. Something wrong here. */
    _set_errno(EBADF);
    return NULL;
  }

  /* Lets instantiate appropriate ElfFileImpl object for this ELF. */
  if (elf_hdr->ei_info.ei_class == ELFCLASS32) {
    ret = new ElfFileImpl<Elf32_Addr, Elf32_Off>;
  } else {
    ret = new ElfFileImpl<Elf64_Addr, Elf64_Off>;
  }
  assert(ret != NULL);
  if (ret != NULL) {
    if (!ret->initialize(elf_hdr, path)) {
      delete ret;
      ret = NULL;
    }
  } else {
    _set_errno(ENOMEM);
  }

  return ret;
}

bool ElfFile::initialize(const Elf_CommonHdr* elf_hdr, const char* path) {
  /* Must be created first! */
  allocator_ = new ElfAllocator();
  assert(allocator_ != NULL);
  if (allocator_ == NULL) {
    _set_errno(ENOMEM);
    return false;
  }

  /* Copy file path. */
  size_t path_len = strlen(path) + 1;
  elf_file_path_ = new char[path_len];
  assert(elf_file_path_ != NULL);
  if (elf_file_path_ == NULL) {
    _set_errno(ENOMEM);
    return false;
  }
  memcpy(elf_file_path_, path, path_len);

  /* Cache some basic ELF properties. */
  is_ELF_64_ = elf_hdr->ei_info.ei_class == ELFCLASS64;
  is_elf_big_endian_ = elf_hdr->ei_info.ei_data == ELFDATA2MSB;
  same_endianness_ = is_elf_little_endian() == is_little_endian_cpu();
  is_exec_ = elf_hdr->e_type == 2;

  /* Reopen file for further reads and mappings. */
  elf_handle_ = mapfile_open(elf_file_path_, O_RDONLY | O_BINARY, 0);
  return mapfile_is_valid(elf_handle_);
}

bool ElfFile::get_pc_address_info(Elf_Xword address,
                                  Elf_AddressInfo* address_info) {
  assert(address_info != NULL);
  if (address_info == NULL) {
    _set_errno(EINVAL);
    return false;
  }

  /* Collect routine information for all CUs in this file. */
  if (parse_compilation_units(&parse_rt_context) == -1) {
    return false;
  }

  /* Iterate through the collected CUs looking for the one that
   * contains the given address. */
  address_info->inline_stack = NULL;
  DwarfCU* cu = last_cu();
  while (cu != NULL) {
    /* Find a leaf DIE object in the current CU that contains the address. */
    Dwarf_AddressInfo info;
    info.die_obj = cu->get_leaf_die_for_address(address);
    if (info.die_obj != NULL) {
      /* Convert the address to a location inside source file. */
      if (cu->get_pc_address_file_info(address, &info)) {
          /* Copy location information to the returning structure. */
          address_info->file_name = info.file_name;
          address_info->dir_name = info.dir_name;
          address_info->line_number = info.line_number;
      } else {
          address_info->file_name = NULL;
          address_info->dir_name = NULL;
          address_info->line_number = 0;
      }

      /* Lets see if the DIE represents a routine (rather than
       * a lexical block, for instance). */
      Dwarf_Tag tag = info.die_obj->get_tag();
      while (!dwarf_tag_is_routine(tag)) {
        /* This is not a routine DIE. Lets loop trhough the parents of that
         * DIE looking for the first routine DIE. */
        info.die_obj = info.die_obj->parent_die();
        if (info.die_obj == NULL) {
          /* Reached compilation unit DIE. Can't go any further. */
          address_info->routine_name = "<unknown>";
          return true;
        }
        tag = info.die_obj->get_tag();
      }

      /* Save name of the routine that contains the address. */
      address_info->routine_name = info.die_obj->get_name();
      if (address_info->routine_name == NULL) {
        /* In some cases (minimum debugging info in the file) routine
         * name may be not avaible. We, however, are obliged by API
         * considerations to return something in this field. */
          address_info->routine_name = "<unknown>";
      }

      /* Lets see if address belongs to an inlined routine. */
      if (tag != DW_TAG_inlined_subroutine) {
        address_info->inline_stack = NULL;
        return true;
      }

      /*
       * Address belongs to an inlined routine. Create inline stack.
       */

      /* Allocate inline stack array big enough to fit all parent entries. */
      address_info->inline_stack =
        new Elf_InlineInfo[info.die_obj->get_level() + 1];
      assert(address_info->inline_stack != NULL);
      if (address_info->inline_stack == NULL) {
        _set_errno(ENOMEM);
        return false;
      }
      memset(address_info->inline_stack, 0,
             sizeof(Elf_InlineInfo) * (info.die_obj->get_level() + 1));

      /* Reverse DIEs filling in inline stack entries for inline
       * routine tags. */
      int inl_index = 0;
      do {
        /* Save source file information. */
        DIEAttrib file_desc;
        if (info.die_obj->get_attrib(DW_AT_call_file, &file_desc)) {
          const Dwarf_STMTL_FileDesc* desc =
              cu->get_stmt_file_info(file_desc.value()->u32);
          if (desc != NULL) {
            address_info->inline_stack[inl_index].inlined_in_file =
                desc->file_name;
            address_info->inline_stack[inl_index].inlined_in_file_dir =
                cu->get_stmt_dir_name(desc->get_dir_index());
          }
        }
        if (address_info->inline_stack[inl_index].inlined_in_file == NULL) {
          address_info->inline_stack[inl_index].inlined_in_file = "<unknown>";
          address_info->inline_stack[inl_index].inlined_in_file_dir = NULL;
        }

        /* Save source line information. */
        if (info.die_obj->get_attrib(DW_AT_call_line, &file_desc)) {
          address_info->inline_stack[inl_index].inlined_at_line = file_desc.value()->u32;
        }

        /* Advance DIE to the parent routine, and save its name. */
        info.die_obj = info.die_obj->parent_die();
        assert(info.die_obj != NULL);
        if (info.die_obj != NULL) {
          tag = info.die_obj->get_tag();
          while (!dwarf_tag_is_routine(tag)) {
            info.die_obj = info.die_obj->parent_die();
            if (info.die_obj == NULL) {
              break;
            }
            tag = info.die_obj->get_tag();
          }
          if (info.die_obj != NULL) {
            address_info->inline_stack[inl_index].routine_name =
                info.die_obj->get_name();
          }
        }
        if (address_info->inline_stack[inl_index].routine_name == NULL) {
          address_info->inline_stack[inl_index].routine_name = "<unknown>";
        }

        /* Continue with the parent DIE. */
        inl_index++;
      } while (info.die_obj != NULL && tag == DW_TAG_inlined_subroutine);

      return true;
    }
    cu = cu->prev_cu();
  }

  return false;
}

void ElfFile::free_pc_address_info(Elf_AddressInfo* address_info) const {
  assert(address_info != NULL);
  if (address_info != NULL && address_info->inline_stack != NULL) {
    delete address_info->inline_stack;
    address_info->inline_stack = NULL;
  }
}

//=============================================================================
// ElfFileImpl
//=============================================================================

template <typename Elf_Addr, typename Elf_Off>
bool ElfFileImpl<Elf_Addr, Elf_Off>::initialize(const Elf_CommonHdr* elf_hdr,
                                                const char* path) {
  /* Must be called first! */
  if (!ElfFile::initialize(elf_hdr, path)) {
    return false;
  }

  /* Cache some header data, so later we can discard the header. */
  const Elf_FHdr<Elf_Addr, Elf_Off>* header =
      reinterpret_cast<const Elf_FHdr<Elf_Addr, Elf_Off>*>(elf_hdr);
  sec_count_ = pull_val(header->e_shnum);
  sec_entry_size_ = pull_val(header->e_shentsize);
  fixed_base_address_ = pull_val(header->e_entry) & ~0xFFF;

  /* Cache section table (must have one!) */
  const Elf_Off sec_table_off = pull_val(header->e_shoff);
  assert(sec_table_off != 0 && sec_count_ != 0);
  if (sec_table_off == 0 || sec_count_ == 0) {
    _set_errno(EBADF);
    return false;
  }
  const size_t sec_table_size = sec_count_ * sec_entry_size_;
  sec_table_ = new Elf_Byte[sec_table_size];
  assert(sec_table_ != NULL);
  if (sec_table_ == NULL) {
    _set_errno(ENOMEM);
    return false;
  }
  if (mapfile_read_at(elf_handle_, sec_table_off, sec_table_,
                      sec_table_size) < 0) {
      return false;
  }

  /* Map ELF's string section (must have one!). */
  const Elf_Half str_sec_index = pull_val(header->e_shstrndx);
  assert(str_sec_index != SHN_UNDEF);
  if (str_sec_index == SHN_UNDEF) {
    _set_errno(EBADF);
    return false;
  }
  const Elf_SHdr<Elf_Addr, Elf_Off>* str_sec =
      reinterpret_cast<const Elf_SHdr<Elf_Addr, Elf_Off>*>
          (get_section_by_index(str_sec_index));
  assert(str_sec != NULL);
  if (str_sec == NULL) {
    _set_errno(EBADF);
    return false;
  }
  if (!string_section_.map(elf_handle_, pull_val(str_sec->sh_offset),
                           pull_val(str_sec->sh_size))) {
    return false;
  }

  /* Lets determine DWARF format. According to the docs, DWARF is 64 bit, if
   * first 4 bytes in the compilation unit header are set to 0xFFFFFFFF.
   * .debug_info section of the ELF file begins with the first CU header. */
  if (!map_section_by_name(".debug_info", &debug_info_)) {
    _set_errno(EBADF);
    return false;
  }

  /* Note that we don't care about endianness here, since 0xFFFFFFFF is an
   * endianness-independent value, so we don't have to pull_val here. */
  is_DWARF_64_ =
    *reinterpret_cast<const Elf_Word*>(debug_info_.data()) == 0xFFFFFFFF;

  return true;
}

template <typename Elf_Addr, typename Elf_Off>
int ElfFileImpl<Elf_Addr, Elf_Off>::parse_compilation_units(
    const DwarfParseContext* parse_context) {
  /* Lets see if we already parsed the file. */
  if (last_cu() != NULL) {
    return cu_count_;
  }

  /* Cache sections required for this parsing. */
  if (!map_section_by_name(".debug_abbrev", &debug_abbrev_) ||
      !map_section_by_name(".debug_ranges", &debug_ranges_) ||
      !map_section_by_name(".debug_line", &debug_line_) ||
      !map_section_by_name(".debug_str", &debug_str_)) {
    _set_errno(EBADF);
    return false;
  }

  /* .debug_info section opens with the first CU header. */
  const void* next_cu = debug_info_.data();

  /* Iterate through CUs until we reached the end of .debug_info section, or
   * advanced to a CU with zero size, indicating the end of CU list for this
   * file. */
  while (is_valid_cu(next_cu)) {
    /* Instatiate CU, depending on DWARF "bitness". */
    DwarfCU* cu = DwarfCU::create_instance(this, next_cu);
    if (cu == NULL) {
      _set_errno(ENOMEM);
      return -1;
    }

    if (cu->parse(parse_context, &next_cu)) {
      cu->set_prev_cu(last_cu_);
      last_cu_ = cu;
      cu_count_++;
    } else {
      delete cu;
      return -1;
    }
  };

  return cu_count_;
}

template <typename Elf_Addr, typename Elf_Off>
bool ElfFileImpl<Elf_Addr, Elf_Off>::get_section_info_by_name(const char* name,
                                                              Elf_Off* offset,
                                                              Elf_Word* size) {
  const Elf_SHdr<Elf_Addr, Elf_Off>* cur_section =
      reinterpret_cast<const Elf_SHdr<Elf_Addr, Elf_Off>*>(sec_table_);

  for (Elf_Half sec = 0; sec < sec_count_; sec++) {
    const char* sec_name = get_str_sec_str(pull_val(cur_section->sh_name));
    if (sec_name != NULL && strcmp(name, sec_name) == 0) {
      *offset = pull_val(cur_section->sh_offset);
      *size = pull_val(cur_section->sh_size);
      return true;
    }
    cur_section = reinterpret_cast<const Elf_SHdr<Elf_Addr, Elf_Off>*>
                                  (INC_CPTR(cur_section, sec_entry_size_));
  }
  _set_errno(EINVAL);
  return false;
}

template <typename Elf_Addr, typename Elf_Off>
bool ElfFileImpl<Elf_Addr, Elf_Off>::map_section_by_name(
    const char* name,
    ElfMappedSection* section) {
  if (section->is_mapped()) {
    return true;
  }

  Elf_Off offset;
  Elf_Word size;
  if (!get_section_info_by_name(name, &offset, &size)) {
    return false;
  }

  return section->map(elf_handle_, offset, size);
}