Remove dead code from x86-32 SSSE3 strncmp.
[glibc.git] / sysdeps / generic / elf / backtracesyms.c
blob319b2076050222f4a8c8b85d816dc06817948c9d
1 /* Return list with names for address in backtrace.
2 Copyright (C) 1998,1999,2000,2001,2003,2009 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <assert.h>
22 #include <execinfo.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
27 #include <ldsodefs.h>
29 #if __ELF_NATIVE_CLASS == 32
30 # define WORD_WIDTH 8
31 #else
32 /* We assyme 64bits. */
33 # define WORD_WIDTH 16
34 #endif
37 char **
38 __backtrace_symbols (array, size)
39 void *const *array;
40 int size;
42 Dl_info info[size];
43 int status[size];
44 int cnt;
45 size_t total = 0;
46 char **result;
48 /* Fill in the information we can get from `dladdr'. */
49 for (cnt = 0; cnt < size; ++cnt)
51 struct link_map *map;
52 status[cnt] = _dl_addr (array[cnt], &info[cnt], &map, NULL);
53 if (status[cnt] && info[cnt].dli_fname && info[cnt].dli_fname[0] != '\0')
55 /* We have some info, compute the length of the string which will be
56 "<file-name>(<sym-name>+offset) [address]. */
57 total += (strlen (info[cnt].dli_fname ?: "")
58 + strlen (info[cnt].dli_sname ?: "")
59 + 3 + WORD_WIDTH + 3 + WORD_WIDTH + 5);
61 /* The load bias is more useful to the user than the load
62 address. The use of these addresses is to calculate an
63 address in the ELF file, so its prelinked bias is not
64 something we want to subtract out. */
65 info[cnt].dli_fbase = (void *) map->l_addr;
67 else
68 total += 5 + WORD_WIDTH;
71 /* Allocate memory for the result. */
72 result = (char **) malloc (size * sizeof (char *) + total);
73 if (result != NULL)
75 char *last = (char *) (result + size);
77 for (cnt = 0; cnt < size; ++cnt)
79 result[cnt] = last;
81 if (status[cnt]
82 && info[cnt].dli_fname != NULL && info[cnt].dli_fname[0] != '\0')
84 if (info[cnt].dli_sname == NULL)
85 /* We found no symbol name to use, so describe it as
86 relative to the file. */
87 info[cnt].dli_saddr = info[cnt].dli_fbase;
89 if (info[cnt].dli_sname == NULL && info[cnt].dli_saddr == 0)
90 last += 1 + sprintf (last, "%s(%s) [%p]",
91 info[cnt].dli_fname ?: "",
92 info[cnt].dli_sname ?: "",
93 array[cnt]);
94 else
96 char sign;
97 ptrdiff_t offset;
98 if (array[cnt] >= (void *) info[cnt].dli_saddr)
100 sign = '+';
101 offset = array[cnt] - info[cnt].dli_saddr;
103 else
105 sign = '-';
106 offset = info[cnt].dli_saddr - array[cnt];
109 last += 1 + sprintf (last, "%s(%s%c%#tx) [%p]",
110 info[cnt].dli_fname ?: "",
111 info[cnt].dli_sname ?: "",
112 sign, offset, array[cnt]);
115 else
116 last += 1 + sprintf (last, "[%p]", array[cnt]);
118 assert (last <= (char *) result + size * sizeof (char *) + total);
121 return result;
123 weak_alias (__backtrace_symbols, backtrace_symbols)