1/* libunwind - a platform-independent unwind library
2
3This file is part of libunwind.
4
5Permission is hereby granted, free of charge, to any person obtaining
6a copy of this software and associated documentation files (the
7"Software"), to deal in the Software without restriction, including
8without limitation the rights to use, copy, modify, merge, publish,
9distribute, sublicense, and/or sell copies of the Software, and to
10permit persons to whom the Software is furnished to do so, subject to
11the following conditions:
12
13The above copyright notice and this permission notice shall be
14included in all copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
23
24#include <inttypes.h>
25#include <string.h>
26#include <stdlib.h>
27
28#include "libunwind_i.h"
29
30HIDDEN int
31unwi_print_error (const char *string)
32{
33  return write (2, string, strlen (string));
34}
35
36HIDDEN void
37unwi_invalidate_edi (struct elf_dyn_info *edi)
38{
39  if (edi->ei.image)
40    munmap (edi->ei.image, edi->ei.size);
41  memset (edi, 0, sizeof (*edi));
42  edi->di_cache.format = -1;
43  edi->di_debug.format = -1;
44#if UNW_TARGET_ARM
45  edi->di_arm.format = -1;
46#endif
47}
48
49HIDDEN void
50unwi_invalidate_as_edi (struct as_elf_dyn_info *edi)
51{
52  free(edi->ehdr.data);
53  free(edi->phdr.data);
54  free(edi->eh.data);
55  free(edi->dyn.data);
56
57  memset (edi, 0, sizeof (*edi));
58  edi->di_cache.format = -1;
59  edi->di_debug.format = -1;
60}
61
62HIDDEN int
63unwi_load_as_contents (unw_addr_space_t as, struct as_contents *contents,
64                       unw_word_t offset, size_t size, void *arg)
65{
66  int ret;
67  unw_accessors_t *a = unw_get_accessors (as);
68
69  Debug(3, "(%p, %p, 0x%" PRIxPTR ", 0x%zx)\n", as, contents, offset, size);
70
71  contents->data = malloc (size);
72  if (contents->data == NULL)
73    {
74      Debug(3, "returning, OOM\n");
75      return -UNW_ENOMEM;
76    }
77  ret = (*a->access_raw_mem) (as, offset, contents->data, size, 0, arg);
78  if (ret < 0)
79    {
80        Debug(3, "returning, access_raw_mem failed: %d\n", ret);
81      return -UNW_ENOINFO;
82    }
83  contents->size = size;
84  Debug(3, "returning 0\n");
85  return 0;
86}
87