aboutsummaryrefslogtreecommitdiff
path: root/src/elfxx.c
blob: a0500380174332d432510978104884ab69835b0e (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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
/* libunwind - a platform-independent unwind library
   Copyright (C) 2003-2005 Hewlett-Packard Co
   Copyright (C) 2007 David Mosberger-Tang
	Contributed by David Mosberger-Tang <dmosberger@gmail.com>

This file is part of libunwind.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */

#include "libunwind_i.h"

#include <stdio.h>
#include <sys/param.h>

#if HAVE_LZMA
#include <7zCrc.h>
#include <Xz.h>
#include <XzCrc64.h>
#endif /* HAVE_LZMA */

// --------------------------------------------------------------------------
// Functions to read elf data from memory.
// --------------------------------------------------------------------------
extern size_t elf_w (memory_read) (
    struct elf_image* ei, unw_word_t addr, uint8_t* buffer, size_t bytes, bool string_read) {
  uintptr_t end = ei->u.memory.end;
  unw_accessors_t* a = unw_get_accessors (ei->u.memory.as);
  if (end - addr < bytes) {
    bytes = end - addr;
  }
  size_t bytes_read = 0;
  unw_word_t data_word;
  size_t align_bytes = addr & (sizeof(unw_word_t) - 1);
  if (align_bytes != 0) {
    if ((*a->access_mem) (ei->u.memory.as, addr & ~(sizeof(unw_word_t) - 1), &data_word,
                          0, ei->u.memory.as_arg) != 0) {
      return 0;
    }
    size_t copy_bytes = MIN(sizeof(unw_word_t) - align_bytes, bytes);
    memcpy (buffer, (uint8_t*) (&data_word) + align_bytes, copy_bytes);
    if (string_read) {
      // Check for nul terminator.
      uint8_t* nul_terminator = memchr (buffer, '\0', copy_bytes);
      if (nul_terminator != NULL) {
        return nul_terminator - buffer;
      }
    }

    addr += copy_bytes;
    bytes_read += copy_bytes;
    bytes -= copy_bytes;
    buffer += copy_bytes;
  }

  size_t num_words = bytes / sizeof(unw_word_t);
  size_t i;
  for (i = 0; i < num_words; i++) {
    if ((*a->access_mem) (ei->u.memory.as, addr, &data_word, 0, ei->u.memory.as_arg) != 0) {
      return bytes_read;
    }

    memcpy (buffer, &data_word, sizeof(unw_word_t));
    if (string_read) {
      // Check for nul terminator.
      uint8_t* nul_terminator = memchr (buffer, '\0', sizeof(unw_word_t));
      if (nul_terminator != NULL) {
        return nul_terminator - buffer + bytes_read;
      }
    }

    addr += sizeof(unw_word_t);
    bytes_read += sizeof(unw_word_t);
    buffer += sizeof(unw_word_t);
  }

  size_t left_over = bytes & (sizeof(unw_word_t) - 1);
  if (left_over) {
    if ((*a->access_mem) (ei->u.memory.as, addr, &data_word, 0, ei->u.memory.as_arg) != 0) {
      return bytes_read;
    }

    memcpy (buffer, &data_word, left_over);
    if (string_read) {
      // Check for nul terminator.
      uint8_t* nul_terminator = memchr (buffer, '\0', sizeof(unw_word_t));
      if (nul_terminator != NULL) {
        return nul_terminator - buffer + bytes_read;
      }
    }

    bytes_read += left_over;
  }
  return bytes_read;
}

static bool elf_w (section_table_offset) (struct elf_image* ei, Elf_W(Ehdr)* ehdr, Elf_W(Off)* offset) {
  GET_EHDR_FIELD(ei, ehdr, e_shoff, true);
  GET_EHDR_FIELD(ei, ehdr, e_shentsize, true);
  GET_EHDR_FIELD(ei, ehdr, e_shnum, true);

  uintptr_t size = ei->u.memory.end - ei->u.memory.start;
  if (ehdr->e_shoff + ehdr->e_shnum * ehdr->e_shentsize > size) {
    Debug (1, "section table outside of image? (%lu > %lu)\n",
           (unsigned long) (ehdr->e_shoff + ehdr->e_shnum * ehdr->e_shentsize),
           (unsigned long) size);
    return false;
  }

  *offset = ehdr->e_shoff;
  return true;
}

static bool elf_w (string_table_offset) (
    struct elf_image* ei, int section, Elf_W(Ehdr)* ehdr, Elf_W(Off)* offset) {
  GET_EHDR_FIELD(ei, ehdr, e_shoff, true);
  GET_EHDR_FIELD(ei, ehdr, e_shentsize, true);
  unw_word_t str_soff = ehdr->e_shoff + (section * ehdr->e_shentsize);
  uintptr_t size = ei->u.memory.end - ei->u.memory.start;
  if (str_soff + ehdr->e_shentsize > size) {
    Debug (1, "string shdr table outside of image? (%lu > %lu)\n",
           (unsigned long) (str_soff + ehdr->e_shentsize),
           (unsigned long) size);
    return false;
  }

  Elf_W(Shdr) shdr;
  GET_SHDR_FIELD(ei, str_soff, &shdr, sh_offset);
  GET_SHDR_FIELD(ei, str_soff, &shdr, sh_size);
  if (shdr.sh_offset + shdr.sh_size > size) {
    Debug (1, "string table outside of image? (%lu > %lu)\n",
           (unsigned long) (shdr.sh_offset + shdr.sh_size),
           (unsigned long) size);
    return false;
  }

  Debug (16, "strtab=0x%lx\n", (long) shdr.sh_offset);
  *offset = shdr.sh_offset;
  return true;
}

static bool elf_w (lookup_symbol_memory) (
    unw_addr_space_t as, unw_word_t ip, struct elf_image* ei, Elf_W(Addr) load_offset,
    char* buf, size_t buf_len, unw_word_t* offp, Elf_W(Ehdr)* ehdr) {
  Elf_W(Off) shdr_offset;
  if (!elf_w (section_table_offset) (ei, ehdr, &shdr_offset)) {
    return false;
  }

  GET_EHDR_FIELD(ei, ehdr, e_shnum, true);
  GET_EHDR_FIELD(ei, ehdr, e_shentsize, true);
  int i;
  for (i = 0; i < ehdr->e_shnum; ++i) {
    Elf_W(Shdr) shdr;
    GET_SHDR_FIELD(ei, shdr_offset, &shdr, sh_type);
    switch (shdr.sh_type) {
      case SHT_SYMTAB:
      case SHT_DYNSYM:
      {
        GET_SHDR_FIELD(ei, shdr_offset, &shdr, sh_link);

        Elf_W(Off) strtab_offset;
        if (!elf_w (string_table_offset) (ei, shdr.sh_link, ehdr, &strtab_offset)) {
          continue;
        }

        GET_SHDR_FIELD(ei, shdr_offset, &shdr, sh_offset);
        GET_SHDR_FIELD(ei, shdr_offset, &shdr, sh_size);
        GET_SHDR_FIELD(ei, shdr_offset, &shdr, sh_entsize);

        Debug (16, "symtab=0x%lx[%d]\n", (long) shdr.sh_offset, shdr.sh_type);

        unw_word_t sym_offset;
        unw_word_t symtab_end = shdr.sh_offset + shdr.sh_size;
        for (sym_offset = shdr.sh_offset;
             sym_offset < symtab_end;
             sym_offset += shdr.sh_entsize) {
          Elf_W(Sym) sym;
          GET_SYM_FIELD(ei, sym_offset, &sym, st_info);
          GET_SYM_FIELD(ei, sym_offset, &sym, st_shndx);

          if (ELF_W (ST_TYPE) (sym.st_info) == STT_FUNC && sym.st_shndx != SHN_UNDEF) {
            GET_SYM_FIELD(ei, sym_offset, &sym, st_value);
            Elf_W(Addr) val;
            if (tdep_get_func_addr (as, sym.st_value, &val) < 0) {
              continue;
            }
            if (sym.st_shndx != SHN_ABS) {
              val += load_offset;
            }
            Debug (16, "0x%016lx info=0x%02x\n", (long) val, sym.st_info);

            GET_SYM_FIELD(ei, sym_offset, &sym, st_size);
            if (ip >= val && (Elf_W(Addr)) (ip - val) < sym.st_size) {
              GET_SYM_FIELD(ei, sym_offset, &sym, st_name);
              uintptr_t size = ei->u.memory.end - ei->u.memory.start;
              Elf_W(Off) strname_offset = strtab_offset + sym.st_name;
              if (strname_offset > size || strname_offset < strtab_offset) {
                // Malformed elf symbol table.
                break;
              }

              size_t bytes_read = elf_w (memory_read) (
                  ei, ei->u.memory.start + strname_offset,
                  (uint8_t*) buf, buf_len, true);
              if (bytes_read == 0) {
                // Empty name, so keep checking the other symbol tables
                // for a possible match.
                break;
              }
              // Ensure the string is nul terminated, it is assumed that
              // sizeof(buf) >= buf_len + 1.
              buf[buf_len] = '\0';

              if (offp != NULL) {
                *offp = ip - val;
              }
              return true;
            }
          }
        }
        break;
      }

      default:
        break;
    }
    shdr_offset += ehdr->e_shentsize;
  }
  return false;
}

static bool elf_w (get_load_offset_memory) (
    struct elf_image* ei, unsigned long segbase, unsigned long mapoff,
    Elf_W(Ehdr)* ehdr, Elf_W(Addr)* load_offset) {
  GET_EHDR_FIELD(ei, ehdr, e_phoff, true);
  GET_EHDR_FIELD(ei, ehdr, e_phnum, true);

  unw_word_t offset = ehdr->e_phoff;
  int i;
  for (i = 0; i < ehdr->e_phnum; ++i) {
    Elf_W(Phdr) phdr;
    GET_PHDR_FIELD(ei, offset, &phdr, p_type);
    if (phdr.p_type == PT_LOAD) {
      GET_PHDR_FIELD(ei, offset, &phdr, p_offset);
      if (phdr.p_offset == mapoff) {
        GET_PHDR_FIELD(ei, offset, &phdr, p_vaddr);
        *load_offset = segbase - phdr.p_vaddr;
        return true;
      }
    }
    offset += sizeof(Elf_W(Phdr));
  }
  return false;
}

// --------------------------------------------------------------------------
// Functions to read elf data from the mapped elf image.
// --------------------------------------------------------------------------
static Elf_W(Shdr)* elf_w (section_table) (struct elf_image* ei) {
  Elf_W(Ehdr)* ehdr = ei->u.mapped.image;
  Elf_W(Off) soff = ehdr->e_shoff;
  if (soff + ehdr->e_shnum * ehdr->e_shentsize > ei->u.mapped.size) {
    Debug (1, "section table outside of image? (%lu > %lu)\n",
           (unsigned long) (soff + ehdr->e_shnum * ehdr->e_shentsize),
           (unsigned long) ei->u.mapped.size);
    return NULL;
  }

  return (Elf_W(Shdr) *) ((char *) ei->u.mapped.image + soff);
}

static char* elf_w (string_table) (struct elf_image* ei, int section) {
  Elf_W(Ehdr)* ehdr = ei->u.mapped.image;
  Elf_W(Off) str_soff = ehdr->e_shoff + (section * ehdr->e_shentsize);
  if (str_soff + ehdr->e_shentsize > ei->u.mapped.size) {
    Debug (1, "string shdr table outside of image? (%lu > %lu)\n",
           (unsigned long) (str_soff + ehdr->e_shentsize),
           (unsigned long) ei->u.mapped.size);
    return NULL;
  }
  Elf_W(Shdr)* str_shdr = (Elf_W(Shdr) *) ((char *) ei->u.mapped.image + str_soff);

  if (str_shdr->sh_offset + str_shdr->sh_size > ei->u.mapped.size) {
    Debug (1, "string table outside of image? (%lu > %lu)\n",
           (unsigned long) (str_shdr->sh_offset + str_shdr->sh_size),
           (unsigned long) ei->u.mapped.size);
    return NULL;
  }

  Debug (16, "strtab=0x%lx\n", (long) str_shdr->sh_offset);
  return (char*) ((uintptr_t) ei->u.mapped.image + str_shdr->sh_offset);
}

static bool elf_w (lookup_symbol_mapped) (
    unw_addr_space_t as, unw_word_t ip, struct elf_image* ei, Elf_W(Addr) load_offset,
    char* buf, size_t buf_len, unw_word_t* offp) {

  Elf_W(Shdr)* shdr = elf_w (section_table) (ei);
  if (!shdr) {
    return false;
  }

  Elf_W(Ehdr)* ehdr = ei->u.mapped.image;
  int i;
  for (i = 0; i < ehdr->e_shnum; ++i) {
    switch (shdr->sh_type) {
      case SHT_SYMTAB:
      case SHT_DYNSYM:
      {
        Elf_W(Sym)* symtab = (Elf_W(Sym) *) ((char *) ei->u.mapped.image + shdr->sh_offset);
        Elf_W(Sym)* symtab_end = (Elf_W(Sym) *) ((char *) symtab + shdr->sh_size);

        char* strtab = elf_w (string_table) (ei, shdr->sh_link);
        if (!strtab) {
          continue;
        }

        Debug (16, "symtab=0x%lx[%d]\n", (long) shdr->sh_offset, shdr->sh_type);

        Elf_W(Sym)* sym;
        for (sym = symtab;
             sym < symtab_end;
             sym = (Elf_W(Sym) *) ((char *) sym + shdr->sh_entsize)) {
          if (ELF_W (ST_TYPE) (sym->st_info) == STT_FUNC && sym->st_shndx != SHN_UNDEF) {
            Elf_W(Addr) val;
            if (tdep_get_func_addr (as, sym->st_value, &val) < 0) {
              continue;
            }
            if (sym->st_shndx != SHN_ABS) {
              val += load_offset;
            }
            Debug (16, "0x%016lx info=0x%02x\n", (long) val, sym->st_info);
            if (ip >= val && (Elf_W(Addr)) (ip - val) < sym->st_size) {
              char* str_name = strtab + sym->st_name;
              if (str_name > (char*) ei->u.mapped.image + ei->u.mapped.size ||
                  str_name < strtab) {
                // Malformed elf symbol table.
                break;
              }

              // Make sure we don't try and read past the end of the image.
              uintptr_t max_size = (uintptr_t) str_name - (uintptr_t) ei->u.mapped.image;
              if (buf_len > max_size) {
                buf_len = max_size;
              }

              strncpy (buf, str_name, buf_len);
              // Ensure the string is nul terminated, it is assumed that
              // sizeof(buf) >= buf_len + 1.
              buf[buf_len] = '\0';
              if (buf[0] == '\0') {
                // Empty name, so keep checking the other symbol tables
                // for a possible match.
                break;
              }
              if (offp != NULL) {
                *offp = ip - val;
              }
              return true;
            }
          }
        }
        break;
      }

      default:
        break;
    }
    shdr = (Elf_W(Shdr) *) (((char *) shdr) + ehdr->e_shentsize);
  }
  return false;
}

static bool elf_w (get_load_offset_mapped) (
    struct elf_image *ei, unsigned long segbase, unsigned long mapoff, Elf_W(Addr)* load_offset) {
  Elf_W(Ehdr) *ehdr = ei->u.mapped.image;
  Elf_W(Phdr) *phdr = (Elf_W(Phdr) *) ((char *) ei->u.mapped.image + ehdr->e_phoff);

  int i;
  for (i = 0; i < ehdr->e_phnum; ++i) {
    if (phdr[i].p_type == PT_LOAD && phdr[i].p_offset == mapoff) {
      *load_offset = segbase - phdr[i].p_vaddr;
      return true;
    }
  }
  return false;
}

static Elf_W(Addr) elf_w (get_min_vaddr_mapped) (struct elf_image *ei) {
  Elf_W(Ehdr) *ehdr = ei->u.mapped.image;
  Elf_W(Phdr) *phdr = (Elf_W(Phdr) *) ((char *) ei->u.mapped.image + ehdr->e_phoff);
  Elf_W(Addr) min_vaddr = ~0u;
  int i;
  for (i = 0; i < ehdr->e_phnum; ++i) {
    if (phdr[i].p_type == PT_LOAD && phdr[i].p_vaddr < min_vaddr) {
      min_vaddr = phdr[i].p_vaddr;
    }
  }
  return min_vaddr;
}

// --------------------------------------------------------------------------

static inline bool elf_w (lookup_symbol) (
    unw_addr_space_t as, unw_word_t ip, struct elf_image *ei, Elf_W(Addr) load_offset,
    char *buf, size_t buf_len, unw_word_t* offp, Elf_W(Ehdr)* ehdr) {
  if (!ei->valid)
    return false;

  if (buf_len <= 1) {
    Debug (1, "lookup_symbol called with a buffer too small to hold a name %zu\n", buf_len);
    return false;
  }

  // Leave enough space for the nul terminator.
  buf_len--;

  if (ei->mapped) {
    return elf_w (lookup_symbol_mapped) (as, ip, ei, load_offset, buf, buf_len, offp);
  } else {
    return elf_w (lookup_symbol_memory) (as, ip, ei, load_offset, buf, buf_len, offp, ehdr);
  }
}

static bool elf_w (get_load_offset) (
    struct elf_image* ei, unsigned long segbase, unsigned long mapoff,
    Elf_W(Ehdr)* ehdr, Elf_W(Addr)* load_offset) {
  if (ei->mapped) {
    return elf_w (get_load_offset_mapped) (ei, segbase, mapoff, load_offset);
  } else {
    return elf_w (get_load_offset_memory) (ei, segbase, mapoff, ehdr, load_offset);
  }
}

/* ANDROID support update. */
static void* xz_alloc(void* p, size_t size) {
  return malloc(size);
}

static void xz_free(void* p, void* address) {
  free(address);
}

HIDDEN bool
elf_w (xz_decompress) (uint8_t* src, size_t src_size,
                       uint8_t** dst, size_t* dst_size) {
#if HAVE_LZMA
  size_t src_offset = 0;
  size_t dst_offset = 0;
  size_t src_remaining;
  size_t dst_remaining;
  ISzAlloc alloc;
  CXzUnpacker state;
  ECoderStatus status;
  alloc.Alloc = xz_alloc;
  alloc.Free = xz_free;
  XzUnpacker_Construct(&state, &alloc);
  CrcGenerateTable();
  Crc64GenerateTable();
  *dst_size = 2 * src_size;
  *dst = NULL;
  do {
    *dst_size *= 2;
    *dst = realloc(*dst, *dst_size);
    if (*dst == NULL) {
      Debug (1, "LZMA decompression failed due to failed realloc.\n");
      XzUnpacker_Free(&state);
      return false;
    }
    src_remaining = src_size - src_offset;
    dst_remaining = *dst_size - dst_offset;
    int res = XzUnpacker_Code(&state,
                              *dst + dst_offset, &dst_remaining,
                              src + src_offset, &src_remaining,
                              CODER_FINISH_ANY, &status);
    if (res != SZ_OK) {
      Debug (1, "LZMA decompression failed with error %d\n", res);
      free(*dst);
      XzUnpacker_Free(&state);
      return false;
    }
    src_offset += src_remaining;
    dst_offset += dst_remaining;
  } while (status == CODER_STATUS_NOT_FINISHED);
  XzUnpacker_Free(&state);
  if (!XzUnpacker_IsStreamWasFinished(&state)) {
    Debug (1, "LZMA decompression failed due to incomplete stream.\n");
    free(*dst);
    return false;
  }
  *dst_size = dst_offset;
  *dst = realloc(*dst, *dst_size);
  return true;
#else
  Debug (1, "Decompression failed - compiled without LZMA support.\n");
  return false;
#endif // HAVE_LZMA
}

HIDDEN bool
elf_w (find_section_mapped) (struct elf_image *ei, const char* name,
                             uint8_t** section, size_t* size, Elf_W(Addr)* vaddr) {
  Elf_W (Ehdr) *ehdr = ei->u.mapped.image;
  Elf_W (Shdr) *shdr;
  char *strtab;
  int i;

  if (!ei->valid || !ei->mapped) {
    return false;
  }

  shdr = elf_w (section_table) (ei);
  if (!shdr) {
    return false;
  }

  strtab = elf_w (string_table) (ei, ehdr->e_shstrndx);
  if (!strtab) {
    return false;
  }

  for (i = 0; i < ehdr->e_shnum; ++i) {
    if (strcmp (strtab + shdr->sh_name, name) == 0) {
      if (section != NULL && size != NULL) {
        if (shdr->sh_offset + shdr->sh_size > ei->u.mapped.size) {
          Debug (1, "section %s outside image? (0x%lu > 0x%lu)\n", name,
                 (unsigned long) (shdr->sh_offset + shdr->sh_size),
                 (unsigned long) ei->u.mapped.size);
          return false;
        }
        *section = ((uint8_t *) ei->u.mapped.image) + shdr->sh_offset;
        *size = shdr->sh_size;
      }
      if (vaddr != NULL) {
        *vaddr = shdr->sh_addr;
      }
      return true;
    }
    shdr = (Elf_W (Shdr) *) (((char *) shdr) + ehdr->e_shentsize);
  }
  return false;
}
/* ANDROID support update. */

// Find the ELF image that contains IP and return the procedure name from
// the symbol table that matches the IP.
HIDDEN bool elf_w (get_proc_name_in_image) (
    unw_addr_space_t as, struct elf_image* ei, unsigned long segbase, unsigned long mapoff,
    unw_word_t ip, char* buf, size_t buf_len, unw_word_t* offp) {
  Elf_W(Ehdr) ehdr;
  memset(&ehdr, 0, sizeof(ehdr));
  Elf_W(Addr) load_offset;
  if (!elf_w (get_load_offset) (ei, segbase, mapoff, &ehdr, &load_offset)) {
    return false;
  }

  if (elf_w (lookup_symbol) (as, ip, ei, load_offset, buf, buf_len, offp, &ehdr) != 0) {
    return true;
  }

  // If the ELF image doesn't contain a match, look up the symbol in
  // the MiniDebugInfo.
  if (ei->mapped && ei->mini_debug_info_data) {
    struct elf_image mdi;
    mdi.mapped = true;
    mdi.u.mapped.image = ei->mini_debug_info_data;
    mdi.u.mapped.size = ei->mini_debug_info_size;
    mdi.valid = elf_w (valid_object_mapped) (&mdi);
    // The ELF file might have been relocated after the debug
    // information has been compresses and embedded.
    ElfW(Addr) ei_text_address, mdi_text_address;
    if (elf_w (find_section_mapped) (ei, ".text", NULL, NULL, &ei_text_address) &&
        elf_w (find_section_mapped) (&mdi, ".text", NULL, NULL, &mdi_text_address)) {
      load_offset += ei_text_address - mdi_text_address;
    }
    bool ret_val = elf_w (lookup_symbol) (as, ip, &mdi, load_offset, buf, buf_len, offp, &ehdr);
    return ret_val;
  }
  return false;
}

HIDDEN bool elf_w (get_proc_name) (
    unw_addr_space_t as, pid_t pid, unw_word_t ip, char* buf, size_t buf_len,
    unw_word_t* offp, void* as_arg) {
  unsigned long segbase, mapoff;
  struct elf_image ei;

  if (tdep_get_elf_image(as, &ei, pid, ip, &segbase, &mapoff, NULL, as_arg) < 0) {
    return false;
  }

  return elf_w (get_proc_name_in_image) (as, &ei, segbase, mapoff, ip, buf, buf_len, offp);
}

HIDDEN bool elf_w (get_load_base) (struct elf_image* ei, unw_word_t mapoff, unw_word_t* load_base) {
  if (!ei->valid) {
    return false;
  }

  if (ei->mapped) {
    Elf_W(Ehdr)* ehdr = ei->u.mapped.image;
    Elf_W(Phdr)* phdr = (Elf_W(Phdr)*) ((char*) ei->u.mapped.image + ehdr->e_phoff);
    int i;
    for (i = 0; i < ehdr->e_phnum; ++i) {
      if (phdr[i].p_type == PT_LOAD && phdr[i].p_offset == mapoff) {
        *load_base = phdr[i].p_vaddr;
        return true;
      }
    }
    return false;
  } else {
    Elf_W(Ehdr) ehdr;
    GET_EHDR_FIELD(ei, &ehdr, e_phnum, false);
    GET_EHDR_FIELD(ei, &ehdr, e_phoff, false);
    int i;
    unw_word_t offset = ehdr.e_phoff;
    for (i = 0; i < ehdr.e_phnum; ++i) {
      Elf_W(Phdr) phdr;
      GET_PHDR_FIELD(ei, offset, &phdr, p_type);
      GET_PHDR_FIELD(ei, offset, &phdr, p_offset);
      // Always use zero as the map offset for in memory maps.
      // The dlopen of a shared library from an APK will result in a
      // non-zero map offset which would mean we would never find the
      // correct program header using the passed in map offset.
      if (phdr.p_type == PT_LOAD && phdr.p_offset == 0) {
        GET_PHDR_FIELD(ei, offset, &phdr, p_vaddr);
        *load_base = phdr.p_vaddr;
        return true;
      }
      offset += sizeof(phdr);
    }
    return false;
  }
  return false;
}