Imported Upstream version 1.1
[platform/upstream/libunwind.git] / src / dwarf / Gfind_unwind_table.c
1 /* libunwind - a platform-independent unwind library
2    Copyright (C) 2003-2004 Hewlett-Packard Co
3         Contributed by David Mosberger-Tang <davidm@hpl.hp.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 <elf.h>
27 #include <fcntl.h>
28 #include <string.h>
29 #include <unistd.h>
30
31 #include <sys/mman.h>
32
33 #include "libunwind_i.h"
34 #include "dwarf-eh.h"
35 #include "dwarf_i.h"
36
37 int
38 dwarf_find_unwind_table (struct elf_dyn_info *edi, unw_addr_space_t as,
39                          char *path, unw_word_t segbase, unw_word_t mapoff,
40                          unw_word_t ip)
41 {
42   Elf_W(Phdr) *phdr, *ptxt = NULL, *peh_hdr = NULL, *pdyn = NULL;
43   unw_word_t addr, eh_frame_start, fde_count, load_base;
44   unw_word_t max_load_addr = 0;
45   unw_word_t start_ip = (unw_word_t) -1;
46   unw_word_t end_ip = 0;
47   struct dwarf_eh_frame_hdr *hdr;
48   unw_proc_info_t pi;
49   unw_accessors_t *a;
50   Elf_W(Ehdr) *ehdr;
51 #if UNW_TARGET_ARM
52   const Elf_W(Phdr) *parm_exidx = NULL;
53 #endif
54   int i, ret, found = 0;
55
56   /* XXX: Much of this code is Linux/LSB-specific.  */
57
58   if (!elf_w(valid_object) (&edi->ei))
59     return -UNW_ENOINFO;
60
61   ehdr = edi->ei.image;
62   phdr = (Elf_W(Phdr) *) ((char *) edi->ei.image + ehdr->e_phoff);
63
64   for (i = 0; i < ehdr->e_phnum; ++i)
65     {
66       switch (phdr[i].p_type)
67         {
68         case PT_LOAD:
69           if (phdr[i].p_vaddr < start_ip)
70             start_ip = phdr[i].p_vaddr;
71
72           if (phdr[i].p_vaddr + phdr[i].p_memsz > end_ip)
73             end_ip = phdr[i].p_vaddr + phdr[i].p_memsz;
74
75           if (phdr[i].p_offset == mapoff)
76             ptxt = phdr + i;
77           if ((uintptr_t) edi->ei.image + phdr->p_filesz > max_load_addr)
78             max_load_addr = (uintptr_t) edi->ei.image + phdr->p_filesz;
79           break;
80
81         case PT_GNU_EH_FRAME:
82           peh_hdr = phdr + i;
83           break;
84
85         case PT_DYNAMIC:
86           pdyn = phdr + i;
87           break;
88
89 #if UNW_TARGET_ARM
90         case PT_ARM_EXIDX:
91           parm_exidx = phdr + i;
92           break;
93 #endif
94
95         default:
96           break;
97         }
98     }
99
100   if (!ptxt)
101     return 0;
102
103   load_base = segbase - ptxt->p_vaddr;
104   start_ip += load_base;
105   end_ip += load_base;
106
107   if (peh_hdr)
108     {
109       if (pdyn)
110         {
111           /* For dynamicly linked executables and shared libraries,
112              DT_PLTGOT is the value that data-relative addresses are
113              relative to for that object.  We call this the "gp".  */
114                 Elf_W(Dyn) *dyn = (Elf_W(Dyn) *)(pdyn->p_offset
115                                                  + (char *) edi->ei.image);
116           for (; dyn->d_tag != DT_NULL; ++dyn)
117             if (dyn->d_tag == DT_PLTGOT)
118               {
119                 /* Assume that _DYNAMIC is writable and GLIBC has
120                    relocated it (true for x86 at least).  */
121                 edi->di_cache.gp = dyn->d_un.d_ptr;
122                 break;
123               }
124         }
125       else
126         /* Otherwise this is a static executable with no _DYNAMIC.  Assume
127            that data-relative addresses are relative to 0, i.e.,
128            absolute.  */
129         edi->di_cache.gp = 0;
130
131       hdr = (struct dwarf_eh_frame_hdr *) (peh_hdr->p_offset
132                                            + (char *) edi->ei.image);
133       if (hdr->version != DW_EH_VERSION)
134         {
135           Debug (1, "table `%s' has unexpected version %d\n",
136                  path, hdr->version);
137           return -UNW_ENOINFO;
138         }
139
140       a = unw_get_accessors (unw_local_addr_space);
141       addr = (unw_word_t) (hdr + 1);
142
143       /* Fill in a dummy proc_info structure.  We just need to fill in
144          enough to ensure that dwarf_read_encoded_pointer() can do it's
145          job.  Since we don't have a procedure-context at this point, all
146          we have to do is fill in the global-pointer.  */
147       memset (&pi, 0, sizeof (pi));
148       pi.gp = edi->di_cache.gp;
149
150       /* (Optionally) read eh_frame_ptr: */
151       if ((ret = dwarf_read_encoded_pointer (unw_local_addr_space, a,
152                                              &addr, hdr->eh_frame_ptr_enc, &pi,
153                                              &eh_frame_start, NULL)) < 0)
154         return -UNW_ENOINFO;
155
156       /* (Optionally) read fde_count: */
157       if ((ret = dwarf_read_encoded_pointer (unw_local_addr_space, a,
158                                              &addr, hdr->fde_count_enc, &pi,
159                                              &fde_count, NULL)) < 0)
160         return -UNW_ENOINFO;
161
162       if (hdr->table_enc != (DW_EH_PE_datarel | DW_EH_PE_sdata4))
163         {
164     #if 1
165           abort ();
166     #else
167           unw_word_t eh_frame_end;
168
169           /* If there is no search table or it has an unsupported
170              encoding, fall back on linear search.  */
171           if (hdr->table_enc == DW_EH_PE_omit)
172             Debug (4, "EH lacks search table; doing linear search\n");
173           else
174             Debug (4, "EH table has encoding 0x%x; doing linear search\n",
175                    hdr->table_enc);
176
177           eh_frame_end = max_load_addr; /* XXX can we do better? */
178
179           if (hdr->fde_count_enc == DW_EH_PE_omit)
180             fde_count = ~0UL;
181           if (hdr->eh_frame_ptr_enc == DW_EH_PE_omit)
182             abort ();
183
184           return linear_search (unw_local_addr_space, ip,
185                                 eh_frame_start, eh_frame_end, fde_count,
186                                 pi, need_unwind_info, NULL);
187     #endif
188         }
189
190       edi->di_cache.start_ip = start_ip;
191       edi->di_cache.end_ip = end_ip;
192       edi->di_cache.format = UNW_INFO_FORMAT_REMOTE_TABLE;
193       edi->di_cache.u.rti.name_ptr = 0;
194       /* two 32-bit values (ip_offset/fde_offset) per table-entry: */
195       edi->di_cache.u.rti.table_len = (fde_count * 8) / sizeof (unw_word_t);
196       edi->di_cache.u.rti.table_data = ((load_base + peh_hdr->p_vaddr)
197                                        + (addr - (unw_word_t) edi->ei.image
198                                           - peh_hdr->p_offset));
199
200       /* For the binary-search table in the eh_frame_hdr, data-relative
201          means relative to the start of that section... */
202       edi->di_cache.u.rti.segbase = ((load_base + peh_hdr->p_vaddr)
203                                     + ((unw_word_t) hdr - (unw_word_t) edi->ei.image
204                                        - peh_hdr->p_offset));
205       found = 1;
206     }
207
208 #if UNW_TARGET_ARM
209   if (parm_exidx)
210     {
211       edi->di_arm.format = UNW_INFO_FORMAT_ARM_EXIDX;
212       edi->di_arm.start_ip = start_ip;
213       edi->di_arm.end_ip = end_ip;
214       edi->di_arm.u.rti.name_ptr = (unw_word_t) path;
215       edi->di_arm.u.rti.table_data = load_base + parm_exidx->p_vaddr;
216       edi->di_arm.u.rti.table_len = parm_exidx->p_memsz;
217       found = 1;
218     }
219 #endif
220
221 #ifdef CONFIG_DEBUG_FRAME
222   /* Try .debug_frame. */
223   found = dwarf_find_debug_frame (found, &edi->di_debug, ip, load_base, path,
224                                   start_ip, end_ip);
225 #endif
226
227   return found;
228 }