1 /* libunwind - a platform-independent unwind library
2 Copyright (C) 2013 Garmin International
3 Contributed by Matt Fischer <matt.fischer@garmin.com>
4
5 This file is part of libunwind.
6
7 Permission is hereby granted, free of charge, to any person obtaining
8 a copy of this software and associated documentation files (the
9 "Software"), to deal in the Software without restriction, including
10 without limitation the rights to use, copy, modify, merge, publish,
11 distribute, sublicense, and/or sell copies of the Software, and to
12 permit persons to whom the Software is furnished to do so, subject to
13 the following conditions:
14
15 The above copyright notice and this permission notice shall be
16 included in all copies or substantial portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
25
26 #include <string.h>
27
28 #include "libunwind_i.h"
29
30 struct cb_info
31 {
32 unw_word_t ip;
33 unsigned long segbase;
34 unsigned long offset;
35 const char *path;
36 };
37
callback(const struct dl_phdr_info * info,size_t size,void * data)38 static int callback(const struct dl_phdr_info *info, size_t size, void *data)
39 {
40 int i;
41 struct cb_info *cbi = (struct cb_info*)data;
42 for(i=0; i<info->dlpi_phnum; i++) {
43 int segbase = info->dlpi_addr + info->dlpi_phdr[i].p_vaddr;
44 if(cbi->ip >= segbase && cbi->ip < segbase + info->dlpi_phdr[i].p_memsz)
45 {
46 cbi->path = info->dlpi_name;
47 cbi->offset = info->dlpi_phdr[i].p_offset;
48 cbi->segbase = segbase;
49 return 1;
50 }
51 }
52
53 return 0;
54 }
55
56 int
tdep_get_elf_image(struct elf_image * ei,pid_t pid,unw_word_t ip,unsigned long * segbase,unsigned long * mapoff,char * path,size_t pathlen)57 tdep_get_elf_image (struct elf_image *ei, pid_t pid, unw_word_t ip,
58 unsigned long *segbase, unsigned long *mapoff,
59 char *path, size_t pathlen)
60 {
61 struct cb_info cbi;
62 int ret = -1;
63 cbi.ip = ip;
64 cbi.segbase = 0;
65 cbi.offset = 0;
66 cbi.path = NULL;
67
68 /* QNX's support for accessing symbol maps is severely broken. There is
69 a devctl() call that can be made on a proc node (DCMD_PROC_MAPDEBUG)
70 which returns information similar to Linux's /proc/<pid>/maps
71 node, however the filename that is returned by this call is not an
72 absolute path, and there is no foolproof way to map the filename
73 back to the file that it came from.
74
75 Therefore, the normal approach for implementing this function,
76 which works equally well for both local and remote unwinding,
77 will not work here. The only type of image lookup which works
78 reliably is locally, using dl_iterate_phdr(). However, the only
79 time that this function is required to look up a remote image is for
80 ptrace support, which doesn't work on QNX anyway. Local unwinding,
81 which is the main case that makes use of this function, will work
82 fine with dl_iterate_phdr(). Therefore, in lieu of any better
83 platform support for remote image lookup, this function has just
84 been implemented in terms of dl_iterate_phdr().
85 */
86
87 if (pid != getpid())
88 {
89 /* Return an error if an attempt is made to perform remote image lookup */
90 return -1;
91 }
92
93 if (dl_iterate_phdr (callback, &cbi) != 0)
94 {
95 if (path)
96 {
97 strncpy (path, cbi.path, pathlen);
98 }
99
100 *mapoff = cbi.offset;
101 *segbase = cbi.segbase;
102
103 ret = elf_map_image (ei, cbi.path);
104 }
105
106 return ret;
107 }
108
109 #ifndef UNW_REMOTE_ONLY
110
111 void
tdep_get_exe_image_path(char * path)112 tdep_get_exe_image_path (char *path)
113 {
114 path[0] = 0; /* XXX */
115 }
116
117 #endif
118